ARTS  2.3.1285(git:92a29ea9-dirty)
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 
50 bool is_bool(const Index& x) { return (x == 0 || x == 1); }
51 
53 
65 bool is_multiple(const Index& x, const Index& y) {
66  assert(y != 0);
67  return (0 == fmod(Numeric(x), Numeric(y)));
68 }
69 
71 
79 bool is_size(ConstVectorView x, const Index& n) { return (n == x.nelem()); }
80 
82 
88 bool 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 
236 bool is_increasing(const ArrayOfIndex& x) {
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 
274 bool 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  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  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 
369 bool is_lon_cyclic(ConstVectorView grid, const Numeric& epsilon) {
370  return is_same_within_epsilon(
371  grid[grid.nelem() - 1] - grid[0], 360., epsilon);
372 }
Index npages() const
Returns the number of pages.
Definition: matpackV.cc:50
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
#define precision
Definition: logic.cc:43
bool is_lon_cyclic(ConstVectorView grid, const Numeric &epsilon)
Check if the given longitude grid is cyclic.
Definition: logic.cc:369
Index nrows() const
Returns the number of rows.
Definition: matpackV.cc:53
Index nelem() const
Number of elements.
Definition: array.h:195
A constant view of a Tensor7.
Definition: matpackVII.h:147
#define abs(x)
Index nvitrines() const
Returns the number of vitrines.
Definition: matpackVI.cc:42
A constant view of a Tensor6.
Definition: matpackVI.h:149
bool is_increasing(ConstVectorView x)
Checks if a vector is sorted and strictly increasing.
Definition: logic.cc:215
bool is_multiple(const Index &x, const Index &y)
Checks if an integer is a multiple of another integer.
Definition: logic.cc:65
Index nrows() const
Returns the number of rows.
Definition: matpackVII.cc:57
Index npages() const
Returns the number of pages.
Definition: matpackIV.cc:60
Index nbooks() const
Returns the number of books.
Definition: matpackV.cc:47
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:147
Index nrows() const
Returns the number of rows.
Definition: matpackIV.cc:63
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:51
Contains sorting routines.
A constant view of a Tensor4.
Definition: matpackIV.h:133
bool is_unique(const ArrayOfIndex &x)
Checks if an ArrayOfIndex is unique, i.e., has no duplicate values.
Definition: logic.cc:274
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:432
Index nshelves() const
Returns the number of shelves.
Definition: matpackV.cc:44
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_sorted(ConstVectorView x)
Checks if a vector is sorted in ascending order.
Definition: logic.cc:199
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:150
Index nrows() const
Returns the number of rows.
Definition: matpackVI.cc:54
A constant view of a Tensor5.
Definition: matpackV.h:143
bool is_diagonal(ConstMatrixView A)
Checks if a square matrix is diagonal.
Definition: logic.cc:323
#define temp
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
Index nlibraries() const
Returns the number of libraries.
Definition: matpackVII.cc:42
Header file for logic.cc.
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:144
This can be used to make arrays out of anything.
Definition: array.h:40
Index nshelves() const
Returns the number of shelves.
Definition: matpackVI.cc:45
bool is_singular(ConstMatrixView A)
Checks if a square matrix is singular.
Definition: logic.cc:295
Index ncols() const
Returns the number of columns.
Definition: matpackVI.cc:57
#define max(a, b)
A constant view of a Tensor3.
Definition: matpackIII.h:132
A constant view of a Vector.
Definition: matpackI.h:476
Index npages() const
Returns the number of pages.
Definition: matpackVI.cc:51
Index nshelves() const
Returns the number of shelves.
Definition: matpackVII.cc:48
A constant view of a Matrix.
Definition: matpackI.h:982
Index npages() const
Returns the number of pages.
Definition: matpackVII.cc:54
Index nbooks() const
Returns the number of books.
Definition: matpackIV.cc:57
bool is_bool(const Index &x)
Checks if a variable equals 0 or 1.
Definition: logic.cc:50
Index ncols() const
Returns the number of columns.
Definition: matpackV.cc:56
bool is_decreasing(ConstVectorView x)
Checks if a vector is sorted in reversed order and is strictly decreasing.
Definition: logic.cc:252
bool is_size(ConstVectorView x, const Index &n)
Verifies that the size of x is l.
Definition: logic.cc:79
Index nvitrines() const
Returns the number of vitrines.
Definition: matpackVII.cc:45
Index ncols() const
Returns the number of columns.
Definition: matpackVII.cc:60
Index ncols() const
Returns the number of columns.
Definition: matpackIV.cc:66
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:429
Index nbooks() const
Returns the number of books.
Definition: matpackVII.cc:51
Index nbooks() const
Returns the number of books.
Definition: matpackVI.cc:48