/usr/share/doc/root/test/vmatrix.cxx is in root-system-doc 5.34.30-0ubuntu8.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 | // @(#)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 "Riostream.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)
{
cout << "******************************************************************" <<endl;
cout << "* Starting Matrix - S T R E S S suite *" <<endl;
cout << "******************************************************************" <<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();
cout << "******************************************************************" <<endl;
}
//------------------------------------------------------------------------
void StatusPrint(Int_t id,const Char_t *title,Bool_t status)
{
// 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 += '.';
// cout << header << (status ? "OK" : "FAILED") << endl;
// Print test program number and its title
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] = ' ';
cout << header << (status ? "OK" : "FAILED") << endl;
}
//------------------------------------------------------------------------
// Test allocation functions and compatibility check
//
void stress_allocation()
{
if (gVerbose)
cout << "\n\n---> Test allocation and compatibility check" << 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) {
cout << "\nStatus information reported for matrix m3:" << endl;
cout << " Row lower bound ... " << m3.GetRowLwb() << endl;
cout << " Row upper bound ... " << m3.GetRowUpb() << endl;
cout << " Col lower bound ... " << m3.GetColLwb() << endl;
cout << " Col upper bound ... " << m3.GetColUpb() << endl;
cout << " No. rows ..........." << m3.GetNrows() << endl;
cout << " No. cols ..........." << m3.GetNcols() << endl;
cout << " No. of elements ...." << m3.GetNoElements() << endl;
}
if (gVerbose)
cout << "\nCheck matrices 1 & 2 for compatibility" << endl;
ok &= AreCompatible(m1,m2,gVerbose);
if (gVerbose)
cout << "Check matrices 1 & 4 for compatibility" << endl;
ok &= AreCompatible(m1,m4,gVerbose);
if (gVerbose)
cout << "m2 has to be compatible with m3 after resizing to m3" << 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)
cout << "m1 has to be compatible with m5 after resizing to m5" << endl;
m1.ResizeTo(m5.GetNrows(),m5.GetNcols());
ok &= AreCompatible(m1,m5,gVerbose);
if (gVerbose)
cout << "m1 has to be equal to m4 after stretching and shrinking" << endl;
m1.ResizeTo(m4.GetNrows(),m4.GetNcols());
ok &= VerifyMatrixIdentity(m1,m4,gVerbose,EPSILON);
if (gVerbose)
cout << "m5 has to be equal to m1 after shrinking" << endl;
m5.ResizeTo(m1.GetNrows(),m1.GetNcols());
ok &= VerifyMatrixIdentity(m1,m5,gVerbose,EPSILON);
if (gVerbose)
cout << "stretching and shrinking for small matrices (stack)" << endl;
if (gVerbose)
cout << "m8 has to be equal to m7 after stretching and shrinking" << 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)
cout << "m6 has to be equal to m8 after shrinking" << endl;
m6.ResizeTo(3,3);
ok &= VerifyMatrixIdentity(m6,m8,gVerbose,EPSILON);
if (gVerbose)
cout << "\nDone\n" << 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)
cout << "\n\n---> Test different matrix filling methods\n" << endl;
Bool_t ok = kTRUE;
if (gVerbose)
cout << "Creating m with Apply function..." << 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)
cout << "Check identity between m and matrix filled through (i,j)" << 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)
cout << "Check identity between m and matrix filled through [i][j]" << endl;
ok &= VerifyMatrixIdentity(m,m_overload2,gVerbose,EPSILON);
if (gVerbose)
cout << "Check identity between matrix filled through [i][j] and (i,j)" << 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)
cout << "Creating m_fortran by filling with fortran stored matrix" << endl;
TMatrixD m_fortran(-1,rsize-2,1,csize,a_fortran.GetArray(),"F");
if (gVerbose)
cout << "Check identity between m and m_fortran" << endl;
ok &= VerifyMatrixIdentity(m,m_fortran,gVerbose,EPSILON);
if (gVerbose)
cout << "Creating m_c by filling with c stored matrix" << endl;
TMatrixD m_c(-1,rsize-2,1,csize,a_c.GetArray());
if (gVerbose)
cout << "Check identity between m and m_c" << endl;
ok &= VerifyMatrixIdentity(m,m_c,gVerbose,EPSILON);
}
{
if (gVerbose)
cout << "Check insertion/extraction of sub-matrices" << 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)
cout << "Check array Use" << 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)
cout << "\nDone\n" << 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)
cout << "\nWriting zeros to m..." << 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)
cout << "Creating zero m1 ..." << endl;
TMatrixD m1(TMatrixD::kZero, m);
ok &= VerifyMatrixValue(m1,0.,gVerbose,EPSILON);
if (gVerbose)
cout << "Comparing m1 with 0 ..." << endl;
R__ASSERT(m1 == 0);
R__ASSERT(!(m1 != 0));
if (gVerbose)
cout << "Writing a pattern " << pattern << " by assigning to m(i,j)..." << 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)
cout << "Writing the pattern by assigning to m1 as a whole ..." << endl;
m1 = pattern;
ok &= VerifyMatrixValue(m1,pattern,gVerbose,EPSILON);
if (gVerbose)
cout << "Comparing m and m1 ..." << endl;
R__ASSERT(m == m1);
if (gVerbose)
cout << "Comparing (m=0) and m1 ..." << endl;
R__ASSERT(!(m.Zero() == m1));
if (gVerbose)
cout << "Clearing m1 ..." << endl;
m1.Zero();
ok &= VerifyMatrixValue(m1,0.,gVerbose,EPSILON);
if (gVerbose)
cout << "\nClear m and add the pattern" << endl;
m.Zero();
m += pattern;
ok &= VerifyMatrixValue(m,pattern,gVerbose,EPSILON);
if (gVerbose)
cout << " add the doubled pattern with the negative sign" << endl;
m += -2*pattern;
ok &= VerifyMatrixValue(m,-pattern,gVerbose,EPSILON);
if (gVerbose)
cout << " subtract the trippled pattern with the negative sign" << endl;
m -= -3*pattern;
ok &= VerifyMatrixValue(m,2*pattern,gVerbose,EPSILON);
if (gVerbose)
cout << "\nVerify comparison operations when all elems are the same" << 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)
cout << "\nVerify comparison operations when not all elems are the same" << 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)
cout << "\nAssign 2*pattern to m by repeating additions" << endl;
m = 0; m += pattern; m += pattern;
if (gVerbose)
cout << "Assign 2*pattern to m1 by multiplying by two " << endl;
m1 = pattern; m1 *= 2;
ok &= VerifyMatrixValue(m1,2*pattern,gVerbose,EPSILON);
R__ASSERT( m == m1 );
if (gVerbose)
cout << "Multiply m1 by one half returning it to the 1*pattern" << endl;
m1 *= 1/2.;
ok &= VerifyMatrixValue(m1,pattern,gVerbose,EPSILON);
if (gVerbose)
cout << "\nAssign -pattern to m and m1" << endl;
m.Zero(); m -= pattern; m1 = -pattern;
ok &= VerifyMatrixValue(m,-pattern,gVerbose,EPSILON);
R__ASSERT( m == m1 );
if (gVerbose)
cout << "m = sqrt(sqr(m)); m1 = abs(m1); Now m and m1 have to be the same" << 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)
cout << "\nCheck out to see that sin^2(x) + cos^2(x) = 1" << 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)
cout << "\nDone\n" << 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)
cout << "\n---> Test Binary Matrix element-by-element operations" << 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)
cout << "\nVerify assignment of a matrix to the matrix" << endl;
m = pattern;
m1.Zero();
m1 = m;
ok &= VerifyMatrixValue(m1,pattern,gVerbose,EPSILON);
R__ASSERT( m1 == m );
if (gVerbose)
cout << "\nAdding the matrix to itself, uniform pattern " << pattern << endl;
m.Zero(); m = pattern;
m1 = m; m1 += m1;
ok &= VerifyMatrixValue(m1,2*pattern,gVerbose,EPSILON);
if (gVerbose)
cout << " subtracting two matrices ..." << endl;
m1 -= m;
ok &= VerifyMatrixValue(m1,pattern,gVerbose,EPSILON);
if (gVerbose)
cout << " subtracting the matrix from itself" << endl;
m1 -= m1;
ok &= VerifyMatrixValue(m1,0.,gVerbose,EPSILON);
if (gVerbose)
cout << " adding two matrices together" << endl;
m1 += m;
ok &= VerifyMatrixValue(m1,pattern,gVerbose,EPSILON);
if (gVerbose) {
cout << "\nArithmetic operations on matrices with not the same elements" << endl;
cout << " adding mp to the zero matrix..." << endl;
}
m.Zero(); m += mp;
ok &= VerifyMatrixIdentity(m,mp,gVerbose,EPSILON);
m1 = m;
if (gVerbose)
cout << " making m = 3*mp and m1 = 3*mp, via add() and succesive mult" << endl;
Add(m,2.,mp);
m1 += m1; m1 += mp;
ok &= VerifyMatrixIdentity(m,m1,gVerbose,EPSILON);
if (gVerbose)
cout << " clear both m and m1, by subtracting from itself and via add()" << endl;
m1 -= m1;
Add(m,-3.,mp);
ok &= VerifyMatrixIdentity(m,m1,gVerbose,EPSILON);
if (gVerbose) {
cout << "\nTesting element-by-element multiplications and divisions" << endl;
cout << " squaring each element with sqr() and via multiplication" << endl;
}
m = mp; m1 = mp;
m.Sqr();
ElementMult(m1,m1);
ok &= VerifyMatrixIdentity(m,m1,gVerbose,EPSILON);
if (gVerbose)
cout << " compare (m = pattern^2)/pattern with pattern" << endl;
m = pattern; m1 = pattern;
m.Sqr();
ElementDiv(m,m1);
ok &= VerifyMatrixValue(m,pattern,gVerbose,EPSILON);
if (gVerbose)
Compare(m1,m);
if (gVerbose)
cout << "\nDone\n" << endl;
StatusPrint(4,"Binary Matrix element-by-element operations",ok);
}
//
//------------------------------------------------------------------------
// Verify matrix transposition
//
void stress_transposition(Int_t msize)
{
if (gVerbose) {
cout << "\n---> Verify matrix transpose "
"for matrices of a characteristic size " << msize << endl;
}
Bool_t ok = kTRUE;
{
if (gVerbose)
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)
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)
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)
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)
cout << "\nCheck double transposing a non-symmetric matrix" << endl;
TMatrixD mtt(TMatrixD::kTransposed,mt);
ok &= ( m == mtt ) ? kTRUE : kFALSE;
}
if (gVerbose)
cout << "\nDone\n" << 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)
cout << "\n---> Check creating some special matrices of dimension " << dim << endl;
Int_t j;
Bool_t ok = kTRUE;
{
if (gVerbose)
cout << "\ntest creating Hilbert matrices" << 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)
cout << "test creating zero matrix and copy constructor" << 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)
cout << "test creating unit matrices" << 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)
cout << "check to see that Haar matrix has *exactly* orthogonal columns" << 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)
cout << "make Haar (sub)matrix and test it *is* a submatrix" << 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)
cout << "\nDone\n" << 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)
cout << "\n---> Check making/forcing promises, (lazy)matrices of dimension " << dim << endl;
Bool_t ok = kTRUE;
{
if (gVerbose)
cout << "\nmake a promise and force it by a constructor" << endl;
TMatrixD m = hilbert_matrix_promise(dim,dim+1);
TMatrixD m1 = THilbertMatrixD(dim,dim+1);
ok &= VerifyMatrixIdentity(m,m1,gVerbose,EPSILON);
}
{
if (gVerbose)
cout << "make a promise and force it by an assignment" << 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)
cout << "\nDone\n" << endl;
StatusPrint(7,"Matrix promises",ok);
}
//
//------------------------------------------------------------------------
// Verify the norm calculation
//
void stress_norms(Int_t rsize,Int_t csize)
{
if (gVerbose)
cout << "\n---> Verify norm calculations" << 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)
cout << "\nAssign " << pattern << " to all the elements and check norms" << endl;
m = pattern;
if (gVerbose)
cout << " 1. (col) norm should be pattern*nrows" << endl;
ok &= ( m.Norm1() == pattern*m.GetNrows() ) ? kTRUE : kFALSE;
ok &= ( m.Norm1() == m.ColNorm() ) ? kTRUE : kFALSE;
if (gVerbose)
cout << " Inf (row) norm should be pattern*ncols" << endl;
ok &= ( m.NormInf() == pattern*m.GetNcols() ) ? kTRUE : kFALSE;
ok &= ( m.NormInf() == m.RowNorm() ) ? kTRUE : kFALSE;
if (gVerbose)
cout << " Square of the Eucl norm has got to be pattern^2 * no_elems" << 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)
cout << "\nDone\n" << endl;
StatusPrint(8,"Matrix Norms",ok);
}
//
//------------------------------------------------------------------------
// Verify the determinant evaluation
//
void stress_determinant(Int_t msize)
{
if (gVerbose)
cout << "\n---> Verify determinant evaluation for a square matrix of size " << msize << endl;
Bool_t ok = kTRUE;
TMatrixD m(msize,msize);
const double pattern = 2.5;
if (gVerbose)
cout << "\nCheck to see that the determinant of the unit matrix is one";
m.UnitMatrix();
if (gVerbose)
cout << "\n determinant is " << m.Determinant();
ok &= ( m.Determinant() == 1 ) ? kTRUE : kFALSE;
if (gVerbose)
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)
cout << "\n\tdeterminant is " << m.Determinant() << " should be " << TMath::Power(pattern,(double)m.GetNrows()) <<endl;
ok &= ( TMath::Abs(m.Determinant()-TMath::Power(pattern,(double)m.GetNrows())) < DBL_EPSILON ) ? kTRUE : kFALSE;
if (gVerbose)
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)
cout << "\nswap two rows/cols of a matrix through method 1 and watch det's sign";
m.UnitMatrix();
TMatrixDRow(m,3) = 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)
cout << "\nswap two rows/cols of a matrix through method 2 and watch det's sign";
m.UnitMatrix();
TMatrixDRow(m,3) = pattern;
const double det1 = m.Determinant();
TMatrixD m_save( m);
TMatrixDRow(m,1) = TMatrixDRow(m_save,3);
TMatrixDRow(m,3) = TMatrixDRow(m_save,1);
ok &= ( m.Determinant() == -det1 ) ? kTRUE : kFALSE;
m_save = m;
TMatrixDColumn(m,2) = TMatrixDColumn(m_save,4);
TMatrixDColumn(m,4) = TMatrixDColumn(m_save,2);
ok &= ( m.Determinant() == det1 ) ? kTRUE : kFALSE;
}
if (gVerbose)
cout << "\nCheck the determinant for the matrix with " << pattern << " at the anti-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==(m.GetColUpb()+m.GetColLwb()-j) ? pattern : 0 );
ok &= ( m.Determinant() == TMath::Power(pattern,(double)m.GetNrows()) *
( m.GetNrows()*(m.GetNrows()-1)/2 & 1 ? -1 : 1 ) ) ? kTRUE : kFALSE;
}
if (0)
{
if (gVerbose)
cout << "\nCheck the determinant for the singular matrix"
"\n\tdefined as above with zero first row";
m.Zero();
{
for (Int_t i = m.GetRowLwb()+1; i <= m.GetRowUpb(); i++)
for (Int_t j = m.GetColLwb(); j <= m.GetColUpb(); j++)
m(i,j) = ( i==(m.GetColUpb()+m.GetColLwb()-j) ? pattern : 0 );
}
if (gVerbose)
cout << "\n\tdeterminant is " << m.Determinant();
ok &= ( m.Determinant() == 0 ) ? kTRUE : kFALSE;
}
if (gVerbose)
cout << "\nCheck out the determinant of the Hilbert matrix";
TMatrixD H = THilbertMatrixD(3,3);
if (gVerbose) {
cout << "\n 3x3 Hilbert matrix: exact determinant 1/2160 ";
cout << "\n computed 1/"<< 1/H.Determinant();
}
H.ResizeTo(4,4);
H = THilbertMatrixD(4,4);
if (gVerbose) {
cout << "\n 4x4 Hilbert matrix: exact determinant 1/6048000 ";
cout << "\n computed 1/"<< 1/H.Determinant();
}
H.ResizeTo(5,5);
H = THilbertMatrixD(5,5);
if (gVerbose) {
cout << "\n 5x5 Hilbert matrix: exact determinant 3.749295e-12";
cout << "\n computed "<< H.Determinant();
}
if (gVerbose) {
TDecompQRH qrh(H);
Double_t d1,d2;
qrh.Det(d1,d2);
cout << "\n qrh det = " << d1*TMath::Power(2.0,d2) <<endl;
}
if (gVerbose) {
TDecompSVD svd(H);
Double_t d1,d2;
svd.Det(d1,d2);
cout << "\n svd det = " << d1*TMath::Power(2.0,d2) <<endl;
}
H.ResizeTo(7,7);
H = THilbertMatrixD(7,7);
if (gVerbose) {
cout << "\n 7x7 Hilbert matrix: exact determinant 4.8358e-25";
cout << "\n computed "<< H.Determinant();
}
H.ResizeTo(9,9);
H = THilbertMatrixD(9,9);
if (gVerbose) {
cout << "\n 9x9 Hilbert matrix: exact determinant 9.72023e-43";
cout << "\n computed "<< H.Determinant();
}
H.ResizeTo(10,10);
H = THilbertMatrixD(10,10);
if (gVerbose) {
cout << "\n 10x10 Hilbert matrix: exact determinant 2.16418e-53";
cout << "\n computed "<< H.Determinant();
}
if (gVerbose)
cout << "\nDone\n" << endl;
StatusPrint(9,"Matrix Determinant",ok);
}
//
//------------------------------------------------------------------------
// Verify matrix multiplications
//
void stress_mm_multiplications(Int_t msize)
{
if (gVerbose)
cout << "\n---> Verify matrix multiplications "
"for matrices of the characteristic size " << msize << endl;
const Double_t epsilon = EPSILON*msize/100;
Int_t i,j;
Bool_t ok = kTRUE;
{
if (gVerbose)
cout << "\nTest inline multiplications of the UnitMatrix" << endl;
TMatrixD m = THilbertMatrixD(-1,msize,-1,msize);
TMatrixD u(TMatrixD::kUnit,m);
m(3,1) = TMath::Pi();
u *= m;
ok &= VerifyMatrixIdentity(u,m,gVerbose,epsilon);
}
{
if (gVerbose)
cout << "Test inline multiplications by a DiagMat" << endl;
TMatrixD m = THilbertMatrixD(msize+3,msize);
m(1,3) = TMath::Pi();
TVectorD v(msize);
for (i = v.GetLwb(); i <= v.GetUpb(); i++)
v(i) = 1+i;
TMatrixD diag(msize,msize);
TMatrixDDiag d = TMatrixDDiag(diag);
d = v;
TMatrixD eth = m;
for (i = eth.GetRowLwb(); i <= eth.GetRowUpb(); i++)
for (j = eth.GetColLwb(); j <= eth.GetColUpb(); j++)
eth(i,j) *= v(j);
m *= diag;
ok &= VerifyMatrixIdentity(m,eth,gVerbose,epsilon);
}
{
if (gVerbose)
cout << "Test XPP = X where P is a permutation matrix" << endl;
TMatrixD m = THilbertMatrixD(msize-1,msize);
m(2,3) = TMath::Pi();
TMatrixD eth = m;
TMatrixD p(msize,msize);
for (i = p.GetRowLwb(); i <= p.GetRowUpb(); i++)
p(p.GetRowUpb()+p.GetRowLwb()-i,i) = 1;
m *= p;
m *= p;
ok &= VerifyMatrixIdentity(m,eth,gVerbose,epsilon);
}
{
if (gVerbose)
cout << "Test general matrix multiplication through inline mult" << endl;
TMatrixD m = THilbertMatrixD(msize-2,msize);
m(3,3) = TMath::Pi();
TMatrixD mt(TMatrixD::kTransposed,m);
TMatrixD p = THilbertMatrixD(msize,msize);
TMatrixDDiag(p) += 1;
TMatrixD mp(m,TMatrixD::kMult,p);
TMatrixD m1 = m;
m *= p;
ok &= VerifyMatrixIdentity(m,mp,gVerbose,epsilon);
TMatrixD mp1(mt,TMatrixD::kTransposeMult,p);
VerifyMatrixIdentity(m,mp1,gVerbose,epsilon);
ok &= ( !(m1 == m) );
TMatrixD mp2(TMatrixD::kZero,m1);
ok &= ( mp2 == 0 );
mp2.Mult(m1,p);
ok &= VerifyMatrixIdentity(m,mp2,gVerbose,epsilon);
if (gVerbose)
cout << "Test XP=X*P vs XP=X;XP*=P" << endl;
TMatrixD mp3 = m1*p;
ok &= VerifyMatrixIdentity(m,mp3,gVerbose,epsilon);
}
{
if (gVerbose)
cout << "Check to see UU' = U'U = E when U is the Haar matrix" << endl;
const Int_t order = 5;
const Int_t no_sub_cols = (1<<order)-5;
TMatrixD haar_sub = THaarMatrixD(5,no_sub_cols);
TMatrixD haar_sub_t(TMatrixD::kTransposed,haar_sub);
TMatrixD hsths(haar_sub_t,TMatrixD::kMult,haar_sub);
TMatrixD hsths1(TMatrixD::kZero,hsths); hsths1.Mult(haar_sub_t,haar_sub);
TMatrixD hsths_eth(TMatrixD::kUnit,hsths);
ok &= ( hsths.GetNrows() == no_sub_cols && hsths.GetNcols() == no_sub_cols );
ok &= VerifyMatrixIdentity(hsths,hsths_eth,gVerbose,EPSILON);
ok &= VerifyMatrixIdentity(hsths1,hsths_eth,gVerbose,EPSILON);
TMatrixD haar = THaarMatrixD(5);
TMatrixD unit(TMatrixD::kUnit,haar);
TMatrixD haar_t(TMatrixD::kTransposed,haar);
TMatrixD hth(haar,TMatrixD::kTransposeMult,haar);
TMatrixD hht(haar,TMatrixD::kMult,haar_t);
TMatrixD hht1 = haar; hht1 *= haar_t;
TMatrixD hht2(TMatrixD::kZero,haar); hht2.Mult(haar,haar_t);
ok &= VerifyMatrixIdentity(unit,hth,gVerbose,EPSILON);
ok &= VerifyMatrixIdentity(unit,hht,gVerbose,EPSILON);
ok &= VerifyMatrixIdentity(unit,hht1,gVerbose,EPSILON);
ok &= VerifyMatrixIdentity(unit,hht2,gVerbose,EPSILON);
}
if (gVerbose)
cout << "\nDone\n" << endl;
StatusPrint(10,"General Matrix Multiplications",ok);
}
//
//------------------------------------------------------------------------
// Verify symmetric matrix multiplications
//
void stress_sym_mm_multiplications(Int_t msize)
{
if (gVerbose)
cout << "\n---> Verify symmetric matrix multiplications "
"for matrices of the characteristic size " << msize << endl;
Bool_t ok = kTRUE;
Int_t i,j;
const Double_t epsilon = EPSILON*msize/100;
{
if (gVerbose)
cout << "\nTest inline multiplications of the UnitMatrix" << endl;
TMatrixD m = THilbertMatrixD(-1,msize,-1,msize);
TMatrixDSym m_sym(-1,msize,m.GetMatrixArray());
TMatrixDSym u(TMatrixDSym::kUnit,m_sym);
TMatrixD u2 = u * m_sym;
ok &= VerifyMatrixIdentity(u2,m_sym,gVerbose,epsilon);
}
if (ok)
{
if (gVerbose)
cout << "\nTest symmetric multiplications" << endl;
{
if (gVerbose)
cout << "\n Test m * m_sym == m_sym * m == m_sym * m_sym multiplications" << endl;
TMatrixD m = THilbertMatrixD(-1,msize,-1,msize);
TMatrixDSym m_sym(-1,msize,m.GetMatrixArray());
TMatrixD mm = m * m;
TMatrixD mm_sym1 = m_sym * m_sym;
TMatrixD mm_sym2 = m * m_sym;
TMatrixD mm_sym3 = m_sym * m;
ok &= VerifyMatrixIdentity(mm,mm_sym1,gVerbose,epsilon);
ok &= VerifyMatrixIdentity(mm,mm_sym2,gVerbose,epsilon);
ok &= VerifyMatrixIdentity(mm,mm_sym3,gVerbose,epsilon);
}
{
if (gVerbose)
cout << "\n Test n * m_sym == n * m multiplications" << endl;
TMatrixD n = THilbertMatrixD(-1,msize,-1,msize);
TMatrixD m = n;
n(1,3) = TMath::Pi();
n(3,1) = TMath::Pi();
TMatrixDSym m_sym(-1,msize,m.GetMatrixArray());
TMatrixD nm1 = n * m_sym;
TMatrixD nm2 = n * m;
ok &= VerifyMatrixIdentity(nm1,nm2,gVerbose,epsilon);
}
}
if (ok)
{
if (gVerbose)
cout << "Test inline multiplications by a DiagMatrix" << endl;
TMatrixDSym m = THilbertMatrixDSym(msize);
m(1,3) = TMath::Pi();
m(3,1) = TMath::Pi();
TVectorD v(msize);
for (i = v.GetLwb(); i <= v.GetUpb(); i++)
v(i) = 1+i;
TMatrixDSym diag(msize);
TMatrixDDiag d(diag); d = v;
TMatrixDSym eth = m;
for (i = eth.GetRowLwb(); i <= eth.GetRowUpb(); i++)
for (j = eth.GetColLwb(); j <= eth.GetColUpb(); j++)
eth(i,j) *= v(j);
TMatrixD m2 = m * diag;
ok &= VerifyMatrixIdentity(m2,eth,gVerbose,epsilon);
}
if (ok)
{
if (gVerbose)
cout << "Test XPP = X where P is a permutation matrix" << endl;
TMatrixDSym m = THilbertMatrixDSym(msize);
m(2,3) = TMath::Pi();
m(3,2) = TMath::Pi();
TMatrixDSym eth = m;
TMatrixDSym p(msize);
for (i = p.GetRowLwb(); i <= p.GetRowUpb(); i++)
p(p.GetRowUpb()+p.GetRowLwb()-i,i) = 1;
TMatrixD m2 = m * p;
m2 *= p;
ok &= VerifyMatrixIdentity(m2,eth,gVerbose,epsilon);
}
if (ok)
{
if (gVerbose)
cout << "Test general matrix multiplication through inline mult" << endl;
TMatrixDSym m = THilbertMatrixDSym(msize);
m(2,3) = TMath::Pi();
m(3,2) = TMath::Pi();
TMatrixDSym mt(TMatrixDSym::kTransposed,m);
TMatrixDSym p = THilbertMatrixDSym(msize);
TMatrixDDiag(p) += 1;
TMatrixD mp(m,TMatrixD::kMult,p);
TMatrixDSym m1 = m;
TMatrixD m3(m,TMatrixD::kMult,p);
memcpy(m.GetMatrixArray(),m3.GetMatrixArray(),msize*msize*sizeof(Double_t));
ok &= VerifyMatrixIdentity(m,mp,gVerbose,epsilon);
TMatrixD mp1(mt,TMatrixD::kTransposeMult,p);
ok &= VerifyMatrixIdentity(m,mp1,gVerbose,epsilon);
ok &= ( !(m1 == m) ) ? kTRUE : kFALSE;
TMatrixDSym mp2(TMatrixDSym::kZero,m);
ok &= ( mp2 == 0 ) ? kTRUE : kFALSE;
if (gVerbose)
cout << "Test XP=X*P vs XP=X;XP*=P" << endl;
TMatrixD mp3 = m1*p;
ok &= VerifyMatrixIdentity(m,mp3,gVerbose,epsilon);
}
if (ok)
{
if (gVerbose)
cout << "Check to see UU' = U'U = E when U is the Haar matrix" << endl;
{
const Int_t order = 5;
const Int_t no_sub_cols = (1<<order)-5;
TMatrixD haarb = THaarMatrixD(5,no_sub_cols);
TMatrixD haarb_t(TMatrixD::kTransposed,haarb);
TMatrixD hth(haarb_t,TMatrixD::kMult,haarb);
TMatrixDSym hth1(TMatrixDSym::kAtA,haarb);
ok &= VerifyMatrixIdentity(hth,hth1,gVerbose,epsilon);
}
{
TMatrixD haar = THaarMatrixD(5);
TMatrixD unit(TMatrixD::kUnit,haar);
TMatrixD haar_t(TMatrixD::kTransposed,haar);
TMatrixDSym hth(TMatrixDSym::kAtA,haar);
TMatrixD hht(haar,TMatrixD::kMult,haar_t);
TMatrixD hht1 = haar; hht1 *= haar_t;
ok &= VerifyMatrixIdentity(unit,hth,gVerbose,epsilon);
ok &= VerifyMatrixIdentity(unit,hht,gVerbose,epsilon);
ok &= VerifyMatrixIdentity(unit,hht1,gVerbose,epsilon);
}
}
if (gVerbose)
cout << "\nDone\n" << endl;
StatusPrint(11,"Symmetric Matrix Multiplications",ok);
}
//
//------------------------------------------------------------------------
// Verify vector-matrix multiplications
//
void stress_vm_multiplications(Int_t msize)
{
if (gVerbose)
cout << "\n---> Verify vector-matrix multiplications "
"for matrices of the characteristic size " << msize << endl;
const Double_t epsilon = EPSILON*msize/100;
Bool_t ok = kTRUE;
{
if (gVerbose)
cout << "\nCheck shrinking a vector by multiplying by a non-sq unit matrix" << endl;
TVectorD vb(-2,msize);
for (Int_t i = vb.GetLwb(); i <= vb.GetUpb(); i++)
vb(i) = TMath::Pi()-i;
ok &= ( vb != 0 ) ? kTRUE : kFALSE;
TMatrixD mc(1,msize-2,-2,msize); // contracting matrix
mc.UnitMatrix();
TVectorD v1 = vb;
TVectorD v2 = vb;
v1 *= mc;
v2.ResizeTo(1,msize-2);
ok &= VerifyVectorIdentity(v1,v2,gVerbose,epsilon);
}
{
if (gVerbose)
cout << "Check expanding a vector by multiplying by a non-sq unit matrix" << endl;
TVectorD vb(msize);
for (Int_t i = vb.GetLwb(); i <= vb.GetUpb(); i++)
vb(i) = TMath::Pi()+i;
ok &= ( vb != 0 ) ? kTRUE : kFALSE;
TMatrixD me(2,msize+5,0,msize-1); // expanding matrix
me.UnitMatrix();
TVectorD v1 = vb;
TVectorD v2 = vb;
v1 *= me;
v2.ResizeTo(v1);
ok &= VerifyVectorIdentity(v1,v2,gVerbose,epsilon);
}
{
if (gVerbose)
cout << "Check general matrix-vector multiplication" << endl;
TVectorD vb(msize);
for (Int_t i = vb.GetLwb(); i <= vb.GetUpb(); i++)
vb(i) = TMath::Pi()+i;
TMatrixD vm(msize,1);
TMatrixDColumn(vm,0) = vb;
TMatrixD m = THilbertMatrixD(0,msize,0,msize-1);
vb *= m;
ok &= ( vb.GetLwb() == 0 ) ? kTRUE : kFALSE;
TMatrixD mvm(m,TMatrixD::kMult,vm);
TMatrixD mvb(msize+1,1);
TMatrixDColumn(mvb,0) = vb;
ok &= VerifyMatrixIdentity(mvb,mvm,gVerbose,epsilon);
}
if (gVerbose)
cout << "\nDone\n" << endl;
StatusPrint(12,"Matrix Vector Multiplications",ok);
}
//
//------------------------------------------------------------------------
// Verify matrix inversion
//
void stress_inversion(Int_t msize)
{
if (gVerbose)
cout << "\n---> Verify matrix inversion for square matrices of size " << msize << endl;
const Double_t epsilon = EPSILON*msize/10;
Bool_t ok = kTRUE;
{
if (gVerbose)
cout << "\nTest inversion of a diagonal matrix" << endl;
TMatrixD m(-1,msize,-1,msize);
TMatrixD mi(TMatrixD::kZero,m);
for (Int_t i = m.GetRowLwb(); i <= m.GetRowUpb(); i++)
mi(i,i) = 1/(m(i,i)=i-m.GetRowLwb()+1);
TMatrixD mi1(TMatrixD::kInverted,m);
m.Invert();
ok &= VerifyMatrixIdentity(m,mi,gVerbose,epsilon);
ok &= VerifyMatrixIdentity(mi1,mi,gVerbose,epsilon);
}
{
if (gVerbose)
cout << "Test inversion of an orthonormal (Haar) matrix" << endl;
TMatrixD m = THaarMatrixD(3);
TMatrixD morig = m;
TMatrixD mt(TMatrixD::kTransposed,m);
double det = -1; // init to a wrong val to see if it's changed
m.Invert(&det);
ok &= ( TMath::Abs(det-1) <= msize*epsilon ) ? kTRUE : kFALSE;
ok &= VerifyMatrixIdentity(m,mt,gVerbose,epsilon);
TMatrixD mti(TMatrixD::kInverted,mt);
ok &= VerifyMatrixIdentity(mti,morig,gVerbose,msize*epsilon);
}
{
if (gVerbose)
cout << "Test inversion of a good matrix with diagonal dominance" << endl;
TMatrixD m = THilbertMatrixD(msize,msize);
TMatrixDDiag(m) += 1;
TMatrixD morig = m;
Double_t det_inv = 0;
const Double_t det_comp = m.Determinant();
m.Invert(&det_inv);
if (gVerbose) {
cout << "\tcomputed determinant " << det_comp << endl;
cout << "\tdeterminant returned by Invert() " << det_inv << endl;
}
if (gVerbose)
cout << "\tcheck to see M^(-1) * M is E" << endl;
TMatrixD mim(m,TMatrixD::kMult,morig);
TMatrixD unit(TMatrixD::kUnit,m);
ok &= VerifyMatrixIdentity(mim,unit,gVerbose,epsilon);
if (gVerbose)
cout << "\tcheck to see M * M^(-1) is E" << endl;
TMatrixD mmi = morig; mmi *= m;
ok &= VerifyMatrixIdentity(mmi,unit,gVerbose,epsilon);
}
if (gVerbose)
cout << "\nDone\n" << endl;
StatusPrint(13,"Matrix Inversion",ok);
}
//
//------------------------------------------------------------------------
// Test matrix I/O
//
void stress_matrix_io()
{
if (gVerbose)
cout << "\n---> Test matrix I/O" << endl;
Bool_t ok = kTRUE;
const double pattern = TMath::Pi();
TMatrixD m(40,40);
m = pattern;
if (gVerbose)
cout << "\nWrite matrix m to database" << endl;
TFile *f = new TFile("vmatrix.root", "RECREATE");
m.Write("m");
if (gVerbose)
cout << "\nClose database" << endl;
delete f;
if (gVerbose)
cout << "\nOpen database in read-only mode and read matrix" << endl;
TFile *f1 = new TFile("vmatrix.root");
TMatrixD *mr = (TMatrixD*) f1->Get("m");
if (gVerbose)
cout << "\nRead matrix should be same as original still in memory" << endl;
ok &= ((*mr) == m) ? kTRUE : kFALSE;
delete f1;
if (gVerbose)
cout << "\nDone\n" << endl;
StatusPrint(14,"Matrix Persistence",ok);
}
|