Overview
Matrix operations compute fundamental properties and transformations of matrices, including inverse, pseudo-inverse, norms, determinant, trace, condition number, and rank.inv
Compute the inverse of a matrix. Finds matrix A^(-1) such that A × A^(-1) = I.Signature
Parameters
Square matrix of shape (N, N). Must be non-singular.
Returns
Inverse matrix of shape (N, N)Algorithm
LU decomposition with partial pivoting:- Factor A = P × L × U
- Solve A × X = I for each column of X
Properties
- A × inv(A) = I
- inv(inv(A)) = A
- inv(A×B) = inv(B) × inv(A)
- det(inv(A)) = 1 / det(A)
Requirements
- Matrix must be square
- Matrix must be non-singular (det(A) ≠ 0)
Errors
- ShapeError: If matrix is not square or not 2D
- DTypeError: If input has string dtype
- DataValidationError: If matrix is singular or contains non-finite values
pinv
Compute the Moore-Penrose pseudo-inverse. Generalization of matrix inverse for non-square or singular matrices.Signature
Parameters
Input matrix of shape (M, N). Can be any shape.
Cutoff for small singular values. Default: machine epsilon × max(M, N).
Singular values smaller than rcond × largest_singular_value are treated as zero.
Returns
Pseudo-inverse of shape (N, M)Algorithm
Using SVD:- A = U × Σ × V^T
- pinv(A) = V × Σ^+ × U^T
Properties
- A × pinv(A) × A = A
- pinv(A) × A × pinv(A) = pinv(A)
- For full rank square matrix: pinv(A) = inv(A)
- For overdetermined system: pinv(A) gives least squares solution
- For underdetermined system: pinv(A) gives minimum norm solution
Use Cases
Errors
- ShapeError: If input is not 2D matrix
- DTypeError: If input has string dtype
- InvalidParameterError: If rcond is negative or non-finite
- DataValidationError: If input contains non-finite values (NaN, Infinity)
det
Compute the determinant of a matrix. Scalar value representing signed volume of the parallelepiped formed by matrix rows.Signature
Parameters
Square matrix of shape (N, N).
Returns
Determinant value (scalar)Algorithm
LU decomposition with partial pivoting:- det(A) = pivSign × product(diag(U))
- Time Complexity: O(N³)
- Space Complexity: O(N²)
Properties
- det(A) = 0 if and only if A is singular (non-invertible)
- det(A×B) = det(A) × det(B)
- det(A^T) = det(A)
- det(c×A) = c^N × det(A) for scalar c and N×N matrix A
- det(I) = 1 for identity matrix
- det(inv(A)) = 1 / det(A)
Errors
- ShapeError: If input is not a 2D square matrix
- DTypeError: If input has string dtype
- DataValidationError: If input contains non-finite values (NaN, Infinity)
slogdet
Compute sign and natural logarithm of the determinant. More numerically stable thandet() for large matrices or matrices with very large/small determinants.
Signature
Parameters
Square matrix of shape (N, N).
Returns
Tuple of two 0D tensors[sign, logdet]:
- sign: +1, -1, or 0
- logdet: Natural log of |det(A)|, -Infinity if singular
Mathematical Relation
det(A) = sign × exp(logdet)Advantages over det()
- Avoids overflow for large determinants
- Avoids underflow for small determinants
- More numerically stable for ill-conditioned matrices
Errors
- ShapeError: If input is not a 2D square matrix
- DTypeError: If input has string dtype
- DataValidationError: If input contains non-finite values (NaN, Infinity)
trace
Compute the trace of a matrix. Sum of diagonal elements. Supports offset diagonals and batched operations.Signature
Parameters
Input matrix (at least 2D).
Integer offset from main diagonal:
- 0: main diagonal
- Greater than 0: upper diagonal (k-th diagonal above main)
- Less than 0: lower diagonal (k-th diagonal below main)
First axis to take the diagonal from.
Second axis to take the diagonal from.
Returns
Trace values as tensor (one per slice if input is batched)Algorithm
Direct summation:- Time Complexity: O(min(M, N)) where M×N is matrix size
- Space Complexity: O(1)
Properties
- trace(A) = sum of eigenvalues (for square matrices)
- trace(A×B) = trace(B×A) (cyclic property)
- trace(A + B) = trace(A) + trace(B) (linearity)
- trace(c×A) = c × trace(A) for scalar c
- trace(A^T) = trace(A)
Errors
- ShapeError: If input is not at least 2D
- InvalidParameterError: If axis values are invalid/identical or offset is non-integer
- DTypeError: If input has string dtype
- DataValidationError: If input contains non-finite values (NaN, Infinity)
norm
Matrix or vector norm. Computes various matrix and vector norms measuring magnitude or size.Signature
Parameters
Input array (vector or matrix).
Order of the norm:For vectors:
- undefined or 2: L2 norm (Euclidean)
- 1: L1 norm (Manhattan)
- Infinity: Max norm
- -Infinity: Min norm
- 0: L0 “norm” (number of non-zero elements)
- p: Lp norm (p > 0)
- ‘fro’: Frobenius norm
- ‘nuc’: Nuclear norm (sum of singular values)
- 1: Max column sum
- -1: Min column sum
- 2: Largest singular value (spectral norm)
- -2: Smallest singular value
- Infinity: Max row sum
- -Infinity: Min row sum
Axis or axes along which to compute norm.
Keep reduced dimensions.
Returns
Norm value (scalar or tensor depending on axis)Common Norm Types
Vector Norms
Matrix Norms
Errors
- DTypeError: If input has string dtype
- DataValidationError: If input contains non-finite values (NaN, Infinity)
- InvalidParameterError: If norm order or axis values are invalid
- ShapeError: If axis configuration is incompatible with input
cond
Condition number of a matrix. Measures how sensitive the solution of A×x=b is to changes in b. Large condition number indicates ill-conditioned matrix.Signature
Parameters
Input matrix of shape (M, N).
Norm order. Only 2-norm and Frobenius norm are supported.
Returns
Condition number (≥ 1, infinity for singular matrices)Formula
cond(A) = ||A|| × ||A^(-1)|| For 2-norm: cond(A) = σ_max / σ_min (ratio of largest to smallest singular value)Interpretation
- cond(A) = 1: Perfectly conditioned (e.g., identity matrix)
- cond(A) < 100: Well-conditioned
- cond(A) > 10^6: Ill-conditioned (loss of precision)
- cond(A) = ∞: Singular matrix
Errors
- ShapeError: If input is not a 2D matrix
- DTypeError: If input has string dtype
- DataValidationError: If input contains non-finite values (NaN, Infinity)
- InvalidParameterError: If p is unsupported
matrixRank
Compute the rank of a matrix. Number of linearly independent rows/columns. Uses SVD to count singular values above a threshold.Signature
Parameters
Input matrix of shape (M, N).
Threshold for small singular values.
Default: max(M,N) × largest_singular_value × machine_epsilon.
Singular values > tol are counted as non-zero.
Returns
Rank (integer between 0 and min(M, N))Algorithm
SVD-based rank computation:- Compute SVD: A = U × Σ × V^T
- Count singular values > threshold
- Time Complexity: O(min(M,N) × M × N)
- Space Complexity: O(M×N)
Properties
- 0 ≤ rank(A) ≤ min(M, N)
- rank(A) = rank(A^T)
- rank(A) = number of non-zero singular values
- Full rank: rank(A) = min(M, N)
- Rank deficient: rank(A) < min(M, N)
Use Cases
Errors
- ShapeError: If input is not a 2D matrix
- DTypeError: If input has string dtype
- InvalidParameterError: If tol is negative or non-finite
- DataValidationError: If input contains non-finite values (NaN, Infinity)