Eigen  3.2.91
SparseCwiseUnaryOp.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2014 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  inline Index nonZerosEstimate() const {
35  return m_argImpl.nonZerosEstimate();
36  }
37 
38  protected:
39  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
40 // typedef typename evaluator<ArgType>::ReverseInnerIterator EvalReverseIterator;
41 
42  const UnaryOp m_functor;
43  evaluator<ArgType> m_argImpl;
44 };
45 
46 template<typename UnaryOp, typename ArgType>
47 class unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::InnerIterator
48  : public unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator
49 {
50  typedef typename XprType::Scalar Scalar;
51  typedef typename unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator Base;
52  public:
53 
54  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
55  : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
56  {}
57 
58  EIGEN_STRONG_INLINE InnerIterator& operator++()
59  { Base::operator++(); return *this; }
60 
61  EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
62 
63  protected:
64  const UnaryOp m_functor;
65  private:
66  Scalar& valueRef();
67 };
68 
69 // template<typename UnaryOp, typename ArgType>
70 // class unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::ReverseInnerIterator
71 // : public unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalReverseIterator
72 // {
73 // typedef typename XprType::Scalar Scalar;
74 // typedef typename unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalReverseIterator Base;
75 // public:
76 //
77 // EIGEN_STRONG_INLINE ReverseInnerIterator(const XprType& unaryOp, typename XprType::Index outer)
78 // : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
79 // {}
80 //
81 // EIGEN_STRONG_INLINE ReverseInnerIterator& operator--()
82 // { Base::operator--(); return *this; }
83 //
84 // EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
85 //
86 // protected:
87 // const UnaryOp m_functor;
88 // private:
89 // Scalar& valueRef();
90 // };
91 
92 
93 
94 
95 
96 template<typename ViewOp, typename ArgType>
97 struct unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>
98  : public evaluator_base<CwiseUnaryView<ViewOp,ArgType> >
99 {
100  public:
101  typedef CwiseUnaryView<ViewOp, ArgType> XprType;
102 
103  class InnerIterator;
104  class ReverseInnerIterator;
105 
106  enum {
107  CoeffReadCost = evaluator<ArgType>::CoeffReadCost + functor_traits<ViewOp>::Cost,
108  Flags = XprType::Flags
109  };
110 
111  explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression()) {}
112 
113  protected:
114  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
115 // typedef typename evaluator<ArgType>::ReverseInnerIterator EvalReverseIterator;
116 
117  const ViewOp m_functor;
118  evaluator<ArgType> m_argImpl;
119 };
120 
121 template<typename ViewOp, typename ArgType>
122 class unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::InnerIterator
123  : public unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator
124 {
125  typedef typename XprType::Scalar Scalar;
126  typedef typename unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator Base;
127  public:
128 
129  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
130  : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
131  {}
132 
133  EIGEN_STRONG_INLINE InnerIterator& operator++()
134  { Base::operator++(); return *this; }
135 
136  EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
137  EIGEN_STRONG_INLINE Scalar& valueRef() { return m_functor(Base::valueRef()); }
138 
139  protected:
140  const ViewOp m_functor;
141 };
142 
143 // template<typename ViewOp, typename ArgType>
144 // class unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::ReverseInnerIterator
145 // : public unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalReverseIterator
146 // {
147 // typedef typename XprType::Scalar Scalar;
148 // typedef typename unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalReverseIterator Base;
149 // public:
150 //
151 // EIGEN_STRONG_INLINE ReverseInnerIterator(const XprType& unaryOp, typename XprType::Index outer)
152 // : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
153 // {}
154 //
155 // EIGEN_STRONG_INLINE ReverseInnerIterator& operator--()
156 // { Base::operator--(); return *this; }
157 //
158 // EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
159 // EIGEN_STRONG_INLINE Scalar& valueRef() { return m_functor(Base::valueRef()); }
160 //
161 // protected:
162 // const ViewOp m_functor;
163 // };
164 
165 
166 } // end namespace internal
167 
168 template<typename Derived>
169 EIGEN_STRONG_INLINE Derived&
170 SparseMatrixBase<Derived>::operator*=(const Scalar& other)
171 {
172  for (Index j=0; j<outerSize(); ++j)
173  for (typename Derived::InnerIterator i(derived(),j); i; ++i)
174  i.valueRef() *= other;
175  return derived();
176 }
177 
178 template<typename Derived>
179 EIGEN_STRONG_INLINE Derived&
180 SparseMatrixBase<Derived>::operator/=(const Scalar& other)
181 {
182  for (Index j=0; j<outerSize(); ++j)
183  for (typename Derived::InnerIterator i(derived(),j); i; ++i)
184  i.valueRef() /= other;
185  return derived();
186 }
187 
188 } // end namespace Eigen
189 
190 #endif // EIGEN_SPARSE_CWISE_UNARY_OP_H
Definition: LDLT.h:16
Definition: Eigen_Colamd.h:54