diff --git a/packages/common/src/complex.ts b/packages/common/src/complex.ts index 96c287745..6d0438f44 100644 --- a/packages/common/src/complex.ts +++ b/packages/common/src/complex.ts @@ -91,7 +91,7 @@ export class Complex { } isApproximatelyEqualTo(other: number | Complex | unknown, epsilon: number): boolean { - if (other instanceof Complex || typeof other === 'number') { + if (typeof other === 'number' || other instanceof Complex) { const d = this.minus(Complex.from(other)) return Math.abs(d.real) <= epsilon && Math.abs(d.imag) <= epsilon && d.abs() <= epsilon } diff --git a/packages/common/test/complex.test.ts b/packages/common/test/complex.test.ts index 29de112d3..e932d8ceb 100644 --- a/packages/common/test/complex.test.ts +++ b/packages/common/test/complex.test.ts @@ -360,4 +360,66 @@ describe('Complex', () => { }) }) }) + + describe('norm2', () => { + describe('0+0i', () => { + test('equal 0', () => { + expect(Complex.ZERO.norm2()).toBe(0) + }) + }) + + describe('1+0i', () => { + test('equal 1', () => { + expect(Complex.ONE.norm2()).toBe(1) + }) + }) + + describe('0+i', () => { + test('equal 1', () => { + expect(Complex.I.norm2()).toBe(1) + }) + }) + + describe('1+i', () => { + test('equal 2', () => { + expect(new Complex(1, 1).norm2()).toBe(2) + }) + }) + + describe('2+0i', () => { + test('equal 4', () => { + expect(new Complex(2, 0).norm2()).toBe(4) + }) + }) + + describe('0+2i', () => { + test('equal 4', () => { + expect(new Complex(0, 2).norm2()).toBe(4) + }) + }) + + describe('-2+0i', () => { + test('equal 4', () => { + expect(new Complex(-2, 0).norm2()).toBe(4) + }) + }) + + describe('0-2i', () => { + test('equal 4', () => { + expect(new Complex(0, -2).norm2()).toBe(4) + }) + }) + + describe('2+3i', () => { + test('equal 13', () => { + expect(new Complex(2, 3).norm2()).toBe(13) + }) + }) + + describe('-3-4i', () => { + test('equal 25', () => { + expect(new Complex(-3, -4).norm2()).toBe(25) + }) + }) + }) })