Skip to main content

SquareMatrix

Struct SquareMatrix 

Source
pub struct SquareMatrix {
    pub entries: Vec<f64>,
    pub dim: usize,
}
Expand description

A square matrix operator represented as a flat row-major array.

Fields§

§entries: Vec<f64>

Row-major entries.

§dim: usize

Dimension (n x n).

Implementations§

Source§

impl SquareMatrix

Source

pub fn new(entries: Vec<f64>, dim: usize) -> Self

Create a new square matrix from row-major entries.

Panics if entries.len() != dim * dim.

Source

pub fn identity(dim: usize) -> Self

The n x n identity matrix.

Source

pub fn zero(dim: usize) -> Self

The n x n zero matrix.

Source

pub fn get(&self, row: usize, col: usize) -> f64

Get entry at (row, col).

Source

pub fn set(&mut self, row: usize, col: usize, val: f64)

Set entry at (row, col).

Source

pub fn add(&self, other: &SquareMatrix) -> SquareMatrix

Matrix addition.

Source

pub fn sub(&self, other: &SquareMatrix) -> SquareMatrix

Matrix subtraction.

Source

pub fn scale(&self, s: f64) -> SquareMatrix

Scalar multiplication.

Source

pub fn mul(&self, other: &SquareMatrix) -> SquareMatrix

Matrix multiplication.

Source

pub fn pow(&self, k: u32) -> SquareMatrix

Compute A^k (matrix exponentiation by repeated squaring).

Source

pub fn operator_norm(&self) -> f64

The operator norm (approximated by the Frobenius norm).

Source

pub fn frobenius_norm(&self) -> f64

The Frobenius norm: ||A||_F = sqrt(sum a_ij^2).

Source

pub fn trace(&self) -> f64

The trace: sum of diagonal entries.

Source

pub fn poly_eval(&self, poly: &Polynomial) -> SquareMatrix

Evaluate a polynomial at a matrix: p(A) = c_0 I + c_1 A + c_2 A^2 + …

Uses Horner’s method for efficiency: p(A) = (… ((c_n A + c_{n-1}) A + c_{n-2}) A + …) + c_0.

Source

pub fn spectral_radius(&self, max_iter: u32) -> f64

Approximate spectral radius: r(A) = lim ||A^n||^{1/n}.

Computed by evaluating ||A^n||^{1/n} for increasing n and returning the value at max_iter.

Source

pub fn resolvent_2x2(&self, lambda: f64) -> Option<SquareMatrix>

Compute the resolvent (lambda I - A)^{-1} for a 2x2 matrix using the explicit inverse formula.

Returns None if the matrix is not 2x2 or the determinant is zero (i.e., lambda is in the spectrum).

Trait Implementations§

Source§

impl Clone for SquareMatrix

Source§

fn clone(&self) -> SquareMatrix

Returns a duplicate of the value. Read more
1.0.0 · 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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.