it.grypho.scala.leonardo.matrix
Members list
Type members
Classlikes
Determinant of a matrix: det(A) -- a SCALAR result.
Determinant of a matrix: det(A) -- a SCALAR result.
Deliberately does NOT extend _MatrixOperation (and isMatrixShaped must not match it), so 2 * det(A) builds a scalar Product, not a matrix scale -- the same design as _MatrixIndex.
eval reduces the operand: a dense value uses the O(n^3) _MatrixValue.determinant kernel; a symbolic square matrix expands by cofactors (capped at MaxSymbolicDim); non-square or over-cap operands stay symbolic.
Value parameters
- m
-
the matrix expression whose determinant to compute
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _Expressionclass Objecttrait Matchableclass AnyShow all
Identity matrix: eye(n) -- an n x n matrix with 1s on the diagonal.
Identity matrix: eye(n) -- an n x n matrix with 1s on the diagonal.
Extends _MatrixOperation so isMatrixShaped picks it up and the parser routes eye(3) + M to MatSum rather than Sum. Non-integer, non-positive, or over-MaxDenseDim arguments stay symbolic.
Value parameters
- n
-
the dimension expression (must reduce to a positive integer at most
MaxDenseDim)
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _MatrixOperationtrait _Expressionclass Objecttrait Matchableclass AnyShow all
Matrix inverse: inv(A).
Matrix inverse: inv(A).
Result is a matrix, so this extends _MatrixOperation and isMatrixShaped matches it. eval reduces the operand: a dense value uses the Gauss-Jordan _MatrixValue.inverse kernel (None -> singular or non-square -> stays symbolic, like x/0 in scalar division); a symbolic square matrix builds adjugate/det (capped at MaxSymbolicDim) and reduces element-wise.
Value parameters
- m
-
the matrix expression to invert
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _MatrixOperationtrait _Expressionclass Objecttrait Matchableclass AnyShow all
Matrix product: (A * B)_ij = sum_k A_ik * B_kj, for A: r x n and B: n x c.
Matrix product: (A * B)_ij = sum_k A_ik * B_kj, for A: r x n and B: n x c.
A _Number operand scales instead of multiplying -- the parser builds MatProduct(M, y) for M * y with a non-literal y, because y may be either a scalar or a matrix at eval time (variable bound to _MatrixValue). Both cases are resolved by reduceProduct at eval time.
Value parameters
- a
-
left operand (matrix or scalar)
- b
-
right operand (matrix or scalar)
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _MatrixOperationtrait _Expressionclass Objecttrait Matchableclass AnyShow all
Scalar multiple: (k * A)_ij = k * A_ij, where k is any scalar expression.
Scalar multiple: (k * A)_ij = k * A_ij, where k is any scalar expression.
When k turns out to be matrix-valued at eval time (a variable bound to a _MatrixValue, or a scalar Product that reduced to one), the node is really a matrix product k * m and is reduced as such via reduceProduct, preserving operand order (matrix products are not commutative in general).
Value parameters
- k
-
the scalar multiplier
- m
-
the matrix operand
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _MatrixOperationtrait _Expressionclass Objecttrait Matchableclass AnyShow all
Element-wise matrix addition: (A + B)_ij = A_ij + B_ij.
Element-wise matrix addition: (A + B)_ij = A_ij + B_ij.
Marked _ElementWise because addition is linear: d/dx (A + B) = dA/dx + dB/dx. MatProduct and MatScale are NOT marked -- they need product rules.
Value parameters
- a
-
left matrix operand
- b
-
right matrix operand
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _ElementWisetrait _MatrixOperationtrait _Expressionclass Objecttrait Matchableclass AnyShow all
Matrix transpose: (A^T)_ij = A_ji.
Matrix transpose: (A^T)_ij = A_ji.
Marked _ElementWise because transposition is linear and element-independent: d/dx (A^T) = (dA/dx)^T.
Value parameters
- m
-
the matrix to transpose
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _ElementWisetrait _MatrixOperationtrait _Expressionclass Objecttrait Matchableclass AnyShow all
Zero matrix: zeros(rows, cols) -- a rows x cols matrix of zeros.
Zero matrix: zeros(rows, cols) -- a rows x cols matrix of zeros.
zeros(n) in the grammar is shorthand for zeros(n, n) (square zero matrix); the parser handles the one-argument form. Non-integer, non-positive, or over-MaxDenseDim arguments stay symbolic.
Value parameters
- nCols
-
number-of-columns expression
- nRows
-
number-of-rows expression
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _MatrixOperationtrait _Expressionclass Objecttrait Matchableclass AnyShow all
Eigenvalue/eigenvector decomposition: eig(A) -- returns [[V, D]] where A * V = V * D.
Eigenvalue/eigenvector decomposition: eig(A) -- returns [[V, D]] where A * V = V * D.
V columns are right eigenvectors; D is diagonal with the eigenvalues. Evaluates when A reduces to a dense square _MatrixValue; stays symbolic for non-convergent or defective-detected cases (when the eigenvector matrix V fails isJordanInvertible).
The result is Left(_Matrix(1, 2, ...)) because a matrix of matrices is not a _Value; individual components are accessible via at(result, 1, k).
Value parameters
- m
-
the square matrix expression to decompose
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _Expressionclass Objecttrait Matchableclass AnyShow all
Eigenvalue decomposition: eigen(A) -- returns [[lambda_1, lambda_2, ..., lambda_n]].
Eigenvalue decomposition: eigen(A) -- returns [[lambda_1, lambda_2, ..., lambda_n]].
Eigenvalues are _Number for real results and _Complex for complex conjugate pairs. Evaluates when A reduces to a square dense _MatrixValue via the QR iteration kernel; stays symbolic for non-square operands or when the iteration does not converge.
Does NOT extend _MatrixOperation because the result is not a matrix of Doubles (eigenvalues can be complex): it stays as Left(_Matrix(1, n, ...)) rather than collapsing to a single _MatrixValue.
Value parameters
- m
-
the square matrix expression whose eigenvalues to compute
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _Expressionclass Objecttrait Matchableclass AnyShow all
Jordan decomposition: jordan(A) -- returns [[P, J]] where A = P * J * P^(-1).
Jordan decomposition: jordan(A) -- returns [[P, J]] where A = P * J * P^(-1).
For diagonalizable matrices J is diagonal (identical to D in eig(A)); P = V. Non-diagonalizable matrices (defective / repeated eigenvalues where V is singular, detected via isJordanInvertible) stay symbolic -- the test is conservative but safe.
The result is Left(_Matrix(1, 2, ...)) because a matrix of matrices is not a _Value.
Value parameters
- m
-
the square matrix expression to decompose
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _Expressionclass Objecttrait Matchableclass AnyShow all
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _Expressionclass Objecttrait Matchableclass AnyShow all
A symbolic matrix whose elements are arbitrary expressions.
A symbolic matrix whose elements are arbitrary expressions.
This is the AST-side matrix; the fully-reduced counterpart is _MatrixValue (a dense Array[Double] in core). Elements may be numbers, variables, functions, functionals, or any other _Expression.
eval reduces every element: when all of them fold to _Number values the matrix collapses to a single _MatrixValue; otherwise it stays symbolic with each element as far reduced as it goes (the dual-evaluation contract, applied element-wise).
Marked _ElementWise: derive/simplify/expand/integrate distribute over the elements (e.g. d/dx [a_ij] = [d(a_ij)/dx]), which is valid because the matrix is a plain container with no coupling between cells.
children / rebuild expose the elements to generic traversals, so substitute and dependsOn work through matrix elements without matrix-specific cases.
Value parameters
- cols
-
number of columns (must be positive)
- elems
-
row-major element vector; must have exactly
rows * colsentries - rows
-
number of rows (must be positive)
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _MatrixShapedtrait _ElementWisetrait _Expressionclass Objecttrait Matchableclass AnyShow all
Matrix exponential: expm(A).
Matrix exponential: expm(A).
Evaluates when A reduces to a square dense _MatrixValue, via the core._MatrixValue.expm kernel (scaling and squaring with a degree-13 Padé approximant); stays symbolic for a non-square or symbolic operand.
Extends _MatrixOperation, unlike the decompositions beside it, because its result is a single matrix rather than a row of them -- so isMatrixShaped picks it up and expm(A) * B dispatches as a matrix product rather than a scalar one.
Distinct from A^n: that is repeated multiplication by binary exponentiation, this is the exponential series, and the two share no machinery.
Value parameters
- m
-
the matrix expression to exponentiate
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _MatrixOperationtrait _Expressionclass Objecttrait Matchableclass AnyShow all
Element access: at(A, i, j) returns the element at 1-based row i, column j.
Element access: at(A, i, j) returns the element at 1-based row i, column j.
Does NOT extend _MatrixOperation because the result is a scalar, not a matrix -- isMatrixShaped must not match it or the REPL would dispatch it as a matrix op. When the matrix is still symbolic but the indices are concrete, the element is extracted directly from the _Matrix literal so that at([[x, 2]], 1, 2) -> 2.0 without requiring the whole matrix to be dense.
Value parameters
- col
-
the column index expression (1-based)
- matrix
-
the matrix expression to index into
- row
-
the row index expression (1-based)
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _Expressionclass Objecttrait Matchableclass AnyShow all
Marker trait for matrix-valued AST nodes.
Marker trait for matrix-valued AST nodes.
Evaluation is two-path: when both operands reduce to dense _MatrixValues the dense kernels in _MatrixValue are used (fast path); otherwise, matrix literals combine element-wise via the sumOf/productOf helpers, and the resulting _Matrix collapses to a dense value when every element folded, or stays symbolic otherwise.
Dimension mismatches and non-finite results are domain errors and stay symbolic, exactly like x/0 in scalar.Ratio.
Not every matrix-related node extends _MatrixOperation: Determinant and _MatrixIndex produce scalar results, so they extend _Expression directly to prevent the parser from treating 2 * det(A) as a matrix scale.
Attributes
- Supertypes
- Known subtypes
-
class IdentityMatrixclass Inverseclass MatProductclass MatScaleclass MatSumclass Transposeclass ZeroMatrixclass _MatrixExponentialShow all
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait _Expressionclass Objecttrait Matchableclass AnyShow all
Value members
Concrete methods
Reads a symbolic matrix as a dense Double one.
Reads a symbolic matrix as a dense Double one.
The demotion the iterative decompositions (lu, qr, eigen, eig, jordan) need: they cannot be exact, so an exact operand must degrade rather than stay symbolic and lose the feature. Reads through the widening _Number extractor, so exact and inexact cells alike are accepted.
Value parameters
- m
-
the symbolic matrix
Attributes
- Returns
-
the dense matrix, or
Noneif any cell is not a real number
The cells of m as exact rationals, when every cell is one.
The cells of m as exact rationals, when every cell is one.
All-or-nothing on purpose: a matrix with one inexact entry is an inexact matrix, and running an exact kernel over it would dress float contagion up as an exact answer.
Value parameters
- m
-
the symbolic matrix
Attributes
- Returns
-
the row-major exact cells, or
Noneif any cell is not exact
The exact determinant of a square rational matrix, by Gaussian elimination.
The exact determinant of a square rational matrix, by Gaussian elimination.
Elimination is O(n³) against the cofactor expansion's O(n!), which is what lets this be uncapped. A row swap flips the sign; a column with no usable pivot means a singular matrix, whose determinant is exactly zero — not a failure.
Value parameters
- cells
-
the row-major exact cells
- n
-
the dimension
- policy
-
the reduction policy for the intermediate arithmetic
Attributes
- Returns
-
the exact determinant
The exact inverse of a square rational matrix, by Gauss–Jordan elimination.
The exact inverse of a square rational matrix, by Gauss–Jordan elimination.
Value parameters
- cells
-
the row-major exact cells
- n
-
the dimension
- policy
-
the reduction policy for the intermediate arithmetic
Attributes
- Returns
-
the row-major cells of the inverse, or
Nonewhen the matrix is singular