-
Notifications
You must be signed in to change notification settings - Fork 4
/
p3a_svd.hpp
222 lines (211 loc) · 5.59 KB
/
p3a_svd.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#pragma once
#include "p3a_eigen.hpp"
#include "p3a_matrix2x2.hpp"
#include "p3a_matrix3x3.hpp"
#include "p3a_diagonal3x3.hpp"
#include "p3a_static_matrix.hpp"
#include "p3a_quantity.hpp"
namespace p3a {
template <class T>
P3A_HOST_DEVICE
void givens(
T const& a,
T const& b,
T& c,
T& s)
{
c = 1.0;
s = 0.0;
if (b != 0.0) {
if (p3a::abs(b) > p3a::abs(a)) {
auto const t = -a / b;
s = 1.0 / p3a::sqrt(1.0 + t * t);
c = t * s;
} else {
auto const t = -b / a;
c = 1.0 / p3a::sqrt(1.0 + t * t);
s = t * c;
}
}
}
// Singular value decomposition (SVD) for 2x2
// bidiagonal matrix. Used for general 2x2 SVD.
// Adapted from LAPAPCK's DLASV2, Netlib's dlasv2.c
// and LBNL computational crystallography toolbox
// \param f, g, h where A = [f, g; 0, h]
// \return \f$ A = USV^T\f$
template <class T>
P3A_HOST_DEVICE
void svd_bidiagonal(
T f,
T const& g,
T h,
matrix2x2<T>& U,
matrix2x2<T>& S,
matrix2x2<T>& V)
{
T fa = p3a::abs(f);
T ga = p3a::abs(g);
T ha = p3a::abs(h);
T s0 = 0.0;
T s1 = 0.0;
T cu = 1.0;
T su = 0.0;
T cv = 1.0;
T sv = 0.0;
auto const swap_diag = (ha > fa);
if (swap_diag == true) {
p3a::swap(fa, ha);
p3a::swap(f, h);
}
T constexpr epsilon = epsilon_value<T>();
// diagonal matrix
if (ga == 0.0) {
s1 = ha;
s0 = fa;
} else if (ga > fa && fa / ga < epsilon) {
// case of very large ga
s0 = ga;
s1 = ha > 1.0 ? (fa / (ga / ha)) : ((fa / ga) * ha);
cu = 1.0;
su = h / g;
cv = f / g;
sv = 1.0;
} else {
// normal case
T const d = fa - ha;
T const l = d / fa; // l \in [0,1]
T const m = g / f; // m \in (-1/macheps, 1/macheps)
T const t = 2.0 - l; // t \in [1,2]
T const mm = m * m;
T const tt = t * t;
T const s = p3a::sqrt(tt + mm); // s \in [1,1 + 1/macheps]
T const r = ((l != 0.0) ? (p3a::sqrt(l * l + mm)) : (abs(m))); // r \in [0,1 + 1/macheps]
T const a = 0.5 * (s + r); // a \in [1,1 + |m|]
s1 = ha / a;
s0 = fa * a;
// Compute singular vectors
T tau; // second assignment to T in DLASV2
if (mm != 0.0) {
tau = (m / (s + t) + m / (r + l)) * (1.0 + a);
} else {
// note that m is very tiny
tau = (l == 0.0) ? (std::copysign(2.0, f) * std::copysign(1.0, g))
: (g / std::copysign(d, f) + m / t);
}
T const lv = p3a::sqrt(tau * tau + 4.0); // second assignment to L in DLASV2
cv = 2.0 / lv;
sv = tau / lv;
cu = (cv + sv * m) / a;
su = (h / f) * sv / a;
}
// Fix signs of singular values in accordance to sign of singular vectors
s0 = std::copysign(s0, f);
s1 = std::copysign(s1, h);
if (swap_diag == true) {
p3a::swap(cu, sv);
p3a::swap(su, cv);
}
U = matrix2x2<T>(cu, -su, su, cu);
S = matrix2x2<T>(s0, 0.0, 0.0, s1);
V = matrix2x2<T>(cv, -sv, sv, cv);
}
template <class T>
P3A_HOST_DEVICE
void svd_2x2(
matrix2x2<T> const& A,
matrix2x2<T>& U,
matrix2x2<T>& S,
matrix2x2<T>& V)
{
// First compute a givens rotation to eliminate 1,0 entry in tensor
T c, s;
givens(A.xx(), A.yx(), c, s);
auto const R = matrix2x2<T>(c, -s, s, c);
auto const B = R * A;
// B is bidiagonal. Use specialized algorithm to compute its SVD
matrix2x2<T> U_B, S_B, V_B;
svd_bidiagonal(B.xx(), B.xy(), B.yy(), U_B, S_B, V_B);
auto const X = U_B;
S = S_B;
V = V_B;
// Complete general 2x2 SVD with givens rotation calculated above
U = transpose(R) * X;
}
// R^N singular value decomposition (SVD)
// \param A tensor
// \return \f$ A = USV^T\f$
template <class T, int N>
P3A_HOST_DEVICE
void decompose_singular_values(
static_matrix<T, N, N> const& A,
static_matrix<T, N, N>& U,
static_matrix<T, N, N>& S,
static_matrix<T, N, N>& V)
{
// Scale first
T const norm_a = norm(A);
T const scale = norm_a > 0.0 ? norm_a : T(1.0);
S = A / scale;
U = static_matrix<T, N, N>::identity();
V = static_matrix<T, N, N>::identity();
auto off = off_diagonal_norm(S);
T constexpr tol = epsilon_value<T>();
int const max_iter = 2048;
int num_iter = 0;
while (off > tol && num_iter < max_iter) {
// Find largest off-diagonal entry
int p, q;
maximum_off_diagonal_indices(S, p, q);
// Obtain left and right Givens rotations by using 2x2 SVD
auto const Spq = matrix2x2<T>(
S(p, p), S(p, q), S(q, p), S(q, q));
matrix2x2<T> U_2x2, S_2x2, V_2x2;
svd_2x2(Spq, U_2x2, S_2x2, V_2x2);
auto const L = U_2x2;
auto const R = V_2x2;
T const cl = L.xx();
T const sl = L.xy();
T const cr = R.xx();
T const sr =
(sign(R.xy()) == sign(R.yx())) ?
(-R.xy()) : (R.xy());
// Apply both Givens rotations to matrices
// that are converging to singular values and singular vectors
rotate_givens_left(cl, sl, p, q, S);
rotate_givens_right(cr, sr, p, q, S);
rotate_givens_right(cl, sl, p, q, U);
rotate_givens_left(cr, sr, p, q, V);
off = off_diagonal_norm(S);
++num_iter;
}
// Fix signs for entries in the diagonal matrix S
// that are negative
for (int i = 0; i < N; ++i) {
if (S(i, i) < 0.0) {
S(i, i) = -S(i, i);
for (int j = 0; j < N; ++j) {
U(j, i) = -U(j, i);
}
}
}
S *= scale;
}
template <class T>
P3A_HOST_DEVICE
void decompose_singular_values(
matrix3x3<T> const& A,
matrix3x3<T>& U,
diagonal3x3<T>& S,
matrix3x3<T>& V)
{
static_matrix<T, 3, 3> A2(A);
static_matrix<T, 3, 3> U2, S2, V2;
decompose_singular_values(A2, U2, S2, V2);
S.xx() = S2(0, 0);
S.yy() = S2(1, 1);
S.zz() = S2(2, 2);
U = static_cast<matrix3x3<T>>(U2);
V = static_cast<matrix3x3<T>>(V2);
}
}