chore: show how to inject multiple instances of the same function (WIP)

This commit is contained in:
Jos de Jong 2023-01-04 14:06:30 +01:00
parent d147d0a576
commit 0ea786dcc2

View File

@ -78,11 +78,14 @@ export const divide =
// in fact, we need `add_real` on both T and Complex<T>, hence the dependency // in fact, we need `add_real` on both T and Complex<T>, hence the dependency
// with a custom name, not generated via Dependencies<...> // with a custom name, not generated via Dependencies<...>
export const sqrt = export const sqrt =
<T>(dep: Dependencies<'add' | 'equal' | 'conservativeSqrt' | 'unaryMinus', <T>(dep: Dependencies<'equal' | 'conservativeSqrt' | 'unaryMinus', RealType<T>>
RealType<T>> & Dependencies<'zero' | 'complex', T>
& Dependencies<'zero' | 'addReal' | 'complex', T>
& Dependencies<'absquare' | 're' | 'divideReal', Complex<T>> & Dependencies<'absquare' | 're' | 'divideReal', Complex<T>>
& {add_complex_real: Signature<'addReal', Complex<T>>}): & {
addNumber: Signature<'addReal', T>, // TODO: should be possible to use Signature<'add'> here
addReal: Signature<'add', RealType<T>>,
addComplex: Signature<'addReal', Complex<T>> // TODO: should be possible to use Signature<'add'> here
}):
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,10 +94,10 @@ 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.addReal(z.im, dep.conservativeSqrt(negr))) dep.zero(z.re), dep.addNumber(z.im, dep.conservativeSqrt(negr)))
} }
const num = dep.add_complex_real(z, myabs) const num = dep.addComplex(z, myabs)
const denomsq = dep.add(dep.add(myabs, myabs), dep.add(r, r)) const denomsq = dep.addReal(dep.addReal(myabs, myabs), dep.addReal(r, r))
const denom = dep.conservativeSqrt(denomsq) const denom = dep.conservativeSqrt(denomsq)
return dep.divideReal(num, denom) return dep.divideReal(num, denom)
} }