<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
gesvd.hpp
Go to the documentation of this file.
1
5//
6// Copyright (c) 2021-2023, 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_GESVD_HH
13#define TLAPACK_GESVD_HH
14
19
20namespace tlapack {
21
25struct GesvdOpts {
28 float shapethresh = 1.6;
29};
30
65template <TLAPACK_SMATRIX matrix_t, TLAPACK_SVECTOR r_vector_t>
66int gesvd(bool want_u,
67 bool want_vt,
68 matrix_t& A,
70 matrix_t& U,
71 matrix_t& Vt,
72 const GesvdOpts& opts = {})
73{
74 using idx_t = size_type<matrix_t>;
75 using range = pair<idx_t, idx_t>;
76
77 // Functors
78 Create<vector_type<matrix_t>> new_vector;
79 Create<vector_type<r_vector_t>> new_rvector;
80
81 // constants
82 const idx_t m = nrows(A);
83 const idx_t n = ncols(A);
84 const idx_t k = min(m, n);
85 const Uplo uplo = (m >= n) ? Uplo::Upper : Uplo::Lower;
86
87 // Allocate vectors
88 std::vector<type_t<matrix_t>> tauv_, tauw_;
89 auto tauv = new_vector(tauv_, k);
90 auto tauw = new_vector(tauw_, k);
91 std::vector<type_t<r_vector_t>> e_;
92 auto e = new_rvector(e_, k);
93
94 // Reduce A to bidiagonal form
95 gebrd(A, tauv, tauw);
96
97 if (m >= n) {
98 // copy upper bidiagonal matrix
99 for (idx_t i = 0; i < k; ++i) {
100 s[i] = real(A(i, i));
101 if (i + 1 < n) e[i] = real(A(i, i + 1));
102 }
103 }
104 else {
105 // copy lower bidiagonal matrix
106 for (idx_t i = 0; i < k; ++i) {
107 s[i] = real(A(i, i));
108 if (i + 1 < m) e[i] = real(A(i + 1, i));
109 }
110 }
111
112 if (want_u) {
113 auto Ui = slice(U, range{0, m}, range{0, k});
114 lacpy(Uplo::Lower, slice(A, range{0, m}, range{0, k}), Ui);
115 ungbr_q(n, U, tauv);
116 }
117
118 if (want_vt) {
119 auto Vti = slice(Vt, range{0, k}, range{0, n});
120 lacpy(Uplo::Upper, slice(A, range{0, k}, range{0, n}), Vti);
121 ungbr_p(m, Vt, tauw);
122 }
123
124 return svd_qr(uplo, want_u, want_vt, s, e, U, Vt);
125}
126
127} // namespace tlapack
128
129#endif // TLAPACK_GESVD_HH
constexpr real_type< T > real(const T &x) noexcept
Extends std::real() to real datatypes.
Definition utils.hpp:71
int ungbr_q(const size_type< matrix_t > k, matrix_t &A, const vector_t &tau, const UngbrOpts &opts={})
Generates the unitary matrix Q determined by gebrd when reducing a matrix A to bidiagonal form: A = Q...
Definition ungbr.hpp:201
int gebrd(matrix_t &A, vector_t &tauv, vector_t &tauw, const GebrdOpts &opts={})
Reduces a general m by n matrix A to an upper real bidiagonal form B by a unitary transformation:
Definition gebrd.hpp:202
int ungbr_p(const size_type< matrix_t > k, matrix_t &A, const vector_t &tau, const UngbrOpts &opts={})
Generates the unitary matrix P**H determined by gebrd when reducing a matrix A to bidiagonal form: A ...
Definition ungbr.hpp:311
void lacpy(uplo_t uplo, const matrixA_t &A, matrixB_t &B)
Copies a matrix from A to B.
Definition lacpy.hpp:38
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 gesvd(bool want_u, bool want_vt, matrix_t &A, r_vector_t &s, matrix_t &U, matrix_t &Vt, const GesvdOpts &opts={})
Computes the singular values and, optionally, the right and/or left singular vectors from the singula...
Definition gesvd.hpp:66
Concept for types that represent tlapack::Uplo.
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
Options struct for gesvd.
Definition gesvd.hpp:25
float shapethresh
Definition gesvd.hpp:28