2022-12-07 01:21:05 +00:00
|
|
|
import {configDependency} from '../core/Config.js'
|
2022-12-22 05:14:58 +00:00
|
|
|
import {
|
2022-12-27 22:55:17 +00:00
|
|
|
Dependency, ImpType
|
2022-12-22 05:14:58 +00:00
|
|
|
} from '../core/Dispatcher.js'
|
|
|
|
import type {Complex, UnderlyingReal} from '../Complex/type.js'
|
2022-12-07 01:21:05 +00:00
|
|
|
|
2022-12-27 22:55:17 +00:00
|
|
|
type UnaryNumber = (a: number) => number
|
|
|
|
type BinaryNumber = (a: number, b:number) => number
|
2022-12-22 05:14:58 +00:00
|
|
|
|
2022-12-27 22:55:17 +00:00
|
|
|
declare module "./type" {
|
|
|
|
interface NumbersImpTypes {
|
|
|
|
add: BinaryNumber
|
|
|
|
unaryMinus: UnaryNumber
|
|
|
|
conj: UnaryNumber
|
|
|
|
subtract: BinaryNumber
|
|
|
|
multiply: BinaryNumber
|
|
|
|
absquare: UnaryNumber
|
|
|
|
reciprocal: UnaryNumber
|
|
|
|
divide: BinaryNumber
|
|
|
|
conservativeSqrt: UnaryNumber
|
2022-12-21 05:18:42 +00:00
|
|
|
// Best we can do for sqrt at compile time, since actual return
|
|
|
|
// type depends on config. Not sure how this will play out
|
|
|
|
// when we make a number-only bundle, but at least the import type
|
|
|
|
// above for Complex<> does not lead to any emitted JavaScript.
|
2022-12-27 22:55:17 +00:00
|
|
|
sqrt: (a: number) => number | Complex<number>
|
2022-12-21 05:18:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const add: ImpType<'add', [number, number]> = (a, b) => a + b
|
|
|
|
export const unaryMinus: ImpType<'unaryMinus', [number]> = a => -a
|
2022-12-22 05:14:58 +00:00
|
|
|
export const conj: ImpType<'conj', [number]> = a => a
|
2022-12-21 05:18:42 +00:00
|
|
|
export const subtract: ImpType<'subtract', [number, number]> = (a, b) => a - b
|
|
|
|
export const multiply: ImpType<'multiply', [number, number]> = (a, b) => a * b
|
2022-12-22 05:14:58 +00:00
|
|
|
export const absquare: ImpType<'absquare', [number]> = a => a*a
|
|
|
|
export const reciprocal: ImpType<'reciprocal', [number]> = a => 1/a
|
2022-12-21 05:18:42 +00:00
|
|
|
export const divide: ImpType<'divide', [number, number]> = (a, b) => a / b
|
2022-12-22 05:14:58 +00:00
|
|
|
|
|
|
|
export const conservativeSqrt: ImpType<'conservativeSqrt', [number]> =
|
|
|
|
a => isNaN(a) ? NaN : Math.sqrt(a)
|
|
|
|
|
2022-12-07 01:21:05 +00:00
|
|
|
export const sqrt =
|
|
|
|
(dep: configDependency
|
2022-12-21 05:18:42 +00:00
|
|
|
& Dependency<'complex', [number, number]>): ImpType<'sqrt', [number]> => {
|
2022-12-22 05:14:58 +00:00
|
|
|
if (dep.config.predictable || !dep.complex) return conservativeSqrt
|
2022-12-21 05:18:42 +00:00
|
|
|
return a => {
|
2022-12-07 01:21:05 +00:00
|
|
|
if (isNaN(a)) return NaN
|
|
|
|
if (a >= 0) return Math.sqrt(a)
|
|
|
|
return dep.complex(0, Math.sqrt(unaryMinus(a)))
|
|
|
|
}
|
|
|
|
}
|