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
Showing only changes of commit 0ea786dcc2 - Show all commits

View File

@ -78,11 +78,14 @@ export const divide =
// in fact, we need `add_real` on both T and Complex<T>, hence the dependency
// with a custom name, not generated via Dependencies<...>
export const sqrt =
<T>(dep: Dependencies<'add' | 'equal' | 'conservativeSqrt' | 'unaryMinus',
RealType<T>>
& Dependencies<'zero' | 'addReal' | 'complex', T>
<T>(dep: Dependencies<'equal' | 'conservativeSqrt' | 'unaryMinus', RealType<T>>
& Dependencies<'zero' | '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>> =>
z => {
const myabs = dep.conservativeSqrt(dep.absquare(z))
@ -91,10 +94,10 @@ export const sqrt =
if (dep.equal(myabs, negr)) {
// pure imaginary square root; z.im already zero
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 denomsq = dep.add(dep.add(myabs, myabs), dep.add(r, r))
const num = dep.addComplex(z, myabs)
const denomsq = dep.addReal(dep.addReal(myabs, myabs), dep.addReal(r, r))
const denom = dep.conservativeSqrt(denomsq)
return dep.divideReal(num, denom)
}