Struct Matrix

Source
pub struct Matrix<T: MatrixElementRequiredTraits<T>> {
    pub n: usize,
    pub m: usize,
    pub entries: Vec<T>,
}

Fields§

§n: usize§m: usize§entries: Vec<T>

Implementations§

Source§

impl<T: MatrixElementRequiredTraits<T>> Matrix<T>

Source

pub fn new(m: usize, n: usize, entries: Vec<T>) -> Self

Standard initializer for a Matrix Takes dimensions m (number of rows) and n (number of columns) together with a vector representing the elements. The number of elements should be equal to m x n

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new(2, 3, [1.0, 2.0, -1.0, 0.0, 3.0, 7.0].to_vec());
println!("{test_matrix}");

Will output
⌜1 2 -1 ⌝
⌞0 3 7 ⌟

Source

pub fn new_constant_value( m: usize, n: usize, value: T, ) -> Result<Self, &'static str>

Convenience initializer to initialize a matrix of dimension m x n with all entries equal to the provided value

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new_constant_value(2, 3, 5.0).expect("Unable to initialize matrix");
println!("{test_matrix}");

/// Will output
/// ⌜5 5 5 ⌝
/// ⌞5 5 5 ⌟

Source

pub fn get_entry_ij(&self, i: usize, j: usize) -> &T

Getter for entry at position i,j (zero indexed) Returns a reference to the entry

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new(2, 3, [1.0, 2.0, -1.0, 0.0, 3.0, 7.0].to_vec());
let entry = test_matrix.get_entry_ij(1, 1);
println!("{entry}");

Will output
3

Source

pub fn set_entry_ij(&mut self, i: usize, j: usize, new_value: &T)

Setter for entry at position i, j (zero indexed)

use matrix::matrix_algebra::Matrix;
let mut test_matrix = Matrix::new(2, 3, [1.0, 2.0, -1.0, 0.0, 3.0, 7.0].to_vec());
let entry = test_matrix.get_entry_ij(1, 1);
println!("{entry}");
test_matrix.set_entry_ij(1, 1, &4.0);
let entry = test_matrix.get_entry_ij(1, 1);
println!("{entry}");

Will output:
3
4

Source

pub fn rows(&self) -> Vec<Vec<T>>

Retrieves the rows of a matrix

use matrix::matrix_algebra::Matrix;
let mut test_matrix = Matrix::new(2, 3, [1.0, 2.0, -1.0, 0.0, 3.0, 7.0].to_vec());
let rows = test_matrix.rows();
Source

pub fn row_interchange( &self, first_row_index: usize, second_row_index: usize, ) -> Self

The first elementary operation on a matrix Returns a new matrix with the rows at the specified indices interchanged.

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new(2, 3, [1.0, 2.0, 3.0, 4.0, 5.0, 6.0].to_vec());
let result = test_matrix.row_interchange(0, 1);

assert_eq!(
   result,
   Matrix::new(2, 3, [4.0, 5.0, 6.0, 1.0, 2.0, 3.0].to_vec()),
);
Source

pub fn multiply_row_by_scalar(&self, row_index: usize, scalar: T) -> Self

The second elementary operation on a matrix Returns a new matrix with all elements in the row at the specified index multiplied by the specified scalar.

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new(3, 3, [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0].to_vec());
let result = test_matrix.multiply_row_by_scalar(2, 2.0);

assert_eq!(
   result,
   Matrix::new(
       3,
       3,
       [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 14.0, 16.0, 18.0].to_vec()
   ),
);
Source

pub fn add_row_to_scalar_multiple_of_row( &self, target_index: usize, source_index: usize, scalar: T, ) -> Self

The third elementary operation on a matrix Adds a scalar multiple of a row to an existing row in a matrix

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new(3, 3, [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0].to_vec());
let result = test_matrix.add_row_to_scalar_multiple_of_row(0, 2, 5.0);

assert_eq!(
    result,
    Matrix::new(
        3,
        3,
        [36.0, 42.0, 48.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0].to_vec()
    ),
);
Source

pub fn row_echolon_form(&self) -> Self

Reduces a matrix to Row Echolon Form

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new(
    3,
    4,
    [
        0.0, 0.0, 3.0, -1.0, 0.0, -1.0, 4.0, 7.0, 0.0, -1.0, 7.0, 6.0,
    ]
    .to_vec(),
);

let result = test_matrix.row_echolon_form();

assert_eq!(
    result,
    Matrix::new(
        3,
        4,
        [
            0.0,
            1.0,
            0.0,
            -25.0 / 3.0,
            0.0,
            0.0,
            1.0,
            -1.0 / 3.0,
            0.0,
            0.0,
            0.0,
            0.0
        ]
        .to_vec()
    ),
);
Source

pub fn column_echolon_form(&self) -> Self

Reduces a matrix to column echolon form

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new(2, 3, [1.0, 2.0, 3.0, 2.0, 3.0, 4.0].to_vec());
let result = test_matrix.column_echolon_form();

assert_eq!(
    result,
    Matrix::new(2, 3, [1.0, 0.0, 0.0, 0.0, 1.0, 0.0,].to_vec())
);
let test_matrix = Matrix::new(3, 3, [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0].to_vec());
let result = test_matrix.column_echolon_form();
assert_eq!(
    result,
    Matrix::new(3, 3, [1.0, 0.0, 0.0, 2.0, 0.0, 0.0, 3.0, 0.0, 0.0].to_vec())
);
Source

pub fn columns(&self) -> Vec<Vec<T>>

Convenience function that extracts the columns of a matrix

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new(
3,
4,
[
   1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
]
.to_vec(),
);

let columns = test_matrix.columns();

assert_eq!(columns.len(), 4);

assert_eq!(columns[0], vec![1.0, 5.0, 9.0]);
assert_eq!(columns[1], vec![2.0, 6.0, 10.0]);
assert_eq!(columns[2], vec![3.0, 7.0, 11.0]);
assert_eq!(columns[3], vec![4.0, 8.0, 12.0]);
Source

pub fn transpose(&self) -> Self

Creates a transpose of a matrix

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new(2, 3, [1.0, 2.0, -1.0, 0.0, 3.0, 7.0].to_vec());

let transpose_matrix = test_matrix.transpose();

assert_eq!(transpose_matrix.m, test_matrix.n);
assert_eq!(transpose_matrix.n, test_matrix.m);
assert_eq!(transpose_matrix.entries, [1.0, 0.0, 2.0, 3.0, -1.0, 7.0]);
Source

pub fn determinant(&self) -> Result<T, &'static str>

Calculates the determinant of a matrix The following example shows that there can be some rounding issues with the calculations.

use matrix::matrix_algebra::Matrix;
use matrix::complex_number::ComplexNumber;
let test_matrix = Matrix::new(
    3,
    3,
    [
        ComplexNumber::new(1.0, 0.0),
        ComplexNumber::new(1.0, 0.0),
        ComplexNumber::new(0.0, 1.0),
        ComplexNumber::new(1.0, 1.0),
        ComplexNumber::new(1.0, 1.0),
        ComplexNumber::new(1.0, 0.0),
        ComplexNumber::new(2.0, 3.0),
        ComplexNumber::new(0.0, -1.0),
        ComplexNumber::new(3.0, 0.0),
    ]
    .to_vec(),
);

let determinant = test_matrix.determinant();

fn delta_real(determinant: ComplexNumber<f64>, expectation: f64) -> bool {
    determinant.real - expectation < (1.0 / 1000000000000000000000000000000.0)
}

fn delta_complex(determinant: ComplexNumber<f64>, expectation: f64) -> bool {
    determinant.complex - expectation < (1.0 / 1000000000000000000000000000000.0)
}
assert!(delta_real(
    determinant.expect("Unable to calculate determinant"),
    8.0
));
assert!(delta_complex(
    determinant.expect("Unable to calculate determinant"),
    6.0
));
Source

pub fn minor( &self, column_indices: &[usize], row_indices: &[usize], ) -> Result<T, &'static str>

Calculates the minor of a matrix (the determinant of a given submatrix) based on the provided column indices and row indices to be selected from the matrix

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new(3, 3, [1.0, 2.0, 1.0, 6.0, -1.0, 0.0, -1.0, -2.0, -1.0].to_vec());
assert_eq!(test_matrix.minor(&[1, 2], &[1,2]).expect("Unable to create minor"), 1.0);
assert_eq!(test_matrix.minor(&[0, 2], &[1,2]).expect("Unable to create minor"), -6.0);
assert_eq!(test_matrix.minor(&[0, 1], &[0,1]).expect("Unable to create minor"), -13.0);
Source

pub fn is_nonsingular(&self) -> Result<bool, &'static str>

Convenience function to identify a non-singular matrix

Source

pub fn matrix_of_cofactors(&self) -> Result<Self, &'static str>

Creates a matrix of cofactors from a matrix

Source

pub fn adjoint(&self) -> Result<Self, &'static str>

Caluculates the adjoint of a matrix

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new(3, 3, [3.0, 4.0, 3.0, 5.0, 7.0, 2.0, 0.0, 0.0, 1.0].to_vec());

assert_eq!(
    test_matrix
        .adjoint()
        .expect("test_adjoint: unable to calculate matrix adjoint"),
    Matrix::new(
        3,
        3,
        [
            7.0,
            -4.0,
            -13.0,
            -5.0,
            3.0,
            9.0,
            0.0,
            0.0,
            1.0000000000000018
        ]
        .to_vec()
    )
);
Source

pub fn inverse(&self) -> Result<Self, &'static str>

Calculates the inverse of a matrix

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new(
    3,
    3,
    [2.0, 3.0, 0.0, 0.0, 3.0, -3.0, -2.0, 3.0, 3.0].to_vec(),
);

let inverse = test_matrix
.inverse()
.expect("Unable to compute matrix inverse in test_inverse");

assert_eq!(inverse.m, 3);
assert_eq!(inverse.n, 3);

let expected = [
    1.0 / 3.0,
    -1.0 / 6.0,
    -1.0 / 6.0,
    1.0 / 9.0,
    1.0 / 9.0,
    1.0 / 9.0,
    1.0 / 9.0,
    -2.0 / 9.0,
    1.0 / 9.0,
    ]
    .to_vec();
     
    for i in 0..expected.len() {
        assert!(expected[i] - inverse.entries[i] < 1.0 / 1000000000000000.0);
    }
Source

pub fn partition( &self, column_partitioning: &[usize], row_partitioning: &[usize], ) -> Vec<Self>

Partitions a matrix based on the provided column and row indices

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new(
5,
5,
[
    1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0,
    16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0,
]
.to_vec(),
);

let submatrices = test_matrix.partition(&[3, 2], &[2, 3]);

assert_eq!(
    submatrices,
    [
        Matrix::new(2, 3, [1.0, 2.0, 3.0, 6.0, 7.0, 8.0].to_vec()),
        Matrix::new(2, 2, [4.0, 5.0, 9.0, 10.0].to_vec()),
        Matrix::new(
            3,
            3,
            [11.0, 12.0, 13.0, 16.0, 17.0, 18.0, 21.0, 22.0, 23.0].to_vec()
        ),
        Matrix::new(3, 2, [14.0, 15.0, 19.0, 20.0, 24.0, 25.0].to_vec())
    ]
);
Source

pub fn submatrix(&self, column_indices: &[usize], row_indices: &[usize]) -> Self

Generates a submatrix based on the provided column and row indices

use matrix::matrix_algebra::Matrix;
let test_matrix = Matrix::new(
    5,
    5,
    [
        1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0,
        16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0,
    ]
    .to_vec(),
);

let submatrix = test_matrix.submatrix(&[1, 3], &[0, 2, 4]);
assert_eq!(submatrix.n, 2);
assert_eq!(submatrix.m, 3);
assert_eq!(submatrix.entries, [2.0, 4.0, 12.0, 14.0, 22.0, 24.0]);

Trait Implementations§

Source§

impl<T: MatrixElementRequiredTraits<T>> Add for Matrix<T>
where for<'a> &'a T: Add<Output = T>,

Implementation of the Add trait for Matrix<T> Uses matrix_add

Source§

type Output = Matrix<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl<T: Clone + MatrixElementRequiredTraits<T>> Clone for Matrix<T>

Source§

fn clone(&self) -> Matrix<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + MatrixElementRequiredTraits<T>> Debug for Matrix<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: MatrixElementRequiredTraits<T>> Display for Matrix<T>

Implementation of the Display trait for Matrix which can be used for debugging purposes

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: MatrixElementRequiredTraits<T>> Mul for Matrix<T>

Implementation of the Mul trait for Matrix<T> Uses matrix_multiply

use matrix::matrix_algebra::Matrix;
let test_matrix_a = Matrix::new(
    3,
    4,
    [
        1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
    ]
    .to_vec(),
);
let test_matrix_b = Matrix::new(
    4,
    3,
    [
        12.0, 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0,
    ]
    .to_vec(),
);

let matrix_product = test_matrix_a * test_matrix_b;

assert_eq!(matrix_product.entries.len(), 9);

assert_eq!(
matrix_product.entries,
    [
        1.0 * 12.0 + 2.0 * 9.0 + 3.0 * 6.0 + 4.0 * 3.0,
        1.0 * 11.0 + 2.0 * 8.0 + 3.0 * 5.0 + 4.0 * 2.0,
        1.0 * 10.0 + 2.0 * 7.0 + 3.0 * 4.0 + 4.0 * 1.0,
        5.0 * 12.0 + 6.0 * 9.0 + 7.0 * 6.0 + 8.0 * 3.0,
        5.0 * 11.0 + 6.0 * 8.0 + 7.0 * 5.0 + 8.0 * 2.0,
        5.0 * 10.0 + 6.0 * 7.0 + 7.0 * 4.0 + 8.0 * 1.0,
        9.0 * 12.0 + 10.0 * 9.0 + 11.0 * 6.0 + 12.0 * 3.0,
        9.0 * 11.0 + 10.0 * 8.0 + 11.0 * 5.0 + 12.0 * 2.0,
        9.0 * 10.0 + 10.0 * 7.0 + 11.0 * 4.0 + 12.0 * 1.0
    ]
);
Source§

type Output = Matrix<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self

Performs the * operation. Read more
Source§

impl<T: PartialEq + MatrixElementRequiredTraits<T>> PartialEq for Matrix<T>

Source§

fn eq(&self, other: &Matrix<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd + MatrixElementRequiredTraits<T>> PartialOrd for Matrix<T>

Source§

fn partial_cmp(&self, other: &Matrix<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: MatrixElementRequiredTraits<T>> StructuralPartialEq for Matrix<T>

Auto Trait Implementations§

§

impl<T> Freeze for Matrix<T>

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.