1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*!

## Common matrix operations for all the matrix representations.

### Other matrix representations
Currently the algorithms are implemented for the `nalgebra` **DMatrix** type.
You can use the algorithms for other matrix representations (e.g. matrix-free)
by providing your own implementation of the **Matrixoperations** trait.

*/
extern crate nalgebra as na;
use na::{DMatrix, DMatrixSlice, DVector, DVectorSlice};
use std::clone::Clone;

/// Trait containing the matrix free operations
pub trait MatrixOperations: Clone  {
    fn matrix_vector_prod(&self, vs: DVectorSlice<f64>) -> DVector<f64>;
    fn matrix_matrix_prod(&self, mtx: DMatrixSlice<f64>) -> DMatrix<f64>;
    fn diagonal(&self) -> DVector<f64>;
    fn set_diagonal(&mut self, diag: &DVector<f64>);
    fn cols(&self) -> usize;
    fn rows(&self) -> usize;
}

impl MatrixOperations for DMatrix<f64> {
    fn matrix_vector_prod(&self, vs: DVectorSlice<f64>) -> DVector<f64> {
        self * vs
    }
    fn matrix_matrix_prod(&self, mtx: DMatrixSlice<f64>) -> DMatrix<f64> {
        self * mtx
    }
    fn diagonal(&self) -> DVector<f64> {
        self.diagonal()
    }
    fn set_diagonal(&mut self, diag: &DVector<f64>) {
        self.set_diagonal(diag);
    }

    fn cols(&self) -> usize {
        self.ncols()
    }
    fn rows(&self) -> usize {
        self.nrows()
    }
}