Skip to content

Commit

Permalink
fix(eigs): Handle matrices with complex entries (#2445)
Browse files Browse the repository at this point in the history
This change fixes a typing problem in complexEigs.js in which
   real-valued norms were inadvertently being typed as complex numbers.

   Resolves #2439

Co-authored-by: Jos de Jong <[email protected]>
  • Loading branch information
gwhitney and josdejong authored Mar 1, 2022
1 parent dd1d08a commit 9d4ea92
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
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

0 comments on commit 9d4ea92

Please sign in to comment.