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