Matrix types and linear algebra operations.
Provides fixed-size matrix types (Mat2x2, Mat3x3, Mat4x4), together with support for general double[][] arrays and Apache Commons Math RealMatrix. Common operations are unified behind a protocol, so functions such as mulm, inverse, transpose or det work uniformly across all supported representations.
Constructors build matrices from scalars, rows, columns, diagonals or existing arrays and RealMatrix objects: mat2x2, mat3x3, mat4x4, mat, real-matrix, and their rows->, cols->, diag-> and array2d-> counterparts, plus eye, zero and diagonal.
Element and structural access is provided by entry, row, col, rows, cols, diag, nrow, ncol, shape, mat->seq, mat->array, mat->array2d and their float variants, along with the predicates square? and symmetric?, and symmetrize for the symmetric part of a square matrix.
Arithmetic and linear algebra operations include add, adds, sub, negate, mulm (and its transposed variants mulmt, tmulm, tmulmt), emulm, mulv, vtmul, muls, outer, kronecker, transpose, inverse, det, trace, cholesky, pow, norm and condition.
Matrix decompositions (qr-decomposition, rrqr-decomposition, cholesky-decomposition, sv-decomposition, lu-decomposition, eigen-decomposition) return a common decomposition value, accessed with decomposition-component and usable as input to solve, inverse and singular?. Related eigen helpers (eigenvalues, eigenvalues-matrix, eigenvectors, singular-values) provide direct shortcuts without building a decomposition explicitly.
Data-oriented helpers treat a matrix as a table of row observations: bind-rows, bind-cols, map-rows, map-cols, differences, normalize, demean, standardize, shift-rows, shift-cols, scale-rows and scale-cols.
Geometric transformations are provided by rotation-matrix-2d, the 3d rotation family rotation-matrix-3d, rotation-matrix-3d-x, rotation-matrix-3d-y, rotation-matrix-3d-z and rotation-matrix-axis-3d, and block-diagonal for combining smaller matrices into a larger one.
Matrix types and linear algebra operations. Provides fixed-size matrix types (`Mat2x2`, `Mat3x3`, `Mat4x4`), together with support for general `double[][]` arrays and Apache Commons Math `RealMatrix`. Common operations are unified behind a protocol, so functions such as `mulm`, `inverse`, `transpose` or `det` work uniformly across all supported representations. Constructors build matrices from scalars, rows, columns, diagonals or existing arrays and `RealMatrix` objects: `mat2x2`, `mat3x3`, `mat4x4`, `mat`, `real-matrix`, and their `rows->`, `cols->`, `diag->` and `array2d->` counterparts, plus `eye`, `zero` and `diagonal`. Element and structural access is provided by `entry`, `row`, `col`, `rows`, `cols`, `diag`, `nrow`, `ncol`, `shape`, `mat->seq`, `mat->array`, `mat->array2d` and their float variants, along with the predicates `square?` and `symmetric?`, and `symmetrize` for the symmetric part of a square matrix. Arithmetic and linear algebra operations include `add`, `adds`, `sub`, `negate`, `mulm` (and its transposed variants `mulmt`, `tmulm`, `tmulmt`), `emulm`, `mulv`, `vtmul`, `muls`, `outer`, `kronecker`, `transpose`, `inverse`, `det`, `trace`, `cholesky`, `pow`, `norm` and `condition`. Matrix decompositions (`qr-decomposition`, `rrqr-decomposition`, `cholesky-decomposition`, `sv-decomposition`, `lu-decomposition`, `eigen-decomposition`) return a common decomposition value, accessed with `decomposition-component` and usable as input to `solve`, `inverse` and `singular?`. Related eigen helpers (`eigenvalues`, `eigenvalues-matrix`, `eigenvectors`, `singular-values`) provide direct shortcuts without building a decomposition explicitly. Data-oriented helpers treat a matrix as a table of row observations: `bind-rows`, `bind-cols`, `map-rows`, `map-cols`, `differences`, `normalize`, `demean`, `standardize`, `shift-rows`, `shift-cols`, `scale-rows` and `scale-cols`. Geometric transformations are provided by `rotation-matrix-2d`, the 3d rotation family `rotation-matrix-3d`, `rotation-matrix-3d-x`, `rotation-matrix-3d-y`, `rotation-matrix-3d-z` and `rotation-matrix-axis-3d`, and `block-diagonal` for combining smaller matrices into a larger one.
(acos vector)Applies fastmath.core/acos to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/acos]] to every element of a matrix, elementwise, returning a matrix of the same type.
(acosh vector)Applies fastmath.core/acosh to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/acosh]] to every element of a matrix, elementwise, returning a matrix of the same type.
(acot vector)Applies fastmath.core/acot to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/acot]] to every element of a matrix, elementwise, returning a matrix of the same type.
(acoth vector)Applies fastmath.core/acoth to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/acoth]] to every element of a matrix, elementwise, returning a matrix of the same type.
(acsc vector)Applies fastmath.core/acsc to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/acsc]] to every element of a matrix, elementwise, returning a matrix of the same type.
(acsch vector)Applies fastmath.core/acsch to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/acsch]] to every element of a matrix, elementwise, returning a matrix of the same type.
(adds A s)Adds scalar to all matrix elements
Adds scalar to all matrix elements
(array2d->mat2x2 arrs)Creates 2x2 matrix from 2d double array.
Creates 2x2 matrix from 2d double array.
(array2d->mat3x3 arrs)Creates 3x3 matrix from 2d double array.
Creates 3x3 matrix from 2d double array.
(array2d->mat4x4 arrs)Creates 4x4 matrix from 2d double array.
Creates 4x4 matrix from 2d double array.
(array2d->RealMatrix arrs)Creates RealMatrix matrix from 2d double array.
Creates RealMatrix matrix from 2d double array.
(asec vector)Applies fastmath.core/asec to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/asec]] to every element of a matrix, elementwise, returning a matrix of the same type.
(asech vector)Applies fastmath.core/asech to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/asech]] to every element of a matrix, elementwise, returning a matrix of the same type.
(asin vector)Applies fastmath.core/asin to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/asin]] to every element of a matrix, elementwise, returning a matrix of the same type.
(asinh vector)Applies fastmath.core/asinh to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/asinh]] to every element of a matrix, elementwise, returning a matrix of the same type.
(atan vector)Applies fastmath.core/atan to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/atan]] to every element of a matrix, elementwise, returning a matrix of the same type.
(atanh vector)Applies fastmath.core/atanh to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/atanh]] to every element of a matrix, elementwise, returning a matrix of the same type.
(bind-cols mats)(bind-cols m & r)Creates a RealMatrix by placing the given matrices side by side as column blocks, in order.
A matrix shorter than the tallest one is zero-padded at the bottom.
Creates a `RealMatrix` by placing the given matrices side by side as column blocks, in order. A matrix shorter than the tallest one is zero-padded at the bottom.
(bind-rows mats)(bind-rows m & r)Creates a RealMatrix by stacking the given matrices on top of each other as row blocks, in order.
A matrix narrower than the widest one is zero-padded on the right.
Creates a `RealMatrix` by stacking the given matrices on top of each other as row blocks, in order. A matrix narrower than the widest one is zero-padded on the right.
(block-diagonal mats)(block-diagonal m & r)Creates a block diagonal RealMatrix from a sequence of square matrices, placed along the diagonal in order, zero elsewhere.
Throws when any input matrix is not square.
Creates a block diagonal `RealMatrix` from a sequence of square matrices, placed along the diagonal in order, zero elsewhere. Throws when any input matrix is not square.
(cb vector)Applies fastmath.core/cb to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/cb]] to every element of a matrix, elementwise, returning a matrix of the same type.
(cbrt vector)Applies fastmath.core/cbrt to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/cbrt]] to every element of a matrix, elementwise, returning a matrix of the same type.
(ceil vector)Applies fastmath.core/ceil to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/ceil]] to every element of a matrix, elementwise, returning a matrix of the same type.
(cholesky A)(cholesky A upper?)Calculates L (lower by default) triangular for where L * L^T = A.
Checks only for symmetry, can return NaNs when A is not positive-definite.
Calculates L (lower by default) triangular for where L * L^T = A. Checks only for symmetry, can return NaNs when A is not positive-definite.
(cholesky-decomposition mat)(cholesky-decomposition mat symmetry_threshold positivity_threshold)Performs Cholesky decomposition of a matrix.
Decomposes a real, symmetric, positive-definite matrix A as A = L x LT, where L is lower triangular. The solver minimizes using the least squares method.
Parameters:
mat - a real symmetric positive-definite matrix to decompose.symmetry_threshold (optional) - maximum relative difference allowed between symmetric off-diagonal elements for mat to be treated as symmetric; defaults to the implementation's built-in threshold.positivity_threshold (optional) - smallest value accepted for diagonal elements during decomposition; defaults to the implementation's built-in threshold.Both threshold parameters, when provided, must be given together.
Returns a decomposition value. Access individual parts with decomposition-component using one of the following keys:
:L, :LT - matrices.:det - determinant.Throws an exception when mat is not symmetric or not positive-definite within the given thresholds.
The result can be used as input for solve, inverse and singular?.
See also qr-decomposition, rrqr-decomposition, lu-decomposition, sv-decomposition, eigen-decomposition.
Performs Cholesky decomposition of a matrix. Decomposes a real, symmetric, positive-definite matrix `A` as `A = L x LT`, where `L` is lower triangular. The solver minimizes using the least squares method. Parameters: - `mat` - a real symmetric positive-definite matrix to decompose. - `symmetry_threshold` (optional) - maximum relative difference allowed between symmetric off-diagonal elements for `mat` to be treated as symmetric; defaults to the implementation's built-in threshold. - `positivity_threshold` (optional) - smallest value accepted for diagonal elements during decomposition; defaults to the implementation's built-in threshold. Both threshold parameters, when provided, must be given together. Returns a decomposition value. Access individual parts with `decomposition-component` using one of the following keys: - `:L`, `:LT` - matrices. - `:det` - determinant. Throws an exception when `mat` is not symmetric or not positive-definite within the given thresholds. The result can be used as input for `solve`, `inverse` and `singular?`. See also [[qr-decomposition]], [[rrqr-decomposition]], [[lu-decomposition]], [[sv-decomposition]], [[eigen-decomposition]].
(cols->mat real-matrix-cols)(cols->mat [a00 a10] [a01 a11])(cols->mat [a00 a10 a20] [a01 a11 a21] [a02 a12 a22])(cols->mat [a00 a10 a20 a30]
[a01 a11 a21 a31]
[a02 a12 a22 a32]
[a03 a13 a23 a33])Creates a matrix from column vectors, dispatching on argument count.
Arity:
RealMatrix (see cols->RealMatrix)Mat2x2Mat3x3Mat4x4Creates a matrix from column vectors, dispatching on argument count. Arity: * 1 - a single sequence of column vectors, builds a `RealMatrix` (see [[cols->RealMatrix]]) * 2 - two separate 2d column vectors, builds a `Mat2x2` * 3 - three separate 3d column vectors, builds a `Mat3x3` * 4 - four separate 4d column vectors, builds a `Mat4x4`
(cols->mat2x2 [a00 a10] [a01 a11])Creates 2x2 matrix from two 2d column vectors, given as two separate arguments.
Creates 2x2 matrix from two 2d column vectors, given as two separate arguments.
(cols->mat3x3 [a00 a10 a20] [a01 a11 a21] [a02 a12 a22])Creates 3x3 matrix from three 3d column vectors, given as three separate arguments.
Creates 3x3 matrix from three 3d column vectors, given as three separate arguments.
(cols->mat4x4 [a00 a10 a20 a30]
[a01 a11 a21 a31]
[a02 a12 a22 a32]
[a03 a13 a23 a33])Creates 4x4 matrix from four 4d column vectors, given as four separate arguments.
Creates 4x4 matrix from four 4d column vectors, given as four separate arguments.
(cols->RealMatrix cols)Creates an Apache Commons Math RealMatrix from a sequence of column vectors.
Equivalent to transposing the result of rows->RealMatrix applied to cols.
See also real-matrix.
Creates an Apache Commons Math `RealMatrix` from a sequence of column vectors. Equivalent to transposing the result of [[rows->RealMatrix]] applied to `cols`. See also [[real-matrix]].
(condition A)(condition A norm-type)Calculates the condition number of a matrix.
The condition number is defined as cond(A) = norm(A) x norm(inv(A)) and measures how sensitive the solution of a linear system involving A is to small changes in the input; large values indicate a matrix close to singular.
Parameters:
A - a matrix.norm-type (optional, default: 2) - the norm used in the computation, see norm for all supported types.Returns the condition number as a double, or ##Inf when A is singular.
Calculates the condition number of a matrix. The condition number is defined as `cond(A) = norm(A) x norm(inv(A))` and measures how sensitive the solution of a linear system involving `A` is to small changes in the input; large values indicate a matrix close to singular. Parameters: - `A` - a matrix. - `norm-type` (optional, default: `2`) - the norm used in the computation, see [[norm]] for all supported types. Returns the condition number as a double, or `##Inf` when `A` is singular. See also [[norm]], [[singular?]], [[inverse]].
(cos vector)Applies fastmath.core/cos to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/cos]] to every element of a matrix, elementwise, returning a matrix of the same type.
(cosh vector)Applies fastmath.core/cosh to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/cosh]] to every element of a matrix, elementwise, returning a matrix of the same type.
(cot vector)Applies fastmath.core/cot to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/cot]] to every element of a matrix, elementwise, returning a matrix of the same type.
(coth vector)Applies fastmath.core/coth to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/coth]] to every element of a matrix, elementwise, returning a matrix of the same type.
(csc vector)Applies fastmath.core/csc to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/csc]] to every element of a matrix, elementwise, returning a matrix of the same type.
(csch vector)Applies fastmath.core/csch to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/csch]] to every element of a matrix, elementwise, returning a matrix of the same type.
(decomposition-component decomposition-matrix component-name)Returns a component of a matrix decomposition by its keyword name.
Used together with the decomposition functions (qr-decomposition, rrqr-decomposition, cholesky-decomposition, sv-decomposition, lu-decomposition, eigen-decomposition) to extract matrices, vectors or other derived values from the resulting decomposition. Components may be computed lazily and are materialized only on first access.
Parameters:
decomposition-matrix - a decomposition value returned by one of the *-decomposition functions.component-name - a keyword naming the component to retrieve, for example :U, :V, :L, :D, :det, :eigenvectors. The available keys depend on which decomposition function produced decomposition-matrix, see the docstring of the corresponding decomposition function for the full list.Returns the value of the requested component, or nil when component-name is not present in the decomposition.
See also qr-decomposition, rrqr-decomposition, cholesky-decomposition, sv-decomposition, lu-decomposition, eigen-decomposition.
Returns a component of a matrix decomposition by its keyword name. Used together with the decomposition functions (`qr-decomposition`, `rrqr-decomposition`, `cholesky-decomposition`, `sv-decomposition`, `lu-decomposition`, `eigen-decomposition`) to extract matrices, vectors or other derived values from the resulting decomposition. Components may be computed lazily and are materialized only on first access. Parameters: - `decomposition-matrix` - a decomposition value returned by one of the `*-decomposition` functions. - `component-name` - a keyword naming the component to retrieve, for example `:U`, `:V`, `:L`, `:D`, `:det`, `:eigenvectors`. The available keys depend on which decomposition function produced `decomposition-matrix`, see the docstring of the corresponding decomposition function for the full list. Returns the value of the requested component, or `nil` when `component-name` is not present in the decomposition. See also [[qr-decomposition]], [[rrqr-decomposition]], [[cholesky-decomposition]], [[sv-decomposition]], [[lu-decomposition]], [[eigen-decomposition]].
(degrees vector)Applies fastmath.core/degrees to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/degrees]] to every element of a matrix, elementwise, returning a matrix of the same type.
(demean A)(demean A rows?)Subtracts the mean from columns (or rows, when rows? is true).
Subtracts the mean from columns (or rows, when `rows?` is true).
(diag A)Returns diagonal of the matrix as a vector.
Returns diagonal of the matrix as a vector.
(diag->mat2x2 d)(diag->mat2x2 d1 d2)Creates 2x2 diagonal matrix.
Arity:
Creates 2x2 diagonal matrix. Arity: * 1 - fills the diagonal with the given value * 2 - one value per diagonal element
(diag->mat3x3 d)(diag->mat3x3 d1 d2 d3)Creates 3x3 diagonal matrix.
Arity:
Creates 3x3 diagonal matrix. Arity: * 1 - fills the diagonal with the given value * 3 - one value per diagonal element
(diag->mat4x4 d)(diag->mat4x4 d1 d2 d3 d4)Creates 4x4 diagonal matrix.
Arity:
Creates 4x4 diagonal matrix. Arity: * 1 - fills the diagonal with the given value * 4 - one value per diagonal element
(diagonal v)(diagonal a11 a22)(diagonal a11 a22 a33)(diagonal a11 a22 a33 a44)Creates a diagonal matrix.
Arity:
v, a sequence of diagonal values) - always builds a RealMatrix, regardless of v's length, even 2, 3 or 4Mat2x2/Mat3x3/Mat4x4Creates a diagonal matrix. Arity: * 1 (`v`, a sequence of diagonal values) - always builds a `RealMatrix`, regardless of `v`'s length, even 2, 3 or 4 * 2, 3, 4 (direct scalar arguments) - builds the corresponding fixed `Mat2x2`/`Mat3x3`/`Mat4x4`
(differences m)(differences m diffs)(differences m diffs lag)Apply lagged differences on matrix columns, always returns RealMatrix.
Apply lagged differences on matrix columns, always returns RealMatrix.
(eigen-decomposition mat)(eigen-decomposition mat
{:keys [backend eigenvectors-scaling]
:or {backend :acm eigenvectors-scaling true}})Performs eigen decomposition of a square matrix.
Decomposes a square matrix A as A = V x D x VT, where D holds the eigenvalues (real values on the diagonal, imaginary parts on the subdiagonal for complex conjugate pairs) and V holds the eigenvectors as columns. The solver is exact.
Parameters:
mat - a square matrix to decompose.
options (map, optional):
:backend - :acm (default) or :colt, selects the underlying implementation.:eigenvectors-scaling - how the :eigenvectors component is derived from the raw columns of V, one of:
:raw - the raw columns of V, unmodified. For a complex-conjugate eigenvalue pair, V holds the real and imaginary part of the (shared) eigenvector as two separate real columns, not a genuine complex eigenvector.false - genuine eigenvectors: real columns are returned as-is, complex-conjugate pairs are recombined into true complex eigenvectors (see fastmath.complex). Not normalized.true (default) or :normalized - as false, with every eigenvector additionally normalized to unit (Euclidean) length.:lapack - as :normalized, with every eigenvector belonging to a complex eigenvalue additionally phase-rotated so that its largest-magnitude component becomes a (positive) real number; eigenvectors belonging to real eigenvalues are unaffected (identical to :normalized). Intended to resemble, but not guaranteed to exactly reproduce, the convention used by LAPACK-based tools such as R's eigen.A real eigenvector's sign (under :normalized/true or :lapack) is an arbitrary artifact of the underlying algorithm, not a meaningful convention; it may differ between backends, or from other tools such as R.
With the :acm backend (Apache Commons Math), a solver is created only when eigenvalues are real; when eigenvalues are complex, singularity is determined from the determinant instead. This backend also exposes a :sqrt component (matrix square root); computing it throws MathUnsupportedOperationException for matrices Apache Commons Math does not support (e.g. most matrices with complex eigenvalues).
With the :colt backend (Colt library), no solver is created, so solve and inverse can not be used on the resulting decomposition; singularity is always determined from the determinant, and the :sqrt component is not available. This backend should be used to decompose matrices when :acm fails.
Returns a decomposition value. Access individual parts with decomposition-component using one of the following keys:
:D, :V, :VT - matrices, and :sqrt (:acm backend only) - matrix square root.:det - determinant, computed as the product of the eigenvalues (via complex arithmetic, keeping the real part); correct for real or complex eigenvalues alike, for both backends.:real-eigenvalues, :imag-eigenvalues - real and imaginary parts of the eigenvalues.:eigenvalues - eigenvalues as a sequence of 2d vectors, real and imaginary part each.:eigenvectors - sequence of eigenvectors, scaled per :eigenvectors-scaling.:complex? - true when eigenvalues are complex.The result can be used as input for solve, inverse and singular?, subject to the backend limitations noted above.
See also eigenvalues, eigenvalues-matrix, eigenvectors, singular-values.
Performs eigen decomposition of a square matrix.
Decomposes a square matrix `A` as `A = V x D x VT`, where `D` holds the eigenvalues (real values on the diagonal, imaginary parts on the subdiagonal for complex conjugate pairs) and `V` holds the eigenvectors as columns. The solver is exact.
Parameters:
- `mat` - a square matrix to decompose.
- `options` (map, optional):
- `:backend` - `:acm` (default) or `:colt`, selects the underlying implementation.
- `:eigenvectors-scaling` - how the `:eigenvectors` component is derived from the raw columns of `V`, one of:
- `:raw` - the raw columns of `V`, unmodified. For a complex-conjugate eigenvalue pair, `V` holds the real and imaginary part of the (shared) eigenvector as two separate real columns, not a genuine complex eigenvector.
- `false` - genuine eigenvectors: real columns are returned as-is, complex-conjugate pairs are recombined into true complex eigenvectors (see `fastmath.complex`). Not normalized.
- `true` (default) or `:normalized` - as `false`, with every eigenvector additionally normalized to unit (Euclidean) length.
- `:lapack` - as `:normalized`, with every eigenvector belonging to a complex eigenvalue additionally phase-rotated so that its largest-magnitude component becomes a (positive) real number; eigenvectors belonging to real eigenvalues are unaffected (identical to `:normalized`). Intended to resemble, but not guaranteed to exactly reproduce, the convention used by LAPACK-based tools such as R's `eigen`.
A real eigenvector's sign (under `:normalized`/`true` or `:lapack`) is an arbitrary artifact of the underlying algorithm, not a meaningful convention; it may differ between backends, or from other tools such as R.
With the `:acm` backend (Apache Commons Math), a solver is created only when eigenvalues are real; when eigenvalues are complex, singularity is determined from the determinant instead. This backend also exposes a `:sqrt` component (matrix square root); computing it throws `MathUnsupportedOperationException` for matrices Apache Commons Math does not support (e.g. most matrices with complex eigenvalues).
With the `:colt` backend (Colt library), no solver is created, so `solve` and `inverse` can not be used on the resulting decomposition; singularity is always determined from the determinant, and the `:sqrt` component is not available. This backend should be used to decompose matrices when `:acm` fails.
Returns a decomposition value. Access individual parts with `decomposition-component` using one of the following keys:
- `:D`, `:V`, `:VT` - matrices, and `:sqrt` (`:acm` backend only) - matrix square root.
- `:det` - determinant, computed as the product of the eigenvalues (via complex arithmetic, keeping the real part); correct for real or complex eigenvalues alike, for both backends.
- `:real-eigenvalues`, `:imag-eigenvalues` - real and imaginary parts of the eigenvalues.
- `:eigenvalues` - eigenvalues as a sequence of 2d vectors, real and imaginary part each.
- `:eigenvectors` - sequence of eigenvectors, scaled per `:eigenvectors-scaling`.
- `:complex?` - true when eigenvalues are complex.
The result can be used as input for `solve`, `inverse` and `singular?`, subject to the backend limitations noted above.
See also [[eigenvalues]], [[eigenvalues-matrix]], [[eigenvectors]], [[singular-values]].(eigenvalues A)Returns the eigenvalues of a matrix.
Internally uses the :colt backend of eigen-decomposition, so no exception is raised for complex eigenvalues.
Parameters:
A - a square matrix.Returns a sequence of 2d vectors, one per eigenvalue, each holding the real and imaginary part of the eigenvalue in that order. Real eigenvalues have an imaginary part equal to zero.
See also eigen-decomposition, eigenvalues-matrix, eigenvectors, singular-values.
Returns the eigenvalues of a matrix. Internally uses the `:colt` backend of `eigen-decomposition`, so no exception is raised for complex eigenvalues. Parameters: - `A` - a square matrix. Returns a sequence of 2d vectors, one per eigenvalue, each holding the real and imaginary part of the eigenvalue in that order. Real eigenvalues have an imaginary part equal to zero. See also [[eigen-decomposition]], [[eigenvalues-matrix]], [[eigenvectors]], [[singular-values]].
(eigenvalues-matrix A)Returns the eigenvalues of a matrix as a diagonal or block diagonal matrix.
Real eigenvalues appear as entries on the diagonal. Each pair of complex conjugate eigenvalues appears as a 2x2 block on the diagonal instead, holding the real and imaginary parts of that pair.
Parameters:
A - a square matrix.Returns a matrix of the same size as A.
See also eigenvalues, eigen-decomposition, decomposition-component.
Returns the eigenvalues of a matrix as a diagonal or block diagonal matrix. Real eigenvalues appear as entries on the diagonal. Each pair of complex conjugate eigenvalues appears as a 2x2 block on the diagonal instead, holding the real and imaginary parts of that pair. Parameters: - `A` - a square matrix. Returns a matrix of the same size as `A`. See also [[eigenvalues]], [[eigen-decomposition]], [[decomposition-component]].
(eigenvectors A)(eigenvectors A scaling)Returns the eigenvectors of a matrix.
Internally uses the :colt backend of eigen-decomposition, so no exception is raised for complex eigenvalues.
Parameters:
A - a square matrix.scaling (optional, default: false) - how eigenvectors are derived from the raw columns of V, one of:
:raw - the raw columns of V, unmodified. For a complex-conjugate eigenvalue pair, V holds the real and imaginary part of the (shared) eigenvector as two separate real columns, not a genuine complex eigenvector.false (default) - genuine eigenvectors: real columns are returned as-is, complex-conjugate pairs are recombined into true complex eigenvectors (see fastmath.complex). Not normalized.true or :normalized - as false, with every eigenvector additionally normalized to unit (Euclidean) length.:lapack - as :normalized, with every eigenvector belonging to a complex eigenvalue additionally phase-rotated so that its largest-magnitude component becomes a (positive) real number; eigenvectors belonging to real eigenvalues are unaffected (identical to :normalized). Intended to resemble, but not guaranteed to exactly reproduce, the convention used by LAPACK-based tools such as R's eigen.A real eigenvector's sign (under :normalized/true or :lapack) is an arbitrary artifact of the underlying algorithm, not a meaningful convention; it may differ between backends, or from other tools such as R.
Returns a sequence of eigenvectors, in the same order as the corresponding eigenvalues returned by eigenvalues. Each eigenvector is itself a sequence of components: plain real numbers under :raw, complex numbers (Vec2, see fastmath.complex) otherwise.
See also eigenvalues, eigen-decomposition, decomposition-component.
Returns the eigenvectors of a matrix. Internally uses the `:colt` backend of `eigen-decomposition`, so no exception is raised for complex eigenvalues. Parameters: - `A` - a square matrix. - `scaling` (optional, default: `false`) - how eigenvectors are derived from the raw columns of `V`, one of: - `:raw` - the raw columns of `V`, unmodified. For a complex-conjugate eigenvalue pair, `V` holds the real and imaginary part of the (shared) eigenvector as two separate real columns, not a genuine complex eigenvector. - `false` (default) - genuine eigenvectors: real columns are returned as-is, complex-conjugate pairs are recombined into true complex eigenvectors (see `fastmath.complex`). Not normalized. - `true` or `:normalized` - as `false`, with every eigenvector additionally normalized to unit (Euclidean) length. - `:lapack` - as `:normalized`, with every eigenvector belonging to a complex eigenvalue additionally phase-rotated so that its largest-magnitude component becomes a (positive) real number; eigenvectors belonging to real eigenvalues are unaffected (identical to `:normalized`). Intended to resemble, but not guaranteed to exactly reproduce, the convention used by LAPACK-based tools such as R's `eigen`. A real eigenvector's sign (under `:normalized`/`true` or `:lapack`) is an arbitrary artifact of the underlying algorithm, not a meaningful convention; it may differ between backends, or from other tools such as R. Returns a sequence of eigenvectors, in the same order as the corresponding eigenvalues returned by [[eigenvalues]]. Each eigenvector is itself a sequence of components: plain real numbers under `:raw`, complex numbers (`Vec2`, see `fastmath.complex`) otherwise. See also [[eigenvalues]], [[eigen-decomposition]], [[decomposition-component]].
(emulm A B)Multiplies two matrices element-wise, Hadamard product, C=AoB
Multiplies two matrices element-wise, Hadamard product, C=AoB
(entry A row col)Returns the entry at given 0-based row and column.
Throws an exception when row/col are out of bounds.
Returns the entry at given 0-based row and column. Throws an exception when `row`/`col` are out of bounds.
(exp vector)Applies fastmath.core/exp to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/exp]] to every element of a matrix, elementwise, returning a matrix of the same type.
(expm1 vector)Applies fastmath.core/expm1 to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/expm1]] to every element of a matrix, elementwise, returning a matrix of the same type.
(eye size)(eye size real-matrix?)Creates an identity matrix of the given size.
Sizes 2, 3 or 4 build the corresponding fixed Mat2x2/Mat3x3/Mat4x4; any other size builds a RealMatrix.
A truthy real-matrix? (2-arity) forces a RealMatrix regardless of size.
Creates an identity matrix of the given size. Sizes 2, 3 or 4 build the corresponding fixed `Mat2x2`/`Mat3x3`/`Mat4x4`; any other size builds a `RealMatrix`. A truthy `real-matrix?` (2-arity) forces a `RealMatrix` regardless of size.
(floor vector)Applies fastmath.core/floor to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/floor]] to every element of a matrix, elementwise, returning a matrix of the same type.
(fmap A f)Applies a function f to each matrix element.
Applies a function `f` to each matrix element.
(frac vector)Applies fastmath.core/frac to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/frac]] to every element of a matrix, elementwise, returning a matrix of the same type.
(inverse m)Matrix inversion.
On a singular Mat2x2/Mat3x3/Mat4x4, returns nil. On a singular RealMatrix/double[][], throws SingularMatrixException. On 0.0 (Number, a degenerate 1x1 matrix), returns ##Inf. On a decomposition (returned by one of the *-decomposition functions) of a singular matrix, throws SingularMatrixException (check singular? first).
Matrix inversion. On a singular `Mat2x2`/`Mat3x3`/`Mat4x4`, returns `nil`. On a singular `RealMatrix`/`double[][]`, throws `SingularMatrixException`. On `0.0` (`Number`, a degenerate 1x1 matrix), returns `##Inf`. On a decomposition (returned by one of the `*-decomposition` functions) of a singular matrix, throws `SingularMatrixException` (check `singular?` first).
(kronecker mat1 mat2)Returns Kronecker product of two matrices.
Returns Kronecker product of two matrices.
(ln vector)Applies fastmath.core/ln to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/ln]] to every element of a matrix, elementwise, returning a matrix of the same type.
(log vector)Applies fastmath.core/log to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/log]] to every element of a matrix, elementwise, returning a matrix of the same type.
(log10 vector)Applies fastmath.core/log10 to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/log10]] to every element of a matrix, elementwise, returning a matrix of the same type.
(log1mexp vector)Applies fastmath.core/log1mexp to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/log1mexp]] to every element of a matrix, elementwise, returning a matrix of the same type.
(log1p vector)Applies fastmath.core/log1p to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/log1p]] to every element of a matrix, elementwise, returning a matrix of the same type.
(log1pexp vector)Applies fastmath.core/log1pexp to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/log1pexp]] to every element of a matrix, elementwise, returning a matrix of the same type.
(log1pmx vector)Applies fastmath.core/log1pmx to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/log1pmx]] to every element of a matrix, elementwise, returning a matrix of the same type.
(log1psq vector)Applies fastmath.core/log1psq to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/log1psq]] to every element of a matrix, elementwise, returning a matrix of the same type.
(log2 vector)Applies fastmath.core/log2 to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/log2]] to every element of a matrix, elementwise, returning a matrix of the same type.
(logexpm1 vector)Applies fastmath.core/logexpm1 to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/logexpm1]] to every element of a matrix, elementwise, returning a matrix of the same type.
(logit vector)Applies fastmath.core/logit to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/logit]] to every element of a matrix, elementwise, returning a matrix of the same type.
(logmxp1 vector)Applies fastmath.core/logmxp1 to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/logmxp1]] to every element of a matrix, elementwise, returning a matrix of the same type.
(lu-decomposition mat)(lu-decomposition mat threshold)Performs LU decomposition of a matrix.
Decomposes a square matrix A as A = inv(P) x L x U, where L is lower triangular, U is upper triangular and P is a permutation matrix. The solver is exact.
Parameters:
mat - a square matrix to decompose.threshold (optional, default: 1.0e-11) - singularity threshold below which a pivot element is considered zero.Returns a decomposition value. Access individual parts with decomposition-component using one of the following keys:
:L, :U, :P (permutation) - matrices.:det - determinant.:pivot - pivot permutation vector.The result can be used as input for solve, inverse and singular?.
See also qr-decomposition, rrqr-decomposition, cholesky-decomposition, sv-decomposition, eigen-decomposition.
Performs LU decomposition of a matrix. Decomposes a square matrix `A` as `A = inv(P) x L x U`, where `L` is lower triangular, `U` is upper triangular and `P` is a permutation matrix. The solver is exact. Parameters: - `mat` - a square matrix to decompose. - `threshold` (optional, default: `1.0e-11`) - singularity threshold below which a pivot element is considered zero. Returns a decomposition value. Access individual parts with `decomposition-component` using one of the following keys: - `:L`, `:U`, `:P` (permutation) - matrices. - `:det` - determinant. - `:pivot` - pivot permutation vector. The result can be used as input for `solve`, `inverse` and `singular?`. See also [[qr-decomposition]], [[rrqr-decomposition]], [[cholesky-decomposition]], [[sv-decomposition]], [[eigen-decomposition]].
(map-cols f A)Applies f to each column vector of A; f must return a same-length column vector.
Returns a RealMatrix when A is a RealMatrix, otherwise the corresponding fixed Mat2x2/Mat3x3/Mat4x4.
Applies `f` to each column vector of `A`; `f` must return a same-length column vector. Returns a `RealMatrix` when `A` is a `RealMatrix`, otherwise the corresponding fixed `Mat2x2`/`Mat3x3`/`Mat4x4`.
(map-rows f A)Applies f to each row vector of A; f must return a same-length row vector.
Returns a RealMatrix when A is a RealMatrix, otherwise the corresponding fixed Mat2x2/Mat3x3/Mat4x4.
Applies `f` to each row vector of `A`; `f` must return a same-length row vector. Returns a `RealMatrix` when `A` is a `RealMatrix`, otherwise the corresponding fixed `Mat2x2`/`Mat3x3`/`Mat4x4`.
(mat real-matrix-rows)(mat a00 a01 a10 a11)(mat a00 a01 a02 a10 a11 a12 a20 a21 a22)(mat a00 a01 a02 a03 a10 a11 a12 a13 a20 a21 a22 a23 a30 a31 a32 a33)Creates a matrix, dispatching on argument count.
Arity:
RealMatrix (see rows->RealMatrix)Mat2x2Mat3x3Mat4x4Creates a matrix, dispatching on argument count. Arity: * 1 - a sequence of row vectors, builds a `RealMatrix` (see [[rows->RealMatrix]]) * 4 - row-ordered entries, builds a `Mat2x2` * 9 - row-ordered entries, builds a `Mat3x3` * 16 - row-ordered entries, builds a `Mat4x4`
(mat->array A)Returns flat double array of entries (row order)
Returns flat double array of entries (row order)
(mat->float-array A)Returns flat float array of entries (row order)
Returns flat float array of entries (row order)
(mat->float-array2d A)Returns floats of floats
Returns floats of floats
(mat->RealMatrix A)Returns an Apache Commons Math RealMatrix built from A, whichever supported matrix representation it is.
Returns an Apache Commons Math `RealMatrix` built from `A`, whichever supported matrix representation it is.
(mat->seq A)Returns flat sequence of entries (row order)
Returns flat sequence of entries (row order)
(mat2x2 v)(mat2x2 d1 d2)(mat2x2 a00 a01 a10 a11)Creates 2x2 matrix.
Arity:
Creates 2x2 matrix. Arity: * 1 - fills matrix with given value * 2 - creates diagonal matrix * 4 - creates row ordered matrix
(mat3x3 v)(mat3x3 d1 d2 d3)(mat3x3 a00 a01 a02 a10 a11 a12 a20 a21 a22)Creates 3x3 matrix.
Arity:
Creates 3x3 matrix. Arity: * 1 - fills matrix with given value * 3 - creates diagonal matrix * 9 - creates row ordered matrix
(mat4x4 v)(mat4x4 d1 d2 d3 d4)(mat4x4 a00 a01 a02 a03 a10 a11 a12 a13 a20 a21 a22 a23 a30 a31 a32 a33)Creates 4x4 matrix.
Arity:
Creates 4x4 matrix. Arity: * 1 - fills matrix with given value * 4 - creates diagonal matrix * 16 - creates row ordered matrix
(mulm A B)(mulm A transposeA? B transposeB?)Multiplies two matrices, C=AxB.
Optionally you can apply transposition of matrices.
Multiplies two matrices, C=AxB. Optionally you can apply transposition of matrices.
(mulmt A B)Multiplies with transposed matrix, C=AxB^T
Multiplies with transposed matrix, C=AxB^T
(muls A s)Multiplies matrix by a scalar, C=sA
Multiplies matrix by a scalar, C=sA
(mulv A v)Multiplies matrix by a vector, x=Av
Multiplies matrix by a vector, x=Av
(negate A)Negates all matrix elements, C=-A.
Identical to 1-arity sub.
Negates all matrix elements, C=-A. Identical to 1-arity [[sub]].
(norm A)(norm A norm-type)Calculates a norm of the matrix.
Parameters:
A - a matrix.norm-type (optional, default: 1) - selects which norm to compute:
1 - maximum absolute column sum.:inf - maximum absolute row sum.2 - spectral norm, the largest singular value of A.:max - maximum absolute value among all entries.:frobenius - Frobenius norm, equivalent to [2 2].[p q] - generalized entrywise L_pq norm; [2 2] is the Frobenius norm, [p p] is the entrywise p-norm.[p] - Schatten p-norm, computed from the singular values of A; [1] is the nuclear (trace) norm.Returns the norm as a double.
See also condition, singular-values, sv-decomposition.
Calculates a norm of the matrix. Parameters: - `A` - a matrix. - `norm-type` (optional, default: `1`) - selects which norm to compute: - `1` - maximum absolute column sum. - `:inf` - maximum absolute row sum. - `2` - spectral norm, the largest singular value of `A`. - `:max` - maximum absolute value among all entries. - `:frobenius` - Frobenius norm, equivalent to `[2 2]`. - `[p q]` - generalized entrywise L_pq norm; `[2 2]` is the Frobenius norm, `[p p]` is the entrywise p-norm. - `[p]` - Schatten p-norm, computed from the singular values of `A`; `[1]` is the nuclear (trace) norm. Returns the norm as a double. See also [[condition]], [[singular-values]], [[sv-decomposition]].
(normalize A)(normalize A rows?)Normalizes columns (or rows, when rows? is true) to unit (Euclidean) length.
Normalizes columns (or rows, when `rows?` is true) to unit (Euclidean) length.
(outer v1 v2)Outer product of two vectors.
Vec2/Vec3/Vec4 build the corresponding fixed Mat2x2/Mat3x3/Mat4x4; any other vector type builds a RealMatrix.
Outer product of two vectors. `Vec2`/`Vec3`/`Vec4` build the corresponding fixed `Mat2x2`/`Mat3x3`/`Mat4x4`; any other vector type builds a `RealMatrix`.
(pow m exponent)Applies fastmath.core/pow to every element of a matrix, raising each to exponent.
Applies [[fastmath.core/pow]] to every element of a matrix, raising each to `exponent`.
(pow10 vector)Applies fastmath.core/pow10 to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/pow10]] to every element of a matrix, elementwise, returning a matrix of the same type.
(qr-decomposition mat)(qr-decomposition mat threshold)Performs QR decomposition of a matrix.
Decomposes a matrix A as A = Q x R, where Q is orthogonal (QT x Q = I) and R is upper triangular. The solver minimizes using the least squares method.
Parameters:
mat - a matrix to decompose.threshold (optional, default: 0.0) - singularity threshold used to determine whether a diagonal element of R is considered zero.Returns a decomposition value. Access individual parts with decomposition-component using one of the following keys:
:H - Householder reflector vectors, :Q, :QT, :R - matrices.The result can be used as input for solve, inverse and singular?.
See also rrqr-decomposition, lu-decomposition, cholesky-decomposition, sv-decomposition, eigen-decomposition.
Performs QR decomposition of a matrix. Decomposes a matrix `A` as `A = Q x R`, where `Q` is orthogonal (`QT x Q = I`) and `R` is upper triangular. The solver minimizes using the least squares method. Parameters: - `mat` - a matrix to decompose. - `threshold` (optional, default: `0.0`) - singularity threshold used to determine whether a diagonal element of `R` is considered zero. Returns a decomposition value. Access individual parts with `decomposition-component` using one of the following keys: - `:H` - Householder reflector vectors, `:Q`, `:QT`, `:R` - matrices. The result can be used as input for `solve`, `inverse` and `singular?`. See also [[rrqr-decomposition]], [[lu-decomposition]], [[cholesky-decomposition]], [[sv-decomposition]], [[eigen-decomposition]].
(radians vector)Applies fastmath.core/radians to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/radians]] to every element of a matrix, elementwise, returning a matrix of the same type.
(real-matrix rows)Creates an Apache Commons Math RealMatrix from a sequence of row vectors, or from an already-double[][]-typed array directly.
Identical to rows->RealMatrix.
See also cols->RealMatrix.
Creates an Apache Commons Math `RealMatrix` from a sequence of row vectors, or from an already-`double[][]`-typed array directly. Identical to [[rows->RealMatrix]]. See also [[cols->RealMatrix]].
(rint vector)Applies fastmath.core/rint to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/rint]] to every element of a matrix, elementwise, returning a matrix of the same type.
(rotation-matrix-2d theta)Creates a 2d rotation matrix.
Rotates vectors counterclockwise around the origin by theta radians.
Parameters:
theta (double): rotation angle in radians.Returns a Mat2x2 rotation matrix.
See also rotation-matrix-3d, rotation-matrix-axis-3d.
Creates a 2d rotation matrix. Rotates vectors counterclockwise around the origin by `theta` radians. Parameters: - `theta` (double): rotation angle in radians. Returns a `Mat2x2` rotation matrix. See also [[rotation-matrix-3d]], [[rotation-matrix-axis-3d]].
(rotation-matrix-3d [x y z])(rotation-matrix-3d x y z)Creates a 3d rotation matrix from Tait-Bryan angles.
Combines rotations around all three axes using the intrinsic z-y'-x'' convention: yaw around z, then pitch around the rotated y', then roll around the twice-rotated x''. This is equivalent to the matrix product Rx(x) times Ry(y) times Rz(z) (matches fastmath.quaternion/from-angles's identical convention: (quaternion/to-rotation-matrix (quaternion/from-angles x y z)) equals this function's result).
Parameters:
[x y z] (sequence of three doubles): roll, pitch and yaw angles in radians, given as a single sequence.x, y, z (doubles): roll, pitch and yaw angles in radians, given directly as arguments.Returns a Mat3x3 rotation matrix.
See also rotation-matrix-3d-x, rotation-matrix-3d-y, rotation-matrix-3d-z, rotation-matrix-axis-3d.
Creates a 3d rotation matrix from Tait-Bryan angles. Combines rotations around all three axes using the intrinsic z-y'-x'' convention: yaw around z, then pitch around the rotated y', then roll around the twice-rotated x''. This is equivalent to the matrix product Rx(x) times Ry(y) times Rz(z) (matches `fastmath.quaternion/from-angles`'s identical convention: `(quaternion/to-rotation-matrix (quaternion/from-angles x y z))` equals this function's result). Parameters: - `[x y z]` (sequence of three doubles): roll, pitch and yaw angles in radians, given as a single sequence. - `x`, `y`, `z` (doubles): roll, pitch and yaw angles in radians, given directly as arguments. Returns a `Mat3x3` rotation matrix. See also [[rotation-matrix-3d-x]], [[rotation-matrix-3d-y]], [[rotation-matrix-3d-z]], [[rotation-matrix-axis-3d]].
(rotation-matrix-3d-x a)Creates a 3d rotation matrix around the x-axis.
Rotates by a radians following the right-hand rule.
Parameters:
a (double): rotation angle in radians.Returns a Mat3x3 rotation matrix.
See also rotation-matrix-3d-y, rotation-matrix-3d-z, rotation-matrix-3d, rotation-matrix-axis-3d.
Creates a 3d rotation matrix around the x-axis. Rotates by `a` radians following the right-hand rule. Parameters: - `a` (double): rotation angle in radians. Returns a `Mat3x3` rotation matrix. See also [[rotation-matrix-3d-y]], [[rotation-matrix-3d-z]], [[rotation-matrix-3d]], [[rotation-matrix-axis-3d]].
(rotation-matrix-3d-y a)Creates a 3d rotation matrix around the y-axis.
Rotates by a radians following the right-hand rule.
Parameters:
a (double): rotation angle in radians.Returns a Mat3x3 rotation matrix.
See also rotation-matrix-3d-x, rotation-matrix-3d-z, rotation-matrix-3d, rotation-matrix-axis-3d.
Creates a 3d rotation matrix around the y-axis. Rotates by `a` radians following the right-hand rule. Parameters: - `a` (double): rotation angle in radians. Returns a `Mat3x3` rotation matrix. See also [[rotation-matrix-3d-x]], [[rotation-matrix-3d-z]], [[rotation-matrix-3d]], [[rotation-matrix-axis-3d]].
(rotation-matrix-3d-z a)Creates a 3d rotation matrix around the z-axis.
Rotates by a radians following the right-hand rule.
Parameters:
a (double): rotation angle in radians.Returns a Mat3x3 rotation matrix.
See also rotation-matrix-3d-x, rotation-matrix-3d-y, rotation-matrix-3d, rotation-matrix-axis-3d.
Creates a 3d rotation matrix around the z-axis. Rotates by `a` radians following the right-hand rule. Parameters: - `a` (double): rotation angle in radians. Returns a `Mat3x3` rotation matrix. See also [[rotation-matrix-3d-x]], [[rotation-matrix-3d-y]], [[rotation-matrix-3d]], [[rotation-matrix-axis-3d]].
(rotation-matrix-axis-3d angle axis)Creates a 3d rotation matrix around an arbitrary axis.
Uses Rodrigues' rotation formula to build a rotation of angle radians around axis.
Parameters:
angle (double): rotation angle in radians.axis (Vec3): the axis of rotation. It is normalized internally, so it does not need to be a unit vector.Returns a Mat3x3 rotation matrix.
See also rotation-matrix-3d, rotation-matrix-3d-x, rotation-matrix-3d-y, rotation-matrix-3d-z.
Creates a 3d rotation matrix around an arbitrary axis. Uses Rodrigues' rotation formula to build a rotation of `angle` radians around `axis`. Parameters: - `angle` (double): rotation angle in radians. - `axis` (`Vec3`): the axis of rotation. It is normalized internally, so it does not need to be a unit vector. Returns a `Mat3x3` rotation matrix. See also [[rotation-matrix-3d]], [[rotation-matrix-3d-x]], [[rotation-matrix-3d-y]], [[rotation-matrix-3d-z]].
(round vector)Applies fastmath.core/round to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/round]] to every element of a matrix, elementwise, returning a matrix of the same type.
(rows->mat real-matrix-rows)(rows->mat [a00 a01] [a10 a11])(rows->mat [a00 a01 a02] [a10 a11 a12] [a20 a21 a22])(rows->mat [a00 a01 a02 a03]
[a10 a11 a12 a13]
[a20 a21 a22 a23]
[a30 a31 a32 a33])Creates a matrix from row vectors, dispatching on argument count.
Arity:
RealMatrix (see rows->RealMatrix)Mat2x2Mat3x3Mat4x4Creates a matrix from row vectors, dispatching on argument count. Arity: * 1 - a single sequence of row vectors, builds a `RealMatrix` (see [[rows->RealMatrix]]) * 2 - two separate 2d row vectors, builds a `Mat2x2` * 3 - three separate 3d row vectors, builds a `Mat3x3` * 4 - four separate 4d row vectors, builds a `Mat4x4`
(rows->mat2x2 [a00 a01] [a10 a11])Creates 2x2 matrix from two 2d row vectors, given as two separate arguments.
Creates 2x2 matrix from two 2d row vectors, given as two separate arguments.
(rows->mat3x3 [a00 a01 a02] [a10 a11 a12] [a20 a21 a22])Creates 3x3 matrix from three 3d row vectors, given as three separate arguments.
Creates 3x3 matrix from three 3d row vectors, given as three separate arguments.
(rows->mat4x4 [a00 a01 a02 a03]
[a10 a11 a12 a13]
[a20 a21 a22 a23]
[a30 a31 a32 a33])Creates 4x4 matrix from four 4d row vectors, given as four separate arguments.
Creates 4x4 matrix from four 4d row vectors, given as four separate arguments.
(rows->RealMatrix rows)Creates an Apache Commons Math RealMatrix from a sequence of row vectors, or from an already-double[][]-typed array directly.
Identical to real-matrix.
See also cols->RealMatrix.
Creates an Apache Commons Math `RealMatrix` from a sequence of row vectors, or from an already-`double[][]`-typed array directly. Identical to [[real-matrix]]. See also [[cols->RealMatrix]].
(rrqr-decomposition mat)(rrqr-decomposition mat threshold)Performs Rank-Revealing QR decomposition of a matrix.
Decomposes a matrix A as A = Q x R x inv(P), where Q is orthogonal (QT x Q = I), R is upper triangular and P is a permutation matrix chosen so that the numerical rank of A can be estimated. The solver minimizes using the least squares method.
Parameters:
mat - a matrix to decompose.threshold (optional, default: 0.0) - singularity threshold used to determine whether a diagonal element of R is considered zero.Returns a decomposition value. Access individual parts with decomposition-component using one of the following keys:
:H - Householder reflector vectors, :Q, :QT, :R, :P (permutation) - matrices.:rank-fn - a function computing the numerical rank of the matrix; called with no arguments it uses a drop threshold of 0.0, or with one drop-threshold argument to control which singular directions are treated as zero.The result can be used as input for solve, inverse and singular?.
See also qr-decomposition, lu-decomposition, cholesky-decomposition, sv-decomposition, eigen-decomposition.
Performs Rank-Revealing QR decomposition of a matrix. Decomposes a matrix `A` as `A = Q x R x inv(P)`, where `Q` is orthogonal (`QT x Q = I`), `R` is upper triangular and `P` is a permutation matrix chosen so that the numerical rank of `A` can be estimated. The solver minimizes using the least squares method. Parameters: - `mat` - a matrix to decompose. - `threshold` (optional, default: `0.0`) - singularity threshold used to determine whether a diagonal element of `R` is considered zero. Returns a decomposition value. Access individual parts with `decomposition-component` using one of the following keys: - `:H` - Householder reflector vectors, `:Q`, `:QT`, `:R`, `:P` (permutation) - matrices. - `:rank-fn` - a function computing the numerical rank of the matrix; called with no arguments it uses a drop threshold of `0.0`, or with one `drop-threshold` argument to control which singular directions are treated as zero. The result can be used as input for `solve`, `inverse` and `singular?`. See also [[qr-decomposition]], [[lu-decomposition]], [[cholesky-decomposition]], [[sv-decomposition]], [[eigen-decomposition]].
(safe-sqrt vector)Applies fastmath.core/safe-sqrt to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/safe-sqrt]] to every element of a matrix, elementwise, returning a matrix of the same type.
(scale-cols A)(scale-cols A scale)Multiplies cols by a value (default: 1/(sqrt(sum(x^2)/(n-1)))) or a result of the function
Multiplies cols by a value (default: 1/(sqrt(sum(x^2)/(n-1)))) or a result of the function
(scale-rows A)(scale-rows A scale)Multiplies rows by a value (default: 1/(sqrt(sum(x^2)/(n-1)))) or a result of the function
Multiplies rows by a value (default: 1/(sqrt(sum(x^2)/(n-1)))) or a result of the function
(sec vector)Applies fastmath.core/sec to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/sec]] to every element of a matrix, elementwise, returning a matrix of the same type.
(sech vector)Applies fastmath.core/sech to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/sech]] to every element of a matrix, elementwise, returning a matrix of the same type.
(sfrac vector)Applies fastmath.core/sfrac to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/sfrac]] to every element of a matrix, elementwise, returning a matrix of the same type.
(sgn vector)Applies fastmath.core/sgn to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/sgn]] to every element of a matrix, elementwise, returning a matrix of the same type.
(shift-cols A)(shift-cols A shift)Shifts columns by a value or a result of the function (negative of mean by default)
Shifts columns by a value or a result of the function (negative of mean by default)
(shift-rows A)(shift-rows A shift)Shifts rows by a value or a result of the function (negative of mean by default)
Shifts rows by a value or a result of the function (negative of mean by default)
(sigmoid vector)Applies fastmath.core/sigmoid to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/sigmoid]] to every element of a matrix, elementwise, returning a matrix of the same type.
(signum vector)Applies fastmath.core/signum to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/signum]] to every element of a matrix, elementwise, returning a matrix of the same type.
(sin vector)Applies fastmath.core/sin to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/sin]] to every element of a matrix, elementwise, returning a matrix of the same type.
(sinc vector)Applies fastmath.core/sinc to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/sinc]] to every element of a matrix, elementwise, returning a matrix of the same type.
(singular-values A)Returns the singular values of a matrix.
Computed as the square roots of the (real parts of the) eigenvalues of A^T x A.
Parameters:
A - a matrix.Returns a sequence of singular values as doubles, one per eigenvalue of A^T x A, in the order produced by eigenvalues.
See also eigenvalues, sv-decomposition, norm.
Returns the singular values of a matrix. Computed as the square roots of the (real parts of the) eigenvalues of `A^T x A`. Parameters: - `A` - a matrix. Returns a sequence of singular values as doubles, one per eigenvalue of `A^T x A`, in the order produced by [[eigenvalues]]. See also [[eigenvalues]], [[sv-decomposition]], [[norm]].
(singular? mat)Checks whether a matrix, or a matrix decomposition, is singular.
A matrix is singular when it has no inverse, which is equivalent to a zero determinant.
Parameters:
mat - a matrix, or a decomposition value returned by one of the *-decomposition functions.Returns true when mat is singular, false otherwise. When mat is a decomposition, the result was computed once at decomposition time (typically from the solver or, when no solver is available, from the determinant).
Checks whether a matrix, or a matrix decomposition, is singular. A matrix is singular when it has no inverse, which is equivalent to a zero determinant. Parameters: - `mat` - a matrix, or a decomposition value returned by one of the `*-decomposition` functions. Returns `true` when `mat` is singular, `false` otherwise. When `mat` is a decomposition, the result was computed once at decomposition time (typically from the solver or, when no solver is available, from the determinant). See also [[inverse]], [[det]], [[solve]].
(sinh vector)Applies fastmath.core/sinh to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/sinh]] to every element of a matrix, elementwise, returning a matrix of the same type.
(solve A b)Solves the linear equation A x = b for x.
Parameters:
A - a square matrix, or a decomposition value returned by one of the *-decomposition functions.b - a vector, the right-hand side of the equation.When A is a plain matrix, it is solved via inverse, which requires A to be square; on a singular A, the result follows inverse's own per-representation behavior (nil for Mat2x2/Mat3x3/Mat4x4, an exception for RealMatrix/double[][]). When A is a decomposition, its associated solver is used directly; qr-decomposition, rrqr-decomposition, cholesky-decomposition and sv-decomposition solve using the least squares method, while lu-decomposition and eigen-decomposition (with real eigenvalues) solve exactly. On a decomposition of a singular matrix, throws SingularMatrixException regardless of the source matrix's own representation (check singular? first). A decomposition produced by the :colt backend of eigen-decomposition has no solver and can not be used here.
Returns a vector x satisfying the equation, or its least squares approximation.
See also inverse, singular?, decomposition-component.
Solves the linear equation `A x = b` for `x`. Parameters: - `A` - a square matrix, or a decomposition value returned by one of the `*-decomposition` functions. - `b` - a vector, the right-hand side of the equation. When `A` is a plain matrix, it is solved via `inverse`, which requires `A` to be square; on a singular `A`, the result follows `inverse`'s own per-representation behavior (`nil` for `Mat2x2`/`Mat3x3`/`Mat4x4`, an exception for `RealMatrix`/`double[][]`). When `A` is a decomposition, its associated solver is used directly; `qr-decomposition`, `rrqr-decomposition`, `cholesky-decomposition` and `sv-decomposition` solve using the least squares method, while `lu-decomposition` and `eigen-decomposition` (with real eigenvalues) solve exactly. On a decomposition of a singular matrix, throws `SingularMatrixException` regardless of the source matrix's own representation (check `singular?` first). A decomposition produced by the `:colt` backend of `eigen-decomposition` has no solver and can not be used here. Returns a vector `x` satisfying the equation, or its least squares approximation. See also [[inverse]], [[singular?]], [[decomposition-component]].
(sq vector)Applies fastmath.core/sq to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/sq]] to every element of a matrix, elementwise, returning a matrix of the same type.
(sqrt vector)Applies fastmath.core/sqrt to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/sqrt]] to every element of a matrix, elementwise, returning a matrix of the same type.
(standardize A)(standardize A rows?)Normalizes columns (or rows, when rows? is true) to have mean = 0 and stddev = 1
Normalizes columns (or rows, when `rows?` is true) to have mean = 0 and stddev = 1
(sub A)(sub A B)Subtracts matrices, C=A-B.
1-arity negates all elements, C=-A; identical to negate.
Subtracts matrices, C=A-B. 1-arity negates all elements, C=-A; identical to [[negate]].
(sv-decomposition mat)Performs Singular Value Decomposition (SVD) of a matrix.
Decomposes a matrix A as A = U x S x VT, where U and V are orthogonal and S is diagonal, holding the singular values in descending order. The solver minimizes using the least squares method.
Parameters:
mat - a matrix to decompose.Returns a decomposition value. Access individual parts with decomposition-component using one of the following keys:
:S, :U, :UT, :V, :VT - matrices.:singular-values - vector of singular values.:info - a map summarizing the decomposition:
:condition-number - ratio of the largest to the smallest singular value.:inv-condition-number - inverse of the condition number.:norm - L2 norm of the matrix.:rank - effective numerical rank of the matrix.:covariance-fn - a function returning the covariance matrix V x J x VT, where J is the inverse of the squared singular values; called with no arguments all singular values are used, or with one min-sv argument singular values below it are ignored.The result can be used as input for solve, inverse and singular?.
See also qr-decomposition, rrqr-decomposition, cholesky-decomposition, lu-decomposition, eigen-decomposition, singular-values.
Performs Singular Value Decomposition (SVD) of a matrix. Decomposes a matrix `A` as `A = U x S x VT`, where `U` and `V` are orthogonal and `S` is diagonal, holding the singular values in descending order. The solver minimizes using the least squares method. Parameters: - `mat` - a matrix to decompose. Returns a decomposition value. Access individual parts with `decomposition-component` using one of the following keys: - `:S`, `:U`, `:UT`, `:V`, `:VT` - matrices. - `:singular-values` - vector of singular values. - `:info` - a map summarizing the decomposition: - `:condition-number` - ratio of the largest to the smallest singular value. - `:inv-condition-number` - inverse of the condition number. - `:norm` - L2 norm of the matrix. - `:rank` - effective numerical rank of the matrix. - `:covariance-fn` - a function returning the covariance matrix `V x J x VT`, where `J` is the inverse of the squared singular values; called with no arguments all singular values are used, or with one `min-sv` argument singular values below it are ignored. The result can be used as input for `solve`, `inverse` and `singular?`. See also [[qr-decomposition]], [[rrqr-decomposition]], [[cholesky-decomposition]], [[lu-decomposition]], [[eigen-decomposition]], [[singular-values]].
(symmetric? A)(symmetric? A tolerance)Checks if matrix is symmetric
Checks if matrix is symmetric
(symmetrize A)Returns the symmetric part of a square matrix, (A + A^T) / 2.
Rounding errors make a matrix that is symmetric in exact arithmetic (a covariance matrix, a normal matrix X^T W X) slightly asymmetric. Entries that are close to zero can then differ by a large relative amount, which makes the symmetry check of cholesky-decomposition fail. Symmetrizing the matrix first removes the asymmetry exactly: the result is symmetric entry by entry.
Parameters:
A - a square matrix of any type of this namespaceReturns a matrix of the same type as A. A symmetric matrix is returned with the same values.
Throws ex-info with the :shape of A when A is not square.
See also symmetric?, cholesky-decomposition.
Returns the symmetric part of a square matrix, `(A + A^T) / 2`. Rounding errors make a matrix that is symmetric in exact arithmetic (a covariance matrix, a normal matrix `X^T W X`) slightly asymmetric. Entries that are close to zero can then differ by a large relative amount, which makes the symmetry check of [[cholesky-decomposition]] fail. Symmetrizing the matrix first removes the asymmetry exactly: the result is symmetric entry by entry. Parameters: - `A` - a square matrix of any type of this namespace Returns a matrix of the same type as `A`. A symmetric matrix is returned with the same values. Throws `ex-info` with the `:shape` of `A` when `A` is not square. See also [[symmetric?]], [[cholesky-decomposition]].
(tan vector)Applies fastmath.core/tan to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/tan]] to every element of a matrix, elementwise, returning a matrix of the same type.
(tanh vector)Applies fastmath.core/tanh to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/tanh]] to every element of a matrix, elementwise, returning a matrix of the same type.
(tmulm A B)Transposes and multiplies, C=A^TxB
Transposes and multiplies, C=A^TxB
(tmulmt A B)Transposes both and multiplies, C=A^TxB^T
Transposes both and multiplies, C=A^TxB^T
(trace A)Returns trace of the matrix (sum of diagonal elements)
Returns trace of the matrix (sum of diagonal elements)
(trunc vector)Applies fastmath.core/trunc to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/trunc]] to every element of a matrix, elementwise, returning a matrix of the same type.
(vtmul A v)Multiplies transposed vector by matrix, C=v^T A
Multiplies transposed vector by matrix, C=v^T A
(xlogx vector)Applies fastmath.core/xlogx to every element of a matrix, elementwise, returning a matrix of the same type.
Applies [[fastmath.core/xlogx]] to every element of a matrix, elementwise, returning a matrix of the same type.
(zero size)(zero size real-matrix?)(zero rows cols real-matrix?)Creates a zero matrix of the given size.
A square size of 2, 3 or 4 (1-arity, or 2/3-arity with a falsy real-matrix?) builds the corresponding fixed Mat2x2/Mat3x3/Mat4x4; any other size, a non-square rows/cols pair, or a truthy real-matrix? builds a RealMatrix.
Creates a zero matrix of the given size. A square size of 2, 3 or 4 (1-arity, or 2/3-arity with a falsy `real-matrix?`) builds the corresponding fixed `Mat2x2`/`Mat3x3`/`Mat4x4`; any other size, a non-square `rows`/`cols` pair, or a truthy `real-matrix?` builds a `RealMatrix`.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |