TensorFunctors.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) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
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_CXX11_TENSOR_TENSOR_FUNCTORS_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_FUNCTORS_H
12 
13 namespace Eigen {
14 namespace internal {
15 
16 
20 template <typename Scalar>
21 struct scalar_mod_op {
22  EIGEN_DEVICE_FUNC scalar_mod_op(const Scalar& divisor) : m_divisor(divisor) {}
25 };
26 template <typename Scalar>
28 { enum { Cost = scalar_div_cost<Scalar,false>::value, PacketAccess = false }; };
29 
30 
34 template <typename Scalar>
37  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator() (const Scalar& a, const Scalar& b) const { return a % b; }
38 };
39 template <typename Scalar>
41 { enum { Cost = scalar_div_cost<Scalar,false>::value, PacketAccess = false }; };
42 
43 template <typename Scalar>
47  operator()(const Scalar& a, const Scalar& b) const {
48  return numext::fmod(a, b);
49  }
50 };
51 template <typename Scalar>
53  enum { Cost = 13, // Reciprocal throughput of FPREM on Haswell.
54  PacketAccess = false };
55 };
56 
57 template<typename Reducer, typename Device>
59  enum {
60  Cost = 1,
61  PacketAccess = false,
62  IsStateful = false,
63  IsExactlyAssociative = true
64  };
65 };
66 
67 // Standard reduction functors
68 template <typename T> struct SumReducer
69 {
70  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T t, T* accum) const {
72  *accum = sum_op(*accum, t);
73  }
74  template <typename Packet>
76  (*accum) = padd<Packet>(*accum, p);
77  }
78 
81  return conv(0);
82  }
83  template <typename Packet>
85  return pset1<Packet>(initialize());
86  }
88  return accum;
89  }
90  template <typename Packet>
92  return vaccum;
93  }
94  template <typename Packet>
95  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalizeBoth(const T saccum, const Packet& vaccum) const {
97  return sum_op(saccum, predux(vaccum));
98  }
99 };
100 
101 template <typename T, typename Device>
102 struct reducer_traits<SumReducer<T>, Device> {
103  enum {
106  IsStateful = false,
107  IsExactlyAssociative = NumTraits<T>::IsInteger
108  };
109 };
110 
111 template <typename T> struct MeanReducer
112 {
114  MeanReducer() : scalarCount_(0), packetCount_(0) { }
115 
118  *accum = sum_op(*accum, t);
119  scalarCount_++;
120  }
121  template <typename Packet>
123  (*accum) = padd<Packet>(*accum, p);
124  packetCount_++;
125  }
126 
129  return conv(0);
130  }
131  template <typename Packet>
133  return pset1<Packet>(initialize());
134  }
137  return quotient_op(accum, T(scalarCount_));
138  }
139  template <typename Packet>
141  return pdiv(vaccum, pset1<Packet>(T(packetCount_)));
142  }
143  template <typename Packet>
144  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalizeBoth(const T saccum, const Packet& vaccum) const {
147  return quotient_op(
148  sum_op(saccum, predux(vaccum)),
149  T(scalarCount_ + packetCount_ * unpacket_traits<Packet>::size));
150  }
151 
152  protected:
155 };
156 
157 template <typename T, typename Device>
158 struct reducer_traits<MeanReducer<T>, Device> {
159  enum {
163  IsStateful = true,
164  IsExactlyAssociative = NumTraits<T>::IsInteger
165  };
166 };
167 
168 
169 template <typename T, bool IsMax = true, bool IsInteger = true>
173  }
174 };
175 template <typename T>
176 struct MinMaxBottomValue<T, true, false> {
179  }
180 };
181 template <typename T>
182 struct MinMaxBottomValue<T, false, true> {
185  }
186 };
187 template <typename T>
188 struct MinMaxBottomValue<T, false, false> {
191  }
192 };
193 
194 
195 template <typename T, int NaNPropagation=PropagateFast> struct MaxReducer
196 {
197  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T t, T* accum) const {
199  *accum = op(t, *accum);
200  }
201  template <typename Packet>
204  (*accum) = op.packetOp(*accum, p);
205  }
207  return MinMaxBottomValue<T, /*IsMax=*/true, Eigen::NumTraits<T>::IsInteger>::bottom_value();
208  }
209  template <typename Packet>
211  return pset1<Packet>(initialize());
212  }
214  return accum;
215  }
216  template <typename Packet>
218  return vaccum;
219  }
220  template <typename Packet>
221  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalizeBoth(const T saccum, const Packet& vaccum) const {
223  return op(saccum, op.predux(vaccum));
224  }
225 };
226 
227 template <typename T, typename Device, int NaNPropagation>
228  struct reducer_traits<MaxReducer<T, NaNPropagation>, Device> {
229  enum {
232  IsStateful = false,
233  IsExactlyAssociative = (NaNPropagation!=PropagateFast)
234  };
235 };
236 
237 template <typename T, int NaNPropagation=PropagateFast> struct MinReducer
238 {
239  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T t, T* accum) const {
241  *accum = op(t, *accum);
242  }
243  template <typename Packet>
246  (*accum) = op.packetOp(*accum, p);
247  }
249  return MinMaxBottomValue<T, /*IsMax=*/false, Eigen::NumTraits<T>::IsInteger>::bottom_value();
250  }
251  template <typename Packet>
253  return pset1<Packet>(initialize());
254  }
256  return accum;
257  }
258  template <typename Packet>
260  return vaccum;
261  }
262  template <typename Packet>
263  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalizeBoth(const T saccum, const Packet& vaccum) const {
265  return op(saccum, op.predux(vaccum));
266  }
267 };
268 
269 template <typename T, typename Device, int NaNPropagation>
270  struct reducer_traits<MinReducer<T, NaNPropagation>, Device> {
271  enum {
274  IsStateful = false,
275  IsExactlyAssociative = (NaNPropagation!=PropagateFast)
276  };
277 };
278 
279 template <typename T> struct ProdReducer
280 {
281  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T t, T* accum) const {
283  (*accum) = prod_op(*accum, t);
284  }
285  template <typename Packet>
287  (*accum) = pmul<Packet>(*accum, p);
288  }
291  return conv(1);
292  }
293  template <typename Packet>
295  return pset1<Packet>(initialize());
296  }
298  return accum;
299  }
300  template <typename Packet>
302  return vaccum;
303  }
304  template <typename Packet>
305  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalizeBoth(const T saccum, const Packet& vaccum) const {
307  return prod_op(saccum, predux_mul(vaccum));
308  }
309 };
310 
311 template <typename T, typename Device>
312 struct reducer_traits<ProdReducer<T>, Device> {
313  enum {
316  IsStateful = false,
317  IsExactlyAssociative = true
318  };
319 };
320 
321 
323 {
324  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(bool t, bool* accum) const {
325  *accum = *accum && t;
326  }
328  return true;
329  }
331  return accum;
332  }
333 };
334 
335 template <typename Device>
336 struct reducer_traits<AndReducer, Device> {
337  enum {
338  Cost = 1,
339  PacketAccess = false,
340  IsStateful = false,
341  IsExactlyAssociative = true
342  };
343 };
344 
345 
346 struct OrReducer {
347  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(bool t, bool* accum) const {
348  *accum = *accum || t;
349  }
351  return false;
352  }
354  return accum;
355  }
356 };
357 
358 template <typename Device>
359 struct reducer_traits<OrReducer, Device> {
360  enum {
361  Cost = 1,
362  PacketAccess = false,
363  IsStateful = false,
364  IsExactlyAssociative = true
365  };
366 };
367 
368 // Argmin/Argmax reducers. Returns the first occurrence if multiple locations
369 // contain the same min/max value.
370 template <typename T> struct ArgMaxTupleReducer
371 {
372  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T t, T* accum) const {
373  if (t.second < accum->second) {
374  return;
375  } else if (t.second > accum->second || accum->first > t.first ) {
376  *accum = t;
377  }
378  }
381  }
383  return accum;
384  }
385 };
386 
387 template <typename T, typename Device>
389  enum {
391  PacketAccess = false,
392  IsStateful = false,
393  IsExactlyAssociative = true
394  };
395 };
396 
397 
398 template <typename T> struct ArgMinTupleReducer
399 {
400  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T& t, T* accum) const {
401  if (t.second > accum->second) {
402  return;
403  } else if (t.second < accum->second || accum->first > t.first) {
404  *accum = t;
405  }
406  }
409  }
411  return accum;
412  }
413 };
414 
415 template <typename T, typename Device>
417  enum {
419  PacketAccess = false,
420  IsStateful = false,
421  IsExactlyAssociative = true
422  };
423 };
424 
425 
426 template <typename T, typename Index, size_t NumDims>
428  public:
429  static const bool PacketAccess = false;
430 
432  const array<T, NumDims>& std_devs)
433  : m_means(means)
434  {
436  for (size_t i = 0; i < NumDims; ++i) {
437  m_two_sigmas[i] = std_devs[i] * std_devs[i] * 2;
438  }
439  }
440 
442  T tmp = T(0);
444  for (size_t i = 0; i < NumDims; ++i) {
445  T offset = coordinates[i] - m_means[i];
446  tmp += offset * offset / m_two_sigmas[i];
447  }
448  return numext::exp(-tmp);
449  }
450 
451  private:
454 };
455 
456 template <typename T, typename Index, size_t NumDims>
458  enum {
463  };
464 };
465 
466 template <typename Scalar>
468  EIGEN_DEVICE_FUNC inline scalar_clamp_op(const Scalar& _min, const Scalar& _max) : m_min(_min), m_max(_max) {}
470  operator()(const Scalar& x) const {
471  return numext::mini(numext::maxi(x, m_min), m_max);
472  }
473  template <typename Packet>
475  packetOp(const Packet& x) const {
476  return internal::pmin(internal::pmax(x, pset1<Packet>(m_min)), pset1<Packet>(m_max));
477  }
478  const Scalar m_min;
479  const Scalar m_max;
480 };
481 template<typename Scalar>
484 
485 } // end namespace internal
486 } // end namespace Eigen
487 
488 #endif // EIGEN_CXX11_TENSOR_TENSOR_FUNCTORS_H
EIGEN_DEVICE_FUNC scalar_clamp_op(const Scalar &_min, const Scalar &_max)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet finalizePacket(const Packet &vaccum) const
SCALAR Scalar
Definition: bench_gemm.cpp:46
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T initialize() const
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
EIGEN_DEVICE_FUNC GaussianGenerator(const array< T, NumDims > &means, const array< T, NumDims > &std_devs)
#define EIGEN_EMPTY_STRUCT_CTOR(X)
Definition: XprHelper.h:22
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T initialize() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T initialize() const
EIGEN_DEVICE_FUNC T operator()(const array< Index, NumDims > &coordinates) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalize(const T accum) const
Scalar * b
Definition: benchVecAdd.cpp:17
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet finalizePacket(const Packet &vaccum) const
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T exp(const T &x)
PyObject * conv(PyObject *o)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalize(const T accum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reducePacket(const Packet &p, Packet *accum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const Scalar &a) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T initialize() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet finalizePacket(const Packet &vaccum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(bool t, bool *accum) const
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 y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate offset
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet finalizePacket(const Packet &vaccum) const
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T bottom_value()
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
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 T initialize() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalize(const T &accum) const
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T bottom_value()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T t, T *accum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T &t, T *accum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool finalize(bool accum) const
EIGEN_DEVICE_FUNC Packet pmin(const Packet &a, const Packet &b)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalize(const T accum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet initializePacket() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool finalize(bool accum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T t, T *accum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T t, T *accum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet &x) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool initialize() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reducePacket(const Packet &p, Packet *accum) const
EIGEN_DEVICE_FUNC scalar_mod_op(const Scalar &divisor)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet initializePacket() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalize(const T accum) const
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reducePacket(const Packet &p, Packet *accum)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE MeanReducer()
Eigen::Triplet< double > T
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T t, T *accum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet &a, const Packet &b) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet initializePacket() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalize(const T &accum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalizeBoth(const T saccum, const Packet &vaccum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalizeBoth(const T saccum, const Packet &vaccum) const
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalizeBoth(const T saccum, const Packet &vaccum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet initializePacket() const
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Definition: Meta.h:66
Point2Pair means(const std::vector< Point2Pair > &abPointPairs)
Calculate the two means of a set of Point2 pairs.
Definition: Point2.cpp:116
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T initialize() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(bool t, bool *accum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalizeBoth(const T saccum, const Packet &vaccum) const
EIGEN_DEVICE_FUNC Packet pdiv(const Packet &a, const Packet &b)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reducePacket(const Packet &p, Packet *accum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool initialize() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T initialize() const
EIGEN_DEVICE_FUNC unpacket_traits< Packet >::type predux_mul(const Packet &a)
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T bottom_value()
float * p
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type predux(const Packet &a) const
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T bottom_value()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reducePacket(const Packet &p, Packet *accum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T t, T *accum)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalize(const T accum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet initializePacket() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet finalizePacket(const Packet &vaccum) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(const Scalar &x) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet &a, const Packet &b) const
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
Values initialize(const NonlinearFactorGraph &graph, bool useOdometricPath)
Definition: lago.cpp:375
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type predux(const Packet &a) const
EIGEN_DEVICE_FUNC Packet pmax(const Packet &a, const Packet &b)
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T fmod(const T &a, const T &b)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalizeBoth(const T saccum, const Packet &vaccum) const
Point2 t(10, 10)
#define EIGEN_UNROLL_LOOP
Definition: Macros.h:1461
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const Scalar &a, const Scalar &b) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T t, T *accum) const


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:36:59