feat: Template types (#45)
Includes a full implementation of a type-homogeneous Tuple type, using the template types feature, as a demonstration/check of its operation. Co-authored-by: Glen Whitney <glen@studioinfinity.org> Reviewed-on: #45
This commit is contained in:
parent
fd32ee1f10
commit
845a2354c9
28 changed files with 920 additions and 129 deletions
|
@ -7,16 +7,16 @@ export const add = {
|
|||
*/
|
||||
'Complex,number': ({
|
||||
'self(number,number)': addNum,
|
||||
'complex(any,any)': cplx
|
||||
'complex(number,number)': cplx
|
||||
}) => (z,x) => cplx(addNum(z.re, x), z.im),
|
||||
|
||||
'Complex,bigint': ({
|
||||
'self(bigint,bigint)': addBigInt,
|
||||
'complex(any,any)': cplx
|
||||
'complex(bigint,bigint)': cplx
|
||||
}) => (z,x) => cplx(addBigInt(z.re, x), z.im),
|
||||
|
||||
'Complex,Complex': ({
|
||||
self,
|
||||
'complex(any,any)': cplx
|
||||
}) => (w,z) => cplx(self(w.re, z.re), self(w.im, z.im))
|
||||
complex
|
||||
}) => (w,z) => complex(self(w.re, z.re), self(w.im, z.im))
|
||||
}
|
||||
|
|
17
src/complex/associate.mjs
Normal file
17
src/complex/associate.mjs
Normal 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,
|
||||
'equalTT(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))
|
||||
}
|
||||
}
|
|
@ -9,7 +9,8 @@ export const complex = {
|
|||
'undefined': () => u => u,
|
||||
'undefined,any': () => (u, y) => u,
|
||||
'any,undefined': () => (x, u) => u,
|
||||
'any,any': () => (x, y) => ({re: x, im: y}),
|
||||
'undefined,undefined': () => (u, v) => u,
|
||||
'T,T': () => (x, y) => ({re: x, im: y}),
|
||||
/* Take advantage of conversions in typed-function */
|
||||
Complex: () => z => z
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
export * from './Types/Complex.mjs'
|
||||
|
||||
export const equal = {
|
||||
export const equalTT = {
|
||||
'Complex,number': ({
|
||||
'isZero(number)': isZ,
|
||||
'self(number,number)': eqNum
|
|
@ -3,15 +3,18 @@ import * as Complex from './Types/Complex.mjs'
|
|||
import gcdType from '../generic/gcdType.mjs'
|
||||
|
||||
const imps = {
|
||||
gcdComplexRaw: gcdType('Complex'),
|
||||
gcdGIRaw: gcdType('GaussianInteger'),
|
||||
gcd: { // Only return gcds with positive real part
|
||||
'Complex, Complex': ({gcdComplexRaw, sign, one, negate}) => (z,m) => {
|
||||
const raw = gcdComplexRaw(z, m)
|
||||
if (sign(raw.re) === one(raw.re)) return raw
|
||||
return negate(raw)
|
||||
'GaussianInteger,GaussianInteger': ({
|
||||
'gcdGIRaw(GaussianInteger,GaussianInteger)': gcdRaw,
|
||||
'sign(bigint)': sgn,
|
||||
'negate(GaussianInteger)': neg
|
||||
}) => (z,m) => {
|
||||
const raw = gcdRaw(z, m)
|
||||
if (sgn(raw.re) === 1n) return raw
|
||||
return neg(raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const gcd = PocomathInstance.merge(Complex, imps)
|
||||
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
import gcdType from '../generic/gcdType.mjs'
|
||||
|
||||
export * from './Types/Complex.mjs'
|
||||
|
||||
export {abs} from './abs.mjs'
|
||||
export {absquare} from './absquare.mjs'
|
||||
export {add} from './add.mjs'
|
||||
export {conjugate} from './conjugate.mjs'
|
||||
export {associate} from './associate.mjs'
|
||||
export {complex} from './complex.mjs'
|
||||
export {equal} from './equal.mjs'
|
||||
export {conjugate} from './conjugate.mjs'
|
||||
export {equalTT} from './equalTT.mjs'
|
||||
export {gcd} from './gcd.mjs'
|
||||
export {invert} from './invert.mjs'
|
||||
export {isZero} from './isZero.mjs'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue