1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud (at) inria.fr> 5 // 6 // This Source Code Form is subject to the terms of the Mozilla 7 // Public License v. 2.0. If a copy of the MPL was not distributed 8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 10 #ifndef EIGEN_FUNCTORS_H 11 #define EIGEN_FUNCTORS_H 12 13 namespace Eigen { 14 15 namespace internal { 16 17 // associative functors: 18 19 /** \internal 20 * \brief Template functor to compute the sum of two scalars 21 * 22 * \sa class CwiseBinaryOp, MatrixBase::operator+, class VectorwiseOp, MatrixBase::sum() 23 */ 24 template<typename Scalar> struct scalar_sum_op { 25 EIGEN_EMPTY_STRUCT_CTOR(scalar_sum_op) 26 EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a, const Scalar& b) const { return a + b; } 27 template<typename Packet> 28 EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const 29 { return internal::padd(a,b); } 30 template<typename Packet> 31 EIGEN_STRONG_INLINE const Scalar predux(const Packet& a) const 32 { return internal::predux(a); } 33 }; 34 template<typename Scalar> 35 struct functor_traits<scalar_sum_op<Scalar> > { 36 enum { 37 Cost = NumTraits<Scalar>::AddCost, 38 PacketAccess = packet_traits<Scalar>::HasAdd 39 }; 40 }; 41 42 /** \internal 43 * \brief Template functor to compute the product of two scalars 44 * 45 * \sa class CwiseBinaryOp, Cwise::operator*(), class VectorwiseOp, MatrixBase::redux() 46 */ 47 template<typename LhsScalar,typename RhsScalar> struct scalar_product_op { 48 enum { 49 // TODO vectorize mixed product 50 Vectorizable = is_same<LhsScalar,RhsScalar>::value && packet_traits<LhsScalar>::HasMul && packet_traits<RhsScalar>::HasMul 51 }; 52 typedef typename scalar_product_traits<LhsScalar,RhsScalar>::ReturnType result_type; 53 EIGEN_EMPTY_STRUCT_CTOR(scalar_product_op) 54 EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return a * b; } 55 template<typename Packet> 56 EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const 57 { return internal::pmul(a,b); } 58 template<typename Packet> 59 EIGEN_STRONG_INLINE const result_type predux(const Packet& a) const 60 { return internal::predux_mul(a); } 61 }; 62 template<typename LhsScalar,typename RhsScalar> 63 struct functor_traits<scalar_product_op<LhsScalar,RhsScalar> > { 64 enum { 65 Cost = (NumTraits<LhsScalar>::MulCost + NumTraits<RhsScalar>::MulCost)/2, // rough estimate! 66 PacketAccess = scalar_product_op<LhsScalar,RhsScalar>::Vectorizable 67 }; 68 }; 69 70 /** \internal 71 * \brief Template functor to compute the conjugate product of two scalars 72 * 73 * This is a short cut for conj(x) * y which is needed for optimization purpose; in Eigen2 support mode, this becomes x * conj(y) 74 */ 75 template<typename LhsScalar,typename RhsScalar> struct scalar_conj_product_op { 76 77 enum { 78 Conj = NumTraits<LhsScalar>::IsComplex 79 }; 80 81 typedef typename scalar_product_traits<LhsScalar,RhsScalar>::ReturnType result_type; 82 83 EIGEN_EMPTY_STRUCT_CTOR(scalar_conj_product_op) 84 EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const 85 { return conj_helper<LhsScalar,RhsScalar,Conj,false>().pmul(a,b); } 86 87 template<typename Packet> 88 EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const 89 { return conj_helper<Packet,Packet,Conj,false>().pmul(a,b); } 90 }; 91 template<typename LhsScalar,typename RhsScalar> 92 struct functor_traits<scalar_conj_product_op<LhsScalar,RhsScalar> > { 93 enum { 94 Cost = NumTraits<LhsScalar>::MulCost, 95 PacketAccess = internal::is_same<LhsScalar, RhsScalar>::value && packet_traits<LhsScalar>::HasMul 96 }; 97 }; 98 99 /** \internal 100 * \brief Template functor to compute the min of two scalars 101 * 102 * \sa class CwiseBinaryOp, MatrixBase::cwiseMin, class VectorwiseOp, MatrixBase::minCoeff() 103 */ 104 template<typename Scalar> struct scalar_min_op { 105 EIGEN_EMPTY_STRUCT_CTOR(scalar_min_op) 106 EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a, const Scalar& b) const { using std::min; return (min)(a, b); } 107 template<typename Packet> 108 EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const 109 { return internal::pmin(a,b); } 110 template<typename Packet> 111 EIGEN_STRONG_INLINE const Scalar predux(const Packet& a) const 112 { return internal::predux_min(a); } 113 }; 114 template<typename Scalar> 115 struct functor_traits<scalar_min_op<Scalar> > { 116 enum { 117 Cost = NumTraits<Scalar>::AddCost, 118 PacketAccess = packet_traits<Scalar>::HasMin 119 }; 120 }; 121 122 /** \internal 123 * \brief Template functor to compute the max of two scalars 124 * 125 * \sa class CwiseBinaryOp, MatrixBase::cwiseMax, class VectorwiseOp, MatrixBase::maxCoeff() 126 */ 127 template<typename Scalar> struct scalar_max_op { 128 EIGEN_EMPTY_STRUCT_CTOR(scalar_max_op) 129 EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a, const Scalar& b) const { using std::max; return (max)(a, b); } 130 template<typename Packet> 131 EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const 132 { return internal::pmax(a,b); } 133 template<typename Packet> 134 EIGEN_STRONG_INLINE const Scalar predux(const Packet& a) const 135 { return internal::predux_max(a); } 136 }; 137 template<typename Scalar> 138 struct functor_traits<scalar_max_op<Scalar> > { 139 enum { 140 Cost = NumTraits<Scalar>::AddCost, 141 PacketAccess = packet_traits<Scalar>::HasMax 142 }; 143 }; 144 145 /** \internal 146 * \brief Template functor to compute the hypot of two scalars 147 * 148 * \sa MatrixBase::stableNorm(), class Redux 149 */ 150 template<typename Scalar> struct scalar_hypot_op { 151 EIGEN_EMPTY_STRUCT_CTOR(scalar_hypot_op) 152 // typedef typename NumTraits<Scalar>::Real result_type; 153 EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& _x, const Scalar& _y) const 154 { 155 using std::max; 156 using std::min; 157 Scalar p = (max)(_x, _y); 158 Scalar q = (min)(_x, _y); 159 Scalar qp = q/p; 160 return p * sqrt(Scalar(1) + qp*qp); 161 } 162 }; 163 template<typename Scalar> 164 struct functor_traits<scalar_hypot_op<Scalar> > { 165 enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess=0 }; 166 }; 167 168 /** \internal 169 * \brief Template functor to compute the pow of two scalars 170 */ 171 template<typename Scalar, typename OtherScalar> struct scalar_binary_pow_op { 172 EIGEN_EMPTY_STRUCT_CTOR(scalar_binary_pow_op) 173 inline Scalar operator() (const Scalar& a, const OtherScalar& b) const { return internal::pow(a, b); } 174 }; 175 template<typename Scalar, typename OtherScalar> 176 struct functor_traits<scalar_binary_pow_op<Scalar,OtherScalar> > { 177 enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = false }; 178 }; 179 180 // other binary functors: 181 182 /** \internal 183 * \brief Template functor to compute the difference of two scalars 184 * 185 * \sa class CwiseBinaryOp, MatrixBase::operator- 186 */ 187 template<typename Scalar> struct scalar_difference_op { 188 EIGEN_EMPTY_STRUCT_CTOR(scalar_difference_op) 189 EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a, const Scalar& b) const { return a - b; } 190 template<typename Packet> 191 EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const 192 { return internal::psub(a,b); } 193 }; 194 template<typename Scalar> 195 struct functor_traits<scalar_difference_op<Scalar> > { 196 enum { 197 Cost = NumTraits<Scalar>::AddCost, 198 PacketAccess = packet_traits<Scalar>::HasSub 199 }; 200 }; 201 202 /** \internal 203 * \brief Template functor to compute the quotient of two scalars 204 * 205 * \sa class CwiseBinaryOp, Cwise::operator/() 206 */ 207 template<typename Scalar> struct scalar_quotient_op { 208 EIGEN_EMPTY_STRUCT_CTOR(scalar_quotient_op) 209 EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a, const Scalar& b) const { return a / b; } 210 template<typename Packet> 211 EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const 212 { return internal::pdiv(a,b); } 213 }; 214 template<typename Scalar> 215 struct functor_traits<scalar_quotient_op<Scalar> > { 216 enum { 217 Cost = 2 * NumTraits<Scalar>::MulCost, 218 PacketAccess = packet_traits<Scalar>::HasDiv 219 }; 220 }; 221 222 /** \internal 223 * \brief Template functor to compute the and of two booleans 224 * 225 * \sa class CwiseBinaryOp, ArrayBase::operator&& 226 */ 227 struct scalar_boolean_and_op { 228 EIGEN_EMPTY_STRUCT_CTOR(scalar_boolean_and_op) 229 EIGEN_STRONG_INLINE bool operator() (const bool& a, const bool& b) const { return a && b; } 230 }; 231 template<> struct functor_traits<scalar_boolean_and_op> { 232 enum { 233 Cost = NumTraits<bool>::AddCost, 234 PacketAccess = false 235 }; 236 }; 237 238 /** \internal 239 * \brief Template functor to compute the or of two booleans 240 * 241 * \sa class CwiseBinaryOp, ArrayBase::operator|| 242 */ 243 struct scalar_boolean_or_op { 244 EIGEN_EMPTY_STRUCT_CTOR(scalar_boolean_or_op) 245 EIGEN_STRONG_INLINE bool operator() (const bool& a, const bool& b) const { return a || b; } 246 }; 247 template<> struct functor_traits<scalar_boolean_or_op> { 248 enum { 249 Cost = NumTraits<bool>::AddCost, 250 PacketAccess = false 251 }; 252 }; 253 254 // unary functors: 255 256 /** \internal 257 * \brief Template functor to compute the opposite of a scalar 258 * 259 * \sa class CwiseUnaryOp, MatrixBase::operator- 260 */ 261 template<typename Scalar> struct scalar_opposite_op { 262 EIGEN_EMPTY_STRUCT_CTOR(scalar_opposite_op) 263 EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a) const { return -a; } 264 template<typename Packet> 265 EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const 266 { return internal::pnegate(a); } 267 }; 268 template<typename Scalar> 269 struct functor_traits<scalar_opposite_op<Scalar> > 270 { enum { 271 Cost = NumTraits<Scalar>::AddCost, 272 PacketAccess = packet_traits<Scalar>::HasNegate }; 273 }; 274 275 /** \internal 276 * \brief Template functor to compute the absolute value of a scalar 277 * 278 * \sa class CwiseUnaryOp, Cwise::abs 279 */ 280 template<typename Scalar> struct scalar_abs_op { 281 EIGEN_EMPTY_STRUCT_CTOR(scalar_abs_op) 282 typedef typename NumTraits<Scalar>::Real result_type; 283 EIGEN_STRONG_INLINE const result_type operator() (const Scalar& a) const { return internal::abs(a); } 284 template<typename Packet> 285 EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const 286 { return internal::pabs(a); } 287 }; 288 template<typename Scalar> 289 struct functor_traits<scalar_abs_op<Scalar> > 290 { 291 enum { 292 Cost = NumTraits<Scalar>::AddCost, 293 PacketAccess = packet_traits<Scalar>::HasAbs 294 }; 295 }; 296 297 /** \internal 298 * \brief Template functor to compute the squared absolute value of a scalar 299 * 300 * \sa class CwiseUnaryOp, Cwise::abs2 301 */ 302 template<typename Scalar> struct scalar_abs2_op { 303 EIGEN_EMPTY_STRUCT_CTOR(scalar_abs2_op) 304 typedef typename NumTraits<Scalar>::Real result_type; 305 EIGEN_STRONG_INLINE const result_type operator() (const Scalar& a) const { return internal::abs2(a); } 306 template<typename Packet> 307 EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const 308 { return internal::pmul(a,a); } 309 }; 310 template<typename Scalar> 311 struct functor_traits<scalar_abs2_op<Scalar> > 312 { enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasAbs2 }; }; 313 314 /** \internal 315 * \brief Template functor to compute the conjugate of a complex value 316 * 317 * \sa class CwiseUnaryOp, MatrixBase::conjugate() 318 */ 319 template<typename Scalar> struct scalar_conjugate_op { 320 EIGEN_EMPTY_STRUCT_CTOR(scalar_conjugate_op) 321 EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a) const { return internal::conj(a); } 322 template<typename Packet> 323 EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const { return internal::pconj(a); } 324 }; 325 template<typename Scalar> 326 struct functor_traits<scalar_conjugate_op<Scalar> > 327 { 328 enum { 329 Cost = NumTraits<Scalar>::IsComplex ? NumTraits<Scalar>::AddCost : 0, 330 PacketAccess = packet_traits<Scalar>::HasConj 331 }; 332 }; 333 334 /** \internal 335 * \brief Template functor to cast a scalar to another type 336 * 337 * \sa class CwiseUnaryOp, MatrixBase::cast() 338 */ 339 template<typename Scalar, typename NewType> 340 struct scalar_cast_op { 341 EIGEN_EMPTY_STRUCT_CTOR(scalar_cast_op) 342 typedef NewType result_type; 343 EIGEN_STRONG_INLINE const NewType operator() (const Scalar& a) const { return cast<Scalar, NewType>(a); } 344 }; 345 template<typename Scalar, typename NewType> 346 struct functor_traits<scalar_cast_op<Scalar,NewType> > 347 { enum { Cost = is_same<Scalar, NewType>::value ? 0 : NumTraits<NewType>::AddCost, PacketAccess = false }; }; 348 349 /** \internal 350 * \brief Template functor to extract the real part of a complex 351 * 352 * \sa class CwiseUnaryOp, MatrixBase::real() 353 */ 354 template<typename Scalar> 355 struct scalar_real_op { 356 EIGEN_EMPTY_STRUCT_CTOR(scalar_real_op) 357 typedef typename NumTraits<Scalar>::Real result_type; 358 EIGEN_STRONG_INLINE result_type operator() (const Scalar& a) const { return internal::real(a); } 359 }; 360 template<typename Scalar> 361 struct functor_traits<scalar_real_op<Scalar> > 362 { enum { Cost = 0, PacketAccess = false }; }; 363 364 /** \internal 365 * \brief Template functor to extract the imaginary part of a complex 366 * 367 * \sa class CwiseUnaryOp, MatrixBase::imag() 368 */ 369 template<typename Scalar> 370 struct scalar_imag_op { 371 EIGEN_EMPTY_STRUCT_CTOR(scalar_imag_op) 372 typedef typename NumTraits<Scalar>::Real result_type; 373 EIGEN_STRONG_INLINE result_type operator() (const Scalar& a) const { return internal::imag(a); } 374 }; 375 template<typename Scalar> 376 struct functor_traits<scalar_imag_op<Scalar> > 377 { enum { Cost = 0, PacketAccess = false }; }; 378 379 /** \internal 380 * \brief Template functor to extract the real part of a complex as a reference 381 * 382 * \sa class CwiseUnaryOp, MatrixBase::real() 383 */ 384 template<typename Scalar> 385 struct scalar_real_ref_op { 386 EIGEN_EMPTY_STRUCT_CTOR(scalar_real_ref_op) 387 typedef typename NumTraits<Scalar>::Real result_type; 388 EIGEN_STRONG_INLINE result_type& operator() (const Scalar& a) const { return internal::real_ref(*const_cast<Scalar*>(&a)); } 389 }; 390 template<typename Scalar> 391 struct functor_traits<scalar_real_ref_op<Scalar> > 392 { enum { Cost = 0, PacketAccess = false }; }; 393 394 /** \internal 395 * \brief Template functor to extract the imaginary part of a complex as a reference 396 * 397 * \sa class CwiseUnaryOp, MatrixBase::imag() 398 */ 399 template<typename Scalar> 400 struct scalar_imag_ref_op { 401 EIGEN_EMPTY_STRUCT_CTOR(scalar_imag_ref_op) 402 typedef typename NumTraits<Scalar>::Real result_type; 403 EIGEN_STRONG_INLINE result_type& operator() (const Scalar& a) const { return internal::imag_ref(*const_cast<Scalar*>(&a)); } 404 }; 405 template<typename Scalar> 406 struct functor_traits<scalar_imag_ref_op<Scalar> > 407 { enum { Cost = 0, PacketAccess = false }; }; 408 409 /** \internal 410 * 411 * \brief Template functor to compute the exponential of a scalar 412 * 413 * \sa class CwiseUnaryOp, Cwise::exp() 414 */ 415 template<typename Scalar> struct scalar_exp_op { 416 EIGEN_EMPTY_STRUCT_CTOR(scalar_exp_op) 417 inline const Scalar operator() (const Scalar& a) const { return internal::exp(a); } 418 typedef typename packet_traits<Scalar>::type Packet; 419 inline Packet packetOp(const Packet& a) const { return internal::pexp(a); } 420 }; 421 template<typename Scalar> 422 struct functor_traits<scalar_exp_op<Scalar> > 423 { enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasExp }; }; 424 425 /** \internal 426 * 427 * \brief Template functor to compute the logarithm of a scalar 428 * 429 * \sa class CwiseUnaryOp, Cwise::log() 430 */ 431 template<typename Scalar> struct scalar_log_op { 432 EIGEN_EMPTY_STRUCT_CTOR(scalar_log_op) 433 inline const Scalar operator() (const Scalar& a) const { return internal::log(a); } 434 typedef typename packet_traits<Scalar>::type Packet; 435 inline Packet packetOp(const Packet& a) const { return internal::plog(a); } 436 }; 437 template<typename Scalar> 438 struct functor_traits<scalar_log_op<Scalar> > 439 { enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasLog }; }; 440 441 /** \internal 442 * \brief Template functor to multiply a scalar by a fixed other one 443 * 444 * \sa class CwiseUnaryOp, MatrixBase::operator*, MatrixBase::operator/ 445 */ 446 /* NOTE why doing the pset1() in packetOp *is* an optimization ? 447 * indeed it seems better to declare m_other as a Packet and do the pset1() once 448 * in the constructor. However, in practice: 449 * - GCC does not like m_other as a Packet and generate a load every time it needs it 450 * - on the other hand GCC is able to moves the pset1() away the loop :) 451 * - simpler code ;) 452 * (ICC and gcc 4.4 seems to perform well in both cases, the issue is visible with y = a*x + b*y) 453 */ 454 template<typename Scalar> 455 struct scalar_multiple_op { 456 typedef typename packet_traits<Scalar>::type Packet; 457 // FIXME default copy constructors seems bugged with std::complex<> 458 EIGEN_STRONG_INLINE scalar_multiple_op(const scalar_multiple_op& other) : m_other(other.m_other) { } 459 EIGEN_STRONG_INLINE scalar_multiple_op(const Scalar& other) : m_other(other) { } 460 EIGEN_STRONG_INLINE Scalar operator() (const Scalar& a) const { return a * m_other; } 461 EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const 462 { return internal::pmul(a, pset1<Packet>(m_other)); } 463 typename add_const_on_value_type<typename NumTraits<Scalar>::Nested>::type m_other; 464 }; 465 template<typename Scalar> 466 struct functor_traits<scalar_multiple_op<Scalar> > 467 { enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasMul }; }; 468 469 template<typename Scalar1, typename Scalar2> 470 struct scalar_multiple2_op { 471 typedef typename scalar_product_traits<Scalar1,Scalar2>::ReturnType result_type; 472 EIGEN_STRONG_INLINE scalar_multiple2_op(const scalar_multiple2_op& other) : m_other(other.m_other) { } 473 EIGEN_STRONG_INLINE scalar_multiple2_op(const Scalar2& other) : m_other(other) { } 474 EIGEN_STRONG_INLINE result_type operator() (const Scalar1& a) const { return a * m_other; } 475 typename add_const_on_value_type<typename NumTraits<Scalar2>::Nested>::type m_other; 476 }; 477 template<typename Scalar1,typename Scalar2> 478 struct functor_traits<scalar_multiple2_op<Scalar1,Scalar2> > 479 { enum { Cost = NumTraits<Scalar1>::MulCost, PacketAccess = false }; }; 480 481 template<typename Scalar, bool IsInteger> 482 struct scalar_quotient1_impl { 483 typedef typename packet_traits<Scalar>::type Packet; 484 // FIXME default copy constructors seems bugged with std::complex<> 485 EIGEN_STRONG_INLINE scalar_quotient1_impl(const scalar_quotient1_impl& other) : m_other(other.m_other) { } 486 EIGEN_STRONG_INLINE scalar_quotient1_impl(const Scalar& other) : m_other(static_cast<Scalar>(1) / other) {} 487 EIGEN_STRONG_INLINE Scalar operator() (const Scalar& a) const { return a * m_other; } 488 EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const 489 { return internal::pmul(a, pset1<Packet>(m_other)); } 490 const Scalar m_other; 491 }; 492 template<typename Scalar> 493 struct functor_traits<scalar_quotient1_impl<Scalar,false> > 494 { enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasMul }; }; 495 496 template<typename Scalar> 497 struct scalar_quotient1_impl<Scalar,true> { 498 // FIXME default copy constructors seems bugged with std::complex<> 499 EIGEN_STRONG_INLINE scalar_quotient1_impl(const scalar_quotient1_impl& other) : m_other(other.m_other) { } 500 EIGEN_STRONG_INLINE scalar_quotient1_impl(const Scalar& other) : m_other(other) {} 501 EIGEN_STRONG_INLINE Scalar operator() (const Scalar& a) const { return a / m_other; } 502 typename add_const_on_value_type<typename NumTraits<Scalar>::Nested>::type m_other; 503 }; 504 template<typename Scalar> 505 struct functor_traits<scalar_quotient1_impl<Scalar,true> > 506 { enum { Cost = 2 * NumTraits<Scalar>::MulCost, PacketAccess = false }; }; 507 508 /** \internal 509 * \brief Template functor to divide a scalar by a fixed other one 510 * 511 * This functor is used to implement the quotient of a matrix by 512 * a scalar where the scalar type is not necessarily a floating point type. 513 * 514 * \sa class CwiseUnaryOp, MatrixBase::operator/ 515 */ 516 template<typename Scalar> 517 struct scalar_quotient1_op : scalar_quotient1_impl<Scalar, NumTraits<Scalar>::IsInteger > { 518 EIGEN_STRONG_INLINE scalar_quotient1_op(const Scalar& other) 519 : scalar_quotient1_impl<Scalar, NumTraits<Scalar>::IsInteger >(other) {} 520 }; 521 template<typename Scalar> 522 struct functor_traits<scalar_quotient1_op<Scalar> > 523 : functor_traits<scalar_quotient1_impl<Scalar, NumTraits<Scalar>::IsInteger> > 524 {}; 525 526 // nullary functors 527 528 template<typename Scalar> 529 struct scalar_constant_op { 530 typedef typename packet_traits<Scalar>::type Packet; 531 EIGEN_STRONG_INLINE scalar_constant_op(const scalar_constant_op& other) : m_other(other.m_other) { } 532 EIGEN_STRONG_INLINE scalar_constant_op(const Scalar& other) : m_other(other) { } 533 template<typename Index> 534 EIGEN_STRONG_INLINE const Scalar operator() (Index, Index = 0) const { return m_other; } 535 template<typename Index> 536 EIGEN_STRONG_INLINE const Packet packetOp(Index, Index = 0) const { return internal::pset1<Packet>(m_other); } 537 const Scalar m_other; 538 }; 539 template<typename Scalar> 540 struct functor_traits<scalar_constant_op<Scalar> > 541 // FIXME replace this packet test by a safe one 542 { enum { Cost = 1, PacketAccess = packet_traits<Scalar>::Vectorizable, IsRepeatable = true }; }; 543 544 template<typename Scalar> struct scalar_identity_op { 545 EIGEN_EMPTY_STRUCT_CTOR(scalar_identity_op) 546 template<typename Index> 547 EIGEN_STRONG_INLINE const Scalar operator() (Index row, Index col) const { return row==col ? Scalar(1) : Scalar(0); } 548 }; 549 template<typename Scalar> 550 struct functor_traits<scalar_identity_op<Scalar> > 551 { enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = false, IsRepeatable = true }; }; 552 553 template <typename Scalar, bool RandomAccess> struct linspaced_op_impl; 554 555 // linear access for packet ops: 556 // 1) initialization 557 // base = [low, ..., low] + ([step, ..., step] * [-size, ..., 0]) 558 // 2) each step 559 // base += [size*step, ..., size*step] 560 template <typename Scalar> 561 struct linspaced_op_impl<Scalar,false> 562 { 563 typedef typename packet_traits<Scalar>::type Packet; 564 565 linspaced_op_impl(Scalar low, Scalar step) : 566 m_low(low), m_step(step), 567 m_packetStep(pset1<Packet>(packet_traits<Scalar>::size*step)), 568 m_base(padd(pset1<Packet>(low),pmul(pset1<Packet>(step),plset<Scalar>(-packet_traits<Scalar>::size)))) {} 569 570 template<typename Index> 571 EIGEN_STRONG_INLINE const Scalar operator() (Index i) const { return m_low+i*m_step; } 572 template<typename Index> 573 EIGEN_STRONG_INLINE const Packet packetOp(Index) const { return m_base = padd(m_base,m_packetStep); } 574 575 const Scalar m_low; 576 const Scalar m_step; 577 const Packet m_packetStep; 578 mutable Packet m_base; 579 }; 580 581 // random access for packet ops: 582 // 1) each step 583 // [low, ..., low] + ( [step, ..., step] * ( [i, ..., i] + [0, ..., size] ) ) 584 template <typename Scalar> 585 struct linspaced_op_impl<Scalar,true> 586 { 587 typedef typename packet_traits<Scalar>::type Packet; 588 589 linspaced_op_impl(Scalar low, Scalar step) : 590 m_low(low), m_step(step), 591 m_lowPacket(pset1<Packet>(m_low)), m_stepPacket(pset1<Packet>(m_step)), m_interPacket(plset<Scalar>(0)) {} 592 593 template<typename Index> 594 EIGEN_STRONG_INLINE const Scalar operator() (Index i) const { return m_low+i*m_step; } 595 596 template<typename Index> 597 EIGEN_STRONG_INLINE const Packet packetOp(Index i) const 598 { return internal::padd(m_lowPacket, pmul(m_stepPacket, padd(pset1<Packet>(i),m_interPacket))); } 599 600 const Scalar m_low; 601 const Scalar m_step; 602 const Packet m_lowPacket; 603 const Packet m_stepPacket; 604 const Packet m_interPacket; 605 }; 606 607 // ----- Linspace functor ---------------------------------------------------------------- 608 609 // Forward declaration (we default to random access which does not really give 610 // us a speed gain when using packet access but it allows to use the functor in 611 // nested expressions). 612 template <typename Scalar, bool RandomAccess = true> struct linspaced_op; 613 template <typename Scalar, bool RandomAccess> struct functor_traits< linspaced_op<Scalar,RandomAccess> > 614 { enum { Cost = 1, PacketAccess = packet_traits<Scalar>::HasSetLinear, IsRepeatable = true }; }; 615 template <typename Scalar, bool RandomAccess> struct linspaced_op 616 { 617 typedef typename packet_traits<Scalar>::type Packet; 618 linspaced_op(Scalar low, Scalar high, int num_steps) : impl((num_steps==1 ? high : low), (num_steps==1 ? Scalar() : (high-low)/(num_steps-1))) {} 619 620 template<typename Index> 621 EIGEN_STRONG_INLINE const Scalar operator() (Index i) const { return impl(i); } 622 623 // We need this function when assigning e.g. a RowVectorXd to a MatrixXd since 624 // there row==0 and col is used for the actual iteration. 625 template<typename Index> 626 EIGEN_STRONG_INLINE const Scalar operator() (Index row, Index col) const 627 { 628 eigen_assert(col==0 || row==0); 629 return impl(col + row); 630 } 631 632 template<typename Index> 633 EIGEN_STRONG_INLINE const Packet packetOp(Index i) const { return impl.packetOp(i); } 634 635 // We need this function when assigning e.g. a RowVectorXd to a MatrixXd since 636 // there row==0 and col is used for the actual iteration. 637 template<typename Index> 638 EIGEN_STRONG_INLINE const Packet packetOp(Index row, Index col) const 639 { 640 eigen_assert(col==0 || row==0); 641 return impl.packetOp(col + row); 642 } 643 644 // This proxy object handles the actual required temporaries, the different 645 // implementations (random vs. sequential access) as well as the 646 // correct piping to size 2/4 packet operations. 647 const linspaced_op_impl<Scalar,RandomAccess> impl; 648 }; 649 650 // all functors allow linear access, except scalar_identity_op. So we fix here a quick meta 651 // to indicate whether a functor allows linear access, just always answering 'yes' except for 652 // scalar_identity_op. 653 // FIXME move this to functor_traits adding a functor_default 654 template<typename Functor> struct functor_has_linear_access { enum { ret = 1 }; }; 655 template<typename Scalar> struct functor_has_linear_access<scalar_identity_op<Scalar> > { enum { ret = 0 }; }; 656 657 // in CwiseBinaryOp, we require the Lhs and Rhs to have the same scalar type, except for multiplication 658 // where we only require them to have the same _real_ scalar type so one may multiply, say, float by complex<float>. 659 // FIXME move this to functor_traits adding a functor_default 660 template<typename Functor> struct functor_allows_mixing_real_and_complex { enum { ret = 0 }; }; 661 template<typename LhsScalar,typename RhsScalar> struct functor_allows_mixing_real_and_complex<scalar_product_op<LhsScalar,RhsScalar> > { enum { ret = 1 }; }; 662 template<typename LhsScalar,typename RhsScalar> struct functor_allows_mixing_real_and_complex<scalar_conj_product_op<LhsScalar,RhsScalar> > { enum { ret = 1 }; }; 663 664 665 /** \internal 666 * \brief Template functor to add a scalar to a fixed other one 667 * \sa class CwiseUnaryOp, Array::operator+ 668 */ 669 /* If you wonder why doing the pset1() in packetOp() is an optimization check scalar_multiple_op */ 670 template<typename Scalar> 671 struct scalar_add_op { 672 typedef typename packet_traits<Scalar>::type Packet; 673 // FIXME default copy constructors seems bugged with std::complex<> 674 inline scalar_add_op(const scalar_add_op& other) : m_other(other.m_other) { } 675 inline scalar_add_op(const Scalar& other) : m_other(other) { } 676 inline Scalar operator() (const Scalar& a) const { return a + m_other; } 677 inline const Packet packetOp(const Packet& a) const 678 { return internal::padd(a, pset1<Packet>(m_other)); } 679 const Scalar m_other; 680 }; 681 template<typename Scalar> 682 struct functor_traits<scalar_add_op<Scalar> > 683 { enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = packet_traits<Scalar>::HasAdd }; }; 684 685 /** \internal 686 * \brief Template functor to compute the square root of a scalar 687 * \sa class CwiseUnaryOp, Cwise::sqrt() 688 */ 689 template<typename Scalar> struct scalar_sqrt_op { 690 EIGEN_EMPTY_STRUCT_CTOR(scalar_sqrt_op) 691 inline const Scalar operator() (const Scalar& a) const { return internal::sqrt(a); } 692 typedef typename packet_traits<Scalar>::type Packet; 693 inline Packet packetOp(const Packet& a) const { return internal::psqrt(a); } 694 }; 695 template<typename Scalar> 696 struct functor_traits<scalar_sqrt_op<Scalar> > 697 { enum { 698 Cost = 5 * NumTraits<Scalar>::MulCost, 699 PacketAccess = packet_traits<Scalar>::HasSqrt 700 }; 701 }; 702 703 /** \internal 704 * \brief Template functor to compute the cosine of a scalar 705 * \sa class CwiseUnaryOp, ArrayBase::cos() 706 */ 707 template<typename Scalar> struct scalar_cos_op { 708 EIGEN_EMPTY_STRUCT_CTOR(scalar_cos_op) 709 inline Scalar operator() (const Scalar& a) const { return internal::cos(a); } 710 typedef typename packet_traits<Scalar>::type Packet; 711 inline Packet packetOp(const Packet& a) const { return internal::pcos(a); } 712 }; 713 template<typename Scalar> 714 struct functor_traits<scalar_cos_op<Scalar> > 715 { 716 enum { 717 Cost = 5 * NumTraits<Scalar>::MulCost, 718 PacketAccess = packet_traits<Scalar>::HasCos 719 }; 720 }; 721 722 /** \internal 723 * \brief Template functor to compute the sine of a scalar 724 * \sa class CwiseUnaryOp, ArrayBase::sin() 725 */ 726 template<typename Scalar> struct scalar_sin_op { 727 EIGEN_EMPTY_STRUCT_CTOR(scalar_sin_op) 728 inline const Scalar operator() (const Scalar& a) const { return internal::sin(a); } 729 typedef typename packet_traits<Scalar>::type Packet; 730 inline Packet packetOp(const Packet& a) const { return internal::psin(a); } 731 }; 732 template<typename Scalar> 733 struct functor_traits<scalar_sin_op<Scalar> > 734 { 735 enum { 736 Cost = 5 * NumTraits<Scalar>::MulCost, 737 PacketAccess = packet_traits<Scalar>::HasSin 738 }; 739 }; 740 741 742 /** \internal 743 * \brief Template functor to compute the tan of a scalar 744 * \sa class CwiseUnaryOp, ArrayBase::tan() 745 */ 746 template<typename Scalar> struct scalar_tan_op { 747 EIGEN_EMPTY_STRUCT_CTOR(scalar_tan_op) 748 inline const Scalar operator() (const Scalar& a) const { return internal::tan(a); } 749 typedef typename packet_traits<Scalar>::type Packet; 750 inline Packet packetOp(const Packet& a) const { return internal::ptan(a); } 751 }; 752 template<typename Scalar> 753 struct functor_traits<scalar_tan_op<Scalar> > 754 { 755 enum { 756 Cost = 5 * NumTraits<Scalar>::MulCost, 757 PacketAccess = packet_traits<Scalar>::HasTan 758 }; 759 }; 760 761 /** \internal 762 * \brief Template functor to compute the arc cosine of a scalar 763 * \sa class CwiseUnaryOp, ArrayBase::acos() 764 */ 765 template<typename Scalar> struct scalar_acos_op { 766 EIGEN_EMPTY_STRUCT_CTOR(scalar_acos_op) 767 inline const Scalar operator() (const Scalar& a) const { return internal::acos(a); } 768 typedef typename packet_traits<Scalar>::type Packet; 769 inline Packet packetOp(const Packet& a) const { return internal::pacos(a); } 770 }; 771 template<typename Scalar> 772 struct functor_traits<scalar_acos_op<Scalar> > 773 { 774 enum { 775 Cost = 5 * NumTraits<Scalar>::MulCost, 776 PacketAccess = packet_traits<Scalar>::HasACos 777 }; 778 }; 779 780 /** \internal 781 * \brief Template functor to compute the arc sine of a scalar 782 * \sa class CwiseUnaryOp, ArrayBase::asin() 783 */ 784 template<typename Scalar> struct scalar_asin_op { 785 EIGEN_EMPTY_STRUCT_CTOR(scalar_asin_op) 786 inline const Scalar operator() (const Scalar& a) const { return internal::asin(a); } 787 typedef typename packet_traits<Scalar>::type Packet; 788 inline Packet packetOp(const Packet& a) const { return internal::pasin(a); } 789 }; 790 template<typename Scalar> 791 struct functor_traits<scalar_asin_op<Scalar> > 792 { 793 enum { 794 Cost = 5 * NumTraits<Scalar>::MulCost, 795 PacketAccess = packet_traits<Scalar>::HasASin 796 }; 797 }; 798 799 /** \internal 800 * \brief Template functor to raise a scalar to a power 801 * \sa class CwiseUnaryOp, Cwise::pow 802 */ 803 template<typename Scalar> 804 struct scalar_pow_op { 805 // FIXME default copy constructors seems bugged with std::complex<> 806 inline scalar_pow_op(const scalar_pow_op& other) : m_exponent(other.m_exponent) { } 807 inline scalar_pow_op(const Scalar& exponent) : m_exponent(exponent) {} 808 inline Scalar operator() (const Scalar& a) const { return internal::pow(a, m_exponent); } 809 const Scalar m_exponent; 810 }; 811 template<typename Scalar> 812 struct functor_traits<scalar_pow_op<Scalar> > 813 { enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = false }; }; 814 815 /** \internal 816 * \brief Template functor to compute the quotient between a scalar and array entries. 817 * \sa class CwiseUnaryOp, Cwise::inverse() 818 */ 819 template<typename Scalar> 820 struct scalar_inverse_mult_op { 821 scalar_inverse_mult_op(const Scalar& other) : m_other(other) {} 822 inline Scalar operator() (const Scalar& a) const { return m_other / a; } 823 template<typename Packet> 824 inline const Packet packetOp(const Packet& a) const 825 { return internal::pdiv(pset1<Packet>(m_other),a); } 826 Scalar m_other; 827 }; 828 829 /** \internal 830 * \brief Template functor to compute the inverse of a scalar 831 * \sa class CwiseUnaryOp, Cwise::inverse() 832 */ 833 template<typename Scalar> 834 struct scalar_inverse_op { 835 EIGEN_EMPTY_STRUCT_CTOR(scalar_inverse_op) 836 inline Scalar operator() (const Scalar& a) const { return Scalar(1)/a; } 837 template<typename Packet> 838 inline const Packet packetOp(const Packet& a) const 839 { return internal::pdiv(pset1<Packet>(Scalar(1)),a); } 840 }; 841 template<typename Scalar> 842 struct functor_traits<scalar_inverse_op<Scalar> > 843 { enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasDiv }; }; 844 845 /** \internal 846 * \brief Template functor to compute the square of a scalar 847 * \sa class CwiseUnaryOp, Cwise::square() 848 */ 849 template<typename Scalar> 850 struct scalar_square_op { 851 EIGEN_EMPTY_STRUCT_CTOR(scalar_square_op) 852 inline Scalar operator() (const Scalar& a) const { return a*a; } 853 template<typename Packet> 854 inline const Packet packetOp(const Packet& a) const 855 { return internal::pmul(a,a); } 856 }; 857 template<typename Scalar> 858 struct functor_traits<scalar_square_op<Scalar> > 859 { enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasMul }; }; 860 861 /** \internal 862 * \brief Template functor to compute the cube of a scalar 863 * \sa class CwiseUnaryOp, Cwise::cube() 864 */ 865 template<typename Scalar> 866 struct scalar_cube_op { 867 EIGEN_EMPTY_STRUCT_CTOR(scalar_cube_op) 868 inline Scalar operator() (const Scalar& a) const { return a*a*a; } 869 template<typename Packet> 870 inline const Packet packetOp(const Packet& a) const 871 { return internal::pmul(a,pmul(a,a)); } 872 }; 873 template<typename Scalar> 874 struct functor_traits<scalar_cube_op<Scalar> > 875 { enum { Cost = 2*NumTraits<Scalar>::MulCost, PacketAccess = packet_traits<Scalar>::HasMul }; }; 876 877 // default functor traits for STL functors: 878 879 template<typename T> 880 struct functor_traits<std::multiplies<T> > 881 { enum { Cost = NumTraits<T>::MulCost, PacketAccess = false }; }; 882 883 template<typename T> 884 struct functor_traits<std::divides<T> > 885 { enum { Cost = NumTraits<T>::MulCost, PacketAccess = false }; }; 886 887 template<typename T> 888 struct functor_traits<std::plus<T> > 889 { enum { Cost = NumTraits<T>::AddCost, PacketAccess = false }; }; 890 891 template<typename T> 892 struct functor_traits<std::minus<T> > 893 { enum { Cost = NumTraits<T>::AddCost, PacketAccess = false }; }; 894 895 template<typename T> 896 struct functor_traits<std::negate<T> > 897 { enum { Cost = NumTraits<T>::AddCost, PacketAccess = false }; }; 898 899 template<typename T> 900 struct functor_traits<std::logical_or<T> > 901 { enum { Cost = 1, PacketAccess = false }; }; 902 903 template<typename T> 904 struct functor_traits<std::logical_and<T> > 905 { enum { Cost = 1, PacketAccess = false }; }; 906 907 template<typename T> 908 struct functor_traits<std::logical_not<T> > 909 { enum { Cost = 1, PacketAccess = false }; }; 910 911 template<typename T> 912 struct functor_traits<std::greater<T> > 913 { enum { Cost = 1, PacketAccess = false }; }; 914 915 template<typename T> 916 struct functor_traits<std::less<T> > 917 { enum { Cost = 1, PacketAccess = false }; }; 918 919 template<typename T> 920 struct functor_traits<std::greater_equal<T> > 921 { enum { Cost = 1, PacketAccess = false }; }; 922 923 template<typename T> 924 struct functor_traits<std::less_equal<T> > 925 { enum { Cost = 1, PacketAccess = false }; }; 926 927 template<typename T> 928 struct functor_traits<std::equal_to<T> > 929 { enum { Cost = 1, PacketAccess = false }; }; 930 931 template<typename T> 932 struct functor_traits<std::not_equal_to<T> > 933 { enum { Cost = 1, PacketAccess = false }; }; 934 935 template<typename T> 936 struct functor_traits<std::binder2nd<T> > 937 { enum { Cost = functor_traits<T>::Cost, PacketAccess = false }; }; 938 939 template<typename T> 940 struct functor_traits<std::binder1st<T> > 941 { enum { Cost = functor_traits<T>::Cost, PacketAccess = false }; }; 942 943 template<typename T> 944 struct functor_traits<std::unary_negate<T> > 945 { enum { Cost = 1 + functor_traits<T>::Cost, PacketAccess = false }; }; 946 947 template<typename T> 948 struct functor_traits<std::binary_negate<T> > 949 { enum { Cost = 1 + functor_traits<T>::Cost, PacketAccess = false }; }; 950 951 #ifdef EIGEN_STDEXT_SUPPORT 952 953 template<typename T0,typename T1> 954 struct functor_traits<std::project1st<T0,T1> > 955 { enum { Cost = 0, PacketAccess = false }; }; 956 957 template<typename T0,typename T1> 958 struct functor_traits<std::project2nd<T0,T1> > 959 { enum { Cost = 0, PacketAccess = false }; }; 960 961 template<typename T0,typename T1> 962 struct functor_traits<std::select2nd<std::pair<T0,T1> > > 963 { enum { Cost = 0, PacketAccess = false }; }; 964 965 template<typename T0,typename T1> 966 struct functor_traits<std::select1st<std::pair<T0,T1> > > 967 { enum { Cost = 0, PacketAccess = false }; }; 968 969 template<typename T0,typename T1> 970 struct functor_traits<std::unary_compose<T0,T1> > 971 { enum { Cost = functor_traits<T0>::Cost + functor_traits<T1>::Cost, PacketAccess = false }; }; 972 973 template<typename T0,typename T1,typename T2> 974 struct functor_traits<std::binary_compose<T0,T1,T2> > 975 { enum { Cost = functor_traits<T0>::Cost + functor_traits<T1>::Cost + functor_traits<T2>::Cost, PacketAccess = false }; }; 976 977 #endif // EIGEN_STDEXT_SUPPORT 978 979 // allow to add new functors and specializations of functor_traits from outside Eigen. 980 // this macro is really needed because functor_traits must be specialized after it is declared but before it is used... 981 #ifdef EIGEN_FUNCTORS_PLUGIN 982 #include EIGEN_FUNCTORS_PLUGIN 983 #endif 984 985 } // end namespace internal 986 987 } // end namespace Eigen 988 989 #endif // EIGEN_FUNCTORS_H 990