SparseView.h
Go to the documentation of this file.
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;
26 };
27 
28 } // end namespace internal
29 
44 template <typename MatrixType>
45 class SparseView : public SparseMatrixBase<SparseView<MatrixType> > {
46  typedef typename MatrixType::Nested MatrixTypeNested;
49 
50  public:
53 
54  explicit SparseView(const MatrixType& mat, const Scalar& reference = Scalar(0),
55  const RealScalar& epsilon = NumTraits<Scalar>::dummy_precision())
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 
66 
67  Scalar reference() const { return m_reference; }
68  RealScalar epsilon() const { return m_epsilon; }
69 
70  protected:
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> > {
85 
86  public:
88 
89  class InnerIterator : public EvalIterator {
90  protected:
91  typedef typename XprType::Scalar Scalar;
92 
93  public:
95  : EvalIterator(sve.m_argImpl, outer), m_view(sve.m_view) {
96  incrementToNonZero();
97  }
98 
101  incrementToNonZero();
102  return *this;
103  }
104 
105  using EvalIterator::value;
106 
107  protected:
108  const XprType& m_view;
109 
110  private:
112  while ((bool(*this)) && internal::isMuchSmallerThan(value(), m_view.reference(), m_view.epsilon())) {
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:
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:
131 
132  protected:
133  enum { IsRowMajor = (XprType::Flags & RowMajorBit) == RowMajorBit };
134  typedef typename XprType::Scalar Scalar;
136 
137  public:
139  public:
141  : m_sve(sve), m_inner(0), m_outer(outer), m_end(sve.m_view.innerSize()) {
142  incrementToNonZero();
143  }
144 
146  m_inner++;
147  incrementToNonZero();
148  return *this;
149  }
150 
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:
164  const Index m_outer;
165  const Index m_end;
166 
167  private:
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:
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>
220  return SparseView<Derived>(derived(), reference, epsilon);
221 }
222 
223 } // end namespace Eigen
224 
225 #endif
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
#define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
Definition: SparseUtil.h:39
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:62
An InnerIterator allows to loop over the element of any matrix expression.
Definition: CoreIterators.h:37
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
internal::traits< SparseView< MatrixType > >::StorageIndex StorageIndex
Definition: SparseMatrixBase.h:44
internal::traits< SparseView< MatrixType > >::Scalar Scalar
Definition: SparseMatrixBase.h:32
NumTraits< Scalar >::Real RealScalar
Definition: SparseMatrixBase.h:128
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:219
Expression of a dense or sparse matrix with zero or too small values removed.
Definition: SparseView.h:45
SparseMatrixBase< SparseView > Base
Definition: SparseView.h:48
internal::remove_all_t< MatrixType > NestedExpression
Definition: SparseView.h:52
Scalar reference() const
Definition: SparseView.h:67
MatrixType::Nested MatrixTypeNested
Definition: SparseView.h:46
Scalar m_reference
Definition: SparseView.h:72
const internal::remove_all_t< MatrixTypeNested > & nestedExpression() const
Definition: SparseView.h:65
internal::remove_all_t< MatrixTypeNested > MatrixTypeNested_
Definition: SparseView.h:47
Index outerSize() const
Definition: SparseView.h:62
Index innerSize() const
Definition: SparseView.h:61
RealScalar m_epsilon
Definition: SparseView.h:73
Index cols() const
Definition: SparseView.h:59
Index rows() const
Definition: SparseView.h:58
MatrixTypeNested m_matrix
Definition: SparseView.h:71
RealScalar epsilon() const
Definition: SparseView.h:68
EIGEN_STRONG_INLINE InnerIterator & operator++()
Definition: SparseView.h:145
EIGEN_STRONG_INLINE Scalar value() const
Definition: SparseView.h:151
EIGEN_STRONG_INLINE StorageIndex index() const
Definition: SparseView.h:155
EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator &sve, Index outer)
Definition: SparseView.h:140
EIGEN_STRONG_INLINE InnerIterator & operator++()
Definition: SparseView.h:99
EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator &sve, Index outer)
Definition: SparseView.h:94
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
const unsigned int RowMajorBit
Definition: Constants.h:70
return int(ret)+1
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 operator++(bfloat16 &a)
Definition: BFloat16.h:307
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: MathFunctions.h:1916
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
squared absolute value
Definition: GlobalFunctions.h:87
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
Definition: Eigen_Colamd.h:49
double epsilon
Definition: osc_ring_sarah_asymptotics.h:43
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:43
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: Constants.h:522
Definition: Constants.h:577
Definition: Constants.h:580
Definition: CoreEvaluators.h:118
Definition: CoreEvaluators.h:104
MatrixType::StorageIndex StorageIndex
Definition: SparseView.h:23
Sparse StorageKind
Definition: SparseView.h:24
Definition: ForwardDeclarations.h:21
XprType::StorageIndex StorageIndex
Definition: SparseView.h:135
evaluator< ArgType > m_argImpl
Definition: SparseView.h:180
SparseView< ArgType > XprType
Definition: SparseView.h:130
unary_evaluator(const XprType &xpr)
Definition: SparseView.h:177
evaluator< ArgType > m_argImpl
Definition: SparseView.h:123
SparseView< ArgType > XprType
Definition: SparseView.h:87
evaluator< ArgType >::InnerIterator EvalIterator
Definition: SparseView.h:84
unary_evaluator(const XprType &xpr)
Definition: SparseView.h:120
Definition: CoreEvaluators.h:82