42 #ifndef SACADO_PCE_SCALARTRAITSIMP_HPP
43 #define SACADO_PCE_SCALARTRAITSIMP_HPP
45 #ifdef HAVE_SACADO_TEUCHOS
50 #include "Sacado_mpl_apply.hpp"
59 template <
typename PCEType>
60 struct ScalarTraitsImp {
61 typedef typename Sacado::ValueType<PCEType>::type ValueT;
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;
71 static const bool isComparable =
73 static const bool hasMachineParameters =
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!" );
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() {
138 "Complex conjugate is not a differentiable "
139 "function of complex inputs.");
151 "Real component is not a differentiable "
152 "function of complex inputs.");
164 "Imaginary component is not a differentiable "
165 "function of complex inputs.");
170 static ValueT nan() {
173 static bool isnaninf(
const PCEType&
x) {
174 for (
int i=0; i<
x.size(); i++)
179 static void seedrandom(
unsigned int s) {
182 static 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) {
213 static bool is_pce_real(
const PCEType&
x) {
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;
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;
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;
376 static const bool supportsDirectSerialization =
false;
380 expansion(expansion_), vs(vs_), sz(expansion->size()) {}
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