Eigen  3.3.3
NullaryFunctors.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2016 Gael Guennebaud <gael.guennebaud@inria.fr>
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_NULLARY_FUNCTORS_H
00011 #define EIGEN_NULLARY_FUNCTORS_H
00012 
00013 namespace Eigen {
00014 
00015 namespace internal {
00016 
00017 template<typename Scalar>
00018 struct scalar_constant_op {
00019   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const scalar_constant_op& other) : m_other(other.m_other) { }
00020   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const Scalar& other) : m_other(other) { }
00021   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() () const { return m_other; }
00022   template<typename PacketType>
00023   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const PacketType packetOp() const { return internal::pset1<PacketType>(m_other); }
00024   const Scalar m_other;
00025 };
00026 template<typename Scalar>
00027 struct functor_traits<scalar_constant_op<Scalar> >
00028 { enum { Cost = 0 /* as the constant value should be loaded in register only once for the whole expression */,
00029          PacketAccess = packet_traits<Scalar>::Vectorizable, IsRepeatable = true }; };
00030 
00031 template<typename Scalar> struct scalar_identity_op {
00032   EIGEN_EMPTY_STRUCT_CTOR(scalar_identity_op)
00033   template<typename IndexType>
00034   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType row, IndexType col) const { return row==col ? Scalar(1) : Scalar(0); }
00035 };
00036 template<typename Scalar>
00037 struct functor_traits<scalar_identity_op<Scalar> >
00038 { enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = false, IsRepeatable = true }; };
00039 
00040 template <typename Scalar, typename Packet, bool IsInteger> struct linspaced_op_impl;
00041 
00042 template <typename Scalar, typename Packet>
00043 struct linspaced_op_impl<Scalar,Packet,/*IsInteger*/false>
00044 {
00045   linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) :
00046     m_low(low), m_high(high), m_size1(num_steps==1 ? 1 : num_steps-1), m_step(num_steps==1 ? Scalar() : (high-low)/Scalar(num_steps-1)),
00047     m_interPacket(plset<Packet>(0)),
00048     m_flip(numext::abs(high)<numext::abs(low))
00049   {}
00050 
00051   template<typename IndexType>
00052   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType i) const {
00053     if(m_flip)
00054       return (i==0)? m_low : (m_high - (m_size1-i)*m_step);
00055     else
00056       return (i==m_size1)? m_high : (m_low + i*m_step);
00057   }
00058 
00059   template<typename IndexType>
00060   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(IndexType i) const
00061   {
00062     // Principle:
00063     // [low, ..., low] + ( [step, ..., step] * ( [i, ..., i] + [0, ..., size] ) )
00064     if(m_flip)
00065     {
00066       Packet pi = padd(pset1<Packet>(Scalar(i-m_size1)),m_interPacket);
00067       Packet res = padd(pset1<Packet>(m_high), pmul(pset1<Packet>(m_step), pi));
00068       if(i==0)
00069         res = pinsertfirst(res, m_low);
00070       return res;
00071     }
00072     else
00073     {
00074       Packet pi = padd(pset1<Packet>(Scalar(i)),m_interPacket);
00075       Packet res = padd(pset1<Packet>(m_low), pmul(pset1<Packet>(m_step), pi));
00076       if(i==m_size1-unpacket_traits<Packet>::size+1)
00077         res = pinsertlast(res, m_high);
00078       return res;
00079     }
00080   }
00081 
00082   const Scalar m_low;
00083   const Scalar m_high;
00084   const Index m_size1;
00085   const Scalar m_step;
00086   const Packet m_interPacket;
00087   const bool m_flip;
00088 };
00089 
00090 template <typename Scalar, typename Packet>
00091 struct linspaced_op_impl<Scalar,Packet,/*IsInteger*/true>
00092 {
00093   linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) :
00094     m_low(low),
00095     m_multiplier((high-low)/convert_index<Scalar>(num_steps<=1 ? 1 : num_steps-1)),
00096     m_divisor(convert_index<Scalar>((high>=low?num_steps:-num_steps)+(high-low))/((numext::abs(high-low)+1)==0?1:(numext::abs(high-low)+1))),
00097     m_use_divisor(num_steps>1 && (numext::abs(high-low)+1)<num_steps)
00098   {}
00099 
00100   template<typename IndexType>
00101   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00102   const Scalar operator() (IndexType i) const
00103   {
00104     if(m_use_divisor) return m_low + convert_index<Scalar>(i)/m_divisor;
00105     else              return m_low + convert_index<Scalar>(i)*m_multiplier;
00106   }
00107 
00108   const Scalar m_low;
00109   const Scalar m_multiplier;
00110   const Scalar m_divisor;
00111   const bool m_use_divisor;
00112 };
00113 
00114 // ----- Linspace functor ----------------------------------------------------------------
00115 
00116 // Forward declaration (we default to random access which does not really give
00117 // us a speed gain when using packet access but it allows to use the functor in
00118 // nested expressions).
00119 template <typename Scalar, typename PacketType> struct linspaced_op;
00120 template <typename Scalar, typename PacketType> struct functor_traits< linspaced_op<Scalar,PacketType> >
00121 {
00122   enum
00123   {
00124     Cost = 1,
00125     PacketAccess =   (!NumTraits<Scalar>::IsInteger) && packet_traits<Scalar>::HasSetLinear && packet_traits<Scalar>::HasBlend,
00126                   /*&& ((!NumTraits<Scalar>::IsInteger) || packet_traits<Scalar>::HasDiv),*/ // <- vectorization for integer is currently disabled
00127     IsRepeatable = true
00128   };
00129 };
00130 template <typename Scalar, typename PacketType> struct linspaced_op
00131 {
00132   linspaced_op(const Scalar& low, const Scalar& high, Index num_steps)
00133     : impl((num_steps==1 ? high : low),high,num_steps)
00134   {}
00135 
00136   template<typename IndexType>
00137   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType i) const { return impl(i); }
00138 
00139   template<typename Packet,typename IndexType>
00140   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(IndexType i) const { return impl.packetOp(i); }
00141 
00142   // This proxy object handles the actual required temporaries and the different
00143   // implementations (integer vs. floating point).
00144   const linspaced_op_impl<Scalar,PacketType,NumTraits<Scalar>::IsInteger> impl;
00145 };
00146 
00147 // Linear access is automatically determined from the operator() prototypes available for the given functor.
00148 // If it exposes an operator()(i,j), then we assume the i and j coefficients are required independently
00149 // and linear access is not possible. In all other cases, linear access is enabled.
00150 // Users should not have to deal with this structure.
00151 template<typename Functor> struct functor_has_linear_access { enum { ret = !has_binary_operator<Functor>::value }; };
00152 
00153 // For unreliable compilers, let's specialize the has_*ary_operator
00154 // helpers so that at least built-in nullary functors work fine.
00155 #if !( (EIGEN_COMP_MSVC>1600) || (EIGEN_GNUC_AT_LEAST(4,8)) || (EIGEN_COMP_ICC>=1600))
00156 template<typename Scalar,typename IndexType>
00157 struct has_nullary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 1}; };
00158 template<typename Scalar,typename IndexType>
00159 struct has_unary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 0}; };
00160 template<typename Scalar,typename IndexType>
00161 struct has_binary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 0}; };
00162 
00163 template<typename Scalar,typename IndexType>
00164 struct has_nullary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 0}; };
00165 template<typename Scalar,typename IndexType>
00166 struct has_unary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 0}; };
00167 template<typename Scalar,typename IndexType>
00168 struct has_binary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 1}; };
00169 
00170 template<typename Scalar, typename PacketType,typename IndexType>
00171 struct has_nullary_operator<linspaced_op<Scalar,PacketType>,IndexType> { enum { value = 0}; };
00172 template<typename Scalar, typename PacketType,typename IndexType>
00173 struct has_unary_operator<linspaced_op<Scalar,PacketType>,IndexType> { enum { value = 1}; };
00174 template<typename Scalar, typename PacketType,typename IndexType>
00175 struct has_binary_operator<linspaced_op<Scalar,PacketType>,IndexType> { enum { value = 0}; };
00176 
00177 template<typename Scalar,typename IndexType>
00178 struct has_nullary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 1}; };
00179 template<typename Scalar,typename IndexType>
00180 struct has_unary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 0}; };
00181 template<typename Scalar,typename IndexType>
00182 struct has_binary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 0}; };
00183 #endif
00184 
00185 } // end namespace internal
00186 
00187 } // end namespace Eigen
00188 
00189 #endif // EIGEN_NULLARY_FUNCTORS_H
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends