Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:34:42

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2008-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_SPARSE_CWISE_UNARY_OP_H
0011 #define EIGEN_SPARSE_CWISE_UNARY_OP_H
0012 
0013 namespace Eigen { 
0014 
0015 namespace internal {
0016   
0017 template<typename UnaryOp, typename ArgType>
0018 struct unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>
0019   : public evaluator_base<CwiseUnaryOp<UnaryOp,ArgType> >
0020 {
0021   public:
0022     typedef CwiseUnaryOp<UnaryOp, ArgType> XprType;
0023 
0024     class InnerIterator;
0025     
0026     enum {
0027       CoeffReadCost = int(evaluator<ArgType>::CoeffReadCost) + int(functor_traits<UnaryOp>::Cost),
0028       Flags = XprType::Flags
0029     };
0030     
0031     explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression())
0032     {
0033       EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<UnaryOp>::Cost);
0034       EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
0035     }
0036     
0037     inline Index nonZerosEstimate() const {
0038       return m_argImpl.nonZerosEstimate();
0039     }
0040 
0041   protected:
0042     typedef typename evaluator<ArgType>::InnerIterator        EvalIterator;
0043     
0044     const UnaryOp m_functor;
0045     evaluator<ArgType> m_argImpl;
0046 };
0047 
0048 template<typename UnaryOp, typename ArgType>
0049 class unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::InnerIterator
0050     : public unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator
0051 {
0052   protected:
0053     typedef typename XprType::Scalar Scalar;
0054     typedef typename unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>::EvalIterator Base;
0055   public:
0056 
0057     EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
0058       : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
0059     {}
0060 
0061     EIGEN_STRONG_INLINE InnerIterator& operator++()
0062     { Base::operator++(); return *this; }
0063 
0064     EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
0065 
0066   protected:
0067     const UnaryOp m_functor;
0068   private:
0069     Scalar& valueRef();
0070 };
0071 
0072 template<typename ViewOp, typename ArgType>
0073 struct unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>
0074   : public evaluator_base<CwiseUnaryView<ViewOp,ArgType> >
0075 {
0076   public:
0077     typedef CwiseUnaryView<ViewOp, ArgType> XprType;
0078 
0079     class InnerIterator;
0080     
0081     enum {
0082       CoeffReadCost = int(evaluator<ArgType>::CoeffReadCost) + int(functor_traits<ViewOp>::Cost),
0083       Flags = XprType::Flags
0084     };
0085     
0086     explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression())
0087     {
0088       EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<ViewOp>::Cost);
0089       EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
0090     }
0091 
0092   protected:
0093     typedef typename evaluator<ArgType>::InnerIterator        EvalIterator;
0094     
0095     const ViewOp m_functor;
0096     evaluator<ArgType> m_argImpl;
0097 };
0098 
0099 template<typename ViewOp, typename ArgType>
0100 class unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::InnerIterator
0101     : public unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator
0102 {
0103   protected:
0104     typedef typename XprType::Scalar Scalar;
0105     typedef typename unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>::EvalIterator Base;
0106   public:
0107 
0108     EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& unaryOp, Index outer)
0109       : Base(unaryOp.m_argImpl,outer), m_functor(unaryOp.m_functor)
0110     {}
0111 
0112     EIGEN_STRONG_INLINE InnerIterator& operator++()
0113     { Base::operator++(); return *this; }
0114 
0115     EIGEN_STRONG_INLINE Scalar value() const { return m_functor(Base::value()); }
0116     EIGEN_STRONG_INLINE Scalar& valueRef() { return m_functor(Base::valueRef()); }
0117 
0118   protected:
0119     const ViewOp m_functor;
0120 };
0121 
0122 } // end namespace internal
0123 
0124 template<typename Derived>
0125 EIGEN_STRONG_INLINE Derived&
0126 SparseMatrixBase<Derived>::operator*=(const Scalar& other)
0127 {
0128   typedef typename internal::evaluator<Derived>::InnerIterator EvalIterator;
0129   internal::evaluator<Derived> thisEval(derived());
0130   for (Index j=0; j<outerSize(); ++j)
0131     for (EvalIterator i(thisEval,j); i; ++i)
0132       i.valueRef() *= other;
0133   return derived();
0134 }
0135 
0136 template<typename Derived>
0137 EIGEN_STRONG_INLINE Derived&
0138 SparseMatrixBase<Derived>::operator/=(const Scalar& other)
0139 {
0140   typedef typename internal::evaluator<Derived>::InnerIterator EvalIterator;
0141   internal::evaluator<Derived> thisEval(derived());
0142   for (Index j=0; j<outerSize(); ++j)
0143     for (EvalIterator i(thisEval,j); i; ++i)
0144       i.valueRef() /= other;
0145   return derived();
0146 }
0147 
0148 } // end namespace Eigen
0149 
0150 #endif // EIGEN_SPARSE_CWISE_UNARY_OP_H