diff options
author | Gael Guennebaud <g.gael@free.fr> | 2009-02-12 15:18:59 +0000 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2009-02-12 15:18:59 +0000 |
commit | 51c991af459b86aab4a0a9575edba378e20e8621 (patch) | |
tree | 853f08330abde7f20d4eb938c1344b8807923c1a /test | |
parent | dc97d483fd0cf4575d7b7faca86bc45539539f49 (diff) |
* exit Sum.h, exit Prod.h, welcome vectorization of redux() !
* add vectorization for minCoeff and maxCoeff
Diffstat (limited to 'test')
-rw-r--r-- | test/CMakeLists.txt | 3 | ||||
-rw-r--r-- | test/packetmath.cpp | 10 | ||||
-rw-r--r-- | test/prod.cpp | 86 | ||||
-rw-r--r-- | test/redux.cpp | 127 | ||||
-rw-r--r-- | test/sum.cpp | 86 |
5 files changed, 138 insertions, 174 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a20a6a12f..4046cf533 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -111,7 +111,7 @@ ei_add_test(vectorization_logic) ei_add_test(basicstuff) ei_add_test(linearstructure) ei_add_test(cwiseop) -ei_add_test(sum) +ei_add_test(redux) ei_add_test(product_small) ei_add_test(product_large ${EI_OFLAG}) ei_add_test(adjoint) @@ -149,7 +149,6 @@ ei_add_test(sparse_basic) ei_add_test(sparse_product) ei_add_test(sparse_solvers " " "${SPARSE_LIBS}") ei_add_test(reverse) -ei_add_test(prod) # print a summary of the different options message("************************************************************") diff --git a/test/packetmath.cpp b/test/packetmath.cpp index 0397d9b28..f56532f91 100644 --- a/test/packetmath.cpp +++ b/test/packetmath.cpp @@ -129,6 +129,16 @@ template<typename Scalar> void packetmath() for (int i=0; i<PacketSize; ++i) ref[0] *= data1[i]; VERIFY(ei_isApprox(ref[0], ei_predux_mul(ei_pload(data1))) && "ei_predux_mul"); + + ref[0] = data1[0]; + for (int i=0; i<PacketSize; ++i) + ref[0] = std::min(ref[0],data1[i]); + VERIFY(ei_isApprox(ref[0], ei_predux_min(ei_pload(data1))) && "ei_predux_min"); + + ref[0] = data1[0]; + for (int i=0; i<PacketSize; ++i) + ref[0] = std::min(ref[0],data1[i]); + VERIFY(ei_isApprox(ref[0], ei_predux_min(ei_pload(data1))) && "ei_predux_max"); for (int j=0; j<PacketSize; ++j) { diff --git a/test/prod.cpp b/test/prod.cpp deleted file mode 100644 index 12f8c0c48..000000000 --- a/test/prod.cpp +++ /dev/null @@ -1,86 +0,0 @@ -// This file is part of Eigen, a lightweight C++ template library -// for linear algebra. Eigen itself is part of the KDE project. -// -// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com> -// -// Eigen is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 3 of the License, or (at your option) any later version. -// -// Alternatively, you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of -// the License, or (at your option) any later version. -// -// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY -// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License and a copy of the GNU General Public License along with -// Eigen. If not, see <http://www.gnu.org/licenses/>. - -#include "main.h" - -template<typename MatrixType> void matrixProd(const MatrixType& m) -{ - typedef typename MatrixType::Scalar Scalar; - - int rows = m.rows(); - int cols = m.cols(); - - MatrixType m1 = MatrixType::Random(rows, cols); - - VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows, cols).prod(), Scalar(1)); - VERIFY_IS_APPROX(MatrixType::Ones(rows, cols).prod(), Scalar(1)); - Scalar x = Scalar(1); - for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) x *= m1(i,j); - VERIFY_IS_APPROX(m1.prod(), x); -} - -template<typename VectorType> void vectorProd(const VectorType& w) -{ - typedef typename VectorType::Scalar Scalar; - int size = w.size(); - - VectorType v = VectorType::Random(size); - for(int i = 1; i < size; i++) - { - Scalar s = Scalar(1); - for(int j = 0; j < i; j++) s *= v[j]; - VERIFY_IS_APPROX(s, v.start(i).prod()); - } - - for(int i = 0; i < size-1; i++) - { - Scalar s = Scalar(1); - for(int j = i; j < size; j++) s *= v[j]; - VERIFY_IS_APPROX(s, v.end(size-i).prod()); - } - - for(int i = 0; i < size/2; i++) - { - Scalar s = Scalar(1); - for(int j = i; j < size-i; j++) s *= v[j]; - VERIFY_IS_APPROX(s, v.segment(i, size-2*i).prod()); - } -} - -void test_prod() -{ - for(int i = 0; i < g_repeat; i++) { - CALL_SUBTEST( matrixProd(Matrix<float, 1, 1>()) ); - CALL_SUBTEST( matrixProd(Matrix2f()) ); - CALL_SUBTEST( matrixProd(Matrix4d()) ); - CALL_SUBTEST( matrixProd(MatrixXcf(3, 3)) ); - CALL_SUBTEST( matrixProd(MatrixXf(8, 12)) ); - CALL_SUBTEST( matrixProd(MatrixXi(8, 12)) ); - } - for(int i = 0; i < g_repeat; i++) { - CALL_SUBTEST( vectorProd(VectorXf(5)) ); - CALL_SUBTEST( vectorProd(VectorXd(10)) ); - CALL_SUBTEST( vectorProd(VectorXf(33)) ); - } -} diff --git a/test/redux.cpp b/test/redux.cpp new file mode 100644 index 000000000..dee9d4bbd --- /dev/null +++ b/test/redux.cpp @@ -0,0 +1,127 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com> +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see <http://www.gnu.org/licenses/>. + +#include "main.h" + +template<typename MatrixType> void matrixRedux(const MatrixType& m) +{ + typedef typename MatrixType::Scalar Scalar; + + int rows = m.rows(); + int cols = m.cols(); + + MatrixType m1 = MatrixType::Random(rows, cols); + + VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows, cols).sum(), Scalar(1)); + VERIFY_IS_APPROX(MatrixType::Ones(rows, cols).sum(), Scalar(float(rows*cols))); // the float() here to shut up excessive MSVC warning about int->complex conversion being lossy + Scalar s(0), p(1), minc(ei_real(m1.coeff(0))), maxc(ei_real(m1.coeff(0))); + for(int j = 0; j < cols; j++) + for(int i = 0; i < rows; i++) + { + s += m1(i,j); + p *= m1(i,j); + minc = std::min(ei_real(minc), ei_real(m1(i,j))); + maxc = std::max(ei_real(maxc), ei_real(m1(i,j))); + } + VERIFY_IS_APPROX(m1.sum(), s); + VERIFY_IS_APPROX(m1.prod(), p); + VERIFY_IS_APPROX(m1.real().minCoeff(), ei_real(minc)); + VERIFY_IS_APPROX(m1.real().maxCoeff(), ei_real(maxc)); +} + +template<typename VectorType> void vectorRedux(const VectorType& w) +{ + typedef typename VectorType::Scalar Scalar; + typedef typename NumTraits<Scalar>::Real RealScalar; + int size = w.size(); + + VectorType v = VectorType::Random(size); + for(int i = 1; i < size; i++) + { + Scalar s(0), p(1); + RealScalar minc(ei_real(v.coeff(0))), maxc(ei_real(v.coeff(0))); + for(int j = 0; j < i; j++) + { + s += v[j]; + p *= v[j]; + minc = std::min(minc, ei_real(v[j])); + maxc = std::max(maxc, ei_real(v[j])); + } + VERIFY_IS_APPROX(s, v.start(i).sum()); + VERIFY_IS_APPROX(p, v.start(i).prod()); + VERIFY_IS_APPROX(minc, v.real().start(i).minCoeff()); + VERIFY_IS_APPROX(maxc, v.real().start(i).maxCoeff()); + } + + for(int i = 0; i < size-1; i++) + { + Scalar s(0), p(1); + RealScalar minc(ei_real(v.coeff(i))), maxc(ei_real(v.coeff(i))); + for(int j = i; j < size; j++) + { + s += v[j]; + p *= v[j]; + minc = std::min(minc, ei_real(v[j])); + maxc = std::max(maxc, ei_real(v[j])); + } + VERIFY_IS_APPROX(s, v.end(size-i).sum()); + VERIFY_IS_APPROX(p, v.end(size-i).prod()); + VERIFY_IS_APPROX(minc, v.real().end(size-i).minCoeff()); + VERIFY_IS_APPROX(maxc, v.real().end(size-i).maxCoeff()); + } + + for(int i = 0; i < size/2; i++) + { + Scalar s(0), p(1); + RealScalar minc(ei_real(v.coeff(i))), maxc(ei_real(v.coeff(i))); + for(int j = i; j < size-i; j++) + { + s += v[j]; + p *= v[j]; + minc = std::min(minc, ei_real(v[j])); + maxc = std::max(maxc, ei_real(v[j])); + } + VERIFY_IS_APPROX(s, v.segment(i, size-2*i).sum()); + VERIFY_IS_APPROX(p, v.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()); + } +} + +void test_redux() +{ + for(int i = 0; i < g_repeat; i++) { + CALL_SUBTEST( matrixRedux(Matrix<float, 1, 1>()) ); + CALL_SUBTEST( matrixRedux(Matrix2f()) ); + CALL_SUBTEST( matrixRedux(Matrix4d()) ); + CALL_SUBTEST( matrixRedux(MatrixXcf(3, 3)) ); + CALL_SUBTEST( matrixRedux(MatrixXd(8, 12)) ); + CALL_SUBTEST( matrixRedux(MatrixXi(8, 12)) ); + } + for(int i = 0; i < g_repeat; i++) { + CALL_SUBTEST( vectorRedux(VectorXf(5)) ); + CALL_SUBTEST( vectorRedux(VectorXd(10)) ); + CALL_SUBTEST( vectorRedux(VectorXf(33)) ); + } +} diff --git a/test/sum.cpp b/test/sum.cpp deleted file mode 100644 index fe707e9b2..000000000 --- a/test/sum.cpp +++ /dev/null @@ -1,86 +0,0 @@ -// This file is part of Eigen, a lightweight C++ template library -// for linear algebra. Eigen itself is part of the KDE project. -// -// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com> -// -// Eigen is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 3 of the License, or (at your option) any later version. -// -// Alternatively, you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of -// the License, or (at your option) any later version. -// -// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY -// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License and a copy of the GNU General Public License along with -// Eigen. If not, see <http://www.gnu.org/licenses/>. - -#include "main.h" - -template<typename MatrixType> void matrixSum(const MatrixType& m) -{ - typedef typename MatrixType::Scalar Scalar; - - int rows = m.rows(); - int cols = m.cols(); - - MatrixType m1 = MatrixType::Random(rows, cols); - - VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows, cols).sum(), Scalar(1)); - VERIFY_IS_APPROX(MatrixType::Ones(rows, cols).sum(), Scalar(float(rows*cols))); // the float() here to shut up excessive MSVC warning about int->complex conversion being lossy - Scalar x = Scalar(0); - for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) x += m1(i,j); - VERIFY_IS_APPROX(m1.sum(), x); -} - -template<typename VectorType> void vectorSum(const VectorType& w) -{ - typedef typename VectorType::Scalar Scalar; - int size = w.size(); - - VectorType v = VectorType::Random(size); - for(int i = 1; i < size; i++) - { - Scalar s = Scalar(0); - for(int j = 0; j < i; j++) s += v[j]; - VERIFY_IS_APPROX(s, v.start(i).sum()); - } - - for(int i = 0; i < size-1; i++) - { - Scalar s = Scalar(0); - for(int j = i; j < size; j++) s += v[j]; - VERIFY_IS_APPROX(s, v.end(size-i).sum()); - } - - for(int i = 0; i < size/2; i++) - { - Scalar s = Scalar(0); - for(int j = i; j < size-i; j++) s += v[j]; - VERIFY_IS_APPROX(s, v.segment(i, size-2*i).sum()); - } -} - -void test_sum() -{ - for(int i = 0; i < g_repeat; i++) { - CALL_SUBTEST( matrixSum(Matrix<float, 1, 1>()) ); - CALL_SUBTEST( matrixSum(Matrix2f()) ); - CALL_SUBTEST( matrixSum(Matrix4d()) ); - CALL_SUBTEST( matrixSum(MatrixXcf(3, 3)) ); - CALL_SUBTEST( matrixSum(MatrixXf(8, 12)) ); - CALL_SUBTEST( matrixSum(MatrixXi(8, 12)) ); - } - for(int i = 0; i < g_repeat; i++) { - CALL_SUBTEST( vectorSum(VectorXf(5)) ); - CALL_SUBTEST( vectorSum(VectorXd(10)) ); - CALL_SUBTEST( vectorSum(VectorXf(33)) ); - } -} |