56 namespace ProductBasisUtilsUnitTest {
59 template <
typename OrdinalType,
typename ValueType>
78 template <
typename ordinal_type>
87 template <
typename scalar_type>
90 for (
int i=0; i<x.
size(); i++)
96 template <
typename scalar_type>
99 for (
int i=0; i<x.
size(); i++)
113 out <<
"For n =" << n <<
", k = " << k <<
", n_choose_k = " << v1
114 <<
" != " << v2 << std::endl;
122 out <<
"For n =" << n <<
", k = " << k <<
", n_choose_k = " << v1
123 <<
" != " << v2 << std::endl;
134 typedef index_set_type::multiindex_type multiindex_type;
136 typedef std::set<multiindex_type, less_type> multiindex_set;
137 typedef multiindex_set::iterator iterator;
139 multiindex_set sortedIndexSet(indexSet.begin(), indexSet.end());
142 std::ostream_iterator<multiindex_type> out_iterator(out,
"\n");
143 out << std::endl <<
"Sorted total order index set (dimension = " <<
setup.d
144 <<
", order = " <<
setup.p <<
"):" << std::endl;
145 std::copy(sortedIndexSet.begin(), sortedIndexSet.end(), out_iterator);
148 iterator prev = sortedIndexSet.begin();
149 iterator curr = prev; ++curr;
150 while (curr != sortedIndexSet.end()) {
154 while (i <
setup.d && order_prev == order_curr) {
155 order_prev -= (*prev)[i];
156 order_curr -= (*curr)[i];
159 if (order_prev >= order_curr) {
160 out <<
"previous index " << *prev <<
" and current index "
161 << *curr <<
" are out of order" << std::endl;
174 typedef index_set_type::multiindex_type multiindex_type;
176 typedef std::set<multiindex_type, less_type> multiindex_set;
177 typedef multiindex_set::iterator iterator;
179 multiindex_set sortedIndexSet(indexSet.begin(), indexSet.end());
182 std::ostream_iterator<multiindex_type> out_iterator(out,
"\n");
183 out << std::endl <<
"Sorted lexicographic index set (dimension = "
184 <<
setup.d <<
", order = " <<
setup.p <<
"):" << std::endl;
185 std::copy(sortedIndexSet.begin(), sortedIndexSet.end(), out_iterator);
188 iterator prev = sortedIndexSet.begin();
189 iterator curr = prev; ++curr;
190 while (curr != sortedIndexSet.end()) {
192 while (i <
setup.d && (*prev)[i] == (*curr)[i]) ++i;
193 if (i ==
setup.d || (*prev)[i] >= (*curr)[i]) {
194 out <<
"previous index " << *prev <<
" and current index "
195 << *curr <<
" are out of order" << std::endl;
223 term_type a(2), b(2);
225 a[0] = -0.774597; a[1] = -0.774597;
226 b[0] = -0.774597; b[1] = 0.0;
237 typedef index_set_type::multiindex_type multiindex_type;
238 typedef index_set_type::iterator iterator;
242 out << std::endl <<
"Total order index set (dimension = " <<
setup.d
243 <<
", order = " <<
setup.p <<
"):" << std::endl;
244 std::ostream_iterator<multiindex_type> out_iterator(out,
"\n");
245 std::copy(indexSet.begin(), indexSet.end(), out_iterator);
248 for (iterator i=indexSet.begin(); i!=indexSet.end(); ++i) {
249 if (i->order() < 0 || i->order() >
setup.p) {
250 out <<
"index " << *i <<
" does not lie in total order set! "
259 typedef std::set<multiindex_type, less_type> multiindex_set;
260 multiindex_set sortedIndexSet(indexSet.begin(), indexSet.end());
262 out <<
"sorted index set size = " << sortedIndexSet.size() << std::endl;
263 out <<
"expected index set size = "
265 if (static_cast<ordinal_type>(sortedIndexSet.size()) !=
271 AnisotropicTotalOrderIndexSet ) {
276 typedef index_set_type::multiindex_type multiindex_type;
277 typedef index_set_type::iterator iterator;
278 multiindex_type upper(
setup.d);
281 index_set_type indexSet(
setup.p, upper);
284 out << std::endl <<
"Anisotropic total order index set (dimension = "
285 <<
setup.d <<
", order = " <<
setup.p <<
", and component orders = "
286 << upper <<
"):" << std::endl;
287 std::ostream_iterator<multiindex_type> out_iterator(out,
"\n");
288 std::copy(indexSet.begin(), indexSet.end(), out_iterator);
291 for (iterator i=indexSet.begin(); i!=indexSet.end(); ++i) {
292 if (i->order() < 0 || i->order() >
setup.p || !i->termWiseLEQ(upper)) {
293 out <<
"index " << *i <<
" does not lie in total order set! "
326 typedef std::map<coeff_type, ordinal_type, less_type> basis_set_type;
328 basis_set_type basis_set;
329 basis_map_type basis_map;
331 indexSet, basis_set, basis_map);
342 static_cast<ordinal_type>(basis_map.size()),
345 static_cast<ordinal_type>(terms.
size()),
350 std::ostream_iterator<ordinal_type> out_iterator(out,
" ");
354 out <<
"term " << basis_map[i] <<
" == " << terms[i] <<
" : ";
355 bool is_equal =
true;
357 is_equal = is_equal && terms[i][
j] == basis_map[i][
j];
359 out <<
"passed" << std::endl;
361 out <<
"failed" << std::endl;
381 typedef std::map<coeff_type, ordinal_type, less_type> basis_set_type;
383 basis_set_type basis_set;
384 basis_map_type basis_map;
386 indexSet, basis_set, basis_map);
395 static_cast<ordinal_type>(basis_map.size()),
400 std::ostream_iterator<ordinal_type> out_iterator(out,
" ");
404 out <<
"term " << basis_map[i] <<
" <= " <<
setup.p <<
" : ";
407 is_less = is_less && basis_map[i][
j] <=
setup.p;
409 out <<
"passed" << std::endl;
411 out <<
"failed" << std::endl;
421 template <
typename ordinal_type>
428 template <
typename term_type>
438 template <
typename basis_set_type>
445 template <
typename term_type>
460 index_set_type indexSet(dim, 0, order);
465 typedef std::map<coeff_type, ordinal_type, less_type> basis_set_type;
467 basis_set_type basis_set;
468 basis_map_type basis_map;
470 indexSet, basis_set, basis_map);
482 Stokhos::ProductBasisUtils::computeTripleProductTensor<ordinal_type,value_type>(bases, basis_set, basis_map, pred, pred);
487 basis->computeTripleProductTensor();
494 for (Cijk_type::k_iterator k_it=Cijk2->k_begin();
495 k_it!=Cijk2->k_end(); ++k_it) {
496 int k = Stokhos::index(k_it);
497 for (Cijk_type::kj_iterator j_it = Cijk2->j_begin(k_it);
498 j_it != Cijk2->j_end(k_it); ++j_it) {
499 int j = Stokhos::index(j_it);
500 for (Cijk_type::kji_iterator i_it = Cijk2->i_begin(j_it);
501 i_it != Cijk2->i_end(j_it); ++i_it) {
502 int i = Stokhos::index(i_it);
503 double c = Cijk->getValue(i,j,k);
504 double c2 = Stokhos::value(i_it);
510 <<
"Check: rel_err( C(" << i <<
"," << j <<
"," << k <<
") )"
511 <<
" = " <<
"rel_err( " << c <<
", " << c2 <<
" ) = " << err
512 <<
" <= " << tol <<
" : ";
513 if (s) out <<
"Passed.";
514 else out <<
"Failed!";
517 success = success && s;
532 index_set_type indexSet(dim, 0, order);
537 typedef std::map<coeff_type, ordinal_type, less_type> basis_set_type;
539 basis_set_type basis_set;
540 basis_map_type basis_map;
542 indexSet, basis_set, basis_map);
554 Stokhos::ProductBasisUtils::computeTripleProductTensorNew<ordinal_type,value_type>(bases, basis_set, basis_map, pred, pred,
false);
559 basis->computeTripleProductTensor();
566 for (Cijk_type::k_iterator k_it=Cijk2->k_begin();
567 k_it!=Cijk2->k_end(); ++k_it) {
568 int k = Stokhos::index(k_it);
569 for (Cijk_type::kj_iterator j_it = Cijk2->j_begin(k_it);
570 j_it != Cijk2->j_end(k_it); ++j_it) {
571 int j = Stokhos::index(j_it);
572 for (Cijk_type::kji_iterator i_it = Cijk2->i_begin(j_it);
573 i_it != Cijk2->i_end(j_it); ++i_it) {
574 int i = Stokhos::index(i_it);
575 double c = Cijk->getValue(i,j,k);
576 double c2 = Stokhos::value(i_it);
582 <<
"Check: rel_err( C(" << i <<
"," << j <<
"," << k <<
") )"
583 <<
" = " <<
"rel_err( " << c <<
", " << c2 <<
" ) = " << err
584 <<
" <= " << tol <<
" : ";
585 if (s) out <<
"Passed.";
586 else out <<
"Failed!";
589 success = success && s;
627 for (Cijk_type::k_iterator k_it=Cijk2->k_begin();
628 k_it!=Cijk2->k_end(); ++k_it) {
629 int k = Stokhos::index(k_it);
630 for (Cijk_type::kj_iterator j_it = Cijk2->j_begin(k_it);
631 j_it != Cijk2->j_end(k_it); ++j_it) {
632 int j = Stokhos::index(j_it);
633 for (Cijk_type::kji_iterator i_it = Cijk2->i_begin(j_it);
634 i_it != Cijk2->i_end(j_it); ++i_it) {
635 int i = Stokhos::index(i_it);
636 double c = Cijk->getValue(i,j,k);
637 double c2 = Stokhos::value(i_it);
643 <<
"Check: rel_err( C(" << i <<
"," << j <<
"," << k <<
") )"
644 <<
" = " <<
"rel_err( " << c <<
", " << c2 <<
" ) = " << err
645 <<
" <= " << tol <<
" : ";
646 if (s) out <<
"Passed.";
647 else out <<
"Failed!";
650 success = success && s;
661 typedef index_set_type::multiindex_type multiindex_type;
663 typedef std::set<multiindex_type, less_type> multiindex_set;
664 typedef multiindex_set::iterator iterator;
666 multiindex_set sortedIndexSet(indexSet.begin(), indexSet.end());
670 iterator i = sortedIndexSet.begin();
672 while (i != sortedIndexSet.end()) {
674 out << *i <<
": index = " << idx <<
" mapped index = " << idx_mapping
676 if (idx == idx_mapping)
693 typedef index_set_type::multiindex_type multiindex_type;
695 typedef std::set<multiindex_type, less_type> multiindex_set;
696 typedef multiindex_set::iterator iterator;
698 multiindex_set sortedIndexSet(indexSet.begin(), indexSet.end());
702 iterator i = sortedIndexSet.begin();
704 while (i != sortedIndexSet.end()) {
706 out << *i <<
": index = " << idx <<
" mapped index = " << idx_mapping
708 if (idx == idx_mapping)
727 typedef index_set_type::multiindex_type multiindex_type;
729 typedef std::set<multiindex_type, less_type> multiindex_set;
730 index_set_type indexSet(d, 0, p);
731 multiindex_set sortedIndexSet(indexSet.begin(), indexSet.end());
735 multiindex_type index(d), num_terms(d), orders(d,p);
744 while (dim > 0 && index[dim] > orders[dim]) {
750 orders[i] = orders[i-1] - index[i-1];
752 if (index[dim] > orders[dim])
766 out << index <<
": index = " << idx <<
" mapped index = " << I
A functor for comparing floating-point numbers to some tolerance.
ordinal_type factorial(const ordinal_type &n)
const basis_set_type & basis_set
ordinal_type n_choose_k(const ordinal_type &n, const ordinal_type &k)
Compute bionomial coefficient (n ; k) = n!/( k! (n-k)! )
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...
Container storing a term in a generalized tensor product.
Data structure storing a sparse 3-tensor C(i,j,k) in a a compressed format.
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
TEUCHOS_UNIT_TEST(Stokhos_ProductBasisUtils, NChooseK)
static void buildProductBasis(const index_set_type &index_set, const growth_rule_type &growth_rule, basis_set_type &basis_set, basis_map_type &basis_map)
Generate a product basis from an index set.
A comparison functor implementing a strict weak ordering based total-order ordering, recursive on the dimension.
UnitTestSetup< ordinal_type, value_type > setup
total_order_predicate(ordinal_type dim_, ordinal_type order_)
static int runUnitTestsFromMain(int argc, char *argv[])
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
bool operator()(const term_type &term) const
scalar_type quad_func2(const Teuchos::Array< scalar_type > &x)
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
general_predicate(const basis_set_type &basis_set_)
An anisotropic total order index set.
Multivariate orthogonal polynomial basis generated from a total-order complete-polynomial tensor prod...
KOKKOS_INLINE_FUNCTION PCE< Storage > exp(const PCE< Storage > &a)
Legendre polynomial basis.
Stokhos::Sparse3Tensor< int, double > Cijk_type
scalar_type quad_func1(const Teuchos::Array< scalar_type > &x)
int main(int argc, char **argv)
#define TEUCHOS_TEST_EQUALITY(v1, v2, out, success)
An isotropic total order index set.
A comparison functor implementing a strict weak ordering based lexographic ordering.
KOKKOS_INLINE_FUNCTION PCE< Storage > sin(const PCE< Storage > &a)
bool operator()(const term_type &term) const
ordinal_type lexicographicMapping(const Stokhos::MultiIndex< ordinal_type > &index, ordinal_type max_order)
A tensor product index set.
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< RD, RP...> >::value &&Kokkos::is_view_uq_pce< Kokkos::View< XD, XP...> >::value >::type sum(const Kokkos::View< RD, RP...> &r, const Kokkos::View< XD, XP...> &x)
static ordinal_type compute_terms(ordinal_type p, ordinal_type d, ordinal_type &sz, Teuchos::Array< MultiIndex< ordinal_type > > &terms, Teuchos::Array< ordinal_type > &num_terms)
Compute the 2-D array of basis terms which maps a basis index into the orders for each basis dimensio...
ordinal_type totalOrderMapping(const Stokhos::MultiIndex< ordinal_type > &index)