opensrdk_linear_algebra/matrix/ge/operators/
index.rs

1use crate::matrix::ge::Matrix;
2use crate::number::Number;
3use std::ops::{Index, IndexMut};
4
5impl<T> Index<usize> for Matrix<T>
6where
7    T: Number,
8{
9    type Output = [T];
10    fn index(&self, index: usize) -> &Self::Output {
11        let i = self.rows * index;
12        &self.elems[i..i + self.rows]
13    }
14}
15
16impl<T> IndexMut<usize> for Matrix<T>
17where
18    T: Number,
19{
20    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
21        let i = self.rows * index;
22        &mut self.elems[i..i + self.rows]
23    }
24}
25
26impl<T> Index<(usize, usize)> for Matrix<T>
27where
28    T: Number,
29{
30    type Output = T;
31    fn index(&self, index: (usize, usize)) -> &Self::Output {
32        &self.elems[index.0 + index.1 * self.rows]
33    }
34}
35
36impl<T> IndexMut<(usize, usize)> for Matrix<T>
37where
38    T: Number,
39{
40    fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output {
41        &mut self.elems[index.0 + index.1 * self.rows]
42    }
43}
44
45mod tests {
46    #[allow(unused_imports)]
47    use crate::*;
48    #[test]
49    fn it_works() {
50        let a = mat!(
51            1.0, 2.0;
52            3.0, 4.0
53        );
54        assert_eq!(a[0], [1.0, 3.0]);
55        assert_eq!(a[1], [2.0, 4.0]);
56    }
57
58    #[test]
59    fn it_works2() {
60        let a = mat!(
61            1.0, 2.0;
62            3.0, 4.0
63        );
64        assert_eq!(a[(0, 0)], 1.0);
65        assert_eq!(a[(0, 1)], 2.0);
66        assert_eq!(a[(1, 0)], 3.0);
67        assert_eq!(a[(1, 1)], 4.0);
68    }
69}