Skip to main content

eigenvalues/
matrix_operations.rs

1/*!
2
3## Common matrix operations for all the matrix representations.
4
5### Other matrix representations
6Currently the algorithms are implemented for the `nalgebra` **DMatrix** type.
7You can use the algorithms for other matrix representations (e.g. matrix-free)
8by providing your own implementation of the **Matrixoperations** trait.
9
10*/
11use nalgebra::{DMatrix, DMatrixSlice, DVector, DVectorSlice};
12use std::clone::Clone;
13
14/// Trait containing the matrix free operations
15pub trait MatrixOperations: Clone  {
16    /// Matrix vector multiplication
17    fn matrix_vector_prod(&self, vs: DVectorSlice<f64>) -> DVector<f64>;
18    /// Matrix matrix multiplication
19    fn matrix_matrix_prod(&self, mtx: DMatrixSlice<f64>) -> DMatrix<f64>;
20    /// Get the matrix diagonal
21    fn diagonal(&self) -> DVector<f64>;
22    /// Set the matrix diagonal
23    fn set_diagonal(&mut self, diag: &DVector<f64>);
24    /// Get the number of columns
25    fn ncols(&self) -> usize;
26    /// Get the number of rows
27    fn nrows(&self) -> usize;
28}
29
30impl MatrixOperations for DMatrix<f64> {
31    fn matrix_vector_prod(&self, vs: DVectorSlice<f64>) -> DVector<f64> {
32        self * vs
33    }
34    fn matrix_matrix_prod(&self, mtx: DMatrixSlice<f64>) -> DMatrix<f64> {
35        self * mtx
36    }
37    fn diagonal(&self) -> DVector<f64> {
38        self.diagonal()
39    }
40    fn set_diagonal(&mut self, diag: &DVector<f64>) {
41        self.set_diagonal(diag);
42    }
43
44    fn ncols(&self) -> usize {
45        self.ncols()
46    }
47    fn nrows(&self) -> usize {
48        self.nrows()
49    }
50}
51
52