All checks were successful
/ test (push) Successful in 17s
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>
27 lines
872 B
JavaScript
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))
|