00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <cmath>
00019 #include <iostream>
00020 #include "matpackVII.h"
00021 #include "array.h"
00022 #include "make_array.h"
00023 #include "mystring.h"
00024 #include "make_vector.h"
00025 #include "math_funcs.h"
00026 #include "describe.h"
00027 #include "matpackII.h"
00028
00030 Joker joker;
00031
00032 Numeric by_reference(const Numeric& x)
00033 {
00034 return x+1;
00035 }
00036
00037 Numeric by_value(Numeric x)
00038 {
00039 return x+1;
00040 }
00041
00042 void fill_with_junk(VectorView x)
00043 {
00044 x = 999;
00045 }
00046
00047 void fill_with_junk(MatrixView x)
00048 {
00049 x = 888;
00050 }
00051
00052 int test1()
00053 {
00054 Vector v(20);
00055
00056 cout << "v.nelem() = " << v.nelem() << "\n";
00057
00058 for (Index i=0; i<v.nelem(); ++i )
00059 v[i] = (Numeric)i;
00060
00061 cout << "v.begin() = " << *v.begin() << "\n";
00062
00063 cout << "v = \n" << v << "\n";
00064
00065 fill_with_junk(v[Range(1,8,2)][Range(2,joker)]);
00066
00067
00068 Vector v2 = v[Range(2,4)];
00069
00070 cout << "v2 = \n" << v2 << "\n";
00071
00072 for (Index i=0 ; i<1000; ++i)
00073 {
00074 Vector v3(1000);
00075 v3 = (Numeric)i;
00076 }
00077
00078 v2[Range(joker)] = 88;
00079
00080 v2[Range(0,2)] = 77;
00081
00082 cout << "v = \n" << v << "\n";
00083 cout << "v2 = \n" << v2 << "\n";
00084 cout << "v2.nelem() = \n" << v2.nelem() << "\n";
00085
00086 Vector v3;
00087 v3.resize(v2.nelem());
00088 v3 = v2;
00089
00090 cout << "\nv3 = \n" << v3 << "\n";
00091 fill_with_junk(v2);
00092 cout << "\nv3 after junking v2 = \n" << v3 << "\n";
00093 v3 *= 2;
00094 cout << "\nv3 after *2 = \n" << v3 << "\n";
00095
00096 Matrix M(10,15);
00097 {
00098 Numeric n=0;
00099 for (Index i=0; i<M.nrows(); ++i)
00100 for (Index j=0; j<M.ncols(); ++j)
00101 M(i,j) = ++n;
00102 }
00103
00104 cout << "\nM =\n" << M << "\n";
00105
00106 cout << "\nM(Range(2,4),Range(2,4)) =\n" << M(Range(2,4),Range(2,4)) << "\n";
00107
00108 cout << "\nM(Range(2,4),Range(2,4))(Range(1,2),Range(1,2)) =\n"
00109 << M(Range(2,4),Range(2,4))(Range(1,2),Range(1,2)) << "\n";
00110
00111 cout << "\nM(1,Range(joker)) =\n" << M(1,Range(joker)) << "\n";
00112
00113 cout << "\nFilling M(1,Range(1,2)) with junk.\n";
00114 fill_with_junk(M(1,Range(1,2)));
00115
00116 cout << "\nM(Range(0,4),Range(0,4)) =\n" << M(Range(0,4),Range(0,4)) << "\n";
00117
00118 cout << "\nFilling M(Range(4,2,2),Range(6,3)) with junk.\n";
00119
00120 MatrixView s = M(Range(4,2,2),Range(6,3));
00121 fill_with_junk(s);
00122
00123 cout << "\nM =\n" << M << "\n";
00124
00125 const Matrix C = M;
00126
00127 cout << "\nC(Range(3,4,2),Range(2,3,3)) =\n"
00128 << C(Range(3,4,2),Range(2,3,3)) << "\n";
00129
00130 cout << "\nC(Range(3,4,2),Range(2,3,3)).transpose() =\n"
00131 << transpose(C(Range(3,4,2),Range(2,3,3))) << "\n";
00132
00133 return 0;
00134 }
00135
00136 void test2()
00137 {
00138 Vector v(50000000);
00139
00140 cout << "v.nelem() = " << v.nelem() << "\n";
00141
00142 cout << "Filling\n";
00143
00144
00145 v = 1.;
00146 cout << "Done\n";
00147
00148 }
00149
00150
00151 void test4()
00152 {
00153 Vector a(10);
00154 Vector b(a.nelem());
00155
00156 for ( Index i=0; i<a.nelem(); ++i )
00157 {
00158 a[i] = (Numeric)(i+1);
00159 b[i] = (Numeric)(a.nelem()-i);
00160 }
00161
00162 cout << "a = \n" << a << "\n";
00163 cout << "b = \n" << b << "\n";
00164 cout << "a*b \n= " << a*b << "\n";
00165
00166 Matrix A(11,6);
00167 Matrix B(10,20);
00168 Matrix C(20,5);
00169
00170 B = 2;
00171 C = 3;
00172 mult(A(Range(1,joker),Range(1,joker)),B,C);
00173
00174
00175
00176 cout << "\nB*C =\n" << A << "\n";
00177
00178 }
00179
00180 void test5()
00181 {
00182 Vector a(10);
00183 Vector b(20);
00184 Matrix M(10,20);
00185
00186
00187 b = 1;
00188 M = 2;
00189
00190 cout << "b = \n" << b << "\n";
00191 cout << "M =\n" << M << "\n";
00192
00193 mult(a,M,b);
00194 cout << "\na = M*b = \n" << a << "\n";
00195
00196 mult(transpose(b),transpose(a),M);
00197 cout << "\nb^t = a^t * M = \n" << transpose(b) << "\n";
00198
00199 }
00200
00201 void test6()
00202 {
00203 Index n = 5000;
00204 Vector x(1,n,1), y(n);
00205 Matrix M(n,n);
00206 M = 1;
00207
00208
00209 cout << "Transforming.\n";
00210
00211
00212
00213 for (Index i=0; i<1000; ++i)
00214 {
00215
00216 transform(y,sin,static_cast<MatrixView>(x));
00217 x+=1;
00218 }
00219
00220
00221 cout << "Done.\n";
00222 }
00223
00224 void test7()
00225 {
00226 Vector x(1,20000000,1);
00227 Vector y(x.nelem());
00228 transform(y,sin,x);
00229 cout << "min(sin(x)), max(sin(x)) = " << min(y) << ", " << max(y) << "\n";
00230 }
00231
00232 void test8()
00233 {
00234 Vector x(80000000);
00235 for ( Index i=0; i<x.nelem(); ++i )
00236 x[i] = (Numeric)i;
00237 cout << "Done." << "\n";
00238 }
00239
00240 void test9()
00241 {
00242
00243 Matrix A(4,8);
00244 Matrix B(A(Range(joker),Range(0,3)));
00245 cout << "B = " << B << "\n";
00246 }
00247
00248 void test10()
00249 {
00250
00251
00252
00253
00254 Vector v(1,8,1);
00255 Matrix M((const Vector)v);
00256 cout << "M = " << M << "\n";
00257 }
00258
00259 void test11()
00260 {
00261
00262
00263
00264
00265 Vector v(1,8,1);
00266 Matrix M(v.nelem(),1);
00267 M = v;
00268 cout << "M = " << M << "\n";
00269 }
00270
00271 void test12()
00272 {
00273
00274
00275 Array<String> sa(3);
00276 sa[0] = "It's ";
00277 sa[1] = "a ";
00278 sa[2] = "test.";
00279
00280 Array<String> sb(sa), sc(sa.nelem());
00281
00282 cout << "sb = \n" << sb << "\n";
00283
00284 sc = sa;
00285
00286 cout << "sc = \n" << sc << "\n";
00287
00288 }
00289
00290 void test13()
00291 {
00292
00293 const Vector v(1,8,1);
00294
00295
00296 Matrix M(v);
00297 M += v;
00298 cout << "M = \n" << M << "\n";
00299 }
00300
00301 void test14()
00302 {
00303
00304 Array<String> a = MakeArray<String>("Test");
00305 Array<Index> b = MakeArray<Index>(1,2);
00306 Array<Numeric> c = MakeArray<Numeric>(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0);
00307 cout << "a = \n" << a << "\n";
00308 cout << "b = \n" << b << "\n";
00309 cout << "c = \n" << c << "\n";
00310 }
00311
00312 void test15()
00313 {
00314
00315 String a = "Nur ein Test.";
00316 cout << "a = " << a << "\n";
00317 String b(a,5,-1);
00318 cout << "b = " << b << "\n";
00319 }
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 void test17()
00336 {
00337
00338 Vector a(1,10,1);
00339 cout << "a.sum() = " << a.sum() << "\n";
00340 }
00341
00342 void test18()
00343 {
00344
00345 Vector a(1,10,1);
00346 a *= a;
00347 cout << "a *= a =\n" << a << "\n";
00348 }
00349
00350 void test19()
00351 {
00352
00353
00354
00355
00356 Vector a(1,10,1);
00357 Vector b(5.3,10,0);
00358 cout << "a =\n" << a << "\n";
00359 cout << "b =\n" << b << "\n";
00360 }
00361
00362 void test20()
00363 {
00364
00365 MakeVector a(1,2,3,4,5,6,7,8,9,10);
00366 cout << "a =\n" << a << "\n";
00367 }
00368
00369 void test21()
00370 {
00371 Numeric s=0;
00372
00373 cout << "By reference:\n";
00374 for ( Index i=0; i<1e8; ++i )
00375 {
00376 s += by_reference(s);
00377 s -= by_reference(s);
00378 }
00379 cout << "s = " << s << "\n";
00380 }
00381
00382 void test22()
00383 {
00384 Numeric s=0;
00385
00386 cout << "By value:\n";
00387 for ( Index i=0; i<1e8; ++i )
00388 {
00389 s += by_value(s);
00390 s -= by_value(s);
00391 }
00392 cout << "s = " << s << "\n";
00393 }
00394
00395 void test23()
00396 {
00397
00398 Vector a(10,3.5);
00399 cout << "a =\n" << a << "\n";
00400 Matrix b(10,10,4.5);
00401 cout << "b =\n" << b << "\n";
00402 }
00403
00404 void test24()
00405 {
00406
00407 Matrix a(5,1,2.5);
00408 Vector b(1,5,1);
00409 a *= b;
00410 cout << "a*=b =\n" << a << "\n";
00411 a /= b;
00412 cout << "a/=b =\n" << a << "\n";
00413 a += b;
00414 cout << "a+=b =\n" << a << "\n";
00415 a -= b;
00416 cout << "a-=b =\n" << a << "\n";
00417 }
00418
00419 void test25()
00420 {
00421
00422 MakeArray<Index> a(1,2,3,4,5,6,5,4,3,2,1);
00423 cout << "min/max of a = " << min(a) << "/" << max(a) << "\n";
00424 }
00425
00426 void test26()
00427 {
00428 cout << "Test filling constructor for Array:\n";
00429 Array<String> a(4,"Hello");
00430 cout << "a =\n" << a << "\n";
00431 }
00432
00433 void test27()
00434 {
00435 cout << "Test Arrays of Vectors:\n";
00436 Array<Vector> a;
00437 a.push_back(MakeVector(1.0,2.0));
00438 a.push_back(Vector(1.0,10,1.0));
00439 cout << "a =\n" << a << "\n";
00440 }
00441
00442 void test28()
00443 {
00444 cout << "Test default constructor for Matrix:\n";
00445 Matrix a;
00446 Matrix b(a);
00447 cout << "b =\n" << b << "\n";
00448 }
00449
00450 void test29()
00451 {
00452 cout << "Test Arrays of Matrix:\n";
00453 ArrayOfMatrix a;
00454 Matrix b;
00455
00456 b.resize(2,2);
00457 b(0,0) = 1;
00458 b(0,1) = 2;
00459 b(1,0) = 3;
00460 b(1,1) = 4;
00461 a.push_back(b);
00462 b *= 2;
00463 a.push_back(b);
00464
00465 a[0].resize(2,3);
00466 a[0] = 4;
00467
00468 a.resize(3);
00469 a[2].resize(4,5);
00470 a[2] = 5;
00471
00472 cout << "a =\n" << a << "\n";
00473 }
00474
00475 void test30()
00476 {
00477 cout << "Test Matrices of size 0:\n";
00478 Matrix a(0,0);
00479
00480 a.resize(2,2);
00481 a = 1;
00482 cout << "a =\n" << a << "\n";
00483
00484 Matrix b(3,0);
00485
00486 b.resize(b.nrows(),b.ncols()+3);
00487 b = 2;
00488 cout << "b =\n" << b << "\n";
00489
00490 Matrix c(0,3);
00491
00492 c.resize(c.nrows()+3,c.ncols());
00493 c = 3;
00494 cout << "c =\n" << c << "\n";
00495 }
00496
00497 void test31()
00498 {
00499 cout << "Test Tensor3:\n";
00500
00501 Tensor3 a(2,3,4,1.0);
00502
00503 Index fill = 0;
00504
00505
00506 for ( Index i=0; i<a.npages(); ++i )
00507 for ( Index j=0; j<a.nrows(); ++j )
00508 for ( Index k=0; k<a.ncols(); ++k )
00509 a(i,j,k) = (Numeric)(++fill);
00510
00511 cout << "a =\n" << a << "\n";
00512
00513 cout << "Taking out first row of first page:\n"
00514 << a(0,0,Range(joker)) << "\n";
00515
00516 cout << "Taking out last column of second page:\n"
00517 << a(1,Range(joker),a.ncols()-1) << "\n";
00518
00519 cout << "Taking out the first letter on every page:\n"
00520 << a(Range(joker),0,0) << "\n";
00521
00522 cout << "Taking out first page:\n"
00523 << a(0,Range(joker),Range(joker)) << "\n";
00524
00525 cout << "Taking out last row of all pages:\n"
00526 << a(Range(joker),a.nrows()-1,Range(joker)) << "\n";
00527
00528 cout << "Taking out second column of all pages:\n"
00529 << a(Range(joker),Range(joker),1) << "\n";
00530
00531 a *= 2;
00532
00533 cout << "After element-vise multiplication with 2:\n"
00534 << a << "\n";
00535
00536 transform(a,sqrt,a);
00537
00538 cout << "After taking the square-root:\n"
00539 << a << "\n";
00540
00541 Index s = 200;
00542 cout << "Let's allocate a large tensor, "
00543 << (Numeric)(s*s*s*8)/1024./1024.
00544 << " MB...\n";
00545
00546 a.resize(s,s,s);
00547
00548 cout << "Set it to 1...\n";
00549
00550 a = 1;
00551
00552 cout << "a(900,900,900) = " << a(90,90,90) << "\n";
00553
00554 fill = 0;
00555
00556 cout << "Fill with running numbers, using for loops...\n";
00557 for ( Index i=0; i<a.npages(); ++i )
00558 for ( Index j=0; j<a.nrows(); ++j )
00559 for ( Index k=0; k<a.ncols(); ++k )
00560 a(i,j,k) = (Numeric)(++fill);
00561
00562 cout << "Max(a) = ...\n";
00563
00564 cout << max(a) << "\n";
00565
00566 }
00567
00568 void test32()
00569 {
00570 cout << "Test von X = A*X:\n";
00571 Matrix X(3,3),A(3,3),B(3,3);
00572
00573 for ( Index j=0; j<A.nrows(); ++j )
00574 for ( Index k=0; k<A.ncols(); ++k )
00575 {
00576 X(j,k) = 1;
00577 A(j,k) = (Numeric)(j+k);
00578 }
00579 cout << "A:\n" << A << "\n";
00580 cout << "X:\n" << X << "\n";
00581
00582 mult(B,A,X);
00583 cout << "B = A*X:\n" << B << "\n";
00584 mult(X,A,X);
00585 cout << "X = A*X:\n" << X << "\n";
00586
00587 cout << "This is not the same, and should not be, because you\n"
00588 << "are not allowed to use the same matrix as input and output\n"
00589 << "for mult!\n";
00590 }
00591
00592 void test33()
00593 {
00594 cout << "Making things look bigger than they are...\n";
00595
00596 {
00597 cout << "1. Make a scalar look like a vector:\n";
00598 Numeric a = 3.1415;
00599 VectorView av(a);
00600 cout << "a, viewed as a vector: " << av << "\n";
00601 cout << "Describe a: " << describe(a) << "\n";
00602 av[0] += 1;
00603 cout << "a, after the first element\n"
00604 << "of the vector has been increased by 1: " << a << "\n";
00605 }
00606
00607 {
00608 cout << "\n2. Make a vector look like a matrix:\n"
00609 << "This is an exception, because the new dimension is added at the end.\n";
00610 MakeVector a(1,2,3,4,5);
00611 MatrixView am = a;
00612 cout << "a, viewed as a matrix:\n" << am << "\n";
00613 cout << "Trasnpose view:\n" << transpose(am) << "\n";
00614 cout << "Describe transpose(am): " << describe(transpose(am)) << "\n";
00615
00616 cout << "\n3. Make a matrix look like a tensor3:\n";
00617 Tensor3View at3 = am;
00618 cout << "at3 = \n" << at3 << "\n";
00619 cout << "Describe at3: " << describe(at3) << "\n";
00620 at3 (0,2,0) += 1;
00621 cout << "a after Increasing element at3(0,2,0) by 1: \n" << a << "\n\n";
00622
00623 Tensor4View at4 = at3;
00624 cout << "at4 = \n" << at4 << "\n";
00625 cout << "Describe at4: " << describe(at4) << "\n";
00626
00627 Tensor5View at5 = at4;
00628 cout << "at5 = \n" << at5 << "\n";
00629 cout << "Describe at5: " << describe(at5) << "\n";
00630
00631 Tensor6View at6 = at5;
00632 cout << "at6 = \n" << at6 << "\n";
00633 cout << "Describe at6: " << describe(at6) << "\n";
00634
00635 Tensor7View at7 = at6;
00636 cout << "at7 = \n" << at7 << "\n";
00637 cout << "Describe at7: " << describe(at7) << "\n";
00638
00639 at7(0,0,0,0,0,2,0) -=1 ;
00640
00641 cout << "After subtracting 1 from at7(0,0,0,0,0,2,0)\n"
00642 << "a = " << a << "\n";
00643
00644 cout << "\nAll in one go:\n";
00645 Numeric b = 3.1415;
00646 Tensor7View bt7 =
00647 Tensor6View(
00648 Tensor5View(
00649 Tensor4View(
00650 Tensor3View(
00651 MatrixView(
00652 VectorView(b)
00653 )
00654 )
00655 )
00656 )
00657 );
00658 cout << "bt7:\n" << bt7 << "\n";
00659 cout << "Describe bt7: " << describe(bt7) << "\n";
00660 }
00661 }
00662
00663 void junk4(Tensor4View a)
00664 {
00665 cout << "Describe a: " << describe(a) << "\n";
00666 }
00667
00668 void junk2(ConstVectorView a)
00669 {
00670 cout << "Describe a: " << describe(a) << "\n";
00671 }
00672
00673 void test34()
00674 {
00675 cout << "Test, if dimension expansion works implicitly.\n";
00676
00677 Tensor3 t3(2,3,4);
00678 junk4(t3);
00679
00680 Numeric x;
00681 junk2(ConstVectorView(x));
00682 }
00683
00684 void test35()
00685 {
00686 cout << "Test the new copy semantics.\n";
00687 Vector a(1,4,1);
00688 Vector b;
00689
00690 b = a;
00691 cout << "b = " << b << "\n";
00692
00693 Vector aa(1,5,1);
00694 ConstVectorView c = aa;
00695 b = c;
00696 cout << "b = " << b << "\n";
00697
00698 Vector aaa(1,6,1);
00699 VectorView d = aaa;
00700 b = d;
00701 cout << "b = " << b << "\n";
00702 }
00703
00704 void test36()
00705 {
00706 cout << "Test using naked joker on Vector.\n";
00707 Vector a(1,4,1);
00708 VectorView b = a[joker];
00709 cout << "a = " << a << "\n";
00710 cout << "b = " << b << "\n";
00711 }
00712
00713 void test37(const Index& i)
00714 {
00715 Vector v1(5e-15, 10, 0.42e-15/11);
00716 Vector v2=v1;
00717
00718
00719 v1 /= (Numeric)i;
00720 v2 /=(Numeric)i;
00721 cout.precision(12);
00722
00723 v1*=v1;
00724 v2*=v2;
00725 cout << v1 << endl;
00726 cout << v2 << endl;
00727 }
00728
00729 void test38 ()
00730 {
00731 Vector v (5, 0.);
00732 Numeric * const a = v.get_c_array();
00733
00734 a[4] = 5.;
00735
00736 cout << v << endl;
00737 cout << endl << "========================" << endl << endl;
00738
00739 Matrix m (5, 5, 0.);
00740 Numeric * const b = m.get_c_array();
00741
00742 b[4] = 5.;
00743
00744 cout << m << endl;
00745 cout << endl << "========================" << endl << endl;
00746
00747 Tensor3 t3 (5, 6, 7, 0.);
00748 Numeric * const c = t3.get_c_array();
00749
00750 c[6] = 5.;
00751
00752 cout << t3 << endl;
00753 }
00754
00755 void test39 ()
00756 {
00757 Vector v1(1,5,1),v2(5);
00758
00759 v2 = v1 * 2;
00760
00761
00762
00763
00764
00765 cout << v2 << endl;
00766
00767 }
00768
00769 int main()
00770 {
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810 test39();
00811
00812 return 0;
00813 }