Debian Codegraph @

mex/sources/libkorder/sylv/tests/tests.cc

raw
1 /*
2 * Copyright © 2004-2011 Ondra Kamenik
3 * Copyright © 2019-2026 Dynare Team
4 *
5 * This file is part of Dynare.
6 *
7 * Dynare is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * Dynare is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Dynare. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 #include "GeneralSylvester.hh"
22 #include "IterativeSylvester.hh"
23 #include "KronUtils.hh"
24 #include "KronVector.hh"
25 #include "QuasiTriangular.hh"
26 #include "QuasiTriangularZero.hh"
27 #include "SchurDecompEig.hh"
28 #include "SimilarityDecomp.hh"
29 #include "SylvException.hh"
30 #include "SylvMatrix.hh"
31 #include "TriangularSylvester.hh"
32 #include "Vector.hh"
33 #include "int_power.hh"
34
35 #include "MMMatrix.hh"
36
37 #include <cmath>
38 #include <ctime>
39 #include <iomanip>
40 #include <iostream>
41 #include <memory>
42 #include <string>
43 #include <utility>
44
45 class TestRunnable
46 {
47 public:
48 const std::string name;
49 static constexpr double eps_norm = 1.0e-10;
50 TestRunnable(std::string n) : name(std::move(n))
51 {
52 }
53 virtual ~TestRunnable() = default;
54 [[nodiscard]] bool test() const;
55 [[nodiscard]] virtual bool run() const = 0;
56
57 protected:
58 // declaration of auxiliary static methods
59 static bool quasi_solve(bool trans, const std::string& mname, const std::string& vname);
60 static bool mult_kron(bool trans, const std::string& mname, const std::string& vname,
61 const std::string& cname, int m, int n, int depth);
62 static bool level_kron(bool trans, const std::string& mname, const std::string& vname,
63 const std::string& cname, int level, int m, int n, int depth);
64 static bool kron_power(const std::string& m1name, const std::string& m2name,
65 const std::string& vname, const std::string& cname, int m, int n,
66 int depth);
67 static bool lin_eval(const std::string& m1name, const std::string& m2name,
68 const std::string& vname, const std::string& cname, int m, int n, int depth,
69 double alpha, double beta1, double beta2);
70 static bool qua_eval(const std::string& m1name, const std::string& m2name,
71 const std::string& vname, const std::string& cname, int m, int n, int depth,
72 double alpha, double betas, double gamma, double delta1, double delta2);
73 static bool tri_sylv(const std::string& m1name, const std::string& m2name,
74 const std::string& vname, int m, int n, int depth);
75 static bool gen_sylv(const std::string& aname, const std::string& bname, const std::string& cname,
76 const std::string& dname, int m, int n, int order);
77 static bool eig_bubble(const std::string& aname, int from, int to);
78 static bool block_diag(const std::string& aname, double log10norm = 3.0);
79 static bool iter_sylv(const std::string& m1name, const std::string& m2name,
80 const std::string& vname, int m, int n, int depth);
81 };
82
83 bool
84 TestRunnable::test() const
85 {
86 std::cout << "Running test <" << name << '>' << '\n';
87 clock_t start = clock();
88 bool passed = run();
89 clock_t end = clock();
90 std::cout << "CPU time " << (static_cast<double>(end - start)) / CLOCKS_PER_SEC
91 << " (CPU seconds)..................";
92 if (passed)
93 std::cout << "passed";
94 else
95 std::cout << "FAILED";
96 std::cout << '\n' << '\n';
97 return passed;
98 }
99
100 /**********************************************************/
101 /* auxiliary methods */
102 /**********************************************************/
103
104 bool
105 TestRunnable::quasi_solve(bool trans, const std::string& mname, const std::string& vname)
106 {
107 MMMatrixIn mmt(mname);
108 MMMatrixIn mmv(vname);
109
110 std::unique_ptr<QuasiTriangular> t;
111 std::unique_ptr<QuasiTriangular> tsave;
112 if (mmt.row() == mmt.col())
113 {
114 t = std::make_unique<QuasiTriangular>(mmt.getData(), mmt.row());
115 tsave = std::make_unique<QuasiTriangular>(*t);
116 }
117 else if (mmt.row() > mmt.col())
118 {
119 t = std::make_unique<QuasiTriangularZero>(mmt.row() - mmt.col(), mmt.getData(), mmt.col());
120 tsave = std::make_unique<QuasiTriangularZero>(
121 const_cast<const QuasiTriangularZero&>(dynamic_cast<QuasiTriangularZero&>(*t)));
122 }
123 else
124 {
125 std::cout << " Wrong quasi triangular dimensions, rows must be >= cols.\n";
126 return false;
127 }
128 ConstVector v {mmv.getData()};
129 Vector x(v.length());
130 double eig_min = 1.0e20;
131 if (trans)
132 t->solveTrans(x, v, eig_min);
133 else
134 t->solve(x, v, eig_min);
135 std::cout << "eig_min = " << eig_min << '\n';
136 Vector xx(v.length());
137 if (trans)
138 tsave->multVecTrans(xx, ConstVector(x));
139 else
140 tsave->multVec(xx, ConstVector(x));
141 xx.add(-1.0, v);
142 xx.add(1.0, x);
143 double norm = xx.getNorm();
144 std::cout << "\terror norm = " << norm << '\n';
145 return norm < eps_norm;
146 }
147
148 bool
149 TestRunnable::mult_kron(bool trans, const std::string& mname, const std::string& vname,
150 const std::string& cname, int m, int n, int depth)
151 {
152 MMMatrixIn mmt(mname);
153 MMMatrixIn mmv(vname);
154 MMMatrixIn mmc(cname);
155
156 int length = power(m, depth) * n;
157 if (mmt.row() != m || mmv.row() != length || mmc.row() != length)
158 {
159 std::cout << " Incompatible sizes for kron mult action, len=" << length
160 << ", matrow=" << mmt.row() << ", m=" << m << ", vrow=" << mmv.row()
161 << ", crow=" << mmc.row() << '\n';
162 return false;
163 }
164
165 QuasiTriangular t(mmt.getData(), mmt.row());
166 Vector vraw {mmv.getData()};
167 KronVector v(vraw, m, n, depth);
168 Vector craw {mmc.getData()};
169 KronVector c(craw, m, n, depth);
170 if (trans)
171 t.multKronTrans(v);
172 else
173 t.multKron(v);
174 c.add(-1.0, v);
175 double norm = c.getNorm();
176 std::cout << "\terror norm = " << norm << '\n';
177 return norm < eps_norm;
178 }
179
180 bool
181 TestRunnable::level_kron(bool trans, const std::string& mname, const std::string& vname,
182 const std::string& cname, int level, int m, int n, int depth)
183 {
184 MMMatrixIn mmt(mname);
185 MMMatrixIn mmv(vname);
186 MMMatrixIn mmc(cname);
187
188 int length = power(m, depth) * n;
189 if ((level > 0 && mmt.row() != m) || (level == 0 && mmt.row() != n) || mmv.row() != length
190 || mmc.row() != length)
191 {
192 std::cout << " Incompatible sizes for kron mult action, len=" << length
193 << ", matrow=" << mmt.row() << ", m=" << m << ", n=" << n << ", vrow=" << mmv.row()
194 << ", crow=" << mmc.row() << '\n';
195 return false;
196 }
197
198 QuasiTriangular t(mmt.getData(), mmt.row());
199 Vector vraw {mmv.getData()};
200 ConstKronVector v(vraw, m, n, depth);
201 Vector craw {mmc.getData()};
202 KronVector c(craw, m, n, depth);
203 KronVector x(v);
204 if (trans)
205 KronUtils::multAtLevelTrans(level, t, x);
206 else
207 KronUtils::multAtLevel(level, t, x);
208 x.add(-1, c);
209 double norm = x.getNorm();
210 std::cout << "\terror norm = " << norm << '\n';
211 return norm < eps_norm;
212 }
213
214 bool
215 TestRunnable::kron_power(const std::string& m1name, const std::string& m2name,
216 const std::string& vname, const std::string& cname, int m, int n,
217 int depth)
218 {
219 MMMatrixIn mmt1(m1name);
220 MMMatrixIn mmt2(m2name);
221 MMMatrixIn mmv(vname);
222 MMMatrixIn mmc(cname);
223
224 int length = power(m, depth) * n;
225 if (mmt1.row() != m || mmt2.row() != n || mmv.row() != length || mmc.row() != length)
226 {
227 std::cout << " Incompatible sizes for kron power mult action, len=" << length
228 << ", row1=" << mmt1.row() << ", row2=" << mmt2.row() << ", m=" << m << ", n=" << n
229 << ", vrow=" << mmv.row() << ", crow=" << mmc.row() << '\n';
230 return false;
231 }
232
233 QuasiTriangular t1(mmt1.getData(), mmt1.row());
234 QuasiTriangular t2(mmt2.getData(), mmt2.row());
235 Vector vraw {mmv.getData()};
236 ConstKronVector v(vraw, m, n, depth);
237 Vector craw {mmc.getData()};
238 KronVector c(craw, m, n, depth);
239 KronVector x(v);
240 KronUtils::multKron(t1, t2, x);
241 x.add(-1, c);
242 double norm = x.getNorm();
243 std::cout << "\terror norm = " << norm << '\n';
244 return norm < eps_norm;
245 }
246
247 bool
248 TestRunnable::lin_eval(const std::string& m1name, const std::string& m2name,
249 const std::string& vname, const std::string& cname, int m, int n, int depth,
250 double alpha, double beta1, double beta2)
251 {
252 MMMatrixIn mmt1(m1name);
253 MMMatrixIn mmt2(m2name);
254 MMMatrixIn mmv(vname);
255 MMMatrixIn mmc(cname);
256
257 int length = power(m, depth) * n;
258 if (mmt1.row() != m || mmt2.row() != n || mmv.row() != 2 * length || mmc.row() != 2 * length)
259 {
260 std::cout << " Incompatible sizes for lin eval action, len=" << length
261 << ", row1=" << mmt1.row() << ", row2=" << mmt2.row() << ", m=" << m << ", n=" << n
262 << ", vrow=" << mmv.row() << ", crow=" << mmc.row() << '\n';
263 return false;
264 }
265
266 QuasiTriangular t1(mmt1.getData(), mmt1.row());
267 QuasiTriangular t2(mmt2.getData(), mmt2.row());
268 TriangularSylvester ts(t2, t1);
269 ConstVector vraw1 {mmv.getData(), 0, length};
270 ConstKronVector v1(vraw1, m, n, depth);
271 ConstVector vraw2 {mmv.getData(), length, length};
272 ConstKronVector v2(vraw2, m, n, depth);
273 ConstVector craw1 {mmc.getData(), 0, length};
274 ConstKronVector c1(craw1, m, n, depth);
275 ConstVector craw2 {mmc.getData(), length, length};
276 ConstKronVector c2(craw2, m, n, depth);
277 KronVector x1(m, n, depth);
278 KronVector x2(m, n, depth);
279 ts.linEval(alpha, beta1, beta2, x1, x2, v1, v2);
280 x1.add(-1, c1);
281 x2.add(-1, c2);
282 double norm1 = x1.getNorm();
283 double norm2 = x2.getNorm();
284 std::cout << "\terror norm1 = " << norm1 << "\n\terror norm2 = " << norm2 << '\n';
285 return norm1 * norm1 + norm2 * norm2 < eps_norm * eps_norm;
286 }
287
288 bool
289 TestRunnable::qua_eval(const std::string& m1name, const std::string& m2name,
290 const std::string& vname, const std::string& cname, int m, int n, int depth,
291 double alpha, double betas, double gamma, double delta1, double delta2)
292 {
293 MMMatrixIn mmt1(m1name);
294 MMMatrixIn mmt2(m2name);
295 MMMatrixIn mmv(vname);
296 MMMatrixIn mmc(cname);
297
298 int length = power(m, depth) * n;
299 if (mmt1.row() != m || mmt2.row() != n || mmv.row() != 2 * length || mmc.row() != 2 * length)
300 {
301 std::cout << " Incompatible sizes for qua eval action, len=" << length
302 << ", row1=" << mmt1.row() << ", row2=" << mmt2.row() << ", m=" << m << ", n=" << n
303 << ", vrow=" << mmv.row() << ", crow=" << mmc.row() << '\n';
304 return false;
305 }
306
307 QuasiTriangular t1(mmt1.getData(), mmt1.row());
308 QuasiTriangular t2(mmt2.getData(), mmt2.row());
309 TriangularSylvester ts(t2, t1);
310 ConstVector vraw1 {mmv.getData(), 0, length};
311 ConstKronVector v1(vraw1, m, n, depth);
312 ConstVector vraw2 {mmv.getData(), length, length};
313 ConstKronVector v2(vraw2, m, n, depth);
314 ConstVector craw1 {mmc.getData(), 0, length};
315 ConstKronVector c1(craw1, m, n, depth);
316 ConstVector craw2 {mmc.getData(), length, length};
317 ConstKronVector c2(craw2, m, n, depth);
318 KronVector x1(m, n, depth);
319 KronVector x2(m, n, depth);
320 ts.quaEval(alpha, betas, gamma, delta1, delta2, x1, x2, v1, v2);
321 x1.add(-1, c1);
322 x2.add(-1, c2);
323 double norm1 = x1.getNorm();
324 double norm2 = x2.getNorm();
325 std::cout << "\terror norm1 = " << norm1 << "\n\terror norm2 = " << norm2 << '\n';
326 return norm1 * norm1 + norm2 * norm2 < 100 * eps_norm * eps_norm; // relax norm
327 }
328
329 bool
330 TestRunnable::tri_sylv(const std::string& m1name, const std::string& m2name,
331 const std::string& vname, int m, int n, int depth)
332 {
333 MMMatrixIn mmt1(m1name);
334 MMMatrixIn mmt2(m2name);
335 MMMatrixIn mmv(vname);
336
337 int length = power(m, depth) * n;
338 if (mmt1.row() != m || mmt2.row() != n || mmv.row() != length)
339 {
340 std::cout << " Incompatible sizes for triangular sylvester action, len=" << length
341 << ", row1=" << mmt1.row() << ", row2=" << mmt2.row() << ", m=" << m << ", n=" << n
342 << ", vrow=" << mmv.row() << '\n';
343 return false;
344 }
345
346 QuasiTriangular t1(mmt1.getData(), mmt1.row());
347 QuasiTriangular t2(mmt2.getData(), mmt2.row());
348 TriangularSylvester ts(t2, t1);
349 Vector vraw {mmv.getData()};
350 ConstKronVector v(vraw, m, n, depth);
351 KronVector d(v); // copy of v
352 SylvParams pars;
353 ts.solve(pars, d);
354 pars.print("\t");
355 KronVector dcheck(const_cast<const KronVector&>(d));
356 KronUtils::multKron(t1, t2, dcheck);
357 dcheck.add(1.0, d);
358 dcheck.add(-1.0, v);
359 double norm = dcheck.getNorm();
360 double xnorm = v.getNorm();
361 std::cout << "\trel. error norm = " << norm / xnorm << '\n';
362 double max = dcheck.getMax();
363 double xmax = v.getMax();
364 std::cout << "\trel. error max = " << max / xmax << '\n';
365 return norm < xnorm * eps_norm;
366 }
367
368 bool
369 TestRunnable::gen_sylv(const std::string& aname, const std::string& bname, const std::string& cname,
370 const std::string& dname, int m, int n, int order)
371 {
372 MMMatrixIn mma(aname);
373 MMMatrixIn mmb(bname);
374 MMMatrixIn mmc(cname);
375 MMMatrixIn mmd(dname);
376
377 if (m != mmc.row() || m != mmc.col() || n != mma.row() || n != mma.col() || n != mmb.row()
378 || n < mmb.col() || n != mmd.row() || power(m, order) != mmd.col())
379 {
380 std::cout << " Incompatible sizes for gen_sylv.\n";
381 return false;
382 }
383
384 SylvParams ps(true);
385 GeneralSylvester gs(order, n, m, n - mmb.col(), mma.getData(), mmb.getData(), mmc.getData(),
386 mmd.getData(), ps);
387 gs.solve();
388 gs.check(mmd.getData());
389 const SylvParams& pars = gs.getParams();
390 pars.print("\t");
391 return *(pars.mat_err1) < eps_norm && *(pars.mat_errI) < eps_norm && *(pars.mat_errF) < eps_norm
392 && *(pars.vec_err1) < eps_norm && *(pars.vec_errI) < eps_norm;
393 }
394
395 bool
396 TestRunnable::eig_bubble(const std::string& aname, int from, int to)
397 {
398 MMMatrixIn mma(aname);
399
400 if (mma.row() != mma.col())
401 {
402 std::cout << " Matrix is not square\n";
403 return false;
404 }
405
406 int n = mma.row();
407 QuasiTriangular orig(mma.getData(), n);
408 SchurDecompEig dec(const_cast<const QuasiTriangular&>(orig));
409 auto itf = dec.getT().diag_begin();
410 auto itt = dec.getT().diag_begin();
411 for (int i = 0; i < from; i++)
412 ++itf;
413 for (int i = 0; i < to; i++)
414 ++itt;
415 itt = dec.bubbleEigen(itf, itt);
416 SqSylvMatrix check(dec.getQ() * dec.getT());
417 check.multRightTrans(dec.getQ());
418 check.add(-1, orig);
419 double norm1 = check.getNorm1();
420 double normInf = check.getNormInf();
421 double onorm1 = orig.getNorm1();
422 double onormInf = orig.getNormInf();
423 std::cout << "\tabs. error1 = " << norm1 << '\n'
424 << "\tabs. error∞ = " << normInf << '\n'
425 << "\trel. error1 = " << norm1 / onorm1 << '\n'
426 << "\trel. error∞ = " << normInf / onormInf << '\n';
427 return norm1 < eps_norm * onorm1 && normInf < eps_norm * onormInf;
428 }
429
430 bool
431 TestRunnable::block_diag(const std::string& aname, double log10norm)
432 {
433 MMMatrixIn mma(aname);
434
435 if (mma.row() != mma.col())
436 {
437 std::cout << " Matrix is not square\n";
438 return false;
439 }
440
441 int n = mma.row();
442 SqSylvMatrix orig(mma.getData(), n);
443 SimilarityDecomp dec(orig.getData(), orig.nrows(), log10norm);
444 dec.getB().printInfo();
445 SqSylvMatrix check(dec.getQ() * dec.getB());
446 check.multRight(dec.getInvQ());
447 check.add(-1, orig);
448 double norm1 = check.getNorm1();
449 double normInf = check.getNormInf();
450 double onorm1 = orig.getNorm1();
451 double onormInf = orig.getNormInf();
452 std::cout << "\terror Q·B·Q⁻¹:" << '\n'
453 << "\tabs. error1 = " << norm1 << '\n'
454 << "\tabs. error∞ = " << normInf << '\n'
455 << "\trel. error1 = " << norm1 / onorm1 << '\n'
456 << "\trel. error∞ = " << normInf / onormInf << '\n';
457 SqSylvMatrix check2(dec.getQ() * dec.getInvQ());
458 SqSylvMatrix in(n);
459 in.setUnit();
460 check2.add(-1, in);
461 double nor1 = check2.getNorm1();
462 double norInf = check2.getNormInf();
463 std::cout << "\terror Q·Q⁻¹:" << '\n'
464 << "\tabs. error1 = " << nor1 << '\n'
465 << "\tabs. error∞ = " << norInf << '\n';
466 return norm1 < eps_norm * pow(10, log10norm) * onorm1;
467 }
468
469 bool
470 TestRunnable::iter_sylv(const std::string& m1name, const std::string& m2name,
471 const std::string& vname, int m, int n, int depth)
472 {
473 MMMatrixIn mmt1(m1name);
474 MMMatrixIn mmt2(m2name);
475 MMMatrixIn mmv(vname);
476
477 int length = power(m, depth) * n;
478 if (mmt1.row() != m || mmt2.row() != n || mmv.row() != length)
479 {
480 std::cout << " Incompatible sizes for triangular sylvester iteration, len=" << length
481 << ", row1=" << mmt1.row() << ", row2=" << mmt2.row() << ", m=" << m << ", n=" << n
482 << ", vrow=" << mmv.row() << '\n';
483 return false;
484 }
485
486 QuasiTriangular t1(mmt1.getData(), mmt1.row());
487 QuasiTriangular t2(mmt2.getData(), mmt2.row());
488 IterativeSylvester is(t2, t1);
489 Vector vraw {mmv.getData()};
490 ConstKronVector v(vraw, m, n, depth);
491 KronVector d(v); // copy of v
492 SylvParams pars;
493 pars.method = SylvParams::solve_method::iter;
494 is.solve(pars, d);
495 pars.print("\t");
496 KronVector dcheck(const_cast<const KronVector&>(d));
497 KronUtils::multKron(t1, t2, dcheck);
498 dcheck.add(1.0, d);
499 dcheck.add(-1.0, v);
500 double cnorm = dcheck.getNorm();
501 double xnorm = v.getNorm();
502 std::cout << "\trel. error norm = " << cnorm / xnorm << '\n';
503 double max = dcheck.getMax();
504 double xmax = v.getMax();
505 std::cout << "\trel. error max = " << max / xmax << '\n';
506 return cnorm < xnorm * eps_norm;
507 }
508
509 /**********************************************************/
510 /* sub classes declarations */
511 /**********************************************************/
512
513 class PureTriangTest : public TestRunnable
514 {
515 public:
516 PureTriangTest() : TestRunnable("pure triangular solve (5)")
517 {
518 }
519 [[nodiscard]] bool run() const override;
520 };
521
522 class PureTriangTransTest : public TestRunnable
523 {
524 public:
525 PureTriangTransTest() : TestRunnable("pure triangular solve trans (5)")
526 {
527 }
528 [[nodiscard]] bool run() const override;
529 };
530
531 class PureTrLargeTest : public TestRunnable
532 {
533 public:
534 PureTrLargeTest() : TestRunnable("pure triangular large solve (300)")
535 {
536 }
537 [[nodiscard]] bool run() const override;
538 };
539
540 class PureTrLargeTransTest : public TestRunnable
541 {
542 public:
543 PureTrLargeTransTest() : TestRunnable("pure triangular large solve trans (300)")
544 {
545 }
546 [[nodiscard]] bool run() const override;
547 };
548
549 class QuasiTriangTest : public TestRunnable
550 {
551 public:
552 QuasiTriangTest() : TestRunnable("quasi triangular solve (7)")
553 {
554 }
555 [[nodiscard]] bool run() const override;
556 };
557
558 class QuasiTriangTransTest : public TestRunnable
559 {
560 public:
561 QuasiTriangTransTest() : TestRunnable("quasi triangular solve trans (7)")
562 {
563 }
564 [[nodiscard]] bool run() const override;
565 };
566
567 class QuasiTrLargeTest : public TestRunnable
568 {
569 public:
570 QuasiTrLargeTest() : TestRunnable("quasi triangular solve large (250)")
571 {
572 }
573 [[nodiscard]] bool run() const override;
574 };
575
576 class QuasiTrLargeTransTest : public TestRunnable
577 {
578 public:
579 QuasiTrLargeTransTest() : TestRunnable("quasi triangular solve large trans (250)")
580 {
581 }
582 [[nodiscard]] bool run() const override;
583 };
584
585 class QuasiZeroSmallTest : public TestRunnable
586 {
587 public:
588 QuasiZeroSmallTest() : TestRunnable("quasi tr. zero small test (2×1)")
589 {
590 }
591 [[nodiscard]] bool run() const override;
592 };
593
594 class MultKronSmallTest : public TestRunnable
595 {
596 public:
597 MultKronSmallTest() : TestRunnable("kronecker small mult (2=2×1)")
598 {
599 }
600 [[nodiscard]] bool run() const override;
601 };
602
603 class MultKronTest : public TestRunnable
604 {
605 public:
606 MultKronTest() : TestRunnable("kronecker mult (245=7×7×5)")
607 {
608 }
609 [[nodiscard]] bool run() const override;
610 };
611
612 class MultKronSmallTransTest : public TestRunnable
613 {
614 public:
615 MultKronSmallTransTest() : TestRunnable("kronecker small trans mult (2=2×1)")
616 {
617 }
618 [[nodiscard]] bool run() const override;
619 };
620
621 class MultKronTransTest : public TestRunnable
622 {
623 public:
624 MultKronTransTest() : TestRunnable("kronecker trans mult (245=7×7×5)")
625 {
626 }
627 [[nodiscard]] bool run() const override;
628 };
629
630 class LevelKronTest : public TestRunnable
631 {
632 public:
633 LevelKronTest() : TestRunnable("kronecker level mult (1715=7×[7]×7×5)")
634 {
635 }
636 [[nodiscard]] bool run() const override;
637 };
638
639 class LevelKronTransTest : public TestRunnable
640 {
641 public:
642 LevelKronTransTest() : TestRunnable("kronecker level trans mult (1715=7×[7]×7×5)")
643 {
644 }
645 [[nodiscard]] bool run() const override;
646 };
647
648 class LevelZeroKronTest : public TestRunnable
649 {
650 public:
651 LevelZeroKronTest() : TestRunnable("kronecker level mult (1715=7×7×7×[5])")
652 {
653 }
654 [[nodiscard]] bool run() const override;
655 };
656
657 class LevelZeroKronTransTest : public TestRunnable
658 {
659 public:
660 LevelZeroKronTransTest() : TestRunnable("kronecker level trans mult (1715=7×7×7×[5])")
661 {
662 }
663 [[nodiscard]] bool run() const override;
664 };
665
666 class KronPowerTest : public TestRunnable
667 {
668 public:
669 KronPowerTest() : TestRunnable("kronecker power mult (1715=7×7×7×5)")
670 {
671 }
672 [[nodiscard]] bool run() const override;
673 };
674
675 class SmallLinEvalTest : public TestRunnable
676 {
677 public:
678 SmallLinEvalTest() : TestRunnable("lin eval (24=2 × 2×2×3)")
679 {
680 }
681 [[nodiscard]] bool run() const override;
682 };
683
684 class LinEvalTest : public TestRunnable
685 {
686 public:
687 LinEvalTest() : TestRunnable("lin eval (490=2 × 7×7×5)")
688 {
689 }
690 [[nodiscard]] bool run() const override;
691 };
692
693 class SmallQuaEvalTest : public TestRunnable
694 {
695 public:
696 SmallQuaEvalTest() : TestRunnable("qua eval (24=2 × 2×2×3)")
697 {
698 }
699 [[nodiscard]] bool run() const override;
700 };
701
702 class QuaEvalTest : public TestRunnable
703 {
704 public:
705 QuaEvalTest() : TestRunnable("qua eval (490=2 × 7×7×5)")
706 {
707 }
708 [[nodiscard]] bool run() const override;
709 };
710
711 class TriSylvSmallRealTest : public TestRunnable
712 {
713 public:
714 TriSylvSmallRealTest() : TestRunnable("triangular sylvester small real solve (12=2×2×3)")
715 {
716 }
717 [[nodiscard]] bool run() const override;
718 };
719
720 class TriSylvSmallComplexTest : public TestRunnable
721 {
722 public:
723 TriSylvSmallComplexTest() : TestRunnable("triangular sylvester small complx solve (12=2×2×3)")
724 {
725 }
726 [[nodiscard]] bool run() const override;
727 };
728
729 class TriSylvTest : public TestRunnable
730 {
731 public:
732 TriSylvTest() : TestRunnable("triangular sylvester solve (245=7×7×5)")
733 {
734 }
735 [[nodiscard]] bool run() const override;
736 };
737
738 class TriSylvBigTest : public TestRunnable
739 {
740 public:
741 TriSylvBigTest() : TestRunnable("triangular sylvester big solve (48000=40×40×30)")
742 {
743 }
744 [[nodiscard]] bool run() const override;
745 };
746
747 class TriSylvLargeTest : public TestRunnable
748 {
749 public:
750 TriSylvLargeTest() : TestRunnable("triangular sylvester large solve (1920000=40×40×40×30)")
751 {
752 }
753 [[nodiscard]] bool run() const override;
754 };
755
756 class IterSylvTest : public TestRunnable
757 {
758 public:
759 IterSylvTest() : TestRunnable("iterative sylvester solve (245=7×7×5)")
760 {
761 }
762 [[nodiscard]] bool run() const override;
763 };
764
765 class IterSylvLargeTest : public TestRunnable
766 {
767 public:
768 IterSylvLargeTest() : TestRunnable("iterative sylvester large solve (1920000=40×40×40×30)")
769 {
770 }
771 [[nodiscard]] bool run() const override;
772 };
773
774 class GenSylvSmallTest : public TestRunnable
775 {
776 public:
777 GenSylvSmallTest() : TestRunnable("general sylvester small solve (18=3×3×2)")
778 {
779 }
780 [[nodiscard]] bool run() const override;
781 };
782
783 class GenSylvTest : public TestRunnable
784 {
785 public:
786 GenSylvTest() : TestRunnable("general sylvester solve (12000=20×20×30)")
787 {
788 }
789 [[nodiscard]] bool run() const override;
790 };
791
792 class GenSylvSingTest : public TestRunnable
793 {
794 public:
795 GenSylvSingTest() : TestRunnable("general sylvester solve for sing. C (2500000=50×50×50×20)")
796 {
797 }
798 [[nodiscard]] bool run() const override;
799 };
800
801 class GenSylvLargeTest : public TestRunnable
802 {
803 public:
804 GenSylvLargeTest() : TestRunnable("general sylvester solve (2500000=50×50×50×20)")
805 {
806 }
807 [[nodiscard]] bool run() const override;
808 };
809
810 class EigBubFrankTest : public TestRunnable
811 {
812 public:
813 EigBubFrankTest() : TestRunnable("eig. bubble frank test (12×12)")
814 {
815 }
816 [[nodiscard]] bool run() const override;
817 };
818
819 class EigBubSplitTest : public TestRunnable
820 {
821 // complex eigenvalue is split by swapping it with real
822 public:
823 EigBubSplitTest() : TestRunnable("eig. bubble complex split test (3×3)")
824 {
825 }
826 [[nodiscard]] bool run() const override;
827 };
828
829 class EigBubSameTest : public TestRunnable
830 {
831 // complex eigenevalue bypasses the same complex eigenvalue
832 public:
833 EigBubSameTest() : TestRunnable("eig. bubble same test (5×5)")
834 {
835 }
836 [[nodiscard]] bool run() const override;
837 };
838
839 class BlockDiagSmallTest : public TestRunnable
840 {
841 public:
842 BlockDiagSmallTest() : TestRunnable("block diagonalization small test (7×7)")
843 {
844 }
845 [[nodiscard]] bool run() const override;
846 };
847
848 class BlockDiagFrankTest : public TestRunnable
849 {
850 public:
851 BlockDiagFrankTest() : TestRunnable("block diagonalization of frank (12×12)")
852 {
853 }
854 [[nodiscard]] bool run() const override;
855 };
856
857 class BlockDiagIllCondTest : public TestRunnable
858 {
859 public:
860 BlockDiagIllCondTest() : TestRunnable("block diagonalization of ill conditioned (15×15)")
861 {
862 }
863 [[nodiscard]] bool run() const override;
864 };
865
866 class BlockDiagBigTest : public TestRunnable
867 {
868 public:
869 BlockDiagBigTest() : TestRunnable("block diagonalization big test (50×50)")
870 {
871 }
872 [[nodiscard]] bool run() const override;
873 };
874
875 /**********************************************************/
876 /* run methods of sub classes */
877 /**********************************************************/
878
879 bool
880 PureTriangTest::run() const
881 {
882 return quasi_solve(false, "tr5x5.mm", "v5.mm");
883 }
884
885 bool
886 PureTriangTransTest::run() const
887 {
888 return quasi_solve(true, "tr5x5.mm", "v5.mm");
889 }
890
891 bool
892 PureTrLargeTest::run() const
893 {
894 return quasi_solve(false, "tr300x300.mm", "v300.mm");
895 }
896
897 bool
898 PureTrLargeTransTest::run() const
899 {
900 return quasi_solve(true, "tr300x300.mm", "v300.mm");
901 }
902
903 bool
904 QuasiTriangTest::run() const
905 {
906 return quasi_solve(false, "qt7x7.mm", "v7.mm");
907 }
908
909 bool
910 QuasiTriangTransTest::run() const
911 {
912 return quasi_solve(true, "qt7x7.mm", "v7.mm");
913 }
914
915 bool
916 QuasiTrLargeTest::run() const
917 {
918 return quasi_solve(false, "qt250x250.mm", "v250.mm");
919 }
920
921 bool
922 QuasiTrLargeTransTest::run() const
923 {
924 return quasi_solve(true, "qt250x250.mm", "v250.mm");
925 }
926
927 bool
928 QuasiZeroSmallTest::run() const
929 {
930 return quasi_solve(false, "b2x1.mm", "v2.mm");
931 }
932
933 bool
934 MultKronSmallTest::run() const
935 {
936 return mult_kron(false, "tr2x2.mm", "v2.mm", "vcheck2.mm", 2, 1, 1);
937 }
938
939 bool
940 MultKronTest::run() const
941 {
942 return mult_kron(false, "qt7x7.mm", "v245.mm", "vcheck245.mm", 7, 5, 2);
943 }
944
945 bool
946 MultKronSmallTransTest::run() const
947 {
948 return mult_kron(true, "tr2x2.mm", "v2.mm", "vcheck2a.mm", 2, 1, 1);
949 }
950
951 bool
952 MultKronTransTest::run() const
953 {
954 return mult_kron(true, "qt7x7.mm", "v245.mm", "vcheck245a.mm", 7, 5, 2);
955 }
956
957 bool
958 LevelKronTest::run() const
959 {
960 return level_kron(false, "qt7x7.mm", "v1715.mm", "vcheck1715.mm", 2, 7, 5, 3);
961 }
962
963 bool
964 LevelKronTransTest::run() const
965 {
966 return level_kron(true, "qt7x7.mm", "v1715.mm", "vcheck1715a.mm", 2, 7, 5, 3);
967 }
968
969 bool
970 LevelZeroKronTest::run() const
971 {
972 return level_kron(false, "tr5x5.mm", "v1715.mm", "vcheck1715b.mm", 0, 7, 5, 3);
973 }
974
975 bool
976 LevelZeroKronTransTest::run() const
977 {
978 return level_kron(true, "tr5x5.mm", "v1715.mm", "vcheck1715c.mm", 0, 7, 5, 3);
979 }
980
981 bool
982 KronPowerTest::run() const
983 {
984 return kron_power("qt7x7.mm", "tr5x5.mm", "v1715.mm", "vcheck1715d.mm", 7, 5, 3);
985 }
986
987 bool
988 SmallLinEvalTest::run() const
989 {
990 return lin_eval("qt2x2.mm", "qt3x3.mm", "v24.mm", "vcheck24.mm", 2, 3, 2, 2, 1, 3);
991 }
992
993 bool
994 LinEvalTest::run() const
995 {
996 return lin_eval("qt7x7.mm", "tr5x5.mm", "v490.mm", "vcheck490.mm", 7, 5, 2, 2, 1, 3);
997 }
998
999 bool
1000 SmallQuaEvalTest::run() const
1001 {
1002 return qua_eval("qt2x2.mm", "qt3x3.mm", "v24.mm", "vcheck24q.mm", 2, 3, 2, -0.5, 3, 2, 1, 3);
1003 }
1004
1005 bool
1006 QuaEvalTest::run() const
1007 {
1008 return qua_eval("qt7x7.mm", "tr5x5.mm", "v490.mm", "vcheck490q.mm", 7, 5, 2, -0.5, 3, 2, 1, 3);
1009 }
1010
1011 bool
1012 TriSylvSmallRealTest::run() const
1013 {
1014 return tri_sylv("tr2x2.mm", "qt3x3.mm", "v12r.mm", 2, 3, 2);
1015 }
1016
1017 bool
1018 TriSylvSmallComplexTest::run() const
1019 {
1020 return tri_sylv("qt2x2.mm", "qt3x3.mm", "v12r.mm", 2, 3, 2);
1021 }
1022
1023 bool
1024 TriSylvTest::run() const
1025 {
1026 return tri_sylv("qt7x7eig06-09.mm", "tr5x5.mm", "v245r.mm", 7, 5, 2);
1027 }
1028
1029 bool
1030 TriSylvBigTest::run() const
1031 {
1032 return tri_sylv("qt40x40.mm", "qt30x30eig011-095.mm", "v48000.mm", 40, 30, 2);
1033 }
1034
1035 bool
1036 TriSylvLargeTest::run() const
1037 {
1038 return tri_sylv("qt40x40.mm", "qt30x30eig011-095.mm", "v1920000.mm", 40, 30, 3);
1039 }
1040
1041 bool
1042 IterSylvTest::run() const
1043 {
1044 return iter_sylv("qt7x7eig06-09.mm", "qt5x5.mm", "v245r.mm", 7, 5, 2);
1045 }
1046
1047 bool
1048 IterSylvLargeTest::run() const
1049 {
1050 return iter_sylv("qt40x40.mm", "qt30x30eig011-095.mm", "v1920000.mm", 40, 30, 3);
1051 }
1052
1053 bool
1054 GenSylvSmallTest::run() const
1055 {
1056 return gen_sylv("a2x2.mm", "b2x1.mm", "c3x3.mm", "d2x9.mm", 3, 2, 2);
1057 }
1058
1059 bool
1060 GenSylvTest::run() const
1061 {
1062 return gen_sylv("a30x30.mm", "b30x25.mm", "c20x20.mm", "d30x400.mm", 20, 30, 2);
1063 }
1064
1065 bool
1066 GenSylvSingTest::run() const
1067 {
1068 return gen_sylv("a20x20.mm", "b20x4.mm", "c50x50sing.mm", "d20x125000.mm", 50, 20, 3);
1069 }
1070
1071 bool
1072 GenSylvLargeTest::run() const
1073 {
1074 return gen_sylv("a20x20.mm", "b20x15.mm", "c50x50.mm", "d20x125000.mm", 50, 20, 3);
1075 }
1076
1077 bool
1078 EigBubFrankTest::run() const
1079 {
1080 return eig_bubble("qt_frank12x12.mm", 8, 0);
1081 }
1082
1083 bool
1084 EigBubSplitTest::run() const
1085 {
1086 return eig_bubble("qt_eps3x3.mm", 1, 0);
1087 }
1088
1089 bool
1090 EigBubSameTest::run() const
1091 {
1092 return eig_bubble("qt5x5.mm", 2, 0);
1093 }
1094
1095 bool
1096 BlockDiagSmallTest::run() const
1097 {
1098 return block_diag("qt7x7.mm", 0.1);
1099 }
1100
1101 bool
1102 BlockDiagFrankTest::run() const
1103 {
1104 return block_diag("qt_frank12x12.mm", 5);
1105 }
1106
1107 bool
1108 BlockDiagIllCondTest::run() const
1109 {
1110 return block_diag("ill_cond15x15.mm", 4.14);
1111 }
1112
1113 bool
1114 BlockDiagBigTest::run() const
1115 {
1116 return block_diag("c50x50.mm", 1.3);
1117 }
1118
1119 /**********************************************************/
1120 /* main */
1121 /**********************************************************/
1122
1123 int
1124 main()
1125 {
1126 std::vector<std::unique_ptr<TestRunnable>> all_tests;
1127 // fill in vector of all tests
1128 all_tests.push_back(std::make_unique<PureTriangTest>());
1129 all_tests.push_back(std::make_unique<PureTriangTransTest>());
1130 all_tests.push_back(std::make_unique<PureTrLargeTest>());
1131 all_tests.push_back(std::make_unique<PureTrLargeTransTest>());
1132 all_tests.push_back(std::make_unique<QuasiTriangTest>());
1133 all_tests.push_back(std::make_unique<QuasiTriangTransTest>());
1134 all_tests.push_back(std::make_unique<QuasiTrLargeTest>());
1135 all_tests.push_back(std::make_unique<QuasiTrLargeTransTest>());
1136 all_tests.push_back(std::make_unique<QuasiZeroSmallTest>());
1137 all_tests.push_back(std::make_unique<MultKronSmallTest>());
1138 all_tests.push_back(std::make_unique<MultKronTest>());
1139 all_tests.push_back(std::make_unique<MultKronSmallTransTest>());
1140 all_tests.push_back(std::make_unique<MultKronTransTest>());
1141 all_tests.push_back(std::make_unique<LevelKronTest>());
1142 all_tests.push_back(std::make_unique<LevelKronTransTest>());
1143 all_tests.push_back(std::make_unique<LevelZeroKronTest>());
1144 all_tests.push_back(std::make_unique<LevelZeroKronTransTest>());
1145 all_tests.push_back(std::make_unique<KronPowerTest>());
1146 all_tests.push_back(std::make_unique<SmallLinEvalTest>());
1147 all_tests.push_back(std::make_unique<LinEvalTest>());
1148 all_tests.push_back(std::make_unique<SmallQuaEvalTest>());
1149 all_tests.push_back(std::make_unique<QuaEvalTest>());
1150 all_tests.push_back(std::make_unique<EigBubFrankTest>());
1151 all_tests.push_back(std::make_unique<EigBubSplitTest>());
1152 all_tests.push_back(std::make_unique<EigBubSameTest>());
1153 all_tests.push_back(std::make_unique<BlockDiagSmallTest>());
1154 all_tests.push_back(std::make_unique<BlockDiagFrankTest>());
1155 all_tests.push_back(std::make_unique<BlockDiagIllCondTest>());
1156 all_tests.push_back(std::make_unique<BlockDiagBigTest>());
1157 all_tests.push_back(std::make_unique<TriSylvSmallRealTest>());
1158 all_tests.push_back(std::make_unique<TriSylvSmallComplexTest>());
1159 all_tests.push_back(std::make_unique<TriSylvTest>());
1160 all_tests.push_back(std::make_unique<TriSylvBigTest>());
1161 all_tests.push_back(std::make_unique<TriSylvLargeTest>());
1162 all_tests.push_back(std::make_unique<IterSylvTest>());
1163 all_tests.push_back(std::make_unique<IterSylvLargeTest>());
1164 all_tests.push_back(std::make_unique<GenSylvSmallTest>());
1165 all_tests.push_back(std::make_unique<GenSylvTest>());
1166 all_tests.push_back(std::make_unique<GenSylvSingTest>());
1167 all_tests.push_back(std::make_unique<GenSylvLargeTest>());
1168
1169 // launch the tests
1170 std::cout << std::setprecision(4);
1171 int success = 0;
1172 for (const auto& test : all_tests)
1173 {
1174 try
1175 {
1176 if (test->test())
1177 success++;
1178 }
1179 catch (const MMException& e)
1180 {
1181 std::cout << "Caught MM exception in <" << test->name << ">:\n" << e.getMessage();
1182 }
1183 catch (SylvException& e)
1184 {
1185 std::cout << "Caught Sylv exception in " << test->name << ":\n";
1186 e.printMessage();
1187 }
1188 }
1189
1190 int nfailed = all_tests.size() - success;
1191 std::cout << "There were " << nfailed << " tests that failed out of " << all_tests.size()
1192 << " tests run." << '\n';
1193
1194 if (nfailed)
1195 return EXIT_FAILURE;
1196 else
1197 return EXIT_SUCCESS;
1198 }