Functors.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 // 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_FUNCTORS_H
00011 #define EIGEN_FUNCTORS_H
00012 
00013 namespace Eigen {
00014 
00015 namespace internal {
00016 
00017 // associative functors:
00018 
00024 template<typename Scalar> struct scalar_sum_op {
00025   EIGEN_EMPTY_STRUCT_CTOR(scalar_sum_op)
00026   EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a, const Scalar& b) const { return a + b; }
00027   template<typename Packet>
00028   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
00029   { return internal::padd(a,b); }
00030   template<typename Packet>
00031   EIGEN_STRONG_INLINE const Scalar predux(const Packet& a) const
00032   { return internal::predux(a); }
00033 };
00034 template<typename Scalar>
00035 struct functor_traits<scalar_sum_op<Scalar> > {
00036   enum {
00037     Cost = NumTraits<Scalar>::AddCost,
00038     PacketAccess = packet_traits<Scalar>::HasAdd
00039   };
00040 };
00041 
00047 template<typename LhsScalar,typename RhsScalar> struct scalar_product_op {
00048   enum {
00049     // TODO vectorize mixed product
00050     Vectorizable = is_same<LhsScalar,RhsScalar>::value && packet_traits<LhsScalar>::HasMul && packet_traits<RhsScalar>::HasMul
00051   };
00052   typedef typename scalar_product_traits<LhsScalar,RhsScalar>::ReturnType result_type;
00053   EIGEN_EMPTY_STRUCT_CTOR(scalar_product_op)
00054   EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return a * b; }
00055   template<typename Packet>
00056   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
00057   { return internal::pmul(a,b); }
00058   template<typename Packet>
00059   EIGEN_STRONG_INLINE const result_type predux(const Packet& a) const
00060   { return internal::predux_mul(a); }
00061 };
00062 template<typename LhsScalar,typename RhsScalar>
00063 struct functor_traits<scalar_product_op<LhsScalar,RhsScalar> > {
00064   enum {
00065     Cost = (NumTraits<LhsScalar>::MulCost + NumTraits<RhsScalar>::MulCost)/2, // rough estimate!
00066     PacketAccess = scalar_product_op<LhsScalar,RhsScalar>::Vectorizable
00067   };
00068 };
00069 
00075 template<typename LhsScalar,typename RhsScalar> struct scalar_conj_product_op {
00076 
00077   enum {
00078     Conj = NumTraits<LhsScalar>::IsComplex
00079   };
00080   
00081   typedef typename scalar_product_traits<LhsScalar,RhsScalar>::ReturnType result_type;
00082   
00083   EIGEN_EMPTY_STRUCT_CTOR(scalar_conj_product_op)
00084   EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const
00085   { return conj_helper<LhsScalar,RhsScalar,Conj,false>().pmul(a,b); }
00086   
00087   template<typename Packet>
00088   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
00089   { return conj_helper<Packet,Packet,Conj,false>().pmul(a,b); }
00090 };
00091 template<typename LhsScalar,typename RhsScalar>
00092 struct functor_traits<scalar_conj_product_op<LhsScalar,RhsScalar> > {
00093   enum {
00094     Cost = NumTraits<LhsScalar>::MulCost,
00095     PacketAccess = internal::is_same<LhsScalar, RhsScalar>::value && packet_traits<LhsScalar>::HasMul
00096   };
00097 };
00098 
00104 template<typename Scalar> struct scalar_min_op {
00105   EIGEN_EMPTY_STRUCT_CTOR(scalar_min_op)
00106   EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a, const Scalar& b) const { using std::min; return (min)(a, b); }
00107   template<typename Packet>
00108   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
00109   { return internal::pmin(a,b); }
00110   template<typename Packet>
00111   EIGEN_STRONG_INLINE const Scalar predux(const Packet& a) const
00112   { return internal::predux_min(a); }
00113 };
00114 template<typename Scalar>
00115 struct functor_traits<scalar_min_op<Scalar> > {
00116   enum {
00117     Cost = NumTraits<Scalar>::AddCost,
00118     PacketAccess = packet_traits<Scalar>::HasMin
00119   };
00120 };
00121 
00127 template<typename Scalar> struct scalar_max_op {
00128   EIGEN_EMPTY_STRUCT_CTOR(scalar_max_op)
00129   EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a, const Scalar& b) const { using std::max; return (max)(a, b); }
00130   template<typename Packet>
00131   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
00132   { return internal::pmax(a,b); }
00133   template<typename Packet>
00134   EIGEN_STRONG_INLINE const Scalar predux(const Packet& a) const
00135   { return internal::predux_max(a); }
00136 };
00137 template<typename Scalar>
00138 struct functor_traits<scalar_max_op<Scalar> > {
00139   enum {
00140     Cost = NumTraits<Scalar>::AddCost,
00141     PacketAccess = packet_traits<Scalar>::HasMax
00142   };
00143 };
00144 
00150 template<typename Scalar> struct scalar_hypot_op {
00151   EIGEN_EMPTY_STRUCT_CTOR(scalar_hypot_op)
00152 //   typedef typename NumTraits<Scalar>::Real result_type;
00153   EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& _x, const Scalar& _y) const
00154   {
00155     using std::max;
00156     using std::min;
00157     using std::sqrt;
00158     Scalar p = (max)(_x, _y);
00159     Scalar q = (min)(_x, _y);
00160     Scalar qp = q/p;
00161     return p * sqrt(Scalar(1) + qp*qp);
00162   }
00163 };
00164 template<typename Scalar>
00165 struct functor_traits<scalar_hypot_op<Scalar> > {
00166   enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess=0 };
00167 };
00168 
00172 template<typename Scalar, typename OtherScalar> struct scalar_binary_pow_op {
00173   EIGEN_EMPTY_STRUCT_CTOR(scalar_binary_pow_op)
00174   inline Scalar operator() (const Scalar& a, const OtherScalar& b) const { return numext::pow(a, b); }
00175 };
00176 template<typename Scalar, typename OtherScalar>
00177 struct functor_traits<scalar_binary_pow_op<Scalar,OtherScalar> > {
00178   enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = false };
00179 };
00180 
00181 // other binary functors:
00182 
00188 template<typename Scalar> struct scalar_difference_op {
00189   EIGEN_EMPTY_STRUCT_CTOR(scalar_difference_op)
00190   EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a, const Scalar& b) const { return a - b; }
00191   template<typename Packet>
00192   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
00193   { return internal::psub(a,b); }
00194 };
00195 template<typename Scalar>
00196 struct functor_traits<scalar_difference_op<Scalar> > {
00197   enum {
00198     Cost = NumTraits<Scalar>::AddCost,
00199     PacketAccess = packet_traits<Scalar>::HasSub
00200   };
00201 };
00202 
00208 template<typename LhsScalar,typename RhsScalar> struct scalar_quotient_op {
00209   enum {
00210     // TODO vectorize mixed product
00211     Vectorizable = is_same<LhsScalar,RhsScalar>::value && packet_traits<LhsScalar>::HasDiv && packet_traits<RhsScalar>::HasDiv
00212   };
00213   typedef typename scalar_product_traits<LhsScalar,RhsScalar>::ReturnType result_type;
00214   EIGEN_EMPTY_STRUCT_CTOR(scalar_quotient_op)
00215   EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return a / b; }
00216   template<typename Packet>
00217   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
00218   { return internal::pdiv(a,b); }
00219 };
00220 template<typename LhsScalar,typename RhsScalar>
00221 struct functor_traits<scalar_quotient_op<LhsScalar,RhsScalar> > {
00222   enum {
00223     Cost = (NumTraits<LhsScalar>::MulCost + NumTraits<RhsScalar>::MulCost), // rough estimate!
00224     PacketAccess = scalar_quotient_op<LhsScalar,RhsScalar>::Vectorizable
00225   };
00226 };
00227 
00228 
00229 
00235 struct scalar_boolean_and_op {
00236   EIGEN_EMPTY_STRUCT_CTOR(scalar_boolean_and_op)
00237   EIGEN_STRONG_INLINE bool operator() (const bool& a, const bool& b) const { return a && b; }
00238 };
00239 template<> struct functor_traits<scalar_boolean_and_op> {
00240   enum {
00241     Cost = NumTraits<bool>::AddCost,
00242     PacketAccess = false
00243   };
00244 };
00245 
00251 struct scalar_boolean_or_op {
00252   EIGEN_EMPTY_STRUCT_CTOR(scalar_boolean_or_op)
00253   EIGEN_STRONG_INLINE bool operator() (const bool& a, const bool& b) const { return a || b; }
00254 };
00255 template<> struct functor_traits<scalar_boolean_or_op> {
00256   enum {
00257     Cost = NumTraits<bool>::AddCost,
00258     PacketAccess = false
00259   };
00260 };
00261 
00262 // unary functors:
00263 
00269 template<typename Scalar> struct scalar_opposite_op {
00270   EIGEN_EMPTY_STRUCT_CTOR(scalar_opposite_op)
00271   EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a) const { return -a; }
00272   template<typename Packet>
00273   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const
00274   { return internal::pnegate(a); }
00275 };
00276 template<typename Scalar>
00277 struct functor_traits<scalar_opposite_op<Scalar> >
00278 { enum {
00279     Cost = NumTraits<Scalar>::AddCost,
00280     PacketAccess = packet_traits<Scalar>::HasNegate };
00281 };
00282 
00288 template<typename Scalar> struct scalar_abs_op {
00289   EIGEN_EMPTY_STRUCT_CTOR(scalar_abs_op)
00290   typedef typename NumTraits<Scalar>::Real result_type;
00291   EIGEN_STRONG_INLINE const result_type operator() (const Scalar& a) const { using std::abs; return abs(a); }
00292   template<typename Packet>
00293   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const
00294   { return internal::pabs(a); }
00295 };
00296 template<typename Scalar>
00297 struct functor_traits<scalar_abs_op<Scalar> >
00298 {
00299   enum {
00300     Cost = NumTraits<Scalar>::AddCost,
00301     PacketAccess = packet_traits<Scalar>::HasAbs
00302   };
00303 };
00304 
00310 template<typename Scalar> struct scalar_abs2_op {
00311   EIGEN_EMPTY_STRUCT_CTOR(scalar_abs2_op)
00312   typedef typename NumTraits<Scalar>::Real result_type;
00313   EIGEN_STRONG_INLINE const result_type operator() (const Scalar& a) const { return numext::abs2(a); }
00314   template<typename Packet>
00315   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const
00316   { return internal::pmul(a,a); }
00317 };
00318 template<typename Scalar>
00319 struct functor_traits<scalar_abs2_op<Scalar> >
00320 { enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasAbs2 }; };
00321 
00327 template<typename Scalar> struct scalar_conjugate_op {
00328   EIGEN_EMPTY_STRUCT_CTOR(scalar_conjugate_op)
00329   EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a) const { using numext::conj; return conj(a); }
00330   template<typename Packet>
00331   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const { return internal::pconj(a); }
00332 };
00333 template<typename Scalar>
00334 struct functor_traits<scalar_conjugate_op<Scalar> >
00335 {
00336   enum {
00337     Cost = NumTraits<Scalar>::IsComplex ? NumTraits<Scalar>::AddCost : 0,
00338     PacketAccess = packet_traits<Scalar>::HasConj
00339   };
00340 };
00341 
00347 template<typename Scalar, typename NewType>
00348 struct scalar_cast_op {
00349   EIGEN_EMPTY_STRUCT_CTOR(scalar_cast_op)
00350   typedef NewType result_type;
00351   EIGEN_STRONG_INLINE const NewType operator() (const Scalar& a) const { return cast<Scalar, NewType>(a); }
00352 };
00353 template<typename Scalar, typename NewType>
00354 struct functor_traits<scalar_cast_op<Scalar,NewType> >
00355 { enum { Cost = is_same<Scalar, NewType>::value ? 0 : NumTraits<NewType>::AddCost, PacketAccess = false }; };
00356 
00362 template<typename Scalar>
00363 struct scalar_real_op {
00364   EIGEN_EMPTY_STRUCT_CTOR(scalar_real_op)
00365   typedef typename NumTraits<Scalar>::Real result_type;
00366   EIGEN_STRONG_INLINE result_type operator() (const Scalar& a) const { return numext::real(a); }
00367 };
00368 template<typename Scalar>
00369 struct functor_traits<scalar_real_op<Scalar> >
00370 { enum { Cost = 0, PacketAccess = false }; };
00371 
00377 template<typename Scalar>
00378 struct scalar_imag_op {
00379   EIGEN_EMPTY_STRUCT_CTOR(scalar_imag_op)
00380   typedef typename NumTraits<Scalar>::Real result_type;
00381   EIGEN_STRONG_INLINE result_type operator() (const Scalar& a) const { return numext::imag(a); }
00382 };
00383 template<typename Scalar>
00384 struct functor_traits<scalar_imag_op<Scalar> >
00385 { enum { Cost = 0, PacketAccess = false }; };
00386 
00392 template<typename Scalar>
00393 struct scalar_real_ref_op {
00394   EIGEN_EMPTY_STRUCT_CTOR(scalar_real_ref_op)
00395   typedef typename NumTraits<Scalar>::Real result_type;
00396   EIGEN_STRONG_INLINE result_type& operator() (const Scalar& a) const { return numext::real_ref(*const_cast<Scalar*>(&a)); }
00397 };
00398 template<typename Scalar>
00399 struct functor_traits<scalar_real_ref_op<Scalar> >
00400 { enum { Cost = 0, PacketAccess = false }; };
00401 
00407 template<typename Scalar>
00408 struct scalar_imag_ref_op {
00409   EIGEN_EMPTY_STRUCT_CTOR(scalar_imag_ref_op)
00410   typedef typename NumTraits<Scalar>::Real result_type;
00411   EIGEN_STRONG_INLINE result_type& operator() (const Scalar& a) const { return numext::imag_ref(*const_cast<Scalar*>(&a)); }
00412 };
00413 template<typename Scalar>
00414 struct functor_traits<scalar_imag_ref_op<Scalar> >
00415 { enum { Cost = 0, PacketAccess = false }; };
00416 
00423 template<typename Scalar> struct scalar_exp_op {
00424   EIGEN_EMPTY_STRUCT_CTOR(scalar_exp_op)
00425   inline const Scalar operator() (const Scalar& a) const { using std::exp; return exp(a); }
00426   typedef typename packet_traits<Scalar>::type Packet;
00427   inline Packet packetOp(const Packet& a) const { return internal::pexp(a); }
00428 };
00429 template<typename Scalar>
00430 struct functor_traits<scalar_exp_op<Scalar> >
00431 { enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasExp }; };
00432 
00439 template<typename Scalar> struct scalar_log_op {
00440   EIGEN_EMPTY_STRUCT_CTOR(scalar_log_op)
00441   inline const Scalar operator() (const Scalar& a) const { using std::log; return log(a); }
00442   typedef typename packet_traits<Scalar>::type Packet;
00443   inline Packet packetOp(const Packet& a) const { return internal::plog(a); }
00444 };
00445 template<typename Scalar>
00446 struct functor_traits<scalar_log_op<Scalar> >
00447 { enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasLog }; };
00448 
00454 /* NOTE why doing the pset1() in packetOp *is* an optimization ?
00455  * indeed it seems better to declare m_other as a Packet and do the pset1() once
00456  * in the constructor. However, in practice:
00457  *  - GCC does not like m_other as a Packet and generate a load every time it needs it
00458  *  - on the other hand GCC is able to moves the pset1() outside the loop :)
00459  *  - simpler code ;)
00460  * (ICC and gcc 4.4 seems to perform well in both cases, the issue is visible with y = a*x + b*y)
00461  */
00462 template<typename Scalar>
00463 struct scalar_multiple_op {
00464   typedef typename packet_traits<Scalar>::type Packet;
00465   // FIXME default copy constructors seems bugged with std::complex<>
00466   EIGEN_STRONG_INLINE scalar_multiple_op(const scalar_multiple_op& other) : m_other(other.m_other) { }
00467   EIGEN_STRONG_INLINE scalar_multiple_op(const Scalar& other) : m_other(other) { }
00468   EIGEN_STRONG_INLINE Scalar operator() (const Scalar& a) const { return a * m_other; }
00469   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const
00470   { return internal::pmul(a, pset1<Packet>(m_other)); }
00471   typename add_const_on_value_type<typename NumTraits<Scalar>::Nested>::type m_other;
00472 };
00473 template<typename Scalar>
00474 struct functor_traits<scalar_multiple_op<Scalar> >
00475 { enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasMul }; };
00476 
00477 template<typename Scalar1, typename Scalar2>
00478 struct scalar_multiple2_op {
00479   typedef typename scalar_product_traits<Scalar1,Scalar2>::ReturnType result_type;
00480   EIGEN_STRONG_INLINE scalar_multiple2_op(const scalar_multiple2_op& other) : m_other(other.m_other) { }
00481   EIGEN_STRONG_INLINE scalar_multiple2_op(const Scalar2& other) : m_other(other) { }
00482   EIGEN_STRONG_INLINE result_type operator() (const Scalar1& a) const { return a * m_other; }
00483   typename add_const_on_value_type<typename NumTraits<Scalar2>::Nested>::type m_other;
00484 };
00485 template<typename Scalar1,typename Scalar2>
00486 struct functor_traits<scalar_multiple2_op<Scalar1,Scalar2> >
00487 { enum { Cost = NumTraits<Scalar1>::MulCost, PacketAccess = false }; };
00488 
00497 template<typename Scalar>
00498 struct scalar_quotient1_op {
00499   typedef typename packet_traits<Scalar>::type Packet;
00500   // FIXME default copy constructors seems bugged with std::complex<>
00501   EIGEN_STRONG_INLINE scalar_quotient1_op(const scalar_quotient1_op& other) : m_other(other.m_other) { }
00502   EIGEN_STRONG_INLINE scalar_quotient1_op(const Scalar& other) : m_other(other) {}
00503   EIGEN_STRONG_INLINE Scalar operator() (const Scalar& a) const { return a / m_other; }
00504   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const
00505   { return internal::pdiv(a, pset1<Packet>(m_other)); }
00506   typename add_const_on_value_type<typename NumTraits<Scalar>::Nested>::type m_other;
00507 };
00508 template<typename Scalar>
00509 struct functor_traits<scalar_quotient1_op<Scalar> >
00510 { enum { Cost = 2 * NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasDiv }; };
00511 
00512 // nullary functors
00513 
00514 template<typename Scalar>
00515 struct scalar_constant_op {
00516   typedef typename packet_traits<Scalar>::type Packet;
00517   EIGEN_STRONG_INLINE scalar_constant_op(const scalar_constant_op& other) : m_other(other.m_other) { }
00518   EIGEN_STRONG_INLINE scalar_constant_op(const Scalar& other) : m_other(other) { }
00519   template<typename Index>
00520   EIGEN_STRONG_INLINE const Scalar operator() (Index, Index = 0) const { return m_other; }
00521   template<typename Index>
00522   EIGEN_STRONG_INLINE const Packet packetOp(Index, Index = 0) const { return internal::pset1<Packet>(m_other); }
00523   const Scalar m_other;
00524 };
00525 template<typename Scalar>
00526 struct functor_traits<scalar_constant_op<Scalar> >
00527 // FIXME replace this packet test by a safe one
00528 { enum { Cost = 1, PacketAccess = packet_traits<Scalar>::Vectorizable, IsRepeatable = true }; };
00529 
00530 template<typename Scalar> struct scalar_identity_op {
00531   EIGEN_EMPTY_STRUCT_CTOR(scalar_identity_op)
00532   template<typename Index>
00533   EIGEN_STRONG_INLINE const Scalar operator() (Index row, Index col) const { return row==col ? Scalar(1) : Scalar(0); }
00534 };
00535 template<typename Scalar>
00536 struct functor_traits<scalar_identity_op<Scalar> >
00537 { enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = false, IsRepeatable = true }; };
00538 
00539 template <typename Scalar, bool RandomAccess> struct linspaced_op_impl;
00540 
00541 // linear access for packet ops:
00542 // 1) initialization
00543 //   base = [low, ..., low] + ([step, ..., step] * [-size, ..., 0])
00544 // 2) each step (where size is 1 for coeff access or PacketSize for packet access)
00545 //   base += [size*step, ..., size*step]
00546 //
00547 // TODO: Perhaps it's better to initialize lazily (so not in the constructor but in packetOp)
00548 //       in order to avoid the padd() in operator() ?
00549 template <typename Scalar>
00550 struct linspaced_op_impl<Scalar,false>
00551 {
00552   typedef typename packet_traits<Scalar>::type Packet;
00553 
00554   linspaced_op_impl(const Scalar& low, const Scalar& step) :
00555   m_low(low), m_step(step),
00556   m_packetStep(pset1<Packet>(packet_traits<Scalar>::size*step)),
00557   m_base(padd(pset1<Packet>(low), pmul(pset1<Packet>(step),plset<Scalar>(-packet_traits<Scalar>::size)))) {}
00558 
00559   template<typename Index>
00560   EIGEN_STRONG_INLINE const Scalar operator() (Index i) const 
00561   { 
00562     m_base = padd(m_base, pset1<Packet>(m_step));
00563     return m_low+Scalar(i)*m_step; 
00564   }
00565 
00566   template<typename Index>
00567   EIGEN_STRONG_INLINE const Packet packetOp(Index) const { return m_base = padd(m_base,m_packetStep); }
00568 
00569   const Scalar m_low;
00570   const Scalar m_step;
00571   const Packet m_packetStep;
00572   mutable Packet m_base;
00573 };
00574 
00575 // random access for packet ops:
00576 // 1) each step
00577 //   [low, ..., low] + ( [step, ..., step] * ( [i, ..., i] + [0, ..., size] ) )
00578 template <typename Scalar>
00579 struct linspaced_op_impl<Scalar,true>
00580 {
00581   typedef typename packet_traits<Scalar>::type Packet;
00582 
00583   linspaced_op_impl(const Scalar& low, const Scalar& step) :
00584   m_low(low), m_step(step),
00585   m_lowPacket(pset1<Packet>(m_low)), m_stepPacket(pset1<Packet>(m_step)), m_interPacket(plset<Scalar>(0)) {}
00586 
00587   template<typename Index>
00588   EIGEN_STRONG_INLINE const Scalar operator() (Index i) const { return m_low+i*m_step; }
00589 
00590   template<typename Index>
00591   EIGEN_STRONG_INLINE const Packet packetOp(Index i) const
00592   { return internal::padd(m_lowPacket, pmul(m_stepPacket, padd(pset1<Packet>(i),m_interPacket))); }
00593 
00594   const Scalar m_low;
00595   const Scalar m_step;
00596   const Packet m_lowPacket;
00597   const Packet m_stepPacket;
00598   const Packet m_interPacket;
00599 };
00600 
00601 // ----- Linspace functor ----------------------------------------------------------------
00602 
00603 // Forward declaration (we default to random access which does not really give
00604 // us a speed gain when using packet access but it allows to use the functor in
00605 // nested expressions).
00606 template <typename Scalar, bool RandomAccess = true> struct linspaced_op;
00607 template <typename Scalar, bool RandomAccess> struct functor_traits< linspaced_op<Scalar,RandomAccess> >
00608 { enum { Cost = 1, PacketAccess = packet_traits<Scalar>::HasSetLinear, IsRepeatable = true }; };
00609 template <typename Scalar, bool RandomAccess> struct linspaced_op
00610 {
00611   typedef typename packet_traits<Scalar>::type Packet;
00612   linspaced_op(const Scalar& low, const Scalar& high, DenseIndex num_steps) : impl((num_steps==1 ? high : low), (num_steps==1 ? Scalar() : (high-low)/(num_steps-1))) {}
00613 
00614   template<typename Index>
00615   EIGEN_STRONG_INLINE const Scalar operator() (Index i) const { return impl(i); }
00616 
00617   // We need this function when assigning e.g. a RowVectorXd to a MatrixXd since
00618   // there row==0 and col is used for the actual iteration.
00619   template<typename Index>
00620   EIGEN_STRONG_INLINE const Scalar operator() (Index row, Index col) const 
00621   {
00622     eigen_assert(col==0 || row==0);
00623     return impl(col + row);
00624   }
00625 
00626   template<typename Index>
00627   EIGEN_STRONG_INLINE const Packet packetOp(Index i) const { return impl.packetOp(i); }
00628 
00629   // We need this function when assigning e.g. a RowVectorXd to a MatrixXd since
00630   // there row==0 and col is used for the actual iteration.
00631   template<typename Index>
00632   EIGEN_STRONG_INLINE const Packet packetOp(Index row, Index col) const
00633   {
00634     eigen_assert(col==0 || row==0);
00635     return impl.packetOp(col + row);
00636   }
00637 
00638   // This proxy object handles the actual required temporaries, the different
00639   // implementations (random vs. sequential access) as well as the
00640   // correct piping to size 2/4 packet operations.
00641   const linspaced_op_impl<Scalar,RandomAccess> impl;
00642 };
00643 
00644 // all functors allow linear access, except scalar_identity_op. So we fix here a quick meta
00645 // to indicate whether a functor allows linear access, just always answering 'yes' except for
00646 // scalar_identity_op.
00647 // FIXME move this to functor_traits adding a functor_default
00648 template<typename Functor> struct functor_has_linear_access { enum { ret = 1 }; };
00649 template<typename Scalar> struct functor_has_linear_access<scalar_identity_op<Scalar> > { enum { ret = 0 }; };
00650 
00651 // In Eigen, any binary op (Product, CwiseBinaryOp) require the Lhs and Rhs to have the same scalar type, except for multiplication
00652 // where the mixing of different types is handled by scalar_product_traits
00653 // In particular, real * complex<real> is allowed.
00654 // FIXME move this to functor_traits adding a functor_default
00655 template<typename Functor> struct functor_is_product_like { enum { ret = 0 }; };
00656 template<typename LhsScalar,typename RhsScalar> struct functor_is_product_like<scalar_product_op<LhsScalar,RhsScalar> > { enum { ret = 1 }; };
00657 template<typename LhsScalar,typename RhsScalar> struct functor_is_product_like<scalar_conj_product_op<LhsScalar,RhsScalar> > { enum { ret = 1 }; };
00658 template<typename LhsScalar,typename RhsScalar> struct functor_is_product_like<scalar_quotient_op<LhsScalar,RhsScalar> > { enum { ret = 1 }; };
00659 
00660 
00665 /* If you wonder why doing the pset1() in packetOp() is an optimization check scalar_multiple_op */
00666 template<typename Scalar>
00667 struct scalar_add_op {
00668   typedef typename packet_traits<Scalar>::type Packet;
00669   // FIXME default copy constructors seems bugged with std::complex<>
00670   inline scalar_add_op(const scalar_add_op& other) : m_other(other.m_other) { }
00671   inline scalar_add_op(const Scalar& other) : m_other(other) { }
00672   inline Scalar operator() (const Scalar& a) const { return a + m_other; }
00673   inline const Packet packetOp(const Packet& a) const
00674   { return internal::padd(a, pset1<Packet>(m_other)); }
00675   const Scalar m_other;
00676 };
00677 template<typename Scalar>
00678 struct functor_traits<scalar_add_op<Scalar> >
00679 { enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = packet_traits<Scalar>::HasAdd }; };
00680 
00685 template<typename Scalar> struct scalar_sqrt_op {
00686   EIGEN_EMPTY_STRUCT_CTOR(scalar_sqrt_op)
00687   inline const Scalar operator() (const Scalar& a) const { using std::sqrt; return sqrt(a); }
00688   typedef typename packet_traits<Scalar>::type Packet;
00689   inline Packet packetOp(const Packet& a) const { return internal::psqrt(a); }
00690 };
00691 template<typename Scalar>
00692 struct functor_traits<scalar_sqrt_op<Scalar> >
00693 { enum {
00694     Cost = 5 * NumTraits<Scalar>::MulCost,
00695     PacketAccess = packet_traits<Scalar>::HasSqrt
00696   };
00697 };
00698 
00703 template<typename Scalar> struct scalar_cos_op {
00704   EIGEN_EMPTY_STRUCT_CTOR(scalar_cos_op)
00705   inline Scalar operator() (const Scalar& a) const { using std::cos; return cos(a); }
00706   typedef typename packet_traits<Scalar>::type Packet;
00707   inline Packet packetOp(const Packet& a) const { return internal::pcos(a); }
00708 };
00709 template<typename Scalar>
00710 struct functor_traits<scalar_cos_op<Scalar> >
00711 {
00712   enum {
00713     Cost = 5 * NumTraits<Scalar>::MulCost,
00714     PacketAccess = packet_traits<Scalar>::HasCos
00715   };
00716 };
00717 
00722 template<typename Scalar> struct scalar_sin_op {
00723   EIGEN_EMPTY_STRUCT_CTOR(scalar_sin_op)
00724   inline const Scalar operator() (const Scalar& a) const { using std::sin; return sin(a); }
00725   typedef typename packet_traits<Scalar>::type Packet;
00726   inline Packet packetOp(const Packet& a) const { return internal::psin(a); }
00727 };
00728 template<typename Scalar>
00729 struct functor_traits<scalar_sin_op<Scalar> >
00730 {
00731   enum {
00732     Cost = 5 * NumTraits<Scalar>::MulCost,
00733     PacketAccess = packet_traits<Scalar>::HasSin
00734   };
00735 };
00736 
00737 
00742 template<typename Scalar> struct scalar_tan_op {
00743   EIGEN_EMPTY_STRUCT_CTOR(scalar_tan_op)
00744   inline const Scalar operator() (const Scalar& a) const { using std::tan; return tan(a); }
00745   typedef typename packet_traits<Scalar>::type Packet;
00746   inline Packet packetOp(const Packet& a) const { return internal::ptan(a); }
00747 };
00748 template<typename Scalar>
00749 struct functor_traits<scalar_tan_op<Scalar> >
00750 {
00751   enum {
00752     Cost = 5 * NumTraits<Scalar>::MulCost,
00753     PacketAccess = packet_traits<Scalar>::HasTan
00754   };
00755 };
00756 
00761 template<typename Scalar> struct scalar_acos_op {
00762   EIGEN_EMPTY_STRUCT_CTOR(scalar_acos_op)
00763   inline const Scalar operator() (const Scalar& a) const { using std::acos; return acos(a); }
00764   typedef typename packet_traits<Scalar>::type Packet;
00765   inline Packet packetOp(const Packet& a) const { return internal::pacos(a); }
00766 };
00767 template<typename Scalar>
00768 struct functor_traits<scalar_acos_op<Scalar> >
00769 {
00770   enum {
00771     Cost = 5 * NumTraits<Scalar>::MulCost,
00772     PacketAccess = packet_traits<Scalar>::HasACos
00773   };
00774 };
00775 
00780 template<typename Scalar> struct scalar_asin_op {
00781   EIGEN_EMPTY_STRUCT_CTOR(scalar_asin_op)
00782   inline const Scalar operator() (const Scalar& a) const { using std::asin; return asin(a); }
00783   typedef typename packet_traits<Scalar>::type Packet;
00784   inline Packet packetOp(const Packet& a) const { return internal::pasin(a); }
00785 };
00786 template<typename Scalar>
00787 struct functor_traits<scalar_asin_op<Scalar> >
00788 {
00789   enum {
00790     Cost = 5 * NumTraits<Scalar>::MulCost,
00791     PacketAccess = packet_traits<Scalar>::HasASin
00792   };
00793 };
00794 
00799 template<typename Scalar>
00800 struct scalar_pow_op {
00801   // FIXME default copy constructors seems bugged with std::complex<>
00802   inline scalar_pow_op(const scalar_pow_op& other) : m_exponent(other.m_exponent) { }
00803   inline scalar_pow_op(const Scalar& exponent) : m_exponent(exponent) {}
00804   inline Scalar operator() (const Scalar& a) const { return numext::pow(a, m_exponent); }
00805   const Scalar m_exponent;
00806 };
00807 template<typename Scalar>
00808 struct functor_traits<scalar_pow_op<Scalar> >
00809 { enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = false }; };
00810 
00815 template<typename Scalar>
00816 struct scalar_inverse_mult_op {
00817   scalar_inverse_mult_op(const Scalar& other) : m_other(other) {}
00818   inline Scalar operator() (const Scalar& a) const { return m_other / a; }
00819   template<typename Packet>
00820   inline const Packet packetOp(const Packet& a) const
00821   { return internal::pdiv(pset1<Packet>(m_other),a); }
00822   Scalar m_other;
00823 };
00824 
00829 template<typename Scalar>
00830 struct scalar_inverse_op {
00831   EIGEN_EMPTY_STRUCT_CTOR(scalar_inverse_op)
00832   inline Scalar operator() (const Scalar& a) const { return Scalar(1)/a; }
00833   template<typename Packet>
00834   inline const Packet packetOp(const Packet& a) const
00835   { return internal::pdiv(pset1<Packet>(Scalar(1)),a); }
00836 };
00837 template<typename Scalar>
00838 struct functor_traits<scalar_inverse_op<Scalar> >
00839 { enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasDiv }; };
00840 
00845 template<typename Scalar>
00846 struct scalar_square_op {
00847   EIGEN_EMPTY_STRUCT_CTOR(scalar_square_op)
00848   inline Scalar operator() (const Scalar& a) const { return a*a; }
00849   template<typename Packet>
00850   inline const Packet packetOp(const Packet& a) const
00851   { return internal::pmul(a,a); }
00852 };
00853 template<typename Scalar>
00854 struct functor_traits<scalar_square_op<Scalar> >
00855 { enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasMul }; };
00856 
00861 template<typename Scalar>
00862 struct scalar_cube_op {
00863   EIGEN_EMPTY_STRUCT_CTOR(scalar_cube_op)
00864   inline Scalar operator() (const Scalar& a) const { return a*a*a; }
00865   template<typename Packet>
00866   inline const Packet packetOp(const Packet& a) const
00867   { return internal::pmul(a,pmul(a,a)); }
00868 };
00869 template<typename Scalar>
00870 struct functor_traits<scalar_cube_op<Scalar> >
00871 { enum { Cost = 2*NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasMul }; };
00872 
00873 // default functor traits for STL functors:
00874 
00875 template<typename T>
00876 struct functor_traits<std::multiplies<T> >
00877 { enum { Cost = NumTraits<T>::MulCost, PacketAccess = false }; };
00878 
00879 template<typename T>
00880 struct functor_traits<std::divides<T> >
00881 { enum { Cost = NumTraits<T>::MulCost, PacketAccess = false }; };
00882 
00883 template<typename T>
00884 struct functor_traits<std::plus<T> >
00885 { enum { Cost = NumTraits<T>::AddCost, PacketAccess = false }; };
00886 
00887 template<typename T>
00888 struct functor_traits<std::minus<T> >
00889 { enum { Cost = NumTraits<T>::AddCost, PacketAccess = false }; };
00890 
00891 template<typename T>
00892 struct functor_traits<std::negate<T> >
00893 { enum { Cost = NumTraits<T>::AddCost, PacketAccess = false }; };
00894 
00895 template<typename T>
00896 struct functor_traits<std::logical_or<T> >
00897 { enum { Cost = 1, PacketAccess = false }; };
00898 
00899 template<typename T>
00900 struct functor_traits<std::logical_and<T> >
00901 { enum { Cost = 1, PacketAccess = false }; };
00902 
00903 template<typename T>
00904 struct functor_traits<std::logical_not<T> >
00905 { enum { Cost = 1, PacketAccess = false }; };
00906 
00907 template<typename T>
00908 struct functor_traits<std::greater<T> >
00909 { enum { Cost = 1, PacketAccess = false }; };
00910 
00911 template<typename T>
00912 struct functor_traits<std::less<T> >
00913 { enum { Cost = 1, PacketAccess = false }; };
00914 
00915 template<typename T>
00916 struct functor_traits<std::greater_equal<T> >
00917 { enum { Cost = 1, PacketAccess = false }; };
00918 
00919 template<typename T>
00920 struct functor_traits<std::less_equal<T> >
00921 { enum { Cost = 1, PacketAccess = false }; };
00922 
00923 template<typename T>
00924 struct functor_traits<std::equal_to<T> >
00925 { enum { Cost = 1, PacketAccess = false }; };
00926 
00927 template<typename T>
00928 struct functor_traits<std::not_equal_to<T> >
00929 { enum { Cost = 1, PacketAccess = false }; };
00930 
00931 template<typename T>
00932 struct functor_traits<std::binder2nd<T> >
00933 { enum { Cost = functor_traits<T>::Cost, PacketAccess = false }; };
00934 
00935 template<typename T>
00936 struct functor_traits<std::binder1st<T> >
00937 { enum { Cost = functor_traits<T>::Cost, PacketAccess = false }; };
00938 
00939 template<typename T>
00940 struct functor_traits<std::unary_negate<T> >
00941 { enum { Cost = 1 + functor_traits<T>::Cost, PacketAccess = false }; };
00942 
00943 template<typename T>
00944 struct functor_traits<std::binary_negate<T> >
00945 { enum { Cost = 1 + functor_traits<T>::Cost, PacketAccess = false }; };
00946 
00947 #ifdef EIGEN_STDEXT_SUPPORT
00948 
00949 template<typename T0,typename T1>
00950 struct functor_traits<std::project1st<T0,T1> >
00951 { enum { Cost = 0, PacketAccess = false }; };
00952 
00953 template<typename T0,typename T1>
00954 struct functor_traits<std::project2nd<T0,T1> >
00955 { enum { Cost = 0, PacketAccess = false }; };
00956 
00957 template<typename T0,typename T1>
00958 struct functor_traits<std::select2nd<std::pair<T0,T1> > >
00959 { enum { Cost = 0, PacketAccess = false }; };
00960 
00961 template<typename T0,typename T1>
00962 struct functor_traits<std::select1st<std::pair<T0,T1> > >
00963 { enum { Cost = 0, PacketAccess = false }; };
00964 
00965 template<typename T0,typename T1>
00966 struct functor_traits<std::unary_compose<T0,T1> >
00967 { enum { Cost = functor_traits<T0>::Cost + functor_traits<T1>::Cost, PacketAccess = false }; };
00968 
00969 template<typename T0,typename T1,typename T2>
00970 struct functor_traits<std::binary_compose<T0,T1,T2> >
00971 { enum { Cost = functor_traits<T0>::Cost + functor_traits<T1>::Cost + functor_traits<T2>::Cost, PacketAccess = false }; };
00972 
00973 #endif // EIGEN_STDEXT_SUPPORT
00974 
00975 // allow to add new functors and specializations of functor_traits from outside Eigen.
00976 // this macro is really needed because functor_traits must be specialized after it is declared but before it is used...
00977 #ifdef EIGEN_FUNCTORS_PLUGIN
00978 #include EIGEN_FUNCTORS_PLUGIN
00979 #endif
00980 
00981 } // end namespace internal
00982 
00983 } // end namespace Eigen
00984 
00985 #endif // EIGEN_FUNCTORS_H


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Sat Jun 8 2019 19:37:10