feat(gcd,lcm): Allow arbitrarily many arguments

And add an 'associate' predicate for two complex numbers to check the results
This commit is contained in:
Glen Whitney 2022-08-02 10:32:06 -07:00
parent 1b8314c0cc
commit 880efac15b
5 changed files with 31 additions and 0 deletions

17
src/complex/associate.mjs Normal file
View File

@ -0,0 +1,17 @@
export * from './Types/Complex.mjs'
/* Returns true if w is z multiplied by a complex unit */
export const associate = {
'Complex,Complex': ({
'multiply(Complex,Complex)': times,
'equal(Complex,Complex)': eq,
zero,
one,
complex,
'negate(Complex)': neg
}) => (w,z) => {
if (eq(w,z) || eq(w,neg(z))) return true
const ti = times(z, complex(zero(z.re), one(z.im)))
return eq(w,ti) || eq(w,neg(ti))
}
}

View File

@ -3,6 +3,7 @@ export * from './Types/Complex.mjs'
export {abs} from './abs.mjs'
export {absquare} from './absquare.mjs'
export {add} from './add.mjs'
export {associate} from './associate.mjs'
export {conjugate} from './conjugate.mjs'
export {complex} from './complex.mjs'
export {equal} from './equal.mjs'

View File

@ -3,6 +3,7 @@ import {reducingOperation} from './reducingOperation.mjs'
export * from './Types/generic.mjs'
export const add = reducingOperation
export const gcd = reducingOperation
export {lcm} from './lcm.mjs'
export {mean} from './mean.mjs'
export {mod} from './mod.mjs'

View File

@ -1,3 +1,5 @@
import {reducingOperation} from './reducingOperation.mjs'
export const lcm = {
'T,T': ({
'multiply(T,T)': multT,
@ -5,3 +7,4 @@ export const lcm = {
'gcd(T,T)': gcdT
}) => (a,b) => multT(quotT(a, gcdT(a,b)), b)
}
Object.assign(lcm, reducingOperation)

View File

@ -103,4 +103,13 @@ describe('The default full pocomath instance "math"', () => {
assert.strictEqual(math.choose(21n, 2n), 210n)
})
it('calculates multi-way gcds and lcms', () => {
assert.strictEqual(math.gcd(30,105,42), 3)
assert.ok(
math.associate(
math.lcm(
math.complex(2n,1n), math.complex(1n,1n), math.complex(0n,1n)),
math.complex(1n,3n)))
})
})