Scalar.h
Go to the documentation of this file.00001 #ifndef SimTK_SIMMATRIX_SCALAR_H_
00002 #define SimTK_SIMMATRIX_SCALAR_H_
00003
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
00041 #include "SimTKcommon/internal/common.h"
00042 #include "SimTKcommon/Constants.h"
00043 #include "SimTKcommon/internal/Exception.h"
00044 #include "SimTKcommon/internal/ExceptionMacros.h"
00045 #include "SimTKcommon/internal/String.h"
00046
00047 #include "SimTKcommon/internal/conjugate.h"
00048 #include "SimTKcommon/internal/CompositeNumericalTypes.h"
00049 #include "SimTKcommon/internal/NTraits.h"
00050 #include "SimTKcommon/internal/negator.h"
00051
00052 #include <complex>
00053 #include <cmath>
00054 #include <climits>
00055
00056 namespace SimTK {
00057
00059
00061
00062 typedef conjugate<Real> Conjugate;
00063
00064
00065
00066
00067
00068
00069
00070
00071 static const Real& NaN = NTraits<Real>::getNaN();
00072 static const Real& Infinity = NTraits<Real>::getInfinity();
00073
00074
00075
00076 static const Real& Eps = NTraits<Real>::getEps();
00077 static const Real& SqrtEps = NTraits<Real>::getSqrtEps();
00078 static const Real& TinyReal = NTraits<Real>::getTiny();
00079 static const Real& SignificantReal = NTraits<Real>::getSignificant();
00080
00081 static const Real& LeastPositiveReal = NTraits<Real>::getLeastPositive();
00082 static const Real& MostPositiveReal = NTraits<Real>::getMostPositive();
00083 static const Real& LeastNegativeReal = NTraits<Real>::getLeastNegative();
00084 static const Real& MostNegativeReal = NTraits<Real>::getMostNegative();
00085
00086
00087
00088 static const int NumDigitsReal = NTraits<Real>::getNumDigits();
00089
00090
00091
00092
00093 static const int LosslessNumDigitsReal = NTraits<Real>::getLosslessNumDigits();
00094
00095
00096
00097 static const Real& Zero = NTraits<Real>::getZero();
00098 static const Real& One = NTraits<Real>::getOne();
00099 static const Real& MinusOne = NTraits<Real>::getMinusOne();
00100 static const Real& Two = NTraits<Real>::getTwo();
00101 static const Real& Three = NTraits<Real>::getThree();
00102
00103 static const Real& OneHalf = NTraits<Real>::getOneHalf();
00104 static const Real& OneThird = NTraits<Real>::getOneThird();
00105 static const Real& OneFourth = NTraits<Real>::getOneFourth();
00106 static const Real& OneFifth = NTraits<Real>::getOneFifth();
00107 static const Real& OneSixth = NTraits<Real>::getOneSixth();
00108 static const Real& OneSeventh = NTraits<Real>::getOneSeventh();
00109 static const Real& OneEighth = NTraits<Real>::getOneEighth();
00110 static const Real& OneNinth = NTraits<Real>::getOneNinth();
00111 static const Real& Pi = NTraits<Real>::getPi();
00112 static const Real& OneOverPi = NTraits<Real>::getOneOverPi();
00113 static const Real& E = NTraits<Real>::getE();
00114 static const Real& Log2E = NTraits<Real>::getLog2E();
00115 static const Real& Log10E = NTraits<Real>::getLog10E();
00116 static const Real& Sqrt2 = NTraits<Real>::getSqrt2();
00117 static const Real& OneOverSqrt2 = NTraits<Real>::getOneOverSqrt2();
00118 static const Real& Sqrt3 = NTraits<Real>::getSqrt3();
00119 static const Real& OneOverSqrt3 = NTraits<Real>::getOneOverSqrt3();
00120 static const Real& CubeRoot2 = NTraits<Real>::getCubeRoot2();
00121 static const Real& CubeRoot3 = NTraits<Real>::getCubeRoot3();
00122 static const Real& Ln2 = NTraits<Real>::getLn2();
00123 static const Real& Ln10 = NTraits<Real>::getLn10();
00124
00125
00126
00127
00128 static const Complex& I = NTraits<Complex>::getI();
00129
00130
00132
00134
00135
00149
00150
00151 inline bool atMostOneBitIsSet(unsigned char v) {return (v&v-1)==0;}
00152 inline bool atMostOneBitIsSet(unsigned short v) {return (v&v-1)==0;}
00153 inline bool atMostOneBitIsSet(unsigned int v) {return (v&v-1)==0;}
00154 inline bool atMostOneBitIsSet(unsigned long v) {return (v&v-1)==0;}
00155 inline bool atMostOneBitIsSet(unsigned long long v) {return (v&v-1)==0;}
00156 inline bool atMostOneBitIsSet(signed char v) {return (v&v-1)==0;}
00157 inline bool atMostOneBitIsSet(char v) {return (v&v-1)==0;}
00158 inline bool atMostOneBitIsSet(short v) {return (v&v-1)==0;}
00159 inline bool atMostOneBitIsSet(int v) {return (v&v-1)==0;}
00160 inline bool atMostOneBitIsSet(long v) {return (v&v-1)==0;}
00161 inline bool atMostOneBitIsSet(long long v) {return (v&v-1)==0;}
00163
00178 inline bool exactlyOneBitIsSet(unsigned char v) {return v && atMostOneBitIsSet(v);}
00179 inline bool exactlyOneBitIsSet(unsigned short v) {return v && atMostOneBitIsSet(v);}
00180 inline bool exactlyOneBitIsSet(unsigned int v) {return v && atMostOneBitIsSet(v);}
00181 inline bool exactlyOneBitIsSet(unsigned long v) {return v && atMostOneBitIsSet(v);}
00182 inline bool exactlyOneBitIsSet(unsigned long long v) {return v && atMostOneBitIsSet(v);}
00183 inline bool exactlyOneBitIsSet(signed char v) {return v && atMostOneBitIsSet(v);}
00184 inline bool exactlyOneBitIsSet(char v) {return v && atMostOneBitIsSet(v);}
00185 inline bool exactlyOneBitIsSet(short v) {return v && atMostOneBitIsSet(v);}
00186 inline bool exactlyOneBitIsSet(int v) {return v && atMostOneBitIsSet(v);}
00187 inline bool exactlyOneBitIsSet(long v) {return v && atMostOneBitIsSet(v);}
00188 inline bool exactlyOneBitIsSet(long long v) {return v && atMostOneBitIsSet(v);}
00190
00217 inline bool signBit(unsigned char) {return 0;}
00218 inline bool signBit(unsigned short) {return 0;}
00219 inline bool signBit(unsigned int) {return 0;}
00220 inline bool signBit(unsigned long) {return 0;}
00221 inline bool signBit(unsigned long long) {return 0;}
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 inline bool signBit(signed char i) {return ((unsigned char)i & 0x80U) != 0;}
00232 inline bool signBit(short i) {return ((unsigned short)i & 0x8000U) != 0;}
00233 inline bool signBit(int i) {return ((unsigned int)i & 0x80000000U) != 0;}
00234 inline bool signBit(long long i) {return ((unsigned long long)i & 0x8000000000000000ULL) != 0;}
00235
00236 inline bool signBit(long i) {return ((unsigned long)i
00237 & (unsigned long)LONG_MIN) != 0;}
00238
00239 inline bool signBit(const float& f) {return std::signbit(f);}
00240 inline bool signBit(const double& d) {return std::signbit(d);}
00241 inline bool signBit(const negator<float>& nf) {return std::signbit(-nf);}
00242 inline bool signBit(const negator<double>& nd) {return std::signbit(-nd);}
00244
00257 inline unsigned int sign(unsigned char u) {return u==0 ? 0 : 1;}
00258 inline unsigned int sign(unsigned short u) {return u==0 ? 0 : 1;}
00259 inline unsigned int sign(unsigned int u) {return u==0 ? 0 : 1;}
00260 inline unsigned int sign(unsigned long u) {return u==0 ? 0 : 1;}
00261 inline unsigned int sign(unsigned long long u) {return u==0 ? 0 : 1;}
00262
00263
00264
00265
00266 inline int sign(signed char i) {return i>0 ? 1 : (i<0 ? -1 : 0);}
00267 inline int sign(short i) {return i>0 ? 1 : (i<0 ? -1 : 0);}
00268 inline int sign(int i) {return i>0 ? 1 : (i<0 ? -1 : 0);}
00269 inline int sign(long i) {return i>0 ? 1 : (i<0 ? -1 : 0);}
00270 inline int sign(long long i) {return i>0 ? 1 : (i<0 ? -1 : 0);}
00271
00272 inline int sign(const float& x) {return x>0 ? 1 : (x<0 ? -1 : 0);}
00273 inline int sign(const double& x) {return x>0 ? 1 : (x<0 ? -1 : 0);}
00274 inline int sign(const long double& x) {return x>0 ? 1 : (x<0 ? -1 : 0);}
00275
00276 inline int sign(const negator<float>& x) {return -sign(-x);}
00277 inline int sign(const negator<double>& x) {return -sign(-x);}
00278 inline int sign(const negator<long double>& x) {return -sign(-x);}
00280
00297 inline unsigned char square(unsigned char u) {return u*u;}
00298 inline unsigned short square(unsigned short u) {return u*u;}
00299 inline unsigned int square(unsigned int u) {return u*u;}
00300 inline unsigned long square(unsigned long u) {return u*u;}
00301
00302 inline char square(char c) {return c*c;}
00303
00304 inline signed char square(signed char i) {return i*i;}
00305 inline short square(short i) {return i*i;}
00306 inline int square(int i) {return i*i;}
00307 inline long square(long i) {return i*i;}
00308
00309 inline float square(const float& x) {return x*x;}
00310 inline double square(const double& x) {return x*x;}
00311 inline long double square(const long double& x) {return x*x;}
00312
00313
00314
00315 inline float square(const negator<float>& x) {return square(-x);}
00316 inline double square(const negator<double>& x) {return square(-x);}
00317 inline long double square(const negator<long double>& x) {return square(-x);}
00318
00319
00320
00321
00322 template <class P> inline
00323 std::complex<P> square(const std::complex<P>& x) {
00324 const P re=x.real(), im=x.imag();
00325 return std::complex<P>(re*re-im*im, 2*re*im);
00326 }
00327
00328
00329
00330
00331 template <class P> inline
00332 std::complex<P> square(const conjugate<P>& x) {
00333 const P re=x.real(), nim=x.negImag();
00334 return std::complex<P>(re*re-nim*nim, -2*re*nim);
00335 }
00336
00337 template <class P> inline
00338 std::complex<P> square(const negator< std::complex<P> >& x) {
00339 return square(-x);
00340 }
00341
00342
00343
00344 template <class P> inline
00345 std::complex<P> square(const negator< conjugate<P> >& x) {
00346 return square(-x);
00347 }
00349
00368 inline unsigned char cube(unsigned char u) {return u*u*u;}
00369 inline unsigned short cube(unsigned short u) {return u*u*u;}
00370 inline unsigned int cube(unsigned int u) {return u*u*u;}
00371 inline unsigned long cube(unsigned long u) {return u*u*u;}
00372
00373 inline char cube(char c) {return c*c*c;}
00374
00375 inline signed char cube(signed char i) {return i*i*i;}
00376 inline short cube(short i) {return i*i*i;}
00377 inline int cube(int i) {return i*i*i;}
00378 inline long cube(long i) {return i*i*i;}
00379
00380 inline float cube(const float& x) {return x*x*x;}
00381 inline double cube(const double& x) {return x*x*x;}
00382 inline long double cube(const long double& x) {return x*x*x;}
00383
00384
00385
00386
00387 inline negator<float> cube(const negator<float>& x) {
00388 return negator<float>::recast(cube(-x));
00389 }
00390 inline negator<double> cube(const negator<double>& x) {
00391 return negator<double>::recast(cube(-x));
00392 }
00393 inline negator<long double> cube(const negator<long double>& x) {
00394 return negator<long double>::recast(cube(-x));
00395 }
00396
00397
00398
00399
00400 template <class P> inline
00401 std::complex<P> cube(const std::complex<P>& x) {
00402 const P re=x.real(), im=x.imag(), rr=re*re, ii=im*im;
00403 return std::complex<P>(re*(rr-3*ii), im*(3*rr-ii));
00404 }
00405
00406
00407
00408
00409
00410 template <class P> inline
00411 std::complex<P> cube(const negator< std::complex<P> >& x) {
00412
00413 const P nre=(-x).real(), nim=(-x).imag(), rr=nre*nre, ii=nim*nim;
00414 return std::complex<P>(nre*(3*ii-rr), nim*(ii-3*rr));
00415 }
00416
00417
00418
00419 template <class P> inline
00420 std::complex<P> cube(const conjugate<P>& x) {
00421 const P re=x.real(), nim=x.negImag(), rr=re*re, ii=nim*nim;
00422 return std::complex<P>(re*(rr-3*ii), nim*(ii-3*rr));
00423 }
00424
00425
00426
00427
00428 template <class P> inline
00429 std::complex<P> cube(const negator< conjugate<P> >& x) {
00430
00431 const P nre=(-x).real(), im=(-x).negImag(), rr=nre*nre, ii=im*im;
00432 return std::complex<P>(nre*(3*ii-rr), im*(3*rr-ii));
00433 }
00435
00436 }
00437
00438 #endif //SimTK_SIMMATRIX_SCALAR_H_