WPILibC++ 2023.4.3
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
14namespace Eigen {
15
16namespace internal {
17
18template<typename MatrixType>
19struct traits<SparseView<MatrixType> > : traits<MatrixType>
20{
21 typedef typename MatrixType::StorageIndex StorageIndex;
23 enum {
25 };
26};
27
28} // end namespace internal
29
30/** \ingroup SparseCore_Module
31 * \class SparseView
32 *
33 * \brief Expression of a dense or sparse matrix with zero or too small values removed
34 *
35 * \tparam MatrixType the type of the object of which we are removing the small entries
36 *
37 * This class represents an expression of a given dense or sparse matrix with
38 * entries smaller than \c reference * \c epsilon are removed.
39 * It is the return type of MatrixBase::sparseView() and SparseMatrixBase::pruned()
40 * and most of the time this is the only way it is used.
41 *
42 * \sa MatrixBase::sparseView(), SparseMatrixBase::pruned()
43 */
44template<typename MatrixType>
45class SparseView : public SparseMatrixBase<SparseView<MatrixType> >
46{
47 typedef typename MatrixType::Nested MatrixTypeNested;
48 typedef typename internal::remove_all<MatrixTypeNested>::type _MatrixTypeNested;
50public:
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
64 /** \returns the nested expression */
66 nestedExpression() const { return m_matrix; }
67
68 Scalar reference() const { return m_reference; }
69 RealScalar epsilon() const { return m_epsilon; }
70
71protected:
72 MatrixTypeNested m_matrix;
75};
76
77namespace 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
83template<typename ArgType>
85 : public evaluator_base<SparseView<ArgType> >
86{
88 public:
90
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:
114
115 private:
116 void incrementToNonZero()
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:
135};
136
137template<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;
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:
183
184 private:
185 void incrementToNonZero()
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:
204};
205
206} // end namespace internal
207
208/** \ingroup SparseCore_Module
209 *
210 * \returns a sparse expression of the dense expression \c *this with values smaller than
211 * \a reference * \a epsilon removed.
212 *
213 * This method is typically used when prototyping to convert a quickly assembled dense Matrix \c D to a SparseMatrix \c S:
214 * \code
215 * MatrixXd D(n,m);
216 * SparseMatrix<double> S;
217 * S = D.sparseView(); // suppress numerical zeros (exact)
218 * S = D.sparseView(reference);
219 * S = D.sparseView(reference,epsilon);
220 * \endcode
221 * where \a reference is a meaningful non zero reference value,
222 * and \a epsilon is a tolerance factor defaulting to NumTraits<Scalar>::dummy_precision().
223 *
224 * \sa SparseMatrixBase::pruned(), class SparseView */
225template<typename Derived>
227 const typename NumTraits<Scalar>::Real& epsilon) const
228{
229 return SparseView<Derived>(derived(), reference, epsilon);
230}
231
232/** \returns an expression of \c *this with values smaller than
233 * \a reference * \a epsilon removed.
234 *
235 * This method is typically used in conjunction with the product of two sparse matrices
236 * to automatically prune the smallest values as follows:
237 * \code
238 * C = (A*B).pruned(); // suppress numerical zeros (exact)
239 * C = (A*B).pruned(ref);
240 * C = (A*B).pruned(ref,epsilon);
241 * \endcode
242 * where \c ref is a meaningful non zero reference value.
243 * */
244template<typename Derived>
247 const RealScalar& epsilon) const
248{
249 return SparseView<Derived>(derived(), reference, epsilon);
250}
251
252} // end namespace Eigen
253
254#endif
#define EIGEN_STRONG_INLINE
Definition: Macros.h:927
#define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
Definition: SparseUtil.h:43
internal::traits< Derived >::Scalar Scalar
The numeric type of the expression' coefficients, e.g.
Definition: DenseBase.h:66
An InnerIterator allows to loop over the element of any matrix expression.
Definition: CoreIterators.h:34
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:28
internal::traits< SparseView< MatrixType > >::StorageIndex StorageIndex
The integer type used to store indices within a SparseMatrix.
Definition: SparseMatrixBase.h:43
internal::traits< SparseView< MatrixType > >::Scalar Scalar
Definition: SparseMatrixBase.h:31
NumTraits< Scalar >::Real RealScalar
This is the "real scalar" type; if the Scalar type is already real numbers (e.g.
Definition: SparseMatrixBase.h:128
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:246
Expression of a dense or sparse matrix with zero or too small values removed.
Definition: SparseView.h:46
const internal::remove_all< MatrixTypeNested >::type & nestedExpression() const
Definition: SparseView.h:66
internal::remove_all< MatrixType >::type NestedExpression
Definition: SparseView.h:52
Scalar reference() const
Definition: SparseView.h:68
Scalar m_reference
Definition: SparseView.h:73
Index outerSize() const
Definition: SparseView.h:62
Index innerSize() const
Definition: SparseView.h:61
RealScalar m_epsilon
Definition: SparseView.h:74
Index cols() const
Definition: SparseView.h:59
Index rows() const
Definition: SparseView.h:58
MatrixTypeNested m_matrix
Definition: SparseView.h:72
RealScalar epsilon() const
Definition: SparseView.h:69
EIGEN_STRONG_INLINE Scalar value() const
Definition: SparseView.h:166
EIGEN_STRONG_INLINE StorageIndex index() const
Definition: SparseView.h:172
EIGEN_STRONG_INLINE InnerIterator & operator++()
Definition: SparseView.h:159
EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator &sve, Index outer)
Definition: SparseView.h:153
EIGEN_STRONG_INLINE InnerIterator & operator++()
Definition: SparseView.h:103
EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator &sve, Index outer)
Definition: SparseView.h:97
Definition: core.h:1240
type
Definition: core.h:575
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
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition: Constants.h:66
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: MathFunctions.h:1940
Namespace containing all symbols from the Eigen library.
Definition: MatrixExponential.h:16
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
GHC_FS_API uintmax_t remove_all(const path &p, std::error_code &ec) noexcept
Definition: filesystem.hpp:4732
Definition: Eigen_Colamd.h:50
unit_t< Units, T, NonLinearScale > & operator++(unit_t< Units, T, NonLinearScale > &u) noexcept
Definition: base.h:2335
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:39
Holds information about the various numeric (i.e.
Definition: NumTraits.h:233
The type used to identify a general sparse storage.
Definition: Constants.h:510
Definition: Constants.h:542
Definition: Constants.h:545
Definition: CoreEvaluators.h:111
Definition: CoreEvaluators.h:91
T type
Definition: Meta.h:126
MatrixType::StorageIndex StorageIndex
Definition: SparseView.h:21
Sparse StorageKind
Definition: SparseView.h:22
Definition: ForwardDeclarations.h:17
XprType::StorageIndex StorageIndex
Definition: SparseView.h:146
evaluator< ArgType > m_argImpl
Definition: SparseView.h:202
SparseView< ArgType > XprType
Definition: SparseView.h:142
unary_evaluator(const XprType &xpr)
Definition: SparseView.h:199
evaluator< ArgType > m_argImpl
Definition: SparseView.h:133
SparseView< ArgType > XprType
Definition: SparseView.h:89
evaluator< ArgType >::InnerIterator EvalIterator
Definition: SparseView.h:87
unary_evaluator(const XprType &xpr)
Definition: SparseView.h:130
Definition: CoreEvaluators.h:65