66 lines
2.3 KiB
JavaScript
66 lines
2.3 KiB
JavaScript
import Returns from '../core/Returns.mjs'
|
|
export * from './Types/Complex.mjs'
|
|
|
|
export const polynomialRoot = {
|
|
'Complex<T>,...Complex<T>': ({
|
|
T,
|
|
'tuple(...Complex<T>)': tupCplx,
|
|
'tuple(...T)': tupReal,
|
|
'isZero(Complex<T>)': zero,
|
|
'complex(T)': C,
|
|
'multiply(Complex<T>,Complex<T>)': mul,
|
|
'divide(Complex<T>,Complex<T>)': div,
|
|
'negate(Complex<T>)': neg,
|
|
'isReal(Complex<T>)': real,
|
|
'equalTT(Complex<T>, Complex<T>)': eq,
|
|
'subtract(Complex<T>, Complex<T>)': sub,
|
|
'sqrtc(Complex<T>)': sqt,
|
|
}) => Returns(`Tuple<${T}>|Tuple<Complex<${T}>>`, (constant, rest) => {
|
|
// helper to convert results to appropriate tuple type
|
|
const typedTup = arr => {
|
|
if (arr.every(real)) {
|
|
return tupReal.apply(tupReal, arr.map(z => z.re))
|
|
}
|
|
return tupCplx.apply(tupCplx, arr)
|
|
}
|
|
|
|
const coeffs = [constant, ...rest]
|
|
while (coeffs.length > 0 && zero(coeffs[coeffs.length - 1])) {
|
|
coeffs.pop()
|
|
}
|
|
if (coeffs.length < 2) {
|
|
}
|
|
switch (coeffs.length) {
|
|
case 0: case 1:
|
|
throw new RangeError(
|
|
`Polynomial [${constant}, ${rest}] must have at least one`
|
|
+ 'non-zero non-constant coefficient')
|
|
case 2: // linear
|
|
return typedTup([neg(div(coeffs[0], coeffs[1]))])
|
|
case 3: { // quadratic
|
|
const [c, b, a] = coeffs
|
|
console.log('solving', a, b, c)
|
|
const denom = mul(C(2), a)
|
|
const d1 = mul(b, b)
|
|
const d2 = mul(C(4), mul(a, c))
|
|
console.log('Whoa', denom, d1, d2)
|
|
if (eq(d1, d2)) {
|
|
console.log('Hello', b, denom, div(neg(b), denom))
|
|
return typedTup([div(neg(b), denom)])
|
|
}
|
|
let discriminant = sqt(sub(d1, d2))
|
|
console.log('Uhoh', discriminant)
|
|
console.log('Roots', div(sub(discriminant, b), denom), div(sub(neg(discriminant), b), denom))
|
|
return typedTup([
|
|
div(sub(discriminant, b), denom),
|
|
div(sub(neg(discriminant), b), denom)
|
|
])
|
|
}
|
|
default:
|
|
throw new RangeError(
|
|
'only implemented for cubic or lower-order polynomials, '
|
|
+ `not ${JSON.stringify(coeffs)}`)
|
|
}
|
|
})
|
|
}
|