42 idx r = std::min(
m, n);
49 for (
idx i = 0;
i < n; ++
i) V(
i,
i) = 1.0;
52 bool converged =
false;
57 for (
idx p = 0; p <
r-1; ++p) {
58 for (
idx q = p+1; q <
r; ++q) {
64 gamma +=
A(
i,p) *
A(
i,q);
77 real c = 1.0 / std::sqrt(1.0 + t*t);
88 for (
idx i = 0;
i < n; ++
i) {
97 if (
max_cos <
tol) { converged =
true;
break; }
106 S[
j] = std::sqrt(
nrm);
112 for (
idx i = 0;
i <
r-1; ++
i) {
118 std::swap(S[
i], S[
max_j]);
127 for (
idx j = 0;
j < n; ++
j)
130 return {U, S, Vt, sweeps, converged};
145 const idx m =
A.rows(), n =
A.cols();
146 if (
k == 0 ||
k > std::min(
m, n))
147 throw std::invalid_argument(
"svd_truncated: k out of range");
158 for (
idx i = 0;
i < n; ++
i)
174 for (
idx j = 0;
j < n; ++
j)
194 for (
idx j = 0;
j < n; ++
j)
197 return {U, S, Vt, 0,
true};
Dense row-major matrix with optional GPU storage.
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.
real beta(real a, real b)
B(a, b) – beta function.
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
QRResult qr(const Matrix &A)
QR factorization of an mxn matrix A (m >= n) via Householder reflections.
real rng_normal(Rng *r, real mean, real stddev)
Normal (Gaussian) sample with given mean and standard deviation.
real zeta(real x)
zeta(x) – Riemann zeta function
SVDResult svd(const Matrix &A, Backend backend=Backend::seq, real tol=1e-12, idx max_sweeps=100)
Full SVD of an mxn matrix via one-sided Jacobi.
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ᵀ
Singular Value Decomposition – dense and randomized truncated.