00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00035 #include <cmath>
00036 #include <algorithm>
00037 #include <stdexcept>
00038 #include "logic.h"
00039 #include "sorting.h"
00040
00041
00042
00043
00044
00045 #define precision 0.
00046
00047
00049
00053 bool is_bool( const Index& x )
00054 {
00055 return ( x==0 || x==1 );
00056 }
00057
00058
00059
00061
00073 bool is_multiple( const Index& x, const Index& y )
00074 {
00075 assert( y != 0 );
00076 return ( 0 == fmod( Numeric(x), Numeric(y) ) );
00077 }
00078
00079
00080
00082
00090 bool is_size( ConstVectorView x,
00091 const Index& n )
00092 {
00093 return( n == x.nelem() );
00094 }
00095
00097
00103 bool is_size( ConstMatrixView x,
00104 const Index& r,
00105 const Index& c )
00106 {
00107 return( r == x.nrows() &&
00108 c == x.ncols() );
00109 }
00110
00112
00119 bool is_size( ConstTensor3View x,
00120 const Index& p,
00121 const Index& r,
00122 const Index& c )
00123 {
00124 return( p == x.npages() &&
00125 r == x.nrows() &&
00126 c == x.ncols() );
00127 }
00128
00130
00138 bool is_size( ConstTensor4View x,
00139 const Index& b,
00140 const Index& p,
00141 const Index& r,
00142 const Index& c )
00143 {
00144 return( b == x.nbooks() &&
00145 p == x.npages() &&
00146 r == x.nrows() &&
00147 c == x.ncols() );
00148 }
00149
00151
00160 bool is_size( ConstTensor5View x,
00161 const Index& s,
00162 const Index& b,
00163 const Index& p,
00164 const Index& r,
00165 const Index& c )
00166 {
00167 return( s == x.nshelves() &&
00168 b == x.nbooks() &&
00169 p == x.npages() &&
00170 r == x.nrows() &&
00171 c == x.ncols() );
00172 }
00173
00175
00185 bool is_size( ConstTensor6View x,
00186 const Index& v,
00187 const Index& s,
00188 const Index& b,
00189 const Index& p,
00190 const Index& r,
00191 const Index& c )
00192 {
00193 return( v == x.nvitrines() &&
00194 s == x.nshelves() &&
00195 b == x.nbooks() &&
00196 p == x.npages() &&
00197 r == x.nrows() &&
00198 c == x.ncols() );
00199 }
00200
00202
00213 bool is_size( ConstTensor7View x,
00214 const Index& l,
00215 const Index& v,
00216 const Index& s,
00217 const Index& b,
00218 const Index& p,
00219 const Index& r,
00220 const Index& c )
00221 {
00222 return( l == x.nlibraries() &&
00223 v == x.nvitrines() &&
00224 s == x.nshelves() &&
00225 b == x.nbooks() &&
00226 p == x.npages() &&
00227 r == x.nrows() &&
00228 c == x.ncols() );
00229 }
00230
00232
00238 bool is_sorted( ConstVectorView x )
00239 {
00240 if( x.nelem() > 1 )
00241 {
00242 for( Index i=1; i<x.nelem(); i++ )
00243 {
00244 if( x[i] < x[i-1] )
00245 return false;
00246 }
00247 }
00248 return true;
00249 }
00250
00252
00258 bool is_increasing( ConstVectorView x )
00259 {
00260 if( x.nelem() > 1 )
00261 {
00262 for( Index i=1; i<x.nelem(); i++ )
00263 {
00264 if( x[i] <= x[i-1] )
00265 return false;
00266 }
00267 }
00268 return true;
00269 }
00270
00272
00283 bool is_increasing( const ArrayOfIndex& x )
00284 {
00285 if( x.nelem() > 1 )
00286 {
00287 for( Index i=1; i<x.nelem(); i++ )
00288 {
00289 if( x[i] <= x[i-1] )
00290 return false;
00291 }
00292 }
00293 return true;
00294 }
00295
00297
00303 bool is_decreasing( ConstVectorView x )
00304 {
00305 if( x.nelem() > 1 )
00306 {
00307 for( Index i=1; i<x.nelem(); i++ )
00308 {
00309 if( x[i] >= x[i-1] )
00310 return false;
00311 }
00312 }
00313 return true;
00314 }
00315
00316
00318
00330 bool is_unique( const ArrayOfIndex& x )
00331 {
00332
00333
00334
00335 for (Index i=1; i<x.nelem(); ++i)
00336 for (Index s=0; s<i; ++s)
00337 if (x[i]==x[s])
00338 return false;
00339
00340 return true;
00341 }
00342
00344
00353 bool is_singular( ConstMatrixView A )
00354 {
00355 assert( A.nrows() == A.ncols() );
00356 Numeric temp = 0.;
00357
00358 for( Index i=0; i<A.nrows(); i++)
00359 {
00360 Numeric big = 0.;
00361 for( Index j=0; j<A.nrows(); j++)
00362 {
00363 if ((temp = fabs(A(i,j))) > big)
00364 big = temp;
00365 }
00366
00367 if (big < precision)
00368 {
00369 throw runtime_error ("Matrix is singular.");
00370 return true;
00371 }
00372 }
00373 return false;
00374 }
00375
00376
00378
00387 bool is_diagonal( ConstMatrixView A )
00388 {
00389 assert( A.nrows() == A.ncols() );
00390
00391 for( Index i=1; i<A.ncols(); i++ )
00392 {
00393 for( Index j=0; j<i; j++ )
00394 {
00395 if( fabs(A(i,j)) > precision ||
00396 fabs(A(j,i)) > precision )
00397 return false;
00398 }
00399 }
00400 return true;
00401 }
00402
00404
00421 bool is_same_within_epsilon( const Numeric& a,
00422 const Numeric& b,
00423 const Numeric& epsilon )
00424 {
00425 if ( abs(a-b) <= abs( epsilon * max(a,b) ) )
00426 return true;
00427 else
00428 return false;
00429 }