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

View File

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

View File

@ -4,7 +4,7 @@
//! The tests here only check that the necessary trait implementations are correctly implemented,
//! 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};
@ -31,6 +31,7 @@ quickcheck! {
#[test]
fn assert_matrix_eq_dense_positive_comparison() {
#[rustfmt::skip]
let a = MatrixMN::<_, U4, U5>::from_row_slice(&[
1210, 1320, 1430, 1540, 1650,
2310, 2420, 2530, 2640, 2750,
@ -38,6 +39,7 @@ fn assert_matrix_eq_dense_positive_comparison() {
4510, 4620, 4730, 4840, 4950,
]);
#[rustfmt::skip]
let b = MatrixMN::<_, U4, U5>::from_row_slice(&[
1210, 1320, 1430, 1540, 1650,
2310, 2420, 2530, 2640, 2750,
@ -63,6 +65,7 @@ fn assert_matrix_eq_dense_positive_comparison() {
#[test]
#[should_panic]
fn assert_matrix_eq_dense_negative_comparison() {
#[rustfmt::skip]
let a = MatrixMN::<_, U4, U5>::from_row_slice(&[
1210, 1320, 1430, 1540, 1650,
2310, 2420, 2530, 2640, 2750,
@ -70,6 +73,7 @@ fn assert_matrix_eq_dense_negative_comparison() {
4510, 4620, -4730, 4840, 4950,
]);
#[rustfmt::skip]
let b = MatrixMN::<_, U4, U5>::from_row_slice(&[
1210, 1320, 1430, 1540, 1650,
2310, 2420, 2530, 2640, 2750,