$darkmode
Eigen  5.0.1-dev
SparseCwiseUnaryOp.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SPARSE_CWISE_UNARY_OP_H
11 #define EIGEN_SPARSE_CWISE_UNARY_OP_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 template <typename UnaryOp, typename ArgType>
21 struct unary_evaluator<CwiseUnaryOp<UnaryOp, ArgType>, IteratorBased>
22  : public evaluator_base<CwiseUnaryOp<UnaryOp, ArgType> > {
23  public:
24  typedef CwiseUnaryOp<UnaryOp, ArgType> XprType;
25 
26  class InnerIterator;
27 
28  enum {
29  CoeffReadCost = int(evaluator<ArgType>::CoeffReadCost) + int(functor_traits<UnaryOp>::Cost),
30  Flags = XprType::Flags
31  };
32 
33  explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression()) {
34  EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<UnaryOp>::Cost);
35  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
36  }
37 
38  inline Index nonZerosEstimate() const { return m_argImpl.nonZerosEstimate(); }
39 
40  protected:
41  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
42 
43  const UnaryOp m_functor;
44  evaluator<ArgType> m_argImpl;
45 };
46 
47 template <typename UnaryOp, typename ArgType>
48 class unary_evaluator<CwiseUnaryOp<UnaryOp, ArgType>, IteratorBased>::InnerIterator
49  : public unary_evaluator<CwiseUnaryOp<UnaryOp, ArgType>, IteratorBased>::EvalIterator {
50  protected:
51  typedef typename XprType::Scalar Scalar;
52  typedef typename unary_evaluator<CwiseUnaryOp<UnaryOp, ArgType>, IteratorBased>::EvalIterator Base;
53 
54  public:
55  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
56  : Base(unaryOp.m_argImpl, outer), m_functor(unaryOp.m_functor) {}
57 
58  EIGEN_STRONG_INLINE InnerIterator& operator++() {
59  Base::operator++();
60  return *this;
61  }
62 
63  EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
64 
65  protected:
66  const UnaryOp m_functor;
67 
68  private:
69  Scalar& valueRef();
70 };
71 
72 template <typename ViewOp, typename ArgType>
73 struct unary_evaluator<CwiseUnaryView<ViewOp, ArgType>, IteratorBased>
74  : public evaluator_base<CwiseUnaryView<ViewOp, ArgType> > {
75  public:
76  typedef CwiseUnaryView<ViewOp, ArgType> XprType;
77 
78  class InnerIterator;
79 
80  enum {
81  CoeffReadCost = int(evaluator<ArgType>::CoeffReadCost) + int(functor_traits<ViewOp>::Cost),
82  Flags = XprType::Flags
83  };
84 
85  explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression()) {
86  EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<ViewOp>::Cost);
87  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
88  }
89 
90  protected:
91  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
92 
93  const ViewOp m_functor;
94  evaluator<ArgType> m_argImpl;
95 };
96 
97 template <typename ViewOp, typename ArgType>
98 class unary_evaluator<CwiseUnaryView<ViewOp, ArgType>, IteratorBased>::InnerIterator
99  : public unary_evaluator<CwiseUnaryView<ViewOp, ArgType>, IteratorBased>::EvalIterator {
100  protected:
101  typedef typename XprType::Scalar Scalar;
102  typedef typename unary_evaluator<CwiseUnaryView<ViewOp, ArgType>, IteratorBased>::EvalIterator Base;
103 
104  public:
105  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
106  : Base(unaryOp.m_argImpl, outer), m_functor(unaryOp.m_functor) {}
107 
108  EIGEN_STRONG_INLINE InnerIterator& operator++() {
109  Base::operator++();
110  return *this;
111  }
112 
113  EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
114  EIGEN_STRONG_INLINE Scalar& valueRef() { return m_functor(Base::valueRef()); }
115 
116  protected:
117  const ViewOp m_functor;
118 };
119 
120 } // end namespace internal
121 
122 template <typename Derived>
123 EIGEN_STRONG_INLINE Derived& SparseMatrixBase<Derived>::operator*=(const Scalar& other) {
124  typedef typename internal::evaluator<Derived>::InnerIterator EvalIterator;
125  internal::evaluator<Derived> thisEval(derived());
126  for (Index j = 0; j < outerSize(); ++j)
127  for (EvalIterator i(thisEval, j); i; ++i) i.valueRef() *= other;
128  return derived();
129 }
130 
131 template <typename Derived>
132 EIGEN_STRONG_INLINE Derived& SparseMatrixBase<Derived>::operator/=(const Scalar& other) {
133  typedef typename internal::evaluator<Derived>::InnerIterator EvalIterator;
134  internal::evaluator<Derived> thisEval(derived());
135  for (Index j = 0; j < outerSize(); ++j)
136  for (EvalIterator i(thisEval, j); i; ++i) i.valueRef() /= other;
137  return derived();
138 }
139 
140 } // end namespace Eigen
141 
142 #endif // EIGEN_SPARSE_CWISE_UNARY_OP_H
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82