-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinAlg.gd
590 lines (433 loc) · 10 KB
/
LinAlg.gd
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
588
589
590
class_name LinAlg
"""
Linear Algebra library class
All methods are optimised for maximum speed. They take arrays and assume the
right dimension for them. If the inputs aren't right they'll crash. Third input
is used for the answer, preallocated. There are no conditional branchings.
Just use the method appropriate to the situation. The names are coded to reflect
that. s = scalar, v = vector and m = matrix. So for example
dot_vm
is a dot product between vector and matrix (in that order). Wherever the in_place
argument is provided, it is possible to perform the operation on the object
itself instead of instantiating a new one (this too optimises performance).
"""
# Initialise a vector
static func init_v(n: int, v0: float=0.0)->Array:
var ans = []
ans.resize(n)
for i in range(n):
ans[i] = v0
return ans
# Initialise a matrix
static func init_m(m: int, n: int, m0: float=0.0)->Array:
var ans = []
ans.resize(m)
for i in range(m):
var row = []
row.resize(n)
for j in range(n):
row[j] = m0
ans[i] = row
return ans
# Identity matrix
static func eye(n: int)->Array:
var ans = []
ans.resize(n)
for i in range(n):
var row = []
row.resize(n)
for j in range(n):
row[j] = 1 if i == j else 0
ans[i] = row
return ans
# Diagonal matrix
static func diag(v: Array)->Array:
var n = len(v)
var ans = []
ans.resize(n)
for i in range(n):
var row = []
row.resize(n)
var vi = v[i]
for j in range(n):
row[j] = vi if i == j else 0
ans[i] = row
return ans
# Dyadic matrix
static func dyadic(v: Array)->Array:
var n = len(v)
var ans = []
ans.resize(n)
for i in range(n):
var vi = v[i]
var row = v.duplicate()
for j in range(n):
row[j] *= vi
ans[i] = row
return ans
# Transpose
static func transpose(M: Array, in_place: bool=false)->Array:
var n = len(M)
var ans
if not in_place:
ans = M.duplicate(true)
else:
ans = M
for i in range(n-1):
var row = ans[i]
for j in range(i+1,n):
var dummy = row[j]
row[j] = ans[j][i]
ans[j][i] = dummy
return ans
# Householder matrix from vector
# (https://en.wikipedia.org/wiki/Householder_transformation)
static func householder(v: Array)->Array:
var n = len(v)
var ans = []
ans.resize(n)
for i in range(n):
var vi = -v[i]*2
var row = v.duplicate()
for j in range(n):
row[j] *= vi
if i == j:
row[j] += 1
ans[i] = row
return ans
# Random vector
static func rand_v(n: int, s: float=1)->Array:
var ans = []
ans.resize(n)
for i in range(n):
ans[i] = randf()*s
return ans
# Random matrix
static func rand_m(m: int, n: int, s: float=1)->Array:
var ans = []
ans.resize(n)
for i in range(m):
var row = []
row.resize(n)
for j in range(n):
row[j] = randf()*s
ans[i] = row
return ans
# Element-wise: vector plus scalar
static func ewise_vs_add(v: Array, s: float, in_place: bool=false)->Array:
var n = len(v)
var ans
if in_place:
ans = v
else:
ans = init_v(n)
for i in range(n):
ans[i] = v[i]+s
return ans
# Element-wise: vector times scalar
static func ewise_vs_mul(v: Array, s: float, in_place: bool=false)->Array:
var n = len(v)
var ans
if in_place:
ans = v
else:
ans = init_v(n)
for i in range(n):
ans[i] = v[i]*s
return ans
# Element-wise: vector plus vector
static func ewise_vv_add(v: Array, v2: Array, in_place: bool=false)->Array:
var n = len(v)
var ans
if in_place:
ans = v
else:
ans = init_v(n)
for i in range(n):
ans[i] = v[i]+v2[i]
return ans
# Element-wise: vector times vector
static func ewise_vv_mul(v: Array, v2: Array, in_place: bool=false)->Array:
var n = len(v)
var ans
if in_place:
ans = v
else:
ans = init_v(n)
for i in range(n):
ans[i] = v[i]*v2[i]
return ans
# Element-wise: matrix plus scalar
static func ewise_ms_add(M: Array, s: float, in_place: bool=false)->Array:
var m = len(M)
var n = len(M[0])
var ans
if in_place:
ans = M
else:
ans = init_m(m, n)
for i in range(m):
var rowin = M[i]
var rowout = ans[i]
for j in range(n):
rowout[j] = rowin[j]+s
return ans
# Element-wise: matrix times scalar
static func ewise_ms_mul(M: Array, s: float, in_place: bool=false)->Array:
var m = len(M)
var n = len(M[0])
var ans
if in_place:
ans = M
else:
ans = init_m(m, n)
for i in range(m):
var rowin = M[i]
var rowout = ans[i]
for j in range(n):
rowout[j] = rowin[j]*s
return ans
# Element-wise: matrix plus matrix
static func ewise_mm_add(M: Array, M2: Array, in_place: bool=false)->Array:
var m = len(M)
var n = len(M[0])
var ans
if in_place:
ans = M
else:
ans = init_m(m, n)
for i in range(m):
var row1 = M[i]
var row2 = M2[i]
var rowout = ans[i]
for j in range(n):
rowout[j] = row1[j]+row2[j]
return ans
static func column_add(M: Array, M2: Array, in_place: bool=false)->Array:
var ans = [[0],[1],[2]]
ans[0][0] = M[0][0] + M2[0][0]
ans[1][0] = M[1][0] + M2[1][0]
ans[2][0] = M[2][0] + M2[2][0]
return ans
static func column_mul(M: Array, s, in_place: bool=false)->Array:
var ans = [[0],[1],[2]]
ans[0][0] = M[0][0] *s
ans[1][0] = M[1][0] *s
ans[2][0] = M[2][0] *s
return ans
static func ewise_mm_sub(M: Array, M2: Array, in_place: bool=false)->Array:
var m = len(M)
var n = len(M[0])
var ans
if in_place:
ans = M
else:
ans = init_m(m, n)
for i in range(m):
var row1 = M[i]
var row2 = M2[i]
var rowout = ans[i]
for j in range(n):
rowout[j] = row1[j]-row2[j]
return ans
# Element-wise: matrix times matrix
static func ewise_mm_mul(M: Array, M2: Array, in_place: bool=false)->Array:
var m = len(M)
var n = len(M[0])
var ans
if in_place:
ans = M
else:
ans = init_m(m, n)
for i in range(m):
var row1 = M[i]
var row2 = M2[i]
var rowout = ans[i]
for j in range(n):
rowout[j] = row1[j]*row2[j]
return ans
static func column_cross_old(M: Array, M2: Array, in_place: bool=false)->Array:
var m = len(M)
var n = len(M[0])
var ans
if in_place:
ans = M
else:
ans = init_m(m, n)
for i in range(m):
var row1 = M[i]
var row2 = M2[i]
var rowout = ans[i]
for j in range(n):
rowout[j] = row1[j]*row2[j]
return ans
# Norm^2 of vector
static func norm2_v(v: Array)->float:
var ans = 0.0
for i in range(len(v)):
ans += pow(v[i], 2)
return ans
# Norm of vector
static func norm_v(v: Array)->float:
return sqrt(norm2_v(v))
# Normalize
static func normalized_v(v: Array, in_place: bool=false)->Array:
var norm = norm_v(v)
return ewise_vs_mul(v, 1.0/norm, in_place)
# Dot product: matrix times vector
static func dot_mv(M: Array, v: Array)->Array:
var m = len(M)
var n = len(v)
var ans = init_v(m)
for i in range(m):
var tot = 0.0
var row = M[i]
for j in range(n):
tot += row[j]*v[j]
ans[i] = tot
return ans
# Dot product: matrix times matrix
static func dot_mm(M: Array, M2: Array)->Array:
var m = len(M)
var n = len(M2[0])
var nn = len(M2)
var ans = init_m(m, n)
for i in range(m):
var row = M[i]
var rowout = ans[i]
for j in range(n):
var tot = 0.0
for k in range(nn):
tot += row[k]*M2[k][j]
rowout[j] = tot
return ans
# Dot product: vector times vector
static func dot_vv(v: Array, v2: Array)->float:
var n = len(v)
var ans = 0.0
for i in range(n):
ans += v[i]*v2[i]
return ans
# Utilities for QR: Extract minor
static func _minor(M: Array, d: int, ans: Array)->void:
var n = len(M)
for i in range(n):
var row = M[i]
var rowout = ans[i]
rowout.resize(n)
for j in range(n):
var x = 1 if i == j else 0
x = x if (i < d or j < d) else row[j]
rowout[j] = x
# Utilities for QR: copy column
static func _copycol(M: Array, v: Array, j: int)->void:
var m = len(M)
for i in range(m):
v[i] = M[i][j]
# QR decomposition
static func qr(M: Array)->Array:
var m = len(M)
var n = len(M[0])
var kmax = min(n, m-1)
var e = init_v(m)
var x = init_v(m)
var z = M.duplicate(true)
var z1 = init_m(m, n)
var vq = []
vq.resize(kmax)
for k in range(kmax):
var a
# Compute minor
_minor(z, k, z1)
# Extract column
_copycol(z1, x, k)
a = norm_v(x)
a = -a if M[k][k] > 0 else a
for i in range(m):
e[i] = x[i]
if i == k:
e[i] += a
normalized_v(e, true)
vq[k] = householder(e)
z = dot_mm(vq[k], z1)
var Q = vq[0]
for i in range(1, kmax):
Q = dot_mm(vq[i], Q)
var R = dot_mm(Q, M)
transpose(Q, true)
return [Q, R]
# Eigenvalues by power iteration for symmetric matrices
static func eigs_powerit(M: Array, tol: float=1e-5, in_place: bool=false)->Array:
var n = len(M)
if not in_place:
M = M.duplicate(true)
var evals = []
var evecs = []
evals.resize(n)
evecs.resize(n)
for k in range(n):
# Start with a random vector
var v0 = rand_v(n)
var e0 = 0
var v1
var e1
for t in range(100):
v1 = dot_mv(M, v0)
e1 = norm_v(v1)
ewise_vs_mul(v1, 1.0/e1, true)
if abs(e1-e0) < tol:
# Sign fix
e1 *= dot_vv(v0, v1)
break
e0 = e1
v0 = v1
evals[k] = e1
evecs[k] = v0
# Shift
for i in range(n):
var row = M[i]
var vi = v0[i]
for j in range(n):
row[j] -= e1*vi*v0[j]
return [evals, evecs]
static func column_cross(M: Array, M2: Array, in_place: bool=false)->Array:
var ax = M[0][0]
var ay = M[1][0]
var az = M[2][0]
var bx = M2[0][0]
var by = M2[1][0]
var bz = M2[2][0]
var cx = (ay*bz)-(az*by)
var cy = (az*bx)-(ax*bz)
var cz = (ax*by)-(ay*bx)
var ans = [[cx],[cy],[cz]]
return ans
# Eigenvalues by QR decomposition (still in development, commented out for now)
"""
static func eigs_qr(M: Array, tol: float=1e-8)->Array:
var n = len(M)
var evals = []
var evecs = eye(n)
evals.resize(n)
var A = M.duplicate(true)
for t in range(100):
# Compute the Wilkinson shift
var a = A[n-2][n-2]
var b = A[n-1][n-2]
var c = A[n-1][n-1]
var del = (a-c)/2.0
var ws = c-(sign(del) if del != 0 else 1)*b*b/(abs(del)+sqrt(del*del+a*a))
for i in range(n):
A[i][i] -= ws
var QR = qr(A)
A = dot_mm(QR[1], QR[0])
for i in range(n):
A[i][i] += ws
evecs = dot_mm(evecs, QR[0])
if abs(A[n-1][n-2]) < tol:
break
for i in range(n):
evals[i] = A[i][i]
transpose(evecs, true)
return [evals, evecs]
"""