Function russell_lab::solve_lin_sys
source · [−]Expand description
Solves a general linear system (real numbers)
For a general matrix a
(square, symmetric, non-symmetric, dense,
sparse), find x
such that:
a ⋅ x = b
(m,m) (m) (m)
However, the right-hand-side will hold the solution:
b := a⁻¹⋅b == x
The solution is obtained via LU decomposition using Lapack dgesv routine.
Note
- The matrix
a
will be modified - The right-hand-side
b
will contain the solutionx
use russell_lab::{solve_lin_sys, Matrix, Vector, StrError};
fn main() -> Result<(), StrError> {
// set matrix and right-hand side
let mut a = Matrix::from(&[
[1.0, 3.0, -2.0],
[3.0, 5.0, 6.0],
[2.0, 4.0, 3.0],
]);
let mut b = Vector::from(&[5.0, 7.0, 8.0]);
// solve linear system b := a⁻¹⋅b
solve_lin_sys(&mut b, &mut a)?;
// check
let x_correct = "┌ ┐\n\
│ -15.000 │\n\
│ 8.000 │\n\
│ 2.000 │\n\
└ ┘";
assert_eq!(format!("{:.3}", b), x_correct);
Ok(())
}