ARTS  2.3.1285(git:92a29ea9-dirty)
test_utils.cc
Go to the documentation of this file.
1 /* Copyright (C) 2001-2012 Stefan Buehler <sbuehler@ltu.se>
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 
24 #include "test_utils.h"
25 #include <cmath>
26 #include "arts.h"
27 #include "lin_alg.h"
28 #include "matpackII.h"
29 
30 using std::abs;
31 
33 
38 void add_noise(VectorView v, Numeric scale) {
39  Rand<Numeric> rand(0, scale);
40 
41  for (Index i = 0; i < v.nelem(); i++) {
42  v[i] += rand();
43  }
44 }
45 
47 
59 void random_fill_matrix(MatrixView A, Numeric range, bool positive) {
60  Index m = A.nrows();
61  Index n = A.ncols();
62 
63  Rand<Numeric> rand(positive ? 0 : -range, range);
64 
65  for (Index i = 0; i < m; i++) {
66  for (Index j = 0; j < n; j++) {
67  A(i, j) = (Numeric)rand();
68  }
69  }
70 }
71 void random_fill_matrix(ComplexMatrixView A, Numeric range, bool positive) {
72  Index m = A.nrows();
73  Index n = A.ncols();
74 
75  Rand<Numeric> rand(positive ? 0 : -range, range);
76 
77  for (Index i = 0; i < m; i++) {
78  for (Index j = 0; j < n; j++) {
79  A(i, j) = Complex((Numeric)rand(), (Numeric)rand());
80  }
81  }
82 }
83 
85 
96 void random_fill_matrix(Sparse& A, Numeric range, bool positive) {
97  Index m = A.nrows();
98  Index n = A.ncols();
99 
100  Rand<Numeric> random_number(positive ? 0 : -range, range);
101 
102  Index nelem = max(m, n);
103 
104  for (Index i = 0; i < nelem; i++) {
105  Index m1, n1;
106  m1 = rand() % m;
107  n1 = rand() % n;
108 
109  A.rw(m1, n1) = random_number();
110  }
111 }
112 
114 
126 void random_fill_matrix(Matrix& A, Sparse& B, Numeric range, bool positive) {
127  Index m = A.nrows();
128  Index n = A.ncols();
129 
130  assert(B.nrows() == m);
131  assert(B.ncols() == n);
132 
133  Rand<Numeric> random_number(positive ? 0 : -range, range);
134 
135  Index nelem = max(m, n);
136 
137  for (Index i = 0; i < nelem; i++) {
138  Index m1, n1;
139  m1 = rand() % m;
140  n1 = rand() % n;
141 
142  A(m1, n1) = random_number();
143  B.rw(m1, n1) = A(m1, n1);
144  }
145 }
146 
148 
156 void random_fill_matrix_symmetric(MatrixView A, Numeric range, bool positive) {
157  random_fill_matrix(A, range, positive);
158  Matrix M(A);
159  A += transpose(M);
160 }
162  Numeric range,
163  bool positive) {
164  random_fill_matrix(A, range, positive);
165  ComplexMatrix M(A);
166  A += transpose(M);
167 }
168 
170 
181 void random_fill_matrix_pos_def(MatrixView A, Numeric range, bool positive) {
182  Index n = A.ncols();
183 
184  // Ensure that A is square.
185  assert(A.ncols() == A.nrows());
186 
187  // Generate random, pos. semi-def. Matrix
188  random_fill_matrix(A, range, positive);
189  Matrix M(A);
190  mult(A, M, transpose(M));
191 
192  // Add identity matrix.
193  for (Index i = 0; i < n; i++) {
194  A(i, i) += 1.0;
195  }
196 }
197 
199 
211  Numeric range,
212  bool positive) {
213  random_fill_matrix(A, range, positive);
214  Matrix M(A);
215  mult(A, M, transpose(M));
216 }
217 
219 
230 void random_fill_vector(VectorView v, Numeric range, bool positive) {
231  Index n = v.nelem();
232 
233  Rand<Numeric> rand(positive ? 0 : -range, range);
234 
235  for (Index i = 0; i < n; i++) {
236  v[i] = rand();
237  }
238 }
239 
241 
252  Index m0(A.nrows()), n0(A.ncols());
253  assert((0 <= m) && (m <= m0));
254  assert((0 <= n) && (m <= n0));
255 
256  Rand<Index> rand_m(0, (m0 - m - 1)), rand_n(0, (n0 - n - 1));
257  Index m1, n1;
258  m1 = rand_m();
259  n1 = rand_n();
260 
261  Range r1(m1, m), r2(n1, n);
262  return A(r1, r2);
263 }
264 
266 
274  Rand<Index> extent(1, n);
275  Index e = extent();
276 
277  Index s = 0;
278  if (0 <= (n - e - 1)) {
279  Rand<Index> start(0, n - e - 1);
280  s = start();
281  }
282  return Range(s, e);
283 }
284 
286 
298  ConstVectorView v2,
299  bool relative) {
300  Index n = min(v1.nelem(), v2.nelem());
301 
302  Numeric max = 0.0, err = 0.0;
303 
304  for (Index i = 0; i < n; i++) {
305  err = 0.0;
306 
307  if (relative) {
308  if (v2[i] != 0.0) {
309  err = abs((v2[i] - v1[i]) / v2[i]);
310  }
311 
312  } else {
313  err = abs(v2[i] - v2[i]);
314  }
315 
316  if (err > max) {
317  max = err;
318  }
319  }
320  return err;
321 }
322 
324 
336  ConstMatrixView A2,
337  bool relative) {
338  Index m = min(A1.nrows(), A2.nrows());
339  Index n = min(A1.ncols(), A2.ncols());
340 
341  Numeric max = 0.0, err = 0.0;
342 
343  for (Index i = 0; i < m; i++) {
344  for (Index j = 0; j < n; j++) {
345  err = 0.0;
346 
347  if (relative) {
348  if (A2(i, j) != 0.0) {
349  err = abs((A2(i, j) - A1(i, j)) / A2(i, j));
350  }
351 
352  } else {
353  err = A2(i, j) - A1(i, j);
354  }
355 
356  if (err > max) {
357  max = err;
358  }
359  }
360  }
361 
362  return max;
363 }
366  bool relative) {
367  Index m = min(A1.nrows(), A2.nrows());
368  Index n = min(A1.ncols(), A2.ncols());
369 
370  Numeric max = 0.0, err = 0.0;
371 
372  for (Index i = 0; i < m; i++) {
373  for (Index j = 0; j < n; j++) {
374  err = 0.0;
375 
376  if (relative) {
377  if (A2(i, j).real() != 0.0 && A2(i, j).imag() != 0.0) {
378  err = abs((A2(i, j) - A1(i, j)) / A2(i, j));
379  }
380 
381  } else {
382  err = abs(A2(i, j) - A1(i, j));
383  }
384 
385  if (err > max) {
386  max = err;
387  }
388  }
389  }
390 
391  return max;
392 }
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Utility functions for testing.
The VectorView class.
Definition: matpackI.h:610
Range random_range(Index n)
Generate random sub-range of the range [0, n-1].
Definition: test_utils.cc:273
#define M
Definition: rng.cc:165
Random number class.
Definition: test_utils.h:41
Index nrows() const
Definition: complex.h:630
ConstMatrixView real() const
Get a view of the real part of the matrix.
Definition: complex.h:657
#define abs(x)
The MatrixView class.
Definition: matpackI.h:1093
The Sparse class.
Definition: matpackII.h:60
The range class.
Definition: matpackI.h:160
Linear algebra functions.
MatrixView random_submatrix(MatrixView A, int m, int n)
Pick random random submatrix of size m times n.
Definition: test_utils.cc:251
void add_noise(VectorView v, Numeric scale)
Add noise to vector.
Definition: test_utils.cc:38
Index ncols() const
Returns the number of columns.
Definition: matpackII.cc:69
Numeric & rw(Index r, Index c)
Read and write index operator.
Definition: matpackII.cc:91
#define min(a, b)
ConstMatrixView imag() const
Get a view of the imaginary part of the matrix.
Definition: complex.h:661
void random_fill_vector(VectorView v, Numeric range, bool positive)
Fill vector with random values.
Definition: test_utils.cc:230
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:51
void random_fill_matrix_pos_semi_def(MatrixView A, Numeric range, bool positive)
Generate random, positive semi-definite matrix.
Definition: test_utils.cc:210
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:432
The global header file for ARTS.
The ComplexMatrix class.
Definition: complex.h:858
void random_fill_matrix_symmetric(MatrixView A, Numeric range, bool positive)
Generate random, symmetric matrix.
Definition: test_utils.cc:156
Header file for sparse matrices.
void random_fill_matrix(MatrixView A, Numeric range, bool positive)
Fill matrix with random values.
Definition: test_utils.cc:59
std::complex< Numeric > Complex
Definition: complex.h:33
Index nrows() const
Returns the number of rows.
Definition: matpackII.cc:66
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
The Matrix class.
Definition: matpackI.h:1193
void mult(ComplexVectorView y, const ConstComplexMatrixView &M, const ConstComplexVectorView &x)
Matrix-Vector Multiplication.
Definition: complex.cc:1579
ConstComplexMatrixView transpose(ConstComplexMatrixView m)
Const version of transpose.
Definition: complex.cc:1509
#define max(a, b)
A constant view of a Vector.
Definition: matpackI.h:476
Index nelem(const Lines &l)
Number of lines.
constexpr Rational start(Rational Ju, Rational Jl, Polarization type) noexcept
Gives the lowest M for a polarization type of this transition.
Definition: zeemandata.h:77
A constant view of a Matrix.
Definition: matpackI.h:982
A constant view of a ComplexMatrix.
Definition: complex.h:618
Numeric get_maximum_error(ConstVectorView v1, ConstVectorView v2, bool relative)
Maximum element-wise error of two vectors.
Definition: test_utils.cc:297
Index ncols() const
Definition: complex.h:631
void random_fill_matrix_pos_def(MatrixView A, Numeric range, bool positive)
Generate random, positive definite matrix.
Definition: test_utils.cc:181
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:429
The ComplexMatrixView class.
Definition: complex.h:731