10#ifndef EIGEN_MATHFUNCTIONS_H
11#define EIGEN_MATHFUNCTIONS_H
14#define EIGEN_PI 3.141592653589793238462643383279502884197169399375105820974944592307816406
20#if EIGEN_OS_WINCE && EIGEN_COMP_MSVC && EIGEN_COMP_MSVC<=1500
21long abs(
long x) {
return (labs(x)); }
22double abs(
double x) {
return (fabs(x)); }
23float abs(
float x) {
return (fabsf(x)); }
24long double abs(
long double x) {
return (fabsl(x)); }
49template<
typename T,
typename dummy =
void>
63 typedef typename T::Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl type;
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
76 typedef typename NumTraits<Scalar>::Real
RealScalar;
84template<
typename Scalar>
87 typedef typename NumTraits<Scalar>::Real
RealScalar;
98template<
typename Scalar>
101 typedef typename NumTraits<Scalar>::Real
type;
111 typedef typename NumTraits<Scalar>::Real
RealScalar;
119template<
typename Scalar>
122 typedef typename NumTraits<Scalar>::Real
RealScalar;
133template<
typename Scalar>
136 typedef typename NumTraits<Scalar>::Real
type;
143template<
typename Scalar>
146 typedef typename NumTraits<Scalar>::Real
RealScalar;
153 static inline const RealScalar& run(
const Scalar& x)
155 return reinterpret_cast<const RealScalar*
>(&x)[0];
159template<
typename Scalar>
162 typedef typename NumTraits<Scalar>::Real &
type;
169template<
typename Scalar,
bool IsComplex>
172 typedef typename NumTraits<Scalar>::Real
RealScalar;
179 static inline const RealScalar& run(
const Scalar& x)
185template<
typename Scalar>
189 static inline Scalar run(Scalar&)
194 static inline const Scalar run(
const Scalar&)
200template<
typename Scalar>
203template<
typename Scalar>
206 typedef typename NumTraits<Scalar>::Real &
type;
217 static inline Scalar run(
const Scalar& x)
223template<
typename Scalar>
227 static inline Scalar run(
const Scalar& x)
234template<
typename Scalar>
244template<
typename Scalar,
bool IsComplex>
247 typedef typename NumTraits<Scalar>::Real
RealScalar;
255template<
typename Scalar>
258 typedef typename NumTraits<Scalar>::Real
RealScalar;
262 return real(x)*real(x) + imag(x)*imag(x);
266template<
typename Scalar>
269 typedef typename NumTraits<Scalar>::Real
RealScalar;
277template<
typename Scalar>
280 typedef typename NumTraits<Scalar>::Real
type;
287template<
typename Scalar,
bool IsComplex>
290 typedef typename NumTraits<Scalar>::Real
RealScalar;
295 return abs(real(x)) + abs(imag(x));
299template<
typename Scalar>
303 static inline Scalar run(
const Scalar& x)
310template<
typename Scalar>
313template<
typename Scalar>
316 typedef typename NumTraits<Scalar>::Real
type;
323template<
typename Scalar>
326 typedef typename NumTraits<Scalar>::Real
RealScalar;
327 static inline RealScalar run(
const Scalar& x,
const Scalar& y)
349template<
typename Scalar>
352 typedef typename NumTraits<Scalar>::Real
type;
359template<
typename OldType,
typename NewType>
365 return static_cast<NewType>(x);
371template<
typename OldType,
typename NewType>
382#if EIGEN_HAS_CXX11_MATH
383 template<
typename Scalar>
385 static inline Scalar run(
const Scalar& x)
393 template<
typename Scalar>
396 static inline Scalar run(
const Scalar& x)
401 return (x > Scalar(0)) ? floor(x + Scalar(0.5)) : ceil(x - Scalar(0.5));
406template<
typename Scalar>
416#if EIGEN_HAS_CXX11_MATH
417 template<
typename Scalar>
419 static inline Scalar run(
const Scalar& x)
426 template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
429 typedef typename NumTraits<Scalar>::Real
RealScalar;
433 return (x < Scalar(0)) ? Scalar(EIGEN_PI) : Scalar(0); }
436 template<
typename Scalar>
439 typedef typename NumTraits<Scalar>::Real
RealScalar;
451template<
typename Scalar>
454 typedef typename NumTraits<Scalar>::Real
type;
463 static inline Scalar run(
const Scalar& x)
465 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
466 typedef typename NumTraits<Scalar>::Real RealScalar;
468 Scalar
x1p = RealScalar(1) + x;
469 return (
x1p == Scalar(1) ) ? x : x * ( log(
x1p) / (
x1p - RealScalar(1)) );
473#if EIGEN_HAS_CXX11_MATH
474template<
typename Scalar>
476 static inline Scalar run(
const Scalar& x)
478 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
485template<
typename Scalar>
495template<
typename Scalar,
bool IsInteger>
498 typedef Scalar retval;
499 static inline Scalar run(
const Scalar& x,
const Scalar& y)
506template<
typename Scalar>
509 static inline Scalar run(Scalar x, Scalar y)
525template<
typename Scalar>
528template<
typename Scalar>
538template<
typename Scalar,
543template<
typename Scalar>
546template<
typename Scalar>
552template<
typename Scalar>
inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random(
const Scalar& x,
const Scalar& y);
553template<
typename Scalar>
inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random();
555template<
typename Scalar>
558 static inline Scalar run(
const Scalar& x,
const Scalar& y)
560 return x + (y-x) * Scalar(std::rand()) / Scalar(
RAND_MAX);
562 static inline Scalar run()
569 meta_floor_log2_terminate,
570 meta_floor_log2_move_up,
571 meta_floor_log2_move_down,
572 meta_floor_log2_bogus
577 enum { middle = (lower + upper) / 2,
578 value = (upper <= lower + 1) ? int(meta_floor_log2_terminate)
579 : (n < (1 << middle)) ? int(meta_floor_log2_move_down)
580 : (n==0) ?
int(meta_floor_log2_bogus)
581 : int(meta_floor_log2_move_up)
585template<
unsigned int n,
587 int upper =
sizeof(
unsigned int) *
CHAR_BIT - 1,
591template<
unsigned int n,
int lower,
int upper>
597template<
unsigned int n,
int lower,
int upper>
603template<
unsigned int n,
int lower,
int upper>
606 enum { value = (n >= ((
unsigned int)(1) << (lower+1))) ? lower+1 : lower };
609template<
unsigned int n,
int lower,
int upper>
615template<
typename Scalar>
618 static inline Scalar run(
const Scalar& x,
const Scalar& y)
624 std::size_t offset = 0;
631 }
while (offset > range);
633 return Scalar(
ScalarX(x) + offset);
636 static inline Scalar run()
638#ifdef EIGEN_MAKING_DOCS
646 return Scalar((std::rand() >> shift) - offset);
651template<
typename Scalar>
654 static inline Scalar run(
const Scalar& x,
const Scalar& y)
656 return Scalar(random(real(x), real(y)),
657 random(imag(x), imag(y)));
659 static inline Scalar run()
661 typedef typename NumTraits<Scalar>::Real RealScalar;
666template<
typename Scalar>
667inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random(
const Scalar& x,
const Scalar& y)
669 return EIGEN_MATHFUNC_IMPL(random, Scalar)::run(x, y);
672template<
typename Scalar>
673inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random()
675 return EIGEN_MATHFUNC_IMPL(random, Scalar)::run();
681#if (EIGEN_HAS_CXX11_MATH && !(EIGEN_COMP_GNUC_STRICT && __FINITE_MATH_ONLY__)) || (EIGEN_COMP_MSVC>=1800) || (EIGEN_COMP_CLANG)
682#define EIGEN_USE_STD_FPCLASSIFY 1
684#define EIGEN_USE_STD_FPCLASSIFY 0
689typename internal::enable_if<internal::is_integral<T>::value,
bool>::type
690isnan_impl(
const T&) {
return false; }
694typename internal::enable_if<internal::is_integral<T>::value,
bool>::type
695isinf_impl(
const T&) {
return false; }
699typename internal::enable_if<internal::is_integral<T>::value,
bool>::type
700isfinite_impl(
const T&) {
return true; }
704typename internal::enable_if<(!internal::is_integral<T>::value)&&(!NumTraits<T>::IsComplex),
bool>::type
705isfinite_impl(
const T& x)
707 #if EIGEN_USE_STD_FPCLASSIFY
709 return isfinite EIGEN_NOT_A_MACRO (x);
711 return x<NumTraits<T>::highest() && x>NumTraits<T>::lowest();
717typename internal::enable_if<(!internal::is_integral<T>::value)&&(!NumTraits<T>::IsComplex),
bool>::type
718isinf_impl(
const T& x)
720 #if EIGEN_USE_STD_FPCLASSIFY
722 return isinf EIGEN_NOT_A_MACRO (x);
724 return x>NumTraits<T>::highest() || x<NumTraits<T>::lowest();
730typename internal::enable_if<(!internal::is_integral<T>::value)&&(!NumTraits<T>::IsComplex),
bool>::type
731isnan_impl(
const T& x)
733 #if EIGEN_USE_STD_FPCLASSIFY
735 return isnan EIGEN_NOT_A_MACRO (x);
741#if (!EIGEN_USE_STD_FPCLASSIFY)
745template<
typename T> EIGEN_DEVICE_FUNC
bool isinf_msvc_helper(T x)
747 return _fpclass(x)==_FPCLASS_NINF || _fpclass(x)==_FPCLASS_PINF;
751EIGEN_DEVICE_FUNC
inline bool isnan_impl(
const long double& x) {
return _isnan(x); }
752EIGEN_DEVICE_FUNC
inline bool isnan_impl(
const double& x) {
return _isnan(x); }
753EIGEN_DEVICE_FUNC
inline bool isnan_impl(
const float& x) {
return _isnan(x); }
755EIGEN_DEVICE_FUNC
inline bool isinf_impl(
const long double& x) {
return isinf_msvc_helper(x); }
756EIGEN_DEVICE_FUNC
inline bool isinf_impl(
const double& x) {
return isinf_msvc_helper(x); }
757EIGEN_DEVICE_FUNC
inline bool isinf_impl(
const float& x) {
return isinf_msvc_helper(x); }
759#elif (defined __FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ && EIGEN_COMP_GNUC)
761#if EIGEN_GNUC_AT_LEAST(5,0)
762 #define EIGEN_TMP_NOOPT_ATTRIB EIGEN_DEVICE_FUNC inline __attribute__((optimize("no-finite-math-only")))
766 #define EIGEN_TMP_NOOPT_ATTRIB EIGEN_DEVICE_FUNC inline __attribute__((noinline,optimize("no-finite-math-only")))
769template<> EIGEN_TMP_NOOPT_ATTRIB
bool isnan_impl(
const long double& x) {
return __builtin_isnan(x); }
770template<> EIGEN_TMP_NOOPT_ATTRIB
bool isnan_impl(
const double& x) {
return __builtin_isnan(x); }
771template<> EIGEN_TMP_NOOPT_ATTRIB
bool isnan_impl(
const float& x) {
return __builtin_isnan(x); }
772template<> EIGEN_TMP_NOOPT_ATTRIB
bool isinf_impl(
const double& x) {
return __builtin_isinf(x); }
773template<> EIGEN_TMP_NOOPT_ATTRIB
bool isinf_impl(
const float& x) {
return __builtin_isinf(x); }
774template<> EIGEN_TMP_NOOPT_ATTRIB
bool isinf_impl(
const long double& x) {
return __builtin_isinf(x); }
776#undef EIGEN_TMP_NOOPT_ATTRIB
783template<
typename T>
bool isfinite_impl(
const std::complex<T>& x);
784template<
typename T>
bool isnan_impl(
const std::complex<T>& x);
785template<
typename T>
bool isinf_impl(
const std::complex<T>& x);
798EIGEN_ALWAYS_INLINE T mini(
const T& x,
const T& y)
800 EIGEN_USING_STD_MATH(min);
801 return min EIGEN_NOT_A_MACRO (x,y);
806EIGEN_ALWAYS_INLINE T maxi(
const T& x,
const T& y)
808 EIGEN_USING_STD_MATH(max);
809 return max EIGEN_NOT_A_MACRO (x,y);
814EIGEN_ALWAYS_INLINE T mini(
const T& x,
const T& y)
816 return y < x ? y : x;
820EIGEN_ALWAYS_INLINE
float mini(
const float& x,
const float& y)
826EIGEN_ALWAYS_INLINE T maxi(
const T& x,
const T& y)
828 return x < y ? y : x;
832EIGEN_ALWAYS_INLINE
float maxi(
const float& x,
const float& y)
839template<
typename Scalar>
841inline EIGEN_MATHFUNC_RETVAL(real, Scalar) real(
const Scalar& x)
843 return EIGEN_MATHFUNC_IMPL(real, Scalar)::run(x);
846template<
typename Scalar>
848inline typename internal::add_const_on_value_type< EIGEN_MATHFUNC_RETVAL(real_ref, Scalar) >::type real_ref(
const Scalar& x)
850 return internal::real_ref_impl<Scalar>::run(x);
853template<
typename Scalar>
855inline EIGEN_MATHFUNC_RETVAL(real_ref, Scalar) real_ref(Scalar& x)
857 return EIGEN_MATHFUNC_IMPL(real_ref, Scalar)::run(x);
860template<
typename Scalar>
862inline EIGEN_MATHFUNC_RETVAL(imag, Scalar) imag(
const Scalar& x)
864 return EIGEN_MATHFUNC_IMPL(imag, Scalar)::run(x);
867template<
typename Scalar>
869inline EIGEN_MATHFUNC_RETVAL(arg, Scalar) arg(
const Scalar& x)
871 return EIGEN_MATHFUNC_IMPL(arg, Scalar)::run(x);
874template<
typename Scalar>
876inline typename internal::add_const_on_value_type< EIGEN_MATHFUNC_RETVAL(imag_ref, Scalar) >::type imag_ref(
const Scalar& x)
878 return internal::imag_ref_impl<Scalar>::run(x);
881template<
typename Scalar>
883inline EIGEN_MATHFUNC_RETVAL(imag_ref, Scalar) imag_ref(Scalar& x)
885 return EIGEN_MATHFUNC_IMPL(imag_ref, Scalar)::run(x);
888template<
typename Scalar>
890inline EIGEN_MATHFUNC_RETVAL(conj, Scalar) conj(
const Scalar& x)
892 return EIGEN_MATHFUNC_IMPL(conj, Scalar)::run(x);
895template<
typename Scalar>
897inline EIGEN_MATHFUNC_RETVAL(abs2, Scalar) abs2(
const Scalar& x)
899 return EIGEN_MATHFUNC_IMPL(abs2, Scalar)::run(x);
902template<
typename Scalar>
904inline EIGEN_MATHFUNC_RETVAL(norm1, Scalar) norm1(
const Scalar& x)
906 return EIGEN_MATHFUNC_IMPL(norm1, Scalar)::run(x);
909template<
typename Scalar>
911inline EIGEN_MATHFUNC_RETVAL(hypot, Scalar) hypot(
const Scalar& x,
const Scalar& y)
913 return EIGEN_MATHFUNC_IMPL(hypot, Scalar)::run(x, y);
916template<
typename Scalar>
918inline EIGEN_MATHFUNC_RETVAL(log1p, Scalar) log1p(
const Scalar& x)
920 return EIGEN_MATHFUNC_IMPL(log1p, Scalar)::run(x);
923template<
typename Scalar>
925inline EIGEN_MATHFUNC_RETVAL(pow, Scalar) pow(
const Scalar& x,
const Scalar& y)
927 return EIGEN_MATHFUNC_IMPL(pow, Scalar)::run(x, y);
930template<
typename T> EIGEN_DEVICE_FUNC bool (isnan) (
const T &x) {
return internal::isnan_impl(x); }
931template<
typename T> EIGEN_DEVICE_FUNC bool (isinf) (
const T &x) {
return internal::isinf_impl(x); }
932template<
typename T> EIGEN_DEVICE_FUNC bool (isfinite)(
const T &x) {
return internal::isfinite_impl(x); }
934template<
typename Scalar>
936inline EIGEN_MATHFUNC_RETVAL(round, Scalar) round(
const Scalar& x)
938 return EIGEN_MATHFUNC_IMPL(round, Scalar)::run(x);
945 EIGEN_USING_STD_MATH(floor);
953 EIGEN_USING_STD_MATH(ceil);
959inline int log2(
int x)
963 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 };
969 return table[(v * 0x07C4ACDDU) >> 27];
977bool isfinite_impl(
const std::complex<T>& x)
979 return (numext::isfinite)(numext::real(x)) && (numext::isfinite)(numext::imag(x));
983bool isnan_impl(
const std::complex<T>& x)
985 return (numext::isnan)(numext::real(x)) || (numext::isnan)(numext::imag(x));
989bool isinf_impl(
const std::complex<T>& x)
991 return ((numext::isinf)(numext::real(x)) || (numext::isinf)(numext::imag(x))) && (!(numext::isnan)(x));
998template<
typename Scalar,
1003template<
typename Scalar>
1006 typedef typename NumTraits<Scalar>::Real
RealScalar;
1011 return abs(x) <= abs(y) *
prec;
1014 static inline bool isApprox(
const Scalar& x,
const Scalar& y,
const RealScalar&
prec)
1017 return abs(x - y) <= numext::mini(abs(x), abs(y)) *
prec;
1020 static inline bool isApproxOrLessThan(
const Scalar& x,
const Scalar& y,
const RealScalar&
prec)
1022 return x <= y || isApprox(x, y,
prec);
1026template<
typename Scalar>
1029 typedef typename NumTraits<Scalar>::Real
RealScalar;
1031 static inline bool isMuchSmallerThan(
const Scalar& x,
const Scalar&,
const RealScalar&)
1033 return x == Scalar(0);
1036 static inline bool isApprox(
const Scalar& x,
const Scalar& y,
const RealScalar&)
1041 static inline bool isApproxOrLessThan(
const Scalar& x,
const Scalar& y,
const RealScalar&)
1047template<
typename Scalar>
1050 typedef typename NumTraits<Scalar>::Real
RealScalar;
1051 template<
typename OtherScalar>
1054 return numext::abs2(x) <= numext::abs2(y) *
prec *
prec;
1056 static inline bool isApprox(
const Scalar& x,
const Scalar& y,
const RealScalar&
prec)
1058 return numext::abs2(x - y) <= numext::mini(numext::abs2(x), numext::abs2(y)) *
prec *
prec;
1062template<
typename Scalar>
1066inline bool isMuchSmallerThan(
const Scalar& x,
const OtherScalar& y,
1073inline bool isApprox(
const Scalar& x,
const Scalar& y,
1076 return scalar_fuzzy_impl<Scalar>::isApprox(x, y, precision);
1079template<
typename Scalar> EIGEN_DEVICE_FUNC
1080inline bool isApproxOrLessThan(
const Scalar& x,
const Scalar& y,
1081 typename NumTraits<Scalar>::Real precision = NumTraits<Scalar>::dummy_precision())
1083 return scalar_fuzzy_impl<Scalar>::isApproxOrLessThan(x, y, precision);
1092 static inline bool run()
1100 typedef bool RealScalar;
1103 static inline bool isMuchSmallerThan(
const bool& x,
const bool&,
const bool&)
1109 static inline bool isApprox(
bool x,
bool y,
bool)
1115 static inline bool isApproxOrLessThan(
const bool& x,
const bool& y,
const bool&)
Pseudo expression representing a solving operation.
Definition Solve.h:63
Holds information about the various numeric (i.e.
Definition NumTraits.h:108
Definition MathFunctions.h:246
Definition MathFunctions.h:268
Definition MathFunctions.h:279
Definition MathFunctions.h:55
Definition MathFunctions.h:428
Definition MathFunctions.h:448
Definition MathFunctions.h:453
Definition MathFunctions.h:361
Definition MathFunctions.h:215
Definition MathFunctions.h:236
Definition MathFunctions.h:51
Definition MathFunctions.h:325
Definition MathFunctions.h:351
Definition MathFunctions.h:110
Definition MathFunctions.h:131
Definition MathFunctions.h:171
Definition MathFunctions.h:201
Definition MathFunctions.h:205
Definition MathFunctions.h:135
Definition MathFunctions.h:462
Definition MathFunctions.h:487
Definition MathFunctions.h:289
Definition MathFunctions.h:311
Definition MathFunctions.h:315
Definition MathFunctions.h:497
Definition MathFunctions.h:526
Definition MathFunctions.h:530
Definition MathFunctions.h:541
Definition MathFunctions.h:544
Definition MathFunctions.h:548
Definition MathFunctions.h:75
Definition MathFunctions.h:96
Definition MathFunctions.h:145
Definition MathFunctions.h:161
Definition MathFunctions.h:100
Definition MathFunctions.h:395
Definition MathFunctions.h:408
Definition MathFunctions.h:1001
Definition MathFunctions.h:1063