mirror of
https://github.com/SinTan1729/matrix-basic.git
synced 2024-12-25 21:48:35 -06:00
new: Added some documentation
This commit is contained in:
parent
68dd53a8db
commit
9af71d9b72
2 changed files with 44 additions and 10 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
//! This is a crate for very basic matrix operations
|
||||||
|
//! with any type that supports addition, substraction,
|
||||||
|
//! and multiplication. Additional properties might be
|
||||||
|
//! needed for certain operations.
|
||||||
|
//!
|
||||||
|
//! Sayantan Santra (2023)
|
||||||
|
|
||||||
pub mod matrix;
|
pub mod matrix;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -8,12 +8,25 @@ use std::{
|
||||||
result::Result,
|
result::Result,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// A generic matrix struct (over any type with addition, substraction
|
||||||
|
/// and multiplication defined on it).
|
||||||
|
/// Look at [`from`](Self::from()) to see examples.
|
||||||
#[derive(PartialEq, Debug, Clone)]
|
#[derive(PartialEq, Debug, Clone)]
|
||||||
pub struct Matrix<T: Mul + Add + Sub> {
|
pub struct Matrix<T: Mul + Add + Sub> {
|
||||||
entries: Vec<Vec<T>>,
|
entries: Vec<Vec<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Mul + Add + Sub> Matrix<T> {
|
impl<T: Mul + Add + Sub> Matrix<T> {
|
||||||
|
/// Creates a matrix from given 2D "array" in a `Vec<Vec<T>>` form.
|
||||||
|
/// It'll throw error if all the given rows aren't of the same size.
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use matrix::matrix::Matrix;
|
||||||
|
/// let m = Matrix::from(vec![vec![1,2,3], vec![4,5,6]]);
|
||||||
|
/// ```
|
||||||
|
/// will create the following matrix:
|
||||||
|
/// ⌈1,2,3⌉
|
||||||
|
/// ⌊4,5,6⌋
|
||||||
pub fn from(entries: Vec<Vec<T>>) -> Result<Matrix<T>, &'static str> {
|
pub fn from(entries: Vec<Vec<T>>) -> Result<Matrix<T>, &'static str> {
|
||||||
let mut equal_rows = true;
|
let mut equal_rows = true;
|
||||||
let row_len = entries[0].len();
|
let row_len = entries[0].len();
|
||||||
|
@ -30,14 +43,17 @@ impl<T: Mul + Add + Sub> Matrix<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the height of a matrix.
|
||||||
pub fn height(&self) -> usize {
|
pub fn height(&self) -> usize {
|
||||||
self.entries.len()
|
self.entries.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the width of a matrix.
|
||||||
pub fn width(&self) -> usize {
|
pub fn width(&self) -> usize {
|
||||||
self.entries[0].len()
|
self.entries[0].len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the transpose of a matrix.
|
||||||
pub fn transpose(&self) -> Self
|
pub fn transpose(&self) -> Self
|
||||||
where
|
where
|
||||||
T: Copy,
|
T: Copy,
|
||||||
|
@ -53,13 +69,12 @@ impl<T: Mul + Add + Sub> Matrix<T> {
|
||||||
Matrix { entries: out }
|
Matrix { entries: out }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rows(&self) -> Vec<Vec<T>>
|
/// Return a reference to the rows of a matrix as `&Vec<Vec<T>>`.
|
||||||
where
|
pub fn rows(&self) -> &Vec<Vec<T>> {
|
||||||
T: Copy,
|
&self.entries
|
||||||
{
|
|
||||||
self.entries.clone()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the columns of a matrix as `Vec<Vec<T>>`.
|
||||||
pub fn columns(&self) -> Vec<Vec<T>>
|
pub fn columns(&self) -> Vec<Vec<T>>
|
||||||
where
|
where
|
||||||
T: Copy,
|
T: Copy,
|
||||||
|
@ -67,22 +82,32 @@ impl<T: Mul + Add + Sub> Matrix<T> {
|
||||||
self.transpose().entries
|
self.transpose().entries
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return true if a matrix is square and false otherwise.
|
||||||
pub fn is_square(&self) -> bool {
|
pub fn is_square(&self) -> bool {
|
||||||
self.height() == self.width()
|
self.height() == self.width()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn submatrix(&self, i: usize, j: usize) -> Self
|
/// Return a matrix after removing the provided row and column from it.
|
||||||
|
/// Note: Row and column numbers are 0-indexed.
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use matrix::matrix::Matrix;
|
||||||
|
/// 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);
|
||||||
|
/// ```
|
||||||
|
pub fn submatrix(&self, row: usize, col: usize) -> Self
|
||||||
where
|
where
|
||||||
T: Copy,
|
T: Copy,
|
||||||
{
|
{
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
for (m, row) in self.rows().iter().enumerate() {
|
for (m, row_iter) in self.entries.iter().enumerate() {
|
||||||
if m == i {
|
if m == row {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let mut new_row = Vec::new();
|
let mut new_row = Vec::new();
|
||||||
for (n, entry) in row.iter().enumerate() {
|
for (n, entry) in row_iter.iter().enumerate() {
|
||||||
if n != j {
|
if n != col {
|
||||||
new_row.push(*entry);
|
new_row.push(*entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -119,6 +144,7 @@ impl<T: Mul + Add + Sub> Matrix<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a zero matrix of a given size.
|
||||||
pub fn zero(height: usize, width: usize) -> Self
|
pub fn zero(height: usize, width: usize) -> Self
|
||||||
where
|
where
|
||||||
T: Zero,
|
T: Zero,
|
||||||
|
@ -134,6 +160,7 @@ impl<T: Mul + Add + Sub> Matrix<T> {
|
||||||
Matrix { entries: out }
|
Matrix { entries: out }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates an identity matrix of a given size.
|
||||||
pub fn identity(size: usize) -> Self
|
pub fn identity(size: usize) -> Self
|
||||||
where
|
where
|
||||||
T: Zero,
|
T: Zero,
|
||||||
|
|
Loading…
Reference in a new issue