Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eigs): Handle matrices with complex entries #2445

Merged
merged 2 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/function/matrix/eigs/complexEigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ export function createComplexEigs ({ addScalar, subtract, flatten, multiply, mul
const big = type === 'BigNumber'
const cplx = type === 'Complex'

const zero = big ? bignumber(0) : cplx ? complex(0) : 0
const realzero = big ? bignumber(0) : 0
const one = big ? bignumber(1) : cplx ? complex(1) : 1
const realone = big ? bignumber(1) : 1

// base of the floating-point arithmetic
const radix = big ? bignumber(10) : 2
Expand All @@ -87,12 +88,12 @@ export function createComplexEigs ({ addScalar, subtract, flatten, multiply, mul
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
let colNorm = realzero
let rowNorm = realzero

for (let j = 0; j < N; j++) {
if (i === j) continue
const c = abs(arr[i][j])
const c = abs(arr[i][j]) // should be real
colNorm = addScalar(colNorm, c)
rowNorm = addScalar(rowNorm, c)
}
Expand All @@ -102,7 +103,7 @@ export function createComplexEigs ({ addScalar, subtract, flatten, multiply, mul
// (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 f = realone
let c = colNorm

const rowDivRadix = divideScalar(rowNorm, radix)
Expand Down
9 changes: 9 additions & 0 deletions test/unit-tests/function/matrix/eigs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ describe('eigs', function () {
)
})

it('calculates eigenvalues for 2x2 matrix with complex entries', () => {
approx.deepEqual(
eigs([[3, -2], [complex(4, 2), -1]]).values,
[complex(0.08982028, 2.197368227), complex(1.91017972, -2.197368227)])
approx.deepEqual(
eigs([[2, -2], [complex(0, 2), complex(0, -2)]]).values,
[0, complex(2, -2)])
})

it('calculates eigenvalues for 3x3 and 4x4 matrix', function () {
// 3x3 test and 4x4
approx.deepEqual(eigs(
Expand Down