forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmatrix.cxx
1549 lines (1369 loc) · 50 KB
/
vmatrix.cxx
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
// @(#)root/test:$Id$
// Author: Fons Rademakers and Eddy Offermann Nov 2003
//////////////////////////////////////////////////////////////////////////
// //
// Linear Algebra Package -- Matrix Verifications. //
// //
// This file implements a large set of TMat operation tests. //
// ******************************************************************* //
// * Starting Matrix - S T R E S S suite //
// ******************************************************************* //
// Test 1 : Allocation, Resizing.................................. OK //
// Test 2 : Filling, Inserting, Using............................. OK //
// Test 3 : Uniform matrix operations............................. OK //
// Test 4 : Binary Matrix element-by-element operations............OK //
// Test 5 : Matrix transposition...................................OK //
// Test 6 : Haar/Hilbert Matrix....................................OK //
// Test 7 : Matrix promises........................................OK //
// Test 8 : Matrix Norms...........................................OK //
// Test 9 : Matrix Determinant.....................................OK //
// Test 10 : General Matrix Multiplications.........................OK //
// Test 11 : Symmetric Matrix Multiplications.......................OK //
// Test 12 : Matrix Vector Multiplications..........................OK //
// Test 13 : Matrix Inversion.......................................OK //
// Test 14 : Matrix Persistence.....................................OK //
// ******************************************************************* //
// //
//////////////////////////////////////////////////////////////////////////
//_____________________________batch only_____________________
#ifndef __CINT__
#include <iostream>
#include "snprintf.h"
#include "TFile.h"
#include "TMatrixD.h"
#include "TMatrixDSym.h"
#include "TMatrixDLazy.h"
#include "TVectorD.h"
#include "TArrayD.h"
#include "TMath.h"
#include "TDecompLU.h"
#include "TDecompQRH.h"
#include "TDecompSVD.h"
void stress_matrix (Int_t verbose);
void StatusPrint (Int_t id,const TString &title,Int_t status);
void stress_allocation ();
void stress_matrix_fill (Int_t rsize,Int_t csize);
void stress_element_op (Int_t rsize,Int_t csize);
void stress_binary_ebe_op (Int_t rsize, Int_t csize);
void stress_transposition (Int_t msize);
void stress_special_creation (Int_t dim);
void stress_matrix_fill (Int_t rsize,Int_t csize);
void stress_matrix_promises (Int_t dim);
void stress_norms (Int_t rsize,Int_t csize);
void stress_determinant (Int_t msize);
void stress_mm_multiplications (Int_t msize);
void stress_sym_mm_multiplications(Int_t msize);
void stress_vm_multiplications (Int_t msize);
void stress_inversion (Int_t msize);
void stress_matrix_io ();
int main(int argc,char **argv)
{
Int_t verbose = 0;
Char_t c;
while (argc > 1 && argv[1][0] == '-' && argv[1][1] != 0) {
for (Int_t i = 1; (c = argv[1][i]) != 0; i++) {
switch (c) {
case 'v':
verbose = 1;
break;
default:
Error("vmatrix", "unknown flag -%c",c);
break;
}
}
argc--;
argv++;
}
stress_matrix(verbose);
return 0;
}
#endif
#define EPSILON 1.0e-14
Int_t gVerbose = 0;
//________________________________common part_________________________
void stress_matrix(Int_t verbose=0)
{
std::cout << "******************************************************************" <<std::endl;
std::cout << "* Starting Matrix - S T R E S S suite *" <<std::endl;
std::cout << "******************************************************************" <<std::endl;
gVerbose = verbose;
stress_allocation();
stress_matrix_fill(20,10);
stress_element_op(20,10);
stress_binary_ebe_op(10,20);
stress_transposition(20);
stress_special_creation(20);
#ifndef __CINT__
stress_matrix_promises(20);
#endif
stress_norms(40,20);
stress_determinant(20);
stress_mm_multiplications(20);
stress_sym_mm_multiplications(20);
stress_vm_multiplications(20);
stress_inversion(20);
stress_matrix_io();
std::cout << "******************************************************************" <<std::endl;
}
////////////////////////////////////////////////////////////////////////////////
/// Print test program number and its title
/// const Int_t kMAX = 65;
/// TString header = TString("Test ")+Form("%2d",id)+" : "+title;
/// const Int_t nch = header.Length();
/// for (Int_t i = nch; i < kMAX; i++) header += '.';
/// std::cout << header << (status ? "OK" : "FAILED") << std::endl;
/// Print test program number and its title
void StatusPrint(Int_t id,const Char_t *title,Bool_t status)
{
const Int_t kMAX = 65;
char header[80];
snprintf(header,80,"Test %2d : %s",id,title);
Int_t nch = strlen(header);
for (Int_t i=nch;i<kMAX;i++) header[i] = '.';
header[kMAX] = 0;
header[kMAX-1] = ' ';
std::cout << header << (status ? "OK" : "FAILED") << std::endl;
}
//------------------------------------------------------------------------
// Test allocation functions and compatibility check
//
void stress_allocation()
{
if (gVerbose)
std::cout << "\n\n---> Test allocation and compatibility check" << std::endl;
Bool_t ok = kTRUE;
Int_t i,j;
TMatrixD m1(4,20);
for (i = m1.GetRowLwb(); i <= m1.GetRowUpb(); i++)
for (j = m1.GetColLwb(); j <= m1.GetColUpb(); j++)
m1(i,j) = TMath::Pi()*i+TMath::E()*j;
TMatrixD m2(0,3,0,19);
TMatrixD m3(1,4,0,19);
TMatrixD m4(m1);
if (gVerbose) {
std::cout << "\nStatus information reported for matrix m3:" << std::endl;
std::cout << " Row lower bound ... " << m3.GetRowLwb() << std::endl;
std::cout << " Row upper bound ... " << m3.GetRowUpb() << std::endl;
std::cout << " Col lower bound ... " << m3.GetColLwb() << std::endl;
std::cout << " Col upper bound ... " << m3.GetColUpb() << std::endl;
std::cout << " No. rows ..........." << m3.GetNrows() << std::endl;
std::cout << " No. cols ..........." << m3.GetNcols() << std::endl;
std::cout << " No. of elements ...." << m3.GetNoElements() << std::endl;
}
if (gVerbose)
std::cout << "\nCheck matrices 1 & 2 for compatibility" << std::endl;
ok &= AreCompatible(m1,m2,gVerbose);
if (gVerbose)
std::cout << "Check matrices 1 & 4 for compatibility" << std::endl;
ok &= AreCompatible(m1,m4,gVerbose);
if (gVerbose)
std::cout << "m2 has to be compatible with m3 after resizing to m3" << std::endl;
m2.ResizeTo(m3);
ok &= AreCompatible(m2,m3,gVerbose);
TMatrixD m5(m1.GetNrows()+1,m1.GetNcols()+5);
for (i = m5.GetRowLwb(); i <= m5.GetRowUpb(); i++)
for (j = m5.GetColLwb(); j <= m5.GetColUpb(); j++)
m5(i,j) = TMath::Pi()*i+TMath::E()*j;
if (gVerbose)
std::cout << "m1 has to be compatible with m5 after resizing to m5" << std::endl;
m1.ResizeTo(m5.GetNrows(),m5.GetNcols());
ok &= AreCompatible(m1,m5,gVerbose);
if (gVerbose)
std::cout << "m1 has to be equal to m4 after stretching and shrinking" << std::endl;
m1.ResizeTo(m4.GetNrows(),m4.GetNcols());
ok &= VerifyMatrixIdentity(m1,m4,gVerbose,EPSILON);
if (gVerbose)
std::cout << "m5 has to be equal to m1 after shrinking" << std::endl;
m5.ResizeTo(m1.GetNrows(),m1.GetNcols());
ok &= VerifyMatrixIdentity(m1,m5,gVerbose,EPSILON);
if (gVerbose)
std::cout << "stretching and shrinking for small matrices (stack)" << std::endl;
if (gVerbose)
std::cout << "m8 has to be equal to m7 after stretching and shrinking" << std::endl;
TMatrixD m6(4,4);
for (i = m6.GetRowLwb(); i <= m6.GetRowUpb(); i++)
for (j = m6.GetColLwb(); j <= m6.GetColUpb(); j++)
m6(i,j) = TMath::Pi()*i+TMath::E()*j;
TMatrixD m8(3,3);
for (i = m8.GetRowLwb(); i <= m8.GetRowUpb(); i++)
for (j = m8.GetColLwb(); j <= m8.GetColUpb(); j++)
m8(i,j) = TMath::Pi()*i+TMath::E()*j;
TMatrixD m7(m8);
m8.ResizeTo(4,4);
m8.ResizeTo(3,3);
ok &= VerifyMatrixIdentity(m7,m8,gVerbose,EPSILON);
if (gVerbose)
std::cout << "m6 has to be equal to m8 after shrinking" << std::endl;
m6.ResizeTo(3,3);
ok &= VerifyMatrixIdentity(m6,m8,gVerbose,EPSILON);
if (gVerbose)
std::cout << "\nDone\n" << std::endl;
StatusPrint(1,"Allocation, Resizing",ok);
}
class FillMatrix : public TElementPosActionD {
Int_t no_elems,no_cols;
void Operation(Double_t &element) const
{ element = 4*TMath::Pi()/no_elems * (fI*no_cols+fJ); }
public:
FillMatrix() {}
FillMatrix(const TMatrixD &m) :
no_elems(m.GetNoElements()),no_cols(m.GetNcols()) { }
};
//
//------------------------------------------------------------------------
// Test Filling of matrix
//
void stress_matrix_fill(Int_t rsize,Int_t csize)
{
if (gVerbose)
std::cout << "\n\n---> Test different matrix filling methods\n" << std::endl;
Bool_t ok = kTRUE;
if (gVerbose)
std::cout << "Creating m with Apply function..." << std::endl;
TMatrixD m(-1,rsize-2,1,csize);
#ifndef __CINT__
FillMatrix f(m);
m.Apply(f);
#else
for (Int_t i = m.GetRowLwb(); i <= m.GetRowUpb(); i++)
for (Int_t j = m.GetColLwb(); j <= m.GetColUpb(); j++)
m(i,j) = 4*TMath::Pi()/m.GetNoElements() * (i*m.GetNcols()+j);
#endif
{
if (gVerbose)
std::cout << "Check identity between m and matrix filled through (i,j)" << std::endl;
TMatrixD m_overload1(-1,rsize-2,1,csize);
TMatrixD m_overload2(-1,rsize-2,1,csize);
for (Int_t i = m.GetRowLwb(); i <= m.GetRowUpb(); i++)
{
for (Int_t j = m.GetColLwb(); j <= m.GetColUpb(); j++)
{
const Double_t val = 4*TMath::Pi()/rsize/csize*(i*csize+j);
m_overload1(i,j) = val;
m_overload2[i][j] = val;
}
}
ok &= VerifyMatrixIdentity(m,m_overload1,gVerbose,EPSILON);
if (gVerbose)
std::cout << "Check identity between m and matrix filled through [i][j]" << std::endl;
ok &= VerifyMatrixIdentity(m,m_overload2,gVerbose,EPSILON);
if (gVerbose)
std::cout << "Check identity between matrix filled through [i][j] and (i,j)" << std::endl;
ok &= VerifyMatrixIdentity(m_overload1,m_overload2,gVerbose,EPSILON);
}
{
TArrayD a_fortran(rsize*csize);
TArrayD a_c (rsize*csize);
for (Int_t i = 0; i < rsize; i++)
{
for (Int_t j = 0; j < csize; j++)
{
a_c[i*csize+j] = 4*TMath::Pi()/rsize/csize*((i-1)*csize+j+1);
a_fortran[i+rsize*j] = a_c[i*csize+j];
}
}
if (gVerbose)
std::cout << "Creating m_fortran by filling with fortran stored matrix" << std::endl;
TMatrixD m_fortran(-1,rsize-2,1,csize,a_fortran.GetArray(),"F");
if (gVerbose)
std::cout << "Check identity between m and m_fortran" << std::endl;
ok &= VerifyMatrixIdentity(m,m_fortran,gVerbose,EPSILON);
if (gVerbose)
std::cout << "Creating m_c by filling with c stored matrix" << std::endl;
TMatrixD m_c(-1,rsize-2,1,csize,a_c.GetArray());
if (gVerbose)
std::cout << "Check identity between m and m_c" << std::endl;
ok &= VerifyMatrixIdentity(m,m_c,gVerbose,EPSILON);
}
{
if (gVerbose)
std::cout << "Check insertion/extraction of sub-matrices" << std::endl;
{
TMatrixD m_sub1 = m;
m_sub1.ResizeTo(0,rsize-2,2,csize);
TMatrixD m_sub2 = m.GetSub(0,rsize-2,2,csize,"");
ok &= VerifyMatrixIdentity(m_sub1,m_sub2,gVerbose,EPSILON);
}
{
TMatrixD m2(-1,rsize-2,1,csize);
TMatrixD m_part1 = m.GetSub(0,rsize-2,2,csize,"");
TMatrixD m_part2 = m.GetSub(0,rsize-2,1,1,"");
TMatrixD m_part3 = m.GetSub(-1,-1,2,csize,"");
TMatrixD m_part4 = m.GetSub(-1,-1,1,1,"");
m2.SetSub(0,2,m_part1);
m2.SetSub(0,1,m_part2);
m2.SetSub(-1,2,m_part3);
m2.SetSub(-1,1,m_part4);
ok &= VerifyMatrixIdentity(m,m2,gVerbose,EPSILON);
}
{
TMatrixD m2(-1,rsize-2,1,csize);
TMatrixD m_part1 = m.GetSub(0,rsize-2,2,csize,"S");
TMatrixD m_part2 = m.GetSub(0,rsize-2,1,1,"S");
TMatrixD m_part3 = m.GetSub(-1,-1,2,csize,"S");
TMatrixD m_part4 = m.GetSub(-1,-1,1,1,"S");
m2.SetSub(0,2,m_part1);
m2.SetSub(0,1,m_part2);
m2.SetSub(-1,2,m_part3);
m2.SetSub(-1,1,m_part4);
ok &= VerifyMatrixIdentity(m,m2,gVerbose,EPSILON);
}
}
{
if (gVerbose)
std::cout << "Check array Use" << std::endl;
{
TMatrixD *m1 = new TMatrixD(m);
TMatrixD *m2 = new TMatrixD();
m2->Use(m1->GetRowLwb(),m1->GetRowUpb(),m1->GetColLwb(),m1->GetColUpb(),m1->GetMatrixArray());
ok &= VerifyMatrixIdentity(m,*m2,gVerbose,EPSILON);
m2->Sqr();
TMatrixD m3 = m; m3.Sqr();
ok &= VerifyMatrixIdentity(m3,*m1,gVerbose,EPSILON);
delete m1;
delete m2;
}
}
if (gVerbose)
std::cout << "\nDone\n" << std::endl;
StatusPrint(2,"Filling, Inserting, Using",ok);
}
//
//------------------------------------------------------------------------
// Test uniform element operations
//
typedef double (*dfunc)(double);
class ApplyFunction : public TElementActionD {
dfunc fFunc;
void Operation(Double_t &element) const { element = fFunc(double(element)); }
public:
ApplyFunction(dfunc func) : fFunc(func) { }
};
void stress_element_op(Int_t rsize,Int_t csize)
{
Bool_t ok = kTRUE;
const Double_t pattern = 8.625;
TMatrixD m(-1,rsize-2,1,csize);
if (gVerbose)
std::cout << "\nWriting zeros to m..." << std::endl;
{
for (Int_t i = m.GetRowLwb(); i <= m.GetRowUpb(); i++)
for(Int_t j = m.GetColLwb(); j <= m.GetColUpb(); j++)
m(i,j) = 0;
ok &= VerifyMatrixValue(m,0.,gVerbose,EPSILON);
}
if (gVerbose)
std::cout << "Creating zero m1 ..." << std::endl;
TMatrixD m1(TMatrixD::kZero, m);
ok &= VerifyMatrixValue(m1,0.,gVerbose,EPSILON);
if (gVerbose)
std::cout << "Comparing m1 with 0 ..." << std::endl;
R__ASSERT(m1 == 0);
R__ASSERT(!(m1 != 0));
if (gVerbose)
std::cout << "Writing a pattern " << pattern << " by assigning to m(i,j)..." << std::endl;
{
for (Int_t i = m.GetRowLwb(); i <= m.GetRowUpb(); i++)
for (Int_t j = m.GetColLwb(); j <= m.GetColUpb(); j++)
m(i,j) = pattern;
ok &= VerifyMatrixValue(m,pattern,gVerbose,EPSILON);
}
if (gVerbose)
std::cout << "Writing the pattern by assigning to m1 as a whole ..." << std::endl;
m1 = pattern;
ok &= VerifyMatrixValue(m1,pattern,gVerbose,EPSILON);
if (gVerbose)
std::cout << "Comparing m and m1 ..." << std::endl;
R__ASSERT(m == m1);
if (gVerbose)
std::cout << "Comparing (m=0) and m1 ..." << std::endl;
R__ASSERT(!(m.Zero() == m1));
if (gVerbose)
std::cout << "Clearing m1 ..." << std::endl;
m1.Zero();
ok &= VerifyMatrixValue(m1,0.,gVerbose,EPSILON);
if (gVerbose)
std::cout << "\nClear m and add the pattern" << std::endl;
m.Zero();
m += pattern;
ok &= VerifyMatrixValue(m,pattern,gVerbose,EPSILON);
if (gVerbose)
std::cout << " add the doubled pattern with the negative sign" << std::endl;
m += -2*pattern;
ok &= VerifyMatrixValue(m,-pattern,gVerbose,EPSILON);
if (gVerbose)
std::cout << " subtract the trippled pattern with the negative sign" << std::endl;
m -= -3*pattern;
ok &= VerifyMatrixValue(m,2*pattern,gVerbose,EPSILON);
if (gVerbose)
std::cout << "\nVerify comparison operations when all elems are the same" << std::endl;
m = pattern;
R__ASSERT( m == pattern && !(m != pattern) );
R__ASSERT( m > 0 && m >= pattern && m <= pattern );
R__ASSERT( m > -pattern && m >= -pattern );
R__ASSERT( m <= pattern && !(m < pattern) );
m -= 2*pattern;
R__ASSERT( m < -pattern/2 && m <= -pattern/2 );
R__ASSERT( m >= -pattern && !(m > -pattern) );
if (gVerbose)
std::cout << "\nVerify comparison operations when not all elems are the same" << std::endl;
m = pattern; m(m.GetRowUpb(),m.GetColUpb()) = pattern-1;
R__ASSERT( !(m == pattern) && !(m != pattern) );
R__ASSERT( m != 0 ); // none of elements are 0
R__ASSERT( !(m >= pattern) && m <= pattern && !(m<pattern) );
R__ASSERT( !(m <= pattern-1) && m >= pattern-1 && !(m>pattern-1) );
if (gVerbose)
std::cout << "\nAssign 2*pattern to m by repeating additions" << std::endl;
m = 0; m += pattern; m += pattern;
if (gVerbose)
std::cout << "Assign 2*pattern to m1 by multiplying by two " << std::endl;
m1 = pattern; m1 *= 2;
ok &= VerifyMatrixValue(m1,2*pattern,gVerbose,EPSILON);
R__ASSERT( m == m1 );
if (gVerbose)
std::cout << "Multiply m1 by one half returning it to the 1*pattern" << std::endl;
m1 *= 1/2.;
ok &= VerifyMatrixValue(m1,pattern,gVerbose,EPSILON);
if (gVerbose)
std::cout << "\nAssign -pattern to m and m1" << std::endl;
m.Zero(); m -= pattern; m1 = -pattern;
ok &= VerifyMatrixValue(m,-pattern,gVerbose,EPSILON);
R__ASSERT( m == m1 );
if (gVerbose)
std::cout << "m = sqrt(sqr(m)); m1 = abs(m1); Now m and m1 have to be the same" << std::endl;
m.Sqr();
ok &= VerifyMatrixValue(m,pattern*pattern,gVerbose,EPSILON);
m.Sqrt();
ok &= VerifyMatrixValue(m,pattern,gVerbose,EPSILON);
m1.Abs();
ok &= VerifyMatrixValue(m1,pattern,gVerbose,EPSILON);
ok &= VerifyMatrixIdentity(m,m1,gVerbose,EPSILON);
if (gVerbose)
std::cout << "\nCheck out to see that sin^2(x) + cos^2(x) = 1" << std::endl;
{
#ifndef __CINT__
FillMatrix f(m);
m.Apply(f);
#else
for (Int_t i = m.GetRowLwb(); i <= m.GetRowUpb(); i++)
for (Int_t j = m.GetColLwb(); j <= m.GetColUpb(); j++)
m(i,j) = 4*TMath::Pi()/m.GetNoElements() * (i*m.GetNcols()+j);
#endif
}
m1 = m;
{
#ifndef __CINT__
ApplyFunction s(&TMath::Sin);
ApplyFunction c(&TMath::Cos);
m.Apply(s);
m1.Apply(c);
#else
for (Int_t i = m.GetRowLwb(); i <= m.GetRowUpb(); i++) {
for (Int_t j = m.GetColLwb(); j <= m.GetColUpb(); j++) {
m(i,j) = TMath::Sin(m(i,j));
m1(i,j) = TMath::Cos(m1(i,j));
}
}
#endif
}
m.Sqr();
m1.Sqr();
m += m1;
ok &= VerifyMatrixValue(m,1.,gVerbose,EPSILON);
if (gVerbose)
std::cout << "\nDone\n" << std::endl;
StatusPrint(3,"Uniform matrix operations",ok);
}
//
//------------------------------------------------------------------------
// Test binary matrix element-by-element operations
//
void stress_binary_ebe_op(Int_t rsize, Int_t csize)
{
if (gVerbose)
std::cout << "\n---> Test Binary Matrix element-by-element operations" << std::endl;
Bool_t ok = kTRUE;
const double pattern = 4.25;
TMatrixD m(2,rsize+1,0,csize-1);
TMatrixD m1(TMatrixD::kZero,m);
TMatrixD mp(TMatrixD::kZero,m);
{
for (Int_t i = mp.GetRowLwb(); i <= mp.GetRowUpb(); i++)
for (Int_t j = mp.GetColLwb(); j <= mp.GetColUpb(); j++)
mp(i,j) = (i-m.GetNrows()/2.)*j*pattern;
}
if (gVerbose)
std::cout << "\nVerify assignment of a matrix to the matrix" << std::endl;
m = pattern;
m1.Zero();
m1 = m;
ok &= VerifyMatrixValue(m1,pattern,gVerbose,EPSILON);
R__ASSERT( m1 == m );
if (gVerbose)
std::cout << "\nAdding the matrix to itself, uniform pattern " << pattern << std::endl;
m.Zero(); m = pattern;
m1 = m; m1 += m1;
ok &= VerifyMatrixValue(m1,2*pattern,gVerbose,EPSILON);
if (gVerbose)
std::cout << " subtracting two matrices ..." << std::endl;
m1 -= m;
ok &= VerifyMatrixValue(m1,pattern,gVerbose,EPSILON);
if (gVerbose)
std::cout << " subtracting the matrix from itself" << std::endl;
// Hiding the self-subtraction from the compiler, causing warning by -Wself-assign-overloaded in clang 7.0
m1 -= static_cast<TMatrixD&>(m1);
ok &= VerifyMatrixValue(m1,0.,gVerbose,EPSILON);
if (gVerbose)
std::cout << " adding two matrices together" << std::endl;
m1 += m;
ok &= VerifyMatrixValue(m1,pattern,gVerbose,EPSILON);
if (gVerbose) {
std::cout << "\nArithmetic operations on matrices with not the same elements" << std::endl;
std::cout << " adding mp to the zero matrix..." << std::endl;
}
m.Zero(); m += mp;
ok &= VerifyMatrixIdentity(m,mp,gVerbose,EPSILON);
m1 = m;
if (gVerbose)
std::cout << " making m = 3*mp and m1 = 3*mp, via add() and succesive mult" << std::endl;
Add(m,2.,mp);
m1 += m1; m1 += mp;
ok &= VerifyMatrixIdentity(m,m1,gVerbose,EPSILON);
if (gVerbose)
std::cout << " clear both m and m1, by subtracting from itself and via add()" << std::endl;
// Hiding the self-subtraction from the compiler, causing warning by -Wself-assign-overloaded in clang 7.0
m1 -= static_cast<TMatrixD&>(m1);
Add(m,-3.,mp);
ok &= VerifyMatrixIdentity(m,m1,gVerbose,EPSILON);
if (gVerbose) {
std::cout << "\nTesting element-by-element multiplications and divisions" << std::endl;
std::cout << " squaring each element with sqr() and via multiplication" << std::endl;
}
m = mp; m1 = mp;
m.Sqr();
ElementMult(m1,m1);
ok &= VerifyMatrixIdentity(m,m1,gVerbose,EPSILON);
if (gVerbose)
std::cout << " compare (m = pattern^2)/pattern with pattern" << std::endl;
m = pattern; m1 = pattern;
m.Sqr();
ElementDiv(m,m1);
ok &= VerifyMatrixValue(m,pattern,gVerbose,EPSILON);
if (gVerbose)
Compare(m1,m);
if (gVerbose)
std::cout << "\nDone\n" << std::endl;
StatusPrint(4,"Binary Matrix element-by-element operations",ok);
}
//
//------------------------------------------------------------------------
// Verify matrix transposition
//
void stress_transposition(Int_t msize)
{
if (gVerbose) {
std::cout << "\n---> Verify matrix transpose "
"for matrices of a characteristic size " << msize << std::endl;
}
Bool_t ok = kTRUE;
{
if (gVerbose)
std::cout << "\nCheck to see that a square UnitMatrix stays the same";
TMatrixD m(msize,msize);
m.UnitMatrix();
TMatrixD mt(TMatrixD::kTransposed,m);
ok &= ( m == mt ) ? kTRUE : kFALSE;
}
{
if (gVerbose)
std::cout << "\nTest a non-square UnitMatrix";
TMatrixD m(msize,msize+1);
m.UnitMatrix();
TMatrixD mt(TMatrixD::kTransposed,m);
R__ASSERT(m.GetNrows() == mt.GetNcols() && m.GetNcols() == mt.GetNrows() );
for (Int_t i = m.GetRowLwb(); i <= TMath::Min(m.GetRowUpb(),m.GetColUpb()); i++)
for (Int_t j = m.GetColLwb(); j <= TMath::Min(m.GetRowUpb(),m.GetColUpb()); j++)
ok &= ( m(i,j) == mt(i,j) ) ? kTRUE : kFALSE;
}
{
if (gVerbose)
std::cout << "\nCheck to see that a symmetric (Hilbert)Matrix stays the same";
TMatrixD m = THilbertMatrixD(msize,msize);
TMatrixD mt(TMatrixD::kTransposed,m);
ok &= ( m == mt ) ? kTRUE : kFALSE;
}
{
if (gVerbose)
std::cout << "\nCheck transposing a non-symmetric matrix";
TMatrixD m = THilbertMatrixD(msize+1,msize);
m(1,2) = TMath::Pi();
TMatrixD mt(TMatrixD::kTransposed,m);
R__ASSERT(m.GetNrows() == mt.GetNcols() && m.GetNcols() == mt.GetNrows());
R__ASSERT(mt(2,1) == (Double_t)TMath::Pi() && mt(1,2) != (Double_t)TMath::Pi());
R__ASSERT(mt[2][1] == (Double_t)TMath::Pi() && mt[1][2] != (Double_t)TMath::Pi());
if (gVerbose)
std::cout << "\nCheck double transposing a non-symmetric matrix" << std::endl;
TMatrixD mtt(TMatrixD::kTransposed,mt);
ok &= ( m == mtt ) ? kTRUE : kFALSE;
}
if (gVerbose)
std::cout << "\nDone\n" << std::endl;
StatusPrint(5,"Matrix transposition",ok);
}
//
//------------------------------------------------------------------------
// Test special matrix creation
//
class MakeHilbert : public TElementPosActionD {
void Operation(Double_t &element) const { element = 1./(fI+fJ+1); }
public:
MakeHilbert() { }
};
#ifndef __CINT__
class TestUnit : public TElementPosActionD {
mutable Int_t fIsUnit;
void Operation(Double_t &element) const
{ if (fIsUnit)
fIsUnit = ((fI==fJ) ? (element == 1.0) : (element == 0)); }
public:
TestUnit() : fIsUnit(0==0) { }
Int_t is_indeed_unit() const { return fIsUnit; }
};
#else
Bool_t is_indeed_unit(TMatrixD &m) {
Bool_t isUnit = kTRUE;
for (Int_t i = m.GetRowLwb(); i <= m.GetRowUpb(); i++)
for (Int_t j = m.GetColLwb(); j <= m.GetColUpb(); j++) {
if (isUnit)
isUnit = ((i==j) ? (m(i,j) == 1.0) : (m(i,j) == 0));
}
return isUnit;
}
#endif
void stress_special_creation(Int_t dim)
{
if (gVerbose)
std::cout << "\n---> Check creating some special matrices of dimension " << dim << std::endl;
Int_t j;
Bool_t ok = kTRUE;
{
if (gVerbose)
std::cout << "\ntest creating Hilbert matrices" << std::endl;
TMatrixD m = THilbertMatrixD(dim+1,dim);
TMatrixD m1(TMatrixD::kZero,m);
ok &= ( !(m == m1) ) ? kTRUE : kFALSE;
ok &= ( m != 0 ) ? kTRUE : kFALSE;
#ifndef __CINT__
MakeHilbert mh;
m1.Apply(mh);
#else
for (Int_t i = m1.GetRowLwb(); i <= m1.GetRowUpb(); i++)
for (j = m1.GetColLwb(); j <= m1.GetColUpb(); j++)
m1(i,j) = 1./(i+j+1);
#endif
ok &= ( m1 != 0 ) ? kTRUE : kFALSE;
ok &= ( m == m1 ) ? kTRUE : kFALSE;
}
{
if (gVerbose)
std::cout << "test creating zero matrix and copy constructor" << std::endl;
TMatrixD m = THilbertMatrixD(dim,dim+1);
ok &= ( m != 0 ) ? kTRUE : kFALSE;
TMatrixD m1(m); // Applying the copy constructor
ok &= ( m1 == m ) ? kTRUE : kFALSE;
TMatrixD m2(TMatrixD::kZero,m);
ok &= ( m2 == 0 ) ? kTRUE : kFALSE;
ok &= ( m != 0 ) ? kTRUE : kFALSE;
}
{
if (gVerbose)
std::cout << "test creating unit matrices" << std::endl;
TMatrixD m(dim,dim);
#ifndef __CINT__
{
TestUnit test_unit;
m.Apply(test_unit);
ok &= ( !test_unit.is_indeed_unit() ) ? kTRUE : kFALSE;
}
#else
ok &= ( !is_indeed_unit(m) ) ? kTRUE : kFALSE;
#endif
m.UnitMatrix();
#ifndef __CINT__
{
TestUnit test_unit;
m.Apply(test_unit);
ok &= ( test_unit.is_indeed_unit() ) ? kTRUE : kFALSE;
}
#else
ok &= ( is_indeed_unit(m) ) ? kTRUE : kFALSE;
#endif
m.ResizeTo(dim-1,dim);
TMatrixD m2(TMatrixD::kUnit,m);
#ifndef __CINT__
{
TestUnit test_unit;
m2.Apply(test_unit);
ok &= ( test_unit.is_indeed_unit() ) ? kTRUE : kFALSE;
}
#else
ok &= ( is_indeed_unit(m2) ) ? kTRUE : kFALSE;
#endif
m.ResizeTo(dim,dim-2);
m.UnitMatrix();
#ifndef __CINT__
{
TestUnit test_unit;
m.Apply(test_unit);
ok &= ( test_unit.is_indeed_unit() ) ? kTRUE : kFALSE;
}
#else
ok &= ( is_indeed_unit(m) ) ? kTRUE : kFALSE;
#endif
}
{
if (gVerbose)
std::cout << "check to see that Haar matrix has *exactly* orthogonal columns" << std::endl;
const Int_t order = 5;
const TMatrixD haar = THaarMatrixD(order);
ok &= ( haar.GetNrows() == (1<<order) &&
haar.GetNrows() == haar.GetNcols() ) ? kTRUE : kFALSE;
TVectorD colj(1<<order);
TVectorD coll(1<<order);
for (j = haar.GetColLwb(); j <= haar.GetColUpb(); j++) {
colj = TMatrixDColumn_const(haar,j);
ok &= (TMath::Abs(colj*colj-1.0) <= 1.0e-15 ) ? kTRUE : kFALSE;
for (Int_t l = j+1; l <= haar.GetColUpb(); l++) {
coll = TMatrixDColumn_const(haar,l);
const Double_t val = colj*coll;
ok &= ( TMath::Abs(val) <= 1.0e-15 ) ? kTRUE : kFALSE;
}
}
if (gVerbose)
std::cout << "make Haar (sub)matrix and test it *is* a submatrix" << std::endl;
const Int_t no_sub_cols = (1<<order) - 3;
const TMatrixD haar_sub = THaarMatrixD(order,no_sub_cols);
ok &= ( haar_sub.GetNrows() == (1<<order) &&
haar_sub.GetNcols() == no_sub_cols ) ? kTRUE : kFALSE;
for (j = haar_sub.GetColLwb(); j <= haar_sub.GetColUpb(); j++) {
colj = TMatrixDColumn_const(haar,j);
coll = TMatrixDColumn_const(haar_sub,j);
ok &= VerifyVectorIdentity(colj,coll,gVerbose);
}
}
if (gVerbose)
std::cout << "\nDone\n" << std::endl;
StatusPrint(6,"Haar/Hilbert Matrix",ok);
}
//
//------------------------------------------------------------------------
// Test matrix promises
//
class hilbert_matrix_promise : public TMatrixDLazy {
void FillIn(TMatrixD &m) const { m = THilbertMatrixD(m.GetRowLwb(),m.GetRowUpb(),
m.GetColLwb(),m.GetColUpb()); }
public:
hilbert_matrix_promise(Int_t nrows,Int_t ncols)
: TMatrixDLazy(nrows,ncols) {}
hilbert_matrix_promise(Int_t row_lwb,Int_t row_upb,
Int_t col_lwb,Int_t col_upb)
: TMatrixDLazy(row_lwb,row_upb,col_lwb,col_upb) { }
};
void stress_matrix_promises(Int_t dim)
{
if (gVerbose)
std::cout << "\n---> Check making/forcing promises, (lazy)matrices of dimension " << dim << std::endl;
Bool_t ok = kTRUE;
{
if (gVerbose)
std::cout << "\nmake a promise and force it by a constructor" << std::endl;
TMatrixD m = hilbert_matrix_promise(dim,dim+1);
TMatrixD m1 = THilbertMatrixD(dim,dim+1);
ok &= VerifyMatrixIdentity(m,m1,gVerbose,EPSILON);
}
{
if (gVerbose)
std::cout << "make a promise and force it by an assignment" << std::endl;
TMatrixD m(-1,dim,0,dim);
m = hilbert_matrix_promise(-1,dim,0,dim);
TMatrixD m1 = THilbertMatrixD(-1,dim,0,dim);
ok &= VerifyMatrixIdentity(m,m1,gVerbose,EPSILON);
}
if (gVerbose)
std::cout << "\nDone\n" << std::endl;
StatusPrint(7,"Matrix promises",ok);
}
//
//------------------------------------------------------------------------
// Verify the norm calculation
//
void stress_norms(Int_t rsize,Int_t csize)
{
if (gVerbose)
std::cout << "\n---> Verify norm calculations" << std::endl;
Bool_t ok = kTRUE;
const double pattern = 10.25;
if (rsize % 2 == 1 || csize %2 == 1)
Fatal("stress_norms","Sorry, size of the matrix to test must be even for this test\n");
TMatrixD m(rsize,csize);
if (gVerbose)
std::cout << "\nAssign " << pattern << " to all the elements and check norms" << std::endl;
m = pattern;
if (gVerbose)
std::cout << " 1. (col) norm should be pattern*nrows" << std::endl;
ok &= ( m.Norm1() == pattern*m.GetNrows() ) ? kTRUE : kFALSE;
ok &= ( m.Norm1() == m.ColNorm() ) ? kTRUE : kFALSE;
if (gVerbose)
std::cout << " Inf (row) norm should be pattern*ncols" << std::endl;
ok &= ( m.NormInf() == pattern*m.GetNcols() ) ? kTRUE : kFALSE;
ok &= ( m.NormInf() == m.RowNorm() ) ? kTRUE : kFALSE;
if (gVerbose)
std::cout << " Square of the Eucl norm has got to be pattern^2 * no_elems" << std::endl;
ok &= ( m.E2Norm() == (pattern*pattern)*m.GetNoElements() ) ? kTRUE : kFALSE;
TMatrixD m1(TMatrixD::kZero,m);
ok &= ( m.E2Norm() == E2Norm(m,m1) ) ? kTRUE : kFALSE;
if (gVerbose)
std::cout << "\nDone\n" << std::endl;
StatusPrint(8,"Matrix Norms",ok);
}
//
//------------------------------------------------------------------------
// Verify the determinant evaluation
//
void stress_determinant(Int_t msize)
{
if (gVerbose)
std::cout << "\n---> Verify determinant evaluation for a square matrix of size " << msize << std::endl;
Bool_t ok = kTRUE;
TMatrixD m(msize,msize);
const double pattern = 2.5;
if (gVerbose)
std::cout << "\nCheck to see that the determinant of the unit matrix is one";
m.UnitMatrix();
if (gVerbose)
std::cout << "\n\tdeterminant is " << m.Determinant();
ok &= ( m.Determinant() == 1 ) ? kTRUE : kFALSE;
if (gVerbose)
std::cout << "\nCheck the determinant for the matrix with " << pattern << " at the diagonal";
{
for (Int_t i = m.GetRowLwb(); i <= m.GetRowUpb(); i++)
for (Int_t j = m.GetColLwb(); j <= m.GetColUpb(); j++)
m(i,j) = ( i==j ? pattern : 0 );
}
if (gVerbose)
std::cout << "\n\tdeterminant is " << m.Determinant() << " should be " << TMath::Power(pattern,(double)m.GetNrows()) <<std::endl;
ok &= ( TMath::Abs(m.Determinant()-TMath::Power(pattern,(double)m.GetNrows())) < DBL_EPSILON ) ? kTRUE : kFALSE;
if (gVerbose)
std::cout << "\nCheck the determinant of the transposed matrix";
m.UnitMatrix();
m(1,2) = pattern;
TMatrixD m_tran(TMatrixD::kTransposed,m);
ok &= ( !(m == m_tran) ) ? kTRUE : kFALSE;
ok &= ( m.Determinant() == m_tran.Determinant() ) ? kTRUE : kFALSE;
{
if (gVerbose)
std::cout << "\nswap two rows/cols of a matrix through method 1 and watch det's sign";
m.UnitMatrix();
TMatrixDRow(m,3).Assign(pattern);
const double det1 = m.Determinant();
TMatrixDRow row1(m,1);
TVectorD vrow1(m.GetRowLwb(),m.GetRowUpb()); vrow1 = row1;
TVectorD vrow3(m.GetRowLwb(),m.GetRowUpb()); vrow3 = TMatrixDRow(m,3);
row1 = vrow3; TMatrixDRow(m,3) = vrow1;
ok &= ( m.Determinant() == -det1 ) ? kTRUE : kFALSE;
TMatrixDColumn col2(m,2);
TVectorD vcol2(m.GetRowLwb(),m.GetRowUpb()); vcol2 = col2;
TVectorD vcol4(m.GetRowLwb(),m.GetRowUpb()); vcol4 = TMatrixDColumn(m,4);
col2 = vcol4; TMatrixDColumn(m,4) = vcol2;
ok &= ( m.Determinant() == det1 ) ? kTRUE : kFALSE;
}
{
if (gVerbose)
std::cout << "\nswap two rows/cols of a matrix through method 2 and watch det's sign";
m.UnitMatrix();
TMatrixDRow(m,3).Assign(pattern);
const double det1 = m.Determinant();