add the missing templated version of block for sub-vectors

This commit is contained in:
Gael Guennebaud 2008-09-09 09:30:23 +00:00
parent c41ceee750
commit 703539110b
7 changed files with 67 additions and 8 deletions

View File

@ -1,7 +1,7 @@
#ifndef EIGEN_GEOMETRY_MODULE_H #ifndef EIGEN_GEOMETRY_MODULE_H
#define EIGEN_GEOMETRY_MODULE_H #define EIGEN_GEOMETRY_MODULE_H
#include "Core" #include "Array"
#ifndef M_PI #ifndef M_PI
#define M_PI 3.14159265358979323846 #define M_PI 3.14159265358979323846
@ -23,11 +23,6 @@ namespace Eigen {
* \endcode * \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/OrthoMethods.h"
#include "src/Geometry/RotationBase.h" #include "src/Geometry/RotationBase.h"
#include "src/Geometry/Rotation2D.h" #include "src/Geometry/Rotation2D.h"

View File

@ -89,7 +89,7 @@ struct ei_any_unroller<Derived, Dynamic>
* \sa MatrixBase::any(), Cwise::operator<() * \sa MatrixBase::any(), Cwise::operator<()
*/ */
template<typename Derived> template<typename Derived>
bool MatrixBase<Derived>::all(void) const inline bool MatrixBase<Derived>::all(void) const
{ {
const bool unroll = SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost) const bool unroll = SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost)
<= EIGEN_UNROLLING_LIMIT; <= EIGEN_UNROLLING_LIMIT;
@ -113,7 +113,7 @@ bool MatrixBase<Derived>::all(void) const
* \sa MatrixBase::all() * \sa MatrixBase::all()
*/ */
template<typename Derived> template<typename Derived>
bool MatrixBase<Derived>::any(void) const inline bool MatrixBase<Derived>::any(void) const
{ {
const bool unroll = SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost) const bool unroll = SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost)
<= EIGEN_UNROLLING_LIMIT; <= EIGEN_UNROLLING_LIMIT;

View File

@ -451,6 +451,44 @@ MatrixBase<Derived>::end(int size) const
ColsAtCompileTime == 1 ? 1 : size); 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<typename Derived>
template<int Size>
inline typename BlockReturnType<Derived,Size>::SubVectorType
MatrixBase<Derived>::block(int start)
{
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
return Block<Derived, (RowsAtCompileTime == 1 ? 1 : Size),
(ColsAtCompileTime == 1 ? 1 : Size)>
(derived(), RowsAtCompileTime == 1 ? 0 : start,
ColsAtCompileTime == 1 ? 0 : start);
}
/** This is the const version of block<int>(int).*/
template<typename Derived>
template<int Size>
inline const typename BlockReturnType<Derived,Size>::SubVectorType
MatrixBase<Derived>::block(int start) const
{
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
return Block<Derived, (RowsAtCompileTime == 1 ? 1 : Size),
(ColsAtCompileTime == 1 ? 1 : Size)>
(derived(), RowsAtCompileTime == 1 ? 0 : start,
ColsAtCompileTime == 1 ? 0 : start);
}
/** \returns a fixed-size expression of the first coefficients of *this. /** \returns a fixed-size expression of the first coefficients of *this.
* *
* \only_for_vectors * \only_for_vectors

View File

@ -381,6 +381,9 @@ template<typename Derived> class MatrixBase
template<int Size> typename BlockReturnType<Derived,Size>::SubVectorType end(); template<int Size> typename BlockReturnType<Derived,Size>::SubVectorType end();
template<int Size> const typename BlockReturnType<Derived,Size>::SubVectorType end() const; template<int Size> const typename BlockReturnType<Derived,Size>::SubVectorType end() const;
template<int Size> typename BlockReturnType<Derived,Size>::SubVectorType block(int start);
template<int Size> const typename BlockReturnType<Derived,Size>::SubVectorType block(int start) const;
DiagonalCoeffs<Derived> diagonal(); DiagonalCoeffs<Derived> diagonal();
const DiagonalCoeffs<Derived> diagonal() const; const DiagonalCoeffs<Derived> diagonal() const;

View File

@ -351,6 +351,8 @@ mat = 2 7 8
\endcode</td></tr> \endcode</td></tr>
</table> </table>
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.
<span class="note">\b Side \b note: The all() and any() functions are especially useful in combinaison with coeff-wise comparison operators (\ref CwiseAll "example").</span> <span class="note">\b Side \b note: The all() and any() functions are especially useful in combinaison with coeff-wise comparison operators (\ref CwiseAll "example").</span>

View File

@ -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;

View File

@ -123,6 +123,22 @@ template<typename MatrixType> void submatrices(const MatrixType& m)
VERIFY_IS_APPROX(b, m1.block(3,3,BlockRows,BlockCols)); 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 // stress some basic stuffs with block matrices
VERIFY(ones.col(c1).sum() == Scalar(rows)); VERIFY(ones.col(c1).sum() == Scalar(rows));
VERIFY(ones.row(r1).sum() == Scalar(cols)); VERIFY(ones.row(r1).sum() == Scalar(cols));