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     Scalar p = (max)(_x, _y);
00158     Scalar q = (min)(_x, _y);
00159     Scalar qp = q/p;
00160     return p * sqrt(Scalar(1) + qp*qp);
00161   }
00162 };
00163 template<typename Scalar>
00164 struct functor_traits<scalar_hypot_op<Scalar> > {
00165   enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess=0 };
00166 };
00167 
00171 template<typename Scalar, typename OtherScalar> struct scalar_binary_pow_op {
00172   EIGEN_EMPTY_STRUCT_CTOR(scalar_binary_pow_op)
00173   inline Scalar operator() (const Scalar& a, const OtherScalar& b) const { return internal::pow(a, b); }
00174 };
00175 template<typename Scalar, typename OtherScalar>
00176 struct functor_traits<scalar_binary_pow_op<Scalar,OtherScalar> > {
00177   enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = false };
00178 };
00179 
00180 // other binary functors:
00181 
00187 template<typename Scalar> struct scalar_difference_op {
00188   EIGEN_EMPTY_STRUCT_CTOR(scalar_difference_op)
00189   EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a, const Scalar& b) const { return a - b; }
00190   template<typename Packet>
00191   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
00192   { return internal::psub(a,b); }
00193 };
00194 template<typename Scalar>
00195 struct functor_traits<scalar_difference_op<Scalar> > {
00196   enum {
00197     Cost = NumTraits<Scalar>::AddCost,
00198     PacketAccess = packet_traits<Scalar>::HasSub
00199   };
00200 };
00201 
00207 template<typename Scalar> struct scalar_quotient_op {
00208   EIGEN_EMPTY_STRUCT_CTOR(scalar_quotient_op)
00209   EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a, const Scalar& b) const { return a / b; }
00210   template<typename Packet>
00211   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
00212   { return internal::pdiv(a,b); }
00213 };
00214 template<typename Scalar>
00215 struct functor_traits<scalar_quotient_op<Scalar> > {
00216   enum {
00217     Cost = 2 * NumTraits<Scalar>::MulCost,
00218     PacketAccess = packet_traits<Scalar>::HasDiv
00219   };
00220 };
00221 
00227 struct scalar_boolean_and_op {
00228   EIGEN_EMPTY_STRUCT_CTOR(scalar_boolean_and_op)
00229   EIGEN_STRONG_INLINE bool operator() (const bool& a, const bool& b) const { return a && b; }
00230 };
00231 template<> struct functor_traits<scalar_boolean_and_op> {
00232   enum {
00233     Cost = NumTraits<bool>::AddCost,
00234     PacketAccess = false
00235   };
00236 };
00237 
00243 struct scalar_boolean_or_op {
00244   EIGEN_EMPTY_STRUCT_CTOR(scalar_boolean_or_op)
00245   EIGEN_STRONG_INLINE bool operator() (const bool& a, const bool& b) const { return a || b; }
00246 };
00247 template<> struct functor_traits<scalar_boolean_or_op> {
00248   enum {
00249     Cost = NumTraits<bool>::AddCost,
00250     PacketAccess = false
00251   };
00252 };
00253 
00254 // unary functors:
00255 
00261 template<typename Scalar> struct scalar_opposite_op {
00262   EIGEN_EMPTY_STRUCT_CTOR(scalar_opposite_op)
00263   EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a) const { return -a; }
00264   template<typename Packet>
00265   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const
00266   { return internal::pnegate(a); }
00267 };
00268 template<typename Scalar>
00269 struct functor_traits<scalar_opposite_op<Scalar> >
00270 { enum {
00271     Cost = NumTraits<Scalar>::AddCost,
00272     PacketAccess = packet_traits<Scalar>::HasNegate };
00273 };
00274 
00280 template<typename Scalar> struct scalar_abs_op {
00281   EIGEN_EMPTY_STRUCT_CTOR(scalar_abs_op)
00282   typedef typename NumTraits<Scalar>::Real result_type;
00283   EIGEN_STRONG_INLINE const result_type operator() (const Scalar& a) const { return internal::abs(a); }
00284   template<typename Packet>
00285   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const
00286   { return internal::pabs(a); }
00287 };
00288 template<typename Scalar>
00289 struct functor_traits<scalar_abs_op<Scalar> >
00290 {
00291   enum {
00292     Cost = NumTraits<Scalar>::AddCost,
00293     PacketAccess = packet_traits<Scalar>::HasAbs
00294   };
00295 };
00296 
00302 template<typename Scalar> struct scalar_abs2_op {
00303   EIGEN_EMPTY_STRUCT_CTOR(scalar_abs2_op)
00304   typedef typename NumTraits<Scalar>::Real result_type;
00305   EIGEN_STRONG_INLINE const result_type operator() (const Scalar& a) const { return internal::abs2(a); }
00306   template<typename Packet>
00307   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const
00308   { return internal::pmul(a,a); }
00309 };
00310 template<typename Scalar>
00311 struct functor_traits<scalar_abs2_op<Scalar> >
00312 { enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasAbs2 }; };
00313 
00319 template<typename Scalar> struct scalar_conjugate_op {
00320   EIGEN_EMPTY_STRUCT_CTOR(scalar_conjugate_op)
00321   EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a) const { return internal::conj(a); }
00322   template<typename Packet>
00323   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const { return internal::pconj(a); }
00324 };
00325 template<typename Scalar>
00326 struct functor_traits<scalar_conjugate_op<Scalar> >
00327 {
00328   enum {
00329     Cost = NumTraits<Scalar>::IsComplex ? NumTraits<Scalar>::AddCost : 0,
00330     PacketAccess = packet_traits<Scalar>::HasConj
00331   };
00332 };
00333 
00339 template<typename Scalar, typename NewType>
00340 struct scalar_cast_op {
00341   EIGEN_EMPTY_STRUCT_CTOR(scalar_cast_op)
00342   typedef NewType result_type;
00343   EIGEN_STRONG_INLINE const NewType operator() (const Scalar& a) const { return cast<Scalar, NewType>(a); }
00344 };
00345 template<typename Scalar, typename NewType>
00346 struct functor_traits<scalar_cast_op<Scalar,NewType> >
00347 { enum { Cost = is_same<Scalar, NewType>::value ? 0 : NumTraits<NewType>::AddCost, PacketAccess = false }; };
00348 
00354 template<typename Scalar>
00355 struct scalar_real_op {
00356   EIGEN_EMPTY_STRUCT_CTOR(scalar_real_op)
00357   typedef typename NumTraits<Scalar>::Real result_type;
00358   EIGEN_STRONG_INLINE result_type operator() (const Scalar& a) const { return internal::real(a); }
00359 };
00360 template<typename Scalar>
00361 struct functor_traits<scalar_real_op<Scalar> >
00362 { enum { Cost = 0, PacketAccess = false }; };
00363 
00369 template<typename Scalar>
00370 struct scalar_imag_op {
00371   EIGEN_EMPTY_STRUCT_CTOR(scalar_imag_op)
00372   typedef typename NumTraits<Scalar>::Real result_type;
00373   EIGEN_STRONG_INLINE result_type operator() (const Scalar& a) const { return internal::imag(a); }
00374 };
00375 template<typename Scalar>
00376 struct functor_traits<scalar_imag_op<Scalar> >
00377 { enum { Cost = 0, PacketAccess = false }; };
00378 
00384 template<typename Scalar>
00385 struct scalar_real_ref_op {
00386   EIGEN_EMPTY_STRUCT_CTOR(scalar_real_ref_op)
00387   typedef typename NumTraits<Scalar>::Real result_type;
00388   EIGEN_STRONG_INLINE result_type& operator() (const Scalar& a) const { return internal::real_ref(*const_cast<Scalar*>(&a)); }
00389 };
00390 template<typename Scalar>
00391 struct functor_traits<scalar_real_ref_op<Scalar> >
00392 { enum { Cost = 0, PacketAccess = false }; };
00393 
00399 template<typename Scalar>
00400 struct scalar_imag_ref_op {
00401   EIGEN_EMPTY_STRUCT_CTOR(scalar_imag_ref_op)
00402   typedef typename NumTraits<Scalar>::Real result_type;
00403   EIGEN_STRONG_INLINE result_type& operator() (const Scalar& a) const { return internal::imag_ref(*const_cast<Scalar*>(&a)); }
00404 };
00405 template<typename Scalar>
00406 struct functor_traits<scalar_imag_ref_op<Scalar> >
00407 { enum { Cost = 0, PacketAccess = false }; };
00408 
00415 template<typename Scalar> struct scalar_exp_op {
00416   EIGEN_EMPTY_STRUCT_CTOR(scalar_exp_op)
00417   inline const Scalar operator() (const Scalar& a) const { return internal::exp(a); }
00418   typedef typename packet_traits<Scalar>::type Packet;
00419   inline Packet packetOp(const Packet& a) const { return internal::pexp(a); }
00420 };
00421 template<typename Scalar>
00422 struct functor_traits<scalar_exp_op<Scalar> >
00423 { enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasExp }; };
00424 
00431 template<typename Scalar> struct scalar_log_op {
00432   EIGEN_EMPTY_STRUCT_CTOR(scalar_log_op)
00433   inline const Scalar operator() (const Scalar& a) const { return internal::log(a); }
00434   typedef typename packet_traits<Scalar>::type Packet;
00435   inline Packet packetOp(const Packet& a) const { return internal::plog(a); }
00436 };
00437 template<typename Scalar>
00438 struct functor_traits<scalar_log_op<Scalar> >
00439 { enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasLog }; };
00440 
00446 /* NOTE why doing the pset1() in packetOp *is* an optimization ?
00447  * indeed it seems better to declare m_other as a Packet and do the pset1() once
00448  * in the constructor. However, in practice:
00449  *  - GCC does not like m_other as a Packet and generate a load every time it needs it
00450  *  - on the other hand GCC is able to moves the pset1() outside the loop :)
00451  *  - simpler code ;)
00452  * (ICC and gcc 4.4 seems to perform well in both cases, the issue is visible with y = a*x + b*y)
00453  */
00454 template<typename Scalar>
00455 struct scalar_multiple_op {
00456   typedef typename packet_traits<Scalar>::type Packet;
00457   // FIXME default copy constructors seems bugged with std::complex<>
00458   EIGEN_STRONG_INLINE scalar_multiple_op(const scalar_multiple_op& other) : m_other(other.m_other) { }
00459   EIGEN_STRONG_INLINE scalar_multiple_op(const Scalar& other) : m_other(other) { }
00460   EIGEN_STRONG_INLINE Scalar operator() (const Scalar& a) const { return a * m_other; }
00461   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const
00462   { return internal::pmul(a, pset1<Packet>(m_other)); }
00463   typename add_const_on_value_type<typename NumTraits<Scalar>::Nested>::type m_other;
00464 };
00465 template<typename Scalar>
00466 struct functor_traits<scalar_multiple_op<Scalar> >
00467 { enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasMul }; };
00468 
00469 template<typename Scalar1, typename Scalar2>
00470 struct scalar_multiple2_op {
00471   typedef typename scalar_product_traits<Scalar1,Scalar2>::ReturnType result_type;
00472   EIGEN_STRONG_INLINE scalar_multiple2_op(const scalar_multiple2_op& other) : m_other(other.m_other) { }
00473   EIGEN_STRONG_INLINE scalar_multiple2_op(const Scalar2& other) : m_other(other) { }
00474   EIGEN_STRONG_INLINE result_type operator() (const Scalar1& a) const { return a * m_other; }
00475   typename add_const_on_value_type<typename NumTraits<Scalar2>::Nested>::type m_other;
00476 };
00477 template<typename Scalar1,typename Scalar2>
00478 struct functor_traits<scalar_multiple2_op<Scalar1,Scalar2> >
00479 { enum { Cost = NumTraits<Scalar1>::MulCost, PacketAccess = false }; };
00480 
00489 template<typename Scalar>
00490 struct scalar_quotient1_op {
00491   typedef typename packet_traits<Scalar>::type Packet;
00492   // FIXME default copy constructors seems bugged with std::complex<>
00493   EIGEN_STRONG_INLINE scalar_quotient1_op(const scalar_quotient1_op& other) : m_other(other.m_other) { }
00494   EIGEN_STRONG_INLINE scalar_quotient1_op(const Scalar& other) : m_other(other) {}
00495   EIGEN_STRONG_INLINE Scalar operator() (const Scalar& a) const { return a / m_other; }
00496   EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const
00497   { return internal::pdiv(a, pset1<Packet>(m_other)); }
00498   typename add_const_on_value_type<typename NumTraits<Scalar>::Nested>::type m_other;
00499 };
00500 template<typename Scalar>
00501 struct functor_traits<scalar_quotient1_op<Scalar> >
00502 { enum { Cost = 2 * NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasDiv }; };
00503 
00504 // nullary functors
00505 
00506 template<typename Scalar>
00507 struct scalar_constant_op {
00508   typedef typename packet_traits<Scalar>::type Packet;
00509   EIGEN_STRONG_INLINE scalar_constant_op(const scalar_constant_op& other) : m_other(other.m_other) { }
00510   EIGEN_STRONG_INLINE scalar_constant_op(const Scalar& other) : m_other(other) { }
00511   template<typename Index>
00512   EIGEN_STRONG_INLINE const Scalar operator() (Index, Index = 0) const { return m_other; }
00513   template<typename Index>
00514   EIGEN_STRONG_INLINE const Packet packetOp(Index, Index = 0) const { return internal::pset1<Packet>(m_other); }
00515   const Scalar m_other;
00516 };
00517 template<typename Scalar>
00518 struct functor_traits<scalar_constant_op<Scalar> >
00519 // FIXME replace this packet test by a safe one
00520 { enum { Cost = 1, PacketAccess = packet_traits<Scalar>::Vectorizable, IsRepeatable = true }; };
00521 
00522 template<typename Scalar> struct scalar_identity_op {
00523   EIGEN_EMPTY_STRUCT_CTOR(scalar_identity_op)
00524   template<typename Index>
00525   EIGEN_STRONG_INLINE const Scalar operator() (Index row, Index col) const { return row==col ? Scalar(1) : Scalar(0); }
00526 };
00527 template<typename Scalar>
00528 struct functor_traits<scalar_identity_op<Scalar> >
00529 { enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = false, IsRepeatable = true }; };
00530 
00531 template <typename Scalar, bool RandomAccess> struct linspaced_op_impl;
00532 
00533 // linear access for packet ops:
00534 // 1) initialization
00535 //   base = [low, ..., low] + ([step, ..., step] * [-size, ..., 0])
00536 // 2) each step
00537 //   base += [size*step, ..., size*step]
00538 template <typename Scalar>
00539 struct linspaced_op_impl<Scalar,false>
00540 {
00541   typedef typename packet_traits<Scalar>::type Packet;
00542 
00543   linspaced_op_impl(Scalar low, Scalar step) :
00544   m_low(low), m_step(step),
00545   m_packetStep(pset1<Packet>(packet_traits<Scalar>::size*step)),
00546   m_base(padd(pset1<Packet>(low),pmul(pset1<Packet>(step),plset<Scalar>(-packet_traits<Scalar>::size)))) {}
00547 
00548   template<typename Index>
00549   EIGEN_STRONG_INLINE const Scalar operator() (Index i) const { return m_low+i*m_step; }
00550   template<typename Index>
00551   EIGEN_STRONG_INLINE const Packet packetOp(Index) const { return m_base = padd(m_base,m_packetStep); }
00552 
00553   const Scalar m_low;
00554   const Scalar m_step;
00555   const Packet m_packetStep;
00556   mutable Packet m_base;
00557 };
00558 
00559 // random access for packet ops:
00560 // 1) each step
00561 //   [low, ..., low] + ( [step, ..., step] * ( [i, ..., i] + [0, ..., size] ) )
00562 template <typename Scalar>
00563 struct linspaced_op_impl<Scalar,true>
00564 {
00565   typedef typename packet_traits<Scalar>::type Packet;
00566 
00567   linspaced_op_impl(Scalar low, Scalar step) :
00568   m_low(low), m_step(step),
00569   m_lowPacket(pset1<Packet>(m_low)), m_stepPacket(pset1<Packet>(m_step)), m_interPacket(plset<Scalar>(0)) {}
00570 
00571   template<typename Index>
00572   EIGEN_STRONG_INLINE const Scalar operator() (Index i) const { return m_low+i*m_step; }
00573 
00574   template<typename Index>
00575   EIGEN_STRONG_INLINE const Packet packetOp(Index i) const
00576   { return internal::padd(m_lowPacket, pmul(m_stepPacket, padd(pset1<Packet>(i),m_interPacket))); }
00577 
00578   const Scalar m_low;
00579   const Scalar m_step;
00580   const Packet m_lowPacket;
00581   const Packet m_stepPacket;
00582   const Packet m_interPacket;
00583 };
00584 
00585 // ----- Linspace functor ----------------------------------------------------------------
00586 
00587 // Forward declaration (we default to random access which does not really give
00588 // us a speed gain when using packet access but it allows to use the functor in
00589 // nested expressions).
00590 template <typename Scalar, bool RandomAccess = true> struct linspaced_op;
00591 template <typename Scalar, bool RandomAccess> struct functor_traits< linspaced_op<Scalar,RandomAccess> >
00592 { enum { Cost = 1, PacketAccess = packet_traits<Scalar>::HasSetLinear, IsRepeatable = true }; };
00593 template <typename Scalar, bool RandomAccess> struct linspaced_op
00594 {
00595   typedef typename packet_traits<Scalar>::type Packet;
00596   linspaced_op(Scalar low, Scalar high, int num_steps) : impl((num_steps==1 ? high : low), (num_steps==1 ? Scalar() : (high-low)/(num_steps-1))) {}
00597 
00598   template<typename Index>
00599   EIGEN_STRONG_INLINE const Scalar operator() (Index i) const { return impl(i); }
00600 
00601   // We need this function when assigning e.g. a RowVectorXd to a MatrixXd since
00602   // there row==0 and col is used for the actual iteration.
00603   template<typename Index>
00604   EIGEN_STRONG_INLINE const Scalar operator() (Index row, Index col) const 
00605   {
00606     eigen_assert(col==0 || row==0);
00607     return impl(col + row);
00608   }
00609 
00610   template<typename Index>
00611   EIGEN_STRONG_INLINE const Packet packetOp(Index i) const { return impl.packetOp(i); }
00612 
00613   // We need this function when assigning e.g. a RowVectorXd to a MatrixXd since
00614   // there row==0 and col is used for the actual iteration.
00615   template<typename Index>
00616   EIGEN_STRONG_INLINE const Packet packetOp(Index row, Index col) const
00617   {
00618     eigen_assert(col==0 || row==0);
00619     return impl.packetOp(col + row);
00620   }
00621 
00622   // This proxy object handles the actual required temporaries, the different
00623   // implementations (random vs. sequential access) as well as the
00624   // correct piping to size 2/4 packet operations.
00625   const linspaced_op_impl<Scalar,RandomAccess> impl;
00626 };
00627 
00628 // all functors allow linear access, except scalar_identity_op. So we fix here a quick meta
00629 // to indicate whether a functor allows linear access, just always answering 'yes' except for
00630 // scalar_identity_op.
00631 // FIXME move this to functor_traits adding a functor_default
00632 template<typename Functor> struct functor_has_linear_access { enum { ret = 1 }; };
00633 template<typename Scalar> struct functor_has_linear_access<scalar_identity_op<Scalar> > { enum { ret = 0 }; };
00634 
00635 // in CwiseBinaryOp, we require the Lhs and Rhs to have the same scalar type, except for multiplication
00636 // where we only require them to have the same _real_ scalar type so one may multiply, say, float by complex<float>.
00637 // FIXME move this to functor_traits adding a functor_default
00638 template<typename Functor> struct functor_allows_mixing_real_and_complex { enum { ret = 0 }; };
00639 template<typename LhsScalar,typename RhsScalar> struct functor_allows_mixing_real_and_complex<scalar_product_op<LhsScalar,RhsScalar> > { enum { ret = 1 }; };
00640 template<typename LhsScalar,typename RhsScalar> struct functor_allows_mixing_real_and_complex<scalar_conj_product_op<LhsScalar,RhsScalar> > { enum { ret = 1 }; };
00641 
00642 
00647 /* If you wonder why doing the pset1() in packetOp() is an optimization check scalar_multiple_op */
00648 template<typename Scalar>
00649 struct scalar_add_op {
00650   typedef typename packet_traits<Scalar>::type Packet;
00651   // FIXME default copy constructors seems bugged with std::complex<>
00652   inline scalar_add_op(const scalar_add_op& other) : m_other(other.m_other) { }
00653   inline scalar_add_op(const Scalar& other) : m_other(other) { }
00654   inline Scalar operator() (const Scalar& a) const { return a + m_other; }
00655   inline const Packet packetOp(const Packet& a) const
00656   { return internal::padd(a, pset1<Packet>(m_other)); }
00657   const Scalar m_other;
00658 };
00659 template<typename Scalar>
00660 struct functor_traits<scalar_add_op<Scalar> >
00661 { enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = packet_traits<Scalar>::HasAdd }; };
00662 
00667 template<typename Scalar> struct scalar_sqrt_op {
00668   EIGEN_EMPTY_STRUCT_CTOR(scalar_sqrt_op)
00669   inline const Scalar operator() (const Scalar& a) const { return internal::sqrt(a); }
00670   typedef typename packet_traits<Scalar>::type Packet;
00671   inline Packet packetOp(const Packet& a) const { return internal::psqrt(a); }
00672 };
00673 template<typename Scalar>
00674 struct functor_traits<scalar_sqrt_op<Scalar> >
00675 { enum {
00676     Cost = 5 * NumTraits<Scalar>::MulCost,
00677     PacketAccess = packet_traits<Scalar>::HasSqrt
00678   };
00679 };
00680 
00685 template<typename Scalar> struct scalar_cos_op {
00686   EIGEN_EMPTY_STRUCT_CTOR(scalar_cos_op)
00687   inline Scalar operator() (const Scalar& a) const { return internal::cos(a); }
00688   typedef typename packet_traits<Scalar>::type Packet;
00689   inline Packet packetOp(const Packet& a) const { return internal::pcos(a); }
00690 };
00691 template<typename Scalar>
00692 struct functor_traits<scalar_cos_op<Scalar> >
00693 {
00694   enum {
00695     Cost = 5 * NumTraits<Scalar>::MulCost,
00696     PacketAccess = packet_traits<Scalar>::HasCos
00697   };
00698 };
00699 
00704 template<typename Scalar> struct scalar_sin_op {
00705   EIGEN_EMPTY_STRUCT_CTOR(scalar_sin_op)
00706   inline const Scalar operator() (const Scalar& a) const { return internal::sin(a); }
00707   typedef typename packet_traits<Scalar>::type Packet;
00708   inline Packet packetOp(const Packet& a) const { return internal::psin(a); }
00709 };
00710 template<typename Scalar>
00711 struct functor_traits<scalar_sin_op<Scalar> >
00712 {
00713   enum {
00714     Cost = 5 * NumTraits<Scalar>::MulCost,
00715     PacketAccess = packet_traits<Scalar>::HasSin
00716   };
00717 };
00718 
00719 
00724 template<typename Scalar> struct scalar_tan_op {
00725   EIGEN_EMPTY_STRUCT_CTOR(scalar_tan_op)
00726   inline const Scalar operator() (const Scalar& a) const { return internal::tan(a); }
00727   typedef typename packet_traits<Scalar>::type Packet;
00728   inline Packet packetOp(const Packet& a) const { return internal::ptan(a); }
00729 };
00730 template<typename Scalar>
00731 struct functor_traits<scalar_tan_op<Scalar> >
00732 {
00733   enum {
00734     Cost = 5 * NumTraits<Scalar>::MulCost,
00735     PacketAccess = packet_traits<Scalar>::HasTan
00736   };
00737 };
00738 
00743 template<typename Scalar> struct scalar_acos_op {
00744   EIGEN_EMPTY_STRUCT_CTOR(scalar_acos_op)
00745   inline const Scalar operator() (const Scalar& a) const { return internal::acos(a); }
00746   typedef typename packet_traits<Scalar>::type Packet;
00747   inline Packet packetOp(const Packet& a) const { return internal::pacos(a); }
00748 };
00749 template<typename Scalar>
00750 struct functor_traits<scalar_acos_op<Scalar> >
00751 {
00752   enum {
00753     Cost = 5 * NumTraits<Scalar>::MulCost,
00754     PacketAccess = packet_traits<Scalar>::HasACos
00755   };
00756 };
00757 
00762 template<typename Scalar> struct scalar_asin_op {
00763   EIGEN_EMPTY_STRUCT_CTOR(scalar_asin_op)
00764   inline const Scalar operator() (const Scalar& a) const { return internal::asin(a); }
00765   typedef typename packet_traits<Scalar>::type Packet;
00766   inline Packet packetOp(const Packet& a) const { return internal::pasin(a); }
00767 };
00768 template<typename Scalar>
00769 struct functor_traits<scalar_asin_op<Scalar> >
00770 {
00771   enum {
00772     Cost = 5 * NumTraits<Scalar>::MulCost,
00773     PacketAccess = packet_traits<Scalar>::HasASin
00774   };
00775 };
00776 
00781 template<typename Scalar>
00782 struct scalar_pow_op {
00783   // FIXME default copy constructors seems bugged with std::complex<>
00784   inline scalar_pow_op(const scalar_pow_op& other) : m_exponent(other.m_exponent) { }
00785   inline scalar_pow_op(const Scalar& exponent) : m_exponent(exponent) {}
00786   inline Scalar operator() (const Scalar& a) const { return internal::pow(a, m_exponent); }
00787   const Scalar m_exponent;
00788 };
00789 template<typename Scalar>
00790 struct functor_traits<scalar_pow_op<Scalar> >
00791 { enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = false }; };
00792 
00797 template<typename Scalar>
00798 struct scalar_inverse_mult_op {
00799   scalar_inverse_mult_op(const Scalar& other) : m_other(other) {}
00800   inline Scalar operator() (const Scalar& a) const { return m_other / a; }
00801   template<typename Packet>
00802   inline const Packet packetOp(const Packet& a) const
00803   { return internal::pdiv(pset1<Packet>(m_other),a); }
00804   Scalar m_other;
00805 };
00806 
00811 template<typename Scalar>
00812 struct scalar_inverse_op {
00813   EIGEN_EMPTY_STRUCT_CTOR(scalar_inverse_op)
00814   inline Scalar operator() (const Scalar& a) const { return Scalar(1)/a; }
00815   template<typename Packet>
00816   inline const Packet packetOp(const Packet& a) const
00817   { return internal::pdiv(pset1<Packet>(Scalar(1)),a); }
00818 };
00819 template<typename Scalar>
00820 struct functor_traits<scalar_inverse_op<Scalar> >
00821 { enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasDiv }; };
00822 
00827 template<typename Scalar>
00828 struct scalar_square_op {
00829   EIGEN_EMPTY_STRUCT_CTOR(scalar_square_op)
00830   inline Scalar operator() (const Scalar& a) const { return a*a; }
00831   template<typename Packet>
00832   inline const Packet packetOp(const Packet& a) const
00833   { return internal::pmul(a,a); }
00834 };
00835 template<typename Scalar>
00836 struct functor_traits<scalar_square_op<Scalar> >
00837 { enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasMul }; };
00838 
00843 template<typename Scalar>
00844 struct scalar_cube_op {
00845   EIGEN_EMPTY_STRUCT_CTOR(scalar_cube_op)
00846   inline Scalar operator() (const Scalar& a) const { return a*a*a; }
00847   template<typename Packet>
00848   inline const Packet packetOp(const Packet& a) const
00849   { return internal::pmul(a,pmul(a,a)); }
00850 };
00851 template<typename Scalar>
00852 struct functor_traits<scalar_cube_op<Scalar> >
00853 { enum { Cost = 2*NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasMul }; };
00854 
00855 // default functor traits for STL functors:
00856 
00857 template<typename T>
00858 struct functor_traits<std::multiplies<T> >
00859 { enum { Cost = NumTraits<T>::MulCost, PacketAccess = false }; };
00860 
00861 template<typename T>
00862 struct functor_traits<std::divides<T> >
00863 { enum { Cost = NumTraits<T>::MulCost, PacketAccess = false }; };
00864 
00865 template<typename T>
00866 struct functor_traits<std::plus<T> >
00867 { enum { Cost = NumTraits<T>::AddCost, PacketAccess = false }; };
00868 
00869 template<typename T>
00870 struct functor_traits<std::minus<T> >
00871 { enum { Cost = NumTraits<T>::AddCost, PacketAccess = false }; };
00872 
00873 template<typename T>
00874 struct functor_traits<std::negate<T> >
00875 { enum { Cost = NumTraits<T>::AddCost, PacketAccess = false }; };
00876 
00877 template<typename T>
00878 struct functor_traits<std::logical_or<T> >
00879 { enum { Cost = 1, PacketAccess = false }; };
00880 
00881 template<typename T>
00882 struct functor_traits<std::logical_and<T> >
00883 { enum { Cost = 1, PacketAccess = false }; };
00884 
00885 template<typename T>
00886 struct functor_traits<std::logical_not<T> >
00887 { enum { Cost = 1, PacketAccess = false }; };
00888 
00889 template<typename T>
00890 struct functor_traits<std::greater<T> >
00891 { enum { Cost = 1, PacketAccess = false }; };
00892 
00893 template<typename T>
00894 struct functor_traits<std::less<T> >
00895 { enum { Cost = 1, PacketAccess = false }; };
00896 
00897 template<typename T>
00898 struct functor_traits<std::greater_equal<T> >
00899 { enum { Cost = 1, PacketAccess = false }; };
00900 
00901 template<typename T>
00902 struct functor_traits<std::less_equal<T> >
00903 { enum { Cost = 1, PacketAccess = false }; };
00904 
00905 template<typename T>
00906 struct functor_traits<std::equal_to<T> >
00907 { enum { Cost = 1, PacketAccess = false }; };
00908 
00909 template<typename T>
00910 struct functor_traits<std::not_equal_to<T> >
00911 { enum { Cost = 1, PacketAccess = false }; };
00912 
00913 template<typename T>
00914 struct functor_traits<std::binder2nd<T> >
00915 { enum { Cost = functor_traits<T>::Cost, PacketAccess = false }; };
00916 
00917 template<typename T>
00918 struct functor_traits<std::binder1st<T> >
00919 { enum { Cost = functor_traits<T>::Cost, PacketAccess = false }; };
00920 
00921 template<typename T>
00922 struct functor_traits<std::unary_negate<T> >
00923 { enum { Cost = 1 + functor_traits<T>::Cost, PacketAccess = false }; };
00924 
00925 template<typename T>
00926 struct functor_traits<std::binary_negate<T> >
00927 { enum { Cost = 1 + functor_traits<T>::Cost, PacketAccess = false }; };
00928 
00929 #ifdef EIGEN_STDEXT_SUPPORT
00930 
00931 template<typename T0,typename T1>
00932 struct functor_traits<std::project1st<T0,T1> >
00933 { enum { Cost = 0, PacketAccess = false }; };
00934 
00935 template<typename T0,typename T1>
00936 struct functor_traits<std::project2nd<T0,T1> >
00937 { enum { Cost = 0, PacketAccess = false }; };
00938 
00939 template<typename T0,typename T1>
00940 struct functor_traits<std::select2nd<std::pair<T0,T1> > >
00941 { enum { Cost = 0, PacketAccess = false }; };
00942 
00943 template<typename T0,typename T1>
00944 struct functor_traits<std::select1st<std::pair<T0,T1> > >
00945 { enum { Cost = 0, PacketAccess = false }; };
00946 
00947 template<typename T0,typename T1>
00948 struct functor_traits<std::unary_compose<T0,T1> >
00949 { enum { Cost = functor_traits<T0>::Cost + functor_traits<T1>::Cost, PacketAccess = false }; };
00950 
00951 template<typename T0,typename T1,typename T2>
00952 struct functor_traits<std::binary_compose<T0,T1,T2> >
00953 { enum { Cost = functor_traits<T0>::Cost + functor_traits<T1>::Cost + functor_traits<T2>::Cost, PacketAccess = false }; };
00954 
00955 #endif // EIGEN_STDEXT_SUPPORT
00956 
00957 // allow to add new functors and specializations of functor_traits from outside Eigen.
00958 // this macro is really needed because functor_traits must be specialized after it is declared but before it is used...
00959 #ifdef EIGEN_FUNCTORS_PLUGIN
00960 #include EIGEN_FUNCTORS_PLUGIN
00961 #endif
00962 
00963 } // end namespace internal
00964 
00965 } // end namespace Eigen
00966 
00967 #endif // EIGEN_FUNCTORS_H


win_eigen
Author(s): Daniel Stonier
autogenerated on Mon Oct 6 2014 12:24:32