refactor: clarify implementation names in sqrt

Also removes some obsolete comments
This commit is contained in:
Glen Whitney 2023-01-21 20:21:05 -05:00
parent bd05dc9267
commit 637e339b17

View File

@ -5,10 +5,6 @@ import type {
declare module "../interfaces/type" {
interface Signatures<T> {
// TODO: Make Dispatcher collapse operations that match
// after removing any `_...` suffixes; the following should be
// additional dispatches for add and divide, not separate
// operations, in the final mathjs bundle.
addReal: AliasOf<'add', (a: T, b: RealType<T>) => T>
divideReal: AliasOf<'divide', (a: T, b: RealType<T>) => T>
}
@ -18,7 +14,7 @@ export const add =
<T>(dep: Dependencies<'add' | 'complex', T>): Signature<'add', Complex<T>> =>
(w, z) => dep.complex(dep.add(w.re, z.re), dep.add(w.im, z.im))
export const add_real =
export const addReal =
<T>(dep: Dependencies<'addReal' | 'complex', T>):
Signature<'addReal', Complex<T>> =>
(z, r) => dep.complex(dep.addReal(z.re, r), z.im)
@ -57,7 +53,7 @@ export const absquare =
Signature<'absquare', Complex<T>> =>
z => dep.add(dep.absquare(z.re), dep.absquare(z.im))
export const divideByReal =
export const divideReal =
<T>(dep: Dependencies<'divideReal' | 'complex', T>):
Signature<'divideReal', Complex<T>> =>
(z, r) => dep.complex(dep.divideReal(z.re, r), dep.divideReal(z.im, r))
@ -75,16 +71,16 @@ export const divide =
// The dependencies are slightly tricky here, because there are three types
// involved: Complex<T>, T, and RealType<T>, all of which might be different,
// and we have to get it straight which operations we need on each type, and
// in fact, we need `add_real` on both T and Complex<T>, hence the dependency
// in fact, we need `addReal` on both T and Complex<T>, hence the dependency
// with a custom name, not generated via Dependencies<...>
export const sqrt =
<T>(dep: Dependencies<'equal' | 'conservativeSqrt' | 'unaryMinus', RealType<T>>
& Dependencies<'zero' | 'complex', T>
& Dependencies<'absquare' | 're' | 'divideReal', Complex<T>>
& {
addNumber: Signature<'addReal', T>, // TODO: should use Signature<'add'> here
addReal: Signature<'add', RealType<T>>,
addComplex: Signature<'addReal', Complex<T>> // TODO: should use Signature<'add'> here
addTR: Signature<'addReal', T>,
addRR: Signature<'add', RealType<T>>,
addCR: Signature<'addReal', Complex<T>>
}):
Signature<'sqrt', Complex<T>> =>
z => {
@ -94,10 +90,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.addNumber(z.im, dep.conservativeSqrt(negr)))
dep.zero(z.re), dep.addTR(z.im, dep.conservativeSqrt(negr)))
}
const num = dep.addComplex(z, myabs)
const denomsq = dep.addReal(dep.addReal(myabs, myabs), dep.addReal(r, r))
const num = dep.addCR(z, myabs)
const denomsq = dep.addRR(dep.addRR(myabs, myabs), dep.addRR(r, r))
const denom = dep.conservativeSqrt(denomsq)
return dep.divideReal(num, denom)
}