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 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 #ifndef SACADO_FAD_SCALARTRAITSIMP_HPP
31 #define SACADO_FAD_SCALARTRAITSIMP_HPP
32 
33 #include "Sacado_ConfigDefs.h"
34 
35 #ifdef HAVE_SACADO_TEUCHOSCORE
36 
37 #include "Teuchos_ScalarTraits.hpp"
39 #include "Sacado_mpl_apply.hpp"
40 
41 namespace Sacado {
42 
43  namespace Fad {
44 
46  template <typename FadType>
47  struct ScalarTraitsImp {
48  typedef typename Sacado::ValueType<FadType>::type ValueT;
49 
50  typedef typename mpl::apply<FadType,typename Teuchos::ScalarTraits<ValueT>::magnitudeType>::type magnitudeType;
51  typedef typename mpl::apply<FadType,typename Teuchos::ScalarTraits<ValueT>::halfPrecision>::type halfPrecision;
52  typedef typename mpl::apply<FadType,typename Teuchos::ScalarTraits<ValueT>::doublePrecision>::type doublePrecision;
53 
54  static const bool isComplex = Teuchos::ScalarTraits<ValueT>::isComplex;
55  static const bool isOrdinal = Teuchos::ScalarTraits<ValueT>::isOrdinal;
56  static const bool isComparable =
58  static const bool hasMachineParameters =
60  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType eps() {
62  }
63  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType sfmin() {
65  }
66  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType base() {
68  }
69  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType prec() {
71  }
74  }
77  }
78  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emin() {
80  }
81  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmin() {
83  }
84  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emax() {
86  }
87  static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmax() {
89  }
90  static magnitudeType magnitude(const FadType& a) {
91 #ifdef TEUCHOS_DEBUG
92  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
93  a, "Error, the input value to magnitude(...) a = " << a <<
94  " can not be NaN!" );
95  TEUCHOS_TEST_FOR_EXCEPTION(is_fad_real(a) == false, std::runtime_error,
96  "Complex magnitude is not a differentiable "
97  "function of complex inputs.");
98 #endif
99  //return std::fabs(a);
100  magnitudeType b(a.size(),
102  if (Teuchos::ScalarTraits<ValueT>::real(a.val()) >= 0)
103  for (int i=0; i<a.size(); i++)
104  b.fastAccessDx(i) =
105  Teuchos::ScalarTraits<ValueT>::magnitude(a.fastAccessDx(i));
106  else
107  for (int i=0; i<a.size(); i++)
108  b.fastAccessDx(i) =
109  -Teuchos::ScalarTraits<ValueT>::magnitude(a.fastAccessDx(i));
110  return b;
111  }
112  static ValueT zero() {
113  return ValueT(0.0);
114  }
115  static ValueT one() {
116  return ValueT(1.0);
117  }
118 
119  // Conjugate is only defined for real derivative components
120  static FadType conjugate(const FadType& x) {
121 #ifdef TEUCHOS_DEBUG
122  TEUCHOS_TEST_FOR_EXCEPTION(is_fad_real(x) == false, std::runtime_error,
123  "Complex conjugate is not a differentiable "
124  "function of complex inputs.");
125 #endif
126  FadType y = x;
127  y.val() = Teuchos::ScalarTraits<ValueT>::conjugate(x.val());
128  return y;
129  }
130 
131  // Real part is only defined for real derivative components
132  static FadType real(const FadType& x) {
133 #ifdef TEUCHOS_DEBUG
134  TEUCHOS_TEST_FOR_EXCEPTION(is_fad_real(x) == false, std::runtime_error,
135  "Real component is not a differentiable "
136  "function of complex inputs.");
137 #endif
138  FadType y = x;
139  y.val() = Teuchos::ScalarTraits<ValueT>::real(x.val());
140  return y;
141  }
142 
143  // Imaginary part is only defined for real derivative components
144  static FadType imag(const FadType& x) {
145 #ifdef TEUCHOS_DEBUG
146  TEUCHOS_TEST_FOR_EXCEPTION(is_fad_real(x) == false, std::runtime_error,
147  "Imaginary component is not a differentiable "
148  "function of complex inputs.");
149 #endif
151  }
152 
153  static ValueT nan() {
155  }
156  static bool isnaninf(const FadType& x) {
158  return true;
159  for (int i=0; i<x.size(); i++)
161  return true;
162  return false;
163  }
164  static void seedrandom(unsigned int s) {
166  }
167  static ValueT random() {
169  }
170  static std::string name() {
172  }
173  static FadType squareroot(const FadType& x) {
174 #ifdef TEUCHOS_DEBUG
175  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
176  x, "Error, the input value to squareroot(...) a = " << x <<
177  " can not be NaN!" );
178 #endif
179  return std::sqrt(x);
180  }
181  static FadType pow(const FadType& x, const FadType& y) {
182  return std::pow(x,y);
183  }
184 
185  // Helper function to determine whether a complex value is real
186  static bool is_complex_real(const ValueT& x) {
187  return
189  }
190 
191  // Helper function to determine whether a Fad type is real
192  static bool is_fad_real(const FadType& x) {
193  if (x.size() == 0)
194  return true;
196  if (!is_complex_real(x.val()))
197  return false;
198  for (int i=0; i<x.size(); i++)
199  if (!is_complex_real(x.fastAccessDx(i)))
200  return false;
201  }
202  return true;
203  }
204 
205  }; // class ScalarTraitsImp
206 
207  } // namespace Fad
208 
209 } // namespace Sacado
210 
211 #endif // HAVE_SACADO_TEUCHOSCORE
212 
213 #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()
KOKKOS_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 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
static magnitudeType emin()
static magnitudeType rnd()