Trait rusty_machine::linalg::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

Rows in the matrix.

Columns in the matrix.

Row stride in the matrix.

Top left index of the matrix.

Provided Methods

Returns true if the matrix contais no elements

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();

Get a reference to a point in the matrix without bounds checking.

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());

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);

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]);

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());
}

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.

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]);

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]);

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);

Convert the matrix struct into a owned Matrix.

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.

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.

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.

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.

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.

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.

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.

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]);

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();

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);

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.

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.

Split the matrix at the specified axis returning two MatrixSlices.

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);

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