Skip to main content

Matrix

Struct Matrix 

Source
pub struct Matrix<T> { /* private fields */ }
Expand description

A dense rows × cols matrix stored row-major.

Implementations§

Source§

impl<T> Matrix<T>

Source

pub fn new(rows: usize, cols: usize, data: Vec<T>) -> Matrix<T>

Builds a rows × cols matrix from row-major data. Panics on a length mismatch.

Source

pub fn from_rows(rows: Vec<Vec<T>>) -> Matrix<T>

Builds a matrix from a list of rows. Panics if the rows differ in length.

Source

pub fn rows(&self) -> usize

Returns the number of rows.

Source

pub fn cols(&self) -> usize

Returns the number of columns.

Source

pub fn is_square(&self) -> bool

Returns true if the matrix is square.

Source

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

Returns the entry at (row, col).

Source

pub fn set(&mut self, row: usize, col: usize, value: T)

Sets the entry at (row, col).

Source

pub fn as_slice(&self) -> &[T]

Returns the entries in row-major order.

Source§

impl<T: Clone> Matrix<T>

Source

pub fn filled(value: T, rows: usize, cols: usize) -> Matrix<T>

Builds a rows × cols matrix with every entry a clone of value.

This is the context-carrying constructor: unlike zeros (which needs a context-free Default), it works for rings whose zero depends on a runtime context (ModInt, GfElement) — pass their Ring::zero.

Source

pub fn transpose(&self) -> Matrix<T>

Returns the transpose.

Source§

impl<T: Clone + Default> Matrix<T>

Source

pub fn zeros(rows: usize, cols: usize) -> Matrix<T>

Builds a rows × cols zero matrix.

Source§

impl<T: Ring> Matrix<T>

Source

pub fn zeros_like(sample: &T, rows: usize, cols: usize) -> Matrix<T>

Builds a rows × cols zero matrix, taking the ring’s zero from sample.

The context-carrying counterpart of zeros: use it for component rings whose zero depends on a runtime context (ModInt, GfElement).

Source

pub fn identity_like(sample: &T, n: usize) -> Matrix<T>

Builds the n × n identity, taking the ring’s zero/one from sample.

The context-carrying counterpart of the concrete Matrix::<Int>::identity / Matrix::<Rational>::identity.

Source§

impl<T: Ring> Matrix<T>

Source

pub fn add(&self, rhs: &Matrix<T>) -> Matrix<T>

Returns self + rhs. Panics on a shape mismatch.

Source

pub fn sub(&self, rhs: &Matrix<T>) -> Matrix<T>

Returns self - rhs. Panics on a shape mismatch.

Source

pub fn mul(&self, rhs: &Matrix<T>) -> Matrix<T>

Returns the matrix product self · rhs. Panics if the inner dimensions disagree.

For component rings whose arithmetic is exact and associative (Ring::REASSOCIATIVE) the product is computed by the recursive Strassen–Winograd algorithm above a size threshold (7 recursive block products instead of 8), falling back to the naive triple loop below the threshold and for small or awkward shapes. The visible result is always bit-identical to the naive product.

Source

pub fn scalar_mul(&self, scalar: &T) -> Matrix<T>

Returns self · scalar.

Source§

impl<T: Ring> Matrix<T>

Source

pub fn neg(&self) -> Matrix<T>

Returns -self.

Source§

impl Matrix<Int>

Source

pub fn identity(n: usize) -> Matrix<Int>

Returns the n × n integer identity matrix.

Source

pub fn determinant(&self) -> Int

Returns the exact determinant via the fraction-free Bareiss algorithm. Panics if the matrix is not square.

Source§

impl Matrix<Rational>

Source

pub fn identity(n: usize) -> Matrix<Rational>

Returns the n × n rational identity matrix.

Source

pub fn determinant(&self) -> Rational

Returns the exact determinant. Panics if the matrix is not square.

Source

pub fn rank(&self) -> usize

Returns the rank (number of linearly independent rows).

Source

pub fn inverse(&self) -> Option<Matrix<Rational>>

Returns the inverse, or None if the matrix is singular. Panics if not square.

Source

pub fn solve(&self, b: &[Rational]) -> Option<Vec<Rational>>

Solves self · x = b, returning x, or None if there is no unique solution. Panics if self is not square or b has the wrong length.

Source§

impl Matrix<Rational>

Exact eigenvalues of a rational matrix, as real algebraic numbers.

These methods compose the two exact building blocks the crate already provides: the division-free RingMatrix::charpoly (Samuelson–Berkowitz) gives the monic characteristic polynomial det(x·I − A) over ℚ, and Algebraic::real_roots_of isolates its real roots exactly (Sturm sequences + bisection). Every returned eigenvalue is therefore an exact Algebraic — compared and combined by its true real value, never a float approximation.

§Real eigenvalues only

Algebraic models real algebraic numbers, so only the real eigenvalues are returned; non-real (complex-conjugate) eigenvalues are silently omitted. A matrix whose spectrum is entirely complex (e.g. the rotation [[0,−1],[1,0]], eigenvalues ±i) yields an empty list without error.

§Cost

The characteristic polynomial costs O(n⁴) rational multiplications (Samuelson–Berkowitz), and real-root isolation runs Sturm sequences and polynomial GCDs by the subresultant PRS on the degree-n char poly. Both are exact but grow quickly with n; this is intended for modest matrix sizes.

Source

pub fn characteristic_polynomial(&self) -> Poly<Rational>

Returns the characteristic polynomial det(x·I − A) over ℚ, monic and in low-to-high coefficient order (coeffs()[i] is the coefficient of xⁱ).

Computed division-free via RingMatrix::charpoly. Panics if the matrix is not square (or is 0×0, which has no ring context).

Source

pub fn real_eigenvalues(&self) -> Vec<Algebraic>

Returns the distinct real eigenvalues as exact algebraic numbers, in increasing order.

Non-real (complex) eigenvalues are not returned — see the type-level note. A repeated eigenvalue appears once; use real_eigenvalues_with_multiplicity for algebraic multiplicities.

Panics if the matrix is not square (or is 0×0).

Source

pub fn real_eigenvalues_with_multiplicity(&self) -> Vec<(Algebraic, usize)>

Returns the distinct real eigenvalues paired with their algebraic multiplicity (their multiplicity as roots of the characteristic polynomial), in increasing order of eigenvalue.

The multiplicities come from a squarefree decomposition of the char poly (Yun’s algorithm — repeated GCDs with the derivative): the roots of the multiplicity-k squarefree factor are exactly the eigenvalues of algebraic multiplicity k. Non-real eigenvalues are omitted, so the returned multiplicities need not sum to n.

Panics if the matrix is not square (or is 0×0).

Trait Implementations§

Source§

impl<T: Ring> Add for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Matrix<T>) -> Matrix<T>

Performs the + operation. Read more
Source§

impl<T: Ring> Add<&Matrix<T>> for &Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Matrix<T>) -> Matrix<T>

Performs the + operation. Read more
Source§

impl<T: Clone> Clone for Matrix<T>

Source§

fn clone(&self) -> Matrix<T>

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<T: Debug> Debug for Matrix<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Display> Display for Matrix<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Eq> Eq for Matrix<T>

Source§

impl<T: Field> FieldMatrix<T> for Matrix<T>

Source§

fn determinant(&self) -> T

The determinant, computed as (∏ pivots) · (−1)^{#row swaps} after forward elimination to upper-triangular form. A wholly-zero pivot column means the matrix is singular, so the determinant is the field’s zero. Panics if the matrix is not square (or is 0 × 0, which has no sample element from which to take the ring’s one).
Source§

fn inverse(&self) -> Option<Matrix<T>>

The inverse via Gauss–Jordan elimination on the augmented [A | I], or None if A is singular. Panics if the matrix is not square.
Source§

fn solve(&self, b: &[T]) -> Option<Vec<T>>

Solves self · x = b, returning x, or None when there is no unique solution (a zero pivot column ⇒ singular). Panics if self is not square or b has the wrong length.
Source§

fn rank(&self) -> usize

The rank: the number of nonzero pivots produced by row-reducing to row echelon form (works for any shape).
Source§

impl<T: Hash> Hash for Matrix<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Ring> Mul for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Matrix<T>) -> Matrix<T>

Performs the * operation. Read more
Source§

impl<T: Ring> Mul<&Matrix<T>> for &Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Matrix<T>) -> Matrix<T>

Performs the * operation. Read more
Source§

impl<T: PartialEq> PartialEq for Matrix<T>

Source§

fn eq(&self, other: &Matrix<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Ring> RingMatrix<T> for Matrix<T>

Source§

fn charpoly(&self) -> Vec<T>

The characteristic polynomial det(x·I − self) as coefficients from the constant term up: charpoly()[i] is the coefficient of xⁱ, so charpoly()[0] is (−1)ⁿ·det and the leading coefficient is one. Read more
Source§

fn det(&self) -> T

The determinant of self, computed division-free. Read more
Source§

impl<T: PartialEq> StructuralPartialEq for Matrix<T>

Source§

impl<T: Ring> Sub for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Matrix<T>) -> Matrix<T>

Performs the - operation. Read more
Source§

impl<T: Ring> Sub<&Matrix<T>> for &Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Matrix<T>) -> Matrix<T>

Performs the - operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Matrix<T>

§

impl<T> RefUnwindSafe for Matrix<T>
where T: RefUnwindSafe,

§

impl<T> Send for Matrix<T>
where T: Send,

§

impl<T> Sync for Matrix<T>
where T: Sync,

§

impl<T> Unpin for Matrix<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Matrix<T>

§

impl<T> UnwindSafe for Matrix<T>
where T: UnwindSafe,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.