* various fixes related to sub diagonals and band matrix

* allows 0 sized objects in Block/Map
This commit is contained in:
Gael Guennebaud 2009-07-21 11:19:52 +02:00
parent 34490f1493
commit afa8f2ca95
6 changed files with 60 additions and 27 deletions

View File

@ -103,12 +103,21 @@ class BandMatrix : public MultiplierBase<BandMatrix<_Scalar,Rows,Cols,Supers,Sub
inline int subs() const { return m_subs.value(); } inline int subs() const { return m_subs.value(); }
/** \returns a vector expression of the \a i -th column, /** \returns a vector expression of the \a i -th column,
* only the meaningful part is returned */ * only the meaningful part is returned.
* \warning the internal storage must be column major. */
inline Block<DataType,Dynamic,1> col(int i) inline Block<DataType,Dynamic,1> col(int i)
{ {
int j = i - (cols() - supers() + 1); EIGEN_STATIC_ASSERT((Options&RowMajor)==0,THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
int start = std::max(0,subs() - i + 1); int start = 0;
return Block<DataType,Dynamic,1>(m_data, start, i, m_data.rows() - (j<0 ? start : j), 1); int len = m_data.rows();
if (i<=supers())
{
start = supers()-i;
len = std::min(rows(),std::max(0,m_data.rows() - (supers()-i)));
}
else if (i>=rows()-subs())
len = std::max(0,m_data.rows() - (i + 1 - rows() + subs()));
return Block<DataType,Dynamic,1>(m_data, start, i, len, 1);
} }
/** \returns a vector expression of the main diagonal */ /** \returns a vector expression of the main diagonal */
@ -164,7 +173,6 @@ class BandMatrix : public MultiplierBase<BandMatrix<_Scalar,Rows,Cols,Supers,Sub
PlainMatrixType toDense() const PlainMatrixType toDense() const
{ {
std::cerr << m_data << "\n\n";
PlainMatrixType res(rows(),cols()); PlainMatrixType res(rows(),cols());
res.setZero(); res.setZero();
res.diagonal() = diagonal(); res.diagonal() = diagonal();

View File

@ -124,7 +124,7 @@ template<typename MatrixType, int BlockRows, int BlockCols, int PacketAccess, in
{ {
EIGEN_STATIC_ASSERT(RowsAtCompileTime!=Dynamic && ColsAtCompileTime!=Dynamic,THIS_METHOD_IS_ONLY_FOR_FIXED_SIZE) EIGEN_STATIC_ASSERT(RowsAtCompileTime!=Dynamic && ColsAtCompileTime!=Dynamic,THIS_METHOD_IS_ONLY_FOR_FIXED_SIZE)
ei_assert(startRow >= 0 && BlockRows >= 1 && startRow + BlockRows <= matrix.rows() ei_assert(startRow >= 0 && BlockRows >= 1 && startRow + BlockRows <= matrix.rows()
&& startCol >= 0 && BlockCols >= 1 && startCol + BlockCols <= matrix.cols()); && startCol >= 0 && BlockCols >= 1 && startCol + BlockCols <= matrix.cols());
} }
/** Dynamic-size constructor /** Dynamic-size constructor
@ -137,8 +137,8 @@ template<typename MatrixType, int BlockRows, int BlockCols, int PacketAccess, in
{ {
ei_assert((RowsAtCompileTime==Dynamic || RowsAtCompileTime==blockRows) ei_assert((RowsAtCompileTime==Dynamic || RowsAtCompileTime==blockRows)
&& (ColsAtCompileTime==Dynamic || ColsAtCompileTime==blockCols)); && (ColsAtCompileTime==Dynamic || ColsAtCompileTime==blockCols));
ei_assert(startRow >= 0 && blockRows >= 1 && startRow + blockRows <= matrix.rows() ei_assert(startRow >= 0 && blockRows >= 0 && startRow + blockRows <= matrix.rows()
&& startCol >= 0 && blockCols >= 1 && startCol + blockCols <= matrix.cols()); && startCol >= 0 && blockCols >= 0 && startCol + blockCols <= matrix.cols());
} }
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Block) EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Block)
@ -265,8 +265,8 @@ class Block<MatrixType,BlockRows,BlockCols,PacketAccess,HasDirectAccess>
{ {
ei_assert((RowsAtCompileTime==Dynamic || RowsAtCompileTime==blockRows) ei_assert((RowsAtCompileTime==Dynamic || RowsAtCompileTime==blockRows)
&& (ColsAtCompileTime==Dynamic || ColsAtCompileTime==blockCols)); && (ColsAtCompileTime==Dynamic || ColsAtCompileTime==blockCols));
ei_assert(startRow >= 0 && blockRows >= 1 && startRow + blockRows <= matrix.rows() ei_assert(startRow >= 0 && blockRows >= 0 && startRow + blockRows <= matrix.rows()
&& startCol >= 0 && blockCols >= 1 && startCol + blockCols <= matrix.cols()); && startCol >= 0 && blockCols >= 0 && startCol + blockCols <= matrix.cols());
} }
inline int stride(void) const { return m_matrix.stride(); } inline int stride(void) const { return m_matrix.stride(); }

View File

@ -64,10 +64,10 @@ class CwiseNullaryOp : ei_no_assignment_operator,
CwiseNullaryOp(int rows, int cols, const NullaryOp& func = NullaryOp()) CwiseNullaryOp(int rows, int cols, const NullaryOp& func = NullaryOp())
: m_rows(rows), m_cols(cols), m_functor(func) : m_rows(rows), m_cols(cols), m_functor(func)
{ {
ei_assert(rows > 0 ei_assert(rows >= 0
&& (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows) && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
&& cols > 0 && cols >= 0
&& (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)); && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
} }
EIGEN_STRONG_INLINE int rows() const { return m_rows.value(); } EIGEN_STRONG_INLINE int rows() const { return m_rows.value(); }

View File

@ -79,7 +79,9 @@ template<typename MatrixType, int Index> class Diagonal
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Diagonal) EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Diagonal)
inline int rows() const{ return m_matrix.diagonalSize() - absIndex(); } inline int rows() const
{ return m_index.value()<0 ? std::min(m_matrix.cols(),m_matrix.rows()+m_index.value()) : std::min(m_matrix.rows(),m_matrix.cols()-m_index.value()); }
inline int cols() const { return 1; } inline int cols() const { return 1; }
inline Scalar& coeffRef(int row, int) inline Scalar& coeffRef(int row, int)

View File

@ -154,16 +154,16 @@ template<typename Derived> class MapBase
m_cols(ColsAtCompileTime == Dynamic ? size : ColsAtCompileTime) m_cols(ColsAtCompileTime == Dynamic ? size : ColsAtCompileTime)
{ {
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
ei_assert(size > 0 || data == 0); ei_assert(size >= 0);
ei_assert(SizeAtCompileTime == Dynamic || SizeAtCompileTime == size); ei_assert(data == 0 || SizeAtCompileTime == Dynamic || SizeAtCompileTime == size);
} }
inline MapBase(const Scalar* data, int rows, int cols) inline MapBase(const Scalar* data, int rows, int cols)
: m_data(data), m_rows(rows), m_cols(cols) : m_data(data), m_rows(rows), m_cols(cols)
{ {
ei_assert( (data == 0) ei_assert( (data == 0)
|| ( rows > 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows) || ( rows >= 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
&& cols > 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols))); && cols >= 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)));
} }
Derived& operator=(const MapBase& other) Derived& operator=(const MapBase& other)

View File

@ -24,15 +24,20 @@
#include "main.h" #include "main.h"
template<typename MatrixType> void bandmatrix(MatrixType& m) template<typename MatrixType> void bandmatrix(const MatrixType& _m)
{ {
typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::Scalar Scalar;
typedef typename NumTraits<Scalar>::Real RealScalar; typedef typename NumTraits<Scalar>::Real RealScalar;
typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrixType; typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrixType;
int size = m.rows(); int rows = _m.rows();
int cols = _m.cols();
int supers = _m.supers();
int subs = _m.subs();
DenseMatrixType dm1(size,size); MatrixType m(rows,cols,supers,subs);
DenseMatrixType dm1(rows,cols);
dm1.setZero(); dm1.setZero();
m.diagonal().setConstant(123); m.diagonal().setConstant(123);
@ -47,15 +52,33 @@ template<typename MatrixType> void bandmatrix(MatrixType& m)
m.diagonal(-i).setConstant(-i); m.diagonal(-i).setConstant(-i);
dm1.diagonal(-i).setConstant(-i); dm1.diagonal(-i).setConstant(-i);
} }
std::cerr << m.toDense() << "\n\n" << dm1 << "\n\n"; //std::cerr << m.m_data << "\n\n" << m.toDense() << "\n\n" << dm1 << "\n\n\n\n";
VERIFY_IS_APPROX(dm1,m.toDense());
for (int i=0; i<cols; ++i)
{
m.col(i).setConstant(i+1);
dm1.col(i).setConstant(i+1);
}
int d = std::min(rows,cols);
int a = std::max(0,cols-d-supers);
int b = std::max(0,rows-d-subs);
if(a>0) dm1.block(0,d+supers,rows,a).setZero();
dm1.block(0,supers+1,cols-supers-1-a,cols-supers-1-a).template triangularView<UpperTriangular>().setZero();
dm1.block(subs+1,0,rows-subs-1-b,rows-subs-1-b).template triangularView<LowerTriangular>().setZero();
if(b>0) dm1.block(d+subs,0,b,cols).setZero();
//std::cerr << m.m_data << "\n\n" << m.toDense() << "\n\n" << dm1 << "\n\n";
VERIFY_IS_APPROX(dm1,m.toDense()); VERIFY_IS_APPROX(dm1,m.toDense());
} }
void test_bandmatrix() void test_bandmatrix()
{ {
for(int i = 0; i < g_repeat ; i++) { for(int i = 0; i < 10*g_repeat ; i++) {
BandMatrix<float,Dynamic,Dynamic,Dynamic> m(6,6,3,2); int rows = ei_random<int>(1,10);
CALL_SUBTEST( bandmatrix(m) ); int cols = ei_random<int>(1,10);
int sups = ei_random<int>(0,cols-1);
int subs = ei_random<int>(0,rows-1);
CALL_SUBTEST( bandmatrix(BandMatrix<float>(rows,cols,sups,subs)) );
} }
} }