bool compareVecs(const VectorType& a1, const std::string& a1_name, const VectorType&a2, const std::string& a2_name, const ValueType& rel_tol, const ValueType& abs_tol, Teuchos::FancyOStream& out) { bool success = true; out << "Comparing " << a1_name << " == " << a2_name << " ... "; const int n = a1.size(); // Compare sizes if (a2.size() != n) { out << "\nError, "<<a1_name<<".size() = "<<a1.size()<<" == " << a2_name<<".size() = "<<a2.size()<<" : failed!\n"; return false; } // Compare elements for( int i = 0; i < n; ++i ) { ValueType err = std::abs(a1.coeff(i) - a2.coeff(i)); ValueType tol = abs_tol + rel_tol*std::max(std::abs(a1.fastAccessCoeff(i)), std::abs(a2.fastAccessCoeff(i))); if (err > tol) { out <<"\nError, relErr("<<a1_name<<"["<<i<<"]," <<a2_name<<"["<<i<<"]) = relErr("<<a1.coeff(i)<<","<<a2.coeff(i) <<") = "<<err<<" <= tol = "<<tol<<": failed!\n"; success = false; } } if (success) { out << "passed\n"; } else { out << std::endl << a1_name << " = " << a1 << std::endl << a2_name << " = " << a2 << std::endl; } return success; }
template<typename VectorType> void vectorRedux(const VectorType& w) { using std::abs; typedef typename VectorType::Index Index; typedef typename VectorType::Scalar Scalar; typedef typename NumTraits<Scalar>::Real RealScalar; Index size = w.size(); VectorType v = VectorType::Random(size); VectorType v_for_prod = VectorType::Ones(size) + Scalar(0.2) * v; // see comment above declaration of m1_for_prod for(int i = 1; i < size; i++) { Scalar s(0), p(1); RealScalar minc(numext::real(v.coeff(0))), maxc(numext::real(v.coeff(0))); for(int j = 0; j < i; j++) { s += v[j]; p *= v_for_prod[j]; minc = (std::min)(minc, numext::real(v[j])); maxc = (std::max)(maxc, numext::real(v[j])); } VERIFY_IS_MUCH_SMALLER_THAN(abs(s - v.head(i).sum()), Scalar(1)); VERIFY_IS_APPROX(p, v_for_prod.head(i).prod()); VERIFY_IS_APPROX(minc, v.real().head(i).minCoeff()); VERIFY_IS_APPROX(maxc, v.real().head(i).maxCoeff()); } for(int i = 0; i < size-1; i++) { Scalar s(0), p(1); RealScalar minc(numext::real(v.coeff(i))), maxc(numext::real(v.coeff(i))); for(int j = i; j < size; j++) { s += v[j]; p *= v_for_prod[j]; minc = (std::min)(minc, numext::real(v[j])); maxc = (std::max)(maxc, numext::real(v[j])); } VERIFY_IS_MUCH_SMALLER_THAN(abs(s - v.tail(size-i).sum()), Scalar(1)); VERIFY_IS_APPROX(p, v_for_prod.tail(size-i).prod()); VERIFY_IS_APPROX(minc, v.real().tail(size-i).minCoeff()); VERIFY_IS_APPROX(maxc, v.real().tail(size-i).maxCoeff()); } for(int i = 0; i < size/2; i++) { Scalar s(0), p(1); RealScalar minc(numext::real(v.coeff(i))), maxc(numext::real(v.coeff(i))); for(int j = i; j < size-i; j++) { s += v[j]; p *= v_for_prod[j]; minc = (std::min)(minc, numext::real(v[j])); maxc = (std::max)(maxc, numext::real(v[j])); } VERIFY_IS_MUCH_SMALLER_THAN(abs(s - v.segment(i, size-2*i).sum()), Scalar(1)); VERIFY_IS_APPROX(p, v_for_prod.segment(i, size-2*i).prod()); VERIFY_IS_APPROX(minc, v.real().segment(i, size-2*i).minCoeff()); VERIFY_IS_APPROX(maxc, v.real().segment(i, size-2*i).maxCoeff()); } // test empty objects VERIFY_IS_APPROX(v.head(0).sum(), Scalar(0)); VERIFY_IS_APPROX(v.tail(0).prod(), Scalar(1)); VERIFY_RAISES_ASSERT(v.head(0).mean()); VERIFY_RAISES_ASSERT(v.head(0).minCoeff()); VERIFY_RAISES_ASSERT(v.head(0).maxCoeff()); }