chore: implement AliasOf

This commit is contained in:
Jos de Jong 2023-01-04 12:35:37 +01:00
parent 1b9d6b6428
commit d147d0a576
2 changed files with 18 additions and 16 deletions

View File

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