compbio
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  typedef typename XprType::Scalar Scalar;
53  typedef typename unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator Base;
54  public:
55 
56  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
57  : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
58  {}
59 
60  EIGEN_STRONG_INLINE InnerIterator& operator++()
61  { Base::operator++(); return *this; }
62 
63  EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
64 
65  protected:
66  const UnaryOp m_functor;
67  private:
68  Scalar& valueRef();
69 };
70 
71 template<typename ViewOp, typename ArgType>
73  : public evaluator_base<CwiseUnaryView<ViewOp,ArgType> >
74 {
75  public:
77 
78  class InnerIterator;
79 
80  enum {
82  Flags = XprType::Flags
83  };
84 
85  explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression())
86  {
87  EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<ViewOp>::Cost);
88  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
89  }
90 
91  protected:
92  typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
93 
94  const ViewOp m_functor;
95  evaluator<ArgType> m_argImpl;
96 };
97 
98 template<typename ViewOp, typename ArgType>
100  : public unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator
101 {
102  typedef typename XprType::Scalar Scalar;
103  typedef typename unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator Base;
104  public:
105 
106  EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
107  : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
108  {}
109 
110  EIGEN_STRONG_INLINE InnerIterator& operator++()
111  { Base::operator++(); return *this; }
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&
124 SparseMatrixBase<Derived>::operator*=(const Scalar& other)
125 {
126  for (Index j=0; j<outerSize(); ++j)
127  for (typename Derived::InnerIterator i(derived(),j); i; ++i)
128  i.valueRef() *= other;
129  return derived();
130 }
131 
132 template<typename Derived>
133 EIGEN_STRONG_INLINE Derived&
134 SparseMatrixBase<Derived>::operator/=(const Scalar& other)
135 {
136  for (Index j=0; j<outerSize(); ++j)
137  for (typename Derived::InnerIterator i(derived(),j); i; ++i)
138  i.valueRef() /= other;
139  return derived();
140 }
141 
142 } // end namespace Eigen
143 
144 #endif // EIGEN_SPARSE_CWISE_UNARY_OP_H
const internal::remove_all< MatrixTypeNested >::type & nestedExpression() const
Definition: CwiseUnaryView.h:80
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const UnaryOp & functor() const
Definition: CwiseUnaryOp.h:75
Definition: CoreEvaluators.h:90
Definition: CoreEvaluators.h:65
const ViewOp & functor() const
Definition: CwiseUnaryView.h:76
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
Definition: Constants.h:529
Definition: CoreEvaluators.h:109
Generic lvalue expression of a coefficient-wise unary operator of a matrix or a vector.
Definition: CwiseUnaryView.h:58
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:281
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Definition: BandTriangularSolver.h:13
Definition: XprHelper.h:146
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const internal::remove_all< XprTypeNested >::type & nestedExpression() const
Definition: CwiseUnaryOp.h:80
An InnerIterator allows to loop over the element of any matrix expression.
Definition: CoreIterators.h:33