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