42 #ifndef SACADO_PCE_SCALARTRAITSIMP_HPP 43 #define SACADO_PCE_SCALARTRAITSIMP_HPP 45 #ifdef HAVE_SACADO_TEUCHOS 47 #include "Teuchos_ScalarTraits.hpp" 48 #include "Teuchos_SerializationTraits.hpp" 49 #include "Teuchos_Assert.hpp" 50 #include "Sacado_mpl_apply.hpp" 59 template <
typename PCEType>
60 struct ScalarTraitsImp {
61 typedef typename Sacado::ValueType<PCEType>::type ValueT;
63 typedef typename Teuchos::ScalarTraits<ValueT>::magnitudeType magnitudeType;
65 typedef ValueT innerProductType;
66 typedef typename mpl::apply<PCEType,typename Teuchos::ScalarTraits<ValueT>::halfPrecision>::type halfPrecision;
67 typedef typename mpl::apply<PCEType,typename Teuchos::ScalarTraits<ValueT>::doublePrecision>::type doublePrecision;
69 static const bool isComplex = Teuchos::ScalarTraits<ValueT>::isComplex;
70 static const bool isOrdinal = Teuchos::ScalarTraits<ValueT>::isOrdinal;
71 static const bool isComparable =
72 Teuchos::ScalarTraits<ValueT>::isComparable;
73 static const bool hasMachineParameters =
74 Teuchos::ScalarTraits<ValueT>::hasMachineParameters;
75 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType eps() {
76 return Teuchos::ScalarTraits<ValueT>::eps();
78 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType sfmin() {
79 return Teuchos::ScalarTraits<ValueT>::sfmin();
81 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType base() {
82 return Teuchos::ScalarTraits<ValueT>::base();
84 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType prec() {
85 return Teuchos::ScalarTraits<ValueT>::prec();
87 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType t() {
88 return Teuchos::ScalarTraits<ValueT>::t();
90 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType
rnd() {
93 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emin() {
94 return Teuchos::ScalarTraits<ValueT>::emin();
96 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmin() {
97 return Teuchos::ScalarTraits<ValueT>::rmin();
99 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emax() {
100 return Teuchos::ScalarTraits<ValueT>::emax();
102 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmax() {
103 return Teuchos::ScalarTraits<ValueT>::rmax();
105 static magnitudeType magnitude(
const PCEType& a) {
107 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
108 a,
"Error, the input value to magnitude(...) a = " << a <<
109 " can not be NaN!" );
110 TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(a) ==
false, std::runtime_error,
111 "Complex magnitude is not a differentiable " 112 "function of complex inputs.");
116 static innerProductType innerProduct(
const PCEType& a,
const PCEType& b) {
118 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
119 a,
"Error, the input value to innerProduct(...) a = " << a <<
120 " can not be NaN!" );
121 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
122 b,
"Error, the input value to innerProduct(...) b = " << b <<
123 " can not be NaN!" );
125 return a.inner_product(b);
127 static ValueT zero() {
130 static ValueT one() {
137 TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(
x) ==
false, std::runtime_error,
138 "Complex conjugate is not a differentiable " 139 "function of complex inputs.");
143 y.val() = Teuchos::ScalarTraits<ValueT>::conjugate(
x.val());
150 TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(
x) ==
false, std::runtime_error,
151 "Real component is not a differentiable " 152 "function of complex inputs.");
156 y.val() = Teuchos::ScalarTraits<ValueT>::real(
x.val());
163 TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(
x) ==
false, std::runtime_error,
164 "Imaginary component is not a differentiable " 165 "function of complex inputs.");
167 return PCEType(Teuchos::ScalarTraits<ValueT>::imag(
x.val()));
170 static ValueT nan() {
171 return Teuchos::ScalarTraits<ValueT>::nan();
173 static bool isnaninf(
const PCEType&
x) {
174 for (
int i=0; i<
x.size(); i++)
175 if (Teuchos::ScalarTraits<ValueT>::isnaninf(
x.fastAccessCoeff(i)))
179 static void seedrandom(
unsigned int s) {
180 Teuchos::ScalarTraits<ValueT>::seedrandom(s);
182 static ValueT random() {
183 return Teuchos::ScalarTraits<ValueT>::random();
185 static std::string name() {
186 return Sacado::StringName<PCEType>::eval();
190 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
191 x,
"Error, the input value to squareroot(...) a = " <<
x <<
192 " can not be NaN!" );
207 static bool is_complex_real(
const ValueT&
x) {
209 Teuchos::ScalarTraits<ValueT>::magnitude(
x-Teuchos::ScalarTraits<ValueT>::real(
x)) == 0;
213 static bool is_pce_real(
const PCEType&
x) {
216 if (Teuchos::ScalarTraits<ValueT>::isComplex) {
217 for (
int i=0; i<
x.size(); i++)
218 if (!is_complex_real(
x.fastAccessCoeff(i)))
227 template <
typename TypeTo,
typename PCEType>
228 struct ValueTypeConversionTraitsImp {
229 typedef typename Sacado::ValueType<PCEType>::type ValueT;
230 typedef Teuchos::ValueTypeConversionTraits<TypeTo,ValueT> VTCT;
231 static TypeTo convert(
const PCEType t ) {
232 return VTCT::convert(t.val());
234 static TypeTo safeConvert(
const PCEType t ) {
235 return VTCT::safeConvert(t.val());
241 template <
typename Ordinal,
typename PCEType>
242 class SerializationTraitsImp {
243 typedef typename Sacado::ValueType<PCEType>::type ValueT;
244 typedef Teuchos::SerializationTraits<Ordinal,ValueT> vSerT;
245 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
246 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
251 static const bool supportsDirectSerialization =
false;
260 for (
Ordinal i=0; i<count; i++) {
261 int sz = buffer[i].size();
262 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
263 Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
264 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
271 static void serialize (
const Ordinal count,
275 for (
Ordinal i=0; i<count; i++) {
277 int sz = buffer[i].size();
278 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
279 iSerT::serialize(1, &sz, b1, charBuffer);
283 Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
284 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
285 oSerT::serialize(1, &b2, b3, charBuffer);
287 vSerT::serialize(sz, buffer[i].coeff(), b2, charBuffer);
294 const char charBuffer[]) {
297 while (bytes_used < bytes) {
300 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
305 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
306 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
318 static void deserialize (
const Ordinal bytes,
319 const char charBuffer[],
322 for (
Ordinal i=0; i<count; i++) {
325 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
326 const int *sz = iSerT::convertFromCharPtr(charBuffer);
332 if (buffer[i].size() != *sz)
333 buffer[i].reset(buffer[i].expansion(), *sz);
334 buffer[i].copyForWrite();
337 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
338 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
340 vSerT::deserialize(*b2, charBuffer, *sz, buffer[i].coeff());
352 template <
typename Ordinal,
typename PCEType,
typename ValueSerializer>
353 class SerializerImp {
358 typedef ValueSerializer value_serializer_type;
361 typedef typename PCEType::expansion_type expansion_type;
365 typedef typename Sacado::ValueType<PCEType>::type ValueT;
366 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
367 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
369 Teuchos::RCP<expansion_type> expansion;
370 Teuchos::RCP<const ValueSerializer> vs;
376 static const bool supportsDirectSerialization =
false;
378 SerializerImp(
const Teuchos::RCP<expansion_type>& expansion_,
379 const Teuchos::RCP<const ValueSerializer>& vs_) :
380 expansion(expansion_), vs(vs_), sz(expansion->size()) {}
383 Teuchos::RCP<expansion_type> getSerializerExpansion()
const {
387 Teuchos::RCP<const value_serializer_type> getValueSerializer()
const {
395 const PCEType buffer[])
const {
399 for (
Ordinal i=0; i<count; i++) {
400 int my_sz = buffer[i].size();
410 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
411 Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
412 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
421 void serialize (
const Ordinal count,
424 char charBuffer[])
const {
427 for (
Ordinal i=0; i<count; i++) {
429 int my_sz = buffer[i].size();
439 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
440 iSerT::serialize(1, &sz, b1, charBuffer);
444 Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
445 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
446 oSerT::serialize(1, &b2, b3, charBuffer);
448 vs->serialize(sz, cx->coeff(), b2, charBuffer);
457 const char charBuffer[])
const {
460 while (bytes_used < bytes) {
463 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
468 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
469 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
481 void deserialize (
const Ordinal bytes,
482 const char charBuffer[],
485 for (
Ordinal i=0; i<count; i++) {
488 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
489 const int *my_sz = iSerT::convertFromCharPtr(charBuffer);
493 buffer[i] =
PCEType(expansion);
496 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
497 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
499 vs->deserialize(*b2, charBuffer, *my_sz, buffer[i].coeff());
513 #endif // HAVE_SACADO_TEUCHOS 515 #endif // SACADO_FAD_SCALARTRAITSIMP_HPP KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
OrthogPoly< T, Storage > log(const OrthogPoly< T, Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > pow(const PCE< Storage > &a, const PCE< Storage > &b)
OrthogPoly< T, Storage > pow(const OrthogPoly< T, Storage > &a, const OrthogPoly< T, Storage > &b)
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
OrthogPoly< T, Storage > log10(const OrthogPoly< T, Storage > &a)
Sacado::Random< double > rnd
Sacado::UQ::PCE< storage_type > PCEType
KOKKOS_INLINE_FUNCTION PCE< Storage > log(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > log10(const PCE< Storage > &a)
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType ValueType * y