17 #ifndef _TEUCHOS_SCALARTRAITS_HPP_
18 #define _TEUCHOS_SCALARTRAITS_HPP_
26 #ifdef HAVE_TEUCHOSCORE_KOKKOS
27 #include "Kokkos_Complex.hpp"
28 #endif // HAVE_TEUCHOSCORE_KOKKOS
30 #ifdef HAVE_TEUCHOS_ARPREC
31 #include <arprec/mp_real.h>
34 #ifdef HAVE_TEUCHOSCORE_QUADMATH
52 operator<< (std::ostream& out,
const __float128& x);
64 operator>> (std::istream& in, __float128& x);
68 #endif // HAVE_TEUCHOSCORE_QUADMATH
70 #ifdef HAVE_TEUCHOS_QD
71 #include <qd/qd_real.h>
72 #include <qd/dd_real.h>
75 #ifdef HAVE_TEUCHOS_GNU_MP
87 #ifndef DOXYGEN_SHOULD_SKIP_THIS
90 TEUCHOSCORE_LIB_DLL_EXPORT
91 void throwScalarTraitsNanInfError(
const std::string &errMsg );
94 template<
class Scalar>
95 bool generic_real_isnaninf(
const Scalar &x)
97 #ifdef HAVE_TEUCHOSCORE_CXX11
98 if (std::isnan(x))
return true;
99 if (std::isinf(x))
return true;
102 typedef std::numeric_limits<Scalar> STD_NL;
104 const Scalar tol = 1.0;
105 if (!(x <= tol) && !(x > tol))
return true;
107 Scalar z =
static_cast<Scalar
>(0.0) * x;
108 if (!(z <= tol) && !(z > tol))
return true;
110 if (x == STD_NL::infinity() || x == -STD_NL::infinity())
return true;
117 #define TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR( VALUE, MSG ) \
118 if (isnaninf(VALUE)) { \
119 std::ostringstream omsg; \
121 Teuchos::throwScalarTraitsNanInfError(omsg.str()); \
126 struct ScalarTraits<char>
137 static inline magnitudeType magnitude(
char a) {
return static_cast<char>(std::fabs(static_cast<double>(a))); }
138 static inline char zero() {
return 0; }
139 static inline char one() {
return 1; }
140 static inline char conjugate(
char x) {
return x; }
141 static inline char real(
char x) {
return x; }
142 static inline char imag(
char) {
return 0; }
143 static inline bool isnaninf(
char ) {
return false; }
144 static inline void seedrandom(
unsigned int s) {
153 static inline char random() {
return std::rand(); }
154 static inline std::string
name() {
return "char"; }
155 static inline char squareroot(
char x) {
return (
char) std::sqrt((
double) x); }
156 static inline char pow(
char x,
char y) {
return (
char) std::pow((
double)x,(
double)y); }
157 static inline char log(
char x) {
return static_cast<char> (std::log (static_cast<double> (x))); }
158 static inline char log10(
char x) {
return static_cast<char> (std::log10 (static_cast<double> (x))); }
163 struct ScalarTraits<short int>
174 static inline magnitudeType magnitude(
short int a) {
return static_cast<short int>(std::fabs(static_cast<double>(a))); }
175 static inline short int zero() {
return 0; }
176 static inline short int one() {
return 1; }
177 static inline short int conjugate(
short int x) {
return x; }
178 static inline short int real(
short int x) {
return x; }
179 static inline short int imag(
short int) {
return 0; }
180 static inline bool isnaninf(
short int) {
return false; }
181 static inline void seedrandom(
unsigned int s) {
190 static inline short int random() {
return std::rand(); }
191 static inline std::string
name() {
return "short int"; }
192 static inline short int squareroot(
short int x) {
return (
short int) std::sqrt((
double) x); }
193 static inline short int pow(
short int x,
short int y) {
return (
short int) std::pow((
double)x,(
double)y); }
194 static inline short int log(
short int x) {
return static_cast<short int> (std::log (static_cast<double> (x))); }
195 static inline short int log10(
short int x) {
return static_cast<short int> (std::log10 (static_cast<double> (x))); }
199 struct ScalarTraits<unsigned short int>
210 static inline magnitudeType magnitude(
unsigned short int a) {
return static_cast<unsigned short int>(std::fabs(static_cast<double>(a))); }
211 static inline unsigned short int zero() {
return 0; }
212 static inline unsigned short int one() {
return 1; }
213 static inline unsigned short int conjugate(
unsigned short int x) {
return x; }
214 static inline unsigned short int real(
unsigned short int x) {
return x; }
215 static inline unsigned short int imag(
unsigned short int) {
return 0; }
216 static inline bool isnaninf(
unsigned short int) {
return false; }
217 static inline void seedrandom(
unsigned int s) {
226 static inline unsigned short int random() {
return std::rand(); }
227 static inline std::string
name() {
return "unsigned short int"; }
228 static inline unsigned short int squareroot(
unsigned short int x) {
return (
unsigned short int) std::sqrt((
double) x); }
229 static inline unsigned short int pow(
unsigned short int x,
unsigned short int y) {
return (
unsigned short int) std::pow((
double)x,(
double)y); }
230 static inline unsigned short int log(
unsigned short int x) {
return static_cast<unsigned short int> (std::log (static_cast<double> (x))); }
231 static inline unsigned short int log10(
unsigned short int x) {
return static_cast<unsigned short int> (std::log10 (static_cast<double> (x))); }
236 struct ScalarTraits<int>
247 static inline magnitudeType magnitude(
int a) {
return static_cast<int>(std::fabs(static_cast<double>(a))); }
248 static inline int zero() {
return 0; }
249 static inline int one() {
return 1; }
250 static inline int conjugate(
int x) {
return x; }
251 static inline int real(
int x) {
return x; }
252 static inline int imag(
int) {
return 0; }
253 static inline bool isnaninf(
int) {
return false; }
254 static inline void seedrandom(
unsigned int s) {
263 static inline int random() {
return std::rand(); }
264 static inline std::string
name() {
return "int"; }
265 static inline int squareroot(
int x) {
return (
int) std::sqrt((
double) x); }
266 static inline int pow(
int x,
int y) {
return (
int) std::pow((
double)x,(
double)y); }
267 static inline int log(
int x) {
return static_cast<int> (std::log (static_cast<double> (x))); }
268 static inline int log10(
int x) {
return static_cast<int> (std::log10 (static_cast<double> (x))); }
273 struct ScalarTraits<unsigned int>
284 static inline magnitudeType magnitude(
unsigned int a) {
return static_cast<unsigned int>(std::fabs(static_cast<double>(a))); }
285 static inline unsigned int zero() {
return 0; }
286 static inline unsigned int one() {
return 1; }
287 static inline unsigned int conjugate(
unsigned int x) {
return x; }
288 static inline unsigned int real(
unsigned int x) {
return x; }
289 static inline unsigned int imag(
unsigned int) {
return 0; }
290 static inline bool isnaninf(
unsigned int) {
return false; }
291 static inline void seedrandom(
unsigned int s) {
300 static inline unsigned int random() {
return std::rand(); }
301 static inline std::string
name() {
return "unsigned int"; }
302 static inline unsigned int squareroot(
unsigned int x) {
return (
unsigned int) std::sqrt((
double) x); }
303 static inline unsigned int pow(
unsigned int x,
unsigned int y) {
return (
unsigned int) std::pow((
double)x,(
double)y); }
304 static inline unsigned int log(
unsigned int x) {
return static_cast<unsigned int> (std::log (static_cast<double> (x))); }
305 static inline unsigned int log10(
unsigned int x) {
return static_cast<unsigned int> (std::log10 (static_cast<double> (x))); }
310 struct ScalarTraits<long int>
321 static inline magnitudeType magnitude(
long int a) {
return static_cast<long int>(std::fabs(static_cast<double>(a))); }
322 static inline long int zero() {
return 0; }
323 static inline long int one() {
return 1; }
324 static inline long int conjugate(
long int x) {
return x; }
325 static inline long int real(
long int x) {
return x; }
326 static inline long int imag(
long int) {
return 0; }
327 static inline bool isnaninf(
long int) {
return false; }
328 static inline void seedrandom(
unsigned int s) {
337 static inline long int random() {
return std::rand(); }
338 static inline std::string
name() {
return "long int"; }
339 static inline long int squareroot(
long int x) {
return (
long int) std::sqrt((
double) x); }
340 static inline long int pow(
long int x,
long int y) {
return (
long int) std::pow((
double)x,(
double)y); }
343 static inline long int log(
long int x) {
return static_cast<long int> (std::log (static_cast<double> (x))); }
344 static inline long int log10(
long int x) {
return static_cast<long int> (std::log10 (static_cast<double> (x))); }
349 struct ScalarTraits<long unsigned int>
360 static inline magnitudeType magnitude(
long unsigned int a) {
return static_cast<long unsigned int>(std::fabs(static_cast<double>(a))); }
361 static inline long unsigned int zero() {
return 0; }
362 static inline long unsigned int one() {
return 1; }
363 static inline long unsigned int conjugate(
long unsigned int x) {
return x; }
364 static inline long unsigned int real(
long unsigned int x) {
return x; }
365 static inline long unsigned int imag(
long unsigned int) {
return 0; }
366 static inline bool isnaninf(
long unsigned int) {
return false; }
367 static inline void seedrandom(
unsigned int s) {
376 static inline long unsigned int random() {
return std::rand(); }
377 static inline std::string
name() {
return "long unsigned int"; }
378 static inline long unsigned int squareroot(
long unsigned int x) {
return (
long unsigned int) std::sqrt((
double) x); }
379 static inline long unsigned int pow(
long unsigned int x,
long unsigned int y) {
return (
long unsigned int) std::pow((
double)x,(
double)y); }
382 static inline long unsigned int log(
long unsigned int x) {
return static_cast<long unsigned int> (std::log (static_cast<double> (x))); }
383 static inline long unsigned int log10(
long unsigned int x) {
return static_cast<long unsigned int> (std::log10 (static_cast<double> (x))); }
388 struct ScalarTraits<long long int>
399 static inline magnitudeType magnitude(
long long int a) {
return static_cast<long long int>(std::fabs(static_cast<double>(a))); }
400 static inline long long int zero() {
return 0; }
401 static inline long long int one() {
return 1; }
402 static inline long long int conjugate(
long long int x) {
return x; }
403 static inline long long int real(
long long int x) {
return x; }
404 static inline long long int imag(
long long int) {
return 0; }
405 static inline bool isnaninf(
long long int) {
return false; }
406 static inline void seedrandom(
unsigned int s) {
415 static inline long long int random() {
return std::rand(); }
416 static inline std::string
name() {
return "long long int"; }
417 static inline long long int squareroot(
long long int x) {
return (
long long int) std::sqrt((
double) x); }
418 static inline long long int pow(
long long int x,
long long int y) {
return (
long long int) std::pow((
double)x,(
double)y); }
421 static inline long long int log(
long long int x) {
return static_cast<long long int> (std::log (static_cast<double> (x))); }
422 static inline long long int log10(
long long int x) {
return static_cast<long long int> (std::log10 (static_cast<double> (x))); }
426 struct ScalarTraits<unsigned long long int>
437 static inline magnitudeType magnitude(
unsigned long long int a) {
return static_cast<unsigned long long int>(std::fabs(static_cast<double>(a))); }
438 static inline unsigned long long int zero() {
return 0; }
439 static inline unsigned long long int one() {
return 1; }
440 static inline unsigned long long int conjugate(
unsigned long long int x) {
return x; }
441 static inline unsigned long long int real(
unsigned long long int x) {
return x; }
442 static inline unsigned long long int imag(
unsigned long long int) {
return 0; }
443 static inline bool isnaninf(
unsigned long long int) {
return false; }
444 static inline void seedrandom(
unsigned int s) {
453 static inline unsigned long long int random() {
return std::rand(); }
454 static inline std::string
name() {
return "unsigned long long int"; }
455 static inline unsigned long long int squareroot(
unsigned long long int x) {
return (
unsigned long long int) std::sqrt((
double) x); }
456 static inline unsigned long long int pow(
unsigned long long int x,
unsigned long long int y) {
return (
unsigned long long int) std::pow((
double)x,(
double)y); }
459 static inline unsigned long long int log(
unsigned long long int x) {
return static_cast<unsigned long long int> (std::log (static_cast<double> (x))); }
460 static inline unsigned long long int log10(
unsigned long long int x) {
return static_cast<unsigned long long int> (std::log10 (static_cast<double> (x))); }
464 #ifdef HAVE_TEUCHOS___INT64
467 struct ScalarTraits<__int64>
478 static inline magnitudeType magnitude(__int64 a) {
return static_cast<__int64
>(std::fabs(static_cast<double>(a))); }
479 static inline __int64
zero() {
return 0; }
480 static inline __int64
one() {
return 1; }
481 static inline __int64
conjugate(__int64 x) {
return x; }
482 static inline __int64
real(__int64 x) {
return x; }
483 static inline __int64
imag(__int64) {
return 0; }
484 static inline void seedrandom(
unsigned int s) {
493 static inline __int64
random() {
return std::rand(); }
494 static inline std::string
name() {
return "__int64"; }
495 static inline __int64
squareroot(__int64 x) {
return (__int64) std::sqrt((
double) x); }
496 static inline __int64
pow(__int64 x, __int64 y) {
return (__int64) std::pow((
double)x,(
double)y); }
499 static inline __int64 log(__int64 x) {
return static_cast<__int64
> (std::log (static_cast<double> (x))); }
500 static inline __int64 log10(__int64 x) {
return static_cast<__int64
> (std::log10 (static_cast<double> (x))); }
504 struct ScalarTraits<unsigned __int64>
515 static inline magnitudeType magnitude(
unsigned __int64 a) {
return static_cast<unsigned __int64
>(std::fabs(static_cast<double>(a))); }
516 static inline unsigned __int64
zero() {
return 0; }
517 static inline unsigned __int64
one() {
return 1; }
518 static inline unsigned __int64
conjugate(
unsigned __int64 x) {
return x; }
519 static inline unsigned __int64
real(
unsigned __int64 x) {
return x; }
520 static inline unsigned __int64
imag(
unsigned __int64) {
return 0; }
521 static inline void seedrandom(
unsigned int s) {
530 static inline unsigned __int64
random() {
return std::rand(); }
531 static inline std::string
name() {
return "unsigned __int64"; }
532 static inline unsigned __int64
squareroot(
unsigned __int64 x) {
return (
unsigned __int64) std::sqrt((
double) x); }
533 static inline unsigned __int64
pow(
unsigned __int64 x,
unsigned __int64 y) {
return (
unsigned __int64) std::pow((
double)x,(
double)y); }
536 static inline unsigned __int64 log(
unsigned __int64 x) {
return static_cast<unsigned __int64
> (std::log (static_cast<double> (x))); }
537 static inline unsigned __int64 log10(
unsigned __int64 x) {
return static_cast<unsigned __int64
> (std::log10 (static_cast<double> (x))); }
540 #endif // HAVE_TEUCHOS___INT64
544 extern TEUCHOSCORE_LIB_DLL_EXPORT
const float flt_nan;
549 struct ScalarTraits<float>
559 static inline float eps() {
560 return std::numeric_limits<float>::epsilon();
562 static inline float sfmin() {
563 return std::numeric_limits<float>::min();
565 static inline float base() {
566 return static_cast<float>(std::numeric_limits<float>::radix);
568 static inline float prec() {
571 static inline float t() {
572 return static_cast<float>(std::numeric_limits<float>::digits);
574 static inline float rnd() {
575 return ( std::numeric_limits<float>::round_style == std::round_to_nearest ?
one() :
zero() );
577 static inline float emin() {
578 return static_cast<float>(std::numeric_limits<float>::min_exponent);
580 static inline float rmin() {
581 return std::numeric_limits<float>::min();
583 static inline float emax() {
584 return static_cast<float>(std::numeric_limits<float>::max_exponent);
586 static inline float rmax() {
587 return std::numeric_limits<float>::max();
592 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
593 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
597 static inline float zero() {
return(0.0f); }
598 static inline float one() {
return(1.0f); }
599 static inline float conjugate(
float x) {
return(x); }
600 static inline float real(
float x) {
return x; }
601 static inline float imag(
float) {
return zero(); }
602 static inline float nan() {
604 return 0.0f/std::sin(0.0f);
606 return std::numeric_limits<float>::quiet_NaN();
609 static inline bool isnaninf(
float x) {
610 return generic_real_isnaninf<float>(x);
612 static inline void seedrandom(
unsigned int s) {
620 static inline float random() {
float rnd = (float) std::rand() /
static_cast<float>(RAND_MAX);
return (-1.0f + 2.0f * rnd); }
621 static inline std::string
name() {
return "float"; }
625 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
626 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
629 const float rtn = std::sqrt(x);
634 static inline float pow(
float x,
float y) {
return std::pow(x,y); }
635 static inline float pi() {
return 3.14159265358979323846f; }
636 static inline float log(
float x) {
return std::log(x); }
637 static inline float log10(
float x) {
return std::log10(x); }
642 extern TEUCHOSCORE_LIB_DLL_EXPORT
const double dbl_nan;
647 struct ScalarTraits<double>
660 #if defined(HAVE_TEUCHOS_DOUBLE_TO_QD)
662 #elif defined(HAVE_TEUCHOS_DOUBLE_TO_ARPREC)
672 static inline double eps() {
673 return std::numeric_limits<double>::epsilon();
675 static inline double sfmin() {
676 return std::numeric_limits<double>::min();
678 static inline double base() {
679 return std::numeric_limits<double>::radix;
681 static inline double prec() {
684 static inline double t() {
685 return std::numeric_limits<double>::digits;
687 static inline double rnd() {
688 return ( std::numeric_limits<double>::round_style == std::round_to_nearest ?
double(1.0) :
double(0.0) );
690 static inline double emin() {
691 return std::numeric_limits<double>::min_exponent;
693 static inline double rmin() {
694 return std::numeric_limits<double>::min();
696 static inline double emax() {
697 return std::numeric_limits<double>::max_exponent;
699 static inline double rmax() {
700 return std::numeric_limits<double>::max();
705 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
706 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
710 static inline double zero() {
return 0.0; }
711 static inline double one() {
return 1.0; }
712 static inline double conjugate(
double x) {
return(x); }
713 static inline double real(
double x) {
return(x); }
714 static inline double imag(
double) {
return(0); }
715 static inline double nan() {
717 return 0.0/std::sin(0.0);
719 return std::numeric_limits<double>::quiet_NaN();
722 static inline bool isnaninf(
double x) {
723 return generic_real_isnaninf<double>(x);
725 static inline void seedrandom(
unsigned int s) {
733 static inline double random() {
double rnd = (double) std::rand() / RAND_MAX;
return (
double)(-1.0 + 2.0 *
rnd); }
734 static inline std::string
name() {
return "double"; }
738 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
739 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
742 const double rtn = std::sqrt(x);
747 static inline double pow(
double x,
double y) {
return std::pow(x,y); }
748 static inline double pi() {
return 3.14159265358979323846; }
749 static inline double log(
double x) {
return std::log(x); }
750 static inline double log10(
double x) {
return std::log10(x); }
754 #ifdef HAVE_TEUCHOS_LONG_DOUBLE
756 struct ScalarTraits<long double>
766 static inline long double eps() {
767 return std::numeric_limits<long double>::epsilon();
769 static inline long double sfmin() {
770 return std::numeric_limits<long double>::min();
772 static inline long double base() {
773 return std::numeric_limits<long double>::radix;
775 static inline long double prec() {
778 static inline long double t() {
779 return std::numeric_limits<long double>::digits;
781 static inline long double rnd() {
782 return ( std::numeric_limits<long double>::round_style == std::round_to_nearest ? (
long double)(1.0) : (
long double)(0.0) );
784 static inline long double emin() {
785 return std::numeric_limits<long double>::min_exponent;
787 static inline long double rmin() {
788 return std::numeric_limits<long double>::min();
790 static inline long double emax() {
791 return std::numeric_limits<long double>::max_exponent;
793 static inline long double rmax() {
794 return std::numeric_limits<long double>::max();
799 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
800 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
804 static inline long double zero() {
return 0.0; }
805 static inline long double one() {
return 1.0; }
806 static inline long double conjugate(
long double x) {
return(x); }
807 static inline long double real(
long double x) {
return(x); }
808 static inline long double imag(
long double) {
return(0); }
809 static inline long double nan() {
811 return 0.0/std::sin(0.0);
813 return std::numeric_limits<long double>::quiet_NaN();
816 static inline bool isnaninf(
long double x) {
817 return generic_real_isnaninf<long double>(x);
819 static inline void seedrandom(
unsigned int s) {
827 static inline long double random() {
long double rnd = (
long double) std::rand() / RAND_MAX;
return (
long double)(-1.0 + 2.0 *
rnd); }
828 static inline std::string
name() {
return "long double"; }
829 static inline long double squareroot(
long double x)
832 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
833 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
836 const long double rtn = std::sqrt(x);
841 static inline long double pow(
long double x,
long double y) {
return std::pow(x,y); }
842 static inline long double pi() {
return 3.14159265358979323846264338327950288419716939937510; }
843 static inline long double log(
double x) {
return std::log(x); }
844 static inline long double log10(
double x) {
return std::log10(x); }
848 #ifdef HAVE_TEUCHOSCORE_QUADMATH
851 struct ScalarTraits<__float128> {
865 static __float128
eps () {
866 return FLT128_EPSILON;
868 static __float128
sfmin () {
871 static __float128
base () {
874 static __float128
prec () {
877 static __float128
t () {
878 return FLT128_MANT_DIG;
880 static __float128
rnd () {
883 static __float128
emin () {
884 return FLT128_MIN_EXP;
886 static __float128
rmin () {
889 static __float128
emax () {
890 return FLT128_MAX_EXP;
892 static __float128
rmax () {
898 static __float128
zero () {
901 static __float128
one () {
904 static __float128
conjugate (
const __float128& x) {
907 static __float128
real (
const __float128& x) {
910 static __float128
imag (
const __float128& ) {
913 static __float128
nan () {
914 return strtoflt128 (
"NAN()", NULL);
916 static bool isnaninf (
const __float128& x) {
917 return isinfq (x) || isnanq (x);
919 static inline void seedrandom (
unsigned int s) {
927 static __float128
random () {
930 const __float128 scalingFactor =
931 static_cast<__float128
> (std::numeric_limits<double>::min ()) /
932 static_cast<__float128> (2.0);
933 const __float128 higherOrderTerm =
935 const __float128 lowerOrderTerm =
938 return higherOrderTerm + lowerOrderTerm;
940 static std::string
name () {
943 static __float128
squareroot (
const __float128& x) {
946 static __float128
pow (
const __float128& x,
const __float128& y) {
949 static __float128
pi() {
return 3.14159265358979323846; }
950 static __float128 log (
const __float128& x) {
953 static __float128 log10 (
const __float128& x) {
957 #endif // HAVE_TEUCHOSCORE_QUADMATH
961 #ifdef HAVE_TEUCHOS_QD
963 bool operator&&(
const dd_real &a,
const dd_real &b);
964 bool operator&&(
const qd_real &a,
const qd_real &b);
967 struct ScalarTraits<dd_real>
977 static inline dd_real
eps() {
return std::numeric_limits<dd_real>::epsilon(); }
978 static inline dd_real
sfmin() {
return std::numeric_limits<dd_real>::min(); }
979 static inline dd_real
base() {
return std::numeric_limits<dd_real>::radix; }
980 static inline dd_real
prec() {
return eps()*
base(); }
981 static inline dd_real
t() {
return std::numeric_limits<dd_real>::digits; }
982 static inline dd_real
rnd() {
return ( std::numeric_limits<dd_real>::round_style == std::round_to_nearest ? dd_real(1.0) : dd_real(0.0) ); }
983 static inline dd_real
emin() {
return std::numeric_limits<dd_real>::min_exponent; }
984 static inline dd_real
rmin() {
return std::numeric_limits<dd_real>::min(); }
985 static inline dd_real
emax() {
return std::numeric_limits<dd_real>::max_exponent; }
986 static inline dd_real
rmax() {
return std::numeric_limits<dd_real>::max(); }
990 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
991 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
995 static inline dd_real
zero() {
return dd_real(0.0); }
996 static inline dd_real
one() {
return dd_real(1.0); }
997 static inline dd_real
conjugate(dd_real x) {
return(x); }
998 static inline dd_real
real(dd_real x) {
return x ; }
999 static inline dd_real
imag(dd_real) {
return zero(); }
1000 static inline dd_real
nan() {
return dd_real::_nan; }
1001 static inline bool isnaninf(dd_real x) {
return isnan(x) || isinf(x); }
1002 static inline void seedrandom(
unsigned int s) {
1011 static inline dd_real
random() {
return ddrand(); }
1012 static inline std::string
name() {
return "dd_real"; }
1015 #ifdef TEUCHOS_DEBUG
1016 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1017 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
1021 static inline dd_real
pow(dd_real x, dd_real y) { return ::pow(x,y); }
1022 static inline dd_real
pi() {
return 3.14159265358979323846; }
1024 static inline dd_real log(dd_real x) { return ::log(x); }
1025 static inline dd_real log10(dd_real x) { return ::log10(x); }
1030 struct ScalarTraits<qd_real>
1040 static inline qd_real
eps() {
return std::numeric_limits<qd_real>::epsilon(); }
1041 static inline qd_real
sfmin() {
return std::numeric_limits<qd_real>::min(); }
1042 static inline qd_real
base() {
return std::numeric_limits<qd_real>::radix; }
1043 static inline qd_real
prec() {
return eps()*
base(); }
1044 static inline qd_real
t() {
return std::numeric_limits<qd_real>::digits; }
1045 static inline qd_real
rnd() {
return ( std::numeric_limits<qd_real>::round_style == std::round_to_nearest ? qd_real(1.0) : qd_real(0.0) ); }
1046 static inline qd_real
emin() {
return std::numeric_limits<qd_real>::min_exponent; }
1047 static inline qd_real
rmin() {
return std::numeric_limits<qd_real>::min(); }
1048 static inline qd_real
emax() {
return std::numeric_limits<qd_real>::max_exponent; }
1049 static inline qd_real
rmax() {
return std::numeric_limits<qd_real>::max(); }
1052 #ifdef TEUCHOS_DEBUG
1053 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1054 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
1058 static inline qd_real
zero() {
return qd_real(0.0); }
1059 static inline qd_real
one() {
return qd_real(1.0); }
1060 static inline qd_real
conjugate(qd_real x) {
return(x); }
1061 static inline qd_real
real(qd_real x) {
return x ; }
1062 static inline qd_real
imag(qd_real) {
return zero(); }
1063 static inline qd_real
nan() {
return qd_real::_nan; }
1064 static inline bool isnaninf(qd_real x) {
return isnan(x) || isinf(x); }
1065 static inline void seedrandom(
unsigned int s) {
1074 static inline qd_real
random() {
return qdrand(); }
1075 static inline std::string
name() {
return "qd_real"; }
1078 #ifdef TEUCHOS_DEBUG
1079 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1080 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
1084 static inline qd_real
pow(qd_real x, qd_real y) { return ::pow(x,y); }
1085 static inline qd_real
pi() {
return 3.14159265358979323846; }
1087 static inline qd_real log(qd_real x) { return ::log(x); }
1088 static inline qd_real log10(qd_real x) { return ::log10(x); }
1092 #endif // HAVE_TEUCHOS_QD
1095 #ifdef HAVE_TEUCHOS_GNU_MP
1098 extern gmp_randclass gmp_rng;
1118 struct ScalarTraits<mpf_class>
1128 static inline mpf_class
zero() { mpf_class
zero = 0.0;
return zero; }
1129 static inline mpf_class
one() { mpf_class
one = 1.0;
return one; }
1130 static inline mpf_class
conjugate(mpf_class x) {
return x; }
1131 static inline mpf_class
real(mpf_class x) {
return(x); }
1132 static inline mpf_class
imag(mpf_class x) {
return(0); }
1133 static inline bool isnaninf(mpf_class x) {
return false; }
1134 static inline void seedrandom(
unsigned int s) {
1135 unsigned long int seedVal =
static_cast<unsigned long int>(s);
1136 gmp_rng.seed( seedVal );
1138 static inline mpf_class
random() {
1139 return gmp_rng.get_f();
1141 static inline std::string
name() {
return "mpf_class"; }
1142 static inline mpf_class
squareroot(mpf_class x) {
return std::sqrt(x); }
1143 static inline mpf_class
pow(mpf_class x, mpf_class y) {
return pow(x,y); }
1147 #endif // HAVE_TEUCHOS_GNU_MP
1149 #ifdef HAVE_TEUCHOS_ARPREC
1154 struct ScalarTraits<mp_real>
1166 static inline mp_real
zero() { mp_real
zero = 0.0;
return zero; }
1167 static inline mp_real
one() { mp_real
one = 1.0;
return one; }
1168 static inline mp_real
conjugate(mp_real x) {
return x; }
1169 static inline mp_real
real(mp_real x) {
return(x); }
1170 static inline mp_real
imag(mp_real x) {
return zero(); }
1171 static inline bool isnaninf(mp_real x) {
return false; }
1172 static inline void seedrandom(
unsigned int s) {
1173 long int seedVal =
static_cast<long int>(s);
1176 static inline mp_real
random() {
return mp_rand(); }
1177 static inline std::string
name() {
return "mp_real"; }
1178 static inline mp_real
squareroot(mp_real x) {
return sqrt(x); }
1179 static inline mp_real
pow(mp_real x, mp_real y) {
return pow(x,y); }
1180 static inline mp_real
pi() {
return 3.14159265358979323846; }
1185 #endif // HAVE_TEUCHOS_ARPREC
1188 #ifdef HAVE_TEUCHOS_COMPLEX
1193 struct ScalarTraits<
1197 typedef std::complex<T> ComplexT;
1198 typedef std::complex<typename ScalarTraits<T>::halfPrecision>
halfPrecision;
1199 typedef std::complex<typename ScalarTraits<T>::doublePrecision>
doublePrecision;
1200 typedef typename ScalarTraits<T>::magnitudeType
magnitudeType;
1203 static const bool isOrdinal = ScalarTraits<T>::isOrdinal;
1218 #ifdef TEUCHOS_DEBUG
1219 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1220 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
1224 static inline ComplexT
zero() {
return ComplexT(ScalarTraits<magnitudeType>::zero(),ScalarTraits<magnitudeType>::zero()); }
1225 static inline ComplexT
one() {
return ComplexT(ScalarTraits<magnitudeType>::one(),ScalarTraits<magnitudeType>::zero()); }
1226 static inline ComplexT
conjugate(ComplexT a){
return ComplexT(a.real(),-a.imag()); }
1229 static inline ComplexT
nan() {
return ComplexT(ScalarTraits<magnitudeType>::nan(),ScalarTraits<magnitudeType>::nan()); }
1232 static inline ComplexT
random()
1236 return ComplexT(rnd1,rnd2);
1238 static inline std::string
name() {
return std::string(
"std::complex<")+std::string(ScalarTraits<magnitudeType>::name())+std::string(
">"); }
1240 static inline ComplexT
squareroot(ComplexT x)
1242 #ifdef TEUCHOS_DEBUG
1243 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1244 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
1246 typedef ScalarTraits<magnitudeType> STMT;
1247 const T r = x.real(), i = x.imag(),
zero = STMT::zero(), two = 2.0;
1248 const T a = STMT::squareroot((r*r)+(i*i));
1249 const T nr = STMT::squareroot((a+r)/two);
1250 const T ni = ( i ==
zero ?
zero : STMT::squareroot((a-r)/two) );
1251 return ComplexT(nr,ni);
1265 static inline ComplexT
pow(ComplexT x, ComplexT y) {
return pow(x,y); }
1268 #endif // HAVE_TEUCHOS_COMPLEX
1270 #ifdef HAVE_TEUCHOSCORE_KOKKOS
1273 struct ScalarTraits<
1277 typedef Kokkos::complex<T> ComplexT;
1278 typedef Kokkos::complex<typename ScalarTraits<T>::halfPrecision>
halfPrecision;
1279 typedef Kokkos::complex<typename ScalarTraits<T>::doublePrecision>
doublePrecision;
1280 typedef typename ScalarTraits<T>::magnitudeType
magnitudeType;
1283 static const bool isOrdinal = ScalarTraits<T>::isOrdinal;
1298 #ifdef TEUCHOS_DEBUG
1299 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1300 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
1302 return std::abs(std::complex<T>(a));
1304 static inline ComplexT
zero() {
return ComplexT(ScalarTraits<magnitudeType>::zero(),ScalarTraits<magnitudeType>::zero()); }
1305 static inline ComplexT
one() {
return ComplexT(ScalarTraits<magnitudeType>::one(),ScalarTraits<magnitudeType>::zero()); }
1306 static inline ComplexT
conjugate(ComplexT a){
return ComplexT(a.real(),-a.imag()); }
1309 static inline ComplexT
nan() {
return ComplexT(ScalarTraits<magnitudeType>::nan(),ScalarTraits<magnitudeType>::nan()); }
1312 static inline ComplexT
random()
1316 return ComplexT(rnd1,rnd2);
1318 static inline std::string
name() {
return std::string(
"Kokkos::complex<")+std::string(ScalarTraits<magnitudeType>::name())+std::string(
">"); }
1320 static inline ComplexT
squareroot(ComplexT x)
1322 #ifdef TEUCHOS_DEBUG
1323 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1324 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
1326 typedef ScalarTraits<magnitudeType> STMT;
1327 const T r = x.real(), i = x.imag(),
zero = STMT::zero(), two = 2.0;
1328 const T a = STMT::squareroot((r*r)+(i*i));
1329 const T nr = STMT::squareroot((a+r)/two);
1330 const T ni = ( i ==
zero ?
zero : STMT::squareroot((a-r)/two) );
1331 return ComplexT(nr,ni);
1333 static inline ComplexT
pow(ComplexT x, ComplexT y) {
return pow(std::complex<T>(x), std::complex<T>(y)); }
1336 #endif // HAVE_TEUCHOSCORE_KOKKOS
1338 #endif // DOXYGEN_SHOULD_SKIP_THIS
1342 #endif // _TEUCHOS_SCALARTRAITS_HPP_
T magnitudeType
Mandatory typedef for result of magnitude.
static magnitudeType eps()
Returns relative machine precision.
static T squareroot(T x)
Returns a number of magnitudeType that is the square root of this scalar type x.
static const bool isComparable
Determines if scalar type supports relational operators such as <, >, <=, >=.
static magnitudeType real(T a)
Returns the real part of the scalar type a.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
Declaration and default implementation for basic traits for the scalar field type.
static T pow(T x, T y)
Returns the result of raising one scalar x to the power y.
static magnitudeType emax()
Returns the largest exponent before overflow.
static magnitudeType base()
Returns the base of the machine.
static const bool hasMachineParameters
Determines if scalar type have machine-specific parameters (i.e. eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax() are supported).
static std::string name()
Returns the name of this scalar type.
static magnitudeType rmax()
Overflow theshold - (base^emax)*(1-eps)
static magnitudeType sfmin()
Returns safe minimum (sfmin), such that 1/sfmin does not overflow.
static T pi()
Returns the value of PI.
T coordinateType
Typedef for coordinates.
static T conjugate(T a)
Returns the conjugate of the scalar type a.
static const bool isOrdinal
Determines if scalar type is an ordinal type.
static magnitudeType prec()
Returns eps*base.
static magnitudeType t()
Returns the number of (base) digits in the mantissa.
static magnitudeType rmin()
Returns the underflow threshold - base^(emin-1)
T doublePrecision
Typedef for double precision.
static void seedrandom(unsigned int s)
Seed the random number generator returned by random().
static magnitudeType imag(T a)
Returns the imaginary part of the scalar type a.
static bool isnaninf(const T &x)
Returns true if x is NaN or Inf.
static magnitudeType magnitude(T a)
Returns the magnitudeType of the scalar type a.
static T nan()
Returns a number that represents NaN.
static T zero()
Returns representation of zero for this scalar type.
static T random()
Returns a random number (between -one() and +one()) of this scalar type.
T halfPrecision
Typedef for half precision.
static const bool isComplex
Determines if scalar type is std::complex.
static magnitudeType emin()
Returns the minimum exponent before (gradual) underflow.
static magnitudeType rnd()
Returns 1.0 when rounding occurs in addition, 0.0 otherwise.
static T one()
Returns representation of one for this scalar type.