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 //
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  static Teuchos::RCP<expansion_type> const_expansion_;
363 
365  Sacado::Handle< Stokhos::OrthogPolyApprox<int,value_type,Storage> > th_;
366 
367  }; // class OrthogPolyImpl
368 
369  template <typename T, typename Storage>
371  OrthogPolyImpl<T,Storage>::const_expansion_ =
373 
375 
380  template <typename T, typename Storage>
381  class Expr< OrthogPolyImpl<T,Storage> > :
382  public OrthogPolyImpl<T,Storage> {
383 
384  public:
385 
389  typedef typename OrthogPolyImpl<T,Storage>::approx_type approx_type;
391  typedef typename OrthogPolyImpl<T,Storage>::const_reference const_reference;
392 
393  typedef OrthogPoly<T,Storage> base_expr_type;
394 
396  static const int num_args = 1;
397 
399  Expr() :
400  OrthogPolyImpl<T,Storage>() {}
401 
403  Expr(const T & x) :
404  OrthogPolyImpl<T,Storage>(x) {}
405 
407  Expr(const Teuchos::RCP<typename OrthogPolyImpl<T,Storage>::expansion_type>& expansion) :
408  OrthogPolyImpl<T,Storage>(expansion) {}
409 
411  Expr(const Teuchos::RCP<typename OrthogPolyImpl<T,Storage>::expansion_type>& expansion,
413  OrthogPolyImpl<T,Storage>(expansion, sz) {}
414 
416  Expr(const Expr& x) :
417  OrthogPolyImpl<T,Storage>(static_cast<const OrthogPolyImpl<T,Storage>&>(x)) {}
418 
420  Expr(const OrthogPolyImpl<T,Storage>& x) :
421  OrthogPolyImpl<T,Storage>(x) {}
422 
424  template <typename S> Expr(const Expr<S>& x) :
425  OrthogPolyImpl<T,Storage>(x) {}
426 
428  ~Expr() {}
429 
430  const approx_type& getArg(int i) const {
431  return this->getOrthogPolyApprox(); }
432 
433  bool has_fast_access(int sz) const { return this->size() >= sz; }
434 
435  bool has_nonconst_expansion() const {
436  return this->expansion_ != this->const_expansion_;
437  }
438 
439  int order() const { return this->size() == 1 ? 0 : 1; }
440 
441  value_type fast_higher_order_coeff(int i) const {
442  return this->fastAccessCoeff(i);
443  }
444 
445  value_type higher_order_coeff(int i) const {
446  return this->coeff(i);
447  }
448 
449  template <int offset, typename tuple_type>
450  KERNEL_PREFIX
451  value_type eval_sample(tuple_type x) const {
452  return get<offset>(x);
453  }
454 
455  std::string name() const { return "x"; }
456 
457  }; // class Expr<OrthogPolyImpl>
458 
459  } // namespace ETPCE
460 
461 } // namespace Sacado
462 
467 
468 namespace Sacado {
469 
470  namespace ETPCE {
471 
473  template <typename T, typename Storage>
474  class OrthogPoly : public Expr< OrthogPolyImpl<T,Storage> > {
475  public:
476 
479 
482 
485 
488 
491 
493  typedef typename OrthogPolyImpl<T,Storage>::expansion_type expansion_type;
494 
496  typedef typename OrthogPolyImpl<T,Storage>::approx_type approx_type;
497 
498  typedef typename OrthogPolyImpl<T,Storage>::pointer pointer;
499  typedef typename OrthogPolyImpl<T,Storage>::const_pointer const_pointer;
500  typedef typename OrthogPolyImpl<T,Storage>::reference reference;
501  typedef typename OrthogPolyImpl<T,Storage>::const_reference const_reference;
502 
504  template <typename S>
505  struct apply {
506  typedef typename Sacado::mpl::apply<Storage,ordinal_type,S>::type storage_type;
507  typedef OrthogPoly<S,storage_type> type;
508  };
509 
511 
514  OrthogPoly() :
515  Expr< OrthogPolyImpl<T,Storage> >() {}
516 
518 
521  OrthogPoly(const value_type& x) :
522  Expr< OrthogPolyImpl<T,Storage> >(x) {}
523 
525 
528  OrthogPoly(const Teuchos::RCP<expansion_type>& expansion) :
529  Expr< OrthogPolyImpl<T,Storage> >(expansion) {}
530 
532 
535  OrthogPoly(const Teuchos::RCP<expansion_type>& expansion,
536  ordinal_type sz) :
537  Expr< OrthogPolyImpl<T,Storage> >(expansion, sz) {}
538 
540  OrthogPoly(const OrthogPoly& x) :
541  Expr< OrthogPolyImpl<T,Storage> >(x) {}
542 
544  template <typename S> OrthogPoly(const Expr<S>& x) :
545  Expr< OrthogPolyImpl<T,Storage> >(x) {}
546 
548  ~OrthogPoly() {}
549 
551  OrthogPoly& operator=(const value_type& val) {
552  OrthogPolyImpl<T,Storage>::operator=(val);
553  return *this;
554  }
555 
557  OrthogPoly& operator=(const OrthogPoly& x) {
558  OrthogPolyImpl<T,Storage>::operator=(static_cast<const OrthogPolyImpl<T,Storage>&>(x));
559  return *this;
560  }
561 
563  OrthogPoly& operator=(const Expr< OrthogPolyImpl<T,Storage> >& x) {
564  OrthogPolyImpl<T,Storage>::operator=(static_cast<const OrthogPolyImpl<T,Storage>&>(x));
565  return *this;
566  }
567 
569  template <typename S>
570  OrthogPoly& operator=(const Expr<S>& x) {
571  OrthogPolyImpl<T,Storage>::operator=(x);
572  return *this;
573  }
574 
576 
578  OrthogPoly& operator += (const value_type& x) {
579  OrthogPolyImpl<T,Storage>::operator+=(x);
580  return *this;
581  }
582 
584  OrthogPoly& operator -= (const value_type& x) {
585  OrthogPolyImpl<T,Storage>::operator-=(x);
586  return *this;
587  }
588 
590  OrthogPoly& operator *= (const value_type& x) {
591  OrthogPolyImpl<T,Storage>::operator*=(x);
592  return *this;
593  }
594 
596  OrthogPoly& operator /= (const value_type& x) {
597  OrthogPolyImpl<T,Storage>::operator/=(x);
598  return *this;
599  }
600 
602  template <typename S>
603  OrthogPoly& operator += (const Expr<S>& x) {
604  *this = *this + x;
605  return *this;
606  }
607 
609  template <typename S>
610  OrthogPoly& operator -= (const Expr<S>& x) {
611  *this = *this - x;
612  return *this;
613  }
614 
616  template <typename S>
617  OrthogPoly& operator *= (const Expr<S>& x) {
618  *this = *this * x;
619  return *this;
620  }
621 
623  template <typename S>
624  OrthogPoly& operator /= (const Expr<S>& x) {
625  *this = *this / x;
626  return *this;
627  }
628 
630 
631  }; // class OrthogPoly
632 
633  } // namespace ETPCE
634 
635  template <typename T>
636  struct IsExpr< ETPCE::Expr<T> > {
637  static const bool value = true;
638  };
639 
640  template <typename T>
641  struct BaseExprType< ETPCE::Expr<T> > {
642  typedef typename ETPCE::Expr<T>::base_expr_type type;
643  };
644 
645  template <typename T, typename S>
646  struct IsExpr< ETPCE::OrthogPoly<T,S> > {
647  static const bool value = true;
648  };
649 
650  template <typename T, typename S>
651  struct BaseExprType< ETPCE::OrthogPoly<T,S> > {
652  typedef ETPCE::OrthogPoly<T,S> type;
653  };
654 
655 } // namespace Sacado
656 
657 #endif // HAVE_STOKHOS_SACADO
658 
659 #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.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
expr1 expr1 expr1 expr2 expr1 expr1 c expr2 expr1 c fastAccessCoeff(j)-expr2.val(j)
Orthogonal polynomial expansion class for constant (size 1) expansions.
Class to store coefficients of a projection onto an orthogonal polynomial basis.
expr val()
Orthogonal polynomial expansions based on numerical quadrature.