Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_OrthogPolyApproxImp.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 #include "Stokhos_ProductBasis.hpp"
11 
12 template <typename ordinal_type, typename value_type, typename storage_type>
16  ordinal_type sz,
17  const value_type* vals) :
18  basis_(theBasis),
19  coeff_(0)
20 {
21  if (sz != 0)
22  coeff_.resize(sz);
23  else if (theBasis != Teuchos::null)
24  coeff_.resize(theBasis->size());
25  else
27  if (vals != NULL)
28  coeff_.init(vals);
29 }
30 
31 template <typename ordinal_type, typename value_type, typename storage_type>
34  basis_(x.basis_),
35  coeff_(x.coeff_)
36 {
37 }
38 
39 template <typename ordinal_type, typename value_type, typename storage_type>
42 {
43 }
44 
45 template <typename ordinal_type, typename value_type, typename storage_type>
49 {
50  if (this != &x) {
51  basis_ = x.basis_;
52  coeff_ = x.coeff_;
53  }
54 
55  return *this;
56 }
57 
58 template <typename ordinal_type, typename value_type, typename storage_type>
62 {
63  coeff_.init(value_type(0));
64  coeff_.init(&v, 1);
65 
66  return *this;
67 }
68 
69 template <typename ordinal_type, typename value_type, typename storage_type>
70 void
72 init(const value_type& v)
73 {
74  coeff_.init(v);
75 }
76 
77 template <typename ordinal_type, typename value_type, typename storage_type>
78 void
80 init(const value_type* v)
81 {
82  coeff_.init(v);
83 }
84 
85 template <typename ordinal_type, typename value_type, typename storage_type>
86 void
89 {
90  coeff_.load(v);
91 }
92 
93 template <typename ordinal_type, typename value_type, typename storage_type>
96 basis() const
97 {
98  return basis_;
99 }
100 
101 template <typename ordinal_type, typename value_type, typename storage_type>
102 void
105 {
106  basis_ = new_basis;
107  if (sz != 0)
108  resize(sz);
109  else if (basis_ != Teuchos::null)
110  resize(basis_->size());
111  else
112  resize(1);
113 }
114 
115 template <typename ordinal_type, typename value_type, typename storage_type>
116 void
119 {
120  coeff_.resize(sz);
121 }
122 
123 template <typename ordinal_type, typename value_type, typename storage_type>
126 size() const
127 {
128  return coeff_.size();
129 }
130 
131 template <typename ordinal_type, typename value_type, typename storage_type>
135 {
136 #ifdef STOKHOS_DEBUG
137  TEUCHOS_TEST_FOR_EXCEPTION(coeff_.size() == 0, std::logic_error,
138  "Stokhos::OrthogPolyApprox::coeff(): " <<
139  "Coefficient array is empty!");
140 #endif
141  return coeff_.coeff();
142 }
143 
144 template <typename ordinal_type, typename value_type, typename storage_type>
147 coeff() const
148 {
149 #ifdef STOKHOS_DEBUG
150  TEUCHOS_TEST_FOR_EXCEPTION(coeff_.size() == 0, std::logic_error,
151  "Stokhos::OrthogPolyApprox::coeff(): " <<
152  "Coefficient array is empty!");
153 #endif
154  return coeff_.coeff();
155 }
156 
157 template <typename ordinal_type, typename value_type, typename storage_type>
161 {
162  return coeff_[i];
163 }
164 
165 template <typename ordinal_type, typename value_type, typename storage_type>
169 {
170  return coeff_[i];
171 }
172 
173 template <typename ordinal_type, typename value_type, typename storage_type>
176 term(ordinal_type dimension, ordinal_type theOrder)
177 {
179  product_basis = Teuchos::rcp_dynamic_cast< const Stokhos::ProductBasis<ordinal_type, value_type> >(basis_, true);
180  ordinal_type d = basis_->dimension();
181  MultiIndex<ordinal_type> theTerm(d);
182  theTerm[dimension] = theOrder;
183  ordinal_type index = product_basis->index(theTerm);
184  return coeff_[index];
185 }
186 
187 template <typename ordinal_type, typename value_type, typename storage_type>
190 term(ordinal_type dimension, ordinal_type theOrder) const
191 {
193  product_basis = Teuchos::rcp_dynamic_cast< const Stokhos::ProductBasis<ordinal_type, value_type> >(basis_, true);
194  ordinal_type d = basis_->dimension();
195  MultiIndex<ordinal_type> theTerm(d);
196  theTerm[dimension] = theOrder;
197  ordinal_type index = product_basis->index(theTerm);
198  return coeff_[index];
199 }
200 
201 template <typename ordinal_type, typename value_type, typename storage_type>
204 order(ordinal_type theTerm) const
205 {
207  product_basis = Teuchos::rcp_dynamic_cast< const Stokhos::ProductBasis<ordinal_type, value_type> >(basis_, true);
208  return product_basis->term(theTerm);
209 }
210 
211 template <typename ordinal_type, typename value_type, typename storage_type>
215 {
216  Teuchos::Array<value_type> basis_vals(basis_->size());
217  basis_->evaluateBases(point, basis_vals);
218  return evaluate(point, basis_vals);
219 }
220 
221 template <typename ordinal_type, typename value_type, typename storage_type>
225  const Teuchos::Array<value_type>& basis_vals) const
226 {
227  value_type val = value_type(0.0);
228  for (ordinal_type i=0; i<static_cast<ordinal_type>(coeff_.size()); i++)
229  val += coeff_[i]*basis_vals[i];
230 
231  return val;
232 }
233 
234 template <typename ordinal_type, typename value_type, typename storage_type>
237 mean() const
238 {
239  return coeff_[0];
240 }
241 
242 template <typename ordinal_type, typename value_type, typename storage_type>
246 {
247  value_type std_dev = 0.0;
248  for (ordinal_type i=1; i<static_cast<ordinal_type>(coeff_.size()); i++) {
249  std_dev += coeff_[i]*coeff_[i]*basis_->norm_squared(i);
250  }
251  std_dev = std::sqrt(std_dev);
252  return std_dev;
253 }
254 
255 template <typename ordinal_type, typename value_type, typename storage_type>
258 two_norm() const
259 {
260  return std::sqrt(this->two_norm_squared());
261 }
262 
263 template <typename ordinal_type, typename value_type, typename storage_type>
267 {
268  value_type nrm = 0.0;
269  if (basis_ == Teuchos::null) { // Check for special case of constants
271  coeff_.size() != 1, std::logic_error,
272  "basis_ == null && coeff_.size() > 1");
273  nrm = coeff_[0]*coeff_[0];
274  }
275  else {
276  for (ordinal_type i=0; i<static_cast<ordinal_type>(coeff_.size()); i++)
277  nrm += coeff_[i]*coeff_[i]*basis_->norm_squared(i);
278  }
279  return nrm;
280 }
281 
282 template <typename ordinal_type, typename value_type, typename storage_type>
286 {
287  // Check a and b are compatible
289  basis_ == Teuchos::null && coeff_.size() != 1, std::logic_error,
290  "basis_ == null && coeff_.size() > 1");
292  b.basis_ == Teuchos::null && b.coeff_.size() != 1, std::logic_error,
293  "b.basis_ == null && b.coeff_.size() > 1");
295  coeff_.size() != b.coeff_.size() &&
296  coeff_.size() != 1 && b.coeff_.size() != 1, std::logic_error,
297  "Coefficient array sizes do not match");
298 
299  value_type v = 0.0;
300  if (coeff_.size() == 1 || b.coeff_.size() == 1)
301  v = coeff_[0]*b.coeff_[0];
302  else
303  for (ordinal_type i=0; i<static_cast<ordinal_type>(coeff_.size()); i++)
304  v += coeff_[i]*b.coeff_[i]*basis_->norm_squared(i);
305 
306  return v;
307 }
308 
309 template <typename ordinal_type, typename value_type, typename storage_type>
310 std::ostream&
312 print(std::ostream& os) const
313 {
314  os << "Stokhos::OrthogPolyApprox of size " << coeff_.size() << " in basis "
315  << "\n\t" << basis_->getName() << ":" << std::endl;
316 
318  product_basis = Teuchos::rcp_dynamic_cast< const Stokhos::ProductBasis<ordinal_type, value_type> >(basis_);
319 
320  if (product_basis != Teuchos::null) {
321  for (ordinal_type i=0; i<static_cast<ordinal_type>(coeff_.size()); i++) {
322  const Stokhos::MultiIndex<ordinal_type>& trm = product_basis->term(i);
323  os << "\t\t(";
324  for (ordinal_type j=0; j<static_cast<ordinal_type>(trm.size())-1; j++)
325  os << trm[j] << ", ";
326  os << trm[trm.size()-1] << ") = " << coeff_[i] << std::endl;
327  }
328  }
329  else {
330  os << "[ ";
331 
332  for (ordinal_type i=0; i<static_cast<ordinal_type>(coeff_.size()); i++) {
333  os << coeff_[i] << " ";
334  }
335 
336  os << "]\n";
337  }
338 
339  return os;
340 }
341 
342 template <typename ordinal_type, typename value_type, typename storage_type>
343 std::ostream&
345  std::ostream& os,
347 {
348  os << "[ ";
349 
350  for (ordinal_type i=0; i<static_cast<ordinal_type>(a.size()); i++) {
351  os << a[i] << " ";
352  }
353 
354  os << "]\n";
355  return os;
356 }
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
value_type two_norm_squared() const
Compute the squared two-norm of expansion.
void resize(ordinal_type sz)
Resize coefficient array (coefficients are preserved)
value_type evaluate(const Teuchos::Array< value_type > &point) const
Evaluate polynomial approximation at a point.
Teuchos::RCP< const Stokhos::OrthogPolyBasis< ordinal_type, value_type > > basis_
Basis expansion is relative to.
OrthogPolyApprox & operator=(const OrthogPolyApprox &x)
Assignment operator (deep copy)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
ordinal_type size() const
Size.
void init(const value_type &v)
Initialize coefficients to value.
OrthogPolyApprox(const Teuchos::RCP< const Stokhos::OrthogPolyBasis< ordinal_type, value_type > > &basis=Teuchos::null, ordinal_type sz=0, const value_type *vals=NULL)
Constructor with supplied size sz.
pointer coeff()
Return coefficient array.
value_type two_norm() const
Compute the two-norm of expansion.
value_type inner_product(const OrthogPolyApprox &b) const
Compute the L2 inner product of 2 PCEs.
value_type standard_deviation() const
Compute standard deviation of expansion.
Abstract base class for multivariate orthogonal polynomials.
reference operator[](ordinal_type i)
Array access.
storage_type::reference reference
std::ostream & operator<<(std::ostream &os, const ProductContainer< coeff_type > &vec)
storage_type coeff_
OrthogPolyApprox coefficients.
void reset(const Teuchos::RCP< const Stokhos::OrthogPolyBasis< ordinal_type, value_type > > &new_basis, ordinal_type sz=0)
Reset to a new basis.
Teuchos::RCP< const Stokhos::OrthogPolyBasis< ordinal_type, value_type > > basis() const
Return basis.
Abstract base class for multivariate orthogonal polynomials generated from tensor products of univari...
void load(value_type *v)
Load coefficients to an array of values.
value_type mean() const
Compute mean of expansion.
storage_type::const_reference const_reference
storage_type::const_pointer const_pointer
Class to store coefficients of a projection onto an orthogonal polynomial basis.
expr val()
void init(const_reference v)
Initialize values to a constant value.
std::ostream & print(std::ostream &os) const
Print approximation in basis.
ordinal_type size() const
Return size.
void resize(const ordinal_type &sz)
Resize to new size (values are preserved)
const MultiIndex< ordinal_type > & order(ordinal_type term) const
Get orders for a given term.
ordinal_type size() const
Return size.
reference term(ordinal_type dimension, ordinal_type order)
Get coefficient term for given dimension and order.