mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-08-20 07:09:59 +00:00
Refactor element-wise comparison implementation for matrices
This commit is contained in:
parent
ea8e1c8471
commit
601c1c58d0
@ -184,30 +184,27 @@ impl<'a, T> MatrixRow<'a, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PartialEq for element-wise comparison
|
macro_rules! impl_cmp {
|
||||||
impl<T: PartialEq> Matrix<T> {
|
($name:ident, $op:tt) => {
|
||||||
/// Performs element-wise equality comparison with another matrix.
|
pub fn $name(&self, rhs: &Matrix<T>) -> BoolMatrix {
|
||||||
/// Returns a new `Matrix<bool>` where each element is the result of comparing the corresponding elements.
|
assert_eq!(self.rows, rhs.rows, "…");
|
||||||
/// Panics if the matrices have different dimensions.
|
assert_eq!(self.cols, rhs.cols, "…");
|
||||||
pub fn eq_elementwise(&self, rhs: &Matrix<T>) -> BoolMatrix {
|
|
||||||
assert_eq!(
|
|
||||||
self.rows, rhs.rows,
|
|
||||||
"Matrices must have the same number of rows for element-wise comparison"
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
self.cols, rhs.cols,
|
|
||||||
"Matrices must have the same number of columns for element-wise comparison"
|
|
||||||
);
|
|
||||||
|
|
||||||
let data = self
|
let data = self.data.iter()
|
||||||
.data
|
.zip(rhs.data.iter())
|
||||||
.iter()
|
.map(|(a, b)| a $op b)
|
||||||
.zip(rhs.data.iter())
|
.collect();
|
||||||
.map(|(a, b)| a == b) // Use T::PartialEq::eq
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
BoolMatrix::from_vec(data, self.rows, self.cols)
|
BoolMatrix::from_vec(data, self.rows, self.cols)
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: PartialOrd> Matrix<T> {
|
||||||
|
impl_cmp!(lt_elementwise, <);
|
||||||
|
impl_cmp!(le_elementwise, <=);
|
||||||
|
impl_cmp!(gt_elementwise, >);
|
||||||
|
impl_cmp!(ge_elementwise, >=);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates element-wise arithmetic implementations for matrices.
|
/// Generates element-wise arithmetic implementations for matrices.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user