ARTS  1.0.222
test_matpack.cc
Go to the documentation of this file.
1 /* Copyright (C) 2001 Stefan Buehler <sbuehler@uni-bremen.de>
2 
3  This program is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License as published by the
5  Free Software Foundation; either version 2, or (at your option) any
6  later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16  USA. */
17 
18 #include <math.h>
19 #include "matpackI.h"
20 #include "matpackIII.h"
21 #include "array.h"
22 #include "make_array.h"
23 #include "mystring.h"
24 #include "make_vector.h"
25 #include "math_funcs.h"
26 
27 
30 
32 {
33  return x+1;
34 }
35 
37 {
38  return x+1;
39 }
40 
42 {
43  x = 999;
44 }
45 
47 {
48  x = 888;
49 }
50 
51 int test1()
52 {
53  Vector v(20);
54 
55  cout << "v.nelem() = " << v.nelem() << "\n";
56 
57  for (Index i=0; i<v.nelem(); ++i )
58  v[i] = (Numeric)i;
59 
60  cout << "v.begin() = " << *v.begin() << "\n";
61 
62  cout << "v = \n" << v << "\n";
63 
64  fill_with_junk(v[Range(1,8,2)][Range(2,joker)]);
65  // fill_with_junk(v);
66 
67  Vector v2 = v[Range(2,4)];
68 
69  cout << "v2 = \n" << v2 << "\n";
70 
71  for (Index i=0 ; i<1000; ++i)
72  {
73  Vector v3(1000);
74  v3 = (Numeric)i;
75  }
76 
77  v2[Range(joker)] = 88;
78 
79  v2[Range(0,2)] = 77;
80 
81  cout << "v = \n" << v << "\n";
82  cout << "v2 = \n" << v2 << "\n";
83  cout << "v2.nelem() = \n" << v2.nelem() << "\n";
84 
85  Vector v3;
86  v3.resize(v2.nelem());
87  v3 = v2;
88 
89  cout << "\nv3 = \n" << v3 << "\n";
90  fill_with_junk(v2);
91  cout << "\nv3 after junking v2 = \n" << v3 << "\n";
92  v3 *= 2;
93  cout << "\nv3 after *2 = \n" << v3 << "\n";
94 
95  Matrix M(10,15);
96  {
97  Numeric n=0;
98  for (Index i=0; i<M.nrows(); ++i)
99  for (Index j=0; j<M.ncols(); ++j)
100  M(i,j) = ++n;
101  }
102 
103  cout << "\nM =\n" << M << "\n";
104 
105  cout << "\nM(Range(2,4),Range(2,4)) =\n" << M(Range(2,4),Range(2,4)) << "\n";
106 
107  cout << "\nM(Range(2,4),Range(2,4))(Range(1,2),Range(1,2)) =\n"
108  << M(Range(2,4),Range(2,4))(Range(1,2),Range(1,2)) << "\n";
109 
110  cout << "\nM(1,Range(joker)) =\n" << M(1,Range(joker)) << "\n";
111 
112  cout << "\nFilling M(1,Range(1,2)) with junk.\n";
113  fill_with_junk(M(1,Range(1,2)));
114 
115  cout << "\nM(Range(0,4),Range(0,4)) =\n" << M(Range(0,4),Range(0,4)) << "\n";
116 
117  cout << "\nFilling M(Range(4,2,2),Range(6,3)) with junk.\n";
118 
119  MatrixView s = M(Range(4,2,2),Range(6,3));
120  fill_with_junk(s);
121 
122  cout << "\nM =\n" << M << "\n";
123 
124  const Matrix C = M;
125 
126  cout << "\nC(Range(3,4,2),Range(2,3,3)) =\n"
127  << C(Range(3,4,2),Range(2,3,3)) << "\n";
128 
129  cout << "\nC(Range(3,4,2),Range(2,3,3)).transpose() =\n"
130  << transpose(C(Range(3,4,2),Range(2,3,3))) << "\n";
131 
132  return 0;
133 }
134 
135 void test2()
136 {
137  Vector v(50000000);
138 
139  cout << "v.nelem() = " << v.nelem() << "\n";
140 
141  cout << "Filling\n";
142 // for (Index i=0; i<v.nelem(); ++i )
143 // v[i] = sqrt(i);
144  v = 1.;
145  cout << "Done\n";
146 
147 }
148 
149 
150 // void test3()
151 // {
152 // SparseMatrix M(10,15);
153 
154 // cout << "M.nrows(), M.ncols() = "
155 // << M.nrows() << ", " << M.ncols() << "\n";
156 
157 // for (Index i=0; i<10; ++i)
158 // M(i,i) = i+1;
159 
160 // cout << "\nM = \n" << M;
161 
162 // const SparseMatrix S(M);
163 
164 // cout << "\nS(2,0) = " << S(2,0) << "\n";
165 
166 // cout << "\nS = \n" << S;
167 
168 // }
169 
170 void test4()
171 {
172  Vector a(10);
173  Vector b(a.nelem());
174 
175  for ( Index i=0; i<a.nelem(); ++i )
176  {
177  a[i] = (Numeric)i+1;
178  b[i] = (Numeric)(a.nelem()-i);
179  }
180 
181  cout << "a = \n" << a << "\n";
182  cout << "b = \n" << b << "\n";
183  cout << "a*b \n= " << a*b << "\n";
184 
185  Matrix A(11,6);
186  Matrix B(10,20);
187  Matrix C(20,5);
188 
189  B = 2;
190  C = 3;
191  mult(A(Range(1,joker),Range(1,joker)),B,C);
192 
193  // cout << "\nB =\n" << B << "\n";
194  // cout << "\nC =\n" << C << "\n";
195  cout << "\nB*C =\n" << A << "\n";
196 
197 }
198 
199 void test5()
200 {
201  Vector a(10);
202  Vector b(20);
203  Matrix M(10,20);
204 
205  // Fill b and M with a constant number:
206  b = 1;
207  M = 2;
208 
209  cout << "b = \n" << b << "\n";
210  cout << "M =\n" << M << "\n";
211 
212  mult(a,M,b); // a = M*b
213  cout << "\na = M*b = \n" << a << "\n";
214 
215  mult(transpose(b),transpose(a),M); // b^t = a^t * M
216  cout << "\nb^t = a^t * M = \n" << transpose(b) << "\n";
217 
218 }
219 
220 void test6()
221 {
222  Index n = 5000;
223  Vector x(1,n,1), y(n);
224  Matrix M(n,n);
225  M = 1;
226  // cout << "x = \n" << x << "\n";
227 
228  cout << "Transforming.\n";
229  // transform(x,sin,x);
230  // transform(transpose(y),sin,transpose(x));
231  // cout << "sin(x) =\n" << y << "\n";
232  for (Index i=0; i<1000; ++i)
233  {
234  // mult(y,M,x);
235  transform(y,sin,static_cast<MatrixView>(x));
236  x+=1;
237  }
238  // cout << "y =\n" << y << "\n";
239 
240  cout << "Done.\n";
241 }
242 
243 void test7()
244 {
245  Vector x(1,20000000,1);
246  Vector y(x.nelem());
247  transform(y,sin,x);
248  cout << "min(sin(x)), max(sin(x)) = " << min(y) << ", " << max(y) << "\n";
249 }
250 
251 void test8()
252 {
253  Vector x(80000000);
254  for ( Index i=0; i<x.nelem(); ++i )
255  x[i] = (Numeric)i;
256  cout << "Done." << "\n";
257 }
258 
259 void test9()
260 {
261  // Initialization of Matrix with view of other Matrix:
262  Matrix A(4,8);
263  Matrix B(A(Range(joker),Range(0,3)));
264  cout << "B = " << B << "\n";
265 }
266 
267 void test10()
268 {
269  // Initialization of Matrix with a vector (giving a 1 column Matrix).
270 
271  // At the moment doing this with a non-const Vector will result in a
272  // warning message.
273  Vector v(1,8,1);
274  Matrix M((const Vector)v);
275  cout << "M = " << M << "\n";
276 }
277 
278 void test11()
279 {
280  // Assignment between Vector and Matrix:
281 
282  // At the moment doing this with a non-const Vector will result in a
283  // warning message.
284  Vector v(1,8,1);
285  Matrix M(v.nelem(),1);
286  M = v;
287  cout << "M = " << M << "\n";
288 }
289 
290 void test12()
291 {
292  // Copying of Arrays
293 
294  Array<String> sa(3);
295  sa[0] = "It's ";
296  sa[1] = "a ";
297  sa[2] = "test.";
298 
299  Array<String> sb(sa), sc(sa.nelem());
300 
301  cout << "sb = \n" << sb << "\n";
302 
303  sc = sa;
304 
305  cout << "sc = \n" << sc << "\n";
306 
307 }
308 
309 void test13()
310 {
311  // Mix vector and one-column matrix in += operator.
312  const Vector v(1,8,1); // The const is necessary here to
313  // avoid compiler warnings about
314  // different conversion paths.
315  Matrix M(v);
316  M += v;
317  cout << "M = \n" << M << "\n";
318 }
319 
320 void test14()
321 {
322  // Test explicit Array constructors.
323  Array<String> a = MakeArray<String>("Test");
325  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);
326  cout << "a = \n" << a << "\n";
327  cout << "b = \n" << b << "\n";
328  cout << "c = \n" << c << "\n";
329 }
330 
331 void test15()
332 {
333  // Test String.
334  String a = "Nur ein Test.";
335  cout << "a = " << a << "\n";
336  String b(a,5,-1);
337  cout << "b = " << b << "\n";
338 }
339 
340 void test16()
341 {
342  // Test interaction between Array<Numeric> and Vector.
343  Vector a;
344  Array<Numeric> b;
345  b.push_back(1);
346  b.push_back(2);
347  b.push_back(3);
348  a.resize(b.nelem());
349  a = b;
350  cout << "b =\n" << b << "\n";
351  cout << "a =\n" << a << "\n";
352 }
353 
354 void test17()
355 {
356  // Test Sum.
357  Vector a(1,10,1);
358  cout << "a.sum() = " << a.sum() << "\n";
359 }
360 
361 void test18()
362 {
363  // Test elementvise square of a vector:
364  Vector a(1,10,1);
365  a *= a;
366  cout << "a *= a =\n" << a << "\n";
367 }
368 
369 void test19()
370 {
371  // There exists no explicit filling constructor of the form
372  // Vector a(3,1.7).
373  // But you can use the more general filling constructor with 3 arguments.
374 
375  Vector a(1,10,1);
376  Vector b(5.3,10,0);
377  cout << "a =\n" << a << "\n";
378  cout << "b =\n" << b << "\n";
379 }
380 
381 void test20()
382 {
383  // Test MakeVector:
384  MakeVector a(1,2,3,4,5,6,7,8,9,10);
385  cout << "a =\n" << a << "\n";
386 }
387 
388 void test21()
389 {
390  Numeric s=0;
391  // Test speed of call by reference:
392  cout << "By reference:\n";
393  for ( Index i=0; i<1e8; ++i )
394  {
395  s += by_reference(s);
396  s -= by_reference(s);
397  }
398  cout << "s = " << s << "\n";
399 }
400 
401 void test22()
402 {
403  Numeric s=0;
404  // Test speed of call by value:
405  cout << "By value:\n";
406  for ( Index i=0; i<1e8; ++i )
407  {
408  s += by_value(s);
409  s -= by_value(s);
410  }
411  cout << "s = " << s << "\n";
412 }
413 
414 void test23()
415 {
416  // Test constructors that fills with constant:
417  Vector a(10,3.5);
418  cout << "a =\n" << a << "\n";
419  Matrix b(10,10,4.5);
420  cout << "b =\n" << b << "\n";
421 }
422 
423 void test24()
424 {
425  // Try element-vise multiplication of Matrix and Vector:
426  Matrix a(5,1,2.5);
427  Vector b(1,5,1);
428  a *= b;
429  cout << "a*=b =\n" << a << "\n";
430  a /= b;
431  cout << "a/=b =\n" << a << "\n";
432  a += b;
433  cout << "a+=b =\n" << a << "\n";
434  a -= b;
435  cout << "a-=b =\n" << a << "\n";
436 }
437 
438 void test25()
439 {
440  // Test min and max for Array:
441  MakeArray<Index> a(1,2,3,4,5,6,5,4,3,2,1);
442  cout << "min/max of a = " << min(a) << "/" << max(a) << "\n";
443 }
444 
445 void test26()
446 {
447  cout << "Test filling constructor for Array:\n";
448  Array<String> a(4,"Hello");
449  cout << "a =\n" << a << "\n";
450 }
451 
452 void test27()
453 {
454  cout << "Test Arrays of Vectors:\n";
455  Array<Vector> a;
456  a.push_back(MakeVector(1.0,2.0));
457  a.push_back(Vector(1.0,10,1.0));
458  cout << "a =\n" << a << "\n";
459 }
460 
461 void test28()
462 {
463  cout << "Test default constructor for Matrix:\n";
464  Matrix a;
465  Matrix b(a);
466  cout << "b =\n" << b << "\n";
467 }
468 
469 void test29()
470 {
471  cout << "Test Arrays of Matrix:\n";
472  ArrayOfMatrix a;
473  Matrix b;
474 
475  b.resize(2,2);
476  b(0,0) = 1;
477  b(0,1) = 2;
478  b(1,0) = 3;
479  b(1,1) = 4;
480  a.push_back(b);
481  b *= 2;
482  a.push_back(b);
483 
484  a[0].resize(2,3);
485  a[0] = 4;
486 
487  a.resize(3);
488  a[2].resize(4,5);
489  a[2] = 5;
490 
491  cout << "a =\n" << a << "\n";
492 }
493 
494 void test30()
495 {
496  cout << "Test Matrices of size 0:\n";
497  Matrix a(0,0);
498  // cout << "a(0,0) =\n" << a(0,0) << "\n";
499  a.resize(2,2);
500  a = 1;
501  cout << "a =\n" << a << "\n";
502 
503  Matrix b(3,0);
504  // cout << "b(0,0) =\n" << b(0,0) << "\n";
505  b.resize(b.nrows(),b.ncols()+3);
506  b = 2;
507  cout << "b =\n" << b << "\n";
508 
509  Matrix c(0,3);
510  // cout << "c(0,0) =\n" << c(0,0) << "\n";
511  c.resize(c.nrows()+3,c.ncols());
512  c = 3;
513  cout << "c =\n" << c << "\n";
514 }
515 
516 void test31()
517 {
518  cout << "Test Tensor3:\n";
519 
520  Tensor3 a(2,3,4,1.0);
521 
522  Index fill = 0;
523 
524  // Fill with some numbers
525  for ( Index i=0; i<a.npages(); ++i )
526  for ( Index j=0; j<a.nrows(); ++j )
527  for ( Index k=0; k<a.ncols(); ++k )
528  a(i,j,k) = (Numeric)(++fill);
529 
530  cout << "a =\n" << a << "\n";
531 
532  cout << "Taking out first row of first page:\n"
533  << a(0,0,Range(joker)) << "\n";
534 
535  cout << "Taking out last column of second page:\n"
536  << a(1,Range(joker),a.ncols()-1) << "\n";
537 
538  cout << "Taking out the first letter on every page:\n"
539  << a(Range(joker),0,0) << "\n";
540 
541  cout << "Taking out first page:\n"
542  << a(0,Range(joker),Range(joker)) << "\n";
543 
544  cout << "Taking out last row of all pages:\n"
545  << a(Range(joker),a.nrows()-1,Range(joker)) << "\n";
546 
547  cout << "Taking out second column of all pages:\n"
548  << a(Range(joker),Range(joker),1) << "\n";
549 
550  a *= 2;
551 
552  cout << "After element-vise multiplication with 2:\n"
553  << a << "\n";
554 
555  transform(a,sqrt,a);
556 
557  cout << "After taking the square-root:\n"
558  << a << "\n";
559 
560  Index s = 200;
561  cout << "Let's allocate a large tensor, "
562  << (Numeric)(s*s*s)*8/1024./1024.
563  << " MB...\n";
564 
565  a.resize(s,s,s);
566 
567  cout << "Set it to 1...\n";
568 
569  a = 1;
570 
571  cout << "a(900,900,900) = " << a(90,90,90) << "\n";
572 
573  fill = 0;
574 
575  cout << "Fill with running numbers, using for loops...\n";
576  for ( Index i=0; i<a.npages(); ++i )
577  for ( Index j=0; j<a.nrows(); ++j )
578  for ( Index k=0; k<a.ncols(); ++k )
579  a(i,j,k) = (Numeric)(++fill);
580 
581  cout << "Max(a) = ...\n";
582 
583  cout << max(a) << "\n";
584 
585 }
586 
587 int main()
588 {
589  test31();
590  return 0;
591 }
Matrix
The Matrix class.
Definition: matpackI.h:544
transform
void transform(VectorView y, double(&my_func)(double), ConstVectorView x)
A generic transform function for vectors, which can be used to implement mathematical functions opera...
Definition: matpackI.h:2405
main
int main()
Definition: test_matpack.cc:587
MatrixView
The MatrixView class.
Definition: matpackI.h:476
test13
void test13()
Definition: test_matpack.cc:309
test20
void test20()
Definition: test_matpack.cc:381
Tensor3
The Tensor3 class.
Definition: matpackIII.h:227
test18
void test18()
Definition: test_matpack.cc:361
test19
void test19()
Definition: test_matpack.cc:369
transpose
ConstMatrixView transpose(ConstMatrixView m)
Const version of transpose.
Definition: matpackI.h:2373
test30
void test30()
Definition: test_matpack.cc:494
Vector::resize
void resize(Index n)
Resize function.
Definition: matpackI.h:1467
by_reference
Numeric by_reference(const Numeric &x)
Definition: test_matpack.cc:31
ConstMatrixView::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackI.h:1491
test25
void test25()
Definition: test_matpack.cc:438
array.h
This file contains the definition of Array.
mult
void mult(VectorView y, const ConstMatrixView &M, const ConstVectorView &x)
Matrix Vector multiplication.
Definition: matpackI.h:2291
ConstTensor3View::npages
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:392
joker
Joker joker
Define the global joker objekt.
Definition: test_matpack.cc:29
test6
void test6()
Definition: test_matpack.cc:220
matpackI.h
Array< String >
test2
void test2()
Definition: test_matpack.cc:135
ConstMatrixView::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackI.h:1497
test26
void test26()
Definition: test_matpack.cc:445
my_basic_string< char >
test21
void test21()
Definition: test_matpack.cc:388
test1
int test1()
Definition: test_matpack.cc:51
test22
void test22()
Definition: test_matpack.cc:401
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: arts.h:147
ConstVectorView::nelem
Index nelem() const
Returns the number of elements.
Definition: matpackI.h:937
VectorView
The VectorView class.
Definition: matpackI.h:281
ConstVectorView::sum
Numeric sum() const
The sum of all elements of a Vector.
Definition: matpackI.h:943
test10
void test10()
Definition: test_matpack.cc:267
test17
void test17()
Definition: test_matpack.cc:354
matpackIII.h
math_funcs.h
Contains declerations of basic mathematical and vector/matrix functions.
make_array.h
Implements the class MakeArray, which is a derived class of Array, allowing explicit initialization.
make_vector.h
The class MakeVector is a special kind of Vector that can be initialized explicitly from one or more ...
Matrix::resize
void resize(Index r, Index c)
Resize function.
Definition: matpackI.h:2237
VectorView::begin
ConstIterator1D begin() const
Return const iterator to first element.
Definition: matpackI.h:1095
ConstTensor3View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:398
max
#define max(a, b)
Definition: continua.cc:12949
test14
void test14()
Definition: test_matpack.cc:320
test27
void test27()
Definition: test_matpack.cc:452
test29
void test29()
Definition: test_matpack.cc:469
MakeVector
Definition: make_vector.h:42
Range
The range class.
Definition: matpackI.h:139
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: arts.h:153
test28
void test28()
Definition: test_matpack.cc:461
Joker
Implementation of Matrix, Vector, and such stuff.
Definition: matpackI.h:111
ConstTensor3View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:404
test7
void test7()
Definition: test_matpack.cc:243
min
#define min(a, b)
Definition: continua.cc:12948
test24
void test24()
Definition: test_matpack.cc:423
test9
void test9()
Definition: test_matpack.cc:259
test16
void test16()
Definition: test_matpack.cc:340
test5
void test5()
Definition: test_matpack.cc:199
MakeArray
Explicit construction of Arrays.
Definition: make_array.h:52
fill_with_junk
void fill_with_junk(VectorView x)
Definition: test_matpack.cc:41
test31
void test31()
Definition: test_matpack.cc:516
test4
void test4()
Definition: test_matpack.cc:170
test12
void test12()
Definition: test_matpack.cc:290
by_value
Numeric by_value(Numeric x)
Definition: test_matpack.cc:36
Vector
The Vector class.
Definition: matpackI.h:389
test11
void test11()
Definition: test_matpack.cc:278
test15
void test15()
Definition: test_matpack.cc:331
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:115
mystring.h
This file contains the definition of String, the ARTS string class.
test8
void test8()
Definition: test_matpack.cc:251
test23
void test23()
Definition: test_matpack.cc:414