diff --git a/src/complex/helpers.js b/src/complex/helpers.js index 2f3a6ec..c6c4aac 100644 --- a/src/complex/helpers.js +++ b/src/complex/helpers.js @@ -1,5 +1,5 @@ import {Complex} from './Complex.js' -import {OneOf, Returns, ReturnTyping} from '#core/Type.js' +import {ReturnTyping} from '#core/Type.js' import {match} from '#core/TypePatterns.js' import {ReturnsAs} from '#generic/helpers.js' diff --git a/src/complex/type.js b/src/complex/type.js index 6df2bec..cc3ee58 100644 --- a/src/complex/type.js +++ b/src/complex/type.js @@ -35,7 +35,7 @@ export const arg = match( /* Returns true if w is z multiplied by a complex unit */ export const associate = match( [Complex, Complex], - (math, [W, Z], strategy) => { + (math, [W, Z]) => { if (Z.Component.complex) { throw new Error( `The group of units of type ${Z} is not yet implemented`) diff --git a/src/generic/__test__/arithmetic.spec.js b/src/generic/__test__/arithmetic.spec.js index 6ccb17c..e4a71c0 100644 --- a/src/generic/__test__/arithmetic.spec.js +++ b/src/generic/__test__/arithmetic.spec.js @@ -1,11 +1,18 @@ import assert from 'assert' import math from '#nanomath' +import {ReturnTyping} from '#core/Type.js' + +const {Complex, NumberT} = math.types describe('generic arithmetic', () => { it('squares anything', () => { - assert.strictEqual(math.square(7), 49) - assert.strictEqual( - math.square.resolve([math.types.NumberT]).returns, - math.types.NumberT) + const sq = math.square + assert.strictEqual(sq(7), 49) + assert.strictEqual(math.square.resolve([NumberT]).returns, NumberT) + assert.deepStrictEqual(sq(math.complex(3, 4)), math.complex(-7, 24)) + const eyes = math.complex(0, 2) + assert.strictEqual(sq(eyes), -4) + const sqFull = math.square.resolve(Complex(NumberT), ReturnTyping.full) + assert.deepStrictEqual(sqFull(eyes), math.complex(-4, 0)) }) }) diff --git a/src/generic/arithmetic.js b/src/generic/arithmetic.js index cf67fdc..5168111 100644 --- a/src/generic/arithmetic.js +++ b/src/generic/arithmetic.js @@ -2,7 +2,7 @@ import {Returns} from '#core/Type.js' import {match, Any} from '#core/TypePatterns.js' export const conj = match(Any, (_math, T) => Returns(T, a => a)) -export const square = match(Any, (math, T) => { - const mult = math.multiply.resolve([T, T]) +export const square = match(Any, (math, T, strategy) => { + const mult = math.multiply.resolve([T, T], strategy) return Returns(mult.returns, a => mult(a, a)) }) diff --git a/src/generic/relational.js b/src/generic/relational.js index bd23f92..e128cc0 100644 --- a/src/generic/relational.js +++ b/src/generic/relational.js @@ -1,8 +1,10 @@ import {ReturnsAs} from './helpers.js' -import {Returns} from '#core/Type.js' +import {Returns, ReturnTyping} from '#core/Type.js' import {Any, Passthru, match, matched} from '#core/TypePatterns.js' import {boolnum} from '#number/helpers.js' +const {full} = ReturnTyping + export const equal = match([Any, Any], (math, [T, U]) => { // Finding the correct signature of `indistinguishable` to use for // testing (approximate) equality is tricky, because T or U might @@ -12,7 +14,7 @@ export const equal = match([Any, Any], (math, [T, U]) => { // the matching type, and then we look up with tolerances. let exactChecker try { - exactChecker = math.indistinguishable.resolve([T, U]) + exactChecker = math.indistinguishable.resolve([T, U], full) } catch { // can't compare, so no way they can be equal return boolnum(() => false)(math) } @@ -25,7 +27,7 @@ export const equal = match([Any, Any], (math, [T, U]) => { const {relTol, absTol} = typeConfig const RT = math.typeOf(relTol) const AT = math.typeOf(absTol) - const approx = math.indistinguishable.resolve([T, U, RT, AT]) + const approx = math.indistinguishable.resolve([T, U, RT, AT], full) return ReturnsAs( approx, (t, u) => approx(t, u, relTol, absTol)) } catch {} // fall through to case with no tolerances