feat: vector negate and subtract
This commit is contained in:
parent
cb3a93dd1c
commit
f398454d59
3 changed files with 15 additions and 1 deletions
|
@ -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))
|
export const quotient = plain((a,b) => Math.floor(a/b))
|
||||||
|
|
|
@ -24,6 +24,12 @@ describe('Vector arithmetic functions', () => {
|
||||||
assert.deepStrictEqual(
|
assert.deepStrictEqual(
|
||||||
add([[1, 2], [4, 2]], [0, -1]), [[1, 1], [4, 1]])
|
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', () => {
|
it('computes the sum of a vector', () => {
|
||||||
const sum = math.sum
|
const sum = math.sum
|
||||||
assert.strictEqual(sum([-3, 4, -5]), -4)
|
assert.strictEqual(sum([-3, 4, -5]), -4)
|
||||||
|
|
|
@ -16,6 +16,9 @@ export const normsq = match(Vector, (math, V) => {
|
||||||
export const abs = promoteUnary('abs')
|
export const abs = promoteUnary('abs')
|
||||||
export const add = promoteBinary('add')
|
export const add = promoteBinary('add')
|
||||||
|
|
||||||
|
export const negate = promoteUnary('negate')
|
||||||
|
export const subtract = promoteBinary('subtract')
|
||||||
|
|
||||||
export const sum = match(Vector, (math, V) => {
|
export const sum = match(Vector, (math, V) => {
|
||||||
const add = math.add.resolve([V.Component, V.Component])
|
const add = math.add.resolve([V.Component, V.Component])
|
||||||
const haszero = math.haszero(V.Component)
|
const haszero = math.haszero(V.Component)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue