From 703539110b5c26f01ac37fe6114afdbb5c3c6c99 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Tue, 9 Sep 2008 09:30:23 +0000 Subject: add the missing templated version of block for sub-vectors --- Eigen/Geometry | 7 +----- Eigen/src/Array/AllAndAny.h | 4 ++-- Eigen/src/Core/Block.h | 38 ++++++++++++++++++++++++++++++++ Eigen/src/Core/MatrixBase.h | 3 +++ doc/QuickStartGuide.dox | 2 ++ doc/snippets/MatrixBase_template_int.cpp | 5 +++++ test/submatrices.cpp | 16 ++++++++++++++ 7 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 doc/snippets/MatrixBase_template_int.cpp diff --git a/Eigen/Geometry b/Eigen/Geometry index 1ac279b99..2a2dfda2b 100644 --- a/Eigen/Geometry +++ b/Eigen/Geometry @@ -1,7 +1,7 @@ #ifndef EIGEN_GEOMETRY_MODULE_H #define EIGEN_GEOMETRY_MODULE_H -#include "Core" +#include "Array" #ifndef M_PI #define M_PI 3.14159265358979323846 @@ -23,11 +23,6 @@ namespace Eigen { * \endcode */ -// the Geometry module use cwiseCos and cwiseSin which are defined in the Array module -#include "src/Array/CwiseOperators.h" -#include "src/Array/Functors.h" -#include "src/Array/PartialRedux.h" - #include "src/Geometry/OrthoMethods.h" #include "src/Geometry/RotationBase.h" #include "src/Geometry/Rotation2D.h" diff --git a/Eigen/src/Array/AllAndAny.h b/Eigen/src/Array/AllAndAny.h index 76cc2db84..cc4560b2f 100644 --- a/Eigen/src/Array/AllAndAny.h +++ b/Eigen/src/Array/AllAndAny.h @@ -89,7 +89,7 @@ struct ei_any_unroller * \sa MatrixBase::any(), Cwise::operator<() */ template -bool MatrixBase::all(void) const +inline bool MatrixBase::all(void) const { const bool unroll = SizeAtCompileTime * (CoeffReadCost + NumTraits::AddCost) <= EIGEN_UNROLLING_LIMIT; @@ -113,7 +113,7 @@ bool MatrixBase::all(void) const * \sa MatrixBase::all() */ template -bool MatrixBase::any(void) const +inline bool MatrixBase::any(void) const { const bool unroll = SizeAtCompileTime * (CoeffReadCost + NumTraits::AddCost) <= EIGEN_UNROLLING_LIMIT; diff --git a/Eigen/src/Core/Block.h b/Eigen/src/Core/Block.h index 18fcdbb2b..71ad6d5f1 100644 --- a/Eigen/src/Core/Block.h +++ b/Eigen/src/Core/Block.h @@ -451,6 +451,44 @@ MatrixBase::end(int size) const ColsAtCompileTime == 1 ? 1 : size); } +/** \returns a fixed-size expression of a sub-vector of \c *this + * + * \only_for_vectors + * + * The template parameter \a Size is the number of coefficients in the block + * + * \param start the index of the first element of the sub-vector + * + * Example: \include MatrixBase_template_int.cpp + * Output: \verbinclude MatrixBase_template_int.out + * + * \sa class Block + */ +template +template +inline typename BlockReturnType::SubVectorType +MatrixBase::block(int start) +{ + EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived); + return Block + (derived(), RowsAtCompileTime == 1 ? 0 : start, + ColsAtCompileTime == 1 ? 0 : start); +} + +/** This is the const version of block(int).*/ +template +template +inline const typename BlockReturnType::SubVectorType +MatrixBase::block(int start) const +{ + EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived); + return Block + (derived(), RowsAtCompileTime == 1 ? 0 : start, + ColsAtCompileTime == 1 ? 0 : start); +} + /** \returns a fixed-size expression of the first coefficients of *this. * * \only_for_vectors diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index a9d15032c..81aae3593 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -381,6 +381,9 @@ template class MatrixBase template typename BlockReturnType::SubVectorType end(); template const typename BlockReturnType::SubVectorType end() const; + template typename BlockReturnType::SubVectorType block(int start); + template const typename BlockReturnType::SubVectorType block(int start) const; + DiagonalCoeffs diagonal(); const DiagonalCoeffs diagonal() const; diff --git a/doc/QuickStartGuide.dox b/doc/QuickStartGuide.dox index 8d1799fdd..d55e07856 100644 --- a/doc/QuickStartGuide.dox +++ b/doc/QuickStartGuide.dox @@ -351,6 +351,8 @@ mat = 2 7 8 \endcode +Also note that maxCoeff and minCoeff can takes optional arguments returning the coordinates of the respective min/max coeff: \link MatrixBase::maxCoeff(int*,int*) const maxCoeff(int* i, int* j) \endlink, \link MatrixBase::minCoeff(int*,int*) const minCoeff(int* i, int* j) \endlink. + \b Side \b note: The all() and any() functions are especially useful in combinaison with coeff-wise comparison operators (\ref CwiseAll "example"). diff --git a/doc/snippets/MatrixBase_template_int.cpp b/doc/snippets/MatrixBase_template_int.cpp new file mode 100644 index 000000000..4041a2214 --- /dev/null +++ b/doc/snippets/MatrixBase_template_int.cpp @@ -0,0 +1,5 @@ +RowVector5i v = RowVector5i::Random(); +cout << "Here is the vector v:" << endl << v << endl; +cout << "Here is v.block<2>(1):" << endl << v.start<2>() << endl; +v.block<2>(2).setZero(); +cout << "Now the vector v is:" << endl << v << endl; diff --git a/test/submatrices.cpp b/test/submatrices.cpp index 74bf8c7af..369930df1 100644 --- a/test/submatrices.cpp +++ b/test/submatrices.cpp @@ -123,6 +123,22 @@ template void submatrices(const MatrixType& m) VERIFY_IS_APPROX(b, m1.block(3,3,BlockRows,BlockCols)); } + if (rows>2) + { + // test sub vectors + VERIFY_IS_APPROX(v1.template start<2>(), v1.block(0,0,2,1)); + VERIFY_IS_APPROX(v1.template start<2>(), v1.start(2)); + VERIFY_IS_APPROX(v1.template start<2>(), v1.block(0,2)); + VERIFY_IS_APPROX(v1.template start<2>(), v1.template block<2>(0)); + int i = rows-2; + VERIFY_IS_APPROX(v1.template end<2>(), v1.block(i,0,2,1)); + VERIFY_IS_APPROX(v1.template end<2>(), v1.end(2)); + VERIFY_IS_APPROX(v1.template end<2>(), v1.block(i,2)); + VERIFY_IS_APPROX(v1.template end<2>(), v1.template block<2>(i)); + i = ei_random(0,rows-2); + VERIFY_IS_APPROX(v1.block(i,2), v1.template block<2>(i)); + } + // stress some basic stuffs with block matrices VERIFY(ones.col(c1).sum() == Scalar(rows)); VERIFY(ones.row(r1).sum() == Scalar(cols)); -- cgit v1.2.3