00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #ifndef CLIPPER_UTIL
00046 #define CLIPPER_UTIL
00047
00048
00049 #include "clipper_precision.h"
00050 #include "clipper_sysdep.h"
00051
00052
00053 namespace clipper
00054 {
00055
00057
00060 class Util
00061 {
00062 public:
00063 Util();
00064
00065 static const ftype& nan() { return nan_; }
00067 static const float& nanf() { return nanf_; }
00069 static const double& nand() { return nand_; }
00071 inline static void set_null( ftype32& f ) { *((uitype32*)&f) = *((const uitype32*)&nanf_); }
00073 inline static void set_null( ftype64& f ) { *((uitype64*)&f) = *((const uitype64*)&nand_); }
00075 inline static bool is_null( const ftype32& f ) { return ( *((const uitype32*)&f) == *((const uitype32*)&nanf_) ); }
00077 inline static bool is_null( const ftype64& f ) { return ( *((const uitype64*)&f) == *((const uitype64*)&nand_) ); }
00079
00080 inline static bool is_nan( const ftype32 f ) { return ((*((const uitype32*)&f)&CLIPPER_NAN_MASK_A_32)==CLIPPER_NAN_MASK_A_32); }
00082
00083 inline static bool is_nan( const ftype64 f ) { return ((*((const uitype64*)&f)&CLIPPER_NAN_MASK_A_64)==CLIPPER_NAN_MASK_A_64); }
00085
00086 inline static bool isnan(const ftype32 f) { return ((*((const uitype32*)&f)&CLIPPER_NAN_MASK_A_32)==CLIPPER_NAN_MASK_A_32)&&((*((const uitype32*)&f)&CLIPPER_NAN_MASK_B_32)!=0U); }
00088
00089 inline static bool isnan(const ftype64 f) { return ((*((const uitype64*)&f)&CLIPPER_NAN_MASK_A_64)==CLIPPER_NAN_MASK_A_64)&&((*((const uitype64*)&f)&CLIPPER_NAN_MASK_B_64)!=0U); }
00091 static ftype sim( const ftype& x );
00093 static ftype invsim( const ftype& x );
00095 static ftype sim_integ( const ftype& x );
00097 static ftype sim_deriv( const ftype& x );
00099 static ftype sim_deriv_recur( const ftype& x );
00101 static ftype atanh( const ftype& x ) { return log((1.0+x)/(1.0-x))/2.0; }
00103 static ftype bessel_i0( const ftype& x );
00105 static ftype u2b( const ftype& x ) { return x * eightpi2_; }
00107 static ftype b2u( const ftype& x ) { return x / eightpi2_; }
00109 template<class T> inline static T mean( const T& pl, const T& mi )
00110 {
00111 if ( Util::is_nan(pl) ) return mi;
00112 else if (Util::is_nan(mi) ) return pl;
00113 else return 0.5*(pl+mi);
00114 }
00116 template<class T> inline static T sig_mean( const T& pl, const T& mi, const T& cov )
00117 {
00118 if ( Util::is_nan(pl) ) return mi;
00119 else if (Util::is_nan(mi) ) return pl;
00120 else if (Util::is_nan(cov) ) return 0.5*sqrt(pl*pl+mi*mi);
00121 else return 0.5*sqrt(pl*pl+mi*mi+2*cov);
00122 }
00123
00125 inline static int intf( const ftype& a ) { return int( floor( a ) ); }
00127 inline static int intc( const ftype& a ) { return int( ceil( a ) ); }
00129 inline static int intr( const ftype& a ) { return int( rint( a ) ); }
00130
00132 inline static ftype mod( const ftype& a, const ftype& b )
00133 { ftype c = fmod(a, b); if (c < 0) c+=b; return c;}
00135 inline static int mod( const int& a, const int& b )
00136 { int c = a%b; if (c < 0) c+=b; return c; }
00138 template<class T> inline static T max(const T& a, const T& b)
00139 { return (a > b) ? a : b; }
00141 template<class T> inline static T min(const T& a, const T& b)
00142 { return (a < b) ? a : b; }
00144 template<class T> inline static T bound( const T& min, const T& val, const T& max ) { return ( (val < max) ? ( (val > min ) ? val : min ) : max ); }
00146 template<class T> inline static void swap( T& a, T& b )
00147 { T c = a; a = b; b = c; }
00149 template<class T> inline static void swap( T& a, T& b, T& c )
00150 { c = a; a = b; b = c; }
00152 template<class T> inline static T sqr( const T& a ) { return a*a; }
00154 template<class T> inline static T isqrt( const T& n )
00155 { return T(floor(sqrt(ftype(n)))); }
00156
00158 inline static const ftype& pi() { return onepi_; }
00160 inline static const ftype& twopi() { return twopi_; }
00162 inline static const ftype& twopi2() { return twopi2_; }
00164 inline static const ftype& eightpi2() { return eightpi2_; }
00166 static ftype d2rad( const ftype& x );
00168 static ftype rad2d( const ftype& x );
00169
00170 private:
00171 static float nanf_;
00172 static double nand_;
00173 static ftype nan_;
00174 static ftype onepi_;
00175 static ftype twopi_;
00176 static ftype twopi2_;
00177 static ftype eightpi2_;
00178 static ftype d2rad_;
00179 static ftype sim_a;
00180 static ftype sim_b;
00181 static ftype sim_c;
00182 static ftype sim_d;
00183 static ftype sim_e;
00184 static ftype sim_A;
00185 static ftype sim_B;
00186 static ftype sim_C;
00187 static ftype sim_g;
00188 static ftype sim_p;
00189 static ftype sim_q;
00190 static ftype sim_r;
00191 };
00192
00193 }
00194
00195 #endif