ARTS  2.4.0(git:4fb77825)
test_utils.h
Go to the documentation of this file.
1 /* Copyright (C) 2001-2012 Stefan Buehler <sbuehler@ltu.se>
2  This program is free software; you can redistribute it and/or modify it
3  under the terms of the GNU General Public License as published by the
4  Free Software Foundation; either version 2, or (at your option) any
5  later version.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
15  USA. */
16 
25 #ifndef test_utils_h
26 #define test_utils_h
27 
28 #include <stdlib.h>
29 #include <time.h>
30 #include "complex.h"
31 #include "matpackI.h"
32 
40 template <class rand_type>
41 class Rand {
42  public:
43  Rand(rand_type lo, rand_type hi) : low(lo), range(hi - lo) { srand(rand()); }
44 
45  rand_type operator()() const {
46  rand_type r =
47  (rand_type)(((Numeric)rand()) / ((Numeric)RAND_MAX) * (Numeric)range);
48  return low + r;
49  }
50 
56  private:
57  rand_type low, range;
58 };
59 
60 template <>
61 class Rand<Index> {
62  public:
63  Rand(Index lo, Index hi) : low(lo), range(hi - lo) {
64  // Avoid negative ranges.
65  if (hi <= lo) range = 0;
66  srand(rand());
67  }
68 
69  Index operator()() const { return low + rand() % (range + 1); }
70 
71  private:
73 };
74 
75 // Add noise to vector.
76 void add_noise(VectorView v, Numeric range);
77 
78 // Fill matrix with random values.
79 void random_fill_matrix(MatrixView A, Numeric range, bool positive);
80 void random_fill_matrix(ComplexMatrixView A, Numeric range, bool positive);
81 
82 // Fill sparse matrix with random values.
83 void random_fill_matrix(Sparse& A, Numeric range, bool positive);
84 
85 // Fill a dense and a sparse matrix with the identical, random values.
86 void random_fill_matrix(Matrix& A, Sparse& B, Numeric range, bool positive);
87 
88 // Fill matrix with random values symmetrically.
89 void random_fill_matrix_symmetric(MatrixView A, Numeric range, bool positive);
91  Numeric range,
92  bool positive);
93 
94 // Generate random, positive semi-definite matrix.
95 void random_fill_matrix_pos_def(MatrixView A, Numeric range, bool positive);
96 
97 // Generate random, positive semi-definite matrix.
99  Numeric range,
100  bool positive);
101 
102 // Fill vector with random values.
103 void random_fill_vector(VectorView A, Numeric range, bool positive);
104 
105 // Pick random submatrix.
107 
108 // Generate random range in the range [0, n - 1]
110 
111 // Maximum element-wise error of two matrices.
113  ConstMatrixView A2,
114  bool relative);
115 
118  bool relative);
119 
120 // Maximum element-wise error of two matrices.
122  ConstVectorView v2,
123  bool relative);
124 
125 #endif // test_utils_h
Matrix
The Matrix class.
Definition: matpackI.h:1193
MatrixView
The MatrixView class.
Definition: matpackI.h:1093
ARTS::Var::lo
Numeric lo(Workspace &ws) noexcept
Definition: autoarts.h:4039
Rand::Rand
Rand(rand_type lo, rand_type hi)
Definition: test_utils.h:43
random_fill_vector
void random_fill_vector(VectorView A, Numeric range, bool positive)
Fill vector with random values.
Definition: test_utils.cc:230
Rand::operator()
rand_type operator()() const
Definition: test_utils.h:45
random_fill_matrix_symmetric
void random_fill_matrix_symmetric(MatrixView A, Numeric range, bool positive)
Generate random, symmetric matrix.
Definition: test_utils.cc:156
Sparse
The Sparse class.
Definition: matpackII.h:60
Rand< Index >::operator()
Index operator()() const
Definition: test_utils.h:69
matpackI.h
Implementation of Matrix, Vector, and such stuff.
complex.h
A class implementing complex numbers for ARTS.
random_fill_matrix_pos_semi_def
void random_fill_matrix_pos_semi_def(MatrixView A, Numeric range, bool positive)
Generate random, positive semi-definite matrix.
Definition: test_utils.cc:210
Rand< Index >::Rand
Rand(Index lo, Index hi)
Definition: test_utils.h:63
Rand::low
rand_type low
Random Index class.
Definition: test_utils.h:57
VectorView
The VectorView class.
Definition: matpackI.h:610
add_noise
void add_noise(VectorView v, Numeric range)
Add noise to vector.
Definition: test_utils.cc:38
random_submatrix
MatrixView random_submatrix(MatrixView A, Index m, Index n)
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
ConstMatrixView
A constant view of a Matrix.
Definition: matpackI.h:982
Rand
Random number class.
Definition: test_utils.h:41
ComplexMatrixView
The ComplexMatrixView class.
Definition: complex.h:731
Range
The range class.
Definition: matpackI.h:160
random_fill_matrix
void random_fill_matrix(MatrixView A, Numeric range, bool positive)
Fill matrix with random values.
Definition: test_utils.cc:59
random_fill_matrix_pos_def
void random_fill_matrix_pos_def(MatrixView A, Numeric range, bool positive)
Generate random, positive definite matrix.
Definition: test_utils.cc:181
ConstComplexMatrixView
A constant view of a ComplexMatrix.
Definition: complex.h:618
random_range
Range random_range(Index n)
Generate random sub-range of the range [0, n-1].
Definition: test_utils.cc:273
Rand< Index >::range
Index range
Definition: test_utils.h:72
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Rand::range
rand_type range
Definition: test_utils.h:57
ConstVectorView
A constant view of a Vector.
Definition: matpackI.h:476
get_maximum_error
Numeric get_maximum_error(ConstMatrixView A1, ConstMatrixView A2, bool relative)
Maximum element-wise error of two matrices.
Definition: test_utils.cc:335