test_GSymMatrix.cpp
25.6 KB
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
/***************************************************************************
* test_GSymMatrix.cpp - test GSymMatrix class *
* ----------------------------------------------------------------------- *
* copyright : (C) 2006 by Jurgen Knodlseder *
* ----------------------------------------------------------------------- *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* __ Includes ___________________________________________________________ */
#include "test_GSymMatrix.hpp"
/* __ Namespaces _________________________________________________________ */
using namespace std;
/* __ Globals ____________________________________________________________ */
double g_matrix[] = {4.0, 1.0, 2.0, 1.0, 5.0, 3.0, 2.0, 3.0, 6.0};
double g_vector[] = {1.0, 2.0, 3.0};
unsigned g_rows = 3;
unsigned g_cols = 3;
/***************************************************************************
* Set test matrix *
***************************************************************************/
GSymMatrix set_matrix(void)
{
try {
GSymMatrix matrix(g_rows,g_cols);
for (unsigned row = 0; row < g_rows; ++row) {
for (unsigned col = 0; col < g_cols; ++col)
matrix(row,col) = g_matrix[col+row*g_cols];
}
return matrix;
}
catch (exception &e) {
cout << "TEST ERROR: Unable to set test matrix." << endl;
throw;
}
}
/***************************************************************************
* Set test matrix with zero lines *
***************************************************************************/
GSymMatrix set_matrix_zero(void)
{
try {
GSymMatrix matrix(g_rows+1,g_cols+1);
unsigned i = 0;
for (unsigned row = 0; row < g_rows+1; ++row) {
if (row == 2) continue;
unsigned j = 0;
for (unsigned col = 0; col < g_cols+1; ++col) {
if (col == 2) continue;
matrix(row,col) = g_matrix[j+i*g_cols];
j++;
}
i++;
}
return matrix;
}
catch (exception &e) {
cout << "TEST ERROR: Unable to set test matrix." << endl;
throw;
}
}
/***************************************************************************
* Set test vector *
***************************************************************************/
GVector set_vector(void)
{
try {
GVector vector(g_cols);
for (unsigned col = 0; col < g_cols; ++col)
vector(col) = g_vector[col];
return vector;
}
catch (exception &e) {
cout << "TEST ERROR: Unable to set test vector." << endl;
throw;
}
}
/***************************************************************************
* Check test matrix *
***************************************************************************/
int check_matrix(const GSymMatrix& m, const double scale, const double add)
{
int result = 1;
try {
for (unsigned row = 0; row < g_rows; ++row) {
for (unsigned col = 0; col < g_cols; ++col) {
double value = g_matrix[col+row*g_cols] * scale + add;
if (fabs(m(row,col)-value) > 1.0e-15) {
result = 0;
break;
}
}
}
}
catch (exception &e) {
cout << "TEST ERROR: Unable to check test matrix." << endl;
throw;
}
return result;
}
/***************************************************************************
* Check full test matrix *
***************************************************************************/
int check_matrix(const GMatrix& m, const double scale, const double add)
{
int result = 1;
try {
for (unsigned row = 0; row < g_rows; ++row) {
for (unsigned col = 0; col < g_cols; ++col) {
double value = g_matrix[col+row*g_cols] * scale + add;
if (m(row,col) != value) {
result = 0;
break;
}
}
}
}
catch (exception &e) {
cout << "TEST ERROR: Unable to check test matrix." << endl;
throw;
}
return result;
}
/***************************************************************************
* Check full test matrix (lower triangle) *
***************************************************************************/
int check_matrix_lt(const GMatrix& m, const double scale, const double add)
{
int result = 1;
try {
for (unsigned row = 0; row < g_rows; ++row) {
for (unsigned col = 0; col < g_cols; ++col) {
double value = (col <= row) ? g_matrix[col+row*g_cols] * scale + add : 0.0;
if (m(row,col) != value) {
result = 0;
break;
}
}
}
}
catch (exception &e) {
cout << "TEST ERROR: Unable to check test matrix." << endl;
throw;
}
return result;
}
/***************************************************************************
* Check full test matrix (upper triangle) *
***************************************************************************/
int check_matrix_ut(const GMatrix& m, const double scale, const double add)
{
int result = 1;
try {
for (unsigned row = 0; row < g_rows; ++row) {
for (unsigned col = 0; col < g_cols; ++col) {
double value = (col >= row) ? g_matrix[col+row*g_cols] * scale + add : 0.0;
if (m(row,col) != value) {
result = 0;
break;
}
}
}
}
catch (exception &e) {
cout << "TEST ERROR: Unable to check test matrix." << endl;
throw;
}
return result;
}
/***************************************************************************
* Check matrix*vector *
***************************************************************************/
int check_matrix_vector(const GVector& v)
{
int result = 1;
try {
for (unsigned row = 0; row < v.size(); ++row) {
double value = 0.0;
for (unsigned col = 0; col < g_cols; ++col)
value += g_matrix[col+row*g_cols] * g_vector[col];
if (v(row) != value) {
result = 0;
break;
}
}
}
catch (exception &e) {
cout << "TEST ERROR: Unable to check matrix*vector product." << endl;
throw;
}
return result;
}
/***************************************************************************
* Check matrix*matrix *
***************************************************************************/
int check_matrix_matrix(const GSymMatrix& m)
{
if (m.rows() != g_rows || m.cols() != g_rows)
return 0;
int result = 1;
try {
for (unsigned row = 0; row < m.rows(); ++row) {
for (unsigned col = 0; col < m.cols(); ++col) {
double value = 0.0;
for (unsigned i = 0; i < g_cols; ++i)
value += g_matrix[i+row*g_cols] * g_matrix[i+col*g_cols];
if (m(row,col) != value) {
result = 0;
break;
}
}
}
}
catch (exception &e) {
cout << "TEST ERROR: Unable to check matrix*matrix product." << endl;
throw;
}
return result;
}
/***************************************************************************
* Check matrix min *
***************************************************************************/
int check_matrix_min(const double min)
{
double value = g_matrix[0];
for (unsigned row = 0; row < g_rows; ++row) {
for (unsigned col = 0; col < g_cols; ++col) {
if (g_matrix[col+row*g_cols] < value)
value = g_matrix[col+row*g_cols];
}
}
return (min == value);
}
/***************************************************************************
* Check matrix max *
***************************************************************************/
int check_matrix_max(const double max)
{
double value = g_matrix[0];
for (unsigned row = 0; row < g_rows; ++row) {
for (unsigned col = 0; col < g_cols; ++col) {
if (g_matrix[col+row*g_cols] > value)
value = g_matrix[col+row*g_cols];
}
}
return (max == value);
}
/***************************************************************************
* Check matrix sum *
***************************************************************************/
int check_matrix_sum(const double sum)
{
double value = 0.0;
for (unsigned row = 0; row < g_rows; ++row) {
for (unsigned col = 0; col < g_cols; ++col)
value += g_matrix[col+row*g_cols];
}
return (sum == value);
}
/***************************************************************************
* Main test function *
***************************************************************************/
int main(void)
{
// Dump header
cout << endl;
cout << "****************************" << endl;
cout << "* GSymMatrix class testing *" << endl;
cout << "****************************" << endl;
// Set test matrix and vector
GSymMatrix m_test = set_matrix();
GVector v_test = set_vector();
// Set bigger matrix (only used for collision, don't care about content)
GSymMatrix bigger(g_rows+1,g_cols+1);
// Prepare result matrix
GSymMatrix result = m_test;
// Test 1: Allocate zero matrix
try {
cout << "GSymMatrix - Test 1: Allocate zero matrix: ";
GSymMatrix test1(0,0);
}
catch (empty &e) {
cout << "ok." << endl;
}
catch (exception &e) {
cout << endl << "TEST ERROR: Did not signal zero matrix allocation." << endl;
cout << e.what() << endl;
throw;
}
// Test 2: Allocate too large matrix
try {
cout << "GSymMatrix - Test 2: Allocate too large matrix: ";
GSymMatrix test2(100000000,100000000);
}
catch (bad_alloc &e) {
cout << "ok." << endl;
}
catch (exception &e) {
cout << endl << "TEST ERROR: Did not signal out of memory condition." << endl;
cout << e.what() << endl;
throw;
}
// Test 3: Assign values
try {
cout << "GSymMatrix - Test 3: Assign matrix values: ";
//
GSymMatrix test3(3,3);
test3(1,1) = 1.0;
if (test3(0,0) != 0.0 || test3(1,0) != 0.0 || test3(2,0) != 0.0 ||
test3(0,1) != 0.0 || test3(1,1) != 1.0 || test3(2,1) != 0.0 ||
test3(0,2) != 0.0 || test3(1,2) != 0.0 || test3(2,2) != 0.0) {
cout << endl << "TEST ERROR: Unable to set matrix value." << endl;
cout << test3 << endl;
throw;
}
}
catch (exception &e) {
cout << e.what() << endl;
throw;
}
#if defined(G_RANGE_CHECK)
try {
GSymMatrix test3(3,3);
test3(3,3) = 1.0;
}
catch (GMatrix::out_of_range &e) {
}
catch (exception &e) {
cout << e.what() << endl;
throw;
}
#endif
cout << "ok." << endl;
// Test 4: Matrix copy constructor
try {
cout << "GSymMatrix - Test 4: Define matrix using copy constructor: ";
//
GSymMatrix test4 = m_test;
if (!check_matrix(test4, 1.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt copy constructor." << endl;
cout << test4 << endl;
throw;
}
}
catch (exception &e) {
cout << e.what() << endl;
throw;
}
cout << "ok." << endl;
// Test 5: Matrix assignment
try {
cout << "GSymMatrix - Test 5: Matrix assignment: ";
//
// GSymMatrix = GSymMatrix
result = m_test;
if (!check_matrix(result, 1.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt assignment." << endl;
cout << result << endl;
throw;
}
//
// GSymMatrix = GSymMatrix (bigger matrix)
result = bigger;
}
catch (exception &e) {
cout << e.what() << endl;
throw;
}
cout << "ok." << endl;
// Test 6: Matrix*Vector multiplication
try {
cout << "GSymMatrix - Test 6: Matrix*Vector multiplication: ";
//
GVector v_test6 = m_test*v_test;
if (!check_matrix_vector(v_test6) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt Matrix*Vector multiplication." << endl;
cout << v_test6 << endl;
throw;
}
}
catch (exception &e) {
cout << e.what() << endl;
throw;
}
try {
GVector v_test6 = bigger*v_test;
}
catch (GMatrix::matrix_vector_mismatch &e) {
}
catch (exception &e) {
cout << e.what() << endl;
throw;
}
cout << "ok." << endl;
// Test 7: Matrix*Matrix multiplication
try {
cout << "GSymMatrix - Test 7: Matrix*Matrix multiplication: ";
//
GSymMatrix m_test7 = m_test*m_test;
if (!check_matrix_matrix(m_test7) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt Matrix*Matrix multiplication." << endl;
cout << m_test7 << endl;
throw;
}
}
catch (exception &e) {
cout << e.what() << endl;
throw;
}
try {
GSymMatrix m_test7 = m_test*bigger;
}
catch (GMatrix::matrix_mismatch &e) {
}
catch (exception &e) {
cout << e.what() << endl;
throw;
}
try {
GSymMatrix m_test7 = bigger*m_test;
}
catch (GMatrix::matrix_mismatch &e) {
}
catch (exception &e) {
cout << e.what() << endl;
throw;
}
cout << "ok." << endl;
// Test 8: Assignment and arithmetics
try {
cout << "GSymMatrix - Test 8: Assignment and arithmetics: ";
//
// -GSymMatrix
result = -m_test;
if (!check_matrix(result, -1.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt -GSymMatrix operator." << endl;
cout << result << endl;
throw;
}
//
// GSymMatrix += GSymMatrix
result = m_test;
result += m_test;
if (!check_matrix(result, 2.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix += GSymMatrix operator." << endl;
cout << result << endl;
throw;
}
//
// GSymMatrix -= GSymMatrix
result = m_test;
result -= m_test;
if (!check_matrix(result, 0.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix -= GSymMatrix operator." << endl;
cout << result << endl;
throw;
}
//
// GSymMatrix *= 3.0
result = m_test;
result *= 3.0;
if (!check_matrix(result, 3.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix *= double operator." << endl;
cout << result << endl;
throw;
}
//
// GSymMatrix /= 3.0
result = m_test;
result /= 3.0;
if (!check_matrix(result, 1.0/3.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix /= double operator." << endl;
cout << result << endl;
throw;
}
//
// GSymMatrix + GSymMatrix
result = m_test + m_test;
if (!check_matrix(result, 2.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix + GSymMatrix operator." << endl;
cout << result << endl;
throw;
}
//
// GSymMatrix - GSymMatrix
result = m_test - m_test;
if (!check_matrix(result, 0.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix - GSymMatrix operator." << endl;
cout << result << endl;
throw;
}
//
// GSymMatrix - GSymMatrix
result = m_test - m_test;
if (!check_matrix(result, 0.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix - GSymMatrix operator." << endl;
cout << result << endl;
throw;
}
//
// GSymMatrix * 3.0
result = m_test * 3.0;
if (!check_matrix(result, 3.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix * double operator." << endl;
cout << result << endl;
throw;
}
//
// 3.0 * GSymMatrix
result = 3.0 * m_test;
if (!check_matrix(result, 3.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt double * GSymMatrix operator." << endl;
cout << result << endl;
throw;
}
//
// GSymMatrix / 3.0
result = m_test / 3.0;
if (!check_matrix(result, 1.0/3.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix / double operator." << endl;
cout << result << endl;
throw;
}
}
catch (exception &e) {
cout << e.what() << endl;
throw;
}
try {
result = m_test;
result += bigger;
}
catch (GMatrix::matrix_mismatch) {
}
catch (exception &e) {
cout << e.what() << endl;
throw;
}
cout << "ok." << endl;
// Test 9: Matrix functions
try {
cout << "GSymMatrix - Test 9: Matrix functions: ";
//
double min = m_test.min();
if (!check_matrix_min(min)) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix.min() function." << endl;
cout << min << endl;
throw;
}
//
double max = m_test.max();
if (!check_matrix_max(max)) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix.max() function." << endl;
cout << min << endl;
throw;
}
//
double sum = m_test.sum();
if (!check_matrix_sum(sum)) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix.sum() function." << endl;
cout << sum << endl;
throw;
}
}
catch (exception &e) {
cout << e.what() << endl;
}
cout << "ok." << endl;
// Test 10: Matrix comparison
try {
cout << "GSymMatrix - Test 10: Comparison: ";
//
if ((m_test == m_test) != 1) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix == GSymMatrix operator." << endl;
throw;
}
//
GSymMatrix m_test10(g_rows,g_cols);
if ((m_test == m_test10) != 0) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix == GSymMatrix operator." << endl;
throw;
}
//
if ((m_test == bigger) != 0) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix == GSymMatrix operator." << endl;
throw;
}
//
if ((m_test != m_test) != 0) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix != GSymMatrix operator." << endl;
throw;
}
//
if ((m_test != m_test10) != 1) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix != GSymMatrix operator." << endl;
throw;
}
//
if ((m_test != bigger) != 1) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix != GSymMatrix operator." << endl;
throw;
}
}
catch (exception &e) {
cout << e.what() << endl;
throw;
}
cout << "ok." << endl;
// Test 11: Transformations
try {
cout << "GSymMatrix - Test 11: Transformations: ";
//
// transpose(GSymMatrix)
result = transpose(m_test);
if (!check_matrix(result, 1.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt transpose(GSymMatrix) function." << endl;
cout << result << endl;
throw;
}
//
// GSymMatrix.transpose()
result = m_test;
result.transpose();
if (!check_matrix(result, 1.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix.transpose() function." << endl;
cout << result << endl;
throw;
}
//
// convert_to_full()
GMatrix m_test11 = m_test.convert_to_full();
if (!check_matrix(m_test11, 1.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt convert_to_full() operator." << endl;
cout << m_test11 << endl;
throw;
}
//
// extract_lower_triangle()
m_test11 = m_test.extract_lower_triangle();
if (!check_matrix_lt(m_test11, 1.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt extract_lower_triangle() function." << endl;
cout << m_test11 << endl;
throw;
}
//
// extract_upper_triangle()
m_test11 = m_test.extract_upper_triangle();
if (!check_matrix_ut(m_test11, 1.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) {
cout << endl << "TEST ERROR: Corrupt extract_upper_triangle() function." << endl;
cout << m_test11 << endl;
throw;
}
}
catch (exception &e) {
cout << e.what() << endl;
throw;
}
cout << "ok." << endl;
// Test 12: Perform Cholesky decomposition
try {
cout << "GSymMatrix - Test 12: Cholesky decomposition, solver and inverter: ";
//
// Test Cholesky decomposition
GSymMatrix cd = cholesky_decompose(m_test);
GMatrix cd_lower = cd.extract_lower_triangle();
GMatrix cd_upper = transpose(cd_lower);
GMatrix cd_product = cd_lower * cd_upper;
GMatrix cd_residuals = m_test.convert_to_full() - cd_product;
//
double res = (fabs(cd_residuals)).max();
if (res < 1.0e-15)
cout << "Res(CD)=" << res << " ";
else {
cout << endl << "TEST ERROR: Corrupt cholesky_decompose(GSymMatrix) function." << endl;
cout << "Residual matrix (all elements should be zero):" << endl;
cout << cd_residuals << endl;
throw;
}
//
// Test compressed Cholesky decomposition
GSymMatrix m_test_zero = set_matrix_zero();
GSymMatrix cd_zero = cholesky_decompose(m_test_zero);
GMatrix cd_zero_lower = cd_zero.extract_lower_triangle();
GMatrix cd_zero_upper = transpose(cd_zero_lower);
GMatrix cd_zero_product = cd_zero_lower * cd_zero_upper;
GMatrix cd_zero_residuals = m_test_zero.convert_to_full() - cd_zero_product;
//
res = (fabs(cd_zero_residuals)).max();
if (res < 1.0e-15)
cout << "Res(CDZ)=" << res << " ";
else {
cout << endl << "TEST ERROR: Corrupt compressed cholesky_decompose(GSymMatrix) function." << endl;
cout << "Residual matrix (all elements should be zero):" << endl;
cout << cd_zero_residuals << endl;
throw;
}
//
// Test Cholesky inplace decomposition
GSymMatrix m_test12 = m_test;
m_test12.cholesky_decompose();
GMatrix cd_lower2 = m_test12.extract_lower_triangle();
if (cd_lower2 != cd_lower) {
cout << endl << "TEST ERROR: Corrupt GSymMatrix.cholesky_decompose() function." << endl;
cout << cd_lower2 << endl;
throw;
}
//
// Test Cholesky solver
GVector e0(g_rows);
GVector a0(g_rows);
e0(0) = 1.0;
e0(1) = 0.0;
e0(2) = 0.0;
a0(0) = g_matrix[0];
a0(1) = g_matrix[3];
a0(2) = g_matrix[6];
GVector s0 = cd.cholesky_solver(a0) - e0;
res = max(fabs(s0));
if (res < 1.0e-15)
cout << "Res(S0)=" << res << " ";
else {
cout << endl << "TEST ERROR: Corrupt cholesky_solver(GVector) function." << endl;
cout << "Residual vector (all elements should be zero):" << endl;
cout << s0 << endl;
throw;
}
//
e0(0) = 0.0;
e0(1) = 1.0;
e0(2) = 0.0;
a0(0) = g_matrix[1];
a0(1) = g_matrix[4];
a0(2) = g_matrix[7];
s0 = cd.cholesky_solver(a0) - e0;
res = max(fabs(s0));
if (res < 1.0e-15)
cout << "Res(S1)=" << res << " ";
else {
cout << endl << "TEST ERROR: Corrupt cholesky_solver(GVector) function." << endl;
cout << "Residual vector (all elements should be zero):" << endl;
cout << s0 << endl;
throw;
}
//
e0(0) = 0.0;
e0(1) = 0.0;
e0(2) = 1.0;
a0(0) = g_matrix[2];
a0(1) = g_matrix[5];
a0(2) = g_matrix[8];
s0 = cd.cholesky_solver(a0) - e0;
res = max(fabs(s0));
if (res < 1.0e-15)
cout << "Res(S2)=" << res << " ";
else {
cout << endl << "TEST ERROR: Corrupt cholesky_solver(GVector) function." << endl;
cout << "Residual vector (all elements should be zero):" << endl;
cout << s0 << endl;
throw;
}
//
// Test compressed Cholesky solver
e0 = GVector(g_rows+1);
a0 = GVector(g_rows+1);
e0(0) = 1.0;
e0(1) = 0.0;
e0(2) = 0.0;
e0(3) = 0.0;
a0(0) = g_matrix[0];
a0(1) = g_matrix[3];
a0(2) = 0.0;
a0(3) = g_matrix[6];
s0 = cd_zero.cholesky_solver(a0) - e0;
res = max(fabs(s0));
if (res < 1.0e-15)
cout << "Res(S0Z)=" << res << " ";
else {
cout << endl << "TEST ERROR: Corrupt compressed cholesky_solver(GVector) function." << endl;
cout << "Residual vector (all elements should be zero):" << endl;
cout << s0 << endl;
throw;
}
//
e0(0) = 0.0;
e0(1) = 1.0;
e0(2) = 0.0;
e0(3) = 0.0;
a0(0) = g_matrix[1];
a0(1) = g_matrix[4];
a0(2) = 0.0;
a0(3) = g_matrix[7];
s0 = cd_zero.cholesky_solver(a0) - e0;
res = max(fabs(s0));
if (res < 1.0e-15)
cout << "Res(S1Z)=" << res << " ";
else {
cout << endl << "TEST ERROR: Corrupt compressed cholesky_solver(GVector) function." << endl;
cout << "Residual vector (all elements should be zero):" << endl;
cout << s0 << endl;
throw;
}
//
e0(0) = 0.0;
e0(1) = 0.0;
e0(2) = 0.0;
e0(3) = 1.0;
a0(0) = g_matrix[2];
a0(1) = g_matrix[5];
a0(2) = 0.0;
a0(3) = g_matrix[8];
s0 = cd_zero.cholesky_solver(a0) - e0;
res = max(fabs(s0));
if (res < 1.0e-15)
cout << "Res(S2Z)=" << res << " ";
else {
cout << endl << "TEST ERROR: Corrupt compressed cholesky_solver(GVector) function." << endl;
cout << "Residual vector (all elements should be zero):" << endl;
cout << s0 << endl;
throw;
}
//
// Test Cholesky inverter
GSymMatrix unit(g_rows,g_cols);
unit(0,0) = unit(1,1) = unit(2,2) = 1.0;
GSymMatrix m_test12_inv = m_test;
m_test12_inv.cholesky_invert();
GSymMatrix ci_product = m_test * m_test12_inv;
GSymMatrix ci_residuals = ci_product - unit;
//
res = (fabs(ci_residuals)).max();
if (res < 1.0e-15)
cout << "Res(CI)=" << res << " ";
else {
cout << endl << "TEST ERROR: Corrupt GSymMatrix.cholesky_invert() function." << endl;
cout << "Residual matrix (all elements should be zero):" << endl;
cout << ci_residuals << endl;
throw;
}
//
// Test Cholesky inverter for compressed matrix
unit = GSymMatrix(4,4);
unit(0,0) = unit(1,1) = unit(3,3) = 1.0;
GSymMatrix m_test12_zero_inv = m_test_zero;
m_test12_zero_inv.cholesky_invert();
GSymMatrix ciz_product = m_test_zero * m_test12_zero_inv;
GSymMatrix ciz_residuals = ciz_product - unit;
//
res = (fabs(ciz_residuals)).max();
if (res < 1.0e-15)
cout << "Res(CIZ)=" << res << " ";
else {
cout << endl << "TEST ERROR: Corrupt compressed GSymMatrix.cholesky_invert() function." << endl;
cout << "Residual matrix (all elements should be zero):" << endl;
cout << ciz_residuals << endl;
throw;
}
}
catch (exception &e) {
cout << e.what() << endl;
throw;
}
cout << "ok." << endl;
}