feat: Implement Vector type #28

Merged
glen merged 12 commits from vector into main 2025-05-07 00:03:50 +00:00
3 changed files with 15 additions and 1 deletions
Showing only changes of commit f398454d59 - Show all commits

View file

@ -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))

View file

@ -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)

View file

@ -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)