2023-05-25 20:20:31 -05:00
|
|
|
//! This is a crate for very basic matrix operations
|
2023-05-27 01:02:06 -05:00
|
|
|
//! with any type that implement [`Add`], [`Sub`], [`Mul`],
|
2023-05-27 02:22:10 -05:00
|
|
|
//! [`Zero`], [`Neg`] and [`Copy`]. Additional properties might be
|
2023-05-25 20:20:31 -05:00
|
|
|
//! needed for certain operations.
|
2023-05-27 18:35:06 -05:00
|
|
|
//!
|
2023-05-25 20:40:09 -05:00
|
|
|
//! I created it mostly to learn using generic types
|
|
|
|
//! and traits.
|
2023-05-25 20:20:31 -05:00
|
|
|
//!
|
|
|
|
//! Sayantan Santra (2023)
|
|
|
|
|
2023-05-28 02:36:29 -05:00
|
|
|
use errors::MatrixError;
|
2023-05-25 20:28:36 -05:00
|
|
|
use num::{
|
|
|
|
traits::{One, Zero},
|
|
|
|
Integer,
|
|
|
|
};
|
|
|
|
use std::{
|
|
|
|
fmt::{self, Debug, Display, Formatter},
|
2023-05-26 01:18:52 -05:00
|
|
|
ops::{Add, Div, Mul, Neg, Sub},
|
2023-05-25 20:28:36 -05:00
|
|
|
result::Result,
|
|
|
|
};
|
|
|
|
|
2023-05-28 02:36:29 -05:00
|
|
|
pub mod errors;
|
2023-05-25 20:28:36 -05:00
|
|
|
mod tests;
|
|
|
|
|
2023-05-27 02:22:10 -05:00
|
|
|
/// Trait a type must satisfy to be element of a matrix. This is
|
|
|
|
/// mostly to reduce writing trait bounds afterwards.
|
|
|
|
pub trait ToMatrix:
|
|
|
|
Mul<Output = Self>
|
|
|
|
+ Add<Output = Self>
|
|
|
|
+ Sub<Output = Self>
|
|
|
|
+ Zero<Output = Self>
|
|
|
|
+ Neg<Output = Self>
|
|
|
|
+ Copy
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-27 03:43:37 -05:00
|
|
|
/// Blanket implementation for [`ToMatrix`] for any type that satisfies its bounds.
|
2023-05-27 02:22:10 -05:00
|
|
|
impl<T> ToMatrix for T where
|
|
|
|
T: Mul<Output = T>
|
|
|
|
+ Add<Output = T>
|
|
|
|
+ Sub<Output = T>
|
|
|
|
+ Zero<Output = T>
|
|
|
|
+ Neg<Output = T>
|
|
|
|
+ Copy
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A generic matrix struct (over any type with [`Add`], [`Sub`], [`Mul`],
|
|
|
|
/// [`Zero`], [`Neg`] and [`Copy`] implemented).
|
2023-05-25 20:28:36 -05:00
|
|
|
/// Look at [`from`](Self::from()) to see examples.
|
|
|
|
#[derive(PartialEq, Debug, Clone)]
|
2023-05-27 02:22:10 -05:00
|
|
|
pub struct Matrix<T: ToMatrix> {
|
2023-05-25 20:28:36 -05:00
|
|
|
entries: Vec<Vec<T>>,
|
|
|
|
}
|
|
|
|
|
2023-05-27 02:22:10 -05:00
|
|
|
impl<T: ToMatrix> Matrix<T> {
|
2023-05-25 20:28:36 -05:00
|
|
|
/// Creates a matrix from given 2D "array" in a `Vec<Vec<T>>` form.
|
2023-05-25 20:40:09 -05:00
|
|
|
/// It'll throw an error if all the given rows aren't of the same size.
|
2023-05-25 20:28:36 -05:00
|
|
|
/// # Example
|
|
|
|
/// ```
|
2023-05-25 21:06:16 -05:00
|
|
|
/// use matrix_basic::Matrix;
|
2023-05-27 01:41:41 -05:00
|
|
|
/// let m = Matrix::from(vec![vec![1, 2, 3], vec![4, 5, 6]]);
|
2023-05-25 20:28:36 -05:00
|
|
|
/// ```
|
|
|
|
/// will create the following matrix:
|
2023-05-27 01:41:41 -05:00
|
|
|
/// ⌈1, 2, 3⌉
|
|
|
|
/// ⌊4, 5, 6⌋
|
2023-05-28 02:36:29 -05:00
|
|
|
pub fn from(entries: Vec<Vec<T>>) -> Result<Matrix<T>, MatrixError> {
|
2023-05-25 20:28:36 -05:00
|
|
|
let mut equal_rows = true;
|
|
|
|
let row_len = entries[0].len();
|
|
|
|
for row in &entries {
|
|
|
|
if row_len != row.len() {
|
|
|
|
equal_rows = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if equal_rows {
|
|
|
|
Ok(Matrix { entries })
|
|
|
|
} else {
|
2023-05-28 02:36:29 -05:00
|
|
|
Err(MatrixError::UnequalRows)
|
2023-05-25 20:28:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 17:33:51 -05:00
|
|
|
/// Returns the height of a matrix.
|
2023-05-25 20:28:36 -05:00
|
|
|
pub fn height(&self) -> usize {
|
|
|
|
self.entries.len()
|
|
|
|
}
|
|
|
|
|
2023-05-26 17:33:51 -05:00
|
|
|
/// Returns the width of a matrix.
|
2023-05-25 20:28:36 -05:00
|
|
|
pub fn width(&self) -> usize {
|
|
|
|
self.entries[0].len()
|
|
|
|
}
|
|
|
|
|
2023-05-26 17:33:51 -05:00
|
|
|
/// Returns the transpose of a matrix.
|
2023-05-27 01:02:06 -05:00
|
|
|
pub fn transpose(&self) -> Self {
|
2023-05-25 20:28:36 -05:00
|
|
|
let mut out = Vec::new();
|
|
|
|
for i in 0..self.width() {
|
|
|
|
let mut column = Vec::new();
|
|
|
|
for row in &self.entries {
|
|
|
|
column.push(row[i]);
|
|
|
|
}
|
|
|
|
out.push(column)
|
|
|
|
}
|
|
|
|
Matrix { entries: out }
|
|
|
|
}
|
|
|
|
|
2023-05-26 17:33:51 -05:00
|
|
|
/// Returns a reference to the rows of a matrix as `&Vec<Vec<T>>`.
|
2023-05-25 20:28:36 -05:00
|
|
|
pub fn rows(&self) -> &Vec<Vec<T>> {
|
|
|
|
&self.entries
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the columns of a matrix as `Vec<Vec<T>>`.
|
2023-05-27 01:02:06 -05:00
|
|
|
pub fn columns(&self) -> Vec<Vec<T>> {
|
2023-05-25 20:28:36 -05:00
|
|
|
self.transpose().entries
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if a matrix is square and false otherwise.
|
|
|
|
pub fn is_square(&self) -> bool {
|
|
|
|
self.height() == self.width()
|
|
|
|
}
|
|
|
|
|
2023-05-26 17:33:51 -05:00
|
|
|
/// Returns a matrix after removing the provided row and column from it.
|
2023-05-25 20:28:36 -05:00
|
|
|
/// Note: Row and column numbers are 0-indexed.
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
2023-05-25 21:06:16 -05:00
|
|
|
/// use matrix_basic::Matrix;
|
2023-05-27 01:41:41 -05:00
|
|
|
/// let m = Matrix::from(vec![vec![1, 2, 3], vec![4, 5, 6]]).unwrap();
|
|
|
|
/// let n = Matrix::from(vec![vec![5, 6]]).unwrap();
|
|
|
|
/// assert_eq!(m.submatrix(0, 0), n);
|
2023-05-25 20:28:36 -05:00
|
|
|
/// ```
|
2023-05-27 01:02:06 -05:00
|
|
|
pub fn submatrix(&self, row: usize, col: usize) -> Self {
|
2023-05-25 20:28:36 -05:00
|
|
|
let mut out = Vec::new();
|
|
|
|
for (m, row_iter) in self.entries.iter().enumerate() {
|
|
|
|
if m == row {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let mut new_row = Vec::new();
|
|
|
|
for (n, entry) in row_iter.iter().enumerate() {
|
|
|
|
if n != col {
|
|
|
|
new_row.push(*entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out.push(new_row);
|
|
|
|
}
|
|
|
|
Matrix { entries: out }
|
|
|
|
}
|
|
|
|
|
2023-05-27 01:09:52 -05:00
|
|
|
/// Returns the determinant of a square matrix.
|
|
|
|
/// This uses basic recursive algorithm using cofactor-minor.
|
2023-05-25 22:59:01 -05:00
|
|
|
/// See [`det_in_field`](Self::det_in_field()) for faster determinant calculation in fields.
|
2023-05-25 20:40:09 -05:00
|
|
|
/// It'll throw an error if the provided matrix isn't square.
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
2023-05-25 21:06:16 -05:00
|
|
|
/// use matrix_basic::Matrix;
|
2023-05-27 01:41:41 -05:00
|
|
|
/// let m = Matrix::from(vec![vec![1, 2], vec![3, 4]]).unwrap();
|
|
|
|
/// assert_eq!(m.det(), Ok(-2));
|
2023-05-25 20:40:09 -05:00
|
|
|
/// ```
|
2023-05-28 02:36:29 -05:00
|
|
|
pub fn det(&self) -> Result<T, MatrixError> {
|
2023-05-25 20:28:36 -05:00
|
|
|
if self.is_square() {
|
2023-05-25 20:40:09 -05:00
|
|
|
// It's a recursive algorithm using minors.
|
2023-05-25 22:59:01 -05:00
|
|
|
// TODO: Implement a faster algorithm.
|
2023-05-25 20:28:36 -05:00
|
|
|
let out = if self.width() == 1 {
|
|
|
|
self.entries[0][0]
|
|
|
|
} else {
|
2023-05-25 20:40:09 -05:00
|
|
|
// Add the minors multiplied by cofactors.
|
2023-05-25 20:28:36 -05:00
|
|
|
let n = 0..self.width();
|
|
|
|
let mut out = T::zero();
|
|
|
|
for i in n {
|
|
|
|
if i.is_even() {
|
|
|
|
out = out + (self.entries[0][i] * self.submatrix(0, i).det().unwrap());
|
|
|
|
} else {
|
|
|
|
out = out - (self.entries[0][i] * self.submatrix(0, i).det().unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out
|
|
|
|
};
|
|
|
|
Ok(out)
|
|
|
|
} else {
|
2023-05-28 02:36:29 -05:00
|
|
|
Err(MatrixError::NotSquare)
|
2023-05-25 20:28:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 17:33:51 -05:00
|
|
|
/// Returns the determinant of a square matrix over a field i.e. needs [`One`] and [`Div`] traits.
|
2023-05-25 22:59:01 -05:00
|
|
|
/// See [`det`](Self::det()) for determinants in rings.
|
|
|
|
/// This method uses row reduction as is much faster.
|
|
|
|
/// It'll throw an error if the provided matrix isn't square.
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// use matrix_basic::Matrix;
|
2023-05-27 01:41:41 -05:00
|
|
|
/// let m = Matrix::from(vec![vec![1.0, 2.0], vec![3.0, 4.0]]).unwrap();
|
2023-05-27 18:35:06 -05:00
|
|
|
/// assert_eq!(m.det_in_field(), Ok(-2.0));
|
2023-05-25 22:59:01 -05:00
|
|
|
/// ```
|
2023-05-28 02:36:29 -05:00
|
|
|
pub fn det_in_field(&self) -> Result<T, MatrixError>
|
2023-05-25 22:59:01 -05:00
|
|
|
where
|
|
|
|
T: One,
|
|
|
|
T: PartialEq,
|
|
|
|
T: Div<Output = T>,
|
|
|
|
{
|
|
|
|
if self.is_square() {
|
|
|
|
// Cloning is necessary as we'll be doing row operations on it.
|
|
|
|
let mut rows = self.entries.clone();
|
|
|
|
let mut multiplier = T::one();
|
2023-05-26 00:43:33 -05:00
|
|
|
let h = self.height();
|
|
|
|
let w = self.width();
|
2023-05-27 18:35:06 -05:00
|
|
|
for i in 0..(h - 1) {
|
2023-05-25 22:59:01 -05:00
|
|
|
// First check if the row has diagonal element 0, if yes, then swap.
|
|
|
|
if rows[i][i] == T::zero() {
|
|
|
|
let mut zero_column = true;
|
2023-05-26 00:43:33 -05:00
|
|
|
for j in (i + 1)..h {
|
2023-05-25 22:59:01 -05:00
|
|
|
if rows[j][i] != T::zero() {
|
|
|
|
rows.swap(i, j);
|
2023-05-27 18:35:06 -05:00
|
|
|
multiplier = -multiplier;
|
2023-05-25 22:59:01 -05:00
|
|
|
zero_column = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if zero_column {
|
|
|
|
return Ok(T::zero());
|
|
|
|
}
|
|
|
|
}
|
2023-05-26 00:43:33 -05:00
|
|
|
for j in (i + 1)..h {
|
2023-05-26 00:06:41 -05:00
|
|
|
let ratio = rows[j][i] / rows[i][i];
|
2023-05-26 00:43:33 -05:00
|
|
|
for k in i..w {
|
2023-05-26 00:06:41 -05:00
|
|
|
rows[j][k] = rows[j][k] - rows[i][k] * ratio;
|
2023-05-25 22:59:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i, row) in rows.iter().enumerate() {
|
|
|
|
multiplier = multiplier * row[i];
|
|
|
|
}
|
|
|
|
Ok(multiplier)
|
|
|
|
} else {
|
2023-05-28 02:36:29 -05:00
|
|
|
Err(MatrixError::NotSquare)
|
2023-05-25 22:59:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-27 01:09:52 -05:00
|
|
|
/// Returns the row echelon form of a matrix over a field i.e. needs the [`Div`] trait.
|
2023-05-26 00:06:41 -05:00
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// use matrix_basic::Matrix;
|
2023-05-27 01:41:41 -05:00
|
|
|
/// let m = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![3.0, 4.0, 5.0]]).unwrap();
|
|
|
|
/// let n = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![0.0, -2.0, -4.0]]).unwrap();
|
|
|
|
/// assert_eq!(m.row_echelon(), n);
|
2023-05-26 00:06:41 -05:00
|
|
|
/// ```
|
|
|
|
pub fn row_echelon(&self) -> Self
|
|
|
|
where
|
|
|
|
T: PartialEq,
|
|
|
|
T: Div<Output = T>,
|
|
|
|
{
|
|
|
|
// Cloning is necessary as we'll be doing row operations on it.
|
|
|
|
let mut rows = self.entries.clone();
|
|
|
|
let mut offset = 0;
|
2023-05-26 00:43:33 -05:00
|
|
|
let h = self.height();
|
|
|
|
let w = self.width();
|
2023-05-27 18:35:06 -05:00
|
|
|
for i in 0..(h - 1) {
|
2023-05-26 00:06:41 -05:00
|
|
|
// Check if all the rows below are 0
|
|
|
|
if i + offset >= self.width() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// First check if the row has diagonal element 0, if yes, then swap.
|
|
|
|
if rows[i][i + offset] == T::zero() {
|
|
|
|
let mut zero_column = true;
|
2023-05-26 00:43:33 -05:00
|
|
|
for j in (i + 1)..h {
|
2023-05-26 00:06:41 -05:00
|
|
|
if rows[j][i + offset] != T::zero() {
|
|
|
|
rows.swap(i, j);
|
|
|
|
zero_column = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if zero_column {
|
|
|
|
offset += 1;
|
|
|
|
}
|
|
|
|
}
|
2023-05-26 00:43:33 -05:00
|
|
|
for j in (i + 1)..h {
|
2023-05-26 00:06:41 -05:00
|
|
|
let ratio = rows[j][i + offset] / rows[i][i + offset];
|
2023-05-26 00:43:33 -05:00
|
|
|
for k in (i + offset)..w {
|
2023-05-26 00:06:41 -05:00
|
|
|
rows[j][k] = rows[j][k] - rows[i][k] * ratio;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Matrix { entries: rows }
|
|
|
|
}
|
|
|
|
|
2023-05-27 01:09:52 -05:00
|
|
|
/// Returns the column echelon form of a matrix over a field i.e. needs the [`Div`] trait.
|
2023-05-26 00:43:33 -05:00
|
|
|
/// It's just the transpose of the row echelon form of the transpose.
|
|
|
|
/// See [`row_echelon`](Self::row_echelon()) and [`transpose`](Self::transpose()).
|
|
|
|
pub fn column_echelon(&self) -> Self
|
|
|
|
where
|
|
|
|
T: PartialEq,
|
|
|
|
T: Div<Output = T>,
|
|
|
|
{
|
|
|
|
self.transpose().row_echelon().transpose()
|
|
|
|
}
|
|
|
|
|
2023-05-27 01:09:52 -05:00
|
|
|
/// Returns the reduced row echelon form of a matrix over a field i.e. needs the `Div`] trait.
|
2023-05-26 00:43:33 -05:00
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// use matrix_basic::Matrix;
|
2023-05-27 01:41:41 -05:00
|
|
|
/// let m = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![3.0, 4.0, 5.0]]).unwrap();
|
|
|
|
/// let n = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![0.0, 1.0, 2.0]]).unwrap();
|
|
|
|
/// assert_eq!(m.reduced_row_echelon(), n);
|
2023-05-26 00:43:33 -05:00
|
|
|
/// ```
|
|
|
|
pub fn reduced_row_echelon(&self) -> Self
|
|
|
|
where
|
|
|
|
T: PartialEq,
|
|
|
|
T: Div<Output = T>,
|
|
|
|
{
|
|
|
|
let mut echelon = self.row_echelon();
|
|
|
|
let mut offset = 0;
|
|
|
|
for row in &mut echelon.entries {
|
|
|
|
while row[offset] == T::zero() {
|
|
|
|
offset += 1;
|
|
|
|
}
|
|
|
|
let divisor = row[offset];
|
|
|
|
for entry in row.iter_mut().skip(offset) {
|
|
|
|
*entry = *entry / divisor;
|
|
|
|
}
|
|
|
|
offset += 1;
|
|
|
|
}
|
|
|
|
echelon
|
|
|
|
}
|
|
|
|
|
2023-05-25 20:28:36 -05:00
|
|
|
/// Creates a zero matrix of a given size.
|
2023-05-27 01:02:06 -05:00
|
|
|
pub fn zero(height: usize, width: usize) -> Self {
|
2023-05-25 20:28:36 -05:00
|
|
|
let mut out = Vec::new();
|
|
|
|
for _ in 0..height {
|
|
|
|
let mut new_row = Vec::new();
|
|
|
|
for _ in 0..width {
|
|
|
|
new_row.push(T::zero());
|
|
|
|
}
|
|
|
|
out.push(new_row);
|
|
|
|
}
|
|
|
|
Matrix { entries: out }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates an identity matrix of a given size.
|
2023-05-27 01:09:52 -05:00
|
|
|
/// It needs the [`One`] trait.
|
2023-05-25 20:28:36 -05:00
|
|
|
pub fn identity(size: usize) -> Self
|
|
|
|
where
|
|
|
|
T: One,
|
|
|
|
{
|
2023-05-27 01:28:01 -05:00
|
|
|
let mut out = Matrix::zero(size, size);
|
|
|
|
for (i, row) in out.entries.iter_mut().enumerate() {
|
|
|
|
row[i] = T::one();
|
2023-05-25 20:28:36 -05:00
|
|
|
}
|
2023-05-27 01:28:01 -05:00
|
|
|
out
|
2023-05-25 20:28:36 -05:00
|
|
|
}
|
2023-05-26 00:43:33 -05:00
|
|
|
|
2023-05-27 01:09:52 -05:00
|
|
|
/// Returns the trace of a square matrix.
|
|
|
|
/// It'll throw an error if the provided matrix isn't square.
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// use matrix_basic::Matrix;
|
2023-05-27 01:41:41 -05:00
|
|
|
/// let m = Matrix::from(vec![vec![1, 2], vec![3, 4]]).unwrap();
|
|
|
|
/// assert_eq!(m.trace(), Ok(5));
|
2023-05-27 01:09:52 -05:00
|
|
|
/// ```
|
2023-05-28 02:36:29 -05:00
|
|
|
pub fn trace(self) -> Result<T, MatrixError> {
|
2023-05-27 01:09:52 -05:00
|
|
|
if self.is_square() {
|
|
|
|
let mut out = self.entries[0][0];
|
|
|
|
for i in 1..self.height() {
|
|
|
|
out = out + self.entries[i][i];
|
|
|
|
}
|
|
|
|
Ok(out)
|
|
|
|
} else {
|
2023-05-28 02:36:29 -05:00
|
|
|
Err(MatrixError::NotSquare)
|
2023-05-27 01:09:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-27 01:28:01 -05:00
|
|
|
/// Returns a diagonal matrix with a given diagonal.
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// use matrix_basic::Matrix;
|
2023-05-27 01:41:41 -05:00
|
|
|
/// let m = Matrix::diagonal_matrix(vec![1, 2, 3]);
|
|
|
|
/// let n = Matrix::from(vec![vec![1, 0, 0], vec![0, 2, 0], vec![0, 0, 3]]).unwrap();
|
2023-05-27 01:28:01 -05:00
|
|
|
///
|
2023-05-27 01:41:41 -05:00
|
|
|
/// assert_eq!(m, n);
|
2023-05-27 01:28:01 -05:00
|
|
|
/// ```
|
|
|
|
pub fn diagonal_matrix(diag: Vec<T>) -> Self {
|
|
|
|
let size = diag.len();
|
|
|
|
let mut out = Matrix::zero(size, size);
|
|
|
|
for (i, row) in out.entries.iter_mut().enumerate() {
|
|
|
|
row[i] = diag[i];
|
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2023-05-27 01:41:41 -05:00
|
|
|
/// Multiplies all entries of a matrix by a scalar.
|
|
|
|
/// Note that it modifies the supplied matrix.
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// use matrix_basic::Matrix;
|
|
|
|
/// let mut m = Matrix::from(vec![vec![1, 2, 0], vec![0, 2, 5], vec![0, 0, 3]]).unwrap();
|
|
|
|
/// let n = Matrix::from(vec![vec![2, 4, 0], vec![0, 4, 10], vec![0, 0, 6]]).unwrap();
|
|
|
|
/// m.mul_scalar(2);
|
|
|
|
///
|
|
|
|
/// assert_eq!(m, n);
|
|
|
|
/// ```
|
|
|
|
pub fn mul_scalar(&mut self, scalar: T) {
|
|
|
|
for row in &mut self.entries {
|
|
|
|
for entry in row {
|
|
|
|
*entry = *entry * scalar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-27 18:35:06 -05:00
|
|
|
/// Returns the inverse of a square matrix. Throws an error if the matrix isn't square.
|
|
|
|
/// /// # Example
|
|
|
|
/// ```
|
|
|
|
/// use matrix_basic::Matrix;
|
|
|
|
/// let m = Matrix::from(vec![vec![1.0, 2.0], vec![3.0, 4.0]]).unwrap();
|
|
|
|
/// let n = Matrix::from(vec![vec![-2.0, 1.0], vec![1.5, -0.5]]).unwrap();
|
|
|
|
/// assert_eq!(m.inverse(), Ok(n));
|
|
|
|
/// ```
|
2023-05-28 02:36:29 -05:00
|
|
|
pub fn inverse(&self) -> Result<Self, MatrixError>
|
2023-05-27 18:35:06 -05:00
|
|
|
where
|
|
|
|
T: Div<Output = T>,
|
|
|
|
T: One,
|
|
|
|
T: PartialEq,
|
|
|
|
{
|
|
|
|
if self.is_square() {
|
|
|
|
// We'll use the basic technique of using an augmented matrix (in essence)
|
|
|
|
// Cloning is necessary as we'll be doing row operations on it.
|
|
|
|
let mut rows = self.entries.clone();
|
|
|
|
let h = self.height();
|
|
|
|
let w = self.width();
|
|
|
|
let mut out = Self::identity(h).entries;
|
|
|
|
|
|
|
|
// First we get row echelon form
|
|
|
|
for i in 0..(h - 1) {
|
|
|
|
// First check if the row has diagonal element 0, if yes, then swap.
|
|
|
|
if rows[i][i] == T::zero() {
|
|
|
|
let mut zero_column = true;
|
|
|
|
for j in (i + 1)..h {
|
|
|
|
if rows[j][i] != T::zero() {
|
|
|
|
rows.swap(i, j);
|
|
|
|
out.swap(i, j);
|
|
|
|
zero_column = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if zero_column {
|
2023-05-28 02:36:29 -05:00
|
|
|
return Err(MatrixError::Singular);
|
2023-05-27 18:35:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for j in (i + 1)..h {
|
|
|
|
let ratio = rows[j][i] / rows[i][i];
|
|
|
|
for k in i..w {
|
|
|
|
rows[j][k] = rows[j][k] - rows[i][k] * ratio;
|
|
|
|
}
|
|
|
|
// We cannot skip entries here as they might not be 0
|
|
|
|
for k in 0..w {
|
|
|
|
out[j][k] = out[j][k] - out[i][k] * ratio;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then we reduce the rows
|
|
|
|
for i in 0..h {
|
|
|
|
if rows[i][i] == T::zero() {
|
2023-05-28 02:36:29 -05:00
|
|
|
return Err(MatrixError::Singular);
|
2023-05-27 18:35:06 -05:00
|
|
|
}
|
|
|
|
let divisor = rows[i][i];
|
|
|
|
for entry in rows[i].iter_mut().skip(i) {
|
|
|
|
*entry = *entry / divisor;
|
|
|
|
}
|
|
|
|
for entry in out[i].iter_mut() {
|
|
|
|
*entry = *entry / divisor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we do upside down row reduction
|
|
|
|
for i in (1..h).rev() {
|
|
|
|
for j in (0..i).rev() {
|
|
|
|
let ratio = rows[j][i];
|
|
|
|
for k in 0..w {
|
|
|
|
out[j][k] = out[j][k] - out[i][k] * ratio;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Matrix { entries: out })
|
|
|
|
} else {
|
2023-05-28 02:36:29 -05:00
|
|
|
Err(MatrixError::NotSquare)
|
2023-05-27 18:35:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 00:43:33 -05:00
|
|
|
// TODO: Canonical forms, eigenvalues, eigenvectors etc.
|
2023-05-25 20:28:36 -05:00
|
|
|
}
|
|
|
|
|
2023-05-27 02:22:10 -05:00
|
|
|
impl<T: Debug + ToMatrix> Display for Matrix<T> {
|
2023-05-25 20:28:36 -05:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{:?}", self.entries)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-27 02:22:10 -05:00
|
|
|
impl<T: Mul<Output = T> + ToMatrix> Mul for Matrix<T> {
|
2023-05-25 22:59:01 -05:00
|
|
|
// TODO: Implement a faster algorithm.
|
2023-05-25 20:28:36 -05:00
|
|
|
type Output = Self;
|
2023-05-26 01:18:52 -05:00
|
|
|
fn mul(self, other: Self) -> Self::Output {
|
2023-05-25 20:28:36 -05:00
|
|
|
let width = self.width();
|
|
|
|
if width != other.height() {
|
2023-05-28 02:36:29 -05:00
|
|
|
panic!("row length of first matrix != column length of second matrix");
|
2023-05-25 20:28:36 -05:00
|
|
|
} else {
|
|
|
|
let mut out = Vec::new();
|
|
|
|
for row in self.rows() {
|
|
|
|
let mut new_row = Vec::new();
|
|
|
|
for col in other.columns() {
|
|
|
|
let mut prod = row[0] * col[0];
|
|
|
|
for i in 1..width {
|
|
|
|
prod = prod + (row[i] * col[i]);
|
|
|
|
}
|
|
|
|
new_row.push(prod)
|
|
|
|
}
|
|
|
|
out.push(new_row);
|
|
|
|
}
|
|
|
|
Matrix { entries: out }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-27 02:22:10 -05:00
|
|
|
impl<T: Mul<Output = T> + ToMatrix> Add for Matrix<T> {
|
2023-05-25 20:28:36 -05:00
|
|
|
type Output = Self;
|
2023-05-26 01:18:52 -05:00
|
|
|
fn add(self, other: Self) -> Self::Output {
|
2023-05-25 20:28:36 -05:00
|
|
|
if self.height() == other.height() && self.width() == other.width() {
|
|
|
|
let mut out = self.entries.clone();
|
|
|
|
for (i, row) in self.rows().iter().enumerate() {
|
|
|
|
for (j, entry) in other.rows()[i].iter().enumerate() {
|
|
|
|
out[i][j] = row[j] + *entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Matrix { entries: out }
|
|
|
|
} else {
|
2023-05-28 02:36:29 -05:00
|
|
|
panic!("provided matrices have different dimensions");
|
2023-05-25 20:28:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-27 02:22:10 -05:00
|
|
|
impl<T: ToMatrix> Neg for Matrix<T> {
|
2023-05-25 20:28:36 -05:00
|
|
|
type Output = Self;
|
2023-05-26 01:18:52 -05:00
|
|
|
fn neg(self) -> Self::Output {
|
|
|
|
let mut out = self;
|
|
|
|
for row in &mut out.entries {
|
|
|
|
for entry in row {
|
|
|
|
*entry = -*entry;
|
2023-05-25 20:28:36 -05:00
|
|
|
}
|
2023-05-26 01:18:52 -05:00
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-27 02:22:10 -05:00
|
|
|
impl<T: ToMatrix> Sub for Matrix<T> {
|
2023-05-26 01:18:52 -05:00
|
|
|
type Output = Self;
|
|
|
|
fn sub(self, other: Self) -> Self::Output {
|
|
|
|
if self.height() == other.height() && self.width() == other.width() {
|
|
|
|
self + -other
|
2023-05-25 20:28:36 -05:00
|
|
|
} else {
|
2023-05-28 02:36:29 -05:00
|
|
|
panic!("provided matrices have different dimensions");
|
2023-05-25 20:28:36 -05:00
|
|
|
}
|
2023-05-25 20:07:10 -05:00
|
|
|
}
|
2023-05-24 21:46:25 -05:00
|
|
|
}
|
2023-05-27 00:44:36 -05:00
|
|
|
|
|
|
|
/// Trait for conversion between matrices of different types.
|
2023-05-27 18:59:09 -05:00
|
|
|
/// It only has a [`matrix_from()`](Self::matrix_from()) method.
|
2023-05-27 00:44:36 -05:00
|
|
|
/// This is needed since negative trait bound are not supported in stable Rust
|
|
|
|
/// yet, so we'll have a conflict trying to implement [`From`].
|
|
|
|
/// I plan to change this to the default From trait as soon as some sort
|
|
|
|
/// of specialization system is implemented.
|
|
|
|
/// You can track this issue [here](https://github.com/rust-lang/rust/issues/42721).
|
2023-05-27 18:59:09 -05:00
|
|
|
pub trait MatrixFrom<T: ToMatrix> {
|
|
|
|
/// Method for getting a matrix of a new type from a matrix of type [`Matrix<T>`].
|
2023-05-27 16:05:26 -05:00
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// use matrix_basic::Matrix;
|
2023-05-27 18:59:09 -05:00
|
|
|
/// use matrix_basic::MatrixFrom;
|
2023-05-27 16:05:26 -05:00
|
|
|
///
|
|
|
|
/// let a = Matrix::from(vec![vec![1, 2, 3], vec![0, 1, 2]]).unwrap();
|
|
|
|
/// let b = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![0.0, 1.0, 2.0]]).unwrap();
|
2023-05-27 18:59:09 -05:00
|
|
|
/// let c = Matrix::<f64>::matrix_from(a); // Type annotation is needed here
|
2023-05-27 16:05:26 -05:00
|
|
|
///
|
|
|
|
/// assert_eq!(c, b);
|
|
|
|
/// ```
|
2023-05-27 18:59:09 -05:00
|
|
|
fn matrix_from(input: Matrix<T>) -> Self;
|
2023-05-27 00:44:36 -05:00
|
|
|
}
|
|
|
|
|
2023-05-27 18:59:09 -05:00
|
|
|
/// Blanket implementation of [`MatrixFrom<T>`] for converting [`Matrix<S>`] to [`Matrix<T>`] whenever
|
|
|
|
/// `S` implements [`From(T)`]. Look at [`matrix_into`](Self::matrix_into()).
|
|
|
|
impl<T: ToMatrix, S: ToMatrix + From<T>> MatrixFrom<T> for Matrix<S> {
|
|
|
|
fn matrix_from(input: Matrix<T>) -> Self {
|
2023-05-27 00:44:36 -05:00
|
|
|
let mut out = Vec::new();
|
2023-05-27 18:59:09 -05:00
|
|
|
for row in input.entries {
|
|
|
|
let mut new_row: Vec<S> = Vec::new();
|
2023-05-27 00:44:36 -05:00
|
|
|
for entry in row {
|
|
|
|
new_row.push(entry.into());
|
|
|
|
}
|
|
|
|
out.push(new_row)
|
|
|
|
}
|
|
|
|
Matrix { entries: out }
|
|
|
|
}
|
|
|
|
}
|
2023-05-27 16:05:26 -05:00
|
|
|
|
2023-05-27 18:59:09 -05:00
|
|
|
/// Sister trait of [`MatrixFrom`]. Basically does the same thing, just with a
|
2023-05-27 16:05:26 -05:00
|
|
|
/// different syntax.
|
2023-05-27 18:59:09 -05:00
|
|
|
pub trait MatrixInto<T> {
|
|
|
|
/// Method for converting a matrix [`Matrix<T>`] to another type.
|
2023-05-27 16:05:26 -05:00
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// use matrix_basic::Matrix;
|
2023-05-27 18:59:09 -05:00
|
|
|
/// use matrix_basic::MatrixInto;
|
2023-05-27 16:05:26 -05:00
|
|
|
///
|
|
|
|
/// let a = Matrix::from(vec![vec![1, 2, 3], vec![0, 1, 2]]).unwrap();
|
|
|
|
/// let b = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![0.0, 1.0, 2.0]]).unwrap();
|
2023-05-27 18:59:09 -05:00
|
|
|
/// let c: Matrix<f64> = a.matrix_into(); // Type annotation is needed here
|
|
|
|
///
|
2023-05-27 16:05:26 -05:00
|
|
|
///
|
|
|
|
/// assert_eq!(c, b);
|
|
|
|
/// ```
|
2023-05-27 18:59:09 -05:00
|
|
|
fn matrix_into(self) -> T;
|
2023-05-27 16:05:26 -05:00
|
|
|
}
|
|
|
|
|
2023-05-27 18:59:09 -05:00
|
|
|
/// Blanket implementation of [`MatrixInto<T>`] for [`Matrix<S>`] whenever `T`
|
|
|
|
/// (which is actually some)[`Matrix<U>`] implements [`MatrixFrom<S>`].
|
|
|
|
impl<T: MatrixFrom<S>, S: ToMatrix> MatrixInto<T> for Matrix<S> {
|
|
|
|
fn matrix_into(self) -> T {
|
|
|
|
T::matrix_from(self)
|
2023-05-27 16:05:26 -05:00
|
|
|
}
|
|
|
|
}
|