8 changed files with 80 additions and 7 deletions
@ -0,0 +1,16 @@
|
||||
import { anyComplex } from './complex.js' |
||||
|
||||
export default function create(pmath) { |
||||
const complex = pmath('complex') |
||||
return pmath('add', [anyComplex, // naive, but this is just a P-o-C
|
||||
(...addends) => { |
||||
let sum = complex(addends[0]) |
||||
for (let i = 1; i < addends.length; ++i) { |
||||
const addend = complex(addends[i]) |
||||
sum.re += addend.re |
||||
sum.im += addend.im |
||||
} |
||||
return sum |
||||
}]) |
||||
} |
||||
|
@ -0,0 +1,12 @@
|
||||
import createComplex from './complex.js' |
||||
import createAdd from './add.js' |
||||
import createNegate from './negate.js' |
||||
import createSubtract from '../generic/subtract.js' |
||||
|
||||
export default function create(pmath) { |
||||
createComplex(pmath) |
||||
createAdd(pmath) |
||||
createNegate(pmath) |
||||
createSubtract(pmath) |
||||
// not sure if there's anything reasonable to return here
|
||||
} |
@ -0,0 +1,21 @@
|
||||
/* 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})] |
||||
]) |
||||
} |
||||
|
@ -0,0 +1,6 @@
|
||||
import { isComplex } from './complex.js' |
||||
|
||||
export default function create(pmath) { |
||||
return pmath('negate', [args => args.length == 1 && isComplex(args[0]), |
||||
z => ({re: -z.re, im: -z.im})]) |
||||
} |
@ -1,14 +1,18 @@
|
||||
export function isNumber(x) { |
||||
return typeof x === 'number' |
||||
} |
||||
|
||||
export function allNumbers(args) { |
||||
for (let i = 0; i < args.length; ++i) { |
||||
if (typeof args[i] !== 'number') return false |
||||
} |
||||
return true |
||||
for (let i = 0; i < args.length; ++i) { |
||||
if (!isNumber(args[i])) return false |
||||
} |
||||
return true |
||||
} |
||||
|
||||
export function oneNumber(args) { |
||||
return args.length === 1 && typeof args[0] === 'number' |
||||
return args.length === 1 && isNumber(args[0]) |
||||
} |
||||
|
||||
export default function create(pmath) { |
||||
return pmath('number', [() => true, x => Number(x)]) |
||||
return pmath('number', [() => true, x => Number(x)]) |
||||
} |
||||
|
@ -1,8 +1,10 @@
|
||||
/* Core of picomath: generates an instance */ |
||||
import picomathInstance from './picomathInstance.js' |
||||
import createNumbers from './number/all.js' |
||||
import createComplex from './complex/all.js' |
||||
|
||||
const math = picomathInstance('math') |
||||
createNumbers(math) |
||||
|
||||
createComplex(math) |
||||
|
||||
export default math |
||||
|
Loading…
Reference in new issue