SparseCwiseUnaryOp.h 4.6 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
4
// Copyright (C) 2008-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
Don Gagne's avatar
Don Gagne committed
5 6 7 8 9 10 11 12 13 14
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef EIGEN_SPARSE_CWISE_UNARY_OP_H
#define EIGEN_SPARSE_CWISE_UNARY_OP_H

namespace Eigen { 

15 16 17 18 19
namespace internal {
  
template<typename UnaryOp, typename ArgType>
struct unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>
  : public evaluator_base<CwiseUnaryOp<UnaryOp,ArgType> >
Don Gagne's avatar
Don Gagne committed
20 21
{
  public:
22
    typedef CwiseUnaryOp<UnaryOp, ArgType> XprType;
Don Gagne's avatar
Don Gagne committed
23 24

    class InnerIterator;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    
    enum {
      CoeffReadCost = evaluator<ArgType>::CoeffReadCost + functor_traits<UnaryOp>::Cost,
      Flags = XprType::Flags
    };
    
    explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression())
    {
      EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<UnaryOp>::Cost);
      EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
    }
    
    inline Index nonZerosEstimate() const {
      return m_argImpl.nonZerosEstimate();
    }
Don Gagne's avatar
Don Gagne committed
40 41

  protected:
42 43 44 45
    typedef typename evaluator<ArgType>::InnerIterator        EvalIterator;
    
    const UnaryOp m_functor;
    evaluator<ArgType> m_argImpl;
Don Gagne's avatar
Don Gagne committed
46 47
};

48 49 50
template<typename UnaryOp, typename ArgType>
class unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::InnerIterator
    : public unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator
Don Gagne's avatar
Don Gagne committed
51
{
52 53
    typedef typename XprType::Scalar Scalar;
    typedef typename unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator Base;
Don Gagne's avatar
Don Gagne committed
54 55
  public:

56 57
    EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
      : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
Don Gagne's avatar
Don Gagne committed
58 59 60 61 62
    {}

    EIGEN_STRONG_INLINE InnerIterator& operator++()
    { Base::operator++(); return *this; }

63
    EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
Don Gagne's avatar
Don Gagne committed
64 65 66 67

  protected:
    const UnaryOp m_functor;
  private:
68
    Scalar& valueRef();
Don Gagne's avatar
Don Gagne committed
69 70
};

71 72 73
template<typename ViewOp, typename ArgType>
struct unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>
  : public evaluator_base<CwiseUnaryView<ViewOp,ArgType> >
Don Gagne's avatar
Don Gagne committed
74 75
{
  public:
76
    typedef CwiseUnaryView<ViewOp, ArgType> XprType;
Don Gagne's avatar
Don Gagne committed
77 78

    class InnerIterator;
79 80 81 82 83 84 85 86 87 88 89
    
    enum {
      CoeffReadCost = evaluator<ArgType>::CoeffReadCost + functor_traits<ViewOp>::Cost,
      Flags = XprType::Flags
    };
    
    explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression())
    {
      EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<ViewOp>::Cost);
      EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
    }
Don Gagne's avatar
Don Gagne committed
90 91

  protected:
92 93 94 95
    typedef typename evaluator<ArgType>::InnerIterator        EvalIterator;
    
    const ViewOp m_functor;
    evaluator<ArgType> m_argImpl;
Don Gagne's avatar
Don Gagne committed
96 97
};

98 99 100
template<typename ViewOp, typename ArgType>
class unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::InnerIterator
    : public unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator
Don Gagne's avatar
Don Gagne committed
101
{
102 103
    typedef typename XprType::Scalar Scalar;
    typedef typename unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator Base;
Don Gagne's avatar
Don Gagne committed
104 105
  public:

106 107
    EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
      : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
Don Gagne's avatar
Don Gagne committed
108 109 110 111 112
    {}

    EIGEN_STRONG_INLINE InnerIterator& operator++()
    { Base::operator++(); return *this; }

113 114
    EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
    EIGEN_STRONG_INLINE Scalar& valueRef() { return m_functor(Base::valueRef()); }
Don Gagne's avatar
Don Gagne committed
115 116 117 118 119

  protected:
    const ViewOp m_functor;
};

120
} // end namespace internal
Don Gagne's avatar
Don Gagne committed
121 122 123 124 125

template<typename Derived>
EIGEN_STRONG_INLINE Derived&
SparseMatrixBase<Derived>::operator*=(const Scalar& other)
{
126 127
  typedef typename internal::evaluator<Derived>::InnerIterator EvalIterator;
  internal::evaluator<Derived> thisEval(derived());
Don Gagne's avatar
Don Gagne committed
128
  for (Index j=0; j<outerSize(); ++j)
129
    for (EvalIterator i(thisEval,j); i; ++i)
Don Gagne's avatar
Don Gagne committed
130 131 132 133 134 135 136 137
      i.valueRef() *= other;
  return derived();
}

template<typename Derived>
EIGEN_STRONG_INLINE Derived&
SparseMatrixBase<Derived>::operator/=(const Scalar& other)
{
138 139
  typedef typename internal::evaluator<Derived>::InnerIterator EvalIterator;
  internal::evaluator<Derived> thisEval(derived());
Don Gagne's avatar
Don Gagne committed
140
  for (Index j=0; j<outerSize(); ++j)
141
    for (EvalIterator i(thisEval,j); i; ++i)
Don Gagne's avatar
Don Gagne committed
142 143 144 145 146 147 148
      i.valueRef() /= other;
  return derived();
}

} // end namespace Eigen

#endif // EIGEN_SPARSE_CWISE_UNARY_OP_H