picomath/complex/complex.js

22 lines
617 B
JavaScript
Raw Normal View History

/* Use a plain object with keys re and im for a complex */
export function isComplex(z) {
return z && typeof z === 'object' && 're' in z && 'im' in z
}
export function anyComplex(args) {
for (let i = 0; i < args.length; ++i) {
if (isComplex(args[i])) return true
}
return false
}
export default function create(pmath) {
const number = pmath('number')
return pmath('complex', [
[args => args.length == 2, (x,y) => ({re: number(x), im: number(y)})],
[args => args.length == 1 && isComplex(args[0]), z => z],
[args => args.length == 1, x => ({re: number(x), im: 0})]
])
}