Eigen  3.3.0
 
Loading...
Searching...
No Matches
SelfAdjointView.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_SELFADJOINTMATRIX_H
11#define EIGEN_SELFADJOINTMATRIX_H
12
13namespace Eigen {
14
31namespace internal {
32template<typename MatrixType, unsigned int UpLo>
33struct traits<SelfAdjointView<MatrixType, UpLo> > : traits<MatrixType>
34{
35 typedef typename ref_selector<MatrixType>::non_const_type MatrixTypeNested;
36 typedef typename remove_all<MatrixTypeNested>::type MatrixTypeNestedCleaned;
37 typedef MatrixType ExpressionType;
38 typedef typename MatrixType::PlainObject FullMatrixType;
39 enum {
40 Mode = UpLo | SelfAdjoint,
41 FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
42 Flags = MatrixTypeNestedCleaned::Flags & (HereditaryBits|FlagsLvalueBit)
43 & (~(PacketAccessBit | DirectAccessBit | LinearAccessBit)) // FIXME these flags should be preserved
44 };
45};
46}
47
48// FIXME could also be called SelfAdjointWrapper to be consistent with DiagonalWrapper ??
49template<typename _MatrixType, unsigned int UpLo> class SelfAdjointView
50 : public TriangularBase<SelfAdjointView<_MatrixType, UpLo> >
51{
52 public:
53
54 typedef _MatrixType MatrixType;
56 typedef typename internal::traits<SelfAdjointView>::MatrixTypeNested MatrixTypeNested;
57 typedef typename internal::traits<SelfAdjointView>::MatrixTypeNestedCleaned MatrixTypeNestedCleaned;
58 typedef MatrixTypeNestedCleaned NestedExpression;
59
61 typedef typename internal::traits<SelfAdjointView>::Scalar Scalar;
62 typedef typename MatrixType::StorageIndex StorageIndex;
63
64 enum {
65 Mode = internal::traits<SelfAdjointView>::Mode,
66 Flags = internal::traits<SelfAdjointView>::Flags
67 };
68 typedef typename MatrixType::PlainObject PlainObject;
69
70 EIGEN_DEVICE_FUNC
71 explicit inline SelfAdjointView(MatrixType& matrix) : m_matrix(matrix)
72 {}
73
74 EIGEN_DEVICE_FUNC
75 inline Index rows() const { return m_matrix.rows(); }
76 EIGEN_DEVICE_FUNC
77 inline Index cols() const { return m_matrix.cols(); }
78 EIGEN_DEVICE_FUNC
79 inline Index outerStride() const { return m_matrix.outerStride(); }
80 EIGEN_DEVICE_FUNC
81 inline Index innerStride() const { return m_matrix.innerStride(); }
82
86 EIGEN_DEVICE_FUNC
87 inline Scalar coeff(Index row, Index col) const
88 {
89 Base::check_coordinates_internal(row, col);
90 return m_matrix.coeff(row, col);
91 }
92
96 EIGEN_DEVICE_FUNC
97 inline Scalar& coeffRef(Index row, Index col)
98 {
99 EIGEN_STATIC_ASSERT_LVALUE(SelfAdjointView);
100 Base::check_coordinates_internal(row, col);
101 return m_matrix.coeffRef(row, col);
102 }
103
105 EIGEN_DEVICE_FUNC
106 const MatrixTypeNestedCleaned& _expression() const { return m_matrix; }
107
108 EIGEN_DEVICE_FUNC
109 const MatrixTypeNestedCleaned& nestedExpression() const { return m_matrix; }
110 EIGEN_DEVICE_FUNC
111 MatrixTypeNestedCleaned& nestedExpression() { return m_matrix; }
112
114 template<typename OtherDerived>
115 EIGEN_DEVICE_FUNC
116 const Product<SelfAdjointView,OtherDerived>
118 {
120 }
121
123 template<typename OtherDerived> friend
124 EIGEN_DEVICE_FUNC
127 {
129 }
130
131 friend EIGEN_DEVICE_FUNC
132 const SelfAdjointView<const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar,MatrixType,product),UpLo>
133 operator*(const Scalar& s, const SelfAdjointView& mat)
134 {
135 return (s*mat.nestedExpression()).template selfadjointView<UpLo>();
136 }
137
148 template<typename DerivedU, typename DerivedV>
149 EIGEN_DEVICE_FUNC
151
162 template<typename DerivedU>
163 EIGEN_DEVICE_FUNC
165
176 template<unsigned int TriMode>
177 EIGEN_DEVICE_FUNC
178 typename internal::conditional<(TriMode&(Upper|Lower))==(UpLo&(Upper|Lower)),
182 {
183 typename internal::conditional<(TriMode&(Upper|Lower))==(UpLo&(Upper|Lower)), MatrixType&, typename MatrixType::ConstTransposeReturnType>::type tmp1(m_matrix);
184 typename internal::conditional<(TriMode&(Upper|Lower))==(UpLo&(Upper|Lower)), MatrixType&, typename MatrixType::AdjointReturnType>::type tmp2(tmp1);
185 return typename internal::conditional<(TriMode&(Upper|Lower))==(UpLo&(Upper|Lower)),
188 }
189
195 EIGEN_DEVICE_FUNC
196 typename MatrixType::ConstDiagonalReturnType diagonal() const
197 {
198 return typename MatrixType::ConstDiagonalReturnType(m_matrix);
199 }
200
202
203 const LLT<PlainObject, UpLo> llt() const;
204 const LDLT<PlainObject, UpLo> ldlt() const;
205
207
209 typedef typename NumTraits<Scalar>::Real RealScalar;
212
213 EIGEN_DEVICE_FUNC
215 EIGEN_DEVICE_FUNC
216 RealScalar operatorNorm() const;
217
218 protected:
219 MatrixTypeNested m_matrix;
220};
221
222
223// template<typename OtherDerived, typename MatrixType, unsigned int UpLo>
224// internal::selfadjoint_matrix_product_returntype<OtherDerived,SelfAdjointView<MatrixType,UpLo> >
225// operator*(const MatrixBase<OtherDerived>& lhs, const SelfAdjointView<MatrixType,UpLo>& rhs)
226// {
227// return internal::matrix_selfadjoint_product_returntype<OtherDerived,SelfAdjointView<MatrixType,UpLo> >(lhs.derived(),rhs);
228// }
229
230// selfadjoint to dense matrix
231
232namespace internal {
233
234// TODO currently a selfadjoint expression has the form SelfAdjointView<.,.>
235// in the future selfadjoint-ness should be defined by the expression traits
236// such that Transpose<SelfAdjointView<.,.> > is valid. (currently TriangularBase::transpose() is overloaded to make it work)
237template<typename MatrixType, unsigned int Mode>
238struct evaluator_traits<SelfAdjointView<MatrixType,Mode> >
239{
240 typedef typename storage_kind_to_evaluator_kind<typename MatrixType::StorageKind>::Kind Kind;
241 typedef SelfAdjointShape Shape;
242};
243
244template<int UpLo, int SetOpposite, typename DstEvaluatorTypeT, typename SrcEvaluatorTypeT, typename Functor, int Version>
245class triangular_dense_assignment_kernel<UpLo,SelfAdjoint,SetOpposite,DstEvaluatorTypeT,SrcEvaluatorTypeT,Functor,Version>
246 : public generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version>
247{
248protected:
249 typedef generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version> Base;
250 typedef typename Base::DstXprType DstXprType;
251 typedef typename Base::SrcXprType SrcXprType;
252 using Base::m_dst;
253 using Base::m_src;
254 using Base::m_functor;
255public:
256
257 typedef typename Base::DstEvaluatorType DstEvaluatorType;
258 typedef typename Base::SrcEvaluatorType SrcEvaluatorType;
259 typedef typename Base::Scalar Scalar;
260 typedef typename Base::AssignmentTraits AssignmentTraits;
261
262
263 EIGEN_DEVICE_FUNC triangular_dense_assignment_kernel(DstEvaluatorType &dst, const SrcEvaluatorType &src, const Functor &func, DstXprType& dstExpr)
264 : Base(dst, src, func, dstExpr)
265 {}
266
267 EIGEN_DEVICE_FUNC void assignCoeff(Index row, Index col)
268 {
269 eigen_internal_assert(row!=col);
270 Scalar tmp = m_src.coeff(row,col);
271 m_functor.assignCoeff(m_dst.coeffRef(row,col), tmp);
272 m_functor.assignCoeff(m_dst.coeffRef(col,row), numext::conj(tmp));
273 }
274
275 EIGEN_DEVICE_FUNC void assignDiagonalCoeff(Index id)
276 {
277 Base::assignCoeff(id,id);
278 }
279
280 EIGEN_DEVICE_FUNC void assignOppositeCoeff(Index, Index)
281 { eigen_internal_assert(false && "should never be called"); }
282};
283
284} // end namespace internal
285
286/***************************************************************************
287* Implementation of MatrixBase methods
288***************************************************************************/
289
290template<typename Derived>
291template<unsigned int UpLo>
292typename MatrixBase<Derived>::template ConstSelfAdjointViewReturnType<UpLo>::Type
293MatrixBase<Derived>::selfadjointView() const
294{
295 return typename ConstSelfAdjointViewReturnType<UpLo>::Type(derived());
296}
297
298template<typename Derived>
299template<unsigned int UpLo>
300typename MatrixBase<Derived>::template SelfAdjointViewReturnType<UpLo>::Type
301MatrixBase<Derived>::selfadjointView()
302{
303 return typename SelfAdjointViewReturnType<UpLo>::Type(derived());
304}
305
306} // end namespace Eigen
307
308#endif // EIGEN_SELFADJOINTMATRIX_H
Derived & derived()
Definition: EigenBase.h:44
Robust Cholesky decomposition of a matrix with pivoting.
Definition: LDLT.h:51
Standard Cholesky decomposition (LL^T) of a matrix and associated features.
Definition: LLT.h:53
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:50
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:180
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:75
Expression of a selfadjoint matrix from a triangular part of a dense matrix.
Definition: SelfAdjointView.h:51
internal::conditional<(TriMode &(Upper|Lower))==(UpLo &(Upper|Lower)), TriangularView< MatrixType, TriMode >, TriangularView< typenameMatrixType::AdjointReturnType, TriMode > >::type triangularView() const
Definition: SelfAdjointView.h:181
RealScalar operatorNorm() const
Computes the L2 operator norm.
Definition: MatrixBaseEigenvalues.h:153
const LLT< PlainObject, UpLo > llt() const
Definition: LLT.h:527
const LDLT< PlainObject, UpLo > ldlt() const
Definition: LDLT.h:651
SelfAdjointView & rankUpdate(const MatrixBase< DerivedU > &u, const Scalar &alpha=Scalar(1))
Matrix< RealScalar, internal::traits< MatrixType >::ColsAtCompileTime, 1 > EigenvaluesReturnType
Definition: SelfAdjointView.h:211
MatrixType::ConstDiagonalReturnType diagonal() const
Definition: SelfAdjointView.h:196
Scalar coeff(Index row, Index col) const
Definition: SelfAdjointView.h:87
const Product< SelfAdjointView, OtherDerived > operator*(const MatrixBase< OtherDerived > &rhs) const
Definition: SelfAdjointView.h:117
EigenvaluesReturnType eigenvalues() const
Computes the eigenvalues of a matrix.
Definition: MatrixBaseEigenvalues.h:89
friend const Product< OtherDerived, SelfAdjointView > operator*(const MatrixBase< OtherDerived > &lhs, const SelfAdjointView &rhs)
Definition: SelfAdjointView.h:126
Scalar & coeffRef(Index row, Index col)
Definition: SelfAdjointView.h:97
internal::traits< SelfAdjointView >::Scalar Scalar
The type of coefficients in this matrix.
Definition: SelfAdjointView.h:61
SelfAdjointView & rankUpdate(const MatrixBase< DerivedU > &u, const MatrixBase< DerivedV > &v, const Scalar &alpha=Scalar(1))
NumTraits< Scalar >::Real RealScalar
Definition: SelfAdjointView.h:209
Base class for triangular part in a matrix.
Definition: TriangularMatrix.h:28
Expression of a triangular part in a matrix.
Definition: TriangularMatrix.h:188
@ SelfAdjoint
Definition: Constants.h:220
@ Lower
Definition: Constants.h:204
@ Upper
Definition: Constants.h:206
const unsigned int PacketAccessBit
Definition: Constants.h:89
const unsigned int LinearAccessBit
Definition: Constants.h:125
const unsigned int DirectAccessBit
Definition: Constants.h:150
const unsigned int LvalueBit
Definition: Constants.h:139
Namespace containing all symbols from the Eigen library.
Definition: Core:287
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:37