Trait rusty_machine::prelude::BaseMatrix
[−]
pub trait BaseMatrix<T> { fn rows(&self) -> usize; fn cols(&self) -> usize; fn row_stride(&self) -> usize; fn as_ptr(&self) -> *const T; fn is_empty(&self) -> bool { ... } fn as_slice(&self) -> MatrixSlice<T> { ... } unsafe fn get_unchecked(&self, index: [usize; 2]) -> &T { ... } fn get_row(&self, index: usize) -> Option<&[T]> { ... } unsafe fn get_row_unchecked(&self, index: usize) -> &[T] { ... } fn iter<'a>(&self) -> SliceIter<'a, T> where T: 'a { ... } fn iter_rows(&self) -> Rows<T> { ... } fn iter_diag(&self, k: DiagOffset) -> Diagonal<T, Self> { ... } fn sum_rows(&self) -> Vector<T> where T: Copy + Zero<Output=T> + Add<T> { ... } fn sum_cols(&self) -> Vector<T> where T: Copy + Zero<Output=T> + Add<T> { ... } fn sum(&self) -> T where T: Copy + Zero<Output=T> + Add<T> { ... } fn into_matrix(self) -> Matrix<T> where T: Copy { ... } fn select_rows<'a, I>(&self, rows: I) -> Matrix<T> where I: IntoIterator<Item=&'a usize>, T: Copy, I::IntoIter: ExactSizeIterator, I::IntoIter: Clone { ... } fn select_cols<'a, I>(&self, cols: I) -> Matrix<T> where I: IntoIterator<Item=&'a usize>, T: Copy, I::IntoIter: ExactSizeIterator, I::IntoIter: Clone { ... } fn elemul(&self, m: &Self) -> Matrix<T> where T: Copy + Mul<T, Output=T> { ... } fn elediv(&self, m: &Self) -> Matrix<T> where T: Copy + Div<T, Output=T> { ... } fn select(&self, rows: &[usize], cols: &[usize]) -> Matrix<T> where T: Copy { ... } fn hcat<S>(&self, m: &S) -> Matrix<T> where S: BaseMatrix<T>, T: Copy { ... } fn vcat<S>(&self, m: &S) -> Matrix<T> where S: BaseMatrix<T>, T: Copy { ... } fn diag(&self) -> Vector<T> where T: Copy { ... } fn transpose(&self) -> Matrix<T> where T: Copy { ... } fn is_diag(&self) -> bool where T: Zero + PartialEq<T> { ... } fn solve_u_triangular(&self, y: Vector<T>) -> Result<Vector<T>, Error> where T: Any + Float { ... } fn solve_l_triangular(&self, y: Vector<T>) -> Result<Vector<T>, Error> where T: Any + Float { ... } fn split_at(&self, mid: usize, axis: Axes) -> (MatrixSlice<T>, MatrixSlice<T>) { ... } fn sub_slice<'a>(&self,
start: [usize; 2],
rows: usize,
cols: usize)
-> MatrixSlice<'a, T> where T: 'a { ... } }
Trait for immutable matrix structs.
Required Methods
fn rows(&self) -> usize
Rows in the matrix.
fn cols(&self) -> usize
Columns in the matrix.
fn row_stride(&self) -> usize
Row stride in the matrix.
fn as_ptr(&self) -> *const T
Top left index of the matrix.
Provided Methods
fn is_empty(&self) -> bool
Returns true if the matrix contais no elements
fn as_slice(&self) -> MatrixSlice<T>
Returns a MatrixSlice
over the whole matrix.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::new(3, 3, vec![2.0; 9]); let b = a.as_slice();
unsafe fn get_unchecked(&self, index: [usize; 2]) -> &T
Get a reference to a point in the matrix without bounds checking.
fn get_row(&self, index: usize) -> Option<&[T]>
Returns the row of a matrix at the given index.
None
if the index is out of bounds.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::new(3,3, (0..9).collect::<Vec<usize>>()); let slice = a.sub_slice([1,1], 2, 2); let row = slice.get_row(1); let expected = vec![7usize, 8]; assert_eq!(row, Some(&*expected)); assert!(slice.get_row(5).is_none());
unsafe fn get_row_unchecked(&self, index: usize) -> &[T]
Returns the row of a matrix at the given index without doing unbounds checking
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::new(3,3, (0..9).collect::<Vec<usize>>()); let slice = a.sub_slice([1,1], 2, 2); let row = unsafe { slice.get_row_unchecked(1) }; let mut expected = vec![7usize, 8]; assert_eq!(row, &*expected);
fn iter<'a>(&self) -> SliceIter<'a, T> where T: 'a
Returns an iterator over the matrix data.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::new(3,3, (0..9).collect::<Vec<usize>>()); let slice = a.sub_slice([1,1], 2, 2); let slice_data = slice.iter().map(|v| *v).collect::<Vec<usize>>(); assert_eq!(slice_data, vec![4,5,7,8]);
fn iter_rows(&self) -> Rows<T>
Iterate over the rows of the matrix.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::new(3, 2, (0..6).collect::<Vec<usize>>()); // Prints "2" three times. for row in a.iter_rows() { println!("{}", row.len()); }
fn iter_diag(&self, k: DiagOffset) -> Diagonal<T, Self>
Iterate over diagonal entries
Examples
use rulinalg::matrix::{DiagOffset, Matrix, BaseMatrix}; let a = matrix![0, 1, 2; 3, 4, 5; 6, 7, 8]; // Print super diag [1, 5] for d in a.iter_diag(DiagOffset::Above(1)) { println!("{}", d); } // Print sub diag [3, 7] // Equivalent to `iter_diag(DiagOffset::Below(1))` for d in a.iter_diag(DiagOffset::from(-1)) { println!("{}", d); }
Panics
If using an Above
or Below
offset which is
out-of-bounds this function will panic.
This function will never panic if the Main
diagonal
offset is used.
fn sum_rows(&self) -> Vector<T> where T: Copy + Zero<Output=T> + Add<T>
The sum of the rows of the matrix.
Returns a Vector equal to the sums of elements over the matrices rows.
Note that the resulting vector is identical to the sums of elements along each column of the matrix.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::new(2,2,vec![1.0,2.0,3.0,4.0]); let c = a.sum_rows(); assert_eq!(*c.data(), vec![4.0, 6.0]);
fn sum_cols(&self) -> Vector<T> where T: Copy + Zero<Output=T> + Add<T>
The sum of the columns of the matrix.
Returns a Vector equal to the sums of elements over the matrices columns.
Note that the resulting vector is identical to the sums of elements along each row of the matrix.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::new(2,2,vec![1.0,2.0,3.0,4.0]); let c = a.sum_cols(); assert_eq!(*c.data(), vec![3.0, 7.0]);
fn sum(&self) -> T where T: Copy + Zero<Output=T> + Add<T>
The sum of all elements in the matrix
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::new(2,2,vec![1.0,2.0,3.0,4.0]); let c = a.sum(); assert_eq!(c, 10.0);
fn into_matrix(self) -> Matrix<T> where T: Copy
Convert the matrix struct into a owned Matrix.
fn select_rows<'a, I>(&self, rows: I) -> Matrix<T> where I: IntoIterator<Item=&'a usize>, T: Copy, I::IntoIter: ExactSizeIterator, I::IntoIter: Clone
Select rows from matrix
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::<f64>::ones(3,3); let b = &a.select_rows(&[2]); assert_eq!(b.rows(), 1); assert_eq!(b.cols(), 3); let c = &a.select_rows(&[1,2]); assert_eq!(c.rows(), 2); assert_eq!(c.cols(), 3);
Panics
- Panics if row indices exceed the matrix dimensions.
fn select_cols<'a, I>(&self, cols: I) -> Matrix<T> where I: IntoIterator<Item=&'a usize>, T: Copy, I::IntoIter: ExactSizeIterator, I::IntoIter: Clone
Select columns from matrix
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::<f64>::ones(3,3); let b = &a.select_cols(&[2]); assert_eq!(b.rows(), 3); assert_eq!(b.cols(), 1); let c = &a.select_cols(&[1,2]); assert_eq!(c.rows(), 3); assert_eq!(c.cols(), 2);
Panics
- Panics if column indices exceed the matrix dimensions.
fn elemul(&self, m: &Self) -> Matrix<T> where T: Copy + Mul<T, Output=T>
The elementwise product of two matrices.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::new(2,2,vec![1.0,2.0,3.0,4.0]); let b = Matrix::new(2,2,vec![1.0,2.0,3.0,4.0]); let c = &a.elemul(&b); assert_eq!(*c.data(), vec![1.0, 4.0, 9.0, 16.0]);
Panics
- The matrices have different row counts.
- The matrices have different column counts.
fn elediv(&self, m: &Self) -> Matrix<T> where T: Copy + Div<T, Output=T>
The elementwise division of two matrices.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::new(2,2,vec![1.0,2.0,3.0,4.0]); let b = Matrix::new(2,2,vec![1.0,2.0,3.0,4.0]); let c = &a.elediv(&b); assert_eq!(*c.data(), vec![1.0; 4]);
Panics
- The matrices have different row counts.
- The matrices have different column counts.
fn select(&self, rows: &[usize], cols: &[usize]) -> Matrix<T> where T: Copy
Select block matrix from matrix
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::<f64>::identity(3); let b = &a.select(&[0,1], &[1,2]); // We get the 2x2 block matrix in the upper right corner. assert_eq!(b.rows(), 2); assert_eq!(b.cols(), 2); // Prints [0,0,1,0] println!("{:?}", b.data());
Panics
- Panics if row or column indices exceed the matrix dimensions.
fn hcat<S>(&self, m: &S) -> Matrix<T> where S: BaseMatrix<T>, T: Copy
Horizontally concatenates two matrices. With self on the left.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::new(3,2, vec![1.0,2.0,3.0,4.0,5.0,6.0]); let b = Matrix::new(3,1, vec![4.0,5.0,6.0]); let c = &a.hcat(&b); assert_eq!(c.cols(), a.cols() + b.cols()); assert_eq!(c[[1, 2]], 5.0);
Panics
- Self and m have different row counts.
fn vcat<S>(&self, m: &S) -> Matrix<T> where S: BaseMatrix<T>, T: Copy
Vertically concatenates two matrices. With self on top.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::new(2,3, vec![1.0,2.0,3.0,4.0,5.0,6.0]); let b = Matrix::new(1,3, vec![4.0,5.0,6.0]); let c = &a.vcat(&b); assert_eq!(c.rows(), a.rows() + b.rows()); assert_eq!(c[[2, 2]], 6.0);
Panics
- Self and m have different column counts.
fn diag(&self) -> Vector<T> where T: Copy
Extract the diagonal of the matrix
Examples
use rulinalg::vector::Vector; use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::new(3,3,vec![1,2,3,4,5,6,7,8,9]); let b = Matrix::new(3,2,vec![1,2,3,4,5,6]); let c = Matrix::new(2,3,vec![1,2,3,4,5,6]); let d = &a.diag(); // 1,5,9 let e = &b.diag(); // 1,4 let f = &c.diag(); // 1,5 assert_eq!(*d.data(), vec![1,5,9]); assert_eq!(*e.data(), vec![1,4]); assert_eq!(*f.data(), vec![1,5]);
fn transpose(&self) -> Matrix<T> where T: Copy
Tranposes the given matrix
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let mat = Matrix::new(2,3, vec![1.0,2.0,3.0,4.0,5.0,6.0]); let mt = mat.transpose();
fn is_diag(&self) -> bool where T: Zero + PartialEq<T>
Checks if matrix is diagonal.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; let a = Matrix::new(2,2, vec![1.0,0.0,0.0,1.0]); let a_diag = a.is_diag(); assert_eq!(a_diag, true); let b = Matrix::new(2,2, vec![1.0,0.0,1.0,0.0]); let b_diag = b.is_diag(); assert_eq!(b_diag, false);
fn solve_u_triangular(&self, y: Vector<T>) -> Result<Vector<T>, Error> where T: Any + Float
Solves an upper triangular linear system.
Given a matrix A
and a vector b
, this function returns the
solution of the upper triangular system Ux = b
, where U
is
the upper triangular part of A
.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; use rulinalg::vector::Vector; use std::f32; let u = Matrix::new(2,2, vec![1.0, 2.0, 0.0, 1.0]); let y = Vector::new(vec![3.0, 1.0]); let x = u.solve_u_triangular(y).expect("A solution should exist!"); assert!((x[0] - 1.0) < f32::EPSILON); assert!((x[1] - 1.0) < f32::EPSILON);
Panics
- Vector size and matrix column count are not equal.
Failures
- There is no valid solution to the system (matrix is singular).
- The matrix is empty.
fn solve_l_triangular(&self, y: Vector<T>) -> Result<Vector<T>, Error> where T: Any + Float
Solves a lower triangular linear system.
Given a matrix A
and a vector b
, this function returns the
solution of the lower triangular system Lx = b
, where L
is
the lower triangular part of A
.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix}; use rulinalg::vector::Vector; use std::f32; let l = Matrix::new(2,2, vec![1.0, 0.0, 2.0, 1.0]); let y = Vector::new(vec![1.0, 3.0]); let x = l.solve_l_triangular(y).expect("A solution should exist!"); println!("{:?}", x); assert!((x[0] - 1.0) < f32::EPSILON); assert!((x[1] - 1.0) < f32::EPSILON);
Panics
- Vector size and matrix column count are not equal.
Failures
- There is no valid solution to the system (matrix is singular).
- The matrix is empty.
fn split_at(&self, mid: usize, axis: Axes) -> (MatrixSlice<T>, MatrixSlice<T>)
Split the matrix at the specified axis returning two MatrixSlice
s.
Examples
use rulinalg::matrix::{Axes, Matrix, BaseMatrix}; let a = Matrix::new(3,3, vec![2.0; 9]); let (b,c) = a.split_at(1, Axes::Row);
fn sub_slice<'a>(&self,
start: [usize; 2],
rows: usize,
cols: usize)
-> MatrixSlice<'a, T> where T: 'a
start: [usize; 2],
rows: usize,
cols: usize)
-> MatrixSlice<'a, T> where T: 'a
Produce a MatrixSlice
from an existing matrix.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix, MatrixSlice}; let a = Matrix::new(3,3, (0..9).collect::<Vec<usize>>()); let slice = MatrixSlice::from_matrix(&a, [1,1], 2, 2); let new_slice = slice.sub_slice([0,0], 1, 1);
Implementors
impl<T> BaseMatrix<T> for Matrix<T>
impl<'a, T> BaseMatrix<T> for MatrixSlice<'a, T>
impl<'a, T> BaseMatrix<T> for MatrixSliceMut<'a, T>