| 77 | } |
| 78 | |
| 79 | template<typename VectorType> void vectorRedux(const VectorType& w) |
| 80 | { |
| 81 | using std::abs; |
| 82 | typedef typename VectorType::Index Index; |
| 83 | typedef typename VectorType::Scalar Scalar; |
| 84 | typedef typename NumTraits<Scalar>::Real RealScalar; |
| 85 | Index size = w.size(); |
| 86 | |
| 87 | VectorType v = VectorType::Random(size); |
| 88 | VectorType v_for_prod = VectorType::Ones(size) + Scalar(0.2) * v; // see comment above declaration of m1_for_prod |
| 89 | |
| 90 | for(int i = 1; i < size; i++) |
| 91 | { |
| 92 | Scalar s(0), p(1); |
| 93 | RealScalar minc(numext::real(v.coeff(0))), maxc(numext::real(v.coeff(0))); |
| 94 | for(int j = 0; j < i; j++) |
| 95 | { |
| 96 | s += v[j]; |
| 97 | p *= v_for_prod[j]; |
| 98 | minc = (std::min)(minc, numext::real(v[j])); |
| 99 | maxc = (std::max)(maxc, numext::real(v[j])); |
| 100 | } |
| 101 | VERIFY_IS_MUCH_SMALLER_THAN(abs(s - v.head(i).sum()), Scalar(1)); |
| 102 | VERIFY_IS_APPROX(p, v_for_prod.head(i).prod()); |
| 103 | VERIFY_IS_APPROX(minc, v.real().head(i).minCoeff()); |
| 104 | VERIFY_IS_APPROX(maxc, v.real().head(i).maxCoeff()); |
| 105 | } |
| 106 | |
| 107 | for(int i = 0; i < size-1; i++) |
| 108 | { |
| 109 | Scalar s(0), p(1); |
| 110 | RealScalar minc(numext::real(v.coeff(i))), maxc(numext::real(v.coeff(i))); |
| 111 | for(int j = i; j < size; j++) |
| 112 | { |
| 113 | s += v[j]; |
| 114 | p *= v_for_prod[j]; |
| 115 | minc = (std::min)(minc, numext::real(v[j])); |
| 116 | maxc = (std::max)(maxc, numext::real(v[j])); |
| 117 | } |
| 118 | VERIFY_IS_MUCH_SMALLER_THAN(abs(s - v.tail(size-i).sum()), Scalar(1)); |
| 119 | VERIFY_IS_APPROX(p, v_for_prod.tail(size-i).prod()); |
| 120 | VERIFY_IS_APPROX(minc, v.real().tail(size-i).minCoeff()); |
| 121 | VERIFY_IS_APPROX(maxc, v.real().tail(size-i).maxCoeff()); |
| 122 | } |
| 123 | |
| 124 | for(int i = 0; i < size/2; i++) |
| 125 | { |
| 126 | Scalar s(0), p(1); |
| 127 | RealScalar minc(numext::real(v.coeff(i))), maxc(numext::real(v.coeff(i))); |
| 128 | for(int j = i; j < size-i; j++) |
| 129 | { |
| 130 | s += v[j]; |
| 131 | p *= v_for_prod[j]; |
| 132 | minc = (std::min)(minc, numext::real(v[j])); |
| 133 | maxc = (std::max)(maxc, numext::real(v[j])); |
| 134 | } |
| 135 | VERIFY_IS_MUCH_SMALLER_THAN(abs(s - v.segment(i, size-2*i).sum()), Scalar(1)); |
| 136 | VERIFY_IS_APPROX(p, v_for_prod.segment(i, size-2*i).prod()); |