Skip to main content

SquareMatrix

Struct SquareMatrix 

Source
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

Source

pub fn new(n: usize) -> Self

Allocate an n×n matrix initialised to all zeros.

Source

pub fn n(&self) -> usize

The matrix order n (number of rows = number of columns).

Source

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.

Source

pub fn set(&mut self, i: usize, j: usize, v: f64)

Set the element in row i, column j to v (0-based indices < n).

§Out of range is ignored, and never panics

See get for why this does not panic. A write with i >= n or j >= n is discarded; the matrix is left unchanged.

Source

pub fn add(&mut self, i: usize, j: usize, v: f64)

Add v to the element in row i, column j (0-based indices < n).

§Out of range is ignored, and never panics

See get. An accumulation with i >= n or j >= n is discarded.

Source

pub fn fill_zero(&mut self)

Reset every entry to zero, keeping the same order n.

Source

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.

Source

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>&).

Source

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.

Trait Implementations§

Source§

impl Clone for SquareMatrix

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SquareMatrix

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.