-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.h
269 lines (226 loc) · 8.95 KB
/
manager.h
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#pragma once
#include "algorithm_iterations.h"
#include "givens_rotation.h"
#include "householder_reflection.h"
#include "hessenberg_form.h"
#include "qr_decomposition.h"
#include "steps.h"
#include <algorithm>
#include <string>
namespace QR_algorithm {
template<typename T>
class Manager {
public:
void set_symmetry_mode(bool mode) {
symmetry_mode = mode;
}
bool set_calculation_mode(const std::string& mode) {
if (mode == "eigenvalues_only") {
calculation_mode = EIGENVALUES_ONLY;
return true;
} else if (mode == "without_unit") {
calculation_mode = WITHOUT_UNIT;
return true;
} else if (mode == "with_unit") {
calculation_mode = WITH_UNIT;
return true;
} else {
return false;
}
}
bool set_hessenberg_transform(const std::string& transform) {
if (transform == "householder") {
hessenberg_transform = HT_HOUSEHOLDER_REFLECTION;
return true;
} else if (transform == "givens") {
hessenberg_transform = HT_GIVENS_ROTATION;
return true;
} else {
return false;
}
}
bool set_qr_transform(const std::string& transform) {
if (transform == "householder") {
qr_transform = QR_HOUSEHOLDER_REFLECTION;
return true;
} else if (transform == "givens") {
qr_transform = QR_GIVENS_ROTATION;
return true;
} else {
return false;
}
}
bool set_shift(const std::string& shift) {
if (shift == "none") {
shift_mode = NONE;
return true;
} else if (shift == "rayleigh"){
shift_mode = RAYLEIGH;
return true;
} else if (shift == "wilkinson") {
shift_mode = WILKINSON;
return true;
} else if (shift == "francis") {
shift_mode = FRANCIS;
return true;
} else {
return false;
}
}
void set_maximum_iterations(size_t max_iterations_) {
max_iterations = max_iterations_;
}
void set_each_step_zeros_mode(bool make_each_step_zeros_) {
make_each_step_zeros = make_each_step_zeros_;
}
void set_accurance (double accurance_) {
if (accurance_ >= 0) {
accurance = accurance_;
} else {
accurance = 0;
}
}
void set_pseudo_shur_mode (bool pseudo_shur_) {
pseudo_shur = pseudo_shur_;
}
void shur_decomposition(const Eigen::MatrixX<T>& matrix, Eigen::MatrixX<T>* center, Eigen::MatrixX<T>* unit);
void shur_decomposition_inplace(Eigen::MatrixX<T>* center, Eigen::MatrixX<T>* unit);
void qr_decomposition(const Eigen::MatrixX<T>& matrix, Eigen::MatrixX<T>* center, Eigen::MatrixX<T>* unit);
void qr_decomposition_inplace(Eigen::MatrixX<T>* center, Eigen::MatrixX<T>* unit);
size_t svd_decomposition(const Eigen::MatrixX<T>& matrix, Eigen::MatrixX<T>* U, std::vector<T>* singular_values, Eigen::MatrixX<T>* Vh);
void eigenvalues(const Eigen::MatrixX<T>& matrix, std::vector<T>* eigenvalues);
private:
bool symmetry_mode = false;
CALCULATION_MODE calculation_mode = WITH_UNIT;
SHIFT shift_mode = WILKINSON;
double accurance = 1e-4;
size_t max_iterations = 1000;
bool make_each_step_zeros = false;
HESSENBERG_TRANSFORM hessenberg_transform = HT_HOUSEHOLDER_REFLECTION;
QR_TRANSFORM qr_transform = QR_HOUSEHOLDER_REFLECTION;
bool pseudo_shur = false;
double eps_ignore_in_svd = 1e-8;
};
template<typename T>
void Manager<T>::shur_decomposition_inplace(Eigen::MatrixX<T>* center, Eigen::MatrixX<T>* unit) {
size_t check_size = center->rows();
assert(check_size != 0);
assert(check_size == center->cols());
if (calculation_mode == WITH_UNIT) {
assert(check_size == unit->rows());
assert(check_size == unit->cols());
*unit = Eigen::MatrixX<T>::Identity(check_size, check_size);
}
if (calculation_mode != WITH_UNIT) {
make_hessenberg_form<T>(hessenberg_transform, nullptr, center);
} else {
make_hessenberg_form<T>(hessenberg_transform, unit, center);
}
if (symmetry_mode) {
if (calculation_mode == WITH_UNIT) {
symmetrical_iterations<T>(max_iterations, accurance, make_each_step_zeros, calculation_mode,
unit, center);
} else {
symmetrical_iterations<T>(max_iterations, accurance, make_each_step_zeros, calculation_mode,
nullptr, center);
}
} else {
if (calculation_mode == WITH_UNIT) {
shift_iterations<T>(max_iterations, accurance, make_each_step_zeros, calculation_mode,
shift_mode, pseudo_shur, unit, center);
} else {
shift_iterations<T>(max_iterations, accurance, make_each_step_zeros, calculation_mode,
shift_mode, pseudo_shur, nullptr, center);
}
}
}
template<typename T>
void Manager<T>::qr_decomposition_inplace(Eigen::MatrixX<T>* center, Eigen::MatrixX<T>* unit) {
size_t check_size = center->rows();
assert(check_size != 0);
assert(check_size == unit->rows());
assert(check_size == unit->cols());
*unit = Eigen::MatrixX<T>::Identity(check_size, check_size);
find_full_qr_decomposition(qr_transform, unit, center);
}
template<typename T>
void Manager<T>::shur_decomposition(const Eigen::MatrixX<T>& matrix, Eigen::MatrixX<T>* center, Eigen::MatrixX<T>* unit) {
assert(matrix.rows() == center->rows());
assert(matrix.cols() == center->cols());
*center = matrix;
shur_decomposition_inplace(center, unit);
}
template<typename T>
void Manager<T>::eigenvalues(const Eigen::MatrixX<T>& matrix, std::vector<T>* eigenvalues) {
assert(matrix.rows() == matrix.cols());
Eigen::MatrixX<T> center = matrix;
auto old_calc_mode = calculation_mode;
calculation_mode = WITHOUT_UNIT;
shur_decomposition_inplace(¢er, nullptr);
calculation_mode = old_calc_mode;
for (size_t i = 0; i < matrix.rows(); i++) {
(*eigenvalues)[i] = center(i, i);
}
}
template<typename T>
void Manager<T>::qr_decomposition(const Eigen::MatrixX<T>& matrix, Eigen::MatrixX<T>* center, Eigen::MatrixX<T>* unit) {
assert(matrix.rows() == center->rows());
assert(matrix.cols() == center->cols());
*center = matrix;
qr_decomposition_inplace(center, unit);
}
template<typename T>
size_t Manager<T>::svd_decomposition(const Eigen::MatrixX<T>& matrix, Eigen::MatrixX<T>* U, std::vector<T>* singular_values, Eigen::MatrixX<T>* Vh) {
if (matrix.rows() <= matrix.cols()) {
std::vector<T>& singular_values_ref = *singular_values;
Eigen::MatrixX<T> center_matrix = matrix * matrix.adjoint();
Eigen::MatrixX<T> center_matrix_conserve = center_matrix;
size_t size = center_matrix.rows();
Eigen::MatrixX<T> unit_matrix = Eigen::MatrixX<T>::Identity(size, size);
bool old_symmetry_mode = symmetry_mode;
auto old_calc_mode = calculation_mode;
set_symmetry_mode(true);
set_calculation_mode("with_unit");
shur_decomposition_inplace(¢er_matrix, &unit_matrix);
set_symmetry_mode(old_symmetry_mode);
calculation_mode = old_calc_mode;
std::vector<std::pair<T, int>> sing_values_with_indeces(size);
for (int i = 0; i < size; i++) {
sing_values_with_indeces[i].first = center_matrix(i, i);
sing_values_with_indeces[i].second = i;
}
std::sort(sing_values_with_indeces.begin(), sing_values_with_indeces.end(), [](std::pair<T, size_t> a, std::pair<T, size_t> b){
return abs(a.first) > abs(b.first);
});
for (int i = 0; i < size; i++) {
singular_values_ref[i] = sqrt(sing_values_with_indeces[i].first);
U->col(i) = unit_matrix.col(sing_values_with_indeces[i].second);
}
Eigen::MatrixX<T> res_center = Eigen::MatrixX<T>::Zero(size, size);
for (int i = 0; i < size; i++) {
res_center(i, i) = singular_values_ref[i];
}
*Vh = U->adjoint() * matrix;
const Eigen::RowVectorX<T> zero_row = Eigen::MatrixX<T>::Zero(1, Vh->cols());
const Eigen::VectorX<T> zero_col = Eigen::MatrixX<T>::Zero(U->rows(), 1);
int answer = 0;
for (int i = 0; i < size; i++) {
if (abs(singular_values_ref[i]) < eps_ignore_in_svd) {
Vh->row(i) = zero_row;
U->col(i) = zero_col;
} else {
Vh->row(i) /= singular_values_ref[i];
answer = i + 1;
}
}
return answer;
} else {
Eigen::MatrixX<T> Vhextra = Vh->adjoint();
Eigen::MatrixX<T> Uextra = U->adjoint();
size_t answer = svd_decomposition(matrix.adjoint(), &Vhextra, singular_values, &Uextra);
*Vh = Vhextra.adjoint();
*U = Uextra.adjoint();
return answer;
}
}
}