Improve matrixcompare example

This commit is contained in:
Andreas Longva 2020-06-29 19:03:20 +02:00
parent f6730dac1f
commit 9196759bc2
3 changed files with 27 additions and 18 deletions

View File

@ -1,60 +1,63 @@
extern crate nalgebra as na; extern crate nalgebra as na;
#[macro_use] use matrixcompare::comparators::{AbsoluteElementwiseComparator, ExactElementwiseComparator};
extern crate quickcheck;
use na::{U3, U4, MatrixMN};
use matrixcompare::compare_matrices; use matrixcompare::compare_matrices;
use matrixcompare::comparators::{ExactElementwiseComparator, AbsoluteElementwiseComparator}; use na::{MatrixMN, U3, U4};
fn compare_integers_fail() { fn compare_integers_fail() {
println!("Comparing two integer matrices."); println!("Comparing two integer matrices.");
#[rustfmt::skip]
let a = MatrixMN::<_, U3, U4>::from_row_slice(&[ let a = MatrixMN::<_, U3, U4>::from_row_slice(&[
0, 1, 2, 3, 0, 1, 2, 3,
4, 5, 6, 7, 4, 5, 6, 7,
8, 9, -2, 11 8, 9, -2, 11
]); ]);
#[rustfmt::skip]
let b = MatrixMN::<_, U3, U4>::from_row_slice(&[ let b = MatrixMN::<_, U3, U4>::from_row_slice(&[
0, 1, 2, 3, 0, 1, 2, 3,
4, 5, 6, 7, 4, 5, 6, 7,
8, 9, 10, 11 8, 9, 10, 11
]); ]);
if let Some(msg) = compare_matrices(a, b, &ExactElementwiseComparator).panic_message() { if let Err(err) = compare_matrices(a, b, &ExactElementwiseComparator) {
println!("{}", msg); println!("{}", err);
} }
} }
fn compare_different_size() { fn compare_different_size() {
println!("Comparing matrices of different size."); println!("Comparing matrices of different size.");
#[rustfmt::skip]
let a = MatrixMN::<_, U3, U3>::from_row_slice(&[ let a = MatrixMN::<_, U3, U3>::from_row_slice(&[
0, 1, 2, 0, 1, 2,
4, 5, 6, 4, 5, 6,
8, 9, 10, 8, 9, 10,
]); ]);
#[rustfmt::skip]
let b = MatrixMN::<_, U3, U4>::from_row_slice(&[ let b = MatrixMN::<_, U3, U4>::from_row_slice(&[
0, 1, 2, 3, 0, 1, 2, 3,
4, 5, 6, 7, 4, 5, 6, 7,
8, 9, 10, 11 8, 9, 10, 11
]); ]);
if let Some(msg) = compare_matrices(a, b, &ExactElementwiseComparator).panic_message() { if let Err(err) = compare_matrices(a, b, &ExactElementwiseComparator) {
println!("{}", msg); println!("{}", err);
} }
} }
fn compare_f64_abs_tol_fail() { fn compare_f64_abs_tol_fail() {
println!("Comparing two f64 matrices."); println!("Comparing two f64 matrices.");
#[rustfmt::skip]
let a = MatrixMN::<f64, U3, U3>::from_row_slice(&[ let a = MatrixMN::<f64, U3, U3>::from_row_slice(&[
0.0, 1.0, 2.0 + 1e-10, 0.0, 1.0, 2.0 + 1e-10,
4.0, 5.0, 6.0, 4.0, 5.0, 6.0,
8.0, 9.0, 10.0, 8.0, 9.0, 10.0,
]); ]);
#[rustfmt::skip]
let b = MatrixMN::<_, U3, U3>::from_row_slice(&[ let b = MatrixMN::<_, U3, U3>::from_row_slice(&[
0.0, 1.0, 2.0, 0.0, 1.0, 2.0,
4.0, 5.0, 6.0, 4.0, 5.0, 6.0,
@ -62,8 +65,8 @@ fn compare_f64_abs_tol_fail() {
]); ]);
let cmp = AbsoluteElementwiseComparator { tol: 1e-12 }; let cmp = AbsoluteElementwiseComparator { tol: 1e-12 };
if let Some(msg) = compare_matrices(a, b, &cmp).panic_message() { if let Err(err) = compare_matrices(a, b, &cmp) {
println!("{}", msg); println!("{}", err);
} }
} }
@ -76,4 +79,4 @@ fn main() {
compare_f64_abs_tol_fail(); compare_f64_abs_tol_fail();
println!("======================================================"); println!("======================================================");
compare_different_size(); compare_different_size();
} }

View File

@ -160,8 +160,9 @@ impl<N: Scalar, R: Dim, C: Dim, S: Abomonation> Abomonation for Matrix<N, R, C,
} }
#[cfg(feature = "compare")] #[cfg(feature = "compare")]
impl<N: Scalar, R: Dim, C: Dim, S: Storage<N, R, C>> impl<N: Scalar, R: Dim, C: Dim, S: Storage<N, R, C>> matrixcompare_core::Matrix<N>
matrixcompare_core::Matrix<N> for Matrix<N, R, C, S> { for Matrix<N, R, C, S>
{
fn rows(&self) -> usize { fn rows(&self) -> usize {
self.nrows() self.nrows()
} }
@ -176,8 +177,9 @@ impl<N: Scalar, R: Dim, C: Dim, S: Storage<N, R, C>>
} }
#[cfg(feature = "compare")] #[cfg(feature = "compare")]
impl<N: Scalar, R: Dim, C: Dim, S: Storage<N, R, C>> impl<N: Scalar, R: Dim, C: Dim, S: Storage<N, R, C>> matrixcompare_core::DenseAccess<N>
matrixcompare_core::DenseAccess<N> for Matrix<N, R, C, S> { for Matrix<N, R, C, S>
{
fn fetch_single(&self, row: usize, col: usize) -> N { fn fetch_single(&self, row: usize, col: usize) -> N {
self.index((row, col)).clone() self.index((row, col)).clone()
} }

View File

@ -4,7 +4,7 @@
//! The tests here only check that the necessary trait implementations are correctly implemented, //! The tests here only check that the necessary trait implementations are correctly implemented,
//! in addition to some sanity checks with example input. //! in addition to some sanity checks with example input.
use nalgebra::{U4, U5, MatrixMN, DMatrix}; use nalgebra::{DMatrix, MatrixMN, U4, U5};
use matrixcompare::{assert_matrix_eq, DenseAccess}; use matrixcompare::{assert_matrix_eq, DenseAccess};
@ -31,6 +31,7 @@ quickcheck! {
#[test] #[test]
fn assert_matrix_eq_dense_positive_comparison() { fn assert_matrix_eq_dense_positive_comparison() {
#[rustfmt::skip]
let a = MatrixMN::<_, U4, U5>::from_row_slice(&[ let a = MatrixMN::<_, U4, U5>::from_row_slice(&[
1210, 1320, 1430, 1540, 1650, 1210, 1320, 1430, 1540, 1650,
2310, 2420, 2530, 2640, 2750, 2310, 2420, 2530, 2640, 2750,
@ -38,6 +39,7 @@ fn assert_matrix_eq_dense_positive_comparison() {
4510, 4620, 4730, 4840, 4950, 4510, 4620, 4730, 4840, 4950,
]); ]);
#[rustfmt::skip]
let b = MatrixMN::<_, U4, U5>::from_row_slice(&[ let b = MatrixMN::<_, U4, U5>::from_row_slice(&[
1210, 1320, 1430, 1540, 1650, 1210, 1320, 1430, 1540, 1650,
2310, 2420, 2530, 2640, 2750, 2310, 2420, 2530, 2640, 2750,
@ -63,6 +65,7 @@ fn assert_matrix_eq_dense_positive_comparison() {
#[test] #[test]
#[should_panic] #[should_panic]
fn assert_matrix_eq_dense_negative_comparison() { fn assert_matrix_eq_dense_negative_comparison() {
#[rustfmt::skip]
let a = MatrixMN::<_, U4, U5>::from_row_slice(&[ let a = MatrixMN::<_, U4, U5>::from_row_slice(&[
1210, 1320, 1430, 1540, 1650, 1210, 1320, 1430, 1540, 1650,
2310, 2420, 2530, 2640, 2750, 2310, 2420, 2530, 2640, 2750,
@ -70,6 +73,7 @@ fn assert_matrix_eq_dense_negative_comparison() {
4510, 4620, -4730, 4840, 4950, 4510, 4620, -4730, 4840, 4950,
]); ]);
#[rustfmt::skip]
let b = MatrixMN::<_, U4, U5>::from_row_slice(&[ let b = MatrixMN::<_, U4, U5>::from_row_slice(&[
1210, 1320, 1430, 1540, 1650, 1210, 1320, 1430, 1540, 1650,
2310, 2420, 2530, 2640, 2750, 2310, 2420, 2530, 2640, 2750,
@ -78,4 +82,4 @@ fn assert_matrix_eq_dense_negative_comparison() {
]); ]);
assert_matrix_eq!(a, b); assert_matrix_eq!(a, b);
} }