2025-04-02 11:22:53 -07:00
|
|
|
import {plain} from './helpers.js'
|
2025-04-28 16:29:33 +00:00
|
|
|
import {NumberT} from './NumberT.js'
|
2025-05-07 00:03:49 +00:00
|
|
|
import {OneOf, Returns, ReturnTyping, Undefined} from '#core/Type.js'
|
2025-04-28 16:29:33 +00:00
|
|
|
import {match} from '#core/TypePatterns.js'
|
|
|
|
|
import {Complex} from '#complex/Complex.js'
|
2025-12-12 11:19:25 -08:00
|
|
|
import {ReturnsAs} from '#generic/helpers.js'
|
2025-04-28 16:29:33 +00:00
|
|
|
|
|
|
|
|
const {conservative, full} = ReturnTyping
|
2025-03-29 17:12:35 -07:00
|
|
|
|
|
|
|
|
export const abs = plain(Math.abs)
|
2025-05-07 00:03:49 +00:00
|
|
|
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))
|
|
|
|
|
]
|
2025-03-29 17:12:35 -07:00
|
|
|
export const divide = plain((a, b) => a / b)
|
2025-12-12 11:19:25 -08:00
|
|
|
const numberCbrt = a => {
|
2025-03-29 17:12:35 -07:00
|
|
|
if (a === 0) return a
|
|
|
|
|
const negate = a < 0
|
|
|
|
|
if (negate) a = -a
|
|
|
|
|
let result = a
|
|
|
|
|
if (isFinite(a)) {
|
2025-04-07 16:18:46 +00:00
|
|
|
result = Math.exp(Math.log(result) / 3)
|
2025-03-29 17:12:35 -07:00
|
|
|
result = (a / (result * result) + (2 * result)) / 3
|
|
|
|
|
}
|
|
|
|
|
return negate ? -result : result
|
2025-12-12 11:19:25 -08:00
|
|
|
}
|
|
|
|
|
const cbrtIm = Math.sqrt(3) / 2
|
|
|
|
|
export const cbrt = match(NumberT, (math, _N, strategy) => {
|
|
|
|
|
if (strategy === full && math.types.Complex && math.types.Vector) {
|
|
|
|
|
// return vector of all three complex roots, real one first
|
|
|
|
|
const C = Complex(NumberT)
|
|
|
|
|
const vec = math.vector.resolve([C, C, C])
|
|
|
|
|
const promote = math.complex.resolve([NumberT])
|
|
|
|
|
const cplx = math.complex.resolve([NumberT, NumberT])
|
|
|
|
|
return ReturnsAs(vec, x => {
|
|
|
|
|
const realroot = numberCbrt(x)
|
|
|
|
|
if (!isFinite(realroot)) {
|
|
|
|
|
const full = cplx(realroot, realroot)
|
|
|
|
|
return(promote(realroot), full, full)
|
|
|
|
|
}
|
|
|
|
|
return vec(promote(realroot),
|
|
|
|
|
cplx(-realroot / 2, realroot * cbrtIm),
|
|
|
|
|
cplx(-realroot / 2, -realroot * cbrtIm))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return Returns(NumberT, numberCbrt)
|
2025-03-29 17:12:35 -07:00
|
|
|
})
|
2025-12-12 11:19:25 -08:00
|
|
|
|
2025-03-29 17:12:35 -07:00
|
|
|
export const invert = plain(a => 1/a)
|
2025-05-07 00:03:49 +00:00
|
|
|
export const multiply = [
|
|
|
|
|
plain((a, b) => a * b),
|
|
|
|
|
match([Undefined, NumberT], Returns(NumberT, () => NaN)),
|
|
|
|
|
match([NumberT, Undefined], Returns(NumberT, () => NaN))
|
|
|
|
|
]
|
2025-03-29 17:12:35 -07:00
|
|
|
export const negate = plain(a => -a)
|
2025-04-28 16:29:33 +00:00
|
|
|
|
|
|
|
|
export const sqrt = match(NumberT, (math, _N, strategy) => {
|
|
|
|
|
if (!math.types.Complex || strategy === conservative) {
|
|
|
|
|
return Returns(NumberT, Math.sqrt)
|
|
|
|
|
}
|
|
|
|
|
const cplx = math.complex.resolve([NumberT, NumberT], full)
|
|
|
|
|
if (strategy === full) {
|
|
|
|
|
const cnan = math.nan(Complex(NumberT))
|
|
|
|
|
return Returns(Complex(NumberT), a => {
|
|
|
|
|
if (isNaN(a)) return cnan
|
|
|
|
|
return a >= 0 ? cplx(Math.sqrt(a), 0) : cplx(0, Math.sqrt(-a))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
// strategy === free, return "best" type
|
|
|
|
|
return Returns(OneOf(NumberT, Complex(NumberT)), a => {
|
|
|
|
|
if (isNaN(a)) return NaN
|
|
|
|
|
return a >= 0 ? Math.sqrt(a) : cplx(0, Math.sqrt(-a))
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2025-05-07 00:03:49 +00:00
|
|
|
export const subtract = [
|
|
|
|
|
plain((a, b) => a - b),
|
|
|
|
|
match([Undefined, NumberT], Returns(NumberT, () => NaN)),
|
|
|
|
|
match([NumberT, Undefined], Returns(NumberT, () => NaN))
|
|
|
|
|
]
|
|
|
|
|
|
2025-03-29 17:12:35 -07:00
|
|
|
export const quotient = plain((a,b) => Math.floor(a/b))
|