SparseCwiseUnaryOp.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) 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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
19 template<typename UnaryOp, typename ArgType>
20 struct unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>
21  : public evaluator_base<CwiseUnaryOp<UnaryOp,ArgType> >
22 {
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  {
35  EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<UnaryOp>::Cost);
36  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
37  }
38 
39  inline Index nonZerosEstimate() const {
40  return m_argImpl.nonZerosEstimate();
41  }
42 
43  protected:
44  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
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  protected:
55  typedef typename XprType::Scalar Scalar;
56  typedef typename unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator Base;
57  public:
58 
59  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
60  : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
61  {}
62 
63  EIGEN_STRONG_INLINE InnerIterator& operator++()
64  { Base::operator++(); return *this; }
65 
66  EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
67 
68  protected:
69  const UnaryOp m_functor;
70  private:
71  Scalar& valueRef();
72 };
73 
74 template<typename ViewOp, typename ArgType>
75 struct unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>
76  : public evaluator_base<CwiseUnaryView<ViewOp,ArgType> >
77 {
78  public:
79  typedef CwiseUnaryView<ViewOp, ArgType> XprType;
80 
81  class InnerIterator;
82 
83  enum {
84  CoeffReadCost = int(evaluator<ArgType>::CoeffReadCost) + int(functor_traits<ViewOp>::Cost),
85  Flags = XprType::Flags
86  };
87 
88  explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression())
89  {
90  EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<ViewOp>::Cost);
91  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
92  }
93 
94  protected:
95  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
96 
97  const ViewOp m_functor;
98  evaluator<ArgType> m_argImpl;
99 };
100 
101 template<typename ViewOp, typename ArgType>
102 class unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::InnerIterator
103  : public unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator
104 {
105  protected:
106  typedef typename XprType::Scalar Scalar;
107  typedef typename unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator Base;
108  public:
109 
110  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
111  : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
112  {}
113 
114  EIGEN_STRONG_INLINE InnerIterator& operator++()
115  { Base::operator++(); return *this; }
116 
117  EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
118  EIGEN_STRONG_INLINE Scalar& valueRef() { return m_functor(Base::valueRef()); }
119 
120  protected:
121  const ViewOp m_functor;
122 };
123 
124 } // end namespace internal
125 
126 template<typename Derived>
127 EIGEN_STRONG_INLINE Derived&
129 {
130  typedef typename internal::evaluator<Derived>::InnerIterator EvalIterator;
131  internal::evaluator<Derived> thisEval(derived());
132  for (Index j=0; j<outerSize(); ++j)
133  for (EvalIterator i(thisEval,j); i; ++i)
134  i.valueRef() *= other;
135  return derived();
136 }
137 
138 template<typename Derived>
139 EIGEN_STRONG_INLINE Derived&
141 {
142  typedef typename internal::evaluator<Derived>::InnerIterator EvalIterator;
143  internal::evaluator<Derived> thisEval(derived());
144  for (Index j=0; j<outerSize(); ++j)
145  for (EvalIterator i(thisEval,j); i; ++i)
146  i.valueRef() /= other;
147  return derived();
148 }
149 
150 } // end namespace Eigen
151 
152 #endif // EIGEN_SPARSE_CWISE_UNARY_OP_H
#define EIGEN_INTERNAL_CHECK_COST_VALUE(C)
Definition: StaticAssert.h:112
Derived & operator*=(const Scalar &other)
Derived & operator/=(const Scalar &other)
internal::traits< Derived >::Scalar Scalar
bfloat16 operator++(bfloat16 &a)
Definition: BFloat16.h:298
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41
std::ptrdiff_t j