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