feat: equality on Vector elementwise

This commit is contained in:
Glen Whitney 2025-05-01 20:00:24 -07:00
parent 7b799f7183
commit c3c2bbbf78
3 changed files with 107 additions and 12 deletions

View file

@ -1,6 +1,6 @@
import {Vector} from './Vector.js'
import {NotAType, Returns} from '#core/Type.js'
import {Any, match} from '#core/TypePatterns.js'
import {NotAType, Returns, Undefined} from '#core/Type.js'
import {Any, Optional, match} from '#core/TypePatterns.js'
import {BooleanT} from '#boolean/BooleanT.js'
export const deepEqual = [
@ -18,3 +18,74 @@ export const deepEqual = [
&& v.every((e, i) => compDeep(e, w[i]))))
})
]
export const indistinguishable = [
match([Vector, Any, Optional([Any, Any])], (math, [V, E, T]) => {
const VComp = V.Component
if (T.length === 0) { // no tolerances
const same = math.indistinguishable.resolve([VComp, E])
return Returns(
Vector(same.returns), (v, e) => v.map(f => same(f, e)))
}
const [[RT, AT]] = T
const same = math.indistinguishable.resolve([VComp, E, RT, AT])
return Returns(
Vector(same.returns),
(v, e, [[rT, aT]]) => v.map(f => same(f, e, rT, aT)))
}),
match([Any, Vector, Optional([Any, Any])], (math, [E, V, T]) => {
// reimplement to get other order in same so as not to assume
// same is symmetric, even though it probably is
const VComp = V.Component
if (T.length === 0) { // no tolerances
const same = math.indistinguishable.resolve([E, VComp])
return Returns(
Vector(same.returns), (e, v) => v.map(f => same(e, f)))
}
const [[RT, AT]] = T
const same = math.indistinguishable.resolve([E, VComp, RT, AT])
return Returns(
Vector(same.returns),
(e, v, [[rT, aT]]) => v.map(f => same(e, f, rT, aT)))
}),
match([Vector, Vector, Optional([Any, Any])], (math, [V, W, T]) => {
const VComp = V.Component
const WComp = W.Component
let same
let sameNoV
let sameNoW
if (T.length === 0) { // no tolerances
same = math.indistinguishable.resolve([VComp, WComp])
sameNoV = math.indistinguishable.resolve([Undefined, WComp])
sameNoW = math.indistinguishable.resolve([VComp, Undefined])
} else {
const [[RT, AT]] = T
same = math.indistinguishable.resolve([VComp, WComp, RT, AT])
sameNoV = math.indistinguishable.resolve([Undefined, WComp, RT, AT])
sameNoW = math.indistinguishable.resolve([VComp, Undefined, RT, AT])
}
return Returns(
Vector(same.returns),
(v, w, [tol = [0, 0]]) => {
const [rT, aT] = tol
const vInc = Number(v.length > 1)
const wInc = Number(w.length >= v.length || w.length > 1)
const retval = []
let vIx = 0
let wIx = 0
let remainder = vIx < v.length || wIx < w.length
while ((vInc && vIx < v.length)
|| (wInc && wIx < w.length)
) {
if (vIx >= v.length) {
retval.push(sameNoV(undefined, w[wIx], rT, aT))
} else if (wIx >= w.length) {
retval.push(sameNoW(v[vIx], undefined, rT, aT))
} else retval.push(same(v[vIx], w[wIx], rT, aT))
vIx += vInc
wIx += wInc
}
return retval
})
})
]