use crate::aux::Polynomial;
use crate::solver::degree_0::degree_0;
use crate::solver::degree_1::degree_1;
use crate::solver::degree_2::degree_2;
use crate::solver::degree_3::degree_3;
#[derive(Debug, PartialEq, Clone)]
pub struct Roots{
pub real: Vec<f64>,
pub complex: Vec<(f64, f64)>,
}
impl Roots {
pub fn new() -> Self {
Self {
real: Vec::new(),
complex: Vec::new(),
}
}
pub fn with_real(roots: Vec<f64>) -> Self {
Self {
real: roots,
complex: Vec::new(),
}
}
pub fn with_complex(roots: Vec<(f64, f64)>) -> Self {
Self {
real: Vec::new(),
complex: roots,
}
}
}
pub fn solve(poly: Polynomial) -> Result<Roots, String> {
let max_degree: u32 = poly.terms.keys().rev().next().copied().unwrap_or(0);
println!("Polynomial degree: {}", max_degree);
match max_degree {
0 => degree_0(poly),
1 => degree_1(poly),
2 => degree_2(poly),
3 => degree_3(poly),
_ => Err("The polynomial degree is strictly greater than 2, I can't solve.".to_string()),
}
}