nanomath/src/complex/type.js

31 lines
933 B
JavaScript
Raw Normal View History

import {Complex} from './Complex.js'
import {Returns} from "#core/Type.js"
import {Any, match} from "#core/TypePatterns.js"
import {NumberT} from '#number/NumberT.js'
export const complex = [
match(Any, (math, T) => {
const z = math.zero(T)
if (math.hasnan(T)) {
const isnan = math.isnan.resolve([T])
const compnan = math.nan(Complex(T))
return Returns(Complex(T), r => {
if (isnan(r)) return compnan
return {re: r, im: z}
})
}
return Returns(Complex(T), r => ({re: r, im: z}))
}),
match([Any, Any], (math, [T, U]) => {
if (T !== U) {
throw new RangeError(
'mixed complex types disallowed '
+ `(real ${T}, imaginary ${U})`)
}
return Returns(Complex(T), (r, m) => ({re: r, im: m}))
})
]
export const arg = match(
Complex(NumberT), Returns(NumberT, z => Math.atan2(z.im, z.re)))