-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr_decomposition.h
47 lines (38 loc) · 1.39 KB
/
qr_decomposition.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
#include "givens_rotation.h"
#include "householder_reflection.h"
#pragma once
#include "givens_rotation.h"
#include "householder_reflection.h"
#include "Eigen/Core"
#include <vector>
namespace QR_algorithm {
enum QR_TRANSFORM {
QR_HOUSEHOLDER_REFLECTION,
QR_GIVENS_ROTATION
};
template<typename T>
void find_full_qr_decomposition(QR_TRANSFORM qr_tr, Eigen::MatrixX<T>* unit, Eigen::MatrixX<T>* center) {
Eigen::MatrixX<T>& center0 = *center;
size_t rows = center0.rows();
size_t cols = center0.cols();
if (qr_tr == QR_HOUSEHOLDER_REFLECTION) {
for (size_t i = 0; i < std::min(rows, cols); i++) {
Eigen::VectorX<T> current_vec = center0.block(i, i, rows - i, 1);
Householder_reflection<T> cur_refl = find_householder_reflector(current_vec, 0);
cur_refl.make_shift(i);
left_multiply(cur_refl, center);
right_multiply(cur_refl, unit);
}
} else if (qr_tr == QR_GIVENS_ROTATION) {
for (size_t i = 0; i < std::min(rows, cols); i++) {
Eigen::VectorX<T> current_vec = center0.block(i, i, rows - i, 1);
std::vector<Givens_rotation<T>> cur_rots = find_givens_rotations(current_vec, 1);
for (auto& x : cur_rots) {
x.make_shift(i);
left_multiply(x, center);
right_multiply(x.adjacent(), unit);
}
}
}
}
}