numerics
0.1.0
Loading...
Searching...
No Matches
svd.cpp
Go to the documentation of this file.
1
/// @file linalg/svd/backends/lapack/svd.cpp
2
/// @brief LAPACK SVD via LAPACKE_dgesdd (divide-and-conquer).
3
#include "../seq/impl.hpp"
4
#include "
impl.hpp
"
5
#include <algorithm>
6
#include <stdexcept>
7
#include <string>
8
9
#if defined(NUMERICS_HAS_LAPACK)
10
#include <lapacke.h>
11
#endif
12
13
namespace
num::backends::lapack
{
14
15
SVDResult
svd
(
const
Matrix
& A_in) {
16
#if defined(NUMERICS_HAS_LAPACK)
17
const
idx
m = A_in.
rows
(), n = A_in.
cols
();
18
const
idx
r = std::min(m, n);
19
Matrix
Aw = A_in;
// dgesdd overwrites A
20
Vector
S(r);
21
// Economy mode ('S'): U is m x r (lda=m), Vt is r x n (lda=r).
22
// Matrix lda equals its row count, so declare Vt as (r x n) not (n x n).
23
Matrix
U(m, r);
24
Matrix
Vt(r, n);
25
26
int
info = LAPACKE_dgesdd(LAPACK_ROW_MAJOR,
27
'S'
,
28
static_cast<
lapack_int
>
(m),
29
static_cast<
lapack_int
>
(n),
30
Aw.
data
(),
31
static_cast<
lapack_int
>
(n),
// lda = cols (row-major)
32
S.
data
(),
33
U.
data
(),
34
static_cast<
lapack_int
>
(r),
// ldu = r (cols of U)
35
Vt.
data
(),
36
static_cast<
lapack_int
>
(n));
// ldvt = n (cols of Vt)
37
if
(info != 0)
38
throw
std::runtime_error(
"svd (lapack): dgesdd failed, info="
39
+ std::to_string(info));
40
41
return
{std::move(U), std::move(S), std::move(Vt), 0,
true
};
42
#else
43
return
seq::svd
(A_in, 1
e
-12, 100);
44
#endif
45
}
46
47
}
// namespace num::backends::lapack
num::BasicMatrix< real >
num::BasicMatrix::rows
constexpr idx rows() const noexcept
Definition
matrix.hpp:87
num::BasicMatrix::data
T * data()
Definition
matrix.hpp:91
num::BasicMatrix::cols
constexpr idx cols() const noexcept
Definition
matrix.hpp:88
num::BasicVector< real >
num::BasicVector::data
T * data()
Definition
vector.hpp:88
num::backends::lapack
Definition
impl.hpp:7
num::backends::lapack::svd
SVDResult svd(const Matrix &A)
Definition
svd.cpp:15
num::backends::seq::svd
SVDResult svd(const Matrix &A, real tol, idx max_sweeps)
Definition
svd.cpp:11
num::idx
std::size_t idx
Definition
types.hpp:11
num::e
constexpr real e
Definition
math.hpp:44
impl.hpp
std::experimental::simd butterfly for FFT.
num::SVDResult
Definition
svd.hpp:16
src
linalg
svd
backends
lapack
svd.cpp
Generated by
1.9.8