Add a generic block() method compatible with Eigen::fix

This commit is contained in:
Gael Guennebaud 2017-01-17 11:34:28 +01:00
parent 71e5b71356
commit 4f36dcfda8
3 changed files with 99 additions and 18 deletions

View File

@ -773,7 +773,7 @@ inline typename ConstNColsBlockXpr<N>::Type middleCols(Index startCol, Index n =
///
EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
///
/// \sa class Block, block(Index,Index,Index,Index)
/// \sa block(Index,Index,NRowsType,NColsType), class Block, block(Index,Index,Index,Index)
///
template<int NRows, int NCols>
EIGEN_DEVICE_FUNC
@ -809,7 +809,7 @@ inline const typename ConstFixedBlockXpr<NRows,NCols>::Type block(Index startRow
///
EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
///
/// \sa class Block, block(Index,Index,Index,Index)
/// \sa block(Index,Index,NRowsType,NColsType), block(Index,Index,Index,Index), class Block
///
template<int NRows, int NCols>
inline typename FixedBlockXpr<NRows,NCols>::Type block(Index startRow, Index startCol,
@ -826,6 +826,62 @@ inline const typename ConstFixedBlockXpr<NRows,NCols>::Type block(Index startRow
return typename ConstFixedBlockXpr<NRows,NCols>::Type(derived(), startRow, startCol, blockRows, blockCols);
}
/// \returns an expression of a block in \c *this.
///
/// \tparam NRowsType the type of the object handling the number of rows in the block, can be any integral type (e.g., int, Index) or any returned by Eigen::fix<N> or Eigen::fix<N>(n).
/// \tparam NColsType analogue of NRowsType but for the number of columns.
/// \param startRow the first row in the block
/// \param startCol the first column in the block
/// \param blockRows number of rows in the block as specified at either run-time or compile-time
/// \param blockCols number of columns in the block as specified at either run-time or compile-time
///
/// \newin{3.4}
///
/// This function covers the same versatility as block<NRows,NCols>(Index, Index), and block<NRows,NCols>(Index, Index, Index, Index)
/// but with less redundancy and more consistency as it does not modify the argument order
/// and seamlessly enable hybrid fixed/dynamic sizes.
/// The one-to-one full equivalences are as follows:
///
/// \code
/// mat.template block<NRows,NCols>(i,j) <--> mat.block(i,j,fix<NRows>,fix<NCols>)
/// mat.template block<NRows,NCols>(i,j,rows,cols) <--> mat.block(i,j,fix<NRows>(rows),fix<NCols>(cols))
/// \endcode
///
/// but of course, with this version one of the compile-time parameter can be completely
/// omitted if it turns out to be a pure runtime one:
/// \code
/// mat.template block<NRows,Dynamic>(i,j,rows,cols) <--> mat.block(i,j,fix<NRows>,cols)
/// \endcode
///
EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
///
/// \sa class Block, block(Index,Index,Index,Index), fix
///
template<typename NRowsType, typename NColsType>
#ifndef EIGEN_PARSED_BY_DOXYGEN
inline typename FixedBlockXpr<internal::get_fixed_value<NRowsType>::value,internal::get_fixed_value<NColsType>::value>::Type
#else
inline typename FixedBlockXpr<...,...>::Type
#endif
block(Index startRow, Index startCol, NRowsType blockRows, NColsType blockCols)
{
return typename FixedBlockXpr<internal::get_fixed_value<NRowsType>::value,internal::get_fixed_value<NColsType>::value>::Type(
derived(), startRow, startCol, internal::get_runtime_value(blockRows), internal::get_runtime_value(blockCols));
}
/// This is the const version of block(Index,Index,NRowsType,NColsType)
template<typename NRowsType, typename NColsType>
#ifndef EIGEN_PARSED_BY_DOXYGEN
inline typename ConstFixedBlockXpr<internal::get_fixed_value<NRowsType>::value,internal::get_fixed_value<NColsType>::value>::Type
#else
inline typename ConstFixedBlockXpr<...,...>::Type
#endif
block(Index startRow, Index startCol, NRowsType blockRows, NColsType blockCols) const
{
return typename ConstFixedBlockXpr<internal::get_fixed_value<NRowsType>::value,internal::get_fixed_value<NColsType>::value>::Type(
derived(), startRow, startCol, internal::get_runtime_value(blockRows), internal::get_runtime_value(blockCols));
}
/// \returns an expression of the \a i-th column of *this. Note that the numbering starts at 0.
///
/// Example: \include MatrixBase_col.cpp

View File

@ -29,6 +29,13 @@ block_real_only(const MatrixType &, Index, Index, Index, Index, const Scalar&) {
return Scalar(0);
}
// Check at compile-time that T1==T2, and at runtime-time that a==b
template<typename T1,typename T2>
typename internal::enable_if<internal::is_same<T1,T2>::value,bool>::type
is_same_block(const T1& a, const T2& b)
{
return a.isApprox(b);
}
template<typename MatrixType> void block(const MatrixType& m)
{
@ -106,6 +113,11 @@ template<typename MatrixType> void block(const MatrixType& m)
m1.template block<BlockRows,Dynamic>(1,1,BlockRows,BlockCols)(0,3) = m1.template block<2,5>(1,1)(1,2);
Matrix<Scalar,Dynamic,Dynamic> b2 = m1.template block<Dynamic,BlockCols>(3,3,2,5);
VERIFY_IS_EQUAL(b2, m1.block(3,3,BlockRows,BlockCols));
VERIFY(is_same_block(m1.block(3,3,BlockRows,BlockCols), m1.block(3,3,fix<Dynamic>(BlockRows),fix<Dynamic>(BlockCols))));
VERIFY(is_same_block(m1.template block<BlockRows,Dynamic>(1,1,BlockRows,BlockCols), m1.block(1,1,fix<BlockRows>,BlockCols)));
VERIFY(is_same_block(m1.template block<BlockRows,BlockCols>(1,1,BlockRows,BlockCols), m1.block(1,1,fix<BlockRows>(),fix<BlockCols>)));
VERIFY(is_same_block(m1.template block<BlockRows,BlockCols>(1,1,BlockRows,BlockCols), m1.block(1,1,fix<BlockRows>,fix<BlockCols>(BlockCols))));
}
if (rows>2)

View File

@ -49,7 +49,7 @@ bool match(const T& xpr, std::string ref, std::string str_xpr = "") {
template<typename T1,typename T2>
typename internal::enable_if<internal::is_same<T1,T2>::value,bool>::type
is_same_type(const T1& a, const T2& b)
is_same_eq(const T1& a, const T2& b)
{
return (a == b).all();
}
@ -219,25 +219,25 @@ void check_indexed_view()
// Check fall-back to Block
{
VERIFY( is_same_type(A.col(0), A(all,0)) );
VERIFY( is_same_type(A.row(0), A(0,all)) );
VERIFY( is_same_type(A.block(0,0,2,2), A(seqN(0,2),seq(0,1))) );
VERIFY( is_same_type(A.middleRows(2,4), A(seqN(2,4),all)) );
VERIFY( is_same_type(A.middleCols(2,4), A(all,seqN(2,4))) );
VERIFY( is_same_eq(A.col(0), A(all,0)) );
VERIFY( is_same_eq(A.row(0), A(0,all)) );
VERIFY( is_same_eq(A.block(0,0,2,2), A(seqN(0,2),seq(0,1))) );
VERIFY( is_same_eq(A.middleRows(2,4), A(seqN(2,4),all)) );
VERIFY( is_same_eq(A.middleCols(2,4), A(all,seqN(2,4))) );
VERIFY( is_same_type(A.col(A.cols()-1), A(all,last)) );
VERIFY( is_same_eq(A.col(A.cols()-1), A(all,last)) );
const ArrayXXi& cA(A);
VERIFY( is_same_type(cA.col(0), cA(all,0)) );
VERIFY( is_same_type(cA.row(0), cA(0,all)) );
VERIFY( is_same_type(cA.block(0,0,2,2), cA(seqN(0,2),seq(0,1))) );
VERIFY( is_same_type(cA.middleRows(2,4), cA(seqN(2,4),all)) );
VERIFY( is_same_type(cA.middleCols(2,4), cA(all,seqN(2,4))) );
VERIFY( is_same_eq(cA.col(0), cA(all,0)) );
VERIFY( is_same_eq(cA.row(0), cA(0,all)) );
VERIFY( is_same_eq(cA.block(0,0,2,2), cA(seqN(0,2),seq(0,1))) );
VERIFY( is_same_eq(cA.middleRows(2,4), cA(seqN(2,4),all)) );
VERIFY( is_same_eq(cA.middleCols(2,4), cA(all,seqN(2,4))) );
VERIFY( is_same_type(a.head(4), a(seq(0,3))) );
VERIFY( is_same_type(a.tail(4), a(seqN(last-3,4))) );
VERIFY( is_same_type(a.tail(4), a(seq(end-4,last))) );
VERIFY( is_same_type(a.segment<4>(3), a(seqN(3,fix<4>))) );
VERIFY( is_same_eq(a.head(4), a(seq(0,3))) );
VERIFY( is_same_eq(a.tail(4), a(seqN(last-3,4))) );
VERIFY( is_same_eq(a.tail(4), a(seq(end-4,last))) );
VERIFY( is_same_eq(a.segment<4>(3), a(seqN(3,fix<4>))) );
}
ArrayXXi A1=A, A2 = ArrayXXi::Random(4,4);
@ -275,6 +275,18 @@ void check_indexed_view()
VERIFY_IS_APPROX( A(legacy::seq(legacy::last,2,-2), legacy::seq(legacy::last-6,7)), A(seq(last,2,-2), seq(last-6,7)) );
VERIFY_IS_APPROX( A(seqN(legacy::last,2,-2), seqN(legacy::last-6,3)), A(seqN(last,2,-2), seqN(last-6,3)) );
// check extended block API
{
VERIFY( is_same_eq( A.block<3,4>(1,1), A.block(1,1,fix<3>,fix<4>)) );
VERIFY( is_same_eq( A.block<3,4>(1,1,3,4), A.block(1,1,fix<3>(),fix<4>(4))) );
VERIFY( is_same_eq( A.block<3,Dynamic>(1,1,3,4), A.block(1,1,fix<3>,4)) );
VERIFY( is_same_eq( A.block<Dynamic,4>(1,1,3,4), A.block(1,1,fix<Dynamic>(3),fix<4>)) );
VERIFY( is_same_eq( A.block(1,1,3,4), A.block(1,1,fix<Dynamic>(3),fix<Dynamic>(4))) );
const ArrayXXi& cA(A);
VERIFY( is_same_eq( cA.block<Dynamic,4>(1,1,3,4), cA.block(1,1,fix<Dynamic>(3),fix<4>)) );
}
}
void test_indexed_view()
@ -282,5 +294,6 @@ void test_indexed_view()
// for(int i = 0; i < g_repeat; i++) {
CALL_SUBTEST_1( check_indexed_view() );
CALL_SUBTEST_2( check_indexed_view() );
CALL_SUBTEST_3( check_indexed_view() );
// }
}