-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
complexEigs.js
693 lines (561 loc) · 20 KB
/
complexEigs.js
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
import { clone } from '../../../utils/object.js'
export function createComplexEigs ({ addScalar, subtract, flatten, multiply, multiplyScalar, divideScalar, sqrt, abs, bignumber, diag, inv, qr, usolve, usolveAll, equal, complex, larger, smaller, matrixFromColumns, dot }) {
/**
* @param {number[][]} arr the matrix to find eigenvalues of
* @param {number} N size of the matrix
* @param {number|BigNumber} prec precision, anything lower will be considered zero
* @param {'number'|'BigNumber'|'Complex'} type
* @param {boolean} findVectors should we find eigenvectors?
*
* @returns {{ values: number[], vectors: number[][] }}
*/
function complexEigs (arr, N, prec, type, findVectors) {
if (findVectors === undefined) {
findVectors = true
}
// TODO check if any row/col are zero except the diagonal
// make sure corresponding rows and columns have similar magnitude
// important because of numerical stability
const R = balance(arr, N, prec, type, findVectors)
// R is the row transformation matrix
// A' = R A R⁻¹, A is the original matrix
// (if findVectors is false, R is undefined)
// TODO if magnitudes of elements vary over many orders,
// move greatest elements to the top left corner
// using similarity transformations, reduce the matrix
// to Hessenberg form (upper triangular plus one subdiagonal row)
// updates the transformation matrix R with new row operationsq
reduceToHessenberg(arr, N, prec, type, findVectors, R)
// find eigenvalues
let { values, C } = iterateUntilTriangular(arr, N, prec, type, findVectors)
// values is the list of eigenvalues, C is the column
// transformation matrix that transforms the hessenberg
// matrix to upper triangular
// compose transformations A → hess. and hess. → triang.
C = multiply(inv(R), C)
let vectors
if (findVectors) {
vectors = findEigenvectors(arr, N, C, values, prec, type)
vectors = matrixFromColumns(...vectors)
}
return { values, vectors }
}
/**
* @param {number[][]} arr
* @param {number} N
* @param {number} prec
* @param {'number'|'BigNumber'|'Complex'} type
* @returns {number[][]}
*/
function balance (arr, N, prec, type, findVectors) {
const big = type === 'BigNumber'
const cplx = type === 'Complex'
const zero = big ? bignumber(0) : cplx ? complex(0) : 0
const one = big ? bignumber(1) : cplx ? complex(1) : 1
// base of the floating-point arithmetic
const radix = big ? bignumber(10) : 2
const radixSq = multiplyScalar(radix, radix)
// the diagonal transformation matrix R
let Rdiag
if (findVectors) {
Rdiag = Array(N).fill(one)
}
// this isn't the only time we loop thru the matrix...
let last = false
while (!last) {
// ...haha I'm joking! unless...
last = true
for (let i = 0; i < N; i++) {
// compute the taxicab norm of i-th column and row
// TODO optimize for complex numbers
let colNorm = zero
let rowNorm = zero
for (let j = 0; j < N; j++) {
if (i === j) continue
const c = abs(arr[i][j])
colNorm = addScalar(colNorm, c)
rowNorm = addScalar(rowNorm, c)
}
if (!equal(colNorm, 0) && !equal(rowNorm, 0)) {
// find integer power closest to balancing the matrix
// (we want to scale only by integer powers of radix,
// so that we don't lose any precision due to round-off)
let f = one
let c = colNorm
const rowDivRadix = divideScalar(rowNorm, radix)
const rowMulRadix = multiplyScalar(rowNorm, radix)
while (smaller(c, rowDivRadix)) {
c = multiplyScalar(c, radixSq)
f = multiplyScalar(f, radix)
}
while (larger(c, rowMulRadix)) {
c = divideScalar(c, radixSq)
f = divideScalar(f, radix)
}
// check whether balancing is needed
// condition = (c + rowNorm) / f < 0.95 * (colNorm + rowNorm)
const condition = smaller(divideScalar(addScalar(c, rowNorm), f), multiplyScalar(addScalar(colNorm, rowNorm), 0.95))
// apply balancing similarity transformation
if (condition) {
// we should loop once again to check whether
// another rebalancing is needed
last = false
const g = divideScalar(1, f)
for (let j = 0; j < N; j++) {
if (i === j) {
continue
}
arr[i][j] = multiplyScalar(arr[i][j], f)
arr[j][i] = multiplyScalar(arr[j][i], g)
}
// keep track of transformations
if (findVectors) {
Rdiag[i] = multiplyScalar(Rdiag[i], f)
}
}
}
}
}
// return the diagonal row transformation matrix
return diag(Rdiag)
}
/**
* @param {number[][]} arr
* @param {number} N
* @param {number} prec
* @param {'number'|'BigNumber'|'Complex'} type
* @param {boolean} findVectors
* @param {number[][]} R the row transformation matrix that will be modified
*/
function reduceToHessenberg (arr, N, prec, type, findVectors, R) {
const big = type === 'BigNumber'
const cplx = type === 'Complex'
const zero = big ? bignumber(0) : cplx ? complex(0) : 0
if (big) { prec = bignumber(prec) }
for (let i = 0; i < N - 2; i++) {
// Find the largest subdiag element in the i-th col
let maxIndex = 0
let max = zero
for (let j = i + 1; j < N; j++) {
const el = arr[j][i]
if (smaller(abs(max), abs(el))) {
max = el
maxIndex = j
}
}
// This col is pivoted, no need to do anything
if (smaller(abs(max), prec)) {
continue
}
if (maxIndex !== i + 1) {
// Interchange maxIndex-th and (i+1)-th row
const tmp1 = arr[maxIndex]
arr[maxIndex] = arr[i + 1]
arr[i + 1] = tmp1
// Interchange maxIndex-th and (i+1)-th column
for (let j = 0; j < N; j++) {
const tmp2 = arr[j][maxIndex]
arr[j][maxIndex] = arr[j][i + 1]
arr[j][i + 1] = tmp2
}
// keep track of transformations
if (findVectors) {
const tmp3 = R[maxIndex]
R[maxIndex] = R[i + 1]
R[i + 1] = tmp3
}
}
// Reduce following rows and columns
for (let j = i + 2; j < N; j++) {
const n = divideScalar(arr[j][i], max)
if (n === 0) {
continue
}
// from j-th row subtract n-times (i+1)th row
for (let k = 0; k < N; k++) {
arr[j][k] = subtract(arr[j][k], multiplyScalar(n, arr[i + 1][k]))
}
// to (i+1)th column add n-times j-th column
for (let k = 0; k < N; k++) {
arr[k][i + 1] = addScalar(arr[k][i + 1], multiplyScalar(n, arr[k][j]))
}
// keep track of transformations
if (findVectors) {
for (let k = 0; k < N; k++) {
R[j][k] = subtract(R[j][k], multiplyScalar(n, R[i + 1][k]))
}
}
}
}
return R
}
/**
* @returns {{values: values, C: Matrix}}
* @see Press, Wiliams: Numerical recipes in Fortran 77
* @see https://en.wikipedia.org/wiki/QR_algorithm
*/
function iterateUntilTriangular (A, N, prec, type, findVectors) {
const big = type === 'BigNumber'
const cplx = type === 'Complex'
const one = big ? bignumber(1) : cplx ? complex(1) : 1
if (big) { prec = bignumber(prec) }
// The Francis Algorithm
// The core idea of this algorithm is that doing successive
// A' = Q⁺AQ transformations will eventually converge to block-
// upper-triangular with diagonal blocks either 1x1 or 2x2.
// The Q here is the one from the QR decomposition, A = QR.
// Since the eigenvalues of a block-upper-triangular matrix are
// the eigenvalues of its diagonal blocks and we know how to find
// eigenvalues of a 2x2 matrix, we know the eigenvalues of A.
let arr = clone(A)
// the list of converged eigenvalues
const lambdas = []
// size of arr, which will get smaller as eigenvalues converge
let n = N
// the diagonal of the block-diagonal matrix that turns
// converged 2x2 matrices into upper triangular matrices
const Sdiag = []
// N×N matrix describing the overall transformation done during the QR algorithm
let Qtotal = findVectors ? diag(Array(N).fill(one)) : undefined
// n×n matrix describing the QR transformations done since last convergence
let Qpartial = findVectors ? diag(Array(n).fill(one)) : undefined
// last eigenvalue converged before this many steps
let lastConvergenceBefore = 0
while (lastConvergenceBefore <= 100) {
lastConvergenceBefore += 1
// TODO if the convergence is slow, do something clever
// Perform the factorization
const k = 0 // TODO set close to an eigenvalue
for (let i = 0; i < n; i++) {
arr[i][i] = subtract(arr[i][i], k)
}
// TODO do an implicit QR transformation
const { Q, R } = qr(arr)
arr = multiply(R, Q)
for (let i = 0; i < n; i++) {
arr[i][i] = addScalar(arr[i][i], k)
}
// keep track of transformations
if (findVectors) {
Qpartial = multiply(Qpartial, Q)
}
// The rightmost diagonal element converged to an eigenvalue
if (n === 1 || smaller(abs(arr[n - 1][n - 2]), prec)) {
lastConvergenceBefore = 0
lambdas.push(arr[n - 1][n - 1])
// keep track of transformations
if (findVectors) {
Sdiag.unshift([[1]])
inflateMatrix(Qpartial, N)
Qtotal = multiply(Qtotal, Qpartial)
if (n > 1) {
Qpartial = diag(Array(n - 1).fill(one))
}
}
// reduce the matrix size
n -= 1
arr.pop()
for (let i = 0; i < n; i++) {
arr[i].pop()
}
// The rightmost diagonal 2x2 block converged
} else if (n === 2 || smaller(abs(arr[n - 2][n - 3]), prec)) {
lastConvergenceBefore = 0
const ll = eigenvalues2x2(
arr[n - 2][n - 2], arr[n - 2][n - 1],
arr[n - 1][n - 2], arr[n - 1][n - 1]
)
lambdas.push(...ll)
// keep track of transformations
if (findVectors) {
Sdiag.unshift(jordanBase2x2(
arr[n - 2][n - 2], arr[n - 2][n - 1],
arr[n - 1][n - 2], arr[n - 1][n - 1],
ll[0], ll[1], prec, type
))
inflateMatrix(Qpartial, N)
Qtotal = multiply(Qtotal, Qpartial)
if (n > 2) {
Qpartial = diag(Array(n - 2).fill(one))
}
}
// reduce the matrix size
n -= 2
arr.pop()
arr.pop()
for (let i = 0; i < n; i++) {
arr[i].pop()
arr[i].pop()
}
}
if (n === 0) {
break
}
}
// standard sorting
lambdas.sort((a, b) => +subtract(abs(a), abs(b)))
// the algorithm didn't converge
if (lastConvergenceBefore > 100) {
const err = Error('The eigenvalues failed to converge. Only found these eigenvalues: ' + lambdas.join(', '))
err.values = lambdas
err.vectors = []
throw err
}
// combine the overall QR transformation Qtotal with the subsequent
// transformation S that turns the diagonal 2x2 blocks to upper triangular
const C = findVectors ? multiply(Qtotal, blockDiag(Sdiag, N)) : undefined
return { values: lambdas, C }
}
/**
* @param {Matrix} A original matrix
* @param {number} N size of A
* @param {Matrix} C column transformation matrix that turns A into upper triangular
* @param {number[]} values array of eigenvalues of A
* @param {'number'|'BigNumber'|'Complex'} type
* @returns {number[][]} eigenvalues
*/
function findEigenvectors (A, N, C, values, prec, type) {
const Cinv = inv(C)
const U = multiply(Cinv, A, C)
const big = type === 'BigNumber'
const cplx = type === 'Complex'
const zero = big ? bignumber(0) : cplx ? complex(0) : 0
const one = big ? bignumber(1) : cplx ? complex(1) : 1
// turn values into a kind of "multiset"
// this way it is easier to find eigenvectors
const uniqueValues = []
const multiplicities = []
for (const λ of values) {
const i = indexOf(uniqueValues, λ, equal)
if (i === -1) {
uniqueValues.push(λ)
multiplicities.push(1)
} else {
multiplicities[i] += 1
}
}
// find eigenvectors by solving U − λE = 0
// TODO replace with an iterative eigenvector algorithm
// (this one might fail for imprecise eigenvalues)
const vectors = []
const len = uniqueValues.length
const b = Array(N).fill(zero)
const E = diag(Array(N).fill(one))
// eigenvalues for which usolve failed (due to numerical error)
const failedLambdas = []
for (let i = 0; i < len; i++) {
const λ = uniqueValues[i]
const A = subtract(U, multiply(λ, E)) // the characteristic matrix
let solutions = usolveAll(A, b)
solutions = solutions.map(v => multiply(C, v))
solutions.shift() // ignore the null vector
// looks like we missed something, try inverse iteration
while (solutions.length < multiplicities[i]) {
const approxVec = inverseIterate(A, N, solutions, prec, type)
if (approxVec == null) {
// no more vectors were found
failedLambdas.push(λ)
break
}
solutions.push(approxVec)
}
vectors.push(...solutions.map(v => flatten(v)))
}
if (failedLambdas.length !== 0) {
const err = new Error('Failed to find eigenvectors for the following eigenvalues: ' + failedLambdas.join(', '))
err.values = values
err.vectors = vectors
throw err
}
return vectors
}
/**
* Compute the eigenvalues of an 2x2 matrix
* @return {[number,number]}
*/
function eigenvalues2x2 (a, b, c, d) {
// λ± = ½ trA ± ½ √( tr²A - 4 detA )
const trA = addScalar(a, d)
const detA = subtract(multiplyScalar(a, d), multiplyScalar(b, c))
const x = multiplyScalar(trA, 0.5)
const y = multiplyScalar(sqrt(subtract(multiplyScalar(trA, trA), multiplyScalar(4, detA))), 0.5)
return [addScalar(x, y), subtract(x, y)]
}
/**
* For an 2x2 matrix compute the transformation matrix S,
* so that SAS⁻¹ is an upper triangular matrix
* @return {[[number,number],[number,number]]}
* @see https://math.berkeley.edu/~ogus/old/Math_54-05/webfoils/jordan.pdf
* @see http://people.math.harvard.edu/~knill/teaching/math21b2004/exhibits/2dmatrices/index.html
*/
function jordanBase2x2 (a, b, c, d, l1, l2, prec, type) {
const big = type === 'BigNumber'
const cplx = type === 'Complex'
const zero = big ? bignumber(0) : cplx ? complex(0) : 0
const one = big ? bignumber(1) : cplx ? complex(1) : 1
// matrix is already upper triangular
// return an identity matrix
if (smaller(abs(c), prec)) {
return [[one, zero], [zero, one]]
}
// matrix is diagonalizable
// return its eigenvectors as columns
if (larger(abs(subtract(l1, l2)), prec)) {
return [[subtract(l1, d), subtract(l2, d)], [c, c]]
}
// matrix is not diagonalizable
// compute off-diagonal elements of N = A - λI
// N₁₂ = 0 ⇒ S = ( N⃗₁, I⃗₁ )
// N₁₂ ≠ 0 ⇒ S = ( N⃗₂, I⃗₂ )
const na = subtract(a, l1)
const nb = subtract(b, l1)
const nc = subtract(c, l1)
const nd = subtract(d, l1)
if (smaller(abs(nb), prec)) {
return [[na, one], [nc, zero]]
} else {
return [[nb, zero], [nd, one]]
}
}
/**
* Enlarge the matrix from n×n to N×N, setting the new
* elements to 1 on diagonal and 0 elsewhere
*/
function inflateMatrix (arr, N) {
// add columns
for (let i = 0; i < arr.length; i++) {
arr[i].push(...Array(N - arr[i].length).fill(0))
}
// add rows
for (let i = arr.length; i < N; i++) {
arr.push(Array(N).fill(0))
arr[i][i] = 1
}
return arr
}
/**
* Create a block-diagonal matrix with the given square matrices on the diagonal
* @param {Matrix[] | number[][][]} arr array of matrices to be placed on the diagonal
* @param {number} N the size of the resulting matrix
*/
function blockDiag (arr, N) {
const M = []
for (let i = 0; i < N; i++) {
M[i] = Array(N).fill(0)
}
let I = 0
for (const sub of arr) {
const n = sub.length
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
M[I + i][I + j] = sub[i][j]
}
}
I += n
}
return M
}
/**
* Finds the index of an element in an array using a custom equality function
* @template T
* @param {Array<T>} arr array in which to search
* @param {T} el the element to find
* @param {function(T, T): boolean} fn the equality function, first argument is an element of `arr`, the second is always `el`
* @returns {number} the index of `el`, or -1 when it's not in `arr`
*/
function indexOf (arr, el, fn) {
for (let i = 0; i < arr.length; i++) {
if (fn(arr[i], el)) {
return i
}
}
return -1
}
/**
* Provided a near-singular upper-triangular matrix A and a list of vectors,
* finds an eigenvector of A with the smallest eigenvalue, which is orthogonal
* to each vector in the list
* @template T
* @param {T[][]} A near-singular square matrix
* @param {number} N dimension
* @param {T[][]} orthog list of vectors
* @param {number} prec epsilon
* @param {'number'|'BigNumber'|'Complex'} type
* @return {T[] | null} eigenvector
*
* @see Numerical Recipes for Fortran 77 – 11.7 Eigenvalues or Eigenvectors by Inverse Iteration
*/
function inverseIterate (A, N, orthog, prec, type) {
const largeNum = type === 'BigNumber' ? bignumber(1000) : 1000
let b // the vector
// you better choose a random vector before I count to five
let i = 0
while (true) {
b = randomOrthogonalVector(N, orthog, type)
b = usolve(A, b)
if (larger(norm(b), largeNum)) { break }
if (++i >= 5) { return null }
}
// you better converge before I count to ten
i = 0
while (true) {
const c = usolve(A, b)
if (smaller(norm(orthogonalComplement(b, [c])), prec)) { break }
if (++i >= 10) { return null }
b = normalize(c)
}
return b
}
/**
* Generates a random unit vector of dimension N, orthogonal to each vector in the list
* @template T
* @param {number} N dimension
* @param {T[][]} orthog list of vectors
* @param {'number'|'BigNumber'|'Complex'} type
* @returns {T[]} random vector
*/
function randomOrthogonalVector (N, orthog, type) {
const big = type === 'BigNumber'
const cplx = type === 'Complex'
// generate random vector with the correct type
let v = Array(N).fill(0).map(_ => 2 * Math.random() - 1)
if (big) { v = v.map(n => bignumber(n)) }
if (cplx) { v = v.map(n => complex(n)) }
// project to orthogonal complement
v = orthogonalComplement(v, orthog)
// normalize
return normalize(v, type)
}
/**
* Project vector v to the orthogonal complement of an array of vectors
*/
function orthogonalComplement (v, orthog) {
for (const w of orthog) {
// v := v − (w, v)/∥w∥² w
v = subtract(v, multiply(divideScalar(dot(w, v), dot(w, w)), w))
}
return v
}
/**
* Calculate the norm of a vector.
* We can't use math.norm because factory can't handle circular dependency.
* Seriously, I'm really fed up with factory.
*/
function norm (v) {
return abs(sqrt(dot(v, v)))
}
/**
* Normalize a vector
* @template T
* @param {T[]} v
* @param {'number'|'BigNumber'|'Complex'} type
* @returns {T[]} normalized vec
*/
function normalize (v, type) {
const big = type === 'BigNumber'
const cplx = type === 'Complex'
const one = big ? bignumber(1) : cplx ? complex(1) : 1
return multiply(divideScalar(one, norm(v)), v)
}
return complexEigs
}