summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/poly.rs29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/poly.rs b/src/poly.rs
index 34444b3..3f298cb 100644
--- a/src/poly.rs
+++ b/src/poly.rs
@@ -4,8 +4,8 @@ use crate::number::Number;
use iter::Iter;
use std::cmp;
use std::cmp::Ordering;
-use std::ops::{Add, Sub};
-use std::iter::{Zip, Take};
+use std::ops::{Add, Sub, Mul};
+use std::iter::{Sum, Zip, Take};
#[derive(PartialEq, Debug)]
pub struct P(Vec<Number>);
@@ -47,6 +47,24 @@ impl Sub for &P {
}
}
+impl Mul for &P {
+ type Output = P;
+
+ fn mul(self, other: Self) -> P {
+ 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()
+ .take(self.degree() + 1)
+ .map(|x| {x * other.0[i]})
+ .collect();
+ prefix.append(&mut suffix);
+ r.push(P(prefix));
+ }
+ r.iter().fold(P(vec![0.0]), |acc, x| &acc + x)
+ }
+}
+
//****************************REFACTORING************************************
pub type Poly = Vec<Number>;
@@ -135,6 +153,13 @@ mod tests {
use super::*;
#[test]
+ fn mul_test() {
+ let a = P(vec![1.0, 2.0, 3.0]);
+ let b = P(vec![1.0, 2.0]);
+ assert_eq!(&a * &b, P(vec![1.0, 4.0, 7.0, 6.0]));
+ }
+
+ #[test]
fn add_test() {
let a = P(vec![1.0]);
let b = P(vec![0.0, 0.0, 0.0, 1.0]);