Daniel Lowengrub | dcd39a9 | 2010-06-14 02:16:46 +0300 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
| 2 | // for linear algebra. |
| 3 | // |
Gael Guennebaud | 3c63446 | 2014-07-01 13:27:35 +0200 | [diff] [blame] | 4 | // Copyright (C) 2011-2014 Gael Guennebaud <gael.guennebaud@inria.fr> |
Daniel Lowengrub | dcd39a9 | 2010-06-14 02:16:46 +0300 | [diff] [blame] | 5 | // Copyright (C) 2010 Daniel Lowengrub <lowdanie@gmail.com> |
| 6 | // |
Benoit Jacob | 69124cf | 2012-07-13 14:42:47 -0400 | [diff] [blame] | 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/. |
Daniel Lowengrub | dcd39a9 | 2010-06-14 02:16:46 +0300 | [diff] [blame] | 10 | |
| 11 | #ifndef EIGEN_SPARSEVIEW_H |
| 12 | #define EIGEN_SPARSEVIEW_H |
| 13 | |
Antonio Sánchez | 6e4d5d4 | 2023-08-21 16:25:22 +0000 | [diff] [blame] | 14 | // IWYU pragma: private |
Rasmus Munk Larsen | d7d0bf8 | 2021-09-10 19:12:26 +0000 | [diff] [blame] | 15 | #include "./InternalHeaderCheck.h" |
| 16 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 17 | namespace Eigen { |
Jitse Niesen | 3c41218 | 2012-04-15 11:06:28 +0100 | [diff] [blame] | 18 | |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 19 | namespace internal { |
| 20 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 21 | template <typename MatrixType> |
| 22 | struct traits<SparseView<MatrixType> > : traits<MatrixType> { |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 23 | typedef typename MatrixType::StorageIndex StorageIndex; |
Gael Guennebaud | 99e4afd | 2010-06-24 22:48:48 +0200 | [diff] [blame] | 24 | typedef Sparse StorageKind; |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 25 | enum { Flags = int(traits<MatrixType>::Flags) & (RowMajorBit) }; |
Gael Guennebaud | 99e4afd | 2010-06-24 22:48:48 +0200 | [diff] [blame] | 26 | }; |
Daniel Lowengrub | dcd39a9 | 2010-06-14 02:16:46 +0300 | [diff] [blame] | 27 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 28 | } // end namespace internal |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 29 | |
Gael Guennebaud | 831fffe | 2017-01-06 18:01:29 +0100 | [diff] [blame] | 30 | /** \ingroup SparseCore_Module |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 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 | */ |
| 44 | template <typename MatrixType> |
| 45 | class SparseView : public SparseMatrixBase<SparseView<MatrixType> > { |
Gael Guennebaud | 99e4afd | 2010-06-24 22:48:48 +0200 | [diff] [blame] | 46 | typedef typename MatrixType::Nested MatrixTypeNested; |
Erik Schultheis | 421cbf0 | 2022-03-16 16:43:40 +0000 | [diff] [blame] | 47 | typedef internal::remove_all_t<MatrixTypeNested> MatrixTypeNested_; |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 48 | typedef SparseMatrixBase<SparseView> Base; |
| 49 | |
| 50 | public: |
Benoit Jacob | 2d65f5d | 2010-06-14 09:06:27 -0400 | [diff] [blame] | 51 | EIGEN_SPARSE_PUBLIC_INTERFACE(SparseView) |
Erik Schultheis | 421cbf0 | 2022-03-16 16:43:40 +0000 | [diff] [blame] | 52 | typedef internal::remove_all_t<MatrixType> NestedExpression; |
Daniel Lowengrub | dcd39a9 | 2010-06-14 02:16:46 +0300 | [diff] [blame] | 53 | |
Gael Guennebaud | 9a2447b | 2015-06-09 09:11:12 +0200 | [diff] [blame] | 54 | explicit SparseView(const MatrixType& mat, const Scalar& reference = Scalar(0), |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 55 | const RealScalar& epsilon = NumTraits<Scalar>::dummy_precision()) |
| 56 | : m_matrix(mat), m_reference(reference), m_epsilon(epsilon) {} |
Gael Guennebaud | fa6d36e | 2010-07-22 15:57:01 +0200 | [diff] [blame] | 57 | |
Gael Guennebaud | fc202ba | 2015-02-13 18:57:41 +0100 | [diff] [blame] | 58 | inline Index rows() const { return m_matrix.rows(); } |
| 59 | inline Index cols() const { return m_matrix.cols(); } |
Daniel Lowengrub | dcd39a9 | 2010-06-14 02:16:46 +0300 | [diff] [blame] | 60 | |
Gael Guennebaud | fc202ba | 2015-02-13 18:57:41 +0100 | [diff] [blame] | 61 | inline Index innerSize() const { return m_matrix.innerSize(); } |
| 62 | inline Index outerSize() const { return m_matrix.outerSize(); } |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 63 | |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 64 | /** \returns the nested expression */ |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 65 | const internal::remove_all_t<MatrixTypeNested>& nestedExpression() const { return m_matrix; } |
| 66 | |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 67 | Scalar reference() const { return m_reference; } |
| 68 | RealScalar epsilon() const { return m_epsilon; } |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 69 | |
| 70 | protected: |
Gael Guennebaud | fe85b7e | 2012-02-03 23:18:26 +0100 | [diff] [blame] | 71 | MatrixTypeNested m_matrix; |
Daniel Lowengrub | dcd39a9 | 2010-06-14 02:16:46 +0300 | [diff] [blame] | 72 | Scalar m_reference; |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 73 | RealScalar m_epsilon; |
Daniel Lowengrub | dcd39a9 | 2010-06-14 02:16:46 +0300 | [diff] [blame] | 74 | }; |
| 75 | |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 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. |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 81 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 82 | template <typename ArgType> |
| 83 | struct unary_evaluator<SparseView<ArgType>, IteratorBased> : public evaluator_base<SparseView<ArgType> > { |
| 84 | typedef typename evaluator<ArgType>::InnerIterator EvalIterator; |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 85 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 86 | public: |
| 87 | typedef SparseView<ArgType> XprType; |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 88 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 89 | class InnerIterator : public EvalIterator { |
| 90 | protected: |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 91 | typedef typename XprType::Scalar Scalar; |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 92 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 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 | } |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 98 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 99 | EIGEN_STRONG_INLINE InnerIterator& operator++() { |
| 100 | EvalIterator::operator++(); |
| 101 | incrementToNonZero(); |
| 102 | return *this; |
| 103 | } |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 104 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 105 | using EvalIterator::value; |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 106 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 107 | protected: |
| 108 | const XprType& m_view; |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 109 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 110 | private: |
| 111 | void incrementToNonZero() { |
| 112 | while ((bool(*this)) && internal::isMuchSmallerThan(value(), m_view.reference(), m_view.epsilon())) { |
| 113 | EvalIterator::operator++(); |
| 114 | } |
| 115 | } |
| 116 | }; |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 117 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 118 | enum { CoeffReadCost = evaluator<ArgType>::CoeffReadCost, Flags = XprType::Flags }; |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 119 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 120 | explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_view(xpr) {} |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 121 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 122 | protected: |
| 123 | evaluator<ArgType> m_argImpl; |
| 124 | const XprType& m_view; |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 125 | }; |
| 126 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 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 |
Gael Guennebaud | f0648f8 | 2014-06-26 13:52:19 +0200 | [diff] [blame] | 185 | |
Gael Guennebaud | 831fffe | 2017-01-06 18:01:29 +0100 | [diff] [blame] | 186 | /** \ingroup SparseCore_Module |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 187 | * |
| 188 | * \returns a sparse expression of the dense expression \c *this with values smaller than |
| 189 | * \a reference * \a epsilon removed. |
| 190 | * |
| 191 | * This method is typically used when prototyping to convert a quickly assembled dense Matrix \c D to a SparseMatrix \c |
| 192 | * S: \code MatrixXd D(n,m); SparseMatrix<double> S; S = D.sparseView(); // suppress numerical zeros (exact) |
| 193 | * S = D.sparseView(reference); |
| 194 | * S = D.sparseView(reference,epsilon); |
| 195 | * \endcode |
| 196 | * where \a reference is a meaningful non zero reference value, |
| 197 | * and \a epsilon is a tolerance factor defaulting to NumTraits<Scalar>::dummy_precision(). |
| 198 | * |
| 199 | * \sa SparseMatrixBase::pruned(), class SparseView */ |
| 200 | template <typename Derived> |
Gael Guennebaud | 746d2db | 2014-07-01 13:18:56 +0200 | [diff] [blame] | 201 | const SparseView<Derived> MatrixBase<Derived>::sparseView(const Scalar& reference, |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 202 | const typename NumTraits<Scalar>::Real& epsilon) const { |
Gael Guennebaud | 746d2db | 2014-07-01 13:18:56 +0200 | [diff] [blame] | 203 | return SparseView<Derived>(derived(), reference, epsilon); |
| 204 | } |
| 205 | |
| 206 | /** \returns an expression of \c *this with values smaller than |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 207 | * \a reference * \a epsilon removed. |
| 208 | * |
| 209 | * This method is typically used in conjunction with the product of two sparse matrices |
| 210 | * to automatically prune the smallest values as follows: |
| 211 | * \code |
| 212 | * C = (A*B).pruned(); // suppress numerical zeros (exact) |
| 213 | * C = (A*B).pruned(ref); |
| 214 | * C = (A*B).pruned(ref,epsilon); |
| 215 | * \endcode |
| 216 | * where \c ref is a meaningful non zero reference value. |
| 217 | * */ |
| 218 | template <typename Derived> |
| 219 | const SparseView<Derived> SparseMatrixBase<Derived>::pruned(const Scalar& reference, const RealScalar& epsilon) const { |
Gael Guennebaud | 746d2db | 2014-07-01 13:18:56 +0200 | [diff] [blame] | 220 | return SparseView<Derived>(derived(), reference, epsilon); |
Daniel Lowengrub | dcd39a9 | 2010-06-14 02:16:46 +0300 | [diff] [blame] | 221 | } |
| 222 | |
Tobias Wood | f38e16c | 2023-11-29 11:12:48 +0000 | [diff] [blame] | 223 | } // end namespace Eigen |
Jitse Niesen | 3c41218 | 2012-04-15 11:06:28 +0100 | [diff] [blame] | 224 | |
Daniel Lowengrub | dcd39a9 | 2010-06-14 02:16:46 +0300 | [diff] [blame] | 225 | #endif |