new: Added trace method

This commit is contained in:
Sayantan Santra 2023-05-27 01:09:52 -05:00
parent 5b9aeb0c34
commit 4a26f0cf71
Signed by: SinTan1729
GPG key ID: EB3E68BFBA25C85F
2 changed files with 28 additions and 10 deletions

View file

@ -118,9 +118,8 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
Matrix { entries: out } Matrix { entries: out }
} }
/// Returns the determinant of a square matrix. This method additionally requires [`Zero`], /// Returns the determinant of a square matrix.
/// [`One`] and [`Copy`] traits. Also, we need that the [`Mul`] and [`Add`] operations /// This uses basic recursive algorithm using cofactor-minor.
/// return the same type `T`. This uses basic recursive algorithm using cofactor-minor.
/// See [`det_in_field`](Self::det_in_field()) for faster determinant calculation in fields. /// See [`det_in_field`](Self::det_in_field()) for faster determinant calculation in fields.
/// It'll throw an error if the provided matrix isn't square. /// It'll throw an error if the provided matrix isn't square.
/// # Example /// # Example
@ -208,7 +207,7 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
} }
} }
/// Returns the row echelon form of a matrix over a field i.e. needs [`One`] and [`Div`] traits. /// Returns the row echelon form of a matrix over a field i.e. needs the [`Div`] trait.
/// # Example /// # Example
/// ``` /// ```
/// use matrix_basic::Matrix; /// use matrix_basic::Matrix;
@ -218,7 +217,6 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
/// ``` /// ```
pub fn row_echelon(&self) -> Self pub fn row_echelon(&self) -> Self
where where
T: One,
T: PartialEq, T: PartialEq,
T: Div<Output = T>, T: Div<Output = T>,
{ {
@ -256,19 +254,18 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
Matrix { entries: rows } Matrix { entries: rows }
} }
/// Returns the column echelon form of a matrix over a field i.e. needs [`One`] and [`Div`] traits. /// Returns the column echelon form of a matrix over a field i.e. needs the [`Div`] trait.
/// It's just the transpose of the row echelon form of the transpose. /// It's just the transpose of the row echelon form of the transpose.
/// See [`row_echelon`](Self::row_echelon()) and [`transpose`](Self::transpose()). /// See [`row_echelon`](Self::row_echelon()) and [`transpose`](Self::transpose()).
pub fn column_echelon(&self) -> Self pub fn column_echelon(&self) -> Self
where where
T: One,
T: PartialEq, T: PartialEq,
T: Div<Output = T>, T: Div<Output = T>,
{ {
self.transpose().row_echelon().transpose() self.transpose().row_echelon().transpose()
} }
/// Returns the reduced row echelon form of a matrix over a field i.e. needs [`One`] and [`Div`] traits. /// Returns the reduced row echelon form of a matrix over a field i.e. needs the `Div`] trait.
/// # Example /// # Example
/// ``` /// ```
/// use matrix_basic::Matrix; /// use matrix_basic::Matrix;
@ -278,7 +275,6 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
/// ``` /// ```
pub fn reduced_row_echelon(&self) -> Self pub fn reduced_row_echelon(&self) -> Self
where where
T: One,
T: PartialEq, T: PartialEq,
T: Div<Output = T>, T: Div<Output = T>,
{ {
@ -311,6 +307,7 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
} }
/// Creates an identity matrix of a given size. /// Creates an identity matrix of a given size.
/// It needs the [`One`] trait.
pub fn identity(size: usize) -> Self pub fn identity(size: usize) -> Self
where where
T: One, T: One,
@ -330,6 +327,26 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
Matrix { entries: out } Matrix { entries: out }
} }
/// Returns the trace of a square matrix.
/// It'll throw an error if the provided matrix isn't square.
/// # Example
/// ```
/// use matrix_basic::Matrix;
/// let m = Matrix::from(vec![vec![1,2],vec![3,4]]).unwrap();
/// assert_eq!(m.det(),Ok(-2));
/// ```
pub fn trace(self) -> Result<T, &'static str> {
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 {
Err("Provided matrix isn't square.")
}
}
// TODO: Canonical forms, eigenvalues, eigenvectors etc. // TODO: Canonical forms, eigenvalues, eigenvectors etc.
} }

View file

@ -24,7 +24,7 @@ fn add_sub_test() {
} }
#[test] #[test]
fn det_test() { fn det_trace_test() {
let a = Matrix::from(vec![vec![1, 2, 0], vec![0, 3, 5], vec![0, 0, 10]]).unwrap(); let a = Matrix::from(vec![vec![1, 2, 0], vec![0, 3, 5], vec![0, 0, 10]]).unwrap();
let b = Matrix::from(vec![vec![1, 2, 0], vec![0, 3, 5]]).unwrap(); let b = Matrix::from(vec![vec![1, 2, 0], vec![0, 3, 5]]).unwrap();
let c = Matrix::from(vec![ let c = Matrix::from(vec![
@ -37,6 +37,7 @@ fn det_test() {
assert_eq!(a.det(), Ok(30)); assert_eq!(a.det(), Ok(30));
assert_eq!(c.det_in_field(), Ok(-30.0)); assert_eq!(c.det_in_field(), Ok(-30.0));
assert!(b.det().is_err()); assert!(b.det().is_err());
assert_eq!(a.trace(), Ok(14));
} }
#[test] #[test]