summaryrefslogtreecommitdiff
path: root/src/poly.rs
blob: c76e48157aaea52093280160aa0f176d465f1411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
mod iter;

use iter::Iter;
use std::fmt;
use std::ops::{Add, Mul, Sub};

#[derive(PartialEq, Debug, Clone)]
pub struct Poly {
    data: Vec<f32>,
    degree: usize,
}

impl Poly {
    pub fn new(data: Vec<f32>) -> Poly {
        let mut i = data.len() - 1;
        while data[i] == 0.0 && i > 0 {
            i -= 1;
        }
        Poly { data, degree: i }
    }

    pub fn degree(&self) -> usize {
        self.degree
    }

    fn iter(&self) -> Iter {
        Iter::new(self.data.clone(), self.degree())
    }
}

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<f32> = 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 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 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 degree_is_five() {
        let p = Poly::new(vec![0.0, 0.0, 0.0, 0.0, 0.0, 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);
    }
}