1 #ifndef SimTK_SIMMATRIX_NTRAITS_H_
2 #define SimTK_SIMMATRIX_NTRAITS_H_
71 template <
class R>
class conjugate;
76 template <
class T>
class CNT;
79 template <
class N>
class NTraits;
80 template <
class R>
class NTraits< complex<R> >;
81 template <
class R>
class NTraits< conjugate<R> >;
82 template <>
class NTraits<float>;
83 template <>
class NTraits<double>;
84 template <>
class NTraits<long double>;
94 template <
class N>
class negator;
101 static const complex<long double>
zeroes(0);
110 template <
class R1,
class R2>
struct Widest {};
119 template <>
struct Widest<long double,long double> {
typedef long double Type;
typedef long double Precision;};
120 template <
class R1,
class R2>
struct Widest< complex<R1>,complex<R2> > {
121 typedef complex< typename Widest<R1,R2>::Type >
Type;
124 template <
class R1,
class R2>
struct Widest< complex<R1>,R2 > {
125 typedef complex< typename Widest<R1,R2>::Type >
Type;
128 template <
class R1,
class R2>
struct Widest< R1,complex<R2> > {
129 typedef complex< typename Widest<R1,R2>::Type >
Type;
152 template <
class R1,
class R2>
struct Narrowest< complex<R1>,complex<R2> > {
153 typedef complex< typename Narrowest<R1,R2>::Type >
Type;
156 template <
class R1,
class R2>
struct Narrowest< complex<R1>,R2 > {
157 typedef complex< typename Narrowest<R1,R2>::Type >
Type;
160 template <
class R1,
class R2>
struct Narrowest< R1,complex<R2> > {
161 typedef complex< typename Narrowest<R1,R2>::Type >
Type;
170 static const float&
getEps() {
static const float c=std::numeric_limits<float>::epsilon();
return c;}
172 static const float&
getSignificant() {
static const float c=std::pow(getEps(), 0.875f);
return c;}
178 static const double&
getEps() {
static const double c=std::numeric_limits<double>::epsilon();
return c;}
179 static const double&
getSignificant() {
static const double c=std::pow(getEps(), 0.875);
return c;}
184 static const long double&
getEps() {
static const long double c=std::numeric_limits<long double>::epsilon();
return c;}
185 static const long double&
getSignificant() {
static const long double c=std::pow(getEps(), 0.875L);
return c;}
207 inline bool isNaN(
const float& x) {
return std::isnan(x);}
208 inline bool isNaN(
const double& x) {
return std::isnan(x);}
209 inline bool isNaN(
const long double& x) {
return std::isnan(x);}
211 template <
class P>
inline bool
215 template <
class P>
inline bool
217 {
return isNaN(x.real()) ||
isNaN(x.negImag());}
234 inline bool isFinite(
const float& x) {
return std::isfinite(x);}
235 inline bool isFinite(
const double& x) {
return std::isfinite(x);}
236 inline bool isFinite(
const long double& x) {
return std::isfinite(x);}
238 template <
class P>
inline bool
242 template <
class P>
inline bool
263 inline bool isInf(
const float& x) {
return std::isinf(x);}
264 inline bool isInf(
const double& x) {
return std::isinf(x);}
265 inline bool isInf(
const long double& x) {
return std::isinf(x);}
267 template <
class P>
inline bool
273 template <
class P>
inline bool
275 return (
isInf(x.real()) && !
isNaN(x.negImag()))
324 return std::abs(a-b) <= scale*(float)tol; }
330 return std::abs(a-b) <= scale*tol; }
336 return std::abs(a-b) <= scale*(
long double)tol; }
391 template <
class P,
class Q>
393 (
const std::complex<P>& a,
const std::complex<Q>& b,
401 template <
class P,
class Q>
411 template <
class P,
class Q>
421 template <
class P,
class Q>
429 template <
class P>
inline bool
434 template <
class P>
inline bool
439 template <
class P>
inline bool
444 template <
class P>
inline bool
449 template <
class P>
inline bool
454 template <
class P>
inline bool
459 template <
class P>
inline bool
464 template <
class P>
inline bool
470 template <
class P>
inline bool
475 template <
class P>
inline bool
480 template <
class P>
inline bool
485 template <
class P>
inline bool
490 template <
class P>
inline bool
495 template <
class P>
inline bool
500 template <
class P>
inline bool
505 template <
class P>
inline bool
519 template <
class R>
class NTraits< complex<R> > {
520 typedef complex<R> C;
556 template <
class P>
struct Result {
564 template <
class P>
struct Substitute {
577 RealStrideFactor = 2,
584 SignInterpretation = 1
588 static const R&
real(
const T& t) {
return (reinterpret_cast<const R*>(&t))[0]; }
589 static R&
real(
T& t) {
return (reinterpret_cast<R*>(&t))[0]; }
590 static const R&
imag(
const T& t) {
return (reinterpret_cast<const R*>(&t))[1]; }
591 static R&
imag(
T& t) {
return (reinterpret_cast<R*>(&t))[1]; }
600 {
return reinterpret_cast<const TPosTrans&
>(t);}
602 {
return reinterpret_cast<TPosTrans&
>(t);}
610 {
return t.real()*t.real() + t.imag()*t.imag(); }
612 {
return std::sqrt(t); }
629 static const T c =
T(0,1);
689 #define SimTK_BNTCMPLX_SPEC(T1,T2) \
690 template<> template<> struct NTraits< complex<T1> >::Result<T2> { \
691 typedef Widest< complex<T1>,T2 >::Type W; \
692 typedef W Mul; typedef W Dvd; typedef W Add; typedef W Sub; \
694 template<> template<> struct NTraits< complex<T1> >::Result< complex<T2> > { \
695 typedef Widest< complex<T1>,complex<T2> >::Type W; \
696 typedef W Mul; typedef W Dvd; typedef W Add; typedef W Sub; \
698 template<> template<> struct NTraits< complex<T1> >::Result< conjugate<T2> > { \
699 typedef Widest< complex<T1>,complex<T2> >::Type W; \
700 typedef W Mul; typedef W Dvd; typedef W Add; typedef W Sub; \
705 #undef SimTK_BNTCMPLX_SPEC
710 typedef complex<R> C;
744 template <
class P>
struct Result {
752 template <
class P>
struct Substitute {
765 RealStrideFactor = 2,
772 SignInterpretation = 1
789 {
return reinterpret_cast<const TPosTrans&
>(t);}
791 {
return reinterpret_cast<TPosTrans&
>(t);}
799 {
return t.real()*t.real() + t.negImag()*t.negImag(); }
801 {
return std::sqrt(C(t)); }
811 return reinterpret_cast<const TInvert&
>(cmplx); }
828 static const T c =
T(0,-1);
901 #define SimTK_NTRAITS_CONJ_SPEC(T1,T2) \
902 template<> template<> struct NTraits< conjugate<T1> >::Result<T2> { \
903 typedef conjugate<Widest<T1,T2>::Type> W; \
904 typedef W Mul; typedef W Dvd; typedef W Add; typedef W Sub; \
906 template<> template<> struct NTraits< conjugate<T1> >::Result<complex<T2> >{\
907 typedef Widest<complex<T1>,complex<T2> >::Type W; \
908 typedef W Mul; typedef W Dvd; typedef W Add; typedef negator<W> Sub; \
910 template<> template<> struct NTraits< conjugate<T1> >::Result<conjugate<T2> >{\
911 typedef Widest<T1,T2>::Type W; typedef complex<W> WC; \
912 typedef negator<WC> Mul; typedef WC Dvd; typedef conjugate<W> Add; typedef WC Sub;\
920 #undef SimTK_NTRAITS_CONJ_SPEC
930 #define SimTK_DEFINE_REAL_NTRAITS(R) \
931 template <> class NTraits<R> { \
934 typedef negator<T> TNeg; \
935 typedef T TWithoutNegator; \
938 typedef complex<T> TComplex; \
940 typedef T TPosTrans; \
941 typedef T TSqHermT; \
942 typedef T TSqTHerm; \
943 typedef T TElement; \
948 typedef T TStandard; \
950 typedef T TNormalize; \
952 typedef T ULessScalar; \
954 typedef T StdNumber; \
955 typedef T Precision; \
956 typedef T ScalarNormSq; \
957 template <class P> struct Result { \
958 typedef typename CNT<P>::template Result<R>::Mul Mul; \
959 typedef typename CNT< typename CNT<P>::THerm >::template Result<R>::Mul Dvd; \
960 typedef typename CNT<P>::template Result<R>::Add Add; \
961 typedef typename CNT< typename CNT<P>::TNeg >::template Result<R>::Add Sub; \
963 template <class P> struct Substitute { \
971 NPackedElements = 1, \
972 NActualElements = 1, \
973 NActualScalars = 1, \
975 RealStrideFactor = 1, \
976 ArgDepth = SCALAR_DEPTH, \
982 SignInterpretation = 1 \
984 static const T* getData(const T& t) { return &t; } \
985 static T* updData(T& t) { return &t; } \
986 static const T& real(const T& t) { return t; } \
987 static T& real(T& t) { return t; } \
988 static const T& imag(const T&) { return reinterpret_cast<const T&>(zeroes); } \
989 static T& imag(T&) { assert(false); return *reinterpret_cast<T*>(0); } \
990 static const TNeg& negate(const T& t) {return reinterpret_cast<const TNeg&>(t);} \
991 static TNeg& negate(T& t) {return reinterpret_cast<TNeg&>(t);} \
992 static const THerm& transpose(const T& t) {return reinterpret_cast<const THerm&>(t);} \
993 static THerm& transpose(T& t) {return reinterpret_cast<THerm&>(t);} \
994 static const TPosTrans& positionalTranspose(const T& t) \
995 {return reinterpret_cast<const TPosTrans&>(t);} \
996 static TPosTrans& positionalTranspose(T& t) \
997 {return reinterpret_cast<TPosTrans&>(t);} \
998 static const TWithoutNegator& castAwayNegatorIfAny(const T& t) \
999 {return reinterpret_cast<const TWithoutNegator&>(t);} \
1000 static TWithoutNegator& updCastAwayNegatorIfAny(T& t) \
1001 {return reinterpret_cast<TWithoutNegator&>(t);} \
1002 static ScalarNormSq scalarNormSqr(const T& t) {return t*t;} \
1003 static TSqrt sqrt(const T& t) {return std::sqrt(t);} \
1004 static TAbs abs(const T& t) {return std::abs(t);} \
1005 static const TStandard& standardize(const T& t) {return t;} \
1006 static TNormalize normalize(const T& t) {return (t>0?T(1):(t<0?T(-1):getNaN()));} \
1007 static TInvert invert(const T& t) {return T(1)/t;} \
1009 static const T& getEps() {return RTraits<T>::getEps();} \
1010 static const T& getSignificant() {return RTraits<T>::getSignificant();} \
1011 static const T& getNaN() {static const T c=std::numeric_limits<T>::quiet_NaN(); return c;} \
1012 static const T& getInfinity() {static const T c=std::numeric_limits<T>::infinity(); return c;} \
1013 static const T& getLeastPositive(){static const T c=std::numeric_limits<T>::min(); return c;} \
1014 static const T& getMostPositive() {static const T c=std::numeric_limits<T>::max(); return c;} \
1015 static const T& getLeastNegative(){static const T c=-std::numeric_limits<T>::min(); return c;} \
1016 static const T& getMostNegative() {static const T c=-std::numeric_limits<T>::max(); return c;} \
1017 static const T& getSqrtEps() {static const T c=std::sqrt(getEps()); return c;} \
1018 static const T& getTiny() {static const T c=std::pow(getEps(), (T)1.25L); return c;} \
1019 static bool isFinite(const T& t) {return SimTK::isFinite(t);} \
1020 static bool isNaN (const T& t) {return SimTK::isNaN(t);} \
1021 static bool isInf (const T& t) {return SimTK::isInf(t);} \
1024 static double getDefaultTolerance() {return RTraits<T>::getDefaultTolerance();} \
1025 static bool isNumericallyEqual(const T& t, const float& f) {return SimTK::isNumericallyEqual(t,f);} \
1026 static bool isNumericallyEqual(const T& t, const double& d) {return SimTK::isNumericallyEqual(t,d);} \
1027 static bool isNumericallyEqual(const T& t, const long double& l) {return SimTK::isNumericallyEqual(t,l);} \
1028 static bool isNumericallyEqual(const T& t, int i) {return SimTK::isNumericallyEqual(t,i);} \
1030 static bool isNumericallyEqual(const T& t, const float& f, double tol){return SimTK::isNumericallyEqual(t,f,tol);} \
1031 static bool isNumericallyEqual(const T& t, const double& d, double tol){return SimTK::isNumericallyEqual(t,d,tol);} \
1032 static bool isNumericallyEqual(const T& t, const long double& l, double tol){return SimTK::isNumericallyEqual(t,l,tol);} \
1033 static bool isNumericallyEqual(const T& t, int i, double tol){return SimTK::isNumericallyEqual(t,i,tol);} \
1035 static const T& getZero() {static const T c=(T)(0); return c;} \
1036 static const T& getOne() {static const T c=(T)(1); return c;} \
1037 static const T& getMinusOne() {static const T c=(T)(-1); return c;} \
1038 static const T& getTwo() {static const T c=(T)(2); return c;} \
1039 static const T& getThree() {static const T c=(T)(3); return c;} \
1040 static const T& getOneHalf() {static const T c=(T)(0.5L); return c;} \
1041 static const T& getOneThird() {static const T c=(T)(1.L/3.L); return c;} \
1042 static const T& getOneFourth() {static const T c=(T)(0.25L); return c;} \
1043 static const T& getOneFifth() {static const T c=(T)(0.2L); return c;} \
1044 static const T& getOneSixth() {static const T c=(T)(1.L/6.L); return c;} \
1045 static const T& getOneSeventh() {static const T c=(T)(1.L/7.L); return c;} \
1046 static const T& getOneEighth() {static const T c=(T)(0.125L); return c;} \
1047 static const T& getOneNinth() {static const T c=(T)(1.L/9.L); return c;} \
1048 static const T& getPi() {static const T c=(T)(SimTK_PI); return c;} \
1049 static const T& getOneOverPi() {static const T c=(T)(1.L/SimTK_PI); return c;} \
1050 static const T& getE() {static const T c=(T)(SimTK_E); return c;} \
1051 static const T& getLog2E() {static const T c=(T)(SimTK_LOG2E); return c;} \
1052 static const T& getLog10E() {static const T c=(T)(SimTK_LOG10E); return c;} \
1053 static const T& getSqrt2() {static const T c=(T)(SimTK_SQRT2); return c;} \
1054 static const T& getOneOverSqrt2() {static const T c=(T)(1.L/SimTK_SQRT2); return c;} \
1055 static const T& getSqrt3() {static const T c=(T)(SimTK_SQRT3); return c;} \
1056 static const T& getOneOverSqrt3() {static const T c=(T)(1.L/SimTK_SQRT3); return c;} \
1057 static const T& getCubeRoot2() {static const T c=(T)(SimTK_CBRT2); return c;} \
1058 static const T& getCubeRoot3() {static const T c=(T)(SimTK_CBRT3); return c;} \
1059 static const T& getLn2() {static const T c=(T)(SimTK_LN2); return c;} \
1060 static const T& getLn10() {static const T c=(T)(SimTK_LN10); return c;} \
1062 static const int getNumDigits() {static const int c=(int)(std::log10(1/getEps()) -0.5); return c;} \
1063 static const int getLosslessNumDigits() {static const int c=(int)(std::log10(1/getTiny())+0.5); return c;} \
1065 template<> struct NTraits<R>::Result<float> \
1066 {typedef Widest<R,float>::Type Mul;typedef Mul Dvd;typedef Mul Add;typedef Mul Sub;}; \
1067 template<> struct NTraits<R>::Result<double> \
1068 {typedef Widest<R,double>::Type Mul;typedef Mul Dvd;typedef Mul Add;typedef Mul Sub;}; \
1069 template<> struct NTraits<R>::Result<long double> \
1070 {typedef Widest<R,long double>::Type Mul;typedef Mul Dvd;typedef Mul Add;typedef Mul Sub;}; \
1071 template<> struct NTraits<R>::Result<complex<float> > \
1072 {typedef Widest<R,complex<float> >::Type Mul;typedef Mul Dvd;typedef Mul Add;typedef Mul Sub;}; \
1073 template<> struct NTraits<R>::Result<complex<double> > \
1074 {typedef Widest<R,complex<double> >::Type Mul;typedef Mul Dvd;typedef Mul Add;typedef Mul Sub;}; \
1075 template<> struct NTraits<R>::Result<complex<long double> > \
1076 {typedef Widest<R,complex<long double> >::Type Mul;typedef Mul Dvd;typedef Mul Add;typedef Mul Sub;}; \
1077 template<> struct NTraits<R>::Result<conjugate<float> > \
1078 {typedef conjugate<Widest<R,float>::Type> Mul;typedef Mul Dvd;typedef Mul Add;typedef Mul Sub;}; \
1079 template<> struct NTraits<R>::Result<conjugate<double> > \
1080 {typedef conjugate<Widest<R,double>::Type> Mul;typedef Mul Dvd;typedef Mul Add;typedef Mul Sub;}; \
1081 template<> struct NTraits<R>::Result<conjugate<long double> > \
1082 {typedef conjugate<Widest<R,long double>::Type> Mul;typedef Mul Dvd;typedef Mul Add;typedef Mul Sub;}
1086 #undef SimTK_DEFINE_REAL_NTRAITS
1089 template <
class R>
class CNT< complex<R> > :
public NTraits< complex<R> > { };
1093 template <>
class CNT<long double> :
public NTraits<long double> { };
1098 #endif //SimTK_SIMMATRIX_NTRAITS_H_