File indexing completed on 2025-01-18 09:56:17
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef EIGEN_SELFADJOINTMATRIX_H
0011 #define EIGEN_SELFADJOINTMATRIX_H
0012
0013 namespace Eigen {
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 namespace internal {
0032 template<typename MatrixType, unsigned int UpLo>
0033 struct traits<SelfAdjointView<MatrixType, UpLo> > : traits<MatrixType>
0034 {
0035 typedef typename ref_selector<MatrixType>::non_const_type MatrixTypeNested;
0036 typedef typename remove_all<MatrixTypeNested>::type MatrixTypeNestedCleaned;
0037 typedef MatrixType ExpressionType;
0038 typedef typename MatrixType::PlainObject FullMatrixType;
0039 enum {
0040 Mode = UpLo | SelfAdjoint,
0041 FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
0042 Flags = MatrixTypeNestedCleaned::Flags & (HereditaryBits|FlagsLvalueBit)
0043 & (~(PacketAccessBit | DirectAccessBit | LinearAccessBit))
0044 };
0045 };
0046 }
0047
0048
0049 template<typename _MatrixType, unsigned int UpLo> class SelfAdjointView
0050 : public TriangularBase<SelfAdjointView<_MatrixType, UpLo> >
0051 {
0052 public:
0053
0054 typedef _MatrixType MatrixType;
0055 typedef TriangularBase<SelfAdjointView> Base;
0056 typedef typename internal::traits<SelfAdjointView>::MatrixTypeNested MatrixTypeNested;
0057 typedef typename internal::traits<SelfAdjointView>::MatrixTypeNestedCleaned MatrixTypeNestedCleaned;
0058 typedef MatrixTypeNestedCleaned NestedExpression;
0059
0060
0061 typedef typename internal::traits<SelfAdjointView>::Scalar Scalar;
0062 typedef typename MatrixType::StorageIndex StorageIndex;
0063 typedef typename internal::remove_all<typename MatrixType::ConjugateReturnType>::type MatrixConjugateReturnType;
0064 typedef SelfAdjointView<typename internal::add_const<MatrixType>::type, UpLo> ConstSelfAdjointView;
0065
0066 enum {
0067 Mode = internal::traits<SelfAdjointView>::Mode,
0068 Flags = internal::traits<SelfAdjointView>::Flags,
0069 TransposeMode = ((int(Mode) & int(Upper)) ? Lower : 0) | ((int(Mode) & int(Lower)) ? Upper : 0)
0070 };
0071 typedef typename MatrixType::PlainObject PlainObject;
0072
0073 EIGEN_DEVICE_FUNC
0074 explicit inline SelfAdjointView(MatrixType& matrix) : m_matrix(matrix)
0075 {
0076 EIGEN_STATIC_ASSERT(UpLo==Lower || UpLo==Upper,SELFADJOINTVIEW_ACCEPTS_UPPER_AND_LOWER_MODE_ONLY);
0077 }
0078
0079 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0080 inline Index rows() const EIGEN_NOEXCEPT { return m_matrix.rows(); }
0081 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0082 inline Index cols() const EIGEN_NOEXCEPT { return m_matrix.cols(); }
0083 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0084 inline Index outerStride() const EIGEN_NOEXCEPT { return m_matrix.outerStride(); }
0085 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0086 inline Index innerStride() const EIGEN_NOEXCEPT { return m_matrix.innerStride(); }
0087
0088
0089
0090
0091 EIGEN_DEVICE_FUNC
0092 inline Scalar coeff(Index row, Index col) const
0093 {
0094 Base::check_coordinates_internal(row, col);
0095 return m_matrix.coeff(row, col);
0096 }
0097
0098
0099
0100
0101 EIGEN_DEVICE_FUNC
0102 inline Scalar& coeffRef(Index row, Index col)
0103 {
0104 EIGEN_STATIC_ASSERT_LVALUE(SelfAdjointView);
0105 Base::check_coordinates_internal(row, col);
0106 return m_matrix.coeffRef(row, col);
0107 }
0108
0109
0110 EIGEN_DEVICE_FUNC
0111 const MatrixTypeNestedCleaned& _expression() const { return m_matrix; }
0112
0113 EIGEN_DEVICE_FUNC
0114 const MatrixTypeNestedCleaned& nestedExpression() const { return m_matrix; }
0115 EIGEN_DEVICE_FUNC
0116 MatrixTypeNestedCleaned& nestedExpression() { return m_matrix; }
0117
0118
0119 template<typename OtherDerived>
0120 EIGEN_DEVICE_FUNC
0121 const Product<SelfAdjointView,OtherDerived>
0122 operator*(const MatrixBase<OtherDerived>& rhs) const
0123 {
0124 return Product<SelfAdjointView,OtherDerived>(*this, rhs.derived());
0125 }
0126
0127
0128 template<typename OtherDerived> friend
0129 EIGEN_DEVICE_FUNC
0130 const Product<OtherDerived,SelfAdjointView>
0131 operator*(const MatrixBase<OtherDerived>& lhs, const SelfAdjointView& rhs)
0132 {
0133 return Product<OtherDerived,SelfAdjointView>(lhs.derived(),rhs);
0134 }
0135
0136 friend EIGEN_DEVICE_FUNC
0137 const SelfAdjointView<const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar,MatrixType,product),UpLo>
0138 operator*(const Scalar& s, const SelfAdjointView& mat)
0139 {
0140 return (s*mat.nestedExpression()).template selfadjointView<UpLo>();
0141 }
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153 template<typename DerivedU, typename DerivedV>
0154 EIGEN_DEVICE_FUNC
0155 SelfAdjointView& rankUpdate(const MatrixBase<DerivedU>& u, const MatrixBase<DerivedV>& v, const Scalar& alpha = Scalar(1));
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167 template<typename DerivedU>
0168 EIGEN_DEVICE_FUNC
0169 SelfAdjointView& rankUpdate(const MatrixBase<DerivedU>& u, const Scalar& alpha = Scalar(1));
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181 template<unsigned int TriMode>
0182 EIGEN_DEVICE_FUNC
0183 typename internal::conditional<(TriMode&(Upper|Lower))==(UpLo&(Upper|Lower)),
0184 TriangularView<MatrixType,TriMode>,
0185 TriangularView<typename MatrixType::AdjointReturnType,TriMode> >::type
0186 triangularView() const
0187 {
0188 typename internal::conditional<(TriMode&(Upper|Lower))==(UpLo&(Upper|Lower)), MatrixType&, typename MatrixType::ConstTransposeReturnType>::type tmp1(m_matrix);
0189 typename internal::conditional<(TriMode&(Upper|Lower))==(UpLo&(Upper|Lower)), MatrixType&, typename MatrixType::AdjointReturnType>::type tmp2(tmp1);
0190 return typename internal::conditional<(TriMode&(Upper|Lower))==(UpLo&(Upper|Lower)),
0191 TriangularView<MatrixType,TriMode>,
0192 TriangularView<typename MatrixType::AdjointReturnType,TriMode> >::type(tmp2);
0193 }
0194
0195 typedef SelfAdjointView<const MatrixConjugateReturnType,UpLo> ConjugateReturnType;
0196
0197 EIGEN_DEVICE_FUNC
0198 inline const ConjugateReturnType conjugate() const
0199 { return ConjugateReturnType(m_matrix.conjugate()); }
0200
0201
0202
0203
0204 template<bool Cond>
0205 EIGEN_DEVICE_FUNC
0206 inline typename internal::conditional<Cond,ConjugateReturnType,ConstSelfAdjointView>::type
0207 conjugateIf() const
0208 {
0209 typedef typename internal::conditional<Cond,ConjugateReturnType,ConstSelfAdjointView>::type ReturnType;
0210 return ReturnType(m_matrix.template conjugateIf<Cond>());
0211 }
0212
0213 typedef SelfAdjointView<const typename MatrixType::AdjointReturnType,TransposeMode> AdjointReturnType;
0214
0215 EIGEN_DEVICE_FUNC
0216 inline const AdjointReturnType adjoint() const
0217 { return AdjointReturnType(m_matrix.adjoint()); }
0218
0219 typedef SelfAdjointView<typename MatrixType::TransposeReturnType,TransposeMode> TransposeReturnType;
0220
0221 EIGEN_DEVICE_FUNC
0222 inline TransposeReturnType transpose()
0223 {
0224 EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
0225 typename MatrixType::TransposeReturnType tmp(m_matrix);
0226 return TransposeReturnType(tmp);
0227 }
0228
0229 typedef SelfAdjointView<const typename MatrixType::ConstTransposeReturnType,TransposeMode> ConstTransposeReturnType;
0230
0231 EIGEN_DEVICE_FUNC
0232 inline const ConstTransposeReturnType transpose() const
0233 {
0234 return ConstTransposeReturnType(m_matrix.transpose());
0235 }
0236
0237
0238
0239
0240
0241
0242 EIGEN_DEVICE_FUNC
0243 typename MatrixType::ConstDiagonalReturnType diagonal() const
0244 {
0245 return typename MatrixType::ConstDiagonalReturnType(m_matrix);
0246 }
0247
0248
0249
0250 const LLT<PlainObject, UpLo> llt() const;
0251 const LDLT<PlainObject, UpLo> ldlt() const;
0252
0253
0254
0255
0256 typedef typename NumTraits<Scalar>::Real RealScalar;
0257
0258 typedef Matrix<RealScalar, internal::traits<MatrixType>::ColsAtCompileTime, 1> EigenvaluesReturnType;
0259
0260 EIGEN_DEVICE_FUNC
0261 EigenvaluesReturnType eigenvalues() const;
0262 EIGEN_DEVICE_FUNC
0263 RealScalar operatorNorm() const;
0264
0265 protected:
0266 MatrixTypeNested m_matrix;
0267 };
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279 namespace internal {
0280
0281
0282
0283
0284 template<typename MatrixType, unsigned int Mode>
0285 struct evaluator_traits<SelfAdjointView<MatrixType,Mode> >
0286 {
0287 typedef typename storage_kind_to_evaluator_kind<typename MatrixType::StorageKind>::Kind Kind;
0288 typedef SelfAdjointShape Shape;
0289 };
0290
0291 template<int UpLo, int SetOpposite, typename DstEvaluatorTypeT, typename SrcEvaluatorTypeT, typename Functor, int Version>
0292 class triangular_dense_assignment_kernel<UpLo,SelfAdjoint,SetOpposite,DstEvaluatorTypeT,SrcEvaluatorTypeT,Functor,Version>
0293 : public generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version>
0294 {
0295 protected:
0296 typedef generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version> Base;
0297 typedef typename Base::DstXprType DstXprType;
0298 typedef typename Base::SrcXprType SrcXprType;
0299 using Base::m_dst;
0300 using Base::m_src;
0301 using Base::m_functor;
0302 public:
0303
0304 typedef typename Base::DstEvaluatorType DstEvaluatorType;
0305 typedef typename Base::SrcEvaluatorType SrcEvaluatorType;
0306 typedef typename Base::Scalar Scalar;
0307 typedef typename Base::AssignmentTraits AssignmentTraits;
0308
0309
0310 EIGEN_DEVICE_FUNC triangular_dense_assignment_kernel(DstEvaluatorType &dst, const SrcEvaluatorType &src, const Functor &func, DstXprType& dstExpr)
0311 : Base(dst, src, func, dstExpr)
0312 {}
0313
0314 EIGEN_DEVICE_FUNC void assignCoeff(Index row, Index col)
0315 {
0316 eigen_internal_assert(row!=col);
0317 Scalar tmp = m_src.coeff(row,col);
0318 m_functor.assignCoeff(m_dst.coeffRef(row,col), tmp);
0319 m_functor.assignCoeff(m_dst.coeffRef(col,row), numext::conj(tmp));
0320 }
0321
0322 EIGEN_DEVICE_FUNC void assignDiagonalCoeff(Index id)
0323 {
0324 Base::assignCoeff(id,id);
0325 }
0326
0327 EIGEN_DEVICE_FUNC void assignOppositeCoeff(Index, Index)
0328 { eigen_internal_assert(false && "should never be called"); }
0329 };
0330
0331 }
0332
0333
0334
0335
0336
0337
0338 template<typename Derived>
0339 template<unsigned int UpLo>
0340 EIGEN_DEVICE_FUNC typename MatrixBase<Derived>::template ConstSelfAdjointViewReturnType<UpLo>::Type
0341 MatrixBase<Derived>::selfadjointView() const
0342 {
0343 return typename ConstSelfAdjointViewReturnType<UpLo>::Type(derived());
0344 }
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355 template<typename Derived>
0356 template<unsigned int UpLo>
0357 EIGEN_DEVICE_FUNC typename MatrixBase<Derived>::template SelfAdjointViewReturnType<UpLo>::Type
0358 MatrixBase<Derived>::selfadjointView()
0359 {
0360 return typename SelfAdjointViewReturnType<UpLo>::Type(derived());
0361 }
0362
0363 }
0364
0365 #endif