compbio
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 
13 namespace Eigen {
14 
31 namespace internal {
32 template<typename MatrixType, unsigned int UpLo>
33 struct traits<SelfAdjointView<MatrixType, UpLo> > : traits<MatrixType>
34 {
35  typedef typename ref_selector<MatrixType>::non_const_type MatrixTypeNested;
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 ??
49 template<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 
62  typedef typename MatrixType::StorageIndex StorageIndex;
63 
64  enum {
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
118  {
119  return Product<SelfAdjointView,OtherDerived>(*this, rhs.derived());
120  }
121 
123  template<typename OtherDerived> friend
124  EIGEN_DEVICE_FUNC
126  operator*(const MatrixBase<OtherDerived>& lhs, const SelfAdjointView& rhs)
127  {
128  return Product<OtherDerived,SelfAdjointView>(lhs.derived(),rhs);
129  }
130 
131  friend EIGEN_DEVICE_FUNC
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
150  SelfAdjointView& rankUpdate(const MatrixBase<DerivedU>& u, const MatrixBase<DerivedV>& v, const Scalar& alpha = Scalar(1));
151 
162  template<typename DerivedU>
163  EIGEN_DEVICE_FUNC
164  SelfAdjointView& rankUpdate(const MatrixBase<DerivedU>& u, const Scalar& alpha = Scalar(1));
165 
176  template<unsigned int TriMode>
177  EIGEN_DEVICE_FUNC
178  typename internal::conditional<(TriMode&(Upper|Lower))==(UpLo&(Upper|Lower)),
182  {
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 
212 
213  EIGEN_DEVICE_FUNC
214  EigenvaluesReturnType eigenvalues() const;
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 
232 namespace 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)
237 template<typename MatrixType, unsigned int Mode>
238 struct evaluator_traits<SelfAdjointView<MatrixType,Mode> >
239 {
241  typedef SelfAdjointShape Shape;
242 };
243 
244 template<int UpLo, int SetOpposite, typename DstEvaluatorTypeT, typename SrcEvaluatorTypeT, typename Functor, int Version>
245 class triangular_dense_assignment_kernel<UpLo,SelfAdjoint,SetOpposite,DstEvaluatorTypeT,SrcEvaluatorTypeT,Functor,Version>
246  : public generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version>
247 {
248 protected:
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;
255 public:
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 
290 template<typename Derived>
291 template<unsigned int UpLo>
292 typename MatrixBase<Derived>::template ConstSelfAdjointViewReturnType<UpLo>::Type
294 {
295  return typename ConstSelfAdjointViewReturnType<UpLo>::Type(derived());
296 }
297 
298 template<typename Derived>
299 template<unsigned int UpLo>
300 typename MatrixBase<Derived>::template SelfAdjointViewReturnType<UpLo>::Type
302 {
303  return typename SelfAdjointViewReturnType<UpLo>::Type(derived());
304 }
305 
306 } // end namespace Eigen
307 
308 #endif // EIGEN_SELFADJOINTMATRIX_H
Robust Cholesky decomposition of a matrix with pivoting.
Definition: LDLT.h:50
Matrix< RealScalar, internal::traits< MatrixType >::ColsAtCompileTime, 1 > EigenvaluesReturnType
Return type of eigenvalues()
Definition: SelfAdjointView.h:211
Definition: NonLinearOptimization.cpp:108
Definition: AssignEvaluator.h:28
Definition: Constants.h:526
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:71
Base class for triangular part in a matrix.
Definition: TriangularMatrix.h:27
const unsigned int DirectAccessBit
Means that the underlying array of coefficients can be directly accessed as a plain strided array...
Definition: Constants.h:150
EIGEN_DEVICE_FUNC MatrixType::ConstDiagonalReturnType diagonal() const
Definition: SelfAdjointView.h:196
const unsigned int LvalueBit
Means the expression has a coeffRef() method, i.e.
Definition: Constants.h:139
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
Holds information about the various numeric (i.e.
Definition: NumTraits.h:150
NumTraits< Scalar >::Real RealScalar
Real part of Scalar.
Definition: SelfAdjointView.h:209
Definition: Meta.h:58
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:37
const unsigned int PacketAccessBit
Short version: means the expression might be vectorized.
Definition: Constants.h:89
internal::traits< SelfAdjointView >::Scalar Scalar
The type of coefficients in this matrix.
Definition: SelfAdjointView.h:61
View matrix as a lower triangular matrix.
Definition: Constants.h:204
Definition: Constants.h:518
Standard Cholesky decomposition (LL^T) of a matrix and associated features.
Definition: LLT.h:52
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Expression of a selfadjoint matrix from a triangular part of a dense matrix.
Definition: SelfAdjointView.h:49
Definition: XprHelper.h:396
Definition: TriangularMatrix.h:730
View matrix as an upper triangular matrix.
Definition: Constants.h:206
Definition: benchGeometry.cpp:23
Definition: BandTriangularSolver.h:13
Definition: CoreEvaluators.h:79
Used in BandMatrix and SelfAdjointView to indicate that the matrix is self-adjoint.
Definition: Constants.h:220
Expression of a triangular part in a matrix.
Definition: TriangularMatrix.h:186
Definition: AssignEvaluator.h:596
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:178
EIGEN_DEVICE_FUNC internal::conditional<(TriMode &(Upper|Lower))==(UpLo &(Upper|Lower)), TriangularView< MatrixType, TriMode >, TriangularView< typename MatrixType::AdjointReturnType, TriMode > >::type triangularView() const
Definition: SelfAdjointView.h:181
EIGEN_DEVICE_FUNC const Product< SelfAdjointView, OtherDerived > operator*(const MatrixBase< OtherDerived > &rhs) const
Efficient triangular matrix times vector/matrix product.
Definition: SelfAdjointView.h:117
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
const unsigned int LinearAccessBit
Short version: means the expression can be seen as 1D vector.
Definition: Constants.h:125
friend EIGEN_DEVICE_FUNC const Product< OtherDerived, SelfAdjointView > operator*(const MatrixBase< OtherDerived > &lhs, const SelfAdjointView &rhs)
Efficient vector/matrix times triangular matrix product.
Definition: SelfAdjointView.h:126
Definition: ForwardDeclarations.h:17
Definition: XprHelper.h:630
EIGEN_DEVICE_FUNC Scalar coeff(Index row, Index col) const
Definition: SelfAdjointView.h:87
EIGEN_DEVICE_FUNC Scalar & coeffRef(Index row, Index col)
Definition: SelfAdjointView.h:97