mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-06-04 18:54:00 +08:00
Patch by Gael Guennebaud: unify fixed-size and dynamic-size Block
expressions, update documentation.
This commit is contained in:
parent
b3268a6e2f
commit
f12e9c53ac
@ -6,8 +6,8 @@
|
||||
|
||||
namespace Eigen {
|
||||
|
||||
#include "src/Core/ForwardDeclarations.h"
|
||||
#include "src/Core/Util.h"
|
||||
#include "src/Core/ForwardDeclarations.h"
|
||||
#include "src/Core/NumTraits.h"
|
||||
#include "src/Core/MathFunctions.h"
|
||||
#include "src/Core/MatrixBase.h"
|
||||
@ -25,7 +25,6 @@ namespace Eigen {
|
||||
#include "src/Core/Product.h"
|
||||
#include "src/Core/Row.h"
|
||||
#include "src/Core/Column.h"
|
||||
#include "src/Core/FixedBlock.h"
|
||||
#include "src/Core/Block.h"
|
||||
#include "src/Core/Minor.h"
|
||||
#include "src/Core/Transpose.h"
|
||||
|
@ -27,19 +27,21 @@
|
||||
|
||||
/** \class Block
|
||||
*
|
||||
* \brief Expression of a dynamic-size block
|
||||
* \brief Expression of a fixed-size or dynamic-size block
|
||||
*
|
||||
* \param MatrixType the type of the object in which we are taking a block
|
||||
* \param BlockRows the number of rows of the block we are taking at compile time (optional)
|
||||
* \param BlockCols the number of columns of the block we are taking at compile time (optional)
|
||||
*
|
||||
* This class represents an expression of a dynamic-size block. It is the return
|
||||
* type of MatrixBase::block(int,int,int,int) and most of the time this is the only way it
|
||||
* is used.
|
||||
* This class represents an expression of either a fixed-size or dynamic-size block. It is the return
|
||||
* type of MatrixBase::block(int,int,int,int) and MatrixBase::block<int,int>(int,int) and
|
||||
* most of the time this is the only way it is used.
|
||||
*
|
||||
* However, if you want to directly maniputate dynamic-size block expressions,
|
||||
* However, if you want to directly maniputate block expressions,
|
||||
* for instance if you want to write a function returning such an expression, you
|
||||
* will need to use this class.
|
||||
*
|
||||
* Here is an example illustrating this:
|
||||
* Here is an example illustrating the dynamic case:
|
||||
* \include class_Block.cpp
|
||||
* Output: \verbinclude class_Block.out
|
||||
*
|
||||
@ -47,10 +49,16 @@
|
||||
* has fixed size, this expression inherits a fixed maximal size which means that evaluating
|
||||
* it does not cause a dynamic memory allocation.
|
||||
*
|
||||
* \sa MatrixBase::block(int,int,int,int), class VectorBlock
|
||||
* Here is an example illustrating the fixed-size case:
|
||||
* \include class_FixedBlock.cpp
|
||||
* Output: \verbinclude class_FixedBlock.out
|
||||
*
|
||||
* \sa MatrixBase::block(int,int,int,int), MatrixBase::block(int,int), class VectorBlock
|
||||
*/
|
||||
template<typename MatrixType> class Block
|
||||
: public MatrixBase<typename MatrixType::Scalar, Block<MatrixType> >
|
||||
template<typename MatrixType,
|
||||
int BlockRows/*=Dynamic*/, int BlockCols/*=Dynamic*/> class Block
|
||||
: public MatrixBase<typename MatrixType::Scalar,
|
||||
Block<MatrixType, BlockRows, BlockCols> >
|
||||
{
|
||||
public:
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
@ -58,12 +66,26 @@ template<typename MatrixType> class Block
|
||||
friend class MatrixBase<Scalar, Block>;
|
||||
typedef MatrixBase<Scalar, Block> Base;
|
||||
|
||||
/** Fixed-size constructor
|
||||
*/
|
||||
Block(const MatRef& matrix, int startRow, int startCol)
|
||||
: m_matrix(matrix), m_startRow(startRow), m_startCol(startCol)
|
||||
{
|
||||
assert(RowsAtCompileTime!=Dynamic && RowsAtCompileTime!=Dynamic);
|
||||
assert(startRow >= 0 && BlockRows >= 1 && startRow + BlockRows <= matrix.rows()
|
||||
&& startCol >= 0 && BlockCols >= 1 && startCol + BlockCols <= matrix.cols());
|
||||
}
|
||||
|
||||
/** Dynamic-size constructor
|
||||
*/
|
||||
Block(const MatRef& matrix,
|
||||
int startRow, int startCol,
|
||||
int blockRows, int blockCols)
|
||||
: m_matrix(matrix), m_startRow(startRow), m_startCol(startCol),
|
||||
m_blockRows(blockRows), m_blockCols(blockCols)
|
||||
{
|
||||
assert((RowsAtCompileTime==Dynamic || RowsAtCompileTime==1)
|
||||
&& (ColsAtCompileTime==Dynamic || ColsAtCompileTime==1));
|
||||
assert(startRow >= 0 && blockRows >= 1 && startRow + blockRows <= matrix.rows()
|
||||
&& startCol >= 0 && blockCols >= 1 && startCol + blockCols <= matrix.cols());
|
||||
}
|
||||
@ -71,11 +93,13 @@ template<typename MatrixType> class Block
|
||||
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Block)
|
||||
|
||||
private:
|
||||
enum {
|
||||
RowsAtCompileTime = MatrixType::Traits::RowsAtCompileTime == 1 ? 1 : Dynamic,
|
||||
ColsAtCompileTime = MatrixType::Traits::ColsAtCompileTime == 1 ? 1 : Dynamic,
|
||||
MaxRowsAtCompileTime = RowsAtCompileTime == 1 ? 1 : MatrixType::Traits::MaxRowsAtCompileTime,
|
||||
MaxColsAtCompileTime = ColsAtCompileTime == 1 ? 1 : MatrixType::Traits::MaxColsAtCompileTime
|
||||
enum{
|
||||
RowsAtCompileTime = MatrixType::Traits::RowsAtCompileTime == 1 ? 1 : BlockRows,
|
||||
ColsAtCompileTime = MatrixType::Traits::ColsAtCompileTime == 1 ? 1 : BlockCols,
|
||||
MaxRowsAtCompileTime = RowsAtCompileTime == 1 ? 1
|
||||
: (BlockRows==Dynamic ? MatrixType::Traits::MaxRowsAtCompileTime : BlockRows),
|
||||
MaxColsAtCompileTime = ColsAtCompileTime == 1 ? 1
|
||||
: (BlockCols==Dynamic ? MatrixType::Traits::MaxColsAtCompileTime : BlockCols)
|
||||
};
|
||||
|
||||
const Block& _ref() const { return *this; }
|
||||
@ -93,11 +117,10 @@ template<typename MatrixType> class Block
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
MatRef m_matrix;
|
||||
IntAtRunTimeIfDynamic<MatrixType::Traits::RowsAtCompileTime == 1 ? 0 : Dynamic>
|
||||
m_startRow;
|
||||
IntAtRunTimeIfDynamic<MatrixType::Traits::ColsAtCompileTime == 1 ? 0 : Dynamic>
|
||||
m_startCol;
|
||||
IntAtRunTimeIfDynamic<MatrixType::Traits::RowsAtCompileTime == 1 ? 0 : Dynamic> m_startRow;
|
||||
IntAtRunTimeIfDynamic<MatrixType::Traits::ColsAtCompileTime == 1 ? 0 : Dynamic> m_startCol;
|
||||
IntAtRunTimeIfDynamic<RowsAtCompileTime> m_blockRows;
|
||||
IntAtRunTimeIfDynamic<ColsAtCompileTime> m_blockCols;
|
||||
};
|
||||
@ -116,7 +139,7 @@ template<typename MatrixType> class Block
|
||||
* when it is applied to a fixed-size matrix, it inherits a fixed maximal size,
|
||||
* which means that evaluating it does not cause a dynamic memory allocation.
|
||||
*
|
||||
* \sa class Block, fixedBlock(int,int)
|
||||
* \sa class Block, block(int,int)
|
||||
*/
|
||||
template<typename Scalar, typename Derived>
|
||||
Block<Derived> MatrixBase<Scalar, Derived>
|
||||
@ -147,7 +170,7 @@ const Block<Derived> MatrixBase<Scalar, Derived>
|
||||
* when it is applied to a fixed-size vector, it inherits a fixed maximal size,
|
||||
* which means that evaluating it does not cause a dynamic memory allocation.
|
||||
*
|
||||
* \sa class Block, fixedBlock(int)
|
||||
* \sa class Block, block(int)
|
||||
*/
|
||||
template<typename Scalar, typename Derived>
|
||||
Block<Derived> MatrixBase<Scalar, Derived>
|
||||
@ -293,4 +316,34 @@ const Block<Derived> MatrixBase<Scalar, Derived>
|
||||
return Block<Derived>(ref(), rows() - cRows, cols() - cCols, cRows, cCols);
|
||||
}
|
||||
|
||||
/** \returns a fixed-size expression of a block in *this.
|
||||
*
|
||||
* The template parameters \a BlockRows and \a BlockCols are the number of
|
||||
* rows and columns in the block.
|
||||
*
|
||||
* \param startRow the first row in the block
|
||||
* \param startCol the first column in the block
|
||||
*
|
||||
* Example: \include MatrixBase_block_int_int.cpp
|
||||
* Output: \verbinclude MatrixBase_block_int_int.out
|
||||
*
|
||||
* \sa class Block, block(int,int,int,int)
|
||||
*/
|
||||
template<typename Scalar, typename Derived>
|
||||
template<int BlockRows, int BlockCols>
|
||||
Block<Derived, BlockRows, BlockCols> MatrixBase<Scalar, Derived>
|
||||
::block(int startRow, int startCol)
|
||||
{
|
||||
return Block<Derived, BlockRows, BlockCols>(ref(), startRow, startCol);
|
||||
}
|
||||
|
||||
/** This is the const version of block<>(int, int). */
|
||||
template<typename Scalar, typename Derived>
|
||||
template<int BlockRows, int BlockCols>
|
||||
const Block<Derived, BlockRows, BlockCols> MatrixBase<Scalar, Derived>
|
||||
::block(int startRow, int startCol) const
|
||||
{
|
||||
return Block<Derived, BlockRows, BlockCols>(ref(), startRow, startCol);
|
||||
}
|
||||
|
||||
#endif // EIGEN_BLOCK_H
|
||||
|
@ -31,8 +31,7 @@ template<typename NewScalar, typename MatrixType> class Cast;
|
||||
template<typename MatrixType> class Row;
|
||||
template<typename MatrixType> class Column;
|
||||
template<typename MatrixType> class Minor;
|
||||
template<typename MatrixType> class Block;
|
||||
template<typename MatrixType, int BlockRows, int BlockCols> class FixedBlock;
|
||||
template<typename MatrixType, int BlockRows=Dynamic, int BlockCols=Dynamic> class Block;
|
||||
template<typename MatrixType> class Transpose;
|
||||
template<typename MatrixType> class Conjugate;
|
||||
template<typename MatrixType> class Opposite;
|
||||
|
@ -200,9 +200,9 @@ template<typename Scalar, typename Derived> class MatrixBase
|
||||
const Block<Derived> corner(CornerType type, int cRows, int cCols) const;
|
||||
|
||||
template<int BlockRows, int BlockCols>
|
||||
FixedBlock<Derived, BlockRows, BlockCols> fixedBlock(int startRow, int startCol);
|
||||
Block<Derived, BlockRows, BlockCols> block(int startRow, int startCol);
|
||||
template<int BlockRows, int BlockCols>
|
||||
const FixedBlock<Derived, BlockRows, BlockCols> fixedBlock(int startRow, int startCol) const;
|
||||
const Block<Derived, BlockRows, BlockCols> block(int startRow, int startCol) const;
|
||||
|
||||
Transpose<Derived> transpose();
|
||||
const Transpose<Derived> transpose() const;
|
||||
|
@ -3,17 +3,17 @@ USING_PART_OF_NAMESPACE_EIGEN
|
||||
using namespace std;
|
||||
|
||||
template<typename Scalar, typename Derived>
|
||||
Eigen::FixedBlock<Derived, 2, 2>
|
||||
Eigen::Block<Derived, 2, 2>
|
||||
topLeft2x2Corner(MatrixBase<Scalar, Derived>& m)
|
||||
{
|
||||
return Eigen::FixedBlock<Derived, 2, 2>(m.ref(), 0, 0);
|
||||
return Eigen::Block<Derived, 2, 2>(m.ref(), 0, 0);
|
||||
}
|
||||
|
||||
template<typename Scalar, typename Derived>
|
||||
const Eigen::FixedBlock<Derived, 2, 2>
|
||||
const Eigen::Block<Derived, 2, 2>
|
||||
topLeft2x2Corner(const MatrixBase<Scalar, Derived>& m)
|
||||
{
|
||||
return Eigen::FixedBlock<Derived, 2, 2>(m.ref(), 0, 0);
|
||||
return Eigen::Block<Derived, 2, 2>(m.ref(), 0, 0);
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
|
@ -1,5 +1,5 @@
|
||||
Matrix4d m = Vector4d(1,2,3,4).asDiagonal();
|
||||
cout << "Here is the matrix m:" << endl << m << endl;
|
||||
cout << "Here is m.fixedBlock<2, 2>(2, 2):" << endl << m.fixedBlock<2, 2>(2, 2) << endl;
|
||||
m.fixedBlock<2, 2>(2, 0) = m.fixedBlock<2, 2>(2, 2);
|
||||
cout << "Here is m.fixed<2, 2>(2, 2):" << endl << m.block<2, 2>(2, 2) << endl;
|
||||
m.block<2, 2>(2, 0) = m.block<2, 2>(2, 2);
|
||||
cout << "Now the matrix m is:" << endl << m << endl;
|
||||
|
@ -1,3 +1,3 @@
|
||||
Matrix4i m = Matrix4i::zero();
|
||||
m.fixedBlock<3,3>(1,0).setIdentity();
|
||||
m.block<3,3>(1,0).setIdentity();
|
||||
cout << m << endl;
|
||||
|
@ -18,13 +18,13 @@ int main(int, char **)
|
||||
// notice how we are mixing fixed-size and dynamic-size types.
|
||||
|
||||
cout << "In the top-left block, we put the matrix m shown above." << endl;
|
||||
m2.fixedBlock<2,2>(0,0) = m;
|
||||
m2.block<2,2>(0,0) = m;
|
||||
cout << "In the bottom-left block, we put the matrix m*m, which is:" << endl << m*m << endl;
|
||||
m2.fixedBlock<2,2>(2,0) = m * m;
|
||||
m2.block<2,2>(2,0) = m * m;
|
||||
cout << "In the top-right block, we put the matrix m+m, which is:" << endl << m+m << endl;
|
||||
m2.fixedBlock<2,2>(0,2) = m + m;
|
||||
m2.block<2,2>(0,2) = m + m;
|
||||
cout << "In the bottom-right block, we put the matrix m-m, which is:" << endl << m-m << endl;
|
||||
m2.fixedBlock<2,2>(2,2) = m - m;
|
||||
m2.block<2,2>(2,2) = m - m;
|
||||
cout << "Now the 4x4 matrix m2 is:" << endl << m2 << endl;
|
||||
|
||||
cout << "Row 0 of m2 is:" << endl << m2.row(0) << endl;
|
||||
|
@ -29,7 +29,7 @@ namespace Eigen {
|
||||
template<typename MatrixType> void submatrices(const MatrixType& m)
|
||||
{
|
||||
/* this test covers the following files:
|
||||
Row.h Column.h FixedBlock.h Block.h Minor.h DiagonalCoeffs.h
|
||||
Row.h Column.h Block.h Minor.h DiagonalCoeffs.h
|
||||
*/
|
||||
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
@ -103,17 +103,17 @@ void EigenTest::testSubmatrices()
|
||||
submatrices(MatrixXi(8, 12));
|
||||
submatrices(MatrixXcd(20, 20));
|
||||
|
||||
// test fixedBlock() separately as it is a template method so doesn't support
|
||||
// test fixed block() separately as it is a template method so doesn't support
|
||||
// being called as a member of a class that is itself a template parameter
|
||||
// (at least as of g++ 4.2)
|
||||
Matrix<float, 6, 8> m = Matrix<float, 6, 8>::random();
|
||||
float s = ei_random<float>();
|
||||
// test fixedBlock() as lvalue
|
||||
m.fixedBlock<2,5>(1,1) *= s;
|
||||
// test operator() on fixedBlock() both as constant and non-constant
|
||||
m.fixedBlock<2,5>(1,1)(0, 3) = m.fixedBlock<2,5>(1,1)(1,2);
|
||||
// check that fixedBlock() and block() agree
|
||||
MatrixXf b = m.fixedBlock<3,2>(3,3);
|
||||
// test fixed block() as lvalue
|
||||
m.block<2,5>(1,1) *= s;
|
||||
// test operator() on fixed block() both as constant and non-constant
|
||||
m.block<2,5>(1,1)(0, 3) = m.block<2,5>(1,1)(1,2);
|
||||
// check that fixed block() and block() agree
|
||||
MatrixXf b = m.block<3,2>(3,3);
|
||||
VERIFY_IS_APPROX(b, m.block(3,3,3,2));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user