matrixlib/matrix/
mtuple.rs

1//! Operations on individual an matrix tuple (mtuple).
2
3use crate::matrix::Matrix;
4
5pub enum Direction {
6    Row,
7    Column,
8}
9
10impl<const R: usize, const C: usize> Matrix<R, C> {
11    /// Gets an n-tuple from a matrix using an index `i` and a given direction (e.g row or column)
12    pub fn get_tuple(&self, i: usize, direction: Direction) -> Vec<f64> {
13        match direction {
14            Direction::Row => {
15                // Translates a row index into an index on the actual internal vector.
16                let i_as_row_index = C * i;
17
18                // This iterator extracts a specific row from the internal vector
19                (i_as_row_index..(C + i_as_row_index))
20                    .map(|i| self.matrix[i])
21                    .collect()
22            }
23            Direction::Column => {
24                // This iterator extracts a column from the internal vector vec by starting at the index incrementing by the amount of
25                // columns every iteration and pushing the value at that entry.
26                (i..self.matrix.len())
27                    .step_by(C)
28                    .map(|j| self.matrix[j])
29                    .collect()
30            }
31        }
32    }
33}
34
35#[cfg(test)]
36mod matrix_mtuple_tests {
37    use super::*;
38
39    #[test]
40    fn get_tuple_row() {
41        let data = vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0];
42
43        let matrix: Matrix<4, 3> = Matrix::new(data).unwrap();
44
45        let first_row = matrix.get_tuple(0, Direction::Row);
46        let second_row = matrix.get_tuple(1, Direction::Row);
47        let third_row = matrix.get_tuple(2, Direction::Row);
48        let fourth_row = matrix.get_tuple(3, Direction::Row);
49
50        assert_eq!(first_row, [0.0, 1.0, 2.0]);
51        assert_eq!(second_row, [3.0, 4.0, 5.0]);
52        assert_eq!(third_row, [6.0, 7.0, 8.0]);
53        assert_eq!(fourth_row, [9.0, 10.0, 11.0]);
54    }
55
56    #[test]
57    fn get_tuple_column() {
58        let data = vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0];
59
60        let matrix: Matrix<4, 3> = Matrix::new(data).unwrap();
61
62        let first_column = matrix.get_tuple(0, Direction::Column);
63        let second_column = matrix.get_tuple(1, Direction::Column);
64        let third_column = matrix.get_tuple(2, Direction::Column);
65
66        assert_eq!(first_column, [0.0, 3.0, 6.0, 9.0]);
67        assert_eq!(second_column, [1.0, 4.0, 7.0, 10.0]);
68        assert_eq!(third_column, [2.0, 5.0, 8.0, 11.0]);
69    }
70
71    #[test]
72    fn get_2x2matrix_row() {
73        let data = vec![1.0, 2.0, 3.0, 4.0];
74
75        let matrix: Matrix<2, 2> = Matrix::new(data).unwrap();
76
77        assert_eq!(matrix.get_tuple(1, Direction::Row), [3.0, 4.0]);
78    }
79
80    #[test]
81    fn get_2x2matrix_column() {
82        let data = vec![1.0, 2.0, 3.0, 4.0];
83
84        let matrix: Matrix<2, 2> = Matrix::new(data).unwrap();
85
86        assert_eq!(matrix.get_tuple(1, Direction::Column), [2.0, 4.0]);
87    }
88}