Debian Codegraph @

debian/missing-sources/nalgebra/examples/matrixcompare.rs

raw
1 extern crate nalgebra as na;
2
3 use matrixcompare::comparators::{AbsoluteElementwiseComparator, ExactElementwiseComparator};
4 use matrixcompare::compare_matrices;
5 use na::{OMatrix, U3, U4};
6
7 fn compare_integers_fail() {
8 println!("Comparing two integer matrices.");
9
10 #[rustfmt::skip]
11 let a = OMatrix::<_, U3, U4>::from_row_slice(&[
12 0, 1, 2, 3,
13 4, 5, 6, 7,
14 8, 9, -2, 11
15 ]);
16
17 #[rustfmt::skip]
18 let b = OMatrix::<_, U3, U4>::from_row_slice(&[
19 0, 1, 2, 3,
20 4, 5, 6, 7,
21 8, 9, 10, 11
22 ]);
23
24 if let Err(err) = compare_matrices(a, b, &ExactElementwiseComparator) {
25 println!("{}", err);
26 }
27 }
28
29 fn compare_different_size() {
30 println!("Comparing matrices of different size.");
31 #[rustfmt::skip]
32 let a = OMatrix::<_, U3, U3>::from_row_slice(&[
33 0, 1, 2,
34 4, 5, 6,
35 8, 9, 10,
36 ]);
37
38 #[rustfmt::skip]
39 let b = OMatrix::<_, U3, U4>::from_row_slice(&[
40 0, 1, 2, 3,
41 4, 5, 6, 7,
42 8, 9, 10, 11
43 ]);
44
45 if let Err(err) = compare_matrices(a, b, &ExactElementwiseComparator) {
46 println!("{}", err);
47 }
48 }
49
50 fn compare_f64_abs_tol_fail() {
51 println!("Comparing two f64 matrices.");
52
53 #[rustfmt::skip]
54 let a = OMatrix::<f64, U3, U3>::from_row_slice(&[
55 0.0, 1.0, 2.0 + 1e-10,
56 4.0, 5.0, 6.0,
57 8.0, 9.0, 10.0,
58 ]);
59
60 #[rustfmt::skip]
61 let b = OMatrix::<_, U3, U3>::from_row_slice(&[
62 0.0, 1.0, 2.0,
63 4.0, 5.0, 6.0,
64 8.0, 9.0, 10.0
65 ]);
66
67 let cmp = AbsoluteElementwiseComparator { tol: 1e-12 };
68 if let Err(err) = compare_matrices(a, b, &cmp) {
69 println!("{}", err);
70 }
71 }
72
73 fn main() {
74 // This example mostly serves the purpose of demonstrating the kind of error messages
75 // that are given upon comparison failure.
76 // The more typical use case is using `assert_matrix_eq!` in tests.
77 compare_integers_fail();
78 println!("======================================================");
79 compare_f64_abs_tol_fail();
80 println!("======================================================");
81 compare_different_size();
82 }