-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_spectral.cpp
More file actions
86 lines (63 loc) · 2.21 KB
/
test_spectral.cpp
File metadata and controls
86 lines (63 loc) · 2.21 KB
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
#include <gtest/gtest.h>
#include "../src/algorithms/qr_algorithm.h"
#include "helpers.h"
namespace {
template <typename T = long double>
using Complex = std::complex<T>;
template <typename T = long double>
using Matrix = LinearKit::Matrix<T>;
using namespace LinearKit::Algorithm;
using namespace LinearKit::Utils;
using namespace LinearKit::MatrixUtils;
using LinearKit::Tests::RandomGenerator;
template <MatrixType M, MatrixType F, MatrixType S>
void CheckSpectral(const M &matrix, const F &D, const S &Q) {
using T = typename M::ElemType;
auto eps = 1e-10l;
EXPECT_TRUE(IsUnitary(Q, eps));
EXPECT_TRUE(
AreEqualMatrices(matrix, Q * D * Matrix<T>::Conjugated(Q), eps));
}
TEST(TEST_SPECTRAL, SpectralClear) {
using Matrix = Matrix<long double>;
Matrix matrix;
auto [D, Q] = GetSpecDecomposition(matrix);
CheckSpectral(matrix, D, Q);
}
TEST(TEST_SPECTRAL, SpectralReal) {
using Matrix = Matrix<long double>;
Matrix matrix = {{1, 2, 3}, {2, 4, 5}, {3, 5, 6}};
auto [D, Q] = GetSpecDecomposition(matrix);
CheckSpectral(matrix, D, Q);
}
TEST(TEST_SPECTRAL, SpectralComplex) {
using Matrix = Matrix<Complex<long double>>;
Matrix matrix = {{{1, 0}, {2, 2}, {3, 3}},
{{2, 2}, {5, 0}, {6, 6}},
{{3, 3}, {6, 6}, {9, 0}}};
auto [D, Q] = GetSpecDecomposition(matrix);
CheckSpectral(matrix, D, Q);
}
TEST(TEST_SPECTRAL, SpectralView) {
using Matrix = Matrix<long double>;
Matrix matrix = {{1, 2, 3, 4}, {2, 5, 6, 7}, {3, 6, 5, 8}, {4, 7, 8, 9}};
auto view = matrix.GetSubmatrix({1, -1}, {1, -1});
auto [D, Q] = GetSpecDecomposition(view);
CheckSpectral(view, D, Q);
}
TEST(TEST_SPECTRAL, Stress) {
using Type = long double;
using MatrixGenerator = RandomGenerator<Type>;
using Matrix = Matrix<Type>;
const size_t it_count = 1u;
for (int32_t seed = 1; seed < 10; ++seed) {
MatrixGenerator gen(seed);
for (size_t it = 0; it < it_count; ++it) {
int32_t size = gen.GetMatrixSize();
auto matrix = gen.GetSymmetricMatrix(size);
auto [D, Q] = GetSpecDecomposition(matrix);
CheckSpectral(matrix, D, Q);
}
}
}
} // namespace