10 #ifndef BASKER_SCALARTRAITS_HPP
11 #define BASKER_SCALARTRAITS_HPP
13 #define MY_SCALAR_ABS(x) (((x) < 0) ? -(x) : (x))
17 struct BASKER_ScalarTraits
19 typedef T magnitudeType;
20 static inline double reciprocal(
double c ){
return 0;}
21 static inline double divide(
double a,
double b){
return 0;}
22 static inline magnitudeType approxABS(
double a){
return 0;}
23 static inline magnitudeType abs(
double a){
return 0;}
24 static inline bool gt (
double a,
double b){
return 0;}
29 struct BASKER_ScalarTraits<double>
31 typedef double magnitudeType;
32 static inline double reciprocal(
double c){
return 1.0/c;}
33 static inline double divide(
double a,
double b){
return a/b;}
34 static inline magnitudeType approxABS(
double a)
35 {
return (MY_SCALAR_ABS(a));}
36 static inline magnitudeType abs(
double a)
37 {
return (MY_SCALAR_ABS(a));}
38 static inline bool gt (
double a,
double b){
return (a>b);}
44 struct BASKER_ScalarTraits<float>
46 typedef float magnitudeType;
47 static inline float reciprocal(
float c){
return 1.0/c;}
48 static inline float divide(
float a,
float b){
return a/b;}
49 static inline magnitudeType approxABS(
float a)
50 {
return (MY_SCALAR_ABS(a));}
51 static inline magnitudeType abs(
float a)
52 {
return (MY_SCALAR_ABS(a));}
53 static inline bool gt(
float a,
float b){
return (a>b);}
60 #ifdef HAVE_TEUCHOS_COMPLEX
63 struct BASKER_ScalarTraits< std::complex<T> >
65 typedef std::complex<T> ComplexT ;
66 typedef typename BASKER_ScalarTraits<T>::magnitudeType magnitudeType ;
68 static inline ComplexT reciprocal (ComplexT c)
72 cr = (Teuchos::ScalarTraits<ComplexT>::real(c)) ;
73 ci = (Teuchos::ScalarTraits<ComplexT>::imag(c)) ;
74 if (MY_SCALAR_ABS (cr) >= MY_SCALAR_ABS (ci))
78 ret = std::complex<T>(1.0 / den, -r / den) ;
84 ret = std::complex<T>(r / den, -1.0 / den) ;
89 static inline ComplexT divide (ComplexT a, ComplexT b)
91 T r, den, ar, ai, br, bi ;
94 br = (Teuchos::ScalarTraits<ComplexT>::real(b)) ;
95 bi = (Teuchos::ScalarTraits<ComplexT>::imag(b)) ;
96 ar = (Teuchos::ScalarTraits<ComplexT>::real(a)) ;
97 ai = (Teuchos::ScalarTraits<ComplexT>::imag(a)) ;
98 if (MY_SCALAR_ABS (br) >= MY_SCALAR_ABS (bi))
102 ret = std::complex<T>((ar + ai * r) / den, (ai - ar * r) / den) ;
108 ret = std::complex<T>((ar * r + ai) / den, (ai * r - ar) / den) ;
113 static inline magnitudeType approxABS (ComplexT a)
115 return ( MY_SCALAR_ABS (Teuchos::ScalarTraits<ComplexT>::real(a)) +
116 MY_SCALAR_ABS (Teuchos::ScalarTraits<ComplexT>::imag(a)) ) ;
119 static inline magnitudeType abs (ComplexT a)
124 ar = MY_SCALAR_ABS (Teuchos::ScalarTraits<ComplexT>::real(a)) ;
125 ai = MY_SCALAR_ABS (Teuchos::ScalarTraits<ComplexT>::imag(a)) ;
135 (s) = ar * sqrt (1.0 + r*r) ;
147 (s) = ai * sqrt (1.0 + r*r) ;
152 static inline bool gt(ComplexT a, ComplexT b)
154 return( (Teuchos::ScalarTraits<ComplexT>::real(a)+Teuchos::ScalarTraits<ComplexT>::imag(a)) > (Teuchos::ScalarTraits<ComplexT>::real(b) + Teuchos::ScalarTraits<ComplexT>::imag(b)));
164 struct BASKER_ScalarTraits< std::complex<T> >
166 typedef std::complex<T> ComplexT ;
167 typedef typename BASKER_ScalarTraits<T>::magnitudeType magnitudeType ;
169 static inline ComplexT reciprocal (ComplexT c)
173 cr = (std::real(c)) ;
174 ci = (std::imag(c)) ;
175 if (MY_SCALAR_ABS (cr) >= MY_SCALAR_ABS (ci))
179 ret = std::complex<T>(1.0 / den, -r / den) ;
185 ret = std::complex<T>(r / den, -1.0 / den) ;
190 static inline ComplexT divide (ComplexT a, ComplexT b)
192 T r, den, ar, ai, br, bi ;
195 br = (std::real(b)) ;
196 bi = (std::imag(b)) ;
197 ar = (std::real(a)) ;
198 ai = (std::imag(a)) ;
199 if (MY_SCALAR_ABS (br) >= MY_SCALAR_ABS (bi))
203 ret = std::complex<T>((ar + ai * r) / den, (ai - ar * r) / den) ;
209 ret = std::complex<T>((ar * r + ai) / den, (ai * r - ar) / den) ;
214 static inline magnitudeType approxABS (ComplexT a)
216 return ( MY_SCALAR_ABS (std::real(a)) +
217 MY_SCALAR_ABS (std::imag(a)) ) ;
220 static inline magnitudeType abs (ComplexT a)
225 ar = MY_SCALAR_ABS (std::real(a)) ;
226 ai = MY_SCALAR_ABS (std::imag(a)) ;
236 (s) = ar * sqrt (1.0 + r*r) ;
248 (s) = ai * sqrt (1.0 + r*r) ;
253 static inline bool gt(ComplexT a, ComplexT b)
255 return ((std::real(a)+std::imag(a)) > (std::real(b) + std::imag(b)));
261 #endif // HAVE _TEUCHOS_COMPLEX