1 #ifndef STAN_MATH_PRIM_FUN_MODIFIED_BESSEL_FIRST_KIND_HPP
2 #define STAN_MATH_PRIM_FUN_MODIFIED_BESSEL_FIRST_KIND_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/functor/apply_scalar_binary.hpp>
7 #include <boost/math/special_functions/bessel.hpp>
8 
9 namespace stan {
10 namespace math {
11 
12 /**
13  *
14    \f[
15    \mbox{modified\_bessel\_first\_kind}(v, z) =
16    \begin{cases}
17      I_v(z) & \mbox{if } -\infty\leq z \leq \infty \\[6pt]
18      \textrm{error} & \mbox{if } z = \textrm{NaN}
19    \end{cases}
20    \f]
21 
22    \f[
23    \frac{\partial\, \mbox{modified\_bessel\_first\_kind}(v, z)}{\partial z} =
24    \begin{cases}
25      \frac{\partial\, I_v(z)}{\partial z} & \mbox{if } -\infty\leq z\leq \infty
26  \\[6pt] \textrm{error} & \mbox{if } z = \textrm{NaN} \end{cases} \f]
27 
28    \f[
29      {I_v}(z) = \left(\frac{1}{2}z\right)^v\sum_{k=0}^\infty
30  \frac{\left(\frac{1}{4}z^2\right)^k}{k!\Gamma(v+k+1)} \f]
31 
32    \f[
33    \frac{\partial \, I_v(z)}{\partial z} = I_{v-1}(z)-\frac{v}{z}I_v(z)
34    \f]
35  *
36  */
37 template <typename T2, require_arithmetic_t<T2>* = nullptr>
modified_bessel_first_kind(int v,const T2 z)38 inline T2 modified_bessel_first_kind(int v, const T2 z) {
39   check_not_nan("modified_bessel_first_kind", "z", z);
40 
41   return boost::math::cyl_bessel_i(v, z);
42 }
43 
44 /**
45  * This function exists because when z is of type integer,
46  * cyl_bessel_i(v, z) returns an integer. This
47  * results in overflow when the function value is large.
48  */
modified_bessel_first_kind(int v,int z)49 inline double modified_bessel_first_kind(int v, int z) {
50   check_not_nan("modified_bessel_first_kind", "z", z);
51 
52   return boost::math::cyl_bessel_i(v, static_cast<double>(z));
53 }
54 
55 /**
56  * Enables the vectorised application of the modified_bessel_first_kind
57  * function, when the first and/or second arguments are containers.
58  *
59  * @tparam T1 type of first input
60  * @tparam T2 type of second input
61  * @param a First input
62  * @param b Second input
63  * @return modified_bessel_first_kind function applied to the two inputs.
64  */
65 template <typename T1, typename T2, require_any_container_t<T1, T2>* = nullptr>
modified_bessel_first_kind(const T1 & a,const T2 & b)66 inline auto modified_bessel_first_kind(const T1& a, const T2& b) {
67   return apply_scalar_binary(a, b, [&](const auto& c, const auto& d) {
68     return modified_bessel_first_kind(c, d);
69   });
70 }
71 
72 }  // namespace math
73 }  // namespace stan
74 
75 #endif
76