[][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, 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, 2);
// Matrix of zeros
let matrix: Matrix<i32> = Matrix::zeros(2, 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

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

Subtract a scalar constant from the matrix

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

Multiply a scalar constant with the matrix

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

Divide each entry in the matrix by a scalar constant

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

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

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

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

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(height: usize, width: usize, 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 zeros(height: usize, width: usize) -> Self[src]

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

use mtrs::Matrix;

let matrix = Matrix::zeros(2, 2);

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

pub fn ones(height: usize, width: usize) -> Self[src]

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

use mtrs::Matrix;

let matrix = Matrix::ones(2, 2);

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

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) -> *const T[src]

Wrapper function for self.data.as_mut_ptr()

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, 2, vec![2, 1, 4, 3]);
assert_eq!(matrix.as_vec(), vec![vec![2, 1], vec![4, 3]]);

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

Returns a Vec of all the columns

Trait Implementations

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

Perform addition using the + operator

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

extern crate mtrs;
use mtrs::Matrix;

let matrix: Matrix<i32> = Matrix::identity(3);
println!("{}", matrix);

impl<T: Num> Index<(usize, usize)> for Matrix<T>[src]

Allows for the indexing of Matrix

extern crate mtrs;
use mtrs::Matrix;

let matrix: Matrix<u8> = Matrix::identity(3);
assert_eq!(matrix[(1, 1)], 1);

type Output = T

The returned type after indexing.

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

Perform multiplication using the * operator

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]

Perform subtraction using the - operator

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.