pub struct Matrix<T> { /* private fields */ }Expand description
A dense rows × cols matrix stored row-major.
Implementations§
Source§impl<T> Matrix<T>
impl<T> Matrix<T>
Source§impl<T: Clone> Matrix<T>
impl<T: Clone> Matrix<T>
Sourcepub fn filled(value: T, rows: usize, cols: usize) -> Matrix<T>
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§impl<T: Ring> Matrix<T>
impl<T: Ring> Matrix<T>
Sourcepub fn zeros_like(sample: &T, rows: usize, cols: usize) -> Matrix<T>
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).
Sourcepub fn identity_like(sample: &T, n: usize) -> Matrix<T>
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>
impl<T: Ring> Matrix<T>
Sourcepub fn add(&self, rhs: &Matrix<T>) -> Matrix<T>
pub fn add(&self, rhs: &Matrix<T>) -> Matrix<T>
Returns self + rhs. Panics on a shape mismatch.
Sourcepub fn sub(&self, rhs: &Matrix<T>) -> Matrix<T>
pub fn sub(&self, rhs: &Matrix<T>) -> Matrix<T>
Returns self - rhs. Panics on a shape mismatch.
Sourcepub fn mul(&self, rhs: &Matrix<T>) -> Matrix<T>
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.
Sourcepub fn scalar_mul(&self, scalar: &T) -> Matrix<T>
pub fn scalar_mul(&self, scalar: &T) -> Matrix<T>
Returns self · scalar.
Source§impl Matrix<Rational>
impl Matrix<Rational>
Source§impl Matrix<Rational>
Exact eigenvalues of a rational matrix, as real algebraic numbers.
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.
Sourcepub fn characteristic_polynomial(&self) -> Poly<Rational>
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).
Sourcepub fn real_eigenvalues(&self) -> Vec<Algebraic>
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).
Sourcepub fn real_eigenvalues_with_multiplicity(&self) -> Vec<(Algebraic, usize)>
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§
impl<T: Eq> Eq for Matrix<T>
Source§impl<T: Field> FieldMatrix<T> for Matrix<T>
impl<T: Field> FieldMatrix<T> for Matrix<T>
Source§fn determinant(&self) -> T
fn determinant(&self) -> T
(∏ 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>>
fn inverse(&self) -> Option<Matrix<T>>
[A | I], or
None if A is singular. Panics if the matrix is not square.