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 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;
23  enum {
25  };
26 };
27 
28 } // end namespace internal
29 
44 template<typename MatrixType>
45 class SparseView : public SparseMatrixBase<SparseView<MatrixType> >
46 {
47  typedef typename MatrixType::Nested MatrixTypeNested;
50 public:
52  typedef typename internal::remove_all<MatrixType>::type NestedExpression;
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  nestedExpression() const { return m_matrix; }
67 
68  Scalar reference() const { return m_reference; }
69  RealScalar epsilon() const { return m_epsilon; }
70 
71 protected:
75 };
76 
77 namespace internal {
78 
79 // TODO find a way to unify the two following variants
80 // This is tricky because implementing an inner iterator on top of an IndexBased evaluator is
81 // not easy because the evaluators do not expose the sizes of the underlying expression.
82 
83 template<typename ArgType>
85  : public evaluator_base<SparseView<ArgType> >
86 {
88  public:
90 
91  class InnerIterator : public EvalIterator
92  {
93  protected:
94  typedef typename XprType::Scalar Scalar;
95  public:
96 
98  : EvalIterator(sve.m_argImpl,outer), m_view(sve.m_view)
99  {
100  incrementToNonZero();
101  }
102 
104  {
106  incrementToNonZero();
107  return *this;
108  }
109 
110  using EvalIterator::value;
111 
112  protected:
113  const XprType &m_view;
114 
115  private:
117  {
118  while((bool(*this)) && internal::isMuchSmallerThan(value(), m_view.reference(), m_view.epsilon()))
119  {
121  }
122  }
123  };
124 
125  enum {
127  Flags = XprType::Flags
128  };
129 
130  explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_view(xpr) {}
131 
132  protected:
134  const XprType &m_view;
135 };
136 
137 template<typename ArgType>
139  : public evaluator_base<SparseView<ArgType> >
140 {
141  public:
143  protected:
144  enum { IsRowMajor = (XprType::Flags&RowMajorBit)==RowMajorBit };
145  typedef typename XprType::Scalar Scalar;
146  typedef typename XprType::StorageIndex StorageIndex;
147  public:
148 
150  {
151  public:
152 
154  : m_sve(sve), m_inner(0), m_outer(outer), m_end(sve.m_view.innerSize())
155  {
156  incrementToNonZero();
157  }
158 
160  {
161  m_inner++;
162  incrementToNonZero();
163  return *this;
164  }
165 
167  {
168  return (IsRowMajor) ? m_sve.m_argImpl.coeff(m_outer, m_inner)
169  : m_sve.m_argImpl.coeff(m_inner, m_outer);
170  }
171 
172  EIGEN_STRONG_INLINE StorageIndex index() const { return m_inner; }
173  inline Index row() const { return IsRowMajor ? m_outer : index(); }
174  inline Index col() const { return IsRowMajor ? index() : m_outer; }
175 
176  EIGEN_STRONG_INLINE operator bool() const { return m_inner < m_end && m_inner>=0; }
177 
178  protected:
181  const Index m_outer;
182  const Index m_end;
183 
184  private:
186  {
187  while((bool(*this)) && internal::isMuchSmallerThan(value(), m_sve.m_view.reference(), m_sve.m_view.epsilon()))
188  {
189  m_inner++;
190  }
191  }
192  };
193 
194  enum {
196  Flags = XprType::Flags
197  };
198 
199  explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_view(xpr) {}
200 
201  protected:
203  const XprType &m_view;
204 };
205 
206 } // end namespace internal
207 
225 template<typename Derived>
227  const typename NumTraits<Scalar>::Real& epsilon) const
228 {
229  return SparseView<Derived>(derived(), reference, epsilon);
230 }
231 
244 template<typename Derived>
247  const RealScalar& epsilon) const
248 {
249  return SparseView<Derived>(derived(), reference, epsilon);
250 }
251 
252 } // end namespace Eigen
253 
254 #endif
Eigen::SparseView::_MatrixTypeNested
internal::remove_all< MatrixTypeNested >::type _MatrixTypeNested
Definition: SparseView.h:48
Eigen::internal::unary_evaluator< SparseView< ArgType >, IteratorBased >::InnerIterator::InnerIterator
EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator &sve, Index outer)
Definition: SparseView.h:97
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::InnerIterator::m_inner
Index m_inner
Definition: SparseView.h:180
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
Eigen::internal::unary_evaluator< SparseView< ArgType >, IteratorBased >::m_argImpl
evaluator< ArgType > m_argImpl
Definition: SparseView.h:133
Eigen::internal::unary_evaluator< SparseView< ArgType >, IteratorBased >::InnerIterator::m_view
const XprType & m_view
Definition: SparseView.h:113
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::SparseView::innerSize
Index innerSize() const
Definition: SparseView.h:61
Eigen::internal::unary_evaluator< SparseView< ArgType >, IteratorBased >::m_view
const XprType & m_view
Definition: SparseView.h:134
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::InnerIterator::value
EIGEN_STRONG_INLINE Scalar value() const
Definition: SparseView.h:166
Eigen::SparseView::m_epsilon
RealScalar m_epsilon
Definition: SparseView.h:74
Eigen::Sparse
Definition: Constants.h:510
Eigen::DenseBase< Solve< Decomposition, RhsType > >::Scalar
internal::traits< Solve< Decomposition, RhsType > >::Scalar Scalar
Definition: DenseBase.h:66
Eigen::internal::unary_evaluator< SparseView< ArgType >, IteratorBased >::InnerIterator::incrementToNonZero
void incrementToNonZero()
Definition: SparseView.h:116
Eigen::RowMajorBit
const unsigned int RowMajorBit
Definition: Constants.h:66
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::m_argImpl
evaluator< ArgType > m_argImpl
Definition: SparseView.h:202
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::unary_evaluator
unary_evaluator(const XprType &xpr)
Definition: SparseView.h:199
Eigen::internal::traits< SparseView< MatrixType > >::StorageKind
Sparse StorageKind
Definition: SparseView.h:22
Eigen::SparseView::nestedExpression
const internal::remove_all< MatrixTypeNested >::type & nestedExpression() const
Definition: SparseView.h:66
type
Definition: pytypes.h:1525
mat
MatrixXf mat
Definition: Tutorial_AdvancedInitialization_CommaTemporary.cpp:1
Eigen::internal::isMuchSmallerThan
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: Eigen/src/Core/MathFunctions.h:1940
Eigen::internal::evaluator_base
Definition: CoreEvaluators.h:110
Eigen::SparseView::rows
Index rows() const
Definition: SparseView.h:58
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::InnerIterator::col
Index col() const
Definition: SparseView.h:174
Eigen::internal::unary_evaluator< SparseView< ArgType >, IteratorBased >::InnerIterator::Scalar
XprType::Scalar Scalar
Definition: SparseView.h:94
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::InnerIterator::row
Index row() const
Definition: SparseView.h:173
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::StorageIndex
XprType::StorageIndex StorageIndex
Definition: SparseView.h:146
epsilon
static double epsilon
Definition: testRot3.cpp:37
Eigen::internal::unary_evaluator< SparseView< ArgType >, IteratorBased >::InnerIterator::operator++
EIGEN_STRONG_INLINE InnerIterator & operator++()
Definition: SparseView.h:103
Eigen::SparseView::outerSize
Index outerSize() const
Definition: SparseView.h:62
Eigen::SparseView::m_reference
Scalar m_reference
Definition: SparseView.h:73
EIGEN_SPARSE_PUBLIC_INTERFACE
#define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
Definition: SparseUtil.h:43
Eigen::internal::traits< SparseView< MatrixType > >::StorageIndex
MatrixType::StorageIndex StorageIndex
Definition: SparseView.h:21
Eigen::SparseMatrixBase< CwiseBinaryOp< BinaryOp, Lhs, Rhs > >::Scalar
internal::traits< Derived >::Scalar Scalar
Definition: SparseMatrixBase.h:31
Eigen::internal::unary_evaluator< SparseView< ArgType >, IteratorBased >::unary_evaluator
unary_evaluator(const XprType &xpr)
Definition: SparseView.h:130
Eigen::SparseView::cols
Index cols() const
Definition: SparseView.h:59
return_value_policy::reference
@ reference
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
Eigen::internal::IteratorBased
Definition: Constants.h:545
Eigen::SparseView::Base
SparseMatrixBase< SparseView > Base
Definition: SparseView.h:49
Eigen::SparseView::MatrixTypeNested
MatrixType::Nested MatrixTypeNested
Definition: SparseView.h:47
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::InnerIterator::operator++
EIGEN_STRONG_INLINE InnerIterator & operator++()
Definition: SparseView.h:159
Eigen::Triplet< double >
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::XprType
SparseView< ArgType > XprType
Definition: SparseView.h:142
Eigen::internal::evaluator
Definition: CoreEvaluators.h:90
Eigen::bfloat16_impl::operator++
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 operator++(bfloat16 &a)
Definition: BFloat16.h:200
Eigen::InnerIterator
An InnerIterator allows to loop over the element of any matrix expression.
Definition: CoreIterators.h:33
Eigen::SparseView::reference
Scalar reference() const
Definition: SparseView.h:68
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::InnerIterator::InnerIterator
EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator &sve, Index outer)
Definition: SparseView.h:153
RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
Eigen::SparseView::m_matrix
MatrixTypeNested m_matrix
Definition: SparseView.h:72
Eigen::internal::unary_evaluator< SparseView< ArgType >, IteratorBased >::EvalIterator
evaluator< ArgType >::InnerIterator EvalIterator
Definition: SparseView.h:87
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
Eigen::internal::unary_evaluator
Definition: CoreEvaluators.h:65
Eigen::SparseView
Expression of a dense or sparse matrix with zero or too small values removed.
Definition: ForwardDeclarations.h:124
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::InnerIterator::index
EIGEN_STRONG_INLINE StorageIndex index() const
Definition: SparseView.h:172
Eigen::SparseMatrixBase
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:301
Eigen::internal::unary_evaluator< SparseView< ArgType >, IteratorBased >::XprType
SparseView< ArgType > XprType
Definition: SparseView.h:89
Eigen::SparseMatrixBase< CwiseBinaryOp< BinaryOp, Lhs, Rhs > >::RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: SparseMatrixBase.h:128
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::InnerIterator::m_outer
const Index m_outer
Definition: SparseView.h:181
internal
Definition: BandTriangularSolver.h:13
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::Scalar
XprType::Scalar Scalar
Definition: SparseView.h:145
Eigen::internal::IndexBased
Definition: Constants.h:542
Eigen::MatrixBase::sparseView
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:226
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
Eigen::SparseView::epsilon
RealScalar epsilon() const
Definition: SparseView.h:69
test_callbacks.value
value
Definition: test_callbacks.py:160
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::InnerIterator::incrementToNonZero
void incrementToNonZero()
Definition: SparseView.h:185
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::m_view
const XprType & m_view
Definition: SparseView.h:203
Eigen::SparseMatrixBase::pruned
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:246
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::InnerIterator::m_sve
const unary_evaluator & m_sve
Definition: SparseView.h:179
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Eigen::internal::unary_evaluator< SparseView< ArgType >, IndexBased >::InnerIterator::m_end
const Index m_end
Definition: SparseView.h:182


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:04:58