feat: Implement Vector type (#28)
All checks were successful
/ test (push) Successful in 18s

The Vector type is simply a generic type for built-in JavaScript Arrays. The parameter type is the type of all of the entries of the Array. The Vector type also supports inhomogeneous arrays by using the special type `Unknown` as the argument type, but note that when computing with inhomogeneous arrays, method dispatch must be performed separately for every entry in a calculation, making all operations considerably slower than on homogeneous Vector instances.

Note also that arithmetic operations on nested Vectors, e.g. `Vector(Vector(NumberT))` are defined so as to interpret such entities as ordinary matrices, represented in row-major format (i.e., the component `Vector(NumberT)` items of such an entity are the _rows_ of the matrix.

Reviewed-on: #28
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
This commit is contained in:
Glen Whitney 2025-05-07 00:03:49 +00:00 committed by Glen Whitney
parent 0765ba7202
commit 95d81d0338
47 changed files with 1204 additions and 122 deletions

View file

@ -1,14 +1,19 @@
import {plain} from './helpers.js'
import {NumberT} from './NumberT.js'
import {OneOf, Returns, ReturnTyping} from '#core/Type.js'
import {OneOf, Returns, ReturnTyping, Undefined} from '#core/Type.js'
import {match} from '#core/TypePatterns.js'
import {Complex} from '#complex/Complex.js'
const {conservative, full} = ReturnTyping
export const abs = plain(Math.abs)
export const absquare = plain(a => a*a)
export const add = plain((a, b) => a + b)
export const norm = abs
export const normsq = plain(a => a*a)
export const add = [
plain((a, b) => a + b),
match([Undefined, NumberT], Returns(NumberT, () => NaN)),
match([NumberT, Undefined], Returns(NumberT, () => NaN))
]
export const divide = plain((a, b) => a / b)
export const cbrt = plain(a => {
if (a === 0) return a
@ -22,7 +27,11 @@ export const cbrt = plain(a => {
return negate ? -result : result
})
export const invert = plain(a => 1/a)
export const multiply = plain((a, b) => a * b)
export const multiply = [
plain((a, b) => a * b),
match([Undefined, NumberT], Returns(NumberT, () => NaN)),
match([NumberT, Undefined], Returns(NumberT, () => NaN))
]
export const negate = plain(a => -a)
export const sqrt = match(NumberT, (math, _N, strategy) => {
@ -44,5 +53,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))