30#ifndef SACADO_FAD_SCALARTRAITSIMP_HPP
31#define SACADO_FAD_SCALARTRAITSIMP_HPP
35#ifdef HAVE_SACADO_TEUCHOSCORE
37#include "Teuchos_ScalarTraits.hpp"
38#include "Teuchos_TestForException.hpp"
46 template <
typename FadType>
47 struct ScalarTraitsImp {
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;
54 static const bool isComplex = Teuchos::ScalarTraits<ValueT>::isComplex;
55 static const bool isOrdinal = Teuchos::ScalarTraits<ValueT>::isOrdinal;
56 static const bool isComparable =
57 Teuchos::ScalarTraits<ValueT>::isComparable;
58 static const bool hasMachineParameters =
59 Teuchos::ScalarTraits<ValueT>::hasMachineParameters;
60 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType eps() {
61 return Teuchos::ScalarTraits<ValueT>::eps();
63 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType sfmin() {
64 return Teuchos::ScalarTraits<ValueT>::sfmin();
66 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType base() {
67 return Teuchos::ScalarTraits<ValueT>::base();
69 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType prec() {
70 return Teuchos::ScalarTraits<ValueT>::prec();
72 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType t() {
73 return Teuchos::ScalarTraits<ValueT>::t();
75 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType
rnd() {
76 return Teuchos::ScalarTraits<ValueT>::rnd();
78 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emin() {
79 return Teuchos::ScalarTraits<ValueT>::emin();
81 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmin() {
82 return Teuchos::ScalarTraits<ValueT>::rmin();
84 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emax() {
85 return Teuchos::ScalarTraits<ValueT>::emax();
87 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmax() {
88 return Teuchos::ScalarTraits<ValueT>::rmax();
90 static magnitudeType magnitude(
const FadType&
a) {
92 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
93 a,
"Error, the input value to magnitude(...) a = " <<
a <<
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.");
100 magnitudeType b(
a.size(),
101 Teuchos::ScalarTraits<ValueT>::magnitude(
a.val()));
102 if (Teuchos::ScalarTraits<ValueT>::real(
a.val()) >= 0)
103 for (
int i=0;
i<
a.size();
i++)
105 Teuchos::ScalarTraits<ValueT>::magnitude(
a.fastAccessDx(
i));
107 for (
int i=0;
i<
a.size();
i++)
109 -Teuchos::ScalarTraits<ValueT>::magnitude(
a.fastAccessDx(
i));
112 static ValueT zero() {
115 static ValueT one() {
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.");
127 y.val() = Teuchos::ScalarTraits<ValueT>::conjugate(
x.val());
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.");
139 y.val() = Teuchos::ScalarTraits<ValueT>::real(
x.val());
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.");
150 return FadType(Teuchos::ScalarTraits<ValueT>::imag(
x.val()));
153 static ValueT nan() {
154 return Teuchos::ScalarTraits<ValueT>::nan();
156 static bool isnaninf(
const FadType&
x) {
157 if (Teuchos::ScalarTraits<ValueT>::isnaninf(
x.val()))
159 for (
int i=0;
i<
x.size();
i++)
160 if (Teuchos::ScalarTraits<ValueT>::isnaninf(
x.dx(
i)))
164 static void seedrandom(
unsigned int s) {
165 Teuchos::ScalarTraits<ValueT>::seedrandom(s);
167 static ValueT random() {
168 return Teuchos::ScalarTraits<ValueT>::random();
170 static std::string name() {
175 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
176 x,
"Error, the input value to squareroot(...) a = " <<
x <<
177 " can not be NaN!" );
182 return std::pow(
x,
y);
186 static bool is_complex_real(
const ValueT&
x) {
188 Teuchos::ScalarTraits<ValueT>::magnitude(
x-Teuchos::ScalarTraits<ValueT>::real(
x)) == 0;
192 static bool is_fad_real(
const FadType&
x) {
195 if (Teuchos::ScalarTraits<ValueT>::isComplex) {
196 if (!is_complex_real(
x.val()))
198 for (
int i=0;
i<
x.size();
i++)
199 if (!is_complex_real(
x.fastAccessDx(
i)))
Sacado::Random< double > rnd
Sacado::Fad::DFad< double > FadType
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 std::string eval()