32 #ifndef SACADO_TAY_SCALARTRAITSIMP_HPP 33 #define SACADO_TAY_SCALARTRAITSIMP_HPP 35 #ifdef HAVE_SACADO_TEUCHOS 37 #include "Teuchos_ScalarTraits.hpp" 38 #include "Teuchos_SerializationTraits.hpp" 39 #include "Teuchos_SerializationTraitsHelpers.hpp" 40 #include "Teuchos_Assert.hpp" 50 template <
typename TayType>
51 struct ScalarTraitsImp {
54 typedef typename mpl::apply<TayType,typename Teuchos::ScalarTraits<ValueT>::magnitudeType>::type magnitudeType;
55 typedef typename mpl::apply<TayType,typename Teuchos::ScalarTraits<ValueT>::halfPrecision>::type halfPrecision;
56 typedef typename mpl::apply<TayType,typename Teuchos::ScalarTraits<ValueT>::doublePrecision>::type doublePrecision;
58 static const bool isComplex = Teuchos::ScalarTraits<ValueT>::isComplex;
59 static const bool isOrdinal = Teuchos::ScalarTraits<ValueT>::isOrdinal;
60 static const bool isComparable =
61 Teuchos::ScalarTraits<ValueT>::isComparable;
62 static const bool hasMachineParameters =
63 Teuchos::ScalarTraits<ValueT>::hasMachineParameters;
64 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType eps() {
65 return Teuchos::ScalarTraits<ValueT>::eps();
67 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType sfmin() {
68 return Teuchos::ScalarTraits<ValueT>::sfmin();
70 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType base() {
71 return Teuchos::ScalarTraits<ValueT>::base();
73 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType prec() {
74 return Teuchos::ScalarTraits<ValueT>::prec();
76 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType t() {
77 return Teuchos::ScalarTraits<ValueT>::t();
79 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType
rnd() {
82 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emin() {
83 return Teuchos::ScalarTraits<ValueT>::emin();
85 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmin() {
86 return Teuchos::ScalarTraits<ValueT>::rmin();
88 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emax() {
89 return Teuchos::ScalarTraits<ValueT>::emax();
91 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmax() {
92 return Teuchos::ScalarTraits<ValueT>::rmax();
94 static magnitudeType magnitude(
const TayType&
a) {
96 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
97 a,
"Error, the input value to magnitude(...) a = " <<
a <<
99 TEUCHOS_TEST_FOR_EXCEPTION(is_tay_real(
a) ==
false, std::runtime_error,
100 "Complex magnitude is not a differentiable " 101 "function of complex inputs.");
104 magnitudeType b(
a.degree(),
105 Teuchos::ScalarTraits<ValueT>::magnitude(
a.val()));
106 if (Teuchos::ScalarTraits<ValueT>::real(
a.val()) >= 0)
107 for (
int i=1; i<=
a.degree(); i++)
108 b.fastAccessCoeff(i) =
109 Teuchos::ScalarTraits<ValueT>::magnitude(
a.fastAccessCoeff(i));
111 for (
int i=1; i<=
a.degree(); i++)
112 b.fastAccessCoeff(i) =
113 -Teuchos::ScalarTraits<ValueT>::magnitude(
a.fastAccessCoeff(i));
116 static ValueT zero() {
119 static ValueT one() {
124 static TayType conjugate(
const TayType& x) {
126 TEUCHOS_TEST_FOR_EXCEPTION(is_tay_real(x) ==
false, std::runtime_error,
127 "Complex conjugate is not a differentiable " 128 "function of complex inputs.");
132 y.val() = Teuchos::ScalarTraits<ValueT>::conjugate(x.val());
137 static TayType real(
const TayType& x) {
139 TEUCHOS_TEST_FOR_EXCEPTION(is_tay_real(x) ==
false, std::runtime_error,
140 "Real component is not a differentiable " 141 "function of complex inputs.");
145 y.val() = Teuchos::ScalarTraits<ValueT>::real(x.val());
150 static TayType imag(
const TayType& x) {
152 TEUCHOS_TEST_FOR_EXCEPTION(is_tay_real(x) ==
false, std::runtime_error,
153 "Imaginary component is not a differentiable " 154 "function of complex inputs.");
156 return TayType(Teuchos::ScalarTraits<ValueT>::imag(x.val()));
159 static ValueT nan() {
160 return Teuchos::ScalarTraits<ValueT>::nan();
162 static bool isnaninf(
const TayType& x) {
163 for (
int i=0; i<=x.degree(); i++)
164 if (Teuchos::ScalarTraits<ValueT>::isnaninf(x.fastAccessCoeff(i)))
168 static void seedrandom(
unsigned int s) {
169 Teuchos::ScalarTraits<ValueT>::seedrandom(s);
171 static ValueT random() {
172 return Teuchos::ScalarTraits<ValueT>::random();
174 static std::string name() {
177 static TayType squareroot(
const TayType& x) {
179 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
180 x,
"Error, the input value to squareroot(...) a = " << x <<
181 " can not be NaN!" );
185 static TayType
pow(
const TayType& x,
const TayType& y) {
190 static bool is_complex_real(
const ValueT& x) {
192 Teuchos::ScalarTraits<ValueT>::magnitude(x-Teuchos::ScalarTraits<ValueT>::real(x)) == 0;
196 static bool is_tay_real(
const TayType& x) {
199 if (Teuchos::ScalarTraits<ValueT>::isComplex) {
200 for (
int i=0; i<=x.degree(); i++)
201 if (!is_complex_real(x.fastAccessCoeff(i)))
210 template <
typename Ordinal,
typename TayType,
typename Serializer>
211 struct SerializationImp {
216 typedef Teuchos::SerializationTraits<Ordinal,unsigned int> iSerT;
219 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
224 static const bool supportsDirectSerialization =
false;
230 static Ordinal fromCountToIndirectBytes(
const Serializer& vs,
232 const TayType buffer[],
237 for (
Ordinal i=0; i<count; i++) {
238 unsigned int my_sz = buffer[i].degree()+1;
239 unsigned int tot_sz = sz;
240 if (sz == 0) tot_sz = my_sz;
241 if (tot_sz != my_sz) {
242 x =
new TayType(buffer[i]);
243 x->resize(tot_sz-1,
true);
248 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
249 Ordinal b2 = vs.fromCountToIndirectBytes(
250 tot_sz, &(cx->fastAccessCoeff(0)));
251 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
262 static void serialize (
const Serializer& vs,
264 const TayType buffer[],
270 for (
Ordinal i=0; i<count; i++) {
272 unsigned int my_sz = buffer[i].degree()+1;
273 unsigned int tot_sz = sz;
274 if (sz == 0) tot_sz = my_sz;
275 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
276 iSerT::serialize(1, &tot_sz, b1, charBuffer);
280 if (tot_sz != my_sz) {
281 x =
new TayType(buffer[i]);
282 x->resize(tot_sz-1,
true);
287 Ordinal b2 = vs.fromCountToIndirectBytes(
288 tot_sz, &(cx->fastAccessCoeff(0)));
289 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
290 oSerT::serialize(1, &b2, b3, charBuffer);
292 vs.serialize(tot_sz, &(cx->fastAccessCoeff(0)), b2, charBuffer);
302 static Ordinal fromIndirectBytesToCount(
const Serializer& vs,
304 const char charBuffer[],
308 while (bytes_used < bytes) {
311 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
316 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
317 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
329 static void deserialize (
const Serializer& vs,
331 const char charBuffer[],
335 for (
Ordinal i=0; i<count; i++) {
338 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
339 const unsigned int *my_sz = iSerT::convertFromCharPtr(charBuffer);
343 unsigned int tot_sz = sz;
344 if (sz == 0) tot_sz = *my_sz;
345 buffer[i] = TayType(tot_sz-1, 0.0);
348 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
349 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
351 vs.deserialize(*b2, charBuffer, *my_sz,
352 &(buffer[i].fastAccessCoeff(0)));
363 template <
typename Ordinal,
typename TayType>
364 struct SerializationTraitsImp {
372 typedef Teuchos::DefaultSerializer<Ordinal,ValueT> DS;
375 typedef typename DS::DefaultSerializerType ValueSerializer;
378 typedef SerializationImp<Ordinal,TayType,ValueSerializer> Imp;
383 static const bool supportsDirectSerialization =
384 Imp::supportsDirectSerialization;
391 const TayType buffer[]) {
392 return Imp::fromCountToIndirectBytes(
393 DS::getDefaultSerializer(), count, buffer);
397 static void serialize (
const Ordinal count,
398 const TayType buffer[],
402 DS::getDefaultSerializer(), count, buffer, bytes, charBuffer);
407 const char charBuffer[]) {
408 return Imp::fromIndirectBytesToCount(
409 DS::getDefaultSerializer(), bytes, charBuffer);
413 static void deserialize (
const Ordinal bytes,
414 const char charBuffer[],
418 DS::getDefaultSerializer(), bytes, charBuffer, count, buffer);
426 template <
typename Ordinal,
typename TayType,
typename ValueSerializer>
427 class SerializerImp {
432 typedef SerializationImp<Ordinal,TayType,ValueSerializer> Imp;
435 Teuchos::RCP<const ValueSerializer> vs;
443 typedef ValueSerializer value_serializer_type;
446 static const bool supportsDirectSerialization =
447 Imp::supportsDirectSerialization;
450 SerializerImp(
const Teuchos::RCP<const ValueSerializer>& vs_,
455 Ordinal getSerializerSize()
const {
return sz; }
458 Teuchos::RCP<const value_serializer_type> getValueSerializer()
const {
466 const TayType buffer[])
const {
467 return Imp::fromCountToIndirectBytes(*vs, count, buffer, sz);
471 void serialize (
const Ordinal count,
472 const TayType buffer[],
474 char charBuffer[])
const {
475 Imp::serialize(*vs, count, buffer, bytes, charBuffer, sz);
480 const char charBuffer[])
const {
481 return Imp::fromIndirectBytesToCount(*vs, bytes, charBuffer, sz);
485 void deserialize (
const Ordinal bytes,
486 const char charBuffer[],
488 TayType buffer[])
const {
489 return Imp::deserialize(*vs, bytes, charBuffer, count, buffer, sz);
500 #endif // HAVE_SACADO_TEUCHOS 502 #endif // SACADO_FAD_SCALARTRAITSIMP_HPP static std::string eval()
PowExprType< Expr< T1 >, Expr< T2 > >::expr_type pow(const Expr< T1 > &expr1, const Expr< T2 > &expr2)
SimpleFad< ValueT > sqrt(const SimpleFad< ValueT > &a)
SimpleFad< ValueT > pow(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
Sacado::Random< double > rnd