compbio
SparseTriangularView.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2012 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_SPARSE_TRIANGULARVIEW_H
12 #define EIGEN_SPARSE_TRIANGULARVIEW_H
13 
14 namespace Eigen {
15 
25 template<typename MatrixType, unsigned int Mode> class TriangularViewImpl<MatrixType,Mode,Sparse>
26  : public SparseMatrixBase<TriangularView<MatrixType,Mode> >
27 {
28  enum { SkipFirst = ((Mode&Lower) && !(MatrixType::Flags&RowMajorBit))
29  || ((Mode&Upper) && (MatrixType::Flags&RowMajorBit)),
30  SkipLast = !SkipFirst,
31  SkipDiag = (Mode&ZeroDiag) ? 1 : 0,
32  HasUnitDiag = (Mode&UnitDiag) ? 1 : 0
33  };
34 
36 
37  protected:
38  // dummy solve function to make TriangularView happy.
39  void solve() const;
40 
42  public:
43 
44  EIGEN_SPARSE_PUBLIC_INTERFACE(TriangularViewType)
45 
46  typedef typename MatrixType::Nested MatrixTypeNested;
49 
50  template<typename RhsType, typename DstType>
51  EIGEN_DEVICE_FUNC
52  EIGEN_STRONG_INLINE void _solve_impl(const RhsType &rhs, DstType &dst) const {
53  if(!(internal::is_same<RhsType,DstType>::value && internal::extract_data(dst) == internal::extract_data(rhs)))
54  dst = rhs;
55  this->solveInPlace(dst);
56  }
57 
58  template<typename OtherDerived> void solveInPlace(MatrixBase<OtherDerived>& other) const;
59  template<typename OtherDerived> void solveInPlace(SparseMatrixBase<OtherDerived>& other) const;
60 
61 };
62 
63 namespace internal {
64 
65 template<typename ArgType, unsigned int Mode>
67  : evaluator_base<TriangularView<ArgType,Mode> >
68 {
70 
71 protected:
72 
73  typedef typename XprType::Scalar Scalar;
74  typedef typename XprType::StorageIndex StorageIndex;
75  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
76 
77  enum { SkipFirst = ((Mode&Lower) && !(ArgType::Flags&RowMajorBit))
78  || ((Mode&Upper) && (ArgType::Flags&RowMajorBit)),
79  SkipLast = !SkipFirst,
80  SkipDiag = (Mode&ZeroDiag) ? 1 : 0,
81  HasUnitDiag = (Mode&UnitDiag) ? 1 : 0
82  };
83 
84 public:
85 
86  enum {
87  CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
88  Flags = XprType::Flags
89  };
90 
91  explicit unary_evaluator(const XprType &xpr) : m_argImpl(xpr.nestedExpression()), m_arg(xpr.nestedExpression()) {}
92 
93  inline Index nonZerosEstimate() const {
94  return m_argImpl.nonZerosEstimate();
95  }
96 
97  class InnerIterator : public EvalIterator
98  {
99  typedef EvalIterator Base;
100  public:
101 
102  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& xprEval, Index outer)
103  : Base(xprEval.m_argImpl,outer), m_returnOne(false), m_containsDiag(Base::outer()<xprEval.m_arg.innerSize())
104  {
105  if(SkipFirst)
106  {
107  while((*this) && ((HasUnitDiag||SkipDiag) ? this->index()<=outer : this->index()<outer))
108  Base::operator++();
109  if(HasUnitDiag)
110  m_returnOne = m_containsDiag;
111  }
112  else if(HasUnitDiag && ((!Base::operator bool()) || Base::index()>=Base::outer()))
113  {
114  if((!SkipFirst) && Base::operator bool())
115  Base::operator++();
116  m_returnOne = m_containsDiag;
117  }
118  }
119 
120  EIGEN_STRONG_INLINE InnerIterator& operator++()
121  {
122  if(HasUnitDiag && m_returnOne)
123  m_returnOne = false;
124  else
125  {
126  Base::operator++();
127  if(HasUnitDiag && (!SkipFirst) && ((!Base::operator bool()) || Base::index()>=Base::outer()))
128  {
129  if((!SkipFirst) && Base::operator bool())
130  Base::operator++();
131  m_returnOne = m_containsDiag;
132  }
133  }
134  return *this;
135  }
136 
137  EIGEN_STRONG_INLINE operator bool() const
138  {
139  if(HasUnitDiag && m_returnOne)
140  return true;
141  if(SkipFirst) return Base::operator bool();
142  else
143  {
144  if (SkipDiag) return (Base::operator bool() && this->index() < this->outer());
145  else return (Base::operator bool() && this->index() <= this->outer());
146  }
147  }
148 
149 // inline Index row() const { return (ArgType::Flags&RowMajorBit ? Base::outer() : this->index()); }
150 // inline Index col() const { return (ArgType::Flags&RowMajorBit ? this->index() : Base::outer()); }
151  inline StorageIndex index() const
152  {
153  if(HasUnitDiag && m_returnOne) return internal::convert_index<StorageIndex>(Base::outer());
154  else return Base::index();
155  }
156  inline Scalar value() const
157  {
158  if(HasUnitDiag && m_returnOne) return Scalar(1);
159  else return Base::value();
160  }
161 
162  protected:
163  bool m_returnOne;
164  bool m_containsDiag;
165  private:
166  Scalar& valueRef();
167  };
168 
169 protected:
170  evaluator<ArgType> m_argImpl;
171  const ArgType& m_arg;
172 };
173 
174 } // end namespace internal
175 
176 template<typename Derived>
177 template<int Mode>
180 {
181  return TriangularView<const Derived, Mode>(derived());
182 }
183 
184 } // end namespace Eigen
185 
186 #endif // EIGEN_SPARSE_TRIANGULARVIEW_H
Definition: Meta.h:63
Definition: CoreEvaluators.h:90
Definition: CoreEvaluators.h:65
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition: Constants.h:61
Definition: Constants.h:529
Definition: CoreEvaluators.h:109
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:281
View matrix as a lower triangular matrix.
Definition: Constants.h:204
Matrix has ones on the diagonal; to be used in combination with Lower or Upper.
Definition: Constants.h:208
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
The type used to identify a general sparse storage.
Definition: Constants.h:494
View matrix as an upper triangular matrix.
Definition: Constants.h:206
Matrix has zeros on the diagonal; to be used in combination with Lower or Upper.
Definition: Constants.h:210
Definition: BandTriangularSolver.h:13
Definition: TriangularMatrix.h:184
Expression of a triangular part in a matrix.
Definition: TriangularMatrix.h:186
EIGEN_DEVICE_FUNC const NestedExpression & nestedExpression() const
Definition: TriangularMatrix.h:233
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
An InnerIterator allows to loop over the element of any matrix expression.
Definition: CoreIterators.h:33