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-12-01 17:47:20 +00:00
|
|
|
'sqrtc(Complex<T>)': predictableSqrt,
|
2022-08-06 15:27:44 +00:00
|
|
|
'isZero(T)': isZ,
|
2022-07-25 11:20:13 +00:00
|
|
|
}) => {
|
2022-12-01 17:47:20 +00:00
|
|
|
if (config.checkingDependency) return undefined
|
|
|
|
const complexReturns = returnTypeOf(predictableSqrt)
|
|
|
|
const baseReturns = complexReturns.slice(8, -1); // Complex<WhatWeWant>
|
2022-07-25 11:20:13 +00:00
|
|
|
if (config.predictable) {
|
2022-12-01 17:47:20 +00:00
|
|
|
return Returns(complexReturns, z => predictableSqrt(z))
|
2022-07-25 11:20:13 +00:00
|
|
|
}
|
2022-08-30 19:36:44 +00:00
|
|
|
|
|
|
|
return Returns(
|
|
|
|
`Complex<${baseReturns}>|${baseReturns}|undefined`,
|
|
|
|
z => {
|
2022-12-01 17:47:20 +00:00
|
|
|
let complexSqrt
|
|
|
|
try {
|
|
|
|
complexSqrt = predictableSqrt(z)
|
|
|
|
} catch (e) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
if (complexSqrt.re === undefined || complexSqrt.im === undefined) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
if (isZ(complexSqrt.im)) return complexSqrt.re
|
|
|
|
return complexSqrt
|
2022-08-30 19:36:44 +00:00
|
|
|
}
|
|
|
|
)
|
2022-07-25 11:20:13 +00:00
|
|
|
}
|
|
|
|
}
|