CwiseUnaryOp.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 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_CWISE_UNARY_OP_H
00027 #define EIGEN_CWISE_UNARY_OP_H
00028 
00029 namespace Eigen { 
00030 
00051 namespace internal {
00052 template<typename UnaryOp, typename XprType>
00053 struct traits<CwiseUnaryOp<UnaryOp, XprType> >
00054  : traits<XprType>
00055 {
00056   typedef typename result_of<
00057                      UnaryOp(typename XprType::Scalar)
00058                    >::type Scalar;
00059   typedef typename XprType::Nested XprTypeNested;
00060   typedef typename remove_reference<XprTypeNested>::type _XprTypeNested;
00061   enum {
00062     Flags = _XprTypeNested::Flags & (
00063       HereditaryBits | LinearAccessBit | AlignedBit
00064       | (functor_traits<UnaryOp>::PacketAccess ? PacketAccessBit : 0)),
00065     CoeffReadCost = _XprTypeNested::CoeffReadCost + functor_traits<UnaryOp>::Cost
00066   };
00067 };
00068 }
00069 
00070 template<typename UnaryOp, typename XprType, typename StorageKind>
00071 class CwiseUnaryOpImpl;
00072 
00073 template<typename UnaryOp, typename XprType>
00074 class CwiseUnaryOp : internal::no_assignment_operator,
00075   public CwiseUnaryOpImpl<UnaryOp, XprType, typename internal::traits<XprType>::StorageKind>
00076 {
00077   public:
00078 
00079     typedef typename CwiseUnaryOpImpl<UnaryOp, XprType,typename internal::traits<XprType>::StorageKind>::Base Base;
00080     EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryOp)
00081 
00082     inline CwiseUnaryOp(const XprType& xpr, const UnaryOp& func = UnaryOp())
00083       : m_xpr(xpr), m_functor(func) {}
00084 
00085     EIGEN_STRONG_INLINE Index rows() const { return m_xpr.rows(); }
00086     EIGEN_STRONG_INLINE Index cols() const { return m_xpr.cols(); }
00087 
00089     const UnaryOp& functor() const { return m_functor; }
00090 
00092     const typename internal::remove_all<typename XprType::Nested>::type&
00093     nestedExpression() const { return m_xpr; }
00094 
00096     typename internal::remove_all<typename XprType::Nested>::type&
00097     nestedExpression() { return m_xpr.const_cast_derived(); }
00098 
00099   protected:
00100     typename XprType::Nested m_xpr;
00101     const UnaryOp m_functor;
00102 };
00103 
00104 // This is the generic implementation for dense storage.
00105 // It can be used for any expression types implementing the dense concept.
00106 template<typename UnaryOp, typename XprType>
00107 class CwiseUnaryOpImpl<UnaryOp,XprType,Dense>
00108   : public internal::dense_xpr_base<CwiseUnaryOp<UnaryOp, XprType> >::type
00109 {
00110   public:
00111 
00112     typedef CwiseUnaryOp<UnaryOp, XprType> Derived;
00113     typedef typename internal::dense_xpr_base<CwiseUnaryOp<UnaryOp, XprType> >::type Base;
00114     EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
00115 
00116     EIGEN_STRONG_INLINE const Scalar coeff(Index row, Index col) const
00117     {
00118       return derived().functor()(derived().nestedExpression().coeff(row, col));
00119     }
00120 
00121     template<int LoadMode>
00122     EIGEN_STRONG_INLINE PacketScalar packet(Index row, Index col) const
00123     {
00124       return derived().functor().packetOp(derived().nestedExpression().template packet<LoadMode>(row, col));
00125     }
00126 
00127     EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
00128     {
00129       return derived().functor()(derived().nestedExpression().coeff(index));
00130     }
00131 
00132     template<int LoadMode>
00133     EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
00134     {
00135       return derived().functor().packetOp(derived().nestedExpression().template packet<LoadMode>(index));
00136     }
00137 };
00138 
00139 } // end namespace Eigen
00140 
00141 #endif // EIGEN_CWISE_UNARY_OP_H