Matrix

Struct Matrix 

Source
pub struct Matrix<'a, T>{
    pub nrows: usize,
    pub ncols: usize,
    /* private fields */
}
Expand description

Helper method to swap to usizes General dense matrix

Fields§

§nrows: usize

Number of rows

§ncols: usize

Number 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>,

Source

pub fn determinant_helper(&self) -> T

Source

pub fn matmul_helper(&self, other: &Self) -> Self

Source

pub fn get_block_size(&self, other: &Self) -> usize

Source

pub fn get_range_for_block_size(&self, other: &Self) -> RangeInclusive<usize>

Source§

impl<'a, T> Matrix<'a, T>

Printer functions for the matrix

Source

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

pub fn sparsity(&'a self) -> f64

Calculates sparsity of a given Matrix

Examples:

use sukker::Matrix;

let mat: Matrix<f32> = Matrix::eye(2);

assert_eq!(mat.sparsity(), 0.5);
Source

pub fn shape(&self) -> Shape

Returns the shape of a matrix represented as
(usize, usize)

Examples:

use sukker::Matrix;

let mat: Matrix<f32> = Matrix::eye(4);

assert_eq!(mat.shape(), (4,4));
Source§

impl<'a, T> Matrix<'a, T>

Implementations of all creatins of matrices

Source

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

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

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

pub fn eye_like(matrix: &Self) -> Self

Produce an identity matrix in the same shape as an already existent matrix

Examples


Source

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

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

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

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

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

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

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

Source

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

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

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

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

Source

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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>

trait MatrixLinAlg contains all common Linear Algebra functions to be performed on matrices

Source

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

pub fn mm(&self, other: &Self) -> Result<Self, MatrixError>

Shorthand method for matmul

Source

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

pub fn det(&self) -> Option<T>

Shorthand call for determinant

Source

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

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

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

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>

Implementations for predicates

Source

pub fn count_where<F>(&'a self, pred: F) -> usize
where F: Fn(&T) -> bool + Sync,

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

pub fn sum_where<F>(&self, pred: F) -> T
where F: Fn(&T) -> bool + Sync,

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

pub fn set_where<P>(&mut self, pred: P)
where P: FnMut(&mut T) + Sync + Send,

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

pub fn any<F>(&self, pred: F) -> bool
where F: Fn(&T) -> bool + Sync + Send,

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

pub fn all<F>(&self, pred: F) -> bool
where F: Fn(&T) -> bool + Sync + Send,

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

pub fn find<F>(&self, pred: F) -> Option<Shape>
where F: Fn(&T) -> bool + Sync,

Finds first index where predicates holds if possible

§Examples
use sukker::Matrix;

let matrix = Matrix::init(2f32, (2,4));

assert_eq!(matrix.find(|&e| e >= 1f32), Some((0,0)));
Source

pub fn find_all<F>(&self, pred: F) -> Option<Vec<Shape>>
where F: Fn(&T) -> bool + Sync,

Finds all indeces where predicates holds if possible

§Examples
use sukker::Matrix;

let matrix = Matrix::init(2.0, (2,4));

assert_eq!(matrix.find_all(|&e| e >= 3.0), None);

Trait Implementations§

Source§

impl<'a, T> Clone for Matrix<'a, T>

Source§

fn clone(&self) -> Matrix<'a, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a, T> Debug for Matrix<'a, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T> Default for Matrix<'a, T>

Source§

fn default() -> Self

Represents a default identity matrix

§Examples
use sukker::Matrix;

let matrix: Matrix<f32> = Matrix::default();

assert_eq!(matrix.size(), 9);
assert_eq!(matrix.shape(), (3,3));
Source§

impl<'de, 'a, T> Deserialize<'de> for Matrix<'a, T>

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'a, T> Display for Matrix<'a, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T> Error for Matrix<'a, T>

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl<'a, T> FromStr for Matrix<'a, T>

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
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

Source§

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

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

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

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

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

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

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

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

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

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

Find the eigenvectors

Source§

impl<'a, T> PartialEq for Matrix<'a, T>

Source§

fn eq(&self, other: &Matrix<'a, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, T> PartialOrd for Matrix<'a, T>

Source§

fn partial_cmp(&self, other: &Matrix<'a, T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, T> Serialize for Matrix<'a, T>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'a, T> Send for Matrix<'a, T>

Source§

impl<'a, T> StructuralPartialEq for Matrix<'a, T>

Source§

impl<'a, T> Sync for Matrix<'a, T>

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>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,