MathCore’sDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Nonanti/mathcore/llms.txt
Use this file to discover all available pages before exploring further.
matrix module bridges symbolic computation and high-performance numerical linear algebra. SymbolicMatrix stores matrix entries as symbolic Expr trees, so determinants, traces, and arithmetic return expressions you can further differentiate or simplify — while LinearAlgebra delegates the heavy numerical work (eigenvalues, decompositions, linear solves) to nalgebra, a battle-tested Rust linear algebra library that can leverage BLAS when available.
Imports
SymbolicMatrix
SymbolicMatrix is a dynamically sized matrix whose elements are MathCore Expr values. Every entry can be a numeric constant, a symbol, or an arbitrarily complex expression.
Construction
SymbolicMatrix::new(rows, cols)
Creates an all-zero matrix of the given dimensions.
SymbolicMatrix::identity(size)
Creates an n × n identity matrix.
SymbolicMatrix::from_vec(data: Vec<Vec<f64>>) -> Result<SymbolicMatrix, MathError>
Constructs a symbolic matrix from a nested Vec<f64>. Each f64 value is lifted into an Expr::Number. Returns MathError::InvalidOperation if the input is empty or rows have inconsistent lengths.
Element Access
get(row, col) -> Option<&Expr>
Returns a reference to the element at (row, col), or None if the index is out of bounds.
set(row, col, value: Expr) -> Result<(), MathError>
Writes a new symbolic value at the given position.
Arithmetic
add(&other) -> Result<SymbolicMatrix, MathError>
Returns the element-wise sum. Both matrices must have identical dimensions.
multiply(&other) -> Result<SymbolicMatrix, MathError>
Returns the matrix product. The number of columns in self must equal the number of rows in other.
Arithmetic results are symbolic — each entry is an
Expr tree representing the formula, not a pre-evaluated number. This lets you compose operations and differentiate the result later.transpose() -> SymbolicMatrix
Returns the transpose (infallible — no allocation can fail for a swap).
Properties
determinant() -> Result<Expr, MathError>
Computes the determinant symbolically via cofactor expansion. Returns an Expr that you can evaluate or simplify. Requires a square matrix.
- 1×1: returns the single element directly.
- 2×2: returns
a*d - b*c. - n×n: recursive cofactor expansion along the first row.
trace() -> Result<Expr, MathError>
Returns the sum of diagonal elements as a symbolic expression. Requires a square matrix.
LinearAlgebra
LinearAlgebra is a unit struct (no state) that exposes static methods for numerical computations, all operating on nalgebra’s DMatrix<f64> and DVector<f64> types.
Solving Linear Systems
LinearAlgebra::solve_system(a: &DMatrix<f64>, b: &DVector<f64>) -> Result<DVector<f64>, MathError>
Solves the linear system Ax = b using nalgebra’s LU decomposition. Returns MathError::InvalidOperation if the system has no unique solution (singular or underdetermined matrix).
Eigenvalues
LinearAlgebra::eigenvalues(matrix: &DMatrix<f64>) -> Result<Vec<Complex64>, MathError>
Computes complex eigenvalues via the Schur decomposition. Returns MathError::InvalidOperation for non-square matrices.
QR Decomposition
LinearAlgebra::qr_decomposition(matrix: &DMatrix<f64>) -> (DMatrix<f64>, DMatrix<f64>)
Returns the tuple (Q, R) from the QR decomposition. Q is orthogonal and R is upper triangular.
Singular Value Decomposition (SVD)
LinearAlgebra::svd(matrix: &DMatrix<f64>) -> Result<(DMatrix<f64>, DVector<f64>, DMatrix<f64>), MathError>
Returns (U, singular_values, Vᵀ). Fails only if nalgebra’s SVD backend fails to converge (extremely rare).
Matrix Rank
LinearAlgebra::rank(matrix: &DMatrix<f64>, tolerance: f64) -> usize
Counts the number of singular values above tolerance. A good default tolerance for f64 matrices is 1e-10.
Matrix Norm
LinearAlgebra::norm(matrix: &DMatrix<f64>, norm_type: NormType) -> f64
Supported norm types from the NormType enum:
| Variant | Description |
|---|---|
NormType::Frobenius | √(∑ aᵢⱼ²) — element-wise Euclidean norm |
NormType::L1 | Maximum absolute column sum |
NormType::L2 | Largest singular value (spectral norm) |
NormType::LInf | Maximum absolute row sum |
Condition Number
LinearAlgebra::condition_number(matrix: &DMatrix<f64>) -> Result<f64, MathError>
Returns the ratio of the largest to smallest singular value — a measure of numerical stability. Returns f64::INFINITY for singular matrices.
Full Example
nalgebra Dependency
MathCore’sLinearAlgebra methods operate on nalgebra types directly. Make sure both crates are in your Cargo.toml:
