Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_Tay_ScalarTraitsImp.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef SACADO_TAY_SCALARTRAITSIMP_HPP
11 #define SACADO_TAY_SCALARTRAITSIMP_HPP
12 
13 #include "Sacado_ConfigDefs.h"
14 
15 #ifdef HAVE_SACADO_TEUCHOSCORE
16 
17 #include "Teuchos_ScalarTraits.hpp"
19 #include "Sacado_mpl_apply.hpp"
20 
21 namespace Sacado {
22 
23  namespace Tay {
24 
26  template <typename TayType>
27  struct ScalarTraitsImp {
28  typedef typename Sacado::ValueType<TayType>::type ValueT;
29 
30  typedef typename mpl::apply<TayType,typename Teuchos::ScalarTraits<ValueT>::magnitudeType>::type magnitudeType;
31  typedef typename mpl::apply<TayType,typename Teuchos::ScalarTraits<ValueT>::halfPrecision>::type halfPrecision;
32  typedef typename mpl::apply<TayType,typename Teuchos::ScalarTraits<ValueT>::doublePrecision>::type doublePrecision;
33 
34  static const bool isComplex = Teuchos::ScalarTraits<ValueT>::isComplex;
35  static const bool isOrdinal = Teuchos::ScalarTraits<ValueT>::isOrdinal;
36  static const bool isComparable =
38  static const bool hasMachineParameters =
40  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType eps() {
42  }
43  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType sfmin() {
45  }
46  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType base() {
48  }
49  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType prec() {
51  }
54  }
57  }
58  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emin() {
60  }
61  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmin() {
63  }
64  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emax() {
66  }
67  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmax() {
69  }
70  static magnitudeType magnitude(const TayType& a) {
71 #ifdef TEUCHOS_DEBUG
72  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
73  a, "Error, the input value to magnitude(...) a = " << a <<
74  " can not be NaN!" );
75  TEUCHOS_TEST_FOR_EXCEPTION(is_tay_real(a) == false, std::runtime_error,
76  "Complex magnitude is not a differentiable "
77  "function of complex inputs.");
78 #endif
79  //return std::fabs(a);
80  magnitudeType b(a.degree(),
82  if (Teuchos::ScalarTraits<ValueT>::real(a.val()) >= 0)
83  for (int i=1; i<=a.degree(); i++)
84  b.fastAccessCoeff(i) =
85  Teuchos::ScalarTraits<ValueT>::magnitude(a.fastAccessCoeff(i));
86  else
87  for (int i=1; i<=a.degree(); i++)
88  b.fastAccessCoeff(i) =
89  -Teuchos::ScalarTraits<ValueT>::magnitude(a.fastAccessCoeff(i));
90  return b;
91  }
92  static ValueT zero() {
93  return ValueT(0.0);
94  }
95  static ValueT one() {
96  return ValueT(1.0);
97  }
98 
99  // Conjugate is only defined for real derivative components
100  static TayType conjugate(const TayType& x) {
101 #ifdef TEUCHOS_DEBUG
102  TEUCHOS_TEST_FOR_EXCEPTION(is_tay_real(x) == false, std::runtime_error,
103  "Complex conjugate is not a differentiable "
104  "function of complex inputs.");
105 #endif
106  TayType y = x;
107  y.copyForWrite();
108  y.val() = Teuchos::ScalarTraits<ValueT>::conjugate(x.val());
109  return y;
110  }
111 
112  // Real part is only defined for real derivative components
113  static TayType real(const TayType& x) {
114 #ifdef TEUCHOS_DEBUG
115  TEUCHOS_TEST_FOR_EXCEPTION(is_tay_real(x) == false, std::runtime_error,
116  "Real component is not a differentiable "
117  "function of complex inputs.");
118 #endif
119  TayType y = x;
120  y.copyForWrite();
121  y.val() = Teuchos::ScalarTraits<ValueT>::real(x.val());
122  return y;
123  }
124 
125  // Imaginary part is only defined for real derivative components
126  static TayType imag(const TayType& x) {
127 #ifdef TEUCHOS_DEBUG
128  TEUCHOS_TEST_FOR_EXCEPTION(is_tay_real(x) == false, std::runtime_error,
129  "Imaginary component is not a differentiable "
130  "function of complex inputs.");
131 #endif
132  return TayType(Teuchos::ScalarTraits<ValueT>::imag(x.val()));
133  }
134 
135  static ValueT nan() {
137  }
138  static bool isnaninf(const TayType& x) {
139  for (int i=0; i<=x.degree(); i++)
140  if (Teuchos::ScalarTraits<ValueT>::isnaninf(x.fastAccessCoeff(i)))
141  return true;
142  return false;
143  }
144  static void seedrandom(unsigned int s) {
146  }
147  static ValueT random() {
149  }
150  static std::string name() {
152  }
153  static TayType squareroot(const TayType& x) {
154 #ifdef TEUCHOS_DEBUG
155  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
156  x, "Error, the input value to squareroot(...) a = " << x <<
157  " can not be NaN!" );
158 #endif
159  return std::sqrt(x);
160  }
161  static TayType pow(const TayType& x, const TayType& y) {
162  return std::pow(x,y);
163  }
164 
165  // Helper function to determine whether a complex value is real
166  static bool is_complex_real(const ValueT& x) {
167  return
169  }
170 
171  // Helper function to determine whether a Fad type is real
172  static bool is_tay_real(const TayType& x) {
173  if (x.size() == 0)
174  return true;
176  for (int i=0; i<=x.degree(); i++)
177  if (!is_complex_real(x.fastAccessCoeff(i)))
178  return false;
179  }
180  return true;
181  }
182 
183  }; // class ScalarTraitsImp
184 
185  } // namespace Tay
186 
187 } // namespace Sacado
188 
189 #endif // HAVE_SACADO_TEUCHOSCORE
190 
191 #endif // SACADO_FAD_SCALARTRAITSIMP_HPP
static std::string eval()
static magnitudeType eps()
PowExprType< Expr< T1 >, Expr< T2 > >::expr_type pow(const Expr< T1 > &expr1, const Expr< T2 > &expr2)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
static magnitudeType real(T a)
static magnitudeType emax()
static magnitudeType base()
static magnitudeType rmax()
static magnitudeType sfmin()
static T conjugate(T a)
static magnitudeType prec()
static magnitudeType t()
sqrt(expr.val())
static magnitudeType rmin()
static void seedrandom(unsigned int s)
static magnitudeType magnitude(T a)
Sacado::Random< double > rnd
SACADO_INLINE_FUNCTION mpl::enable_if_c< ExprLevel< Expr< T1 > >::value==ExprLevel< Expr< T2 > >::value, Expr< PowerOp< Expr< T1 >, Expr< T2 > > > >::type pow(const Expr< T1 > &expr1, const Expr< T2 > &expr2)
static magnitudeType emin()
static magnitudeType rnd()
const double y