pub struct Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,{
pub nrows: usize,
pub ncols: usize,
/* private fields */
}Expand description
Helper method to swap to usizes General dense matrix
Fields§
§nrows: usizeNumber of rows
ncols: usizeNumber of columns
Implementations§
Source§impl<'a, T> Matrix<'a, T>where
T: MatrixElement + 'a,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
impl<'a, T> Matrix<'a, T>where
T: MatrixElement + 'a,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
pub fn determinant_helper(&self) -> T
pub fn matmul_helper(&self, other: &Self) -> Self
pub fn get_block_size(&self, other: &Self) -> usize
pub fn get_range_for_block_size(&self, other: &Self) -> RangeInclusive<usize>
Source§impl<'a, T> Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Printer functions for the matrix
impl<'a, T> Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Printer functions for the matrix
Sourcepub fn print(&self, decimals: usize)
pub fn print(&self, decimals: usize)
Prints out the matrix with however many decimals you want
§Examples
use sukker::Matrix;
let matrix: Matrix<i32> = Matrix::eye(2);
matrix.print(4);
Source§impl<'a, T> Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Implementations of all creatins of matrices
impl<'a, T> Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Implementations of all creatins of matrices
Sourcepub fn new(data: Vec<T>, shape: Shape) -> Result<Self, MatrixError>
pub fn new(data: Vec<T>, shape: Shape) -> Result<Self, MatrixError>
Creates a new matrix from a vector and the shape you want. Will default init if it does not work
§Examples
use sukker::Matrix;
let matrix = Matrix::new(vec![1.0,2.0,3.0,4.0], (2,2)).unwrap();
assert_eq!(matrix.size(), 4);
assert_eq!(matrix.shape(), (2,2));Sourcepub fn init(value: T, shape: Shape) -> Self
pub fn init(value: T, shape: Shape) -> Self
Initializes a matrix with the same value given from parameter ‘value’
§Examples
use sukker::Matrix;
let matrix = Matrix::init(4f32, (1,2));
assert_eq!(matrix.get_vec(), vec![4f32,4f32]);
assert_eq!(matrix.shape(), (1,2));Sourcepub fn eye(size: usize) -> Self
pub fn eye(size: usize) -> Self
Returns an eye matrix which for now is the same as the identity matrix
§Examples
use sukker::Matrix;
let matrix: Matrix<f32> = Matrix::eye(2);
assert_eq!(matrix.get_vec(), vec![1f32, 0f32, 0f32, 1f32]);
assert_eq!(matrix.shape(), (2,2));Sourcepub fn eye_like(matrix: &Self) -> Self
pub fn eye_like(matrix: &Self) -> Self
Produce an identity matrix in the same shape as an already existent matrix
Examples
Sourcepub fn identity(size: usize) -> Self
pub fn identity(size: usize) -> Self
Identity is same as eye, just for nerds
§Examples
use sukker::Matrix;
let matrix: Matrix<i64> = Matrix::identity(2);
assert_eq!(matrix.get_vec(), vec![1i64, 0i64, 0i64, 1i64]);
assert_eq!(matrix.shape(), (2,2));Sourcepub fn from_slice(arr: &[T], shape: Shape) -> Result<Self, MatrixError>
pub fn from_slice(arr: &[T], shape: Shape) -> Result<Self, MatrixError>
Tries to create a matrix from a slize and shape
§Examples
use sukker::Matrix;
let s = vec![1f32, 2f32, 3f32, 4f32];
let matrix = Matrix::from_slice(&s, (4,1)).unwrap();
assert_eq!(matrix.shape(), (4,1));Sourcepub fn zeros(shape: Shape) -> Self
pub fn zeros(shape: Shape) -> Self
Creates a matrix where all values are 0. All sizes are based on a shape
§Examples
use sukker::Matrix;
let matrix: Matrix<f64> = Matrix::zeros((4,1));
assert_eq!(matrix.shape(), (4,1));
assert_eq!(matrix.get(0,0).unwrap(), 0f64);
assert_eq!(matrix.size(), 4);Sourcepub fn ones(shape: Shape) -> Self
pub fn ones(shape: Shape) -> Self
Creates a matrix where all values are 1. All sizes are based on a shape
§Examples
use sukker::Matrix;
let matrix: Matrix<f64> = Matrix::ones((4,1));
assert_eq!(matrix.shape(), (4,1));
assert_eq!(matrix.get(0,0).unwrap(), 1f64);
assert_eq!(matrix.size(), 4);Sourcepub fn zeros_like(other: &Self) -> Self
pub fn zeros_like(other: &Self) -> Self
Creates a matrix where all values are 0. All sizes are based on an already existent matrix
§Examples
use sukker::Matrix;
let matrix1: Matrix<i8> = Matrix::default();
let matrix2 = Matrix::zeros_like(&matrix1);
assert_eq!(matrix2.shape(), matrix1.shape());
assert_eq!(matrix2.get(0,0).unwrap(), 0i8);Sourcepub fn ones_like(other: &Self) -> Self
pub fn ones_like(other: &Self) -> Self
Creates a matrix where all values are 1. All sizes are based on an already existent matrix
§Examples
use sukker::Matrix;
let matrix1: Matrix<i64> = Matrix::default();
let matrix2 = Matrix::ones_like(&matrix1);
assert_eq!(matrix2.shape(), matrix1.shape());
assert_eq!(1i64, matrix2.get(0,0).unwrap());Sourcepub fn random_like(matrix: &Self) -> Self
pub fn random_like(matrix: &Self) -> Self
Creates a matrix where all values are random between 0 and 1. All sizes are based on an already existent matrix
§Examples
use sukker::Matrix;
let matrix1: Matrix<f32> = Matrix::default();
let matrix2 = Matrix::random_like(&matrix1);
assert_eq!(matrix1.shape(), matrix2.shape());
Sourcepub fn randomize_range(start: T, end: T, shape: Shape) -> Self
pub fn randomize_range(start: T, end: T, shape: Shape) -> Self
Creates a matrix where all values are random between start..=end. Shape in new array is given through parameter ‘shape’
§Examples
use sukker::Matrix;
let matrix = Matrix::randomize_range(1f32, 2f32, (2,3));
let elem = matrix.get(1,1).unwrap();
assert_eq!(matrix.shape(), (2,3));
//assert!(elem >= 1f32 && 2f32 <= elem);Sourcepub fn randomize(shape: Shape) -> Self
pub fn randomize(shape: Shape) -> Self
Creates a matrix where all values are random between 0..=1. Shape in new array is given through parameter ‘shape’
§Examples
use sukker::Matrix;
let matrix: Matrix<f64> = Matrix::randomize((2,3));
assert_eq!(matrix.shape(), (2,3));Sourcepub fn from_file(path: &'static str) -> Result<Self, MatrixError>
pub fn from_file(path: &'static str) -> Result<Self, MatrixError>
Parses from file, but will return a default matrix if nothing is given
§Examples
use sukker::Matrix;
// let m: Matrix<f32> = Matrix::from_file("../../test.txt").unwrap();
// m.print(4);Sourcepub fn from_sparse(sparse: SparseMatrix<'a, T>) -> Self
pub fn from_sparse(sparse: SparseMatrix<'a, T>) -> Self
Constructs a new dense matrix from a sparse one.
This transfesrs ownership as well!
Examples
use sukker::{Matrix, SparseMatrix};
let sparse = SparseMatrix::<i32>::eye(3);
let matrix = Matrix::from_sparse(sparse);
assert_eq!(matrix.shape(), (3,3));
assert_eq!(matrix.at(0,0), 1);Source§impl<'a, T> Matrix<'a, T>where
T: MatrixElement + Div<Output = T> + Sum<T>,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Regular matrix methods that are not operating math on them
impl<'a, T> Matrix<'a, T>where
T: MatrixElement + Div<Output = T> + Sum<T>,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Regular matrix methods that are not operating math on them
Sourcepub fn reshape(&mut self, nrows: usize, ncols: usize)
pub fn reshape(&mut self, nrows: usize, ncols: usize)
Reshapes a matrix if possible. If the shapes don’t match up, the old shape will be retained
§Examples
use sukker::Matrix;
let mut matrix = Matrix::init(10.5, (2,3));
matrix.reshape(3,2);
assert_eq!(matrix.shape(), (3,2));Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Get the total size of the matrix
§Examples
use sukker::Matrix;
let matrix = Matrix::init(10.5, (2,3));
assert_eq!(matrix.size(), 6);Sourcepub fn get(&self, i: usize, j: usize) -> Option<T>
pub fn get(&self, i: usize, j: usize) -> Option<T>
Gets element based on is and js
§Examples
use sukker::Matrix;
let matrix = Matrix::init(10.5f32, (2,3));
assert_eq!(matrix.get(0,1).unwrap(), 10.5f32);Sourcepub fn at(&self, i: usize, j: usize) -> T
pub fn at(&self, i: usize, j: usize) -> T
Gets element based on is and js, but will panic if indexes are out of range.
§Examples
use sukker::Matrix;
let val = 10.5;
let matrix = Matrix::init(val, (2,3));
assert_eq!(matrix.at(1,2), val);Sourcepub fn get_vec_slice(&self, start_idx: Shape, size: Shape) -> Vec<T>
pub fn get_vec_slice(&self, start_idx: Shape, size: Shape) -> Vec<T>
Gets a piece of the matrix out as a vector
If some indeces are out of bounds, the vec up until that point will be returned
§Examples
use sukker::Matrix;
let matrix = Matrix::init(10.5, (4,4));
let slice = matrix.get_vec_slice((1,1), (2,2));
assert_eq!(slice, vec![10.5,10.5,10.5,10.5]);Sourcepub fn get_vec(&self) -> Vec<T>
pub fn get_vec(&self) -> Vec<T>
Gets you the whole entire matrix as a vector
§Examples
use sukker::Matrix;
let matrix = Matrix::init(10.5, (4,4));
let slice = matrix.get_vec_slice((1,1), (2,2));
assert_eq!(slice, vec![10.5,10.5,10.5,10.5]);Sourcepub fn get_sub_matrix(
&self,
start_idx: Shape,
size: Shape,
) -> Result<Self, MatrixError>
pub fn get_sub_matrix( &self, start_idx: Shape, size: Shape, ) -> Result<Self, MatrixError>
Gets a piece of the matrix out as a matrix
If some indeces are out of bounds, unlike get_vec_slice
this function will return an IndexOutOfBoundsError
and will not return data
§Examples
use sukker::Matrix;
let matrix = Matrix::init(10.5, (4,4));
let sub_matrix = matrix.get_sub_matrix((1,1), (2,2)).unwrap();
assert_eq!(sub_matrix.get_vec(), vec![10.5,10.5,10.5,10.5]);Sourcepub fn concat(&self, other: &Self, dim: Dimension) -> Result<Self, MatrixError>
pub fn concat(&self, other: &Self, dim: Dimension) -> Result<Self, MatrixError>
Concat two mtrices on a dimension
§Examples
use sukker::Matrix;
use sukker::Dimension;
let matrix = Matrix::init(10.5, (4,4));
let matrix2 = Matrix::init(10.5, (1,4));
let res = matrix.concat(&matrix2, Dimension::Row).unwrap();
assert_eq!(res.shape(), (5,4));Sourcepub fn extend(&mut self, other: &Self, dim: Dimension)
pub fn extend(&mut self, other: &Self, dim: Dimension)
Extend a matrix with another on a dimension
§Examples
use sukker::Matrix;
use sukker::Dimension;
let mut matrix = Matrix::init(10.5, (4,4));
let matrix2 = Matrix::init(10.5, (4,1));
matrix.extend(&matrix2, Dimension::Col);
assert_eq!(matrix.shape(), (4,5));Sourcepub fn set(&mut self, value: T, idx: Shape)
pub fn set(&mut self, value: T, idx: Shape)
Sets element based on is and js
Sets nothing if you;re out of bounds
§Examples
use sukker::Matrix;
let mut matrix = Matrix::init(10.5, (2,3));
matrix.set(11.5, (1, 2));
assert_eq!(matrix.get(1,2).unwrap(), 11.5);Sourcepub fn set_many(&mut self, idx_list: Vec<Shape>, value: T)
pub fn set_many(&mut self, idx_list: Vec<Shape>, value: T)
Sets many elements based on vector of indeces
For indexes out of bounds, nothing is set
§Examples
use sukker::Matrix;
let mut matrix = Matrix::init(10.5, (2,3));
matrix.set_many(vec![(1,2), (1,1)], 11.5);
assert_eq!(matrix.get(1,2).unwrap(), 11.5);
assert_eq!(matrix.get(1,1).unwrap(), 11.5);
assert_eq!(matrix.get(0,1).unwrap(), 10.5);Sourcepub fn set_range(&mut self, start: usize, stop: usize, value: T)
pub fn set_range(&mut self, start: usize, stop: usize, value: T)
Sets all elements of a matrix in a 1d range.
The range is inclusive to stop, and will panic if any indexes are out of range
§Examples
use sukker::Matrix;
let mut matrix = Matrix::init(10.5, (2,3));
matrix.set_range(0, 3, 11.5);
assert_eq!(matrix.get(0,2).unwrap(), 11.5);
assert_eq!(matrix.get(0,1).unwrap(), 11.5);
assert_eq!(matrix.get(1,1).unwrap(), 10.5);Sourcepub fn one_to_2d_idx(&self, idx: usize) -> Shape
pub fn one_to_2d_idx(&self, idx: usize) -> Shape
Calculates the (row, col) for a matrix by a single index
§Examples
use sukker::Matrix;
let matrix = Matrix::init(10.5, (2,2));
let inv = matrix.one_to_2d_idx(1);
assert_eq!(inv, (0,1));Sourcepub fn max(&self) -> T
pub fn max(&self) -> T
Finds maximum element in the matrix
§Examples
use sukker::Matrix;
let matrix = Matrix::init(10.5, (2,3));
assert_eq!(matrix.max(), 10.5);Sourcepub fn min(&self) -> T
pub fn min(&self) -> T
Finds minimum element in the matrix
§Examples
use sukker::Matrix;
let mut matrix = Matrix::init(10.5, (2,3));
matrix.set(1.0, (0,2));
assert_eq!(matrix.min(), 1.0);Sourcepub fn cumsum(&self) -> T
pub fn cumsum(&self) -> T
Finds total sum of matrix
§Examples
use sukker::Matrix;
let matrix = Matrix::init(10f32, (2,2));
assert_eq!(matrix.cumsum(), 40.0);Sourcepub fn cumprod(&self) -> T
pub fn cumprod(&self) -> T
Multiplies all elements in matrix
§Examples
use sukker::Matrix;
let matrix = Matrix::init(10f32, (2,2));
assert_eq!(matrix.cumprod(), 10000.0);Sourcepub fn avg(&self) -> T
pub fn avg(&self) -> T
Gets the average of the matrix
§Examples
use sukker::Matrix;
let matrix = Matrix::init(10f32, (2,2));
assert_eq!(matrix.avg(), 10.0);Sourcepub fn mean(&self) -> T
pub fn mean(&self) -> T
Gets the mean of the matrix
§Examples
use sukker::Matrix;
let matrix = Matrix::init(10f32, (2,2));
assert_eq!(matrix.mean(), 10.0);Sourcepub fn median(&self) -> T
pub fn median(&self) -> T
Gets the median of the matrix
§Examples
use sukker::Matrix;
let matrix = Matrix::new(vec![1.0, 4.0, 6.0, 5.0], (2,2)).unwrap();
assert!(matrix.median() >= 4.45 && matrix.median() <= 4.55);Sourcepub fn sum(&self, rowcol: usize, dimension: Dimension) -> T
pub fn sum(&self, rowcol: usize, dimension: Dimension) -> T
Sums up elements over given axis and dimension. Will return 0 if you’re out of bounds
sum(2, Dimension::Col) means summig up these ones
[ 10 10 (10) 10 10 10 10 (10) 10 10 10 10 (10) 10 10 10 10 (10) 10 10 10 10 (10) 10 10 ]
= 10 * 5 = 50
§Examples
use sukker::Matrix;
use sukker::Dimension;
let matrix = Matrix::init(10f32, (5,5));
assert_eq!(matrix.sum(0, Dimension::Row), 50.0);
assert_eq!(matrix.sum(3, Dimension::Col), 50.0);Sourcepub fn prod(&self, rowcol: usize, dimension: Dimension) -> T
pub fn prod(&self, rowcol: usize, dimension: Dimension) -> T
Prods up elements over given rowcol and dimension Will return 1 if you’re out of bounds.
See sum for example on how this is calculated
§Examples
use sukker::Matrix;
use sukker::Dimension;
let matrix = Matrix::init(10f32, (2,2));
assert_eq!(matrix.prod(0, Dimension::Row), 100.0);
assert_eq!(matrix.prod(0, Dimension::Col), 100.0);Source§impl<'a, T> Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
trait MatrixLinAlg contains all common Linear Algebra functions to be
performed on matrices
impl<'a, T> Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
trait MatrixLinAlg contains all common Linear Algebra functions to be performed on matrices
Sourcepub fn add(&self, other: &Self) -> Result<Self, MatrixError>
pub fn add(&self, other: &Self) -> Result<Self, MatrixError>
Adds one matrix to another
§Examples
use sukker::Matrix;
let matrix1 = Matrix::init(10.0, (2,2));
let matrix2 = Matrix::init(10.0, (2,2));
assert_eq!(matrix1.add(&matrix2).unwrap().get(0,0).unwrap(), 20.0);Sourcepub fn sub(&self, other: &Self) -> Result<Self, MatrixError>
pub fn sub(&self, other: &Self) -> Result<Self, MatrixError>
Subtracts one matrix from another
§Examples
use sukker::Matrix;
let matrix1 = Matrix::init(10.0, (2,2));
let matrix2 = Matrix::init(10.0, (2,2));
assert_eq!(matrix1.sub(&matrix2).unwrap().get(1,0).unwrap(), 0.0);Sourcepub fn sub_abs(&self, other: &Self) -> Result<Self, MatrixError>
pub fn sub_abs(&self, other: &Self) -> Result<Self, MatrixError>
Subtracts one array from another and returns the absolute value
§Examples
use sukker::Matrix;
let matrix1 = Matrix::init(10.0f32, (2,2));
let matrix2 = Matrix::init(15.0f32, (2,2));
assert_eq!(matrix1.sub_abs(&matrix2).unwrap().get(0,0).unwrap(), 5.0);Sourcepub fn mul(&self, other: &Self) -> Result<Self, MatrixError>
pub fn mul(&self, other: &Self) -> Result<Self, MatrixError>
Dot product of two matrices
§Examples
use sukker::Matrix;
let matrix1 = Matrix::init(20.0, (2,2));
let matrix2 = Matrix::init(10.0, (2,2));
assert_eq!(matrix1.mul(&matrix2).unwrap().get(0,0).unwrap(), 200.0);Sourcepub fn dot(&self, other: &Self) -> Result<Self, MatrixError>
pub fn dot(&self, other: &Self) -> Result<Self, MatrixError>
Dot product of two matrices
§Examples
use sukker::Matrix;
let matrix1 = Matrix::init(20.0, (2,2));
let matrix2 = Matrix::init(10.0, (2,2));
assert_eq!(matrix1.dot(&matrix2).unwrap().get(0,0).unwrap(), 200.0);Sourcepub fn div(&self, other: &Self) -> Result<Self, MatrixError>
pub fn div(&self, other: &Self) -> Result<Self, MatrixError>
Bad handling of zero div
§Examples
use sukker::Matrix;
let matrix1 = Matrix::init(20.0, (2,2));
let matrix2 = Matrix::init(10.0, (2,2));
assert_eq!(matrix1.div(&matrix2).unwrap().get(0,0).unwrap(), 2.0);Sourcepub fn neg(&self) -> Self
pub fn neg(&self) -> Self
Negates every value in the matrix
§Examples
use sukker::{Matrix, LinAlgFloats};
let matrix = Matrix::<f32>::ones((2,2));
let negated = matrix.neg();
assert_eq!(negated.all(|&e| e == -1.0), true);Sourcepub fn add_val(&self, val: T) -> Self
pub fn add_val(&self, val: T) -> Self
Adds a value to a matrix and returns a new matrix
§Examples
use sukker::Matrix;
let matrix = Matrix::init(20.0, (2,2));
let value: f32 = 2.0;
assert_eq!(matrix.add_val(value).get(0,0).unwrap(), 22.0);Sourcepub fn sub_val(&self, val: T) -> Self
pub fn sub_val(&self, val: T) -> Self
Substracts a value to a matrix and returns a new matrix
§Examples
use sukker::Matrix;
let matrix = Matrix::init(20.0, (2,2));
let value: f32 = 2.0;
assert_eq!(matrix.sub_val(value).get(0,0).unwrap(), 18.0);Sourcepub fn mul_val(&self, val: T) -> Self
pub fn mul_val(&self, val: T) -> Self
Multiplies a value to a matrix and returns a new matrix
§Examples
use sukker::Matrix;
let matrix = Matrix::init(20.0, (2,2));
let value: f32 = 2.0;
assert_eq!(matrix.mul_val(value).get(0,0).unwrap(), 40.0);Sourcepub fn div_val(&self, val: T) -> Self
pub fn div_val(&self, val: T) -> Self
Divides a value to a matrix and returns a new matrix
§Examples
use sukker::Matrix;
let matrix = Matrix::init(20.0, (2,2));
let value: f32 = 2.0;
let result_mat = matrix.div_val(value);
assert_eq!(result_mat.get(0,0).unwrap(), 10.0);Sourcepub fn pow(&self, val: usize) -> Self
pub fn pow(&self, val: usize) -> Self
Pows each value in a matrix by val times
§Examples
use sukker::Matrix;
let matrix = Matrix::init(2.0, (2,2));
let result_mat = matrix.pow(2);
assert_eq!(result_mat.get_vec(), vec![4.0, 4.0, 4.0, 4.0]);Sourcepub fn abs(&self) -> Self
pub fn abs(&self) -> Self
Takes the absolute values of the matrix
§Examples
use sukker::Matrix;
let matrix = Matrix::init(-20.0, (2,2));
let res = matrix.abs();
assert_eq!(res.all(|&e| e == 20.0), true);Sourcepub fn exp(&self, n: usize) -> Option<Self>
pub fn exp(&self, n: usize) -> Option<Self>
Multiply a matrix with itself n number of times. This is done by performing a matrix multiplication several time on self and the result of mat.exp(i-1).
If matrix is not in form NxN, this function returns None
Examples
use sukker::Matrix;
let a = Matrix::<i32>::init(2, (2,2));
let res = a.exp(3).unwrap();
assert_eq!(res.all(|&e| e == 32), true);Sourcepub fn add_self(&mut self, other: &Self)
pub fn add_self(&mut self, other: &Self)
Adds a matrix in-place to a matrix
§Examples
use sukker::Matrix;
let mut matrix1 = Matrix::init(20.0, (2,2));
let matrix2 = Matrix::init(2.0, (2,2));
matrix1.add_self(&matrix2);
assert_eq!(matrix1.get(0,0).unwrap(), 22.0);Sourcepub fn sub_self(&mut self, other: &Self)
pub fn sub_self(&mut self, other: &Self)
Subtracts a matrix in-place to a matrix
§Examples
use sukker::Matrix;
let mut matrix1 = Matrix::init(20.0, (2,2));
let matrix2 = Matrix::init(2.0, (2,2));
matrix1.sub_self(&matrix2);
assert_eq!(matrix1.get(0,0).unwrap(), 18.0);Sourcepub fn mul_self(&mut self, other: &Self)
pub fn mul_self(&mut self, other: &Self)
Multiplies a matrix in-place to a matrix
§Examples
use sukker::Matrix;
let mut matrix1 = Matrix::init(20.0, (2,2));
let matrix2 = Matrix::init(2.0, (2,2));
matrix1.mul_self(&matrix2);
assert_eq!(matrix1.get(0,0).unwrap(), 40.0);Sourcepub fn div_self(&mut self, other: &Self)
pub fn div_self(&mut self, other: &Self)
Divides a matrix in-place to a matrix
§Examples
use sukker::Matrix;
let mut matrix1 = Matrix::init(20.0, (2,2));
let matrix2 = Matrix::init(2.0, (2,2));
matrix1.div_self(&matrix2);
assert_eq!(matrix1.get(0,0).unwrap(), 10.0);Sourcepub fn abs_self(&mut self)
pub fn abs_self(&mut self)
Abs matrix in-place to a matrix
§Examples
use sukker::Matrix;
let mut matrix = Matrix::init(20.0, (2,2));
matrix.abs_self()
// assert_eq!(matrix1.get(0,0).unwrap(), 22.0);Sourcepub fn add_val_self(&mut self, val: T)
pub fn add_val_self(&mut self, val: T)
Adds a value in-place to a matrix
§Examples
use sukker::Matrix;
let mut matrix = Matrix::init(20.0, (2,2));
let value: f32 = 2.0;
matrix.add_val_self(value);
assert_eq!(matrix.get(0,0).unwrap(), 22.0);Sourcepub fn sub_val_self(&mut self, val: T)
pub fn sub_val_self(&mut self, val: T)
Subtracts a value in-place to a matrix
§Examples
use sukker::Matrix;
let mut matrix = Matrix::init(20.0, (2,2));
let value: f32 = 2.0;
matrix.sub_val_self(value);
assert_eq!(matrix.get(0,0).unwrap(), 18.0);Sourcepub fn mul_val_self(&mut self, val: T)
pub fn mul_val_self(&mut self, val: T)
Mults a value in-place to a matrix
§Examples
use sukker::Matrix;
let mut matrix = Matrix::init(20.0, (2,2));
let value: f32 = 2.0;
matrix.mul_val_self(value);
assert_eq!(matrix.get(0,0).unwrap(), 40.0);Sourcepub fn div_val_self(&mut self, val: T)
pub fn div_val_self(&mut self, val: T)
Divs a value in-place to a matrix
§Examples
use sukker::Matrix;
let mut matrix = Matrix::init(20.0, (2,2));
let value: f32 = 2.0;
matrix.div_val_self(value);
assert_eq!(matrix.get(0,0).unwrap(), 10.0);Sourcepub fn matmul(&self, other: &Self) -> Result<Self, MatrixError>
pub fn matmul(&self, other: &Self) -> Result<Self, MatrixError>
Transposed matrix multiplications
§Examples
use sukker::Matrix;
let mut matrix1 = Matrix::init(2.0, (2,4));
let matrix2 = Matrix::init(2.0, (4,2));
let result = matrix1.matmul(&matrix2).unwrap();
assert_eq!(result.get(0,0).unwrap(), 16.0);
assert_eq!(result.shape(), (2,2));Sourcepub fn mm(&self, other: &Self) -> Result<Self, MatrixError>
pub fn mm(&self, other: &Self) -> Result<Self, MatrixError>
Shorthand method for matmul
Sourcepub fn determinant(&self) -> Option<T>
pub fn determinant(&self) -> Option<T>
Get’s the determinat of a N x N matrix
Examples
use sukker::Matrix;
let mat: Matrix<i32> = Matrix::new(vec![1,3,5,9,1,3,1,7,4,3,9,7,5,2,0,9], (4,4)).unwrap();
let res = mat.determinant().unwrap();
assert_eq!(res, -376);Sourcepub fn inverse(&self) -> Option<Self>
pub fn inverse(&self) -> Option<Self>
Finds the inverse of a matrix if possible
Definition: AA^-1 = A^-1A = I
Examples
use sukker::Matrix;
let matrix = Matrix::new(vec![4,7,2,6], (2,2)).unwrap();
// let inverse = matrix.inverse();
Sourcepub fn transpose(&mut self)
pub fn transpose(&mut self)
Transpose a matrix in-place
§Examples
use sukker::Matrix;
let mut matrix = Matrix::init(2.0, (2,100));
matrix.transpose();
assert_eq!(matrix.shape(), (100,2));Sourcepub fn t(&mut self)
pub fn t(&mut self)
Shorthand call for transpose
§Examples
use sukker::Matrix;
let mut matrix = Matrix::init(2.0, (2,100));
matrix.t();
assert_eq!(matrix.shape(), (100,2));Sourcepub fn transpose_copy(&self) -> Self
pub fn transpose_copy(&self) -> Self
Transpose a matrix and return a copy
§Examples
use sukker::Matrix;
let matrix = Matrix::init(2.0, (2,100));
let result = matrix.transpose_copy();
assert_eq!(result.shape(), (100,2));Source§impl<'a, T> Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Implementations for predicates
impl<'a, T> Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Implementations for predicates
Sourcepub fn count_where<F>(&'a self, pred: F) -> usize
pub fn count_where<F>(&'a self, pred: F) -> usize
Counts all occurances where predicate holds
§Examples
use sukker::Matrix;
let matrix = Matrix::init(2.0f32, (2,4));
assert_eq!(matrix.count_where(|&e| e == 2.0), 8);Sourcepub fn sum_where<F>(&self, pred: F) -> T
pub fn sum_where<F>(&self, pred: F) -> T
Sums all occurances where predicate holds
§Examples
use sukker::Matrix;
let matrix = Matrix::init(2.0, (2,4));
assert_eq!(matrix.sum_where(|&e| e == 2.0), 16.0);Sourcepub fn set_where<P>(&mut self, pred: P)
pub fn set_where<P>(&mut self, pred: P)
Setsall elements where predicate holds true. The new value is to be set inside the predicate as well
§Examples
use sukker::Matrix;
let mut matrix = Matrix::init(2.0, (2,4));
assert_eq!(matrix.get(0,0).unwrap(), 2.0);
matrix.set_where(|e| {
if *e == 2.0 {
*e = 2.3;
}
});
assert_eq!(matrix.get(0,0).unwrap(), 2.3);Sourcepub fn any<F>(&self, pred: F) -> bool
pub fn any<F>(&self, pred: F) -> bool
Return whether or not a predicate holds at least once
§Examples
use sukker::Matrix;
let matrix = Matrix::init(2.0, (2,4));
assert_eq!(matrix.any(|&e| e == 2.0), true);Sourcepub fn all<F>(&self, pred: F) -> bool
pub fn all<F>(&self, pred: F) -> bool
Returns whether or not predicate holds for all values
§Examples
use sukker::Matrix;
let matrix = Matrix::randomize_range(1.0, 4.0, (2,4));
assert_eq!(matrix.all(|&e| e >= 1.0), true);Trait Implementations§
Source§impl<'a, T> Clone for Matrix<'a, T>where
T: MatrixElement + Clone,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
impl<'a, T> Clone for Matrix<'a, T>where
T: MatrixElement + Clone,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Source§impl<'a, T> Debug for Matrix<'a, T>where
T: MatrixElement + Debug,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
impl<'a, T> Debug for Matrix<'a, T>where
T: MatrixElement + Debug,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Source§impl<'a, T> Default for Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
impl<'a, T> Default for Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Source§impl<'de, 'a, T> Deserialize<'de> for Matrix<'a, T>where
T: MatrixElement + Deserialize<'de>,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
impl<'de, 'a, T> Deserialize<'de> for Matrix<'a, T>where
T: MatrixElement + Deserialize<'de>,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<'a, T> Display for Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
impl<'a, T> Display for Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Source§impl<'a, T> Error for Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
impl<'a, T> Error for Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl<'a, T> FromStr for Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
impl<'a, T> FromStr for Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Source§impl<'a, T> LinAlgFloats<'a, T> for Matrix<'a, T>where
T: MatrixElement + Float + 'a,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Linalg on floats
impl<'a, T> LinAlgFloats<'a, T> for Matrix<'a, T>where
T: MatrixElement + Float + 'a,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Linalg on floats
Source§fn log(&self, base: T) -> Self
fn log(&self, base: T) -> Self
Takes the logarithm of each element
§Examples
use sukker::{Matrix, LinAlgFloats};
let matrix = Matrix::init(10.0, (2,2));
let result = matrix.log(10.0);
assert_eq!(result.all(|&e| e == 1.0), true);
Source§fn ln(&self) -> Self
fn ln(&self) -> Self
Takes the natural logarithm of each element in a matrix
§Examples
use sukker::{Matrix, LinAlgFloats};
use sukker::constants::EF64;
let matrix: Matrix<f64> = Matrix::init(EF64, (2,2));
let res = matrix.ln();Source§fn sqrt(&self) -> Self
fn sqrt(&self) -> Self
Takes the square root of each element in a matrix. If some elements are negative, these will be kept the same
§Examples
use sukker::{Matrix, LinAlgFloats};
let matrix: Matrix<f64> = Matrix::init(9.0, (3,3));
let res = matrix.sqrt();
assert_eq!(res.all(|&e| e == 3.0), true);Source§fn sin(&self) -> Self
fn sin(&self) -> Self
Gets sin of every value
§Examples
use sukker::{Matrix, LinAlgFloats};
let matrix = Matrix::init(1.0, (2,2));
let res = matrix.sin();Source§fn cos(&self) -> Self
fn cos(&self) -> Self
Gets cos of every value
§Examples
use sukker::{Matrix, LinAlgFloats};
use sukker::constants::EF32;
let matrix = Matrix::init(EF32, (2,2));
let res = matrix.cos();Source§fn tan(&self) -> Self
fn tan(&self) -> Self
Gets tan of every value
§Examples
use sukker::{Matrix, LinAlgFloats};
use sukker::constants::EF32;
let matrix = Matrix::init(EF32, (2,2));
let res = matrix.tan();Source§fn sinh(&self) -> Self
fn sinh(&self) -> Self
Gets sinh of every value
§Examples
use sukker::{Matrix, LinAlgFloats};
use sukker::constants::EF32;
let matrix = Matrix::init(EF32, (2,2));
let res = matrix.sinh();Source§fn cosh(&self) -> Self
fn cosh(&self) -> Self
Gets cosh of every value
§Examples
use sukker::{Matrix, LinAlgFloats};
use sukker::constants::EF32;
let matrix = Matrix::init(EF32, (2,2));
let res = matrix.cosh();Source§fn tanh(&self) -> Self
fn tanh(&self) -> Self
Gets tanh of every value
§Examples
use sukker::{Matrix, LinAlgFloats};
use sukker::constants::EF32;
let matrix = Matrix::init(EF32, (2,2));
let res = matrix.tanh();Source§fn get_eigenvalues(&self) -> Option<Vec<T>>
fn get_eigenvalues(&self) -> Option<Vec<T>>
Find the eigenvale of a matrix
§Examples
use sukker::Matrix;
let mut matrix = Matrix::init(2.0, (2,100));
Source§fn get_eigenvectors(&self) -> Option<Vec<T>>
fn get_eigenvectors(&self) -> Option<Vec<T>>
Find the eigenvectors
Source§impl<'a, T> PartialEq for Matrix<'a, T>where
T: MatrixElement + PartialEq,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
impl<'a, T> PartialEq for Matrix<'a, T>where
T: MatrixElement + PartialEq,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Source§impl<'a, T> PartialOrd for Matrix<'a, T>where
T: MatrixElement + PartialOrd,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
impl<'a, T> PartialOrd for Matrix<'a, T>where
T: MatrixElement + PartialOrd,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Source§impl<'a, T> Serialize for Matrix<'a, T>where
T: MatrixElement + Serialize,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
impl<'a, T> Serialize for Matrix<'a, T>where
T: MatrixElement + Serialize,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
impl<'a, T> Send for Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
impl<'a, T> StructuralPartialEq for Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
impl<'a, T> Sync for Matrix<'a, T>where
T: MatrixElement,
<T as FromStr>::Err: Error + 'static,
Vec<T>: IntoParallelIterator,
Vec<&'a T>: IntoParallelRefIterator<'a>,
Auto Trait Implementations§
impl<'a, T> Freeze for Matrix<'a, T>
impl<'a, T> RefUnwindSafe for Matrix<'a, T>where
T: RefUnwindSafe,
impl<'a, T> Unpin for Matrix<'a, T>where
T: Unpin,
impl<'a, T> UnwindSafe for Matrix<'a, T>where
T: RefUnwindSafe + UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more