Debian Codegraph @

debian/missing-sources/nalgebra/tests/linalg/inverse.rs

raw
1 use na::{Matrix1, Matrix2, Matrix3, Matrix4, Matrix5};
2
3 #[test]
4 fn matrix1_try_inverse() {
5 let a = Matrix1::new(3.0);
6 let a_inv = a.try_inverse().expect("Matrix is invertible");
7
8 assert_relative_eq!(a_inv, Matrix1::new(1.0 / 3.0));
9 }
10
11 #[test]
12 #[rustfmt::skip]
13 fn matrix2_try_inverse() {
14 let a = Matrix2::new( 5.0, -2.0,
15 -10.0, 1.0);
16 let expected_inverse = Matrix2::new(-0.2 / 3.0, -2.0 / 15.0,
17 -2.0 / 3.0, -1.0 / 3.0);
18 let a_inv = a.try_inverse()
19 .expect("Matrix is invertible");
20
21 assert_relative_eq!(a_inv, expected_inverse);
22 }
23
24 #[test]
25 #[rustfmt::skip]
26 fn matrix3_try_inverse() {
27 let a = Matrix3::new(-3.0, 2.0, 0.0,
28 -6.0, 9.0, -2.0,
29 9.0, -6.0, 4.0);
30 let expected_inverse = Matrix3::new(-0.40, 0.4 / 3.0, 0.2 / 3.00,
31 -0.10, 0.2, 0.10,
32 0.75, 0.0, 0.25);
33 let a_inv = a.try_inverse()
34 .expect("Matrix is invertible");
35
36 assert_relative_eq!(a_inv, expected_inverse);
37 }
38
39 #[test]
40 #[rustfmt::skip]
41 fn matrix4_try_inverse_issue_214() {
42 let m1 = Matrix4::new(
43 -0.34727043, 0.00000005397217, -0.000000000000003822135, -0.000000000000003821371,
44 0.0, -0.000000026986084, -1.0001999, -1.0,
45 0.000000030359345, 0.61736965, -0.000000043720128, -0.00000004371139,
46 -0.0000000029144975, -0.05926739, 3.8007796, 4.0);
47
48
49 let m2 = Matrix4::new(
50 -0.34727043, 0.00000005397217, -0.000000000000003822135, -0.000000000000003821371,
51 0.0, -0.000000026986084, -1.0001999, -1.0,
52 0.000000030359345, 0.61736965, -0.000000043720128, -0.00000004371139,
53 -0.0000000029448568, -0.05988476, 3.8007796, 4.0);
54
55 assert!(m1.try_inverse().is_some());
56 assert!(m2.try_inverse().is_some());
57 assert!(m1.transpose().try_inverse().is_some());
58 assert!(m2.transpose().try_inverse().is_some());
59 }
60
61 #[test]
62 #[rustfmt::skip]
63 fn matrix5_try_inverse() {
64 // Dimension 5 is chosen so that the inversion happens by Gaussian elimination.
65 // (at the time of writing dimensions <= 3 are implemented as analytic formulas, but we choose
66 // 5 in the case that 4 also gets an analytic implementation)
67 let a = Matrix5::new(-2.0, 0.0, 2.0, 5.0, -5.0,
68 -6.0, 4.0, 4.0, 13.0, -15.0,
69 4.0, 16.0, -14.0, -19.0, 12.0,
70 12.0, 12.0, -22.0, -35.0, 34.0,
71 -8.0, 4.0, 12.0, 27.0, -31.0);
72 let expected_inverse = Matrix5::new(
73 3.9333e+00, -1.5667e+00, 2.6667e-01, 6.6667e-02, 3.0000e-01,
74 -1.2033e+01, 3.9667e+00, -1.1167e+00, 2.8333e-01, -1.0000e-01,
75 -1.8233e+01, 5.7667e+00, -1.5667e+00, 2.3333e-01, -2.0000e-01,
76 -4.3333e+00, 1.6667e+00, -6.6667e-01, 3.3333e-01, -4.6950e-19,
77 -1.3400e+01, 4.6000e+00, -1.4000e+00, 4.0000e-01, -2.0000e-01);
78 let a_inv = a.try_inverse().expect("Matrix is not invertible");
79
80 assert_relative_eq!(a_inv, expected_inverse, max_relative=1e-4);
81 }
82
83 #[test]
84 fn matrix1_try_inverse_scaled_identity() {
85 // A perfectly invertible matrix with
86 // very small coefficients
87 let a = Matrix1::new(1.0e-20);
88 let expected_inverse = Matrix1::new(1.0e20);
89 let a_inv = a.try_inverse().expect("Matrix should be invertible");
90
91 assert_relative_eq!(a_inv, expected_inverse);
92 }
93
94 #[test]
95 #[rustfmt::skip]
96 fn matrix2_try_inverse_scaled_identity() {
97 // A perfectly invertible matrix with
98 // very small coefficients
99 let a = Matrix2::new(1.0e-20, 0.0,
100 0.0, 1.0e-20);
101 let expected_inverse = Matrix2::new(1.0e20, 0.0,
102 0.0, 1.0e20);
103 let a_inv = a.try_inverse().expect("Matrix should be invertible");
104
105 assert_relative_eq!(a_inv, expected_inverse);
106 }
107
108 #[test]
109 #[rustfmt::skip]
110 fn matrix3_try_inverse_scaled_identity() {
111 // A perfectly invertible matrix with
112 // very small coefficients
113 let a = Matrix3::new(1.0e-20, 0.0, 0.0,
114 0.0, 1.0e-20, 0.0,
115 0.0, 0.0, 1.0e-20);
116 let expected_inverse = Matrix3::new(1.0e20, 0.0, 0.0,
117 0.0, 1.0e20, 0.0,
118 0.0, 0.0, 1.0e20);
119 let a_inv = a.try_inverse().expect("Matrix should be invertible");
120
121 assert_relative_eq!(a_inv, expected_inverse);
122 }
123
124 #[test]
125 #[rustfmt::skip]
126 fn matrix5_try_inverse_scaled_identity() {
127 // A perfectly invertible matrix with
128 // very small coefficients
129 let a = Matrix5::new(1.0e-20, 0.0, 0.0, 0.0, 0.0,
130 0.0, 1.0e-20, 0.0, 0.0, 0.0,
131 0.0, 0.0, 1.0e-20, 0.0, 0.0,
132 0.0, 0.0, 0.0, 1.0e-20, 0.0,
133 0.0, 0.0, 0.0, 0.0, 1.0e-20);
134 let expected_inverse = Matrix5::new(1.0e+20, 0.0, 0.0, 0.0, 0.0,
135 0.0, 1.0e+20, 0.0, 0.0, 0.0,
136 0.0, 0.0, 1.0e+20, 0.0, 0.0,
137 0.0, 0.0, 0.0, 1.0e+20, 0.0,
138 0.0, 0.0, 0.0, 0.0, 1.0e+20);
139 let a_inv = a.try_inverse().expect("Matrix should be invertible");
140
141 assert_relative_eq!(a_inv, expected_inverse);
142 }