From caaf8c941199dbd5616dffa2986f519ae96298a8 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sun, 27 Apr 2025 01:37:42 +0100 Subject: [PATCH 01/23] Refactor test module imports --- src/frame/ops.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/frame/ops.rs b/src/frame/ops.rs index f52ee9d..c35625e 100644 --- a/src/frame/ops.rs +++ b/src/frame/ops.rs @@ -96,9 +96,10 @@ impl BoolOps for Frame { // } // } +#[cfg(test)] mod tests { - use crate::frame::*; - use crate::matrix::*; + use super::*; + use crate::matrix::Matrix; #[test] fn test_series_ops() { From 38c5c284544282c804803934f14d543d9b096aa9 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Thu, 1 May 2025 23:10:51 +0100 Subject: [PATCH 02/23] Enhance matrix assertions for row and column operations to allow 0-row matrices with empty columns and improve error messages for index out of bounds. --- src/matrix/mat.rs | 62 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 02a042b..07a3d3d 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -14,7 +14,11 @@ impl Matrix { let cols = cols_data.len(); assert!(cols > 0, "need at least one column"); let rows = cols_data[0].len(); - assert!(rows > 0, "need at least one row"); + // Allow 0-row matrices if columns are empty, but not 0-col matrices if rows > 0 + assert!( + rows > 0 || cols == 0, + "need at least one row if columns exist" + ); for (i, col) in cols_data.iter().enumerate().skip(1) { assert!( col.len() == rows, @@ -32,8 +36,14 @@ impl Matrix { } pub fn from_vec(data: Vec, rows: usize, cols: usize) -> Self { - assert!(rows > 0, "need at least one row"); - assert!(cols > 0, "need at least one column"); + assert!( + rows > 0 || cols == 0, + "need at least one row if columns exist" + ); + assert!( + cols > 0 || rows == 0, + "need at least one column if rows exist" + ); assert_eq!(data.len(), rows * cols, "data length mismatch"); Matrix { rows, cols, data } } @@ -67,11 +77,23 @@ impl Matrix { #[inline] pub fn column(&self, c: usize) -> &[T] { + assert!( + c < self.cols, + "column index {} out of bounds for {} columns", + c, + self.cols + ); let start = c * self.rows; &self.data[start..start + self.rows] } #[inline] pub fn column_mut(&mut self, c: usize) -> &mut [T] { + assert!( + c < self.cols, + "column index {} out of bounds for {} columns", + c, + self.cols + ); let start = c * self.rows; &mut self.data[start..start + self.rows] } @@ -90,8 +112,16 @@ impl Matrix { /// Swaps two columns in the matrix. pub fn swap_columns(&mut self, c1: usize, c2: usize) { assert!( - c1 < self.cols && c2 < self.cols, - "column index out of bounds" + c1 < self.cols, + "column index c1={} out of bounds for {} columns", + c1, + self.cols + ); + assert!( + c2 < self.cols, + "column index c2={} out of bounds for {} columns", + c2, + self.cols ); if c1 == c2 { // Indices are equal; no operation required @@ -132,7 +162,7 @@ impl Matrix { impl Matrix { /// Adds a column to the matrix at the specified index. pub fn add_column(&mut self, index: usize, column: Vec) { - assert!(index <= self.cols, "column index out of bounds"); + assert!(index <= self.cols, "add_column index {} out of bounds for {} columns", index, self.cols); assert_eq!(column.len(), self.rows, "column length mismatch"); for (r, value) in column.into_iter().enumerate() { @@ -143,7 +173,7 @@ impl Matrix { /// Adds a row to the matrix at the specified index. pub fn add_row(&mut self, index: usize, row: Vec) { - assert!(index <= self.rows, "row index out of bounds"); + assert!(index <= self.rows, "add_row index {} out of bounds for {} rows", index, self.rows); assert_eq!(row.len(), self.cols, "row length mismatch"); for (c, value) in row.into_iter().enumerate() { @@ -159,7 +189,14 @@ impl Index<(usize, usize)> for Matrix { #[inline] fn index(&self, (r, c): (usize, usize)) -> &T { // Validate that the requested indices are within bounds - assert!(r < self.rows && c < self.cols, "index out of bounds"); + assert!( + r < self.rows && c < self.cols, + "index out of bounds: ({}, {}) vs {}x{}", + r, + c, + self.rows, + self.cols + ); // Compute column-major offset and return reference &self.data[c * self.rows + r] } @@ -169,7 +206,14 @@ impl IndexMut<(usize, usize)> for Matrix { #[inline] fn index_mut(&mut self, (r, c): (usize, usize)) -> &mut T { // Validate that the requested indices are within bounds - assert!(r < self.rows && c < self.cols, "index out of bounds"); + assert!( + r < self.rows && c < self.cols, + "index out of bounds: ({}, {}) vs {}x{}", + r, + c, + self.rows, + self.cols + ); // Compute column-major offset and return mutable reference &mut self.data[c * self.rows + r] } From 7cf41171a8b4a431f2f4f578729ec059c35e5140 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Thu, 1 May 2025 23:12:19 +0100 Subject: [PATCH 03/23] Allow creation of 0-row matrices from empty column data and improve data flattening in from_cols method. --- src/matrix/mat.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 07a3d3d..1523bf7 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -13,13 +13,15 @@ impl Matrix { pub fn from_cols(cols_data: Vec>) -> Self { let cols = cols_data.len(); assert!(cols > 0, "need at least one column"); - let rows = cols_data[0].len(); - // Allow 0-row matrices if columns are empty, but not 0-col matrices if rows > 0 + // Handle empty cols_data + let rows = cols_data.get(0).map_or(0, |c| c.len()); + // Allow 0-row matrices if columns are empty, but not 0-col matrices if rows > 0 assert!( rows > 0 || cols == 0, "need at least one row if columns exist" ); - for (i, col) in cols_data.iter().enumerate().skip(1) { + + for (i, col) in cols_data.iter().enumerate() { assert!( col.len() == rows, "col {} has len {}, expected {}", @@ -28,10 +30,8 @@ impl Matrix { rows ); } - let mut data = Vec::with_capacity(rows * cols); - for col in cols_data { - data.extend(col); - } + // Flatten column data directly + let data = cols_data.into_iter().flatten().collect(); Matrix { rows, cols, data } } From a0a551c7d90e05e096c1c5d1d57e44a85f31969f Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Thu, 1 May 2025 23:13:57 +0100 Subject: [PATCH 04/23] update into_vec, from_vec --- src/matrix/mat.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 1523bf7..194bb4e 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -35,6 +35,7 @@ impl Matrix { Matrix { rows, cols, data } } + /// Build from a flat Vec, assuming column-major order. pub fn from_vec(data: Vec, rows: usize, cols: usize) -> Self { assert!( rows > 0 || cols == 0, @@ -44,7 +45,19 @@ impl Matrix { cols > 0 || rows == 0, "need at least one column if rows exist" ); - assert_eq!(data.len(), rows * cols, "data length mismatch"); + if rows * cols != 0 { + // Only assert length if matrix is non-empty + assert_eq!( + data.len(), + rows * cols, + "data length mismatch: expected {}, got {}", + rows * cols, + data.len() + ); + } else { + assert!(data.is_empty(), "data must be empty for 0-sized matrix"); + } + Matrix { rows, cols, data } } @@ -56,7 +69,13 @@ impl Matrix { &mut self.data } - pub fn as_vec(&self) -> Vec { + /// Consumes the Matrix and returns its underlying data Vec. + pub fn into_vec(self) -> Vec { + self.data + } + + /// Creates a new Vec containing the matrix data (cloned). + pub fn to_vec(&self) -> Vec { self.data.clone() } From ba1e2b3d4392db4267031aeeef4b486e835f789e Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Thu, 1 May 2025 23:14:37 +0100 Subject: [PATCH 05/23] update imports and module docstring --- src/matrix/mat.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 194bb4e..a951c05 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -1,4 +1,6 @@ -use std::ops::{Index, IndexMut, Not}; +//! A simple column-major Matrix implementation with element-wise operations. + +use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Index, IndexMut, Mul, Not, Sub}; /// A column‑major 2D matrix of `T` #[derive(Debug, Clone, PartialEq, Eq)] From f91ddc5c4109825e8d5d1621f5c80e2a8b735051 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Thu, 1 May 2025 23:56:06 +0100 Subject: [PATCH 06/23] Improve assertions and documentation for matrix operations; allow 0-row matrices from empty columns. --- src/matrix/mat.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index a951c05..82acb38 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -16,7 +16,7 @@ impl Matrix { let cols = cols_data.len(); assert!(cols > 0, "need at least one column"); // Handle empty cols_data - let rows = cols_data.get(0).map_or(0, |c| c.len()); + let rows = cols_data.get(0).map_or(0, |c| c.len()); // Allow 0-row matrices if columns are empty, but not 0-col matrices if rows > 0 assert!( rows > 0 || cols == 0, @@ -89,9 +89,12 @@ impl Matrix { self.cols } + /// Get element reference (immutable). Panics on out-of-bounds. pub fn get(&self, r: usize, c: usize) -> &T { &self[(r, c)] } + + /// Get element reference (mutable). Panics on out-of-bounds. pub fn get_mut(&mut self, r: usize, c: usize) -> &mut T { &mut self[(r, c)] } @@ -107,6 +110,7 @@ impl Matrix { let start = c * self.rows; &self.data[start..start + self.rows] } + #[inline] pub fn column_mut(&mut self, c: usize) -> &mut [T] { assert!( @@ -130,7 +134,7 @@ impl Matrix { }) } - /// Swaps two columns in the matrix. + /// Swaps two columns in the matrix. Panics on out-of-bounds. pub fn swap_columns(&mut self, c1: usize, c2: usize) { assert!( c1 < self.cols, @@ -183,7 +187,7 @@ impl Matrix { impl Matrix { /// Adds a column to the matrix at the specified index. pub fn add_column(&mut self, index: usize, column: Vec) { - assert!(index <= self.cols, "add_column index {} out of bounds for {} columns", index, self.cols); + assert!(index <= self.cols,"add_column index {} out of bounds for {} columns",index,self.cols); assert_eq!(column.len(), self.rows, "column length mismatch"); for (r, value) in column.into_iter().enumerate() { @@ -194,8 +198,8 @@ impl Matrix { /// Adds a row to the matrix at the specified index. pub fn add_row(&mut self, index: usize, row: Vec) { - assert!(index <= self.rows, "add_row index {} out of bounds for {} rows", index, self.rows); - assert_eq!(row.len(), self.cols, "row length mismatch"); + assert!(index <= self.rows,"add_row index {} out of bounds for {} rows",index,self.rows); + assert_eq!(row.len(),self.cols,"row length mismatch: expected {} (cols), got {}",self.cols,row.len()); for (c, value) in row.into_iter().enumerate() { self.data.insert(c * (self.rows + 1) + index, value); @@ -261,14 +265,16 @@ impl<'a, T> MatrixRow<'a, T> { /// Specifies the axis along which to perform a reduction operation. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Axis { - /// Apply reduction along columns (vertical axis). + /// Apply reduction along columns (vertical axis). Result has 1 row. Col, - /// Apply reduction along rows (horizontal axis). + /// Apply reduction along rows (horizontal axis). Result has 1 column. Row, } +// --- Broadcasting --- + /// A trait to turn either a `Matrix` or a scalar T into a `Vec` of -/// length `rows*cols` (broadcasting the scalar). +/// length `rows*cols` (broadcasting the scalar). Used for comparisons. pub trait Broadcastable { fn to_vec(&self, rows: usize, cols: usize) -> Vec; } From 20727a2b9124f4949bf0a83456036278d5ae4db5 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 00:25:45 +0100 Subject: [PATCH 07/23] Improve column swapping logic to handle empty matrices and prevent overlapping swaps --- src/matrix/mat.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 82acb38..2b1c564 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -149,19 +149,24 @@ impl Matrix { self.cols ); if c1 == c2 { - // Indices are equal; no operation required return; } - // Iterate over each row to swap corresponding elements - for r in 0..self.rows { - // Compute the one-dimensional index for (row r, column c1) - let idx1 = c1 * self.rows + r; - // Compute the one-dimensional index for (row r, column c2) - let idx2 = c2 * self.rows + r; + if self.rows == 0 { + self.data.swap(c1, c2); + return; + } - // Exchange the two elements in the internal data buffer - self.data.swap(idx1, idx2); + let (start1, end1) = (c1 * self.rows, (c1 + 1) * self.rows); + let (start2, end2) = (c2 * self.rows, (c2 + 1) * self.rows); + + if (start1 < start2 && end1 > start2) || (start2 < start1 && end2 > start1) { + panic!("Cannot swap overlapping columns"); + } + + // element-wise swap + for r in 0..self.rows { + self.data.swap(start1 + r, start2 + r); } } From 8574d86abc66b4c8cce0da398362eafd5e032525 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 00:26:21 +0100 Subject: [PATCH 08/23] Enhance delete_column method to improve efficiency of column removal --- src/matrix/mat.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 2b1c564..2f4ecba 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -170,12 +170,12 @@ impl Matrix { } } - /// Deletes a column from the matrix. + /// Deletes a column from the matrix. Panics on out-of-bounds. + /// This is O(N) where N is the number of elements. pub fn delete_column(&mut self, col: usize) { - assert!(col < self.cols, "column index out of bounds"); - for r in (0..self.rows).rev() { - self.data.remove(col * self.rows + r); - } + assert!(col < self.cols, "column index {} out of bounds for {} columns", col, self.cols); + let start = col * self.rows; + self.data.drain(start..start + self.rows); // Efficient removal self.cols -= 1; } From 9fd3582061753c868d7cf5f1dfcbdac4aec4dd42 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 00:26:43 +0100 Subject: [PATCH 09/23] Refactor delete_row method to improve bounds checking and optimize data rebuilding --- src/matrix/mat.rs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 2f4ecba..14e5937 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -179,13 +179,34 @@ impl Matrix { self.cols -= 1; } - /// Deletes a row from the matrix. + /// Deletes a row from the matrix. Panics on out-of-bounds. + /// This is O(N) where N is the number of elements, as it rebuilds the data vec. pub fn delete_row(&mut self, row: usize) { - assert!(row < self.rows, "row index out of bounds"); - for c in (0..self.cols).rev() { - self.data.remove(c * self.rows + row); + assert!( + row < self.rows, + "row index {} out of bounds for {} rows", + row, + self.rows + ); + if self.rows == 0 { + return; + } // Nothing to delete + + let old_rows = self.rows; + let new_rows = self.rows - 1; + let mut new_data = Vec::with_capacity(new_rows * self.cols); + + for c in 0..self.cols { + let col_start_old = c * old_rows; + for r in 0..old_rows { + if r != row { + // Must clone as we are reading from the old data while building the new one + new_data.push(self.data[col_start_old + r].clone()); + } + } } - self.rows -= 1; + self.data = new_data; + self.rows = new_rows; } } From 4c020061531a4d4a3c6c3c6d421091acf997113b Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 00:29:14 +0100 Subject: [PATCH 10/23] Optimize column swapping logic to handle empty matrices and prevent unnecessary swaps --- src/matrix/mat.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 14e5937..4ef1c4f 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -148,12 +148,7 @@ impl Matrix { c2, self.cols ); - if c1 == c2 { - return; - } - - if self.rows == 0 { - self.data.swap(c1, c2); + if c1 == c2 || self.rows == 0 || self.cols == 0 { return; } From bdd0a2309696228b17b219f3284c586480940fe8 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 00:31:11 +0100 Subject: [PATCH 11/23] Enhance add_column and add_row methods to handle empty matrices and improve error handling --- src/matrix/mat.rs | 75 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 13 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 4ef1c4f..2330da7 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -206,26 +206,75 @@ impl Matrix { } impl Matrix { - /// Adds a column to the matrix at the specified index. + /// Adds a column to the matrix at the specified index. Panics if index > cols or length mismatch. + /// This is O(N) where N is the number of elements. pub fn add_column(&mut self, index: usize, column: Vec) { - assert!(index <= self.cols,"add_column index {} out of bounds for {} columns",index,self.cols); - assert_eq!(column.len(), self.rows, "column length mismatch"); - - for (r, value) in column.into_iter().enumerate() { - self.data.insert(index * self.rows + r, value); + assert!( + index <= self.cols, + "add_column index {} out of bounds for {} columns", + index, + self.cols + ); + assert_eq!( + column.len(), + self.rows, + "column length mismatch: expected {}, got {}", + self.rows, + column.len() + ); + if self.rows == 0 && self.cols == 0 { + // Special case: adding first column to empty matrix + assert!(index == 0, "index must be 0 for adding first column"); + self.data = column; + self.cols = 1; + // self.rows should be correctly set by column.len() assertion + } else { + let insert_pos = index * self.rows; + self.data.splice(insert_pos..insert_pos, column); // Efficient insertion + self.cols += 1; } - self.cols += 1; } - /// Adds a row to the matrix at the specified index. + /// Adds a row to the matrix at the specified index. Panics if index > rows or length mismatch. + /// This is O(N) where N is the number of elements, as it rebuilds the data vec. pub fn add_row(&mut self, index: usize, row: Vec) { - assert!(index <= self.rows,"add_row index {} out of bounds for {} rows",index,self.rows); - assert_eq!(row.len(),self.cols,"row length mismatch: expected {} (cols), got {}",self.cols,row.len()); + assert!(index <= self.rows, "add_row index {} out of bounds for {} rows", index, self.rows); + assert_eq!(row.len(), self.cols, "row length mismatch: expected {} (cols), got {}", self.cols, row.len()); - for (c, value) in row.into_iter().enumerate() { - self.data.insert(c * (self.rows + 1) + index, value); + if self.cols == 0 && self.rows == 0 { + // Special case: adding first row to empty matrix + assert!(index == 0, "index must be 0 for adding first row"); + // Cannot add a row if there are no columns yet. Maybe panic or change API? + assert!( + self.cols > 0 || row.is_empty(), + "cannot add non-empty row to matrix with 0 columns" + ); + if row.is_empty() { + return; + } // Adding empty row to empty matrix is no-op } - self.rows += 1; + + let old_rows = self.rows; + let new_rows = self.rows + 1; + let mut new_data = Vec::with_capacity(new_rows * self.cols); + let mut row_iter = row.into_iter(); // Consume the input row vec + + for c in 0..self.cols { + let old_col_start = c * old_rows; + for r in 0..new_rows { + if r == index { + // Take the next element from the provided row vector + new_data.push(row_iter.next().expect("Row iterator exhausted prematurely - should have been caught by length assert")); + } else { + // Calculate the corresponding old row index + let old_r = if r < index { r } else { r - 1 }; + // Must clone as we are reading from the old data while building the new one + new_data.push(self.data[old_col_start + old_r].clone()); + } + } + } + self.data = new_data; + self.rows = new_rows; } } From ecb1939ec25fb132b22557fee5781564033ad3e3 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 00:32:46 +0100 Subject: [PATCH 12/23] Enhance error messages in Broadcastable trait to clarify row and column count mismatches --- src/matrix/mat.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 2330da7..3bd9ed9 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -357,9 +357,9 @@ impl Broadcastable for T { impl Broadcastable for Matrix { fn to_vec(&self, rows: usize, cols: usize) -> Vec { - assert_eq!(self.rows, rows, "row count mismatch"); - assert_eq!(self.cols, cols, "col count mismatch"); - self.data.clone() + assert_eq!(self.rows, rows, "row count mismatch in broadcast"); + assert_eq!(self.cols, cols, "col count mismatch in broadcast"); + self.data.clone() // Clone the data for the broadcasted vec } } From 10c6116f8f07ab931d58896847dfad1fbe3ecafe Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 00:35:34 +0100 Subject: [PATCH 13/23] Enhance element-wise comparison methods to optimize data handling --- src/matrix/mat.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 3bd9ed9..be4c163 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -372,24 +372,25 @@ macro_rules! impl_elementwise_cmp { impl Matrix { $( #[doc = concat!("Element-wise comparison `self ", stringify!($op), " rhs`,\n\ - where `rhs` may be a `Matrix` or a scalar T.")] + where `rhs` may be a `Matrix` or a scalar T.\n\ + Returns a `BoolMatrix`.")] pub fn $method(&self, rhs: Rhs) -> BoolMatrix where Rhs: Broadcastable, { - // Prepare broadcasted rhs-data + // Prepare broadcasted rhs-data using the trait let rhs_data = rhs.to_vec(self.rows, self.cols); - // Pairwise compare + // Pairwise compare using iterators let data = self .data - .iter() - .cloned() - .zip(rhs_data.into_iter()) - .map(|(a, b)| a $op b) + .iter() // Borrow self's data + .zip(rhs_data.iter()) // Borrow rhs's broadcasted data + .map(|(a, b)| a $op b) // Perform comparison op .collect(); - BoolMatrix::from_vec(data, self.rows, self.cols) + // Create BoolMatrix from result + Matrix::::from_vec(data, self.rows, self.cols) } )* } From a30a7101e86a1d4f06e2a65b3f3e4bf3af13822f Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 00:36:03 +0100 Subject: [PATCH 14/23] Refactor element-wise comparison macro to improve naming consistency --- src/matrix/mat.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index be4c163..110d2d6 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -399,11 +399,12 @@ macro_rules! impl_elementwise_cmp { // Instantiate element-wise comparison implementations for matrices. impl_elementwise_cmp! { - eq_elementwise => ==, - lt_elementwise => <, - le_elementwise => <=, - gt_elementwise => >, - ge_elementwise => >=, + eq_elem => ==, + ne_elem => !=, + lt_elem => <, + le_elem => <=, + gt_elem => >, + ge_elem => >=, } /// Generates element-wise arithmetic implementations for matrices. From 7f587a7b7e2b4bccf3c027fc1043d450ff57bcb2 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 00:37:06 +0100 Subject: [PATCH 15/23] Enhance element-wise arithmetic operations for Matrix to support multiple ownership variants and improve in-place modifications --- src/matrix/mat.rs | 82 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 24 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 110d2d6..4fe80fe 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -407,39 +407,73 @@ impl_elementwise_cmp! { ge_elem => >=, } -/// Generates element-wise arithmetic implementations for matrices. -macro_rules! impl_elementwise_op { +/// Generates element-wise arithmetic implementations for Matrix + Matrix +macro_rules! impl_elementwise_op_matrix_all { ($OpTrait:ident, $method:ident, $op:tt) => { - impl<'a, 'b, T> std::ops::$OpTrait<&'b Matrix> for &'a Matrix - where - T: Clone + std::ops::$OpTrait, - { + // &Matrix + &Matrix + impl<'a, 'b, T> $OpTrait<&'b Matrix> for &'a Matrix + where T: Clone + $OpTrait { type Output = Matrix; - fn $method(self, rhs: &'b Matrix) -> Matrix { - // Ensure both matrices have identical dimensions - assert_eq!(self.rows, rhs.rows, "row count mismatch"); - assert_eq!(self.cols, rhs.cols, "col count mismatch"); - // Apply the operation element-wise and collect into a new matrix - let data = self - .data - .iter() - .cloned() - .zip(rhs.data.iter().cloned()) - .map(|(a, b)| a $op b) - .collect(); + assert_eq!(self.rows, rhs.rows, "row count mismatch"); assert_eq!(self.cols, rhs.cols, "col count mismatch"); + let data = self.data.iter().cloned().zip(rhs.data.iter().cloned()).map(|(a, b)| a $op b).collect(); Matrix { rows: self.rows, cols: self.cols, data } } } + // Matrix + &Matrix (Consumes self) + impl<'b, T> $OpTrait<&'b Matrix> for Matrix + where T: Clone + $OpTrait { + type Output = Matrix; + fn $method(mut self, rhs: &'b Matrix) -> Matrix { // Make self mutable for potential in-place modification + assert_eq!(self.rows, rhs.rows, "row count mismatch"); assert_eq!(self.cols, rhs.cols, "col count mismatch"); + // Modify data in place + for (a, b) in self.data.iter_mut().zip(rhs.data.iter().cloned()) { + *a = a.clone() $op b; // Requires T: Clone for the *a = part + } + // Return modified self (its data vec was consumed conceptually) + self + // Alternative: Collect into new Vec if T is not Clone or in-place is complex + // let data = self.data.into_iter().zip(rhs.data.iter().cloned()).map(|(a, b)| a $op b).collect(); + // Matrix { rows: self.rows, cols: self.cols, data } + } + } + // &Matrix + Matrix (Consumes rhs) + impl<'a, T> $OpTrait> for &'a Matrix + where T: Clone + $OpTrait { + type Output = Matrix; + fn $method(self, mut rhs: Matrix) -> Matrix { // Make rhs mutable + assert_eq!(self.rows, rhs.rows, "row count mismatch"); assert_eq!(self.cols, rhs.cols, "col count mismatch"); + // Modify rhs data in place + for (a, b) in self.data.iter().cloned().zip(rhs.data.iter_mut()) { + *b = a $op b.clone(); // Requires T: Clone for the *b = part + } + // Return modified rhs + rhs + // Alternative: Collect into new Vec + // let data = self.data.iter().cloned().zip(rhs.data.into_iter()).map(|(a, b)| a $op b).collect(); + // Matrix { rows: self.rows, cols: self.cols, data } + } + } + // Matrix + Matrix (Consumes both) + impl $OpTrait> for Matrix + where T: Clone + $OpTrait { + type Output = Matrix; + fn $method(mut self, rhs: Matrix) -> Matrix { // Make self mutable + assert_eq!(self.rows, rhs.rows, "row count mismatch"); assert_eq!(self.cols, rhs.cols, "col count mismatch"); + // Modify self data in place + for (a, b) in self.data.iter_mut().zip(rhs.data.into_iter()) { + *a = a.clone() $op b; // Requires T: Clone + } + // Return modified self + self + // Alternative: Collect into new Vec + // let data = self.data.into_iter().zip(rhs.data.into_iter()).map(|(a, b)| a $op b).collect(); + // Matrix { rows: self.rows, cols: self.cols, data } + } + } }; } -// Instantiate element-wise addition, subtraction, multiplication, and division -impl_elementwise_op!(Add, add, +); -impl_elementwise_op!(Sub, sub, -); -impl_elementwise_op!(Mul, mul, *); -impl_elementwise_op!(Div, div, /); - /// Generates element-wise arithmetic implementations for matrices with scalars. macro_rules! impl_elementwise_op_scalar { ($OpTrait:ident, $method:ident, $op:tt) => { From 36a0846efa5adb5016c64f17e755673a9db26f2c Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 00:38:07 +0100 Subject: [PATCH 16/23] Enhance element-wise arithmetic operations for Matrix + Scalar to support in-place modifications and improve performance --- src/matrix/mat.rs | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 4fe80fe..3cb0484 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -474,29 +474,50 @@ macro_rules! impl_elementwise_op_matrix_all { }; } -/// Generates element-wise arithmetic implementations for matrices with scalars. -macro_rules! impl_elementwise_op_scalar { +/// Generates element-wise arithmetic implementations for Matrix + Scalar +macro_rules! impl_elementwise_op_scalar_all { ($OpTrait:ident, $method:ident, $op:tt) => { - impl<'a, T> std::ops::$OpTrait for &'a Matrix - where - T: Clone + std::ops::$OpTrait, - { + // &Matrix + Scalar + impl<'a, T> $OpTrait for &'a Matrix + where T: Clone + $OpTrait { type Output = Matrix; - fn $method(self, rhs: T) -> Matrix { - // Apply the operation element-wise and collect into a new matrix let data = self.data.iter().cloned().map(|a| a $op rhs.clone()).collect(); Matrix { rows: self.rows, cols: self.cols, data } } } + // Matrix + Scalar (Consumes self) + impl $OpTrait for Matrix + where T: Clone + $OpTrait { + type Output = Matrix; + fn $method(mut self, rhs: T) -> Matrix { // Make self mutable + // Modify self data in place + for a in self.data.iter_mut() { + *a = a.clone() $op rhs.clone(); // Requires T: Clone + } + // Return modified self + self + // Alternative: Collect into new Vec + // let data = self.data.into_iter().map(|a| a $op rhs.clone()).collect(); + // Matrix { rows: self.rows, cols: self.cols, data } + } + } + // NOTE: Scalar + Matrix (e.g., 1.0 + matrix) is NOT implemented here. + // It would require `impl Add> for T`, which is discouraged + // for primitive types unless inside the crate defining T. }; } -// Instantiate element-wise addition, subtraction, multiplication, and division -impl_elementwise_op_scalar!(Add, add, +); -impl_elementwise_op_scalar!(Sub, sub, -); -impl_elementwise_op_scalar!(Mul, mul, *); -impl_elementwise_op_scalar!(Div, div, /); +// Instantiate ALL combinations for arithmetic ops using the new macros +impl_elementwise_op_matrix_all!(Add, add, +); +impl_elementwise_op_matrix_all!(Sub, sub, -); +impl_elementwise_op_matrix_all!(Mul, mul, *); // Element-wise multiplication +impl_elementwise_op_matrix_all!(Div, div, /); // Element-wise division + +impl_elementwise_op_scalar_all!(Add, add, +); +impl_elementwise_op_scalar_all!(Sub, sub, -); +impl_elementwise_op_scalar_all!(Mul, mul, *); +impl_elementwise_op_scalar_all!(Div, div, /); /// Generates element-wise bitwise operations for boolean matrices. macro_rules! impl_bitwise_op { From ca734fbedfd5278a399254df95e8d31c4831b76b Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 00:39:08 +0100 Subject: [PATCH 17/23] Enhance bitwise operations for boolean matrices and implement logical NOT for both owned and borrowed matrices --- src/matrix/mat.rs | 72 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 20 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 3cb0484..f9db3d9 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -520,40 +520,72 @@ impl_elementwise_op_scalar_all!(Mul, mul, *); impl_elementwise_op_scalar_all!(Div, div, /); /// Generates element-wise bitwise operations for boolean matrices. -macro_rules! impl_bitwise_op { +macro_rules! impl_bitwise_op_all { ($OpTrait:ident, $method:ident, $op:tt) => { - impl<'a, 'b> std::ops::$OpTrait<&'b Matrix> for &'a Matrix { + // &Matrix OP &Matrix + impl<'a, 'b> $OpTrait<&'b Matrix> for &'a Matrix { type Output = Matrix; - fn $method(self, rhs: &'b Matrix) -> Matrix { - // Ensure both matrices have identical dimensions - assert_eq!(self.rows, rhs.rows, "row count mismatch"); - assert_eq!(self.cols, rhs.cols, "col count mismatch"); - // Apply the bitwise operation element-wise - let data = self - .data - .iter() - .cloned() - .zip(rhs.data.iter().cloned()) - .map(|(a, b)| a $op b) - .collect(); + assert_eq!(self.rows, rhs.rows); assert_eq!(self.cols, rhs.cols); + let data = self.data.iter().cloned().zip(rhs.data.iter().cloned()).map(|(a, b)| a $op b).collect(); Matrix { rows: self.rows, cols: self.cols, data } } } + // Matrix OP &Matrix + impl<'b> $OpTrait<&'b Matrix> for Matrix { + type Output = Matrix; + fn $method(mut self, rhs: &'b Matrix) -> Matrix { + assert_eq!(self.rows, rhs.rows); assert_eq!(self.cols, rhs.cols); + for (a, b) in self.data.iter_mut().zip(rhs.data.iter()) { *a = *a $op *b; } // bool is Copy + self + } + } + // &Matrix OP Matrix + impl<'a> $OpTrait> for &'a Matrix { + type Output = Matrix; + fn $method(self, mut rhs: Matrix) -> Matrix { + assert_eq!(self.rows, rhs.rows); assert_eq!(self.cols, rhs.cols); + for (a, b) in self.data.iter().zip(rhs.data.iter_mut()) { *b = *a $op *b; } // bool is Copy + rhs + } + } + // Matrix OP Matrix + impl $OpTrait> for Matrix { + type Output = Matrix; + fn $method(mut self, rhs: Matrix) -> Matrix { + assert_eq!(self.rows, rhs.rows); assert_eq!(self.cols, rhs.cols); + for (a, b) in self.data.iter_mut().zip(rhs.data.iter()) { *a = *a $op *b; } // bool is Copy + self + } + } }; } -// Instantiate bitwise AND, OR, and XOR for boolean matrices -impl_bitwise_op!(BitAnd, bitand, &); -impl_bitwise_op!(BitOr, bitor, |); -impl_bitwise_op!(BitXor, bitxor, ^); +// Instantiate ALL combinations for bitwise ops +impl_bitwise_op_all!(BitAnd, bitand, &); +impl_bitwise_op_all!(BitOr, bitor, |); +impl_bitwise_op_all!(BitXor, bitxor, ^); +// --- Logical Not --- + +// `!Matrix` (consumes matrix) impl Not for Matrix { type Output = Matrix; + fn not(mut self) -> Matrix { + // Take by value, make mutable + for val in self.data.iter_mut() { + *val = !*val; // Invert in place + } + self // Return the modified matrix + } +} +// `!&Matrix` (borrows matrix, returns new matrix) +impl Not for &Matrix { + type Output = Matrix; fn not(self) -> Matrix { - // Invert each boolean element in the matrix - let data = self.data.iter().map(|&v| !v).collect(); + // Take by reference + let data = self.data.iter().map(|&v| !v).collect(); // Create new data vec Matrix { rows: self.rows, cols: self.cols, From 3cb68be0621bc2b50d4b9dc8c9d64a72b50c9b85 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 01:11:59 +0100 Subject: [PATCH 18/23] Refactor element-wise arithmetic operations to use a centralized dimension check for improved error handling --- src/matrix/mat.rs | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index f9db3d9..d1be2d5 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -407,6 +407,23 @@ impl_elementwise_cmp! { ge_elem => >=, } +// --- Element-wise Arithmetic Operations (Macros generating all ownership variants) --- + +fn check_matrix_dims_for_ops(lhs: &Matrix, rhs: &Matrix) { + assert!( + lhs.rows == rhs.rows, + "Row count mismatch: left has {} rows, right has {} rows", + lhs.rows, + rhs.rows + ); + assert!( + lhs.cols == rhs.cols, + "Column count mismatch: left has {} columns, right has {} columns", + lhs.cols, + rhs.cols + ); +} + /// Generates element-wise arithmetic implementations for Matrix + Matrix macro_rules! impl_elementwise_op_matrix_all { ($OpTrait:ident, $method:ident, $op:tt) => { @@ -415,7 +432,7 @@ macro_rules! impl_elementwise_op_matrix_all { where T: Clone + $OpTrait { type Output = Matrix; fn $method(self, rhs: &'b Matrix) -> Matrix { - assert_eq!(self.rows, rhs.rows, "row count mismatch"); assert_eq!(self.cols, rhs.cols, "col count mismatch"); + check_matrix_dims_for_ops(self, rhs); let data = self.data.iter().cloned().zip(rhs.data.iter().cloned()).map(|(a, b)| a $op b).collect(); Matrix { rows: self.rows, cols: self.cols, data } } @@ -425,7 +442,7 @@ macro_rules! impl_elementwise_op_matrix_all { where T: Clone + $OpTrait { type Output = Matrix; fn $method(mut self, rhs: &'b Matrix) -> Matrix { // Make self mutable for potential in-place modification - assert_eq!(self.rows, rhs.rows, "row count mismatch"); assert_eq!(self.cols, rhs.cols, "col count mismatch"); + check_matrix_dims_for_ops(&self, rhs); // Modify data in place for (a, b) in self.data.iter_mut().zip(rhs.data.iter().cloned()) { *a = a.clone() $op b; // Requires T: Clone for the *a = part @@ -442,7 +459,7 @@ macro_rules! impl_elementwise_op_matrix_all { where T: Clone + $OpTrait { type Output = Matrix; fn $method(self, mut rhs: Matrix) -> Matrix { // Make rhs mutable - assert_eq!(self.rows, rhs.rows, "row count mismatch"); assert_eq!(self.cols, rhs.cols, "col count mismatch"); + check_matrix_dims_for_ops(self, &rhs); // Modify rhs data in place for (a, b) in self.data.iter().cloned().zip(rhs.data.iter_mut()) { *b = a $op b.clone(); // Requires T: Clone for the *b = part @@ -459,7 +476,7 @@ macro_rules! impl_elementwise_op_matrix_all { where T: Clone + $OpTrait { type Output = Matrix; fn $method(mut self, rhs: Matrix) -> Matrix { // Make self mutable - assert_eq!(self.rows, rhs.rows, "row count mismatch"); assert_eq!(self.cols, rhs.cols, "col count mismatch"); + check_matrix_dims_for_ops(&self, &rhs); // Modify self data in place for (a, b) in self.data.iter_mut().zip(rhs.data.into_iter()) { *a = a.clone() $op b; // Requires T: Clone @@ -519,14 +536,15 @@ impl_elementwise_op_scalar_all!(Sub, sub, -); impl_elementwise_op_scalar_all!(Mul, mul, *); impl_elementwise_op_scalar_all!(Div, div, /); -/// Generates element-wise bitwise operations for boolean matrices. +// --- Element-wise Bitwise Operations (BoolMatrix) --- + macro_rules! impl_bitwise_op_all { ($OpTrait:ident, $method:ident, $op:tt) => { // &Matrix OP &Matrix impl<'a, 'b> $OpTrait<&'b Matrix> for &'a Matrix { type Output = Matrix; fn $method(self, rhs: &'b Matrix) -> Matrix { - assert_eq!(self.rows, rhs.rows); assert_eq!(self.cols, rhs.cols); + check_matrix_dims_for_ops(self, rhs); let data = self.data.iter().cloned().zip(rhs.data.iter().cloned()).map(|(a, b)| a $op b).collect(); Matrix { rows: self.rows, cols: self.cols, data } } @@ -535,17 +553,17 @@ macro_rules! impl_bitwise_op_all { impl<'b> $OpTrait<&'b Matrix> for Matrix { type Output = Matrix; fn $method(mut self, rhs: &'b Matrix) -> Matrix { - assert_eq!(self.rows, rhs.rows); assert_eq!(self.cols, rhs.cols); + check_matrix_dims_for_ops(&self, rhs); for (a, b) in self.data.iter_mut().zip(rhs.data.iter()) { *a = *a $op *b; } // bool is Copy self } } // &Matrix OP Matrix - impl<'a> $OpTrait> for &'a Matrix { + impl<'a> $OpTrait> for &'a Matrix { type Output = Matrix; fn $method(self, mut rhs: Matrix) -> Matrix { - assert_eq!(self.rows, rhs.rows); assert_eq!(self.cols, rhs.cols); - for (a, b) in self.data.iter().zip(rhs.data.iter_mut()) { *b = *a $op *b; } // bool is Copy + check_matrix_dims_for_ops(self, &rhs); + for (a, b) in self.data.iter().zip(rhs.data.iter_mut()) { *b = *a $op *b; } // bool is Copy rhs } } @@ -553,7 +571,7 @@ macro_rules! impl_bitwise_op_all { impl $OpTrait> for Matrix { type Output = Matrix; fn $method(mut self, rhs: Matrix) -> Matrix { - assert_eq!(self.rows, rhs.rows); assert_eq!(self.cols, rhs.cols); + check_matrix_dims_for_ops(&self, &rhs); for (a, b) in self.data.iter_mut().zip(rhs.data.iter()) { *a = *a $op *b; } // bool is Copy self } From 73a30d45c5572917c7bb9be58adf1c5a5921bbbd Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 01:16:33 +0100 Subject: [PATCH 19/23] Rename test matrix creation functions for clarity and consistency --- src/matrix/mat.rs | 125 +++++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 62 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index d1be2d5..ec61e66 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -624,7 +624,7 @@ mod tests { use super::{BoolMatrix, FloatMatrix, Matrix, StringMatrix}; // Helper function to create a basic Matrix for testing - fn create_test_matrix() -> Matrix { + fn static_test_matrix() -> Matrix { // Column-major data: // 1 4 7 // 2 5 8 @@ -634,7 +634,7 @@ mod tests { } // Another helper for a different size - fn create_test_matrix_2x4() -> Matrix { + fn static_test_matrix_2x4() -> Matrix { // Column-major data: // 1 3 5 7 // 2 4 6 8 @@ -733,7 +733,7 @@ mod tests { #[test] fn test_getters() { - let matrix = create_test_matrix(); + let matrix = static_test_matrix(); assert_eq!(matrix.rows(), 3); assert_eq!(matrix.cols(), 3); assert_eq!(matrix.data(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]); @@ -741,7 +741,7 @@ mod tests { #[test] fn test_index_and_get() { - let matrix = create_test_matrix(); + let matrix = static_test_matrix(); assert_eq!(matrix[(0, 0)], 1); assert_eq!(matrix[(1, 1)], 5); assert_eq!(matrix[(2, 2)], 9); @@ -753,21 +753,21 @@ mod tests { #[test] #[should_panic(expected = "index out of bounds")] - fn test_index_out_of_bounds_row() { - let matrix = create_test_matrix(); // 3x3 + fn test_index_out_of_bounds_row_alt() { + let matrix = static_test_matrix(); // 3x3 let _ = matrix[(3, 0)]; } #[test] #[should_panic(expected = "index out of bounds")] - fn test_index_out_of_bounds_col() { - let matrix = create_test_matrix(); // 3x3 + fn test_index_out_of_bounds_col_alt() { + let matrix = static_test_matrix(); // 3x3 let _ = matrix[(0, 3)]; } #[test] fn test_index_mut_and_get_mut() { - let mut matrix = create_test_matrix(); // 3x3 + let mut matrix = static_test_matrix(); // 3x3 matrix[(0, 0)] = 10; matrix[(1, 1)] = 20; @@ -794,20 +794,20 @@ mod tests { #[test] #[should_panic(expected = "index out of bounds")] fn test_index_mut_out_of_bounds_row() { - let mut matrix = create_test_matrix(); // 3x3 + let mut matrix = static_test_matrix(); // 3x3 matrix[(3, 0)] = 99; } #[test] #[should_panic(expected = "index out of bounds")] fn test_index_mut_out_of_bounds_col() { - let mut matrix = create_test_matrix(); // 3x3 + let mut matrix = static_test_matrix(); // 3x3 matrix[(0, 3)] = 99; } #[test] fn test_column() { - let matrix = create_test_matrix_2x4(); // 2x4 + let matrix = static_test_matrix_2x4(); // 2x4 // 1 3 5 7 // 2 4 6 8 @@ -818,15 +818,15 @@ mod tests { } #[test] - #[should_panic(expected = "range end index")] + #[should_panic(expected = "column index 4 out of bounds for 4 columns")] fn test_column_out_of_bounds() { - let matrix = create_test_matrix_2x4(); // 2x4 + let matrix = static_test_matrix_2x4(); // 2x4 matrix.column(4); } #[test] fn test_column_mut() { - let mut matrix = create_test_matrix_2x4(); // 2x4 + let mut matrix = static_test_matrix_2x4(); // 2x4 // 1 3 5 7 // 2 4 6 8 @@ -851,15 +851,15 @@ mod tests { } #[test] - #[should_panic(expected = "range end index")] + #[should_panic(expected = "column index 4 out of bounds for 4 columns")] fn test_column_mut_out_of_bounds() { - let mut matrix = create_test_matrix_2x4(); // 2x4 + let mut matrix = static_test_matrix_2x4(); // 2x4 matrix.column_mut(4); } #[test] fn test_iter_columns() { - let matrix = create_test_matrix_2x4(); // 2x4 + let matrix = static_test_matrix_2x4(); // 2x4 // 1 3 5 7 // 2 4 6 8 @@ -873,7 +873,7 @@ mod tests { #[test] fn test_iter_rows() { - let matrix = create_test_matrix_2x4(); // 2x4 + let matrix = static_test_matrix_2x4(); // 2x4 // 1 3 5 7 // 2 4 6 8 @@ -889,7 +889,7 @@ mod tests { // test data_mut #[test] fn test_data_mut() { - let mut matrix = create_test_matrix(); // 3x3 + let mut matrix = static_test_matrix(); // 3x3 // 1 4 7 // 2 5 8 // 3 6 9 @@ -904,7 +904,7 @@ mod tests { #[test] fn test_matrix_row_get_and_iter() { - let matrix = create_test_matrix_2x4(); // 2x4 + let matrix = static_test_matrix_2x4(); // 2x4 // 1 3 5 7 // 2 4 6 8 @@ -924,7 +924,7 @@ mod tests { #[test] fn test_swap_columns() { - let mut matrix = create_test_matrix(); // 3x3 + let mut matrix = static_test_matrix(); // 3x3 // 1 4 7 // 2 5 8 // 3 6 9 @@ -958,15 +958,15 @@ mod tests { } #[test] - #[should_panic(expected = "column index out of bounds")] + #[should_panic(expected = "column index c2=3 out of bounds for 3 columns")] fn test_swap_columns_out_of_bounds() { - let mut matrix = create_test_matrix(); // 3x3 + let mut matrix = static_test_matrix(); // 3x3 matrix.swap_columns(0, 3); } #[test] fn test_delete_column() { - let mut matrix = create_test_matrix_2x4(); // 2x4 + let mut matrix = static_test_matrix_2x4(); // 2x4 // 1 3 5 7 // 2 4 6 8 @@ -1015,15 +1015,15 @@ mod tests { } #[test] - #[should_panic(expected = "column index out of bounds")] + #[should_panic(expected = "column index 4 out of bounds for 4 columns")] fn test_delete_column_out_of_bounds() { - let mut matrix = create_test_matrix_2x4(); // 2x4 + let mut matrix = static_test_matrix_2x4(); // 2x4 matrix.delete_column(4); } #[test] fn test_delete_row() { - let mut matrix = create_test_matrix(); // 3x3 + let mut matrix = static_test_matrix(); // 3x3 // 1 4 7 // 2 5 8 // 3 6 9 @@ -1065,15 +1065,15 @@ mod tests { } #[test] - #[should_panic(expected = "row index out of bounds")] + #[should_panic(expected = "row index 3 out of bounds for 3 rows")] fn test_delete_row_out_of_bounds() { - let mut matrix = create_test_matrix(); // 3x3 + let mut matrix = static_test_matrix(); // 3x3 matrix.delete_row(3); } #[test] fn test_add_column() { - let mut matrix = create_test_matrix_2x4(); // 2x4 + let mut matrix = static_test_matrix_2x4(); // 2x4 // 1 3 5 7 // 2 4 6 8 @@ -1131,9 +1131,9 @@ mod tests { } #[test] - #[should_panic(expected = "column index out of bounds")] + #[should_panic(expected = "add_column index 5 out of bounds for 4 columns")] fn test_add_column_out_of_bounds() { - let mut matrix = create_test_matrix_2x4(); // 2x4 + let mut matrix = static_test_matrix_2x4(); // 2x4 let new_col = vec![9, 10]; matrix.add_column(5, new_col); // Index 5 is out of bounds for 4 columns } @@ -1141,14 +1141,14 @@ mod tests { #[test] #[should_panic(expected = "column length mismatch")] fn test_add_column_length_mismatch() { - let mut matrix = create_test_matrix_2x4(); // 2x4 (2 rows) + let mut matrix = static_test_matrix_2x4(); // 2x4 (2 rows) let new_col = vec![9, 10, 11]; // Wrong length matrix.add_column(0, new_col); } #[test] fn test_add_row() { - let mut matrix = create_test_matrix_2x4(); // 2x4 + let mut matrix = static_test_matrix_2x4(); // 2x4 // 1 3 5 7 // 2 4 6 8 @@ -1197,8 +1197,9 @@ mod tests { assert_eq!(matrix.rows(), 4); assert_eq!(matrix.cols(), 4); assert_eq!(matrix[(0, 0)], 13); + assert_eq!(matrix[(0, 1)], 14); + assert_eq!(matrix[(0, 2)], 15); assert_eq!(matrix[(0, 3)], 16); - // Check some existing elements to ensure they shifted correctly assert_eq!(matrix[(1, 0)], 1); assert_eq!(matrix[(2, 1)], 10); assert_eq!(matrix[(3, 3)], 8); @@ -1219,9 +1220,9 @@ mod tests { } #[test] - #[should_panic(expected = "row index out of bounds")] + #[should_panic(expected = "add_row index 3 out of bounds for 2 rows")] fn test_add_row_out_of_bounds() { - let mut matrix = create_test_matrix_2x4(); // 2x4 + let mut matrix = static_test_matrix_2x4(); // 2x4 let new_row = vec![9, 10, 11, 12]; matrix.add_row(3, new_row); // Index 3 is out of bounds for 2 rows } @@ -1229,14 +1230,14 @@ mod tests { #[test] #[should_panic(expected = "row length mismatch")] fn test_add_row_length_mismatch() { - let mut matrix = create_test_matrix_2x4(); // 2x4 (4 cols) + let mut matrix = static_test_matrix_2x4(); // 2x4 (4 cols) let new_row = vec![9, 10, 11]; // Wrong length matrix.add_row(0, new_row); } #[test] fn test_elementwise_add() { - let matrix1 = create_test_matrix(); // 3x3 + let matrix1 = static_test_matrix(); // 3x3 let matrix2 = Matrix::from_vec(vec![9, 8, 7, 6, 5, 4, 3, 2, 1], 3, 3); // 3x3 let result = &matrix1 + &matrix2; @@ -1257,7 +1258,7 @@ mod tests { #[test] fn test_elementwise_sub() { - let matrix1 = create_test_matrix(); // 3x3 + let matrix1 = static_test_matrix(); // 3x3 let matrix2 = Matrix::from_vec(vec![1, 1, 1, 2, 2, 2, 3, 3, 3], 3, 3); // 3x3 let result = &matrix1 - &matrix2; @@ -1278,7 +1279,7 @@ mod tests { #[test] fn test_elementwise_mul() { - let matrix1 = create_test_matrix(); // 3x3 + let matrix1 = static_test_matrix(); // 3x3 let matrix2 = Matrix::from_vec(vec![1, 2, 3, 1, 2, 3, 1, 2, 3], 3, 3); // 3x3 let result = &matrix1 * &matrix2; @@ -1299,7 +1300,7 @@ mod tests { #[test] fn test_elementwise_div() { - let matrix1 = create_test_matrix(); // 3x3 + let matrix1 = static_test_matrix(); // 3x3 let matrix2 = Matrix::from_vec(vec![1, 1, 1, 2, 2, 2, 7, 8, 9], 3, 3); // 3x3 let result = &matrix1 / &matrix2; // Integer division @@ -1319,18 +1320,18 @@ mod tests { } #[test] - #[should_panic(expected = "row count mismatch")] + #[should_panic(expected = "Row count mismatch: left has 3 rows, right has 2 rows")] fn test_elementwise_op_row_mismatch() { - let matrix1 = create_test_matrix(); // 3x3 - let matrix2 = create_test_matrix_2x4(); // 2x4 + let matrix1 = static_test_matrix(); // 3x3 + let matrix2 = static_test_matrix_2x4(); // 2x4 let _ = &matrix1 + &matrix2; // Should panic } #[test] - #[should_panic(expected = "row count mismatch")] + #[should_panic(expected = "Row count mismatch: left has 3 rows, right has 2 ro")] fn test_elementwise_op_col_mismatch() { - let matrix1 = create_test_matrix(); // 3x3 - let matrix2 = create_test_matrix_2x4(); // 2x4 + let matrix1 = static_test_matrix(); // 3x3 + let matrix2 = static_test_matrix_2x4(); // 2x4 let _ = &matrix1 * &matrix2; // Should panic } @@ -1421,7 +1422,7 @@ mod tests { } #[test] - #[should_panic(expected = "col count mismatch")] + #[should_panic(expected = "Column count mismatch: left has 2 columns, right has 3 columns")] fn test_bitwise_op_row_mismatch() { let data1 = vec![true, false, true, false]; // 2x2 let data2 = vec![true, true, false, false, true, true]; // 2x3 @@ -1431,7 +1432,7 @@ mod tests { } #[test] - #[should_panic(expected = "col count mismatch")] + #[should_panic(expected = "Column count mismatch: left has 2 columns, right has 3 columns")] fn test_bitwise_op_col_mismatch() { let data1 = vec![true, false, true, false]; // 2x2 let data2 = vec![true, true, false, false, true, true]; // 2x3 @@ -1466,7 +1467,7 @@ mod tests { matrix.add_column(1, new_col); // Add at index 1 // Should be: // hello c d - // b e f + // b e f assert_eq!(matrix.rows(), 2); assert_eq!(matrix.cols(), 3); assert_eq!(matrix[(0, 0)], "hello".to_string()); @@ -1480,9 +1481,9 @@ mod tests { let new_row = vec!["g".to_string(), "h".to_string(), "i".to_string()]; matrix.add_row(0, new_row); // Add at index 0 // Should be: - // g h i + // g h i // hello e c - // b f d + // b f d assert_eq!(matrix.rows(), 3); assert_eq!(matrix.cols(), 3); assert_eq!(matrix[(0, 0)], "g".to_string()); @@ -1596,25 +1597,25 @@ mod tests { // Test broadcastable operations #[test] fn test_comparision_broadcast() { - let matrix = create_test_matrix(); + let matrix = static_test_matrix(); // test all > 0 - let result = matrix.gt_elementwise(0).as_vec(); + let result = matrix.gt_elem(0).into_vec(); let expected = vec![true; result.len()]; assert_eq!(result, expected); - let ma = create_test_matrix(); - let mb = create_test_matrix(); + let ma = static_test_matrix(); + let mb = static_test_matrix(); - let result = ma.eq_elementwise(mb); + let result = ma.eq_elem(mb); assert!(result.all()); - let result = matrix.lt_elementwise(1e10 as i32).all(); + let result = matrix.lt_elem(1e10 as i32).all(); assert!(result); for i in 0..matrix.rows() { for j in 0..matrix.cols() { let vx = matrix[(i, j)]; - let c = &(matrix.le_elementwise(vx)) & &(matrix.ge_elementwise(vx)); + let c = &(matrix.le_elem(vx)) & &(matrix.ge_elem(vx)); assert_eq!(c.count(), 1); } } @@ -1622,7 +1623,7 @@ mod tests { #[test] fn test_arithmetic_broadcast() { - let matrix = create_test_matrix(); + let matrix = static_test_matrix(); let result = &matrix + 1; for i in 0..matrix.rows() { for j in 0..matrix.cols() { From ac0eed2d563886c6e81cab04f0ca5a2dd5f3a2e4 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 01:17:01 +0100 Subject: [PATCH 20/23] Add comprehensive arithmetic and boolean logic tests for Matrix operations --- src/matrix/mat.rs | 237 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 236 insertions(+), 1 deletion(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index ec61e66..f748ced 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -621,7 +621,242 @@ pub type StringMatrix = Matrix; mod tests { use crate::matrix::BoolOps; - use super::{BoolMatrix, FloatMatrix, Matrix, StringMatrix}; + use super::*; // Import items from outer scope + + // Helper to create a 2x2 f64 matrix easily + fn make_f64_matrix(a: f64, b: f64, c: f64, d: f64) -> FloatMatrix { + Matrix::from_cols(vec![vec![a, c], vec![b, d]]) + } + + // Helper to create a 2x2 bool matrix easily + fn make_bool_matrix(a: bool, b: bool, c: bool, d: bool) -> BoolMatrix { + Matrix::from_cols(vec![vec![a, c], vec![b, d]]) + } + + // --- Arithmetic Tests --- + + #[test] + fn test_add_f64() { + let m1 = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + let m2 = make_f64_matrix(5.0, 6.0, 7.0, 8.0); + let expected = make_f64_matrix(6.0, 8.0, 10.0, 12.0); + + assert_eq!(m1.clone() + m2.clone(), expected, "M + M"); + assert_eq!(m1.clone() + &m2, expected, "M + &M"); + assert_eq!(&m1 + m2.clone(), expected, "&M + M"); + assert_eq!(&m1 + &m2, expected, "&M + &M"); + } + + #[test] + fn test_add_scalar_f64() { + let m1 = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + let scalar = 10.0; + let expected = make_f64_matrix(11.0, 12.0, 13.0, 14.0); + + assert_eq!(m1.clone() + scalar, expected, "M + S"); + assert_eq!(&m1 + scalar, expected, "&M + S"); + } + + #[test] + fn test_sub_f64() { + let m1 = make_f64_matrix(10.0, 20.0, 30.0, 40.0); + let m2 = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + let expected = make_f64_matrix(9.0, 18.0, 27.0, 36.0); + + assert_eq!(m1.clone() - m2.clone(), expected, "M - M"); + assert_eq!(m1.clone() - &m2, expected, "M - &M"); + assert_eq!(&m1 - m2.clone(), expected, "&M - M"); + assert_eq!(&m1 - &m2, expected, "&M - &M"); + } + + #[test] + fn test_sub_scalar_f64() { + let m1 = make_f64_matrix(11.0, 12.0, 13.0, 14.0); + let scalar = 10.0; + let expected = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + + assert_eq!(m1.clone() - scalar, expected, "M - S"); + assert_eq!(&m1 - scalar, expected, "&M - S"); + } + + #[test] + fn test_mul_f64() { + // Element-wise + let m1 = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + let m2 = make_f64_matrix(5.0, 6.0, 7.0, 8.0); + let expected = make_f64_matrix(5.0, 12.0, 21.0, 32.0); + + assert_eq!(m1.clone() * m2.clone(), expected, "M * M"); + assert_eq!(m1.clone() * &m2, expected, "M * &M"); + assert_eq!(&m1 * m2.clone(), expected, "&M * M"); + assert_eq!(&m1 * &m2, expected, "&M * &M"); + } + + #[test] + fn test_mul_scalar_f64() { + let m1 = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + let scalar = 3.0; + let expected = make_f64_matrix(3.0, 6.0, 9.0, 12.0); + + assert_eq!(m1.clone() * scalar, expected, "M * S"); + assert_eq!(&m1 * scalar, expected, "&M * S"); + } + + #[test] + fn test_div_f64() { + // Element-wise + let m1 = make_f64_matrix(10.0, 20.0, 30.0, 40.0); + let m2 = make_f64_matrix(2.0, 5.0, 6.0, 8.0); + let expected = make_f64_matrix(5.0, 4.0, 5.0, 5.0); + + assert_eq!(m1.clone() / m2.clone(), expected, "M / M"); + assert_eq!(m1.clone() / &m2, expected, "M / &M"); + assert_eq!(&m1 / m2.clone(), expected, "&M / M"); + assert_eq!(&m1 / &m2, expected, "&M / &M"); + } + + #[test] + fn test_div_scalar_f64() { + let m1 = make_f64_matrix(10.0, 20.0, 30.0, 40.0); + let scalar = 10.0; + let expected = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + + assert_eq!(m1.clone() / scalar, expected, "M / S"); + assert_eq!(&m1 / scalar, expected, "&M / S"); + } + + #[test] + fn test_chained_ops_f64() { + let m = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + let result = (((m.clone() + 1.0) * 2.0) - 4.0) / 2.0; + // Expected: + // m+1 = [2, 3], [4, 5] + // *2 = [4, 6], [8, 10] + // -4 = [0, 2], [4, 6] + // /2 = [0, 1], [2, 3] + let expected = make_f64_matrix(0.0, 1.0, 2.0, 3.0); + assert_eq!(result, expected); + } + + // --- Boolean Logic Tests --- + + #[test] + fn test_bitand_bool() { + let m1 = make_bool_matrix(true, false, true, false); + let m2 = make_bool_matrix(true, true, false, false); + let expected = make_bool_matrix(true, false, false, false); + + assert_eq!(m1.clone() & m2.clone(), expected, "M & M"); + assert_eq!(m1.clone() & &m2, expected, "M & &M"); + assert_eq!(&m1 & m2.clone(), expected, "&M & M"); + assert_eq!(&m1 & &m2, expected, "&M & &M"); + } + + #[test] + fn test_bitor_bool() { + let m1 = make_bool_matrix(true, false, true, false); + let m2 = make_bool_matrix(true, true, false, false); + let expected = make_bool_matrix(true, true, true, false); + + assert_eq!(m1.clone() | m2.clone(), expected, "M | M"); + assert_eq!(m1.clone() | &m2, expected, "M | &M"); + assert_eq!(&m1 | m2.clone(), expected, "&M | M"); + assert_eq!(&m1 | &m2, expected, "&M | &M"); + } + + #[test] + fn test_bitxor_bool() { + let m1 = make_bool_matrix(true, false, true, false); + let m2 = make_bool_matrix(true, true, false, false); + let expected = make_bool_matrix(false, true, true, false); + + assert_eq!(m1.clone() ^ m2.clone(), expected, "M ^ M"); + assert_eq!(m1.clone() ^ &m2, expected, "M ^ &M"); + assert_eq!(&m1 ^ m2.clone(), expected, "&M ^ M"); + assert_eq!(&m1 ^ &m2, expected, "&M ^ &M"); + } + + #[test] + fn test_not_bool() { + let m = make_bool_matrix(true, false, true, false); + let expected = make_bool_matrix(false, true, false, true); + + assert_eq!(!m.clone(), expected, "!M (consuming)"); + assert_eq!(!&m, expected, "!&M (borrowing)"); + + // Check original is unchanged when using !&M + let original = make_bool_matrix(true, false, true, false); + let _negated_ref = !&original; + assert_eq!(original, make_bool_matrix(true, false, true, false)); + } + + // --- Comparison Tests --- + #[test] + fn test_comparison_eq_elem() { + let m1 = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + let m2 = make_f64_matrix(1.0, 0.0, 3.0, 5.0); + let s = 3.0; + let expected_m = make_bool_matrix(true, false, true, false); + let expected_s = make_bool_matrix(false, false, true, false); + + assert_eq!(m1.eq_elem(m2), expected_m, "eq_elem matrix"); + assert_eq!(m1.eq_elem(s), expected_s, "eq_elem scalar"); + } + + #[test] + fn test_comparison_gt_elem() { + let m1 = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + let m2 = make_f64_matrix(0.0, 3.0, 3.0, 5.0); + let s = 2.5; + let expected_m = make_bool_matrix(true, false, false, false); + let expected_s = make_bool_matrix(false, false, true, true); + + assert_eq!(m1.gt_elem(m2), expected_m, "gt_elem matrix"); + assert_eq!(m1.gt_elem(s), expected_s, "gt_elem scalar"); + } + + // Add more comparison tests (lt, le, ge, ne) if desired... + + // --- Basic Method Tests --- + #[test] + fn test_indexing() { + let m = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + assert_eq!(m[(0, 0)], 1.0); + assert_eq!(m[(0, 1)], 2.0); + assert_eq!(m[(1, 0)], 3.0); + assert_eq!(m[(1, 1)], 4.0); + assert_eq!(*m.get(1, 0), 3.0); // Test get() too + } + + #[test] + #[should_panic] + fn test_index_out_of_bounds_row() { + let m = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + let _ = m[(2, 0)]; // Row 2 is out of bounds + } + + #[test] + #[should_panic] + fn test_index_out_of_bounds_col() { + let m = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + let _ = m[(0, 2)]; // Col 2 is out of bounds + } + + #[test] + fn test_dimensions() { + let m = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + assert_eq!(m.rows(), 2); + assert_eq!(m.cols(), 2); + } + + #[test] + fn test_from_vec() { + let data = vec![1.0, 3.0, 2.0, 4.0]; // Column major: [col0_row0, col0_row1, col1_row0, col1_row1] + let m = Matrix::from_vec(data, 2, 2); + let expected = make_f64_matrix(1.0, 2.0, 3.0, 4.0); + assert_eq!(m, expected); + assert_eq!(m.to_vec(), vec![1.0, 3.0, 2.0, 4.0]); + } // Helper function to create a basic Matrix for testing fn static_test_matrix() -> Matrix { From b17863dcdcfaefa9c32fae1bfb4109f2043468dd Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 01:18:24 +0100 Subject: [PATCH 21/23] Organize code structure by adding section comments --- src/matrix/mat.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index f748ced..180650a 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -314,6 +314,8 @@ impl IndexMut<(usize, usize)> for Matrix { } } +// --- Row Iterator Helper --- + /// Represents an immutable view of a single row in the matrix. pub struct MatrixRow<'a, T> { matrix: &'a Matrix, @@ -328,10 +330,12 @@ impl<'a, T> MatrixRow<'a, T> { /// Returns an iterator over all elements in this row. pub fn iter(&self) -> impl Iterator { - (0..self.matrix.cols).map(move |c| &self.matrix[(self.row, c)]) + (0..self.matrix.cols).map(move |c| self.get(c)) } } +// --- Reduction Axis Enum --- + /// Specifies the axis along which to perform a reduction operation. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Axis { @@ -363,6 +367,8 @@ impl Broadcastable for Matrix { } } +// --- Element-wise Comparisons --- + /// Generates element-wise eq, lt, le, gt and ge methods /// where the rhs can be a `Matrix` or a scalar T. macro_rules! impl_elementwise_cmp { @@ -612,11 +618,15 @@ impl Not for &Matrix { } } +// --- Type Aliases --- pub type FloatMatrix = Matrix; pub type BoolMatrix = Matrix; pub type IntMatrix = Matrix; pub type StringMatrix = Matrix; + +// --- Unit Tests --- + #[cfg(test)] mod tests { use crate::matrix::BoolOps; From 1dbe5d0efe544b063ff2261788dd054c04970f6c Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 01:19:01 +0100 Subject: [PATCH 22/23] Update matrix arithmetic operations --- README.md | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3a990ae..931b463 100644 --- a/README.md +++ b/README.md @@ -76,17 +76,26 @@ let total: f64 = result.sum_vertical().iter().sum::(); assert_eq!(total, 184.0); // broadcast & reduce -let result: Matrix = &ma + 1.0; // add scalar -let result: Matrix = &result - 1.0; // subtract scalar -let result: Matrix = &result * 2.0; // multiply by scalar -let result: Matrix = &result / 2.0; // divide by scalar +let result: Matrix = ma.clone() + 1.0; // add scalar +let result: Matrix = result + &ma - &ma; // add matrix +let result: Matrix = result - 1.0; // subtract scalar +let result: Matrix = result * 2.0; // multiply by scalar +let result: Matrix = result / 2.0; // divide by scalar -let check: bool = result.eq_elementwise(ma.clone()).all(); +let check: bool = result.eq_elem(ma.clone()).all(); assert!(check); // The above math can also be written as: let check: bool = (&(&(&(&ma + 1.0) - 1.0) * 2.0) / 2.0) - .eq_elementwise(ma) + .eq_elem(ma.clone()) .all(); assert!(check); + +// The above math can also be written as: +let check: bool = ((((ma.clone() + 1.0) - 1.0) * 2.0) / 2.0) + .eq_elem(ma) + .all(); +assert!(check); + + ``` From 25a2a0d831344bbee8c46a4aadde471a025864b9 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 01:19:27 +0100 Subject: [PATCH 23/23] Add .vscode/ to .gitignore to exclude Visual Studio Code settings --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 90d4e56..665001e 100644 --- a/.gitignore +++ b/.gitignore @@ -12,5 +12,6 @@ data/ *.info .venv/ +.vscode/ tarpaulin-report.* \ No newline at end of file