pocomath/complex/Complex.mjs
Glen Whitney 84a8b9d5c4 feat: Allow self-reference in implementations (#4)
This PR also uses such self-reference to define negate and add
  for Complex numbers in a way that is independent of component types.

  Also adds a bigint type and verifies that pocomath will then handle
  Gaussian integers "for free".

  Ensures that if one function is invalidated, then any that depend on it will be.

Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Reviewed-on: #4
2022-07-19 18:54:22 +00:00

30 lines
884 B
JavaScript

import typed from 'typed-function'
/* Use a plain object with keys re and im for a complex; note the components
* can be any type (for this proof-of-concept; in reality we'd want to
* insist on some numeric or scalar supertype).
*/
export function isComplex(z) {
return z && typeof z === 'object' && 're' in z && 'im' in z
}
typed.addType({name: 'Complex', test: isComplex})
typed.addConversion({
from: 'number',
to: 'Complex',
convert: x => ({re: x, im: 0})
})
/* Pleasantly enough, it is OK to add this conversion even if there is no
* type 'bigint' defined, so everything should Just Work.
*/
typed.addConversion({
from: 'bigint',
to: 'Complex',
convert: x => ({re: x, im: 0n})
})
/* test if an entity is Complex<number>, so to speak: */
export function numComplex(z) {
return isComplex(z) && typeof z.re === 'number' && typeof z.im === 'number'
}