Skip to content

Commit 57b83b8

Browse files
authored
Asserting matrix shapes for Matrix::abs_diff_eq (#1568)
1 parent bbd956a commit 57b83b8

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/base/matrix.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,7 @@ where
17501750

17511751
#[inline]
17521752
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
1753+
assert!(self.shape() == other.shape());
17531754
self.iter()
17541755
.zip(other.iter())
17551756
.all(|(a, b)| a.abs_diff_eq(b, epsilon.clone()))

tests/core/matrix.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,3 +1342,26 @@ fn parallel_column_iteration_mut() {
13421342
assert_eq!(first, second);
13431343
assert_eq!(second, DMatrix::identity(400, 300));
13441344
}
1345+
1346+
#[test]
1347+
fn abs_diff_eq_basic_test() {
1348+
use approx::AbsDiffEq;
1349+
use nalgebra_macros::dvector;
1350+
1351+
let a = dvector![1.0, 2.0];
1352+
let b = dvector![1.0, 2.0];
1353+
1354+
assert!(a.abs_diff_eq(&b, 1e-6));
1355+
}
1356+
1357+
#[test]
1358+
#[should_panic]
1359+
fn abs_diff_eq_nonmatching_shapes() {
1360+
use approx::AbsDiffEq;
1361+
use nalgebra_macros::dvector;
1362+
1363+
let a = dvector![1.0, 2.0, 3.0];
1364+
let b = dvector![1.0, 2.0];
1365+
1366+
a.abs_diff_eq(&b, 1e-6);
1367+
}

0 commit comments

Comments
 (0)