Debian Codegraph @

mex/sources/libkorder/tl/tests/tests.cc

raw
1 /*
2 * Copyright © 2004 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 "SylvException.hh"
22 #include "factory.hh"
23 #include "gs_tensor.hh"
24 #include "monoms.hh"
25 #include "ps_tensor.hh"
26 #include "rfs_tensor.hh"
27 #include "stack_container.hh"
28 #include "t_container.hh"
29 #include "t_polynomial.hh"
30 #include "tl_exception.hh"
31 #include "tl_static.hh"
32
33 #include <cstdlib>
34 #include <ctime>
35 #include <iomanip>
36 #include <iostream>
37 #include <memory>
38 #include <string>
39 #include <utility>
40 #include <vector>
41
42 class TestRunnable
43 {
44 public:
45 const std::string name;
46 int dim; // dimension of the solved problem
47 int nvar; // number of variable of the solved problem
48 TestRunnable(std::string name_arg, int d, int nv) : name {std::move(name_arg)}, dim(d), nvar(nv)
49 {
50 }
51 virtual ~TestRunnable() = default;
52 [[nodiscard]] bool test() const;
53 [[nodiscard]] virtual bool run() const = 0;
54
55 protected:
56 template<class _Ttype>
57 static bool index_forward(const Symmetry& s, const IntSequence& nvs);
58
59 template<class _Ttype>
60 static bool index_backward(const Symmetry& s, const IntSequence& nvs);
61
62 template<class _Ttype>
63 static bool index_offset(const Symmetry& s, const IntSequence& nvs);
64
65 static bool fold_unfold(std::unique_ptr<FTensor> folded);
66 static bool
67 fs_fold_unfold(int r, int nv, int dim)
68 {
69 Factory f;
70 return fold_unfold(f.make<FFSTensor>(r, nv, dim));
71 }
72 static bool
73 r_fold_unfold(int r, int nv, int dim)
74 {
75 Factory f;
76 return fold_unfold(f.make<FRTensor>(r, nv, dim));
77 }
78 static bool
79 gs_fold_unfold(int r, const Symmetry& s, const IntSequence& nvs)
80 {
81 Factory f;
82 return fold_unfold(f.make<FGSTensor>(r, s, nvs));
83 }
84
85 static bool dense_prod(const Symmetry& bsym, const IntSequence& bnvs, int hdim, int hnv,
86 int rows);
87
88 static bool folded_monomial(int ng, int nx, int ny, int nu, int dim);
89
90 static bool unfolded_monomial(int ng, int nx, int ny, int nu, int dim);
91
92 static bool fold_zcont(int nf, int ny, int nu, int nup, int nbigg, int ng, int dim);
93
94 static bool unfold_zcont(int nf, int ny, int nu, int nup, int nbigg, int ng, int dim);
95
96 static bool folded_contraction(int r, int nv, int dim);
97
98 static bool unfolded_contraction(int r, int nv, int dim);
99
100 static bool poly_eval(int r, int nv, int maxdim);
101 };
102
103 bool
104 TestRunnable::test() const
105 {
106 std::cout << "Running test <" << name << ">" << '\n';
107 clock_t start = clock();
108 bool passed = run();
109 clock_t end = clock();
110 std::cout << "CPU time " << static_cast<double>(end - start) / CLOCKS_PER_SEC
111 << " (CPU seconds)..................";
112 if (passed)
113 std::cout << "passed\n\n";
114 else
115 std::cout << "FAILED\n\n";
116 return passed;
117 }
118
119 /****************************************************/
120 /* definition of TestRunnable static methods */
121 /****************************************************/
122 template<class _Ttype>
123 bool
124 TestRunnable::index_forward(const Symmetry& s, const IntSequence& nvs)
125 {
126 int fails = 0;
127 int ndecr = 0;
128 int nincr = 0;
129 _Ttype dummy(0, TensorDimens(s, nvs));
130 auto run = dummy.end();
131 do
132 {
133 --run;
134 ndecr++;
135 auto run2 = dummy.begin();
136 for (int i = 0; i < *run; i++)
137 {
138 ++run2;
139 nincr++;
140 }
141 if (run != run2)
142 fails++;
143 }
144 while (run != dummy.begin());
145
146 std::cout << "\tnumber of columns = " << dummy.ncols() << '\n'
147 << "\tnumber of increments = " << nincr << '\n'
148 << "\tnumber of decrements = " << ndecr << '\n'
149 << "\tnumber of failures = " << fails << '\n';
150
151 return fails == 0;
152 }
153
154 template<class _Ttype>
155 bool
156 TestRunnable::index_backward(const Symmetry& s, const IntSequence& nvs)
157 {
158 int fails = 0;
159 int ndecr = 0;
160 int nincr = 0;
161 _Ttype dummy(0, TensorDimens(s, nvs));
162 auto run = dummy.begin();
163 while (run != dummy.end())
164 {
165 auto run2 = dummy.end();
166 for (int i = 0; i < dummy.ncols() - *run; i++)
167 {
168 --run2;
169 ndecr++;
170 }
171 if (run != run2)
172 fails++;
173 ++run;
174 nincr++;
175 }
176
177 std::cout << "\tnumber of columns = " << dummy.ncols() << '\n'
178 << "\tnumber of increments = " << nincr << '\n'
179 << "\tnumber of decrements = " << ndecr << '\n'
180 << "\tnumber of failures = " << fails << '\n';
181
182 return fails == 0;
183 }
184
185 template<class _Ttype>
186 bool
187 TestRunnable::index_offset(const Symmetry& s, const IntSequence& nvs)
188 {
189 int fails = 0;
190 int nincr = 0;
191 _Ttype dummy(0, TensorDimens(s, nvs));
192 for (auto run = dummy.begin(); run != dummy.end(); ++run, nincr++)
193 {
194 typename _Ttype::index run2(dummy, run.getCoor());
195 if (run != run2)
196 fails++;
197 }
198
199 std::cout << "\tnumber of columns = " << dummy.ncols() << '\n'
200 << "\tnumber of increments = " << nincr << '\n'
201 << "\tnumber of failures = " << fails << '\n';
202
203 return fails == 0;
204 }
205
206 bool
207 TestRunnable::fold_unfold(std::unique_ptr<FTensor> folded)
208 {
209 auto unfolded = folded->unfold();
210 auto folded2 = unfolded->fold();
211 folded2->add(-1.0, *folded);
212 double normInf = folded2->getNormInf();
213 double norm1 = folded2->getNorm1();
214 std::cout << "\tfolded size: (" << folded->nrows() << ", " << folded->ncols() << ")\n"
215 << "\tunfolded size: (" << unfolded->nrows() << ", " << unfolded->ncols() << ")\n"
216 << "\tdifference normInf: " << normInf << '\n'
217 << "\tdifference norm1: " << norm1 << '\n';
218
219 return normInf < 1.0e-15;
220 }
221
222 bool
223 TestRunnable::dense_prod(const Symmetry& bsym, const IntSequence& bnvs, int hdim, int hnv, int rows)
224 {
225 Factory f;
226 FGSContainer cont {f.makeCont<FGSTensor, FGSContainer>(hnv, bnvs, bsym.dimen() - hdim + 1)};
227 auto fh = f.make<FGSTensor>(rows, Symmetry {hdim}, IntSequence(1, hnv));
228 UGSTensor uh(*fh);
229 FGSTensor fb(rows, TensorDimens(bsym, bnvs));
230 fb.getData().zeros();
231 clock_t s1 = clock();
232 cont.multAndAdd(uh, fb);
233 clock_t s2 = clock();
234 UGSContainer ucont(cont);
235 clock_t s3 = clock();
236 UGSTensor ub(rows, fb.getDims());
237 ub.getData().zeros();
238 clock_t s4 = clock();
239 ucont.multAndAdd(uh, ub);
240 clock_t s5 = clock();
241
242 UGSTensor btmp(fb);
243 btmp.add(-1, ub);
244 double norm = btmp.getData().getMax();
245 double norm1 = btmp.getNorm1();
246 double normInf = btmp.getNormInf();
247
248 std::cout << "\ttime for folded product: " << static_cast<double>(s2 - s1) / CLOCKS_PER_SEC
249 << '\n'
250 << "\ttime for unfolded product: " << static_cast<double>(s5 - s4) / CLOCKS_PER_SEC
251 << '\n'
252 << "\ttime for container convert: " << static_cast<double>(s3 - s2) / CLOCKS_PER_SEC
253 << '\n'
254 << "\tunfolded difference normMax: " << norm << '\n'
255 << "\tunfolded difference norm1: " << norm1 << '\n'
256 << "\tunfolded difference normInf: " << normInf << '\n';
257
258 return norm < 1.e-13;
259 }
260
261 bool
262 TestRunnable::folded_monomial(int ng, int nx, int ny, int nu, int dim)
263 {
264 clock_t gen_time = clock();
265 DenseDerivGenerator gen(ng, nx, ny, nu, 5, 0.3, dim);
266 gen_time = clock() - gen_time;
267 std::cout << "\ttime for monom generation: " << static_cast<double>(gen_time) / CLOCKS_PER_SEC
268 << '\n';
269 IntSequence nvs {ny, nu};
270 double maxnorm = 0;
271 for (int ydim = 0; ydim <= dim; ydim++)
272 {
273 Symmetry s {ydim, dim - ydim};
274 std::cout << "\tSymmetry: ";
275 s.print();
276 FGSTensor res(ng, TensorDimens(s, nvs));
277 res.getData().zeros();
278 clock_t stime = clock();
279 for (int d = 1; d <= dim; d++)
280 gen.xcont.multAndAdd(*(gen.ts[d - 1]), res);
281 stime = clock() - stime;
282 std::cout << "\t\ttime for symmetry: " << static_cast<double>(stime) / CLOCKS_PER_SEC << '\n';
283 const FGSTensor& mres = gen.rcont.get(s);
284 res.add(-1.0, mres);
285 double normtmp = res.getData().getMax();
286 std::cout << "\t\terror normMax: " << normtmp << '\n';
287 maxnorm = std::max(maxnorm, normtmp);
288 }
289 return maxnorm < 1.0e-10;
290 }
291
292 bool
293 TestRunnable::unfolded_monomial(int ng, int nx, int ny, int nu, int dim)
294 {
295 clock_t gen_time = clock();
296 DenseDerivGenerator gen(ng, nx, ny, nu, 5, 0.3, dim);
297 gen_time = clock() - gen_time;
298 std::cout << "\ttime for monom generation: " << static_cast<double>(gen_time) / CLOCKS_PER_SEC
299 << '\n';
300 clock_t u_time = clock();
301 gen.unfold();
302 u_time = clock() - u_time;
303 std::cout << "\ttime for monom unfolding: " << static_cast<double>(u_time) / CLOCKS_PER_SEC
304 << '\n';
305 IntSequence nvs {ny, nu};
306 double maxnorm = 0;
307 for (int ydim = 0; ydim <= dim; ydim++)
308 {
309 Symmetry s {ydim, dim - ydim};
310 std::cout << "\tSymmetry: ";
311 s.print();
312 UGSTensor res(ng, TensorDimens(s, nvs));
313 res.getData().zeros();
314 clock_t stime = clock();
315 for (int d = 1; d <= dim; d++)
316 gen.uxcont.multAndAdd(*(gen.uts[d - 1]), res);
317 stime = clock() - stime;
318 std::cout << "\t\ttime for symmetry: " << static_cast<double>(stime) / CLOCKS_PER_SEC << '\n';
319 const FGSTensor& mres = gen.rcont.get(s);
320 FGSTensor foldres(res);
321 foldres.add(-1.0, mres);
322 double normtmp = foldres.getData().getMax();
323 std::cout << "\t\terror normMax: " << normtmp << '\n';
324 maxnorm = std::max(maxnorm, normtmp);
325 }
326 return maxnorm < 1.0e-10;
327 }
328
329 bool
330 TestRunnable::fold_zcont(int nf, int ny, int nu, int nup, int nbigg, int ng, int dim)
331 {
332 clock_t gen_time = clock();
333 SparseDerivGenerator dg(nf, ny, nu, nup, nbigg, ng, 5, 0.55, dim);
334 gen_time = clock() - gen_time;
335 for (int d = 1; d <= dim; d++)
336 std::cout << "\tfill of dim=" << d << " tensor: " << std::setprecision(2) << std::fixed
337 << std::setw(6) << 100 * dg.ts[d - 1].getFillFactor() << std::setprecision(6)
338 << std::defaultfloat << " %\n";
339 std::cout << "\ttime for monom generation: " << static_cast<double>(gen_time) / CLOCKS_PER_SEC
340 << '\n';
341
342 IntSequence nvs {ny, nu, nup, 1};
343 double maxnorm = 0.0;
344
345 // form ZContainer
346 FoldedZContainer zc(&dg.bigg, nbigg, &dg.g, ng, ny, nu);
347
348 for (int d = 2; d <= dim; d++)
349 for (auto& si : SymmetrySet(d, 4))
350 {
351 std::cout << "\tSymmetry: ";
352 si.print();
353 FGSTensor res(nf, TensorDimens(si, nvs));
354 res.getData().zeros();
355 clock_t stime = clock();
356 for (int l = 1; l <= si.dimen(); l++)
357 zc.multAndAdd(dg.ts[l - 1], res);
358 stime = clock() - stime;
359 std::cout << "\t\ttime for symmetry: " << static_cast<double>(stime) / CLOCKS_PER_SEC
360 << '\n';
361 const FGSTensor& mres = dg.rcont.get(si);
362 res.add(-1.0, mres);
363 double normtmp = res.getData().getMax();
364 std::cout << "\t\terror normMax: " << normtmp << '\n';
365 maxnorm = std::max(maxnorm, normtmp);
366 }
367
368 return maxnorm < 1.0e-10;
369 }
370
371 bool
372 TestRunnable::unfold_zcont(int nf, int ny, int nu, int nup, int nbigg, int ng, int dim)
373 {
374 clock_t gen_time = clock();
375 SparseDerivGenerator dg(nf, ny, nu, nup, nbigg, ng, 5, 0.55, dim);
376 gen_time = clock() - gen_time;
377 for (int d = 1; d <= dim; d++)
378 std::cout << "\tfill of dim=" << d << " tensor: " << std::setprecision(2) << std::fixed
379 << std::setw(6) << 100 * dg.ts[d - 1].getFillFactor() << std::setprecision(6)
380 << std::defaultfloat << " %\n";
381 std::cout << "\ttime for monom generation: " << static_cast<double>(gen_time) / CLOCKS_PER_SEC
382 << '\n';
383
384 clock_t con_time = clock();
385 UGSContainer uG_cont(dg.bigg);
386 UGSContainer ug_cont(dg.g);
387 con_time = clock() - con_time;
388 std::cout << "\ttime for container unfold: " << static_cast<double>(con_time) / CLOCKS_PER_SEC
389 << '\n';
390
391 IntSequence nvs {ny, nu, nup, 1};
392 double maxnorm = 0.0;
393
394 // Form ZContainer
395 UnfoldedZContainer zc(&uG_cont, nbigg, &ug_cont, ng, ny, nu);
396
397 for (int d = 2; d <= dim; d++)
398 for (auto& si : SymmetrySet(d, 4))
399 {
400 std::cout << "\tSymmetry: ";
401 si.print();
402 UGSTensor res(nf, TensorDimens(si, nvs));
403 res.getData().zeros();
404 clock_t stime = clock();
405 for (int l = 1; l <= si.dimen(); l++)
406 zc.multAndAdd(dg.ts[l - 1], res);
407 stime = clock() - stime;
408 std::cout << "\t\ttime for symmetry: " << static_cast<double>(stime) / CLOCKS_PER_SEC
409 << '\n';
410 FGSTensor fold_res(res);
411 const FGSTensor& mres = dg.rcont.get(si);
412 fold_res.add(-1.0, mres);
413 double normtmp = fold_res.getData().getMax();
414 std::cout << "\t\terror normMax: " << normtmp << '\n';
415 maxnorm = std::max(maxnorm, normtmp);
416 }
417
418 return maxnorm < 1.0e-10;
419 }
420
421 bool
422 TestRunnable::folded_contraction(int r, int nv, int dim)
423 {
424 Factory fact;
425 Vector x {fact.makeVector(nv)};
426
427 auto forig = fact.make<FFSTensor>(r, nv, dim);
428 auto f = std::make_unique<FFSTensor>(*forig);
429 clock_t ctime = clock();
430 for (int d = dim - 1; d > 0; d--)
431 f = std::make_unique<FFSTensor>(*f, ConstVector(x));
432 ctime = clock() - ctime;
433 Vector res(forig->nrows());
434 res.zeros();
435 f->multaVec(res, x);
436
437 UFSTensor u(*forig);
438 clock_t utime = clock();
439 URSingleTensor ux(x, dim);
440 Vector v(u.nrows());
441 v.zeros();
442 u.multaVec(v, ux.getData());
443 utime = clock() - utime;
444
445 v.add(-1.0, res);
446 std::cout << "\ttime for folded contraction: " << static_cast<double>(ctime) / CLOCKS_PER_SEC
447 << '\n'
448 << "\ttime for unfolded power: " << static_cast<double>(utime) / CLOCKS_PER_SEC
449 << '\n'
450 << "\terror normMax: " << v.getMax() << '\n'
451 << "\terror norm1: " << v.getNorm1() << '\n';
452
453 return v.getMax() < 1.e-10;
454 }
455
456 bool
457 TestRunnable::unfolded_contraction(int r, int nv, int dim)
458 {
459 Factory fact;
460 Vector x {fact.makeVector(nv)};
461
462 auto forig = fact.make<FFSTensor>(r, nv, dim);
463 UFSTensor uorig(*forig);
464 auto u = std::make_unique<UFSTensor>(uorig);
465 clock_t ctime = clock();
466 for (int d = dim - 1; d > 0; d--)
467 u = std::make_unique<UFSTensor>(*u, ConstVector(x));
468 ctime = clock() - ctime;
469 Vector res(uorig.nrows());
470 res.zeros();
471 u->multaVec(res, x);
472
473 clock_t utime = clock();
474 URSingleTensor ux(x, dim);
475 Vector v(uorig.nrows());
476 v.zeros();
477 uorig.multaVec(v, ux.getData());
478 utime = clock() - utime;
479
480 v.add(-1.0, res);
481 std::cout << "\ttime for unfolded contraction: " << static_cast<double>(ctime) / CLOCKS_PER_SEC
482 << '\n'
483 << "\ttime for unfolded power: " << static_cast<double>(utime) / CLOCKS_PER_SEC
484 << '\n'
485 << "\terror normMax: " << v.getMax() << '\n'
486 << "\terror norm1: " << v.getNorm1() << '\n';
487
488 return v.getMax() < 1.e-10;
489 }
490
491 bool
492 TestRunnable::poly_eval(int r, int nv, int maxdim)
493 {
494 Factory fact;
495 Vector x {fact.makeVector(nv)};
496
497 Vector out_ft(r);
498 out_ft.zeros();
499 Vector out_fh(r);
500 out_fh.zeros();
501 Vector out_ut(r);
502 out_ut.zeros();
503 Vector out_uh(r);
504 out_uh.zeros();
505
506 FTensorPolynomial fp {fact.makePoly<FFSTensor, FTensorPolynomial>(r, nv, maxdim)};
507
508 clock_t ft_cl = clock();
509 fp.evalTrad(out_ft, x);
510 ft_cl = clock() - ft_cl;
511 std::cout << "\ttime for folded power eval: " << static_cast<double>(ft_cl) / CLOCKS_PER_SEC
512 << '\n';
513
514 clock_t fh_cl = clock();
515 fp.evalHorner(out_fh, x);
516 fh_cl = clock() - fh_cl;
517 std::cout << "\ttime for folded horner eval: " << static_cast<double>(fh_cl) / CLOCKS_PER_SEC
518 << '\n';
519
520 UTensorPolynomial up {fp};
521
522 clock_t ut_cl = clock();
523 up.evalTrad(out_ut, x);
524 ut_cl = clock() - ut_cl;
525 std::cout << "\ttime for unfolded power eval: " << static_cast<double>(ut_cl) / CLOCKS_PER_SEC
526 << '\n';
527
528 clock_t uh_cl = clock();
529 up.evalHorner(out_uh, x);
530 uh_cl = clock() - uh_cl;
531 std::cout << "\ttime for unfolded horner eval: " << static_cast<double>(uh_cl) / CLOCKS_PER_SEC
532 << '\n';
533
534 out_ft.add(-1.0, out_ut);
535 double max_ft = out_ft.getMax();
536 out_fh.add(-1.0, out_ut);
537 double max_fh = out_fh.getMax();
538 out_uh.add(-1.0, out_ut);
539 double max_uh = out_uh.getMax();
540
541 std::cout << "\tfolded power error norm max: " << max_ft << '\n'
542 << "\tfolded horner error norm max: " << max_fh << '\n'
543 << "\tunfolded horner error norm max: " << max_uh << '\n';
544
545 return max_ft + max_fh + max_uh < 1.0e-10;
546 }
547
548 /****************************************************/
549 /* definition of TestRunnable subclasses */
550 /****************************************************/
551 class SmallIndexForwardFold : public TestRunnable
552 {
553 public:
554 SmallIndexForwardFold() : TestRunnable("small index forward for fold (44)(222)", 5, 4)
555 {
556 }
557 [[nodiscard]] bool
558 run() const override
559 {
560 Symmetry s {2, 3};
561 IntSequence nvs {4, 2};
562 return index_forward<FGSTensor>(s, nvs);
563 }
564 };
565
566 class SmallIndexForwardUnfold : public TestRunnable
567 {
568 public:
569 SmallIndexForwardUnfold() : TestRunnable("small index forward for unfold (44)(222)", 5, 4)
570 {
571 }
572 [[nodiscard]] bool
573 run() const override
574 {
575 Symmetry s {2, 3};
576 IntSequence nvs {4, 2};
577 return index_forward<UGSTensor>(s, nvs);
578 }
579 };
580
581 class IndexForwardFold : public TestRunnable
582 {
583 public:
584 IndexForwardFold() : TestRunnable("index forward for fold (55)(222)(22)", 7, 5)
585 {
586 }
587 [[nodiscard]] bool
588 run() const override
589 {
590 Symmetry s {2, 3, 2};
591 IntSequence nvs {5, 2, 2};
592 return index_forward<FGSTensor>(s, nvs);
593 }
594 };
595
596 class IndexForwardUnfold : public TestRunnable
597 {
598 public:
599 IndexForwardUnfold() : TestRunnable("index forward for unfold (55)(222)(22)", 7, 5)
600 {
601 }
602 [[nodiscard]] bool
603 run() const override
604 {
605 Symmetry s {2, 3, 2};
606 IntSequence nvs {5, 2, 2};
607 return index_forward<UGSTensor>(s, nvs);
608 }
609 };
610
611 class SmallIndexBackwardFold : public TestRunnable
612 {
613 public:
614 SmallIndexBackwardFold() : TestRunnable("small index backward for fold (3)(3)(222)", 5, 3)
615 {
616 }
617 [[nodiscard]] bool
618 run() const override
619 {
620 Symmetry s {1, 1, 3};
621 IntSequence nvs {3, 3, 2};
622 return index_backward<FGSTensor>(s, nvs);
623 }
624 };
625
626 class IndexBackwardFold : public TestRunnable
627 {
628 public:
629 IndexBackwardFold() : TestRunnable("index backward for fold (44)(222)(44)", 7, 4)
630 {
631 }
632 [[nodiscard]] bool
633 run() const override
634 {
635 Symmetry s {2, 3, 2};
636 IntSequence nvs {4, 2, 4};
637 return index_backward<FGSTensor>(s, nvs);
638 }
639 };
640
641 class SmallIndexBackwardUnfold : public TestRunnable
642 {
643 public:
644 SmallIndexBackwardUnfold() : TestRunnable("small index backward for unfold (3)(3)(222)", 5, 3)
645 {
646 }
647 [[nodiscard]] bool
648 run() const override
649 {
650 Symmetry s {1, 1, 3};
651 IntSequence nvs {3, 3, 2};
652 return index_backward<UGSTensor>(s, nvs);
653 }
654 };
655
656 class IndexBackwardUnfold : public TestRunnable
657 {
658 public:
659 IndexBackwardUnfold() : TestRunnable("index backward for unfold (44)(222)(44)", 7, 4)
660 {
661 }
662 [[nodiscard]] bool
663 run() const override
664 {
665 Symmetry s {2, 3, 2};
666 IntSequence nvs {4, 2, 4};
667 return index_backward<UGSTensor>(s, nvs);
668 }
669 };
670
671 class SmallIndexOffsetFold : public TestRunnable
672 {
673 public:
674 SmallIndexOffsetFold() : TestRunnable("small index offset for fold (44)(222)", 5, 4)
675 {
676 }
677 [[nodiscard]] bool
678 run() const override
679 {
680 Symmetry s {2, 3};
681 IntSequence nvs {4, 2};
682 return index_offset<FGSTensor>(s, nvs);
683 }
684 };
685
686 class SmallIndexOffsetUnfold : public TestRunnable
687 {
688 public:
689 SmallIndexOffsetUnfold() : TestRunnable("small index offset for unfold (44)(222)", 5, 4)
690 {
691 }
692 [[nodiscard]] bool
693 run() const override
694 {
695 Symmetry s {2, 3};
696 IntSequence nvs {4, 2};
697 return index_offset<UGSTensor>(s, nvs);
698 }
699 };
700
701 class IndexOffsetFold : public TestRunnable
702 {
703 public:
704 IndexOffsetFold() : TestRunnable("index offset for fold (55)(222)(22)", 5, 5)
705 {
706 }
707 [[nodiscard]] bool
708 run() const override
709 {
710 Symmetry s {2, 3, 2};
711 IntSequence nvs {5, 2, 2};
712 return index_offset<FGSTensor>(s, nvs);
713 }
714 };
715
716 class IndexOffsetUnfold : public TestRunnable
717 {
718 public:
719 IndexOffsetUnfold() : TestRunnable("index offset for unfold (55)(222)(22)", 7, 5)
720 {
721 }
722 [[nodiscard]] bool
723 run() const override
724 {
725 Symmetry s {2, 3, 2};
726 IntSequence nvs {5, 2, 2};
727 return index_offset<UGSTensor>(s, nvs);
728 }
729 };
730
731 class SmallFoldUnfoldFS : public TestRunnable
732 {
733 public:
734 SmallFoldUnfoldFS() : TestRunnable("small fold-unfold for full symmetry (444)", 3, 4)
735 {
736 }
737 [[nodiscard]] bool
738 run() const override
739 {
740 return fs_fold_unfold(5, 4, 3);
741 }
742 };
743
744 class SmallFoldUnfoldGS : public TestRunnable
745 {
746 public:
747 SmallFoldUnfoldGS() : TestRunnable("small fold-unfold for gen symmetry (3)(33)(22)", 5, 3)
748 {
749 }
750 [[nodiscard]] bool
751 run() const override
752 {
753 Symmetry s {1, 2, 2};
754 IntSequence nvs {3, 3, 2};
755 return gs_fold_unfold(5, s, nvs);
756 }
757 };
758
759 class FoldUnfoldFS : public TestRunnable
760 {
761 public:
762 FoldUnfoldFS() : TestRunnable("fold-unfold for full symmetry (9999)", 4, 9)
763 {
764 }
765 [[nodiscard]] bool
766 run() const override
767 {
768 return fs_fold_unfold(5, 9, 4);
769 }
770 };
771
772 class FoldUnfoldGS : public TestRunnable
773 {
774 public:
775 FoldUnfoldGS() : TestRunnable("fold-unfold for gen symmetry (66)(2)(66)", 5, 6)
776 {
777 }
778 [[nodiscard]] bool
779 run() const override
780 {
781 Symmetry s {2, 1, 2};
782 IntSequence nvs {6, 2, 6};
783 return gs_fold_unfold(5, s, nvs);
784 }
785 };
786
787 class SmallFoldUnfoldR : public TestRunnable
788 {
789 public:
790 SmallFoldUnfoldR() : TestRunnable("small fold-unfold for row full symmetry (333)", 3, 3)
791 {
792 }
793 [[nodiscard]] bool
794 run() const override
795 {
796 return r_fold_unfold(5, 3, 3);
797 }
798 };
799
800 class FoldUnfoldR : public TestRunnable
801 {
802 public:
803 FoldUnfoldR() : TestRunnable("fold-unfold for row full symmetry (66666)", 5, 6)
804 {
805 }
806 [[nodiscard]] bool
807 run() const override
808 {
809 return r_fold_unfold(5, 6, 5);
810 }
811 };
812
813 class SmallDenseProd : public TestRunnable
814 {
815 public:
816 SmallDenseProd() : TestRunnable("small dense prod bsym=1-2,nvs=3-2,h=2-3,r=2", 3, 3)
817 {
818 }
819 [[nodiscard]] bool
820 run() const override
821 {
822 IntSequence bnvs {3, 2};
823 return dense_prod(Symmetry {1, 2}, bnvs, 2, 3, 2);
824 }
825 };
826
827 class DenseProd : public TestRunnable
828 {
829 public:
830 DenseProd() : TestRunnable("dense prod bsym=2-3,nvs=10-7,h=3-15,r=10", 5, 15)
831 {
832 }
833 [[nodiscard]] bool
834 run() const override
835 {
836 IntSequence bnvs {10, 7};
837 return dense_prod(Symmetry {2, 3}, bnvs, 3, 15, 10);
838 }
839 };
840
841 class BigDenseProd : public TestRunnable
842 {
843 public:
844 BigDenseProd() : TestRunnable("dense prod bsym=3-2,nvs=13-11,h=3-20,r=20", 6, 20)
845 {
846 }
847 [[nodiscard]] bool
848 run() const override
849 {
850 IntSequence bnvs {13, 11};
851 return dense_prod(Symmetry {3, 2}, bnvs, 3, 20, 20);
852 }
853 };
854
855 class SmallFoldedMonomial : public TestRunnable
856 {
857 public:
858 SmallFoldedMonomial() : TestRunnable("folded vrs. monoms (g,x,y,u)=(10,4,5,3), dim=4", 4, 8)
859 {
860 }
861 [[nodiscard]] bool
862 run() const override
863 {
864 return folded_monomial(10, 4, 5, 3, 4);
865 }
866 };
867
868 class FoldedMonomial : public TestRunnable
869 {
870 public:
871 FoldedMonomial() : TestRunnable("folded vrs. monoms (g,x,y,u)=(20,12,10,5), dim=4", 4, 15)
872 {
873 }
874 [[nodiscard]] bool
875 run() const override
876 {
877 return folded_monomial(20, 12, 10, 5, 4);
878 }
879 };
880
881 class SmallUnfoldedMonomial : public TestRunnable
882 {
883 public:
884 SmallUnfoldedMonomial() : TestRunnable("unfolded vrs. monoms (g,x,y,u)=(10,4,5,3), dim=4", 4, 8)
885 {
886 }
887 [[nodiscard]] bool
888 run() const override
889 {
890 return unfolded_monomial(10, 4, 5, 3, 4);
891 }
892 };
893
894 class UnfoldedMonomial : public TestRunnable
895 {
896 public:
897 UnfoldedMonomial() : TestRunnable("unfolded vrs. monoms (g,x,y,u)=(20,12,10,5), dim=4", 4, 15)
898 {
899 }
900 [[nodiscard]] bool
901 run() const override
902 {
903 return unfolded_monomial(20, 12, 10, 5, 4);
904 }
905 };
906
907 class FoldedContractionSmall : public TestRunnable
908 {
909 public:
910 FoldedContractionSmall() : TestRunnable("folded contraction small (r=5, nv=4, dim=3)", 3, 4)
911 {
912 }
913 [[nodiscard]] bool
914 run() const override
915 {
916 return folded_contraction(5, 4, 3);
917 }
918 };
919
920 class FoldedContractionBig : public TestRunnable
921 {
922 public:
923 FoldedContractionBig() : TestRunnable("folded contraction big (r=20, nv=12, dim=5)", 5, 12)
924 {
925 }
926 [[nodiscard]] bool
927 run() const override
928 {
929 return folded_contraction(20, 12, 5);
930 }
931 };
932
933 class UnfoldedContractionSmall : public TestRunnable
934 {
935 public:
936 UnfoldedContractionSmall() : TestRunnable("unfolded contraction small (r=5, nv=4, dim=3)", 3, 4)
937 {
938 }
939 [[nodiscard]] bool
940 run() const override
941 {
942 return unfolded_contraction(5, 4, 3);
943 }
944 };
945
946 class UnfoldedContractionBig : public TestRunnable
947 {
948 public:
949 UnfoldedContractionBig() : TestRunnable("unfolded contraction big (r=20, nv=12, dim=5)", 5, 12)
950 {
951 }
952 [[nodiscard]] bool
953 run() const override
954 {
955 return unfolded_contraction(20, 12, 5);
956 }
957 };
958
959 class PolyEvalSmall : public TestRunnable
960 {
961 public:
962 PolyEvalSmall() : TestRunnable("polynomial evaluation small (r=4, nv=5, maxdim=4)", 4, 5)
963 {
964 }
965 [[nodiscard]] bool
966 run() const override
967 {
968 return poly_eval(4, 5, 4);
969 }
970 };
971
972 class PolyEvalBig : public TestRunnable
973 {
974 public:
975 PolyEvalBig() : TestRunnable("polynomial evaluation big (r=244, nv=97, maxdim=2)", 2, 97)
976 {
977 }
978 [[nodiscard]] bool
979 run() const override
980 {
981 return poly_eval(244, 97, 2);
982 }
983 };
984
985 class FoldZContSmall : public TestRunnable
986 {
987 public:
988 FoldZContSmall() : TestRunnable("folded Z container (r=3,ny=2,nu=2,nup=1,G=2,g=2,dim=3)", 3, 8)
989 {
990 }
991 [[nodiscard]] bool
992 run() const override
993 {
994 return fold_zcont(3, 2, 2, 1, 2, 2, 3);
995 }
996 };
997
998 class FoldZCont : public TestRunnable
999 {
1000 public:
1001 FoldZCont() : TestRunnable("folded Z container (r=13,ny=5,nu=7,nup=4,G=6,g=7,dim=4)", 4, 25)
1002 {
1003 }
1004 [[nodiscard]] bool
1005 run() const override
1006 {
1007 return fold_zcont(13, 5, 7, 4, 6, 7, 4);
1008 }
1009 };
1010
1011 class UnfoldZContSmall : public TestRunnable
1012 {
1013 public:
1014 UnfoldZContSmall() :
1015 TestRunnable("unfolded Z container (r=3,ny=2,nu=2,nup=1,G=2,g=2,dim=3)", 3, 8)
1016 {
1017 }
1018 [[nodiscard]] bool
1019 run() const override
1020 {
1021 return unfold_zcont(3, 2, 2, 1, 2, 2, 3);
1022 }
1023 };
1024
1025 class UnfoldZCont : public TestRunnable
1026 {
1027 public:
1028 UnfoldZCont() : TestRunnable("unfolded Z container (r=13,ny=5,nu=7,nup=4,G=6,g=7,dim=4", 4, 25)
1029 {
1030 }
1031 [[nodiscard]] bool
1032 run() const override
1033 {
1034 return unfold_zcont(13, 5, 7, 4, 6, 7, 4);
1035 }
1036 };
1037
1038 int
1039 main()
1040 {
1041 std::vector<std::unique_ptr<TestRunnable>> all_tests;
1042 // fill in vector of all tests
1043 all_tests.push_back(std::make_unique<SmallIndexForwardFold>());
1044 all_tests.push_back(std::make_unique<SmallIndexForwardUnfold>());
1045 all_tests.push_back(std::make_unique<IndexForwardFold>());
1046 all_tests.push_back(std::make_unique<IndexForwardUnfold>());
1047 all_tests.push_back(std::make_unique<SmallIndexBackwardFold>());
1048 all_tests.push_back(std::make_unique<IndexBackwardFold>());
1049 all_tests.push_back(std::make_unique<SmallIndexBackwardUnfold>());
1050 all_tests.push_back(std::make_unique<IndexBackwardUnfold>());
1051 all_tests.push_back(std::make_unique<SmallIndexOffsetFold>());
1052 all_tests.push_back(std::make_unique<SmallIndexOffsetUnfold>());
1053 all_tests.push_back(std::make_unique<IndexOffsetFold>());
1054 all_tests.push_back(std::make_unique<IndexOffsetUnfold>());
1055 all_tests.push_back(std::make_unique<SmallFoldUnfoldFS>());
1056 all_tests.push_back(std::make_unique<SmallFoldUnfoldGS>());
1057 all_tests.push_back(std::make_unique<FoldUnfoldFS>());
1058 all_tests.push_back(std::make_unique<FoldUnfoldGS>());
1059 all_tests.push_back(std::make_unique<SmallFoldUnfoldR>());
1060 all_tests.push_back(std::make_unique<FoldUnfoldR>());
1061 all_tests.push_back(std::make_unique<SmallDenseProd>());
1062 all_tests.push_back(std::make_unique<DenseProd>());
1063 all_tests.push_back(std::make_unique<BigDenseProd>());
1064 all_tests.push_back(std::make_unique<SmallFoldedMonomial>());
1065 all_tests.push_back(std::make_unique<FoldedMonomial>());
1066 all_tests.push_back(std::make_unique<SmallUnfoldedMonomial>());
1067 all_tests.push_back(std::make_unique<UnfoldedMonomial>());
1068 all_tests.push_back(std::make_unique<FoldedContractionSmall>());
1069 all_tests.push_back(std::make_unique<FoldedContractionBig>());
1070 all_tests.push_back(std::make_unique<UnfoldedContractionSmall>());
1071 all_tests.push_back(std::make_unique<UnfoldedContractionBig>());
1072 all_tests.push_back(std::make_unique<PolyEvalSmall>());
1073 all_tests.push_back(std::make_unique<PolyEvalBig>());
1074 all_tests.push_back(std::make_unique<FoldZContSmall>());
1075 all_tests.push_back(std::make_unique<FoldZCont>());
1076 all_tests.push_back(std::make_unique<UnfoldZContSmall>());
1077 all_tests.push_back(std::make_unique<UnfoldZCont>());
1078
1079 /* Initialize library. We do it for each test individually instead of
1080 computing the maximum dimension and number of variables, because otherwise
1081 it does not pass the check on maximum problem size. */
1082 for (const auto& test : all_tests)
1083 TLStatic::init(test->dim, test->nvar);
1084
1085 thread_pool::initialize();
1086
1087 // Launch the tests
1088 int success = 0;
1089 for (const auto& test : all_tests)
1090 {
1091 try
1092 {
1093 if (test->test())
1094 success++;
1095 }
1096 catch (const TLException& e)
1097 {
1098 std::cout << "Caught TL exception in <" << test->name << ">:\n";
1099 e.print();
1100 }
1101 catch (SylvException& e)
1102 {
1103 std::cout << "Caught Sylv exception in <" << test->name << ">:\n";
1104 e.printMessage();
1105 }
1106 }
1107
1108 int nfailed = all_tests.size() - success;
1109 std::cout << "There were " << nfailed << " tests that failed out of " << all_tests.size()
1110 << " tests run." << '\n';
1111
1112 if (nfailed)
1113 return EXIT_FAILURE;
1114 else
1115 return EXIT_SUCCESS;
1116 }