55 cout <<
"\nM = \n" <<
M;
184 cout <<
"Testing the new simplified Sparse matrices:\n";
187 cout <<
"Empty A: " << A <<
"\n";
192 cout <<
"Diagonal A:\n" << A <<
"\n";
195 cout <<
"b:\n" << b <<
"\n";
198 cout <<
"c = A*b (should be [11,44,99]):\n" << c <<
"\n";
204 cout <<
"D:\n" << D <<
"\n";
208 cout <<
"E = A*D (should be [11,22],[44,88],[99,198]):\n" << E <<
"\n";
212 cout <<
"Testing transpose for the new simplified sparse matrices:\n";
215 Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
216 Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
217 for (
Index i = 0; i < 9; i++) B.
rw(r[i], c[i]) = (
Numeric)(i + 1);
219 cout <<
"B:\n" << B <<
"\n";
225 cout <<
"A:\n" << A <<
"\n";
227 cout <<
"Testing with a fully occupied matrix:\n";
229 for (
Index ri = 0; ri < 4; ri++)
230 for (
Index ci = 0; ci < 5; ci++) {
234 cout <<
"B:\n" << B <<
"\n";
236 cout <<
"A:\n" << A <<
"\n";
240 cout <<
"Testing sparse-sparse matrix multiplication:\n";
243 Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
244 Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
245 for (
Index i = 0; i < 9; i++) B.
rw(r[i], c[i]) = (
Numeric)(i + 1);
251 cout <<
"A:\n" << A <<
"\n";
255 cout <<
"Testing sparse copying:\n";
258 Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
259 Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
260 for (
Index i = 0; i < 9; i++) B.
rw(r[i], c[i]) = (
Numeric)(i + 1);
262 cout <<
"B:\n" << B <<
"\n";
268 cout <<
"A:\n" << A <<
"\n";
270 for (
Index i = 0; i < 100; ++i) {
275 cout <<
"A now:\n" << A <<
"\n";
279 cout <<
"Test to insert row in sparse:\n";
284 Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
285 Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
286 for (
Index i = 0; i < 9; i++) B.
rw(r[i], c[i]) = (
Numeric)(i + 1);
288 cout <<
"B[" << B.
nrows() <<
"," << B.
ncols() <<
"]:\n" << B <<
"\n";
289 cout <<
"v:\n" << v <<
"\n";
293 cout <<
"B (after insertion):\n" << B <<
"\n";
297 cout <<
"Test Sparse-Sparse multiplication reading matrices from xml "
305 cout <<
" Reading " << a <<
"...";
307 cout <<
"done.\n Reading " << b <<
"...";
310 }
catch (
const std::runtime_error &e) {
311 cerr << e.what() << endl;
315 cout <<
" Performing multiplication...";
321 cout <<
" Writing product to file: test45.xml...";
324 }
catch (
const std::runtime_error &e) {
325 cerr << e.what() << endl;
330 cout <<
"Test transpose with large matrix read from xml file:\n";
336 cout <<
" Reading " << a <<
"...";
339 }
catch (
const std::runtime_error &e) {
340 cerr << e.what() << endl;
349 cout <<
" Writing transpose(A) to file test46.xml" << endl;
351 }
catch (
const std::runtime_error &e) {
352 cerr << e.what() << endl;
359 cout <<
"Test make Identity matrix:\n";
366 cout <<
"A:\n" << A << endl;
370 cout <<
"Test absolute values of sparse matrix:\n";
373 Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
374 Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
375 for (
Index i = 0; i < 9; i++) B.
rw(r[i], c[i]) = -(
Numeric)i * 0.5;
376 cout <<
"B:\n" << B << endl;
381 cout <<
"abs(B):\n" << A << endl;
385 cout <<
"Testing sparse adding:\n";
390 for (
Index i = 0; i < 2; i++) B.
rw(rb[i], cb[i]) = (
Numeric)(i + 1);
393 Index rc[] = {0, 1, 2};
394 Index cc[] = {0, 1, 2};
395 for (
Index i = 0; i < 3; i++) {
397 cout << C.
rw(rc[i], cc[i]) << endl;
400 cout <<
"B:\n" << B <<
"\n";
401 cout <<
"C:\n" << C <<
"\n";
405 cout <<
"A=B+C:\n" << A <<
"\n";
408 cout <<
"D=B-C:\n" << D <<
"\n";
412 if (verbose) cout <<
"Testing xml IO." << endl << endl;
418 for (
int i = 0; i < ntests; i++) {
419 m = rand() % 1000 + 1;
420 n = rand() % 1000 + 1;
431 if (err > err_max) err_max = err;
434 cout <<
"Test " << i <<
": "
435 <<
"Max. Rel. Error = " << err << endl;
464 if (verbose) cout << endl <<
"Testing insert_row:" << endl << endl;
467 for (
Index i = 0; i < ntests; i++) {
468 Index m = (std::rand() % 10) + 1;
469 Index n = (std::rand() % 10) + 1;
470 Index r = std::rand() % m;
479 if ((A_sparse.
nrows() != m) || (A_sparse.
ncols() != n)) {
481 cout <<
"FAILED: Resize failed." << endl;
488 A =
static_cast<Matrix>(A_sparse);
491 if (err > err_max) err_max = err;
495 cout <<
"Maximum relative error: " << err << endl;
525 if (verbose) cout << endl <<
"Testing sparse identity:" << endl << endl;
527 for (
Index i = 0; i < ntests; i++) {
528 Index n = (std::rand() % 1000) + 1;
533 if ((A_sparse.
nrows() != n) || (A_sparse.
ncols() != n)) {
535 cout << endl <<
"FAILED: Resize failed." << endl;
544 if (err > err_max) err_max = err;
547 cout <<
"\t Maximum relative error: " << err << endl;
575 cout <<
"Testing sparse matrix construction and conversion:" << endl;
578 for (
Index i = 0; i < ntests; i++) {
583 A =
static_cast<Matrix>(A_sparse);
586 if (err > err_max) err_max = err;
589 cout <<
"Test No.: " << setw(5) << i;
590 cout <<
" Maximum rel. error: " << err_max << endl;
621 cout <<
"Testing sparse unary operations:" << endl << endl;
622 cout << setw(5) <<
"Test " << setw(15) <<
"Construction";
623 cout << setw(15) <<
"abs" << setw(15) <<
"transpose" << endl;
624 cout << std::string(55,
'-') << endl;
627 for (
Index i = 0; i < ntests; i++) {
629 Sparse A_sparse(m, n), B_sparse(m, n), C_sparse(n, m);
637 if (err > err_max) err_max = err;
639 if (verbose) cout << setw(5) << i << setw(15) << err;
643 for (
Index j = 0; j < m; j++) {
644 for (
Index k = 0; k < n; k++) {
645 B(j, k) =
abs(A(j, k));
649 abs(B_sparse, A_sparse);
652 if (err > err_max) err_max = err;
654 if (verbose) cout << setw(15) << err;
661 if (err > err_max) err_max = err;
663 if (verbose) cout << setw(15) << err << endl;
693 cout <<
"Testing sparse multiplication:" << endl << endl;
694 cout << setw(5) <<
"Test " << setw(5) <<
"m1" << setw(5) <<
"m2";
695 cout << setw(10) <<
"c_stride" << setw(5) <<
"n1" << setw(5) <<
"n2";
696 cout << setw(10) << setw(10) <<
"r_stride" << setw(15) <<
"A = B x C";
697 cout << setw(18) <<
"A^T = C^T x B^T" << endl;
698 cout << std::string(80,
'-') << endl;
706 for (
Index i = 0; i < ntests; i++) {
711 Index m1, dm, n1, dn, c_stride, r_stride;
713 m1 = rand() % (m - 1);
714 n1 = rand() % (n - 1);
715 dm = (rand() % (m - m1)) + 1;
716 dn = (rand() % (n - n1)) + 1;
717 c_stride = (rand() % 10) + 1;
718 r_stride = (rand() % 10) + 1;
720 c_stride =
min(dn, c_stride);
721 r_stride =
min(dm, r_stride);
726 cout << setw(5) << i << setw(5) << m1 << setw(5) << dm;
727 cout << setw(10) << r_stride << setw(5) << n1 << setw(5) << dn;
728 cout << setw(10) << c_stride;
735 Sparse C_sparse(dn, n), C_sparse_transpose(dn, n);
742 C =
static_cast<Matrix>(C_sparse);
744 mult(A_ref_view, B_mul, C);
745 mult(A_view, B_mul, C_sparse);
749 if (err > err_max) err_max = err;
752 cout << setw(15) << err;
764 C_sparse.resize(n, dn);
768 C =
static_cast<Matrix>(C_sparse);
771 B(
Range(n1, dn, c_stride),
Range(m1, dm, r_stride));
779 if (err > err_max) err_max = err;
782 cout << setw(15) << err << endl;
813 cout <<
"Testing sparse multiplication:" << endl << endl;
814 cout << setw(5) <<
"Test " << setw(5) <<
"m1" << setw(5) <<
"m2";
815 cout << setw(10) <<
"c_stride" << setw(5) <<
"n1" << setw(5) <<
"n2";
816 cout << setw(10) << setw(10) <<
"r_stride" << setw(15) <<
"A = B x C";
817 cout << setw(18) <<
"A^T = C^T x B^T" << endl;
818 cout << std::string(80,
'-') << endl;
826 for (
Index i = 0; i < ntests; i++) {
831 Index m1, dm, n1, dn, c_stride, r_stride;
833 m1 = rand() % (m - 1);
834 n1 = rand() % (n - 1);
835 dm = (rand() % (m - m1)) + 1;
836 dn = (rand() % (n - n1)) + 1;
837 c_stride = (rand() % 10) + 1;
838 r_stride = (rand() % 10) + 1;
841 cout << setw(5) << i << setw(5) << m1 << setw(5) << dm;
842 cout << setw(10) << c_stride << setw(5) << n1 << setw(5) << dn;
843 cout << setw(10) << r_stride;
846 c_stride =
min(dn, c_stride);
847 r_stride =
min(dm, r_stride);
855 Sparse B_sparse(m, dm), B_sparse_transpose(m, dm);
862 B =
static_cast<Matrix>(B_sparse);
865 mult(A_ref_view, B, C_mul);
866 mult(A_view, B_sparse, C_mul);
870 if (err > err_max) err_max = err;
873 cout << setw(15) << err;
885 B_sparse.resize(dm, m);
889 B =
static_cast<Matrix>(B_sparse);
899 if (err > err_max) err_max = err;
902 cout << setw(15) << err << endl;
940 cout <<
"Testing sparse multiplication:" << endl << endl;
941 cout << setw(5) <<
"Test " << setw(15) <<
"sparse-sparse";
942 cout << setw(15) <<
"sparse-dense" << setw(15) <<
"matrix-vector";
943 cout <<
"transposed matrix-vector";
944 cout << endl << std::string(80,
'-') << endl;
947 for (
Index i = 0; i < ntests; i++) {
950 k = (rand() % 1000) + 1;
952 Matrix A(m, n), A2(m, n), B, C;
953 Vector y(m), y2(m), yt(k), yt2(k),
x(k), xt(m);
954 Sparse A_sparse(m, n), B_sparse(m, k), C_sparse(k, n);
960 B =
static_cast<Matrix>(B_sparse);
961 C =
static_cast<Matrix>(C_sparse);
965 mult(A_sparse, B_sparse, C_sparse);
968 if (err > err_max) err_max = err;
971 cout << setw(5) << i << setw(15) << err;
976 mult(A2, B_sparse, C);
979 if (err > err_max) err_max = err;
982 cout << setw(15) << err;
987 mult(y2, B_sparse,
x);
990 if (err > err_max) err_max = err;
993 cout << setw(15) << err;
1001 if (err > err_max) err_max = err;
1004 cout << setw(15) << err << endl;
1033 cout <<
"Testing sparse arithmetic:" << endl << endl;
1034 cout << setw(5) <<
"Test " << setw(15) <<
"Addition";
1035 cout << setw(15) <<
"Multiplication" << setw(15) <<
"Subtraction";
1036 cout << setw(15) <<
"Scaling" << endl;
1037 cout << std::string(70,
'-') << endl;
1040 for (
Index i = 0; i < ntests; i++) {
1042 Sparse A_sparse(m, m), B_sparse(m, n), C_sparse(m, n), D_sparse(m, n);
1047 A =
static_cast<Matrix>(A_sparse);
1048 B =
static_cast<Matrix>(B_sparse);
1049 D =
static_cast<Matrix>(D_sparse);
1054 mult(C_sparse, A_sparse, B_sparse);
1057 if (err > err_max) err_max = err;
1059 if (verbose) cout << setw(5) << i << setw(15) << err;
1064 add(B_sparse, C_sparse, D_sparse);
1067 if (err > err_max) err_max = err;
1069 C_sparse += D_sparse;
1072 if (err > err_max) err_max = err;
1074 if (verbose) cout << setw(15) << err;
1078 C =
static_cast<Matrix>(C_sparse);
1080 sub(B_sparse, C_sparse, D_sparse);
1083 if (err > err_max) err_max = err;
1085 C_sparse -= D_sparse;
1088 if (err > err_max) err_max = err;
1090 if (verbose) cout << setw(15) << err;
1098 if (err > err_max) err_max = err;
1104 if (err > err_max) err_max = err;
1106 if (verbose) cout << setw(15) << err << endl;
1127 cout <<
"Testing sparse arithmetic: ";
1130 cout <<
"PASSED" << endl;
1132 cout <<
"FAILED (Error: " << err <<
")" << endl;
1134 cout <<
"Testing xml IO: ";
1137 cout <<
"PASSED" << endl;
1139 cout <<
"FAILED (Error: " << err <<
")" << endl;
1141 cout <<
"Testing identity matrix: ";
1144 cout <<
"PASSED" << endl;
1146 cout <<
"FAILED (Error: " << err <<
")" << endl;
1148 cout <<
"Testing inserting of rows: ";
1151 cout <<
"PASSED" << endl;
1153 cout <<
"FAILED (Error: " << err <<
")" << endl;
1155 cout <<
"Testing abs(...) and transpose(...): ";
1158 cout <<
"PASSED" << endl;
1160 cout <<
"FAILED (Error: " << err <<
")" << endl;
1162 cout <<
"Testing multiplication: ";
1165 cout <<
"PASSED" << endl;
1167 cout <<
"FAILED (Error: " << err <<
")" << endl;
1169 cout <<
"Testing sparse-dense multiplication: ";
1172 cout <<
"PASSED" << endl;
1174 cout <<
"FAILED (Error: " << err <<
")" << endl;
1176 cout <<
"Testing dense-sparse multiplication: ";
1179 cout <<
"PASSED" << endl;
1181 cout <<
"FAILED (Error: " << err <<
")" << endl;