File: tests.cpp

package info (click to toggle)
dynare 4.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 49,408 kB
  • sloc: cpp: 84,998; ansic: 29,058; pascal: 13,843; sh: 4,833; objc: 4,236; yacc: 3,622; makefile: 2,278; lex: 1,541; python: 236; lisp: 69; xml: 8
file content (1022 lines) | stat: -rw-r--r-- 29,794 bytes parent folder | download | duplicates (5)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
/* $Header: /var/lib/cvs/dynare_cpp/sylv/testing/tests.cpp,v 1.2 2004/07/05 19:55:48 kamenik Exp $ */

/* Tag $Name:  $ */

#include "SylvException.h"
#include "QuasiTriangular.h"
#include "QuasiTriangularZero.h"
#include "Vector.h"
#include "KronVector.h"
#include "KronUtils.h"
#include "TriangularSylvester.h"
#include "GeneralSylvester.h"
#include "SylvMemory.h"
#include "SchurDecompEig.h"
#include "SimilarityDecomp.h"
#include "IterativeSylvester.h"
#include "SylvMatrix.h"

#include "MMMatrix.h"

#include <cstdio>
#include <cstring>
#include <ctime>

#include <cmath>

class TestRunnable : public MallocAllocator {
	char name[100];
	static double eps_norm;
public:
	TestRunnable(const char* n){strncpy(name, n, 100);}
	bool test() const;
	virtual bool run() const =0;
	const char* getName() const {return name;}
protected:
	// declaration of auxiliary static methods
	static bool quasi_solve(bool trans, const char* mname, const char* vname);
	static bool mult_kron(bool trans, const char* mname, const char* vname,
						  const char* cname, int m, int n, int depth);
	static bool level_kron(bool trans, const char* mname, const char* vname,
						   const char* cname, int level, int m, int n, int depth);
	static bool kron_power(const char* m1name, const char* m2name, const char* vname,
						   const char* cname, int m, int n, int depth);
	static bool lin_eval(const char* m1name, const char* m2name, const char* vname,
						 const char* cname, int m, int n, int depth,
						 double alpha, double beta1, double beta2);
	static bool qua_eval(const char* m1name, const char* m2name, const char* vname,
						 const char* cname, int m, int n, int depth,
						 double alpha, double betas, double gamma,
						 double delta1, double delta2);
	static bool tri_sylv(const char* m1name, const char* m2name, const char* vname,
						 int m, int n, int depth);
	static bool gen_sylv(const char* aname, const char* bname, const char* cname,
						 const char* dname, int m, int n, int order);
	static bool eig_bubble(const char* aname, int from, int to);
	static bool block_diag(const char* aname, double log10norm = 3.0);
	static bool iter_sylv(const char* m1name, const char* m2name, const char* vname,
						  int m, int n, int depth);
};

double TestRunnable::eps_norm = 1.0e-10;

bool TestRunnable::test() const
{
	printf("Running test <%s>\n",name);
	clock_t start = clock();
	bool passed = run();
	clock_t end = clock();
	printf("CPU time %8.4g (CPU seconds)..................",
		   ((double)(end-start))/CLOCKS_PER_SEC);
	if (passed) {
		printf("passed\n\n");
		return passed;
	} else {
		printf("FAILED\n\n");
		return passed;
	}
}

/**********************************************************/
/*   auxiliary methods                                    */
/**********************************************************/

bool TestRunnable::quasi_solve(bool trans, const char* mname, const char* vname)
{
	MMMatrixIn mmt(mname);
	MMMatrixIn mmv(vname);

	SylvMemoryDriver memdriver(1, mmt.row(), mmt.row(), 1);
	QuasiTriangular* t;
	QuasiTriangular* tsave;
	if (mmt.row()==mmt.col()) {
		t = new QuasiTriangular(mmt.getData(), mmt.row());
		tsave = new QuasiTriangular(*t);
	} else if (mmt.row()>mmt.col()) {
		t = new QuasiTriangularZero(mmt.row()-mmt.col(), mmt.getData(), mmt.col());
		tsave = new QuasiTriangularZero((const QuasiTriangularZero&)*t);
	} else {
		printf("  Wrong quasi triangular dimensions, rows must be >= cols.\n");
		return false;
	}
	ConstVector v(mmv.getData(), mmv.row());
	Vector x(v.length());
	double eig_min = 1.0e20;
	if (trans)
		t->solveTrans(x, v, eig_min);
	else
		t->solve(x, v, eig_min);
	printf("eig_min = %8.4g\n", eig_min);
	Vector xx(v.length());
	if (trans)
		tsave->multVecTrans(xx, ConstVector(x));
	else
		tsave->multVec(xx, ConstVector(x));
	delete tsave;
	delete t;
	xx.add(-1.0, v);
	xx.add(1.0, x);
	double norm = xx.getNorm();
	printf("\terror norm = %8.4g\n",norm);
	return (norm < eps_norm);
}

bool TestRunnable::mult_kron(bool trans, const char* mname, const char* vname,
							 const char* cname, int m, int n, int depth)
{
	MMMatrixIn mmt(mname);
	MMMatrixIn mmv(vname);
	MMMatrixIn mmc(cname);

	int length = power(m,depth)*n;
	if (mmt.row() != m ||
		mmv.row() != length ||
		mmc.row() != length) {
		printf("  Incompatible sizes for krom mult action, len=%d, matrow=%d, m=%d, vrow=%d, crow=%d \n",length,mmt.row(), m, mmv.row(), mmc.row());
		return false;
	}

	SylvMemoryDriver memdriver(1, m, n, depth);
	QuasiTriangular t(mmt.getData(), mmt.row());
	Vector vraw(mmv.getData(), mmv.row());
	KronVector v(vraw, m, n, depth);
	Vector craw(mmc.getData(), mmc.row());
	KronVector c(craw, m, n, depth);
	if (trans)
		t.multKronTrans(v);
	else
		t.multKron(v);
	c.add(-1.0, v);
	double norm = c.getNorm();
	printf("\terror norm = %8.4g\n",norm);
	return (norm < eps_norm);
}

bool TestRunnable::level_kron(bool trans, const char* mname, const char* vname,
							  const char* cname, int level, int m, int n, int depth)
{
	MMMatrixIn mmt(mname);
	MMMatrixIn mmv(vname);
	MMMatrixIn mmc(cname);

	int length = power(m,depth)*n;
	if (level > 0 && mmt.row() != m ||
		level == 0 && mmt.row() != n ||
		mmv.row() != length ||
		mmc.row() != length) {
		printf("  Incompatible sizes for krom mult action, len=%d, matrow=%d, m=%d, n=%d, vrow=%d, crow=%d \n",length, mmt.row(), m, n, mmv.row(), mmc.row());
		return false;
	}

	SylvMemoryDriver memdriver(1, m, n, depth);
	QuasiTriangular t(mmt.getData(), mmt.row());
	Vector vraw(mmv.getData(), mmv.row());
	ConstKronVector v(vraw, m, n, depth);
	Vector craw(mmc.getData(), mmc.row());
	KronVector c(craw, m, n, depth);
	KronVector x(v);
	if (trans)
		KronUtils::multAtLevelTrans(level, t, x);
	else
		KronUtils::multAtLevel(level, t, x);
	x.add(-1, c);
	double norm = x.getNorm();
	printf("\terror norm = %8.4g\n",norm);
	return (norm < eps_norm);
}

bool TestRunnable::kron_power(const char* m1name, const char* m2name, const char* vname,
							  const char* cname, int m, int n, int depth)
{
	MMMatrixIn mmt1(m1name);
	MMMatrixIn mmt2(m2name);
	MMMatrixIn mmv(vname);
	MMMatrixIn mmc(cname);

	int length = power(m,depth)*n;
	if (mmt1.row() != m ||
		mmt2.row() != n ||
		mmv.row() != length ||
		mmc.row() != length) {
		printf("  Incompatible sizes for krom power mult action, len=%d, row1=%d, row2=%d, m=%d, n=%d, vrow=%d, crow=%d \n",length,mmt1.row(), mmt2.row(), m, n, mmv.row(), mmc.row());
		return false;
	}

	SylvMemoryDriver memdriver(2, m, n, depth);
	QuasiTriangular t1(mmt1.getData(), mmt1.row());
	QuasiTriangular t2(mmt2.getData(), mmt2.row());
	Vector vraw(mmv.getData(), mmv.row());
	ConstKronVector v(vraw, m, n, depth);
	Vector craw(mmc.getData(), mmc.row());
	KronVector c(craw, m, n, depth);
	KronVector x(v);
	memdriver.setStackMode(true);
	KronUtils::multKron(t1, t2, x);
	memdriver.setStackMode(false);
	x.add(-1, c);
	double norm = x.getNorm();
	printf("\terror norm = %8.4g\n",norm);
	return (norm < eps_norm);
}

bool TestRunnable::lin_eval(const char* m1name, const char* m2name, const char* vname,
							const char* cname, int m, int n, int depth,
							double alpha, double beta1, double beta2)
{
	MMMatrixIn mmt1(m1name);
	MMMatrixIn mmt2(m2name);
	MMMatrixIn mmv(vname);
	MMMatrixIn mmc(cname);

	int length = power(m,depth)*n;
	if (mmt1.row() != m ||
		mmt2.row() != n ||
		mmv.row() != 2*length ||
		mmc.row() != 2*length) {
		printf("  Incompatible sizes for lin eval action, len=%d, row1=%d, row2=%d, m=%d, n=%d, vrow=%d, crow=%d \n",length,mmt1.row(), mmt2.row(), m, n, mmv.row(), mmc.row());
		return false;
	}

	SylvMemoryDriver memdriver(1, m, n, depth);
	QuasiTriangular t1(mmt1.getData(), mmt1.row());
	QuasiTriangular t2(mmt2.getData(), mmt2.row());
	TriangularSylvester ts(t2, t1);
	Vector vraw1(mmv.getData(), length);
	ConstKronVector v1(vraw1, m, n, depth);
	Vector vraw2(mmv.getData()+length, length);
	ConstKronVector v2(vraw2, m, n, depth);
	Vector craw1(mmc.getData(), length);
	KronVector c1(craw1, m, n, depth);
	Vector craw2(mmc.getData()+length, length);
	KronVector c2(craw2, m, n, depth);
	KronVector x1(m, n, depth);
	KronVector x2(m, n, depth);
	memdriver.setStackMode(true);
	ts.linEval(alpha, beta1, beta2, x1, x2, v1, v2);
	memdriver.setStackMode(false);
	x1.add(-1, c1);
	x2.add(-1, c2);
	double norm1 = x1.getNorm();
	double norm2 = x2.getNorm();
	printf("\terror norm1 = %8.4g\n\terror norm2 = %8.4g\n",norm1,norm2);
	return (norm1*norm1+norm2*norm2 < eps_norm*eps_norm);
}


bool TestRunnable::qua_eval(const char* m1name, const char* m2name, const char* vname,
							const char* cname, int m, int n, int depth,
							double alpha, double betas, double gamma,
							double delta1, double delta2)
{
	MMMatrixIn mmt1(m1name);
	MMMatrixIn mmt2(m2name);
	MMMatrixIn mmv(vname);
	MMMatrixIn mmc(cname);

	int length = power(m,depth)*n;
	if (mmt1.row() != m ||
		mmt2.row() != n ||
		mmv.row() != 2*length ||
		mmc.row() != 2*length) {
		printf("  Incompatible sizes for qua eval action, len=%d, row1=%d, row2=%d, m=%d, n=%d, vrow=%d, crow=%d \n",length,mmt1.row(), mmt2.row(), m, n, mmv.row(), mmc.row());
		return false;
	}

	SylvMemoryDriver memdriver(3, m, n, depth);
	QuasiTriangular t1(mmt1.getData(), mmt1.row());
	QuasiTriangular t2(mmt2.getData(), mmt2.row());
	TriangularSylvester ts(t2, t1);
	Vector vraw1(mmv.getData(), length);
	ConstKronVector v1(vraw1, m, n, depth);
	Vector vraw2(mmv.getData()+length, length);
	ConstKronVector v2(vraw2, m, n, depth);
	Vector craw1(mmc.getData(), length);
	KronVector c1(craw1, m, n, depth);
	Vector craw2(mmc.getData()+length, length);
	KronVector c2(craw2, m, n, depth);
	KronVector x1(m, n, depth);
	KronVector x2(m, n, depth);
	memdriver.setStackMode(true);
	ts.quaEval(alpha, betas, gamma, delta1, delta2, x1, x2, v1, v2);
	memdriver.setStackMode(false);
	x1.add(-1, c1);
	x2.add(-1, c2);
	double norm1 = x1.getNorm();
	double norm2 = x2.getNorm();
	printf("\terror norm1 = %8.4g\n\terror norm2 = %8.4g\n",norm1,norm2);
	return (norm1*norm1+norm2*norm2 < 100*eps_norm*eps_norm); // relax norm
}

bool TestRunnable::tri_sylv(const char* m1name, const char* m2name, const char* vname,
							int m, int n, int depth)
{
	MMMatrixIn mmt1(m1name);
	MMMatrixIn mmt2(m2name);
	MMMatrixIn mmv(vname);

	int length = power(m,depth)*n;
	if (mmt1.row() != m ||
		mmt2.row() != n ||
		mmv.row() != length) {
		printf("  Incompatible sizes for triangular sylvester action, len=%d, row1=%d, row2=%d, m=%d, n=%d, vrow=%d\n",length,mmt1.row(), mmt2.row(), m, n, mmv.row());
		return false;
	}

	SylvMemoryDriver memdriver(4, m, n, depth); // need extra 2 for checks done via KronUtils::multKron
	memdriver.setStackMode(true);
	QuasiTriangular t1(mmt1.getData(), mmt1.row());
	QuasiTriangular t2(mmt2.getData(), mmt2.row());
	TriangularSylvester ts(t2, t1);
	Vector vraw(mmv.getData(), length);
	ConstKronVector v(vraw, m, n, depth);
	KronVector d(v); // copy of v
	SylvParams pars;
	ts.solve(pars, d);
	pars.print("\t");
	KronVector dcheck((const KronVector&)d);
	KronUtils::multKron(t1, t2, dcheck);
	dcheck.add(1.0, d);
	dcheck.add(-1.0, v);
	double norm = dcheck.getNorm();
	double xnorm = v.getNorm();
	printf("\trel. error norm = %8.4g\n",norm/xnorm);
	double max = dcheck.getMax();
	double xmax = v.getMax();
	printf("\trel. error max = %8.4g\n", max/xmax);
	memdriver.setStackMode(false);
	return (norm < xnorm*eps_norm);
}

bool TestRunnable::gen_sylv(const char* aname, const char* bname, const char* cname,
							const char* dname, int m, int n, int order)
{
	MMMatrixIn mma(aname);
	MMMatrixIn mmb(bname);
	MMMatrixIn mmc(cname);
	MMMatrixIn mmd(dname);

	if (m != mmc.row() || m != mmc.col() ||
		n != mma.row() || n != mma.col() ||
		n != mmb.row() || n <  mmb.col() ||
		n != mmd.row() || power(m, order) != mmd.col()) {
		printf("  Incompatible sizes for gen_sylv.\n");
		return false;
	}

	SylvParams ps(true);
	GeneralSylvester gs(order, n, m, n-mmb.col(),
						mma.getData(), mmb.getData(),
						mmc.getData(), mmd.getData(),
						ps);
	gs.solve();
	gs.check(mmd.getData());
	const SylvParams& pars = gs.getParams();
	pars.print("\t");
	return (*(pars.mat_err1) < eps_norm && *(pars.mat_errI) < eps_norm &&
			*(pars.mat_errF) < eps_norm && *(pars.vec_err1) < eps_norm &&
			*(pars.vec_errI) < eps_norm);
}

bool TestRunnable::eig_bubble(const char* aname, int from, int to)
{
	MMMatrixIn mma(aname);

	if (mma.row() != mma.col()) {
		printf("  Matrix is not square\n");
		return false;
	}

	int n = mma.row();
	SylvMemoryDriver memdriver(3, n, n, 2);
	QuasiTriangular orig(mma.getData(), n);
	SchurDecompEig dec((const QuasiTriangular&)orig);
	QuasiTriangular::diag_iter itf = dec.getT().diag_begin();
	QuasiTriangular::diag_iter itt = dec.getT().diag_begin();
	for (int i = 0; i < from; i++)
		++itf;
	for (int i = 0; i < to; i++)
		++itt;
	itt = dec.bubbleEigen(itf, itt);
	SqSylvMatrix check(dec.getQ(), dec.getT());
	check.multRightTrans(dec.getQ());
	check.add(-1, orig);
	double norm1 = check.getNorm1();
	double normInf = check.getNormInf();
	double onorm1 = orig.getNorm1();
	double onormInf = orig.getNormInf();
	printf("\tabs. error1 = %8.4g\n", norm1);
	printf("\tabs. errorI = %8.4g\n", normInf);
	printf("\trel. error1 = %8.4g\n", norm1/onorm1);
	printf("\trel. errorI = %8.4g\n", normInf/onormInf);
	return (norm1 < eps_norm*onorm1 && normInf < eps_norm*onormInf);
}

bool TestRunnable::block_diag(const char* aname, double log10norm)
{
	MMMatrixIn mma(aname);

	if (mma.row() != mma.col()) {
		printf("  Matrix is not square\n");
		return false;
	}

	int n = mma.row();
	SylvMemoryDriver memdriver(3, n, n, 2);
	SqSylvMatrix orig(mma.getData(), n);
	SimilarityDecomp dec(orig.base(), orig.numRows(), log10norm);
	dec.getB().printInfo();
	SqSylvMatrix check(dec.getQ(), dec.getB());
	check.multRight(dec.getInvQ());
	check.add(-1, orig);
	double norm1 = check.getNorm1();
	double normInf = check.getNormInf();
	double onorm1 = orig.getNorm1();
	double onormInf = orig.getNormInf();
	printf("\terror Q*B*invQ:\n");
	printf("\tabs. error1 = %8.4g\n", norm1);
	printf("\tabs. errorI = %8.4g\n", normInf);
	printf("\trel. error1 = %8.4g\n", norm1/onorm1);
	printf("\trel. errorI = %8.4g\n", normInf/onormInf);
	SqSylvMatrix check2(dec.getQ(), dec.getInvQ());
	SqSylvMatrix in(n);
	in.setUnit();
	check2.add(-1, in);
	double nor1 = check2.getNorm1();
	double norInf = check2.getNormInf();
	printf("\terror Q*invQ:\n");
	printf("\tabs. error1 = %8.4g\n", nor1);
	printf("\tabs. errorI = %8.4g\n", norInf);
	return (norm1 < eps_norm*pow(10, log10norm)*onorm1);
}

bool TestRunnable::iter_sylv(const char* m1name, const char* m2name, const char* vname,
							 int m, int n, int depth)
{
	MMMatrixIn mmt1(m1name);
	MMMatrixIn mmt2(m2name);
	MMMatrixIn mmv(vname);

	int length = power(m,depth)*n;
	if (mmt1.row() != m ||
		mmt2.row() != n ||
		mmv.row() != length) {
		printf("  Incompatible sizes for triangular sylvester iteration, len=%d, row1=%d, row2=%d, m=%d, n=%d, vrow=%d\n",length,mmt1.row(), mmt2.row(), m, n, mmv.row());
		return false;
	}

	SylvMemoryDriver memdriver(4, m, n, depth); // need extra 2 for checks done via KronUtils::multKron
	memdriver.setStackMode(true);
	QuasiTriangular t1(mmt1.getData(), mmt1.row());
	QuasiTriangular t2(mmt2.getData(), mmt2.row());
	IterativeSylvester is(t2, t1);
	Vector vraw(mmv.getData(), length);
	ConstKronVector v(vraw, m, n, depth);
	KronVector d(v); // copy of v
	SylvParams pars;
	pars.method = SylvParams::iter;
	is.solve(pars, d);
	pars.print("\t");
	KronVector dcheck((const KronVector&)d);
	KronUtils::multKron(t1, t2, dcheck);
	dcheck.add(1.0, d);
	dcheck.add(-1.0, v);
	double cnorm = dcheck.getNorm();
	double xnorm = v.getNorm();
	printf("\trel. error norm = %8.4g\n",cnorm/xnorm);
	double max = dcheck.getMax();
	double xmax = v.getMax();
	printf("\trel. error max = %8.4g\n", max/xmax);
	memdriver.setStackMode(false);
	return (cnorm < xnorm*eps_norm);
}

/**********************************************************/
/*   sub classes declarations                             */
/**********************************************************/

class PureTriangTest : public TestRunnable {
public:
	PureTriangTest() : TestRunnable("pure triangular solve (5)") {}
	bool run() const;
};

class PureTriangTransTest : public TestRunnable {
public:
	PureTriangTransTest() : TestRunnable("pure triangular solve trans (5)") {}
	bool run() const;
};

class PureTrLargeTest : public TestRunnable {
public:
	PureTrLargeTest() : TestRunnable("pure triangular large solve (300)") {}
	bool run() const;
};

class PureTrLargeTransTest : public TestRunnable {
public:
	PureTrLargeTransTest() : TestRunnable("pure triangular large solve trans (300)") {}
	bool run() const;
};

class QuasiTriangTest : public TestRunnable {
public:
	QuasiTriangTest() : TestRunnable("quasi triangular solve (7)") {}
	bool run() const;
};

class QuasiTriangTransTest : public TestRunnable {
public:
	QuasiTriangTransTest() : TestRunnable("quasi triangular solve trans (7)") {}
	bool run() const;
};

class QuasiTrLargeTest : public TestRunnable {
public:
	QuasiTrLargeTest() : TestRunnable("quasi triangular solve large (250)") {}
	bool run() const;
};

class QuasiTrLargeTransTest : public TestRunnable {
public:
	QuasiTrLargeTransTest() : TestRunnable("quasi triangular solve large trans (250)") {}
	bool run() const;
};

class QuasiZeroSmallTest : public TestRunnable {
public:
	QuasiZeroSmallTest() : TestRunnable("quasi tr. zero small test (2x1)") {}
	bool run() const;
};

class MultKronSmallTest : public TestRunnable {
public:
	MultKronSmallTest() : TestRunnable("kronecker small mult (2=2x1)") {}
	bool run() const;
};

class MultKronTest : public TestRunnable {
public:
	MultKronTest() : TestRunnable("kronecker mult (245=7x7x5)") {}
	bool run() const;
};

class MultKronSmallTransTest : public TestRunnable {
public:
	MultKronSmallTransTest() : TestRunnable("kronecker small trans mult (2=2x1)") {}
	bool run() const;
};

class MultKronTransTest : public TestRunnable {
public:
	MultKronTransTest() : TestRunnable("kronecker trans mult (245=7x7x5)") {}
	bool run() const;
};

class LevelKronTest : public TestRunnable {
public:
	LevelKronTest() : TestRunnable("kronecker level mult (1715=7x[7]x7x5)") {}
	bool run() const;
};

class LevelKronTransTest : public TestRunnable {
public:
	LevelKronTransTest() : TestRunnable("kronecker level trans mult (1715=7x[7]x7x5)") {}
	bool run() const;
};

class LevelZeroKronTest : public TestRunnable {
public:
	LevelZeroKronTest() : TestRunnable("kronecker level mult (1715=7x7x7x[5])") {}
	bool run() const;
};

class LevelZeroKronTransTest : public TestRunnable {
public:
	LevelZeroKronTransTest() : TestRunnable("kronecker level trans mult (1715=7x7x7x[5])") {}
	bool run() const;
};

class KronPowerTest : public TestRunnable {
public:
	KronPowerTest() : TestRunnable("kronecker power mult (1715=7x7x7x5)") {}
	bool run() const;
};

class SmallLinEvalTest : public TestRunnable {
public:
	SmallLinEvalTest() : TestRunnable("lin eval (24=2 x 2x2x3)") {}
	bool run() const;
};

class LinEvalTest : public TestRunnable {
public:
	LinEvalTest() : TestRunnable("lin eval (490=2 x 7x7x5)") {}
	bool run() const;
};

class SmallQuaEvalTest : public TestRunnable {
public:
	SmallQuaEvalTest() : TestRunnable("qua eval (24=2 x 2x2x3)") {}
	bool run() const;
};

class QuaEvalTest : public TestRunnable {
public:
	QuaEvalTest() : TestRunnable("qua eval (490=2 x 7x7x5)") {}
	bool run() const;
};

class TriSylvSmallRealTest : public TestRunnable {
public:
	TriSylvSmallRealTest() : TestRunnable("triangular sylvester small real solve (12=2x2x3)") {}
	bool run() const;
};

class TriSylvSmallComplexTest : public TestRunnable {
public:
	TriSylvSmallComplexTest() : TestRunnable("triangular sylvester small complx solve (12=2x2x3)") {}
	bool run() const;
};

class TriSylvTest : public TestRunnable {
public:
	TriSylvTest() : TestRunnable("triangular sylvester solve (245=7x7x5)") {}
	bool run() const;
};

class TriSylvBigTest : public TestRunnable {
public:
	TriSylvBigTest() : TestRunnable("triangular sylvester big solve (48000=40x40x30)") {}
	bool run() const;
};

class TriSylvLargeTest : public TestRunnable {
public:
	TriSylvLargeTest() : TestRunnable("triangular sylvester large solve (1920000=40x40x40x30)") {}
	bool run() const;
};

class IterSylvTest : public TestRunnable {
public:
	IterSylvTest() : TestRunnable("iterative sylvester solve (245=7x7x5)") {}
	bool run() const;
};

class IterSylvLargeTest : public TestRunnable {
public:
	IterSylvLargeTest() : TestRunnable("iterative sylvester large solve (1920000=40x40x40x30)") {}
	bool run() const;
};

class GenSylvSmallTest : public TestRunnable {
public:
	GenSylvSmallTest() : TestRunnable("general sylvester small solve (18=3x3x2)") {}
	bool run() const;
};

class GenSylvTest : public TestRunnable {
public:
	GenSylvTest() : TestRunnable("general sylvester solve (12000=20x20x30)") {}
	bool run() const;
};

class GenSylvSingTest : public TestRunnable {
public:
	GenSylvSingTest() : TestRunnable("general sylvester solve for sing. C (2500000=50x50x50x20)") {}
	bool run() const;
};

class GenSylvLargeTest : public TestRunnable {
public:
	GenSylvLargeTest() : TestRunnable("general sylvester solve (2500000=50x50x50x20)") {}
	bool run() const;
};

class EigBubFrankTest : public TestRunnable {
public:
	EigBubFrankTest() : TestRunnable("eig. bubble frank test (12x12)") {}
	bool run() const;
};

class EigBubSplitTest : public TestRunnable {
// complex eigenvalue is split by swapping it with real
public:
	EigBubSplitTest() : TestRunnable("eig. bubble complex split test (3x3)") {}
	bool run() const;
};

class EigBubSameTest : public TestRunnable {
// complex eigenevalue bypasses the same complex eigenvalue
public:
	EigBubSameTest() : TestRunnable("eig. bubble same test (5x5)") {}
	bool run() const;
};

class BlockDiagSmallTest : public TestRunnable {
public:
	BlockDiagSmallTest() : TestRunnable("block diagonalization small test (7x7)") {}
	bool run() const;
};

class BlockDiagFrankTest : public TestRunnable {
public:
	BlockDiagFrankTest() : TestRunnable("block diagonalization of frank (12x12)") {}
	bool run() const;
};

class BlockDiagIllCondTest : public TestRunnable {
public:
	BlockDiagIllCondTest() : TestRunnable("block diagonalization of ill conditioned (15x15)") {}
	bool run() const;
};

class BlockDiagBigTest : public TestRunnable {
public:
	BlockDiagBigTest() : TestRunnable("block diagonalization big test (50x50)") {}
	bool run() const;
};

/**********************************************************/
/*   run methods of sub classes                           */
/**********************************************************/

bool PureTriangTest::run() const
{
	return quasi_solve(false, "tr5x5.mm", "v5.mm");
}

bool PureTriangTransTest::run() const
{
	return quasi_solve(true, "tr5x5.mm", "v5.mm");
}

bool PureTrLargeTest::run() const
{
	return quasi_solve(false, "tr300x300.mm", "v300.mm");
}

bool PureTrLargeTransTest::run() const
{
	return quasi_solve(true, "tr300x300.mm", "v300.mm");
}

bool QuasiTriangTest::run() const
{
	return quasi_solve(false, "qt7x7.mm", "v7.mm");
}

bool QuasiTriangTransTest::run() const
{
	return quasi_solve(true, "qt7x7.mm", "v7.mm");
}

bool QuasiTrLargeTest::run() const
{
	return quasi_solve(false, "qt250x250.mm", "v250.mm");
}

bool QuasiTrLargeTransTest::run() const
{
	return quasi_solve(true, "qt250x250.mm", "v250.mm");
}

bool QuasiZeroSmallTest::run() const
{
	return quasi_solve(false, "b2x1.mm", "v2.mm");
}

bool MultKronSmallTest::run() const
{
	return mult_kron(false, "tr2x2.mm", "v2.mm", "vcheck2.mm", 2, 1, 1);
}

bool MultKronTest::run() const
{
	return mult_kron(false, "qt7x7.mm", "v245.mm", "vcheck245.mm", 7, 5, 2);
}

bool MultKronSmallTransTest::run() const
{
	return mult_kron(true, "tr2x2.mm", "v2.mm", "vcheck2a.mm", 2, 1, 1);
}

bool MultKronTransTest::run() const
{
	return mult_kron(true, "qt7x7.mm", "v245.mm", "vcheck245a.mm", 7, 5, 2);
}

bool LevelKronTest::run() const
{
	return level_kron(false, "qt7x7.mm", "v1715.mm", "vcheck1715.mm", 2, 7, 5, 3);
}

bool LevelKronTransTest::run() const
{
	return level_kron(true, "qt7x7.mm", "v1715.mm", "vcheck1715a.mm", 2, 7, 5, 3);
}

bool LevelZeroKronTest::run() const
{
	return level_kron(false, "tr5x5.mm", "v1715.mm", "vcheck1715b.mm", 0, 7, 5, 3);
}

bool LevelZeroKronTransTest::run() const
{
	return level_kron(true, "tr5x5.mm", "v1715.mm", "vcheck1715c.mm", 0, 7, 5, 3);
}

bool KronPowerTest::run() const
{
	return kron_power("qt7x7.mm", "tr5x5.mm", "v1715.mm", "vcheck1715d.mm", 7, 5, 3);
}

bool SmallLinEvalTest::run() const
{
	return lin_eval("qt2x2.mm", "qt3x3.mm", "v24.mm", "vcheck24.mm", 2, 3, 2,
					 2, 1, 3);
}

bool LinEvalTest::run() const
{
	return lin_eval("qt7x7.mm", "tr5x5.mm", "v490.mm", "vcheck490.mm", 7, 5, 2,
					 2, 1, 3);
}

bool SmallQuaEvalTest::run() const
{
	return qua_eval("qt2x2.mm", "qt3x3.mm", "v24.mm", "vcheck24q.mm", 2, 3, 2,
					 -0.5, 3, 2, 1, 3);
}

bool QuaEvalTest::run() const
{
	return qua_eval("qt7x7.mm", "tr5x5.mm", "v490.mm", "vcheck490q.mm", 7, 5, 2,
					 -0.5, 3, 2, 1, 3);
}

bool TriSylvSmallRealTest::run() const
{
	return tri_sylv("tr2x2.mm", "qt3x3.mm", "v12r.mm", 2, 3, 2);
}

bool TriSylvSmallComplexTest::run() const
{
	return tri_sylv("qt2x2.mm", "qt3x3.mm", "v12r.mm", 2, 3, 2);
}

bool TriSylvTest::run() const
{
	return tri_sylv("qt7x7eig06-09.mm", "tr5x5.mm", "v245r.mm", 7, 5, 2);
}

bool TriSylvBigTest::run() const
{
	return tri_sylv("qt40x40.mm", "qt30x30eig011-095.mm", "v48000.mm", 40, 30, 2);
}

bool TriSylvLargeTest::run() const
{
	return tri_sylv("qt40x40.mm", "qt30x30eig011-095.mm", "v1920000.mm", 40, 30, 3);
}

bool IterSylvTest::run() const
{
	return iter_sylv("qt7x7eig06-09.mm", "qt5x5.mm", "v245r.mm", 7, 5, 2);
}

bool IterSylvLargeTest::run() const
{
	return iter_sylv("qt40x40.mm", "qt30x30eig011-095.mm", "v1920000.mm", 40, 30, 3);
}

bool GenSylvSmallTest::run() const
{
	return gen_sylv("a2x2.mm", "b2x1.mm", "c3x3.mm", "d2x9.mm", 3, 2, 2);
}

bool GenSylvTest::run() const
{
	return gen_sylv("a30x30.mm", "b30x25.mm", "c20x20.mm", "d30x400.mm", 20, 30, 2);
}

bool GenSylvSingTest::run() const
{
	return gen_sylv("a20x20.mm", "b20x4.mm", "c50x50sing.mm", "d20x125000.mm", 50, 20, 3);
}

bool GenSylvLargeTest::run() const
{
	return gen_sylv("a20x20.mm", "b20x15.mm", "c50x50.mm", "d20x125000.mm", 50, 20, 3);
}

bool EigBubFrankTest::run() const
{
	return eig_bubble("qt_frank12x12.mm", 8, 0);
}

bool EigBubSplitTest::run() const
{
	return eig_bubble("qt_eps3x3.mm",1,0);
}

bool EigBubSameTest::run() const
{
	return eig_bubble("qt5x5.mm",2,0);
}

bool BlockDiagSmallTest::run() const
{
	return block_diag("qt7x7.mm", 0.1);
}

bool BlockDiagFrankTest::run() const
{
	return block_diag("qt_frank12x12.mm", 5);
}

bool BlockDiagIllCondTest::run() const
{
	return block_diag("ill_cond15x15.mm", 4.14);
}

bool BlockDiagBigTest::run() const
{
	return block_diag("c50x50.mm", 1.3);
}

/**********************************************************/
/*   main                                                 */
/**********************************************************/

int main()
{
	TestRunnable* all_tests[50];
	// fill in vector of all tests
	int num_tests = 0;
	all_tests[num_tests++] = new PureTriangTest();
	all_tests[num_tests++] = new PureTriangTransTest();
	all_tests[num_tests++] = new PureTrLargeTest();
	all_tests[num_tests++] = new PureTrLargeTransTest();
	all_tests[num_tests++] = new QuasiTriangTest();
	all_tests[num_tests++] = new QuasiTriangTransTest();
	all_tests[num_tests++] = new QuasiTrLargeTest();
	all_tests[num_tests++] = new QuasiTrLargeTransTest();
	all_tests[num_tests++] = new QuasiZeroSmallTest();
	all_tests[num_tests++] = new MultKronSmallTest();
	all_tests[num_tests++] = new MultKronTest();
	all_tests[num_tests++] = new MultKronSmallTransTest();
	all_tests[num_tests++] = new MultKronTransTest();
	all_tests[num_tests++] = new LevelKronTest();
	all_tests[num_tests++] = new LevelKronTransTest();
	all_tests[num_tests++] = new LevelZeroKronTest();
	all_tests[num_tests++] = new LevelZeroKronTransTest();
	all_tests[num_tests++] = new KronPowerTest();
	all_tests[num_tests++] = new SmallLinEvalTest();
	all_tests[num_tests++] = new LinEvalTest();
	all_tests[num_tests++] = new SmallQuaEvalTest();
	all_tests[num_tests++] = new QuaEvalTest();
	all_tests[num_tests++] = new EigBubFrankTest();
	all_tests[num_tests++] = new EigBubSplitTest();
	all_tests[num_tests++] = new EigBubSameTest();
	all_tests[num_tests++] = new BlockDiagSmallTest();
	all_tests[num_tests++] = new BlockDiagFrankTest();
	all_tests[num_tests++] = new BlockDiagIllCondTest();
	all_tests[num_tests++] = new BlockDiagBigTest();
	all_tests[num_tests++] = new TriSylvSmallRealTest();
	all_tests[num_tests++] = new TriSylvSmallComplexTest();
	all_tests[num_tests++] = new TriSylvTest();
	all_tests[num_tests++] = new TriSylvBigTest();
	all_tests[num_tests++] = new TriSylvLargeTest();
	all_tests[num_tests++] = new IterSylvTest();
	all_tests[num_tests++] = new IterSylvLargeTest();
	all_tests[num_tests++] = new GenSylvSmallTest();
	all_tests[num_tests++] = new GenSylvTest();
	all_tests[num_tests++] = new GenSylvSingTest();
	all_tests[num_tests++] = new GenSylvLargeTest();

	// launch the tests
	int success = 0;
	for (int i = 0; i < num_tests; i++) {
		try {
			if (all_tests[i]->test())
				success++;
		} catch (const MMException& e) {
			printf("Caugth MM exception in <%s>:\n%s", all_tests[i]->getName(),
				   e.getMessage());
		} catch (SylvException& e) {
			printf("Caught Sylv exception in %s:\n", all_tests[i]->getName());
			e.printMessage();
		}
	}

	printf("There were %d tests that failed out of %d tests run.\n",
		   num_tests - success, num_tests);

	// destroy
	for (int i = 0; i < num_tests; i++) {
		delete all_tests[i];
	}

	return 0;
}