2023-01-22 01:34:57 +00:00
|
|
|
import type {configDependency} from '../core/Config.js'
|
|
|
|
import type {Dependencies, Signature} from '../interfaces/type.js'
|
2023-10-17 22:02:18 +00:00
|
|
|
import {$reflect} from '../interfaces/type.js'
|
2023-01-22 01:34:57 +00:00
|
|
|
|
2023-10-17 22:02:18 +00:00
|
|
|
export const add = (): Signature<'add', number> => (a, b) => a + b
|
|
|
|
const unaMinus = (a: number) => -a
|
|
|
|
export const unaryMinus = (): Signature<'unaryMinus', number> => unaMinus
|
|
|
|
export const conj = (): Signature<'conj', number> => a => a
|
|
|
|
export const subtract = (): Signature<'subtract', number> => (a, b) => a - b
|
|
|
|
export const multiply = (): Signature<'multiply', number> => (a, b) => a * b
|
|
|
|
export const absquare = (): Signature<'absquare', number> => a => a * a
|
|
|
|
export const reciprocal = (): Signature<'reciprocal', number> => a => 1 / a
|
|
|
|
export const divide = (): Signature<'divide', number> => (a, b) => a / b
|
2023-01-22 01:34:57 +00:00
|
|
|
|
2023-10-17 22:02:18 +00:00
|
|
|
const basicSqrt = (a: number) => isNaN(a) ? NaN : Math.sqrt(a)
|
|
|
|
export const conservativeSqrt = (): Signature<'conservativeSqrt', number> =>
|
|
|
|
basicSqrt
|
2022-12-07 01:21:05 +00:00
|
|
|
|
|
|
|
export const sqrt =
|
2023-10-17 22:02:18 +00:00
|
|
|
(dep: configDependency
|
|
|
|
& Dependencies<'complex', number>): Signature<'sqrt', number> => {
|
|
|
|
if (dep.config.predictable || !dep.complex) {
|
|
|
|
return basicSqrt
|
|
|
|
}
|
|
|
|
return a => {
|
|
|
|
if (isNaN(a)) return NaN
|
|
|
|
if (a >= 0) return Math.sqrt(a)
|
|
|
|
return dep.complex(0, Math.sqrt(unaMinus(a)))
|
|
|
|
}
|
2023-01-22 01:34:57 +00:00
|
|
|
}
|
2023-10-17 22:02:18 +00:00
|
|
|
|
|
|
|
$reflect!([
|
|
|
|
add, unaryMinus, conj, subtract, multiply, absquare, reciprocal, divide,
|
|
|
|
conservativeSqrt, sqrt
|
|
|
|
])
|