Eigen  3.2.91
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 #define EIGEN_PI 3.141592653589793238462643383279502884197169399375105820974944592307816406
15 
16 namespace Eigen {
17 
18 // On WINCE, std::abs is defined for int only, so let's defined our own overloads:
19 // This issue has been confirmed with MSVC 2008 only, but the issue might exist for more recent versions too.
20 #if EIGEN_OS_WINCE && EIGEN_COMP_MSVC && EIGEN_COMP_MSVC<=1500
21 long abs(long x) { return (labs(x)); }
22 double abs(double x) { return (fabs(x)); }
23 float abs(float x) { return (fabsf(x)); }
24 long double abs(long double x) { return (fabsl(x)); }
25 #endif
26 
27 namespace internal {
28 
49 template<typename T, typename dummy = void>
50 struct global_math_functions_filtering_base
51 {
52  typedef T type;
53 };
54 
55 template<typename T> struct always_void { typedef void type; };
56 
57 template<typename T>
58 struct global_math_functions_filtering_base
59  <T,
60  typename always_void<typename T::Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl>::type
61  >
62 {
63  typedef typename T::Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl type;
64 };
65 
66 #define EIGEN_MATHFUNC_IMPL(func, scalar) Eigen::internal::func##_impl<typename Eigen::internal::global_math_functions_filtering_base<scalar>::type>
67 #define EIGEN_MATHFUNC_RETVAL(func, scalar) typename Eigen::internal::func##_retval<typename Eigen::internal::global_math_functions_filtering_base<scalar>::type>::type
68 
69 /****************************************************************************
70 * Implementation of real *
71 ****************************************************************************/
72 
73 template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
74 struct real_default_impl
75 {
76  typedef typename NumTraits<Scalar>::Real RealScalar;
77  EIGEN_DEVICE_FUNC
78  static inline RealScalar run(const Scalar& x)
79  {
80  return x;
81  }
82 };
83 
84 template<typename Scalar>
85 struct real_default_impl<Scalar,true>
86 {
87  typedef typename NumTraits<Scalar>::Real RealScalar;
88  EIGEN_DEVICE_FUNC
89  static inline RealScalar run(const Scalar& x)
90  {
91  using std::real;
92  return real(x);
93  }
94 };
95 
96 template<typename Scalar> struct real_impl : real_default_impl<Scalar> {};
97 
98 template<typename Scalar>
99 struct real_retval
100 {
101  typedef typename NumTraits<Scalar>::Real type;
102 };
103 
104 /****************************************************************************
105 * Implementation of imag *
106 ****************************************************************************/
107 
108 template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
109 struct imag_default_impl
110 {
111  typedef typename NumTraits<Scalar>::Real RealScalar;
112  EIGEN_DEVICE_FUNC
113  static inline RealScalar run(const Scalar&)
114  {
115  return RealScalar(0);
116  }
117 };
118 
119 template<typename Scalar>
120 struct imag_default_impl<Scalar,true>
121 {
122  typedef typename NumTraits<Scalar>::Real RealScalar;
123  EIGEN_DEVICE_FUNC
124  static inline RealScalar run(const Scalar& x)
125  {
126  using std::imag;
127  return imag(x);
128  }
129 };
130 
131 template<typename Scalar> struct imag_impl : imag_default_impl<Scalar> {};
132 
133 template<typename Scalar>
134 struct imag_retval
135 {
136  typedef typename NumTraits<Scalar>::Real type;
137 };
138 
139 /****************************************************************************
140 * Implementation of real_ref *
141 ****************************************************************************/
142 
143 template<typename Scalar>
144 struct real_ref_impl
145 {
146  typedef typename NumTraits<Scalar>::Real RealScalar;
147  EIGEN_DEVICE_FUNC
148  static inline RealScalar& run(Scalar& x)
149  {
150  return reinterpret_cast<RealScalar*>(&x)[0];
151  }
152  EIGEN_DEVICE_FUNC
153  static inline const RealScalar& run(const Scalar& x)
154  {
155  return reinterpret_cast<const RealScalar*>(&x)[0];
156  }
157 };
158 
159 template<typename Scalar>
160 struct real_ref_retval
161 {
162  typedef typename NumTraits<Scalar>::Real & type;
163 };
164 
165 /****************************************************************************
166 * Implementation of imag_ref *
167 ****************************************************************************/
168 
169 template<typename Scalar, bool IsComplex>
170 struct imag_ref_default_impl
171 {
172  typedef typename NumTraits<Scalar>::Real RealScalar;
173  EIGEN_DEVICE_FUNC
174  static inline RealScalar& run(Scalar& x)
175  {
176  return reinterpret_cast<RealScalar*>(&x)[1];
177  }
178  EIGEN_DEVICE_FUNC
179  static inline const RealScalar& run(const Scalar& x)
180  {
181  return reinterpret_cast<RealScalar*>(&x)[1];
182  }
183 };
184 
185 template<typename Scalar>
186 struct imag_ref_default_impl<Scalar, false>
187 {
188  EIGEN_DEVICE_FUNC
189  static inline Scalar run(Scalar&)
190  {
191  return Scalar(0);
192  }
193  EIGEN_DEVICE_FUNC
194  static inline const Scalar run(const Scalar&)
195  {
196  return Scalar(0);
197  }
198 };
199 
200 template<typename Scalar>
201 struct imag_ref_impl : imag_ref_default_impl<Scalar, NumTraits<Scalar>::IsComplex> {};
202 
203 template<typename Scalar>
204 struct imag_ref_retval
205 {
206  typedef typename NumTraits<Scalar>::Real & type;
207 };
208 
209 /****************************************************************************
210 * Implementation of conj *
211 ****************************************************************************/
212 
213 template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
214 struct conj_impl
215 {
216  EIGEN_DEVICE_FUNC
217  static inline Scalar run(const Scalar& x)
218  {
219  return x;
220  }
221 };
222 
223 template<typename Scalar>
224 struct conj_impl<Scalar,true>
225 {
226  EIGEN_DEVICE_FUNC
227  static inline Scalar run(const Scalar& x)
228  {
229  using std::conj;
230  return conj(x);
231  }
232 };
233 
234 template<typename Scalar>
235 struct conj_retval
236 {
237  typedef Scalar type;
238 };
239 
240 /****************************************************************************
241 * Implementation of abs2 *
242 ****************************************************************************/
243 
244 template<typename Scalar>
245 struct abs2_impl
246 {
247  typedef typename NumTraits<Scalar>::Real RealScalar;
248  EIGEN_DEVICE_FUNC
249  static inline RealScalar run(const Scalar& x)
250  {
251  return x*x;
252  }
253 };
254 
255 template<typename RealScalar>
256 struct abs2_impl<std::complex<RealScalar> >
257 {
258  EIGEN_DEVICE_FUNC
259  static inline RealScalar run(const std::complex<RealScalar>& x)
260  {
261  return real(x)*real(x) + imag(x)*imag(x);
262  }
263 };
264 
265 template<typename Scalar>
266 struct abs2_retval
267 {
268  typedef typename NumTraits<Scalar>::Real type;
269 };
270 
271 /****************************************************************************
272 * Implementation of norm1 *
273 ****************************************************************************/
274 
275 template<typename Scalar, bool IsComplex>
276 struct norm1_default_impl
277 {
278  typedef typename NumTraits<Scalar>::Real RealScalar;
279  EIGEN_DEVICE_FUNC
280  static inline RealScalar run(const Scalar& x)
281  {
282  EIGEN_USING_STD_MATH(abs);
283  return abs(real(x)) + abs(imag(x));
284  }
285 };
286 
287 template<typename Scalar>
288 struct norm1_default_impl<Scalar, false>
289 {
290  EIGEN_DEVICE_FUNC
291  static inline Scalar run(const Scalar& x)
292  {
293  EIGEN_USING_STD_MATH(abs);
294  return abs(x);
295  }
296 };
297 
298 template<typename Scalar>
299 struct norm1_impl : norm1_default_impl<Scalar, NumTraits<Scalar>::IsComplex> {};
300 
301 template<typename Scalar>
302 struct norm1_retval
303 {
304  typedef typename NumTraits<Scalar>::Real type;
305 };
306 
307 /****************************************************************************
308 * Implementation of hypot *
309 ****************************************************************************/
310 
311 template<typename Scalar>
312 struct hypot_impl
313 {
314  typedef typename NumTraits<Scalar>::Real RealScalar;
315  static inline RealScalar run(const Scalar& x, const Scalar& y)
316  {
317  EIGEN_USING_STD_MATH(max);
318  EIGEN_USING_STD_MATH(min);
319  EIGEN_USING_STD_MATH(abs);
320  EIGEN_USING_STD_MATH(sqrt);
321  RealScalar _x = abs(x);
322  RealScalar _y = abs(y);
323  Scalar p, qp;
324  if(_x>_y)
325  {
326  p = _x;
327  qp = _y / p;
328  }
329  else
330  {
331  p = _y;
332  qp = _x / p;
333  }
334  if(p==RealScalar(0)) return RealScalar(0);
335  return p * sqrt(RealScalar(1) + qp*qp);
336  }
337 };
338 
339 template<typename Scalar>
340 struct hypot_retval
341 {
342  typedef typename NumTraits<Scalar>::Real type;
343 };
344 
345 /****************************************************************************
346 * Implementation of cast *
347 ****************************************************************************/
348 
349 template<typename OldType, typename NewType>
350 struct cast_impl
351 {
352  EIGEN_DEVICE_FUNC
353  static inline NewType run(const OldType& x)
354  {
355  return static_cast<NewType>(x);
356  }
357 };
358 
359 // here, for once, we're plainly returning NewType: we don't want cast to do weird things.
360 
361 template<typename OldType, typename NewType>
362 EIGEN_DEVICE_FUNC
363 inline NewType cast(const OldType& x)
364 {
365  return cast_impl<OldType, NewType>::run(x);
366 }
367 
368 /****************************************************************************
369 * Implementation of round *
370 ****************************************************************************/
371 
372 #if EIGEN_HAS_CXX11_MATH
373  template<typename Scalar>
374  struct round_impl {
375  static inline Scalar run(const Scalar& x)
376  {
377  EIGEN_STATIC_ASSERT((!NumTraits<Scalar>::IsComplex), NUMERIC_TYPE_MUST_BE_REAL)
378  using std::round;
379  return round(x);
380  }
381  };
382 #else
383  template<typename Scalar>
384  struct round_impl
385  {
386  static inline Scalar run(const Scalar& x)
387  {
388  EIGEN_STATIC_ASSERT((!NumTraits<Scalar>::IsComplex), NUMERIC_TYPE_MUST_BE_REAL)
389  EIGEN_USING_STD_MATH(floor);
390  EIGEN_USING_STD_MATH(ceil);
391  return (x > Scalar(0)) ? floor(x + Scalar(0.5)) : ceil(x - Scalar(0.5));
392  }
393  };
394 #endif
395 
396 template<typename Scalar>
397 struct round_retval
398 {
399  typedef Scalar type;
400 };
401 
402 /****************************************************************************
403 * Implementation of arg *
404 ****************************************************************************/
405 
406 #if EIGEN_HAS_CXX11_MATH
407  template<typename Scalar>
408  struct arg_impl {
409  static inline Scalar run(const Scalar& x)
410  {
411  EIGEN_USING_STD_MATH(arg);
412  return arg(x);
413  }
414  };
415 #else
416  template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
417  struct arg_default_impl
418  {
419  typedef typename NumTraits<Scalar>::Real RealScalar;
420  EIGEN_DEVICE_FUNC
421  static inline RealScalar run(const Scalar& x)
422  {
423  return (x < Scalar(0)) ? Scalar(EIGEN_PI) : Scalar(0); }
424  };
425 
426  template<typename Scalar>
427  struct arg_default_impl<Scalar,true>
428  {
429  typedef typename NumTraits<Scalar>::Real RealScalar;
430  EIGEN_DEVICE_FUNC
431  static inline RealScalar run(const Scalar& x)
432  {
433  EIGEN_USING_STD_MATH(arg);
434  return arg(x);
435  }
436  };
437 
438  template<typename Scalar> struct arg_impl : arg_default_impl<Scalar> {};
439 #endif
440 
441 template<typename Scalar>
442 struct arg_retval
443 {
444  typedef typename NumTraits<Scalar>::Real type;
445 };
446 
447 /****************************************************************************
448 * Implementation of log1p *
449 ****************************************************************************/
450 template<typename Scalar, bool isComplex = NumTraits<Scalar>::IsComplex >
451 struct log1p_impl
452 {
453  static inline Scalar run(const Scalar& x)
454  {
455  EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
456  typedef typename NumTraits<Scalar>::Real RealScalar;
457  EIGEN_USING_STD_MATH(log);
458  Scalar x1p = RealScalar(1) + x;
459  return ( x1p == Scalar(1) ) ? x : x * ( log(x1p) / (x1p - RealScalar(1)) );
460  }
461 };
462 
463 #if EIGEN_HAS_CXX11_MATH
464 template<typename Scalar>
465 struct log1p_impl<Scalar, false> {
466  static inline Scalar run(const Scalar& x)
467  {
468  EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
469  using std::log1p;
470  return log1p(x);
471  }
472 };
473 #endif
474 
475 template<typename Scalar>
476 struct log1p_retval
477 {
478  typedef Scalar type;
479 };
480 
481 /****************************************************************************
482 * Implementation of pow *
483 ****************************************************************************/
484 
485 template<typename Scalar, bool IsInteger>
486 struct pow_default_impl
487 {
488  typedef Scalar retval;
489  static inline Scalar run(const Scalar& x, const Scalar& y)
490  {
491  EIGEN_USING_STD_MATH(pow);
492  return pow(x, y);
493  }
494 };
495 
496 template<typename Scalar>
497 struct pow_default_impl<Scalar, true>
498 {
499  static inline Scalar run(Scalar x, Scalar y)
500  {
501  Scalar res(1);
502  eigen_assert(!NumTraits<Scalar>::IsSigned || y >= 0);
503  if(y & 1) res *= x;
504  y >>= 1;
505  while(y)
506  {
507  x *= x;
508  if(y&1) res *= x;
509  y >>= 1;
510  }
511  return res;
512  }
513 };
514 
515 template<typename Scalar>
516 struct pow_impl : pow_default_impl<Scalar, NumTraits<Scalar>::IsInteger> {};
517 
518 template<typename Scalar>
519 struct pow_retval
520 {
521  typedef Scalar type;
522 };
523 
524 /****************************************************************************
525 * Implementation of random *
526 ****************************************************************************/
527 
528 template<typename Scalar,
529  bool IsComplex,
530  bool IsInteger>
531 struct random_default_impl {};
532 
533 template<typename Scalar>
534 struct random_impl : random_default_impl<Scalar, NumTraits<Scalar>::IsComplex, NumTraits<Scalar>::IsInteger> {};
535 
536 template<typename Scalar>
537 struct random_retval
538 {
539  typedef Scalar type;
540 };
541 
542 template<typename Scalar> inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random(const Scalar& x, const Scalar& y);
543 template<typename Scalar> inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random();
544 
545 template<typename Scalar>
546 struct random_default_impl<Scalar, false, false>
547 {
548  static inline Scalar run(const Scalar& x, const Scalar& y)
549  {
550  return x + (y-x) * Scalar(std::rand()) / Scalar(RAND_MAX);
551  }
552  static inline Scalar run()
553  {
554  return run(Scalar(NumTraits<Scalar>::IsSigned ? -1 : 0), Scalar(1));
555  }
556 };
557 
558 enum {
559  meta_floor_log2_terminate,
560  meta_floor_log2_move_up,
561  meta_floor_log2_move_down,
562  meta_floor_log2_bogus
563 };
564 
565 template<unsigned int n, int lower, int upper> struct meta_floor_log2_selector
566 {
567  enum { middle = (lower + upper) / 2,
568  value = (upper <= lower + 1) ? int(meta_floor_log2_terminate)
569  : (n < (1 << middle)) ? int(meta_floor_log2_move_down)
570  : (n==0) ? int(meta_floor_log2_bogus)
571  : int(meta_floor_log2_move_up)
572  };
573 };
574 
575 template<unsigned int n,
576  int lower = 0,
577  int upper = sizeof(unsigned int) * CHAR_BIT - 1,
578  int selector = meta_floor_log2_selector<n, lower, upper>::value>
579 struct meta_floor_log2 {};
580 
581 template<unsigned int n, int lower, int upper>
582 struct meta_floor_log2<n, lower, upper, meta_floor_log2_move_down>
583 {
584  enum { value = meta_floor_log2<n, lower, meta_floor_log2_selector<n, lower, upper>::middle>::value };
585 };
586 
587 template<unsigned int n, int lower, int upper>
588 struct meta_floor_log2<n, lower, upper, meta_floor_log2_move_up>
589 {
590  enum { value = meta_floor_log2<n, meta_floor_log2_selector<n, lower, upper>::middle, upper>::value };
591 };
592 
593 template<unsigned int n, int lower, int upper>
594 struct meta_floor_log2<n, lower, upper, meta_floor_log2_terminate>
595 {
596  enum { value = (n >= ((unsigned int)(1) << (lower+1))) ? lower+1 : lower };
597 };
598 
599 template<unsigned int n, int lower, int upper>
600 struct meta_floor_log2<n, lower, upper, meta_floor_log2_bogus>
601 {
602  // no value, error at compile time
603 };
604 
605 template<typename Scalar>
606 struct random_default_impl<Scalar, false, true>
607 {
608  static inline Scalar run(const Scalar& x, const Scalar& y)
609  {
610  using std::max;
611  using std::min;
612  typedef typename conditional<NumTraits<Scalar>::IsSigned,std::ptrdiff_t,std::size_t>::type ScalarX;
613  if(y<x)
614  return x;
615  std::size_t range = ScalarX(y)-ScalarX(x);
616  std::size_t offset = 0;
617  // rejection sampling
618  std::size_t divisor = (range+RAND_MAX-1)/(range+1);
619  std::size_t multiplier = (range+RAND_MAX-1)/std::size_t(RAND_MAX);
620 
621  do {
622  offset = ( (std::size_t(std::rand()) * multiplier) / divisor );
623  } while (offset > range);
624 
625  return Scalar(ScalarX(x) + offset);
626  }
627 
628  static inline Scalar run()
629  {
630 #ifdef EIGEN_MAKING_DOCS
631  return run(Scalar(NumTraits<Scalar>::IsSigned ? -10 : 0), Scalar(10));
632 #else
633  enum { rand_bits = meta_floor_log2<(unsigned int)(RAND_MAX)+1>::value,
634  scalar_bits = sizeof(Scalar) * CHAR_BIT,
635  shift = EIGEN_PLAIN_ENUM_MAX(0, int(rand_bits) - int(scalar_bits)),
636  offset = NumTraits<Scalar>::IsSigned ? (1 << (EIGEN_PLAIN_ENUM_MIN(rand_bits,scalar_bits)-1)) : 0
637  };
638  return Scalar((std::rand() >> shift) - offset);
639 #endif
640  }
641 };
642 
643 template<typename Scalar>
644 struct random_default_impl<Scalar, true, false>
645 {
646  static inline Scalar run(const Scalar& x, const Scalar& y)
647  {
648  return Scalar(random(real(x), real(y)),
649  random(imag(x), imag(y)));
650  }
651  static inline Scalar run()
652  {
653  typedef typename NumTraits<Scalar>::Real RealScalar;
654  return Scalar(random<RealScalar>(), random<RealScalar>());
655  }
656 };
657 
658 template<typename Scalar>
659 inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random(const Scalar& x, const Scalar& y)
660 {
661  return EIGEN_MATHFUNC_IMPL(random, Scalar)::run(x, y);
662 }
663 
664 template<typename Scalar>
665 inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random()
666 {
667  return EIGEN_MATHFUNC_IMPL(random, Scalar)::run();
668 }
669 
670 } // end namespace internal
671 
672 /****************************************************************************
673 * Generic math functions *
674 ****************************************************************************/
675 
676 namespace numext {
677 
678 #ifndef __CUDA_ARCH__
679 template<typename T>
680 EIGEN_DEVICE_FUNC
681 EIGEN_ALWAYS_INLINE T mini(const T& x, const T& y)
682 {
683  EIGEN_USING_STD_MATH(min);
684  return min EIGEN_NOT_A_MACRO (x,y);
685 }
686 
687 template<typename T>
688 EIGEN_DEVICE_FUNC
689 EIGEN_ALWAYS_INLINE T maxi(const T& x, const T& y)
690 {
691  EIGEN_USING_STD_MATH(max);
692  return max EIGEN_NOT_A_MACRO (x,y);
693 }
694 #else
695 template<typename T>
696 EIGEN_DEVICE_FUNC
697 EIGEN_ALWAYS_INLINE T mini(const T& x, const T& y)
698 {
699  return y < x ? y : x;
700 }
701 template<>
702 EIGEN_DEVICE_FUNC
703 EIGEN_ALWAYS_INLINE float mini(const float& x, const float& y)
704 {
705  return fmin(x, y);
706 }
707 template<typename T>
708 EIGEN_DEVICE_FUNC
709 EIGEN_ALWAYS_INLINE T maxi(const T& x, const T& y)
710 {
711  return x < y ? y : x;
712 }
713 template<>
714 EIGEN_DEVICE_FUNC
715 EIGEN_ALWAYS_INLINE float maxi(const float& x, const float& y)
716 {
717  return fmax(x, y);
718 }
719 #endif
720 
721 
722 template<typename Scalar>
723 EIGEN_DEVICE_FUNC
724 inline EIGEN_MATHFUNC_RETVAL(real, Scalar) real(const Scalar& x)
725 {
726  return EIGEN_MATHFUNC_IMPL(real, Scalar)::run(x);
727 }
728 
729 template<typename Scalar>
730 EIGEN_DEVICE_FUNC
731 inline typename internal::add_const_on_value_type< EIGEN_MATHFUNC_RETVAL(real_ref, Scalar) >::type real_ref(const Scalar& x)
732 {
733  return internal::real_ref_impl<Scalar>::run(x);
734 }
735 
736 template<typename Scalar>
737 EIGEN_DEVICE_FUNC
738 inline EIGEN_MATHFUNC_RETVAL(real_ref, Scalar) real_ref(Scalar& x)
739 {
740  return EIGEN_MATHFUNC_IMPL(real_ref, Scalar)::run(x);
741 }
742 
743 template<typename Scalar>
744 EIGEN_DEVICE_FUNC
745 inline EIGEN_MATHFUNC_RETVAL(imag, Scalar) imag(const Scalar& x)
746 {
747  return EIGEN_MATHFUNC_IMPL(imag, Scalar)::run(x);
748 }
749 
750 template<typename Scalar>
751 EIGEN_DEVICE_FUNC
752 inline EIGEN_MATHFUNC_RETVAL(arg, Scalar) arg(const Scalar& x)
753 {
754  return EIGEN_MATHFUNC_IMPL(arg, Scalar)::run(x);
755 }
756 
757 template<typename Scalar>
758 EIGEN_DEVICE_FUNC
759 inline typename internal::add_const_on_value_type< EIGEN_MATHFUNC_RETVAL(imag_ref, Scalar) >::type imag_ref(const Scalar& x)
760 {
761  return internal::imag_ref_impl<Scalar>::run(x);
762 }
763 
764 template<typename Scalar>
765 EIGEN_DEVICE_FUNC
766 inline EIGEN_MATHFUNC_RETVAL(imag_ref, Scalar) imag_ref(Scalar& x)
767 {
768  return EIGEN_MATHFUNC_IMPL(imag_ref, Scalar)::run(x);
769 }
770 
771 template<typename Scalar>
772 EIGEN_DEVICE_FUNC
773 inline EIGEN_MATHFUNC_RETVAL(conj, Scalar) conj(const Scalar& x)
774 {
775  return EIGEN_MATHFUNC_IMPL(conj, Scalar)::run(x);
776 }
777 
778 template<typename Scalar>
779 EIGEN_DEVICE_FUNC
780 inline EIGEN_MATHFUNC_RETVAL(abs2, Scalar) abs2(const Scalar& x)
781 {
782  return EIGEN_MATHFUNC_IMPL(abs2, Scalar)::run(x);
783 }
784 
785 template<typename Scalar>
786 EIGEN_DEVICE_FUNC
787 inline EIGEN_MATHFUNC_RETVAL(norm1, Scalar) norm1(const Scalar& x)
788 {
789  return EIGEN_MATHFUNC_IMPL(norm1, Scalar)::run(x);
790 }
791 
792 template<typename Scalar>
793 EIGEN_DEVICE_FUNC
794 inline EIGEN_MATHFUNC_RETVAL(hypot, Scalar) hypot(const Scalar& x, const Scalar& y)
795 {
796  return EIGEN_MATHFUNC_IMPL(hypot, Scalar)::run(x, y);
797 }
798 
799 template<typename Scalar>
800 EIGEN_DEVICE_FUNC
801 inline EIGEN_MATHFUNC_RETVAL(log1p, Scalar) log1p(const Scalar& x)
802 {
803  return EIGEN_MATHFUNC_IMPL(log1p, Scalar)::run(x);
804 }
805 
806 template<typename Scalar>
807 EIGEN_DEVICE_FUNC
808 inline EIGEN_MATHFUNC_RETVAL(pow, Scalar) pow(const Scalar& x, const Scalar& y)
809 {
810  return EIGEN_MATHFUNC_IMPL(pow, Scalar)::run(x, y);
811 }
812 
813 template<typename T>
814 EIGEN_DEVICE_FUNC
815 bool (isfinite)(const T& x)
816 {
817  #if EIGEN_HAS_CXX11_MATH
818  using std::isfinite;
819  return isfinite EIGEN_NOT_A_MACRO (x);
820  #else
821  return x<NumTraits<T>::highest() && x>NumTraits<T>::lowest();
822  #endif
823 }
824 
825 template<typename T>
826 EIGEN_DEVICE_FUNC
827 bool (isnan)(const T& x)
828 {
829  #if EIGEN_HAS_CXX11_MATH
830  using std::isnan;
831  return isnan EIGEN_NOT_A_MACRO (x);
832  #else
833  return x != x;
834  #endif
835 }
836 
837 template<typename T>
838 EIGEN_DEVICE_FUNC
839 bool (isinf)(const T& x)
840 {
841  #if EIGEN_HAS_CXX11_MATH
842  using std::isinf;
843  return isinf EIGEN_NOT_A_MACRO (x);
844  #else
845  return x>NumTraits<T>::highest() || x<NumTraits<T>::lowest();
846  #endif
847 }
848 
849 template<typename T>
850 bool (isfinite)(const std::complex<T>& x)
851 {
852  return (numext::isfinite)(numext::real(x)) && (numext::isfinite)(numext::imag(x));
853 }
854 
855 template<typename T>
856 bool (isnan)(const std::complex<T>& x)
857 {
858  return (numext::isnan)(numext::real(x)) || (numext::isnan)(numext::imag(x));
859 }
860 
861 template<typename T>
862 bool (isinf)(const std::complex<T>& x)
863 {
864  return ((numext::isinf)(numext::real(x)) || (numext::isinf)(numext::imag(x))) && (!(numext::isnan)(x));
865 }
866 
867 template<typename Scalar>
868 EIGEN_DEVICE_FUNC
869 inline EIGEN_MATHFUNC_RETVAL(round, Scalar) round(const Scalar& x)
870 {
871  return EIGEN_MATHFUNC_IMPL(round, Scalar)::run(x);
872 }
873 
874 template<typename T>
875 EIGEN_DEVICE_FUNC
876 T (floor)(const T& x)
877 {
878  EIGEN_USING_STD_MATH(floor);
879  return floor(x);
880 }
881 
882 template<typename T>
883 EIGEN_DEVICE_FUNC
884 T (ceil)(const T& x)
885 {
886  EIGEN_USING_STD_MATH(ceil);
887  return ceil(x);
888 }
889 
890 // Log base 2 for 32 bits positive integers.
891 // Conveniently returns 0 for x==0.
892 inline int log2(int x)
893 {
894  eigen_assert(x>=0);
895  unsigned int v(x);
896  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 };
897  v |= v >> 1;
898  v |= v >> 2;
899  v |= v >> 4;
900  v |= v >> 8;
901  v |= v >> 16;
902  return table[(v * 0x07C4ACDDU) >> 27];
903 }
904 
905 } // end namespace numext
906 
907 namespace internal {
908 
909 /****************************************************************************
910 * Implementation of fuzzy comparisons *
911 ****************************************************************************/
912 
913 template<typename Scalar,
914  bool IsComplex,
915  bool IsInteger>
916 struct scalar_fuzzy_default_impl {};
917 
918 template<typename Scalar>
919 struct scalar_fuzzy_default_impl<Scalar, false, false>
920 {
921  typedef typename NumTraits<Scalar>::Real RealScalar;
922  template<typename OtherScalar> EIGEN_DEVICE_FUNC
923  static inline bool isMuchSmallerThan(const Scalar& x, const OtherScalar& y, const RealScalar& prec)
924  {
925  EIGEN_USING_STD_MATH(abs);
926  return abs(x) <= abs(y) * prec;
927  }
928  EIGEN_DEVICE_FUNC
929  static inline bool isApprox(const Scalar& x, const Scalar& y, const RealScalar& prec)
930  {
931  EIGEN_USING_STD_MATH(min);
932  EIGEN_USING_STD_MATH(abs);
933  return abs(x - y) <= (min)(abs(x), abs(y)) * prec;
934  }
935  EIGEN_DEVICE_FUNC
936  static inline bool isApproxOrLessThan(const Scalar& x, const Scalar& y, const RealScalar& prec)
937  {
938  return x <= y || isApprox(x, y, prec);
939  }
940 };
941 
942 template<typename Scalar>
943 struct scalar_fuzzy_default_impl<Scalar, false, true>
944 {
945  typedef typename NumTraits<Scalar>::Real RealScalar;
946  template<typename OtherScalar> EIGEN_DEVICE_FUNC
947  static inline bool isMuchSmallerThan(const Scalar& x, const Scalar&, const RealScalar&)
948  {
949  return x == Scalar(0);
950  }
951  EIGEN_DEVICE_FUNC
952  static inline bool isApprox(const Scalar& x, const Scalar& y, const RealScalar&)
953  {
954  return x == y;
955  }
956  EIGEN_DEVICE_FUNC
957  static inline bool isApproxOrLessThan(const Scalar& x, const Scalar& y, const RealScalar&)
958  {
959  return x <= y;
960  }
961 };
962 
963 template<typename Scalar>
964 struct scalar_fuzzy_default_impl<Scalar, true, false>
965 {
966  typedef typename NumTraits<Scalar>::Real RealScalar;
967  template<typename OtherScalar>
968  static inline bool isMuchSmallerThan(const Scalar& x, const OtherScalar& y, const RealScalar& prec)
969  {
970  return numext::abs2(x) <= numext::abs2(y) * prec * prec;
971  }
972  static inline bool isApprox(const Scalar& x, const Scalar& y, const RealScalar& prec)
973  {
974  EIGEN_USING_STD_MATH(min);
975  return numext::abs2(x - y) <= (min)(numext::abs2(x), numext::abs2(y)) * prec * prec;
976  }
977 };
978 
979 template<typename Scalar>
980 struct scalar_fuzzy_impl : scalar_fuzzy_default_impl<Scalar, NumTraits<Scalar>::IsComplex, NumTraits<Scalar>::IsInteger> {};
981 
982 template<typename Scalar, typename OtherScalar> EIGEN_DEVICE_FUNC
983 inline bool isMuchSmallerThan(const Scalar& x, const OtherScalar& y,
984  typename NumTraits<Scalar>::Real precision = NumTraits<Scalar>::dummy_precision())
985 {
986  return scalar_fuzzy_impl<Scalar>::template isMuchSmallerThan<OtherScalar>(x, y, precision);
987 }
988 
989 template<typename Scalar> EIGEN_DEVICE_FUNC
990 inline bool isApprox(const Scalar& x, const Scalar& y,
991  typename NumTraits<Scalar>::Real precision = NumTraits<Scalar>::dummy_precision())
992 {
993  return scalar_fuzzy_impl<Scalar>::isApprox(x, y, precision);
994 }
995 
996 template<typename Scalar> EIGEN_DEVICE_FUNC
997 inline bool isApproxOrLessThan(const Scalar& x, const Scalar& y,
998  typename NumTraits<Scalar>::Real precision = NumTraits<Scalar>::dummy_precision())
999 {
1000  return scalar_fuzzy_impl<Scalar>::isApproxOrLessThan(x, y, precision);
1001 }
1002 
1003 /******************************************
1004 *** The special case of the bool type ***
1005 ******************************************/
1006 
1007 template<> struct random_impl<bool>
1008 {
1009  static inline bool run()
1010  {
1011  return random<int>(0,1)==0 ? false : true;
1012  }
1013 };
1014 
1015 template<> struct scalar_fuzzy_impl<bool>
1016 {
1017  typedef bool RealScalar;
1018 
1019  template<typename OtherScalar> EIGEN_DEVICE_FUNC
1020  static inline bool isMuchSmallerThan(const bool& x, const bool&, const bool&)
1021  {
1022  return !x;
1023  }
1024 
1025  EIGEN_DEVICE_FUNC
1026  static inline bool isApprox(bool x, bool y, bool)
1027  {
1028  return x == y;
1029  }
1030 
1031  EIGEN_DEVICE_FUNC
1032  static inline bool isApproxOrLessThan(const bool& x, const bool& y, const bool&)
1033  {
1034  return (!x) || y;
1035  }
1036 
1037 };
1038 
1039 
1040 } // end namespace internal
1041 
1042 } // end namespace Eigen
1043 
1044 #endif // EIGEN_MATHFUNCTIONS_H
Definition: LDLT.h:16
Definition: StdDeque.h:58
Definition: Eigen_Colamd.h:54