pub struct SquareMatrix { /* private fields */ }Expand description
Row-major n×n dense matrix of f64. Maps to Foam::scalarSquareMatrix.
LU decomposition uses Crout’s algorithm with scaled partial pivoting,
matching Foam::LUDecompose(scalarSquareMatrix&, labelList&).
Implementations§
Source§impl SquareMatrix
impl SquareMatrix
Sourcepub fn get(&self, i: usize, j: usize) -> f64
pub fn get(&self, i: usize, j: usize) -> f64
Element in row i, column j (both 0-based, must be < n).
§Out of range reads NaN, and never panics
i >= n or j >= n is a caller bug either way. Returning NaN rather
than panicking is what lets this type be lifted verbatim into petir,
which targets bare metal – there a panic is not a stack trace and a
non-zero exit, it is the end of the program. NaN propagates through
every later operation and compares unequal to everything, so the bug
still surfaces, just downstream rather than at the read.
Sourcepub fn lu_decompose(&mut self) -> Vec<usize>
pub fn lu_decompose(&mut self) -> Vec<usize>
In-place LU decomposition with scaled partial pivoting.
After the call, self holds the combined LU factors (lower strictly
triangular, upper including diagonal). Returns the pivot-row indices.
Sourcepub fn lu_back_substitute(&self, pivot: &[usize], b: &mut Vec<f64>)
pub fn lu_back_substitute(&self, pivot: &[usize], b: &mut Vec<f64>)
Solve LU·x = b in-place (b is overwritten with the solution).
pivot must hold n entries and b must hold n values. A shorter
one used to panic; it now returns early with b partly updated, so
that this type can be lifted into petir, which targets bare metal
where a panic ends the program. See get.
Must be called after lu_decompose. Matches
Foam::LUBacksubstitute(scalarSquareMatrix&, labelList&, List<scalar>&).
Sourcepub fn solve(&self, rhs: &[f64]) -> Result<Vec<f64>, MatrixError>
pub fn solve(&self, rhs: &[f64]) -> Result<Vec<f64>, MatrixError>
Convenience: decompose a copy and solve A·x = b.
Returns Err(MatrixError::Singular) if the matrix has an exactly-zero
pivot column (the LU sentinel f64::EPSILON was written to the diagonal).
Ill-conditioned but non-singular matrices are not detected; for those,
check the residual ‖Ax − b‖ manually.