refactor: clarify implementation names in sqrt
Also removes some obsolete comments
This commit is contained in:
parent
bd05dc9267
commit
637e339b17
@ -5,10 +5,6 @@ import type {
|
|||||||
|
|
||||||
declare module "../interfaces/type" {
|
declare module "../interfaces/type" {
|
||||||
interface Signatures<T> {
|
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>
|
addReal: AliasOf<'add', (a: T, b: RealType<T>) => T>
|
||||||
divideReal: AliasOf<'divide', (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>> =>
|
<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))
|
(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>):
|
<T>(dep: Dependencies<'addReal' | 'complex', T>):
|
||||||
Signature<'addReal', Complex<T>> =>
|
Signature<'addReal', Complex<T>> =>
|
||||||
(z, r) => dep.complex(dep.addReal(z.re, r), z.im)
|
(z, r) => dep.complex(dep.addReal(z.re, r), z.im)
|
||||||
@ -57,7 +53,7 @@ export const absquare =
|
|||||||
Signature<'absquare', Complex<T>> =>
|
Signature<'absquare', Complex<T>> =>
|
||||||
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 divideReal =
|
||||||
<T>(dep: Dependencies<'divideReal' | 'complex', T>):
|
<T>(dep: Dependencies<'divideReal' | 'complex', T>):
|
||||||
Signature<'divideReal', Complex<T>> =>
|
Signature<'divideReal', Complex<T>> =>
|
||||||
(z, r) => dep.complex(dep.divideReal(z.re, r), dep.divideReal(z.im, r))
|
(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
|
// The dependencies are slightly tricky here, because there are three types
|
||||||
// involved: Complex<T>, T, and RealType<T>, all of which might be different,
|
// 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
|
// 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<...>
|
// with a custom name, not generated via Dependencies<...>
|
||||||
export const sqrt =
|
export const sqrt =
|
||||||
<T>(dep: Dependencies<'equal' | 'conservativeSqrt' | 'unaryMinus', RealType<T>>
|
<T>(dep: Dependencies<'equal' | 'conservativeSqrt' | 'unaryMinus', RealType<T>>
|
||||||
& Dependencies<'zero' | 'complex', T>
|
& Dependencies<'zero' | 'complex', T>
|
||||||
& Dependencies<'absquare' | 're' | 'divideReal', Complex<T>>
|
& Dependencies<'absquare' | 're' | 'divideReal', Complex<T>>
|
||||||
& {
|
& {
|
||||||
addNumber: Signature<'addReal', T>, // TODO: should use Signature<'add'> here
|
addTR: Signature<'addReal', T>,
|
||||||
addReal: Signature<'add', RealType<T>>,
|
addRR: Signature<'add', RealType<T>>,
|
||||||
addComplex: Signature<'addReal', Complex<T>> // TODO: should use Signature<'add'> here
|
addCR: Signature<'addReal', Complex<T>>
|
||||||
}):
|
}):
|
||||||
Signature<'sqrt', Complex<T>> =>
|
Signature<'sqrt', Complex<T>> =>
|
||||||
z => {
|
z => {
|
||||||
@ -94,10 +90,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.addNumber(z.im, dep.conservativeSqrt(negr)))
|
dep.zero(z.re), dep.addTR(z.im, dep.conservativeSqrt(negr)))
|
||||||
}
|
}
|
||||||
const num = dep.addComplex(z, myabs)
|
const num = dep.addCR(z, myabs)
|
||||||
const denomsq = dep.addReal(dep.addReal(myabs, myabs), dep.addReal(r, r))
|
const denomsq = dep.addRR(dep.addRR(myabs, myabs), dep.addRR(r, r))
|
||||||
const denom = dep.conservativeSqrt(denomsq)
|
const denom = dep.conservativeSqrt(denomsq)
|
||||||
return dep.divideReal(num, denom)
|
return dep.divideReal(num, denom)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user