use super::super::FloatType;
use super::super::Roots;
pub fn find_roots_quartic_depressed<F: FloatType>(a2: F, a1: F, a0: F) -> Roots<F> {
if a1 == F::zero() {
super::biquadratic::find_roots_biquadratic(F::one(), a2, a0)
} else if a0 == F::zero() {
super::cubic_normalized::find_roots_cubic_normalized(F::zero(), a2, a1).add_new_root(F::zero())
} else {
let _2 = F::from(2i16);
let _5 = F::from(5i16);
let a2_pow_2 = a2 * a2;
let a1_div_2 = a1 / _2;
let b2 = a2 * _5 / _2;
let b1 = _2 * a2_pow_2 - a0;
let b0 = (a2_pow_2 * a2 - a2 * a0 - a1_div_2 * a1_div_2) / _2;
let resolvent_roots = super::cubic_normalized::find_roots_cubic_normalized(b2, b1, b0);
let y = resolvent_roots.as_ref().iter().last().unwrap();
let _a2_plus_2y = a2 + _2 * *y;
if _a2_plus_2y > F::zero() {
let sqrt_a2_plus_2y = _a2_plus_2y.sqrt();
let q0a = a2 + *y - a1_div_2 / sqrt_a2_plus_2y;
let q0b = a2 + *y + a1_div_2 / sqrt_a2_plus_2y;
let mut roots = super::quadratic::find_roots_quadratic(F::one(), sqrt_a2_plus_2y, q0a);
for x in super::quadratic::find_roots_quadratic(F::one(), -sqrt_a2_plus_2y, q0b)
.as_ref()
.iter()
{
roots = roots.add_new_root(*x);
}
roots
} else {
Roots::No([])
}
}
}
#[cfg(test)]
mod test {
use super::super::super::*;
#[test]
fn test_find_roots_quartic_depressed() {
assert_eq!(find_roots_quartic_depressed(0f32, 0f32, 0f32), Roots::One([0f32]));
assert_eq!(find_roots_quartic_depressed(1f32, 1f32, 1f32), Roots::No([]));
match find_roots_quartic_depressed(1f64, 1f64, -1f64) {
Roots::Two(x) => {
assert_float_array_eq!(1e-15, x, [-1f64, 0.5698402909980532659114f64]);
}
_ => {
assert!(false);
}
}
match find_roots_quartic_depressed(-10f64, 5f64, 1f64) {
Roots::Four(x) => {
assert_float_array_eq!(
1e-15,
x,
[
-3.3754294311910698f64,
-0.1531811728532153f64,
0.67861075799846644f64,
2.84999984604581877f64
]
);
}
_ => {
assert!(false);
}
}
}
}