ARTS 2.5.0 (git: 9ee3ac6c)
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
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
34#include "logic.h"
35#include <algorithm>
36#include <cmath>
37#include <stdexcept>
38#include "sorting.h"
39
40// For checking, if a Numeric equal zero we have to take into account the
41// numerical precicion. If a value is smaller than *precision* it is
42// taken to be 0.
43#define precision 0.
44
46
50bool is_bool(const Index& x) { return (x == 0 || x == 1); }
51
53
65bool is_multiple(const Index& x, const Index& y) {
66 ARTS_ASSERT(y != 0);
67 return (0 == fmod(Numeric(x), Numeric(y)));
68}
69
71
79bool is_size(ConstVectorView x, const Index& n) { return (n == x.nelem()); }
80
82
88bool is_size(ConstMatrixView x, const Index& r, const Index& c) {
89 return (r == x.nrows() && c == x.ncols());
90}
91
93
101 const Index& p,
102 const Index& r,
103 const Index& c) {
104 return (p == x.npages() && r == x.nrows() && c == x.ncols());
105}
106
108
117 const Index& b,
118 const Index& p,
119 const Index& r,
120 const Index& c) {
121 return (b == x.nbooks() && p == x.npages() && r == x.nrows() &&
122 c == x.ncols());
123}
124
126
136 const Index& s,
137 const Index& b,
138 const Index& p,
139 const Index& r,
140 const Index& c) {
141 return (s == x.nshelves() && b == x.nbooks() && p == x.npages() &&
142 r == x.nrows() && c == x.ncols());
143}
144
146
157 const Index& v,
158 const Index& s,
159 const Index& b,
160 const Index& p,
161 const Index& r,
162 const Index& c) {
163 return (v == x.nvitrines() && s == x.nshelves() && b == x.nbooks() &&
164 p == x.npages() && r == x.nrows() && c == x.ncols());
165}
166
168
180 const Index& l,
181 const Index& v,
182 const Index& s,
183 const Index& b,
184 const Index& p,
185 const Index& r,
186 const Index& c) {
187 return (l == x.nlibraries() && v == x.nvitrines() && s == x.nshelves() &&
188 b == x.nbooks() && p == x.npages() && r == x.nrows() &&
189 c == x.ncols());
190}
191
193
200 if (x.nelem() > 1) {
201 for (Index i = 1; i < x.nelem(); i++) {
202 if (!(x[i] >= x[i - 1])) return false;
203 }
204 }
205 return true;
206}
207
209
216 if (x.nelem() > 1) {
217 for (Index i = 1; i < x.nelem(); i++) {
218 if (!(x[i] > x[i - 1])) return false;
219 }
220 }
221 return true;
222}
223
225
237 if (x.nelem() > 1) {
238 for (Index i = 1; i < x.nelem(); i++) {
239 if (x[i] <= x[i - 1]) return false;
240 }
241 }
242 return true;
243}
244
246
253 if (x.nelem() > 1) {
254 for (Index i = 1; i < x.nelem(); i++) {
255 if (!(x[i] < x[i - 1])) return false;
256 }
257 }
258 return true;
259}
260
262
274bool is_unique(const ArrayOfIndex& x) {
275 // We simply compare the second element to the first,
276 // the third to the first and second, and so on.
277
278 for (Index i = 1; i < x.nelem(); ++i)
279 for (Index s = 0; s < i; ++s)
280 if (x[i] == x[s]) return false;
281
282 return true;
283}
284
286
296 ARTS_ASSERT(A.nrows() == A.ncols());
297 Numeric temp = 0.;
298
299 for (Index i = 0; i < A.nrows(); i++) {
300 Numeric big = 0.;
301 for (Index j = 0; j < A.nrows(); j++) {
302 if ((temp = fabs(A(i, j))) > big) big = temp;
303 }
304 // Due to numerical precision the values can deviate from 0.0
305 if (big < precision) {
306 throw runtime_error("Matrix is singular.");
307 return true;
308 }
309 }
310 return false;
311}
312
314
324 ARTS_ASSERT(A.nrows() == A.ncols());
325
326 for (Index i = 1; i < A.ncols(); i++) {
327 for (Index j = 0; j < i; j++) {
328 if (fabs(A(i, j)) > precision || fabs(A(j, i)) > precision) return false;
329 }
330 }
331 return true;
332}
333
335
352 const Numeric& b,
353 const Numeric& epsilon) {
354 if (abs(a - b) <= epsilon * max(abs(a), abs(b)))
355 return true;
356 else
357 return false;
358}
359
361
369bool is_lon_cyclic(ConstVectorView grid, const Numeric& epsilon) {
371 grid[grid.nelem() - 1] - grid[0], 360., epsilon);
372}
Index nelem() const ARTS_NOEXCEPT
Number of elements.
Definition: array.h:195
A constant view of a Matrix.
Definition: matpackI.h:1014
Index nrows() const ARTS_NOEXCEPT
Returns the number of rows.
Definition: matpackI.cc:433
Index ncols() const ARTS_NOEXCEPT
Returns the number of columns.
Definition: matpackI.cc:436
A constant view of a Tensor3.
Definition: matpackIII.h:132
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:144
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:147
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:150
A constant view of a Tensor4.
Definition: matpackIV.h:133
Index npages() const
Returns the number of pages.
Definition: matpackIV.cc:58
Index nbooks() const
Returns the number of books.
Definition: matpackIV.cc:55
Index ncols() const
Returns the number of columns.
Definition: matpackIV.cc:64
Index nrows() const
Returns the number of rows.
Definition: matpackIV.cc:61
A constant view of a Tensor5.
Definition: matpackV.h:143
Index ncols() const
Returns the number of columns.
Definition: matpackV.cc:56
Index nshelves() const
Returns the number of shelves.
Definition: matpackV.cc:44
Index npages() const
Returns the number of pages.
Definition: matpackV.cc:50
Index nrows() const
Returns the number of rows.
Definition: matpackV.cc:53
Index nbooks() const
Returns the number of books.
Definition: matpackV.cc:47
A constant view of a Tensor6.
Definition: matpackVI.h:149
Index ncols() const
Returns the number of columns.
Definition: matpackVI.cc:57
Index npages() const
Returns the number of pages.
Definition: matpackVI.cc:51
Index nrows() const
Returns the number of rows.
Definition: matpackVI.cc:54
Index nshelves() const
Returns the number of shelves.
Definition: matpackVI.cc:45
Index nbooks() const
Returns the number of books.
Definition: matpackVI.cc:48
Index nvitrines() const
Returns the number of vitrines.
Definition: matpackVI.cc:42
A constant view of a Tensor7.
Definition: matpackVII.h:147
Index nshelves() const
Returns the number of shelves.
Definition: matpackVII.cc:48
Index nrows() const
Returns the number of rows.
Definition: matpackVII.cc:57
Index nvitrines() const
Returns the number of vitrines.
Definition: matpackVII.cc:45
Index npages() const
Returns the number of pages.
Definition: matpackVII.cc:54
Index ncols() const
Returns the number of columns.
Definition: matpackVII.cc:60
Index nlibraries() const
Returns the number of libraries.
Definition: matpackVII.cc:42
Index nbooks() const
Returns the number of books.
Definition: matpackVII.cc:51
A constant view of a Vector.
Definition: matpackI.h:489
Index nelem() const ARTS_NOEXCEPT
Returns the number of elements.
Definition: matpackI.cc:48
#define ARTS_ASSERT(condition,...)
Definition: debug.h:83
#define abs(x)
#define temp
#define max(a, b)
bool is_increasing(ConstVectorView x)
Checks if a vector is sorted and strictly increasing.
Definition: logic.cc:215
bool is_sorted(ConstVectorView x)
Checks if a vector is sorted in ascending order.
Definition: logic.cc:199
bool is_bool(const Index &x)
Checks if a variable equals 0 or 1.
Definition: logic.cc:50
#define precision
Definition: logic.cc:43
bool is_decreasing(ConstVectorView x)
Checks if a vector is sorted in reversed order and is strictly decreasing.
Definition: logic.cc:252
bool is_diagonal(ConstMatrixView A)
Checks if a square matrix is diagonal.
Definition: logic.cc:323
bool is_size(ConstVectorView x, const Index &n)
Verifies that the size of x is l.
Definition: logic.cc:79
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:351
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:65
bool is_unique(const ArrayOfIndex &x)
Checks if an ArrayOfIndex is unique, i.e., has no duplicate values.
Definition: logic.cc:274
bool is_singular(ConstMatrixView A)
Checks if a square matrix is singular.
Definition: logic.cc:295
Header file for logic.cc.
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
constexpr Numeric l(const Index p0, const Index n, const Numeric x, const SortedVectorType &xi, const Index j, const std::pair< Numeric, Numeric > cycle={ -180, 180}) noexcept
#define v
#define a
#define c
#define b
Contains sorting routines.