Path Tracer
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:
23 
24  class InnerIterator;
25 
26  enum {
28  Flags = XprType::Flags
29  };
30 
31  explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression())
32  {
33  EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<UnaryOp>::Cost);
34  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
35  }
36 
37  inline Index nonZerosEstimate() const {
38  return m_argImpl.nonZerosEstimate();
39  }
40 
41  protected:
42  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
43 
44  const UnaryOp m_functor;
45  evaluator<ArgType> m_argImpl;
46 };
47 
48 template<typename UnaryOp, typename ArgType>
50  : public unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator
51 {
52  protected:
53  typedef typename XprType::Scalar Scalar;
54  typedef typename unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator Base;
55  public:
56 
57  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
58  : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
59  {}
60 
61  EIGEN_STRONG_INLINE InnerIterator& operator++()
62  { Base::operator++(); return *this; }
63 
64  EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
65 
66  protected:
67  const UnaryOp m_functor;
68  private:
69  Scalar& valueRef();
70 };
71 
72 template<typename ViewOp, typename ArgType>
74  : public evaluator_base<CwiseUnaryView<ViewOp,ArgType> >
75 {
76  public:
78 
79  class InnerIterator;
80 
81  enum {
83  Flags = XprType::Flags
84  };
85 
86  explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression())
87  {
88  EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<ViewOp>::Cost);
89  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
90  }
91 
92  protected:
93  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
94 
95  const ViewOp m_functor;
96  evaluator<ArgType> m_argImpl;
97 };
98 
99 template<typename ViewOp, typename ArgType>
101  : public unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator
102 {
103  protected:
104  typedef typename XprType::Scalar Scalar;
105  typedef typename unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator Base;
106  public:
107 
108  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
109  : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
110  {}
111 
112  EIGEN_STRONG_INLINE InnerIterator& operator++()
113  { Base::operator++(); return *this; }
114 
115  EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
116  EIGEN_STRONG_INLINE Scalar& valueRef() { return m_functor(Base::valueRef()); }
117 
118  protected:
119  const ViewOp m_functor;
120 };
121 
122 } // end namespace internal
123 
124 template<typename Derived>
125 EIGEN_STRONG_INLINE Derived&
126 SparseMatrixBase<Derived>::operator*=(const Scalar& other)
127 {
128  typedef typename internal::evaluator<Derived>::InnerIterator EvalIterator;
129  internal::evaluator<Derived> thisEval(derived());
130  for (Index j=0; j<outerSize(); ++j)
131  for (EvalIterator i(thisEval,j); i; ++i)
132  i.valueRef() *= other;
133  return derived();
134 }
135 
136 template<typename Derived>
137 EIGEN_STRONG_INLINE Derived&
138 SparseMatrixBase<Derived>::operator/=(const Scalar& other)
139 {
140  typedef typename internal::evaluator<Derived>::InnerIterator EvalIterator;
141  internal::evaluator<Derived> thisEval(derived());
142  for (Index j=0; j<outerSize(); ++j)
143  for (EvalIterator i(thisEval,j); i; ++i)
144  i.valueRef() /= other;
145  return derived();
146 }
147 
148 } // end namespace Eigen
149 
150 #endif // EIGEN_SPARSE_CWISE_UNARY_OP_H
Eigen
Namespace containing all symbols from the Eigen library.
Definition: LDLT.h:16
Eigen::CwiseUnaryView::functor
const ViewOp & functor() const
Definition: CwiseUnaryView.h:76
Eigen::internal::evaluator_base
Definition: CoreEvaluators.h:111
Eigen::CwiseUnaryView
Generic lvalue expression of a coefficient-wise unary operator of a matrix or a vector.
Definition: CwiseUnaryView.h:59
Eigen::CwiseUnaryOp::nestedExpression
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const internal::remove_all< XprTypeNested >::type & nestedExpression() const
Definition: CwiseUnaryOp.h:80
Eigen::internal::IteratorBased
Definition: Constants.h:541
Eigen::internal::evaluator
Definition: CoreEvaluators.h:91
Eigen::InnerIterator
An InnerIterator allows to loop over the element of any matrix expression.
Definition: CoreIterators.h:34
Eigen::CwiseUnaryOp
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:56
Eigen::internal::unary_evaluator
Definition: CoreEvaluators.h:65
Eigen::SparseMatrixBase
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:28
Eigen::internal::functor_traits
Definition: XprHelper.h:172
Eigen::CwiseUnaryOp::functor
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const UnaryOp & functor() const
Definition: CwiseUnaryOp.h:75
Eigen::CwiseUnaryView::nestedExpression
const internal::remove_all< MatrixTypeNested >::type & nestedExpression() const
Definition: CwiseUnaryView.h:80
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:42