[][src]Trait smartcore::linalg::BaseMatrix

pub trait BaseMatrix<T: RealNumber>: Clone + Debug {
    type RowVector: BaseVector<T> + Clone + Debug;
    pub fn from_row_vector(vec: Self::RowVector) -> Self;
pub fn to_row_vector(self) -> Self::RowVector;
pub fn get(&self, row: usize, col: usize) -> T;
pub fn get_row_as_vec(&self, row: usize) -> Vec<T>;
pub fn get_row(&self, row: usize) -> Self::RowVector;
pub fn copy_row_as_vec(&self, row: usize, result: &mut Vec<T>);
pub fn get_col_as_vec(&self, col: usize) -> Vec<T>;
pub fn copy_col_as_vec(&self, col: usize, result: &mut Vec<T>);
pub fn set(&mut self, row: usize, col: usize, x: T);
pub fn eye(size: usize) -> Self;
pub fn zeros(nrows: usize, ncols: usize) -> Self;
pub fn ones(nrows: usize, ncols: usize) -> Self;
pub fn fill(nrows: usize, ncols: usize, value: T) -> Self;
pub fn shape(&self) -> (usize, usize);
pub fn h_stack(&self, other: &Self) -> Self;
pub fn v_stack(&self, other: &Self) -> Self;
pub fn matmul(&self, other: &Self) -> Self;
pub fn dot(&self, other: &Self) -> T;
pub fn slice(&self, rows: Range<usize>, cols: Range<usize>) -> Self;
pub fn approximate_eq(&self, other: &Self, error: T) -> bool;
pub fn add_mut(&mut self, other: &Self) -> &Self;
pub fn sub_mut(&mut self, other: &Self) -> &Self;
pub fn mul_mut(&mut self, other: &Self) -> &Self;
pub fn div_mut(&mut self, other: &Self) -> &Self;
pub fn div_element_mut(&mut self, row: usize, col: usize, x: T);
pub fn mul_element_mut(&mut self, row: usize, col: usize, x: T);
pub fn add_element_mut(&mut self, row: usize, col: usize, x: T);
pub fn sub_element_mut(&mut self, row: usize, col: usize, x: T);
pub fn add_scalar_mut(&mut self, scalar: T) -> &Self;
pub fn sub_scalar_mut(&mut self, scalar: T) -> &Self;
pub fn mul_scalar_mut(&mut self, scalar: T) -> &Self;
pub fn div_scalar_mut(&mut self, scalar: T) -> &Self;
pub fn transpose(&self) -> Self;
pub fn rand(nrows: usize, ncols: usize) -> Self;
pub fn norm2(&self) -> T;
pub fn norm(&self, p: T) -> T;
pub fn column_mean(&self) -> Vec<T>;
pub fn negative_mut(&mut self);
pub fn reshape(&self, nrows: usize, ncols: usize) -> Self;
pub fn copy_from(&mut self, other: &Self);
pub fn abs_mut(&mut self) -> &Self;
pub fn sum(&self) -> T;
pub fn max(&self) -> T;
pub fn min(&self) -> T;
pub fn softmax_mut(&mut self);
pub fn pow_mut(&mut self, p: T) -> &Self;
pub fn argmax(&self) -> Vec<usize>;
pub fn unique(&self) -> Vec<T>;
pub fn cov(&self) -> Self; pub fn add(&self, other: &Self) -> Self { ... }
pub fn sub(&self, other: &Self) -> Self { ... }
pub fn mul(&self, other: &Self) -> Self { ... }
pub fn div(&self, other: &Self) -> Self { ... }
pub fn add_scalar(&self, scalar: T) -> Self { ... }
pub fn sub_scalar(&self, scalar: T) -> Self { ... }
pub fn mul_scalar(&self, scalar: T) -> Self { ... }
pub fn div_scalar(&self, scalar: T) -> Self { ... }
pub fn negative(&self) -> Self { ... }
pub fn abs(&self) -> Self { ... }
pub fn max_diff(&self, other: &Self) -> T { ... }
pub fn pow(&mut self, p: T) -> Self { ... }
pub fn take(&self, index: &[usize], axis: u8) -> Self { ... } }

Generic matrix type.

Associated Types

type RowVector: BaseVector<T> + Clone + Debug[src]

Row vector that is associated with this matrix type, e.g. if we have an implementation of sparce matrix we should have an associated sparce vector type that represents a row in this matrix.

Loading content...

Required methods

pub fn from_row_vector(vec: Self::RowVector) -> Self[src]

Transforms row vector vec into a 1xM matrix.

pub fn to_row_vector(self) -> Self::RowVector[src]

Transforms 1-d matrix of 1xM into a row vector.

pub fn get(&self, row: usize, col: usize) -> T[src]

Get an element of the matrix.

  • row - row number
  • col - column number

pub fn get_row_as_vec(&self, row: usize) -> Vec<T>[src]

Get a vector with elements of the row'th row

  • row - row number

pub fn get_row(&self, row: usize) -> Self::RowVector[src]

Get the row'th row

  • row - row number

pub fn copy_row_as_vec(&self, row: usize, result: &mut Vec<T>)[src]

Copies a vector with elements of the row'th row into result

  • row - row number
  • result - receiver for the row

pub fn get_col_as_vec(&self, col: usize) -> Vec<T>[src]

Get a vector with elements of the col'th column

  • col - column number

pub fn copy_col_as_vec(&self, col: usize, result: &mut Vec<T>)[src]

Copies a vector with elements of the col'th column into result

  • col - column number
  • result - receiver for the col

pub fn set(&mut self, row: usize, col: usize, x: T)[src]

Set an element at col, row to x

pub fn eye(size: usize) -> Self[src]

Create an identity matrix of size size

pub fn zeros(nrows: usize, ncols: usize) -> Self[src]

Create new matrix with zeros of size nrows by ncols.

pub fn ones(nrows: usize, ncols: usize) -> Self[src]

Create new matrix with ones of size nrows by ncols.

pub fn fill(nrows: usize, ncols: usize, value: T) -> Self[src]

Create new matrix of size nrows by ncols where each element is set to value.

pub fn shape(&self) -> (usize, usize)[src]

Return the shape of an array.

pub fn h_stack(&self, other: &Self) -> Self[src]

Stack arrays in sequence vertically (row wise).

use smartcore::linalg::naive::dense_matrix::*;

let a = DenseMatrix::from_2d_array(&[&[1., 2., 3.], &[4., 5., 6.]]);
let b = DenseMatrix::from_2d_array(&[&[1., 2.], &[3., 4.]]);
let expected = DenseMatrix::from_2d_array(&[
    &[1., 2., 3., 1., 2.],
    &[4., 5., 6., 3., 4.]
]);

assert_eq!(a.h_stack(&b), expected);

pub fn v_stack(&self, other: &Self) -> Self[src]

Stack arrays in sequence horizontally (column wise).

use smartcore::linalg::naive::dense_matrix::*;

let a = DenseMatrix::from_array(1, 3, &[1., 2., 3.]);
let b = DenseMatrix::from_array(1, 3, &[4., 5., 6.]);
let expected = DenseMatrix::from_2d_array(&[
    &[1., 2., 3.],
    &[4., 5., 6.]
]);

assert_eq!(a.v_stack(&b), expected);

pub fn matmul(&self, other: &Self) -> Self[src]

Matrix product.

use smartcore::linalg::naive::dense_matrix::*;

let a = DenseMatrix::from_2d_array(&[&[1., 2.], &[3., 4.]]);
let expected = DenseMatrix::from_2d_array(&[
    &[7., 10.],
    &[15., 22.]
]);

assert_eq!(a.matmul(&a), expected);

pub fn dot(&self, other: &Self) -> T[src]

Vector dot product Both matrices should be of size 1xM

use smartcore::linalg::naive::dense_matrix::*;

let a = DenseMatrix::from_array(1, 3, &[1., 2., 3.]);
let b = DenseMatrix::from_array(1, 3, &[4., 5., 6.]);

assert_eq!(a.dot(&b), 32.);

pub fn slice(&self, rows: Range<usize>, cols: Range<usize>) -> Self[src]

Return a slice of the matrix.

  • rows - range of rows to return
  • cols - range of columns to return
use smartcore::linalg::naive::dense_matrix::*;

let m = DenseMatrix::from_2d_array(&[
            &[1., 2., 3., 1.],
            &[4., 5., 6., 3.],
            &[7., 8., 9., 5.]
        ]);
let expected = DenseMatrix::from_2d_array(&[&[2., 3.], &[5., 6.]]);
let result = m.slice(0..2, 1..3);
assert_eq!(result, expected);

pub fn approximate_eq(&self, other: &Self, error: T) -> bool[src]

Returns True if matrices are element-wise equal within a tolerance error.

pub fn add_mut(&mut self, other: &Self) -> &Self[src]

Add matrices, element-wise, overriding original matrix with result.

pub fn sub_mut(&mut self, other: &Self) -> &Self[src]

Subtract matrices, element-wise, overriding original matrix with result.

pub fn mul_mut(&mut self, other: &Self) -> &Self[src]

Multiply matrices, element-wise, overriding original matrix with result.

pub fn div_mut(&mut self, other: &Self) -> &Self[src]

Divide matrices, element-wise, overriding original matrix with result.

pub fn div_element_mut(&mut self, row: usize, col: usize, x: T)[src]

Divide single element of the matrix by x, write result to original matrix.

pub fn mul_element_mut(&mut self, row: usize, col: usize, x: T)[src]

Multiply single element of the matrix by x, write result to original matrix.

pub fn add_element_mut(&mut self, row: usize, col: usize, x: T)[src]

Add single element of the matrix to x, write result to original matrix.

pub fn sub_element_mut(&mut self, row: usize, col: usize, x: T)[src]

Subtract x from single element of the matrix, write result to original matrix.

pub fn add_scalar_mut(&mut self, scalar: T) -> &Self[src]

Add scalar to the matrix, override original matrix with result.

pub fn sub_scalar_mut(&mut self, scalar: T) -> &Self[src]

Subtract scalar from the elements of matrix, override original matrix with result.

pub fn mul_scalar_mut(&mut self, scalar: T) -> &Self[src]

Multiply scalar by the elements of matrix, override original matrix with result.

pub fn div_scalar_mut(&mut self, scalar: T) -> &Self[src]

Divide elements of the matrix by scalar, override original matrix with result.

pub fn transpose(&self) -> Self[src]

Reverse or permute the axes of the matrix, return new matrix.

pub fn rand(nrows: usize, ncols: usize) -> Self[src]

Create new nrows by ncols matrix and populate it with random samples from a uniform distribution over [0, 1).

pub fn norm2(&self) -> T[src]

Returns L2 norm.

pub fn norm(&self, p: T) -> T[src]

Returns matrix norm of order p.

pub fn column_mean(&self) -> Vec<T>[src]

Returns the average of the matrix columns.

pub fn negative_mut(&mut self)[src]

Numerical negative, element-wise. Overrides original matrix.

pub fn reshape(&self, nrows: usize, ncols: usize) -> Self[src]

Returns new matrix of shape nrows by ncols with data copied from original matrix.

use smartcore::linalg::naive::dense_matrix::*;

let a = DenseMatrix::from_array(1, 6, &[1., 2., 3., 4., 5., 6.]);
let expected = DenseMatrix::from_2d_array(&[
            &[1., 2., 3.],
            &[4., 5., 6.]
        ]);

assert_eq!(a.reshape(2, 3), expected);

pub fn copy_from(&mut self, other: &Self)[src]

Copies content of other matrix.

pub fn abs_mut(&mut self) -> &Self[src]

Calculate the absolute value element-wise. Overrides original matrix.

pub fn sum(&self) -> T[src]

Calculates sum of all elements of the matrix.

pub fn max(&self) -> T[src]

Calculates max of all elements of the matrix.

pub fn min(&self) -> T[src]

Calculates min of all elements of the matrix.

pub fn softmax_mut(&mut self)[src]

Calculates Softmax function. Overrides the matrix with result.

pub fn pow_mut(&mut self, p: T) -> &Self[src]

Raises elements of the matrix to the power of p

pub fn argmax(&self) -> Vec<usize>[src]

Returns the indices of the maximum values in each row.

use smartcore::linalg::naive::dense_matrix::*;
let a = DenseMatrix::from_array(2, 3, &[1., 2., 3., -5., -6., -7.]);

assert_eq!(a.argmax(), vec![2, 0]);

pub fn unique(&self) -> Vec<T>[src]

Returns vector with unique values from the matrix.

use smartcore::linalg::naive::dense_matrix::*;
let a = DenseMatrix::from_array(3, 3, &[1., 2., 2., -2., -6., -7., 2., 3., 4.]);

assert_eq!(a.unique(), vec![-7., -6., -2., 1., 2., 3., 4.]);

pub fn cov(&self) -> Self[src]

Calculates the covariance matrix

Loading content...

Provided methods

pub fn add(&self, other: &Self) -> Self[src]

Add matrices, element-wise

pub fn sub(&self, other: &Self) -> Self[src]

Subtract matrices, element-wise

pub fn mul(&self, other: &Self) -> Self[src]

Multiply matrices, element-wise

pub fn div(&self, other: &Self) -> Self[src]

Divide matrices, element-wise

pub fn add_scalar(&self, scalar: T) -> Self[src]

Add scalar to the matrix.

pub fn sub_scalar(&self, scalar: T) -> Self[src]

Subtract scalar from the elements of matrix.

pub fn mul_scalar(&self, scalar: T) -> Self[src]

Multiply scalar by the elements of matrix.

pub fn div_scalar(&self, scalar: T) -> Self[src]

Divide elements of the matrix by scalar.

pub fn negative(&self) -> Self[src]

Numerical negative, element-wise.

pub fn abs(&self) -> Self[src]

Calculate the absolute value element-wise.

pub fn max_diff(&self, other: &Self) -> T[src]

Calculates max(|a - b|) of two matrices

use smartcore::linalg::naive::dense_matrix::*;

let a = DenseMatrix::from_array(2, 3, &[1., 2., 3., 4., -5., 6.]);
let b = DenseMatrix::from_array(2, 3, &[2., 3., 4., 1., 0., -12.]);

assert_eq!(a.max_diff(&b), 18.);
assert_eq!(b.max_diff(&b), 0.);

pub fn pow(&mut self, p: T) -> Self[src]

Returns new matrix with elements raised to the power of p

pub fn take(&self, index: &[usize], axis: u8) -> Self[src]

Take elements from an array along an axis.

Loading content...

Implementors

impl<T: RealNumber> BaseMatrix<T> for DenseMatrix<T>[src]

type RowVector = Vec<T>

Loading content...