ARTS 2.5.10 (git: 2f1c442c)
logic.cc
Go to the documentation of this file.
1/* Copyright (C) 2002-2012 Stefan Buehler <sbuehler@ltu.se>
2
3 This program is free software; you can redistribute it and/or modify it
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
34#include "logic.h"
35
36#include <algorithm>
37#include <cmath>
38#include <limits>
39#include <stdexcept>
40
41#include "sorting.h"
42
43// For checking, if a Numeric equal zero we have to take into account the
44// numerical precicion. If a value is smaller than *precision* it is
45// taken to be 0.
46#define precision 0.
47
49
53bool is_bool(const Index& x) { return (x == 0 || x == 1); }
54
56
68bool is_multiple(const Index& x, const Index& y) {
69 ARTS_ASSERT(y != 0);
70 return (0 == fmod(Numeric(x), Numeric(y)));
71}
72
74
82bool is_size(ConstVectorView x, const Index& n) { return (n == x.nelem()); }
83
85
91bool is_size(ConstMatrixView x, const Index& r, const Index& c) {
92 return (r == x.nrows() && c == x.ncols());
93}
94
96
104 const Index& p,
105 const Index& r,
106 const Index& c) {
107 return (p == x.npages() && r == x.nrows() && c == x.ncols());
108}
109
111
120 const Index& b,
121 const Index& p,
122 const Index& r,
123 const Index& c) {
124 return (b == x.nbooks() && p == x.npages() && r == x.nrows() &&
125 c == x.ncols());
126}
127
129
139 const Index& s,
140 const Index& b,
141 const Index& p,
142 const Index& r,
143 const Index& c) {
144 return (s == x.nshelves() && b == x.nbooks() && p == x.npages() &&
145 r == x.nrows() && c == x.ncols());
146}
147
149
160 const Index& v,
161 const Index& s,
162 const Index& b,
163 const Index& p,
164 const Index& r,
165 const Index& c) {
166 return (v == x.nvitrines() && s == x.nshelves() && b == x.nbooks() &&
167 p == x.npages() && r == x.nrows() && c == x.ncols());
168}
169
171
183 const Index& l,
184 const Index& v,
185 const Index& s,
186 const Index& b,
187 const Index& p,
188 const Index& r,
189 const Index& c) {
190 return (l == x.nlibraries() && v == x.nvitrines() && s == x.nshelves() &&
191 b == x.nbooks() && p == x.npages() && r == x.nrows() &&
192 c == x.ncols());
193}
194
196
203 if (x.nelem() > 1) {
204 for (Index i = 1; i < x.nelem(); i++) {
205 if (!(x[i] >= x[i - 1])) return false;
206 }
207 }
208 return true;
209}
210
212
219 if (x.nelem() > 1) {
220 for (Index i = 1; i < x.nelem(); i++) {
221 if (!(x[i] > x[i - 1])) return false;
222 }
223 }
224 return true;
225}
226
228
240 if (x.nelem() > 1) {
241 for (Index i = 1; i < x.nelem(); i++) {
242 if (x[i] <= x[i - 1]) return false;
243 }
244 }
245 return true;
246}
247
249
256 if (x.nelem() > 1) {
257 for (Index i = 1; i < x.nelem(); i++) {
258 if (!(x[i] < x[i - 1])) return false;
259 }
260 }
261 return true;
262}
263
265
277bool is_unique(const ArrayOfIndex& x) {
278 // We simply compare the second element to the first,
279 // the third to the first and second, and so on.
280
281 for (Index i = 1; i < x.nelem(); ++i)
282 for (Index s = 0; s < i; ++s)
283 if (x[i] == x[s]) return false;
284
285 return true;
286}
287
289
299 ARTS_ASSERT(A.nrows() == A.ncols());
300 Numeric temp = 0.;
301
302 for (Index i = 0; i < A.nrows(); i++) {
303 Numeric big = 0.;
304 for (Index j = 0; j < A.nrows(); j++) {
305 if ((temp = fabs(A(i, j))) > big) big = temp;
306 }
307 // Due to numerical precision the values can deviate from 0.0
308 if (big < precision) {
309 throw runtime_error("Matrix is singular.");
310 return true;
311 }
312 }
313 return false;
314}
315
317
327 ARTS_ASSERT(A.nrows() == A.ncols());
328
329 for (Index i = 1; i < A.ncols(); i++) {
330 for (Index j = 0; j < i; j++) {
331 if (fabs(A(i, j)) > precision || fabs(A(j, i)) > precision) return false;
332 }
333 }
334 return true;
335}
336
338
355 const Numeric& b,
356 const Numeric& epsilon) {
357 return abs(a - b) <= epsilon * max(abs(a), abs(b));
358}
359
361
369bool is_lon_cyclic(ConstVectorView grid, const Numeric& epsilon) {
371 grid[grid.nelem() - 1] - grid[0], 360., epsilon);
372}
373
375 const Numeric epsilon) {
376 if (x.size() < 2) return true;
377 const Numeric dx0 = x[1] - x[0];
378 if (dx0 <= 0) return false;
379
380 for (Index i = 2; i < x.size(); i++) {
381 if (Numeric dxi = x[i] - x[i - 1];
382 dxi <= 0 or std::abs(dxi - dx0) >= epsilon) {
383 return false;
384 }
385 }
386 return true;
387}
base max(const Array< base > &x)
Max function.
Definition: array.h:145
Index nelem() const ARTS_NOEXCEPT
Definition: array.h:92
A constant view of a Matrix.
Definition: matpackI.h:1065
Index nrows() const noexcept
Definition: matpackI.h:1079
Index ncols() const noexcept
Definition: matpackI.h:1080
A constant view of a Tensor3.
Definition: matpackIII.h:133
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:145
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:148
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:151
A constant view of a Tensor4.
Definition: matpackIV.h:133
Index ncols() const noexcept
Definition: matpackIV.h:146
Index nrows() const noexcept
Definition: matpackIV.h:145
Index nbooks() const noexcept
Definition: matpackIV.h:143
Index npages() const noexcept
Definition: matpackIV.h:144
A constant view of a Tensor5.
Definition: matpackV.h:144
Index nrows() const noexcept
Definition: matpackV.h:157
Index ncols() const noexcept
Definition: matpackV.h:158
Index npages() const noexcept
Definition: matpackV.h:156
Index nbooks() const noexcept
Definition: matpackV.h:155
Index nshelves() const noexcept
Definition: matpackV.h:154
A constant view of a Tensor6.
Definition: matpackVI.h:150
Index nbooks() const noexcept
Definition: matpackVI.h:160
Index nvitrines() const noexcept
Definition: matpackVI.h:158
Index ncols() const noexcept
Definition: matpackVI.h:163
Index npages() const noexcept
Definition: matpackVI.h:161
Index nshelves() const noexcept
Definition: matpackVI.h:159
Index nrows() const noexcept
Definition: matpackVI.h:162
A constant view of a Tensor7.
Definition: matpackVII.h:148
Index ncols() const noexcept
Definition: matpackVII.h:164
Index npages() const noexcept
Definition: matpackVII.h:162
Index nrows() const noexcept
Definition: matpackVII.h:163
Index nlibraries() const noexcept
Definition: matpackVII.h:158
Index nvitrines() const noexcept
Definition: matpackVII.h:159
Index nshelves() const noexcept
Definition: matpackVII.h:160
Index nbooks() const noexcept
Definition: matpackVII.h:161
A constant view of a Vector.
Definition: matpackI.h:521
Index nelem() const noexcept
Returns the number of elements.
Definition: matpackI.h:547
Index size() const noexcept
Definition: matpackI.h:550
#define ARTS_ASSERT(condition,...)
Definition: debug.h:102
bool is_increasing(ConstVectorView x)
Checks if a vector is sorted and strictly increasing.
Definition: logic.cc:218
bool is_sorted(ConstVectorView x)
Checks if a vector is sorted in ascending order.
Definition: logic.cc:202
bool is_bool(const Index &x)
Checks if a variable equals 0 or 1.
Definition: logic.cc:53
#define precision
Definition: logic.cc:46
bool is_decreasing(ConstVectorView x)
Checks if a vector is sorted in reversed order and is strictly decreasing.
Definition: logic.cc:255
bool is_diagonal(ConstMatrixView A)
Checks if a square matrix is diagonal.
Definition: logic.cc:326
bool is_size(ConstVectorView x, const Index &n)
Verifies that the size of x is l.
Definition: logic.cc:82
bool is_same_within_epsilon(const Numeric &a, const Numeric &b, const Numeric &epsilon)
Check, if two numbers agree within a given epsilon.
Definition: logic.cc:354
bool is_lon_cyclic(ConstVectorView grid, const Numeric &epsilon)
Check if the given longitude grid is cyclic.
Definition: logic.cc:369
bool is_multiple(const Index &x, const Index &y)
Checks if an integer is a multiple of another integer.
Definition: logic.cc:68
bool is_unique(const ArrayOfIndex &x)
Checks if an ArrayOfIndex is unique, i.e., has no duplicate values.
Definition: logic.cc:277
bool is_regularly_increasing_within_epsilon(ConstVectorView x, const Numeric epsilon)
Definition: logic.cc:374
bool is_singular(ConstMatrixView A)
Checks if a square matrix is singular.
Definition: logic.cc:298
void abs(Sparse &A, const Sparse &B)
Absolute value of sparse matrix elements.
Definition: matpackII.cc:394
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Contains sorting routines.
#define v
#define a
#define c
#define b