Struct rusty_machine::linalg::matrix::Matrix [] [src]

pub struct Matrix<T> {
    // some fields omitted
}

The Matrix struct.

Can be instantiated with any type.

Methods

impl<T> Matrix<T>
[src]

fn new(rows: usize, cols: usize, data: Vec<T>) -> Matrix<T>

Constructor for Matrix struct.

Requires both the row and column dimensions.

Examples

use rusty_machine::linalg::matrix::Matrix;

let mat = Matrix::new(2,2, vec![1.0,2.0,3.0,4.0]);

assert_eq!(mat.rows(), 2);
assert_eq!(mat.cols(), 2);

fn rows(&self) -> usize

Returns the number of rows in the Matrix.

fn cols(&self) -> usize

Returns the number of columns in the Matrix.

fn data(&self) -> &Vec<T>

Returns a non-mutable reference to the underlying data.

fn into_vec(self) -> Vec<T>

Consumes the Matrix and returns the Vec of data.

impl<T: Copy> Matrix<T>
[src]

fn select_rows(&self, rows: &[usize]) -> Matrix<T>

Select rows from matrix

Examples

use rusty_machine::linalg::matrix::Matrix;

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

fn select_cols(&self, cols: &[usize]) -> Matrix<T>

Select columns from matrix

Examples

use rusty_machine::linalg::matrix::Matrix;

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

fn hcat(&self, m: &Matrix<T>) -> Matrix<T>

Horizontally concatenates two matrices. With self on the left.

Examples

use rusty_machine::linalg::matrix::Matrix;

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

fn vcat(&self, m: &Matrix<T>) -> Matrix<T>

Vertically concatenates two matrices. With self on top.

Examples

use rusty_machine::linalg::matrix::Matrix;

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

fn diag(&self) -> Vector<T>

Extract the diagonal of the matrix

Examples

use rusty_machine::linalg::matrix::Matrix;
use rusty_machine::linalg::vector::Vector;

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 apply(self, f: &Fn(T) -> T) -> Matrix<T>

Applies a function to each element in the matrix.

Examples

use rusty_machine::linalg::matrix::Matrix;
fn add_two(a: f64) -> f64 {
    a + 2f64
}

let a = Matrix::new(2, 2, vec![0.;4]);

let b = a.apply(&add_two);

assert_eq!(*b.data(), vec![2.0; 4]);

impl<T: Zero + One + Copy> Matrix<T>
[src]

fn zeros(rows: usize, cols: usize) -> Matrix<T>

Constructs matrix of all zeros.

Requires both the row and the column dimensions.

Examples

use rusty_machine::linalg::matrix::Matrix;

let mat = Matrix::<f64>::zeros(2,3);

fn ones(rows: usize, cols: usize) -> Matrix<T>

Constructs matrix of all ones.

Requires both the row and the column dimensions.

Examples

use rusty_machine::linalg::matrix::Matrix;

let mat = Matrix::<f64>::ones(2,3);

fn identity(size: usize) -> Matrix<T>

Constructs the identity matrix.

Requires the size of the matrix.

Examples

use rusty_machine::linalg::matrix::Matrix;

let I = Matrix::<f64>::identity(4);

fn from_diag(diag: &[T]) -> Matrix<T>

Constructs matrix with given diagonal.

Requires slice of diagonal elements.

Examples

use rusty_machine::linalg::matrix::Matrix;

let mat = Matrix::from_diag(&vec![1.0,2.0,3.0,4.0]);

fn transpose(&self) -> Matrix<T>

Tranposes the given matrix

Examples

use rusty_machine::linalg::matrix::Matrix;

let mat = Matrix::new(2,3, vec![1.0,2.0,3.0,4.0,5.0,6.0]);

let mt = mat.transpose();

impl<T: Copy + Zero + One + PartialEq> Matrix<T>
[src]

fn is_diag(&self) -> bool

Checks if matrix is diagonal.

Examples

use rusty_machine::linalg::matrix::Matrix;

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

impl<T: Copy + Zero + One + Add<T, Output=T>> Matrix<T>
[src]

fn sum_rows(&self) -> Vector<T>

The sum of the rows of the matrix.

Returns a Vector equal to the sum of the matrices rows.

Examples

use rusty_machine::linalg::matrix::Matrix;

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>

The sum of the columns of the matrix.

Returns a Vector equal to the sum of the matrices columns.

Examples

use rusty_machine::linalg::matrix::Matrix;

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

The sum of all elements in the matrix

Examples

use rusty_machine::linalg::matrix::Matrix;

let a = Matrix::new(2,2,vec![1.0,2.0,3.0,4.0]);

let c = a.sum();
assert_eq!(c, 10.0);

impl<T: Copy + Zero + Mul<T, Output=T>> Matrix<T>
[src]

fn elemul(&self, m: &Matrix<T>) -> Matrix<T>

The elementwise product of two matrices.

Examples

use rusty_machine::linalg::matrix::Matrix;

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

impl<T: Copy + Zero + Div<T, Output=T>> Matrix<T>
[src]

fn elediv(&self, m: &Matrix<T>) -> Matrix<T>

The elementwise division of two matrices.

Examples

use rusty_machine::linalg::matrix::Matrix;

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

impl<T: Copy + Zero + Float + FromPrimitive> Matrix<T>
[src]

fn mean(&self, axis: usize) -> Vector<T>

The mean of the matrix along the specified axis.

Axis 0 - Arithmetic mean of rows. Axis 1 - Arithmetic mean of columns.

Examples

use rusty_machine::linalg::matrix::Matrix;

let a = Matrix::<f64>::new(2,2, vec![1.0,2.0,3.0,4.0]);

let c = a.mean(0);
assert_eq!(*c.data(), vec![2.0, 3.0]);

let d = a.mean(1);
assert_eq!(*d.data(), vec![1.5, 3.5]);

fn variance(&self, axis: usize) -> Vector<T>

The variance of the matrix along the specified axis.

Axis 0 - Sample variance of rows. Axis 1 - Sample variance of columns.

Examples

use rusty_machine::linalg::matrix::Matrix;

let a = Matrix::<f32>::new(2,2,vec![1.0,2.0,3.0,4.0]);

let c = a.variance(0);
assert_eq!(*c.data(), vec![2.0, 2.0]);

let d = a.variance(1);
assert_eq!(*d.data(), vec![0.5, 0.5]);

impl<T> Matrix<T> where T: Copy + One + Zero + Neg<Output=T> + Add<T, Output=T> + Mul<T, Output=T> + Sub<T, Output=T> + Div<T, Output=T> + PartialOrd
[src]

fn solve(&self, y: Vector<T>) -> Vector<T>

Solves the equation Ax = y.

Requires a Vector y as input.

Examples

use rusty_machine::linalg::matrix::Matrix;
use rusty_machine::linalg::vector::Vector;

let a = Matrix::new(2,2, vec![2.0,3.0,1.0,2.0]);
let y = Vector::new(vec![13.0,8.0]);

let x = a.solve(y);

assert_eq!(*x.data(), vec![2.0, 3.0]);

fn inverse(&self) -> Matrix<T>

Computes the inverse of the matrix.

Examples

use rusty_machine::linalg::matrix::Matrix;

let a = Matrix::new(2,2, vec![2.,3.,1.,2.]);
let inv = a.inverse();

let I = a * inv;

assert_eq!(*I.data(), vec![1.0,0.0,0.0,1.0]);

fn det(&self) -> T

Computes the determinant of the matrix.

Examples

use rusty_machine::linalg::matrix::Matrix;

let a = Matrix::new(3,3, vec![1.0,2.0,0.0,
                              0.0,3.0,4.0,
                              5.0, 1.0, 2.0]);

let det = a.det();

fn lup_decomp(&self) -> (Matrix<T>, Matrix<T>, Matrix<T>)

Computes L, U, and P for LUP decomposition.

Returns L,U, and P respectively.

Examples

use rusty_machine::linalg::matrix::Matrix;

let a = Matrix::new(3,3, vec![1.0,2.0,0.0,
                              0.0,3.0,4.0,
                              5.0, 1.0, 2.0]);

let (l,u,p) = a.lup_decomp();

impl<T: Copy + Zero + Float> Matrix<T>
[src]

fn cholesky(&self) -> Matrix<T>

Cholesky decomposition

Returns the cholesky decomposition of a positive definite matrix.

Examples

use rusty_machine::linalg::matrix::Matrix;

let m = Matrix::new(3,3, vec![1.0,0.5,0.5,0.5,1.0,0.5,0.5,0.5,1.0]);

let l = m.cholesky();

Panics

  • Matrix is not square.
  • Matrix is not positive definite. (This should probably be a Failure not a Panic).

Trait Implementations

impl<T: Clone> Clone for Matrix<T>
[src]

fn clone(&self) -> Matrix<T>

Clones the Matrix.

fn clone_from(&mut self, source: &Self)
1.0.0

Performs copy-assignment from source. Read more

impl<T: Copy + One + Zero + Mul<T, Output=T>> Mul<T> for Matrix<T>
[src]

Multiplies matrix by scalar.

type Output = Matrix<T>

The resulting type after applying the * operator

fn mul(self, f: T) -> Matrix<T>

The method for the * operator

impl<'a, T: Copy + One + Zero + Mul<T, Output=T>> Mul<&'a T> for Matrix<T>
[src]

Multiplies matrix by scalar.

type Output = Matrix<T>

The resulting type after applying the * operator

fn mul(self, f: &T) -> Matrix<T>

The method for the * operator

impl<'a, T: Copy + One + Zero + Mul<T, Output=T>> Mul<T> for &'a Matrix<T>
[src]

Multiplies matrix by scalar.

type Output = Matrix<T>

The resulting type after applying the * operator

fn mul(self, f: T) -> Matrix<T>

The method for the * operator

impl<'a, 'b, T: Copy + One + Zero + Mul<T, Output=T>> Mul<&'b T> for &'a Matrix<T>
[src]

Multiplies matrix by scalar.

type Output = Matrix<T>

The resulting type after applying the * operator

fn mul(self, f: &T) -> Matrix<T>

The method for the * operator

impl<T: Copy + Zero + One + Mul<T, Output=T> + Add<T, Output=T>> Mul<Matrix<T>> for Matrix<T>
[src]

Multiplies matrix by matrix.

type Output = Matrix<T>

The resulting type after applying the * operator

fn mul(self, m: Matrix<T>) -> Matrix<T>

The method for the * operator

impl<'a, T: Copy + Zero + One + Mul<T, Output=T> + Add<T, Output=T>> Mul<Matrix<T>> for &'a Matrix<T>
[src]

Multiplies matrix by matrix.

type Output = Matrix<T>

The resulting type after applying the * operator

fn mul(self, m: Matrix<T>) -> Matrix<T>

The method for the * operator

impl<'a, T: Copy + Zero + One + Mul<T, Output=T> + Add<T, Output=T>> Mul<&'a Matrix<T>> for Matrix<T>
[src]

Multiplies matrix by matrix.

type Output = Matrix<T>

The resulting type after applying the * operator

fn mul(self, m: &Matrix<T>) -> Matrix<T>

The method for the * operator

impl<'a, 'b, T: Copy + Zero + One + Mul<T, Output=T> + Add<T, Output=T>> Mul<&'b Matrix<T>> for &'a Matrix<T>
[src]

Multiplies matrix by matrix.

type Output = Matrix<T>

The resulting type after applying the * operator

fn mul(self, m: &Matrix<T>) -> Matrix<T>

The method for the * operator

impl<T: Copy + Zero + One + Mul<T, Output=T> + Add<T, Output=T>> Mul<Vector<T>> for Matrix<T>
[src]

Multiplies matrix by vector.

type Output = Vector<T>

The resulting type after applying the * operator

fn mul(self, m: Vector<T>) -> Vector<T>

The method for the * operator

impl<'a, T: Copy + Zero + One + Mul<T, Output=T> + Add<T, Output=T>> Mul<Vector<T>> for &'a Matrix<T>
[src]

Multiplies matrix by vector.

type Output = Vector<T>

The resulting type after applying the * operator

fn mul(self, m: Vector<T>) -> Vector<T>

The method for the * operator

impl<'a, T: Copy + Zero + One + Mul<T, Output=T> + Add<T, Output=T>> Mul<&'a Vector<T>> for Matrix<T>
[src]

Multiplies matrix by vector.

type Output = Vector<T>

The resulting type after applying the * operator

fn mul(self, m: &Vector<T>) -> Vector<T>

The method for the * operator

impl<'a, 'b, T: Copy + One + Zero + Mul<T, Output=T> + Add<T, Output=T>> Mul<&'b Vector<T>> for &'a Matrix<T>
[src]

Multiplies matrix by vector.

type Output = Vector<T>

The resulting type after applying the * operator

fn mul(self, v: &Vector<T>) -> Vector<T>

The method for the * operator

impl<T: Copy + One + Zero + Add<T, Output=T>> Add<T> for Matrix<T>
[src]

Adds scalar to matrix.

type Output = Matrix<T>

The resulting type after applying the + operator

fn add(self, f: T) -> Matrix<T>

The method for the + operator

impl<'a, T: Copy + One + Zero + Add<T, Output=T>> Add<T> for &'a Matrix<T>
[src]

Adds scalar to matrix.

type Output = Matrix<T>

The resulting type after applying the + operator

fn add(self, f: T) -> Matrix<T>

The method for the + operator

impl<'a, T: Copy + One + Zero + Add<T, Output=T>> Add<&'a T> for Matrix<T>
[src]

Adds scalar to matrix.

type Output = Matrix<T>

The resulting type after applying the + operator

fn add(self, f: &T) -> Matrix<T>

The method for the + operator

impl<'a, 'b, T: Copy + One + Zero + Add<T, Output=T>> Add<&'b T> for &'a Matrix<T>
[src]

type Output = Matrix<T>

The resulting type after applying the + operator

fn add(self, f: &T) -> Matrix<T>

The method for the + operator

impl<T: Copy + One + Zero + Add<T, Output=T>> Add<Matrix<T>> for Matrix<T>
[src]

Adds matrix to matrix.

type Output = Matrix<T>

The resulting type after applying the + operator

fn add(self, f: Matrix<T>) -> Matrix<T>

The method for the + operator

impl<'a, T: Copy + One + Zero + Add<T, Output=T>> Add<Matrix<T>> for &'a Matrix<T>
[src]

Adds matrix to matrix.

type Output = Matrix<T>

The resulting type after applying the + operator

fn add(self, f: Matrix<T>) -> Matrix<T>

The method for the + operator

impl<'a, T: Copy + One + Zero + Add<T, Output=T>> Add<&'a Matrix<T>> for Matrix<T>
[src]

Adds matrix to matrix.

type Output = Matrix<T>

The resulting type after applying the + operator

fn add(self, f: &Matrix<T>) -> Matrix<T>

The method for the + operator

impl<'a, 'b, T: Copy + One + Zero + Add<T, Output=T>> Add<&'b Matrix<T>> for &'a Matrix<T>
[src]

Adds matrix to matrix.

type Output = Matrix<T>

The resulting type after applying the + operator

fn add(self, m: &Matrix<T>) -> Matrix<T>

The method for the + operator

impl<T: Copy + One + Zero + Sub<T, Output=T>> Sub<T> for Matrix<T>
[src]

Subtracts scalar from matrix.

type Output = Matrix<T>

The resulting type after applying the - operator

fn sub(self, f: T) -> Matrix<T>

The method for the - operator

impl<'a, T: Copy + One + Zero + Sub<T, Output=T>> Sub<&'a T> for Matrix<T>
[src]

Subtracts scalar from matrix.

type Output = Matrix<T>

The resulting type after applying the - operator

fn sub(self, f: &T) -> Matrix<T>

The method for the - operator

impl<'a, T: Copy + One + Zero + Sub<T, Output=T>> Sub<T> for &'a Matrix<T>
[src]

Subtracts scalar from matrix.

type Output = Matrix<T>

The resulting type after applying the - operator

fn sub(self, f: T) -> Matrix<T>

The method for the - operator

impl<'a, 'b, T: Copy + One + Zero + Sub<T, Output=T>> Sub<&'b T> for &'a Matrix<T>
[src]

Subtracts scalar from matrix.

type Output = Matrix<T>

The resulting type after applying the - operator

fn sub(self, f: &T) -> Matrix<T>

The method for the - operator

impl<T: Copy + One + Zero + Sub<T, Output=T>> Sub<Matrix<T>> for Matrix<T>
[src]

Subtracts matrix from matrix.

type Output = Matrix<T>

The resulting type after applying the - operator

fn sub(self, f: Matrix<T>) -> Matrix<T>

The method for the - operator

impl<'a, T: Copy + One + Zero + Sub<T, Output=T>> Sub<Matrix<T>> for &'a Matrix<T>
[src]

Subtracts matrix from matrix.

type Output = Matrix<T>

The resulting type after applying the - operator

fn sub(self, f: Matrix<T>) -> Matrix<T>

The method for the - operator

impl<'a, T: Copy + One + Zero + Sub<T, Output=T>> Sub<&'a Matrix<T>> for Matrix<T>
[src]

Subtracts matrix from matrix.

type Output = Matrix<T>

The resulting type after applying the - operator

fn sub(self, f: &Matrix<T>) -> Matrix<T>

The method for the - operator

impl<'a, 'b, T: Copy + One + Zero + Sub<T, Output=T>> Sub<&'b Matrix<T>> for &'a Matrix<T>
[src]

Subtracts matrix from matrix.

type Output = Matrix<T>

The resulting type after applying the - operator

fn sub(self, m: &Matrix<T>) -> Matrix<T>

The method for the - operator

impl<T: Copy + One + Zero + PartialEq + Div<T, Output=T>> Div<T> for Matrix<T>
[src]

Divides matrix by scalar.

type Output = Matrix<T>

The resulting type after applying the / operator

fn div(self, f: T) -> Matrix<T>

The method for the / operator

impl<'a, T: Copy + One + Zero + PartialEq + Div<T, Output=T>> Div<T> for &'a Matrix<T>
[src]

Divides matrix by scalar.

type Output = Matrix<T>

The resulting type after applying the / operator

fn div(self, f: T) -> Matrix<T>

The method for the / operator

impl<'a, T: Copy + One + Zero + PartialEq + Div<T, Output=T>> Div<&'a T> for Matrix<T>
[src]

Divides matrix by scalar.

type Output = Matrix<T>

The resulting type after applying the / operator

fn div(self, f: &T) -> Matrix<T>

The method for the / operator

impl<'a, 'b, T: Copy + One + Zero + PartialEq + Div<T, Output=T>> Div<&'b T> for &'a Matrix<T>
[src]

Divides matrix by scalar.

type Output = Matrix<T>

The resulting type after applying the / operator

fn div(self, f: &T) -> Matrix<T>

The method for the / operator

impl<T: Neg<Output=T> + Copy> Neg for Matrix<T>
[src]

Gets negative of matrix.

type Output = Matrix<T>

The resulting type after applying the - operator

fn neg(self) -> Matrix<T>

The method for the unary - operator

impl<'a, T: Neg<Output=T> + Copy> Neg for &'a Matrix<T>
[src]

Gets negative of matrix.

type Output = Matrix<T>

The resulting type after applying the - operator

fn neg(self) -> Matrix<T>

The method for the unary - operator

impl<T> Index<[usize; 2]> for Matrix<T>
[src]

Indexes matrix.

Takes row index first then column.

type Output = T

The returned type after indexing

fn index(&self, idx: [usize; 2]) -> &T

The method for the indexing (Foo[Bar]) operation

impl<T: Float> Metric<T> for Matrix<T>
[src]

fn norm(&self) -> T

Compute euclidean norm for matrix.

Examples

use rusty_machine::linalg::matrix::Matrix;
use rusty_machine::linalg::Metric;

let a = Matrix::new(2,1, vec![3.0,4.0]);
let c = a.norm();

assert_eq!(c, 5.0);