feat(Complex): support division #40

Merged
glen merged 1 commits from complex_division into main 2022-07-31 18:09:53 +00:00
3 changed files with 19 additions and 0 deletions
src/complex
test/complex

9
src/complex/invert.mjs Normal file
View File

@ -0,0 +1,9 @@
export * from './Types/Complex.mjs'
export const invert = {
Complex: ({conjugate, absquare, complex, divide}) => z => {
const c = conjugate(z)
const d = absquare(z)
return complex(divide(c.re, d), divide(c.im, d))
}
}

View File

@ -8,6 +8,7 @@ export {add} from './add.mjs'
export {conjugate} from './conjugate.mjs' export {conjugate} from './conjugate.mjs'
export {complex} from './complex.mjs' export {complex} from './complex.mjs'
export {gcd} from './gcd.mjs' export {gcd} from './gcd.mjs'
export {invert} from './invert.mjs'
export {isZero} from './isZero.mjs' export {isZero} from './isZero.mjs'
export {multiply} from './multiply.mjs' export {multiply} from './multiply.mjs'
export {negate} from './negate.mjs' export {negate} from './negate.mjs'

View File

@ -4,6 +4,15 @@ import PocomathInstance from '../../src/core/PocomathInstance.mjs'
import * as complexSqrt from '../../src/complex/sqrt.mjs' import * as complexSqrt from '../../src/complex/sqrt.mjs'
describe('complex', () => { describe('complex', () => {
it('supports division', () => {
assert.deepStrictEqual(
math.divide(math.complex(3,2), math.complex(0,1)),
math.complex(2,-3))
const reciprocal = math.divide(1, math.complex(1,3))
assert.strictEqual(reciprocal.re, 0.1)
assert.ok(Math.abs(reciprocal.im + 0.3) < 1e-13)
})
it('supports sqrt', () => { it('supports sqrt', () => {
assert.deepStrictEqual(math.sqrt(math.complex(1,0)), 1) assert.deepStrictEqual(math.sqrt(math.complex(1,0)), 1)
assert.deepStrictEqual( assert.deepStrictEqual(