forked from Matway/mpl-sl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algebra.mpl
executable file
·587 lines (526 loc) · 12.4 KB
/
algebra.mpl
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
"algebra" module
"control" useModule
PI: [3.14159265358979323r32];
Vector: [array];
Matrix: [rowCount:; Vector rowCount Vector];
Natx storageSize 8nx = [
{arg: Real32;} Real32 {convention: cdecl;} "acosf" importFunction
{arg: Real64;} Real64 {convention: cdecl;} "acosl" importFunction
acos: [Real32 same] [acosf] pfunc;
acos: [Real64 same] [acosl] pfunc;
{arg: Real32;} Real32 {convention: cdecl;} "asinf" importFunction
{arg: Real64;} Real64 {convention: cdecl;} "asinl" importFunction
asin: [Real32 same] [asinf] pfunc;
asin: [Real64 same] [asinl] pfunc;
{arg: Real32;} Real32 {convention: cdecl;} "tanf" importFunction
{arg: Real64;} Real64 {convention: cdecl;} "tanl" importFunction
tan: [Real32 same] [tanf] pfunc;
tan: [Real64 same] [tanl] pfunc;
{arg: Real32;} Real32 {convention: cdecl;} "atanf" importFunction
{arg: Real64;} Real64 {convention: cdecl;} "atanl" importFunction
atan: [Real32 same] [atanf] pfunc;
atan: [Real64 same] [atanl] pfunc;
{argy: Real32; argx: Real32;} Real32 {convention: cdecl;} "atan2f" importFunction
{argy: Real64; argx: Real64;} Real64 {convention: cdecl;} "atan2l" importFunction
atan2: [Real32 same] [atan2f] pfunc;
atan2: [Real64 same] [atan2l] pfunc;
] [
{arg: Real64;} Real64 {convention: cdecl;} "acos" importFunction
acosFunc: @acos;
acos: [Real32 same] [Real64 cast acosFunc Real32 cast] pfunc;
acos: [Real64 same] [acosFunc] pfunc;
{arg: Real64;} Real64 {convention: cdecl;} "asin" importFunction
asinFunc: @asin;
asin: [Real32 same] [Real64 cast asinFunc Real32 cast] pfunc;
asin: [Real64 same] [asinFunc] pfunc;
{arg: Real64;} Real64 {convention: cdecl;} "tan" importFunction
tanFunc: @tan;
tan: [Real32 same] [Real64 cast tanFunc Real32 cast] pfunc;
tan: [Real64 same] [tanFunc] pfunc;
{arg: Real64;} Real64 {convention: cdecl;} "atan" importFunction
atanFunc: @atan;
atan: [Real32 same] [Real64 cast atanFunc Real32 cast] pfunc;
atan: [Real64 same] [atanFunc] pfunc;
{argy: Real64; argx: Real64;} Real64 {convention: cdecl;} "atan2" importFunction
atan2Func: @atan2;
atan2: [
x:y:;;
x Real32 same
y Real32 same and
] [
y: copy;
x: copy;
x Real64 cast y Real64 cast atan2Func Real32 cast
] pfunc;
atan2: [Real64 same] [atan2Func] pfunc;
] uif
vector?: [v:; FALSE];
vector?: [v:; v 0 fieldName textSize 0nx =] [v:; TRUE] pfunc;
matrix?: [m:; FALSE];
matrix?: [
m:;
colCount: 0 @m @ fieldCount;
@m vector?
@m fieldCount [
v: i @m @;
@v vector?
@v fieldCount colCount = and and
] times
] [m:; TRUE] pfunc;
getColCount: [matrix?] [m:; 0 m @ fieldCount] pfunc;
getRowCount: [matrix?] [fieldCount] pfunc;
angle: [
v:;
v vector?
v fieldCount 2 = and
] [
v:;
0 v @ 1 v @ atan2
] pfunc;
cosSin: [
angle:;
(angle cos angle sin)
];
-: [
v1:v2:;;
v1 vector?
v2 vector? and
v1 fieldCount v2 fieldCount = and
] [
v1:v2:;;
(v1 fieldCount [i v1 @ i v2 @ -] times)
] pfunc;
+: [
v1:v2:;;
v1 vector?
v2 vector? and
v1 fieldCount v2 fieldCount = and
] [
v1:v2:;;
(v1 fieldCount [i v1 @ i v2 @ +] times)
] pfunc;
=: [
v1:v2:;;
v1 vector?
v2 vector? and
v1 fieldCount v2 fieldCount = and
] [
v1:v2:;;
result: TRUE;
v1 fieldCount [
i v1 @ i v2 @ = result and !result
] times
result
] pfunc;
=: [
v1:m2:;;
v1 vector?
v1 matrix? not and
m2 matrix? and
v1 fieldCount m2 getColCount = and
m2 getRowCount 1 = and
] [
v1:m2:;;
result: TRUE;
v1 fieldCount [
i v1 @ i 0 m2 @ @ = result and !result
] times
] pfunc;
/: [
vector:value:;;
vector vector?
] [
vector:value:;;
(vector fieldCount [i vector @ value /] times)
] pfunc;
*: [
vector:value:;;
vector vector?
] [
vector:value:;;
(vector fieldCount [i vector @ value *] times)
] pfunc;
*: [
value:vector:;;
vector vector?
] [
value:vector:;;
(vector fieldCount [i vector @ value *] times)
] pfunc;
*: [
v1:v2:;;
v1 vector?
v2 vector? and
] [
0 .CAN_NOT_MUL_TWO_VECTORS_USE_DOT_OR_CROSS_OR_HADAMAR
] pfunc;
*: [
m1:m2:;;
m1 matrix? m2 matrix? and
m1 getColCount m2 getRowCount = and
] [
m1:m2:;;
result: 0 0 m1 @ @ m2 getColCount m1 getRowCount Matrix;
m1 getRowCount [ row0: i;
m2 getColCount [ col1: i;
0 row0 m1 @ @
col1 0 m2 @ @ *
col1 row0 @result @ @ set
m1 getColCount 1 - [ col0: i 1 +;
col0 row0 m1 @ @
col1 col0 m2 @ @ *
col1 row0 result @ @ +
col1 row0 @result @ @ set
] times
] times
] times
result
] pfunc;
*: [
v1:m2:;;
v1 vector?
v1 matrix? not and
m2 matrix? and
v1 fieldCount m2 getRowCount = and
] [
v:m:;;
result: v copy;
v fieldCount [ col1: i;
0 v @
col1 0 m @ @ *
col1 @result @ set
m getColCount 1 - [ col0: i 1 +;
col0 v @
col1 col0 m @ @ *
col1 result @ +
col1 @result @ set
] times
] times
result
] pfunc;
|: [
m1:m2:;;
m1 matrix?
m2 matrix? and
m1 getColCount m2 getColCount = and
] [
m1:m2:;;
(
m1 getRowCount [i m1 @ copy] times
m2 getRowCount [i m2 @ copy] times
)
] pfunc;
|: [
m:v:;;
m matrix?
v vector? and
m getColCount v fieldCount = and
] [
m:v:;;
(
m getRowCount [i m @ copy] times
v copy
)
] pfunc;
&: [
v1:v2:;;
v1 vector?
v2 vector? and
] [
v1:v2:;;
(v1 fieldCount [i v1 @ copy] times v2 fieldCount [i v2 @ copy] times)
] pfunc;
&: [
m1:m2:;;
m1 matrix?
m2 matrix? and
m1 getRowCount m2 getRowCount = and
] [
m1:m2:;;
(
m1 getRowCount [ row: i;
(
m1 getColCount [i row m1 @ @ copy] times
m2 getColCount [i row m2 @ @ copy] times
)
] times
)
] pfunc;
toColumn: [vector?] [v:;(v fieldCount [(i v @ copy)] times)] pfunc;
multiply: [
v1:v2:;;
v1 vector? v2 vector? and
v1 fieldCount v2 fieldCount = and
] [
v1:v2:;;
(v1 fieldCount [i v1 @ i v2 @ *] times)
] pfunc;
divide: [
v1:v2:;;
v1 vector? v2 vector? and
v1 fieldCount v2 fieldCount = and
] [
v1:v2:;; (v1 fieldCount [i v1 @ i v2 @ /] times)
] pfunc;
dot: [
v0:v1:;;
v0 vector?
v1 vector? and
v0 fieldCount v1 fieldCount = and
] [
v0:v1:;;
sum: 0 v0 @ 0 v1 @ *;
v0 fieldCount 1 - [i1: i 1 +; i1 v0 @ i1 v1 @ * sum + !sum] times
sum
] pfunc;
cross: [
v0:v1:;;
v0 fieldCount 3 =
v1 fieldCount 3 = and
] [
v0:v1:;;
(
1 v0 @ 2 v1 @ * 2 v0 @ 1 v1 @ * -
2 v0 @ 0 v1 @ * 0 v0 @ 2 v1 @ * -
0 v0 @ 1 v1 @ * 1 v0 @ 0 v1 @ * -
)
] pfunc;
squaredLength: [vector?] [
v:; v v dot
] pfunc;
length: [vector?] [
v:; v v dot sqrt
] pfunc;
unit: [vector?] [
v:;
one: 1 0 v @ cast;
v one v length / *
] pfunc;
neg: [vector?] [v:; (v fieldCount [i v @ neg] times)] pfunc;
cast: [v0:v1:;; v0 vector? v1 vector? and] [v0:v1:;; (v1 fieldCount [i v0 @ i v1 @ cast] times)] pfunc;
trans: [
v:;
v vector?
v matrix? not and
] [
v:;
(v fieldCount [(i v @ copy)] times)
] pfunc;
trans: [matrix?] [
m:;
result: 0 0 m @ @ m getRowCount m getColCount Matrix;
m getRowCount [ row: i;
m getColCount [ col: i;
col row m @ @
row col @result @ @ set
] times
] times
result
] pfunc;
lerp: [
f: copy;
v1:;
v0:;
v0 1 f cast f - *
v1 f *
+
];
rotationMatrix: [
Real32 same
] [
angle: copy;
c: angle cos;
s: angle sin;
(
(c copy s copy)
(s neg c copy)
)
] pfunc;
det: [
m:;
m matrix?
m getColCount 1 = and
m getRowCount 1 = and
] [m:; 0 0 m @ copy] pfunc;
det: [
m:;
m matrix?
m getColCount 2 = and
m getRowCount 2 = and
] [
m:;
0 0 m @ @
1 1 m @ @ *
1 0 m @ @
0 1 m @ @ * -
] pfunc;
det: [
m:;
m matrix?
m getColCount 3 = and
m getRowCount 3 = and
] [
m:;
1 0 m @ @
2 1 m @ @ *
2 0 m @ @
1 1 m @ @ * -
0 2 m @ @ *
2 0 m @ @
0 1 m @ @ *
0 0 m @ @
2 1 m @ @ * -
1 2 m @ @ * +
0 0 m @ @
1 1 m @ @ *
1 0 m @ @
0 1 m @ @ * -
2 2 m @ @ * +
] pfunc;
det: [
m:;
m matrix?
m getColCount 4 = and
m getRowCount 4 = and
] [
m:;
m00_11: 0 0 m @ @ 1 1 m @ @ *;
m00_12: 0 0 m @ @ 2 1 m @ @ *;
m00_13: 0 0 m @ @ 3 1 m @ @ *;
m01_10: 1 0 m @ @ 0 1 m @ @ *;
m01_12: 1 0 m @ @ 2 1 m @ @ *;
m01_13: 1 0 m @ @ 3 1 m @ @ *;
m02_10: 2 0 m @ @ 0 1 m @ @ *;
m02_11: 2 0 m @ @ 1 1 m @ @ *;
m02_13: 2 0 m @ @ 3 1 m @ @ *;
m03_10: 3 0 m @ @ 0 1 m @ @ *;
m03_11: 3 0 m @ @ 1 1 m @ @ *;
m03_12: 3 0 m @ @ 2 1 m @ @ *;
m03_12 m02_13 - 1 2 m @ @ * m01_13 m03_11 - 2 2 m @ @ * + m02_11 m01_12 - 3 2 m @ @ * + 0 3 m @ @ *
m03_10 m00_13 - 2 2 m @ @ * m00_12 m02_10 - 3 2 m @ @ * + m02_13 m03_12 - 0 2 m @ @ * + 1 3 m @ @ * +
m01_10 m00_11 - 3 2 m @ @ * m03_11 m01_13 - 0 2 m @ @ * + m00_13 m03_10 - 1 2 m @ @ * + 2 3 m @ @ * +
m01_12 m02_11 - 0 2 m @ @ * m02_10 m00_12 - 1 2 m @ @ * + m00_11 m01_10 - 2 2 m @ @ * + 3 3 m @ @ * +
] pfunc;
adj: [
m:;
m matrix?
m getColCount 1 = and
m getRowCount 1 = and
] [
((0 0 m @ @ 0 0 m @ @ /))
] pfunc;
adj: [
m:;
m matrix?
m getColCount 2 = and
m getRowCount 2 = and
] [
m:;
(
(1 1 m @ @ 1 0 m @ @ neg)
(0 1 m @ @ neg 0 0 m @ @)
)
] pfunc;
adj: [
m:;
m matrix?
m getColCount 3 = and
m getRowCount 3 = and
] [
m:;
(
(
1 1 m @ @ 2 2 m @ @ * 2 1 m @ @ 1 2 m @ @ * -
1 2 m @ @ 2 0 m @ @ * 2 2 m @ @ 1 0 m @ @ * -
1 0 m @ @ 2 1 m @ @ * 2 0 m @ @ 1 1 m @ @ * -
)
(
2 1 m @ @ 0 2 m @ @ * 0 1 m @ @ 2 2 m @ @ * -
2 2 m @ @ 0 0 m @ @ * 0 2 m @ @ 2 0 m @ @ * -
2 0 m @ @ 0 1 m @ @ * 0 0 m @ @ 2 1 m @ @ * -
)
(
0 1 m @ @ 1 2 m @ @ * 1 1 m @ @ 0 2 m @ @ * -
0 2 m @ @ 1 0 m @ @ * 1 2 m @ @ 0 0 m @ @ * -
0 0 m @ @ 1 1 m @ @ * 1 0 m @ @ 0 1 m @ @ * -
)
)
] pfunc;
adj: [
m:;
m matrix?
m getColCount 4 = and
m getRowCount 4 = and
] [
m:;
m00_11: 0 0 m @ @ 1 1 m @ @ *;
m00_12: 0 0 m @ @ 2 1 m @ @ *;
m00_13: 0 0 m @ @ 3 1 m @ @ *;
m00_21: 0 0 m @ @ 1 2 m @ @ *;
m00_22: 0 0 m @ @ 2 2 m @ @ *;
m00_23: 0 0 m @ @ 3 2 m @ @ *;
m01_10: 1 0 m @ @ 0 1 m @ @ *;
m01_12: 1 0 m @ @ 2 1 m @ @ *;
m01_13: 1 0 m @ @ 3 1 m @ @ *;
m01_20: 1 0 m @ @ 0 2 m @ @ *;
m01_22: 1 0 m @ @ 2 2 m @ @ *;
m01_23: 1 0 m @ @ 3 2 m @ @ *;
m02_10: 2 0 m @ @ 0 1 m @ @ *;
m02_11: 2 0 m @ @ 1 1 m @ @ *;
m02_13: 2 0 m @ @ 3 1 m @ @ *;
m02_20: 2 0 m @ @ 0 2 m @ @ *;
m02_21: 2 0 m @ @ 1 2 m @ @ *;
m02_23: 2 0 m @ @ 3 2 m @ @ *;
m03_10: 3 0 m @ @ 0 1 m @ @ *;
m03_11: 3 0 m @ @ 1 1 m @ @ *;
m03_12: 3 0 m @ @ 2 1 m @ @ *;
m03_20: 3 0 m @ @ 0 2 m @ @ *;
m03_21: 3 0 m @ @ 1 2 m @ @ *;
m03_22: 3 0 m @ @ 2 2 m @ @ *;
m10_21: 0 1 m @ @ 1 2 m @ @ *;
m10_22: 0 1 m @ @ 2 2 m @ @ *;
m10_23: 0 1 m @ @ 3 2 m @ @ *;
m11_20: 1 1 m @ @ 0 2 m @ @ *;
m11_22: 1 1 m @ @ 2 2 m @ @ *;
m11_23: 1 1 m @ @ 3 2 m @ @ *;
m12_20: 2 1 m @ @ 0 2 m @ @ *;
m12_21: 2 1 m @ @ 1 2 m @ @ *;
m12_23: 2 1 m @ @ 3 2 m @ @ *;
m13_20: 3 1 m @ @ 0 2 m @ @ *;
m13_21: 3 1 m @ @ 1 2 m @ @ *;
m13_22: 3 1 m @ @ 2 2 m @ @ *;
(
(
m12_23 m13_22 - 1 3 m @ @ * m13_21 m11_23 - 2 3 m @ @ * m11_22 m12_21 - 3 3 m @ @ * + +
m03_22 m02_23 - 1 3 m @ @ * m01_23 m03_21 - 2 3 m @ @ * m02_21 m01_22 - 3 3 m @ @ * + +
m02_13 m03_12 - 1 3 m @ @ * m03_11 m01_13 - 2 3 m @ @ * m01_12 m02_11 - 3 3 m @ @ * + +
m03_12 m02_13 - 1 2 m @ @ * m01_13 m03_11 - 2 2 m @ @ * m02_11 m01_12 - 3 2 m @ @ * + +
)
(
m13_22 m12_23 - 0 3 m @ @ * m10_23 m13_20 - 2 3 m @ @ * m12_20 m10_22 - 3 3 m @ @ * + +
m02_23 m03_22 - 0 3 m @ @ * m03_20 m00_23 - 2 3 m @ @ * m00_22 m02_20 - 3 3 m @ @ * + +
m03_12 m02_13 - 0 3 m @ @ * m00_13 m03_10 - 2 3 m @ @ * m02_10 m00_12 - 3 3 m @ @ * + +
m02_13 m03_12 - 0 2 m @ @ * m03_10 m00_13 - 2 2 m @ @ * m00_12 m02_10 - 3 2 m @ @ * + +
)
(
m11_23 m13_21 - 0 3 m @ @ * m13_20 m10_23 - 1 3 m @ @ * m10_21 m11_20 - 3 3 m @ @ * + +
m03_21 m01_23 - 0 3 m @ @ * m00_23 m03_20 - 1 3 m @ @ * m01_20 m00_21 - 3 3 m @ @ * + +
m01_13 m03_11 - 0 3 m @ @ * m03_10 m00_13 - 1 3 m @ @ * m00_11 m01_10 - 3 3 m @ @ * + +
m03_11 m01_13 - 0 2 m @ @ * m00_13 m03_10 - 1 2 m @ @ * m01_10 m00_11 - 3 2 m @ @ * + +
)
(
m12_21 m11_22 - 0 3 m @ @ * m10_22 m12_20 - 1 3 m @ @ * m11_20 m10_21 - 2 3 m @ @ * + +
m01_22 m02_21 - 0 3 m @ @ * m02_20 m00_22 - 1 3 m @ @ * m00_21 m01_20 - 2 3 m @ @ * + +
m02_11 m01_12 - 0 3 m @ @ * m00_12 m02_10 - 1 3 m @ @ * m01_10 m00_11 - 2 3 m @ @ * + +
m01_12 m02_11 - 0 2 m @ @ * m02_10 m00_12 - 1 2 m @ @ * m00_11 m01_10 - 2 2 m @ @ * + +
)
)
] pfunc;
inv: [
m:;
m matrix?
m getColCount m getRowCount = and
] [
m:;
m adj m det /
] pfunc;