Enhance element-wise comparison methods to optimize data handling

This commit is contained in:
Palash Tyagi 2025-05-03 00:35:34 +01:00
parent ecb1939ec2
commit 10c6116f8f

View File

@ -372,24 +372,25 @@ macro_rules! impl_elementwise_cmp {
impl<T: PartialOrd + Clone> Matrix<T> { impl<T: PartialOrd + Clone> Matrix<T> {
$( $(
#[doc = concat!("Element-wise comparison `self ", stringify!($op), " rhs`,\n\ #[doc = concat!("Element-wise comparison `self ", stringify!($op), " rhs`,\n\
where `rhs` may be a `Matrix<T>` or a scalar T.")] where `rhs` may be a `Matrix<T>` or a scalar T.\n\
Returns a `BoolMatrix`.")]
pub fn $method<Rhs>(&self, rhs: Rhs) -> BoolMatrix pub fn $method<Rhs>(&self, rhs: Rhs) -> BoolMatrix
where where
Rhs: Broadcastable<T>, Rhs: Broadcastable<T>,
{ {
// Prepare broadcasted rhs-data // Prepare broadcasted rhs-data using the trait
let rhs_data = rhs.to_vec(self.rows, self.cols); let rhs_data = rhs.to_vec(self.rows, self.cols);
// Pairwise compare // Pairwise compare using iterators
let data = self let data = self
.data .data
.iter() .iter() // Borrow self's data
.cloned() .zip(rhs_data.iter()) // Borrow rhs's broadcasted data
.zip(rhs_data.into_iter()) .map(|(a, b)| a $op b) // Perform comparison op
.map(|(a, b)| a $op b)
.collect(); .collect();
BoolMatrix::from_vec(data, self.rows, self.cols) // Create BoolMatrix from result
Matrix::<bool>::from_vec(data, self.rows, self.cols)
} }
)* )*
} }