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 const result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return a + b; }
43  template<typename Packet>
44  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
45  { return internal::padd(a,b); }
46  template<typename Packet>
47  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type predux(const Packet& a) const
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 
64 template<> struct scalar_sum_op<bool,bool> : scalar_sum_op<int,int> {
67 };
68 
69 
75 template<typename LhsScalar,typename RhsScalar>
76 struct scalar_product_op : binary_op_base<LhsScalar,RhsScalar>
77 {
79 #ifndef EIGEN_SCALAR_BINARY_OP_PLUGIN
81 #else
84  }
85 #endif
86  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return a * b; }
87  template<typename Packet>
88  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
89  { return internal::pmul(a,b); }
90  template<typename Packet>
91  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type predux(const Packet& a) const
92  { return internal::predux_mul(a); }
93 };
94 template<typename LhsScalar,typename RhsScalar>
95 struct functor_traits<scalar_product_op<LhsScalar,RhsScalar> > {
96  enum {
99  // TODO vectorize mixed product
100  };
101 };
102 
108 template<typename LhsScalar,typename RhsScalar>
109 struct scalar_conj_product_op : binary_op_base<LhsScalar,RhsScalar>
110 {
111 
112  enum {
114  };
115 
117 
119  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const
121 
122  template<typename Packet>
123  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
125 };
126 template<typename LhsScalar,typename RhsScalar>
127 struct functor_traits<scalar_conj_product_op<LhsScalar,RhsScalar> > {
128  enum {
131  };
132 };
133 
139 template<typename LhsScalar,typename RhsScalar>
140 struct scalar_min_op : binary_op_base<LhsScalar,RhsScalar>
141 {
144  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return numext::mini(a, b); }
145  template<typename Packet>
146  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
147  { return internal::pmin(a,b); }
148  template<typename Packet>
149  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type predux(const Packet& a) const
150  { return internal::predux_min(a); }
151 };
152 template<typename LhsScalar,typename RhsScalar>
153 struct functor_traits<scalar_min_op<LhsScalar,RhsScalar> > {
154  enum {
157  };
158 };
159 
165 template<typename LhsScalar,typename RhsScalar>
166 struct scalar_max_op : binary_op_base<LhsScalar,RhsScalar>
167 {
170  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return numext::maxi(a, b); }
171  template<typename Packet>
172  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
173  { return internal::pmax(a,b); }
174  template<typename Packet>
175  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type predux(const Packet& a) const
176  { return internal::predux_max(a); }
177 };
178 template<typename LhsScalar,typename RhsScalar>
179 struct functor_traits<scalar_max_op<LhsScalar,RhsScalar> > {
180  enum {
183  };
184 };
185 
190 template<typename LhsScalar, typename RhsScalar, ComparisonName cmp> struct scalar_cmp_op;
191 
192 template<typename LhsScalar, typename RhsScalar, ComparisonName cmp>
193 struct functor_traits<scalar_cmp_op<LhsScalar,RhsScalar, cmp> > {
194  enum {
196  PacketAccess = false
197  };
198 };
199 
200 template<ComparisonName Cmp, typename LhsScalar, typename RhsScalar>
201 struct result_of<scalar_cmp_op<LhsScalar, RhsScalar, Cmp>(LhsScalar,RhsScalar)> {
202  typedef bool type;
203 };
204 
205 
206 template<typename LhsScalar, typename RhsScalar>
207 struct scalar_cmp_op<LhsScalar,RhsScalar, cmp_EQ> : binary_op_base<LhsScalar,RhsScalar>
208 {
209  typedef bool result_type;
211  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator()(const LhsScalar& a, const RhsScalar& b) const {return a==b;}
212 };
213 template<typename LhsScalar, typename RhsScalar>
214 struct scalar_cmp_op<LhsScalar,RhsScalar, cmp_LT> : binary_op_base<LhsScalar,RhsScalar>
215 {
216  typedef bool result_type;
218  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator()(const LhsScalar& a, const RhsScalar& b) const {return a<b;}
219 };
220 template<typename LhsScalar, typename RhsScalar>
221 struct scalar_cmp_op<LhsScalar,RhsScalar, cmp_LE> : binary_op_base<LhsScalar,RhsScalar>
222 {
223  typedef bool result_type;
225  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator()(const LhsScalar& a, const RhsScalar& b) const {return a<=b;}
226 };
227 template<typename LhsScalar, typename RhsScalar>
228 struct scalar_cmp_op<LhsScalar,RhsScalar, cmp_GT> : binary_op_base<LhsScalar,RhsScalar>
229 {
230  typedef bool result_type;
232  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator()(const LhsScalar& a, const RhsScalar& b) const {return a>b;}
233 };
234 template<typename LhsScalar, typename RhsScalar>
235 struct scalar_cmp_op<LhsScalar,RhsScalar, cmp_GE> : binary_op_base<LhsScalar,RhsScalar>
236 {
237  typedef bool result_type;
239  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator()(const LhsScalar& a, const RhsScalar& b) const {return a>=b;}
240 };
241 template<typename LhsScalar, typename RhsScalar>
242 struct scalar_cmp_op<LhsScalar,RhsScalar, cmp_UNORD> : binary_op_base<LhsScalar,RhsScalar>
243 {
244  typedef bool result_type;
246  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator()(const LhsScalar& a, const RhsScalar& b) const {return !(a<=b || b<=a);}
247 };
248 template<typename LhsScalar, typename RhsScalar>
249 struct scalar_cmp_op<LhsScalar,RhsScalar, cmp_NEQ> : binary_op_base<LhsScalar,RhsScalar>
250 {
251  typedef bool result_type;
253  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator()(const LhsScalar& a, const RhsScalar& b) const {return a!=b;}
254 };
255 
256 
262 template<typename Scalar>
263 struct scalar_hypot_op<Scalar,Scalar> : binary_op_base<Scalar,Scalar>
264 {
266 
267  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (const Scalar &x, const Scalar &y) const
268  {
269  // This functor is used by hypotNorm only for which it is faster to first apply abs
270  // on all coefficients prior to reduction through hypot.
271  // This way we avoid calling abs on positive and real entries, and this also permits
272  // to seamlessly handle complexes. Otherwise we would have to handle both real and complexes
273  // through the same functor...
274  return internal::positive_real_hypot(x,y);
275  }
276 };
277 template<typename Scalar>
279  enum
280  {
284  PacketAccess = false
285  };
286 };
287 
291 template<typename Scalar, typename Exponent>
292 struct scalar_pow_op : binary_op_base<Scalar,Exponent>
293 {
295 #ifndef EIGEN_SCALAR_BINARY_OP_PLUGIN
297 #else
298  scalar_pow_op() {
299  typedef Scalar LhsScalar;
300  typedef Exponent RhsScalar;
302  }
303 #endif
304  EIGEN_DEVICE_FUNC
305  inline result_type operator() (const Scalar& a, const Exponent& b) const { return numext::pow(a, b); }
306 };
307 template<typename Scalar, typename Exponent>
308 struct functor_traits<scalar_pow_op<Scalar,Exponent> > {
309  enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = false };
310 };
311 
312 
313 
314 //---------- non associative binary functors ----------
315 
321 template<typename LhsScalar,typename RhsScalar>
322 struct scalar_difference_op : binary_op_base<LhsScalar,RhsScalar>
323 {
325 #ifndef EIGEN_SCALAR_BINARY_OP_PLUGIN
327 #else
330  }
331 #endif
332  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return a - b; }
333  template<typename Packet>
334  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
335  { return internal::psub(a,b); }
336 };
337 template<typename LhsScalar,typename RhsScalar>
338 struct functor_traits<scalar_difference_op<LhsScalar,RhsScalar> > {
339  enum {
342  };
343 };
344 
350 template<typename LhsScalar,typename RhsScalar>
351 struct scalar_quotient_op : binary_op_base<LhsScalar,RhsScalar>
352 {
354 #ifndef EIGEN_SCALAR_BINARY_OP_PLUGIN
356 #else
359  }
360 #endif
361  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return a / b; }
362  template<typename Packet>
363  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
364  { return internal::pdiv(a,b); }
365 };
366 template<typename LhsScalar,typename RhsScalar>
367 struct functor_traits<scalar_quotient_op<LhsScalar,RhsScalar> > {
369  enum {
372  };
373 };
374 
375 
376 
384  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator() (const bool& a, const bool& b) const { return a && b; }
385 };
387  enum {
389  PacketAccess = false
390  };
391 };
392 
400  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator() (const bool& a, const bool& b) const { return a || b; }
401 };
403  enum {
405  PacketAccess = false
406  };
407 };
408 
416  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator() (const bool& a, const bool& b) const { return a ^ b; }
417 };
419  enum {
421  PacketAccess = false
422  };
423 };
424 
425 
426 
427 //---------- binary functors bound to a constant, thus appearing as a unary functor ----------
428 
429 // The following two classes permits to turn any binary functor into a unary one with one argument bound to a constant value.
430 // They are analogues to std::binder1st/binder2nd but with the following differences:
431 // - they are compatible with packetOp
432 // - they are portable across C++ versions (the std::binder* are deprecated in C++11)
433 template<typename BinaryOp> struct bind1st_op : BinaryOp {
434 
435  typedef typename BinaryOp::first_argument_type first_argument_type;
436  typedef typename BinaryOp::second_argument_type second_argument_type;
437  typedef typename BinaryOp::result_type result_type;
438 
439  bind1st_op(const first_argument_type &val) : m_value(val) {}
440 
441  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type operator() (const second_argument_type& b) const { return BinaryOp::operator()(m_value,b); }
442 
443  template<typename Packet>
444  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet& b) const
445  { return BinaryOp::packetOp(internal::pset1<Packet>(m_value), b); }
446 
447  first_argument_type m_value;
448 };
449 template<typename BinaryOp> struct functor_traits<bind1st_op<BinaryOp> > : functor_traits<BinaryOp> {};
450 
451 
452 template<typename BinaryOp> struct bind2nd_op : BinaryOp {
453 
454  typedef typename BinaryOp::first_argument_type first_argument_type;
455  typedef typename BinaryOp::second_argument_type second_argument_type;
456  typedef typename BinaryOp::result_type result_type;
457 
458  bind2nd_op(const second_argument_type &val) : m_value(val) {}
459 
460  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type operator() (const first_argument_type& a) const { return BinaryOp::operator()(a,m_value); }
461 
462  template<typename Packet>
463  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const
464  { return BinaryOp::packetOp(a,internal::pset1<Packet>(m_value)); }
465 
466  second_argument_type m_value;
467 };
468 template<typename BinaryOp> struct functor_traits<bind2nd_op<BinaryOp> > : functor_traits<BinaryOp> {};
469 
470 
471 } // end namespace internal
472 
473 } // end namespace Eigen
474 
475 #endif // EIGEN_BINARY_FUNCTORS_H
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_sum_op >::ReturnType result_type
SCALAR Scalar
Definition: bench_gemm.cpp:33
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type predux(const Packet &a) const
BinaryOp::result_type result_type
#define EIGEN_STRONG_INLINE
Definition: Macros.h:494
#define EIGEN_EMPTY_STRUCT_CTOR(X)
Definition: XprHelper.h:22
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_min_op >::ReturnType result_type
BinaryOp::first_argument_type first_argument_type
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
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_max_op >::ReturnType result_type
EIGEN_DEVICE_FUNC unpacket_traits< Packet >::type predux_min(const Packet &a)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &a, const Packet &b) const
BinaryOp::second_argument_type second_argument_type
EIGEN_DEVICE_FUNC unpacket_traits< Packet >::type predux_max(const Packet &a)
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &a, const Packet &b) const
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
EIGEN_DEVICE_FUNC unpacket_traits< Packet >::type predux(const Packet &a)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &b) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &a, const Packet &b) const
EIGEN_STRONG_INLINE RealScalar positive_real_hypot(const RealScalar &x, const RealScalar &y)
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
Array33i a
EIGEN_DEVICE_FUNC Packet padd(const Packet &a, const Packet &b)
EIGEN_DEVICE_FUNC Packet pmin(const Packet &a, const Packet &b)
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_product_op >::ReturnType result_type
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const 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 const Packet packetOp(const Packet &a, const Packet &b) const
first_argument_type m_value
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_quotient_op >::ReturnType result_type
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type predux(const Packet &a) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type predux(const Packet &a) const
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
EIGEN_DEVICE_FUNC internal::pow_impl< ScalarX, ScalarY >::result_type pow(const ScalarX &x, const ScalarY &y)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &a, const Packet &b) const
EIGEN_DEVICE_FUNC Packet pdiv(const Packet &a, const Packet &b)
bind1st_op(const first_argument_type &val)
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)
#define EIGEN_DEPRECATED
Definition: Macros.h:600
EIGEN_DEVICE_FUNC Packet psub(const Packet &a, const Packet &b)
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition: XprHelper.h:766
bind2nd_op(const second_argument_type &val)
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
EIGEN_DEVICE_FUNC Packet pmul(const Packet &a, const Packet &b)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &a) const
EIGEN_DEVICE_FUNC Packet pmax(const Packet &a, const Packet &b)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:42