10 #ifndef STOKHOS_UNIT_TEST_HELPERS_HPP
11 #define STOKHOS_UNIT_TEST_HELPERS_HPP
21 template<
class OrdinalType,
class ValueType>
23 const std::string& a1_name,
25 const std::string& a2_name,
26 const ValueType& rel_tol,
const ValueType& abs_tol,
31 out <<
"Comparing " << a1_name <<
" == " << a2_name <<
" ... ";
33 const OrdinalType
n = a1.
size();
37 out <<
"\nError, "<<a1_name<<
".size() = "<<a1.
size()<<
" == "
38 << a2_name<<
".size() = "<<a2.
size()<<
" : failed!\n";
43 for( OrdinalType i = 0; i < n; ++i ) {
45 ValueType err =
std::abs(a1[i] - a2[i]) / nrm;
50 <<
"\nError, relErr("<<a1_name<<
"["<<i<<
"],"
51 <<a2_name<<
"["<<i<<
"]) = relErr("<<a1[i]<<
","<<a2[i]<<
") = "
52 <<err<<
" <= tol = "<<tol<<
": failed!\n";
61 << a1_name <<
" = " << a1 << std::endl
62 << a2_name <<
" = " << a2 << std::endl;
68 template<
class ValueType>
70 const std::string& a1_name,
72 const std::string& a2_name,
73 const ValueType& rel_tol,
const ValueType& abs_tol,
81 out <<
"\nError, relErr(" << a1_name <<
","
82 << a2_name <<
") = relErr(" << a1 <<
"," << a2 <<
") = "
83 << err <<
" <= tol = " << tol <<
": failed!\n";
90 template<
class ValueType,
class VectorType1,
class VectorType2>
92 const std::string& a1_name,
94 const std::string& a2_name,
95 const ValueType& rel_tol,
const ValueType& abs_tol,
100 static_assert(std::is_same<value_t_1,value_t_2>::value,
"Inconsistent types.");
104 out <<
"Comparing " << a1_name <<
" == " << a2_name <<
" ... ";
106 const int n = a1.size();
109 if (a2.size() != n) {
110 out <<
"\nError, "<<a1_name<<
".size() = "<<a1.size()<<
" == "
111 << a2_name<<
".size() = "<<a2.size()<<
" : failed!\n";
116 for(
int i = 0; i < n; ++i ) {
117 ValueType err =
abs(a1.coeff(i) - a2.coeff(i));
119 abs_tol + rel_tol*
std::max(
abs(a1.fastAccessCoeff(i)),
120 abs(a2.fastAccessCoeff(i)));
121 if ( (err > tol) && (err != 0.) ) {
123 <<
"\nError, relErr("<<a1_name<<
"["<<i<<
"],"
124 <<a2_name<<
"["<<i<<
"]) = relErr("<<a1.coeff(i)<<
","<<a2.coeff(i)
125 <<
") = "<<err<<
" <= tol = "<<tol<<
": failed!\n";
134 << a1_name <<
" = " << a1 << std::endl
135 << a2_name <<
" = " << a2 << std::endl;
141 template<
class Array1,
class Array2,
class ValueType>
143 const Array2& a2,
const std::string& a2_name,
144 const ValueType& rel_tol,
145 const ValueType& abs_tol,
151 out <<
"Comparing " << a1_name <<
" == " << a2_name <<
" ... ";
153 const int n = a1.size();
156 if (as<int>(a2.size()) != n) {
157 out <<
"\nError, "<<a1_name<<
".size() = "<<a1.size()<<
" == "
158 << a2_name<<
".size() = "<<a2.size()<<
" : failed!\n";
163 for(
int i = 0; i < n; ++i ) {
164 ValueType err =
std::abs(a1[i] - a2[i]);
168 out <<
"\nError, relErr(" << a1_name <<
"[" << i <<
"]," << a2_name
169 <<
"[" << i <<
"]) = relErr(" << a1[i] <<
"," <<a2[i] <<
") = "
170 << err <<
" <= tol = " << tol <<
": failed!\n";
181 template<
class ordinal_type,
class scalar_type>
184 const std::string& a1_name,
186 const std::string& a2_name,
194 out <<
"Comparing " << a1_name <<
" == " << a2_name <<
" ... ";
201 out <<
"\nError, "<<a1_name<<
".numRows() = "<<a1.
numRows()<<
" == "
202 << a2_name<<
".numRows() = "<<a2.
numRows()<<
" : failed!\n";
208 out <<
"\nError, "<<a1_name<<
".numCols() = "<<a1.
numCols()<<
" == "
209 << a2_name<<
".numCols() = "<<a2.
numCols()<<
" : failed!\n";
220 out <<
"\nError, relErr(" << a1_name <<
"(" << i <<
"," <<
j <<
"), "
221 << a2_name <<
"(" << i <<
"," <<
j <<
")) = relErr("
222 << a1(i,
j) <<
", " <<a2(i,
j) <<
") = "
223 << err <<
" <= tol = " << tol <<
": failed!\n";
235 template<
class ordinal_type,
class scalar_type>
238 const std::string& cijk1_name,
240 const std::string& cijk2_name,
248 out <<
"Comparing " << cijk1_name <<
" == " << cijk2_name <<
" ... ";
255 for (
typename Cijk_type::k_iterator k_it=Cijk2.
k_begin();
256 k_it!=Cijk2.
k_end(); ++k_it) {
258 for (
typename Cijk_type::kj_iterator j_it = Cijk2.
j_begin(k_it);
259 j_it != Cijk2.
j_end(k_it); ++j_it) {
261 for (
typename Cijk_type::kji_iterator i_it = Cijk2.
i_begin(j_it);
262 i_it != Cijk2.
i_end(j_it); ++i_it) {
267 double tol = abs_tol + c2*rel_tol;
272 <<
"Check: rel_err( C(" << i <<
"," << j <<
"," << k <<
") )"
273 <<
" = " <<
"rel_err( " << c1 <<
", " << c2 <<
" ) = " << err
274 <<
" <= " << tol <<
" : ";
275 if (s) out <<
"Passed.";
280 success = success && s;
288 template<
class ordinal_type,
class scalar_type>
304 Cijk_1d[i] = bases[i]->computeTripleProductTensor();
314 c *= (*Cijk_1d[l])(terms_i[l],terms_j[l],terms_k[l]);
327 rel_tol, abs_tol, out);
332 template <
typename operator_type1,
typename operator_type2,
335 const operator_type2& op2,
341 typedef typename operator_type1::const_set_iterator point_iterator_type;
345 if (!success)
return false;
348 point_iterator_type pi1 = op1.set_begin();
349 point_iterator_type pi2 = op2.set_begin();
350 while (pi1 != op1.set_end() || pi2 != op2.set_end()) {
351 std::stringstream ss1, ss2;
356 pi2->first, ss2.str(),
357 rel_tol, abs_tol, out);
359 std::stringstream ss3, ss4;
360 ss3 << pi1->second.first;
361 ss4 << pi2->second.first;
364 pi2->second.first, ss4.str(),
365 rel_tol, abs_tol, out);
373 template <
typename operator_type1,
typename operator_type2,
374 typename func_type1,
typename func_type2,
377 const operator_type2& op2,
378 const func_type1& func1,
379 const func_type2& func2,
387 typedef typename operator_type1::const_iterator point_iterator_type;
394 ordinal_type point_sz1 = op1.point_size();
395 ordinal_type point_sz2 = op2.point_size();
396 ordinal_type coeff_sz = op1.coeff_size();
400 ordinal_type idx = 0;
401 for (point_iterator_type pi = op1.begin(); pi != op1.end(); ++pi) {
402 f(idx,0) = func1(*pi);
403 f(idx,1) = func2(*pi);
409 for (point_iterator_type pi = op2.begin(); pi != op2.end(); ++pi) {
410 f2(idx,0) = func1(*pi);
411 f2(idx,1) = func2(*pi);
416 if (point_sz1 == point_sz2)
422 op1.transformQP2PCE(1.0, f, x, 0.0);
425 op2.transformQP2PCE(1.0, f2, x2, 0.0);
434 template <
typename operator_type1,
typename operator_type2,
435 typename func_type1,
typename func_type2,
438 const operator_type2& op2,
439 const func_type1& func1,
440 const func_type2& func2,
448 typedef typename operator_type1::const_iterator point_iterator_type;
455 ordinal_type point_sz1 = op1.point_size();
456 ordinal_type point_sz2 = op2.point_size();
457 ordinal_type coeff_sz = op1.coeff_size();
461 ordinal_type idx = 0;
462 for (point_iterator_type pi = op1.begin(); pi != op1.end(); ++pi) {
463 f(0,idx) = func1(*pi);
464 f(1,idx) = func2(*pi);
470 for (point_iterator_type pi = op2.begin(); pi != op2.end(); ++pi) {
471 f2(0,idx) = func1(*pi);
472 f2(1,idx) = func2(*pi);
477 if (point_sz1 == point_sz2)
483 op1.transformQP2PCE(1.0, f, x, 0.0,
true);
486 op2.transformQP2PCE(1.0, f2, x2, 0.0,
true);
495 template <
typename basis_type,
typename operator_type,
typename scalar_type>
497 const operator_type& op,
504 ordinal_type coeff_sz = op.coeff_size();
505 ordinal_type point_sz = op.point_size();
508 for (ordinal_type i=0; i<coeff_sz; ++i)
513 op.transformPCE2QP(1.0, eye, f, 0.0);
517 op.transformQP2PCE(1.0, f, x, 0.0);
520 for (ordinal_type i=0; i<coeff_sz; ++i)
527 out <<
"Discrete orthogonality error = " << x.
normInf() << std::endl;
582 #endif // STOKHOS_UNIT_TEST_HELPERS_HPP
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
bool compareVecs(const VectorType1 &a1, const std::string &a1_name, const VectorType2 &a2, const std::string &a2_name, const ValueType &rel_tol, const ValueType &abs_tol, Teuchos::FancyOStream &out)
k_iterator k_begin() const
Iterator pointing to first k entry.
Data structure storing a sparse 3-tensor C(i,j,k) in a a compressed format.
bool compareArrays(const Array1 &a1, const std::string &a1_name, const Array2 &a2, const std::string &a2_name, const ValueType &rel_tol, const ValueType &abs_tol, Teuchos::FancyOStream &out)
kj_iterator j_begin(const k_iterator &k) const
Iterator pointing to first j entry for given k.
bool comparePCEs(const PCEType &a1, const std::string &a1_name, const Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &a2, const std::string &a2_name, const ValueType &rel_tol, const ValueType &abs_tol, Teuchos::FancyOStream &out)
void fillComplete()
Signal all terms have been added.
bool compareValues(const ValueType &a1, const std::string &a1_name, const ValueType &a2, const std::string &a2_name, const ValueType &rel_tol, const ValueType &abs_tol, Teuchos::FancyOStream &out)
virtual ordinal_type dimension() const =0
Return dimension of basis.
bool compareSparse3Tensor(const Stokhos::Sparse3Tensor< ordinal_type, scalar_type > &Cijk1, const std::string &cijk1_name, const Stokhos::Sparse3Tensor< ordinal_type, scalar_type > &Cijk2, const std::string &cijk2_name, const scalar_type &rel_tol, const scalar_type &abs_tol, Teuchos::FancyOStream &out)
kj_iterator j_end(const k_iterator &k) const
Iterator pointing to last j entry for given k.
bool testSparse3Tensor(const Stokhos::Sparse3Tensor< ordinal_type, scalar_type > &Cijk, const Stokhos::ProductBasis< ordinal_type, scalar_type > &basis, const scalar_type &sparse_tol, const scalar_type &rel_tol, const scalar_type &abs_tol, Teuchos::FancyOStream &out, bool linear=false)
virtual const MultiIndex< ordinal_type > & term(ordinal_type i) const =0
Get orders of each coordinate polynomial given an index i.
bool compareSDM(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &a1, const std::string &a1_name, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &a2, const std::string &a2_name, const scalar_type &rel_tol, const scalar_type &abs_tol, Teuchos::FancyOStream &out)
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
void add_term(ordinal_type i, ordinal_type j, ordinal_type k, const value_type &c)
Add new term for given (i,j,k)
KOKKOS_INLINE_FUNCTION PCE< Storage > max(const typename PCE< Storage >::value_type &a, const PCE< Storage > &b)
Teuchos::RCP< const Stokhos::OrthogPolyBasis< ordinal_type, value_type > > basis() const
Return basis.
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
Abstract base class for multivariate orthogonal polynomials generated from tensor products of univari...
TypeTo as(const TypeFrom &t)
virtual const Teuchos::Array< value_type > & norm_squared() const =0
Return array storing norm-squared of each basis polynomial.
k_iterator k_end() const
Iterator pointing to last k entry.
virtual Teuchos::Array< Teuchos::RCP< const OneDOrthogPolyBasis< ordinal_type, value_type > > > getCoordinateBases() const =0
Return array of coordinate bases.
Legendre polynomial basis.
Stokhos::Sparse3Tensor< int, double > Cijk_type
OrdinalType numCols() const
ScalarTraits< ScalarType >::magnitudeType normInf() const
#define TEUCHOS_TEST_EQUALITY(v1, v2, out, success)
bool testPseudoSpectralDiscreteOrthogonality(const basis_type &basis, const operator_type &op, const scalar_type &rel_tol, const scalar_type &abs_tol, Teuchos::FancyOStream &out)
ScalarType f(const Teuchos::Array< ScalarType > &x, double a, double b)
ordinal_type order() const
Compute total order of index.
ordinal_type size() const
Return size.
value_type getValue(ordinal_type i, ordinal_type j, ordinal_type k) const
Get Cijk value for a given i, j, k indices.
kji_iterator i_begin(const kj_iterator &j) const
Iterator pointing to first i entry for given j and k.
bool testPseudoSpectralPoints(const operator_type1 &op1, const operator_type2 &op2, const scalar_type &rel_tol, const scalar_type &abs_tol, Teuchos::FancyOStream &out)
kji_iterator i_end(const kj_iterator &j) const
Iterator pointing to last i entry for given j and k.
bool testPseudoSpectralApply(const operator_type1 &op1, const operator_type2 &op2, const func_type1 &func1, const func_type2 &func2, const scalar_type &rel_tol, const scalar_type &abs_tol, Teuchos::FancyOStream &out)
virtual ordinal_type size() const =0
Return total size of basis.
bool testPseudoSpectralApplyTrans(const operator_type1 &op1, const operator_type2 &op2, const func_type1 &func1, const func_type2 &func2, const scalar_type &rel_tol, const scalar_type &abs_tol, Teuchos::FancyOStream &out)
ordinal_type num_entries() const
Return number of non-zero entries.
OrdinalType numRows() const