diff --git a/src/complex/conjugate.mjs b/src/complex/conjugate.mjs index b94495d..c81180e 100644 --- a/src/complex/conjugate.mjs +++ b/src/complex/conjugate.mjs @@ -1,9 +1,11 @@ +import Returns from '../core/Returns.mjs' export * from './Types/Complex.mjs' export const conjugate = { 'Complex': ({ + T, 'negate(T)': neg, 'complex(T,T)': cplx - }) => z => cplx(z.re, neg(z.im)) + }) => Returns(`Complex<${T}>`, z => cplx(z.re, neg(z.im))) } diff --git a/src/complex/equalTT.mjs b/src/complex/equalTT.mjs index 6899aa0..b979857 100644 --- a/src/complex/equalTT.mjs +++ b/src/complex/equalTT.mjs @@ -1,9 +1,10 @@ +import Returns from '../core/Returns.mjs' export * from './Types/Complex.mjs' export const equalTT = { 'Complex,Complex': ({ 'self(T,T)': me - }) => (w,z) => me(w.re, z.re) && me(w.im, z.im), + }) => Returns('boolean', (w, z) => me(w.re, z.re) && me(w.im, z.im)), // NOTE: Although I do not understand exactly why, with typed-function@3.0's // matching algorithm, the above template must come first to ensure the // most specific match to a template call. I.e, if one of the below @@ -11,16 +12,16 @@ export const equalTT = { // with (Complex>, Complex) (!, hopefully in some // future iteration typed-function will be smart enough to prefer // Complex, Complex. Possibly the problem is in Pocomath's bolted-on - // type resolution and the difficulty will go away when features are moved into - // typed-function. + // type resolution and the difficulty will go away when features are moved + // into typed-function. 'Complex,T': ({ 'isZero(T)': isZ, 'self(T,T)': eqReal - }) => (z, x) => eqReal(z.re, x) && isZ(z.im), + }) => Returns('boolean', (z, x) => eqReal(z.re, x) && isZ(z.im)), 'T,Complex': ({ 'isZero(T)': isZ, 'self(T,T)': eqReal - }) => (b, z) => eqReal(z.re, b) && isZ(z.im), + }) => Returns('boolean', (b, z) => eqReal(z.re, b) && isZ(z.im)), } diff --git a/src/complex/gcd.mjs b/src/complex/gcd.mjs index 30b7dad..be90e4a 100644 --- a/src/complex/gcd.mjs +++ b/src/complex/gcd.mjs @@ -1,5 +1,6 @@ import PocomathInstance from '../core/PocomathInstance.mjs' -import * as Complex from './Types/Complex.mjs' +import Returns from '../core/Returns.mjs' +import * as Complex from './Types/Complex.mjs' import gcdType from '../generic/gcdType.mjs' const gcdComplexRaw = {} @@ -9,15 +10,16 @@ const imps = { gcdComplexRaw, gcd: { // Only return gcds with positive real part 'Complex,Complex': ({ + T, 'gcdComplexRaw(Complex,Complex)': gcdRaw, 'sign(T)': sgn, 'one(T)': uno, 'negate(Complex)': neg - }) => (z,m) => { + }) => Returns(`Complex<${T}>`, (z,m) => { const raw = gcdRaw(z, m) if (sgn(raw.re) === uno(raw.re)) return raw return neg(raw) - } + }) } } diff --git a/src/complex/invert.mjs b/src/complex/invert.mjs index 2f68e43..ce1b932 100644 --- a/src/complex/invert.mjs +++ b/src/complex/invert.mjs @@ -1,14 +1,16 @@ +import Returns from '../core/Returns.mjs' export * from './Types/Complex.mjs' export const invert = { 'Complex': ({ + T, 'conjugate(Complex)': conj, 'absquare(Complex)': asq, 'complex(T,T)': cplx, 'divide(T,T)': div - }) => z => { + }) => Returns(`Complex<${T}>`, z => { const c = conj(z) const d = asq(z) return cplx(div(c.re, d), div(c.im, d)) - } + }) } diff --git a/src/complex/isZero.mjs b/src/complex/isZero.mjs index 01a2f51..3e10c9b 100644 --- a/src/complex/isZero.mjs +++ b/src/complex/isZero.mjs @@ -1,5 +1,7 @@ +import Returns from '../core/Returns.mjs' export * from './Types/Complex.mjs' export const isZero = { - 'Complex': ({'self(T)': me}) => z => me(z.re) && me(z.im) + 'Complex': ({'self(T)': me}) => Returns( + 'boolean', z => me(z.re) && me(z.im)) } diff --git a/src/complex/quaternion.mjs b/src/complex/quaternion.mjs index 4f35d30..a435395 100644 --- a/src/complex/quaternion.mjs +++ b/src/complex/quaternion.mjs @@ -1,5 +1,14 @@ +import Returns from '../core/Returns.mjs' export * from './Types/Complex.mjs' +// Might be nice to have type aliases! export const quaternion = { - 'T,T,T,T': ({complex}) => (r,i,j,k) => complex(complex(r,j), complex(i,k)) + 'T,T,T,T': ({ + T, + 'complex(T,T)': cplxT, + 'complex(Complex,Complex)': quat + }) => Returns( + `Complex>`, + (r,i,j,k) => quat(cplxT(r,j), cplxT(i,k)) + ) } diff --git a/src/complex/quotient.mjs b/src/complex/quotient.mjs index 32299ca..6b53de1 100644 --- a/src/complex/quotient.mjs +++ b/src/complex/quotient.mjs @@ -1,7 +1,9 @@ +import Returns from '../core/Returns.mjs' export * from './roundquotient.mjs' export const quotient = { 'Complex,Complex': ({ + T, 'roundquotient(Complex,Complex)': rq - }) => (w,z) => rq(w,z) + }) => Returns(`Complex<${T}>`, (w,z) => rq(w,z)) } diff --git a/src/complex/roundquotient.mjs b/src/complex/roundquotient.mjs index 5c25765..474077f 100644 --- a/src/complex/roundquotient.mjs +++ b/src/complex/roundquotient.mjs @@ -1,17 +1,19 @@ +import Returns from '../core/Returns.mjs' export * from './Types/Complex.mjs' export const roundquotient = { 'Complex,Complex': ({ + T, 'isZero(Complex)': isZ, 'conjugate(Complex)': conj, 'multiply(Complex,Complex)': mult, 'absquare(Complex)': asq, 'self(T,T)': me, 'complex(T,T)': cplx - }) => (n,d) => { + }) => Returns(`Complex<${T}>`, (n,d) => { if (isZ(d)) return d const cnum = mult(n, conj(d)) const dreal = asq(d) return cplx(me(cnum.re, dreal), me(cnum.im, dreal)) - } + }) } diff --git a/test/_pocomath.mjs b/test/_pocomath.mjs index 549e6ba..4a63d55 100644 --- a/test/_pocomath.mjs +++ b/test/_pocomath.mjs @@ -49,6 +49,12 @@ describe('The default full pocomath instance "math"', () => { math.abs(math.complex(2,1)) //TODO: ditto assert.strictEqual( math.returnTypeOf('abs','Complex'), 'number') + math.multiply(math.quaternion(1,1,1,1), math.quaternion(1,-1,1,-1)) // dit + const quatType = math.returnTypeOf( + 'quaternion', 'NumInt,NumInt,NumInt,NumInt') + assert.strictEqual(quatType, 'Complex>') + assert.strictEqual( + math.returnTypeOf('multiply', quatType + ',' + quatType), quatType) }) it('can subtract numbers', () => {