MOAB  4.9.3pre
SparseCwiseUnaryOp.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2015 Gael Guennebaud <[email protected]>
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 //     class ReverseInnerIterator;
00026     
00027     enum {
00028       CoeffReadCost = evaluator<ArgType>::CoeffReadCost + functor_traits<UnaryOp>::Cost,
00029       Flags = XprType::Flags
00030     };
00031     
00032     explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression())
00033     {
00034       EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<UnaryOp>::Cost);
00035       EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
00036     }
00037     
00038     inline Index nonZerosEstimate() const {
00039       return m_argImpl.nonZerosEstimate();
00040     }
00041 
00042   protected:
00043     typedef typename evaluator<ArgType>::InnerIterator        EvalIterator;
00044 //     typedef typename evaluator<ArgType>::ReverseInnerIterator EvalReverseIterator;
00045     
00046     const UnaryOp m_functor;
00047     evaluator<ArgType> m_argImpl;
00048 };
00049 
00050 template<typename UnaryOp, typename ArgType>
00051 class unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::InnerIterator
00052     : public unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator
00053 {
00054     typedef typename XprType::Scalar Scalar;
00055     typedef typename unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator Base;
00056   public:
00057 
00058     EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
00059       : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
00060     {}
00061 
00062     EIGEN_STRONG_INLINE InnerIterator& operator++()
00063     { Base::operator++(); return *this; }
00064 
00065     EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
00066 
00067   protected:
00068     const UnaryOp m_functor;
00069   private:
00070     Scalar& valueRef();
00071 };
00072 
00073 // template<typename UnaryOp, typename ArgType>
00074 // class unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::ReverseInnerIterator
00075 //     : public unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalReverseIterator
00076 // {
00077 //     typedef typename XprType::Scalar Scalar;
00078 //     typedef typename unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalReverseIterator Base;
00079 //   public:
00080 // 
00081 //     EIGEN_STRONG_INLINE ReverseInnerIterator(const XprType& unaryOp, typename XprType::Index outer)
00082 //       : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
00083 //     {}
00084 // 
00085 //     EIGEN_STRONG_INLINE ReverseInnerIterator& operator--()
00086 //     { Base::operator--(); return *this; }
00087 // 
00088 //     EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
00089 // 
00090 //   protected:
00091 //     const UnaryOp m_functor;
00092 //   private:
00093 //     Scalar& valueRef();
00094 // };
00095 
00096 
00097 
00098 
00099 
00100 template<typename ViewOp, typename ArgType>
00101 struct unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>
00102   : public evaluator_base<CwiseUnaryView<ViewOp,ArgType> >
00103 {
00104   public:
00105     typedef CwiseUnaryView<ViewOp, ArgType> XprType;
00106 
00107     class InnerIterator;
00108     class ReverseInnerIterator;
00109     
00110     enum {
00111       CoeffReadCost = evaluator<ArgType>::CoeffReadCost + functor_traits<ViewOp>::Cost,
00112       Flags = XprType::Flags
00113     };
00114     
00115     explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression())
00116     {
00117       EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<ViewOp>::Cost);
00118       EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
00119     }
00120 
00121   protected:
00122     typedef typename evaluator<ArgType>::InnerIterator        EvalIterator;
00123 //     typedef typename evaluator<ArgType>::ReverseInnerIterator EvalReverseIterator;
00124     
00125     const ViewOp m_functor;
00126     evaluator<ArgType> m_argImpl;
00127 };
00128 
00129 template<typename ViewOp, typename ArgType>
00130 class unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::InnerIterator
00131     : public unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator
00132 {
00133     typedef typename XprType::Scalar Scalar;
00134     typedef typename unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator Base;
00135   public:
00136 
00137     EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
00138       : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
00139     {}
00140 
00141     EIGEN_STRONG_INLINE InnerIterator& operator++()
00142     { Base::operator++(); return *this; }
00143 
00144     EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
00145     EIGEN_STRONG_INLINE Scalar& valueRef() { return m_functor(Base::valueRef()); }
00146 
00147   protected:
00148     const ViewOp m_functor;
00149 };
00150 
00151 // template<typename ViewOp, typename ArgType>
00152 // class unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::ReverseInnerIterator
00153 //     : public unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalReverseIterator
00154 // {
00155 //     typedef typename XprType::Scalar Scalar;
00156 //     typedef typename unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalReverseIterator Base;
00157 //   public:
00158 // 
00159 //     EIGEN_STRONG_INLINE ReverseInnerIterator(const XprType& unaryOp, typename XprType::Index outer)
00160 //       : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
00161 //     {}
00162 // 
00163 //     EIGEN_STRONG_INLINE ReverseInnerIterator& operator--()
00164 //     { Base::operator--(); return *this; }
00165 // 
00166 //     EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
00167 //     EIGEN_STRONG_INLINE Scalar& valueRef() { return m_functor(Base::valueRef()); }
00168 // 
00169 //   protected:
00170 //     const ViewOp m_functor;
00171 // };
00172 
00173 
00174 } // end namespace internal
00175 
00176 template<typename Derived>
00177 EIGEN_STRONG_INLINE Derived&
00178 SparseMatrixBase<Derived>::operator*=(const Scalar& other)
00179 {
00180   for (Index j=0; j<outerSize(); ++j)
00181     for (typename Derived::InnerIterator i(derived(),j); i; ++i)
00182       i.valueRef() *= other;
00183   return derived();
00184 }
00185 
00186 template<typename Derived>
00187 EIGEN_STRONG_INLINE Derived&
00188 SparseMatrixBase<Derived>::operator/=(const Scalar& other)
00189 {
00190   for (Index j=0; j<outerSize(); ++j)
00191     for (typename Derived::InnerIterator i(derived(),j); i; ++i)
00192       i.valueRef() /= other;
00193   return derived();
00194 }
00195 
00196 } // end namespace Eigen
00197 
00198 #endif // EIGEN_SPARSE_CWISE_UNARY_OP_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines