feat: Allow self-reference in implementations
And use it to define negate and add for Complex numbers in a way that is independent of component types. Also add a bigint type and verify that pocomath will then handle Gaussian integers "for free".
This commit is contained in:
parent
77b04fbdbb
commit
66cbccfbbe
13 changed files with 99 additions and 21 deletions
|
@ -29,5 +29,27 @@ describe('The default full pocomath instance "math"', () => {
|
|||
assert.deepStrictEqual(math.complex(2,3), norm13)
|
||||
assert.deepStrictEqual(math.complex(2), math.complex(2,0))
|
||||
assert.deepStrictEqual(math.add(2, math.complex(0,3)), norm13)
|
||||
assert.deepStrictEqual(
|
||||
math.subtract(16, math.add(3, math.complex(0,4), 2)),
|
||||
math.complex(11, -4))
|
||||
assert.strictEqual(math.negate(math.complex(3, 8)).im, -8)
|
||||
})
|
||||
|
||||
it('handles bigints', () => {
|
||||
assert.strictEqual(math.negate(5n), -5n)
|
||||
assert.strictEqual(math.subtract(12n, 5n), 7n)
|
||||
assert.strictEqual(math.add(15n, 25n, 35n), 75n)
|
||||
assert.strictEqual(math.add(10n, math.negate(3n)), 7n)
|
||||
})
|
||||
|
||||
it('handles Gaussian integers', () => {
|
||||
const norm13n = {re: 2n, im: 3n}
|
||||
assert.deepStrictEqual(math.complex(2n,3n), norm13n)
|
||||
assert.deepStrictEqual(math.complex(2n), math.complex(2n, 0n))
|
||||
assert.deepStrictEqual(math.add(2n, math.complex(0n, 3n)), norm13n)
|
||||
assert.deepStrictEqual(
|
||||
math.subtract(16n, math.add(3n, math.complex(0n,4n), 2n)),
|
||||
math.complex(11n, -4n))
|
||||
assert.strictEqual(math.negate(math.complex(3n, 8n)).im, -8n)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import assert from 'assert'
|
||||
import math from '../pocomath.mjs'
|
||||
import typed from 'typed-function'
|
||||
import PocomathInstance from '../PocomathInstance.mjs'
|
||||
import * as numbers from '../number/all.mjs'
|
||||
|
@ -17,5 +18,9 @@ describe('A custom instance', () => {
|
|||
assert.strictEqual(bw.subtract(16, bw.add(3,4,2)), 7)
|
||||
assert.strictEqual(bw.negate('8'), -8)
|
||||
assert.deepStrictEqual(bw.add(bw.complex(1,3), 1), {re: 2, im: 3})
|
||||
assert.deepStrictEqual(
|
||||
bw.subtract(16, bw.add(3, bw.complex(0,4), 2)),
|
||||
math.complex(11, -4)) // note both instances coexist
|
||||
assert.deepStrictEqual(bw.negate(math.complex(3, '8')).im, -8)
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue