Debian Codegraph @

packages/stokhos/src/sacado/Sacado_ETPCE_OrthogPoly.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef SACADO_ETPCE_ORTHOGPOLY_HPP
11 #define SACADO_ETPCE_ORTHOGPOLY_HPP
12
13 #include "Stokhos_ConfigDefs.h"
14
15 #ifdef HAVE_STOKHOS_SACADO
16
17 #include "Teuchos_RCP.hpp"
18
19 #include "Sacado_Traits.hpp"
20 #include "Sacado_Handle.hpp"
21
22 #include "Stokhos_OrthogPolyExpansion.hpp"
23 #include "Stokhos_QuadOrthogPolyExpansion.hpp"
24 #include "Stokhos_ConstantOrthogPolyExpansion.hpp"
25 #include "Stokhos_OrthogPolyApprox.hpp"
26
27 #include "Sacado_mpl_apply.hpp"
28
29 #include <ostream> // for std::ostream
30
31 #ifdef HAVE_STOKHOS_THRUST
32 #include "thrust/tuple.h"
33 #endif
34
35 #ifndef KERNEL_PREFIX
36 #define KERNEL_PREFIX
37 #endif
38
39 namespace Sacado {
40
41 //! Namespace for expression templated polynomial chaos expansion classes
42 namespace ETPCE {
43
44 template <int k, typename T> KERNEL_PREFIX T&
45 get(T* a) { return a[k]; }
46 template <int k, typename T> KERNEL_PREFIX const T&
47 get(const T* a) { return a[k]; }
48
49 template <int k, int N, typename T> KERNEL_PREFIX T&
50 get(T a[N]) { return a[k]; }
51 template <int k, int N, typename T> KERNEL_PREFIX const T&
52 get(const T a[N]) { return a[k]; }
53
54 //! Wrapper for a generic expression template
55 /*!
56 * This template class serves as a wrapper for all expression
57 * template classes.
58 */
59 template <typename ExprT> class Expr {};
60
61 // Forward declaration
62 template <typename T, typename S> class OrthogPoly;
63
64 //! Generalized polynomial chaos expansion class implementation
65 template <typename T, typename Storage >
66 class OrthogPolyImpl {
67 public:
68
69 //! Typename of values
70 typedef T value_type;
71
72 //! Typename of scalar's (which may be different from T)
73 typedef typename ScalarType<T>::type scalar_type;
74
75 //! Typename of ordinals
76 typedef int ordinal_type;
77
78 //! Typename of storage class
79 typedef Storage storage_type;
80
81 //! Basis type
82 typedef Stokhos::OrthogPolyBasis<ordinal_type,T> basis_type;
83
84 //! Quad Expansion type
85 typedef Stokhos::OrthogPolyExpansion<ordinal_type,T,Storage> expansion_type;
86
87 //! Quad Expansion type
88 typedef Stokhos::QuadOrthogPolyExpansion<ordinal_type,T,Storage> quad_expansion_type;
89
90 //! Stokhos approximation type
91 typedef Stokhos::OrthogPolyApprox<ordinal_type,T,Storage> approx_type;
92
93 typedef typename approx_type::pointer pointer;
94 typedef typename approx_type::const_pointer const_pointer;
95 typedef typename approx_type::reference reference;
96 typedef typename approx_type::const_reference const_reference;
97
98
99 //! Default constructor
100 /*!
101 * Sets size to 1 and first coefficient to 0 (represents a constant).
102 */
103 OrthogPolyImpl();
104
105 //! Constructor with supplied value \c x
106 /*!
107 * Sets size to 1 and first coefficient to x (represents a constant).
108 */
109 OrthogPolyImpl(const value_type& x);
110
111 //! Constructor with expansion \c expansion (General case)
112 /*!
113 * Creates array of correct size and initializes coeffiencts to 0.
114 */
115 OrthogPolyImpl(const Teuchos::RCP<expansion_type>& expansion);
116
117 //! Constructor with expansion \c expansion and specified size \c sz
118 /*!
119 * Creates array of size \c sz and initializes coeffiencts to 0.
120 */
121 OrthogPolyImpl(const Teuchos::RCP<expansion_type>& expansion,
122 ordinal_type sz);
123
124 //! Copy constructor
125 OrthogPolyImpl(const OrthogPolyImpl& x);
126
127 //! Copy constructor from any Expression object
128 template <typename S> OrthogPolyImpl(const Expr<S>& x);
129
130 //! Destructor
131 ~OrthogPolyImpl() {}
132
133 //! Initialize coefficients to value
134 void init(const T& v) { th_->init(v); }
135
136 //! Initialize coefficients to an array of values
137 void init(const T* v) { th_->init(v); }
138
139 //! Initialize coefficients from an OrthogPolyImpl with different storage
140 template <typename S>
141 void init(const OrthogPolyImpl<T,S>& v) { th_->init(v.getOrthogPolyApprox()); }
142
143 //! Load coefficients to an array of values
144 void load(T* v) { th_->load(v); }
145
146 //! Load coefficients into an OrthogPolyImpl with different storage
147 template <typename S>
148 void load(OrthogPolyImpl<T,S>& v) { th_->load(v.getOrthogPolyApprox()); }
149
150 //! Reset expansion
151 /*!
152 * May change size of array. Coefficients are preserved.
153 */
154 void reset(const Teuchos::RCP<expansion_type>& expansion);
155
156 //! Reset expansion and size
157 /*!
158 * Coefficients are preserved.
159 */
160 void reset(const Teuchos::RCP<expansion_type>& expansion,
161 ordinal_type sz);
162
163 //! Prepare polynomial for writing
164 /*!
165 * This method prepares the polynomial for writing through coeff() and
166 * fastAccessCoeff() member functions. It ensures the handle for the
167 * %Hermite coefficients is not shared among any other %Hermite polynomial
168 * objects. If the handle is not shared it does nothing, so there
169 * is no cost in calling this method in this case. If the handle is
170 * shared and this method is not called, any changes to the coefficients
171 * by coeff() or fastAccessCoeff() may change other polynomial objects.
172 */
173 void copyForWrite() { th_.makeOwnCopy(); }
174
175 //! Evaluate polynomial approximation at a point
176 value_type evaluate(const Teuchos::Array<value_type>& point) const;
177
178 //! Evaluate polynomial approximation at a point with given basis values
179 value_type evaluate(const Teuchos::Array<value_type>& point,
180 const Teuchos::Array<value_type>& bvals) const;
181
182 //! Compute mean of expansion
183 value_type mean() const {return th_->mean(); }
184
185 //! Compute standard deviation of expansion
186 value_type standard_deviation() const { return th_->standard_deviation(); }
187
188 //! Compute the two-norm of expansion
189 value_type two_norm() const { return th_->two_norm(); }
190
191 //! Compute the squared two-norm of expansion
192 value_type two_norm_squared() const { return th_->two_norm_squared(); }
193
194 //! Compute the L2 inner product of 2 PCEs
195 value_type inner_product(const OrthogPolyImpl& b) const {
196 return th_->inner_product(b.getOrthogPolyApprox()); }
197
198 //! Print approximation in basis
199 std::ostream& print(std::ostream& os) const { return th_->print(os); }
200
201 //! Returns whether two PCE objects have the same values
202 template <typename S> bool isEqualTo(const Expr<S>& x) const;
203
204 /*!
205 * @name Assignment operators
206 */
207 //@{
208
209 //! Assignment operator with constant right-hand-side
210 OrthogPolyImpl& operator=(const value_type& val);
211
212 //! Assignment with OrthogPolyImpl right-hand-side
213 OrthogPolyImpl& operator=(const OrthogPolyImpl& x);
214
215 //! Assignment with any expression right-hand-side
216 template <typename S>
217 OrthogPolyImpl& operator=(const Expr<S>& x);
218
219 //@}
220
221 /*!
222 * Accessor methods
223 */
224 //@{
225
226 //! Get basis
227 Teuchos::RCP<basis_type> basis() const { return th_->basis(); }
228
229 //! Get expansion
230 Teuchos::RCP<expansion_type> expansion() const { return expansion_; }
231
232 //! Get quad expansion
233 Teuchos::RCP<quad_expansion_type> quad_expansion() const { return quad_expansion_; }
234
235 //@}
236
237 /*!
238 * @name Value accessor methods
239 */
240 //@{
241
242 //! Returns value
243 const_reference val() const { return (*th_)[0]; }
244
245 //! Returns value
246 reference val() { return (*th_)[0]; }
247
248 //@}
249
250 /*!
251 * @name Coefficient accessor methods
252 */
253 //@{
254
255 //! Returns size of polynomial
256 ordinal_type size() const { return th_->size();}
257
258 //! Returns true if polynomial has size >= sz
259 bool hasFastAccess(ordinal_type sz) const { return th_->size()>=sz;}
260
261 //! Returns Hermite coefficient array
262 const_pointer coeff() const { return th_->coeff();}
263
264 //! Returns Hermite coefficient array
265 pointer coeff() { return th_->coeff();}
266
267 //! Returns degree \c i term with bounds checking
268 value_type coeff(ordinal_type i) const {
269 return i<th_->size() ? (*th_)[i]:value_type(0.); }
270
271 //! Returns degree \c i term without bounds checking
272 reference fastAccessCoeff(ordinal_type i) { return (*th_)[i];}
273
274 //! Returns degree \c i term without bounds checking
275 value_type fastAccessCoeff(ordinal_type i) const { return (*th_)[i];}
276
277 //! Get coefficient term for given dimension and order
278 reference term(ordinal_type dimension, ordinal_type order) {
279 return th_->term(dimension, order); }
280
281 //! Get coefficient term for given dimension and order
282 const_reference term(ordinal_type dimension, ordinal_type order) const {
283 return th_->term(dimension, order); }
284
285 //! Get orders for a given term
286 Teuchos::Array<ordinal_type> order(ordinal_type term) const {
287 return th_->order(term); }
288
289 //@}
290
291 /*!
292 * @name Unary operators
293 */
294 //@{
295
296 //! Addition-assignment operator with constant right-hand-side
297 OrthogPolyImpl& operator += (const value_type& x);
298
299 //! Subtraction-assignment operator with constant right-hand-side
300 OrthogPolyImpl& operator -= (const value_type& x);
301
302 //! Multiplication-assignment operator with constant right-hand-side
303 OrthogPolyImpl& operator *= (const value_type& x);
304
305 //! Division-assignment operator with constant right-hand-side
306 OrthogPolyImpl& operator /= (const value_type& x);
307
308 //@}
309
310 //! Get underlying Stokhos::OrthogPolyApprox
311 const approx_type& getOrthogPolyApprox() const { return *th_; }
312
313 //! Get underlying Stokhos::OrthogPolyApprox
314 approx_type& getOrthogPolyApprox() { return *th_; }
315
316 protected:
317
318 //! Copy from Expression object
319 template <typename S> void expressionCopy(const Expr<S>& x);
320
321 protected:
322
323 //! Expansion class
324 Teuchos::RCP<expansion_type> expansion_;
325
326 //! Cast of expansion class to QuadExpansion
327 Teuchos::RCP<quad_expansion_type> quad_expansion_;
328
329 //! Constant expansion class for constants
330 Teuchos::RCP<expansion_type> const_expansion_;
331
332 //! Handle to underlying OrthogPolyApprox
333 Sacado::Handle< Stokhos::OrthogPolyApprox<int,value_type,Storage> > th_;
334
335 }; // class OrthogPolyImpl
336
337 //! OrthogPolyImpl expression template specialization
338 /*!
339 * This template class represents a simple OrthogPolyImpl expression and
340 * mixes-in the OrthogPolyImpl interface and the expression template
341 * interface.
342 */
343 template <typename T, typename Storage>
344 class Expr< OrthogPolyImpl<T,Storage> > :
345 public OrthogPolyImpl<T,Storage> {
346
347 public:
348
349 //! Typename of values
350 typedef typename OrthogPolyImpl<T,Storage>::value_type value_type;
351 typedef typename OrthogPolyImpl<T,Storage>::scalar_type scalar_type;
352 typedef typename OrthogPolyImpl<T,Storage>::approx_type approx_type;
353 typedef typename OrthogPolyImpl<T,Storage>::storage_type storage_type;
354 typedef typename OrthogPolyImpl<T,Storage>::const_reference const_reference;
355
356 typedef OrthogPoly<T,Storage> base_expr_type;
357
358 //! Number of arguments
359 static const int num_args = 1;
360
361 //! Default constructor
362 Expr() :
363 OrthogPolyImpl<T,Storage>() {}
364
365 //! Constructor with supplied value \c x
366 Expr(const T & x) :
367 OrthogPolyImpl<T,Storage>(x) {}
368
369 //! Constructor with expansion \c expansion (General case)
370 Expr(const Teuchos::RCP<typename OrthogPolyImpl<T,Storage>::expansion_type>& expansion) :
371 OrthogPolyImpl<T,Storage>(expansion) {}
372
373 //! Constructor with expansion \c expansion and specified size \c sz
374 Expr(const Teuchos::RCP<typename OrthogPolyImpl<T,Storage>::expansion_type>& expansion,
375 typename OrthogPolyImpl<T,Storage>::ordinal_type sz) :
376 OrthogPolyImpl<T,Storage>(expansion, sz) {}
377
378 //! Copy constructor
379 Expr(const Expr& x) :
380 OrthogPolyImpl<T,Storage>(static_cast<const OrthogPolyImpl<T,Storage>&>(x)) {}
381
382 //! Copy constructor
383 Expr(const OrthogPolyImpl<T,Storage>& x) :
384 OrthogPolyImpl<T,Storage>(x) {}
385
386 //! Copy constructor from any Expression object
387 template <typename S> Expr(const Expr<S>& x) :
388 OrthogPolyImpl<T,Storage>(x) {}
389
390 //! Destructor
391 ~Expr() {}
392
393 const approx_type& getArg(int i) const {
394 return this->getOrthogPolyApprox(); }
395
396 bool has_fast_access(int sz) const { return this->size() >= sz; }
397
398 bool has_nonconst_expansion() const {
399 return this->expansion_ != this->const_expansion_;
400 }
401
402 int order() const { return this->size() == 1 ? 0 : 1; }
403
404 value_type fast_higher_order_coeff(int i) const {
405 return this->fastAccessCoeff(i);
406 }
407
408 value_type higher_order_coeff(int i) const {
409 return this->coeff(i);
410 }
411
412 template <int offset, typename tuple_type>
413 KERNEL_PREFIX
414 value_type eval_sample(tuple_type x) const {
415 return get<offset>(x);
416 }
417
418 std::string name() const { return "x"; }
419
420 }; // class Expr<OrthogPolyImpl>
421
422 } // namespace ETPCE
423
424 } // namespace Sacado
425
426 #include "Sacado_ETPCE_ExpressionTraits.hpp"
427 #include "Sacado_ETPCE_OrthogPolyTraits.hpp"
428 #include "Sacado_ETPCE_OrthogPolyImp.hpp"
429 #include "Sacado_ETPCE_OrthogPolyOps.hpp"
430
431 namespace Sacado {
432
433 namespace ETPCE {
434
435 //! Generalized polynomial chaos expansion class
436 template <typename T, typename Storage>
437 class OrthogPoly : public Expr< OrthogPolyImpl<T,Storage> > {
438 public:
439
440 //! Typename of values
441 typedef typename OrthogPolyImpl<T,Storage>::value_type value_type;
442
443 //! Typename of scalars
444 typedef typename OrthogPolyImpl<T,Storage>::scalar_type scalar_type;
445
446 //! Typename of ordinals
447 typedef typename OrthogPolyImpl<T,Storage>::ordinal_type ordinal_type;
448
449 //! Typename of storage class
450 typedef typename OrthogPolyImpl<T,Storage>::storage_type storage_type;
451
452 //! Basis type
453 typedef typename OrthogPolyImpl<T,Storage>::basis_type basis_type;
454
455 //! Expansion type
456 typedef typename OrthogPolyImpl<T,Storage>::expansion_type expansion_type;
457
458 //! Stokhos approximation type
459 typedef typename OrthogPolyImpl<T,Storage>::approx_type approx_type;
460
461 typedef typename OrthogPolyImpl<T,Storage>::pointer pointer;
462 typedef typename OrthogPolyImpl<T,Storage>::const_pointer const_pointer;
463 typedef typename OrthogPolyImpl<T,Storage>::reference reference;
464 typedef typename OrthogPolyImpl<T,Storage>::const_reference const_reference;
465
466 //! Turn OrthogPoly into a meta-function class usable with mpl::apply
467 template <typename S>
468 struct apply {
469 typedef typename Sacado::mpl::apply<Storage,ordinal_type,S>::type storage_type;
470 typedef OrthogPoly<S,storage_type> type;
471 };
472
473 //! Default constructor
474 /*!
475 * Sets size to 1 and first coefficient to 0 (represents a constant).
476 */
477 OrthogPoly() :
478 Expr< OrthogPolyImpl<T,Storage> >() {}
479
480 //! Constructor with supplied value \c x
481 /*!
482 * Sets size to 1 and first coefficient to x (represents a constant).
483 */
484 OrthogPoly(const value_type& x) :
485 Expr< OrthogPolyImpl<T,Storage> >(x) {}
486
487 //! Constructor with expansion \c expansion (General case)
488 /*!
489 * Creates array of correct size and initializes coeffiencts to 0.
490 */
491 OrthogPoly(const Teuchos::RCP<expansion_type>& expansion) :
492 Expr< OrthogPolyImpl<T,Storage> >(expansion) {}
493
494 //! Constructor with expansion \c expansion and specified size \c sz
495 /*!
496 * Creates array of size \c sz and initializes coeffiencts to 0.
497 */
498 OrthogPoly(const Teuchos::RCP<expansion_type>& expansion,
499 ordinal_type sz) :
500 Expr< OrthogPolyImpl<T,Storage> >(expansion, sz) {}
501
502 //! Copy constructor
503 OrthogPoly(const OrthogPoly& x) :
504 Expr< OrthogPolyImpl<T,Storage> >(x) {}
505
506 //! Copy constructor from any Expression object
507 template <typename S> OrthogPoly(const Expr<S>& x) :
508 Expr< OrthogPolyImpl<T,Storage> >(x) {}
509
510 //! Destructor
511 ~OrthogPoly() {}
512
513 //! Assignment operator with constant right-hand-side
514 OrthogPoly& operator=(const value_type& val) {
515 OrthogPolyImpl<T,Storage>::operator=(val);
516 return *this;
517 }
518
519 //! Assignment with OrthogPoly right-hand-side
520 OrthogPoly& operator=(const OrthogPoly& x) {
521 OrthogPolyImpl<T,Storage>::operator=(static_cast<const OrthogPolyImpl<T,Storage>&>(x));
522 return *this;
523 }
524
525 //! Assignment with Expr< OrthogPolyImpl > right-hand-side
526 OrthogPoly& operator=(const Expr< OrthogPolyImpl<T,Storage> >& x) {
527 OrthogPolyImpl<T,Storage>::operator=(static_cast<const OrthogPolyImpl<T,Storage>&>(x));
528 return *this;
529 }
530
531 //! Assignment with any expression right-hand-side
532 template <typename S>
533 OrthogPoly& operator=(const Expr<S>& x) {
534 OrthogPolyImpl<T,Storage>::operator=(x);
535 return *this;
536 }
537
538 //@{
539
540 //! Addition-assignment operator with constant right-hand-side
541 OrthogPoly& operator += (const value_type& x) {
542 OrthogPolyImpl<T,Storage>::operator+=(x);
543 return *this;
544 }
545
546 //! Subtraction-assignment operator with constant right-hand-side
547 OrthogPoly& operator -= (const value_type& x) {
548 OrthogPolyImpl<T,Storage>::operator-=(x);
549 return *this;
550 }
551
552 //! Multiplication-assignment operator with constant right-hand-side
553 OrthogPoly& operator *= (const value_type& x) {
554 OrthogPolyImpl<T,Storage>::operator*=(x);
555 return *this;
556 }
557
558 //! Division-assignment operator with constant right-hand-side
559 OrthogPoly& operator /= (const value_type& x) {
560 OrthogPolyImpl<T,Storage>::operator/=(x);
561 return *this;
562 }
563
564 //! Addition-assignment operator with Expr right-hand-side
565 template <typename S>
566 OrthogPoly& operator += (const Expr<S>& x) {
567 *this = *this + x;
568 return *this;
569 }
570
571 //! Subtraction-assignment operator with Expr right-hand-side
572 template <typename S>
573 OrthogPoly& operator -= (const Expr<S>& x) {
574 *this = *this - x;
575 return *this;
576 }
577
578 //! Multiplication-assignment operator with Expr right-hand-side
579 template <typename S>
580 OrthogPoly& operator *= (const Expr<S>& x) {
581 *this = *this * x;
582 return *this;
583 }
584
585 //! Division-assignment operator with Expr right-hand-side
586 template <typename S>
587 OrthogPoly& operator /= (const Expr<S>& x) {
588 *this = *this / x;
589 return *this;
590 }
591
592 //@}
593
594 }; // class OrthogPoly
595
596 } // namespace ETPCE
597
598 template <typename T>
599 struct IsExpr< ETPCE::Expr<T> > {
600 static const bool value = true;
601 };
602
603 template <typename T>
604 struct BaseExprType< ETPCE::Expr<T> > {
605 typedef typename ETPCE::Expr<T>::base_expr_type type;
606 };
607
608 template <typename T, typename S>
609 struct IsExpr< ETPCE::OrthogPoly<T,S> > {
610 static const bool value = true;
611 };
612
613 template <typename T, typename S>
614 struct BaseExprType< ETPCE::OrthogPoly<T,S> > {
615 typedef ETPCE::OrthogPoly<T,S> type;
616 };
617
618 } // namespace Sacado
619
620 #endif // HAVE_STOKHOS_SACADO
621
622 #endif // SACADO_ETPCE_ORTHOGPOLY_HPP