1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
/// _ _
/// __ ____ _| |_ _ __(_)_ __
/// \ \/ / _` | __| '__| \ \/ /
/// > < (_| | |_| | | |> <
/// /_/\_\__,_|\__|_| |_/_/\_\
///
/// Copyright (c) 1997 - Ognjen 'xolatile' Milan Robovic
///
/// xolatile@chud.cyou - xatrix - Very dumb and slow matrix library, mathematical matrix, because I don't like other people ideas...
///
/// This program is free software, free as in freedom and as in free beer, you can redistribute it and/or modify it under the terms of the GNU
/// General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version if you wish...
///
/// This program is distributed in the hope that it will be useful, but it is probably not, and without any warranty, without even the implied
/// warranty of merchantability or fitness for a particular purpose, because it is pointless. Please see the GNU (Geenoo) General Public License
/// for more details, if you dare, it is a lot of text that nobody wants to read...
typedef real matrix_2 [2] [2];
typedef real matrix_3 [3] [3];
typedef real matrix_4 [4] [4];
static matrix_2 * matrix_2_assign (matrix_2 * destination,
real m00, real m01,
real m10, real m11) {
destination [0] [0] = m00;
destination [0] [1] = m01;
destination [1] [0] = m10;
destination [1] [1] = m11;
return (destination);
}
static matrix_3 * matrix_3_assign (matrix_3 * destination,
real m00, real m01, real m02,
real m10, real m11, real m12,
real m20, real m21, real m22) {
destination [0] [0] = m00;
destination [0] [1] = m01;
destination [0] [2] = m02;
destination [1] [0] = m10;
destination [1] [1] = m11;
destination [1] [2] = m12;
destination [2] [0] = m20;
destination [2] [1] = m21;
destination [2] [2] = m22;
return (destination);
}
static matrix_4 * matrix_4_assign (matrix_4 * destination,
real m00, real m01, real m02, real m03,
real m10, real m11, real m12, real m13,
real m20, real m21, real m22, real m23,
real m30, real m31, real m32, real m33) {
destination [0] [0] = m00;
destination [0] [1] = m01;
destination [0] [2] = m02;
destination [0] [3] = m03;
destination [1] [0] = m10;
destination [1] [1] = m11;
destination [1] [2] = m12;
destination [1] [3] = m13;
destination [2] [0] = m20;
destination [2] [1] = m21;
destination [2] [2] = m22;
destination [2] [3] = m23;
destination [3] [0] = m30;
destination [3] [1] = m31;
destination [3] [2] = m32;
destination [3] [3] = m33;
return (destination);
}
static matrix_2 * matrix_2_nullify (matrix_2 * destination) {
for (natural row = 0; row < 2; ++row) {
for (natural column = 0; column < 2; ++column) {
destination [row] [column] = 0.0f;
}
}
return (destination);
}
static matrix_3 * matrix_3_nullify (matrix_3 * destination) {
for (natural row = 0; row < 3; ++row) {
for (natural column = 0; column < 3; ++column) {
destination [row] [column] = 0.0f;
}
}
return (destination);
}
static matrix_4 * matrix_4_nullify (matrix_4 * destination) {
for (natural row = 0; row < 4; ++row) {
for (natural column = 0; column < 4; ++column) {
destination [row] [column] = 0.0f;
}
}
return (destination);
}
static matrix_2 * matrix_2_identity (matrix_2 * destination) {
destination = matrix_2_nullify (destination);
for (natural index = 0; index < 2; ++index) {
destination [index] [index] = 1.0f;
}
return (destination);
}
static matrix_3 * matrix_3_identity (matrix_3 * destination) {
destination = matrix_3_nullify (destination);
for (natural index = 0; index < 3; ++index) {
destination [index] [index] = 1.0f;
}
return (destination);
}
static matrix_4 * matrix_4_identity (matrix_4 * destination) {
destination = matrix_4_nullify (destination);
for (natural index = 0; index < 4; ++index) {
destination [index] [index] = 1.0f;
}
return (destination);
}
static real matrix_2_determinant (matrix_2 * matrix) {
real a = matrix [0] [0] * matrix [1] [1];
real b = matrix [0] [1] * matrix [1] [0];
return (a - b);
}
static real matrix_3_determinant (matrix_3 * matrix) {
matrix_2 matrix_a = { { matrix [1] [1], matrix [1] [2] },
{ matrix [2] [1], matrix [2] [2] } };
matrix_2 matrix_b = { { matrix [1] [0], matrix [1] [2] },
{ matrix [2] [0], matrix [2] [2] } };
matrix_2 matrix_c = { { matrix [1] [0], matrix [1] [1] },
{ matrix [2] [0], matrix [2] [1] } };
real a = matrix [0] [0] * matrix_2_determinant (& matrix_a);
real b = matrix [0] [1] * matrix_2_determinant (& matrix_b);
real c = matrix [0] [2] * matrix_2_determinant (& matrix_c);
return (a - b + c);
}
static real matrix_4_determinant (matrix_4 * matrix) {
matrix_3 matrix_a = { { matrix [1] [1], matrix [1] [2], matrix [1] [3] },
{ matrix [2] [1], matrix [2] [2], matrix [2] [3] },
{ matrix [3] [1], matrix [3] [2], matrix [3] [3] } };
matrix_3 matrix_b = { { matrix [1] [0], matrix [1] [2], matrix [1] [3] },
{ matrix [2] [0], matrix [2] [2], matrix [2] [3] },
{ matrix [3] [0], matrix [3] [2], matrix [3] [3] } };
matrix_3 matrix_c = { { matrix [1] [0], matrix [1] [1], matrix [1] [3] },
{ matrix [2] [0], matrix [2] [1], matrix [2] [3] },
{ matrix [3] [0], matrix [3] [1], matrix [3] [3] } };
matrix_3 matrix_d = { { matrix [1] [0], matrix [1] [1], matrix [1] [2] },
{ matrix [2] [0], matrix [2] [1], matrix [2] [2] },
{ matrix [3] [0], matrix [3] [1], matrix [3] [2] } };
real a = matrix [0] [0] * matrix_3_determinant (& matrix_a);
real b = matrix [0] [1] * matrix_3_determinant (& matrix_b);
real c = matrix [0] [2] * matrix_3_determinant (& matrix_c);
real d = matrix [0] [3] * matrix_3_determinant (& matrix_d);
return (a - b + c - d);
}
static matrix_2 * matrix_2_copy (matrix_2 * destination, matrix_2 * source) {
for (natural row = 0; row < 2; ++row) {
for (natural column = 0; column < 2; ++column) {
destination [row] [column] = source [row] [column];
}
}
return (destination);
}
static matrix_3 * matrix_3_copy (matrix_3 * destination, matrix_3 * source) {
for (natural row = 0; row < 3; ++row) {
for (natural column = 0; column < 3; ++column) {
destination [row] [column] = source [row] [column];
}
}
return (destination);
}
static matrix_4 * matrix_4_copy (matrix_4 * destination, matrix_4 * source) {
for (natural row = 0; row < 4; ++row) {
for (natural column = 0; column < 4; ++column) {
destination [row] [column] = source [row] [column];
}
}
return (destination);
}
static matrix_2 * matrix_2_scale (matrix_2 * destination, real scale) {
for (natural row = 0; row < 2; ++row) {
for (natural column = 0; column < 2; ++column) {
destination [row] [column] *= scale;
}
}
return (destination);
}
static matrix_3 * matrix_3_scale (matrix_3 * destination, real scale) {
for (natural row = 0; row < 3; ++row) {
for (natural column = 0; column < 3; ++column) {
destination [row] [column] *= scale;
}
}
return (destination);
}
static matrix_4 * matrix_4_scale (matrix_4 * destination, real scale) {
for (natural row = 0; row < 4; ++row) {
for (natural column = 0; column < 4; ++column) {
destination [row] [column] *= scale;
}
}
return (destination);
}
static matrix_2 * matrix_2_scale_to (matrix_2 * destination, matrix_2 * source, real scale) {
for (natural row = 0; row < 2; ++row) {
for (natural column = 0; column < 2; ++column) {
destination [row] [column] = source [row] [column] * scale;
}
}
return (destination);
}
static matrix_3 * matrix_3_scale_to (matrix_3 * destination, matrix_3 * source, real scale) {
for (natural row = 0; row < 3; ++row) {
for (natural column = 0; column < 3; ++column) {
destination [row] [column] = source [row] [column] * scale;
}
}
return (destination);
}
static matrix_4 * matrix_4_scale_to (matrix_4 * destination, matrix_4 * source, real scale) {
for (natural row = 0; row < 4; ++row) {
for (natural column = 0; column < 4; ++column) {
destination [row] [column] = source [row] [column] * scale;
}
}
return (destination);
}
static matrix_2 * matrix_2_add (matrix_2 * destination, matrix_2 * source) {
for (natural row = 0; row < 2; ++row) {
for (natural column = 0; column < 2; ++column) {
destination [row] [column] += source [row] [column];
}
}
return (destination);
}
static matrix_3 * matrix_3_add (matrix_3 * destination, matrix_3 * source) {
for (natural row = 0; row < 3; ++row) {
for (natural column = 0; column < 3; ++column) {
destination [row] [column] += source [row] [column];
}
}
return (destination);
}
static matrix_4 * matrix_4_add (matrix_4 * destination, matrix_4 * source) {
for (natural row = 0; row < 4; ++row) {
for (natural column = 0; column < 4; ++column) {
destination [row] [column] += source [row] [column];
}
}
return (destination);
}
static matrix_2 * matrix_2_add_to (matrix_2 * destination, matrix_2 * matrix_a, matrix_2 * matrix_b) {
for (natural row = 0; row < 2; ++row) {
for (natural column = 0; column < 2; ++column) {
destination [row] [column] = matrix_a [row] [column] + matrix_b [row] [column];
}
}
return (destination);
}
static matrix_3 * matrix_3_add_to (matrix_3 * destination, matrix_3 * matrix_a, matrix_3 * matrix_b) {
for (natural row = 0; row < 3; ++row) {
for (natural column = 0; column < 3; ++column) {
destination [row] [column] = matrix_a [row] [column] + matrix_b [row] [column];
}
}
return (destination);
}
static matrix_4 * matrix_4_add_to (matrix_4 * destination, matrix_4 * matrix_a, matrix_4 * matrix_b) {
for (natural row = 0; row < 4; ++row) {
for (natural column = 0; column < 4; ++column) {
destination [row] [column] = matrix_a [row] [column] + matrix_b [row] [column];
}
}
return (destination);
}
static matrix_2 * matrix_2_subtract (matrix_2 * destination, matrix_2 * source) {
for (natural row = 0; row < 2; ++row) {
for (natural column = 0; column < 2; ++column) {
destination [row] [column] -= source [row] [column];
}
}
return (destination);
}
static matrix_3 * matrix_3_subtract (matrix_3 * destination, matrix_3 * source) {
for (natural row = 0; row < 3; ++row) {
for (natural column = 0; column < 3; ++column) {
destination [row] [column] -= source [row] [column];
}
}
return (destination);
}
static matrix_4 * matrix_4_subtract (matrix_4 * destination, matrix_4 * source) {
for (natural row = 0; row < 4; ++row) {
for (natural column = 0; column < 4; ++column) {
destination [row] [column] -= source [row] [column];
}
}
return (destination);
}
static matrix_2 * matrix_2_subtract_to (matrix_2 * destination, matrix_2 * matrix_a, matrix_2 * matrix_b) {
for (natural row = 0; row < 2; ++row) {
for (natural column = 0; column < 2; ++column) {
destination [row] [column] = matrix_a [row] [column] - matrix_b [row] [column];
}
}
return (destination);
}
static matrix_3 * matrix_3_subtract_to (matrix_3 * destination, matrix_3 * matrix_a, matrix_3 * matrix_b) {
for (natural row = 0; row < 3; ++row) {
for (natural column = 0; column < 3; ++column) {
destination [row] [column] = matrix_a [row] [column] - matrix_b [row] [column];
}
}
return (destination);
}
static matrix_4 * matrix_4_subtract_to (matrix_4 * destination, matrix_4 * matrix_a, matrix_4 * matrix_b) {
for (natural row = 0; row < 4; ++row) {
for (natural column = 0; column < 4; ++column) {
destination [row] [column] = matrix_a [row] [column] - matrix_b [row] [column];
}
}
return (destination);
}
static matrix_2 * matrix_2_multiply (matrix_2 * result, matrix_2 * matrix_a, matrix_2 * matrix_b) {
for (natural row = 0; row < 2; ++row) {
for (natural column = 0; column < 2; ++column) {
result [row] [column] = 0.0f;
for (natural index = 0; index < 2; ++index) {
result [row] [column] += matrix_a [row] [index] * matrix_b [index] [column];
}
}
}
return (result);
}
static matrix_3 * matrix_3_multiply (matrix_3 * result, matrix_3 * matrix_a, matrix_3 * matrix_b) {
for (natural row = 0; row < 3; ++row) {
for (natural column = 0; column < 3; ++column) {
result [row] [column] = 0.0f;
for (natural index = 0; index < 3; ++index) {
result [row] [column] += matrix_a [row] [index] * matrix_b [index] [column];
}
}
}
return (result);
}
static matrix_4 * matrix_4_multiply (matrix_4 * result, matrix_4 * matrix_a, matrix_4 * matrix_b) {
for (natural row = 0; row < 4; ++row) {
for (natural column = 0; column < 4; ++column) {
result [row] [column] = 0.0f;
for (natural index = 0; index < 4; ++index) {
result [row] [column] += matrix_a [row] [index] * matrix_b [index] [column];
}
}
}
return (result);
}
static real matrix_2_trace (matrix_2 * matrix) {
return (matrix [0] [0] + matrix [1] [1]);
}
static real matrix_3_trace (matrix_3 * matrix) {
return (matrix [0] [0] + matrix [1] [1] + matrix [2] [2]);
}
static real matrix_4_trace (matrix_4 * matrix) {
return (matrix [0] [0] + matrix [1] [1] + matrix [2] [2] + matrix [3] [3]);
}
static boolean matrix_2_compare (matrix_2 * matrix_a, matrix_2 * matrix_b) {
for (natural row = 0; row < 2; ++row) {
for (natural column = 0; column < 2; ++column) {
if (matrix_a [row] [column] != matrix_b [row] [column]) {
return (false);
}
}
}
return (true);
}
static boolean matrix_3_compare (matrix_3 * matrix_a, matrix_3 * matrix_b) {
for (natural row = 0; row < 3; ++row) {
for (natural column = 0; column < 3; ++column) {
if (matrix_a [row] [column] != matrix_b [row] [column]) {
return (false);
}
}
}
return (true);
}
static boolean matrix_4_compare (matrix_4 * matrix_a, matrix_4 * matrix_b) {
for (natural row = 0; row < 4; ++row) {
for (natural column = 0; column < 4; ++column) {
if (matrix_a [row] [column] != matrix_b [row] [column]) {
return (false);
}
}
}
return (true);
}
static procedure matrix_2_transpose (matrix_2 * matrix_a, matrix_2 * matrix_b) {
for (natural row = 0; row < 2; ++row) {
for (natural column = row + 1; column < 2; ++column) {
real temporary = matrix_a [row] [column];
matrix_a [row] [column] = matrix_b [column] [row];
matrix_b [column] [row] = temporary;
}
}
}
static procedure matrix_3_transpose (matrix_3 * matrix_a, matrix_3 * matrix_b) {
for (natural row = 0; row < 3; ++row) {
for (natural column = 0; column < 3; ++column) {
real temporary = matrix_a [row] [column];
matrix_a [row] [column] = matrix_b [column] [row];
matrix_b [column] [row] = temporary;
}
}
}
static procedure matrix_4_transpose (matrix_4 * matrix_a, matrix_4 * matrix_b) {
for (natural row = 0; row < 4; ++row) {
for (natural column = 0; column < 4; ++column) {
real temporary = matrix_a [row] [column];
matrix_a [row] [column] = matrix_b [column] [row];
matrix_b [column] [row] = temporary;
}
}
}
|