mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-05-07 03:39:04 +08:00
bug #882: fix various const-correctness issues with *View classes.
This commit is contained in:
parent
118b1113d9
commit
5741349294
@ -105,7 +105,7 @@ const cholmod_sparse viewAsCholmod(const SparseMatrix<_Scalar,_Options,_Index>&
|
|||||||
/** Returns a view of the Eigen sparse matrix \a mat as Cholmod sparse matrix.
|
/** Returns a view of the Eigen sparse matrix \a mat as Cholmod sparse matrix.
|
||||||
* The data are not copied but shared. */
|
* The data are not copied but shared. */
|
||||||
template<typename _Scalar, int _Options, typename _Index, unsigned int UpLo>
|
template<typename _Scalar, int _Options, typename _Index, unsigned int UpLo>
|
||||||
cholmod_sparse viewAsCholmod(const SparseSelfAdjointView<SparseMatrix<_Scalar,_Options,_Index>, UpLo>& mat)
|
cholmod_sparse viewAsCholmod(const SparseSelfAdjointView<const SparseMatrix<_Scalar,_Options,_Index>, UpLo>& mat)
|
||||||
{
|
{
|
||||||
cholmod_sparse res = viewAsCholmod(mat.matrix().const_cast_derived());
|
cholmod_sparse res = viewAsCholmod(mat.matrix().const_cast_derived());
|
||||||
|
|
||||||
|
@ -37,7 +37,8 @@ struct traits<CwiseUnaryView<ViewOp, MatrixType> >
|
|||||||
typedef typename MatrixType::Nested MatrixTypeNested;
|
typedef typename MatrixType::Nested MatrixTypeNested;
|
||||||
typedef typename remove_all<MatrixTypeNested>::type _MatrixTypeNested;
|
typedef typename remove_all<MatrixTypeNested>::type _MatrixTypeNested;
|
||||||
enum {
|
enum {
|
||||||
Flags = traits<_MatrixTypeNested>::Flags & (RowMajorBit | LvalueBit | DirectAccessBit), // FIXME DirectAccessBit should not be handled by expressions
|
FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
|
||||||
|
Flags = traits<_MatrixTypeNested>::Flags & (RowMajorBit | FlagsLvalueBit | DirectAccessBit), // FIXME DirectAccessBit should not be handled by expressions
|
||||||
MatrixTypeInnerStride = inner_stride_at_compile_time<MatrixType>::ret,
|
MatrixTypeInnerStride = inner_stride_at_compile_time<MatrixType>::ret,
|
||||||
// need to cast the sizeof's from size_t to int explicitly, otherwise:
|
// need to cast the sizeof's from size_t to int explicitly, otherwise:
|
||||||
// "error: no integral type can represent all of the enumerator values
|
// "error: no integral type can represent all of the enumerator values
|
||||||
@ -63,7 +64,7 @@ class CwiseUnaryView : public CwiseUnaryViewImpl<ViewOp, MatrixType, typename in
|
|||||||
EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryView)
|
EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryView)
|
||||||
typedef typename internal::remove_all<MatrixType>::type NestedExpression;
|
typedef typename internal::remove_all<MatrixType>::type NestedExpression;
|
||||||
|
|
||||||
explicit inline CwiseUnaryView(const MatrixType& mat, const ViewOp& func = ViewOp())
|
explicit inline CwiseUnaryView(MatrixType& mat, const ViewOp& func = ViewOp())
|
||||||
: m_matrix(mat), m_functor(func) {}
|
: m_matrix(mat), m_functor(func) {}
|
||||||
|
|
||||||
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryView)
|
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryView)
|
||||||
|
@ -38,7 +38,8 @@ struct traits<SelfAdjointView<MatrixType, UpLo> > : traits<MatrixType>
|
|||||||
typedef typename MatrixType::PlainObject FullMatrixType;
|
typedef typename MatrixType::PlainObject FullMatrixType;
|
||||||
enum {
|
enum {
|
||||||
Mode = UpLo | SelfAdjoint,
|
Mode = UpLo | SelfAdjoint,
|
||||||
Flags = MatrixTypeNestedCleaned::Flags & (HereditaryBits)
|
FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
|
||||||
|
Flags = MatrixTypeNestedCleaned::Flags & (HereditaryBits|FlagsLvalueBit)
|
||||||
& (~(PacketAccessBit | DirectAccessBit | LinearAccessBit)) // FIXME these flags should be preserved
|
& (~(PacketAccessBit | DirectAccessBit | LinearAccessBit)) // FIXME these flags should be preserved
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -95,6 +96,7 @@ template<typename _MatrixType, unsigned int UpLo> class SelfAdjointView
|
|||||||
EIGEN_DEVICE_FUNC
|
EIGEN_DEVICE_FUNC
|
||||||
inline Scalar& coeffRef(Index row, Index col)
|
inline Scalar& coeffRef(Index row, Index col)
|
||||||
{
|
{
|
||||||
|
EIGEN_STATIC_ASSERT_LVALUE(SelfAdjointView);
|
||||||
Base::check_coordinates_internal(row, col);
|
Base::check_coordinates_internal(row, col);
|
||||||
return m_matrix.const_cast_derived().coeffRef(row, col);
|
return m_matrix.const_cast_derived().coeffRef(row, col);
|
||||||
}
|
}
|
||||||
|
@ -173,7 +173,8 @@ struct traits<TriangularView<MatrixType, _Mode> > : traits<MatrixType>
|
|||||||
typedef MatrixType ExpressionType;
|
typedef MatrixType ExpressionType;
|
||||||
enum {
|
enum {
|
||||||
Mode = _Mode,
|
Mode = _Mode,
|
||||||
Flags = (MatrixTypeNestedCleaned::Flags & (HereditaryBits | LvalueBit) & (~(PacketAccessBit | DirectAccessBit | LinearAccessBit)))
|
FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
|
||||||
|
Flags = (MatrixTypeNestedCleaned::Flags & (HereditaryBits | FlagsLvalueBit) & (~(PacketAccessBit | DirectAccessBit | LinearAccessBit)))
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -213,7 +214,7 @@ template<typename _MatrixType, unsigned int _Mode> class TriangularView
|
|||||||
|
|
||||||
// FIXME This, combined with const_cast_derived in transpose() leads to a const-correctness loophole
|
// FIXME This, combined with const_cast_derived in transpose() leads to a const-correctness loophole
|
||||||
EIGEN_DEVICE_FUNC
|
EIGEN_DEVICE_FUNC
|
||||||
explicit inline TriangularView(const MatrixType& matrix) : m_matrix(matrix)
|
explicit inline TriangularView(MatrixType& matrix) : m_matrix(matrix)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
using Base::operator=;
|
using Base::operator=;
|
||||||
@ -230,13 +231,8 @@ template<typename _MatrixType, unsigned int _Mode> class TriangularView
|
|||||||
EIGEN_DEVICE_FUNC
|
EIGEN_DEVICE_FUNC
|
||||||
NestedExpression& nestedExpression() { return *const_cast<NestedExpression*>(&m_matrix); }
|
NestedExpression& nestedExpression() { return *const_cast<NestedExpression*>(&m_matrix); }
|
||||||
|
|
||||||
typedef TriangularView<MatrixConjugateReturnType,Mode> ConjugateReturnType;
|
|
||||||
/** \sa MatrixBase::conjugate() */
|
|
||||||
EIGEN_DEVICE_FUNC
|
|
||||||
inline ConjugateReturnType conjugate()
|
|
||||||
{ return ConjugateReturnType(m_matrix.conjugate()); }
|
|
||||||
/** \sa MatrixBase::conjugate() const */
|
/** \sa MatrixBase::conjugate() const */
|
||||||
|
typedef TriangularView<const MatrixConjugateReturnType,Mode> ConjugateReturnType;
|
||||||
EIGEN_DEVICE_FUNC
|
EIGEN_DEVICE_FUNC
|
||||||
inline const ConjugateReturnType conjugate() const
|
inline const ConjugateReturnType conjugate() const
|
||||||
{ return ConjugateReturnType(m_matrix.conjugate()); }
|
{ return ConjugateReturnType(m_matrix.conjugate()); }
|
||||||
@ -253,7 +249,8 @@ template<typename _MatrixType, unsigned int _Mode> class TriangularView
|
|||||||
inline TransposeReturnType transpose()
|
inline TransposeReturnType transpose()
|
||||||
{
|
{
|
||||||
EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
|
EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
|
||||||
return TransposeReturnType(m_matrix.const_cast_derived().transpose());
|
typename MatrixType::TransposeReturnType tmp(m_matrix.const_cast_derived());
|
||||||
|
return TransposeReturnType(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef TriangularView<const typename MatrixType::ConstTransposeReturnType,TransposeMode> ConstTransposeReturnType;
|
typedef TriangularView<const typename MatrixType::ConstTransposeReturnType,TransposeMode> ConstTransposeReturnType;
|
||||||
@ -392,6 +389,7 @@ template<typename _MatrixType, unsigned int _Mode> class TriangularViewImpl<_Mat
|
|||||||
EIGEN_DEVICE_FUNC
|
EIGEN_DEVICE_FUNC
|
||||||
inline Scalar& coeffRef(Index row, Index col)
|
inline Scalar& coeffRef(Index row, Index col)
|
||||||
{
|
{
|
||||||
|
EIGEN_STATIC_ASSERT_LVALUE(TriangularViewType);
|
||||||
Base::check_coordinates_internal(row, col);
|
Base::check_coordinates_internal(row, col);
|
||||||
return derived().nestedExpression().const_cast_derived().coeffRef(row, col);
|
return derived().nestedExpression().const_cast_derived().coeffRef(row, col);
|
||||||
}
|
}
|
||||||
|
@ -237,8 +237,8 @@ template<typename _MatrixType, int _UpLo, typename _Ordering> struct traits<Simp
|
|||||||
typedef typename MatrixType::Scalar Scalar;
|
typedef typename MatrixType::Scalar Scalar;
|
||||||
typedef typename MatrixType::Index Index;
|
typedef typename MatrixType::Index Index;
|
||||||
typedef SparseMatrix<Scalar, ColMajor, Index> CholMatrixType;
|
typedef SparseMatrix<Scalar, ColMajor, Index> CholMatrixType;
|
||||||
typedef TriangularView<CholMatrixType, Eigen::Lower> MatrixL;
|
typedef TriangularView<const CholMatrixType, Eigen::Lower> MatrixL;
|
||||||
typedef TriangularView<typename CholMatrixType::AdjointReturnType, Eigen::Upper> MatrixU;
|
typedef TriangularView<const typename CholMatrixType::AdjointReturnType, Eigen::Upper> MatrixU;
|
||||||
static inline MatrixL getL(const MatrixType& m) { return MatrixL(m); }
|
static inline MatrixL getL(const MatrixType& m) { return MatrixL(m); }
|
||||||
static inline MatrixU getU(const MatrixType& m) { return MatrixU(m.adjoint()); }
|
static inline MatrixU getU(const MatrixType& m) { return MatrixU(m.adjoint()); }
|
||||||
};
|
};
|
||||||
@ -251,8 +251,8 @@ template<typename _MatrixType,int _UpLo, typename _Ordering> struct traits<Simpl
|
|||||||
typedef typename MatrixType::Scalar Scalar;
|
typedef typename MatrixType::Scalar Scalar;
|
||||||
typedef typename MatrixType::Index Index;
|
typedef typename MatrixType::Index Index;
|
||||||
typedef SparseMatrix<Scalar, ColMajor, Index> CholMatrixType;
|
typedef SparseMatrix<Scalar, ColMajor, Index> CholMatrixType;
|
||||||
typedef TriangularView<CholMatrixType, Eigen::UnitLower> MatrixL;
|
typedef TriangularView<const CholMatrixType, Eigen::UnitLower> MatrixL;
|
||||||
typedef TriangularView<typename CholMatrixType::AdjointReturnType, Eigen::UnitUpper> MatrixU;
|
typedef TriangularView<const typename CholMatrixType::AdjointReturnType, Eigen::UnitUpper> MatrixU;
|
||||||
static inline MatrixL getL(const MatrixType& m) { return MatrixL(m); }
|
static inline MatrixL getL(const MatrixType& m) { return MatrixL(m); }
|
||||||
static inline MatrixU getU(const MatrixType& m) { return MatrixU(m.adjoint()); }
|
static inline MatrixU getU(const MatrixType& m) { return MatrixU(m.adjoint()); }
|
||||||
};
|
};
|
||||||
|
@ -297,9 +297,9 @@ template<typename Derived> class SparseMatrixBase : public EigenBase<Derived>
|
|||||||
Derived& operator*=(const SparseMatrixBase<OtherDerived>& other);
|
Derived& operator*=(const SparseMatrixBase<OtherDerived>& other);
|
||||||
|
|
||||||
template<int Mode>
|
template<int Mode>
|
||||||
inline const TriangularView<Derived, Mode> triangularView() const;
|
inline const TriangularView<const Derived, Mode> triangularView() const;
|
||||||
|
|
||||||
template<unsigned int UpLo> inline const SparseSelfAdjointView<Derived, UpLo> selfadjointView() const;
|
template<unsigned int UpLo> inline const SparseSelfAdjointView<const Derived, UpLo> selfadjointView() const;
|
||||||
template<unsigned int UpLo> inline SparseSelfAdjointView<Derived, UpLo> selfadjointView();
|
template<unsigned int UpLo> inline SparseSelfAdjointView<Derived, UpLo> selfadjointView();
|
||||||
|
|
||||||
template<typename OtherDerived> Scalar dot(const MatrixBase<OtherDerived>& other) const;
|
template<typename OtherDerived> Scalar dot(const MatrixBase<OtherDerived>& other) const;
|
||||||
|
@ -170,9 +170,9 @@ template<typename MatrixType, unsigned int _Mode> class SparseSelfAdjointView
|
|||||||
|
|
||||||
template<typename Derived>
|
template<typename Derived>
|
||||||
template<unsigned int Mode>
|
template<unsigned int Mode>
|
||||||
const SparseSelfAdjointView<Derived, Mode> SparseMatrixBase<Derived>::selfadjointView() const
|
const SparseSelfAdjointView<const Derived, Mode> SparseMatrixBase<Derived>::selfadjointView() const
|
||||||
{
|
{
|
||||||
return SparseSelfAdjointView<Derived, Mode>(derived());
|
return SparseSelfAdjointView<const Derived, Mode>(derived());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Derived>
|
template<typename Derived>
|
||||||
|
@ -266,10 +266,10 @@ protected:
|
|||||||
|
|
||||||
template<typename Derived>
|
template<typename Derived>
|
||||||
template<int Mode>
|
template<int Mode>
|
||||||
inline const TriangularView<Derived, Mode>
|
inline const TriangularView<const Derived, Mode>
|
||||||
SparseMatrixBase<Derived>::triangularView() const
|
SparseMatrixBase<Derived>::triangularView() const
|
||||||
{
|
{
|
||||||
return TriangularView<Derived, Mode>(derived());
|
return TriangularView<const Derived, Mode>(derived());
|
||||||
}
|
}
|
||||||
|
|
||||||
} // end namespace Eigen
|
} // end namespace Eigen
|
||||||
|
Loading…
x
Reference in New Issue
Block a user