Eigen  3.2.91
SparseView.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2011-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2010 Daniel Lowengrub <lowdanie@gmail.com>
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_SPARSEVIEW_H
12 #define EIGEN_SPARSEVIEW_H
13 
14 namespace Eigen {
15 
16 namespace internal {
17 
18 template<typename MatrixType>
19 struct traits<SparseView<MatrixType> > : traits<MatrixType>
20 {
21  typedef typename MatrixType::StorageIndex StorageIndex;
22  typedef Sparse StorageKind;
23  enum {
24  Flags = int(traits<MatrixType>::Flags) & (RowMajorBit)
25  };
26 };
27 
28 } // end namespace internal
29 
30 template<typename MatrixType>
31 class SparseView : public SparseMatrixBase<SparseView<MatrixType> >
32 {
33  typedef typename MatrixType::Nested MatrixTypeNested;
34  typedef typename internal::remove_all<MatrixTypeNested>::type _MatrixTypeNested;
35 public:
36  EIGEN_SPARSE_PUBLIC_INTERFACE(SparseView)
37  typedef typename internal::remove_all<MatrixType>::type NestedExpression;
38 
39  explicit SparseView(const MatrixType& mat, const Scalar& reference = Scalar(0),
40  RealScalar epsilon = NumTraits<Scalar>::dummy_precision())
41  : m_matrix(mat), m_reference(reference), m_epsilon(epsilon) {}
42 
43  inline Index rows() const { return m_matrix.rows(); }
44  inline Index cols() const { return m_matrix.cols(); }
45 
46  inline Index innerSize() const { return m_matrix.innerSize(); }
47  inline Index outerSize() const { return m_matrix.outerSize(); }
48 
50  const typename internal::remove_all<MatrixTypeNested>::type&
51  nestedExpression() const { return m_matrix; }
52 
53  Scalar reference() const { return m_reference; }
54  RealScalar epsilon() const { return m_epsilon; }
55 
56 protected:
57  MatrixTypeNested m_matrix;
58  Scalar m_reference;
59  RealScalar m_epsilon;
60 };
61 
62 namespace internal {
63 
64 // TODO find a way to unify the two following variants
65 // This is tricky because implementing an inner iterator on top of an IndexBased evaluator is
66 // not easy because the evaluators do not expose the sizes of the underlying expression.
67 
68 template<typename ArgType>
69 struct unary_evaluator<SparseView<ArgType>, IteratorBased>
70  : public evaluator_base<SparseView<ArgType> >
71 {
72  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
73  public:
74  typedef SparseView<ArgType> XprType;
75 
76  class InnerIterator : public EvalIterator
77  {
78  typedef typename XprType::Scalar Scalar;
79  public:
80 
81  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& sve, Index outer)
82  : EvalIterator(sve.m_argImpl,outer), m_view(sve.m_view)
83  {
84  incrementToNonZero();
85  }
86 
87  EIGEN_STRONG_INLINE InnerIterator& operator++()
88  {
89  EvalIterator::operator++();
90  incrementToNonZero();
91  return *this;
92  }
93 
94  using EvalIterator::value;
95 
96  protected:
97  const XprType &m_view;
98 
99  private:
100  void incrementToNonZero()
101  {
102  while((bool(*this)) && internal::isMuchSmallerThan(value(), m_view.reference(), m_view.epsilon()))
103  {
104  EvalIterator::operator++();
105  }
106  }
107  };
108 
109  enum {
110  CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
111  Flags = XprType::Flags
112  };
113 
114  explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_view(xpr) {}
115 
116  protected:
117  evaluator<ArgType> m_argImpl;
118  const XprType &m_view;
119 };
120 
121 template<typename ArgType>
122 struct unary_evaluator<SparseView<ArgType>, IndexBased>
123  : public evaluator_base<SparseView<ArgType> >
124 {
125  public:
126  typedef SparseView<ArgType> XprType;
127  protected:
128  enum { IsRowMajor = (XprType::Flags&RowMajorBit)==RowMajorBit };
129  typedef typename XprType::Scalar Scalar;
130  typedef typename XprType::StorageIndex StorageIndex;
131  public:
132 
133  class InnerIterator
134  {
135  public:
136 
137  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& sve, Index outer)
138  : m_sve(sve), m_inner(0), m_outer(outer), m_end(sve.m_view.innerSize())
139  {
140  incrementToNonZero();
141  }
142 
143  EIGEN_STRONG_INLINE InnerIterator& operator++()
144  {
145  m_inner++;
146  incrementToNonZero();
147  return *this;
148  }
149 
150  EIGEN_STRONG_INLINE Scalar value() const
151  {
152  return (IsRowMajor) ? m_sve.m_argImpl.coeff(m_outer, m_inner)
153  : m_sve.m_argImpl.coeff(m_inner, m_outer);
154  }
155 
156  EIGEN_STRONG_INLINE StorageIndex index() const { return m_inner; }
157  inline Index row() const { return IsRowMajor ? m_outer : index(); }
158  inline Index col() const { return IsRowMajor ? index() : m_outer; }
159 
160  EIGEN_STRONG_INLINE operator bool() const { return m_inner < m_end && m_inner>=0; }
161 
162  protected:
163  const unary_evaluator &m_sve;
164  Index m_inner;
165  const Index m_outer;
166  const Index m_end;
167 
168  private:
169  void incrementToNonZero()
170  {
171  while((bool(*this)) && internal::isMuchSmallerThan(value(), m_sve.m_view.reference(), m_sve.m_view.epsilon()))
172  {
173  m_inner++;
174  }
175  }
176  };
177 
178  enum {
179  CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
180  Flags = XprType::Flags
181  };
182 
183  explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_view(xpr) {}
184 
185  protected:
186  evaluator<ArgType> m_argImpl;
187  const XprType &m_view;
188 };
189 
190 } // end namespace internal
191 
192 template<typename Derived>
193 const SparseView<Derived> MatrixBase<Derived>::sparseView(const Scalar& reference,
194  const typename NumTraits<Scalar>::Real& epsilon) const
195 {
196  return SparseView<Derived>(derived(), reference, epsilon);
197 }
198 
211 template<typename Derived>
212 const SparseView<Derived>
213 SparseMatrixBase<Derived>::pruned(const Scalar& reference,
214  const RealScalar& epsilon) const
215 {
216  return SparseView<Derived>(derived(), reference, epsilon);
217 }
218 
219 } // end namespace Eigen
220 
221 #endif
Definition: LDLT.h:16
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:213
const unsigned int RowMajorBit
Definition: Constants.h:53
Definition: Eigen_Colamd.h:54