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