Please, help us to better know about our user community by answering the following short survey: https://forms.gle/wpyrxWi18ox9Z5ae9
Eigen  3.3.9
MathFunctions.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@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_MATHFUNCTIONS_H
11 #define EIGEN_MATHFUNCTIONS_H
12 
13 // source: http://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html
14 // TODO this should better be moved to NumTraits
15 #define EIGEN_PI 3.141592653589793238462643383279502884197169399375105820974944592307816406L
16 
17 
18 namespace Eigen {
19 
20 // On WINCE, std::abs is defined for int only, so let's defined our own overloads:
21 // This issue has been confirmed with MSVC 2008 only, but the issue might exist for more recent versions too.
22 #if EIGEN_OS_WINCE && EIGEN_COMP_MSVC && EIGEN_COMP_MSVC<=1500
23 long abs(long x) { return (labs(x)); }
24 double abs(double x) { return (fabs(x)); }
25 float abs(float x) { return (fabsf(x)); }
26 long double abs(long double x) { return (fabsl(x)); }
27 #endif
28 
29 namespace internal {
30 
51 template<typename T, typename dummy = void>
52 struct global_math_functions_filtering_base
53 {
54  typedef T type;
55 };
56 
57 template<typename T> struct always_void { typedef void type; };
58 
59 template<typename T>
60 struct global_math_functions_filtering_base
61  <T,
62  typename always_void<typename T::Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl>::type
63  >
64 {
65  typedef typename T::Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl type;
66 };
67 
68 #define EIGEN_MATHFUNC_IMPL(func, scalar) Eigen::internal::func##_impl<typename Eigen::internal::global_math_functions_filtering_base<scalar>::type>
69 #define EIGEN_MATHFUNC_RETVAL(func, scalar) typename Eigen::internal::func##_retval<typename Eigen::internal::global_math_functions_filtering_base<scalar>::type>::type
70 
71 /****************************************************************************
72 * Implementation of real *
73 ****************************************************************************/
74 
75 template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
76 struct real_default_impl
77 {
78  typedef typename NumTraits<Scalar>::Real RealScalar;
79  EIGEN_DEVICE_FUNC
80  static inline RealScalar run(const Scalar& x)
81  {
82  return x;
83  }
84 };
85 
86 template<typename Scalar>
87 struct real_default_impl<Scalar,true>
88 {
89  typedef typename NumTraits<Scalar>::Real RealScalar;
90  EIGEN_DEVICE_FUNC
91  static inline RealScalar run(const Scalar& x)
92  {
93  using std::real;
94  return real(x);
95  }
96 };
97 
98 template<typename Scalar> struct real_impl : real_default_impl<Scalar> {};
99 
100 #ifdef __CUDA_ARCH__
101 template<typename T>
102 struct real_impl<std::complex<T> >
103 {
104  typedef T RealScalar;
105  EIGEN_DEVICE_FUNC
106  static inline T run(const std::complex<T>& x)
107  {
108  return x.real();
109  }
110 };
111 #endif
112 
113 template<typename Scalar>
114 struct real_retval
115 {
116  typedef typename NumTraits<Scalar>::Real type;
117 };
118 
119 /****************************************************************************
120 * Implementation of imag *
121 ****************************************************************************/
122 
123 template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
124 struct imag_default_impl
125 {
126  typedef typename NumTraits<Scalar>::Real RealScalar;
127  EIGEN_DEVICE_FUNC
128  static inline RealScalar run(const Scalar&)
129  {
130  return RealScalar(0);
131  }
132 };
133 
134 template<typename Scalar>
135 struct imag_default_impl<Scalar,true>
136 {
137  typedef typename NumTraits<Scalar>::Real RealScalar;
138  EIGEN_DEVICE_FUNC
139  static inline RealScalar run(const Scalar& x)
140  {
141  using std::imag;
142  return imag(x);
143  }
144 };
145 
146 template<typename Scalar> struct imag_impl : imag_default_impl<Scalar> {};
147 
148 #ifdef __CUDA_ARCH__
149 template<typename T>
150 struct imag_impl<std::complex<T> >
151 {
152  typedef T RealScalar;
153  EIGEN_DEVICE_FUNC
154  static inline T run(const std::complex<T>& x)
155  {
156  return x.imag();
157  }
158 };
159 #endif
160 
161 template<typename Scalar>
162 struct imag_retval
163 {
164  typedef typename NumTraits<Scalar>::Real type;
165 };
166 
167 /****************************************************************************
168 * Implementation of real_ref *
169 ****************************************************************************/
170 
171 template<typename Scalar>
172 struct real_ref_impl
173 {
174  typedef typename NumTraits<Scalar>::Real RealScalar;
175  EIGEN_DEVICE_FUNC
176  static inline RealScalar& run(Scalar& x)
177  {
178  return reinterpret_cast<RealScalar*>(&x)[0];
179  }
180  EIGEN_DEVICE_FUNC
181  static inline const RealScalar& run(const Scalar& x)
182  {
183  return reinterpret_cast<const RealScalar*>(&x)[0];
184  }
185 };
186 
187 template<typename Scalar>
188 struct real_ref_retval
189 {
190  typedef typename NumTraits<Scalar>::Real & type;
191 };
192 
193 /****************************************************************************
194 * Implementation of imag_ref *
195 ****************************************************************************/
196 
197 template<typename Scalar, bool IsComplex>
198 struct imag_ref_default_impl
199 {
200  typedef typename NumTraits<Scalar>::Real RealScalar;
201  EIGEN_DEVICE_FUNC
202  static inline RealScalar& run(Scalar& x)
203  {
204  return reinterpret_cast<RealScalar*>(&x)[1];
205  }
206  EIGEN_DEVICE_FUNC
207  static inline const RealScalar& run(const Scalar& x)
208  {
209  return reinterpret_cast<RealScalar*>(&x)[1];
210  }
211 };
212 
213 template<typename Scalar>
214 struct imag_ref_default_impl<Scalar, false>
215 {
216  EIGEN_DEVICE_FUNC
217  static inline Scalar run(Scalar&)
218  {
219  return Scalar(0);
220  }
221  EIGEN_DEVICE_FUNC
222  static inline const Scalar run(const Scalar&)
223  {
224  return Scalar(0);
225  }
226 };
227 
228 template<typename Scalar>
229 struct imag_ref_impl : imag_ref_default_impl<Scalar, NumTraits<Scalar>::IsComplex> {};
230 
231 template<typename Scalar>
232 struct imag_ref_retval
233 {
234  typedef typename NumTraits<Scalar>::Real & type;
235 };
236 
237 /****************************************************************************
238 * Implementation of conj *
239 ****************************************************************************/
240 
241 template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
242 struct conj_impl
243 {
244  EIGEN_DEVICE_FUNC
245  static inline Scalar run(const Scalar& x)
246  {
247  return x;
248  }
249 };
250 
251 template<typename Scalar>
252 struct conj_impl<Scalar,true>
253 {
254  EIGEN_DEVICE_FUNC
255  static inline Scalar run(const Scalar& x)
256  {
257  using std::conj;
258  return conj(x);
259  }
260 };
261 
262 template<typename Scalar>
263 struct conj_retval
264 {
265  typedef Scalar type;
266 };
267 
268 /****************************************************************************
269 * Implementation of abs2 *
270 ****************************************************************************/
271 
272 template<typename Scalar,bool IsComplex>
273 struct abs2_impl_default
274 {
275  typedef typename NumTraits<Scalar>::Real RealScalar;
276  EIGEN_DEVICE_FUNC
277  static inline RealScalar run(const Scalar& x)
278  {
279  return x*x;
280  }
281 };
282 
283 template<typename Scalar>
284 struct abs2_impl_default<Scalar, true> // IsComplex
285 {
286  typedef typename NumTraits<Scalar>::Real RealScalar;
287  EIGEN_DEVICE_FUNC
288  static inline RealScalar run(const Scalar& x)
289  {
290  return x.real()*x.real() + x.imag()*x.imag();
291  }
292 };
293 
294 template<typename Scalar>
295 struct abs2_impl
296 {
297  typedef typename NumTraits<Scalar>::Real RealScalar;
298  EIGEN_DEVICE_FUNC
299  static inline RealScalar run(const Scalar& x)
300  {
301  return abs2_impl_default<Scalar,NumTraits<Scalar>::IsComplex>::run(x);
302  }
303 };
304 
305 template<typename Scalar>
306 struct abs2_retval
307 {
308  typedef typename NumTraits<Scalar>::Real type;
309 };
310 
311 /****************************************************************************
312 * Implementation of norm1 *
313 ****************************************************************************/
314 
315 template<typename Scalar, bool IsComplex>
316 struct norm1_default_impl;
317 
318 template<typename Scalar>
319 struct norm1_default_impl<Scalar,true>
320 {
321  typedef typename NumTraits<Scalar>::Real RealScalar;
322  EIGEN_DEVICE_FUNC
323  static inline RealScalar run(const Scalar& x)
324  {
325  EIGEN_USING_STD_MATH(abs);
326  return abs(x.real()) + abs(x.imag());
327  }
328 };
329 
330 template<typename Scalar>
331 struct norm1_default_impl<Scalar, false>
332 {
333  EIGEN_DEVICE_FUNC
334  static inline Scalar run(const Scalar& x)
335  {
336  EIGEN_USING_STD_MATH(abs);
337  return abs(x);
338  }
339 };
340 
341 template<typename Scalar>
342 struct norm1_impl : norm1_default_impl<Scalar, NumTraits<Scalar>::IsComplex> {};
343 
344 template<typename Scalar>
345 struct norm1_retval
346 {
347  typedef typename NumTraits<Scalar>::Real type;
348 };
349 
350 /****************************************************************************
351 * Implementation of hypot *
352 ****************************************************************************/
353 
354 template<typename Scalar> struct hypot_impl;
355 
356 template<typename Scalar>
357 struct hypot_retval
358 {
359  typedef typename NumTraits<Scalar>::Real type;
360 };
361 
362 /****************************************************************************
363 * Implementation of cast *
364 ****************************************************************************/
365 
366 template<typename OldType, typename NewType>
367 struct cast_impl
368 {
369  EIGEN_DEVICE_FUNC
370  static inline NewType run(const OldType& x)
371  {
372  return static_cast<NewType>(x);
373  }
374 };
375 
376 // here, for once, we're plainly returning NewType: we don't want cast to do weird things.
377 
378 template<typename OldType, typename NewType>
379 EIGEN_DEVICE_FUNC
380 inline NewType cast(const OldType& x)
381 {
382  return cast_impl<OldType, NewType>::run(x);
383 }
384 
385 /****************************************************************************
386 * Implementation of round *
387 ****************************************************************************/
388 
389 #if EIGEN_HAS_CXX11_MATH
390  template<typename Scalar>
391  struct round_impl {
392  static inline Scalar run(const Scalar& x)
393  {
394  EIGEN_STATIC_ASSERT((!NumTraits<Scalar>::IsComplex), NUMERIC_TYPE_MUST_BE_REAL)
395  using std::round;
396  return round(x);
397  }
398  };
399 #else
400  template<typename Scalar>
401  struct round_impl
402  {
403  static inline Scalar run(const Scalar& x)
404  {
405  EIGEN_STATIC_ASSERT((!NumTraits<Scalar>::IsComplex), NUMERIC_TYPE_MUST_BE_REAL)
406  EIGEN_USING_STD_MATH(floor);
407  EIGEN_USING_STD_MATH(ceil);
408  return (x > Scalar(0)) ? floor(x + Scalar(0.5)) : ceil(x - Scalar(0.5));
409  }
410  };
411 #endif
412 
413 template<typename Scalar>
414 struct round_retval
415 {
416  typedef Scalar type;
417 };
418 
419 /****************************************************************************
420 * Implementation of arg *
421 ****************************************************************************/
422 
423 #if EIGEN_HAS_CXX11_MATH
424  template<typename Scalar>
425  struct arg_impl {
426  static inline Scalar run(const Scalar& x)
427  {
428  EIGEN_USING_STD_MATH(arg);
429  return arg(x);
430  }
431  };
432 #else
433  template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
434  struct arg_default_impl
435  {
436  typedef typename NumTraits<Scalar>::Real RealScalar;
437  EIGEN_DEVICE_FUNC
438  static inline RealScalar run(const Scalar& x)
439  {
440  return (x < Scalar(0)) ? Scalar(EIGEN_PI) : Scalar(0); }
441  };
442 
443  template<typename Scalar>
444  struct arg_default_impl<Scalar,true>
445  {
446  typedef typename NumTraits<Scalar>::Real RealScalar;
447  EIGEN_DEVICE_FUNC
448  static inline RealScalar run(const Scalar& x)
449  {
450  EIGEN_USING_STD_MATH(arg);
451  return arg(x);
452  }
453  };
454 
455  template<typename Scalar> struct arg_impl : arg_default_impl<Scalar> {};
456 #endif
457 
458 template<typename Scalar>
459 struct arg_retval
460 {
461  typedef typename NumTraits<Scalar>::Real type;
462 };
463 
464 /****************************************************************************
465 * Implementation of log1p *
466 ****************************************************************************/
467 
468 namespace std_fallback {
469  // fallback log1p implementation in case there is no log1p(Scalar) function in namespace of Scalar,
470  // or that there is no suitable std::log1p function available
471  template<typename Scalar>
472  EIGEN_DEVICE_FUNC inline Scalar log1p(const Scalar& x) {
473  EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
474  typedef typename NumTraits<Scalar>::Real RealScalar;
475  EIGEN_USING_STD_MATH(log);
476  Scalar x1p = RealScalar(1) + x;
477  return numext::equal_strict(x1p, Scalar(1)) ? x : x * ( log(x1p) / (x1p - RealScalar(1)) );
478  }
479 }
480 
481 template<typename Scalar>
482 struct log1p_impl {
483  static inline Scalar run(const Scalar& x)
484  {
485  EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
486  #if EIGEN_HAS_CXX11_MATH
487  using std::log1p;
488  #else
489  using std_fallback::log1p;
490  #endif
491  return log1p(x);
492  }
493 };
494 
495 // Specialization for complex types that are not supported by std::log1p.
496 template <typename RealScalar>
497 struct log1p_impl<std::complex<RealScalar> > {
498  EIGEN_DEVICE_FUNC static inline std::complex<RealScalar> run(
499  const std::complex<RealScalar>& x) {
500  EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
501  return std_fallback::log1p(x);
502  }
503 };
504 
505 template<typename Scalar>
506 struct log1p_retval
507 {
508  typedef Scalar type;
509 };
510 
511 /****************************************************************************
512 * Implementation of pow *
513 ****************************************************************************/
514 
515 template<typename ScalarX,typename ScalarY, bool IsInteger = NumTraits<ScalarX>::IsInteger&&NumTraits<ScalarY>::IsInteger>
516 struct pow_impl
517 {
518  //typedef Scalar retval;
519  typedef typename ScalarBinaryOpTraits<ScalarX,ScalarY,internal::scalar_pow_op<ScalarX,ScalarY> >::ReturnType result_type;
520  static EIGEN_DEVICE_FUNC inline result_type run(const ScalarX& x, const ScalarY& y)
521  {
522  EIGEN_USING_STD_MATH(pow);
523  return pow(x, y);
524  }
525 };
526 
527 template<typename ScalarX,typename ScalarY>
528 struct pow_impl<ScalarX,ScalarY, true>
529 {
530  typedef ScalarX result_type;
531  static EIGEN_DEVICE_FUNC inline ScalarX run(ScalarX x, ScalarY y)
532  {
533  ScalarX res(1);
534  eigen_assert(!NumTraits<ScalarY>::IsSigned || y >= 0);
535  if(y & 1) res *= x;
536  y >>= 1;
537  while(y)
538  {
539  x *= x;
540  if(y&1) res *= x;
541  y >>= 1;
542  }
543  return res;
544  }
545 };
546 
547 /****************************************************************************
548 * Implementation of random *
549 ****************************************************************************/
550 
551 template<typename Scalar,
552  bool IsComplex,
553  bool IsInteger>
554 struct random_default_impl {};
555 
556 template<typename Scalar>
557 struct random_impl : random_default_impl<Scalar, NumTraits<Scalar>::IsComplex, NumTraits<Scalar>::IsInteger> {};
558 
559 template<typename Scalar>
560 struct random_retval
561 {
562  typedef Scalar type;
563 };
564 
565 template<typename Scalar> inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random(const Scalar& x, const Scalar& y);
566 template<typename Scalar> inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random();
567 
568 template<typename Scalar>
569 struct random_default_impl<Scalar, false, false>
570 {
571  static inline Scalar run(const Scalar& x, const Scalar& y)
572  {
573  return x + (y-x) * Scalar(std::rand()) / Scalar(RAND_MAX);
574  }
575  static inline Scalar run()
576  {
577  return run(Scalar(NumTraits<Scalar>::IsSigned ? -1 : 0), Scalar(1));
578  }
579 };
580 
581 enum {
582  meta_floor_log2_terminate,
583  meta_floor_log2_move_up,
584  meta_floor_log2_move_down,
585  meta_floor_log2_bogus
586 };
587 
588 template<unsigned int n, int lower, int upper> struct meta_floor_log2_selector
589 {
590  enum { middle = (lower + upper) / 2,
591  value = (upper <= lower + 1) ? int(meta_floor_log2_terminate)
592  : (n < (1 << middle)) ? int(meta_floor_log2_move_down)
593  : (n==0) ? int(meta_floor_log2_bogus)
594  : int(meta_floor_log2_move_up)
595  };
596 };
597 
598 template<unsigned int n,
599  int lower = 0,
600  int upper = sizeof(unsigned int) * CHAR_BIT - 1,
601  int selector = meta_floor_log2_selector<n, lower, upper>::value>
602 struct meta_floor_log2 {};
603 
604 template<unsigned int n, int lower, int upper>
605 struct meta_floor_log2<n, lower, upper, meta_floor_log2_move_down>
606 {
607  enum { value = meta_floor_log2<n, lower, meta_floor_log2_selector<n, lower, upper>::middle>::value };
608 };
609 
610 template<unsigned int n, int lower, int upper>
611 struct meta_floor_log2<n, lower, upper, meta_floor_log2_move_up>
612 {
613  enum { value = meta_floor_log2<n, meta_floor_log2_selector<n, lower, upper>::middle, upper>::value };
614 };
615 
616 template<unsigned int n, int lower, int upper>
617 struct meta_floor_log2<n, lower, upper, meta_floor_log2_terminate>
618 {
619  enum { value = (n >= ((unsigned int)(1) << (lower+1))) ? lower+1 : lower };
620 };
621 
622 template<unsigned int n, int lower, int upper>
623 struct meta_floor_log2<n, lower, upper, meta_floor_log2_bogus>
624 {
625  // no value, error at compile time
626 };
627 
628 template<typename Scalar>
629 struct random_default_impl<Scalar, false, true>
630 {
631  static inline Scalar run(const Scalar& x, const Scalar& y)
632  {
633  if (y <= x)
634  return x;
635  // ScalarU is the unsigned counterpart of Scalar, possibly Scalar itself.
636  typedef typename make_unsigned<Scalar>::type ScalarU;
637  // ScalarX is the widest of ScalarU and unsigned int.
638  // We'll deal only with ScalarX and unsigned int below thus avoiding signed
639  // types and arithmetic and signed overflows (which are undefined behavior).
640  typedef typename conditional<(ScalarU(-1) > unsigned(-1)), ScalarU, unsigned>::type ScalarX;
641  // The following difference doesn't overflow, provided our integer types are two's
642  // complement and have the same number of padding bits in signed and unsigned variants.
643  // This is the case in most modern implementations of C++.
644  ScalarX range = ScalarX(y) - ScalarX(x);
645  ScalarX offset = 0;
646  ScalarX divisor = 1;
647  ScalarX multiplier = 1;
648  const unsigned rand_max = RAND_MAX;
649  if (range <= rand_max) divisor = (rand_max + 1) / (range + 1);
650  else multiplier = 1 + range / (rand_max + 1);
651  // Rejection sampling.
652  do {
653  offset = (unsigned(std::rand()) * multiplier) / divisor;
654  } while (offset > range);
655  return Scalar(ScalarX(x) + offset);
656  }
657 
658  static inline Scalar run()
659  {
660 #ifdef EIGEN_MAKING_DOCS
661  return run(Scalar(NumTraits<Scalar>::IsSigned ? -10 : 0), Scalar(10));
662 #else
663  enum { rand_bits = meta_floor_log2<(unsigned int)(RAND_MAX)+1>::value,
664  scalar_bits = sizeof(Scalar) * CHAR_BIT,
665  shift = EIGEN_PLAIN_ENUM_MAX(0, int(rand_bits) - int(scalar_bits)),
666  offset = NumTraits<Scalar>::IsSigned ? (1 << (EIGEN_PLAIN_ENUM_MIN(rand_bits,scalar_bits)-1)) : 0
667  };
668  return Scalar((std::rand() >> shift) - offset);
669 #endif
670  }
671 };
672 
673 template<typename Scalar>
674 struct random_default_impl<Scalar, true, false>
675 {
676  static inline Scalar run(const Scalar& x, const Scalar& y)
677  {
678  return Scalar(random(x.real(), y.real()),
679  random(x.imag(), y.imag()));
680  }
681  static inline Scalar run()
682  {
683  typedef typename NumTraits<Scalar>::Real RealScalar;
684  return Scalar(random<RealScalar>(), random<RealScalar>());
685  }
686 };
687 
688 template<typename Scalar>
689 inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random(const Scalar& x, const Scalar& y)
690 {
691  return EIGEN_MATHFUNC_IMPL(random, Scalar)::run(x, y);
692 }
693 
694 template<typename Scalar>
695 inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random()
696 {
697  return EIGEN_MATHFUNC_IMPL(random, Scalar)::run();
698 }
699 
700 // Implementatin of is* functions
701 
702 // std::is* do not work with fast-math and gcc, std::is* are available on MSVC 2013 and newer, as well as in clang.
703 #if (EIGEN_HAS_CXX11_MATH && !(EIGEN_COMP_GNUC_STRICT && __FINITE_MATH_ONLY__)) || (EIGEN_COMP_MSVC>=1800) || (EIGEN_COMP_CLANG)
704 #define EIGEN_USE_STD_FPCLASSIFY 1
705 #else
706 #define EIGEN_USE_STD_FPCLASSIFY 0
707 #endif
708 
709 template<typename T>
710 EIGEN_DEVICE_FUNC
711 typename internal::enable_if<internal::is_integral<T>::value,bool>::type
712 isnan_impl(const T&) { return false; }
713 
714 template<typename T>
715 EIGEN_DEVICE_FUNC
716 typename internal::enable_if<internal::is_integral<T>::value,bool>::type
717 isinf_impl(const T&) { return false; }
718 
719 template<typename T>
720 EIGEN_DEVICE_FUNC
721 typename internal::enable_if<internal::is_integral<T>::value,bool>::type
722 isfinite_impl(const T&) { return true; }
723 
724 template<typename T>
725 EIGEN_DEVICE_FUNC
726 typename internal::enable_if<(!internal::is_integral<T>::value)&&(!NumTraits<T>::IsComplex),bool>::type
727 isfinite_impl(const T& x)
728 {
729  #ifdef __CUDA_ARCH__
730  return (::isfinite)(x);
731  #elif EIGEN_USE_STD_FPCLASSIFY
732  using std::isfinite;
733  return isfinite EIGEN_NOT_A_MACRO (x);
734  #else
735  return x<=NumTraits<T>::highest() && x>=NumTraits<T>::lowest();
736  #endif
737 }
738 
739 template<typename T>
740 EIGEN_DEVICE_FUNC
741 typename internal::enable_if<(!internal::is_integral<T>::value)&&(!NumTraits<T>::IsComplex),bool>::type
742 isinf_impl(const T& x)
743 {
744  #ifdef __CUDA_ARCH__
745  return (::isinf)(x);
746  #elif EIGEN_USE_STD_FPCLASSIFY
747  using std::isinf;
748  return isinf EIGEN_NOT_A_MACRO (x);
749  #else
750  return x>NumTraits<T>::highest() || x<NumTraits<T>::lowest();
751  #endif
752 }
753 
754 template<typename T>
755 EIGEN_DEVICE_FUNC
756 typename internal::enable_if<(!internal::is_integral<T>::value)&&(!NumTraits<T>::IsComplex),bool>::type
757 isnan_impl(const T& x)
758 {
759  #ifdef __CUDA_ARCH__
760  return (::isnan)(x);
761  #elif EIGEN_USE_STD_FPCLASSIFY
762  using std::isnan;
763  return isnan EIGEN_NOT_A_MACRO (x);
764  #else
765  return x != x;
766  #endif
767 }
768 
769 #if (!EIGEN_USE_STD_FPCLASSIFY)
770 
771 #if EIGEN_COMP_MSVC
772 
773 template<typename T> EIGEN_DEVICE_FUNC bool isinf_msvc_helper(T x)
774 {
775  return _fpclass(x)==_FPCLASS_NINF || _fpclass(x)==_FPCLASS_PINF;
776 }
777 
778 //MSVC defines a _isnan builtin function, but for double only
779 EIGEN_DEVICE_FUNC inline bool isnan_impl(const long double& x) { return _isnan(x)!=0; }
780 EIGEN_DEVICE_FUNC inline bool isnan_impl(const double& x) { return _isnan(x)!=0; }
781 EIGEN_DEVICE_FUNC inline bool isnan_impl(const float& x) { return _isnan(x)!=0; }
782 
783 EIGEN_DEVICE_FUNC inline bool isinf_impl(const long double& x) { return isinf_msvc_helper(x); }
784 EIGEN_DEVICE_FUNC inline bool isinf_impl(const double& x) { return isinf_msvc_helper(x); }
785 EIGEN_DEVICE_FUNC inline bool isinf_impl(const float& x) { return isinf_msvc_helper(x); }
786 
787 #elif (defined __FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ && EIGEN_COMP_GNUC)
788 
789 #if EIGEN_GNUC_AT_LEAST(5,0)
790  #define EIGEN_TMP_NOOPT_ATTRIB EIGEN_DEVICE_FUNC inline __attribute__((optimize("no-finite-math-only")))
791 #else
792  // NOTE the inline qualifier and noinline attribute are both needed: the former is to avoid linking issue (duplicate symbol),
793  // while the second prevent too aggressive optimizations in fast-math mode:
794  #define EIGEN_TMP_NOOPT_ATTRIB EIGEN_DEVICE_FUNC inline __attribute__((noinline,optimize("no-finite-math-only")))
795 #endif
796 
797 template<> EIGEN_TMP_NOOPT_ATTRIB bool isnan_impl(const long double& x) { return __builtin_isnan(x); }
798 template<> EIGEN_TMP_NOOPT_ATTRIB bool isnan_impl(const double& x) { return __builtin_isnan(x); }
799 template<> EIGEN_TMP_NOOPT_ATTRIB bool isnan_impl(const float& x) { return __builtin_isnan(x); }
800 template<> EIGEN_TMP_NOOPT_ATTRIB bool isinf_impl(const double& x) { return __builtin_isinf(x); }
801 template<> EIGEN_TMP_NOOPT_ATTRIB bool isinf_impl(const float& x) { return __builtin_isinf(x); }
802 template<> EIGEN_TMP_NOOPT_ATTRIB bool isinf_impl(const long double& x) { return __builtin_isinf(x); }
803 
804 #undef EIGEN_TMP_NOOPT_ATTRIB
805 
806 #endif
807 
808 #endif
809 
810 // The following overload are defined at the end of this file
811 template<typename T> EIGEN_DEVICE_FUNC bool isfinite_impl(const std::complex<T>& x);
812 template<typename T> EIGEN_DEVICE_FUNC bool isnan_impl(const std::complex<T>& x);
813 template<typename T> EIGEN_DEVICE_FUNC bool isinf_impl(const std::complex<T>& x);
814 
815 template<typename T> T generic_fast_tanh_float(const T& a_x);
816 
817 } // end namespace internal
818 
819 /****************************************************************************
820 * Generic math functions *
821 ****************************************************************************/
822 
823 namespace numext {
824 
825 #ifndef __CUDA_ARCH__
826 template<typename T>
827 EIGEN_DEVICE_FUNC
828 EIGEN_ALWAYS_INLINE T mini(const T& x, const T& y)
829 {
830  EIGEN_USING_STD_MATH(min);
831  return min EIGEN_NOT_A_MACRO (x,y);
832 }
833 
834 template<typename T>
835 EIGEN_DEVICE_FUNC
836 EIGEN_ALWAYS_INLINE T maxi(const T& x, const T& y)
837 {
838  EIGEN_USING_STD_MATH(max);
839  return max EIGEN_NOT_A_MACRO (x,y);
840 }
841 #else
842 template<typename T>
843 EIGEN_DEVICE_FUNC
844 EIGEN_ALWAYS_INLINE T mini(const T& x, const T& y)
845 {
846  return y < x ? y : x;
847 }
848 template<>
849 EIGEN_DEVICE_FUNC
850 EIGEN_ALWAYS_INLINE float mini(const float& x, const float& y)
851 {
852  return fminf(x, y);
853 }
854 template<typename T>
855 EIGEN_DEVICE_FUNC
856 EIGEN_ALWAYS_INLINE T maxi(const T& x, const T& y)
857 {
858  return x < y ? y : x;
859 }
860 template<>
861 EIGEN_DEVICE_FUNC
862 EIGEN_ALWAYS_INLINE float maxi(const float& x, const float& y)
863 {
864  return fmaxf(x, y);
865 }
866 #endif
867 
868 
869 template<typename Scalar>
870 EIGEN_DEVICE_FUNC
871 inline EIGEN_MATHFUNC_RETVAL(real, Scalar) real(const Scalar& x)
872 {
873  return EIGEN_MATHFUNC_IMPL(real, Scalar)::run(x);
874 }
875 
876 template<typename Scalar>
877 EIGEN_DEVICE_FUNC
878 inline typename internal::add_const_on_value_type< EIGEN_MATHFUNC_RETVAL(real_ref, Scalar) >::type real_ref(const Scalar& x)
879 {
880  return internal::real_ref_impl<Scalar>::run(x);
881 }
882 
883 template<typename Scalar>
884 EIGEN_DEVICE_FUNC
885 inline EIGEN_MATHFUNC_RETVAL(real_ref, Scalar) real_ref(Scalar& x)
886 {
887  return EIGEN_MATHFUNC_IMPL(real_ref, Scalar)::run(x);
888 }
889 
890 template<typename Scalar>
891 EIGEN_DEVICE_FUNC
892 inline EIGEN_MATHFUNC_RETVAL(imag, Scalar) imag(const Scalar& x)
893 {
894  return EIGEN_MATHFUNC_IMPL(imag, Scalar)::run(x);
895 }
896 
897 template<typename Scalar>
898 EIGEN_DEVICE_FUNC
899 inline EIGEN_MATHFUNC_RETVAL(arg, Scalar) arg(const Scalar& x)
900 {
901  return EIGEN_MATHFUNC_IMPL(arg, Scalar)::run(x);
902 }
903 
904 template<typename Scalar>
905 EIGEN_DEVICE_FUNC
906 inline typename internal::add_const_on_value_type< EIGEN_MATHFUNC_RETVAL(imag_ref, Scalar) >::type imag_ref(const Scalar& x)
907 {
908  return internal::imag_ref_impl<Scalar>::run(x);
909 }
910 
911 template<typename Scalar>
912 EIGEN_DEVICE_FUNC
913 inline EIGEN_MATHFUNC_RETVAL(imag_ref, Scalar) imag_ref(Scalar& x)
914 {
915  return EIGEN_MATHFUNC_IMPL(imag_ref, Scalar)::run(x);
916 }
917 
918 template<typename Scalar>
919 EIGEN_DEVICE_FUNC
920 inline EIGEN_MATHFUNC_RETVAL(conj, Scalar) conj(const Scalar& x)
921 {
922  return EIGEN_MATHFUNC_IMPL(conj, Scalar)::run(x);
923 }
924 
925 template<typename Scalar>
926 EIGEN_DEVICE_FUNC
927 inline EIGEN_MATHFUNC_RETVAL(abs2, Scalar) abs2(const Scalar& x)
928 {
929  return EIGEN_MATHFUNC_IMPL(abs2, Scalar)::run(x);
930 }
931 
932 EIGEN_DEVICE_FUNC
933 inline bool abs2(bool x) { return x; }
934 
935 template<typename Scalar>
936 EIGEN_DEVICE_FUNC
937 inline EIGEN_MATHFUNC_RETVAL(norm1, Scalar) norm1(const Scalar& x)
938 {
939  return EIGEN_MATHFUNC_IMPL(norm1, Scalar)::run(x);
940 }
941 
942 template<typename Scalar>
943 EIGEN_DEVICE_FUNC
944 inline EIGEN_MATHFUNC_RETVAL(hypot, Scalar) hypot(const Scalar& x, const Scalar& y)
945 {
946  return EIGEN_MATHFUNC_IMPL(hypot, Scalar)::run(x, y);
947 }
948 
949 template<typename Scalar>
950 EIGEN_DEVICE_FUNC
951 inline EIGEN_MATHFUNC_RETVAL(log1p, Scalar) log1p(const Scalar& x)
952 {
953  return EIGEN_MATHFUNC_IMPL(log1p, Scalar)::run(x);
954 }
955 
956 #ifdef __CUDACC__
957 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
958 float log1p(const float &x) { return ::log1pf(x); }
959 
960 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
961 double log1p(const double &x) { return ::log1p(x); }
962 #endif
963 
964 template<typename ScalarX,typename ScalarY>
965 EIGEN_DEVICE_FUNC
966 inline typename internal::pow_impl<ScalarX,ScalarY>::result_type pow(const ScalarX& x, const ScalarY& y)
967 {
968  return internal::pow_impl<ScalarX,ScalarY>::run(x, y);
969 }
970 
971 template<typename T> EIGEN_DEVICE_FUNC bool (isnan) (const T &x) { return internal::isnan_impl(x); }
972 template<typename T> EIGEN_DEVICE_FUNC bool (isinf) (const T &x) { return internal::isinf_impl(x); }
973 template<typename T> EIGEN_DEVICE_FUNC bool (isfinite)(const T &x) { return internal::isfinite_impl(x); }
974 
975 template<typename Scalar>
976 EIGEN_DEVICE_FUNC
977 inline EIGEN_MATHFUNC_RETVAL(round, Scalar) round(const Scalar& x)
978 {
979  return EIGEN_MATHFUNC_IMPL(round, Scalar)::run(x);
980 }
981 
982 template<typename T>
983 EIGEN_DEVICE_FUNC
984 T (floor)(const T& x)
985 {
986  EIGEN_USING_STD_MATH(floor);
987  return floor(x);
988 }
989 
990 #ifdef __CUDACC__
991 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
992 float floor(const float &x) { return ::floorf(x); }
993 
994 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
995 double floor(const double &x) { return ::floor(x); }
996 #endif
997 
998 template<typename T>
999 EIGEN_DEVICE_FUNC
1000 T (ceil)(const T& x)
1001 {
1002  EIGEN_USING_STD_MATH(ceil);
1003  return ceil(x);
1004 }
1005 
1006 #ifdef __CUDACC__
1007 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1008 float ceil(const float &x) { return ::ceilf(x); }
1009 
1010 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1011 double ceil(const double &x) { return ::ceil(x); }
1012 #endif
1013 
1014 
1017 inline int log2(int x)
1018 {
1019  eigen_assert(x>=0);
1020  unsigned int v(x);
1021  static const int table[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
1022  v |= v >> 1;
1023  v |= v >> 2;
1024  v |= v >> 4;
1025  v |= v >> 8;
1026  v |= v >> 16;
1027  return table[(v * 0x07C4ACDDU) >> 27];
1028 }
1029 
1039 template<typename T>
1040 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1041 T sqrt(const T &x)
1042 {
1043  EIGEN_USING_STD_MATH(sqrt);
1044  return sqrt(x);
1045 }
1046 
1047 template<typename T>
1048 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1049 T log(const T &x) {
1050  EIGEN_USING_STD_MATH(log);
1051  return log(x);
1052 }
1053 
1054 #ifdef __CUDACC__
1055 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1056 float log(const float &x) { return ::logf(x); }
1057 
1058 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1059 double log(const double &x) { return ::log(x); }
1060 #endif
1061 
1062 template<typename T>
1063 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1064 typename internal::enable_if<NumTraits<T>::IsSigned || NumTraits<T>::IsComplex,typename NumTraits<T>::Real>::type
1065 abs(const T &x) {
1066  EIGEN_USING_STD_MATH(abs);
1067  return abs(x);
1068 }
1069 
1070 template<typename T>
1071 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1072 typename internal::enable_if<!(NumTraits<T>::IsSigned || NumTraits<T>::IsComplex),typename NumTraits<T>::Real>::type
1073 abs(const T &x) {
1074  return x;
1075 }
1076 
1077 #if defined(__SYCL_DEVICE_ONLY__)
1078 EIGEN_ALWAYS_INLINE float abs(float x) { return cl::sycl::fabs(x); }
1079 EIGEN_ALWAYS_INLINE double abs(double x) { return cl::sycl::fabs(x); }
1080 #endif // defined(__SYCL_DEVICE_ONLY__)
1081 
1082 #ifdef __CUDACC__
1083 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1084 float abs(const float &x) { return ::fabsf(x); }
1085 
1086 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1087 double abs(const double &x) { return ::fabs(x); }
1088 
1089 template <> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1090 float abs(const std::complex<float>& x) {
1091  return ::hypotf(x.real(), x.imag());
1092 }
1093 
1094 template <> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1095 double abs(const std::complex<double>& x) {
1096  return ::hypot(x.real(), x.imag());
1097 }
1098 #endif
1099 
1100 template<typename T>
1101 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1102 T exp(const T &x) {
1103  EIGEN_USING_STD_MATH(exp);
1104  return exp(x);
1105 }
1106 
1107 #ifdef __CUDACC__
1108 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1109 float exp(const float &x) { return ::expf(x); }
1110 
1111 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1112 double exp(const double &x) { return ::exp(x); }
1113 #endif
1114 
1115 template<typename T>
1116 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1117 T cos(const T &x) {
1118  EIGEN_USING_STD_MATH(cos);
1119  return cos(x);
1120 }
1121 
1122 #ifdef __CUDACC__
1123 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1124 float cos(const float &x) { return ::cosf(x); }
1125 
1126 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1127 double cos(const double &x) { return ::cos(x); }
1128 #endif
1129 
1130 template<typename T>
1131 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1132 T sin(const T &x) {
1133  EIGEN_USING_STD_MATH(sin);
1134  return sin(x);
1135 }
1136 
1137 #ifdef __CUDACC__
1138 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1139 float sin(const float &x) { return ::sinf(x); }
1140 
1141 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1142 double sin(const double &x) { return ::sin(x); }
1143 #endif
1144 
1145 template<typename T>
1146 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1147 T tan(const T &x) {
1148  EIGEN_USING_STD_MATH(tan);
1149  return tan(x);
1150 }
1151 
1152 #ifdef __CUDACC__
1153 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1154 float tan(const float &x) { return ::tanf(x); }
1155 
1156 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1157 double tan(const double &x) { return ::tan(x); }
1158 #endif
1159 
1160 template<typename T>
1161 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1162 T acos(const T &x) {
1163  EIGEN_USING_STD_MATH(acos);
1164  return acos(x);
1165 }
1166 
1167 #ifdef __CUDACC__
1168 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1169 float acos(const float &x) { return ::acosf(x); }
1170 
1171 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1172 double acos(const double &x) { return ::acos(x); }
1173 #endif
1174 
1175 template<typename T>
1176 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1177 T asin(const T &x) {
1178  EIGEN_USING_STD_MATH(asin);
1179  return asin(x);
1180 }
1181 
1182 #ifdef __CUDACC__
1183 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1184 float asin(const float &x) { return ::asinf(x); }
1185 
1186 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1187 double asin(const double &x) { return ::asin(x); }
1188 #endif
1189 
1190 template<typename T>
1191 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1192 T atan(const T &x) {
1193  EIGEN_USING_STD_MATH(atan);
1194  return atan(x);
1195 }
1196 
1197 #ifdef __CUDACC__
1198 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1199 float atan(const float &x) { return ::atanf(x); }
1200 
1201 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1202 double atan(const double &x) { return ::atan(x); }
1203 #endif
1204 
1205 
1206 template<typename T>
1207 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1208 T cosh(const T &x) {
1209  EIGEN_USING_STD_MATH(cosh);
1210  return cosh(x);
1211 }
1212 
1213 #ifdef __CUDACC__
1214 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1215 float cosh(const float &x) { return ::coshf(x); }
1216 
1217 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1218 double cosh(const double &x) { return ::cosh(x); }
1219 #endif
1220 
1221 template<typename T>
1222 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1223 T sinh(const T &x) {
1224  EIGEN_USING_STD_MATH(sinh);
1225  return sinh(x);
1226 }
1227 
1228 #ifdef __CUDACC__
1229 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1230 float sinh(const float &x) { return ::sinhf(x); }
1231 
1232 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1233 double sinh(const double &x) { return ::sinh(x); }
1234 #endif
1235 
1236 template<typename T>
1237 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1238 T tanh(const T &x) {
1239  EIGEN_USING_STD_MATH(tanh);
1240  return tanh(x);
1241 }
1242 
1243 #if (!defined(__CUDACC__)) && EIGEN_FAST_MATH
1244 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1245 float tanh(float x) { return internal::generic_fast_tanh_float(x); }
1246 #endif
1247 
1248 #ifdef __CUDACC__
1249 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1250 float tanh(const float &x) { return ::tanhf(x); }
1251 
1252 template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1253 double tanh(const double &x) { return ::tanh(x); }
1254 #endif
1255 
1256 template <typename T>
1257 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1258 T fmod(const T& a, const T& b) {
1259  EIGEN_USING_STD_MATH(fmod);
1260  return fmod(a, b);
1261 }
1262 
1263 #ifdef __CUDACC__
1264 template <>
1265 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1266 float fmod(const float& a, const float& b) {
1267  return ::fmodf(a, b);
1268 }
1269 
1270 template <>
1271 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1272 double fmod(const double& a, const double& b) {
1273  return ::fmod(a, b);
1274 }
1275 #endif
1276 
1277 } // end namespace numext
1278 
1279 namespace internal {
1280 
1281 template<typename T>
1282 EIGEN_DEVICE_FUNC bool isfinite_impl(const std::complex<T>& x)
1283 {
1284  return (numext::isfinite)(numext::real(x)) && (numext::isfinite)(numext::imag(x));
1285 }
1286 
1287 template<typename T>
1288 EIGEN_DEVICE_FUNC bool isnan_impl(const std::complex<T>& x)
1289 {
1290  return (numext::isnan)(numext::real(x)) || (numext::isnan)(numext::imag(x));
1291 }
1292 
1293 template<typename T>
1294 EIGEN_DEVICE_FUNC bool isinf_impl(const std::complex<T>& x)
1295 {
1296  return ((numext::isinf)(numext::real(x)) || (numext::isinf)(numext::imag(x))) && (!(numext::isnan)(x));
1297 }
1298 
1299 /****************************************************************************
1300 * Implementation of fuzzy comparisons *
1301 ****************************************************************************/
1302 
1303 template<typename Scalar,
1304  bool IsComplex,
1305  bool IsInteger>
1306 struct scalar_fuzzy_default_impl {};
1307 
1308 template<typename Scalar>
1309 struct scalar_fuzzy_default_impl<Scalar, false, false>
1310 {
1311  typedef typename NumTraits<Scalar>::Real RealScalar;
1312  template<typename OtherScalar> EIGEN_DEVICE_FUNC
1313  static inline bool isMuchSmallerThan(const Scalar& x, const OtherScalar& y, const RealScalar& prec)
1314  {
1315  return numext::abs(x) <= numext::abs(y) * prec;
1316  }
1317  EIGEN_DEVICE_FUNC
1318  static inline bool isApprox(const Scalar& x, const Scalar& y, const RealScalar& prec)
1319  {
1320  return numext::abs(x - y) <= numext::mini(numext::abs(x), numext::abs(y)) * prec;
1321  }
1322  EIGEN_DEVICE_FUNC
1323  static inline bool isApproxOrLessThan(const Scalar& x, const Scalar& y, const RealScalar& prec)
1324  {
1325  return x <= y || isApprox(x, y, prec);
1326  }
1327 };
1328 
1329 template<typename Scalar>
1330 struct scalar_fuzzy_default_impl<Scalar, false, true>
1331 {
1332  typedef typename NumTraits<Scalar>::Real RealScalar;
1333  template<typename OtherScalar> EIGEN_DEVICE_FUNC
1334  static inline bool isMuchSmallerThan(const Scalar& x, const Scalar&, const RealScalar&)
1335  {
1336  return x == Scalar(0);
1337  }
1338  EIGEN_DEVICE_FUNC
1339  static inline bool isApprox(const Scalar& x, const Scalar& y, const RealScalar&)
1340  {
1341  return x == y;
1342  }
1343  EIGEN_DEVICE_FUNC
1344  static inline bool isApproxOrLessThan(const Scalar& x, const Scalar& y, const RealScalar&)
1345  {
1346  return x <= y;
1347  }
1348 };
1349 
1350 template<typename Scalar>
1351 struct scalar_fuzzy_default_impl<Scalar, true, false>
1352 {
1353  typedef typename NumTraits<Scalar>::Real RealScalar;
1354  template<typename OtherScalar> EIGEN_DEVICE_FUNC
1355  static inline bool isMuchSmallerThan(const Scalar& x, const OtherScalar& y, const RealScalar& prec)
1356  {
1357  return numext::abs2(x) <= numext::abs2(y) * prec * prec;
1358  }
1359  EIGEN_DEVICE_FUNC
1360  static inline bool isApprox(const Scalar& x, const Scalar& y, const RealScalar& prec)
1361  {
1362  return numext::abs2(x - y) <= numext::mini(numext::abs2(x), numext::abs2(y)) * prec * prec;
1363  }
1364 };
1365 
1366 template<typename Scalar>
1367 struct scalar_fuzzy_impl : scalar_fuzzy_default_impl<Scalar, NumTraits<Scalar>::IsComplex, NumTraits<Scalar>::IsInteger> {};
1368 
1369 template<typename Scalar, typename OtherScalar> EIGEN_DEVICE_FUNC
1370 inline bool isMuchSmallerThan(const Scalar& x, const OtherScalar& y,
1371  const typename NumTraits<Scalar>::Real &precision = NumTraits<Scalar>::dummy_precision())
1372 {
1373  return scalar_fuzzy_impl<Scalar>::template isMuchSmallerThan<OtherScalar>(x, y, precision);
1374 }
1375 
1376 template<typename Scalar> EIGEN_DEVICE_FUNC
1377 inline bool isApprox(const Scalar& x, const Scalar& y,
1378  const typename NumTraits<Scalar>::Real &precision = NumTraits<Scalar>::dummy_precision())
1379 {
1380  return scalar_fuzzy_impl<Scalar>::isApprox(x, y, precision);
1381 }
1382 
1383 template<typename Scalar> EIGEN_DEVICE_FUNC
1384 inline bool isApproxOrLessThan(const Scalar& x, const Scalar& y,
1385  const typename NumTraits<Scalar>::Real &precision = NumTraits<Scalar>::dummy_precision())
1386 {
1387  return scalar_fuzzy_impl<Scalar>::isApproxOrLessThan(x, y, precision);
1388 }
1389 
1390 /******************************************
1391 *** The special case of the bool type ***
1392 ******************************************/
1393 
1394 template<> struct random_impl<bool>
1395 {
1396  static inline bool run()
1397  {
1398  return random<int>(0,1)==0 ? false : true;
1399  }
1400 };
1401 
1402 template<> struct scalar_fuzzy_impl<bool>
1403 {
1404  typedef bool RealScalar;
1405 
1406  template<typename OtherScalar> EIGEN_DEVICE_FUNC
1407  static inline bool isMuchSmallerThan(const bool& x, const bool&, const bool&)
1408  {
1409  return !x;
1410  }
1411 
1412  EIGEN_DEVICE_FUNC
1413  static inline bool isApprox(bool x, bool y, bool)
1414  {
1415  return x == y;
1416  }
1417 
1418  EIGEN_DEVICE_FUNC
1419  static inline bool isApproxOrLessThan(const bool& x, const bool& y, const bool&)
1420  {
1421  return (!x) || y;
1422  }
1423 
1424 };
1425 
1426 
1427 } // end namespace internal
1428 
1429 } // end namespace Eigen
1430 
1431 #endif // EIGEN_MATHFUNCTIONS_H
Namespace containing all symbols from the Eigen library.
Definition: Core:309
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_tanh_op< typename Derived::Scalar >, const Derived > tanh(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_imag_op< typename Derived::Scalar >, const Derived > imag(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_isinf_op< typename Derived::Scalar >, const Derived > isinf(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_atan_op< typename Derived::Scalar >, const Derived > atan(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_cosh_op< typename Derived::Scalar >, const Derived > cosh(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_tan_op< typename Derived::Scalar >, const Derived > tan(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_acos_op< typename Derived::Scalar >, const Derived > acos(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs2_op< typename Derived::Scalar >, const Derived > abs2(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_asin_op< typename Derived::Scalar >, const Derived > asin(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_isnan_op< typename Derived::Scalar >, const Derived > isnan(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_arg_op< typename Derived::Scalar >, const Derived > arg(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_ceil_op< typename Derived::Scalar >, const Derived > ceil(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_conjugate_op< typename Derived::Scalar >, const Derived > conj(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_isfinite_op< typename Derived::Scalar >, const Derived > isfinite(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_floor_op< typename Derived::Scalar >, const Derived > floor(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_log1p_op< typename Derived::Scalar >, const Derived > log1p(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_real_op< typename Derived::Scalar >, const Derived > real(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_cos_op< typename Derived::Scalar >, const Derived > cos(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_round_op< typename Derived::Scalar >, const Derived > round(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_exp_op< typename Derived::Scalar >, const Derived > exp(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sin_op< typename Derived::Scalar >, const Derived > sin(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_log_op< typename Derived::Scalar >, const Derived > log(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sinh_op< typename Derived::Scalar >, const Derived > sinh(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)