rust_linear_algebra/vector/ops/
add.rs

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
use super::super::Vector;
use std::ops::Add;

impl<K> Add for Vector<K>
where
    K: Add<Output = K> + Copy,
{
    type Output = Vector<K>;

    fn add(self, rhs: Self) -> Self::Output {
        let elements = self.iter().zip(rhs.iter()).map(|(&a, &b)| a + b).collect();
        Vector { elements }
    }
}

impl<K> Vector<K>
where
    K: Add<Output = K> + Copy + Default,
{
    pub fn add(&mut self, v: Vector<K>) {
        if self.len() != v.len() {
            panic!("The vector need to be on the same plan");
        }

        self.elements = self.iter().zip(v.iter()).map(|(&a, &b)| a + b).collect();
    }
}