ARTS  2.2.66
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 
35 #include <cmath>
36 #include <algorithm>
37 #include <stdexcept>
38 #include "logic.h"
39 #include "sorting.h"
40 
41 
42 // For checking, if a Numeric equal zero we have to take into account the
43 // numerical precicion. If a value is smaller than *precision* it is
44 // taken to be 0.
45 #define precision 0.
46 
47 
48 
50 
54 bool is_bool( const Index& x )
55 {
56  return ( x==0 || x==1 );
57 }
58 
59 
60 
62 
74 bool is_multiple( const Index& x, const Index& y )
75 {
76  assert( y != 0 );
77  return ( 0 == fmod( Numeric(x), Numeric(y) ) );
78 }
79 
80 
81 
83 
92  const Index& n )
93 {
94  return( n == x.nelem() );
95 }
96 
97 
98 
100 
107  const Index& r,
108  const Index& c )
109 {
110  return( r == x.nrows() &&
111  c == x.ncols() );
112 }
113 
114 
115 
117 
125  const Index& p,
126  const Index& r,
127  const Index& c )
128 {
129  return( p == x.npages() &&
130  r == x.nrows() &&
131  c == x.ncols() );
132 }
133 
134 
135 
137 
146  const Index& b,
147  const Index& p,
148  const Index& r,
149  const Index& c )
150 {
151  return( b == x.nbooks() &&
152  p == x.npages() &&
153  r == x.nrows() &&
154  c == x.ncols() );
155 }
156 
157 
158 
160 
170  const Index& s,
171  const Index& b,
172  const Index& p,
173  const Index& r,
174  const Index& c )
175 {
176  return( s == x.nshelves() &&
177  b == x.nbooks() &&
178  p == x.npages() &&
179  r == x.nrows() &&
180  c == x.ncols() );
181 }
182 
183 
184 
186 
197  const Index& v,
198  const Index& s,
199  const Index& b,
200  const Index& p,
201  const Index& r,
202  const Index& c )
203 {
204  return( v == x.nvitrines() &&
205  s == x.nshelves() &&
206  b == x.nbooks() &&
207  p == x.npages() &&
208  r == x.nrows() &&
209  c == x.ncols() );
210 }
211 
212 
213 
215 
227  const Index& l,
228  const Index& v,
229  const Index& s,
230  const Index& b,
231  const Index& p,
232  const Index& r,
233  const Index& c )
234 {
235  return( l == x.nlibraries() &&
236  v == x.nvitrines() &&
237  s == x.nshelves() &&
238  b == x.nbooks() &&
239  p == x.npages() &&
240  r == x.nrows() &&
241  c == x.ncols() );
242 }
243 
244 
245 
247 
254 {
255  if( x.nelem() > 1 )
256  {
257  for( Index i=1; i<x.nelem(); i++ )
258  {
259  if( !(x[i] >= x[i-1]) )
260  return false;
261  }
262  }
263  return true;
264 }
265 
266 
267 
269 
276 {
277  if( x.nelem() > 1 )
278  {
279  for( Index i=1; i<x.nelem(); i++ )
280  {
281  if( !(x[i] > x[i-1]) )
282  return false;
283  }
284  }
285  return true;
286 }
287 
288 
289 
291 
302 bool is_increasing( const ArrayOfIndex& x )
303 {
304  if( x.nelem() > 1 )
305  {
306  for( Index i=1; i<x.nelem(); i++ )
307  {
308  if( x[i] <= x[i-1] )
309  return false;
310  }
311  }
312  return true;
313 }
314 
315 
316 
318 
325 {
326  if( x.nelem() > 1 )
327  {
328  for( Index i=1; i<x.nelem(); i++ )
329  {
330  if( !(x[i] < x[i-1]) )
331  return false;
332  }
333  }
334  return true;
335 }
336 
337 
338 
340 
352 bool is_unique( const ArrayOfIndex& x )
353 {
354  // We simply compare the second element to the first,
355  // the third to the first and second, and so on.
356 
357  for (Index i=1; i<x.nelem(); ++i)
358  for (Index s=0; s<i; ++s)
359  if (x[i]==x[s])
360  return false;
361 
362  return true;
363 }
364 
365 
366 
368 
378 {
379  assert( A.nrows() == A.ncols() );
380  Numeric temp = 0.;
381 
382  for( Index i=0; i<A.nrows(); i++)
383  {
384  Numeric big = 0.;
385  for( Index j=0; j<A.nrows(); j++)
386  {
387  if ((temp = fabs(A(i,j))) > big)
388  big = temp;
389  }
390  // Due to numerical precision the values can deviate from 0.0
391  if (big < precision)
392  {
393  throw runtime_error ("Matrix is singular.");
394  return true;
395  }
396  }
397  return false;
398 }
399 
400 
402 
412 {
413  assert( A.nrows() == A.ncols() );
414 
415  for( Index i=1; i<A.ncols(); i++ )
416  {
417  for( Index j=0; j<i; j++ )
418  {
419  if( fabs(A(i,j)) > precision ||
420  fabs(A(j,i)) > precision )
421  return false;
422  }
423  }
424  return true;
425 }
426 
427 
428 
430 
447  const Numeric& b,
448  const Numeric& epsilon )
449 {
450  if ( abs(a-b) <= epsilon * max(abs(a),abs(b)) )
451  return true;
452  else
453  return false;
454 }
455 
456 
458 
467  const Numeric& epsilon )
468 {
469  return is_same_within_epsilon(grid[grid.nelem()-1] - grid[0],
470  360., epsilon);
471 }
472 
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:446
ConstTensor7View::nshelves
Index nshelves() const
Returns the number of shelves.
Definition: matpackVII.cc:44
ConstTensor6View::nshelves
Index nshelves() const
Returns the number of shelves.
Definition: matpackVI.cc:38
ConstTensor5View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackV.cc:41
ConstTensor5View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackV.cc:59
ConstTensor6View::npages
Index npages() const
Returns the number of pages.
Definition: matpackVI.cc:50
ConstTensor7View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackVII.cc:68
is_diagonal
bool is_diagonal(ConstMatrixView A)
Checks if a square matrix is diagonal.
Definition: logic.cc:411
ConstTensor7View
A constant view of a Tensor7.
Definition: matpackVII.h:162
ConstTensor6View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackVI.cc:56
is_singular
bool is_singular(ConstMatrixView A)
Checks if a square matrix is singular.
Definition: logic.cc:377
ConstMatrixView::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:832
is_bool
bool is_bool(const Index &x)
Checks if a variable equals 0 or 1.
Definition: logic.cc:54
temp
#define temp
Definition: continua.cc:20773
is_size
bool is_size(ConstVectorView x, const Index &n)
Verifies that the size of x is l.
Definition: logic.cc:91
ConstTensor5View::npages
Index npages() const
Returns the number of pages.
Definition: matpackV.cc:47
ConstTensor3View::npages
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:143
ConstTensor7View::nlibraries
Index nlibraries() const
Returns the number of libraries.
Definition: matpackVII.cc:32
ConstTensor4View
A constant view of a Tensor4.
Definition: matpackIV.h:141
Array< Index >
is_decreasing
bool is_decreasing(ConstVectorView x)
Checks if a vector is sorted in reversed order and is strictly decreasing.
Definition: logic.cc:324
ConstTensor6View::nvitrines
Index nvitrines() const
Returns the number of vitrines.
Definition: matpackVI.cc:32
ConstMatrixView::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:838
abs
#define abs(x)
Definition: continua.cc:20458
ConstVectorView::nelem
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:180
ConstTensor7View::nvitrines
Index nvitrines() const
Returns the number of vitrines.
Definition: matpackVII.cc:38
ConstTensor4View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackIV.cc:81
ConstTensor7View::npages
Index npages() const
Returns the number of pages.
Definition: matpackVII.cc:56
ConstTensor6View
A constant view of a Tensor6.
Definition: matpackVI.h:159
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:29
precision
#define precision
Definition: logic.cc:45
ConstTensor4View::npages
Index npages() const
Returns the number of pages.
Definition: matpackIV.cc:69
ConstTensor4View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackIV.cc:63
is_lon_cyclic
bool is_lon_cyclic(ConstVectorView grid, const Numeric &epsilon)
Check if the given longitude grid is cyclic.
Definition: logic.cc:466
ConstTensor3View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:146
max
#define max(a, b)
Definition: continua.cc:20461
ConstTensor6View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackVI.cc:44
ConstMatrixView
A constant view of a Matrix.
Definition: matpackI.h:596
ConstTensor4View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackIV.cc:75
ConstTensor7View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackVII.cc:50
ConstTensor5View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackV.cc:53
is_increasing
bool is_increasing(ConstVectorView x)
Checks if a vector is sorted and strictly increasing.
Definition: logic.cc:275
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:74
ConstTensor5View::nshelves
Index nshelves() const
Returns the number of shelves.
Definition: matpackV.cc:35
ConstTensor3View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:149
ConstTensor3View
A constant view of a Tensor3.
Definition: matpackIII.h:139
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
ConstTensor7View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackVII.cc:62
is_unique
bool is_unique(const ArrayOfIndex &x)
Checks if an ArrayOfIndex is unique, i.e., has no duplicate values.
Definition: logic.cc:352
is_sorted
bool is_sorted(ConstVectorView x)
Checks if a vector is sorted in ascending order.
Definition: logic.cc:253
ConstVectorView
A constant view of a Vector.
Definition: matpackI.h:292
sorting.h
Contains sorting routines.
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:176
ConstTensor6View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackVI.cc:62
ConstTensor5View
A constant view of a Tensor5.
Definition: matpackV.h:152