nk 1.0.0
C++ Numerical Computing Runtime
Loading...
Searching...
No Matches
Matrix Functions and Numerical Linear Algebra (matfun)

Linear equation solvers, matrix factorizations (LU, QR, SVD, Cholesky), condition numbers, and matrix exponentials. More...

Files

file  matfun.hpp
 Matrix functions, integer division with rounding modes, and matrix algebra.
file  balance.hpp
file  decompositions.hpp
file  eig.hpp
file  eigs.hpp
file  gsvd.hpp
file  ldl.hpp
file  matrix_functions.hpp
file  misc.hpp
file  norms.hpp
file  ordqz.hpp
file  ordschur.hpp
file  page_ops.hpp
file  predicates.hpp
file  properties.hpp
file  pseudo_subspace.hpp
file  qz.hpp
file  schur_convert.hpp
file  solvers.hpp
file  svd_sketch.hpp
file  vector_ops.hpp

Classes

struct  nk::linalg::BalanceResult
 Result of balance_impl. More...
struct  nk::linalg::NnlsResult
 Result of lsqnonneg_impl. More...

Functions

Value nk::builtin::idivide (const Value &a, const Value &b, const std::string &mode="fix", std::pmr::memory_resource *mr=nullptr)
 Integer division with specified rounding mode (idivide(A, B, opt)).
BalanceResult nk::linalg::balance_impl (const Value &A, bool noperm, std::pmr::memory_resource *mr=nullptr)
 Diagonal-similarity balancing for eigenvalue computations ([B, d, p] = balance(A, noperm)).
Value nk::linalg::chol (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Cholesky factorisation (R = chol(A)).
std::tuple< Value, Value, Value > nk::linalg::lu_decompose (const Value &A, std::pmr::memory_resource *mr=nullptr)
 LU with partial pivoting ([L, U, P] = lu_decompose(A)).
Value nk::linalg::lu_combined (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Combined L+U output (LU = lu(A) single-output form).
std::tuple< Value, Value > nk::linalg::qr_decompose (const Value &A, std::pmr::memory_resource *mr=nullptr)
 QR via Householder reflections ([Q, R] = qr_decompose(A)).
Value nk::linalg::qr_R_only (const Value &A, std::pmr::memory_resource *mr=nullptr)
 R-only QR output (R = qr(A) single-output form).
std::tuple< Value, Value, Value > nk::linalg::svd_decompose (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Singular value decomposition ([U, S, V] = svd_decompose(A)).
Value nk::linalg::svd_values (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Singular values only (s = svd(A) single-output form).
std::tuple< Value, Value > nk::linalg::qrupdate (const Value &Q, const Value &R, const Value &u, const Value &v, std::pmr::memory_resource *mr=nullptr)
 Rank-1 QR update ([Q1, R1] = qrupdate(Q, R, u, v)).
std::tuple< Value, Value > nk::linalg::qrinsert (const Value &Q, const Value &R, std::size_t k_1based, const Value &x, std::pmr::memory_resource *mr=nullptr)
 Insert column into QR ([Q1, R1] = qrinsert(Q, R, k, x)).
std::tuple< Value, Value > nk::linalg::qrdelete (const Value &Q, const Value &R, std::size_t k_1based, std::pmr::memory_resource *mr=nullptr)
 Delete column from QR ([Q1, R1] = qrdelete(Q, R, k)).
Value nk::linalg::cholupdate (const Value &R, const Value &x, int sign=1, std::pmr::memory_resource *mr=nullptr)
 Rank-1 Cholesky update / downdate (R1 = cholupdate(R, x[, sign])).
std::tuple< Value, Value > nk::linalg::eig_symmetric (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Symmetric eigendecomposition ([V, D] = eig(A)).
Value nk::linalg::eig_values (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Eigenvalues only (e = eig(A) single-output form, symmetric).
bool nk::linalg::isSymmetricApprox (const Value &A, double tol)
 Approximate-symmetry test used to pick the symmetric (Jacobi) vs general (char-poly) eig path.
Value nk::linalg::poly_of_matrix (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Characteristic polynomial of a matrix (p = poly(A)).
Value nk::linalg::eig_general_values (const Value &A, std::pmr::memory_resource *mr=nullptr)
 General (non-symmetric) eigenvalues via characteristic polynomial.
std::tuple< Value, Value > nk::linalg::eig_general_VD (const Value &A, std::pmr::memory_resource *mr=nullptr)
 General [V, D] eig for real-eigenvalue asymmetric A.
std::tuple< Value, Value > nk::linalg::hess (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Hessenberg reduction ([P, H] = hess(A)).
Value nk::linalg::hess_H_only (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Hessenberg-only output (H = hess(A) single-output form).
std::tuple< Value, Value > nk::linalg::schur_sym (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Schur decomposition for symmetric A ([U, T] = schur(A)).
std::tuple< Value, Value > nk::linalg::schur_general (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Real Schur decomposition for general (nonsymmetric) A ([U, T] = schur(A)).
Value nk::linalg::sylvester_sym (const Value &A, const Value &B, const Value &C, std::pmr::memory_resource *mr=nullptr)
 Sylvester equation for symmetric A and B (X = sylvester_sym(A, B, C)).
Value nk::linalg::polyeig_values (Span< const Value > coeffs, std::pmr::memory_resource *mr=nullptr)
 Polynomial eigenvalue problem (e = polyeig(A0, A1, ..., Ak), possibly [V, e]).
std::tuple< Value, Value > nk::linalg::polyeig_VE (Span< const Value > coeffs, std::pmr::memory_resource *mr=nullptr)
 Polynomial eigenvalue problem, eigenvectors + eigenvalues ([V, e] = polyeig(A0, ..., Ak)).
Value nk::linalg::ordeig (const Value &T, std::pmr::memory_resource *mr=nullptr)
 Eigenvalues from a (quasi-)triangular Schur factor (e = ordeig(T)).
Value nk::linalg::eigs_values (const Value &A, std::size_t k=6, std::pmr::memory_resource *mr=nullptr)
 Compute top k eigenvalues of A as a column vector.
std::tuple< Value, Value > nk::linalg::eigs (const Value &A, std::size_t k=6, std::pmr::memory_resource *mr=nullptr)
 Compute top k eigenvalues and eigenvectors of A: [V, D] = eigs(A, k).
Value nk::linalg::svds_values (const Value &A, std::size_t k=6, std::pmr::memory_resource *mr=nullptr)
 Compute top k singular values of A as a column vector.
std::tuple< Value, Value, Value > nk::linalg::svds (const Value &A, std::size_t k=6, std::pmr::memory_resource *mr=nullptr)
 Compute top k singular values and vectors of A: [U, S, V] = svds(A, k).
Value nk::linalg::gsvd_values (const Value &A, const Value &B, std::pmr::memory_resource *mr=nullptr)
 Compute generalized singular values of matrix pair (A, B) as a column vector (ascending order).
std::tuple< Value, Value, Value, Value, Value > nk::linalg::gsvd (const Value &A, const Value &B, std::pmr::memory_resource *mr=nullptr)
 Full generalized SVD decomposition of matrix pair (A, B).
std::tuple< Value, Value, Value > nk::linalg::ldl (const Value &A, bool upper_form, bool p_as_vector, std::pmr::memory_resource *mr=nullptr)
 Block LDL' factorisation ([L, D, P] = ldl(A, upper_form, p_as_vector)).
Value nk::linalg::expm (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Matrix exponential (B = expm(A)).
Value nk::linalg::logm_sym (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Matrix logarithm for symmetric positive-definite A.
Value nk::linalg::sqrtm_sym (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Matrix square root for symmetric PSD A.
Value nk::linalg::sqrtm (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Matrix square root for general square matrix A (R = sqrtm(A)).
Value nk::linalg::logm (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Matrix logarithm for general square matrix A (L = logm(A)).
Value nk::linalg::sylvester (const Value &A, const Value &B, const Value &C, std::pmr::memory_resource *mr=nullptr)
 Solve Sylvester equation A · X + X · B = C (X = sylvester(A, B, C)).
Value nk::linalg::expmv (double t, const Value &A, const Value &v, std::pmr::memory_resource *mr=nullptr)
 Action of matrix exponential on a vector (w = expmv(t, A, v)).
Value nk::linalg::funm (const Value &A, std::function< std::complex< double >(std::complex< double >)> f, std::pmr::memory_resource *mr=nullptr)
 General matrix function evaluator funm(A, fun).
Value nk::linalg::funm (const Value &A, const std::string &fnName, std::pmr::memory_resource *mr=nullptr)
 General matrix function evaluator by string name ('exp', 'sin', 'cos', 'sinh', 'cosh').
std::pair< Value, Value > nk::linalg::rref (const Value &A, bool have_tol, double tol_user, std::pmr::memory_resource *mr=nullptr)
 Reduced row echelon form ([R, jb] = rref(A, have_tol, tol)).
std::pair< Value, Value > nk::linalg::planerot (const Value &xy, std::pmr::memory_resource *mr=nullptr)
 Givens plane rotation ([G, y_out] = planerot([x; y])).
Value nk::linalg::norm_value (const Value &x, double p, std::pmr::memory_resource *mr=nullptr)
 Vector / matrix p-norm (n = norm(x, p)).
Value nk::linalg::norm_inf (const Value &x, std::pmr::memory_resource *mr=nullptr)
 Inf-norm (n = norm(x, inf)).
Value nk::linalg::norm_fro (const Value &x, std::pmr::memory_resource *mr=nullptr)
 Frobenius norm (n = norm(A, 'fro')).
Value nk::linalg::vecnorm (const Value &A, double p=2.0, int dim=0, std::pmr::memory_resource *mr=nullptr)
 Vector p-norm along a dim (y = vecnorm(A, p, dim)).
std::tuple< Value, Value, Value, Value > nk::linalg::ordqz (const Value &AA, const Value &BB, const Value &Q, const Value &Z, const Value &select, std::pmr::memory_resource *mr=nullptr)
 Reorder Generalized Schur decomposition using a boolean select vector.
std::tuple< Value, Value, Value, Value > nk::linalg::ordqz (const Value &AA, const Value &BB, const Value &Q, const Value &Z, const std::string &domain, std::pmr::memory_resource *mr=nullptr)
 Reorder Generalized Schur decomposition using domain keyword ('lhp', 'rhp', 'uip', 'uop').
std::tuple< Value, Value > nk::linalg::ordschur (const Value &U, const Value &T, const Value &select, std::pmr::memory_resource *mr=nullptr)
 Reorder Schur decomposition A = U * T * U^H using a boolean select vector.
std::tuple< Value, Value > nk::linalg::ordschur (const Value &U, const Value &T, const std::string &domain, std::pmr::memory_resource *mr=nullptr)
 Reorder Schur decomposition A = U * T * U^H using domain keyword ('lhp', 'rhp', 'uip', 'uop').
Value nk::linalg::pageinv (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Page-wise inverse of a 3-D array (B = pageinv(A)).
Value nk::linalg::pageeig_values (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Page-wise eigenvalues (e = pageeig(A)).
std::tuple< Value, Value > nk::linalg::pageeig_VD (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Page-wise eigendecomposition ([V, D] = pageeig(A)).
Value nk::linalg::pagesvd_values (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Page-wise singular values (s = pagesvd(A)).
std::tuple< Value, Value, Value > nk::linalg::pagesvd_decompose (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Page-wise full SVD ([U, S, V] = pagesvd(A)).
Value nk::linalg::pagepinv (const Value &A, double tol=-1.0, std::pmr::memory_resource *mr=nullptr)
 Page-wise pseudo-inverse (B = pagepinv(A[, tol])).
Value nk::linalg::pagenorm (const Value &A, double p, std::pmr::memory_resource *mr=nullptr)
 Page-wise norm (y = pagenorm(A, p)).
Value nk::linalg::pagemldivide (const Value &A, const Value &B, std::pmr::memory_resource *mr=nullptr)
 Page-wise mldivide (X = pagemldivide(A, B)).
Value nk::linalg::pagemrdivide (const Value &A, const Value &B, std::pmr::memory_resource *mr=nullptr)
 Page-wise mrdivide (X = pagemrdivide(A, B)).
Value nk::linalg::pagelsqminnorm (const Value &A, const Value &B, bool have_tol, double tol_user, std::pmr::memory_resource *mr=nullptr)
 Page-wise minimum-norm least-squares (X = pagelsqminnorm(A, B[, tol])).
Value nk::linalg::isbanded (const Value &A, long lower, long upper, std::pmr::memory_resource *mr=nullptr)
 Banded structure test (tf = isbanded(A, lower, upper)).
Value nk::linalg::isdiag (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Diagonal structure (tf = isdiag(A)). True iff isbanded(A, 0, 0).
Value nk::linalg::istril (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Lower-triangular structure (tf = istril(A)).
Value nk::linalg::istriu (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Upper-triangular structure (tf = istriu(A)).
Value nk::linalg::issymmetric (const Value &A, bool skew=false, std::pmr::memory_resource *mr=nullptr)
 Symmetry test (tf = issymmetric(A, skew)).
Value nk::linalg::ishermitian (const Value &A, bool skew=false, std::pmr::memory_resource *mr=nullptr)
 Hermitian test (tf = ishermitian(A, skew)).
std::pair< Value, Value > nk::linalg::bandwidth (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Bandwidth pair ([lower, upper] = bandwidth(A)).
Value nk::linalg::bandwidthOpt (const Value &A, const std::string &which, std::pmr::memory_resource *mr=nullptr)
 Single-output bandwidth selector (x = bandwidth(A, which)).
Value nk::linalg::inv (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Matrix inverse via LU (B = inv(A)).
Value nk::linalg::trace (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Trace = sum of diagonal (t = trace(A)).
Value nk::linalg::det (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Determinant via LU with partial pivoting (d = det(A)).
Value nk::linalg::rank_of (const Value &A, double tol=-1.0, std::pmr::memory_resource *mr=nullptr)
 Numerical rank (r = rank(A, tol)).
Value nk::linalg::cond_2norm (const Value &A, std::pmr::memory_resource *mr=nullptr)
 2-norm condition number (c = cond(A)).
Value nk::linalg::cond_pnorm (const Value &A, int p_kind, std::pmr::memory_resource *mr=nullptr)
 General p-norm condition number (c = cond(A, p)).
Value nk::linalg::normest (const Value &A, std::pmr::memory_resource *mr=nullptr)
 2-norm estimate (n = normest(A)).
Value nk::linalg::rcond (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Reciprocal 1-norm condition estimate (c = rcond(A)).
Value nk::linalg::condest (const Value &A, std::pmr::memory_resource *mr=nullptr)
 1-norm condition number estimate (c = condest(A)).
Value nk::linalg::condeig (const Value &A, std::pmr::memory_resource *mr=nullptr)
 Eigenvalue condition numbers (c = condeig(A)).
Value nk::linalg::pinv (const Value &A, double tol=-1.0, std::pmr::memory_resource *mr=nullptr)
 Moore-Penrose pseudoinverse (P = pinv(A, tol)).
Value nk::linalg::orth (const Value &A, double tol=-1.0, std::pmr::memory_resource *mr=nullptr)
 Orthonormal basis for range(A) (Q = orth(A, tol)).
Value nk::linalg::null_basis (const Value &A, double tol=-1.0, std::pmr::memory_resource *mr=nullptr)
 Orthonormal basis for null(A) (N = null(A, tol)).
Value nk::linalg::subspace (const Value &A, const Value &B, std::pmr::memory_resource *mr=nullptr)
 Subspace angle (theta = subspace(A, B)).
std::tuple< Value, Value, Value, Value > nk::linalg::qz (const Value &A, const Value &B, std::pmr::memory_resource *mr=nullptr)
 Generalized Schur decomposition of matrix pair (A, B).
std::tuple< Value, Value > nk::linalg::cdf2rdf (const Value &V, const Value &D, std::pmr::memory_resource *mr=nullptr)
 Complex-diagonal → real-block-diagonal Schur form ([VR, DR] = cdf2rdf(V, D)).
std::tuple< Value, Value > nk::linalg::rsf2csf (const Value &UR, const Value &TR, std::pmr::memory_resource *mr=nullptr)
 Real Schur → complex Schur form ([U, T] = rsf2csf(UR, TR)).
Value nk::linalg::linsolve (const Value &A, const Value &B, std::pmr::memory_resource *mr=nullptr)
 Solve A·X = B (X = linsolve(A, B)).
Value nk::linalg::lsqminnorm (const Value &A, const Value &B, bool have_tol, double tol_user, std::pmr::memory_resource *mr=nullptr)
 Minimum-norm least-squares solution (X = lsqminnorm(A, B)).
NnlsResult nk::linalg::lsqnonneg_impl (const Value &C, const Value &d, std::pmr::memory_resource *mr=nullptr)
 Non-negative least-squares (x = lsqnonneg(C, d)).
std::tuple< Value, Value, Value > nk::linalg::svdsketch (const Value &A, double tol=1e-6, std::pmr::memory_resource *mr=nullptr)
 Randomized low-rank SVD approximation ([U, S, V] = svdsketch(A, tol)).
std::tuple< Value, Value, Value > nk::linalg::svdappend (const Value &U, const Value &S, const Value &V, const Value &A_new, std::pmr::memory_resource *mr=nullptr)
 Incremental SVD update ([U, S, V] = svdappend(U, S, V, A_new)).
Value nk::linalg::cross (const Value &a, const Value &b, std::pmr::memory_resource *mr=nullptr)
 Cross product of two 3-vectors or 3 × N / N × 3 batches.
Value nk::linalg::cross (const Value &a, const Value &b, int dim, std::pmr::memory_resource *mr=nullptr)
 Cross product along a given dimension (cross(A, B, dim)).
Value nk::linalg::dot (const Value &a, const Value &b, std::pmr::memory_resource *mr=nullptr)
 Dot product of two equal-length vectors.
Value nk::linalg::dot (const Value &a, const Value &b, int dim, std::pmr::memory_resource *mr=nullptr)
 Dot product along a given dimension (dot(A, B, dim)).
Value nk::linalg::kron (const Value &a, const Value &b, std::pmr::memory_resource *mr=nullptr)
 Kronecker product (K = kron(A, B)).

Detailed Description

Linear equation solvers, matrix factorizations (LU, QR, SVD, Cholesky), condition numbers, and matrix exponentials.

Function Documentation

◆ balance_impl()

BalanceResult nk::linalg::balance_impl ( const Value & A,
bool noperm,
std::pmr::memory_resource * mr = nullptr )

Diagonal-similarity balancing for eigenvalue computations ([B, d, p] = balance(A, noperm)).

Parlett-Reinsch (1969). v1 implements only the scaling phase (permutation phase deferred; behaves like balance(A, 'noperm')).

◆ cdf2rdf()

std::tuple< Value, Value > nk::linalg::cdf2rdf ( const Value & V,
const Value & D,
std::pmr::memory_resource * mr = nullptr )

Complex-diagonal → real-block-diagonal Schur form ([VR, DR] = cdf2rdf(V, D)).

Given complex V and complex-diagonal D from eig(A) for a real A, returns real VR, DR such that A == VR · DR · inv(VR). Conjugate eigenvalue pairs a ± b·i become real 2×2 blocks [a -b; b a] on the diagonal of DR; the corresponding pair of complex-conjugate eigenvector columns of V become real columns Re(v), Im(v) in VR.

Real eigenvalues pass through unchanged.

◆ chol()

Value nk::linalg::chol ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Cholesky factorisation (R = chol(A)).

Returns the upper-triangular R such that R' · R == A.

Parameters
ASymmetric positive-definite matrix.
mrMemory resource (nullptr → process default).
Returns
Upper-triangular factor R.
Exceptions
ErrorNon-square or not positive-definite (m:chol:notPosDef).

◆ cholupdate()

Value nk::linalg::cholupdate ( const Value & R,
const Value & x,
int sign = 1,
std::pmr::memory_resource * mr = nullptr )

Rank-1 Cholesky update / downdate (R1 = cholupdate(R, x[, sign])).

Given an upper-triangular R from chol(A), returns R1 such that R1' * R1 == R' * R + sign * x * x' where sign is +1 (default, update) or -1 (downdate; may fail if the result is not positive-definite).

Update path uses the standard Givens-rotation rank-1 update (O(n²)). Downdate path forms R'·R − x·x' and re-cholesky's (O(n³)) — see KNOWN GAP in the source.

Parameters
RUpper-triangular Cholesky factor.
xUpdate vector, length n.
sign+1 for update, -1 for downdate.
mrMemory resource.
Returns
Updated upper-triangular factor.
Exceptions
ErrorShape mismatch, or downdate breaks positive-definiteness.

◆ cond_2norm()

Value nk::linalg::cond_2norm ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

2-norm condition number (c = cond(A)).

sigma_max / sigma_min via SVD. Returns Inf for singular A.

◆ cond_pnorm()

Value nk::linalg::cond_pnorm ( const Value & A,
int p_kind,
std::pmr::memory_resource * mr = nullptr )

General p-norm condition number (c = cond(A, p)).

cond(A, p) = norm(A, p) · norm(inv(A), p) for p in {1, Inf, "fro"}. p == 2 routes through cond_2norm. Returns Inf for singular A.

p_kind: 1 → 1-norm, 2 → 2-norm, 3 → Inf-norm, 4 → Frobenius. (Integer enum to avoid a string allocation in the hot path.)

◆ condeig()

Value nk::linalg::condeig ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Eigenvalue condition numbers (c = condeig(A)).

For each eigenvalue λ_i, the condition number is s_i = 1/|y_i' x_i| where x_i is the right eigenvector and y_i the left eigenvector (both normalised). Symmetric A → all s_i == 1. Non-normal A → large s_i marks ill-conditioned eigenvalues.

Returns a column vector of length n. The 3-output form [V, D, s] = condeig(A) is exposed via the engine adapter.

◆ condest()

Value nk::linalg::condest ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

1-norm condition number estimate (c = condest(A)).

condest(A) ≈ norm(A, 1) * norm(inv(A), 1). The reciprocal of rcond — accurate on well-conditioned A; on near-singular A differs slightly from MATLAB's LAPACK dgecon-based estimator (Higham 1988 power-iteration approximator).

◆ cross() [1/2]

Value nk::linalg::cross ( const Value & a,
const Value & b,
int dim,
std::pmr::memory_resource * mr = nullptr )

Cross product along a given dimension (cross(A, B, dim)).

Evaluates the cross product treating the length-3 vectors as running along dimension dim (1 = down columns, 2 = across rows). A and B must have length 3 in dimension dim. dim = 0 selects the default (first dimension of length 3). 2-D only.

Parameters
aFirst operand.
bSecond operand, same shape as a.
dimOperating dimension (0 = auto, 1 = columns, 2 = rows).
mrMemory resource (nullptr → process default).
Returns
Cross product (same shape as the inputs).
Exceptions
ErrorShape mismatch, dim length ≠ 3, or invalid dim.
See also
cross, dot

◆ cross() [2/2]

Value nk::linalg::cross ( const Value & a,
const Value & b,
std::pmr::memory_resource * mr = nullptr )

Cross product of two 3-vectors or 3 × N / N × 3 batches.

Operates along the first dimension of length 3. Result has the same shape as the inputs.

Parameters
aFirst operand (vector or 3-row/3-col batch).
bSecond operand, same shape as a.
mrMemory resource (nullptr → process default).
Returns
Cross product.
Exceptions
ErrorShape mismatch, or no dimension of length 3.
See also
dot

◆ det()

Value nk::linalg::det ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Determinant via LU with partial pivoting (d = det(A)).

det(A) = sign(P) · prod(diag(U)) where A = P·L·U.

◆ dot() [1/2]

Value nk::linalg::dot ( const Value & a,
const Value & b,
int dim,
std::pmr::memory_resource * mr = nullptr )

Dot product along a given dimension (dot(A, B, dim)).

Computes sum(conj(A) .* B, dim) — the dot product of corresponding vectors along dimension dim. dim = 1 reduces down columns (result is 1 × W), dim = 2 reduces across rows (result is H × 1). dim = 0 selects the default behavior (vector → scalar, matrix → per-column). 2-D only.

Parameters
aFirst operand.
bSecond operand, same shape as a.
dimReduction dimension (0 = auto, 1 = columns, 2 = rows).
mrMemory resource (nullptr → process default).
Returns
Dot product reduced along dim.
Exceptions
ErrorShape mismatch, 3-D input, or invalid dim.
See also
dot

◆ dot() [2/2]

Value nk::linalg::dot ( const Value & a,
const Value & b,
std::pmr::memory_resource * mr = nullptr )

Dot product of two equal-length vectors.

Parameters
aFirst vector.
bSecond vector (same numel as a).
mrMemory resource (nullptr → process default).
Returns
Scalar dot product.
Exceptions
ErrorLength mismatch.
See also
cross

◆ eig_general_values()

Value nk::linalg::eig_general_values ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

General (non-symmetric) eigenvalues via characteristic polynomial.

Less numerically stable than QR iteration but works for moderate n.

◆ eig_general_VD()

std::tuple< Value, Value > nk::linalg::eig_general_VD ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

General [V, D] eig for real-eigenvalue asymmetric A.

Throws if any eigenvalue has non-zero imaginary part (Francis QR for complex eigvec extraction is deferred).

◆ eig_symmetric()

std::tuple< Value, Value > nk::linalg::eig_symmetric ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Symmetric eigendecomposition ([V, D] = eig(A)).

Classical Jacobi rotations. Returns (V, D) such that A·V == V·D, V orthogonal, D diagonal (ascending real eigenvalues). Throws if A is not symmetric within tolerance.

◆ expm()

Value nk::linalg::expm ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Matrix exponential (B = expm(A)).

Padé(6) approximation with scaling-and-squaring (Higham 2005).

◆ expmv()

Value nk::linalg::expmv ( double t,
const Value & A,
const Value & v,
std::pmr::memory_resource * mr = nullptr )

Action of matrix exponential on a vector (w = expmv(t, A, v)).

Computes w ≈ exp(t · A) · v without forming the full matrix exponential. Krylov subspace via Arnoldi + small expm on the resulting upper Hessenberg matrix; fixed Krylov dimension m = min(30, n) in v1 (adaptive refinement deferred).

Faster than expm(t*A) * v for large n when v is a single vector; identical to it at machine precision for small n.

◆ funm()

Value nk::linalg::funm ( const Value & A,
std::function< std::complex< double >(std::complex< double >)> f,
std::pmr::memory_resource * mr = nullptr )

General matrix function evaluator funm(A, fun).

Evaluates scalar function f on square matrix A via Schur–Parlett algorithm.

◆ gsvd()

std::tuple< Value, Value, Value, Value, Value > nk::linalg::gsvd ( const Value & A,
const Value & B,
std::pmr::memory_resource * mr = nullptr )

Full generalized SVD decomposition of matrix pair (A, B).

Returns std::tuple<Value, Value, Value, Value, Value>{U, V, X, C, S} such that A = U * C * X^H and B = V * S * X^H.

◆ hess()

std::tuple< Value, Value > nk::linalg::hess ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Hessenberg reduction ([P, H] = hess(A)).

A = P·H·P', P orthogonal, H upper-Hessenberg.

◆ idivide()

Value nk::builtin::idivide ( const Value & a,
const Value & b,
const std::string & mode = "fix",
std::pmr::memory_resource * mr = nullptr )

Integer division with specified rounding mode (idivide(A, B, opt)).

Divides a by b with integer rounding according to mode. At least one operand must be an integer class (int8..int64, uint8..uint64). The other operand can be a same-class integer or a scalar double.

Parameters
aNumerator matrix or scalar.
bDenominator matrix or scalar.
modeRounding mode: "fix" (towards zero, default), "floor" (towards -inf), "ceil" (towards +inf), or "round" (nearest integer).
mrMemory resource for allocations (nullptr for default).
Returns
Quotient after integer division, maintaining integer type of operands.
Exceptions
std::runtime_errorIf neither argument is an integer class or if invalid mode is passed.
See also
nk::builtin::rdivide, nk::builtin::ldivide

◆ inv()

Value nk::linalg::inv ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Matrix inverse via LU (B = inv(A)).

Prefer linsolve / \\endiskip for solving A·x = b; this function exists when the inverse itself is needed as a matrix.

Parameters
ASquare matrix.
mrMemory resource (nullptr → process default).
Returns
A^{-1}.
Exceptions
ErrorNon-square or singular (m:inv:singular).

◆ isbanded()

Value nk::linalg::isbanded ( const Value & A,
long lower,
long upper,
std::pmr::memory_resource * mr = nullptr )

Banded structure test (tf = isbanded(A, lower, upper)).

True iff entries outside the [-lower, +upper] diagonal band are exactly zero. Exact comparison (no tolerance).

◆ issymmetric()

Value nk::linalg::issymmetric ( const Value & A,
bool skew = false,
std::pmr::memory_resource * mr = nullptr )

Symmetry test (tf = issymmetric(A, skew)).

skew == false (default): A == A.' (transpose, no conj). skew == true: A == -A.'.

◆ isSymmetricApprox()

bool nk::linalg::isSymmetricApprox ( const Value & A,
double tol )

Approximate-symmetry test used to pick the symmetric (Jacobi) vs general (char-poly) eig path.

True iff A is square and every off-diagonal pair matches within tol·(1 + max(|A(i,j)|, |A(j,i)|)). Shared by eig_symmetric's own guard and the eig register-side auto-dispatch.

◆ kron()

Value nk::linalg::kron ( const Value & a,
const Value & b,
std::pmr::memory_resource * mr = nullptr )

Kronecker product (K = kron(A, B)).

Output is (rA·rB) × (cA·cB); the (i, j)-th block (rB × cB) equals A(i, j) · B. DOUBLE only — COMPLEX inputs throw.

Parameters
aFirst operand.
bSecond operand.
mrMemory resource (nullptr → process default).
Returns
Kronecker product.
Exceptions
ErrorCOMPLEX input, or rank > 2.

◆ ldl()

std::tuple< Value, Value, Value > nk::linalg::ldl ( const Value & A,
bool upper_form,
bool p_as_vector,
std::pmr::memory_resource * mr = nullptr )

Block LDL' factorisation ([L, D, P] = ldl(A, upper_form, p_as_vector)).

v1 implements Crout LDL' without pivoting (works for PD/ND and most indefinite matrices). P is identity in v1. Bunch-Kaufman 2×2 pivoting and the sparse [L, D, P, C] form are deferred.

Parameters
ASymmetric matrix.
upper_formWhen true, return upper-triangular L.
p_as_vectorWhen true, return P as a 1 × n perm vector.
mrMemory resource (nullptr → process default).
Returns
(L, D, P) triple.

◆ linsolve()

Value nk::linalg::linsolve ( const Value & A,
const Value & B,
std::pmr::memory_resource * mr = nullptr )

Solve A·X = B (X = linsolve(A, B)).

LU for square A, Householder QR for tall A (least-squares). Backs mldivide / \\endiskip — but mldivide lives in toolboxes/builtin (it's the operator implementation). This is the user-facing linsolve().

◆ logm()

Value nk::linalg::logm ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Matrix logarithm for general square matrix A (L = logm(A)).

Via Schur decomposition and inverse scaling-and-squaring.

◆ lsqminnorm()

Value nk::linalg::lsqminnorm ( const Value & A,
const Value & B,
bool have_tol,
double tol_user,
std::pmr::memory_resource * mr = nullptr )

Minimum-norm least-squares solution (X = lsqminnorm(A, B)).

Computes pinv(A, tol) · B. Full-rank A collapses to A\B; rank- deficient A returns the unique min-norm solution.

◆ lsqnonneg_impl()

NnlsResult nk::linalg::lsqnonneg_impl ( const Value & C,
const Value & d,
std::pmr::memory_resource * mr = nullptr )

Non-negative least-squares (x = lsqnonneg(C, d)).

Lawson-Hanson active-set NNLS: minimises ‖C·x − d‖₂ subject to x ≥ 0. Returns the full NnlsResult for the multi-output forms.

◆ lu_decompose()

std::tuple< Value, Value, Value > nk::linalg::lu_decompose ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

LU with partial pivoting ([L, U, P] = lu_decompose(A)).

P · A == L · U where L is unit-lower-triangular, U is upper-triangular, P is a permutation matrix.

◆ norm_fro()

Value nk::linalg::norm_fro ( const Value & x,
std::pmr::memory_resource * mr = nullptr )

Frobenius norm (n = norm(A, 'fro')).

sqrt(sum(A.^2)).

◆ norm_inf()

Value nk::linalg::norm_inf ( const Value & x,
std::pmr::memory_resource * mr = nullptr )

Inf-norm (n = norm(x, inf)).

Vector: max(|v|). Matrix: max row sum.

◆ norm_value()

Value nk::linalg::norm_value ( const Value & x,
double p,
std::pmr::memory_resource * mr = nullptr )

Vector / matrix p-norm (n = norm(x, p)).

Vector input: (Σ |x|^p)^(1/p); p = 1 → sum of abs, p = 2 → Euclidean. Matrix input: p = 1 → max column sum, p = 2 → largest singular value (via SVD).

Parameters
xInput array (vector or 2-D matrix).
pNorm order (positive real).
mrMemory resource (nullptr → process default).
Returns
Scalar norm.
Exceptions
ErrorUnsupported matrix p-norm.
See also
norm_inf, norm_fro, vecnorm

◆ normest()

Value nk::linalg::normest ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

2-norm estimate (n = normest(A)).

Currently equivalent to svd(A)(1) — no power-iteration shortcut yet (correctness over performance).

◆ null_basis()

Value nk::linalg::null_basis ( const Value & A,
double tol = -1.0,
std::pmr::memory_resource * mr = nullptr )

Orthonormal basis for null(A) (N = null(A, tol)).

Columns of V from SVD whose corresponding sigma is below tol.

◆ ordeig()

Value nk::linalg::ordeig ( const Value & T,
std::pmr::memory_resource * mr = nullptr )

Eigenvalues from a (quasi-)triangular Schur factor (e = ordeig(T)).

Returns eigenvalues in the order they appear in T. For a true triangular T (real eigenvalues only — e.g. diagonal symmetric Schur factor) returns diag(T). For a real quasi-triangular Schur factor, 2×2 sub-diagonal blocks emit conjugate-pair complex eigenvalues from (a ± √(c·d)) where the block is [[a c]; [d a]].

Unlike eig(T), ordeig does NOT sort; the result preserves the diagonal/block ordering of T exactly — useful for reading off eigenvalues from a manually-reordered Schur factor.

◆ ordschur()

std::tuple< Value, Value > nk::linalg::ordschur ( const Value & U,
const Value & T,
const Value & select,
std::pmr::memory_resource * mr = nullptr )

Reorder Schur decomposition A = U * T * U^H using a boolean select vector.

Returns std::tuple<Value, Value>{U_out, T_out}.

◆ orth()

Value nk::linalg::orth ( const Value & A,
double tol = -1.0,
std::pmr::memory_resource * mr = nullptr )

Orthonormal basis for range(A) (Q = orth(A, tol)).

Columns of U from SVD whose corresponding sigma exceeds tol.

◆ pageeig_values()

Value nk::linalg::pageeig_values ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Page-wise eigenvalues (e = pageeig(A)).

For each page, returns a column of eigenvalues. Output is n × 1 × pages. Only real eigenvalues supported (matches the 2-D eig behaviour).

◆ pageeig_VD()

std::tuple< Value, Value > nk::linalg::pageeig_VD ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Page-wise eigendecomposition ([V, D] = pageeig(A)).

Returns (V_pages, D_pages), each shaped n × n × pages.

◆ pageinv()

Value nk::linalg::pageinv ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Page-wise inverse of a 3-D array (B = pageinv(A)).

Each m × n page is independently inverted via LU.

◆ pagemldivide()

Value nk::linalg::pagemldivide ( const Value & A,
const Value & B,
std::pmr::memory_resource * mr = nullptr )

Page-wise mldivide (X = pagemldivide(A, B)).

For each page solves A_p · X_p = B_p. Both inputs must have matching page counts, or one of them may be 2-D (broadcast).

◆ pagemrdivide()

Value nk::linalg::pagemrdivide ( const Value & A,
const Value & B,
std::pmr::memory_resource * mr = nullptr )

Page-wise mrdivide (X = pagemrdivide(A, B)).

For each page solves X_p · B_p = A_p (right division).

◆ pagenorm()

Value nk::linalg::pagenorm ( const Value & A,
double p,
std::pmr::memory_resource * mr = nullptr )

Page-wise norm (y = pagenorm(A, p)).

p may be a positive real (2-norm by default), Inf, or one of "fro" / "inf". Output is 1 × 1 × pages.

◆ pinv()

Value nk::linalg::pinv ( const Value & A,
double tol = -1.0,
std::pmr::memory_resource * mr = nullptr )

Moore-Penrose pseudoinverse (P = pinv(A, tol)).

Via SVD: pinv(A) = V · S⁺ · U' where S⁺ inverts non-zero singular values above tol.

◆ planerot()

std::pair< Value, Value > nk::linalg::planerot ( const Value & xy,
std::pmr::memory_resource * mr = nullptr )

Givens plane rotation ([G, y_out] = planerot([x; y])).

Returns G such that G · [x; y] = [r; 0] where r = hypot(x, y). Real-only.

◆ poly_of_matrix()

Value nk::linalg::poly_of_matrix ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Characteristic polynomial of a matrix (p = poly(A)).

Souriau-Faddeev-LeVerrier. roots(p) == eig(A).

◆ polyeig_values()

Value nk::linalg::polyeig_values ( Span< const Value > coeffs,
std::pmr::memory_resource * mr = nullptr )

Polynomial eigenvalue problem (e = polyeig(A0, A1, ..., Ak), possibly [V, e]).

Finds scalars λ and non-zero vectors x such that (A0 + λ·A1 + λ²·A2 + … + λ^k·Ak) · x = 0.

Implementation: linearisation via the standard companion matrix of size k·n × k·n; eigenvalues of the companion are the polynomial eigenvalues. Right eigenvectors of the companion have a Kronecker structure [x; λ x; λ² x; …]; the first n rows give x.

Eigenvalue-only form returns a column of k·n values; the matrix form [V, e] = polyeig(...) returns the eigenvectors in an n × k·n matrix. The matrix form requires real eigenvalues (Francis QR for complex pairs is deferred).

Parameters
coeffsSpan of k+1 polynomial coefficient matrices, ordered constant first: [A0, A1, ..., Ak]. All n × n, same size. Ak (leading) must be non-singular.
mrMemory resource.
Returns
Eigenvalue column of length k·n (possibly COMPLEX).

◆ polyeig_VE()

std::tuple< Value, Value > nk::linalg::polyeig_VE ( Span< const Value > coeffs,
std::pmr::memory_resource * mr = nullptr )

Polynomial eigenvalue problem, eigenvectors + eigenvalues ([V, e] = polyeig(A0, ..., Ak)).

Throws if any eigenvalue is complex (needs Francis QR).

◆ qr_decompose()

std::tuple< Value, Value > nk::linalg::qr_decompose ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

QR via Householder reflections ([Q, R] = qr_decompose(A)).

Full-size form (not "econ"). A == Q · R. Q is m × m orthogonal, R is m × n upper-triangular.

◆ qrdelete()

std::tuple< Value, Value > nk::linalg::qrdelete ( const Value & Q,
const Value & R,
std::size_t k_1based,
std::pmr::memory_resource * mr = nullptr )

Delete column from QR ([Q1, R1] = qrdelete(Q, R, k)).

Given A = Q * R (m × n), returns the factors of A1 formed by removing the k-th column. Givens-rotation chase to restore upper-triangular R. k is 1-based.

KNOWN GAP: only column deletion in v1; row deletion deferred.

◆ qrinsert()

std::tuple< Value, Value > nk::linalg::qrinsert ( const Value & Q,
const Value & R,
std::size_t k_1based,
const Value & x,
std::pmr::memory_resource * mr = nullptr )

Insert column into QR ([Q1, R1] = qrinsert(Q, R, k, x)).

Given A = Q * R (m × n), returns the factors of A1 = [A(:, 1:k-1), x, A(:, k:end)] (m × (n+1)). Givens-rotation algorithm. k is 1-based per MATLAB convention.

KNOWN GAP: only column insertion in v1; row insertion deferred.

◆ qrupdate()

std::tuple< Value, Value > nk::linalg::qrupdate ( const Value & Q,
const Value & R,
const Value & u,
const Value & v,
std::pmr::memory_resource * mr = nullptr )

Rank-1 QR update ([Q1, R1] = qrupdate(Q, R, u, v)).

Given A = Q * R, returns (Q1, R1) such that Q1 * R1 == A + u * v'. Standard Givens-rotation algorithm (Daniel/Gragg/Kaufman/Stewart 1976), O(m·n).

◆ qz()

std::tuple< Value, Value, Value, Value > nk::linalg::qz ( const Value & A,
const Value & B,
std::pmr::memory_resource * mr = nullptr )

Generalized Schur decomposition of matrix pair (A, B).

Returns std::tuple<Value, Value, Value, Value>{AA, BB, Q, Z} such that Q * A * Z = AA and Q * B * Z = BB, where AA is upper quasi-triangular, BB is upper triangular, and Q, Z are unitary.

◆ rank_of()

Value nk::linalg::rank_of ( const Value & A,
double tol = -1.0,
std::pmr::memory_resource * mr = nullptr )

Numerical rank (r = rank(A, tol)).

Count of singular values above tol. Default tol = max(size(A))·eps(max(svd(A))).

◆ rcond()

Value nk::linalg::rcond ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Reciprocal 1-norm condition estimate (c = rcond(A)).

Cheap path: 1 / (norm(A, 1) · norm(inv(A), 1)). Returns 0 for singular A. KNOWN GAP: accurate on well-conditioned cases; differs from LAPACK's dgecon on near-singular matrices.

◆ rref()

std::pair< Value, Value > nk::linalg::rref ( const Value & A,
bool have_tol,
double tol_user,
std::pmr::memory_resource * mr = nullptr )

Reduced row echelon form ([R, jb] = rref(A, have_tol, tol)).

jb is the 1-based pivot-column indices. Real-only in v1.

◆ rsf2csf()

std::tuple< Value, Value > nk::linalg::rsf2csf ( const Value & UR,
const Value & TR,
std::pmr::memory_resource * mr = nullptr )

Real Schur → complex Schur form ([U, T] = rsf2csf(UR, TR)).

Given a real quasi-triangular Schur factor TR (real diagonal + 2×2 blocks for complex eigenvalue pairs) and the corresponding real orthogonal UR, returns complex U, upper-triangular T such that A == U · T · U'.

Each 2×2 diagonal block of TR is diagonalised via a unitary rotation that brings its complex-conjugate eigenvalues onto the diagonal of T; the corresponding two columns of U get the same rotation applied.

◆ schur_general()

std::tuple< Value, Value > nk::linalg::schur_general ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Real Schur decomposition for general (nonsymmetric) A ([U, T] = schur(A)).

A = U·T·Uᵀ with U orthogonal and T the real Schur form — upper quasi-triangular, with 1×1 diagonal blocks for real eigenvalues and 2×2 blocks for complex-conjugate pairs. Hessenberg reduction + Francis double-shift QR. (The Schur form is not unique; eigenvalues + U·T·Uᵀ are the invariants.)

◆ schur_sym()

std::tuple< Value, Value > nk::linalg::schur_sym ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Schur decomposition for symmetric A ([U, T] = schur(A)).

Equivalent to eigendecomposition: A = U·T·U', T diagonal.

◆ sqrtm()

Value nk::linalg::sqrtm ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Matrix square root for general square matrix A (R = sqrtm(A)).

Via Björck–Hammarling recurrence on Schur form.

◆ subspace()

Value nk::linalg::subspace ( const Value & A,
const Value & B,
std::pmr::memory_resource * mr = nullptr )

Subspace angle (theta = subspace(A, B)).

theta = acos(min(svd(orth(A)' · orth(B)))). Returns radians in [0, π/2].

◆ svd_decompose()

std::tuple< Value, Value, Value > nk::linalg::svd_decompose ( const Value & A,
std::pmr::memory_resource * mr = nullptr )

Singular value decomposition ([U, S, V] = svd_decompose(A)).

A = U · S · V'. One-sided Jacobi rotations.

◆ svdappend()

std::tuple< Value, Value, Value > nk::linalg::svdappend ( const Value & U,
const Value & S,
const Value & V,
const Value & A_new,
std::pmr::memory_resource * mr = nullptr )

Incremental SVD update ([U, S, V] = svdappend(U, S, V, A_new)).

Appends new columns to existing SVD decomposition using Brand's algorithm.

◆ svdsketch()

std::tuple< Value, Value, Value > nk::linalg::svdsketch ( const Value & A,
double tol = 1e-6,
std::pmr::memory_resource * mr = nullptr )

Randomized low-rank SVD approximation ([U, S, V] = svdsketch(A, tol)).

Uses Halko–Martinsson–Tropp algorithm with Gaussian random test matrix.

◆ sylvester()

Value nk::linalg::sylvester ( const Value & A,
const Value & B,
const Value & C,
std::pmr::memory_resource * mr = nullptr )

Solve Sylvester equation A · X + X · B = C (X = sylvester(A, B, C)).

Via Bartels–Stewart algorithm on Schur forms.

◆ sylvester_sym()

Value nk::linalg::sylvester_sym ( const Value & A,
const Value & B,
const Value & C,
std::pmr::memory_resource * mr = nullptr )

Sylvester equation for symmetric A and B (X = sylvester_sym(A, B, C)).

Solves A·X + X·B = C via simultaneous diagonalisation.

◆ vecnorm()

Value nk::linalg::vecnorm ( const Value & A,
double p = 2.0,
int dim = 0,
std::pmr::memory_resource * mr = nullptr )

Vector p-norm along a dim (y = vecnorm(A, p, dim)).

Defaults: p = 2, dim = 0 (first non-singleton). p = Inf → max(|A|), p = -Inf → min(|A|).

Parameters
AInput array.
pNorm order (default 2).
dim1-based dimension (0 → first non-singleton).
mrMemory resource (nullptr → process default).
Returns
Norms reduced along dim.