diff options
Diffstat (limited to 'src/poly.rs')
-rw-r--r-- | src/poly.rs | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/src/poly.rs b/src/poly.rs index cb84d64..7d964a4 100644 --- a/src/poly.rs +++ b/src/poly.rs @@ -2,12 +2,12 @@ mod iter; use crate::number::Number; use iter::Iter; -use std::ops::{Add, Sub, Mul, Div}; +use std::ops::{Add, Div, Mul, Sub}; #[derive(PartialEq, Debug, Clone)] pub struct Poly { data: Vec<Number>, - degree: usize + degree: usize, } impl Poly { @@ -16,7 +16,7 @@ impl Poly { while data[i] == 0.0 && i > 0 { i -= 1; } - Poly { data, degree : i } + Poly { data, degree: i } } fn mono(degree: usize, coefficient: Number) -> Poly { @@ -52,7 +52,7 @@ impl Poly { fn is_zero(&self) -> bool { for i in 0..self.data.len() { if self.data[i] != 0.0 { - return false + return false; } } true @@ -63,7 +63,7 @@ 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()) + Poly::new(self.iter().zip(other.iter()).map(|(x, y)| x + y).collect()) } } @@ -71,7 +71,7 @@ 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()) + Poly::new(self.iter().zip(other.iter()).map(|(x, y)| x - y).collect()) } } @@ -82,9 +82,10 @@ impl Mul for &Poly { let mut r = Vec::new(); for i in 0..other.degree() + 1 { let mut prefix = vec![0.0; i]; - let mut suffix: Vec<Number> = self.iter() + let mut suffix: Vec<Number> = self + .iter() .take(self.degree() + 1) - .map(|x| {x * other.data[i]}) + .map(|x| x * other.data[i]) .collect(); prefix.append(&mut suffix); r.push(Poly::new(prefix)); @@ -139,14 +140,20 @@ mod tests { 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]))); + 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]))); + assert_eq!( + &a / &b, + (Poly::new(vec![-0.5, 1.0 / 12.0]), Poly::new(vec![0.0, 0.0])) + ); } #[test] |