1 | #include "../bdm/math/square_mat.h" |
---|
2 | #include "../bdm/math/chmat.h" |
---|
3 | #include "base/user_info.h" |
---|
4 | #include "UnitTest++.h" |
---|
5 | #include "TestReporterStdout.h" |
---|
6 | #include <iostream> |
---|
7 | #include <iomanip> |
---|
8 | #include <stdlib.h> |
---|
9 | #include <string.h> |
---|
10 | |
---|
11 | using std::cout; |
---|
12 | using std::cerr; |
---|
13 | using std::endl; |
---|
14 | |
---|
15 | using bdm::UIFile; |
---|
16 | using bdm::UI; |
---|
17 | |
---|
18 | const char *agenda_file_name = "agenda.cfg"; |
---|
19 | double epsilon = 0.00001; |
---|
20 | bool fast = false; |
---|
21 | |
---|
22 | namespace UnitTest |
---|
23 | { |
---|
24 | |
---|
25 | // can't include mat_checks.h because CheckClose is different in this file |
---|
26 | extern bool AreClose(const itpp::vec &expected, const itpp::vec &actual, |
---|
27 | double tolerance); |
---|
28 | |
---|
29 | extern bool AreClose(const itpp::mat &expected, const itpp::mat &actual, |
---|
30 | double tolerance); |
---|
31 | |
---|
32 | void CheckClose(TestResults &results, const itpp::mat &expected, |
---|
33 | const itpp::mat &actual, double tolerance, |
---|
34 | TestDetails const& details) { |
---|
35 | if (!AreClose(expected, actual, tolerance)) { |
---|
36 | MemoryOutStream stream; |
---|
37 | stream << "failed at " << expected.rows() |
---|
38 | << " x " << expected.cols(); |
---|
39 | |
---|
40 | results.OnTestFailure(details, stream.GetText()); |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | } |
---|
45 | |
---|
46 | template<typename TMatrix> |
---|
47 | void test_matrix(int index, const mat &A) { |
---|
48 | Real_Timer tt; |
---|
49 | |
---|
50 | cout << "agenda[" << index << ']' << endl; |
---|
51 | int sz = A.rows(); |
---|
52 | CHECK_EQUAL(A.cols(), sz); |
---|
53 | |
---|
54 | tt.tic(); |
---|
55 | TMatrix sqmat(A); |
---|
56 | double elapsed = tt.toc(); |
---|
57 | cout << "ctor(" << sz << " x " << sz << "): " << elapsed << " s" << endl; |
---|
58 | |
---|
59 | tt.tic(); |
---|
60 | mat res = sqmat.to_mat(); |
---|
61 | elapsed = tt.toc(); |
---|
62 | |
---|
63 | if (!fast) { |
---|
64 | CHECK_CLOSE(A, res, epsilon); |
---|
65 | } |
---|
66 | |
---|
67 | cout << "to_mat: " << elapsed << " s" << endl; |
---|
68 | |
---|
69 | vec v = randu(sz); |
---|
70 | double w = randu(); |
---|
71 | TMatrix sqmat2 = sqmat; |
---|
72 | |
---|
73 | tt.tic(); |
---|
74 | sqmat2.opupdt(v, w); |
---|
75 | elapsed = tt.toc(); |
---|
76 | |
---|
77 | if (!fast) { |
---|
78 | mat expA = A + w * outer_product(v, v); |
---|
79 | CHECK_CLOSE(expA, sqmat2.to_mat(), epsilon); |
---|
80 | } |
---|
81 | |
---|
82 | cout << "opupdt: " << elapsed << " s" << endl; |
---|
83 | |
---|
84 | TMatrix invmat(sz); |
---|
85 | |
---|
86 | tt.tic(); |
---|
87 | sqmat.inv(invmat); |
---|
88 | elapsed = tt.toc(); |
---|
89 | |
---|
90 | if (!fast) { |
---|
91 | mat invA = inv(A); |
---|
92 | CHECK_CLOSE(invA, invmat.to_mat(), epsilon); |
---|
93 | } |
---|
94 | |
---|
95 | cout << "inv: " << elapsed << " s" << endl; |
---|
96 | |
---|
97 | tt.tic(); |
---|
98 | double ld = sqmat.logdet(); |
---|
99 | elapsed = tt.toc(); |
---|
100 | |
---|
101 | if (!fast) { |
---|
102 | double d = det(A); |
---|
103 | CHECK_CLOSE(log(d), ld, epsilon); |
---|
104 | } |
---|
105 | |
---|
106 | cout << "logdet: " << elapsed << " s" << endl; |
---|
107 | } |
---|
108 | |
---|
109 | template<typename TMatrix> |
---|
110 | void test_agenda() { |
---|
111 | UIFile fag(agenda_file_name); |
---|
112 | Array<mat> mag; |
---|
113 | UI::get(mag, fag, "agenda"); |
---|
114 | int sz = mag.size(); |
---|
115 | CHECK(sz > 0); |
---|
116 | for (int i = 0; i < sz; ++i) { |
---|
117 | test_matrix<TMatrix>(i, mag(i)); |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | SUITE(ldmat) { |
---|
122 | TEST(cycle) { |
---|
123 | test_agenda<ldmat>(); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | SUITE(fsqmat) { |
---|
128 | TEST(cycle) { |
---|
129 | test_agenda<fsqmat>(); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | SUITE(chmat) { |
---|
134 | TEST(cycle) { |
---|
135 | test_agenda<chmat>(); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | int main(int argc, char const *argv[]) { |
---|
140 | bool unknown = false; |
---|
141 | int update_next = 0; // 1 suite, 2 epsilon, 3 agenda file |
---|
142 | const char *suite = "ldmat"; |
---|
143 | const char **param = argv + 1; |
---|
144 | while (*param && !unknown) { |
---|
145 | if (update_next) { |
---|
146 | if (update_next == 1) { |
---|
147 | suite = *param; |
---|
148 | } else if (update_next == 2) { |
---|
149 | double eps = atof(*param); |
---|
150 | if (eps > 0) { |
---|
151 | epsilon = eps; |
---|
152 | } else { |
---|
153 | cerr << "invalid epsilon value ignored" << endl; |
---|
154 | } |
---|
155 | } else { |
---|
156 | agenda_file_name = *param; |
---|
157 | } |
---|
158 | |
---|
159 | update_next = 0; |
---|
160 | } else { |
---|
161 | if (!strcmp(*param, "-c")) { |
---|
162 | update_next = 1; |
---|
163 | } else if (!strcmp(*param, "-e")) { |
---|
164 | update_next = 2; |
---|
165 | } else if (!strcmp(*param, "-f")) { |
---|
166 | fast = true; |
---|
167 | } else { |
---|
168 | unknown = true; |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | ++param; |
---|
173 | } |
---|
174 | |
---|
175 | if (unknown || update_next) { |
---|
176 | cerr << "usage: " << argv[0] << " [ -f ] [ -e epsilon ] [ -c class ]" << endl; |
---|
177 | } else { |
---|
178 | UnitTest::TestReporterStdout reporter; |
---|
179 | UnitTest::TestRunner runner(reporter); |
---|
180 | return runner.RunTestsIf(UnitTest::Test::GetTestList(), |
---|
181 | suite, |
---|
182 | UnitTest::True(), |
---|
183 | 0); |
---|
184 | } |
---|
185 | } |
---|