orx_v/matrices/
matrix_col_major.rs

1use super::{Matrix, MatrixMut};
2use crate::{NVec, NVecMut, D1};
3
4/// A column major matrix.
5///
6/// Say i represents row-index and j represents col-index.
7/// In a column-major matrix:
8/// * it is more efficient to iterate first over j, and then over i,
9/// * [`col(j)`] often (1) returns a vector over a contagious memory location.
10///
11/// *(1) When the data is represented by a complete allocation; however, recall that
12/// it is possible to use a function or a sparse vector backed up with a lookup as
13/// the underlying vector of the matrix.*
14pub trait MatrixColMajor<T>: Matrix<T> {
15    /// Returns the `j`-th column of the matrix which is a `D1` vector.
16    fn col(&self, j: usize) -> impl NVec<D1, T>;
17
18    /// Returns an iterator over the columns of the matrix.
19    fn cols(&self) -> impl Iterator<Item = impl NVec<D1, T>> {
20        (0..self.num_cols()).map(|j| self.col(j))
21    }
22}
23
24impl<T, M: MatrixColMajor<T>> MatrixColMajor<T> for &M {
25    fn col(&self, j: usize) -> impl NVec<D1, T> {
26        <M as MatrixColMajor<T>>::col(self, j)
27    }
28}
29
30impl<T, M: MatrixColMajor<T>> MatrixColMajor<T> for &mut M {
31    fn col(&self, j: usize) -> impl NVec<D1, T> {
32        <M as MatrixColMajor<T>>::col(self, j)
33    }
34}
35
36// mut
37
38/// A mutable column major matrix.
39pub trait MatrixColMajorMut<T>: MatrixColMajor<T> + MatrixMut<T> {
40    /// Returns a mutable reference to the `j`-th column of the matrix which is a `D1` vector.
41    fn col_mut(&mut self, j: usize) -> impl NVecMut<D1, T>;
42}
43
44impl<T, M: MatrixColMajorMut<T>> MatrixColMajorMut<T> for &mut M {
45    fn col_mut(&mut self, j: usize) -> impl NVecMut<D1, T> {
46        <M as MatrixColMajorMut<T>>::col_mut(self, j)
47    }
48}