Struct libmat::mat::Matrix[]

pub struct Matrix<T> { /* fields omitted */ }
Expand description

Represents a matrix.

Implementations

Create a new matrix of type T with init as the default value for each entry.

Arguments

  • rows - Row count of matrix
  • cols - Column count of matrix
  • init - The initial value of all entries

Example

let mat = Matrix::new(3, 4, 9);
println!("{}", mat);

// Output:
// 9 9 9 9
// 9 9 9 9
// 9 9 9 9

Create a new matrix from a vec.

Arguments

  • rows - Row count of matrix
  • cols - Column count of matrix
  • vec - Vector of length rows x cols where vec[i * cols + j] is the entry in row i and column j

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

Get the number of rows

Get the number of columns

Create an identity matrix of type T with dimensions dim x dim.

Arguments

  • dim - The dimensions of a square matrix

Example

let mat_a: Matrix<u32> = Matrix::one(3);
println!("{}", mat_a);

// Output:
// 1 0 0
// 0 1 0
// 0 0 1

Create a zero-matrix of type T.

Arguments

  • rows - Row count of matrix
  • cols - Column count of matrix

Example

let mat = Matrix::zero(3, 8);
assert_eq!(mat, Matrix::new(3, 8, 0));

Create a diagonal matrix of type T with entries init.

Arguments

  • dim - The dimensions of a square matrix
  • init - The initial value of diagonal entries

Examples

let mat = Matrix::diag(3, 1);
assert_eq!(mat, Matrix::one(3));

Creates a diagonal matrix with dimensions dim x dim and initial entries specified in entries.

Calculate the determinant of a square matrix.

Caution

Calculation may not be exact. Be sure to use round() when calculating the determinant of a integer matrix.

Example

let mat = matrix!{1, 2, 3; 3, 2, 1; 2, 1, 3};
assert_eq!(mat.det(), -12.0);

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

Transpose a matrix.

Example

let mat_a = matrix!{1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12};
// 1  2  3  4
// 5  6  7  8
// 9 10 11 12
let mat_b = matrix!{1, 5, 9; 2, 6, 10; 3, 7, 11; 4, 8, 12};
// 1 5  9
// 2 6 10
// 3 7 11
// 4 8 12
assert_eq!(mat_a.transpose(), mat_b);

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)

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. 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);

The resulting type after applying the / operator.

Performs the / operation. Read more

Performs the conversion.

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

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

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

Performs the mutable indexing (container[index]) operation. Read more

Performs the conversion.

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

The result after applying the operator.

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

The resulting type after applying the * operator.

Performs the * operation. Read more

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

The resulting type after applying the * operator.

Performs the * operation. Read more

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

The resulting type after applying the * operator.

Performs the * operation. Read more

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

The resulting type after applying the * operator.

Performs the * operation. Read more

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

The resulting type after applying the - operator.

Performs the unary - operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.