Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_TotalOrderBasis.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 STOKHOS_TOTAL_ORDER_BASIS_HPP
11 #define STOKHOS_TOTAL_ORDER_BASIS_HPP
12 
13 #include "Teuchos_RCP.hpp"
14 
15 #include "Stokhos_ProductBasis.hpp"
18 
19 namespace Stokhos {
20 
33  template <typename ordinal_type, typename value_type,
34  typename coeff_compare_type =
35  TotalOrderLess<MultiIndex<ordinal_type> > >
36  class TotalOrderBasis :
37  public ProductBasis<ordinal_type,value_type> {
38  public:
39 
41 
48  value_type> > >& bases,
49  const value_type& sparse_tol = 1.0e-12,
50  const coeff_compare_type& coeff_compare = coeff_compare_type());
51 
53  virtual ~TotalOrderBasis();
54 
56 
57 
59  ordinal_type order() const;
60 
62  ordinal_type dimension() const;
63 
65  virtual ordinal_type size() const;
66 
68 
72  virtual const Teuchos::Array<value_type>& norm_squared() const;
73 
75  virtual const value_type& norm_squared(ordinal_type i) const;
76 
78 
84  virtual
87 
89  virtual
92 
94  virtual value_type evaluateZero(ordinal_type i) const;
95 
97 
101  virtual void evaluateBases(
103  Teuchos::Array<value_type>& basis_vals) const;
104 
106  virtual void print(std::ostream& os) const;
107 
109  virtual const std::string& getName() const;
110 
112 
114 
115 
117 
122  virtual const MultiIndex<ordinal_type>& term(ordinal_type i) const;
123 
125 
129  virtual ordinal_type index(const MultiIndex<ordinal_type>& term) const;
130 
132 
136  value_type> > >
137  getCoordinateBases() const;
138 
140  virtual MultiIndex<ordinal_type> getMaxOrders() const;
141 
143 
144  private:
145 
146  // Prohibit copying
148 
149  // Prohibit Assignment
151 
152  protected:
153 
155  typedef std::map<coeff_type,ordinal_type,coeff_compare_type> coeff_set_type;
157 
159  std::string name;
160 
163 
166 
169 
172 
175 
178 
181 
184 
187 
190 
191  }; // class TotalOrderBasis
192 
193  // An approach for building a sparse 3-tensor only for lexicographically
194  // ordered total order basis
195  // To-do:
196  // * Remove the n_choose_k() calls
197  // * Remove the loops in the Cijk_1D_Iterator::increment() functions
198  // * Store the 1-D Cijk tensors in a compressed format and eliminate
199  // the implicit searches with getValue()
200  // * Instead of looping over (i,j,k) multi-indices we could just store
201  // the 1-D Cijk tensors as an array of (i,j,k,c) tuples.
202  template <typename ordinal_type,
203  typename value_type>
207  bool symmetric = false) {
208 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
209  TEUCHOS_FUNC_TIME_MONITOR("Stokhos: Total Triple-Product Tensor Time");
210 #endif
211  using Teuchos::RCP;
212  using Teuchos::rcp;
213  using Teuchos::Array;
214 
215  typedef MultiIndex<ordinal_type> coeff_type;
216  const Array< RCP<const OneDOrthogPolyBasis<ordinal_type, value_type> > >& bases = product_basis.getCoordinateBases();
217  ordinal_type d = bases.size();
218  //ordinal_type p = product_basis.order();
219  Array<ordinal_type> basis_orders(d);
220  for (int i=0; i<d; ++i)
221  basis_orders[i] = bases[i]->order();
222 
223  // Create 1-D triple products
225  for (ordinal_type i=0; i<d; i++) {
226  Cijk_1d[i] =
227  bases[i]->computeSparseTripleProductTensor(bases[i]->order()+1);
228  }
229 
232 
233  // Create i, j, k iterators for each dimension
235  Array<Cijk_Iterator> Cijk_1d_iterators(d);
236  coeff_type terms_i(d,0), terms_j(d,0), terms_k(d,0);
237  Array<ordinal_type> sum_i(d,0), sum_j(d,0), sum_k(d,0);
238  for (ordinal_type dim=0; dim<d; dim++) {
239  Cijk_1d_iterators[dim] = Cijk_Iterator(bases[dim]->order(), symmetric);
240  }
241 
242  ordinal_type I = 0;
243  ordinal_type J = 0;
244  ordinal_type K = 0;
245  ordinal_type cnt = 0;
246  bool stop = false;
247  while (!stop) {
248 
249  // Fill out terms from 1-D iterators
250  for (ordinal_type dim=0; dim<d; ++dim) {
251  terms_i[dim] = Cijk_1d_iterators[dim].i;
252  terms_j[dim] = Cijk_1d_iterators[dim].j;
253  terms_k[dim] = Cijk_1d_iterators[dim].k;
254  }
255 
256  // Compute global I,J,K
257  /*
258  ordinal_type II = lexicographicMapping(terms_i, p);
259  ordinal_type JJ = lexicographicMapping(terms_j, p);
260  ordinal_type KK = lexicographicMapping(terms_k, p);
261  if (I != II || J != JJ || K != KK) {
262  std::cout << "DIFF!!!" << std::endl;
263  std::cout << terms_i << ": I = " << I << ", II = " << II << std::endl;
264  std::cout << terms_j << ": J = " << J << ", JJ = " << JJ << std::endl;
265  std::cout << terms_k << ": K = " << K << ", KK = " << KK << std::endl;
266  }
267  */
268 
269  // Compute triple-product value
270  value_type c = value_type(1.0);
271  for (ordinal_type dim=0; dim<d; dim++) {
272  c *= Cijk_1d[dim]->getValue(Cijk_1d_iterators[dim].i,
273  Cijk_1d_iterators[dim].j,
274  Cijk_1d_iterators[dim].k);
275  }
276 
278  std::abs(c) <= 1.0e-12,
279  std::logic_error,
280  "Got 0 triple product value " << c
281  << ", I = " << I << " = " << terms_i
282  << ", J = " << J << " = " << terms_j
283  << ", K = " << K << " = " << terms_k
284  << std::endl);
285 
286  // Add term to global Cijk
287  Cijk->add_term(I,J,K,c);
288  // Cijk->add_term(I,K,J,c);
289  // Cijk->add_term(J,I,K,c);
290  // Cijk->add_term(J,K,I,c);
291  // Cijk->add_term(K,I,J,c);
292  // Cijk->add_term(K,J,I,c);
293 
294  // Increment iterators to the next valid term
295  ordinal_type cdim = d-1;
296  bool inc = true;
297  while (inc && cdim >= 0) {
298  ordinal_type delta_i, delta_j, delta_k;
299  bool more =
300  Cijk_1d_iterators[cdim].increment(delta_i, delta_j, delta_k);
301 
302  // Update number of terms used for computing global index
303  if (cdim == d-1) {
304  I += delta_i;
305  J += delta_j;
306  K += delta_k;
307  }
308  else {
309  if (delta_i > 0) {
310  for (ordinal_type ii=0; ii<delta_i; ++ii)
311  I +=
313  basis_orders[cdim+1]-sum_i[cdim] -
314  (Cijk_1d_iterators[cdim].i-ii)+d-cdim,
315  d-cdim-1);
316  }
317  else {
318  for (ordinal_type ii=0; ii<-delta_i; ++ii)
319  I -=
321  basis_orders[cdim+1]-sum_i[cdim] -
322  (Cijk_1d_iterators[cdim].i+ii)+d-cdim-1,
323  d-cdim-1);
324  }
325 
326  if (delta_j > 0) {
327  for (ordinal_type jj=0; jj<delta_j; ++jj)
328  J +=
330  basis_orders[cdim+1]-sum_j[cdim] -
331  (Cijk_1d_iterators[cdim].j-jj)+d-cdim,
332  d-cdim-1);
333  }
334  else {
335  for (ordinal_type jj=0; jj<-delta_j; ++jj)
336  J -=
338  basis_orders[cdim+1]-sum_j[cdim] -
339  (Cijk_1d_iterators[cdim].j+jj)+d-cdim-1,
340  d-cdim-1);
341  }
342 
343  if (delta_k > 0) {
344  for (ordinal_type kk=0; kk<delta_k; ++kk)
345  K +=
347  basis_orders[cdim+1]-sum_k[cdim] -
348  (Cijk_1d_iterators[cdim].k-kk)+d-cdim,
349  d-cdim-1);
350  }
351  else {
352  for (ordinal_type kk=0; kk<-delta_k; ++kk)
353  K -=
355  basis_orders[cdim+1]-sum_k[cdim] -
356  (Cijk_1d_iterators[cdim].k+kk)+d-cdim-1,
357  d-cdim-1);
358  }
359  }
360 
361  if (!more) {
362  // If no more terms in this dimension, go to previous one
363  --cdim;
364  }
365  else {
366  // cdim has more terms, so reset iterators for all dimensions > cdim
367  // adjusting max order based on sum of i,j,k for previous dims
368  inc = false;
369 
370  for (ordinal_type dim=cdim+1; dim<d; ++dim) {
371 
372  // Update sums of orders for previous dimension
373  sum_i[dim] = sum_i[dim-1] + Cijk_1d_iterators[dim-1].i;
374  sum_j[dim] = sum_j[dim-1] + Cijk_1d_iterators[dim-1].j;
375  sum_k[dim] = sum_k[dim-1] + Cijk_1d_iterators[dim-1].k;
376 
377  // Reset iterator for this dimension
378  Cijk_1d_iterators[dim] =
379  Cijk_Iterator(basis_orders[dim]-sum_i[dim],
380  basis_orders[dim]-sum_j[dim],
381  basis_orders[dim]-sum_k[dim],
382  symmetric);
383  }
384  }
385  }
386 
387  if (cdim < 0)
388  stop = true;
389 
390  cnt++;
391  }
392 
393  Cijk->fillComplete();
394 
395  return Cijk;
396  }
397 
398 } // Namespace Stokhos
399 
400 // Include template definitions
402 
403 #endif
Teuchos::Array< value_type > norms
Norms.
coeff_map_type basis_map
Basis map.
virtual ordinal_type size() const
Return total size of basis.
#define TEUCHOS_FUNC_TIME_MONITOR(FUNCNAME)
virtual void evaluateBases(const Teuchos::ArrayView< const value_type > &point, Teuchos::Array< value_type > &basis_vals) const
Evaluate basis polynomials at given point point.
Teuchos::Array< Teuchos::Array< value_type > > basis_eval_tmp
Temporary array used in basis evaluation.
ordinal_type n_choose_k(const ordinal_type &n, const ordinal_type &k)
Compute bionomial coefficient (n ; k) = n!/( k! (n-k)! )
virtual const MultiIndex< ordinal_type > & term(ordinal_type i) const
Get orders of each coordinate polynomial given an index i.
Teuchos::RCP< Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensorLTO(const TotalOrderBasis< ordinal_type, value_type, LexographicLess< MultiIndex< ordinal_type > > > &product_basis, bool symmetric=false)
Multivariate orthogonal polynomial basis generated from a total order tensor product of univariate po...
Data structure storing a sparse 3-tensor C(i,j,k) in a a compressed format.
ordinal_type sz
Total size of basis.
ordinal_type dimension() const
Return dimension of basis.
TotalOrderBasis & operator=(const TotalOrderBasis &b)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
coeff_type max_orders
Maximum orders for each dimension.
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeLinearTripleProductTensor() const
Compute linear triple product tensor where k = 0,1,..,d.
TotalOrderBasis(const Teuchos::Array< Teuchos::RCP< const OneDOrthogPolyBasis< ordinal_type, value_type > > > &bases, const value_type &sparse_tol=1.0e-12, const coeff_compare_type &coeff_compare=coeff_compare_type())
Constructor.
virtual value_type evaluateZero(ordinal_type i) const
Evaluate basis polynomial i at zero.
virtual MultiIndex< ordinal_type > getMaxOrders() const
Return maximum order allowable for each coordinate basis.
ordinal_type d
Total dimension of basis.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MultiIndex< ordinal_type > coeff_type
value_type sparse_tol
Tolerance for computing sparse Cijk.
Teuchos::Array< coeff_type > coeff_map_type
coeff_set_type basis_set
Basis set.
std::string name
Name of basis.
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
Abstract base class for multivariate orthogonal polynomials generated from tensor products of univari...
std::map< coeff_type, ordinal_type, coeff_compare_type > coeff_set_type
Abstract base class for 1-D orthogonal polynomials.
Teuchos::Array< Teuchos::RCP< const OneDOrthogPolyBasis< ordinal_type, value_type > > > bases
Array of bases.
size_type size() const
A comparison functor implementing a strict weak ordering based lexographic ordering.
virtual ordinal_type index(const MultiIndex< ordinal_type > &term) const
Get index of the multivariate polynomial given orders of each coordinate.
ordinal_type order() const
Return order of basis.
virtual void print(std::ostream &os) const
Print basis to stream os.
virtual ~TotalOrderBasis()
Destructor.
virtual const Teuchos::Array< value_type > & norm_squared() const
Return array storing norm-squared of each basis polynomial.
Teuchos::Array< Teuchos::RCP< const OneDOrthogPolyBasis< ordinal_type, value_type > > > getCoordinateBases() const
Return coordinate bases.
virtual const std::string & getName() const
Return string name of basis.
ordinal_type p
Total order of basis.