diff --git a/src/number/arithmetic.js b/src/number/arithmetic.js index 71d3d6e..dd03106 100644 --- a/src/number/arithmetic.js +++ b/src/number/arithmetic.js @@ -49,5 +49,10 @@ export const sqrt = match(NumberT, (math, _N, strategy) => { }) }) -export const subtract = plain((a, b) => a - b) +export const subtract = [ + plain((a, b) => a - b), + match([Undefined, NumberT], Returns(NumberT, () => NaN)), + match([NumberT, Undefined], Returns(NumberT, () => NaN)) +] + export const quotient = plain((a,b) => Math.floor(a/b)) diff --git a/src/vector/__test__/arithmetic.spec.js b/src/vector/__test__/arithmetic.spec.js index 4ca9c54..77337b3 100644 --- a/src/vector/__test__/arithmetic.spec.js +++ b/src/vector/__test__/arithmetic.spec.js @@ -24,6 +24,12 @@ describe('Vector arithmetic functions', () => { assert.deepStrictEqual( add([[1, 2], [4, 2]], [0, -1]), [[1, 1], [4, 1]]) }) + it('negates a vector', () => { + assert.deepStrictEqual(math.negate([-3, 4, -5]), [3, -4, 5]) + }) + it('subtracts vectors', () => { + assert.deepStrictEqual(math.subtract([-3, 4, -5], [8, 0, 2]), [-11, 4, -7]) + }) it('computes the sum of a vector', () => { const sum = math.sum assert.strictEqual(sum([-3, 4, -5]), -4) diff --git a/src/vector/arithmetic.js b/src/vector/arithmetic.js index 1ca31bb..4803148 100644 --- a/src/vector/arithmetic.js +++ b/src/vector/arithmetic.js @@ -16,6 +16,9 @@ export const normsq = match(Vector, (math, V) => { export const abs = promoteUnary('abs') export const add = promoteBinary('add') +export const negate = promoteUnary('negate') +export const subtract = promoteBinary('subtract') + export const sum = match(Vector, (math, V) => { const add = math.add.resolve([V.Component, V.Component]) const haszero = math.haszero(V.Component)