Struct Matrix
pub struct Matrix<T> { /* private fields */ }
Expand description
Represents a matrix.
Implementations§
§impl<T> Matrix<T>
impl<T> Matrix<T>
pub fn from_vec(rows: usize, cols: usize, vec: Vec<T>) -> Matrix<T>
pub fn from_vec(rows: usize, cols: usize, vec: Vec<T>) -> Matrix<T>
Create a new matrix from a vec.
§Arguments
rows
- Row count of matrixcols
- Column count of matrixvec
- Vector of lengthrows x cols
wherevec[i * cols + j]
is the entry in rowi
and columnj
§Example
let mat = matrix!{1, 2, 3; 3, 2, 1; 2, 1, 3};
println!("{}", mat);
// Output:
// 1 2 3
// 3 2 1
// 2 1 3
pub fn diag_with(dim: usize, entries: &[T]) -> Matrix<T>
pub fn diag_with(dim: usize, entries: &[T]) -> Matrix<T>
Creates a diagonal matrix with dimensions dim x dim
and initial entries specified in entries
.
pub fn lupdecompose(&self) -> Option<(Matrix<f64>, Vec<usize>)>
Trait Implementations§
§impl<T> Add for &Matrix<T>
Matrices can be added by adding their references. Both matrices need to have the same dimensions.
impl<T> Add for &Matrix<T>
Matrices can be added by adding their references. Both matrices need to have the same dimensions.
§Example
let mat_a = Matrix::one(3);
let mat_b = Matrix::one(3);
let mat_c = Matrix::diag(3, 2);
assert_eq!(&mat_a + &mat_b, mat_c)
§impl<T> AddAssign<&Matrix<T>> for Matrix<T>
impl<T> AddAssign<&Matrix<T>> for Matrix<T>
§fn add_assign(&mut self, mat: &Matrix<T>)
fn add_assign(&mut self, mat: &Matrix<T>)
+=
operation. Read more§impl<T> Div<T> for &Matrix<T>
Dividing a matrix is the same as scaling it with the inverse of the divisor.
impl<T> Div<T> for &Matrix<T>
Dividing a matrix is the same as scaling it with the inverse of the divisor.
§Example
let mat_a = Matrix::new(3, 3, 1_f32);
assert_eq!(&mat_a / 2_f32, &mat_a * 0.5_f32);
§impl<T> Index<usize> for Matrix<T>
Indexing matrices returns the corresponding row of the matrix as a slice.
impl<T> Index<usize> for Matrix<T>
Indexing matrices returns the corresponding row of the matrix as a slice.
§Example
let mat = Matrix::<u32>::one(3);
assert_eq!(mat[0], vec![1_u32, 0, 0]);
assert_eq!(mat[0][0], 1);
assert_eq!(mat[1][1], 1);
assert_eq!(mat[2][1], 0);
§impl<T> IndexMut<usize> for Matrix<T>
Matrices can be manipulated by assigning a value to an indexed matrix.
impl<T> IndexMut<usize> for Matrix<T>
Matrices can be manipulated by assigning a value to an indexed matrix.
§Example
let mut mat = Matrix::<u32>::zero(3, 3);
mat[0][0] = 1;
mat[1][1] = 1;
mat[2][2] = 1;
assert_eq!(mat, Matrix::<u32>::one(3));
§impl<T> Inv for Matrix<T>
impl<T> Inv for Matrix<T>
§fn inv(self) -> Self::Output
fn inv(self) -> Self::Output
Invert a matrix.
§Example
let mat_a: Matrix<i32> = matrix!{{0,-1,2},{1,2,0},{2,1,0}};
let mat_c: Matrix<i32> = matrix!{{1, 2, 3},{3, 2, 1}}; // not a square matrix
let mat_b = matrix!{{0.0, -1.0/3.0, 2.0/3.0}, {0.0, 2.0/3.0, -1.0/3.0}, {1.0/2.0, 1.0/3.0, -1.0/6.0}};
assert_eq!(mat_a.inv(), Some(mat_b));
assert_eq!(mat_c.inv(), None);
§impl<T> Mul<&Matrix<T>> for &Vector<T>
Vectors can also be multiplied with matrices. The result will be a vector.
impl<T> Mul<&Matrix<T>> for &Vector<T>
Vectors can also be multiplied with matrices. The result will be a vector.
§Example
let mat_a = Matrix::<u32>::one(4);
let mat_b = matrix!{1, 2, 3; 4, 4, 3; 2, 1, 3; 4, 1, 2};
let vec_a = vector![4, 5, 6, 7].to_row_vector();
let vec_b = vector![64, 41, 59].to_row_vector();
assert_eq!(&vec_a * &mat_a, vec_a);
assert_eq!(&vec_a * &mat_b, vec_b);
§impl<T> Mul<&Vector<T>> for &Matrix<T>
Matrices can be multiplied with Vectors by multiplying their references.
The dimensions of the two objects need to match like with matrix multiplication.
impl<T> Mul<&Vector<T>> for &Matrix<T>
Matrices can be multiplied with Vectors by multiplying their references. The dimensions of the two objects need to match like with matrix multiplication.
§Example
let v_a = vector![1, 2, 3];
let mat_a = matrix!{1, 2, 3; 4, 5, 6; 7, 8, 9};
let v_b = vector![30, 36, 42].to_row_vector();
assert_eq!(&v_a.to_row_vector() * &mat_a, v_b);
§impl<T> Mul<T> for &Matrix<T>
A matrix can be scaled by scaling a reference to a matrix. Each entry will be scaled by the given factor.
impl<T> Mul<T> for &Matrix<T>
A matrix can be scaled by scaling a reference to a matrix. Each entry will be scaled by the given factor.
§Example
let mat_a = matrix!{1, 2; 3, 4};
let mat_b = matrix!{2, 4; 6, 8};
assert_eq!(&mat_a * 2, mat_b);
§impl<T> Mul for &Matrix<T>
Matrices can be multiplied by multiplying their references.
This is matrix multiplicaiton as described in
Matrix multipication,
so the left matrix needs to have the same amount of columns as the right has rows.
impl<T> Mul for &Matrix<T>
Matrices can be multiplied by multiplying their references. This is matrix multiplicaiton as described in Matrix multipication, so the left matrix needs to have the same amount of columns as the right has rows.
§Example
let mat_a = matrix!{1, 2, 3, 4; 5, 6, 7, 8};
let mat_b = matrix!{1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12};
let mat_c = matrix!{70, 80, 90; 158, 184, 210};
assert_eq!(&mat_a * &mat_b, mat_c);
§impl<T> Neg for &Matrix<T>
A Matrix can be negated by negating a reference to a matrix. This negates every entry of the matrix.
impl<T> Neg for &Matrix<T>
A Matrix can be negated by negating a reference to a matrix. This negates every entry of the matrix.
§Example
let mat_a: Matrix<i32> = matrix!{1, 2; 3, 4};
let mat_b: Matrix<i32> = matrix!{-1, -2; -3, -4};
assert_eq!(-&mat_a, mat_b);
§impl<T> Sub for &Matrix<T>
Matrices can be subtracted by subtracting their references. Both matrices need to have the same dimensions.
impl<T> Sub for &Matrix<T>
Matrices can be subtracted by subtracting their references. Both matrices need to have the same dimensions.
§Example
let mat_a: Matrix<i32> = Matrix::one(3);
let mat_b: Matrix<i32> = Matrix::one(3);
assert_eq!(&mat_a - &mat_b, Matrix::zero(3, 3));
§impl<T> SubAssign<&Matrix<T>> for Matrix<T>
impl<T> SubAssign<&Matrix<T>> for Matrix<T>
§fn sub_assign(&mut self, mat: &Matrix<T>)
fn sub_assign(&mut self, mat: &Matrix<T>)
-=
operation. Read more