feat: add/multiply arbitrarily many arguments; full cbrt(number)

Extends `add` and `multiply` generically so they can handle any number of
  arguments based on their binary implementations; they also pass a
  single argument through unchanged, and return 0 and 1, respectively, on
  no arguments.

  Modifies cbrt(number) so that when the returnTyping is `full`, returns
  all three complex cube roots.
This commit is contained in:
Glen Whitney 2025-12-12 11:19:25 -08:00
parent fbbc2502bd
commit b80b7832b7
6 changed files with 115 additions and 7 deletions

View file

@ -3,6 +3,7 @@ import {NumberT} from './NumberT.js'
import {OneOf, Returns, ReturnTyping, Undefined} from '#core/Type.js'
import {match} from '#core/TypePatterns.js'
import {Complex} from '#complex/Complex.js'
import {ReturnsAs} from '#generic/helpers.js'
const {conservative, full} = ReturnTyping
@ -15,7 +16,7 @@ export const add = [
match([NumberT, Undefined], Returns(NumberT, () => NaN))
]
export const divide = plain((a, b) => a / b)
export const cbrt = plain(a => {
const numberCbrt = a => {
if (a === 0) return a
const negate = a < 0
if (negate) a = -a
@ -25,7 +26,29 @@ export const cbrt = plain(a => {
result = (a / (result * result) + (2 * result)) / 3
}
return negate ? -result : result
}
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)
})
export const invert = plain(a => 1/a)
export const multiply = [
plain((a, b) => a * b),