diff --git a/src/lib.rs b/src/lib.rs index 8d53e57..2107544 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,9 +118,8 @@ impl + Add + Sub + Zero + Copy> Matri Matrix { entries: out } } - /// Returns the determinant of a square matrix. This method additionally requires [`Zero`], - /// [`One`] and [`Copy`] traits. Also, we need that the [`Mul`] and [`Add`] operations - /// return the same type `T`. This uses basic recursive algorithm using cofactor-minor. + /// Returns the determinant of a square matrix. + /// This uses basic recursive algorithm using cofactor-minor. /// 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. /// # Example @@ -208,7 +207,7 @@ impl + Add + Sub + 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 /// ``` /// use matrix_basic::Matrix; @@ -218,7 +217,6 @@ impl + Add + Sub + Zero + Copy> Matri /// ``` pub fn row_echelon(&self) -> Self where - T: One, T: PartialEq, T: Div, { @@ -256,19 +254,18 @@ impl + Add + Sub + Zero + Copy> Matri 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. /// See [`row_echelon`](Self::row_echelon()) and [`transpose`](Self::transpose()). pub fn column_echelon(&self) -> Self where - T: One, T: PartialEq, T: Div, { 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 /// ``` /// use matrix_basic::Matrix; @@ -278,7 +275,6 @@ impl + Add + Sub + Zero + Copy> Matri /// ``` pub fn reduced_row_echelon(&self) -> Self where - T: One, T: PartialEq, T: Div, { @@ -311,6 +307,7 @@ impl + Add + Sub + Zero + Copy> Matri } /// Creates an identity matrix of a given size. + /// It needs the [`One`] trait. pub fn identity(size: usize) -> Self where T: One, @@ -330,6 +327,26 @@ impl + Add + Sub + Zero + Copy> Matri 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 { + 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. } diff --git a/src/tests.rs b/src/tests.rs index 9afaf39..b82a454 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -24,7 +24,7 @@ fn add_sub_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 b = Matrix::from(vec![vec![1, 2, 0], vec![0, 3, 5]]).unwrap(); let c = Matrix::from(vec![ @@ -37,6 +37,7 @@ fn det_test() { assert_eq!(a.det(), Ok(30)); assert_eq!(c.det_in_field(), Ok(-30.0)); assert!(b.det().is_err()); + assert_eq!(a.trace(), Ok(14)); } #[test]