[][src]Struct mtrs::Matrix

pub struct Matrix<T: Num> { /* fields omitted */ }

The main Matrix struct. Can be created in a variety of different ways.

#[macro_use] extern crate mtrs;
use mtrs::Matrix;

// All of these create a 2 x 2 matrix.

// From a 2D vector
let matrix = Matrix::from_vec(2, vec![1, 2, 3, 4]);
// Identity matrix of i32
let matrix: Matrix<i32> = Matrix::identity(2);
// Matrix of ones
let matrix: Matrix<i32> = Matrix::ones(2);
// Matrix of zeros
let matrix: Matrix<i32> = Matrix::zeros(2);
// Matrix of `i32`s
let matrix = matrix![(2, 2); 1, 2, 3, 4];
// Matrix of `f64`s
let matrix = matrix![f64; (2, 2); 1, 2; 3, 4];

Implementations

impl<T: Num + Clone + Copy> Matrix<T>[src]

pub fn transpose(&mut self)[src]

Transposes the matrix, via mutating the original data. Does not return a new struct, instead modifies the old one.

#[macro_use] extern crate mtrs;

let mut matrix = matrix![(2, 2); 1, 2; 3, 4];
matrix.transpose();
assert_eq!(matrix, matrix![(2, 2); 1, 3; 2, 4]);

pub fn scalar_add(&self, value: T) -> Self[src]

Add a scalar constant to the matrix

#[macro_use] extern crate mtrs;
use mtrs::Matrix;

let m1 = Matrix::identity(2);

assert_eq!(m1.scalar_add(2), matrix![(2, 2); 3, 2; 2, 3]);

pub fn scalar_sub(&self, value: T) -> Self[src]

Subtract a scalar constant from the matrix

#[macro_use] extern crate mtrs;
use mtrs::Matrix;

let m1 = Matrix::identity(2);

assert_eq!(m1.scalar_sub(2), matrix![(2, 2); -1, -2; -2, -1]);

pub fn scalar_mul(&self, value: T) -> Self[src]

Multiply a scalar constant with the matrix

#[macro_use] extern crate mtrs;
use mtrs::Matrix;

let m1 = Matrix::identity(2);

assert_eq!(m1.scalar_mul(3), matrix![(2, 2); 3, 0; 0, 3]);

pub fn scalar_div(&self, value: T) -> Self[src]

Divide each entry in the matrix by a scalar constant

#[macro_use] extern crate mtrs;
use mtrs::Matrix;

let m1 = matrix![(2, 2); 4, 6; 8, 10];

assert_eq!(m1.scalar_div(2), matrix![(2, 2); 2, 3; 4, 5]);

pub fn determinant(&self) -> Option<T>[src]

Calculate the determinant of the Matrix (if the Matrix is square)

#[macro_use] extern crate mtrs;

let matrix = matrix![(4, 4); 1, 0, 2, -1; 3, 0, 0, 5; 2, 1, 4, -3; 1, 0, 5, 0];

assert_eq!(matrix.determinant(), Some(30));

Failure

Fails if the matrix is not square

pub fn inverse(&self) -> Option<Self>[src]

Calculate the inverse of Matrix<T>, via multiplying the reciprocal of the determinant

#[macro_use] extern crate mtrs;

let matrix = matrix![f32; (2, 2); -1, 1.5; 1, -1];

assert_eq!(matrix.inverse().expect("Could not take inverse"), matrix![f32; (2, 2); 2, -3; -2, 2]);

Failure

Fails if the matrix is not invertible (that is, it is not square or the determinant is 0)

impl<T: Num + Clone + Copy> Matrix<T>[src]

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

Creates a new identity matrix of size N * N

use mtrs::Matrix;

let matrix: Matrix<i32> = Matrix::identity(2);

assert_eq!(matrix.as_slice(), &[1, 0, 0, 1]);
assert_eq!(matrix.size(), (2, 2));

pub fn from_vec<S: Size>(size: S, body: Vec<T>) -> Self[src]

Creates a new matrix from a pre-given size, passing a 2d Vec<T>

use mtrs::Matrix;

let matrix = Matrix::from_vec((2, 2), vec![1, 2, 7, 6]);

assert_eq!(matrix.as_slice(), &[1, 2, 7, 6]);

pub fn from_slice<S: Size>(size: S, body: &[T]) -> Self[src]

Creates a new matrix from a slice

use mtrs::Matrix;

let matrix = Matrix::from_slice((2, 2), &[1, 2, 7, 6]);

assert_eq!(matrix.as_slice(), &[1, 2, 7, 6]);
assert_eq!(matrix[(1, 0)], 7);

pub fn zeros<S: Size>(size: S) -> Self[src]

Create a Matrix<T> of size M * N filled with 0s

use mtrs::Matrix;

let matrix = Matrix::zeros(2);

assert_eq!(matrix.as_slice(), &[0, 0, 0, 0]);
assert_eq!(matrix, Matrix::from_vec(2, vec![0, 0, 0, 0]));

pub fn ones<S: Size>(size: S) -> Self[src]

Create a Matrix<T> of size M * N filled with 1s

use mtrs::Matrix;

let matrix = Matrix::ones(2);

assert_eq!(matrix.as_slice(), &[1, 1, 1, 1]);
assert_eq!(matrix, Matrix::from_vec(2, vec![1, 1, 1, 1]));

pub fn diag(diagonal: Vec<T>) -> Self[src]

Create a square matrix with a diagonal (all other values initialized at 0)

use mtrs::Matrix;

let matrix = Matrix::diag(vec![1, 2, 3]);

assert_eq!(matrix.as_slice(), &[1, 0, 0, 0, 2, 0, 0, 0, 3]);
assert_eq!(matrix.size(), (3, 3));

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

Returns a tuple representing the dimensions ((height, width))

use mtrs::Matrix;

let matrix: Matrix<i32> = Matrix::ones((2, 3));
assert_eq!(matrix.size(), (2, 3));

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

Wrapper function for self.data.as_slice()

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

Wrapper function for self.data.as_mut_slice()

pub fn as_ptr(&self) -> *const T[src]

Wrapper function for self.data.as_ptr()

pub fn as_mut_ptr(&mut self) -> *mut T[src]

Wrapper function for self.data.as_mut_ptr()

pub unsafe fn erase(&mut self)[src]

Zero out the matrix

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

Return a Vec representation of the Matrix

use mtrs::Matrix;

let matrix = Matrix::from_vec(2, vec![2, 1, 4, 3]);
assert_eq!(matrix.as_vec(), vec![vec![2, 1], vec![4, 3]]);

pub fn get_col(&self, index: usize) -> Option<Vec<T>>[src]

Returns a single column of the Matrix Returns a Vec of all the columns

extern crate mtrs;
use mtrs::matrix;

let mat = matrix![i32; (3, 2); 1, 2; 3, 4; 5, 6];

assert_eq!(mat.get_col(0), Some(vec![1, 3, 5]));
assert_eq!(mat.get_col(3), None)

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

Returns a Vec of all the columns

extern crate mtrs;
use mtrs::matrix;

let mat = matrix![i32; (3, 2); 1, 2; 3, 4; 5, 6];

assert_eq!(mat.cols(), vec![vec![1, 3, 5], vec![2, 4, 6]]);

pub fn get<S: Size>(&self, loc: S) -> Option<&T>[src]

Returns an entry in the Matrix safely, that is:

extern crate mtrs;
use mtrs::matrix;

let mat = matrix![i32; (2, 2); 1, 2; 3, 4];

assert_eq!(mat.get((0, 1)), Some(&2));

Failure

Fails if the location is out of bounds

pub fn set<S: Size>(&mut self, loc: S, val: T) -> Result<()>[src]

Sets an entry in the Matrix

#[macro_use] extern crate mtrs;
use mtrs::Matrix;

let mut mat = matrix![(2, 3); 1, 2, 3; 4, 5, 6];
mat.set(1, 13);
assert_eq!(mat.as_slice(), &[1, 2, 3, 4, 13, 6]);

Failure

Fails if you attempt to set a value that is out of bounds

pub fn resize<S: Size>(&mut self, size: S)[src]

Resizes the Matrix to any size, with all new values initialized to 0

#[macro_use] extern crate mtrs;

let mut mat = matrix![(2, 3); 1, 2, 3; 4, 5, 6];
mat.resize((2, 2));
assert_eq!(mat.as_slice(), &[1, 2, 4, 5]);

Trait Implementations

impl<T: Num + Clone + Copy> Add<Matrix<T>> for Matrix<T>[src]

Implements addition between Matrix<T> and Matrix<T>

type Output = Self

The resulting type after applying the + operator.

impl<T: Num + Clone + Copy> Add<T> for Matrix<T>[src]

Implements addition between Matrix<T> and T

type Output = Self

The resulting type after applying the + operator.

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

impl<T: Debug + Num> Debug for Matrix<T>[src]

impl<T> Display for Matrix<T> where
    T: Num + Clone + Copy + Display
[src]

Pretty print of the Matrix via this impl

impl<T: Num + Clone + Copy> Div<T> for Matrix<T>[src]

Implements division between Matrix<T> and T

type Output = Self

The resulting type after applying the / operator.

impl<T: Num + Clone + Copy> From<Matrix<T>> for Vec<T>[src]

Implements the From<Matrix<T>> trait for Vec<T>

impl<T: Num, S: Size> Index<S> for Matrix<T>[src]

Allows for the indexing of Matrix

type Output = T

The returned type after indexing.

impl<T: Num + Clone + Copy + Sum> Mul<Matrix<T>> for Matrix<T>[src]

Implements multiplication between Matrix<T> and Matrix<T>

type Output = Self

The resulting type after applying the * operator.

impl<T: Num + Clone + Copy> Mul<T> for Matrix<T>[src]

Implements multiplication between Matrix<T> and T

type Output = Self

The resulting type after applying the * operator.

impl<T: PartialEq + Num> PartialEq<Matrix<T>> for Matrix<T>[src]

impl<T: Num> StructuralPartialEq for Matrix<T>[src]

impl<T: Num + Clone + Copy> Sub<Matrix<T>> for Matrix<T>[src]

Implements subtraction between Matrix<T> and Matrix<T>

type Output = Self

The resulting type after applying the - operator.

impl<T: Num + Clone + Copy> Sub<T> for Matrix<T>[src]

Implements subtraction between Matrix<T> and T

type Output = Self

The resulting type after applying the - operator.

Auto Trait Implementations

impl<T> RefUnwindSafe for Matrix<T> where
    T: RefUnwindSafe

impl<T> Send for Matrix<T> where
    T: Send

impl<T> Sync for Matrix<T> where
    T: Sync

impl<T> Unpin for Matrix<T> where
    T: Unpin

impl<T> UnwindSafe for Matrix<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.