1 #ifndef STAN_MATH_PRIM_FUN_MODIFIED_BESSEL_SECOND_KIND_HPP
2 #define STAN_MATH_PRIM_FUN_MODIFIED_BESSEL_SECOND_KIND_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/functor/apply_scalar_binary.hpp>
6 #include <boost/math/special_functions/bessel.hpp>
7 
8 namespace stan {
9 namespace math {
10 
11 /**
12  *
13    \f[
14    \mbox{modified\_bessel\_second\_kind}(v, z) =
15    \begin{cases}
16      \textrm{error} & \mbox{if } z \leq 0 \\
17      K_v(z) & \mbox{if } z > 0 \\[6pt]
18      \textrm{NaN} & \mbox{if } z = \textrm{NaN}
19    \end{cases}
20    \f]
21 
22    \f[
23    \frac{\partial\, \mbox{modified\_bessel\_second\_kind}(v, z)}{\partial z} =
24    \begin{cases}
25      \textrm{error} & \mbox{if } z \leq 0 \\
26      \frac{\partial\, K_v(z)}{\partial z} & \mbox{if } z > 0 \\[6pt]
27      \textrm{NaN} & \mbox{if } z = \textrm{NaN}
28    \end{cases}
29    \f]
30 
31    \f[
32    {K_v}(z)
33    =
34    \frac{\pi}{2}\cdot\frac{I_{-v}(z) - I_{v}(z)}{\sin(v\pi)}
35    \f]
36 
37    \f[
38    \frac{\partial \, K_v(z)}{\partial z} = -\frac{v}{z}K_v(z)-K_{v-1}(z)
39    \f]
40  *
41  */
42 template <typename T2, require_arithmetic_t<T2>* = nullptr>
modified_bessel_second_kind(int v,const T2 z)43 inline T2 modified_bessel_second_kind(int v, const T2 z) {
44   return boost::math::cyl_bessel_k(v, z);
45 }
46 
47 /**
48  * Enables the vectorised application of the modified_bessel_second_kind
49  * function, when the first and/or second arguments are containers.
50  *
51  * @tparam T1 type of first input
52  * @tparam T2 type of second input
53  * @param a First input
54  * @param b Second input
55  * @return modified_bessel_second_kind function applied to the two inputs.
56  */
57 template <typename T1, typename T2, require_any_container_t<T1, T2>* = nullptr>
modified_bessel_second_kind(const T1 & a,const T2 & b)58 inline auto modified_bessel_second_kind(const T1& a, const T2& b) {
59   return apply_scalar_binary(a, b, [&](const auto& c, const auto& d) {
60     return modified_bessel_second_kind(c, d);
61   });
62 }
63 
64 }  // namespace math
65 }  // namespace stan
66 
67 #endif
68