mod iter; use crate::number::Number; use iter::Iter; use std::fmt; use std::ops::{Add, Div, Mul, Sub}; #[derive(PartialEq, Debug, Clone)] pub struct Poly { data: Vec, degree: usize, } impl Poly { pub fn new(data: Vec) -> Poly { let mut i = data.len() - 1; while data[i] == 0.0 && i > 0 { i -= 1; } Poly { data, degree: i } } fn mono(degree: usize, coefficient: Number) -> Poly { if coefficient != 0.0 { let mut p = vec![0.0; degree + 1]; p[degree] = coefficient; Poly::new(p) } else { Poly::new(vec![0.0]) } } pub fn degree(&self) -> usize { self.degree } fn lc(&self) -> Number { self.data[self.degree()] } fn iter(&self) -> Iter { Iter::new(self.data.clone(), self.degree()) } pub fn eval(&self, n: Number) -> Number { let mut r = 0.0; for i in 0..self.data.len() { r += self.data[i] * n.powi(i as i32); } r } fn is_zero(&self) -> bool { for i in 0..self.data.len() { if self.data[i] != 0.0 { return false; } } true } } impl Add for &Poly { type Output = Poly; fn add(self, other: Self) -> Poly { Poly::new(self.iter().zip(other.iter()).map(|(x, y)| x + y).collect()) } } impl Sub for &Poly { type Output = Poly; fn sub(self, other: Self) -> Poly { Poly::new(self.iter().zip(other.iter()).map(|(x, y)| x - y).collect()) } } impl Mul for &Poly { type Output = Poly; fn mul(self, other: Self) -> Poly { let mut r = Vec::new(); for i in 0..other.degree() + 1 { let mut prefix = vec![0.0; i]; let mut suffix: Vec = self .iter() .take(self.degree() + 1) .map(|x| x * other.data[i]) .collect(); prefix.append(&mut suffix); r.push(Poly::new(prefix)); } r.iter().fold(Poly::new(vec![0.0]), |acc, x| &acc + x) } } impl Div for &Poly { type Output = (Poly, Poly); fn div(self, divisor: Self) -> (Poly, Poly) { let mut q = Poly::new(vec![0.0]); let mut r = self.clone(); while !r.is_zero() && r.degree() >= divisor.degree() { let s = Poly::mono(r.degree() - divisor.degree(), r.lc() / divisor.lc()); q = &q + &s; r = &r - &(&s * divisor); } (q, r) } } impl fmt::Display for &Poly { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut s = String::new(); for i in 0..self.data.len() { s.push_str(&self.data[i].clone().to_string()); if i < self.data.len() - 1 { s.push_str(", "); } } write!(f, "{}", s) } } #[cfg(test)] mod tests { use super::*; #[test] fn derivative_test() { let p = Poly::new(vec![1.0, 2.0, 3.0, 4.0, 5.0]); assert_eq!(derivative(&p), Poly::new(vec![2.0, 6.0, 12.0, 20.0])); } #[test] fn div_equal() { let a = Poly::new(vec![6.0, 7.0, 1.0]); let b = Poly::new(vec![-6.0, -5.0, 1.0]); assert_eq!( &a / &b, (Poly::new(vec![1.0]), Poly::new(vec![12.0, 12.0, 0.0])) ); } #[test] fn div_greater() { let a = Poly::new(vec![-6.0, -5.0, 1.0]); let b = Poly::new(vec![12.0, 12.0]); assert_eq!( &a / &b, (Poly::new(vec![-0.5, 1.0 / 12.0]), Poly::new(vec![0.0, 0.0])) ); } #[test] fn div_less() { let a = Poly::new(vec![12.0, 12.0]); let b = Poly::new(vec![-6.0, -5.0, 1.0]); assert_eq!(&a / &b, (Poly::new(vec![0.0]), a)); } #[test] fn mul_test() { let a = Poly::new(vec![1.0, 2.0, 3.0]); let b = Poly::new(vec![1.0, 2.0]); assert_eq!(&a * &b, Poly::new(vec![1.0, 4.0, 7.0, 6.0])); } #[test] fn add_test() { let a = Poly::new(vec![1.0]); let b = Poly::new(vec![0.0, 0.0, 0.0, 1.0]); assert_eq!(&a + &b, Poly::new(vec![1.0, 0.0, 0.0, 1.0])); } #[test] fn sub_test() { let a = Poly::new(vec![1.0, 2.0, 3.0]); let b = Poly::new(vec![1.0, 1.0, 1.0]); assert_eq!(&a - &b, Poly::new(vec![0.0, 1.0, 2.0])); } #[test] fn poly_is_zero() { let p = Poly::new(vec![0.0; 5]); assert_eq!(p.is_zero(), true); } #[test] fn poly_is_not_zero() { let p = Poly::mono(5, 8.0); assert_eq!(p.is_zero(), false); } #[test] fn degree_is_five() { let p = Poly::mono(5, 2.0); assert_eq!(p.degree(), 5); } #[test] fn degree_is_zero() { let p = Poly::new(vec![0.0; 6]); assert_eq!(p.degree(), 0); } #[test] fn gcd_test() { let a = Poly::new(vec![2.0, -8.0, 8.0]); let b = Poly::new(vec![-1.0, 4.0, -1.0]); assert_eq!(gcd(&a, &b), Poly::new(vec![-0.0625, 0.0])); } }