diff options
Diffstat (limited to 'src/poly.rs')
-rw-r--r-- | src/poly.rs | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/src/poly.rs b/src/poly.rs index 3f298cb..f6ee63c 100644 --- a/src/poly.rs +++ b/src/poly.rs @@ -4,7 +4,7 @@ use crate::number::Number; use iter::Iter; use std::cmp; use std::cmp::Ordering; -use std::ops::{Add, Sub, Mul}; +use std::ops::{Add, Sub, Mul, Rem}; use std::iter::{Sum, Zip, Take}; #[derive(PartialEq, Debug)] @@ -65,6 +65,26 @@ impl Mul for &P { } } +impl Rem for &P { + type Output = P; + + fn rem(self, other: Self) -> P { + let ad = self.degree(); + let bd = other.degree(); + let an = self.0[ad]; + let bn = other.0[bd]; + match ad.cmp(&bd) { + Ordering::Equal => self - &(other * &P(vec![an / bn])), + Ordering::Greater => { + let mut qv = vec![0.0; ad]; + qv[ad - bd] = an / bn; + self - &(other * &P(qv)) + }, + Ordering::Less => P(vec![0.0]) + } + } +} + //****************************REFACTORING************************************ pub type Poly = Vec<Number>; @@ -213,16 +233,16 @@ mod tests { #[test] fn rem_equal() { - let a = vec![6.0, 7.0, 1.0]; - let b = vec![-6.0, -5.0, 1.0]; - assert_eq!(rem(&a, &b), vec![12.0, 12.0, 0.0]); + let a = P(vec![6.0, 7.0, 1.0]); + let b = P(vec![-6.0, -5.0, 1.0]); + assert_eq!(&a % &b, P(vec![12.0, 12.0, 0.0])); } #[test] fn rem_greater() { - let a = vec![-6.0, -5.0, 1.0]; - let b = vec![12.0, 12.0, 0.0]; - assert_eq!(rem(&a, &b), vec![-6.0, -6.0, 0.0]); + let a = P(vec![-6.0, -5.0, 1.0]); + let b = P(vec![12.0, 12.0, 0.0]); + assert_eq!(&a % &b, P(vec![-6.0, -6.0, 0.0])); } #[test] |