Struct libmat::mat::Matrix [−]
pub struct Matrix<T> { /* fields omitted */ }
Expand description
Represents a matrix.
Implementations
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
Creates a diagonal matrix with dimensions dim x dim
and initial entries specified in entries
.
pub fn lupdecompose(&self) -> Option<(Matrix<f64>, Vec<usize>)> where
T: Signed + PartialOrd + ToPrimitive,
pub fn det(&self) -> f64 where
T: Signed + PartialOrd + Display + ToPrimitive,
pub fn det(&self) -> f64 where
T: Signed + PartialOrd + Display + ToPrimitive,
Returns true if the matrix is a square matrix, false otherwise.
Example
let mat_a: Matrix<i32> = Matrix::one(3); let mat_b: Matrix<f32> = Matrix::zero(3, 4); assert_eq!(mat_a.is_square(), true); assert_eq!(mat_b.is_square(), false);
Trait Implementations
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)
fn add_assign(&mut self, mat: &Matrix<T>)
fn add_assign(&mut self, mat: &Matrix<T>)
Performs the +=
operation. Read more
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);
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);
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> where
T: One + Zero + Clone + Copy + Signed + PartialOrd + ToPrimitive,
impl<T> Inv for Matrix<T> where
T: One + Zero + Clone + Copy + Signed + PartialOrd + ToPrimitive,
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);
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);
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);
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);
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);
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);
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));
fn sub_assign(&mut self, mat: &Matrix<T>)
fn sub_assign(&mut self, mat: &Matrix<T>)
Performs the -=
operation. Read more
impl<T> StructuralPartialEq for Matrix<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for Matrix<T> where
T: RefUnwindSafe,
impl<T> UnwindSafe for Matrix<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more