refactor: Simpler merging mechanism
Merging of Pocomath modules is eased by allowing one PocomathInstance to be merged into another. That allows types, for example, to be exported as a PocomathInstance (so there is no need for a special identifier convention for types; they can be directly added with an installType method). Also, larger modules can just be exported as an instance, since there is more flexibility and more checking in merging PocomathInstances than raw modules.
This commit is contained in:
parent
58fa661a2d
commit
d9d7af961f
12 changed files with 146 additions and 89 deletions
|
@ -1,3 +1,5 @@
|
|||
import PocomathInstance from '../../core/PocomathInstance.mjs'
|
||||
|
||||
/* 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).
|
||||
|
@ -6,11 +8,13 @@ function isComplex(z) {
|
|||
return z && typeof z === 'object' && 're' in z && 'im' in z
|
||||
}
|
||||
|
||||
export const Type_Complex = {
|
||||
const Complex = new PocomathInstance('Complex')
|
||||
Complex.installType('Complex', {
|
||||
test: isComplex,
|
||||
from: {
|
||||
number: x => ({re: x, im: 0}),
|
||||
bigint: x => ({re: x, im: 0n})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
export {Complex}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
export * from '../generic/arithmetic.mjs'
|
||||
export * from './native.mjs'
|
||||
import PocomathInstance from '../core/PocomathInstance.mjs'
|
||||
import * as complexes from './native.mjs'
|
||||
import * as generic from '../generic/arithmetic.mjs'
|
||||
|
||||
// resolve the conflicts
|
||||
export {sqrt} from './sqrt.mjs'
|
||||
export default PocomathInstance.merge('complex', complexes, generic)
|
||||
|
|
|
@ -35,7 +35,6 @@ export const sqrt = {
|
|||
const reSign = sign(z.re)
|
||||
if (imSign === imZero && reSign === reOne) return self(z.re)
|
||||
const reTwo = add(reOne, reOne)
|
||||
const partial = add(abs(z), z.re)
|
||||
return complex(
|
||||
multiply(sign(z.im), self(divide(add(abs(z),z.re), reTwo))),
|
||||
self(divide(subtract(abs(z),z.re), reTwo))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue