diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index 25a0545c6..f267aa34d 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -675,8 +675,11 @@ template class MatrixBase typename ei_traits::Scalar minCoeff() const; typename ei_traits::Scalar maxCoeff() const; - typename ei_traits::Scalar minCoeff(int* row, int* col = 0) const; - typename ei_traits::Scalar maxCoeff(int* row, int* col = 0) const; + typename ei_traits::Scalar minCoeff(int* row, int* col) const; + typename ei_traits::Scalar maxCoeff(int* row, int* col) const; + + typename ei_traits::Scalar minCoeff(int* index) const; + typename ei_traits::Scalar maxCoeff(int* index) const; template typename ei_result_of::Scalar)>::type diff --git a/Eigen/src/Core/Visitor.h b/Eigen/src/Core/Visitor.h index 598c2db8d..590efc766 100644 --- a/Eigen/src/Core/Visitor.h +++ b/Eigen/src/Core/Visitor.h @@ -164,7 +164,7 @@ struct ei_functor_traits > { /** \returns the minimum of all coefficients of *this * and puts in *row and *col its location. * - * \sa MatrixBase::maxCoeff(int*,int*), MatrixBase::visitor(), MatrixBase::minCoeff() + * \sa MatrixBase::minCoeff(int*), MatrixBase::maxCoeff(int*,int*), MatrixBase::visitor(), MatrixBase::minCoeff() */ template typename ei_traits::Scalar @@ -177,6 +177,22 @@ MatrixBase::minCoeff(int* row, int* col) const return minVisitor.res; } +/** \returns the minimum of all coefficients of *this + * and puts in *index its location. + * + * \sa MatrixBase::minCoeff(int*,int*), MatrixBase::maxCoeff(int*,int*), MatrixBase::visitor(), MatrixBase::minCoeff() + */ +template +typename ei_traits::Scalar +MatrixBase::minCoeff(int* index) const +{ + EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) + ei_min_coeff_visitor minVisitor; + this->visit(minVisitor); + *index = (RowsAtCompileTime==1) ? minVisitor.col : minVisitor.row; + return minVisitor.res; +} + /** \returns the maximum of all coefficients of *this * and puts in *row and *col its location. * @@ -193,5 +209,20 @@ MatrixBase::maxCoeff(int* row, int* col) const return maxVisitor.res; } +/** \returns the maximum of all coefficients of *this + * and puts in *index its location. + * + * \sa MatrixBase::maxCoeff(int*,int*), MatrixBase::minCoeff(int*,int*), MatrixBase::visitor(), MatrixBase::maxCoeff() + */ +template +typename ei_traits::Scalar +MatrixBase::maxCoeff(int* index) const +{ + EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) + ei_max_coeff_visitor maxVisitor; + this->visit(maxVisitor); + *index = (RowsAtCompileTime==1) ? maxVisitor.col : maxVisitor.row; + return maxVisitor.res; +} #endif // EIGEN_VISITOR_H diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4e279ea47..f3c15612f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -94,6 +94,7 @@ ei_add_test(basicstuff) ei_add_test(linearstructure) ei_add_test(cwiseop) ei_add_test(redux) +ei_add_test(visitor) ei_add_test(product_small) ei_add_test(product_large ${EI_OFLAG}) ei_add_test(product_extra ${EI_OFLAG}) diff --git a/test/redux.cpp b/test/redux.cpp index 2a0dc97f1..b929fdc0e 100644 --- a/test/redux.cpp +++ b/test/redux.cpp @@ -120,7 +120,7 @@ void test_redux() CALL_SUBTEST( matrixRedux(MatrixXi(8, 12)) ); } for(int i = 0; i < g_repeat; i++) { - CALL_SUBTEST( vectorRedux(VectorXf(5)) ); + CALL_SUBTEST( vectorRedux(VectorX4f()) ); CALL_SUBTEST( vectorRedux(VectorXd(10)) ); CALL_SUBTEST( vectorRedux(VectorXf(33)) ); } diff --git a/test/visitor.cpp b/test/visitor.cpp new file mode 100644 index 000000000..b78782b78 --- /dev/null +++ b/test/visitor.cpp @@ -0,0 +1,131 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2008 Benoit Jacob +// +// 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 . + +#include "main.h" + +template void matrixVisitor(const MatrixType& p) +{ + typedef typename MatrixType::Scalar Scalar; + + int rows = p.rows(); + int cols = p.cols(); + + // construct a random matrix where all coefficients are different + MatrixType m; + m = MatrixType::Random(rows, cols); + for(int i = 0; i < m.size(); i++) + for(int i2 = 0; i2 < i; i2++) + while(m(i) == m(i2)) // yes, == + m(i) = ei_random(); + + Scalar minc = Scalar(1000), maxc = Scalar(-1000); + int minrow,mincol,maxrow,maxcol; + for(int j = 0; j < cols; j++) + for(int i = 0; i < rows; i++) + { + if(m(i,j) < minc) + { + minc = m(i,j); + minrow = i; + mincol = j; + } + if(m(i,j) > maxc) + { + maxc = m(i,j); + maxrow = i; + maxcol = j; + } + } + int eigen_minrow, eigen_mincol, eigen_maxrow, eigen_maxcol; + Scalar eigen_minc, eigen_maxc; + eigen_minc = m.minCoeff(&eigen_minrow,&eigen_mincol); + eigen_maxc = m.maxCoeff(&eigen_maxrow,&eigen_maxcol); + VERIFY(minrow == eigen_minrow); + VERIFY(maxrow == eigen_maxrow); + VERIFY(mincol == eigen_mincol); + VERIFY(maxcol == eigen_maxcol); + VERIFY_IS_APPROX(minc, eigen_minc); + VERIFY_IS_APPROX(maxc, eigen_maxc); + VERIFY_IS_APPROX(minc, m.minCoeff()); + VERIFY_IS_APPROX(maxc, m.maxCoeff()); +} + +template void vectorVisitor(const VectorType& w) +{ + typedef typename VectorType::Scalar Scalar; + + int size = w.size(); + + // construct a random vector where all coefficients are different + VectorType v; + v = VectorType::Random(size); + for(int i = 0; i < size; i++) + for(int i2 = 0; i2 < i; i2++) + while(v(i) == v(i2)) // yes, == + v(i) = ei_random(); + + Scalar minc = Scalar(1000), maxc = Scalar(-1000); + int minidx,maxidx; + for(int i = 0; i < size; i++) + { + if(v(i) < minc) + { + minc = v(i); + minidx = i; + } + if(v(i) > maxc) + { + maxc = v(i); + maxidx = i; + } + } + int eigen_minidx, eigen_maxidx; + Scalar eigen_minc, eigen_maxc; + eigen_minc = v.minCoeff(&eigen_minidx); + eigen_maxc = v.maxCoeff(&eigen_maxidx); + VERIFY(minidx == eigen_minidx); + VERIFY(maxidx == eigen_maxidx); + VERIFY_IS_APPROX(minc, eigen_minc); + VERIFY_IS_APPROX(maxc, eigen_maxc); + VERIFY_IS_APPROX(minc, v.minCoeff()); + VERIFY_IS_APPROX(maxc, v.maxCoeff()); +} + +void test_visitor() +{ + for(int i = 0; i < g_repeat; i++) { + CALL_SUBTEST( matrixVisitor(Matrix()) ); + CALL_SUBTEST( matrixVisitor(Matrix2f()) ); + CALL_SUBTEST( matrixVisitor(Matrix4d()) ); + CALL_SUBTEST( matrixVisitor(MatrixXd(8, 12)) ); + CALL_SUBTEST( matrixVisitor(Matrix(20, 20)) ); + CALL_SUBTEST( matrixVisitor(MatrixXi(8, 12)) ); + } + for(int i = 0; i < g_repeat; i++) { + CALL_SUBTEST( vectorVisitor(Vector4f()) ); + CALL_SUBTEST( vectorVisitor(VectorXd(10)) ); + CALL_SUBTEST( vectorVisitor(RowVectorXd(10)) ); + CALL_SUBTEST( vectorVisitor(VectorXf(33)) ); + } +}