|
| 1 | + |
| 2 | +""" |
| 3 | + FunctionZeros |
| 4 | +This module provides functions to compute the zeros of the J and Y functions, |
| 5 | +and the zeros of their derivatives, where J and Y are Bessel functions of the first and second kind, respectively. |
| 6 | +""" |
1 | 7 | module FunctionZeros |
2 | 8 | import SpecialFunctions |
3 | 9 | import Roots |
4 | 10 |
|
5 | | -export besselj_zero, bessely_zero |
| 11 | +export besselj_zero, bessely_zero, besselj_deriv_zero, bessely_deriv_zero |
| 12 | + |
| 13 | +# Set max order and index of zeros to precompute and tabulate |
| 14 | +const nupre_max = 1 |
| 15 | +const npre_max = 500 |
| 16 | + |
| 17 | +# Strings used in multiple function docstrings: |
| 18 | +const speeddocstr = """For greater speed, table lookup is used for `Float64` outputs when |
| 19 | + `nu ∈ 0:$nupre_max` and `n ∈ 1:$(npre_max)`.""" |
| 20 | +const argstr = """## Arguments |
| 21 | + - `nu::Real`: The order of the Bessel function. |
| 22 | + - `n::Integer`: Enumerates the zero to be found. |
| 23 | +
|
| 24 | + ## Return Value |
| 25 | + The return value type is determined by `nu`. |
| 26 | + When `nu isa AbstractFloat`, the returned value has the same type as `nu`. |
| 27 | + When `nu isa Integer`, the usual promotion rules apply. |
| 28 | + """ |
| 29 | + |
| 30 | +""" |
| 31 | + besselj_zero_asymptotic(nu, n) |
6 | 32 |
|
| 33 | +Asymptotic formula for the `n`th zero of the the Bessel function of the first kind J of order `nu`. |
7 | 34 |
|
| 35 | +$argstr |
| 36 | +""" |
8 | 37 | besselj_zero_asymptotic(nu, n) = bessel_zero_asymptotic(nu, n, 1) |
9 | 38 |
|
| 39 | +""" |
| 40 | + bessely_zero_asymptotic(nu, n) |
| 41 | +
|
| 42 | +Asymptotic formula for the `n`th zero of the the Bessel function of the second kind Y of order `nu`. |
| 43 | +
|
| 44 | +$argstr |
| 45 | +""" |
| 46 | +bessely_zero_asymptotic(nu, n) = bessel_zero_asymptotic(nu, n, 2) |
| 47 | + |
| 48 | + |
10 | 49 | """ |
11 | 50 | bessel_zero_asymptotic(nu, n, kind=1) |
12 | 51 |
|
|
42 | 81 | # Order 0 is 6 times slower and 50-100 times less accurate |
43 | 82 | # than higher orders, with other parameters constant. |
44 | 83 | """ |
45 | | - besselj_zero(nu, n; order=2) |
| 84 | + _besselj_zero(nu, n) |
46 | 85 |
|
47 | 86 | `n`th zero of the Bessel J function of order `nu`, |
48 | | -for `n` = `1,2,...`. |
| 87 | +for `n` = `1,2,...`. |
49 | 88 |
|
50 | | -`order` is passed to the function `Roots.fzero`. |
| 89 | +$argstr |
51 | 90 | """ |
52 | | -besselj_zero(nu, n; order=2) = Roots.find_zero((x) -> SpecialFunctions.besselj(nu, x), |
53 | | - bessel_zero_asymptotic(nu, n, 1); order=order) |
| 91 | +function _besselj_zero(nu::Real, n::Integer) |
| 92 | + return Roots.find_zero(bessel_zero_asymptotic(nu, n, 1)) do x |
| 93 | + return SpecialFunctions.besselj(nu, x) |
| 94 | + end |
| 95 | +end |
| 96 | + |
| 97 | +# Float64 tabulation of selected besselj_zero values |
| 98 | +const jzero_pre = [_besselj_zero(nu, n) for nu in 0:nupre_max, n in 1:npre_max] |
54 | 99 |
|
55 | 100 | """ |
56 | | - bessely_zero(nu, n; order=2) |
| 101 | + besselj_zero(nu, n) |
| 102 | +
|
| 103 | +Return the `n`th zero of the Bessel J function of order `nu`, |
| 104 | +for `n` = `1,2,...`. |
| 105 | +
|
| 106 | +$argstr |
| 107 | +$speeddocstr |
| 108 | +""" |
| 109 | +besselj_zero(nu::Real, n::Integer) = _besselj_zero(nu, n) |
| 110 | + |
| 111 | +besselj_zero(nu::BigInt, n::Integer) = _besselj_zero(nu, n) |
| 112 | + |
| 113 | +function besselj_zero(nu::Union{Integer,Float64}, n::Integer) |
| 114 | + if nu in 0:nupre_max && n in 1:npre_max |
| 115 | + return jzero_pre[Int(nu) + 1, Int(n)] |
| 116 | + else |
| 117 | + return _besselj_zero(nu, n) |
| 118 | + end |
| 119 | +end |
| 120 | + |
| 121 | + |
| 122 | +""" |
| 123 | + _bessely_zero(nu, n) |
57 | 124 |
|
58 | 125 | `n`th zero of the Bessel Y function of order `nu`, |
59 | 126 | for `n` = `1,2,...`. |
60 | 127 |
|
61 | | -`order` is passed to the function `Roots.fzero`. |
| 128 | +$argstr |
| 129 | +""" |
| 130 | +function _bessely_zero(nu, n) |
| 131 | + if isone(n) && abs(nu) < 0.1587 # See Issue 21 |
| 132 | + return Roots.find_zero((nu + besselj_zero(nu, n)) / 2) do x |
| 133 | + SpecialFunctions.bessely(nu, x) |
| 134 | + end |
| 135 | + else |
| 136 | + return Roots.find_zero(bessel_zero_asymptotic(nu, n, 2)) do x |
| 137 | + SpecialFunctions.bessely(nu, x) |
| 138 | + end |
| 139 | + end |
| 140 | +end |
| 141 | + |
| 142 | +# tabulation of selected bessely_zero values in Float64 |
| 143 | +const yzero_pre = [_bessely_zero(nu, n) for nu in 0:nupre_max, n in 1:npre_max] |
| 144 | + |
| 145 | +""" |
| 146 | + bessely_zero(nu, n) |
| 147 | +
|
| 148 | +Return the `n`th zero of the Bessel Y function of order `nu`, |
| 149 | +for `n` = `1,2,...`. |
| 150 | +
|
| 151 | +$argstr |
| 152 | +$speeddocstr |
| 153 | +""" |
| 154 | +bessely_zero(nu::Real, n::Integer) = _bessely_zero(nu, n) |
| 155 | + |
| 156 | +bessely_zero(nu::BigInt, n::Integer) = _bessely_zero(nu, n) |
| 157 | + |
| 158 | +function bessely_zero(nu::Union{Integer,Float64}, n::Integer) |
| 159 | + if nu in 0:nupre_max && n in 1:npre_max |
| 160 | + return yzero_pre[Int(nu) + 1, Int(n)] |
| 161 | + else |
| 162 | + return _bessely_zero(nu, n) |
| 163 | + end |
| 164 | +end |
| 165 | + |
62 | 166 | """ |
63 | | -bessely_zero(nu, n; order=2) = Roots.find_zero((x) -> SpecialFunctions.bessely(nu, x), |
64 | | - bessel_zero_asymptotic(nu, n, 2); order=order) |
| 167 | + besselj_deriv_zero_asymptotic(nu, n) |
| 168 | +
|
| 169 | +Asymptotic formula for the `n`th zero of the derivative of the Bessel function of the first kind J of order `nu`. |
| 170 | +
|
| 171 | +$argstr |
| 172 | +""" |
| 173 | +besselj_deriv_zero_asymptotic(nu, n) = bessel_deriv_zero_asymptotic(nu, n, 1) |
| 174 | + |
| 175 | +""" |
| 176 | + bessely_deriv_zero_asymptotic(nu, n) |
| 177 | +
|
| 178 | +Asymptotic formula for the `n`th zero of the derivative of the Bessel function of the second kind Y of order `nu`. |
| 179 | +
|
| 180 | +$argstr |
| 181 | +""" |
| 182 | +bessely_deriv_zero_asymptotic(nu, n) = bessel_deriv_zero_asymptotic(nu, n, 2) |
| 183 | + |
| 184 | + |
| 185 | +""" |
| 186 | + bessel_deriv_zero_asymptotic(nu, n, kind=1) |
| 187 | +
|
| 188 | +Asymptotic formula for the `n`th zero of the the derivative of Bessel J (Y) function |
| 189 | +of order `nu`. `kind == 1 (2)` for Bessel function of the first (second) kind, J (Y). |
| 190 | +""" |
| 191 | +function bessel_deriv_zero_asymptotic(nu_in::Real, n::Integer, kind=1) |
| 192 | + # Reference: https://dlmf.nist.gov/10.21.E20 |
| 193 | + nu = abs(nu_in) |
| 194 | + if kind == 1 |
| 195 | + beta = MathConstants.pi * (n + nu / 2 - 3//4) |
| 196 | + else # kind == 2 |
| 197 | + beta = MathConstants.pi * (n + nu / 2 - 1//4) |
| 198 | + end |
| 199 | + delta = 8 * beta |
| 200 | + mu = 4 * nu^2 |
| 201 | + mup2 = mu * mu |
| 202 | + mup3 = mup2 * mu |
| 203 | + mup4 = mup3 * mu |
| 204 | + deltap2 = delta * delta |
| 205 | + deltap3 = deltap2 * delta |
| 206 | + deltap5 = deltap3 * deltap2 |
| 207 | + deltap7 = deltap5 * deltap2 |
| 208 | + t1 = (mu + 3) / delta |
| 209 | + t2 = 4 * (7 * mup2 + 82 * mu - 9) / (3 * deltap3) |
| 210 | + t3 = 32 * (83 * mup3 + 2075 * mup2 - 3039 * mu + 3537) / (15 * deltap5) |
| 211 | + t4 = 64 * (6949 * mup4 + 296492 * mup3 - 1248002 * mup2 + 7414380 * mu - 5853627) / |
| 212 | + (105 * deltap7) |
| 213 | + zero_asymp = beta - (t1 + t2 + t3 + t4) |
| 214 | + return zero_asymp |
| 215 | +end |
| 216 | + |
| 217 | +""" |
| 218 | + _besselj_deriv_zero(nu, n) |
| 219 | +
|
| 220 | +Return the `n`th nonvanishing zero of the derivative of Bessel J function of order `nu`, |
| 221 | +for `n` = `1,2,...`. |
| 222 | +
|
| 223 | +$argstr |
| 224 | +""" |
| 225 | +function _besselj_deriv_zero(nu::Real, n::Integer) |
| 226 | + # Ref: https://dlmf.nist.gov/10.6.E1 |
| 227 | + iszero(nu) && (n += 1) # Skip the zero occuring at zero |
| 228 | + return Roots.find_zero(bessel_deriv_zero_asymptotic(nu, n, 1)) do x |
| 229 | + SpecialFunctions.besselj(nu - 1, x) - SpecialFunctions.besselj(nu + 1, x) |
| 230 | + end |
| 231 | +end |
| 232 | + |
| 233 | +# tabulation of selected besselj_deriv_zero values in Float64 |
| 234 | +const jderivzero_pre = [_besselj_deriv_zero(nu, n) for nu in 0:nupre_max, n in 1:npre_max] |
| 235 | + |
| 236 | +""" |
| 237 | + besselj_deriv_zero(nu, n) |
| 238 | +
|
| 239 | +Return the `n`th nonvanishing zero of the derivative of the Bessel J function of order `nu`, |
| 240 | +for `n` = `1,2,...`. |
| 241 | +
|
| 242 | +$argstr |
| 243 | +$speeddocstr |
| 244 | +""" |
| 245 | +besselj_deriv_zero(nu::Real, n::Integer) = _besselj_deriv_zero(nu, n) |
| 246 | + |
| 247 | +besselj_deriv_zero(nu::BigInt, n::Integer) = _besselj_deriv_zero(nu, n) |
| 248 | + |
| 249 | +function besselj_deriv_zero(nu::Union{Integer,Float64}, n::Integer) |
| 250 | + if nu in 0:nupre_max && n in 1:npre_max |
| 251 | + return jderivzero_pre[Int(nu) + 1, Int(n)] |
| 252 | + else |
| 253 | + return _besselj_deriv_zero(nu, n) |
| 254 | + end |
| 255 | +end |
| 256 | + |
| 257 | +""" |
| 258 | + _bessely_deriv_zero(nu, n) |
| 259 | +
|
| 260 | +Return the `n`th zero of the derivative of the Bessel Y function of order `nu`, |
| 261 | +for `n` = `1,2,...`. |
| 262 | +
|
| 263 | +$argstr |
| 264 | +""" |
| 265 | +function _bessely_deriv_zero(nu::Real, n::Integer) |
| 266 | + # Ref: https://dlmf.nist.gov/10.6.E1 |
| 267 | + return Roots.find_zero(bessel_deriv_zero_asymptotic(nu, n, 2)) do x |
| 268 | + SpecialFunctions.bessely(nu - 1, x) - SpecialFunctions.bessely(nu + 1, x) |
| 269 | + end |
| 270 | +end |
| 271 | + |
| 272 | +# tabulation of selected bessely_deriv_zero values in Float64 |
| 273 | +const yderivzero_pre = [_bessely_deriv_zero(nu, n) for nu in 0:nupre_max, n in 1:npre_max] |
| 274 | + |
| 275 | +""" |
| 276 | + bessely_deriv_zero(nu, n) |
| 277 | +
|
| 278 | +Return the `n`th zero of the derivative of the Bessel Y function of order `nu`, |
| 279 | +for `n` = `1,2,...`. |
| 280 | +
|
| 281 | +$argstr |
| 282 | +$speeddocstr |
| 283 | +""" |
| 284 | +bessely_deriv_zero(nu::Real, n::Integer) = _bessely_deriv_zero(nu, n) |
| 285 | + |
| 286 | +bessely_deriv_zero(nu::BigInt, n::Integer) = _bessely_deriv_zero(nu, n) |
| 287 | + |
| 288 | +function bessely_deriv_zero(nu::Union{Integer,Float64}, n::Integer) |
| 289 | + if nu in 0:nupre_max && n in 1:npre_max |
| 290 | + return yderivzero_pre[Int(nu) + 1, Int(n)] |
| 291 | + else |
| 292 | + return _bessely_deriv_zero(nu, n) |
| 293 | + end |
| 294 | +end |
65 | 295 |
|
66 | 296 | end # module FunctionZeros |
0 commit comments