#![allow(clippy::many_single_char_names)]
use crate::error::ERROR_MARGIN;
use geometric_algebra::{epga1d::*, Reversal, SquaredMagnitude};
#[derive(Debug, Clone, Copy)]
pub struct Root {
pub numerator_real: f32,
pub numerator_imag: f32,
pub denominator: f32,
}
impl Root {
pub fn new(numerator_real: f32, numerator_imag: f32, denominator: f32) -> Self {
Self {
numerator_real,
numerator_imag,
denominator,
}
}
}
pub fn solve_linear(coefficients: [f32; 2]) -> (f32, Vec<Root>) {
if coefficients[1].abs() <= ERROR_MARGIN {
(0.0, vec![])
} else {
(1.0, vec![Root::new(-coefficients[0], 0.0, coefficients[1])])
}
}
pub fn solve_quadratic(coefficients: [f32; 3]) -> (f32, Vec<Root>) {
if coefficients[2].abs() <= ERROR_MARGIN {
return solve_linear([coefficients[0], coefficients[1]]);
}
let discriminant = coefficients[1].powi(2) - 4.0 * coefficients[2] * coefficients[0];
let q = Scalar::new(discriminant).sqrt();
let mut solutions = Vec::with_capacity(3);
for s in &[-q, q] {
let numerator = *s - ComplexNumber::new(coefficients[1], 0.0);
solutions.push(Root::new(numerator.real(), numerator.imaginary(), 2.0 * coefficients[2]));
}
(discriminant, solutions)
}
const ROOTS_OF_UNITY_3: [ComplexNumber; 3] = [
ComplexNumber::new(-0.5, -0.8660254),
ComplexNumber::new(-0.5, 0.8660254),
ComplexNumber::new(1.0, 0.0),
];
pub fn solve_cubic(coefficients: [f32; 4]) -> (f32, Vec<Root>, usize) {
if coefficients[3].abs() <= ERROR_MARGIN {
let (discriminant, roots) = solve_quadratic([coefficients[0], coefficients[1], coefficients[2]]);
return (discriminant, roots, 2);
}
let d = [
coefficients[2].powi(2) - 3.0 * coefficients[3] * coefficients[1],
2.0 * coefficients[2].powi(3) - 9.0 * coefficients[3] * coefficients[2] * coefficients[1] + 27.0 * coefficients[3].powi(2) * coefficients[0],
];
let mut solutions = Vec::with_capacity(3);
let discriminant = d[1].powi(2) - 4.0 * d[0].powi(3);
let c = Scalar::new(discriminant).sqrt();
let c = ((c + ComplexNumber::new(if c.real() + d[1] == 0.0 { -d[1] } else { d[1] }, 0.0)) * Scalar::new(0.5)).powf(1.0 / 3.0);
for root_of_unity in &ROOTS_OF_UNITY_3 {
let ci = c * *root_of_unity;
let denominator = ci * Scalar::new(3.0 * coefficients[3]);
let numerator = (ci * Scalar::new(-coefficients[2]) - ci * ci - ComplexNumber::new(d[0], 0.0)) * denominator.reversal();
solutions.push(Root::new(numerator.real(), numerator.imaginary(), denominator.squared_magnitude().real()));
}
let real_root = (((std::f32::consts::PI - c.arg()) / (std::f32::consts::PI * 2.0 / 3.0)) as usize + 1) % 3;
(discriminant, solutions, real_root)
}
pub fn solve_quartic(coefficients: [f32; 5]) -> (f32, Vec<Root>) {
if coefficients[4].abs() <= ERROR_MARGIN {
let (discriminant, roots, _real_root) = solve_cubic([coefficients[0], coefficients[1], coefficients[2], coefficients[3]]);
return (discriminant, roots);
}
let p = (8.0 * coefficients[4] * coefficients[2] - 3.0 * coefficients[3].powi(2)) / (8.0 * coefficients[4].powi(2));
let q = (coefficients[3].powi(3) - 4.0 * coefficients[4] * coefficients[3] * coefficients[2] + 8.0 * coefficients[4].powi(2) * coefficients[1])
/ (8.0 * coefficients[4].powi(3));
let d = [
coefficients[2].powi(2) - 3.0 * coefficients[3] * coefficients[1] + 12.0 * coefficients[4] * coefficients[0],
2.0 * coefficients[2].powi(3) - 9.0 * coefficients[3] * coefficients[2] * coefficients[1]
+ 27.0 * coefficients[3].powi(2) * coefficients[0]
+ 27.0 * coefficients[4] * coefficients[1].powi(2)
- 72.0 * coefficients[4] * coefficients[2] * coefficients[0],
];
let discriminant = d[1].powi(2) - 4.0 * d[0].powi(3);
let c = Scalar::new(discriminant).sqrt();
let c = ((c + ComplexNumber::new(if c.real() + d[1] == 0.0 { -d[1] } else { d[1] }, 0.0)) * Scalar::new(0.5)).powf(1.0 / 3.0);
let e = ((c + ComplexNumber::new(d[0], 0.0) / c) / Scalar::new(3.0 * coefficients[4]) - ComplexNumber::new(p * 2.0 / 3.0, 0.0)).powf(0.5)
* Scalar::new(0.5);
let mut solutions = Vec::with_capacity(4);
for i in 0..4 {
let f = (e * e * Scalar::new(-4.0) - ComplexNumber::new(2.0 * p, 0.0) + ComplexNumber::new(if i & 2 == 0 { q } else { -q }, 0.0) / e)
.powf(0.5)
* Scalar::new(0.5);
let g =
ComplexNumber::new(-coefficients[3] / (4.0 * coefficients[4]), 0.0) + if i & 2 == 0 { -e } else { e } + if i & 1 == 0 { -f } else { f };
solutions.push(Root::new(g.real(), g.imaginary(), 1.0));
}
(discriminant / -27.0, solutions)
}