Add Bessel functions to SpecialFunctions.

- Split SpecialFunctions files in to a separate BesselFunctions file.

In particular add:
    - Modified bessel functions of the second kind k0, k1, k0e, k1e
    - Bessel functions of the first kind j0, j1
    - Bessel functions of the second kind y0, y1
This commit is contained in:
Srinivas Vasudevan
2019-09-14 12:16:47 -04:00
parent facdec5aa7
commit 6e215cf109
23 changed files with 3427 additions and 548 deletions

View File

@@ -1757,7 +1757,7 @@ struct betainc_helper<double> {
if ((a + b) < maxgam && numext::abs(u) < maxlog) {
t = gamma(a + b) / (gamma(a) * gamma(b));
s = s * t * pow(x, a);
} else {
}
*/
t = lgamma_impl<double>::run(a + b) - lgamma_impl<double>::run(a) -
lgamma_impl<double>::run(b) + u + numext::log(s);
@@ -1864,351 +1864,6 @@ struct betainc_impl<double> {
#endif // EIGEN_HAS_C99_MATH
/****************************************************************************
* Implementation of Bessel function, based on Cephes *
****************************************************************************/
template <typename Scalar>
struct i0e_retval {
typedef Scalar type;
};
template <typename T, typename ScalarType>
struct generic_i0e {
EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run(const T&) {
EIGEN_STATIC_ASSERT((internal::is_same<T, T>::value == false),
THIS_TYPE_IS_NOT_SUPPORTED);
return ScalarType(0);
}
};
template <typename T>
struct generic_i0e<T, float> {
EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run(const T& x) {
/* i0ef.c
*
* Modified Bessel function of order zero,
* exponentially scaled
*
*
*
* SYNOPSIS:
*
* float x, y, i0ef();
*
* y = i0ef( x );
*
*
*
* DESCRIPTION:
*
* Returns exponentially scaled modified Bessel function
* of order zero of the argument.
*
* The function is defined as i0e(x) = exp(-|x|) j0( ix ).
*
*
*
* ACCURACY:
*
* Relative error:
* arithmetic domain # trials peak rms
* IEEE 0,30 100000 3.7e-7 7.0e-8
* See i0f().
*
*/
const float A[] = {-1.30002500998624804212E-8f, 6.04699502254191894932E-8f,
-2.67079385394061173391E-7f, 1.11738753912010371815E-6f,
-4.41673835845875056359E-6f, 1.64484480707288970893E-5f,
-5.75419501008210370398E-5f, 1.88502885095841655729E-4f,
-5.76375574538582365885E-4f, 1.63947561694133579842E-3f,
-4.32430999505057594430E-3f, 1.05464603945949983183E-2f,
-2.37374148058994688156E-2f, 4.93052842396707084878E-2f,
-9.49010970480476444210E-2f, 1.71620901522208775349E-1f,
-3.04682672343198398683E-1f, 6.76795274409476084995E-1f};
const float B[] = {3.39623202570838634515E-9f, 2.26666899049817806459E-8f,
2.04891858946906374183E-7f, 2.89137052083475648297E-6f,
6.88975834691682398426E-5f, 3.36911647825569408990E-3f,
8.04490411014108831608E-1f};
T y = pabs(x);
T y_le_eight = internal::pchebevl<T, 18>::run(
pmadd(pset1<T>(0.5f), y, pset1<T>(-2.0f)), A);
T y_gt_eight = pdiv(
internal::pchebevl<T, 7>::run(
psub(pdiv(pset1<T>(32.0f), y), pset1<T>(2.0f)), B),
psqrt(y));
// TODO: Perhaps instead check whether all packet elements are in
// [-8, 8] and evaluate a branch based off of that. It's possible
// in practice most elements are in this region.
return pselect(pcmp_le(y, pset1<T>(8.0f)), y_le_eight, y_gt_eight);
}
};
template <typename T>
struct generic_i0e<T, double> {
EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run(const T& x) {
/* i0e.c
*
* Modified Bessel function of order zero,
* exponentially scaled
*
*
*
* SYNOPSIS:
*
* double x, y, i0e();
*
* y = i0e( x );
*
*
*
* DESCRIPTION:
*
* Returns exponentially scaled modified Bessel function
* of order zero of the argument.
*
* The function is defined as i0e(x) = exp(-|x|) j0( ix ).
*
*
*
* ACCURACY:
*
* Relative error:
* arithmetic domain # trials peak rms
* IEEE 0,30 30000 5.4e-16 1.2e-16
* See i0().
*
*/
const double A[] = {-4.41534164647933937950E-18, 3.33079451882223809783E-17,
-2.43127984654795469359E-16, 1.71539128555513303061E-15,
-1.16853328779934516808E-14, 7.67618549860493561688E-14,
-4.85644678311192946090E-13, 2.95505266312963983461E-12,
-1.72682629144155570723E-11, 9.67580903537323691224E-11,
-5.18979560163526290666E-10, 2.65982372468238665035E-9,
-1.30002500998624804212E-8, 6.04699502254191894932E-8,
-2.67079385394061173391E-7, 1.11738753912010371815E-6,
-4.41673835845875056359E-6, 1.64484480707288970893E-5,
-5.75419501008210370398E-5, 1.88502885095841655729E-4,
-5.76375574538582365885E-4, 1.63947561694133579842E-3,
-4.32430999505057594430E-3, 1.05464603945949983183E-2,
-2.37374148058994688156E-2, 4.93052842396707084878E-2,
-9.49010970480476444210E-2, 1.71620901522208775349E-1,
-3.04682672343198398683E-1, 6.76795274409476084995E-1};
const double B[] = {
-7.23318048787475395456E-18, -4.83050448594418207126E-18,
4.46562142029675999901E-17, 3.46122286769746109310E-17,
-2.82762398051658348494E-16, -3.42548561967721913462E-16,
1.77256013305652638360E-15, 3.81168066935262242075E-15,
-9.55484669882830764870E-15, -4.15056934728722208663E-14,
1.54008621752140982691E-14, 3.85277838274214270114E-13,
7.18012445138366623367E-13, -1.79417853150680611778E-12,
-1.32158118404477131188E-11, -3.14991652796324136454E-11,
1.18891471078464383424E-11, 4.94060238822496958910E-10,
3.39623202570838634515E-9, 2.26666899049817806459E-8,
2.04891858946906374183E-7, 2.89137052083475648297E-6,
6.88975834691682398426E-5, 3.36911647825569408990E-3,
8.04490411014108831608E-1};
T y = pabs(x);
T y_le_eight = internal::pchebevl<T, 30>::run(
pmadd(pset1<T>(0.5), y, pset1<T>(-2.0)), A);
T y_gt_eight = pdiv(
internal::pchebevl<T, 25>::run(
psub(pdiv(pset1<T>(32.0), y), pset1<T>(2.0)), B),
psqrt(y));
// TODO: Perhaps instead check whether all packet elements are in
// [-8, 8] and evaluate a branch based off of that. It's possible
// in practice most elements are in this region.
return pselect(pcmp_le(y, pset1<T>(8.0)), y_le_eight, y_gt_eight);
}
};
template <typename Scalar>
struct i0e_impl {
EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar run(const Scalar x) {
return generic_i0e<Scalar, Scalar>::run(x);
}
};
template <typename Scalar>
struct i1e_retval {
typedef Scalar type;
};
template <typename T, typename ScalarType>
struct generic_i1e {
EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run(const T&) {
EIGEN_STATIC_ASSERT((internal::is_same<T, T>::value == false),
THIS_TYPE_IS_NOT_SUPPORTED);
return ScalarType(0);
}
};
template <typename T>
struct generic_i1e<T, float> {
EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run(const T& x) {
/* i1ef.c
*
* Modified Bessel function of order one,
* exponentially scaled
*
*
*
* SYNOPSIS:
*
* float x, y, i1ef();
*
* y = i1ef( x );
*
*
*
* DESCRIPTION:
*
* Returns exponentially scaled modified Bessel function
* of order one of the argument.
*
* The function is defined as i1(x) = -i exp(-|x|) j1( ix ).
*
*
*
* ACCURACY:
*
* Relative error:
* arithmetic domain # trials peak rms
* IEEE 0, 30 30000 1.5e-6 1.5e-7
* See i1().
*
*/
const float A[] = {9.38153738649577178388E-9f, -4.44505912879632808065E-8f,
2.00329475355213526229E-7f, -8.56872026469545474066E-7f,
3.47025130813767847674E-6f, -1.32731636560394358279E-5f,
4.78156510755005422638E-5f, -1.61760815825896745588E-4f,
5.12285956168575772895E-4f, -1.51357245063125314899E-3f,
4.15642294431288815669E-3f, -1.05640848946261981558E-2f,
2.47264490306265168283E-2f, -5.29459812080949914269E-2f,
1.02643658689847095384E-1f, -1.76416518357834055153E-1f,
2.52587186443633654823E-1f};
const float B[] = {-3.83538038596423702205E-9f, -2.63146884688951950684E-8f,
-2.51223623787020892529E-7f, -3.88256480887769039346E-6f,
-1.10588938762623716291E-4f, -9.76109749136146840777E-3f,
7.78576235018280120474E-1f};
T y = pabs(x);
T y_le_eight = pmul(y, internal::pchebevl<T, 17>::run(
pmadd(pset1<T>(0.5f), y, pset1<T>(-2.0f)), A));
T y_gt_eight = pdiv(
internal::pchebevl<T, 7>::run(
psub(pdiv(pset1<T>(32.0f), y),
pset1<T>(2.0f)), B),
psqrt(y));
// TODO: Perhaps instead check whether all packet elements are in
// [-8, 8] and evaluate a branch based off of that. It's possible
// in practice most elements are in this region.
y = pselect(pcmp_le(y, pset1<T>(8.0f)), y_le_eight, y_gt_eight);
return pselect(pcmp_lt(x, pset1<T>(0.0f)), -y, y);
}
};
template <typename T>
struct generic_i1e<T, double> {
EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run(const T& x) {
/* i1e.c
*
* Modified Bessel function of order one,
* exponentially scaled
*
*
*
* SYNOPSIS:
*
* double x, y, i1e();
*
* y = i1e( x );
*
*
*
* DESCRIPTION:
*
* Returns exponentially scaled modified Bessel function
* of order one of the argument.
*
* The function is defined as i1(x) = -i exp(-|x|) j1( ix ).
*
*
*
* ACCURACY:
*
* Relative error:
* arithmetic domain # trials peak rms
* IEEE 0, 30 30000 2.0e-15 2.0e-16
* See i1().
*
*/
const double A[] = {2.77791411276104639959E-18, -2.11142121435816608115E-17,
1.55363195773620046921E-16, -1.10559694773538630805E-15,
7.60068429473540693410E-15, -5.04218550472791168711E-14,
3.22379336594557470981E-13, -1.98397439776494371520E-12,
1.17361862988909016308E-11, -6.66348972350202774223E-11,
3.62559028155211703701E-10, -1.88724975172282928790E-9,
9.38153738649577178388E-9, -4.44505912879632808065E-8,
2.00329475355213526229E-7, -8.56872026469545474066E-7,
3.47025130813767847674E-6, -1.32731636560394358279E-5,
4.78156510755005422638E-5, -1.61760815825896745588E-4,
5.12285956168575772895E-4, -1.51357245063125314899E-3,
4.15642294431288815669E-3, -1.05640848946261981558E-2,
2.47264490306265168283E-2, -5.29459812080949914269E-2,
1.02643658689847095384E-1, -1.76416518357834055153E-1,
2.52587186443633654823E-1};
const double B[] = {
7.51729631084210481353E-18, 4.41434832307170791151E-18,
-4.65030536848935832153E-17, -3.20952592199342395980E-17,
2.96262899764595013876E-16, 3.30820231092092828324E-16,
-1.88035477551078244854E-15, -3.81440307243700780478E-15,
1.04202769841288027642E-14, 4.27244001671195135429E-14,
-2.10154184277266431302E-14, -4.08355111109219731823E-13,
-7.19855177624590851209E-13, 2.03562854414708950722E-12,
1.41258074366137813316E-11, 3.25260358301548823856E-11,
-1.89749581235054123450E-11, -5.58974346219658380687E-10,
-3.83538038596423702205E-9, -2.63146884688951950684E-8,
-2.51223623787020892529E-7, -3.88256480887769039346E-6,
-1.10588938762623716291E-4, -9.76109749136146840777E-3,
7.78576235018280120474E-1};
T y = pabs(x);
T y_le_eight = pmul(y, internal::pchebevl<T, 29>::run(
pmadd(pset1<T>(0.5), y, pset1<T>(-2.0)), A));
T y_gt_eight = pdiv(
internal::pchebevl<T, 25>::run(
psub(pdiv(pset1<T>(32.0), y),
pset1<T>(2.0)), B),
psqrt(y));
// TODO: Perhaps instead check whether all packet elements are in
// [-8, 8] and evaluate a branch based off of that. It's possible
// in practice most elements are in this region.
y = pselect(pcmp_le(y, pset1<T>(8.0)), y_le_eight, y_gt_eight);
return pselect(pcmp_lt(x, pset1<T>(0.0f)), -y, y);
}
};
template <typename Scalar>
struct i1e_impl {
EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar run(const Scalar x) {
return generic_i1e<Scalar, Scalar>::run(x);
}
};
} // end namespace internal
namespace numext {
@@ -2285,21 +1940,7 @@ EIGEN_DEVICE_FUNC inline EIGEN_MATHFUNC_RETVAL(betainc, Scalar)
return EIGEN_MATHFUNC_IMPL(betainc, Scalar)::run(a, b, x);
}
template <typename Scalar>
EIGEN_DEVICE_FUNC inline EIGEN_MATHFUNC_RETVAL(i0e, Scalar)
i0e(const Scalar& x) {
return EIGEN_MATHFUNC_IMPL(i0e, Scalar)::run(x);
}
template <typename Scalar>
EIGEN_DEVICE_FUNC inline EIGEN_MATHFUNC_RETVAL(i1e, Scalar)
i1e(const Scalar& x) {
return EIGEN_MATHFUNC_IMPL(i1e, Scalar)::run(x);
}
} // end namespace numext
} // end namespace Eigen
#endif // EIGEN_SPECIAL_FUNCTIONS_H