From 85154a3be0112912d247dca7e006a340880fbdf5 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sun, 6 Jul 2025 18:58:38 +0100 Subject: [PATCH] Add shape method to Matrix and corresponding unit test --- src/matrix/mat.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index dbae21a..c7b0893 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -89,6 +89,10 @@ impl Matrix { self.cols } + pub fn shape(&self) -> (usize, usize) { + (self.rows, self.cols) + } + /// Get element reference (immutable). Panics on out-of-bounds. pub fn get(&self, r: usize, c: usize) -> &T { &self[(r, c)] @@ -1153,6 +1157,14 @@ mod tests { assert_eq!(ma.row(2), &[3, 6, 9]); } + #[test] + fn test_shape(){ + let ma = static_test_matrix_2x4(); + assert_eq!(ma.shape(), (2, 4)); + assert_eq!(ma.rows(), 2); + assert_eq!(ma.cols(), 4); + } + #[test] #[should_panic(expected = "row index 3 out of bounds for 3 rows")] fn test_row_out_of_bounds() {