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-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_SPARSE_CWISE_UNARY_OP_H
00026 #define EIGEN_SPARSE_CWISE_UNARY_OP_H
00027 
00028 namespace Eigen { 
00029 
00030 template<typename UnaryOp, typename MatrixType>
00031 class CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>
00032   : public SparseMatrixBase<CwiseUnaryOp<UnaryOp, MatrixType> >
00033 {
00034   public:
00035 
00036     class InnerIterator;
00037     class ReverseInnerIterator;
00038 
00039     typedef CwiseUnaryOp<UnaryOp, MatrixType> Derived;
00040     EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
00041 
00042   protected:
00043     typedef typename internal::traits<Derived>::_XprTypeNested _MatrixTypeNested;
00044     typedef typename _MatrixTypeNested::InnerIterator MatrixTypeIterator;
00045     typedef typename _MatrixTypeNested::ReverseInnerIterator MatrixTypeReverseIterator;
00046 };
00047 
00048 template<typename UnaryOp, typename MatrixType>
00049 class CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::InnerIterator
00050     : public CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::MatrixTypeIterator
00051 {
00052     typedef typename CwiseUnaryOpImpl::Scalar Scalar;
00053     typedef typename CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::MatrixTypeIterator Base;
00054   public:
00055 
00056     EIGEN_STRONG_INLINE InnerIterator(const CwiseUnaryOpImpl& unaryOp, typename CwiseUnaryOpImpl::Index outer)
00057       : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
00058     {}
00059 
00060     EIGEN_STRONG_INLINE InnerIterator& operator++()
00061     { Base::operator++(); return *this; }
00062 
00063     EIGEN_STRONG_INLINE typename CwiseUnaryOpImpl::Scalar value() const { return m_functor(Base::value()); }
00064 
00065   protected:
00066     const UnaryOp m_functor;
00067   private:
00068     typename CwiseUnaryOpImpl::Scalar& valueRef();
00069 };
00070 
00071 template<typename UnaryOp, typename MatrixType>
00072 class CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::ReverseInnerIterator
00073     : public CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::MatrixTypeReverseIterator
00074 {
00075     typedef typename CwiseUnaryOpImpl::Scalar Scalar;
00076     typedef typename CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::MatrixTypeReverseIterator Base;
00077   public:
00078 
00079     EIGEN_STRONG_INLINE ReverseInnerIterator(const CwiseUnaryOpImpl& unaryOp, typename CwiseUnaryOpImpl::Index outer)
00080       : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
00081     {}
00082 
00083     EIGEN_STRONG_INLINE ReverseInnerIterator& operator--()
00084     { Base::operator--(); return *this; }
00085 
00086     EIGEN_STRONG_INLINE typename CwiseUnaryOpImpl::Scalar value() const { return m_functor(Base::value()); }
00087 
00088   protected:
00089     const UnaryOp m_functor;
00090   private:
00091     typename CwiseUnaryOpImpl::Scalar& valueRef();
00092 };
00093 
00094 template<typename ViewOp, typename MatrixType>
00095 class CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>
00096   : public SparseMatrixBase<CwiseUnaryView<ViewOp, MatrixType> >
00097 {
00098   public:
00099 
00100     class InnerIterator;
00101     class ReverseInnerIterator;
00102 
00103     typedef CwiseUnaryView<ViewOp, MatrixType> Derived;
00104     EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
00105 
00106   protected:
00107     typedef typename internal::traits<Derived>::_MatrixTypeNested _MatrixTypeNested;
00108     typedef typename _MatrixTypeNested::InnerIterator MatrixTypeIterator;
00109     typedef typename _MatrixTypeNested::ReverseInnerIterator MatrixTypeReverseIterator;
00110 };
00111 
00112 template<typename ViewOp, typename MatrixType>
00113 class CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::InnerIterator
00114     : public CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::MatrixTypeIterator
00115 {
00116     typedef typename CwiseUnaryViewImpl::Scalar Scalar;
00117     typedef typename CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::MatrixTypeIterator Base;
00118   public:
00119 
00120     EIGEN_STRONG_INLINE InnerIterator(const CwiseUnaryViewImpl& unaryOp, typename CwiseUnaryViewImpl::Index outer)
00121       : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
00122     {}
00123 
00124     EIGEN_STRONG_INLINE InnerIterator& operator++()
00125     { Base::operator++(); return *this; }
00126 
00127     EIGEN_STRONG_INLINE typename CwiseUnaryViewImpl::Scalar value() const { return m_functor(Base::value()); }
00128     EIGEN_STRONG_INLINE typename CwiseUnaryViewImpl::Scalar& valueRef() { return m_functor(Base::valueRef()); }
00129 
00130   protected:
00131     const ViewOp m_functor;
00132 };
00133 
00134 template<typename ViewOp, typename MatrixType>
00135 class CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::ReverseInnerIterator
00136     : public CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::MatrixTypeReverseIterator
00137 {
00138     typedef typename CwiseUnaryViewImpl::Scalar Scalar;
00139     typedef typename CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::MatrixTypeReverseIterator Base;
00140   public:
00141 
00142     EIGEN_STRONG_INLINE ReverseInnerIterator(const CwiseUnaryViewImpl& unaryOp, typename CwiseUnaryViewImpl::Index outer)
00143       : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
00144     {}
00145 
00146     EIGEN_STRONG_INLINE ReverseInnerIterator& operator--()
00147     { Base::operator--(); return *this; }
00148 
00149     EIGEN_STRONG_INLINE typename CwiseUnaryViewImpl::Scalar value() const { return m_functor(Base::value()); }
00150     EIGEN_STRONG_INLINE typename CwiseUnaryViewImpl::Scalar& valueRef() { return m_functor(Base::valueRef()); }
00151 
00152   protected:
00153     const ViewOp m_functor;
00154 };
00155 
00156 template<typename Derived>
00157 EIGEN_STRONG_INLINE Derived&
00158 SparseMatrixBase<Derived>::operator*=(const Scalar& other)
00159 {
00160   for (Index j=0; j<outerSize(); ++j)
00161     for (typename Derived::InnerIterator i(derived(),j); i; ++i)
00162       i.valueRef() *= other;
00163   return derived();
00164 }
00165 
00166 template<typename Derived>
00167 EIGEN_STRONG_INLINE Derived&
00168 SparseMatrixBase<Derived>::operator/=(const Scalar& other)
00169 {
00170   for (Index j=0; j<outerSize(); ++j)
00171     for (typename Derived::InnerIterator i(derived(),j); i; ++i)
00172       i.valueRef() /= other;
00173   return derived();
00174 }
00175 
00176 } // end namespace Eigen
00177 
00178 #endif // EIGEN_SPARSE_CWISE_UNARY_OP_H