diff --git a/src/maths/solver.rs b/src/maths/solver.rs index a1a9c04..cef3e36 100644 --- a/src/maths/solver.rs +++ b/src/maths/solver.rs @@ -1,5 +1,49 @@ use super::*; -pub fn solve(equation: Vec) -> Vec { +fn zero() -> GaussianRational { + GaussianRational::new(Rational::new(0, 1), Rational::new(0, 1)) +} + +fn minus_one() -> GaussianRational { + GaussianRational::new(Rational::new(-1, 1), Rational::new(0, 1)) +} + +fn degree_zero(equation: Vec) { + println!("Polynomial degree: 0"); + if equation[0] == zero() { + println!("Every real number is a solution ! Personally, I'd rather say that this equation is valid.") + } else { + println!("This equation has no solution ! Personally, I'd rather say that this equation is invalid.") + } +} + +fn degree_one(equation: Vec) { + println!("Polynomial degree: 1"); + let x = minus_one() * equation[0] / equation[1]; + println!("The solution is"); + println!("{:?}", x); +} + +/* +fn degree_two(equation: Vec) { + println!("Polynomial degree: 2"); + let delta = equation[1] * equation[1] + 4 * equation[0] * equation[0]; +} +*/ + +pub fn solve(mut equation: Vec) -> Vec { + for i in (1..equation.len()).rev() { + if equation[i] == zero() { + equation.pop(); + } + } + crate::pretty(&equation); + match equation.len() { + 0 => unreachable!(), + 1 => degree_zero(equation), + 2 => degree_one(equation), + //3 => degree_two(equation), + _ => println!("Polynomial of degree greater than 2 detected, I can't solve that !"), + } vec![] }