mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
- eigen2 now fully enforces constness! found a way to achieve that
with minimal code duplication. There now are only two (2) const_cast remaining in the whole source code. - eigen2 now fully allows copying a row-vector into a column-vector. added a unit-test for that. - split unit tests, improve docs, various improvements.
This commit is contained in:
@@ -95,8 +95,9 @@ template<typename MatrixType, int BlockRows, int BlockCols> class Block
|
||||
|
||||
/** \returns a fixed-size expression of a block in *this.
|
||||
*
|
||||
* \param blockRows the number of rows in the block
|
||||
* \param blockCols the number of columns in the block
|
||||
* 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
|
||||
*
|
||||
@@ -108,10 +109,18 @@ template<typename MatrixType, int BlockRows, int BlockCols> class Block
|
||||
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(). */
|
||||
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>
|
||||
(static_cast<Derived*>(const_cast<MatrixBase*>(this))->ref(), startRow, startCol);
|
||||
return Block<Derived, BlockRows, BlockCols>(ref(), startRow, startCol);
|
||||
}
|
||||
|
||||
#endif // EIGEN_BLOCK_H
|
||||
|
||||
@@ -116,7 +116,7 @@ template<typename Scalar, typename Derived>
|
||||
Scalar MatrixBase<Scalar, Derived>
|
||||
::coeff(int index) const
|
||||
{
|
||||
eigen_internal_assert(IsVector);
|
||||
eigen_internal_assert(IsVectorAtCompileTime);
|
||||
if(RowsAtCompileTime == 1)
|
||||
{
|
||||
eigen_internal_assert(index >= 0 && index < cols());
|
||||
@@ -140,7 +140,7 @@ template<typename Scalar, typename Derived>
|
||||
Scalar MatrixBase<Scalar, Derived>
|
||||
::operator[](int index) const
|
||||
{
|
||||
assert(IsVector);
|
||||
assert(IsVectorAtCompileTime);
|
||||
if(RowsAtCompileTime == 1)
|
||||
{
|
||||
assert(index >= 0 && index < cols());
|
||||
@@ -171,7 +171,7 @@ template<typename Scalar, typename Derived>
|
||||
Scalar& MatrixBase<Scalar, Derived>
|
||||
::coeffRef(int index)
|
||||
{
|
||||
eigen_internal_assert(IsVector);
|
||||
eigen_internal_assert(IsVectorAtCompileTime);
|
||||
if(RowsAtCompileTime == 1)
|
||||
{
|
||||
eigen_internal_assert(index >= 0 && index < cols());
|
||||
@@ -194,7 +194,7 @@ template<typename Scalar, typename Derived>
|
||||
Scalar& MatrixBase<Scalar, Derived>
|
||||
::operator[](int index)
|
||||
{
|
||||
assert(IsVector);
|
||||
assert(IsVectorAtCompileTime);
|
||||
if(RowsAtCompileTime == 1)
|
||||
{
|
||||
assert(index >= 0 && index < cols());
|
||||
|
||||
@@ -89,15 +89,23 @@ template<typename MatrixType> class Column
|
||||
|
||||
/** \returns an expression of the \a i-th column of *this. Note that the numbering starts at 0.
|
||||
*
|
||||
* Example: \include MatrixBase_col.cpp
|
||||
* Output: \verbinclude MatrixBase_col.out
|
||||
* Example: \include MatrixBase_column.cpp
|
||||
* Output: \verbinclude MatrixBase_column.out
|
||||
*
|
||||
* \sa row(), class Column */
|
||||
template<typename Scalar, typename Derived>
|
||||
Column<Derived>
|
||||
MatrixBase<Scalar, Derived>::col(int i)
|
||||
{
|
||||
return Column<Derived>(ref(), i);
|
||||
}
|
||||
|
||||
/** This is the const version of col(). */
|
||||
template<typename Scalar, typename Derived>
|
||||
const Column<Derived>
|
||||
MatrixBase<Scalar, Derived>::col(int i) const
|
||||
{
|
||||
return Column<Derived>(static_cast<Derived*>(const_cast<MatrixBase*>(this))->ref(), i);
|
||||
return Column<Derived>(ref(), i);
|
||||
}
|
||||
|
||||
#endif // EIGEN_COLUMN_H
|
||||
|
||||
@@ -64,10 +64,17 @@ template<typename MatrixType> class DiagonalCoeffs
|
||||
|
||||
template<typename Scalar, typename Derived>
|
||||
DiagonalCoeffs<Derived>
|
||||
MatrixBase<Scalar, Derived>::diagonal()
|
||||
{
|
||||
return DiagonalCoeffs<Derived>(ref());
|
||||
}
|
||||
|
||||
/** This is the const version of diagonal(). */
|
||||
template<typename Scalar, typename Derived>
|
||||
const DiagonalCoeffs<Derived>
|
||||
MatrixBase<Scalar, Derived>::diagonal() const
|
||||
{
|
||||
return DiagonalCoeffs<Derived>
|
||||
(static_cast<Derived*>(const_cast<MatrixBase*>(this))->ref());
|
||||
return DiagonalCoeffs<Derived>(ref());
|
||||
}
|
||||
|
||||
#endif // EIGEN_DIAGONALCOEFFS_H
|
||||
|
||||
@@ -38,7 +38,7 @@ class DiagonalMatrix : NoOperatorEquals,
|
||||
|
||||
DiagonalMatrix(const CoeffsVecRef& coeffs) : m_coeffs(coeffs)
|
||||
{
|
||||
assert(CoeffsVectorType::IsVector
|
||||
assert(CoeffsVectorType::IsVectorAtCompileTime
|
||||
&& _RowsAtCompileTime == _ColsAtCompileTime
|
||||
&& _RowsAtCompileTime == CoeffsVectorType::SizeAtCompileTime
|
||||
&& coeffs.size() > 0);
|
||||
|
||||
@@ -62,7 +62,7 @@ template<typename Scalar, typename Derived>
|
||||
template<typename OtherDerived>
|
||||
Scalar MatrixBase<Scalar, Derived>::dot(const OtherDerived& other) const
|
||||
{
|
||||
assert(IsVector && OtherDerived::IsVector && size() == other.size());
|
||||
assert(IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime && size() == other.size());
|
||||
Scalar res;
|
||||
if(EIGEN_UNROLLED_LOOPS && SizeAtCompileTime != Dynamic && SizeAtCompileTime <= 16)
|
||||
DotUnroller<SizeAtCompileTime-1, SizeAtCompileTime, Derived, OtherDerived>
|
||||
|
||||
@@ -109,10 +109,18 @@ template<typename MatrixType> class DynBlock
|
||||
*/
|
||||
template<typename Scalar, typename Derived>
|
||||
DynBlock<Derived> MatrixBase<Scalar, Derived>
|
||||
::dynBlock(int startRow, int startCol, int blockRows, int blockCols) const
|
||||
::dynBlock(int startRow, int startCol, int blockRows, int blockCols)
|
||||
{
|
||||
return DynBlock<Derived>(static_cast<Derived*>(const_cast<MatrixBase*>(this))->ref(),
|
||||
startRow, startCol, blockRows, blockCols);
|
||||
return DynBlock<Derived>(ref(), startRow, startCol, blockRows, blockCols);
|
||||
}
|
||||
|
||||
/** This is the const version of dynBlock(). */
|
||||
template<typename Scalar, typename Derived>
|
||||
const DynBlock<Derived> MatrixBase<Scalar, Derived>
|
||||
::dynBlock(int startRow, int startCol, int blockRows, int blockCols) const
|
||||
{
|
||||
return DynBlock<Derived>(ref(), startRow, startCol, blockRows, blockCols);
|
||||
}
|
||||
|
||||
|
||||
#endif // EIGEN_DYNBLOCK_H
|
||||
|
||||
@@ -34,7 +34,7 @@ bool MatrixBase<Scalar, Derived>::isApprox(
|
||||
) const
|
||||
{
|
||||
assert(rows() == other.rows() && cols() == other.cols());
|
||||
if(IsVector)
|
||||
if(IsVectorAtCompileTime)
|
||||
{
|
||||
return((*this - other).norm2() <= std::min(norm2(), other.norm2()) * prec * prec);
|
||||
}
|
||||
@@ -54,7 +54,7 @@ bool MatrixBase<Scalar, Derived>::isMuchSmallerThan(
|
||||
const typename NumTraits<Scalar>::Real& prec
|
||||
) const
|
||||
{
|
||||
if(IsVector)
|
||||
if(IsVectorAtCompileTime)
|
||||
{
|
||||
return(norm2() <= abs2(other * prec));
|
||||
}
|
||||
@@ -75,7 +75,7 @@ bool MatrixBase<Scalar, Derived>::isMuchSmallerThan(
|
||||
) const
|
||||
{
|
||||
assert(rows() == other.rows() && cols() == other.cols());
|
||||
if(IsVector)
|
||||
if(IsVectorAtCompileTime)
|
||||
{
|
||||
return(norm2() <= other.norm2() * prec * prec);
|
||||
}
|
||||
|
||||
@@ -66,23 +66,23 @@ template<typename MatrixType> class Map
|
||||
template<typename Scalar, typename Derived>
|
||||
const Map<Derived> MatrixBase<Scalar, Derived>::map(const Scalar* data, int rows, int cols)
|
||||
{
|
||||
return Map<Derived>(const_cast<Scalar*>(data), rows, cols);
|
||||
return Map<Derived>(data, rows, cols);
|
||||
}
|
||||
|
||||
template<typename Scalar, typename Derived>
|
||||
const Map<Derived> MatrixBase<Scalar, Derived>::map(const Scalar* data, int size)
|
||||
{
|
||||
assert(IsVector);
|
||||
assert(IsVectorAtCompileTime);
|
||||
if(ColsAtCompileTime == 1)
|
||||
return Map<Derived>(const_cast<Scalar*>(data), size, 1);
|
||||
return Map<Derived>(data, size, 1);
|
||||
else
|
||||
return Map<Derived>(const_cast<Scalar*>(data), 1, size);
|
||||
return Map<Derived>(data, 1, size);
|
||||
}
|
||||
|
||||
template<typename Scalar, typename Derived>
|
||||
const Map<Derived> MatrixBase<Scalar, Derived>::map(const Scalar* data)
|
||||
{
|
||||
return Map<Derived>(const_cast<Scalar*>(data), RowsAtCompileTime, ColsAtCompileTime);
|
||||
return Map<Derived>(data, RowsAtCompileTime, ColsAtCompileTime);
|
||||
}
|
||||
|
||||
template<typename Scalar, typename Derived>
|
||||
@@ -94,7 +94,7 @@ Map<Derived> MatrixBase<Scalar, Derived>::map(Scalar* data, int rows, int cols)
|
||||
template<typename Scalar, typename Derived>
|
||||
Map<Derived> MatrixBase<Scalar, Derived>::map(Scalar* data, int size)
|
||||
{
|
||||
assert(IsVector);
|
||||
assert(IsVectorAtCompileTime);
|
||||
if(ColsAtCompileTime == 1)
|
||||
return Map<Derived>(data, size, 1);
|
||||
else
|
||||
|
||||
@@ -63,14 +63,23 @@ class Matrix : public MatrixBase<_Scalar, Matrix<_Scalar, _Rows, _Cols> >,
|
||||
template<typename OtherDerived>
|
||||
Matrix& operator=(const MatrixBase<Scalar, OtherDerived>& other)
|
||||
{
|
||||
resize(other.rows(), other.cols());
|
||||
if(_RowsAtCompileTime == 1)
|
||||
{
|
||||
assert(other.isVector());
|
||||
resize(1, other.size());
|
||||
}
|
||||
else if(_ColsAtCompileTime == 1)
|
||||
{
|
||||
assert(other.isVector());
|
||||
resize(other.size(), 1);
|
||||
}
|
||||
else resize(other.rows(), other.cols());
|
||||
return Base::operator=(other);
|
||||
}
|
||||
|
||||
Matrix& operator=(const Matrix& other)
|
||||
{
|
||||
resize(other.rows(), other.cols());
|
||||
return Base::operator=(other);
|
||||
return operator=<Matrix>(other);
|
||||
}
|
||||
|
||||
EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Matrix, +=)
|
||||
|
||||
@@ -77,7 +77,7 @@ template<typename Scalar, typename Derived> class MatrixBase
|
||||
* columns is known at compile-time to be equal to 1. Indeed, in that case,
|
||||
* we are dealing with a column-vector (if there is only one column) or with
|
||||
* a row-vector (if there is only one row). */
|
||||
static const bool IsVector = RowsAtCompileTime == 1 || ColsAtCompileTime == 1;
|
||||
static const bool IsVectorAtCompileTime = RowsAtCompileTime == 1 || ColsAtCompileTime == 1;
|
||||
|
||||
/** This is the "reference type" used to pass objects of type MatrixBase as arguments
|
||||
* to functions. If this MatrixBase type represents an expression, then \a Ref
|
||||
@@ -98,9 +98,11 @@ template<typename Scalar, typename Derived> class MatrixBase
|
||||
/** \returns the number of columns. \sa row(), ColsAtCompileTime*/
|
||||
int cols() const { return static_cast<const Derived *>(this)->_cols(); }
|
||||
/** \returns the number of coefficients, which is \a rows()*cols().
|
||||
* \sa rows(), cols(). */
|
||||
* \sa rows(), cols(), SizeAtCompileTime. */
|
||||
int size() const { return rows() * cols(); }
|
||||
|
||||
/** \returns true if either the number of rows or the number of columns is equal to 1.
|
||||
* \sa rows(), cols(), IsVectorAtCompileTime. */
|
||||
bool isVector() const { return rows()==1 || cols()==1; }
|
||||
/** \returns a Ref to *this. \sa Ref */
|
||||
Ref ref() const
|
||||
{ return static_cast<const Derived *>(this)->_ref(); }
|
||||
@@ -118,15 +120,27 @@ template<typename Scalar, typename Derived> class MatrixBase
|
||||
|
||||
template<typename NewScalar> const Cast<NewScalar, Derived> cast() const;
|
||||
|
||||
Row<Derived> row(int i) const;
|
||||
Column<Derived> col(int i) const;
|
||||
Minor<Derived> minor(int row, int col) const;
|
||||
DynBlock<Derived> dynBlock(int startRow, int startCol,
|
||||
int blockRows, int blockCols) const;
|
||||
template<int BlockRows, int BlockCols>
|
||||
Block<Derived, BlockRows, BlockCols> block(int startRow, int startCol) const;
|
||||
Row<Derived> row(int i);
|
||||
const Row<Derived> row(int i) const;
|
||||
|
||||
Column<Derived> col(int i);
|
||||
const Column<Derived> col(int i) const;
|
||||
|
||||
Minor<Derived> minor(int row, int col);
|
||||
const Minor<Derived> minor(int row, int col) const;
|
||||
|
||||
DynBlock<Derived> dynBlock(int startRow, int startCol, int blockRows, int blockCols);
|
||||
const DynBlock<Derived>
|
||||
dynBlock(int startRow, int startCol, int blockRows, int blockCols) const;
|
||||
|
||||
template<int BlockRows, int BlockCols>
|
||||
Block<Derived, BlockRows, BlockCols> block(int startRow, int startCol);
|
||||
template<int BlockRows, int BlockCols>
|
||||
const Block<Derived, BlockRows, BlockCols> block(int startRow, int startCol) const;
|
||||
|
||||
Transpose<Derived> transpose();
|
||||
const Transpose<Derived> transpose() const;
|
||||
|
||||
Transpose<Derived> transpose() const;
|
||||
const Conjugate<Derived> conjugate() const;
|
||||
const Transpose<Conjugate<Derived> > adjoint() const;
|
||||
Scalar trace() const;
|
||||
@@ -151,7 +165,9 @@ template<typename Scalar, typename Derived> class MatrixBase
|
||||
template<typename OtherDerived>
|
||||
static const DiagonalMatrix<Derived, OtherDerived>
|
||||
diagonal(const OtherDerived& coeffs);
|
||||
DiagonalCoeffs<Derived> diagonal() const;
|
||||
|
||||
DiagonalCoeffs<Derived> diagonal();
|
||||
const DiagonalCoeffs<Derived> diagonal() const;
|
||||
|
||||
static const Map<Derived> map(const Scalar* array, int rows, int cols);
|
||||
static const Map<Derived> map(const Scalar* array, int size);
|
||||
|
||||
@@ -33,7 +33,7 @@ template<typename MatrixType> class MatrixRef
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
friend class MatrixBase<Scalar, MatrixRef>;
|
||||
|
||||
MatrixRef(const MatrixType& matrix) : m_matrix(*const_cast<MatrixType*>(&matrix)) {}
|
||||
MatrixRef(const MatrixType& matrix) : m_matrix(matrix) {}
|
||||
MatrixRef(const MatrixRef& other) : m_matrix(other.m_matrix) {}
|
||||
~MatrixRef() {}
|
||||
|
||||
@@ -53,11 +53,11 @@ template<typename MatrixType> class MatrixRef
|
||||
|
||||
Scalar& _coeffRef(int row, int col)
|
||||
{
|
||||
return m_matrix.coeffRef(row, col);
|
||||
return const_cast<MatrixType*>(&m_matrix)->_coeffRef(row, col);
|
||||
}
|
||||
|
||||
protected:
|
||||
MatrixType& m_matrix;
|
||||
const MatrixType& m_matrix;
|
||||
};
|
||||
|
||||
#endif // EIGEN_MATRIXREF_H
|
||||
|
||||
@@ -78,9 +78,17 @@ template<typename MatrixType> class Minor
|
||||
* row and column. */
|
||||
template<typename Scalar, typename Derived>
|
||||
Minor<Derived>
|
||||
MatrixBase<Scalar, Derived>::minor(int row, int col)
|
||||
{
|
||||
return Minor<Derived>(ref(), row, col);
|
||||
}
|
||||
|
||||
/** This is the const version of minor(). */
|
||||
template<typename Scalar, typename Derived>
|
||||
const Minor<Derived>
|
||||
MatrixBase<Scalar, Derived>::minor(int row, int col) const
|
||||
{
|
||||
return Minor<Derived>(static_cast<Derived*>(const_cast<MatrixBase*>(this))->ref(), row, col);
|
||||
return Minor<Derived>(ref(), row, col);
|
||||
}
|
||||
|
||||
#endif // EIGEN_MINOR_H
|
||||
|
||||
@@ -64,7 +64,7 @@ const Ones<Derived> MatrixBase<Scalar, Derived>::ones(int rows, int cols)
|
||||
template<typename Scalar, typename Derived>
|
||||
const Ones<Derived> MatrixBase<Scalar, Derived>::ones(int size)
|
||||
{
|
||||
assert(IsVector);
|
||||
assert(IsVectorAtCompileTime);
|
||||
if(RowsAtCompileTime == 1) return Ones<Derived>(1, size);
|
||||
else return Ones<Derived>(size, 1);
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ template<typename OtherDerived>
|
||||
Derived& MatrixBase<Scalar, Derived>
|
||||
::operator=(const MatrixBase<Scalar, OtherDerived>& other)
|
||||
{
|
||||
if(IsVector && OtherDerived::IsVector) // copying a vector expression into a vector
|
||||
if(IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime) // copying a vector expression into a vector
|
||||
{
|
||||
assert(size() == other.size());
|
||||
if(EIGEN_UNROLLED_LOOPS && SizeAtCompileTime != Dynamic && SizeAtCompileTime <= 25)
|
||||
|
||||
@@ -64,7 +64,7 @@ Eval<Random<Derived> > MatrixBase<Scalar, Derived>::random(int rows, int cols)
|
||||
template<typename Scalar, typename Derived>
|
||||
Eval<Random<Derived> > MatrixBase<Scalar, Derived>::random(int size)
|
||||
{
|
||||
assert(IsVector);
|
||||
assert(IsVectorAtCompileTime);
|
||||
if(RowsAtCompileTime == 1) return Random<Derived>(1, size).eval();
|
||||
else return Random<Derived>(size, 1).eval();
|
||||
}
|
||||
|
||||
@@ -103,9 +103,17 @@ template<typename MatrixType> class Row
|
||||
* \sa col(), class Row */
|
||||
template<typename Scalar, typename Derived>
|
||||
Row<Derived>
|
||||
MatrixBase<Scalar, Derived>::row(int i)
|
||||
{
|
||||
return Row<Derived>(ref(), i);
|
||||
}
|
||||
|
||||
/** This is the const version of row(). */
|
||||
template<typename Scalar, typename Derived>
|
||||
const Row<Derived>
|
||||
MatrixBase<Scalar, Derived>::row(int i) const
|
||||
{
|
||||
return Row<Derived>(static_cast<Derived*>(const_cast<MatrixBase*>(this))->ref(), i);
|
||||
return Row<Derived>(ref(), i);
|
||||
}
|
||||
|
||||
#endif // EIGEN_ROW_H
|
||||
|
||||
@@ -65,9 +65,17 @@ template<typename MatrixType> class Transpose
|
||||
|
||||
template<typename Scalar, typename Derived>
|
||||
Transpose<Derived>
|
||||
MatrixBase<Scalar, Derived>::transpose()
|
||||
{
|
||||
return Transpose<Derived>(ref());
|
||||
}
|
||||
|
||||
/** This is the const version of transpose(). */
|
||||
template<typename Scalar, typename Derived>
|
||||
const Transpose<Derived>
|
||||
MatrixBase<Scalar, Derived>::transpose() const
|
||||
{
|
||||
return Transpose<Derived>(static_cast<Derived*>(const_cast<MatrixBase*>(this))->ref());
|
||||
return Transpose<Derived>(ref());
|
||||
}
|
||||
|
||||
#endif // EIGEN_TRANSPOSE_H
|
||||
|
||||
@@ -64,7 +64,7 @@ const Zero<Derived> MatrixBase<Scalar, Derived>::zero(int rows, int cols)
|
||||
template<typename Scalar, typename Derived>
|
||||
const Zero<Derived> MatrixBase<Scalar, Derived>::zero(int size)
|
||||
{
|
||||
assert(IsVector);
|
||||
assert(IsVectorAtCompileTime);
|
||||
if(RowsAtCompileTime == 1) return Zero<Derived>(1, size);
|
||||
else return Zero<Derived>(size, 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user