Eigne Matrix Class Library

Dependents:   Eigen_test Odometry_test AttitudeEstimation_usingTicker MPU9250_Quaternion_Binary_Serial ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CwiseUnaryOp.h Source File

CwiseUnaryOp.h

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 // This Source Code Form is subject to the terms of the Mozilla
00008 // Public License v. 2.0. If a copy of the MPL was not distributed
00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00010 
00011 #ifndef EIGEN_CWISE_UNARY_OP_H
00012 #define EIGEN_CWISE_UNARY_OP_H
00013 
00014 namespace Eigen { 
00015 
00016 /** \class CwiseUnaryOp
00017   * \ingroup Core_Module
00018   *
00019   * \brief Generic expression where a coefficient-wise unary operator is applied to an expression
00020   *
00021   * \param UnaryOp template functor implementing the operator
00022   * \param XprType the type of the expression to which we are applying the unary operator
00023   *
00024   * This class represents an expression where a unary operator is applied to an expression.
00025   * It is the return type of all operations taking exactly 1 input expression, regardless of the
00026   * presence of other inputs such as scalars. For example, the operator* in the expression 3*matrix
00027   * is considered unary, because only the right-hand side is an expression, and its
00028   * return type is a specialization of CwiseUnaryOp.
00029   *
00030   * Most of the time, this is the only way that it is used, so you typically don't have to name
00031   * CwiseUnaryOp types explicitly.
00032   *
00033   * \sa MatrixBase::unaryExpr(const CustomUnaryOp &) const, class CwiseBinaryOp, class CwiseNullaryOp
00034   */
00035 
00036 namespace internal {
00037 template<typename UnaryOp, typename XprType>
00038 struct traits<CwiseUnaryOp<UnaryOp, XprType> >
00039  : traits<XprType>
00040 {
00041   typedef typename result_of<
00042                      UnaryOp(typename XprType::Scalar)
00043                    >::type Scalar;
00044   typedef typename XprType::Nested XprTypeNested;
00045   typedef typename remove_reference<XprTypeNested>::type _XprTypeNested;
00046   enum {
00047     Flags = _XprTypeNested::Flags & (
00048       HereditaryBits | LinearAccessBit | AlignedBit
00049       | (functor_traits<UnaryOp>::PacketAccess ? PacketAccessBit : 0)),
00050     CoeffReadCost = EIGEN_ADD_COST(_XprTypeNested::CoeffReadCost, functor_traits<UnaryOp>::Cost)
00051   };
00052 };
00053 }
00054 
00055 template<typename UnaryOp, typename XprType, typename StorageKind>
00056 class CwiseUnaryOpImpl;
00057 
00058 template<typename UnaryOp, typename XprType>
00059 class CwiseUnaryOp : internal::no_assignment_operator,
00060   public CwiseUnaryOpImpl<UnaryOp, XprType, typename internal::traits<XprType>::StorageKind>
00061 {
00062   public:
00063 
00064     typedef typename CwiseUnaryOpImpl<UnaryOp, XprType,typename internal::traits<XprType>::StorageKind>::Base Base;
00065     EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryOp)
00066 
00067     inline CwiseUnaryOp(const XprType& xpr, const UnaryOp& func = UnaryOp())
00068       : m_xpr(xpr), m_functor(func) {}
00069 
00070     EIGEN_STRONG_INLINE Index rows() const { return m_xpr.rows(); }
00071     EIGEN_STRONG_INLINE Index cols() const { return m_xpr.cols(); }
00072 
00073     /** \returns the functor representing the unary operation */
00074     const UnaryOp& functor() const { return m_functor; }
00075 
00076     /** \returns the nested expression */
00077     const typename internal::remove_all<typename XprType::Nested>::type&
00078     nestedExpression() const { return m_xpr; }
00079 
00080     /** \returns the nested expression */
00081     typename internal::remove_all<typename XprType::Nested>::type&
00082     nestedExpression() { return m_xpr.const_cast_derived(); }
00083 
00084   protected:
00085     typename XprType::Nested m_xpr;
00086     const UnaryOp m_functor;
00087 };
00088 
00089 // This is the generic implementation for dense storage.
00090 // It can be used for any expression types implementing the dense concept.
00091 template<typename UnaryOp, typename XprType>
00092 class CwiseUnaryOpImpl<UnaryOp,XprType,Dense>
00093   : public internal::dense_xpr_base<CwiseUnaryOp<UnaryOp, XprType> >::type
00094 {
00095   public:
00096 
00097     typedef CwiseUnaryOp<UnaryOp, XprType> Derived;
00098     typedef typename internal::dense_xpr_base<CwiseUnaryOp<UnaryOp, XprType> >::type Base;
00099     EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
00100 
00101     EIGEN_STRONG_INLINE const Scalar coeff(Index rowId, Index colId) const
00102     {
00103       return derived().functor()(derived().nestedExpression().coeff(rowId, colId));
00104     }
00105 
00106     template<int LoadMode>
00107     EIGEN_STRONG_INLINE PacketScalar packet(Index rowId, Index colId) const
00108     {
00109       return derived().functor().packetOp(derived().nestedExpression().template packet<LoadMode>(rowId, colId));
00110     }
00111 
00112     EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
00113     {
00114       return derived().functor()(derived().nestedExpression().coeff(index));
00115     }
00116 
00117     template<int LoadMode>
00118     EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
00119     {
00120       return derived().functor().packetOp(derived().nestedExpression().template packet<LoadMode>(index));
00121     }
00122 };
00123 
00124 } // end namespace Eigen
00125 
00126 #endif // EIGEN_CWISE_UNARY_OP_H