nalgebra/examples/matrixcompare.rs

83 lines
2.1 KiB
Rust
Raw Normal View History

2019-08-12 00:26:12 +08:00
extern crate nalgebra as na;
2020-06-30 01:03:20 +08:00
use matrixcompare::comparators::{AbsoluteElementwiseComparator, ExactElementwiseComparator};
2019-08-12 00:26:12 +08:00
use matrixcompare::compare_matrices;
2021-04-11 17:00:38 +08:00
use na::{OMatrix, U3, U4};
2019-08-12 00:26:12 +08:00
fn compare_integers_fail() {
println!("Comparing two integer matrices.");
2020-06-30 01:03:20 +08:00
#[rustfmt::skip]
2021-04-11 17:00:38 +08:00
let a = OMatrix::<_, U3, U4>::from_row_slice(&[
2019-08-12 00:26:12 +08:00
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, -2, 11
]);
2020-06-30 01:03:20 +08:00
#[rustfmt::skip]
2021-04-11 17:00:38 +08:00
let b = OMatrix::<_, U3, U4>::from_row_slice(&[
2019-08-12 00:26:12 +08:00
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11
]);
2020-06-30 01:03:20 +08:00
if let Err(err) = compare_matrices(a, b, &ExactElementwiseComparator) {
println!("{}", err);
2019-08-12 00:26:12 +08:00
}
}
fn compare_different_size() {
println!("Comparing matrices of different size.");
2020-06-30 01:03:20 +08:00
#[rustfmt::skip]
2021-04-11 17:00:38 +08:00
let a = OMatrix::<_, U3, U3>::from_row_slice(&[
2019-08-12 00:26:12 +08:00
0, 1, 2,
4, 5, 6,
8, 9, 10,
]);
2020-06-30 01:03:20 +08:00
#[rustfmt::skip]
2021-04-11 17:00:38 +08:00
let b = OMatrix::<_, U3, U4>::from_row_slice(&[
2019-08-12 00:26:12 +08:00
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11
]);
2020-06-30 01:03:20 +08:00
if let Err(err) = compare_matrices(a, b, &ExactElementwiseComparator) {
println!("{}", err);
2019-08-12 00:26:12 +08:00
}
}
fn compare_f64_abs_tol_fail() {
println!("Comparing two f64 matrices.");
2020-06-30 01:03:20 +08:00
#[rustfmt::skip]
2021-04-11 17:00:38 +08:00
let a = OMatrix::<f64, U3, U3>::from_row_slice(&[
2019-08-12 00:26:12 +08:00
0.0, 1.0, 2.0 + 1e-10,
4.0, 5.0, 6.0,
8.0, 9.0, 10.0,
]);
2020-06-30 01:03:20 +08:00
#[rustfmt::skip]
2021-04-11 17:00:38 +08:00
let b = OMatrix::<_, U3, U3>::from_row_slice(&[
2019-08-12 00:26:12 +08:00
0.0, 1.0, 2.0,
4.0, 5.0, 6.0,
8.0, 9.0, 10.0
]);
let cmp = AbsoluteElementwiseComparator { tol: 1e-12 };
2020-06-30 01:03:20 +08:00
if let Err(err) = compare_matrices(a, b, &cmp) {
println!("{}", err);
2019-08-12 00:26:12 +08:00
}
}
fn main() {
// This example mostly serves the purpose of demonstrating the kind of error messages
// that are given upon comparison failure.
// The more typical use case is using `assert_matrix_eq!` in tests.
compare_integers_fail();
println!("======================================================");
compare_f64_abs_tol_fail();
println!("======================================================");
compare_different_size();
2020-06-30 01:03:20 +08:00
}