BinaryFunctors.h
Go to the documentation of this file.
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@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_BINARY_FUNCTORS_H
11 #define EIGEN_BINARY_FUNCTORS_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
17 //---------- associative binary functors ----------
18 
19 template<typename Arg1, typename Arg2>
21 {
22  typedef Arg1 first_argument_type;
23  typedef Arg2 second_argument_type;
24 };
25 
31 template<typename LhsScalar,typename RhsScalar>
32 struct scalar_sum_op : binary_op_base<LhsScalar,RhsScalar>
33 {
35 #ifndef EIGEN_SCALAR_BINARY_OP_PLUGIN
37 #else
38  scalar_sum_op() {
40  }
41 #endif
42  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return a + b; }
43  template<typename Packet>
45  { return internal::padd(a,b); }
46  template<typename Packet>
48  { return internal::predux(a); }
49 };
50 template<typename LhsScalar,typename RhsScalar>
51 struct functor_traits<scalar_sum_op<LhsScalar,RhsScalar> > {
52  enum {
55  // TODO vectorize mixed sum
56  };
57 };
58 
59 
60 template<>
61 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool scalar_sum_op<bool,bool>::operator() (const bool& a, const bool& b) const { return a || b; }
62 
63 
69 template<typename LhsScalar,typename RhsScalar>
70 struct scalar_product_op : binary_op_base<LhsScalar,RhsScalar>
71 {
73 #ifndef EIGEN_SCALAR_BINARY_OP_PLUGIN
75 #else
78  }
79 #endif
80  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return a * b; }
81  template<typename Packet>
83  { return internal::pmul(a,b); }
84  template<typename Packet>
86  { return internal::predux_mul(a); }
87 };
88 template<typename LhsScalar,typename RhsScalar>
89 struct functor_traits<scalar_product_op<LhsScalar,RhsScalar> > {
90  enum {
93  // TODO vectorize mixed product
94  };
95 };
96 
97 template<>
98 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool scalar_product_op<bool,bool>::operator() (const bool& a, const bool& b) const { return a && b; }
99 
100 
106 template<typename LhsScalar,typename RhsScalar>
107 struct scalar_conj_product_op : binary_op_base<LhsScalar,RhsScalar>
108 {
109 
110  enum {
112  };
113 
115 
117  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const LhsScalar& a, const RhsScalar& b) const
119 
120  template<typename Packet>
123 };
124 template<typename LhsScalar,typename RhsScalar>
125 struct functor_traits<scalar_conj_product_op<LhsScalar,RhsScalar> > {
126  enum {
129  };
130 };
131 
137 template<typename LhsScalar,typename RhsScalar, int NaNPropagation>
138 struct scalar_min_op : binary_op_base<LhsScalar,RhsScalar>
139 {
142  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const LhsScalar& a, const RhsScalar& b) const {
143  return internal::pmin<NaNPropagation>(a, b);
144  }
145  template<typename Packet>
147  {
148  return internal::pmin<NaNPropagation>(a,b);
149  }
150  template<typename Packet>
152  {
153  return internal::predux_min<NaNPropagation>(a);
154  }
155 };
156 
157 template<typename LhsScalar,typename RhsScalar, int NaNPropagation>
158 struct functor_traits<scalar_min_op<LhsScalar,RhsScalar, NaNPropagation> > {
159  enum {
162  };
163 };
164 
170 template<typename LhsScalar,typename RhsScalar, int NaNPropagation>
171 struct scalar_max_op : binary_op_base<LhsScalar,RhsScalar>
172 {
175  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const LhsScalar& a, const RhsScalar& b) const {
176  return internal::pmax<NaNPropagation>(a,b);
177  }
178  template<typename Packet>
180  {
181  return internal::pmax<NaNPropagation>(a,b);
182  }
183  template<typename Packet>
185  {
186  return internal::predux_max<NaNPropagation>(a);
187  }
188 };
189 
190 template<typename LhsScalar,typename RhsScalar, int NaNPropagation>
191 struct functor_traits<scalar_max_op<LhsScalar,RhsScalar, NaNPropagation> > {
192  enum {
195  };
196 };
197 
202 template<typename LhsScalar, typename RhsScalar, ComparisonName cmp> struct scalar_cmp_op;
203 
204 template<typename LhsScalar, typename RhsScalar, ComparisonName cmp>
205 struct functor_traits<scalar_cmp_op<LhsScalar,RhsScalar, cmp> > {
206  enum {
208  PacketAccess = false
209  };
210 };
211 
212 template<ComparisonName Cmp, typename LhsScalar, typename RhsScalar>
213 struct result_of<scalar_cmp_op<LhsScalar, RhsScalar, Cmp>(LhsScalar,RhsScalar)> {
214  typedef bool type;
215 };
216 
217 
218 template<typename LhsScalar, typename RhsScalar>
219 struct scalar_cmp_op<LhsScalar,RhsScalar, cmp_EQ> : binary_op_base<LhsScalar,RhsScalar>
220 {
221  typedef bool result_type;
223  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator()(const LhsScalar& a, const RhsScalar& b) const {return a==b;}
224 };
225 template<typename LhsScalar, typename RhsScalar>
226 struct scalar_cmp_op<LhsScalar,RhsScalar, cmp_LT> : binary_op_base<LhsScalar,RhsScalar>
227 {
228  typedef bool result_type;
230  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator()(const LhsScalar& a, const RhsScalar& b) const {return a<b;}
231 };
232 template<typename LhsScalar, typename RhsScalar>
233 struct scalar_cmp_op<LhsScalar,RhsScalar, cmp_LE> : binary_op_base<LhsScalar,RhsScalar>
234 {
235  typedef bool result_type;
237  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator()(const LhsScalar& a, const RhsScalar& b) const {return a<=b;}
238 };
239 template<typename LhsScalar, typename RhsScalar>
240 struct scalar_cmp_op<LhsScalar,RhsScalar, cmp_GT> : binary_op_base<LhsScalar,RhsScalar>
241 {
242  typedef bool result_type;
244  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator()(const LhsScalar& a, const RhsScalar& b) const {return a>b;}
245 };
246 template<typename LhsScalar, typename RhsScalar>
247 struct scalar_cmp_op<LhsScalar,RhsScalar, cmp_GE> : binary_op_base<LhsScalar,RhsScalar>
248 {
249  typedef bool result_type;
251  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator()(const LhsScalar& a, const RhsScalar& b) const {return a>=b;}
252 };
253 template<typename LhsScalar, typename RhsScalar>
254 struct scalar_cmp_op<LhsScalar,RhsScalar, cmp_UNORD> : binary_op_base<LhsScalar,RhsScalar>
255 {
256  typedef bool result_type;
258  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator()(const LhsScalar& a, const RhsScalar& b) const {return !(a<=b || b<=a);}
259 };
260 template<typename LhsScalar, typename RhsScalar>
261 struct scalar_cmp_op<LhsScalar,RhsScalar, cmp_NEQ> : binary_op_base<LhsScalar,RhsScalar>
262 {
263  typedef bool result_type;
265  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator()(const LhsScalar& a, const RhsScalar& b) const {return a!=b;}
266 };
267 
273 template<typename Scalar>
274 struct scalar_hypot_op<Scalar,Scalar> : binary_op_base<Scalar,Scalar>
275 {
277 
278  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (const Scalar &x, const Scalar &y) const
279  {
280  // This functor is used by hypotNorm only for which it is faster to first apply abs
281  // on all coefficients prior to reduction through hypot.
282  // This way we avoid calling abs on positive and real entries, and this also permits
283  // to seamlessly handle complexes. Otherwise we would have to handle both real and complexes
284  // through the same functor...
285  return internal::positive_real_hypot(x,y);
286  }
287 };
288 template<typename Scalar>
290  enum
291  {
295  PacketAccess = false
296  };
297 };
298 
303 template<typename Scalar, typename Exponent>
304 struct scalar_pow_op : binary_op_base<Scalar,Exponent>
305 {
307 #ifndef EIGEN_SCALAR_BINARY_OP_PLUGIN
309 #else
310  scalar_pow_op() {
311  typedef Scalar LhsScalar;
312  typedef Exponent RhsScalar;
314  }
315 #endif
316 
318  inline result_type operator() (const Scalar& a, const Exponent& b) const { return numext::pow(a, b); }
319 
320  template<typename Packet>
322  {
323  return generic_pow(a,b);
324  }
325 };
326 
327 template<typename Scalar, typename Exponent>
328 struct functor_traits<scalar_pow_op<Scalar,Exponent> > {
329  enum {
334  // Temporarly disable packet access for half/bfloat16 until
335  // accuracy is improved.
337  )
338  };
339 };
340 
341 //---------- non associative binary functors ----------
342 
348 template<typename LhsScalar,typename RhsScalar>
349 struct scalar_difference_op : binary_op_base<LhsScalar,RhsScalar>
350 {
352 #ifndef EIGEN_SCALAR_BINARY_OP_PLUGIN
354 #else
357  }
358 #endif
359  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return a - b; }
360  template<typename Packet>
362  { return internal::psub(a,b); }
363 };
364 template<typename LhsScalar,typename RhsScalar>
365 struct functor_traits<scalar_difference_op<LhsScalar,RhsScalar> > {
366  enum {
369  };
370 };
371 
377 template<typename LhsScalar,typename RhsScalar>
378 struct scalar_quotient_op : binary_op_base<LhsScalar,RhsScalar>
379 {
381 #ifndef EIGEN_SCALAR_BINARY_OP_PLUGIN
383 #else
386  }
387 #endif
388  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return a / b; }
389  template<typename Packet>
391  { return internal::pdiv(a,b); }
392 };
393 template<typename LhsScalar,typename RhsScalar>
394 struct functor_traits<scalar_quotient_op<LhsScalar,RhsScalar> > {
396  enum {
399  };
400 };
401 
402 
403 
411  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator() (const bool& a, const bool& b) const { return a && b; }
412  template<typename Packet>
414  { return internal::pand(a,b); }
415 };
417  enum {
419  PacketAccess = true
420  };
421 };
422 
430  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator() (const bool& a, const bool& b) const { return a || b; }
431  template<typename Packet>
433  { return internal::por(a,b); }
434 };
436  enum {
438  PacketAccess = true
439  };
440 };
441 
449  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator() (const bool& a, const bool& b) const { return a ^ b; }
450  template<typename Packet>
452  { return internal::pxor(a,b); }
453 };
455  enum {
457  PacketAccess = true
458  };
459 };
460 
466 template<typename LhsScalar,typename RhsScalar>
467 struct scalar_absolute_difference_op : binary_op_base<LhsScalar,RhsScalar>
468 {
470 #ifndef EIGEN_SCALAR_BINARY_OP_PLUGIN
472 #else
475  }
476 #endif
477  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const
478  { return numext::absdiff(a,b); }
479  template<typename Packet>
481  { return internal::pabsdiff(a,b); }
482 };
483 template<typename LhsScalar,typename RhsScalar>
484 struct functor_traits<scalar_absolute_difference_op<LhsScalar,RhsScalar> > {
485  enum {
488  };
489 };
490 
491 
492 
493 //---------- binary functors bound to a constant, thus appearing as a unary functor ----------
494 
495 // The following two classes permits to turn any binary functor into a unary one with one argument bound to a constant value.
496 // They are analogues to std::binder1st/binder2nd but with the following differences:
497 // - they are compatible with packetOp
498 // - they are portable across C++ versions (the std::binder* are deprecated in C++11)
499 template<typename BinaryOp> struct bind1st_op : BinaryOp {
500 
501  typedef typename BinaryOp::first_argument_type first_argument_type;
502  typedef typename BinaryOp::second_argument_type second_argument_type;
503  typedef typename BinaryOp::result_type result_type;
504 
505  EIGEN_DEVICE_FUNC explicit bind1st_op(const first_argument_type &val) : m_value(val) {}
506 
507  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type operator() (const second_argument_type& b) const { return BinaryOp::operator()(m_value,b); }
508 
509  template<typename Packet>
511  { return BinaryOp::packetOp(internal::pset1<Packet>(m_value), b); }
512 
513  first_argument_type m_value;
514 };
515 template<typename BinaryOp> struct functor_traits<bind1st_op<BinaryOp> > : functor_traits<BinaryOp> {};
516 
517 
518 template<typename BinaryOp> struct bind2nd_op : BinaryOp {
519 
520  typedef typename BinaryOp::first_argument_type first_argument_type;
521  typedef typename BinaryOp::second_argument_type second_argument_type;
522  typedef typename BinaryOp::result_type result_type;
523 
524  EIGEN_DEVICE_FUNC explicit bind2nd_op(const second_argument_type &val) : m_value(val) {}
525 
526  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type operator() (const first_argument_type& a) const { return BinaryOp::operator()(a,m_value); }
527 
528  template<typename Packet>
530  { return BinaryOp::packetOp(a,internal::pset1<Packet>(m_value)); }
531 
532  second_argument_type m_value;
533 };
534 template<typename BinaryOp> struct functor_traits<bind2nd_op<BinaryOp> > : functor_traits<BinaryOp> {};
535 
536 
537 } // end namespace internal
538 
539 } // end namespace Eigen
540 
541 #endif // EIGEN_BINARY_FUNCTORS_H
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_sum_op >::ReturnType result_type
SCALAR Scalar
Definition: bench_gemm.cpp:46
BinaryOp::result_type result_type
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
#define EIGEN_EMPTY_STRUCT_CTOR(X)
Definition: XprHelper.h:22
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_max_op >::ReturnType result_type
BinaryOp::first_argument_type first_argument_type
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &a, const Packet &b) const
BinaryOp::first_argument_type first_argument_type
Scalar * b
Definition: benchVecAdd.cpp:17
scalar_quotient_op< LhsScalar, RhsScalar >::result_type result_type
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &a, const Packet &b) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type predux(const Packet &a) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator()(const LhsScalar &a, const RhsScalar &b) const
BinaryOp::second_argument_type second_argument_type
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &b) const
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_min_op >::ReturnType result_type
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &a, const Packet &b) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet &a, const Packet &b) const
EIGEN_DEVICE_FUNC unpacket_traits< Packet >::type predux(const Packet &a)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator()(const LhsScalar &a, const RhsScalar &b) const
EIGEN_STRONG_INLINE Packet8h pxor(const Packet8h &a, const Packet8h &b)
ScalarBinaryOpTraits< Scalar, Exponent, scalar_pow_op >::ReturnType result_type
BinaryOp::result_type result_type
#define EIGEN_SCALAR_BINARY_OP_PLUGIN
BinaryOp::second_argument_type second_argument_type
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &a, const Packet &b) const
EIGEN_DEVICE_FUNC Packet padd(const Packet &a, const Packet &b)
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_product_op >::ReturnType result_type
EIGEN_DEVICE_FUNC Packet pabsdiff(const Packet &a, const Packet &b)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &a, const Packet &b) const
EIGEN_DEVICE_FUNC bind2nd_op(const second_argument_type &val)
first_argument_type m_value
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet generic_pow(const Packet &x, const Packet &y)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet &a, const Packet &b) const
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_quotient_op >::ReturnType result_type
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet &a, const Packet &b) const
EIGEN_STRONG_INLINE Packet8h pand(const Packet8h &a, const Packet8h &b)
EIGEN_DEVICE_FUNC bind1st_op(const first_argument_type &val)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &a, const Packet &b) const
EIGEN_DEVICE_FUNC internal::pow_impl< ScalarX, ScalarY >::result_type pow(const ScalarX &x, const ScalarY &y)
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
EIGEN_DEVICE_FUNC Packet pdiv(const Packet &a, const Packet &b)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &a) const
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_absolute_difference_op >::ReturnType result_type
second_argument_type m_value
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_conj_product_op >::ReturnType result_type
EIGEN_DEVICE_FUNC unpacket_traits< Packet >::type predux_mul(const Packet &a)
EIGEN_DEVICE_FUNC Packet psub(const Packet &a, const Packet &b)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type predux(const Packet &a) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &a, const Packet &b) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type predux(const Packet &a) const
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition: XprHelper.h:801
EIGEN_STRONG_INLINE Packet8h por(const Packet8h &a, const Packet8h &b)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet &a, const Packet &b) const
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_difference_op >::ReturnType result_type
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
internal::enable_if< internal::valid_indexed_view_overload< RowIndices, ColIndices >::value &&internal::traits< typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::ReturnAsIndexedView, typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::type operator()(const RowIndices &rowIndices, const ColIndices &colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet &a, const Packet &b) const
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T absdiff(const T &x, const T &y)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type predux(const Packet &a) const
EIGEN_DEVICE_FUNC Packet pmul(const Packet &a, const Packet &b)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE RealScalar positive_real_hypot(const RealScalar &x, const RealScalar &y)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:33:58