-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsingular_tests.cpp
More file actions
94 lines (77 loc) · 2.4 KB
/
singular_tests.cpp
File metadata and controls
94 lines (77 loc) · 2.4 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
87
88
89
90
91
92
93
94
// Copyright (c) 2020-2021 The PoWx Core developers
#include <crypto/heavyhash_dummyArray.h>
#include <matrix-utils/singular/Svd.h>
#include <test/util/setup_common.h>
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(singular_tests, BasicTestingSetup)
using namespace std;
const int N = 4;
const int M = 64;
const double DATA[] = {
1, 7, 7, 5,
2, 1, 8, 10,
1, 2, 9, 17,
1, 2, 10, 2
};
const double WRONG_DATA1[] = {
1, 7, 7, 5,
1, 7, 7, 5,
1, 2, 9, 17,
1, 2, 10, 2
};
const double WRONG_DATA2[] = {
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
static bool CheckCompatibility(const double *data) {
singular::Matrix< N, N > matrix;
matrix.fill(data);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (data[i * N + j] != matrix(i, j))
return false;
}
}
return true;
}
static bool CheckMatrixRank4x4(const double *data) {
singular::Matrix< N, N > matrix;
matrix.fill(data);
singular::Svd< N, N >::USV usv = singular::Svd< N, N >::decomposeUSV(matrix);
const singular::DiagonalMatrix< N, N >& sing = singular::Svd< N, N >::getS(usv);
cout << sing << endl;
cout << endl;
return singular::Svd< N, N >::isFullRank(sing, N);
}
static bool CheckMatrixRank64x64(const double *data) {
singular::Matrix< M, M > matrix;
matrix.fill(data);
singular::Svd< M, M >::USV usv = singular::Svd< M, M >::decomposeUSV(matrix);
const singular::DiagonalMatrix< M, M >& sing = singular::Svd< M, M >::getS(usv);
cout << sing << endl;
cout << endl;
return singular::Svd< M, M >::isFullRank(sing, M);
}
static void ConvertReferenceArrayToInline(double* matrix) {
for (int i = 0; i < 64; ++i) {
for (int j = 0; j < 64; ++j) {
matrix[64*i + j] = (double) reference_matrix[i][j];
}
}
}
BOOST_AUTO_TEST_CASE(raw_data_to_singular_matrix_conversion) {
assert(CheckCompatibility(DATA));
}
BOOST_AUTO_TEST_CASE(compute_svd_of_a_matrix) {
assert(CheckMatrixRank4x4(DATA));
assert(!CheckMatrixRank4x4(WRONG_DATA1));
assert(!CheckMatrixRank4x4(WRONG_DATA2));
}
BOOST_AUTO_TEST_CASE(check_full_rank_for_64x64) {
double matrix[64*64];
ConvertReferenceArrayToInline(matrix);
assert(CheckMatrixRank64x64(matrix));
}
BOOST_AUTO_TEST_SUITE_END()