matrixlib/vector/
mod.rs

1//! Vectors.
2
3mod ops;
4mod polar;
5
6#[derive(Debug)]
7/// An `N`x 1 vector struct.
8pub struct VectorN<const N: usize> {
9    data: [f64; N]
10}
11
12impl<const N: usize> VectorN<N> {
13    /// Creates a new vector from an array of f64 values
14    pub fn new(data: [f64; N]) -> Self {
15        Self {
16            data
17        }
18    }
19
20    /// Returns the dimensions of the vector
21    pub fn dimensions(&self) -> usize {
22        N
23    }
24
25    /// Gets an entry in the vector
26    pub fn get(&self, i: usize) -> Option<f64> {
27        if i > self.data.len() {
28            return None;
29        } else {
30            return Some(self.data[i]);
31        }
32
33    }
34
35    /// Returns the internal data of the vector as a slice.
36    pub fn data(&self) -> &[f64] {
37        &self.data
38    }
39
40}
41
42
43#[cfg(test)]
44mod vector_tests {
45    use super::VectorN;
46
47    #[test]
48    pub fn test_dotproduct() {
49        let a = VectorN::new([1.0, 2.0, 3.0, 4.0]);
50
51        let b = VectorN::new([6.9, 4.2, 3.5, 6.7]);
52
53        let dotproduct = a.dotproduct(&b);
54
55        assert!(dotproduct.is_ok());
56
57        let dotproduct = dotproduct.unwrap();
58
59        assert_eq!(dotproduct, 52.6);
60
61    }
62}