Skip to content

Commit

Permalink
added a test for inverse iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
cshaa committed Jun 3, 2021
1 parent e093476 commit c99f3a0
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion test/unit-tests/function/matrix/eigs.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert'
import math from '../../../../src/defaultInstance.js'
import approx from '../../../../tools/approx.js'
const { eigs, complex, matrix, size, bignumber: bignum, Matrix, Complex } = math
const { eigs, complex, divide, dot, matrix, multiply, norm, size, subtract, bignumber: bignum, zeros, Matrix, Complex } = math

describe('eigs', function () {
it('only accepts a square matrix', function () {
Expand Down Expand Up @@ -82,6 +82,38 @@ describe('eigs', function () {
)
})

it('calculates eigenvalues and eigenvectors for 5x5 matrix', function () {
const m = zeros([5, 5])
m[4][3] = m[3][4] = m[3][2] = m[2][4] = 1

approx.deepEqual(eigs(m).values, [
0, 0,
complex(-0.6623589786223121, 0.5622795120622232),
complex(-0.6623589786223121, -0.5622795120622232),
1.3247179572446257
])

const expectedVecs = [
[ 0, 1, 0, 0, 0 ],
[ 1, 0, 0, 0, 0 ],
[ 0, 0, complex(0.11830597156369933, -0.031220615673570772), complex(-0.1200245154270954, -0.023772787955108215), complex(-0.9202478355596486, 0.3913360718714568) ],
[ 0, 0, complex(0.595491754174446, -0.7939890055659293), complex(-1.9907357758894604e-15, -7.492144846834677e-16), 0 ],
[ 0, 0, complex(1.4367985194861642e-30, 1.7021784687445796e-45), complex(0.6439057179284668, 0.7552578345627129), 0 ]
]

const orthogonalSize = (v, w) => v = norm(subtract(v, multiply(divide(dot(w, v), dot(w, w)), w)))

// inverse iteration is stochastic, check it multiple times
for (let i = 0; i < 5; i++) {
const { vectors } = eigs(m);

for (let j = 0; j < 5; j++) {
assert(orthogonalSize(vectors[j], expectedVecs[j]) < 0.5) // this is poor precision, what's wrong?
}
}

})

it('eigenvector check', function () {
const H = [[-4.78, -1.0, -2.59, -3.26, 4.24, 4.14],
[-1.0, -2.45, -0.92, -2.33, -4.68, 4.27],
Expand Down

0 comments on commit c99f3a0

Please sign in to comment.