Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_ETPCE_OrthogPoly.hpp
Go to the documentation of this file.
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 
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 
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 
55 
59  template <typename ExprT> class Expr {};
60 
61  // Forward declaration
62  template <typename T, typename S> class OrthogPoly;
63 
65  template <typename T, typename Storage >
66  class OrthogPolyImpl {
67  public:
68 
70  typedef T value_type;
71 
73  typedef typename ScalarType<T>::type scalar_type;
74 
76  typedef int ordinal_type;
77 
79  typedef Storage storage_type;
80 
83 
86 
89 
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 
100 
103  OrthogPolyImpl();
104 
106 
109  OrthogPolyImpl(const value_type& x);
110 
112 
115  OrthogPolyImpl(const Teuchos::RCP<expansion_type>& expansion);
116 
118 
121  OrthogPolyImpl(const Teuchos::RCP<expansion_type>& expansion,
122  ordinal_type sz);
123 
125  OrthogPolyImpl(const OrthogPolyImpl& x);
126 
128  template <typename S> OrthogPolyImpl(const Expr<S>& x);
129 
131  ~OrthogPolyImpl() {}
132 
134  void init(const T& v) { th_->init(v); }
135 
137  void init(const T* v) { th_->init(v); }
138 
140  template <typename S>
141  void init(const OrthogPolyImpl<T,S>& v) { th_->init(v.getOrthogPolyApprox()); }
142 
144  void load(T* v) { th_->load(v); }
145 
147  template <typename S>
148  void load(OrthogPolyImpl<T,S>& v) { th_->load(v.getOrthogPolyApprox()); }
149 
151 
154  void reset(const Teuchos::RCP<expansion_type>& expansion);
155 
157 
160  void reset(const Teuchos::RCP<expansion_type>& expansion,
161  ordinal_type sz);
162 
164 
173  void copyForWrite() { th_.makeOwnCopy(); }
174 
176  value_type evaluate(const Teuchos::Array<value_type>& point) const;
177 
179  value_type evaluate(const Teuchos::Array<value_type>& point,
180  const Teuchos::Array<value_type>& bvals) const;
181 
183  value_type mean() const {return th_->mean(); }
184 
186  value_type standard_deviation() const { return th_->standard_deviation(); }
187 
189  value_type two_norm() const { return th_->two_norm(); }
190 
192  value_type two_norm_squared() const { return th_->two_norm_squared(); }
193 
195  value_type inner_product(const OrthogPolyImpl& b) const {
196  return th_->inner_product(b.getOrthogPolyApprox()); }
197 
199  std::ostream& print(std::ostream& os) const { return th_->print(os); }
200 
202  template <typename S> bool isEqualTo(const Expr<S>& x) const;
203 
208 
210  OrthogPolyImpl& operator=(const value_type& val);
211 
213  OrthogPolyImpl& operator=(const OrthogPolyImpl& x);
214 
216  template <typename S>
217  OrthogPolyImpl& operator=(const Expr<S>& x);
218 
220 
225 
227  Teuchos::RCP<basis_type> basis() const { return th_->basis(); }
228 
230  Teuchos::RCP<expansion_type> expansion() const { return expansion_; }
231 
233  Teuchos::RCP<quad_expansion_type> quad_expansion() const { return quad_expansion_; }
234 
236 
241 
243  const_reference val() const { return (*th_)[0]; }
244 
246  reference val() { return (*th_)[0]; }
247 
249 
254 
256  ordinal_type size() const { return th_->size();}
257 
259  bool hasFastAccess(ordinal_type sz) const { return th_->size()>=sz;}
260 
262  const_pointer coeff() const { return th_->coeff();}
263 
265  pointer coeff() { return th_->coeff();}
266 
268  value_type coeff(ordinal_type i) const {
269  return i<th_->size() ? (*th_)[i]:value_type(0.); }
270 
272  reference fastAccessCoeff(ordinal_type i) { return (*th_)[i];}
273 
275  value_type fastAccessCoeff(ordinal_type i) const { return (*th_)[i];}
276 
278  reference term(ordinal_type dimension, ordinal_type order) {
279  return th_->term(dimension, order); }
280 
282  const_reference term(ordinal_type dimension, ordinal_type order) const {
283  return th_->term(dimension, order); }
284 
286  Teuchos::Array<ordinal_type> order(ordinal_type term) const {
287  return th_->order(term); }
288 
290 
295 
297  OrthogPolyImpl& operator += (const value_type& x);
298 
300  OrthogPolyImpl& operator -= (const value_type& x);
301 
303  OrthogPolyImpl& operator *= (const value_type& x);
304 
306  OrthogPolyImpl& operator /= (const value_type& x);
307 
309 
311  const approx_type& getOrthogPolyApprox() const { return *th_; }
312 
314  approx_type& getOrthogPolyApprox() { return *th_; }
315 
316  protected:
317 
319  template <typename S> void expressionCopy(const Expr<S>& x);
320 
321  protected:
322 
324  Teuchos::RCP<expansion_type> expansion_;
325 
327  Teuchos::RCP<quad_expansion_type> quad_expansion_;
328 
330  Teuchos::RCP<expansion_type> const_expansion_;
331 
333  Sacado::Handle< Stokhos::OrthogPolyApprox<int,value_type,Storage> > th_;
334 
335  }; // class OrthogPolyImpl
336 
338 
343  template <typename T, typename Storage>
344  class Expr< OrthogPolyImpl<T,Storage> > :
345  public OrthogPolyImpl<T,Storage> {
346 
347  public:
348 
352  typedef typename OrthogPolyImpl<T,Storage>::approx_type approx_type;
354  typedef typename OrthogPolyImpl<T,Storage>::const_reference const_reference;
355 
356  typedef OrthogPoly<T,Storage> base_expr_type;
357 
359  static const int num_args = 1;
360 
362  Expr() :
363  OrthogPolyImpl<T,Storage>() {}
364 
366  Expr(const T & x) :
367  OrthogPolyImpl<T,Storage>(x) {}
368 
370  Expr(const Teuchos::RCP<typename OrthogPolyImpl<T,Storage>::expansion_type>& expansion) :
371  OrthogPolyImpl<T,Storage>(expansion) {}
372 
374  Expr(const Teuchos::RCP<typename OrthogPolyImpl<T,Storage>::expansion_type>& expansion,
376  OrthogPolyImpl<T,Storage>(expansion, sz) {}
377 
379  Expr(const Expr& x) :
380  OrthogPolyImpl<T,Storage>(static_cast<const OrthogPolyImpl<T,Storage>&>(x)) {}
381 
383  Expr(const OrthogPolyImpl<T,Storage>& x) :
384  OrthogPolyImpl<T,Storage>(x) {}
385 
387  template <typename S> Expr(const Expr<S>& x) :
388  OrthogPolyImpl<T,Storage>(x) {}
389 
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 
430 
431 namespace Sacado {
432 
433  namespace ETPCE {
434 
436  template <typename T, typename Storage>
437  class OrthogPoly : public Expr< OrthogPolyImpl<T,Storage> > {
438  public:
439 
442 
445 
448 
451 
454 
456  typedef typename OrthogPolyImpl<T,Storage>::expansion_type expansion_type;
457 
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 
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 
474 
477  OrthogPoly() :
478  Expr< OrthogPolyImpl<T,Storage> >() {}
479 
481 
484  OrthogPoly(const value_type& x) :
485  Expr< OrthogPolyImpl<T,Storage> >(x) {}
486 
488 
491  OrthogPoly(const Teuchos::RCP<expansion_type>& expansion) :
492  Expr< OrthogPolyImpl<T,Storage> >(expansion) {}
493 
495 
498  OrthogPoly(const Teuchos::RCP<expansion_type>& expansion,
499  ordinal_type sz) :
500  Expr< OrthogPolyImpl<T,Storage> >(expansion, sz) {}
501 
503  OrthogPoly(const OrthogPoly& x) :
504  Expr< OrthogPolyImpl<T,Storage> >(x) {}
505 
507  template <typename S> OrthogPoly(const Expr<S>& x) :
508  Expr< OrthogPolyImpl<T,Storage> >(x) {}
509 
511  ~OrthogPoly() {}
512 
514  OrthogPoly& operator=(const value_type& val) {
515  OrthogPolyImpl<T,Storage>::operator=(val);
516  return *this;
517  }
518 
520  OrthogPoly& operator=(const OrthogPoly& x) {
521  OrthogPolyImpl<T,Storage>::operator=(static_cast<const OrthogPolyImpl<T,Storage>&>(x));
522  return *this;
523  }
524 
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 
532  template <typename S>
533  OrthogPoly& operator=(const Expr<S>& x) {
534  OrthogPolyImpl<T,Storage>::operator=(x);
535  return *this;
536  }
537 
539 
541  OrthogPoly& operator += (const value_type& x) {
542  OrthogPolyImpl<T,Storage>::operator+=(x);
543  return *this;
544  }
545 
547  OrthogPoly& operator -= (const value_type& x) {
548  OrthogPolyImpl<T,Storage>::operator-=(x);
549  return *this;
550  }
551 
553  OrthogPoly& operator *= (const value_type& x) {
554  OrthogPolyImpl<T,Storage>::operator*=(x);
555  return *this;
556  }
557 
559  OrthogPoly& operator /= (const value_type& x) {
560  OrthogPolyImpl<T,Storage>::operator/=(x);
561  return *this;
562  }
563 
565  template <typename S>
566  OrthogPoly& operator += (const Expr<S>& x) {
567  *this = *this + x;
568  return *this;
569  }
570 
572  template <typename S>
573  OrthogPoly& operator -= (const Expr<S>& x) {
574  *this = *this - x;
575  return *this;
576  }
577 
579  template <typename S>
580  OrthogPoly& operator *= (const Expr<S>& x) {
581  *this = *this * x;
582  return *this;
583  }
584 
586  template <typename S>
587  OrthogPoly& operator /= (const Expr<S>& x) {
588  *this = *this / x;
589  return *this;
590  }
591 
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
Stokhos::StandardStorage< int, double > storage_type
Stokhos::LegendreBasis< int, double > basis_type
Abstract base class for orthogonal polynomial-based expansions.
Abstract base class for multivariate orthogonal polynomials.
expr1 expr1 expr1 expr2 expr1 expr1 c expr2 expr1 c fastAccessCoeff(j)-expr2.val(j)
Class to store coefficients of a projection onto an orthogonal polynomial basis.
expr val()
Orthogonal polynomial expansions based on numerical quadrature.