pocomath/complex/add.mjs
Glen Whitney 66cbccfbbe feat: Allow self-reference in implementations
And use it to define negate and add for Complex numbers in a way that
  is independent of component types.

  Also add a bigint type and verify that pocomath will then handle Gaussian
  integers "for free".
2022-07-19 11:16:29 -07:00

16 lines
514 B
JavaScript

import {numComplex} from './Complex.mjs'
export const add = {
'...Complex': [['self'], ref => addends => {
if (addends.length === 0) return {re:0, im:0}
const seed = addends.shift()
return addends.reduce((w,z) => {
/* Need a "base case" to avoid infinite self-reference loops */
if (numComplex(z) && numComplex(w)) {
return {re: w.re + z.re, im: w.im + z.im}
}
return {re: ref.self(w.re, z.re), im: ref.self(w.im, z.im)}
}, seed)
}]
}