37 using std::runtime_error;
50 cout <<
"v.nelem() = " << v.
nelem() <<
"\n";
54 cout <<
"v.begin() = " << *v.
begin() <<
"\n";
56 cout <<
"v = \n" << v <<
"\n";
63 cout <<
"v2 = \n" << v2 <<
"\n";
65 for (
Index i = 0; i < 1000; ++i) {
74 cout <<
"v = \n" << v <<
"\n";
75 cout <<
"v2 = \n" << v2 <<
"\n";
76 cout <<
"v2.nelem() = \n" << v2.
nelem() <<
"\n";
82 cout <<
"\nv3 = \n" << v3 <<
"\n";
84 cout <<
"\nv3 after junking v2 = \n" << v3 <<
"\n";
86 cout <<
"\nv3 after *2 = \n" << v3 <<
"\n";
91 for (
Index i = 0; i <
M.nrows(); ++i)
92 for (
Index j = 0; j <
M.ncols(); ++j)
M(i, j) = ++n;
95 cout <<
"\nM =\n" <<
M <<
"\n";
97 cout <<
"\nM(Range(2,4),Range(2,4)) =\n"
100 cout <<
"\nM(Range(2,4),Range(2,4))(Range(1,2),Range(1,2)) =\n"
103 cout <<
"\nM(1,Range(joker)) =\n" <<
M(1,
Range(
joker)) <<
"\n";
105 cout <<
"\nFilling M(1,Range(1,2)) with junk.\n";
108 cout <<
"\nM(Range(0,4),Range(0,4)) =\n"
111 cout <<
"\nFilling M(Range(4,2,2),Range(6,3)) with junk.\n";
116 cout <<
"\nM =\n" <<
M <<
"\n";
120 cout <<
"\nC(Range(3,4,2),Range(2,3,3)) =\n"
123 cout <<
"\nC(Range(3,4,2),Range(2,3,3)).transpose() =\n"
132 cout <<
"v.nelem() = " << v.
nelem() <<
"\n";
150 cout <<
"a = \n" << a <<
"\n";
151 cout <<
"b = \n" << b <<
"\n";
152 cout <<
"a*b \n= " << a * b <<
"\n";
164 cout <<
"\nB*C =\n" << A <<
"\n";
176 cout <<
"b = \n" << b <<
"\n";
177 cout <<
"M =\n" <<
M <<
"\n";
180 cout <<
"\na = M*b = \n" << a <<
"\n";
193 cout <<
"Transforming.\n";
197 for (
Index i = 0; i < 1000; ++i) {
211 cout <<
"min(sin(x)), max(sin(x)) = " <<
min(
y) <<
", " <<
max(
y) <<
"\n";
225 cout <<
"B = " << B <<
"\n";
235 cout <<
"M = " <<
M <<
"\n";
246 cout <<
"M = " <<
M <<
"\n";
259 cout <<
"sb = \n" << sb <<
"\n";
263 cout <<
"sc = \n" << sc <<
"\n";
273 cout <<
"M = \n" <<
M <<
"\n";
280 Array<Numeric> c{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
281 cout <<
"a = \n" << a <<
"\n";
282 cout <<
"b = \n" << b <<
"\n";
283 cout <<
"c = \n" << c <<
"\n";
288 String a =
"Nur ein Test.";
289 cout <<
"a = " << a <<
"\n";
291 cout <<
"b = " << b <<
"\n";
311 cout <<
"a.sum() = " << a.
sum() <<
"\n";
318 cout <<
"a *= a =\n" << a <<
"\n";
328 cout <<
"a =\n" << a <<
"\n";
329 cout <<
"b =\n" << b <<
"\n";
334 Vector a{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
335 cout <<
"a =\n" << a <<
"\n";
341 cout <<
"By reference:\n";
346 cout <<
"s = " << s <<
"\n";
352 cout <<
"By value:\n";
357 cout <<
"s = " << s <<
"\n";
363 cout <<
"a =\n" << a <<
"\n";
365 cout <<
"b =\n" << b <<
"\n";
373 cout <<
"a*=b =\n" << a <<
"\n";
375 cout <<
"a/=b =\n" << a <<
"\n";
377 cout <<
"a+=b =\n" << a <<
"\n";
379 cout <<
"a-=b =\n" << a <<
"\n";
384 Array<Index> a{1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1};
385 cout <<
"min/max of a = " <<
min(a) <<
"/" <<
max(a) <<
"\n";
389 cout <<
"Test filling constructor for Array:\n";
391 cout <<
"a =\n" << a <<
"\n";
395 cout <<
"Test Arrays of Vectors:\n";
397 a.push_back({1.0, 2.0});
398 a.push_back(
Vector(1.0, 10, 1.0));
399 cout <<
"a =\n" << a <<
"\n";
403 cout <<
"Test default constructor for Matrix:\n";
406 cout <<
"b =\n" << b <<
"\n";
410 cout <<
"Test Arrays of Matrix:\n";
430 cout <<
"a =\n" << a <<
"\n";
434 cout <<
"Test Matrices of size 0:\n";
439 cout <<
"a =\n" << a <<
"\n";
445 cout <<
"b =\n" << b <<
"\n";
451 cout <<
"c =\n" << c <<
"\n";
455 cout <<
"Test Tensor3:\n";
466 cout <<
"a =\n" << a <<
"\n";
468 cout <<
"Taking out first row of first page:\n"
471 cout <<
"Taking out last column of second page:\n"
474 cout <<
"Taking out the first letter on every page:\n"
477 cout <<
"Taking out first page:\n"
480 cout <<
"Taking out last row of all pages:\n"
483 cout <<
"Taking out second column of all pages:\n"
488 cout <<
"After element-vise multiplication with 2:\n" << a <<
"\n";
492 cout <<
"After taking the square-root:\n" << a <<
"\n";
495 cout <<
"Let's allocate a large tensor, "
496 << (
Numeric)(s * s * s * 8) / 1024. / 1024. <<
" MB...\n";
500 cout <<
"Set it to 1...\n";
504 cout <<
"a(900,900,900) = " << a(90, 90, 90) <<
"\n";
508 cout <<
"Fill with running numbers, using for loops...\n";
509 for (
Index i = 0; i < a.npages(); ++i)
510 for (
Index j = 0; j < a.nrows(); ++j)
511 for (
Index k = 0; k < a.ncols(); ++k) a(i, j, k) = (
Numeric)(++fill);
513 cout <<
"Max(a) = ...\n";
515 cout <<
max(a) <<
"\n";
519 cout <<
"Test von X = A*X:\n";
520 Matrix X(3, 3), A(3, 3), B(3, 3);
522 for (
Index j = 0; j < A.nrows(); ++j)
523 for (
Index k = 0; k < A.ncols(); ++k) {
527 cout <<
"A:\n" << A <<
"\n";
528 cout <<
"X:\n" << X <<
"\n";
531 cout <<
"B = A*X:\n" << B <<
"\n";
533 cout <<
"X = A*X:\n" << X <<
"\n";
535 cout <<
"This is not the same, and should not be, because you\n"
536 <<
"are not allowed to use the same matrix as input and output\n"
541 cout <<
"Making things look bigger than they are...\n";
544 cout <<
"1. Make a scalar look like a vector:\n";
547 cout <<
"a, viewed as a vector: " << av <<
"\n";
548 cout <<
"Describe a: " <<
describe(a) <<
"\n";
550 cout <<
"a, after the first element\n"
551 <<
"of the vector has been increased by 1: " << a <<
"\n";
556 <<
"\n2. Make a vector look like a matrix:\n"
557 <<
"This is an exception, because the new dimension is added at the end.\n";
560 cout <<
"a, viewed as a matrix:\n" << am <<
"\n";
561 cout <<
"Trasnpose view:\n" <<
transpose(am) <<
"\n";
564 cout <<
"\n3. Make a matrix look like a tensor3:\n";
566 cout <<
"at3 = \n" << at3 <<
"\n";
567 cout <<
"Describe at3: " <<
describe(at3) <<
"\n";
569 cout <<
"a after Increasing element at3(0,2,0) by 1: \n" << a <<
"\n\n";
572 cout <<
"at4 = \n" << at4 <<
"\n";
573 cout <<
"Describe at4: " <<
describe(at4) <<
"\n";
576 cout <<
"at5 = \n" << at5 <<
"\n";
577 cout <<
"Describe at5: " <<
describe(at5) <<
"\n";
580 cout <<
"at6 = \n" << at6 <<
"\n";
581 cout <<
"Describe at6: " <<
describe(at6) <<
"\n";
584 cout <<
"at7 = \n" << at7 <<
"\n";
585 cout <<
"Describe at7: " <<
describe(at7) <<
"\n";
587 at7(0, 0, 0, 0, 0, 2, 0) -= 1;
589 cout <<
"After subtracting 1 from at7(0,0,0,0,0,2,0)\n"
590 <<
"a = " << a <<
"\n";
592 cout <<
"\nAll in one go:\n";
596 cout <<
"bt7:\n" << bt7 <<
"\n";
597 cout <<
"Describe bt7: " <<
describe(bt7) <<
"\n";
606 cout <<
"Test, if dimension expansion works implicitly.\n";
616 cout <<
"Test the new copy semantics.\n";
621 cout <<
"b = " << b <<
"\n";
626 cout <<
"b = " << b <<
"\n";
631 cout <<
"b = " << b <<
"\n";
635 cout <<
"Test using naked joker on Vector.\n";
638 cout <<
"a = " << a <<
"\n";
639 cout <<
"b = " << b <<
"\n";
643 Vector v1(5e-15, 10, 0.42e-15 / 11);
664 cout << endl <<
"========================" << endl << endl;
672 cout << endl <<
"========================" << endl << endl;
683 Vector v1(1, 5, 1), v2(5);
707 cout <<
"Vector: " << v1 << endl;
708 cout <<
"VectorView: " << vv << endl;
712 }
catch (
const std::runtime_error& e) {
713 std::cerr << e.what() << endl;
717 cout <<
"VectorView: " << vv << endl;
718 cout <<
"Vector: " << v1 << endl;
726 Vector x{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
727 cout <<
"x: " <<
x << endl;
730 cout <<
"y: " <<
y << endl;
733 cout <<
"z:\n" << z << endl;
735 cout <<
"Every other z:\n" << z(
Range(1, 2, 2),
joker) << endl;
740 cout <<
"zz:\n" << zz << endl;
741 cout <<
"New x: " <<
x << endl;
753 throw std::logic_error(
"Fail to initialize");;
761 static_assert(r.Nom() == 3,
"Rational fail to initialize properly");
762 static_assert(r.Denom() == 2,
"Rational fail to initialize properly");
766 static_assert(r2.Nom() == 5,
"Rational fail to initialize properly");
767 static_assert(r2.Denom() == 2,
"Rational fail to initialize properly");
771 static_assert(r3.Nom() == 0,
"Rational fail to initialize properly");
772 static_assert(r3.Denom() == 0,
"Rational fail to initialize properly");
773 static_assert(r3 not_eq r3,
"Rational fail to initialize properly");
774 static_assert(r3.isUndefined(),
"Rational fail to initialize properly");
778 static_assert(r4.Nom() == 0,
"Rational fail to initialize properly");
779 static_assert(r4.Denom() == 1,
"Rational fail to initialize properly");
783 static_assert(r5.Nom() == 1,
"Rational fail to initialize properly");
784 static_assert(r5.Denom() == 6,
"Rational fail to initialize properly");
788 static_assert(r6.Nom() == 1,
"Rational fail to initialize properly");
789 static_assert(r6.Denom() == 1,
"Rational fail to initialize properly");
790 static_assert(r6.toInt() == 1,
"Rational fail to initialize properly");
791 static_assert(r6.toIndex() == 1,
"Rational fail to initialize properly");
792 static_assert(r6.toNumeric() == 1e0,
"Rational fail to initialize properly");
794 constexpr
Index rattest=1<<8;
796 static_assert(r7 == 2,
"Rational fail to initialize properly");
800 #define docheck(fn, val, expect) \
801 cout << #fn << "(" << val << ") = " << fn(x) << " (expected " << #expect \
839 auto compare_func = [](
Numeric n) {
return n != 0; };
840 cout << std::any_of(v1.
begin(), v1.
end(), compare_func) << endl;
842 cout << std::any_of(v1.
begin(), v1.
end(), compare_func) << endl;
861 for (
Index i = 0; i < ntests; i++) {
877 for (
Index j = 0; j < n_diag; j++) {
878 pass = pass && (v[j] == A(j, j));
879 pass = pass && (vt[j] == AT(j, j));
881 if (j < vb.
nelem()) pass = pass && (vb[j] == B(j, j));
887 cout <<
"test diagonal: Passed all tests." << endl;
889 cout <<
"test diagonal: Failed." << endl;
897 Vector x(n), xt, x_ref(n),
y(m), y_ref(m);
902 for (
Index i = 0; i < ntests; i++) {
912 if (err_mul > max_err) max_err = err_mul;
915 cout <<
"\t A * x: max. rel. error = " << err_mul << endl;
924 if (err_mul > max_err) max_err = err_mul;
927 cout <<
"\t A^T * x: max. rel. error = " << err_mul << endl;
931 Index column_stride(random_column_stride()),
932 row_stride(random_row_stride());
935 m_sub = (m - 1) / row_stride + 1;
936 n_sub = (n - 1) / column_stride + 1;
948 if (err_mul > max_err) max_err = err_mul;
951 cout <<
"\t Random stride: max. rel. error = " << err_mul << endl << endl;
955 if ((m > 1) && (n > 1)) {
956 Index y_offset = rand() % (m - 1);
957 Index x_offset = rand() % (n - 1);
959 m_sub = m - y_offset - 1;
960 n_sub = n - x_offset - 1;
963 A(
Range(y_offset, m_sub),
Range(x_offset, n_sub)),
964 x[
Range(x_offset, n_sub)]);
966 A(
Range(y_offset, m_sub),
Range(x_offset, n_sub)),
967 x[
Range(x_offset, n_sub)]);
970 y[
Range(y_offset, m_sub)], y_ref[
Range(y_offset, m_sub)],
true);
971 if (err_mul > max_err) max_err = err_mul;
974 cout <<
"\t Random offset: max. rel. error = " << err_mul << endl
987 cout <<
"Matrix-Vector Multiplication: n = m = 100, ntests = 100" << endl;
992 cout <<
"Matrix-Vector Multiplication: n = 100, m = 20, ntests = 100"
997 if (err > max_err) max_err = err;
1000 cout <<
"Matrix-Vector Multiplication: n = 20, m = 100, ntests = 100"
1005 if (err > max_err) max_err = err;
1008 cout << endl <<
"Matrix Vector Multiplication: PASSED" << endl;
1010 cout << endl <<
"Matrix Vector Multiplication: FAILED" << endl;
1014 if (err > max_err) max_err = err;
1017 cout <<
"Matrix-Vector Multiplication: n = 20, m = 100, ntests = 100"
1022 if (err > max_err) max_err = err;
1025 cout <<
"Matrix-Vector Multiplication: n = 20, m = 100, ntests = 100"
1031 cout << endl <<
"Matrix Vector Multiplication: PASSED" << endl;
1033 cout << endl <<
"Matrix Vector Multiplication: FAILED" << endl;
1061 Matrix A(m, k), B(k, n), C(m, n), C_ref(m, n);
1063 for (
Index i = 0; i < ntests; i++) {
1069 cout <<
"MATRIX MULTIPLICATION: m = " << m <<
", k = " << k;
1070 cout <<
", n = " << n << endl;
1082 if (err_mul > max_err) max_err = err_mul;
1086 <<
"A * B: max. rel. error = " << err_mul << endl;
1097 if (err_trans1 > max_err) max_err = err_trans1;
1101 <<
"A^T * B: max. rel. err = " << err_trans1 << endl;
1112 if (err_trans2 > max_err) max_err = err_trans2;
1116 <<
"A * B^T: max. rel. err = " << err_trans2 << endl;
1124 if (err_trans3 > max_err) max_err = err_trans3;
1128 <<
"A^T * B^T: max. rel. err = " << err_trans3 << endl;
1134 Rand<Index> k_rand(1, k - 1), m_rand(1, m - 1), n_rand(1, n - 1);
1136 for (
Index i = 0; i < nsubtests - 1; i++) {
1137 Index k1(k_rand()), m1(m_rand()), n1(n_rand());
1149 mult(C_sub, A_sub, B_sub);
1153 if (err > max_err) max_err = err;
1157 <<
"Submatrix multiplication: max. rel. err = " << err;
1189 if (err > max_err) max_err = err;
1193 if (err > max_err) max_err = err;
1197 if (err > max_err) max_err = err;
1201 if (err > max_err) max_err = err;
1204 err =
matrix_mult(200, 200, 200, 20, 20, verbose);
1205 if (err > max_err) max_err = err;
1209 if (err > max_err) max_err = err;
1213 if (err > max_err) max_err = err;
1216 err =
matrix_mult(100, 100, 100, 0, 100, verbose);
1217 if (err > max_err) max_err = err;
1225 cout <<
"Array" << endl;
1228 cout << v.empty() << endl;
1230 cout << v.empty() << endl;
1233 cout <<
"Vector" << endl;
1236 cout << v.
empty() << endl;
1238 cout << v.
empty() << endl;
1241 cout <<
"Matrix" << endl;
1244 cout << v.
empty() << endl;
1246 cout << v.
empty() << endl;
1248 cout << v.
empty() << endl;
1251 cout <<
"Tensor3" << endl;
1254 cout << v.
empty() << endl;
1256 cout << v.
empty() << endl;
1258 cout << v.
empty() << endl;
1260 cout << v.
empty() << endl;
1263 cout <<
"Tensor4" << endl;
1266 cout << v.
empty() << endl;
1268 cout << v.
empty() << endl;
1270 cout << v.
empty() << endl;
1272 cout << v.
empty() << endl;
1274 cout << v.
empty() << endl;
1277 cout <<
"Tensor5" << endl;
1280 cout << v.
empty() << endl;
1282 cout << v.
empty() << endl;
1284 cout << v.
empty() << endl;
1286 cout << v.
empty() << endl;
1288 cout << v.
empty() << endl;
1290 cout << v.
empty() << endl;
1293 cout <<
"Tensor6" << endl;
1295 v.
resize(0, 1, 1, 1, 1, 1);
1296 cout << v.
empty() << endl;
1297 v.
resize(1, 0, 1, 1, 1, 1);
1298 cout << v.
empty() << endl;
1299 v.
resize(1, 1, 0, 1, 1, 1);
1300 cout << v.
empty() << endl;
1301 v.
resize(1, 1, 1, 0, 1, 1);
1302 cout << v.
empty() << endl;
1303 v.
resize(1, 1, 1, 1, 0, 1);
1304 cout << v.
empty() << endl;
1305 v.
resize(1, 1, 1, 1, 1, 0);
1306 cout << v.
empty() << endl;
1307 v.
resize(1, 1, 1, 1, 1, 1);
1308 cout << v.
empty() << endl;
1311 cout <<
"Tensor7" << endl;
1313 v.
resize(0, 1, 1, 1, 1, 1, 1);
1314 cout << v.
empty() << endl;
1315 v.
resize(1, 0, 1, 1, 1, 1, 1);
1316 cout << v.
empty() << endl;
1317 v.
resize(1, 1, 0, 1, 1, 1, 1);
1318 cout << v.
empty() << endl;
1319 v.
resize(1, 1, 1, 0, 1, 1, 1);
1320 cout << v.
empty() << endl;
1321 v.
resize(1, 1, 1, 1, 0, 1, 1);
1322 cout << v.
empty() << endl;
1323 v.
resize(1, 1, 1, 1, 1, 0, 1);
1324 cout << v.
empty() << endl;
1325 v.
resize(1, 1, 1, 1, 1, 1, 0);
1326 cout << v.
empty() << endl;
1327 v.
resize(1, 1, 1, 1, 1, 1, 1);
1328 cout << v.
empty() << endl;
1339 for (
Index i = 0; i < n - 1; i++)
x[i] =
start + (
double)i * step;
1351 std::cout <<
"vv.nelem: " << vv.
nelem() << std::endl;
1352 std::cout <<
"mv.nrows: " << mv.
nrows() <<
" ";
1353 std::cout <<
"mv.ncols: " << mv.
ncols() << std::endl;
1357 std::cout <<
"mv2.nrows: " << mv2.
nrows() <<
" ";
1358 std::cout <<
"mv2.ncols: " << mv2.
ncols() << std::endl;