nanomath/src/complex/type.js

29 lines
820 B
JavaScript
Raw Normal View History

import {Complex} from './Complex.js'
import {onType} from "#core/helpers.js"
import {Returns} from "#core/Type.js"
import {Any} from "#core/TypePatterns.js"
export const complex = onType(
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}))
},
[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}))
})