ARTS  2.0.49
logic.cc
Go to the documentation of this file.
1 /* Copyright (C) 2002-2008 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 
49 
53 bool is_bool( const Index& x )
54 {
55  return ( x==0 || x==1 );
56 }
57 
58 
59 
61 
73 bool is_multiple( const Index& x, const Index& y )
74 {
75  assert( y != 0 );
76  return ( 0 == fmod( Numeric(x), Numeric(y) ) );
77 }
78 
79 
80 
82 
91  const Index& n )
92 {
93  return( n == x.nelem() );
94 }
95 
97 
104  const Index& r,
105  const Index& c )
106 {
107  return( r == x.nrows() &&
108  c == x.ncols() );
109 }
110 
112 
120  const Index& p,
121  const Index& r,
122  const Index& c )
123 {
124  return( p == x.npages() &&
125  r == x.nrows() &&
126  c == x.ncols() );
127 }
128 
130 
139  const Index& b,
140  const Index& p,
141  const Index& r,
142  const Index& c )
143 {
144  return( b == x.nbooks() &&
145  p == x.npages() &&
146  r == x.nrows() &&
147  c == x.ncols() );
148 }
149 
151 
161  const Index& s,
162  const Index& b,
163  const Index& p,
164  const Index& r,
165  const Index& c )
166 {
167  return( s == x.nshelves() &&
168  b == x.nbooks() &&
169  p == x.npages() &&
170  r == x.nrows() &&
171  c == x.ncols() );
172 }
173 
175 
186  const Index& v,
187  const Index& s,
188  const Index& b,
189  const Index& p,
190  const Index& r,
191  const Index& c )
192 {
193  return( v == x.nvitrines() &&
194  s == x.nshelves() &&
195  b == x.nbooks() &&
196  p == x.npages() &&
197  r == x.nrows() &&
198  c == x.ncols() );
199 }
200 
202 
214  const Index& l,
215  const Index& v,
216  const Index& s,
217  const Index& b,
218  const Index& p,
219  const Index& r,
220  const Index& c )
221 {
222  return( l == x.nlibraries() &&
223  v == x.nvitrines() &&
224  s == x.nshelves() &&
225  b == x.nbooks() &&
226  p == x.npages() &&
227  r == x.nrows() &&
228  c == x.ncols() );
229 }
230 
232 
239 {
240  if( x.nelem() > 1 )
241  {
242  for( Index i=1; i<x.nelem(); i++ )
243  {
244  if( x[i] < x[i-1] )
245  return false;
246  }
247  }
248  return true;
249 }
250 
252 
259 {
260  if( x.nelem() > 1 )
261  {
262  for( Index i=1; i<x.nelem(); i++ )
263  {
264  if( x[i] <= x[i-1] )
265  return false;
266  }
267  }
268  return true;
269 }
270 
272 
283 bool is_increasing( const ArrayOfIndex& x )
284 {
285  if( x.nelem() > 1 )
286  {
287  for( Index i=1; i<x.nelem(); i++ )
288  {
289  if( x[i] <= x[i-1] )
290  return false;
291  }
292  }
293  return true;
294 }
295 
297 
304 {
305  if( x.nelem() > 1 )
306  {
307  for( Index i=1; i<x.nelem(); i++ )
308  {
309  if( x[i] >= x[i-1] )
310  return false;
311  }
312  }
313  return true;
314 }
315 
316 
318 
330 bool is_unique( const ArrayOfIndex& x )
331 {
332  // We simply compare the second element to the first,
333  // the third to the first and second, and so on.
334 
335  for (Index i=1; i<x.nelem(); ++i)
336  for (Index s=0; s<i; ++s)
337  if (x[i]==x[s])
338  return false;
339 
340  return true;
341 }
342 
344 
354 {
355  assert( A.nrows() == A.ncols() );
356  Numeric temp = 0.;
357 
358  for( Index i=0; i<A.nrows(); i++)
359  {
360  Numeric big = 0.;
361  for( Index j=0; j<A.nrows(); j++)
362  {
363  if ((temp = fabs(A(i,j))) > big)
364  big = temp;
365  }
366  // Due to numerical precision the values can deviate from 0.0
367  if (big < precision)
368  {
369  throw runtime_error ("Matrix is singular.");
370  return true;
371  }
372  }
373  return false;
374 }
375 
376 
378 
388 {
389  assert( A.nrows() == A.ncols() );
390 
391  for( Index i=1; i<A.ncols(); i++ )
392  {
393  for( Index j=0; j<i; j++ )
394  {
395  if( fabs(A(i,j)) > precision ||
396  fabs(A(j,i)) > precision )
397  return false;
398  }
399  }
400  return true;
401 }
402 
404 
422  const Numeric& b,
423  const Numeric& epsilon )
424 {
425  if ( abs(a-b) <= abs( epsilon * max(a,b) ) )
426  return true;
427  else
428  return false;
429 }
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:421
ConstTensor7View::nshelves
Index nshelves() const
Returns the number of shelves.
Definition: matpackVII.cc:43
ConstTensor6View::nshelves
Index nshelves() const
Returns the number of shelves.
Definition: matpackVI.cc:37
ConstTensor5View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackV.cc:38
ConstTensor5View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackV.cc:56
ConstTensor6View::npages
Index npages() const
Returns the number of pages.
Definition: matpackVI.cc:49
ConstTensor7View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackVII.cc:67
is_diagonal
bool is_diagonal(ConstMatrixView A)
Checks if a square matrix is diagonal.
Definition: logic.cc:387
ConstTensor7View
A constant view of a Tensor7.
Definition: matpackVII.h:169
ConstTensor6View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackVI.cc:55
is_singular
bool is_singular(ConstMatrixView A)
Checks if a square matrix is singular.
Definition: logic.cc:353
ConstMatrixView::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:796
is_bool
bool is_bool(const Index &x)
Checks if a variable equals 0 or 1.
Definition: logic.cc:53
temp
#define temp
Definition: continua.cc:13409
is_size
bool is_size(ConstVectorView x, const Index &n)
Verifies that the size of x is l.
Definition: logic.cc:90
ConstTensor5View::npages
Index npages() const
Returns the number of pages.
Definition: matpackV.cc:44
ConstTensor3View::npages
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:151
ConstTensor7View::nlibraries
Index nlibraries() const
Returns the number of libraries.
Definition: matpackVII.cc:31
ConstTensor4View
A constant view of a Tensor4.
Definition: matpackIV.h:149
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:303
ConstTensor6View::nvitrines
Index nvitrines() const
Returns the number of vitrines.
Definition: matpackVI.cc:31
ConstMatrixView::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:802
abs
#define abs(x)
Definition: continua.cc:13094
ConstVectorView::nelem
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:175
ConstTensor7View::nvitrines
Index nvitrines() const
Returns the number of vitrines.
Definition: matpackVII.cc:37
ConstTensor4View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackIV.cc:78
ConstTensor7View::npages
Index npages() const
Returns the number of pages.
Definition: matpackVII.cc:55
ConstTensor6View
A constant view of a Tensor6.
Definition: matpackVI.h:167
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
precision
#define precision
Definition: logic.cc:45
ConstTensor4View::npages
Index npages() const
Returns the number of pages.
Definition: matpackIV.cc:66
ConstTensor4View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackIV.cc:60
ConstTensor3View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:154
max
#define max(a, b)
Definition: continua.cc:13097
ConstTensor6View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackVI.cc:43
ConstMatrixView
A constant view of a Matrix.
Definition: matpackI.h:591
ConstTensor4View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackIV.cc:72
ConstTensor7View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackVII.cc:49
ConstTensor5View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackV.cc:50
is_increasing
bool is_increasing(ConstVectorView x)
Checks if a vector is sorted and strictly increasing.
Definition: logic.cc:258
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:73
ConstTensor5View::nshelves
Index nshelves() const
Returns the number of shelves.
Definition: matpackV.cc:32
ConstTensor3View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:157
ConstTensor3View
A constant view of a Tensor3.
Definition: matpackIII.h:147
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
ConstTensor7View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackVII.cc:61
is_unique
bool is_unique(const ArrayOfIndex &x)
Checks if an ArrayOfIndex is unique, i.e., has no duplicate values.
Definition: logic.cc:330
is_sorted
bool is_sorted(ConstVectorView x)
Checks if a vector is sorted in ascending order.
Definition: logic.cc:238
ConstVectorView
A constant view of a Vector.
Definition: matpackI.h:300
sorting.h
Contains sorting routines.
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:172
ConstTensor6View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackVI.cc:61
ConstTensor5View
A constant view of a Tensor5.
Definition: matpackV.h:160