Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_Fad_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_FAD_SCALARTRAITSIMP_HPP
11 #define SACADO_FAD_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 Fad {
24 
26  template <typename FadType>
27  struct ScalarTraitsImp {
28  typedef typename Sacado::ValueType<FadType>::type ValueT;
29 
30  typedef typename mpl::apply<FadType,typename Teuchos::ScalarTraits<ValueT>::magnitudeType>::type magnitudeType;
31  typedef typename mpl::apply<FadType,typename Teuchos::ScalarTraits<ValueT>::halfPrecision>::type halfPrecision;
32  typedef typename mpl::apply<FadType,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 FadType& 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_fad_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.size(),
82  if (Teuchos::ScalarTraits<ValueT>::real(a.val()) >= 0)
83  for (int i=0; i<a.size(); i++)
84  b.fastAccessDx(i) =
86  else
87  for (int i=0; i<a.size(); i++)
88  b.fastAccessDx(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 FadType conjugate(const FadType& x) {
101 #ifdef TEUCHOS_DEBUG
102  TEUCHOS_TEST_FOR_EXCEPTION(is_fad_real(x) == false, std::runtime_error,
103  "Complex conjugate is not a differentiable "
104  "function of complex inputs.");
105 #endif
106  FadType y = x;
107  y.val() = Teuchos::ScalarTraits<ValueT>::conjugate(x.val());
108  return y;
109  }
110 
111  // Real part is only defined for real derivative components
112  static FadType real(const FadType& x) {
113 #ifdef TEUCHOS_DEBUG
114  TEUCHOS_TEST_FOR_EXCEPTION(is_fad_real(x) == false, std::runtime_error,
115  "Real component is not a differentiable "
116  "function of complex inputs.");
117 #endif
118  FadType y = x;
119  y.val() = Teuchos::ScalarTraits<ValueT>::real(x.val());
120  return y;
121  }
122 
123  // Imaginary part is only defined for real derivative components
124  static FadType imag(const FadType& x) {
125 #ifdef TEUCHOS_DEBUG
126  TEUCHOS_TEST_FOR_EXCEPTION(is_fad_real(x) == false, std::runtime_error,
127  "Imaginary component is not a differentiable "
128  "function of complex inputs.");
129 #endif
131  }
132 
133  static ValueT nan() {
135  }
136  static bool isnaninf(const FadType& x) {
138  return true;
139  for (int i=0; i<x.size(); 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 FadType squareroot(const FadType& 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 FadType pow(const FadType& x, const FadType& 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_fad_real(const FadType& x) {
173  if (x.size() == 0)
174  return true;
176  if (!is_complex_real(x.val()))
177  return false;
178  for (int i=0; i<x.size(); i++)
179  if (!is_complex_real(x.fastAccessDx(i)))
180  return false;
181  }
182  return true;
183  }
184 
185  }; // class ScalarTraitsImp
186 
187  } // namespace Fad
188 
189 } // namespace Sacado
190 
191 #endif // HAVE_SACADO_TEUCHOSCORE
192 
193 #endif // SACADO_FAD_SCALARTRAITSIMP_HPP
static std::string eval()
static magnitudeType eps()
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
static magnitudeType real(T a)
Sacado::Fad::DFad< double > FadType
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