2022-08-30 19:36:44 +00:00
|
|
|
import {Returns, returnTypeOf} from '../core/Returns.mjs'
|
2022-07-25 18:56:12 +00:00
|
|
|
export * from './Types/Complex.mjs'
|
2022-07-25 11:20:13 +00:00
|
|
|
|
|
|
|
export const sqrt = {
|
2022-08-06 15:27:44 +00:00
|
|
|
'Complex<T>': ({
|
2022-07-25 11:20:13 +00:00
|
|
|
config,
|
2022-08-06 15:27:44 +00:00
|
|
|
'isZero(T)': isZ,
|
|
|
|
'sign(T)': sgn,
|
|
|
|
'one(T)': uno,
|
|
|
|
'add(T,T)': plus,
|
|
|
|
'complex(T)': cplxU,
|
|
|
|
'complex(T,T)': cplxB,
|
|
|
|
'multiply(T,T)': mult,
|
|
|
|
'self(T)': me,
|
|
|
|
'divide(T,T)': div,
|
2022-08-30 19:36:44 +00:00
|
|
|
'absquare(Complex<T>)': absqC,
|
2022-08-06 15:27:44 +00:00
|
|
|
'subtract(T,T)': sub
|
2022-07-25 11:20:13 +00:00
|
|
|
}) => {
|
2022-08-30 19:36:44 +00:00
|
|
|
let baseReturns = returnTypeOf(me)
|
|
|
|
if (baseReturns.includes('|')) {
|
|
|
|
// Bit of a hack, because it is relying on other implementations
|
|
|
|
// to list the "typical" value of sqrt first
|
|
|
|
baseReturns = baseReturns.split('|', 1)[0]
|
|
|
|
}
|
|
|
|
|
2022-07-25 11:20:13 +00:00
|
|
|
if (config.predictable) {
|
2022-08-30 19:36:44 +00:00
|
|
|
return Returns(`Complex<${baseReturns}>`, z => {
|
2022-08-06 15:27:44 +00:00
|
|
|
const reOne = uno(z.re)
|
|
|
|
if (isZ(z.im) && sgn(z.re) === reOne) return cplxU(me(z.re))
|
|
|
|
const reTwo = plus(reOne, reOne)
|
2022-08-30 19:36:44 +00:00
|
|
|
const myabs = me(absqC(z))
|
2022-08-06 15:27:44 +00:00
|
|
|
return cplxB(
|
2022-08-30 19:36:44 +00:00
|
|
|
mult(sgn(z.im), me(div(plus(myabs, z.re), reTwo))),
|
|
|
|
me(div(sub(myabs, z.re), reTwo))
|
2022-07-25 11:20:13 +00:00
|
|
|
)
|
2022-08-30 19:36:44 +00:00
|
|
|
})
|
2022-07-25 11:20:13 +00:00
|
|
|
}
|
2022-08-30 19:36:44 +00:00
|
|
|
|
|
|
|
return Returns(
|
|
|
|
`Complex<${baseReturns}>|${baseReturns}|undefined`,
|
|
|
|
z => {
|
|
|
|
const reOne = uno(z.re)
|
|
|
|
if (isZ(z.im) && sgn(z.re) === reOne) return me(z.re)
|
|
|
|
const reTwo = plus(reOne, reOne)
|
|
|
|
const myabs = me(absqC(z))
|
|
|
|
const reSqrt = me(div(plus(myabs, z.re), reTwo))
|
|
|
|
const imSqrt = me(div(sub(myabs, z.re), reTwo))
|
|
|
|
if (reSqrt === undefined || imSqrt === undefined) return undefined
|
|
|
|
return cplxB(mult(sgn(z.im), reSqrt), imSqrt)
|
|
|
|
}
|
|
|
|
)
|
2022-07-25 11:20:13 +00:00
|
|
|
}
|
|
|
|
}
|