rust_linear_algebra/vector/
mod.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
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
pub mod angle_cos;
pub mod cross_product;
pub mod linear_combinaison;
pub mod norm;
pub mod ops;

pub use angle_cos::*;
pub use cross_product::*;
pub use linear_combinaison::*;

use std::cmp::PartialEq;
use std::slice::{Iter, IterMut};

#[derive(Debug, Clone)]
pub struct Vector<K> {
    pub elements: Vec<K>,
}

impl<K> From<Vec<K>> for Vector<K> {
    fn from(elements: Vec<K>) -> Self {
        Vector { elements }
    }
}

impl<K, const N: usize> From<[K; N]> for Vector<K>
where
    K: Clone,
{
    fn from(elements: [K; N]) -> Self {
        Vector {
            elements: elements.to_vec(),
        }
    }
}

impl<K> Vector<K> {
    pub fn new(elements: Vec<K>) -> Self {
        Vector { elements }
    }
    pub fn iter(&self) -> Iter<K> {
        self.elements.iter()
    }
    pub fn iter_mut(&mut self) -> IterMut<K> {
        self.elements.iter_mut()
    }
    pub fn len(&self) -> usize {
        self.elements.len()
    }
}

impl<K> PartialEq for Vector<K>
where
    K: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.elements == other.elements
    }
    fn ne(&self, other: &Self) -> bool {
        self.elements != other.elements
    }
}