Declare implementations and dependencies via standard interfaces for operations #8

Merged
glen merged 20 commits from approach4.6 into main 2023-01-22 01:34:57 +00:00
2 changed files with 18 additions and 16 deletions
Showing only changes of commit d147d0a576 - Show all commits

View File

@ -1,6 +1,6 @@
import {Complex} from './type.js'
import type {
Dependencies, Signature, Returns, RealType, ZeroType
Dependencies, Signature, Returns, RealType, AliasOf
} from '../interfaces/type.js'
declare module "../interfaces/type" {
@ -9,8 +9,8 @@ declare module "../interfaces/type" {
// after removing any `_...` suffixes; the following should be
// additional dispatches for add and divide, not separate
// operations, in the final mathjs bundle.
add_real: (a: T, b: RealType<T>) => T
divide_real: (a: T, b: RealType<T>) => T
addReal: AliasOf<'add', (a: T, b: RealType<T>) => T>
divideReal: AliasOf<'divide', (a: T, b: RealType<T>) => T>
}
}
@ -19,9 +19,9 @@ export const add =
(w, z) => dep.complex(dep.add(w.re, z.re), dep.add(w.im, z.im))
export const add_real =
<T>(dep: Dependencies<'add_real' | 'complex', T>):
Signature<'add_real', Complex<T>> =>
(z, r) => dep.complex(dep.add_real(z.re, r), z.im)
<T>(dep: Dependencies<'addReal' | 'complex', T>):
Signature<'addReal', Complex<T>> =>
(z, r) => dep.complex(dep.addReal(z.re, r), z.im)
export const unaryMinus =
<T>(dep: Dependencies<'unaryMinus' | 'complex', T>):
@ -58,14 +58,14 @@ export const absquare =
z => dep.add(dep.absquare(z.re), dep.absquare(z.im))
export const divideByReal =
<T>(dep: Dependencies<'divide_real' | 'complex', T>):
Signature<'divide_real', Complex<T>> =>
(z, r) => dep.complex(dep.divide_real(z.re, r), dep.divide_real(z.im, r))
<T>(dep: Dependencies<'divideReal' | 'complex', T>):
Signature<'divideReal', Complex<T>> =>
(z, r) => dep.complex(dep.divideReal(z.re, r), dep.divideReal(z.im, r))
export const reciprocal =
<T>(dep: Dependencies<'conj' | 'absquare' | 'divide_real', Complex<T>>):
<T>(dep: Dependencies<'conj' | 'absquare' | 'divideReal', Complex<T>>):
Signature<'reciprocal', Complex<T>> =>
z => dep.divide_real(dep.conj(z), dep.absquare(z))
z => dep.divideReal(dep.conj(z), dep.absquare(z))
export const divide =
<T>(dep: Dependencies<'multiply' | 'reciprocal', Complex<T>>):
@ -80,9 +80,9 @@ export const divide =
export const sqrt =
<T>(dep: Dependencies<'add' | 'equal' | 'conservativeSqrt' | 'unaryMinus',
RealType<T>>
& Dependencies<'zero' | 'add_real' | 'complex', T>
& Dependencies<'absquare' | 're' | 'divide_real', Complex<T>>
& {add_complex_real: Signature<'add_real', Complex<T>>}):
& Dependencies<'zero' | 'addReal' | 'complex', T>
& Dependencies<'absquare' | 're' | 'divideReal', Complex<T>>
& {add_complex_real: Signature<'addReal', Complex<T>>}):
Signature<'sqrt', Complex<T>> =>
z => {
const myabs = dep.conservativeSqrt(dep.absquare(z))
@ -91,12 +91,12 @@ export const sqrt =
if (dep.equal(myabs, negr)) {
// pure imaginary square root; z.im already zero
return dep.complex(
dep.zero(z.re), dep.add_real(z.im, dep.conservativeSqrt(negr)))
dep.zero(z.re), dep.addReal(z.im, dep.conservativeSqrt(negr)))
}
const num = dep.add_complex_real(z, myabs)
const denomsq = dep.add(dep.add(myabs, myabs), dep.add(r, r))
const denom = dep.conservativeSqrt(denomsq)
return dep.divide_real(num, denom)
return dep.divideReal(num, denom)
}
export const conservativeSqrt = sqrt

View File

@ -71,3 +71,5 @@ type SignatureKey = keyof Signatures<unknown>
export type Signature<Name extends SignatureKey, T> = Signatures<T>[Name]
export type Returns<Name extends SignatureKey, T> = ReturnType<Signatures<T>[Name]>
export type Dependencies<Name extends SignatureKey, T> = {[K in Name]: Signature<K, T>}
export type AliasOf<Name extends string, T> = T & {aliasOf?: Name}