Bladeren bron

:sparkles: added matrix abstraction as preparation for later matrix types

Felix Bytow 2 jaren geleden
bovenliggende
commit
31d38252e5
4 gewijzigde bestanden met toevoegingen van 49 en 4 verwijderingen
  1. 1 1
      src/lib.rs
  2. 26 2
      src/types/matrix/generic.rs
  3. 21 0
      src/types/matrix/mod.rs
  4. 1 1
      src/types/mod.rs

+ 1 - 1
src/lib.rs

@@ -13,7 +13,7 @@
 //! `Float` in our context.
 pub use self::traits::{Float, Numeric, Primitive};
 #[cfg(feature = "matrix")]
-pub use self::types::{ColumnVector, GenericMatrix, RowVector, SquareMatrix};
+pub use self::types::{ColumnVector, GenericMatrix, Matrix, RowVector, SquareMatrix};
 #[cfg(feature = "angular")]
 pub use self::types::{Angular, Degree, Radiant};
 #[cfg(feature = "complex")]

+ 26 - 2
src/types/matrix/generic.rs

@@ -1,7 +1,7 @@
 use std::ops::{Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Neg, Sub, SubAssign};
 use std::ptr::swap;
 
-use crate::{Float, Numeric, Primitive};
+use crate::{Float, Matrix, Numeric, Primitive};
 #[cfg(feature = "angular")]
 use crate::Angular;
 
@@ -12,6 +12,16 @@ pub struct GenericMatrix<T: Numeric, const ROWS: usize, const COLUMNS: usize> {
     pub data: [[T; COLUMNS]; ROWS],
 }
 
+impl<T: Numeric, const ROWS: usize, const COLUMNS: usize> Matrix<T, ROWS, COLUMNS> for GenericMatrix<T, ROWS, COLUMNS> {
+    fn get(&self, row: usize, col: usize) -> T {
+        self.data[row][col]
+    }
+
+    fn set(&mut self, row: usize, col: usize, value: T) {
+        self.data[row][col] = value;
+    }
+}
+
 /// Special kind of `GenericMatrix` where both dimensions are equal.
 pub type SquareMatrix<T, const DIMENSION: usize> = GenericMatrix<T, DIMENSION, DIMENSION>;
 
@@ -503,7 +513,7 @@ impl<T: Numeric + Float + Primitive> SquareMatrix<T, 4> {
 
 #[cfg(test)]
 mod tests {
-    use crate::{ColumnVector, Complex, cplx, Float, GenericMatrix, Numeric, RowVector, SquareMatrix};
+    use crate::{ColumnVector, Complex, cplx, Float, GenericMatrix, Matrix, Numeric, RowVector, SquareMatrix};
 
     fn matrices_are_equal<T: Numeric + Float + PartialOrd, const ROWS: usize, const COLUMNS: usize>(
         a: &GenericMatrix<T, ROWS, COLUMNS>,
@@ -637,4 +647,18 @@ mod tests {
         assert!(matrices_are_equal(&inverted, &expected, tolerance));
         assert!(matrices_are_equal(&(inverted * mat), &SquareMatrix::identity(), tolerance));
     }
+
+    #[test]
+    #[should_panic = "index out of bounds"]
+    fn out_of_range_get() {
+        let mat = SquareMatrix::from([[1]]);
+        mat.get(1, 1);
+    }
+
+    #[test]
+    #[should_panic = "index out of bounds"]
+    fn out_of_range_set() {
+        let mut mat = SquareMatrix::from([[1]]);
+        mat.set(1, 1, 2);
+    }
 }

+ 21 - 0
src/types/matrix/mod.rs

@@ -1,3 +1,24 @@
+use crate::Numeric;
 pub use self::generic::{ColumnVector, GenericMatrix, RowVector, SquareMatrix};
 
 mod generic;
+
+/// Abstraction for all kinds of fixed size matrices.
+pub trait Matrix<T: Numeric, const ROWS: usize, const COLUMNS: usize> {
+    /// Get the value of the given cell.
+    ///
+    /// # Panics
+    ///
+    /// This function panics, if the row or column are outside of the valid range of the matrix.
+    fn get(&self, row: usize, col: usize) -> T;
+
+    /// Set the value of the given cell.
+    ///
+    /// # Panics
+    ///
+    /// This function panics, if the row or column are outsize of the valid range of the matrix.
+    ///
+    /// Additionally it may panic, if the value is invalid for the given cell.
+    /// E.g. cells outsize of the main diagonal of a `DiagonalMatrix` can only be set to zero.
+    fn set(&mut self, row: usize, col: usize, value: T);
+}

+ 1 - 1
src/types/mod.rs

@@ -3,7 +3,7 @@ pub use self::angular::{Angular, Degree, Radiant};
 #[cfg(feature = "complex")]
 pub use self::complex::Complex;
 #[cfg(feature = "matrix")]
-pub use self::matrix::{ColumnVector, GenericMatrix, RowVector, SquareMatrix};
+pub use self::matrix::{ColumnVector, GenericMatrix, Matrix, RowVector, SquareMatrix};
 #[cfg(feature = "quaternion")]
 pub use self::quaternion::Quaternion;