Skip to main content

xsf/scipy_special/
bessel_prime.rs

1//! Derivatives of Bessel functions
2use crate::utils::neg_one_pow;
3use crate::xsf::bessel::BesselArg;
4
5/// Translated from <https://github.com/scipy/scipy/blob/9531cc5/scipy/special/_basic.py#L803-L814>
6#[inline]
7fn bessel_diff_formula<T, L>(v: f64, n: u32, bessel_fn: L, phase: f64) -> T
8where
9    T: BesselArg,
10    L: Fn(f64) -> T,
11{
12    if n == 0 {
13        return bessel_fn(v);
14    }
15
16    // p = 1.0
17    let mut p = 1.0;
18    // s = L(v-n, z)
19    let mut s = bessel_fn(v - f64::from(n));
20    // for i in range(1, n+1):
21    for i in 1..=n {
22        let (i, n) = (f64::from(i), f64::from(n));
23        // p = phase * (p * (n-i+1)) / i   # = choose(k, i)
24        p = phase * (p * (n - i + 1.0)) / i;
25        // s += p*L(v-n + i*2, z)
26        s = s + bessel_fn(v - (n - i * 2.0)) * p.into();
27    }
28    // return s / (2.**n)
29    s * (1.0 / f64::from(n).exp2()).into()
30}
31
32/// Compute the $n$th derivative of [`bessel_j(v, z)`](crate::bessel_j) w.r.t. `z`
33///
34/// Pure rust implementation of [`scipy.special.jvp`][scipy-jvp].
35///
36/// [scipy-jvp]: https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.jvp.html
37///
38/// # See also
39/// - [`bessel_j`](crate::bessel_j): Bessel function $J_v(z)$
40/// - [`sph_bessel_j_prime`](crate::sph_bessel_j_prime): spherical Bessel derivative $j_n\'(z)$
41#[doc(alias = "jvp")]
42#[inline]
43pub fn bessel_j_prime<T: BesselArg>(v: f64, z: T, n: u32) -> T {
44    bessel_diff_formula(v, n, |v| z.bessel_j(v), -1.0)
45}
46
47/// Compute the $n$th derivative of [`bessel_y(v, z)`](crate::bessel_y) w.r.t. `z`
48///
49/// Pure rust implementation of [`scipy.special.yvp`][scipy-yvp].
50///
51/// [scipy-yvp]: https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.yvp.html
52///
53/// # See also
54/// - [`bessel_y`](crate::bessel_y): Bessel function $Y_v(z)$
55/// - [`sph_bessel_y_prime`](crate::sph_bessel_y_prime): spherical Bessel derivative $y_n\'(z)$
56#[doc(alias = "yvp")]
57#[inline]
58pub fn bessel_y_prime<T: BesselArg>(v: f64, z: T, n: u32) -> T {
59    bessel_diff_formula(v, n, |v| z.bessel_y(v), -1.0)
60}
61
62/// Compute the $n$th derivative of [`bessel_i(v, z)`](crate::bessel_i) w.r.t. `z`
63///
64/// Pure rust implementation of [`scipy.special.ivp`][scipy-ivp].
65///
66/// [scipy-ivp]: https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.ivp.html
67///
68/// # See also
69/// - [`bessel_i`](crate::bessel_i): modified Bessel function $I_v(z)$
70/// - [`sph_bessel_i_prime`](crate::sph_bessel_i_prime): spherical modified Bessel derivative $i_n\'(z)$
71#[doc(alias = "ivp")]
72#[inline]
73pub fn bessel_i_prime<T: BesselArg>(v: f64, z: T, n: u32) -> T {
74    bessel_diff_formula(v, n, |v| z.bessel_i(v), 1.0)
75}
76
77/// Compute the $n$th derivative of [`bessel_k(v, z)`](crate::bessel_k) w.r.t. `z`
78///
79/// Pure rust implementation of [`scipy.special.kvp`][scipy-kvp].
80///
81/// [scipy-kvp]: https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.kvp.html
82///
83/// # See also
84/// - [`bessel_k`](crate::bessel_k): modified Bessel function $K_v(z)$
85/// - [`sph_bessel_k_prime`](crate::sph_bessel_k_prime): spherical modified Bessel derivative $k_n\'(z)$
86#[doc(alias = "kvp")]
87#[inline]
88pub fn bessel_k_prime<T: BesselArg>(v: f64, z: T, n: u32) -> T {
89    let sign = neg_one_pow(n);
90    bessel_diff_formula(v, n, |v| z.bessel_k(v), 1.0) * sign.into()
91}
92
93/// Compute the $n$th derivative of [`hankel_1(v, z)`](crate::hankel_1) w.r.t. `z`
94///
95/// Pure rust implementation of [`scipy.special.h1vp`][scipy-h1vp].
96///
97/// [scipy-h1vp]: https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.h1vp.html
98///
99/// # See also
100/// - [`hankel_1`](crate::hankel_1): Hankel function $H_v^{(1)}(z)$
101#[doc(alias = "h1vp")]
102#[inline]
103pub fn hankel_1_prime<T: BesselArg>(v: f64, z: T, n: u32) -> num_complex::Complex<f64> {
104    bessel_diff_formula(v, n, |v| crate::hankel_1(v, z), -1.0)
105}
106
107/// Compute the $n$th derivative of [`hankel_2(v, z)`](crate::hankel_2) w.r.t. `z`
108///
109/// Pure rust implementation of [`scipy.special.h2vp`][scipy-h2vp].
110///
111/// [scipy-h2vp]: https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.h2vp.html
112///
113/// # See also
114/// - [`hankel_2`](crate::hankel_2): Hankel function $H_v^{(2)}(z)$
115#[doc(alias = "h2vp")]
116#[inline]
117pub fn hankel_2_prime<T: BesselArg>(v: f64, z: T, n: u32) -> num_complex::Complex<f64> {
118    bessel_diff_formula(v, n, |v| crate::hankel_2(v, z), -1.0)
119}