Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
Sacado_ETPCE_OrthogPoly.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef SACADO_ETPCE_ORTHOGPOLY_HPP
43 #define SACADO_ETPCE_ORTHOGPOLY_HPP
44 
45 #include "Stokhos_ConfigDefs.h"
46 
47 #ifdef HAVE_STOKHOS_SACADO
48 
49 #include "Teuchos_RCP.hpp"
50 
51 #include "Sacado_Traits.hpp"
52 #include "Sacado_Handle.hpp"
53 
58 
59 #include "Sacado_mpl_apply.hpp"
60 
61 #include <ostream> // for std::ostream
62 
63 #ifdef HAVE_STOKHOS_THRUST
64 #include "thrust/tuple.h"
65 #endif
66 
67 #ifndef KERNEL_PREFIX
68 #define KERNEL_PREFIX
69 #endif
70 
71 namespace Sacado {
72 
74  namespace ETPCE {
75 
76  template <int k, typename T> KERNEL_PREFIX T&
77  get(T* a) { return a[k]; }
78  template <int k, typename T> KERNEL_PREFIX const T&
79  get(const T* a) { return a[k]; }
80 
81  template <int k, int N, typename T> KERNEL_PREFIX T&
82  get(T a[N]) { return a[k]; }
83  template <int k, int N, typename T> KERNEL_PREFIX const T&
84  get(const T a[N]) { return a[k]; }
85 
87 
91  template <typename ExprT> class Expr {};
92 
93  // Forward declaration
94  template <typename T, typename S> class OrthogPoly;
95 
97  template <typename T, typename Storage >
98  class OrthogPolyImpl {
99  public:
100 
102  typedef T value_type;
103 
105  typedef typename ScalarType<T>::type scalar_type;
106 
108  typedef int ordinal_type;
109 
111  typedef Storage storage_type;
112 
115 
118 
121 
124 
125  typedef typename approx_type::pointer pointer;
126  typedef typename approx_type::const_pointer const_pointer;
127  typedef typename approx_type::reference reference;
128  typedef typename approx_type::const_reference const_reference;
129 
130 
132 
135  OrthogPolyImpl();
136 
138 
141  OrthogPolyImpl(const value_type& x);
142 
144 
147  OrthogPolyImpl(const Teuchos::RCP<expansion_type>& expansion);
148 
150 
153  OrthogPolyImpl(const Teuchos::RCP<expansion_type>& expansion,
154  ordinal_type sz);
155 
157  OrthogPolyImpl(const OrthogPolyImpl& x);
158 
160  template <typename S> OrthogPolyImpl(const Expr<S>& x);
161 
163  ~OrthogPolyImpl() {}
164 
166  void init(const T& v) { th_->init(v); }
167 
169  void init(const T* v) { th_->init(v); }
170 
172  template <typename S>
173  void init(const OrthogPolyImpl<T,S>& v) { th_->init(v.getOrthogPolyApprox()); }
174 
176  void load(T* v) { th_->load(v); }
177 
179  template <typename S>
180  void load(OrthogPolyImpl<T,S>& v) { th_->load(v.getOrthogPolyApprox()); }
181 
183 
186  void reset(const Teuchos::RCP<expansion_type>& expansion);
187 
189 
192  void reset(const Teuchos::RCP<expansion_type>& expansion,
193  ordinal_type sz);
194 
196 
205  void copyForWrite() { th_.makeOwnCopy(); }
206 
208  value_type evaluate(const Teuchos::Array<value_type>& point) const;
209 
211  value_type evaluate(const Teuchos::Array<value_type>& point,
212  const Teuchos::Array<value_type>& bvals) const;
213 
215  value_type mean() const {return th_->mean(); }
216 
218  value_type standard_deviation() const { return th_->standard_deviation(); }
219 
221  value_type two_norm() const { return th_->two_norm(); }
222 
224  value_type two_norm_squared() const { return th_->two_norm_squared(); }
225 
227  value_type inner_product(const OrthogPolyImpl& b) const {
228  return th_->inner_product(b.getOrthogPolyApprox()); }
229 
231  std::ostream& print(std::ostream& os) const { return th_->print(os); }
232 
234  template <typename S> bool isEqualTo(const Expr<S>& x) const;
235 
240 
242  OrthogPolyImpl& operator=(const value_type& val);
243 
245  OrthogPolyImpl& operator=(const OrthogPolyImpl& x);
246 
248  template <typename S>
249  OrthogPolyImpl& operator=(const Expr<S>& x);
250 
252 
257 
259  Teuchos::RCP<basis_type> basis() const { return th_->basis(); }
260 
262  Teuchos::RCP<expansion_type> expansion() const { return expansion_; }
263 
265  Teuchos::RCP<quad_expansion_type> quad_expansion() const { return quad_expansion_; }
266 
268 
273 
275  const_reference val() const { return (*th_)[0]; }
276 
278  reference val() { return (*th_)[0]; }
279 
281 
286 
288  ordinal_type size() const { return th_->size();}
289 
291  bool hasFastAccess(ordinal_type sz) const { return th_->size()>=sz;}
292 
294  const_pointer coeff() const { return th_->coeff();}
295 
297  pointer coeff() { return th_->coeff();}
298 
300  value_type coeff(ordinal_type i) const {
301  return i<th_->size() ? (*th_)[i]:value_type(0.); }
302 
304  reference fastAccessCoeff(ordinal_type i) { return (*th_)[i];}
305 
307  value_type fastAccessCoeff(ordinal_type i) const { return (*th_)[i];}
308 
310  reference term(ordinal_type dimension, ordinal_type order) {
311  return th_->term(dimension, order); }
312 
314  const_reference term(ordinal_type dimension, ordinal_type order) const {
315  return th_->term(dimension, order); }
316 
318  Teuchos::Array<ordinal_type> order(ordinal_type term) const {
319  return th_->order(term); }
320 
322 
327 
329  OrthogPolyImpl& operator += (const value_type& x);
330 
332  OrthogPolyImpl& operator -= (const value_type& x);
333 
335  OrthogPolyImpl& operator *= (const value_type& x);
336 
338  OrthogPolyImpl& operator /= (const value_type& x);
339 
341 
343  const approx_type& getOrthogPolyApprox() const { return *th_; }
344 
346  approx_type& getOrthogPolyApprox() { return *th_; }
347 
348  protected:
349 
351  template <typename S> void expressionCopy(const Expr<S>& x);
352 
353  protected:
354 
356  Teuchos::RCP<expansion_type> expansion_;
357 
359  Teuchos::RCP<quad_expansion_type> quad_expansion_;
360 
362  Teuchos::RCP<expansion_type> const_expansion_;
363 
365  Sacado::Handle< Stokhos::OrthogPolyApprox<int,value_type,Storage> > th_;
366 
367  }; // class OrthogPolyImpl
368 
370 
375  template <typename T, typename Storage>
376  class Expr< OrthogPolyImpl<T,Storage> > :
377  public OrthogPolyImpl<T,Storage> {
378 
379  public:
380 
384  typedef typename OrthogPolyImpl<T,Storage>::approx_type approx_type;
386  typedef typename OrthogPolyImpl<T,Storage>::const_reference const_reference;
387 
388  typedef OrthogPoly<T,Storage> base_expr_type;
389 
391  static const int num_args = 1;
392 
394  Expr() :
395  OrthogPolyImpl<T,Storage>() {}
396 
398  Expr(const T & x) :
399  OrthogPolyImpl<T,Storage>(x) {}
400 
402  Expr(const Teuchos::RCP<typename OrthogPolyImpl<T,Storage>::expansion_type>& expansion) :
403  OrthogPolyImpl<T,Storage>(expansion) {}
404 
406  Expr(const Teuchos::RCP<typename OrthogPolyImpl<T,Storage>::expansion_type>& expansion,
408  OrthogPolyImpl<T,Storage>(expansion, sz) {}
409 
411  Expr(const Expr& x) :
412  OrthogPolyImpl<T,Storage>(static_cast<const OrthogPolyImpl<T,Storage>&>(x)) {}
413 
415  Expr(const OrthogPolyImpl<T,Storage>& x) :
416  OrthogPolyImpl<T,Storage>(x) {}
417 
419  template <typename S> Expr(const Expr<S>& x) :
420  OrthogPolyImpl<T,Storage>(x) {}
421 
423  ~Expr() {}
424 
425  const approx_type& getArg(int i) const {
426  return this->getOrthogPolyApprox(); }
427 
428  bool has_fast_access(int sz) const { return this->size() >= sz; }
429 
430  bool has_nonconst_expansion() const {
431  return this->expansion_ != this->const_expansion_;
432  }
433 
434  int order() const { return this->size() == 1 ? 0 : 1; }
435 
436  value_type fast_higher_order_coeff(int i) const {
437  return this->fastAccessCoeff(i);
438  }
439 
440  value_type higher_order_coeff(int i) const {
441  return this->coeff(i);
442  }
443 
444  template <int offset, typename tuple_type>
445  KERNEL_PREFIX
446  value_type eval_sample(tuple_type x) const {
447  return get<offset>(x);
448  }
449 
450  std::string name() const { return "x"; }
451 
452  }; // class Expr<OrthogPolyImpl>
453 
454  } // namespace ETPCE
455 
456 } // namespace Sacado
457 
462 
463 namespace Sacado {
464 
465  namespace ETPCE {
466 
468  template <typename T, typename Storage>
469  class OrthogPoly : public Expr< OrthogPolyImpl<T,Storage> > {
470  public:
471 
474 
477 
480 
483 
486 
488  typedef typename OrthogPolyImpl<T,Storage>::expansion_type expansion_type;
489 
491  typedef typename OrthogPolyImpl<T,Storage>::approx_type approx_type;
492 
493  typedef typename OrthogPolyImpl<T,Storage>::pointer pointer;
494  typedef typename OrthogPolyImpl<T,Storage>::const_pointer const_pointer;
495  typedef typename OrthogPolyImpl<T,Storage>::reference reference;
496  typedef typename OrthogPolyImpl<T,Storage>::const_reference const_reference;
497 
499  template <typename S>
500  struct apply {
501  typedef typename Sacado::mpl::apply<Storage,ordinal_type,S>::type storage_type;
502  typedef OrthogPoly<S,storage_type> type;
503  };
504 
506 
509  OrthogPoly() :
510  Expr< OrthogPolyImpl<T,Storage> >() {}
511 
513 
516  OrthogPoly(const value_type& x) :
517  Expr< OrthogPolyImpl<T,Storage> >(x) {}
518 
520 
523  OrthogPoly(const Teuchos::RCP<expansion_type>& expansion) :
524  Expr< OrthogPolyImpl<T,Storage> >(expansion) {}
525 
527 
530  OrthogPoly(const Teuchos::RCP<expansion_type>& expansion,
531  ordinal_type sz) :
532  Expr< OrthogPolyImpl<T,Storage> >(expansion, sz) {}
533 
535  OrthogPoly(const OrthogPoly& x) :
536  Expr< OrthogPolyImpl<T,Storage> >(x) {}
537 
539  template <typename S> OrthogPoly(const Expr<S>& x) :
540  Expr< OrthogPolyImpl<T,Storage> >(x) {}
541 
543  ~OrthogPoly() {}
544 
546  OrthogPoly& operator=(const value_type& val) {
547  OrthogPolyImpl<T,Storage>::operator=(val);
548  return *this;
549  }
550 
552  OrthogPoly& operator=(const OrthogPoly& x) {
553  OrthogPolyImpl<T,Storage>::operator=(static_cast<const OrthogPolyImpl<T,Storage>&>(x));
554  return *this;
555  }
556 
558  OrthogPoly& operator=(const Expr< OrthogPolyImpl<T,Storage> >& x) {
559  OrthogPolyImpl<T,Storage>::operator=(static_cast<const OrthogPolyImpl<T,Storage>&>(x));
560  return *this;
561  }
562 
564  template <typename S>
565  OrthogPoly& operator=(const Expr<S>& x) {
566  OrthogPolyImpl<T,Storage>::operator=(x);
567  return *this;
568  }
569 
571 
573  OrthogPoly& operator += (const value_type& x) {
574  OrthogPolyImpl<T,Storage>::operator+=(x);
575  return *this;
576  }
577 
579  OrthogPoly& operator -= (const value_type& x) {
580  OrthogPolyImpl<T,Storage>::operator-=(x);
581  return *this;
582  }
583 
585  OrthogPoly& operator *= (const value_type& x) {
586  OrthogPolyImpl<T,Storage>::operator*=(x);
587  return *this;
588  }
589 
591  OrthogPoly& operator /= (const value_type& x) {
592  OrthogPolyImpl<T,Storage>::operator/=(x);
593  return *this;
594  }
595 
597  template <typename S>
598  OrthogPoly& operator += (const Expr<S>& x) {
599  *this = *this + x;
600  return *this;
601  }
602 
604  template <typename S>
605  OrthogPoly& operator -= (const Expr<S>& x) {
606  *this = *this - x;
607  return *this;
608  }
609 
611  template <typename S>
612  OrthogPoly& operator *= (const Expr<S>& x) {
613  *this = *this * x;
614  return *this;
615  }
616 
618  template <typename S>
619  OrthogPoly& operator /= (const Expr<S>& x) {
620  *this = *this / x;
621  return *this;
622  }
623 
625 
626  }; // class OrthogPoly
627 
628  } // namespace ETPCE
629 
630  template <typename T>
631  struct IsExpr< ETPCE::Expr<T> > {
632  static const bool value = true;
633  };
634 
635  template <typename T>
636  struct BaseExprType< ETPCE::Expr<T> > {
637  typedef typename ETPCE::Expr<T>::base_expr_type type;
638  };
639 
640  template <typename T, typename S>
641  struct IsExpr< ETPCE::OrthogPoly<T,S> > {
642  static const bool value = true;
643  };
644 
645  template <typename T, typename S>
646  struct BaseExprType< ETPCE::OrthogPoly<T,S> > {
647  typedef ETPCE::OrthogPoly<T,S> type;
648  };
649 
650 } // namespace Sacado
651 
652 #endif // HAVE_STOKHOS_SACADO
653 
654 #endif // SACADO_ETPCE_ORTHOGPOLY_HPP
Stokhos::StandardStorage< int, double > storage_type
expr1 expr1 expr1 expr2 expr1 expr1 c expr2 expr1 c fastAccessCoeff(j) - expr2.val(j)
Stokhos::LegendreBasis< int, double > basis_type
Abstract base class for orthogonal polynomial-based expansions.
Abstract base class for multivariate orthogonal polynomials.
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
Definition: csr_vector.h:260
Class to store coefficients of a projection onto an orthogonal polynomial basis.
expr val()
Orthogonal polynomial expansions based on numerical quadrature.