Sacado Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Sacado_Tay_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_TAY_SCALARTRAITSIMP_HPP
31#define SACADO_TAY_SCALARTRAITSIMP_HPP
32
33#include "Sacado_ConfigDefs.h"
34
35#ifdef HAVE_SACADO_TEUCHOSCORE
36
37#include "Teuchos_ScalarTraits.hpp"
38#include "Teuchos_TestForException.hpp"
39#include "Sacado_mpl_apply.hpp"
40
41namespace Sacado {
42
43 namespace Tay {
44
46 template <typename TayType>
47 struct ScalarTraitsImp {
48 typedef typename Sacado::ValueType<TayType>::type ValueT;
49
50 typedef typename mpl::apply<TayType,typename Teuchos::ScalarTraits<ValueT>::magnitudeType>::type magnitudeType;
51 typedef typename mpl::apply<TayType,typename Teuchos::ScalarTraits<ValueT>::halfPrecision>::type halfPrecision;
52 typedef typename mpl::apply<TayType,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 =
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();
62 }
63 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType sfmin() {
64 return Teuchos::ScalarTraits<ValueT>::sfmin();
65 }
66 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType base() {
67 return Teuchos::ScalarTraits<ValueT>::base();
68 }
69 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType prec() {
70 return Teuchos::ScalarTraits<ValueT>::prec();
71 }
72 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType t() {
73 return Teuchos::ScalarTraits<ValueT>::t();
74 }
75 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rnd() {
76 return Teuchos::ScalarTraits<ValueT>::rnd();
77 }
78 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emin() {
79 return Teuchos::ScalarTraits<ValueT>::emin();
80 }
81 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmin() {
82 return Teuchos::ScalarTraits<ValueT>::rmin();
83 }
84 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emax() {
85 return Teuchos::ScalarTraits<ValueT>::emax();
86 }
87 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmax() {
88 return Teuchos::ScalarTraits<ValueT>::rmax();
89 }
90 static magnitudeType magnitude(const TayType& 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_tay_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.degree(),
101 Teuchos::ScalarTraits<ValueT>::magnitude(a.val()));
102 if (Teuchos::ScalarTraits<ValueT>::real(a.val()) >= 0)
103 for (int i=1; i<=a.degree(); i++)
104 b.fastAccessCoeff(i) =
105 Teuchos::ScalarTraits<ValueT>::magnitude(a.fastAccessCoeff(i));
106 else
107 for (int i=1; i<=a.degree(); i++)
108 b.fastAccessCoeff(i) =
109 -Teuchos::ScalarTraits<ValueT>::magnitude(a.fastAccessCoeff(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 TayType conjugate(const TayType& x) {
121#ifdef TEUCHOS_DEBUG
122 TEUCHOS_TEST_FOR_EXCEPTION(is_tay_real(x) == false, std::runtime_error,
123 "Complex conjugate is not a differentiable "
124 "function of complex inputs.");
125#endif
126 TayType y = x;
127 y.copyForWrite();
128 y.val() = Teuchos::ScalarTraits<ValueT>::conjugate(x.val());
129 return y;
130 }
131
132 // Real part is only defined for real derivative components
133 static TayType real(const TayType& x) {
134#ifdef TEUCHOS_DEBUG
135 TEUCHOS_TEST_FOR_EXCEPTION(is_tay_real(x) == false, std::runtime_error,
136 "Real component is not a differentiable "
137 "function of complex inputs.");
138#endif
139 TayType y = x;
140 y.copyForWrite();
141 y.val() = Teuchos::ScalarTraits<ValueT>::real(x.val());
142 return y;
143 }
144
145 // Imaginary part is only defined for real derivative components
146 static TayType imag(const TayType& x) {
147#ifdef TEUCHOS_DEBUG
148 TEUCHOS_TEST_FOR_EXCEPTION(is_tay_real(x) == false, std::runtime_error,
149 "Imaginary component is not a differentiable "
150 "function of complex inputs.");
151#endif
152 return TayType(Teuchos::ScalarTraits<ValueT>::imag(x.val()));
153 }
154
155 static ValueT nan() {
156 return Teuchos::ScalarTraits<ValueT>::nan();
157 }
158 static bool isnaninf(const TayType& x) {
159 for (int i=0; i<=x.degree(); i++)
160 if (Teuchos::ScalarTraits<ValueT>::isnaninf(x.fastAccessCoeff(i)))
161 return true;
162 return false;
163 }
164 static void seedrandom(unsigned int s) {
165 Teuchos::ScalarTraits<ValueT>::seedrandom(s);
166 }
167 static ValueT random() {
168 return Teuchos::ScalarTraits<ValueT>::random();
169 }
170 static std::string name() {
172 }
173 static TayType squareroot(const TayType& 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 TayType pow(const TayType& x, const TayType& 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
188 Teuchos::ScalarTraits<ValueT>::magnitude(x-Teuchos::ScalarTraits<ValueT>::real(x)) == 0;
189 }
190
191 // Helper function to determine whether a Fad type is real
192 static bool is_tay_real(const TayType& x) {
193 if (x.size() == 0)
194 return true;
195 if (Teuchos::ScalarTraits<ValueT>::isComplex) {
196 for (int i=0; i<=x.degree(); i++)
197 if (!is_complex_real(x.fastAccessCoeff(i)))
198 return false;
199 }
200 return true;
201 }
202
203 }; // class ScalarTraitsImp
204
205 } // namespace Tay
206
207} // namespace Sacado
208
209#endif // HAVE_SACADO_TEUCHOSCORE
210
211#endif // SACADO_FAD_SCALARTRAITSIMP_HPP
Sacado::Random< double > rnd
const double y
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()