nanomath/src/generic/arithmetic.js
Glen Whitney 8d3e6e70cb
All checks were successful
/ test (push) Successful in 17s
feat: Fully define complex cube root (#39)
Also extends add and multiply to allow multiple arguments.

Resolves #29.

Reviewed-on: #39
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
2025-12-13 07:34:01 +00:00

27 lines
872 B
JavaScript

import {iterateBinary, ReturnsAs} from './helpers.js'
import {plain} from "#number/helpers.js"
import {Returns, ReturnType, ReturnTyping} from '#core/Type.js'
import {match, Any} from '#core/TypePatterns.js'
export const norm = match(Any, (math, T) => {
const normsq = math.normsq.resolve(T)
const sqrt = math.sqrt.resolve(
ReturnType(normsq), ReturnTyping.conservative)
return ReturnsAs(sqrt, t => sqrt(normsq(t)))
})
export const abs = norm // coincide for most types (scalars)
export const conj = match(Any, (_math, T) => Returns(T, t => t))
export const square = match(Any, (math, T, strategy) => {
const mult = math.multiply.resolve([T, T], strategy)
return ReturnsAs(mult, t => mult(t, t))
})
export const add = iterateBinary('add')
add.push(plain(() => 0))
export const multiply = iterateBinary('multiply')
multiply.push(plain(() => 1))