ARTS  2.4.0(git:4fb77825)
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 
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 }
is_same_within_epsilon
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
is_diagonal
bool is_diagonal(ConstMatrixView A)
Checks if a square matrix is diagonal.
Definition: logic.cc:323
ConstTensor7View
A constant view of a Tensor7.
Definition: matpackVII.h:147
temp
#define temp
Definition: legacy_continua.cc:20951
abs
#define abs(x)
Definition: legacy_continua.cc:20626
ARTS::Var::y
Vector y(Workspace &ws) noexcept
Definition: autoarts.h:7401
is_singular
bool is_singular(ConstMatrixView A)
Checks if a square matrix is singular.
Definition: logic.cc:295
ConstMatrixView::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:429
is_bool
bool is_bool(const Index &x)
Checks if a variable equals 0 or 1.
Definition: logic.cc:50
is_size
bool is_size(ConstVectorView x, const Index &n)
Verifies that the size of x is l.
Definition: logic.cc:79
ConstTensor4View
A constant view of a Tensor4.
Definition: matpackIV.h:133
Array
This can be used to make arrays out of anything.
Definition: array.h:108
is_decreasing
bool is_decreasing(ConstVectorView x)
Checks if a vector is sorted in reversed order and is strictly decreasing.
Definition: logic.cc:252
ConstMatrixView::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:432
ConstVectorView::nelem
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:51
ConstTensor6View
A constant view of a Tensor6.
Definition: matpackVI.h:149
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
precision
#define precision
Definition: logic.cc:43
is_lon_cyclic
bool is_lon_cyclic(ConstVectorView grid, const Numeric &epsilon)
Check if the given longitude grid is cyclic.
Definition: logic.cc:369
max
#define max(a, b)
Definition: legacy_continua.cc:20629
ConstMatrixView
A constant view of a Matrix.
Definition: matpackI.h:982
is_increasing
bool is_increasing(ConstVectorView x)
Checks if a vector is sorted and strictly increasing.
Definition: logic.cc:215
logic.h
Header file for logic.cc.
is_multiple
bool is_multiple(const Index &x, const Index &y)
Checks if an integer is a multiple of another integer.
Definition: logic.cc:65
ConstTensor3View
A constant view of a Tensor3.
Definition: matpackIII.h:132
ARTS::Var::x
Vector x(Workspace &ws) noexcept
Definition: autoarts.h:7346
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
is_unique
bool is_unique(const ArrayOfIndex &x)
Checks if an ArrayOfIndex is unique, i.e., has no duplicate values.
Definition: logic.cc:274
is_sorted
bool is_sorted(ConstVectorView x)
Checks if a vector is sorted in ascending order.
Definition: logic.cc:199
ConstVectorView
A constant view of a Vector.
Definition: matpackI.h:476
sorting.h
Contains sorting routines.
ConstTensor5View
A constant view of a Tensor5.
Definition: matpackV.h:143