diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2021-01-11 09:59:53 -0300 |
---|---|---|
committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2021-01-11 10:00:12 -0300 |
commit | 6ddf0167bf7d8a7a9f37fa37adfd2345cd5e4b89 (patch) | |
tree | 02eecef44d3e7a0e56fb9ed68b87bb7ae934457a /src/poly.rs | |
parent | cb497e172fcb26dda42e45db793db1478db317bd (diff) | |
download | bezier-6ddf0167bf7d8a7a9f37fa37adfd2345cd5e4b89.tar.gz bezier-6ddf0167bf7d8a7a9f37fa37adfd2345cd5e4b89.zip |
Implement rem for P
This one doesn't need shift.
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] |