Merge pull request 'feat(Complex): support division' (#40) from complex_division into main

Reviewed-on: #40
This commit is contained in:
Glen Whitney 2022-07-31 18:09:52 +00:00
commit 2609310b8e
3 changed files with 19 additions and 0 deletions

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 {complex} from './complex.mjs'
export {gcd} from './gcd.mjs'
export {invert} from './invert.mjs'
export {isZero} from './isZero.mjs'
export {multiply} from './multiply.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'
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', () => {
assert.deepStrictEqual(math.sqrt(math.complex(1,0)), 1)
assert.deepStrictEqual(