31 | | extern bool AreClose(const itpp::vec &expected, const itpp::vec &actual, |
32 | | double tolerance); |
33 | | |
34 | | extern bool AreClose(const itpp::mat &expected, const itpp::mat &actual, |
35 | | double tolerance); |
36 | | |
37 | | void CheckClose(TestResults &results, const itpp::mat &expected, |
38 | | const itpp::mat &actual, double tolerance, |
39 | | TestDetails const& details) { |
40 | | if (!AreClose(expected, actual, tolerance)) { |
41 | | MemoryOutStream stream; |
42 | | stream << "failed at " << expected.rows() |
43 | | << " x " << expected.cols(); |
44 | | |
45 | | results.OnTestFailure(details, stream.GetText()); |
46 | | } |
47 | | } |
48 | | |
49 | | } |
50 | | |
51 | | typedef void (*FTestMatrix)(int, square_mat_point *); |
| 30 | extern bool AreClose ( const itpp::vec &expected, const itpp::vec &actual, |
| 31 | double tolerance ); |
| 32 | |
| 33 | extern bool AreClose ( const itpp::mat &expected, const itpp::mat &actual, |
| 34 | double tolerance ); |
| 35 | |
| 36 | void CheckClose ( TestResults &results, const itpp::mat &expected, |
| 37 | const itpp::mat &actual, double tolerance, |
| 38 | TestDetails const& details ) { |
| 39 | if ( !AreClose ( expected, actual, tolerance ) ) { |
| 40 | MemoryOutStream stream; |
| 41 | stream << "failed at " << expected.rows() |
| 42 | << " x " << expected.cols(); |
| 43 | |
| 44 | results.OnTestFailure ( details, stream.GetText() ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | } |
| 49 | |
| 50 | typedef void ( *FTestMatrix ) ( int, square_mat_point * ); |
54 | | void test_matrix(int index, square_mat_point *point) { |
55 | | Real_Timer tt; |
56 | | |
57 | | cout << "agenda[" << index << "]:" << endl; |
58 | | mat A = point->get_matrix(); |
59 | | int sz = A.rows(); |
60 | | CHECK_EQUAL(A.cols(), sz); |
61 | | |
62 | | tt.tic(); |
63 | | TMatrix sqmat(A); |
64 | | double elapsed = tt.toc(); |
65 | | cout << "ctor(" << sz << " x " << sz << "): " << elapsed << " s" << endl; |
66 | | |
67 | | tt.tic(); |
68 | | mat res = sqmat.to_mat(); |
69 | | elapsed = tt.toc(); |
70 | | |
71 | | if (!fast) { |
72 | | CHECK_CLOSE(A, res, epsilon); |
73 | | } |
74 | | |
75 | | cout << "to_mat: " << elapsed << " s" << endl; |
76 | | |
77 | | vec v = point->get_vector(); |
78 | | double w = point->get_scalar(); |
79 | | TMatrix sqmat2 = sqmat; |
80 | | |
81 | | tt.tic(); |
82 | | sqmat2.opupdt(v, w); |
83 | | elapsed = tt.toc(); |
84 | | |
85 | | if (!fast) { |
86 | | mat expA = A + w * outer_product(v, v); |
87 | | CHECK_CLOSE(expA, sqmat2.to_mat(), epsilon); |
88 | | } |
89 | | |
90 | | cout << "opupdt: " << elapsed << " s" << endl; |
91 | | |
92 | | TMatrix invmat(sz); |
93 | | |
94 | | tt.tic(); |
95 | | sqmat.inv(invmat); |
96 | | elapsed = tt.toc(); |
97 | | |
98 | | mat invA; |
99 | | if (!fast) { |
100 | | invA = inv(A); |
101 | | CHECK_CLOSE(invA, invmat.to_mat(), epsilon); |
102 | | } |
103 | | |
104 | | cout << "inv: " << elapsed << " s" << endl; |
105 | | |
106 | | tt.tic(); |
107 | | double ld = sqmat.logdet(); |
108 | | elapsed = tt.toc(); |
109 | | |
110 | | if (!fast) { |
111 | | double d = det(A); |
112 | | CHECK_CLOSE(log(d), ld, epsilon); |
113 | | } |
114 | | |
115 | | cout << "logdet: " << elapsed << " s" << endl; |
116 | | |
117 | | tt.tic(); |
118 | | double q = sqmat.qform(ones(sz)); |
119 | | elapsed = tt.toc(); |
120 | | |
121 | | if (!fast) { |
122 | | CHECK_CLOSE(sumsum(A), q, epsilon); |
123 | | } |
124 | | |
125 | | cout << "qform(1): " << elapsed << " s" << endl; |
126 | | |
127 | | tt.tic(); |
128 | | q = sqmat.qform(v); |
129 | | elapsed = tt.toc(); |
130 | | |
131 | | if (!fast) { |
132 | | double r = (A * v) * v; |
133 | | CHECK_CLOSE(r, q, epsilon); |
134 | | } |
135 | | |
136 | | cout << "qform(v): " << elapsed << " s" << endl; |
137 | | |
138 | | tt.tic(); |
139 | | q = sqmat.invqform(v); |
140 | | elapsed = tt.toc(); |
141 | | |
142 | | if (!fast) { |
143 | | double r = (invA * v) * v; |
144 | | CHECK_CLOSE(r, q, epsilon); |
145 | | } |
146 | | |
147 | | cout << "invqform: " << elapsed << " s" << endl; |
148 | | |
149 | | TMatrix twice = sqmat; |
150 | | |
151 | | tt.tic(); |
152 | | twice += sqmat; |
153 | | elapsed = tt.toc(); |
154 | | |
155 | | if (!fast) { |
156 | | res = 2 * A; |
157 | | CHECK_CLOSE(res, twice.to_mat(), epsilon); |
158 | | } |
159 | | |
160 | | cout << "+=: " << elapsed << " s" << endl; |
161 | | |
162 | | sqmat2 = sqmat; |
163 | | |
164 | | tt.tic(); |
165 | | sqmat2.mult_sym(A); |
166 | | elapsed = tt.toc(); |
167 | | |
168 | | if (!fast) { |
169 | | res = (A * A) * A.T(); |
170 | | CHECK_CLOSE(res, sqmat2.to_mat(), epsilon); |
171 | | } |
172 | | |
173 | | cout << "^2: " << elapsed << " s" << endl; |
174 | | } |
175 | | |
176 | | void test_agenda(FTestMatrix test) { |
177 | | UIFile fag(agenda_file_name); |
178 | | Array<square_mat_point *> mag; |
179 | | UI::get(mag, fag, "agenda"); |
180 | | int sz = mag.size(); |
181 | | CHECK(sz > 0); |
182 | | for (int i = 0; i < sz; ++i) { |
183 | | test(i, mag(i)); |
184 | | } |
185 | | |
186 | | for (int i = 0; i < sz; ++i) { |
187 | | square_mat_point *p = mag(i); |
188 | | mag(i) = 0; |
189 | | delete p; |
190 | | } |
191 | | } |
192 | | |
193 | | SUITE(ldmat) { |
194 | | TEST(agenda) { |
195 | | test_agenda(test_matrix<ldmat>); |
196 | | } |
197 | | } |
198 | | |
199 | | SUITE(fsqmat) { |
200 | | TEST(agenda) { |
201 | | test_agenda(test_matrix<fsqmat>); |
202 | | } |
203 | | } |
204 | | |
205 | | SUITE(chmat) { |
206 | | TEST(agenda) { |
207 | | test_agenda(test_matrix<chmat>); |
208 | | } |
209 | | } |
210 | | |
211 | | int main(int argc, char const *argv[]) { |
212 | | bool unknown = false; |
213 | | int update_next = 0; // 1 suite, 2 epsilon, 3 agenda file |
214 | | const char *suite = "ldmat"; |
215 | | const char **param = argv + 1; |
216 | | while (*param && !unknown) { |
217 | | if (update_next) { |
218 | | if (update_next == 1) { |
219 | | suite = *param; |
220 | | } else if (update_next == 2) { |
221 | | double eps = atof(*param); |
222 | | if (eps > 0) { |
223 | | epsilon = eps; |
| 53 | void test_matrix ( int index, square_mat_point *point ) { |
| 54 | Real_Timer tt; |
| 55 | |
| 56 | cout << "agenda[" << index << "]:" << endl; |
| 57 | mat A = point->get_matrix(); |
| 58 | int sz = A.rows(); |
| 59 | CHECK_EQUAL ( A.cols(), sz ); |
| 60 | |
| 61 | tt.tic(); |
| 62 | TMatrix sqmat ( A ); |
| 63 | double elapsed = tt.toc(); |
| 64 | cout << "ctor(" << sz << " x " << sz << "): " << elapsed << " s" << endl; |
| 65 | |
| 66 | tt.tic(); |
| 67 | mat res = sqmat.to_mat(); |
| 68 | elapsed = tt.toc(); |
| 69 | |
| 70 | if ( !fast ) { |
| 71 | CHECK_CLOSE ( A, res, epsilon ); |
| 72 | } |
| 73 | |
| 74 | cout << "to_mat: " << elapsed << " s" << endl; |
| 75 | |
| 76 | vec v = point->get_vector(); |
| 77 | double w = point->get_scalar(); |
| 78 | TMatrix sqmat2 = sqmat; |
| 79 | |
| 80 | tt.tic(); |
| 81 | sqmat2.opupdt ( v, w ); |
| 82 | elapsed = tt.toc(); |
| 83 | |
| 84 | if ( !fast ) { |
| 85 | mat expA = A + w * outer_product ( v, v ); |
| 86 | CHECK_CLOSE ( expA, sqmat2.to_mat(), epsilon ); |
| 87 | } |
| 88 | |
| 89 | cout << "opupdt: " << elapsed << " s" << endl; |
| 90 | |
| 91 | TMatrix invmat ( sz ); |
| 92 | |
| 93 | tt.tic(); |
| 94 | sqmat.inv ( invmat ); |
| 95 | elapsed = tt.toc(); |
| 96 | |
| 97 | mat invA; |
| 98 | if ( !fast ) { |
| 99 | invA = inv ( A ); |
| 100 | CHECK_CLOSE ( invA, invmat.to_mat(), epsilon ); |
| 101 | } |
| 102 | |
| 103 | cout << "inv: " << elapsed << " s" << endl; |
| 104 | |
| 105 | tt.tic(); |
| 106 | double ld = sqmat.logdet(); |
| 107 | elapsed = tt.toc(); |
| 108 | |
| 109 | if ( !fast ) { |
| 110 | double d = det ( A ); |
| 111 | CHECK_CLOSE ( log ( d ), ld, epsilon ); |
| 112 | } |
| 113 | |
| 114 | cout << "logdet: " << elapsed << " s" << endl; |
| 115 | |
| 116 | tt.tic(); |
| 117 | double q = sqmat.qform ( ones ( sz ) ); |
| 118 | elapsed = tt.toc(); |
| 119 | |
| 120 | if ( !fast ) { |
| 121 | CHECK_CLOSE ( sumsum ( A ), q, epsilon ); |
| 122 | } |
| 123 | |
| 124 | cout << "qform(1): " << elapsed << " s" << endl; |
| 125 | |
| 126 | tt.tic(); |
| 127 | q = sqmat.qform ( v ); |
| 128 | elapsed = tt.toc(); |
| 129 | |
| 130 | if ( !fast ) { |
| 131 | double r = ( A * v ) * v; |
| 132 | CHECK_CLOSE ( r, q, epsilon ); |
| 133 | } |
| 134 | |
| 135 | cout << "qform(v): " << elapsed << " s" << endl; |
| 136 | |
| 137 | tt.tic(); |
| 138 | q = sqmat.invqform ( v ); |
| 139 | elapsed = tt.toc(); |
| 140 | |
| 141 | if ( !fast ) { |
| 142 | double r = ( invA * v ) * v; |
| 143 | CHECK_CLOSE ( r, q, epsilon ); |
| 144 | } |
| 145 | |
| 146 | cout << "invqform: " << elapsed << " s" << endl; |
| 147 | |
| 148 | TMatrix twice = sqmat; |
| 149 | |
| 150 | tt.tic(); |
| 151 | twice += sqmat; |
| 152 | elapsed = tt.toc(); |
| 153 | |
| 154 | if ( !fast ) { |
| 155 | res = 2 * A; |
| 156 | CHECK_CLOSE ( res, twice.to_mat(), epsilon ); |
| 157 | } |
| 158 | |
| 159 | cout << "+=: " << elapsed << " s" << endl; |
| 160 | |
| 161 | sqmat2 = sqmat; |
| 162 | |
| 163 | tt.tic(); |
| 164 | sqmat2.mult_sym ( A ); |
| 165 | elapsed = tt.toc(); |
| 166 | |
| 167 | if ( !fast ) { |
| 168 | res = ( A * A ) * A.T(); |
| 169 | CHECK_CLOSE ( res, sqmat2.to_mat(), epsilon ); |
| 170 | } |
| 171 | |
| 172 | cout << "^2: " << elapsed << " s" << endl; |
| 173 | } |
| 174 | |
| 175 | void test_agenda ( FTestMatrix test ) { |
| 176 | UIFile fag ( agenda_file_name ); |
| 177 | Array<square_mat_point *> mag; |
| 178 | UI::get ( mag, fag, "agenda" ); |
| 179 | int sz = mag.size(); |
| 180 | CHECK ( sz > 0 ); |
| 181 | for ( int i = 0; i < sz; ++i ) { |
| 182 | test ( i, mag ( i ) ); |
| 183 | } |
| 184 | |
| 185 | for ( int i = 0; i < sz; ++i ) { |
| 186 | square_mat_point *p = mag ( i ); |
| 187 | mag ( i ) = 0; |
| 188 | delete p; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | SUITE ( ldmat ) { |
| 193 | TEST ( agenda ) { |
| 194 | test_agenda ( test_matrix<ldmat> ); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | SUITE ( fsqmat ) { |
| 199 | TEST ( agenda ) { |
| 200 | test_agenda ( test_matrix<fsqmat> ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | SUITE ( chmat ) { |
| 205 | TEST ( agenda ) { |
| 206 | test_agenda ( test_matrix<chmat> ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | int main ( int argc, char const *argv[] ) { |
| 211 | bool unknown = false; |
| 212 | int update_next = 0; // 1 suite, 2 epsilon, 3 agenda file |
| 213 | const char *suite = "ldmat"; |
| 214 | const char **param = argv + 1; |
| 215 | while ( *param && !unknown ) { |
| 216 | if ( update_next ) { |
| 217 | if ( update_next == 1 ) { |
| 218 | suite = *param; |
| 219 | } else if ( update_next == 2 ) { |
| 220 | double eps = atof ( *param ); |
| 221 | if ( eps > 0 ) { |
| 222 | epsilon = eps; |
| 223 | } else { |
| 224 | cerr << "invalid epsilon value ignored" << endl; |
| 225 | } |
| 226 | } else { |
| 227 | agenda_file_name = *param; |
| 228 | } |
| 229 | |
| 230 | update_next = 0; |