_MatrixValue

it.grypho.scala.leonardo.core._MatrixValue
See the_MatrixValue companion object
final class _MatrixValue extends _Value

Dense, fully-reduced matrix value: a row-major Array[Double], not _Number nodes.

The concrete counterpart of the symbolic matrix node (matrix._Matrix), exactly as _Number is the concrete counterpart of a scalar expression. Lives in core so Environment can bind matrix values without core depending on any domain.

Immutability: the public factory (_MatrixValue$.apply) takes a defensive copy and the storage is private — kernels build results on arrays they own, skipping the copy.

multiply parallelises over independent row blocks above a work-volume threshold; writes are disjoint and require no locking.

Value parameters

cols

number of columns; must be positive

rows

number of rows; must be positive

Attributes

Companion
object
Graph
Supertypes
trait _Value
trait _Expression
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

Element-wise matrix addition.

Element-wise matrix addition.

Value parameters

that

matrix to add; must have the same rows and cols

Attributes

def apply(i: Int, j: Int): Double

Returns the element at row i, column j (zero-based).

Returns the element at row i, column j (zero-based).

Attributes

override def children: List[_Expression]

Sub-expressions subject to recursive structural traversal. Binder positions (e.g. the differentiation variable) are excluded.

Sub-expressions subject to recursive structural traversal. Binder positions (e.g. the differentiation variable) are excluded.

Attributes

Definition Classes
def determinant: Option[Double]

Determinant via LU decomposition with partial pivoting, O(n³). None when the matrix is non-square; Some(0.0) when singular (zero pivot). Operates on a defensive clone — the value's storage is untouched.

Determinant via LU decomposition with partial pivoting, O(n³). None when the matrix is non-square; Some(0.0) when singular (zero pivot). Operates on a defensive clone — the value's storage is untouched.

Attributes

Returns

Some(det) for square matrices, None for non-square

def display(precision: Int): String

Renders this matrix at precision decimal places for REPL display; mirrors _Number.round behaviour.

Renders this matrix at precision decimal places for REPL display; mirrors _Number.round behaviour.

Value parameters

precision

number of decimal places to show

Attributes

def eigenDecompose: Option[Vector[_Value]]

Eigenvalue decomposition via QR iteration with Wilkinson shifts. Returns the n eigenvalues as _Number (real) or _Complex (conjugate pairs from 2×2 blocks). None for non-square matrices or non-convergence within 300·n steps.

Eigenvalue decomposition via QR iteration with Wilkinson shifts. Returns the n eigenvalues as _Number (real) or _Complex (conjugate pairs from 2×2 blocks). None for non-square matrices or non-convergence within 300·n steps.

Algorithm: shrinking active sub-matrix; deflation when the sub-diagonal drops below tolerance; Wilkinson shift QR step otherwise; 2×2 blocks solved analytically. A tiny perturbation breaks rank deficiency when the shift hits an exact eigenvalue.

Attributes

Returns

Some(eigenvalues) in deflation order, None for non-square or non-convergent

override def equals(other: Any): Boolean

Compares the receiver object (this) with the argument object (that) for equivalence.

Compares the receiver object (this) with the argument object (that) for equivalence.

Any implementation of this method should be an equivalence relation:

  • It is reflexive: for any instance x of type Any, x.equals(x) should return true.
  • It is symmetric: for any instances x and y of type Any, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any instances x, y, and z of type Any if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.

If you override this method, you should verify that your implementation remains an equivalence relation. Additionally, when overriding this method it is usually necessary to override hashCode to ensure that objects which are "equal" (o1.equals(o2) returns true) hash to the same scala.Int. (o1.hashCode.equals(o2.hashCode)).

Value parameters

that

the object to compare against this object for equality.

Attributes

Returns

true if the receiver object is equivalent to the argument; false otherwise.

Definition Classes
Any
override def eval(env: Environment): Either[_Expression, _Value]

Returns Right(this) — a concrete matrix needs no further reduction.

Returns Right(this) — a concrete matrix needs no further reduction.

Attributes

Definition Classes
def expm: Option[_MatrixValue]

Matrix exponential e^A = sum(k >= 0) A^k / k!, by scaling and squaring with a degree-13 Padé approximant.

Matrix exponential e^A = sum(k >= 0) A^k / k!, by scaling and squaring with a degree-13 Padé approximant.

Not the eigen route. V·diag(e^λ)·V⁻¹ is tempting because spectralDecompose already exists, but it needs an eigenbasis and a defective matrix has none — and a defective matrix is ordinary in the problems that want this, since a repeated pole produces one. A Jordan block (λ 1; 0 λ) would come back wrong rather than refused. Scaling and squaring is uniform over both cases, which is why it is the standard method.

The identity e^A = (e^(A/2^s))^(2^s) is what makes it work: A is scaled until its norm is inside the Padé approximant's accurate radius, the approximant is evaluated there, and the result is squared back s times.

Note this is matrix Padé — a fixed rational approximant with tabulated coefficients — and is unrelated to the scalar scalar.padeApproximant, which fits a series.

Exactness follows the decomposition precedent rather than matrix/Exact.scala: the exponential is transcendental, so an exact operand demotes to Double exactly as lu/qr/eigen do — "demote an exact operand rather than lose the operation".

Attributes

Returns

Some(e^A) for a square matrix, None when non-square or non-finite

def guarded(orElse: _Expression): Either[_Expression, _Value]

Lifts this matrix into an Either for use in eval: Right(this) when all elements are finite; Left(orElse) otherwise (non-finite is a domain error — stays symbolic like x / 0). Shared by scalar and matrix packages without requiring cross-imports.

Lifts this matrix into an Either for use in eval: Right(this) when all elements are finite; Left(orElse) otherwise (non-finite is a domain error — stays symbolic like x / 0). Shared by scalar and matrix packages without requiring cross-imports.

Value parameters

orElse

the symbolic fallback expression

Attributes

def inverse: Option[_MatrixValue]

Inverse via Gauss–Jordan elimination with partial pivoting, O(n³). None when the matrix is non-square or singular — the caller stays symbolic, the same "domain error stays symbolic" contract as x / 0 in scalar.Ratio.

Inverse via Gauss–Jordan elimination with partial pivoting, O(n³). None when the matrix is non-square or singular — the caller stays symbolic, the same "domain error stays symbolic" contract as x / 0 in scalar.Ratio.

Attributes

Returns

Some(A⁻¹) for invertible square matrices, None otherwise

def isFinite: Boolean

Returns true when every element is finite (not NaN, not infinite).

Returns true when every element is finite (not NaN, not infinite).

Attributes

Kronecker product this ⊗ that: the (rows·that.rows) × (cols·that.cols) block matrix whose (i, j) block is this(i, j) · that. The kernel behind the vectorized matrix-equation tier: vec(A·X·B) = (Bᵀ ⊗ A) · vec(X).

Kronecker product this ⊗ that: the (rows·that.rows) × (cols·that.cols) block matrix whose (i, j) block is this(i, j) · that. The kernel behind the vectorized matrix-equation tier: vec(A·X·B) = (Bᵀ ⊗ A) · vec(X).

Value parameters

that

the right factor

Attributes

LU decomposition with partial pivoting: P·A = L·U, where L is unit lower triangular, U is upper triangular, and P is the permutation matrix. None when the matrix is non-square or singular (zero pivot encountered).

LU decomposition with partial pivoting: P·A = L·U, where L is unit lower triangular, U is upper triangular, and P is the permutation matrix. None when the matrix is non-square or singular (zero pivot encountered).

Attributes

Returns

Some((L, U, P)) for square non-singular matrices, None otherwise

Matrix multiplication this × that (this.cols must equal that.rows).

Matrix multiplication this × that (this.cols must equal that.rows).

Block-tiled i-k-j loop: Tile × Tile blocks keep the hot tile of that (and out) cache-resident across a whole row block. Within each output cell, k-accumulation is ascending, so results are bit-identical to the untiled kernel. Row blocks write disjoint slices of out and parallelise with no locking above the work-volume threshold.

Value parameters

that

right factor; that.rows must equal this.cols

Attributes

QR decomposition via modified Gram-Schmidt: A = Q·R (m ≥ n). Q is m × n with orthonormal columns; R is n × n upper triangular. None when rows < cols or the matrix is rank-deficient (a column reduces to zero norm).

QR decomposition via modified Gram-Schmidt: A = Q·R (m ≥ n). Q is m × n with orthonormal columns; R is n × n upper triangular. None when rows < cols or the matrix is rank-deficient (a column reduces to zero norm).

Attributes

Returns

Some((Q, R)) or None

override def rebuild(c: List[_Expression]): _Expression

Reconstructs the same node shape with replacement sub-expressions.

Reconstructs the same node shape with replacement sub-expressions.

Value parameters

newChildren

replacements in the same order and count as children

Attributes

Definition Classes
def scale(k: Double): _MatrixValue

Multiplies every element by scalar k.

Multiplies every element by scalar k.

Value parameters

k

the scalar factor

Attributes

def spectralDecompose: Option[(Vector[Vector[_Value]], Vector[_Value])]

Spectral decomposition: eigenvalues and right eigenvectors. Returns (columns of V, eigenvalues) where eigenvector column j corresponds to eigenvalue j. Complex conjugate pairs (α ± βi) occupy consecutive positions. None when non-square, QR iteration fails, or an eigenvector cannot be extracted (numerically degenerate or defective matrix).

Spectral decomposition: eigenvalues and right eigenvectors. Returns (columns of V, eigenvalues) where eigenvector column j corresponds to eigenvalue j. Complex conjugate pairs (α ± βi) occupy consecutive positions. None when non-square, QR iteration fails, or an eigenvector cannot be extracted (numerically degenerate or defective matrix).

Attributes

Returns

Some((V, eigenvalues)) or None

override def toString: String

Renders this matrix at Environment.DefaultPrecision decimal places.

Renders this matrix at Environment.DefaultPrecision decimal places.

Attributes

Definition Classes
Any
def toVector: Vector[Double]

Read-only view of the dense storage in row-major order.

Read-only view of the dense storage in row-major order.

Attributes

Returns the transpose of this matrix (cols × rows).

Returns the transpose of this matrix (cols × rows).

Attributes

Column-stacking vectorisation: returns the (rows·cols) × 1 column vector where vec[j·rows + i] = this(i, j). Paired with _MatrixValue$.unvec.

Column-stacking vectorisation: returns the (rows·cols) × 1 column vector where vec[j·rows + i] = this(i, j). Paired with _MatrixValue$.unvec.

Attributes

Concrete fields

val cols: Int
lazy override val hashCode: Int

Cached hash code; safe because the storage is immutable.

Cached hash code; safe because the storage is immutable.

Attributes

val rows: Int

Inherited fields

lazy val freeVars: Set[String]

Cached set of free variable names; O(1) after the first access.

Cached set of free variable names; O(1) after the first access.

Attributes

Inherited from:
_Expression