<T>LAPACK 0.1.2
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
tik_svd.hpp
Go to the documentation of this file.
1
5//
6// Copyright (c) 2025, University of Colorado Denver. All rights reserved.
7//
8// This file is part of <T>LAPACK.
9// <T>LAPACK is free software: you can redistribute it and/or modify it under
10// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
11
12#ifndef TLAPACK_TIK_SVD
13#define TLAPACK_TIK_SVD
14
21
33using namespace tlapack;
34
38void tik_svd(matrixA_t& A, matrixb_t& b, real_t lambda)
39{
40 using T = type_t<matrixA_t>;
41 using idx_t = size_type<matrixA_t>;
42
44
45 const idx_t m = nrows(A);
46 const idx_t n = ncols(A);
47 const idx_t k = ncols(b);
48
50
51 std::vector<T> tauv(n);
52 std::vector<T> tauw(n);
53
54 // Bidiagonal decomposition
55 bidiag(A, tauv, tauw);
56
57 // Apply Q1ᴴ to b using unmqr
58 //
59 // Note: it is possible to use b for tmp1 and output x in b,
60 // this would remove arrays btmp1 and x. Right now, we chose to have
61 // the same interface for all Tikhonov functions
62
64
65 auto x = slice(b, range{0, n}, range{0, k});
66
67 // Extract diagonal and superdiagonal
68 std::vector<real_t> d(n);
69 std::vector<real_t> e(n - 1);
70 for (idx_t j = 0; j < n; ++j)
71 d[j] = real(A(j, j));
72 for (idx_t j = 0; j < n - 1; ++j)
73 e[j] = real(A(j, j + 1));
74
75 // Allocate and initialize Q2 and P2
76 std::vector<T> Q2_;
77 auto Q2 = new_matrix(Q2_, n, n);
78 std::vector<T> P2_;
79 auto P2 = new_matrix(P2_, n, n);
80 const real_t zero(0);
81 const real_t one(1);
82 laset(Uplo::General, zero, one, Q2);
83 laset(Uplo::General, zero, one, P2);
84
85 int err = svd_qr(Uplo::Upper, true, true, d, e, Q2, P2);
86
87 // Apply Q2ᴴ
88 std::vector<T> work_;
89 auto work = new_matrix(work_, n, k);
91
92 for (idx_t j = 0; j < n; ++j)
93 for (idx_t i = 0; i < k; ++i)
94 work(j, i) *= (d[j] / ((d[j] * d[j]) + (lambda * lambda)));
95
96 // Apply P2ᴴ
97
99
100 // Apply P1ᴴ
101
102 // Finish solving least squares problem
103 auto x1 = slice(x, range{1, n}, range{0, k});
104 unmlq(LEFT_SIDE, CONJ_TRANS, slice(A, range{0, n - 1}, range{1, n}),
105 slice(tauw, range{0, n - 1}), x1);
106
107 // Final result
108
109 // lacpy(GENERAL, x4, x);
110}
111
112#endif // TLAPACK_TIK_SVD
#define TLAPACK_MATRIX
Macro for tlapack::concepts::Matrix compatible with C++17.
Definition concepts.hpp:896
#define TLAPACK_REAL
Macro for tlapack::concepts::Real compatible with C++17.
Definition concepts.hpp:918
void laset(uplo_t uplo, const type_t< matrix_t > &alpha, const type_t< matrix_t > &beta, matrix_t &A)
Initializes a matrix to diagonal and off-diagonal values.
Definition laset.hpp:38
void gemm(Op transA, Op transB, const alpha_t &alpha, const matrixA_t &A, const matrixB_t &B, const beta_t &beta, matrixC_t &C)
General matrix-matrix multiply:
Definition gemm.hpp:61
int svd_qr(Uplo uplo, bool want_u, bool want_vt, d_t &d, e_t &e, matrix_t &U, matrix_t &Vt)
Computes the singular values and, optionally, the right and/or left singular vectors from the singula...
Definition svd_qr.hpp:85
int unmqr(side_t side, trans_t trans, const matrixA_t &A, const tau_t &tau, matrixC_t &C, const UnmqrOpts &opts={})
Applies orthogonal matrix op(Q) to a matrix C using a blocked code.
Definition unmqr.hpp:158
int unmlq(side_t side, trans_t trans, const matrixA_t &A, const tau_t &tau, matrixC_t &C, const UnmlqOpts &opts={})
Applies orthogonal matrix op(Q) to a matrix C using a blocked code.
Definition unmlq.hpp:159
int bidiag(matrix_t &A, vector_t &tauv, vector_t &tauw, const BidiagOpts &opts={})
Reduces a general m by n matrix A to an upper real bidiagonal form B by a unitary transformation:
Definition bidiag.hpp:134
Multiplies the general m-by-n matrix C by Q from tlapack::geqrf()
Sort the numbers in D in increasing order (if ID = 'I') or in decreasing order (if ID = 'D' ).
Definition arrayTraits.hpp:15
typename traits::real_type_traits< Types..., int >::type real_type
The common real type of the list of types.
Definition scalar_type_traits.hpp:113
constexpr real_type< T > real(const T &x) noexcept
Extends std::real() to real datatypes.
Definition utils.hpp:71
constexpr internal::ConjTranspose CONJ_TRANS
conjugate transpose
Definition types.hpp:264
constexpr internal::NoTranspose NO_TRANS
no transpose
Definition types.hpp:260
constexpr internal::LeftSide LEFT_SIDE
left side
Definition types.hpp:294