Debian Codegraph @

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

raw
1 #![cfg_attr(rustfmt, rustfmt_skip)]
2 #[macro_use]
3 extern crate approx; // for assert_relative_eq
4 extern crate nalgebra as na;
5 use na::{Matrix4, Matrix4x3, Vector4};
6
7 fn main() {
8 let a = Matrix4::new(
9 1.0, 1.0, 2.0, -5.0,
10 2.0, 5.0, -1.0, -9.0,
11 2.0, 1.0, -1.0, 3.0,
12 1.0, 3.0, 2.0, 7.0,
13 );
14 let mut b = Vector4::new(3.0, -3.0, -11.0, -5.0);
15 let decomp = a.lu();
16 let x = decomp.solve(&b).expect("Linear resolution failed.");
17 assert_relative_eq!(a * x, b);
18
19 /*
20 * It is possible to perform the resolution in-place.
21 * This is particularly useful to avoid allocations when
22 * `b` is a `DVector` or a `DMatrix`.
23 */
24 assert!(decomp.solve_mut(&mut b), "Linear resolution failed.");
25 assert_relative_eq!(x, b);
26
27 /*
28 * It is possible to solve multiple systems
29 * simultaneously by using a matrix for `b`.
30 */
31 let b = Matrix4x3::new(
32 3.0, 2.0, 0.0,
33 -3.0, 0.0, 0.0,
34 -11.0, 5.0, -3.0,
35 -5.0, 10.0, 4.0,
36 );
37 let x = decomp.solve(&b).expect("Linear resolution failed.");
38 assert_relative_eq!(a * x, b);
39 }