29 #ifndef BASKER_SCALARTRAITS_HPP
30 #define BASKER_SCALARTRAITS_HPP
32 #define MY_SCALAR_ABS(x) (((x) < 0) ? -(x) : (x))
36 struct BASKER_ScalarTraits
38 typedef T magnitudeType;
39 static inline double reciprocal(
double c ){
return 0;}
40 static inline double divide(
double a,
double b){
return 0;}
41 static inline magnitudeType approxABS(
double a){
return 0;}
42 static inline magnitudeType abs(
double a){
return 0;}
43 static inline bool gt (
double a,
double b){
return 0;}
48 struct BASKER_ScalarTraits<double>
50 typedef double magnitudeType;
51 static inline double reciprocal(
double c){
return 1.0/c;}
52 static inline double divide(
double a,
double b){
return a/b;}
53 static inline magnitudeType approxABS(
double a)
54 {
return (MY_SCALAR_ABS(a));}
55 static inline magnitudeType abs(
double a)
56 {
return (MY_SCALAR_ABS(a));}
57 static inline bool gt (
double a,
double b){
return (a>b);}
63 struct BASKER_ScalarTraits<float>
65 typedef float magnitudeType;
66 static inline float reciprocal(
float c){
return 1.0/c;}
67 static inline float divide(
float a,
float b){
return a/b;}
68 static inline magnitudeType approxABS(
float a)
69 {
return (MY_SCALAR_ABS(a));}
70 static inline magnitudeType abs(
float a)
71 {
return (MY_SCALAR_ABS(a));}
72 static inline bool gt(
float a,
float b){
return (a>b);}
79 #ifdef HAVE_TEUCHOS_COMPLEX
82 struct BASKER_ScalarTraits< std::complex<T> >
84 typedef std::complex<T> ComplexT ;
85 typedef typename BASKER_ScalarTraits<T>::magnitudeType magnitudeType ;
87 static inline ComplexT reciprocal (ComplexT c)
91 cr = (Teuchos::ScalarTraits<ComplexT>::real(c)) ;
92 ci = (Teuchos::ScalarTraits<ComplexT>::imag(c)) ;
93 if (MY_SCALAR_ABS (cr) >= MY_SCALAR_ABS (ci))
97 ret = std::complex<T>(1.0 / den, -r / den) ;
103 ret = std::complex<T>(r / den, -1.0 / den) ;
108 static inline ComplexT divide (ComplexT a, ComplexT b)
110 T r, den, ar, ai, br, bi ;
113 br = (Teuchos::ScalarTraits<ComplexT>::real(b)) ;
114 bi = (Teuchos::ScalarTraits<ComplexT>::imag(b)) ;
115 ar = (Teuchos::ScalarTraits<ComplexT>::real(a)) ;
116 ai = (Teuchos::ScalarTraits<ComplexT>::imag(a)) ;
117 if (MY_SCALAR_ABS (br) >= MY_SCALAR_ABS (bi))
121 ret = std::complex<T>((ar + ai * r) / den, (ai - ar * r) / den) ;
127 ret = std::complex<T>((ar * r + ai) / den, (ai * r - ar) / den) ;
132 static inline magnitudeType approxABS (ComplexT a)
134 return ( MY_SCALAR_ABS (Teuchos::ScalarTraits<ComplexT>::real(a)) +
135 MY_SCALAR_ABS (Teuchos::ScalarTraits<ComplexT>::imag(a)) ) ;
138 static inline magnitudeType abs (ComplexT a)
143 ar = MY_SCALAR_ABS (Teuchos::ScalarTraits<ComplexT>::real(a)) ;
144 ai = MY_SCALAR_ABS (Teuchos::ScalarTraits<ComplexT>::imag(a)) ;
154 (s) = ar * sqrt (1.0 + r*r) ;
166 (s) = ai * sqrt (1.0 + r*r) ;
171 static inline bool gt(ComplexT a, ComplexT b)
173 return( (Teuchos::ScalarTraits<ComplexT>::real(a)+Teuchos::ScalarTraits<ComplexT>::imag(a)) > (Teuchos::ScalarTraits<ComplexT>::real(b) + Teuchos::ScalarTraits<ComplexT>::imag(b)));
183 struct BASKER_ScalarTraits< std::complex<T> >
185 typedef std::complex<T> ComplexT ;
186 typedef typename BASKER_ScalarTraits<T>::magnitudeType magnitudeType ;
188 static inline ComplexT reciprocal (ComplexT c)
192 cr = (std::real(c)) ;
193 ci = (std::imag(c)) ;
194 if (MY_SCALAR_ABS (cr) >= MY_SCALAR_ABS (ci))
198 ret = std::complex<T>(1.0 / den, -r / den) ;
204 ret = std::complex<T>(r / den, -1.0 / den) ;
209 static inline ComplexT divide (ComplexT a, ComplexT b)
211 T r, den, ar, ai, br, bi ;
214 br = (std::real(b)) ;
215 bi = (std::imag(b)) ;
216 ar = (std::real(a)) ;
217 ai = (std::imag(a)) ;
218 if (MY_SCALAR_ABS (br) >= MY_SCALAR_ABS (bi))
222 ret = std::complex<T>((ar + ai * r) / den, (ai - ar * r) / den) ;
228 ret = std::complex<T>((ar * r + ai) / den, (ai * r - ar) / den) ;
233 static inline magnitudeType approxABS (ComplexT a)
235 return ( MY_SCALAR_ABS (std::real(a)) +
236 MY_SCALAR_ABS (std::imag(a)) ) ;
239 static inline magnitudeType abs (ComplexT a)
244 ar = MY_SCALAR_ABS (std::real(a)) ;
245 ai = MY_SCALAR_ABS (std::imag(a)) ;
255 (s) = ar * sqrt (1.0 + r*r) ;
267 (s) = ai * sqrt (1.0 + r*r) ;
272 static inline bool gt(ComplexT a, ComplexT b)
274 return ((std::real(a)+std::imag(a)) > (std::real(b) + std::imag(b)));
280 #endif // HAVE _TEUCHOS_COMPLEX