Eigen  3.2.93
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 namespace Eigen {
14 
15 namespace internal {
16 
17 template<typename UnaryOp, typename ArgType>
18 struct unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>
19  : public evaluator_base<CwiseUnaryOp<UnaryOp,ArgType> >
20 {
21  public:
22  typedef CwiseUnaryOp<UnaryOp, ArgType> XprType;
23 
24  class InnerIterator;
25  class ReverseInnerIterator;
26 
27  enum {
28  CoeffReadCost = evaluator<ArgType>::CoeffReadCost + functor_traits<UnaryOp>::Cost,
29  Flags = XprType::Flags
30  };
31 
32  explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression())
33  {
34  EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<UnaryOp>::Cost);
35  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
36  }
37 
38  inline Index nonZerosEstimate() const {
39  return m_argImpl.nonZerosEstimate();
40  }
41 
42  protected:
43  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
44 // typedef typename evaluator<ArgType>::ReverseInnerIterator EvalReverseIterator;
45 
46  const UnaryOp m_functor;
47  evaluator<ArgType> m_argImpl;
48 };
49 
50 template<typename UnaryOp, typename ArgType>
51 class unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::InnerIterator
52  : public unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator
53 {
54  typedef typename XprType::Scalar Scalar;
55  typedef typename unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator Base;
56  public:
57 
58  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
59  : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
60  {}
61 
62  EIGEN_STRONG_INLINE InnerIterator& operator++()
63  { Base::operator++(); return *this; }
64 
65  EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
66 
67  protected:
68  const UnaryOp m_functor;
69  private:
70  Scalar& valueRef();
71 };
72 
73 // template<typename UnaryOp, typename ArgType>
74 // class unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::ReverseInnerIterator
75 // : public unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalReverseIterator
76 // {
77 // typedef typename XprType::Scalar Scalar;
78 // typedef typename unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalReverseIterator Base;
79 // public:
80 //
81 // EIGEN_STRONG_INLINE ReverseInnerIterator(const XprType& unaryOp, typename XprType::Index outer)
82 // : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
83 // {}
84 //
85 // EIGEN_STRONG_INLINE ReverseInnerIterator& operator--()
86 // { Base::operator--(); return *this; }
87 //
88 // EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
89 //
90 // protected:
91 // const UnaryOp m_functor;
92 // private:
93 // Scalar& valueRef();
94 // };
95 
96 
97 
98 
99 
100 template<typename ViewOp, typename ArgType>
101 struct unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>
102  : public evaluator_base<CwiseUnaryView<ViewOp,ArgType> >
103 {
104  public:
105  typedef CwiseUnaryView<ViewOp, ArgType> XprType;
106 
107  class InnerIterator;
108  class ReverseInnerIterator;
109 
110  enum {
111  CoeffReadCost = evaluator<ArgType>::CoeffReadCost + functor_traits<ViewOp>::Cost,
112  Flags = XprType::Flags
113  };
114 
115  explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression())
116  {
117  EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<ViewOp>::Cost);
118  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
119  }
120 
121  protected:
122  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
123 // typedef typename evaluator<ArgType>::ReverseInnerIterator EvalReverseIterator;
124 
125  const ViewOp m_functor;
126  evaluator<ArgType> m_argImpl;
127 };
128 
129 template<typename ViewOp, typename ArgType>
130 class unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::InnerIterator
131  : public unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator
132 {
133  typedef typename XprType::Scalar Scalar;
134  typedef typename unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator Base;
135  public:
136 
137  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
138  : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
139  {}
140 
141  EIGEN_STRONG_INLINE InnerIterator& operator++()
142  { Base::operator++(); return *this; }
143 
144  EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
145  EIGEN_STRONG_INLINE Scalar& valueRef() { return m_functor(Base::valueRef()); }
146 
147  protected:
148  const ViewOp m_functor;
149 };
150 
151 // template<typename ViewOp, typename ArgType>
152 // class unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::ReverseInnerIterator
153 // : public unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalReverseIterator
154 // {
155 // typedef typename XprType::Scalar Scalar;
156 // typedef typename unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalReverseIterator Base;
157 // public:
158 //
159 // EIGEN_STRONG_INLINE ReverseInnerIterator(const XprType& unaryOp, typename XprType::Index outer)
160 // : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
161 // {}
162 //
163 // EIGEN_STRONG_INLINE ReverseInnerIterator& operator--()
164 // { Base::operator--(); return *this; }
165 //
166 // EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
167 // EIGEN_STRONG_INLINE Scalar& valueRef() { return m_functor(Base::valueRef()); }
168 //
169 // protected:
170 // const ViewOp m_functor;
171 // };
172 
173 
174 } // end namespace internal
175 
176 template<typename Derived>
177 EIGEN_STRONG_INLINE Derived&
178 SparseMatrixBase<Derived>::operator*=(const Scalar& other)
179 {
180  for (Index j=0; j<outerSize(); ++j)
181  for (typename Derived::InnerIterator i(derived(),j); i; ++i)
182  i.valueRef() *= other;
183  return derived();
184 }
185 
186 template<typename Derived>
187 EIGEN_STRONG_INLINE Derived&
188 SparseMatrixBase<Derived>::operator/=(const Scalar& other)
189 {
190  for (Index j=0; j<outerSize(); ++j)
191  for (typename Derived::InnerIterator i(derived(),j); i; ++i)
192  i.valueRef() /= other;
193  return derived();
194 }
195 
196 } // end namespace Eigen
197 
198 #endif // EIGEN_SPARSE_CWISE_UNARY_OP_H
Namespace containing all symbols from the Eigen library.
Definition: Core:271
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: XprHelper.h:35
Definition: Eigen_Colamd.h:50