26#include "backends/seq/impl.hpp"
27#include "backends/lapack/impl.hpp"
56 if (k == 0 || k > std::min(m, n))
57 throw std::invalid_argument(
"svd_truncated: k out of range");
59 const idx l = k + oversampling;
67 for (
idx j = 0; j < l; ++j)
68 for (
idx i = 0; i < n; ++i)
73 matmul(A, Omega, Y, backend);
81 for (
idx i = 0; i < l; ++i)
82 for (
idx kk = 0; kk < m; ++kk) {
83 const real q_ki = Q(kk, i);
84 for (
idx j = 0; j < n; ++j)
85 B(i, j) += q_ki * A(kk, j);
93 for (
idx j = 0; j < k; ++j)
94 for (
idx i = 0; i < m; ++i)
95 for (
idx ii = 0; ii < l; ++ii)
96 U(i, j) += Q(i, ii) * small.
U(ii, j);
99 for (
idx i = 0; i < k; ++i)
103 for (
idx i = 0; i < k; ++i)
104 for (
idx j = 0; j < n; ++j)
105 Vt(i, j) = small.
Vt(i, j);
107 return {U, S, Vt, 0,
true};
Dense row-major matrix with optional GPU storage.
constexpr idx rows() const noexcept
constexpr idx cols() const noexcept
SVDResult svd(const Matrix &A)
SVDResult svd(const Matrix &A, real tol, idx max_sweeps)
SVDResult svd_truncated(const Matrix &A, idx k, Backend backend=default_backend, idx oversampling=10, Rng *rng=nullptr)
Randomized truncated SVD – top-k singular triplets.
Backend
Selects which backend handles a linalg operation.
@ lapack
LAPACKE – industry-standard factorizations, SVD, eigen.
QRResult qr(const Matrix &A, Backend backend=lapack_backend)
QR factorization of an mxn matrix A (m >= n) via Householder reflections.
SVDResult svd(const Matrix &A, Backend backend=lapack_backend, real tol=1e-12, idx max_sweeps=100)
Full SVD of an mxn matrix.
real rng_normal(Rng *r, real mean, real stddev)
Normal (Gaussian) sample with given mean and standard deviation.
void matmul(const Matrix &A, const Matrix &B, Matrix &C, Backend b=default_backend)
C = A * B.
QR factorization via Householder reflections.
Result of a QR factorization: A = Q * R.
Seeded pseudo-random number generator (Mersenne Twister). Pass a pointer to rng_* functions to draw s...
Result of a Singular Value Decomposition: A = U * diag(S) * V^T.
Matrix Vt
rxn right singular vectors (rows orthonormal)
Matrix U
mxr left singular vectors (columns orthonormal)
Vector S
r singular values in descending order
Singular Value Decomposition – dense and randomized truncated.