rust_blas/matrix/
mod.rs

1// Copyright 2014 Michael Yang. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5//! Matrix operations.
6use crate::attribute::Order;
7
8pub mod ll;
9pub mod ops;
10
11/// Methods that allow a type to be used in BLAS functions as a matrix.
12pub trait Matrix<T> {
13    /// The leading dimension of the matrix. Defaults to `cols` for `RowMajor`
14    /// order and 'rows' for `ColMajor` order.
15    fn lead_dim(&self) -> u32 {
16        match self.order() {
17            Order::RowMajor => self.cols(),
18            Order::ColMajor => self.rows(),
19        }
20    }
21    /// The order of the matrix. Defaults to `RowMajor`.
22    fn order(&self) -> Order {
23        Order::RowMajor
24    }
25    /// Returns the number of rows.
26    fn rows(&self) -> u32;
27    /// Returns the number of columns.
28    fn cols(&self) -> u32;
29    /// An unsafe pointer to a contiguous block of memory.
30    fn as_ptr(&self) -> *const T;
31    /// An unsafe pointer to a contiguous block of memory.
32    fn as_mut_ptr(&mut self) -> *mut T;
33}
34
35pub trait BandMatrix<T>: Matrix<T> {
36    fn sub_diagonals(&self) -> u32;
37    fn sup_diagonals(&self) -> u32;
38
39    fn as_matrix(&self) -> &dyn Matrix<T>;
40}
41
42#[cfg(test)]
43pub mod tests {
44    use crate::Matrix;
45
46    pub struct M<T>(pub u32, pub u32, pub Vec<T>);
47
48    impl<T> Matrix<T> for M<T> {
49        fn rows(&self) -> u32 {
50            self.0
51        }
52
53        fn cols(&self) -> u32 {
54            self.1
55        }
56
57        #[inline]
58        fn as_ptr(&self) -> *const T {
59            self.2[..].as_ptr()
60        }
61
62        #[inline]
63        fn as_mut_ptr(&mut self) -> *mut T {
64            (&mut self.2[..]).as_mut_ptr()
65        }
66    }
67}