Eigen  3.3.3
SparseCwiseUnaryOp.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_SPARSE_CWISE_UNARY_OP_H
00011 #define EIGEN_SPARSE_CWISE_UNARY_OP_H
00012 
00013 namespace Eigen { 
00014 
00015 namespace internal {
00016   
00017 template<typename UnaryOp, typename ArgType>
00018 struct unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>
00019   : public evaluator_base<CwiseUnaryOp<UnaryOp,ArgType> >
00020 {
00021   public:
00022     typedef CwiseUnaryOp<UnaryOp, ArgType> XprType;
00023 
00024     class InnerIterator;
00025     
00026     enum {
00027       CoeffReadCost = evaluator<ArgType>::CoeffReadCost + functor_traits<UnaryOp>::Cost,
00028       Flags = XprType::Flags
00029     };
00030     
00031     explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression())
00032     {
00033       EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<UnaryOp>::Cost);
00034       EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
00035     }
00036     
00037     inline Index nonZerosEstimate() const {
00038       return m_argImpl.nonZerosEstimate();
00039     }
00040 
00041   protected:
00042     typedef typename evaluator<ArgType>::InnerIterator        EvalIterator;
00043     
00044     const UnaryOp m_functor;
00045     evaluator<ArgType> m_argImpl;
00046 };
00047 
00048 template<typename UnaryOp, typename ArgType>
00049 class unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::InnerIterator
00050     : public unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator
00051 {
00052     typedef typename XprType::Scalar Scalar;
00053     typedef typename unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator Base;
00054   public:
00055 
00056     EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
00057       : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
00058     {}
00059 
00060     EIGEN_STRONG_INLINE InnerIterator& operator++()
00061     { Base::operator++(); return *this; }
00062 
00063     EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
00064 
00065   protected:
00066     const UnaryOp m_functor;
00067   private:
00068     Scalar& valueRef();
00069 };
00070 
00071 template<typename ViewOp, typename ArgType>
00072 struct unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>
00073   : public evaluator_base<CwiseUnaryView<ViewOp,ArgType> >
00074 {
00075   public:
00076     typedef CwiseUnaryView<ViewOp, ArgType> XprType;
00077 
00078     class InnerIterator;
00079     
00080     enum {
00081       CoeffReadCost = evaluator<ArgType>::CoeffReadCost + functor_traits<ViewOp>::Cost,
00082       Flags = XprType::Flags
00083     };
00084     
00085     explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression())
00086     {
00087       EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<ViewOp>::Cost);
00088       EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
00089     }
00090 
00091   protected:
00092     typedef typename evaluator<ArgType>::InnerIterator        EvalIterator;
00093     
00094     const ViewOp m_functor;
00095     evaluator<ArgType> m_argImpl;
00096 };
00097 
00098 template<typename ViewOp, typename ArgType>
00099 class unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::InnerIterator
00100     : public unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator
00101 {
00102     typedef typename XprType::Scalar Scalar;
00103     typedef typename unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator Base;
00104   public:
00105 
00106     EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
00107       : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
00108     {}
00109 
00110     EIGEN_STRONG_INLINE InnerIterator& operator++()
00111     { Base::operator++(); return *this; }
00112 
00113     EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
00114     EIGEN_STRONG_INLINE Scalar& valueRef() { return m_functor(Base::valueRef()); }
00115 
00116   protected:
00117     const ViewOp m_functor;
00118 };
00119 
00120 } // end namespace internal
00121 
00122 template<typename Derived>
00123 EIGEN_STRONG_INLINE Derived&
00124 SparseMatrixBase<Derived>::operator*=(const Scalar& other)
00125 {
00126   typedef typename internal::evaluator<Derived>::InnerIterator EvalIterator;
00127   internal::evaluator<Derived> thisEval(derived());
00128   for (Index j=0; j<outerSize(); ++j)
00129     for (EvalIterator i(thisEval,j); i; ++i)
00130       i.valueRef() *= other;
00131   return derived();
00132 }
00133 
00134 template<typename Derived>
00135 EIGEN_STRONG_INLINE Derived&
00136 SparseMatrixBase<Derived>::operator/=(const Scalar& other)
00137 {
00138   typedef typename internal::evaluator<Derived>::InnerIterator EvalIterator;
00139   internal::evaluator<Derived> thisEval(derived());
00140   for (Index j=0; j<outerSize(); ++j)
00141     for (EvalIterator i(thisEval,j); i; ++i)
00142       i.valueRef() /= other;
00143   return derived();
00144 }
00145 
00146 } // end namespace Eigen
00147 
00148 #endif // EIGEN_SPARSE_CWISE_UNARY_OP_H
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends