30 #ifndef SACADO_FAD_SCALARTRAITSIMP_HPP 31 #define SACADO_FAD_SCALARTRAITSIMP_HPP 33 #ifdef HAVE_SACADO_TEUCHOS 50 template <
typename FadType>
51 struct ScalarTraitsImp {
54 typedef typename mpl::apply<FadType,typename Teuchos::ScalarTraits<ValueT>::magnitudeType>::type magnitudeType;
55 typedef typename mpl::apply<FadType,typename Teuchos::ScalarTraits<ValueT>::halfPrecision>::type halfPrecision;
56 typedef typename mpl::apply<FadType,typename Teuchos::ScalarTraits<ValueT>::doublePrecision>::type doublePrecision;
60 static const bool isComparable =
62 static const bool hasMachineParameters =
94 static magnitudeType magnitude(
const FadType&
a) {
96 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
97 a,
"Error, the input value to magnitude(...) a = " <<
a <<
100 "Complex magnitude is not a differentiable " 101 "function of complex inputs.");
104 magnitudeType b(
a.size(),
107 for (
int i=0; i<
a.size(); i++)
111 for (
int i=0; i<
a.size(); i++)
116 static ValueT zero() {
119 static ValueT one() {
127 "Complex conjugate is not a differentiable " 128 "function of complex inputs.");
139 "Real component is not a differentiable " 140 "function of complex inputs.");
151 "Imaginary component is not a differentiable " 152 "function of complex inputs.");
157 static ValueT nan() {
160 static bool isnaninf(
const FadType& x) {
163 for (
int i=0; i<x.size(); i++)
168 static void seedrandom(
unsigned int s) {
171 static ValueT random() {
174 static std::string name() {
179 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
180 x,
"Error, the input value to squareroot(...) a = " << x <<
181 " can not be NaN!" );
190 static bool is_complex_real(
const ValueT& x) {
196 static bool is_fad_real(
const FadType& x) {
200 if (!is_complex_real(x.val()))
202 for (
int i=0; i<x.size(); i++)
203 if (!is_complex_real(x.fastAccessDx(i)))
212 template <
typename Ordinal,
typename FadType,
typename Serializer>
213 struct SerializationImp {
229 static const bool supportsDirectSerialization =
false;
235 static Ordinal fromCountToIndirectBytes(
const Serializer& vs,
242 for (
Ordinal i=0; i<count; i++) {
243 int my_sz = buffer[i].size();
245 if (sz == 0) tot_sz = my_sz;
246 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
247 Ordinal b2 = vs.fromCountToIndirectBytes(1, &(buffer[i].
val()));
248 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
250 if (tot_sz != my_sz) {
259 b4 = vs.fromCountToIndirectBytes(tot_sz, cx->dx());
260 Ordinal b5 = oSerT::fromCountToIndirectBytes(1, &b4);
261 bytes += b1+b2+b3+b4+b5;
269 static void serialize (
const Serializer& vs,
277 for (
Ordinal i=0; i<count; i++) {
279 int my_sz = buffer[i].size();
281 if (sz == 0) tot_sz = my_sz;
282 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
283 iSerT::serialize(1, &tot_sz, b1, charBuffer);
287 Ordinal b2 = vs.fromCountToIndirectBytes(1, &(buffer[i].
val()));
288 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
289 oSerT::serialize(1, &b2, b3, charBuffer);
291 vs.serialize(1, &(buffer[i].
val()), b2, charBuffer);
296 if (tot_sz != my_sz) {
305 b4 = vs.fromCountToIndirectBytes(tot_sz, cx->dx());
306 Ordinal b5 = oSerT::fromCountToIndirectBytes(1, &b4);
307 oSerT::serialize(1, &b4, b5, charBuffer);
309 vs.serialize(tot_sz, cx->dx(), b4, charBuffer);
317 static Ordinal fromIndirectBytesToCount(
const Serializer& vs,
319 const char charBuffer[],
323 while (bytes_used < bytes) {
326 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
331 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
332 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
339 Ordinal b5 = oSerT::fromCountToDirectBytes(1);
340 const Ordinal *b4 = oSerT::convertFromCharPtr(charBuffer);
352 static void deserialize (
const Serializer& vs,
354 const char charBuffer[],
358 for (
Ordinal i=0; i<count; i++) {
361 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
362 const int *my_sz = iSerT::convertFromCharPtr(charBuffer);
367 if (sz == 0) tot_sz = *my_sz;
368 buffer[i] =
FadType(tot_sz, 0.0);
371 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
372 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
374 vs.deserialize(*b2, charBuffer, 1, &(buffer[i].
val()));
378 Ordinal b5 = oSerT::fromCountToDirectBytes(1);
379 const Ordinal *b4 = oSerT::convertFromCharPtr(charBuffer);
381 vs.deserialize(*b4, charBuffer, *my_sz,
393 template <
typename Ordinal,
typename FadType>
394 struct SerializationTraitsImp {
405 typedef typename DS::DefaultSerializerType ValueSerializer;
408 typedef SerializationImp<Ordinal,FadType,ValueSerializer> Imp;
413 static const bool supportsDirectSerialization =
414 Imp::supportsDirectSerialization;
422 return Imp::fromCountToIndirectBytes(
423 DS::getDefaultSerializer(), count, buffer);
427 static void serialize (
const Ordinal count,
432 DS::getDefaultSerializer(), count, buffer, bytes, charBuffer);
437 const char charBuffer[]) {
438 return Imp::fromIndirectBytesToCount(
439 DS::getDefaultSerializer(), bytes, charBuffer);
443 static void deserialize (
const Ordinal bytes,
444 const char charBuffer[],
448 DS::getDefaultSerializer(), bytes, charBuffer, count, buffer);
456 template <
typename Ordinal,
typename FadType>
457 struct StaticSerializationTraitsImp {
461 typedef Sacado::Fad::SerializationTraitsImp<Ordinal,FadType> STI;
464 static const bool supportsDirectSerialization =
465 vSerT::supportsDirectSerialization;
472 return DSerT::fromCountToDirectBytes(count);
476 static char* convertToCharPtr(
FadType* ptr ) {
477 return DSerT::convertToCharPtr(ptr);
481 static const char* convertToCharPtr(
const FadType* ptr ) {
482 return DSerT::convertToCharPtr(ptr);
487 return DSerT::fromDirectBytesToCount(bytes);
491 static FadType* convertFromCharPtr(
char* ptr ) {
492 return DSerT::convertFromCharPtr(ptr);
496 static const FadType* convertFromCharPtr(
const char* ptr ) {
497 return DSerT::convertFromCharPtr(ptr);
508 if (supportsDirectSerialization)
509 return DSerT::fromCountToIndirectBytes(count, buffer);
511 return STI::fromCountToIndirectBytes(count, buffer);
515 static void serialize (
const Ordinal count,
519 if (supportsDirectSerialization)
520 return DSerT::serialize(count, buffer, bytes, charBuffer);
522 return STI::serialize(count, buffer, bytes, charBuffer);
527 const char charBuffer[]) {
528 if (supportsDirectSerialization)
529 return DSerT::fromIndirectBytesToCount(bytes, charBuffer);
531 return STI::fromIndirectBytesToCount(bytes, charBuffer);
535 static void deserialize (
const Ordinal bytes,
536 const char charBuffer[],
539 if (supportsDirectSerialization)
540 return DSerT::deserialize(bytes, charBuffer, count, buffer);
542 return STI::deserialize(bytes, charBuffer, count, buffer);
550 template <
typename Ordinal,
typename FadType,
typename ValueSerializer>
551 class SerializerImp {
556 typedef SerializationImp<Ordinal,FadType,ValueSerializer> Imp;
567 typedef ValueSerializer value_serializer_type;
570 static const bool supportsDirectSerialization =
571 Imp::supportsDirectSerialization;
579 Ordinal getSerializerSize()
const {
return sz; }
590 const FadType buffer[])
const {
591 return Imp::fromCountToIndirectBytes(*vs, count, buffer, sz);
595 void serialize (
const Ordinal count,
598 char charBuffer[])
const {
599 Imp::serialize(*vs, count, buffer, bytes, charBuffer, sz);
604 const char charBuffer[])
const {
605 return Imp::fromIndirectBytesToCount(*vs, bytes, charBuffer, sz);
609 void deserialize (
const Ordinal bytes,
610 const char charBuffer[],
613 return Imp::deserialize(*vs, bytes, charBuffer, count, buffer, sz);
625 #if defined(HAVE_SACADO_KOKKOSCORE) && defined(HAVE_SACADO_TEUCHOSKOKKOSCOMM) && defined(HAVE_SACADO_VIEW_SPEC) && !defined(SACADO_DISABLE_FAD_VIEW_SPEC) 631 #endif // HAVE_SACADO_TEUCHOS 633 #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
pow(expr1.val(), expr2.val())
static magnitudeType emax()
static magnitudeType base()
static magnitudeType rmax()
static magnitudeType sfmin()
static magnitudeType prec()
static magnitudeType rmin()
static void seedrandom(unsigned int s)
static magnitudeType magnitude(T a)
SimpleFad< ValueT > pow(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
expr expr expr fastAccessDx(i)) FAD_UNARYOP_MACRO(exp
Sacado::Random< double > rnd
static magnitudeType emin()
static magnitudeType rnd()