42 #ifndef SACADO_PCE_SCALARTRAITSIMP_HPP 43 #define SACADO_PCE_SCALARTRAITSIMP_HPP 45 #include "Teuchos_ScalarTraits.hpp" 46 #include "Teuchos_SerializationTraits.hpp" 47 #include "Teuchos_Assert.hpp" 48 #include "Sacado_mpl_apply.hpp" 57 template <
typename PCEType>
59 typedef typename Sacado::ValueType<PCEType>::type
ValueT;
61 typedef typename Teuchos::ScalarTraits<ValueT>::magnitudeType
magnitudeType;
64 typedef typename mpl::apply<PCEType,typename Teuchos::ScalarTraits<ValueT>::halfPrecision>::type
halfPrecision;
65 typedef typename mpl::apply<PCEType,typename Teuchos::ScalarTraits<ValueT>::doublePrecision>::type
doublePrecision;
67 static const bool isComplex = Teuchos::ScalarTraits<ValueT>::isComplex;
68 static const bool isOrdinal = Teuchos::ScalarTraits<ValueT>::isOrdinal;
70 Teuchos::ScalarTraits<ValueT>::isComparable;
72 Teuchos::ScalarTraits<ValueT>::hasMachineParameters;
73 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType
eps() {
74 return Teuchos::ScalarTraits<ValueT>::eps();
76 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType
sfmin() {
77 return Teuchos::ScalarTraits<ValueT>::sfmin();
79 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType
base() {
80 return Teuchos::ScalarTraits<ValueT>::base();
82 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType
prec() {
83 return Teuchos::ScalarTraits<ValueT>::prec();
85 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType
t() {
86 return Teuchos::ScalarTraits<ValueT>::t();
88 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType
rnd() {
91 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType
emin() {
92 return Teuchos::ScalarTraits<ValueT>::emin();
94 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType
rmin() {
95 return Teuchos::ScalarTraits<ValueT>::rmin();
97 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType
emax() {
98 return Teuchos::ScalarTraits<ValueT>::emax();
100 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType
rmax() {
101 return Teuchos::ScalarTraits<ValueT>::rmax();
105 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
106 a,
"Error, the input value to magnitude(...) a = " << a <<
107 " can not be NaN!" );
108 TEUCHOS_TEST_FOR_EXCEPTION(
is_pce_real(a) ==
false, std::runtime_error,
109 "Complex magnitude is not a differentiable " 110 "function of complex inputs.");
116 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
117 a,
"Error, the input value to innerProduct(...) a = " << a <<
118 " can not be NaN!" );
119 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
120 b,
"Error, the input value to innerProduct(...) b = " << b <<
121 " can not be NaN!" );
123 return a.inner_product(b);
135 TEUCHOS_TEST_FOR_EXCEPTION(
is_pce_real(
x) ==
false, std::runtime_error,
136 "Complex conjugate is not a differentiable " 137 "function of complex inputs.");
141 y.val() = Teuchos::ScalarTraits<ValueT>::conjugate(
x.val());
148 TEUCHOS_TEST_FOR_EXCEPTION(
is_pce_real(
x) ==
false, std::runtime_error,
149 "Real component is not a differentiable " 150 "function of complex inputs.");
154 y.val() = Teuchos::ScalarTraits<ValueT>::real(
x.val());
161 TEUCHOS_TEST_FOR_EXCEPTION(
is_pce_real(
x) ==
false, std::runtime_error,
162 "Imaginary component is not a differentiable " 163 "function of complex inputs.");
165 return PCEType(Teuchos::ScalarTraits<ValueT>::imag(
x.val()));
169 return Teuchos::ScalarTraits<ValueT>::nan();
172 for (
int i=0; i<
x.size(); i++)
173 if (Teuchos::ScalarTraits<ValueT>::isnaninf(
x.fastAccessCoeff(i)))
178 Teuchos::ScalarTraits<ValueT>::seedrandom(s);
181 return Teuchos::ScalarTraits<ValueT>::random();
184 return Sacado::StringName<PCEType>::eval();
188 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
189 x,
"Error, the input value to squareroot(...) a = " <<
x <<
190 " can not be NaN!" );
207 Teuchos::ScalarTraits<ValueT>::magnitude(
x-Teuchos::ScalarTraits<ValueT>::real(
x)) == 0;
214 if (Teuchos::ScalarTraits<ValueT>::isComplex) {
215 for (
int i=0; i<
x.size(); i++)
225 template <
typename TypeTo,
typename PCEType>
227 typedef typename Sacado::ValueType<PCEType>::type
ValueT;
228 typedef Teuchos::ValueTypeConversionTraits<TypeTo,ValueT>
VTCT;
230 return VTCT::convert(t.val());
233 return VTCT::safeConvert(t.val());
239 template <
typename Ordinal,
typename PCEType>
241 typedef typename Sacado::ValueType<PCEType>::type
ValueT;
242 typedef Teuchos::SerializationTraits<Ordinal,ValueT>
vSerT;
243 typedef Teuchos::SerializationTraits<Ordinal,int>
iSerT;
244 typedef Teuchos::SerializationTraits<Ordinal,Ordinal>
oSerT;
258 for (
Ordinal i=0; i<count; i++) {
259 int sz = buffer[i].size();
260 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
261 Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
262 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
273 for (
Ordinal i=0; i<count; i++) {
275 int sz = buffer[i].size();
276 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
277 iSerT::serialize(1, &sz, b1, charBuffer);
281 Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
282 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
283 oSerT::serialize(1, &b2, b3, charBuffer);
285 vSerT::serialize(sz, buffer[i].coeff(), b2, charBuffer);
292 const char charBuffer[]) {
295 while (bytes_used < bytes) {
298 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
303 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
304 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
317 const char charBuffer[],
320 for (
Ordinal i=0; i<count; i++) {
323 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
324 const int *sz = iSerT::convertFromCharPtr(charBuffer);
330 if (buffer[i].size() != *sz)
331 buffer[i].reset(buffer[i].expansion(), *sz);
332 buffer[i].copyForWrite();
335 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
336 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
338 vSerT::deserialize(*b2, charBuffer, *sz, buffer[i].coeff());
350 template <
typename Ordinal,
typename PCEType,
typename ValueSerializer>
363 typedef typename Sacado::ValueType<PCEType>::type
ValueT;
364 typedef Teuchos::SerializationTraits<Ordinal,int>
iSerT;
365 typedef Teuchos::SerializationTraits<Ordinal,Ordinal>
oSerT;
368 Teuchos::RCP<const ValueSerializer>
vs;
377 const Teuchos::RCP<const ValueSerializer>& vs_) :
393 const PCEType buffer[])
const {
397 for (
Ordinal i=0; i<count; i++) {
398 int my_sz = buffer[i].size();
408 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &
sz);
409 Ordinal b2 =
vs->fromCountToIndirectBytes(
sz, cx->coeff());
410 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
422 char charBuffer[])
const {
425 for (
Ordinal i=0; i<count; i++) {
427 int my_sz = buffer[i].size();
437 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &
sz);
438 iSerT::serialize(1, &
sz, b1, charBuffer);
442 Ordinal b2 =
vs->fromCountToIndirectBytes(
sz, cx->coeff());
443 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
444 oSerT::serialize(1, &b2, b3, charBuffer);
446 vs->serialize(
sz, cx->coeff(), b2, charBuffer);
455 const char charBuffer[])
const {
458 while (bytes_used < bytes) {
461 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
466 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
467 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
480 const char charBuffer[],
483 for (
Ordinal i=0; i<count; i++) {
486 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
487 const int *my_sz = iSerT::convertFromCharPtr(charBuffer);
494 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
495 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
497 vs->deserialize(*b2, charBuffer, *my_sz, buffer[i].coeff());
511 #endif // SACADO_FAD_SCALARTRAITSIMP_HPP KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
static void deserialize(const Ordinal bytes, const char charBuffer[], const Ordinal count, PCEType buffer[])
Deserialize from an indirect char[] buffer.
static Teuchos::ScalarTraits< ValueT >::magnitudeType eps()
static Teuchos::ScalarTraits< ValueT >::magnitudeType rmin()
SerializerImp(const Teuchos::RCP< expansion_type > &expansion_, const Teuchos::RCP< const ValueSerializer > &vs_)
Implementation for Teuchos::ScalarTraits for all PCE types.
Teuchos::RCP< const ValueSerializer > vs
PCEType::expansion_type expansion_type
Typename of expansion.
static Teuchos::ScalarTraits< ValueT >::magnitudeType emax()
Serializer object for all PCE types.
KOKKOS_INLINE_FUNCTION PCE< Storage > pow(const PCE< Storage > &a, const PCE< Storage > &b)
static PCEType log(const PCEType &x)
static Teuchos::ScalarTraits< ValueT >::magnitudeType sfmin()
static Teuchos::ScalarTraits< ValueT >::magnitudeType rmax()
static PCEType log10(const PCEType &x)
static const bool isComparable
static TypeTo safeConvert(const PCEType t)
Sacado::ValueType< PCEType >::type ValueT
Teuchos::RCP< expansion_type > expansion
Teuchos::SerializationTraits< Ordinal, ValueT > vSerT
static bool isnaninf(const PCEType &x)
Sacado::ValueType< PCEType >::type ValueT
void deserialize(const Ordinal bytes, const char charBuffer[], const Ordinal count, PCEType buffer[]) const
Deserialize from an indirect char[] buffer.
ValueSerializer value_serializer_type
Typename of value serializer.
Teuchos::RCP< const value_serializer_type > getValueSerializer() const
Get nested value serializer.
void serialize(const Ordinal count, const PCEType buffer[], const Ordinal bytes, char charBuffer[]) const
Serialize to an indirect char[] buffer.
Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[]) const
Return the number of objects for bytes of storage.
Teuchos::ScalarTraits< ValueT >::magnitudeType magnitudeType
static Teuchos::ScalarTraits< ValueT >::magnitudeType rnd()
static Teuchos::ScalarTraits< ValueT >::magnitudeType base()
static PCEType squareroot(const PCEType &x)
static const bool isOrdinal
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
static PCEType real(const PCEType &x)
Teuchos::SerializationTraits< Ordinal, Ordinal > oSerT
Sacado::ValueType< PCEType >::type ValueT
static bool is_complex_real(const ValueT &x)
Teuchos::SerializationTraits< Ordinal, int > iSerT
static const bool supportsDirectSerialization
Whether the type T supports direct serialization.
static bool is_pce_real(const PCEType &x)
Teuchos::ValueTypeConversionTraits< TypeTo, ValueT > VTCT
static void seedrandom(unsigned int s)
static PCEType imag(const PCEType &x)
Implementation for Teuchos::ValueTypeConversionTraits for all PCE types.
static const bool hasMachineParameters
static Ordinal fromCountToIndirectBytes(const Ordinal count, const PCEType buffer[])
Return the number of bytes for count objects.
static magnitudeType magnitude(const PCEType &a)
Implementation of Teuchos::SerializationTraits for all PCE types.
Sacado::Random< double > rnd
static Teuchos::ScalarTraits< ValueT >::magnitudeType prec()
static innerProductType innerProduct(const PCEType &a, const PCEType &b)
static void serialize(const Ordinal count, const PCEType buffer[], const Ordinal bytes, char charBuffer[])
Serialize to an indirect char[] buffer.
Sacado::UQ::PCE< storage_type > PCEType
static TypeTo convert(const PCEType t)
static std::string name()
Teuchos::SerializationTraits< Ordinal, Ordinal > oSerT
Ordinal fromCountToIndirectBytes(const Ordinal count, const PCEType buffer[]) const
Return the number of bytes for count objects.
static PCEType pow(const PCEType &x, const PCEType &y)
Teuchos::SerializationTraits< Ordinal, int > iSerT
static Teuchos::ScalarTraits< ValueT >::magnitudeType emin()
static PCEType conjugate(const PCEType &x)
Sacado::ValueType< PCEType >::type ValueT
mpl::apply< PCEType, typename Teuchos::ScalarTraits< ValueT >::halfPrecision >::type halfPrecision
Teuchos::RCP< expansion_type > getSerializerExpansion() const
Return specified serializer size.
static Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[])
Return the number of objects for bytes of storage.
KOKKOS_INLINE_FUNCTION PCE< Storage > log(const PCE< Storage > &a)
static const bool isComplex
KOKKOS_INLINE_FUNCTION PCE< Storage > log10(const PCE< Storage > &a)
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType ValueType * y
static Teuchos::ScalarTraits< ValueT >::magnitudeType t()
mpl::apply< PCEType, typename Teuchos::ScalarTraits< ValueT >::doublePrecision >::type doublePrecision
static const bool supportsDirectSerialization
Whether the type T supports direct serialization.