Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
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 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 template<typename MatrixType>
21 struct traits<SparseView<MatrixType> > : traits<MatrixType>
22 {
23  typedef typename MatrixType::StorageIndex StorageIndex;
24  typedef Sparse StorageKind;
25  enum {
26  Flags = int(traits<MatrixType>::Flags) & (RowMajorBit)
27  };
28 };
29 
30 } // end namespace internal
31 
46 template<typename MatrixType>
47 class SparseView : public SparseMatrixBase<SparseView<MatrixType> >
48 {
49  typedef typename MatrixType::Nested MatrixTypeNested;
50  typedef internal::remove_all_t<MatrixTypeNested> MatrixTypeNested_;
52 public:
53  EIGEN_SPARSE_PUBLIC_INTERFACE(SparseView)
54  typedef internal::remove_all_t<MatrixType> NestedExpression;
55 
56  explicit SparseView(const MatrixType& mat, const Scalar& reference = Scalar(0),
57  const RealScalar &epsilon = NumTraits<Scalar>::dummy_precision())
58  : m_matrix(mat), m_reference(reference), m_epsilon(epsilon) {}
59 
60  inline Index rows() const { return m_matrix.rows(); }
61  inline Index cols() const { return m_matrix.cols(); }
62 
63  inline Index innerSize() const { return m_matrix.innerSize(); }
64  inline Index outerSize() const { return m_matrix.outerSize(); }
65 
67  const internal::remove_all_t<MatrixTypeNested>&
68  nestedExpression() const { return m_matrix; }
69 
70  Scalar reference() const { return m_reference; }
71  RealScalar epsilon() const { return m_epsilon; }
72 
73 protected:
74  MatrixTypeNested m_matrix;
75  Scalar m_reference;
76  RealScalar m_epsilon;
77 };
78 
79 namespace internal {
80 
81 // TODO find a way to unify the two following variants
82 // This is tricky because implementing an inner iterator on top of an IndexBased evaluator is
83 // not easy because the evaluators do not expose the sizes of the underlying expression.
84 
85 template<typename ArgType>
86 struct unary_evaluator<SparseView<ArgType>, IteratorBased>
87  : public evaluator_base<SparseView<ArgType> >
88 {
89  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
90  public:
91  typedef SparseView<ArgType> XprType;
92 
93  class InnerIterator : public EvalIterator
94  {
95  protected:
96  typedef typename XprType::Scalar Scalar;
97  public:
98 
99  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& sve, Index outer)
100  : EvalIterator(sve.m_argImpl,outer), m_view(sve.m_view)
101  {
102  incrementToNonZero();
103  }
104 
105  EIGEN_STRONG_INLINE InnerIterator& operator++()
106  {
107  EvalIterator::operator++();
108  incrementToNonZero();
109  return *this;
110  }
111 
112  using EvalIterator::value;
113 
114  protected:
115  const XprType &m_view;
116 
117  private:
118  void incrementToNonZero()
119  {
120  while((bool(*this)) && internal::isMuchSmallerThan(value(), m_view.reference(), m_view.epsilon()))
121  {
122  EvalIterator::operator++();
123  }
124  }
125  };
126 
127  enum {
128  CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
129  Flags = XprType::Flags
130  };
131 
132  explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_view(xpr) {}
133 
134  protected:
135  evaluator<ArgType> m_argImpl;
136  const XprType &m_view;
137 };
138 
139 template<typename ArgType>
140 struct unary_evaluator<SparseView<ArgType>, IndexBased>
141  : public evaluator_base<SparseView<ArgType> >
142 {
143  public:
144  typedef SparseView<ArgType> XprType;
145  protected:
146  enum { IsRowMajor = (XprType::Flags&RowMajorBit)==RowMajorBit };
147  typedef typename XprType::Scalar Scalar;
148  typedef typename XprType::StorageIndex StorageIndex;
149  public:
150 
151  class InnerIterator
152  {
153  public:
154 
155  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& sve, Index outer)
156  : m_sve(sve), m_inner(0), m_outer(outer), m_end(sve.m_view.innerSize())
157  {
158  incrementToNonZero();
159  }
160 
161  EIGEN_STRONG_INLINE InnerIterator& operator++()
162  {
163  m_inner++;
164  incrementToNonZero();
165  return *this;
166  }
167 
168  EIGEN_STRONG_INLINE Scalar value() const
169  {
170  return (IsRowMajor) ? m_sve.m_argImpl.coeff(m_outer, m_inner)
171  : m_sve.m_argImpl.coeff(m_inner, m_outer);
172  }
173 
174  EIGEN_STRONG_INLINE StorageIndex index() const { return m_inner; }
175  inline Index row() const { return IsRowMajor ? m_outer : index(); }
176  inline Index col() const { return IsRowMajor ? index() : m_outer; }
177 
178  EIGEN_STRONG_INLINE operator bool() const { return m_inner < m_end && m_inner>=0; }
179 
180  protected:
181  const unary_evaluator &m_sve;
182  Index m_inner;
183  const Index m_outer;
184  const Index m_end;
185 
186  private:
187  void incrementToNonZero()
188  {
189  while((bool(*this)) && internal::isMuchSmallerThan(value(), m_sve.m_view.reference(), m_sve.m_view.epsilon()))
190  {
191  m_inner++;
192  }
193  }
194  };
195 
196  enum {
197  CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
198  Flags = XprType::Flags
199  };
200 
201  explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_view(xpr) {}
202 
203  protected:
204  evaluator<ArgType> m_argImpl;
205  const XprType &m_view;
206 };
207 
208 } // end namespace internal
209 
227 template<typename Derived>
229  const typename NumTraits<Scalar>::Real& epsilon) const
230 {
231  return SparseView<Derived>(derived(), reference, epsilon);
232 }
233 
246 template<typename Derived>
248 SparseMatrixBase<Derived>::pruned(const Scalar& reference,
249  const RealScalar& epsilon) const
250 {
251  return SparseView<Derived>(derived(), reference, epsilon);
252 }
253 
254 } // end namespace Eigen
255 
256 #endif
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:61
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:248
Expression of a dense or sparse matrix with zero or too small values removed.
Definition: SparseView.h:48
const internal::remove_all_t< MatrixTypeNested > & nestedExpression() const
Definition: SparseView.h:68
const SparseView< Derived > sparseView(const Scalar &m_reference=Scalar(0), const typename NumTraits< Scalar >::Real &m_epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:228
const unsigned int RowMajorBit
Definition: Constants.h:68
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231