Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaMath / SpecialFunctions.jl / 814

25 Sep 2020 - 15:06 coverage: 85.701% (+0.05%) from 85.647%
814

Pull #242

travis-ci

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
Merge 225521a95 into 1bb0913fc
Pull Request #242: Adds Owen's T function

32 of 33 new or added lines in 1 file covered. (96.97%)

62 existing lines in 10 files now uncovered.

3644 of 4252 relevant lines covered (85.7%)

2014.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.09
/src/bessel.jl
1
# This file contains code that was formerly a part of Julia. License is MIT: http://julialang.org/license
2

3
using Base.Math: nan_dom_err
4

5
struct AmosException <: Exception
6
    id::Int32
56×
7
end
8

9
function Base.showerror(io::IO, ex::AmosException)
10
    print(io, "AmosException with id $(ex.id): ")
4×
11
    if ex.id == 0
4×
12
        print(io, "normal return, computation complete.")
!
13
    elseif ex.id == 1
4×
14
        print(io, "input error.")
4×
15
    elseif ex.id == 2
!
16
        print(io, "overflow.")
!
17
    elseif ex.id == 3
!
18
        print(io, "input argument magnitude large, less than half machine accuracy loss by argument reduction.")
!
19
    elseif ex.id == 4
!
20
        print(io, "input argument magnitude too large, complete loss of accuracy by argument reduction.")
!
21
    elseif ex.id == 5
!
22
        print(io, "algorithm termination condition not met.")
!
23
    else
24
        print(io, "invalid error flag.")
!
25
    end
26
end
27

28
## Airy functions
29
function _airy(z::Complex{Float64}, id::Int32, kode::Int32)
30
    ai1, ai2 = Ref{Float64}(), Ref{Float64}()
276×
31
    ae1, ae2 = Ref{Int32}(), Ref{Int32}()
276×
32

33
    ccall((:zairy_,libopenspecfun), Cvoid,
276×
34
          (Ref{Float64}, Ref{Float64}, Ref{Int32}, Ref{Int32},
35
           Ref{Float64}, Ref{Float64}, Ref{Int32}, Ref{Int32}),
36
           real(z), imag(z), id, kode,
37
           ai1, ai2, ae1, ae2)
38

39
    if ae2[] == 0 || ae2[] == 3 # ignore underflow and less than half machine accuracy loss
280×
40
        return complex(ai1[], ai2[])
272×
41
    else
42
        throw(AmosException(ae2[]))
4×
43
    end
44
end
45

46
function _biry(z::Complex{Float64}, id::Int32, kode::Int32)
47
    ai1, ai2 = Ref{Float64}(), Ref{Float64}()
324×
48
    ae1 = Ref{Int32}()
324×
49

50
    ccall((:zbiry_,libopenspecfun), Cvoid,
324×
51
          (Ref{Float64}, Ref{Float64}, Ref{Int32}, Ref{Int32},
52
           Ref{Float64}, Ref{Float64}, Ref{Int32}),
53
           real(z), imag(z), id, kode,
54
           ai1, ai2, ae1)
55

56
    if ae1[] == 0 || ae1[] == 3 # ignore less than half machine accuracy loss
328×
57
        return complex(ai1[], ai2[])
320×
58
    else
59
        throw(AmosException(ae1[]))
4×
60
    end
61
end
62

63

64
"""
65
    airyai(x)
66

67
Airy function of the first kind ``\\operatorname{Ai}(x)``.
68

69
External links: [DLMF](https://dlmf.nist.gov/9.2), [Wikipedia](https://en.wikipedia.org/wiki/Airy_function)
70

71
See also: [`airyaix`](@ref), [`airyaiprime`](@ref), [`airybi`](@ref)
72
"""
73
function airyai end
74
airyai(z::Complex{Float64}) = _airy(z, Int32(0), Int32(1))
96×
75

76
"""
77
    airyaiprime(x)
78

79
Derivative of the Airy function of the first kind ``\\operatorname{Ai}'(x)``.
80

81
External links: [DLMF](https://dlmf.nist.gov/9.2), [Wikipedia](https://en.wikipedia.org/wiki/Airy_function)
82

83
See also: [`airyaiprimex`](@ref), [`airyai`](@ref), [`airybi`](@ref)
84
"""
85
function airyaiprime end
86
airyaiprime(z::Complex{Float64}) =  _airy(z, Int32(1), Int32(1))
92×
87

88
"""
89
    airybi(x)
90

91
Airy function of the second kind ``\\operatorname{Bi}(x)``.
92

93
External links: [DLMF](https://dlmf.nist.gov/9.2), [Wikipedia](https://en.wikipedia.org/wiki/Airy_function)
94

95
See also: [`airybix`](@ref), [`airybiprime`](@ref),  [`airyai`](@ref)
96
"""
97
function airybi end
98
airybi(z::Complex{Float64}) = _biry(z, Int32(0), Int32(1))
96×
99

100
"""
101
    airybiprime(x)
102

103
Derivative of the Airy function of the second kind ``\\operatorname{Bi}'(x)``.
104

105
External links: [DLMF](https://dlmf.nist.gov/9.2), [Wikipedia](https://en.wikipedia.org/wiki/Airy_function)
106

107
See also: [`airybiprimex`](@ref), [`airybi`](@ref), [`airyai`](@ref)
108
"""
109
function airybiprime end
110
airybiprime(z::Complex{Float64}) = _biry(z, Int32(1), Int32(1))
92×
111

112
"""
113
    airyaix(x)
114

115
Scaled Airy function of the first kind ``\\operatorname{Ai}(x) e^{\\frac{2}{3} x
116
\\sqrt{x}}``.  Throws `DomainError` for negative `Real` arguments.
117

118
External links: [DLMF](https://dlmf.nist.gov/9.2), [Wikipedia](https://en.wikipedia.org/wiki/Airy_function)
119

120
See also: [`airyai`](@ref), [`airyaiprime`](@ref), [`airybi`](@ref)
121
"""
122
function airyaix end
123
airyaix(z::Complex{Float64}) = _airy(z, Int32(0), Int32(2))
44×
124

125
"""
126
    airyaiprimex(x)
127

128
Scaled derivative of the Airy function of the first kind ``\\operatorname{Ai}'(x)
129
e^{\\frac{2}{3} x \\sqrt{x}}``.  Throws `DomainError` for negative `Real` arguments.
130

131
External links: [DLMF](https://dlmf.nist.gov/9.2), [Wikipedia](https://en.wikipedia.org/wiki/Airy_function)
132

133
See also: [`airyaiprime`](@ref), [`airyai`](@ref), [`airybi`](@ref)
134
"""
135
function airyaiprimex end
136
airyaiprimex(z::Complex{Float64}) =  _airy(z, Int32(1), Int32(2))
44×
137

138
"""
139
    airybix(x)
140

141
Scaled Airy function of the second kind ``\\operatorname{Bi}(x) e^{- \\left| \\operatorname{Re} \\left( \\frac{2}{3} x \\sqrt{x} \\right) \\right|}``.
142

143
External links: [DLMF](https://dlmf.nist.gov/9.2), [Wikipedia](https://en.wikipedia.org/wiki/Airy_function)
144

145
See also: [`airybi`](@ref), [`airybiprime`](@ref), [`airyai`](@ref)
146
"""
147
function airybix end
148
airybix(z::Complex{Float64}) = _biry(z, Int32(0), Int32(2))
68×
149

150
"""
151
    airybiprimex(x)
152

153
Scaled derivative of the Airy function of the second kind ``\\operatorname{Bi}'(x) e^{- \\left| \\operatorname{Re} \\left( \\frac{2}{3} x \\sqrt{x} \\right) \\right|}``.
154

155
External links: [DLMF](https://dlmf.nist.gov/9.2), [Wikipedia](https://en.wikipedia.org/wiki/Airy_function)
156

157
See also: [`airybiprime`](@ref), [`airybi`](@ref), [`airyai`](@ref)
158
"""
159
function airybiprimex end
160
airybiprimex(z::Complex{Float64}) = _biry(z, Int32(1), Int32(2))
68×
161

162
for afn in (:airyai, :airyaiprime, :airybi, :airybiprime,
163
            :airyaix, :airyaiprimex, :airybix, :airybiprimex)
164
    @eval begin
165
        $afn(z::Complex) = $afn(float(z))
204×
166
        $afn(z::Complex{Float16}) = Complex{Float16}($afn(Complex{Float32}(z)))
64×
167
        $afn(z::Complex{Float32}) = Complex{Float32}($afn(Complex{Float64}(z)))
128×
168
        $afn(z::Complex{<:AbstractFloat}) = throw(MethodError($afn,(z,)))
4×
169
    end
170
    if afn in (:airyaix, :airyaiprimex)
171
        @eval $afn(x::Real) = x < 0 ? throw(DomainError(x, "`x` must be nonnegative.")) : real($afn(complex(float(x))))
56×
172
    else
173
        @eval $afn(x::Real) = real($afn(complex(float(x))))
220×
174
    end
175
end
176

UNCOV
177
function airyai(x::BigFloat)
!
178
    z = BigFloat()
!
179
    ccall((:mpfr_ai, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Int32), z, x, ROUNDING_MODE[])
!
180
    return z
!
181
end
182

183
## Bessel functions
184

185
# besselj0, besselj1, bessely0, bessely1
186
for jy in ("j","y"), nu in (0,1)
187
    jynu = Expr(:quote, Symbol(jy,nu))
188
    jynuf = Expr(:quote, Symbol(jy,nu,"f"))
189
    bjynu = Symbol("bessel",jy,nu)
190
    if jy == "y"
191
        @eval begin
192
            $bjynu(x::Float64) = nan_dom_err(ccall(($jynu,libm),  Float64, (Float64,), x), x)
40×
193
            $bjynu(x::Float32) = nan_dom_err(ccall(($jynuf,libm), Float32, (Float32,), x), x)
16×
194
            $bjynu(x::Float16) = Float16($bjynu(Float32(x)))
8×
195
        end
196
    else
197
        @eval begin
198
            $bjynu(x::Float64) = ccall(($jynu,libm),  Float64, (Float64,), x)
40×
199
            $bjynu(x::Float32) = ccall(($jynuf,libm), Float32, (Float32,), x)
16×
200
            $bjynu(x::Float16) = Float16($bjynu(Float32(x)))
8×
201
        end
202
    end
203
    @eval begin
204
        $bjynu(x::Real) = $bjynu(float(x))
16×
205
        $bjynu(x::Complex) = $(Symbol("bessel",jy))($nu,x)
16×
206
    end
207
end
208

209

210
function _besselh(nu::Float64, k::Int32, z::Complex{Float64}, kode::Int32)
211
    ai1, ai2 = Ref{Float64}(), Ref{Float64}()
472×
212
    ae1, ae2 = Ref{Int32}(), Ref{Int32}()
472×
213

214
    ccall((:zbesh_,libopenspecfun), Cvoid,
472×
215
           (Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Int32}, Ref{Int32}, Ref{Int},
216
            Ref{Float64}, Ref{Float64}, Ref{Int32}, Ref{Int32}),
217
            real(z), imag(z), nu, kode, k, 1,
218
            ai1, ai2, ae1, ae2)
219

220
    if ae2[] == 0 || ae2[] == 3
480×
221
        return complex(ai1[], ai2[])
464×
222
    else
223
        throw(AmosException(ae2[]))
8×
224
    end
225
end
226

227
function _besseli(nu::Float64, z::Complex{Float64}, kode::Int32)
228
    ai1, ai2 = Ref{Float64}(), Ref{Float64}()
540×
229
    ae1, ae2 = Ref{Int32}(), Ref{Int32}()
540×
230

231
    ccall((:zbesi_,libopenspecfun), Cvoid,
540×
232
          (Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Int32}, Ref{Int32},
233
           Ref{Float64}, Ref{Float64}, Ref{Int32}, Ref{Int32}),
234
           real(z), imag(z), nu, kode, 1,
235
           ai1, ai2, ae1, ae2)
236

237
    if ae2[] == 0 || ae2[] == 3
544×
238
        return complex(ai1[], ai2[])
536×
239
    else
240
        throw(AmosException(ae2[]))
4×
241
    end
242
end
243

244
function _besselj(nu::Float64, z::Complex{Float64}, kode::Int32)
245
    ai1, ai2 = Ref{Float64}(), Ref{Float64}()
552×
246
    ae1, ae2 = Ref{Int32}(), Ref{Int32}()
552×
247

248
    ccall((:zbesj_,libopenspecfun), Cvoid,
552×
249
          (Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Int32}, Ref{Int32},
250
           Ref{Float64}, Ref{Float64}, Ref{Int32}, Ref{Int32}),
251
           real(z), imag(z), nu, kode, 1,
252
           ai1, ai2, ae1, ae2)
253

254
    if ae2[] == 0 || ae2[] == 3
556×
255
        return complex(ai1[], ai2[])
548×
256
    else
257
        throw(AmosException(ae2[]))
4×
258
    end
259
end
260

261
function _besselk(nu::Float64, z::Complex{Float64}, kode::Int32)
262
    ai1, ai2 = Ref{Float64}(), Ref{Float64}()
284×
263
    ae1, ae2 = Ref{Int32}(), Ref{Int32}()
284×
264

265
    ccall((:zbesk_,libopenspecfun), Cvoid,
284×
266
          (Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Int32}, Ref{Int32},
267
           Ref{Float64}, Ref{Float64}, Ref{Int32}, Ref{Int32}),
268
           real(z), imag(z), nu, kode, 1,
269
           ai1, ai2, ae1, ae2)
270

271
    if ae2[] == 0 || ae2[] == 3
292×
272
        return complex(ai1[], ai2[])
276×
273
    else
274
        throw(AmosException(ae2[]))
8×
275
    end
276
end
277

278
function _bessely(nu::Float64, z::Complex{Float64}, kode::Int32)
279
    ai1, ai2 = Ref{Float64}(), Ref{Float64}()
288×
280
    ae1, ae2 = Ref{Int32}(), Ref{Int32}()
288×
281
    wrk1, wrk2 = Ref{Float64}(), Ref{Float64}()
288×
282

283
    ccall((:zbesy_,libopenspecfun), Cvoid,
288×
284
          (Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Int32}, Ref{Int32},
285
           Ref{Float64}, Ref{Float64}, Ref{Int32}, Ref{Float64}, Ref{Float64}, Ref{Int32}),
286
           real(z), imag(z), nu, kode, 1,
287
           ai1, ai2, ae1, wrk1, wrk2, ae2)
288

289
    if ae2[] == 0 || ae2[] == 3
304×
290
        return complex(ai1[], ai2[])
272×
291
    else
292
        throw(AmosException(ae2[]))
16×
293
    end
294
end
295

296
"""
297
    besselh(nu, [k=1,] x)
298

299
Bessel function of the third kind of order `nu` (the Hankel function). `k` is either 1 or 2,
300
selecting [`hankelh1`](@ref) or [`hankelh2`](@ref), respectively.
301
`k` defaults to 1 if it is omitted.
302

303
External links: [DLMF](https://dlmf.nist.gov/10.2.5) and [DLMF](https://dlmf.nist.gov/10.2.6), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Hankel_functions:_H(1)%CE%B1,_H(2)%CE%B1)
304

305
See also: [`besselhx`](@ref) for an exponentially scaled variant.
306
"""
307
function besselh end
308

309
function besselh(nu::Float64, k::Integer, z::Complex{Float64})
310
    if nu < 0
216×
311
        s = (k == 1) ? 1 : -1
64×
312
        return _besselh(-nu,Int32(k),z,Int32(1)) * complex(cospi(nu),-s*sinpi(nu))
64×
313
    end
314
    return _besselh(nu,Int32(k),z,Int32(1))
152×
315
end
316

317
function besselh(nu::Float64, k::Integer, x::Float64)
318
    # Given that x is real, Jnu(x) and Ynu(x) are also real.
319
    if k == 1
81×
320
        return complex(besselj(nu, x), bessely(nu, x))
46×
321
    elseif k == 2
35×
322
        return complex(besselj(nu, x), -bessely(nu, x))
31×
323
    else
324
        # We emulate ZBESH's behaviour
325
        throw(AmosException(1))
4×
326
    end
327
end
328

329
"""
330
    besselhx(nu, [k=1,] z)
331

332
Compute the scaled Hankel function ``\\exp(∓iz) H_ν^{(k)}(z)``, where
333
``k`` is 1 or 2, ``H_ν^{(k)}(z)`` is `besselh(nu, k, z)`, and ``∓`` is
334
``-`` for ``k=1`` and ``+`` for ``k=2``.  `k` defaults to 1 if it is omitted.
335

336
The reason for this function is that ``H_ν^{(k)}(z)`` is asymptotically
337
proportional to ``\\exp(∓iz)/\\sqrt{z}`` for large ``|z|``, and so the
338
[`besselh`](@ref) function is susceptible to overflow or underflow
339
when `z` has a large imaginary part.  The `besselhx` function cancels this
340
exponential factor (analytically), so it avoids these problems.
341

342
External links: [DLMF](https://dlmf.nist.gov/10.2), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Hankel_functions:_H(1)%CE%B1,_H(2)%CE%B1)
343

344
See also: [`besselh`](@ref)
345
"""
346
function besselhx end
347

348
function besselhx(nu::Float64, k::Integer, z::Complex{Float64})
349
    if nu < 0
256×
350
        s = (k == 1) ? 1 : -1
64×
351
        return _besselh(-nu,Int32(k),z,Int32(2)) * complex(cospi(nu),-s*sinpi(nu))
64×
352
    end
353
    return _besselh(nu,Int32(k),z,Int32(2))
192×
354
end
355

356
function besseli(nu::Float64, z::Complex{Float64})
357
    if nu < 0
296×
358
        if isinteger(nu)
112×
359
            return _besseli(-nu,z,Int32(1))
108×
360
        else
361
            return _besseli(-nu,z,Int32(1)) - 2_besselk(-nu,z,Int32(1))*sinpi(nu)/pi
4×
362
        end
363
    else
364
        return _besseli(nu,z,Int32(1))
184×
365
    end
366
end
367

368
function besselix(nu::Float64, z::Complex{Float64})
369
    if nu < 0
244×
370
        if isinteger(nu)
96×
371
            return _besseli(-nu,z,Int32(2))
92×
372
        else
373
            return _besseli(-nu,z,Int32(2)) - 2_besselk(-nu,z,Int32(2))*exp(-abs(real(z))-z)*sinpi(nu)/pi
4×
374
        end
375
    else
376
        return _besseli(nu,z,Int32(2))
148×
377
    end
378
end
379

380
function besselj(nu::Float64, z::Complex{Float64})
381
    if nu < 0
244×
382
        if isinteger(nu)
52×
383
            return _besselj(-nu,z,Int32(1))*cospi(nu)
44×
384
        else
385
            return _besselj(-nu,z,Int32(1))*cospi(nu) + _bessely(-nu,z,Int32(1))*sinpi(nu)
8×
386
        end
387
    else
388
        return _besselj(nu,z,Int32(1))
192×
389
    end
390
end
391

392
besselj(nu::Cint, x::Float64) = ccall((:jn, libm), Float64, (Cint, Float64), nu, x)
195×
393
besselj(nu::Cint, x::Float32) = ccall((:jnf, libm), Float32, (Cint, Float32), nu, x)
32×
394

395

396
function besseljx(nu::Float64, z::Complex{Float64})
397
    if nu < 0
244×
398
        if isinteger(nu)
96×
399
            return _besselj(-nu,z,Int32(2))*cospi(nu)
92×
400
        else
401
            return _besselj(-nu,z,Int32(2))*cospi(nu) + _bessely(-nu,z,Int32(2))*sinpi(nu)
4×
402
        end
403
    else
404
        return _besselj(nu,z,Int32(2))
148×
405
    end
406
end
407

408
besselk(nu::Float64, z::Complex{Float64}) = _besselk(abs(nu), z, Int32(1))
155×
409

410
besselkx(nu::Float64, z::Complex{Float64}) = _besselk(abs(nu), z, Int32(2))
103×
411

412
function bessely(nu::Cint, x::Float64)
413
    if x < 0
156×
414
        throw(DomainError(x, "`x` must be nonnegative."))
4×
415
    end
416
    ccall((:yn, libm), Float64, (Cint, Float64), nu, x)
152×
417
end
418
function bessely(nu::Cint, x::Float32)
419
    if x < 0
8×
420
        throw(DomainError(x, "`x` must be nonnegative."))
4×
421
    end
422
    ccall((:ynf, libm), Float32, (Cint, Float32), nu, x)
4×
423
end
424

425
function bessely(nu::Float64, z::Complex{Float64})
426
    if nu < 0
160×
427
        return _bessely(-nu,z,Int32(1))*cospi(nu) - _besselj(-nu,z,Int32(1))*sinpi(nu)
32×
428
    else
429
        return _bessely(nu,z,Int32(1))
128×
430
    end
431
end
432

433
function besselyx(nu::Float64, z::Complex{Float64})
434
    if nu < 0
116×
435
        return _bessely(-nu,z,Int32(2))*cospi(nu) - _besselj(-nu,z,Int32(2))*sinpi(nu)
32×
436
    else
437
        return _bessely(nu,z,Int32(2))
84×
438
    end
439
end
440

441
"""
442
    besseli(nu, x)
443

444
Modified Bessel function of the first kind of order `nu`, ``I_\\nu(x)``.
445

446
External links: [DLMF](https://dlmf.nist.gov/10.25.2), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Modified_Bessel_functions:_I%CE%B1,_K%CE%B1)
447

448
See also: [`besselix(nu,x)`](@ref SpecialFunctions.besselix), [`besselj(nu,x)`](@ref SpecialFunctions.besselj), [`besselk(nu,x)`](@ref SpecialFunctions.besselk)
449
"""
450
function besseli(nu::Real, x::AbstractFloat)
451
    if x < 0 && !isinteger(nu)
126×
452
        throw(DomainError(x, "`x` must be nonnegative and `nu` must be an integer."))
4×
453
    end
454
    real(besseli(float(nu), complex(x)))
119×
455
end
456

457
"""
458
    besselix(nu, x)
459

460
Scaled modified Bessel function of the first kind of order `nu`, ``I_\\nu(x) e^{- | \\operatorname{Re}(x) |}``.
461

462
External links: [DLMF](https://dlmf.nist.gov/10.25.2), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Modified_Bessel_functions:_I%CE%B1,_K%CE%B1)
463

464
See also: [`besseli(nu,x)`](@ref SpecialFunctions.besseli), [`besselj(nu,x)`](@ref SpecialFunctions.besselj), [`besselk(nu,x)`](@ref SpecialFunctions.besselk)
465
"""
466
function besselix(nu::Real, x::AbstractFloat)
467
    if x < 0 && !isinteger(nu)
94×
468
        throw(DomainError(x, "`x` must be nonnegative and `nu` must be an integer."))
4×
469
    end
470
    real(besselix(float(nu), complex(x)))
87×
471
end
472

473
"""
474
    besselj(nu, x)
475

476
Bessel function of the first kind of order `nu`, ``J_\\nu(x)``.
477

478
External links: [DLMF](https://dlmf.nist.gov/10.2.2), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Bessel_functions_of_the_first_kind:_J%CE%B1)
479

480
See also: [`besseljx(nu,x)`](@ref SpecialFunctions.besseljx), [`besseli(nu,x)`](@ref SpecialFunctions.besseli), [`besselk(nu,x)`](@ref SpecialFunctions.besselk)
481
"""
482
function besselj(nu::Real, x::AbstractFloat)
483
    if isinteger(nu)
275×
484
        if typemin(Cint) <= nu <= typemax(Cint)
227×
485
            return besselj(Cint(nu), x)
227×
486
        end
487
    elseif x < 0
51×
488
        throw(DomainError(x, "`x` must be nonnegative."))
8×
489
    end
490
    real(besselj(float(nu), complex(x)))
40×
491
end
492

493
"""
494
    besseljx(nu, x)
495

496
Scaled Bessel function of the first kind of order `nu`, ``J_\\nu(x) e^{- | \\operatorname{Im}(x) |}``.
497

498
External links: [DLMF](https://dlmf.nist.gov/10.2.2), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Bessel_functions_of_the_first_kind:_J%CE%B1)
499

500
See also: [`besselj(nu,x)`](@ref SpecialFunctions.besselj), [`besseli(nu,x)`](@ref SpecialFunctions.besseli), [`besselk(nu,x)`](@ref SpecialFunctions.besselk)
501
"""
502
function besseljx(nu::Real, x::AbstractFloat)
503
    if x < 0 && !isinteger(nu)
94×
504
        throw(DomainError(x, "`x` must be nonnegative and `nu` must be an integer."))
4×
505
    end
506
    real(besseljx(float(nu), complex(x)))
87×
507
end
508

509
"""
510
    besselk(nu, x)
511

512
Modified Bessel function of the second kind of order `nu`, ``K_\\nu(x)``.
513

514
External links: [DLMF](https://dlmf.nist.gov/10.25.3), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Modified_Bessel_functions:_I%CE%B1,_K%CE%B1)
515

516
See also: See also: [`besselkx(nu,x)`](@ref SpecialFunctions.besselkx), [`besseli(nu,x)`](@ref SpecialFunctions.besseli), [`besselj(nu,x)`](@ref SpecialFunctions.besselj)
517
"""
518
function besselk(nu::Real, x::AbstractFloat)
519
    if x < 0
54×
520
        throw(DomainError(x, "`x` must be nonnegative."))
4×
521
    elseif x == 0
50×
522
        return oftype(x, Inf)
4×
523
    end
524
    real(besselk(float(nu), complex(x)))
43×
525
end
526

527
"""
528
    besselkx(nu, x)
529

530
Scaled modified Bessel function of the second kind of order `nu`, ``K_\\nu(x) e^x``.
531

532
External links: [DLMF](https://dlmf.nist.gov/10.25.3), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Modified_Bessel_functions:_I%CE%B1,_K%CE%B1)
533

534
See also: [`besselk(nu,x)`](@ref SpecialFunctions.besselk), [`besseli(nu,x)`](@ref SpecialFunctions.besseli), [`besselj(nu,x)`](@ref SpecialFunctions.besselj)
535
"""
536
function besselkx(nu::Real, x::AbstractFloat)
537
    if x < 0
22×
538
        throw(DomainError(x, "`x` must be nonnegative."))
4×
539
    elseif x == 0
18×
540
        return oftype(x, Inf)
4×
541
    end
542
    real(besselkx(float(nu), complex(x)))
11×
543
end
544

545
"""
546
    bessely(nu, x)
547

548
Bessel function of the second kind of order `nu`, ``Y_\\nu(x)``.
549

550
External links: [DLMF](https://dlmf.nist.gov/10.2.3), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Bessel_functions_of_the_second_kind:_Y%CE%B1)
551

552
See also [`besselyx(nu,x)`](@ref SpecialFunctions.besselyx) for a scaled variant.
553
"""
554
function bessely(nu::Real, x::AbstractFloat)
555
    if x < 0
214×
556
        throw(DomainError(x, "`x` must be nonnegative."))
20×
557
    elseif isinteger(nu) && typemin(Cint) <= nu <= typemax(Cint)
188×
558
        return bessely(Cint(nu), x)
156×
559
    end
560
    real(bessely(float(nu), complex(x)))
32×
561
end
562

563
"""
564
    besselyx(nu, x)
565

566
Scaled Bessel function of the second kind of order `nu`,
567
``Y_\\nu(x) e^{- | \\operatorname{Im}(x) |}``.
568

569
External links: [DLMF](https://dlmf.nist.gov/10.2.3), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Bessel_functions_of_the_second_kind:_Y%CE%B1)
570

571
See also [`bessely(nu,x)`](@ref SpecialFunctions.bessely)
572
"""
573
function besselyx(nu::Real, x::AbstractFloat)
574
    if x < 0
22×
575
        throw(DomainError(x, "`x` must be nonnegative."))
4×
576
    end
577
    real(besselyx(float(nu), complex(x)))
15×
578
end
579

580
for f in ("i", "ix", "j", "jx", "k", "kx", "y", "yx")
581
    bfn = Symbol("bessel", f)
582
    @eval begin
583
        $bfn(nu::Real, x::Real) = $bfn(nu, float(x))
330×
584
        function $bfn(nu::Real, z::Complex)
585
            Tf = promote_type(float(typeof(nu)),float(typeof(real(z))))
512×
586
            $bfn(Tf(nu), Complex{Tf}(z))
512×
587
        end
588
        $bfn(nu::Float16, x::Complex{Float16}) = Complex{Float16}($bfn(Float32(nu), Complex{Float32}(x)))
12×
589
        $bfn(nu::Float32, x::Complex{Float32}) = Complex{Float32}($bfn(Float64(nu), Complex{Float64}(x)))
20×
590
        $bfn(k::T, z::Complex{T}) where {T<:AbstractFloat} = throw(MethodError($bfn,(k,z)))
68×
591
    end
592
end
593

594

595
for bfn in (:besselh, :besselhx)
596
    @eval begin
597
        $bfn(nu, z) = $bfn(nu, 1, z)
23×
598
        $bfn(nu::Real, k::Integer, x::Real) = $bfn(float(nu), k, float(x))
111×
599
        $bfn(nu::AbstractFloat, k::Integer, x::AbstractFloat) = $bfn(float(nu), k, complex(x))
38×
600
        function $bfn(nu::Real, k::Integer, z::Complex)
601
            Tf = promote_type(float(typeof(nu)),float(typeof(real(z))))
72×
602
            $bfn(Tf(nu), k, Complex{Tf}(z))
72×
603
        end
604
        $bfn(nu::Float16, k::Integer, x::Complex{Float16}) = Complex{Float16}($bfn(Float32(nu), k, Complex{Float32}(x)))
!
605
        $bfn(nu::Float32, k::Integer, x::Complex{Float32}) = Complex{Float32}($bfn(Float64(nu), k, Complex{Float64}(x)))
8×
606
        $bfn(nu::T, k::Integer, z::Complex{T}) where {T<:AbstractFloat} = throw(MethodError($bfn,(nu,k,z)))
32×
607
    end
608
end
609

610
besselh(nu::Float16, k::Integer, x::Float16) = Complex{Float16}(besselh(Float32(nu), k, Float32(x)))
4×
611
besselh(nu::Float32, k::Integer, x::Float32) = Complex{Float32}(besselh(Float64(nu), k, Float64(x)))
8×
612

613
"""
614
    besselj0(x)
615

616
Bessel function of the first kind of order 0, ``J_0(x)``.
617

618
External links: [DLMF](https://dlmf.nist.gov/10.2.2), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Bessel_functions_of_the_first_kind:_J%CE%B1)
619

620
See also: [`besselj(nu,x)`](@ref SpecialFunctions.besselj)
621
"""
622
function besselj0(x::BigFloat)
623
    z = BigFloat()
4×
624
    ccall((:mpfr_j0, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Int32), z, x, ROUNDING_MODE[])
4×
625
    return z
4×
626
end
627

628
"""
629
    besselj1(x)
630

631
Bessel function of the first kind of order 1, ``J_1(x)``.
632

633
External links: [DLMF](https://dlmf.nist.gov/10.2.2), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Bessel_functions_of_the_first_kind:_J%CE%B1)
634

635
See also: [`besselj(nu,x)`](@ref SpecialFunctions.besselj)
636
"""
637
function besselj1(x::BigFloat)
638
    z = BigFloat()
4×
639
    ccall((:mpfr_j1, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Int32), z, x, ROUNDING_MODE[])
4×
640
    return z
4×
641
end
642

643
function besselj(n::Integer, x::BigFloat)
644
    z = BigFloat()
4×
645
    ccall((:mpfr_jn, :libmpfr), Int32, (Ref{BigFloat}, Clong, Ref{BigFloat}, Int32), z, n, x, ROUNDING_MODE[])
4×
646
    return z
4×
647
end
648

649
"""
650
    bessely0(x)
651

652
Bessel function of the second kind of order 0, ``Y_0(x)``.
653

654
External links: [DLMF](https://dlmf.nist.gov/10.2.3), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Bessel_functions_of_the_second_kind:_Y%CE%B1)
655

656
See also: [`bessely(nu,x)`](@ref SpecialFunctions.bessely)
657
"""
658
function bessely0(x::BigFloat)
659
    if x < 0
7×
660
        throw(DomainError(x, "`x` must be nonnegative."))
!
661
    end
662
    z = BigFloat()
4×
663
    ccall((:mpfr_y0, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Int32), z, x, ROUNDING_MODE[])
4×
664
    return z
4×
665
end
666

667
"""
668
    bessely1(x)
669

670
Bessel function of the second kind of order 1, ``Y_1(x)``.
671

672
External links: [DLMF](https://dlmf.nist.gov/10.2.3), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Bessel_functions_of_the_second_kind:_Y%CE%B1)
673

674
See also: [`bessely(nu,x)`](@ref SpecialFunctions.bessely)
675
"""
676
function bessely1(x::BigFloat)
677
    if x < 0
7×
678
        throw(DomainError(x, "`x` must be nonnegative."))
!
679
    end
680
    z = BigFloat()
4×
681
    ccall((:mpfr_y1, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Int32), z, x, ROUNDING_MODE[])
4×
682
    return z
4×
683
end
684

685
function bessely(n::Integer, x::BigFloat)
686
    if x < 0
14×
687
        throw(DomainError(x, "`x` must be nonnegative."))
4×
688
    end
689
    z = BigFloat()
4×
690
    ccall((:mpfr_yn, :libmpfr), Int32, (Ref{BigFloat}, Clong, Ref{BigFloat}, Int32), z, n, x, ROUNDING_MODE[])
4×
691
    return z
4×
692
end
693

694
"""
695
    sphericalbesselj(nu, x)
696

697
Spherical bessel function of the first kind at order `nu`, ``j_ν(x)``. This is the non-singular
698
solution to the radial part of the Helmholz equation in spherical coordinates.
699
"""
700
function sphericalbesselj(nu, x::T) where {T}
701
    besselj_nuhalf_x = besselj(nu + one(nu)/2, x)
36×
702
    if abs(x) ≤ sqrt(eps(real(zero(besselj_nuhalf_x))))
33×
703
        nu == 0 ? one(besselj_nuhalf_x) : zero(besselj_nuhalf_x)
8×
704
    else
705
        √((float(T))(π)/2x) * besselj_nuhalf_x
24×
706
    end
707
end
708

709
"""
710
    sphericalbessely(nu, x)
711

712
Spherical bessel function of the second kind at order `nu`, ``y_ν(x)``. This is the singular
713
solution to the radial part of the Helmholz equation in spherical coordinates. Sometimes
714
known as a spherical Neumann function.
715
"""
716
sphericalbessely(nu, x::T) where {T} = √((float(T))(π)/2x) * bessely(nu + one(nu)/2, x)
32×
717

718
"""
719
    hankelh1(nu, x)
720

721
Bessel function of the third kind of order `nu`, ``H^{(1)}_\\nu(x)``.
722

723
External links: [DLMF](https://dlmf.nist.gov/10.2.5), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Hankel_functions:_H(1)%CE%B1,_H(2)%CE%B1)
724

725
See also: [`hankelh1x`](@ref SpecialFunctions.hankelh1x)
726
"""
727
hankelh1(nu, z) = besselh(nu, 1, z)
127×
728

729
"""
730
    hankelh2(nu, x)
731

732
Bessel function of the third kind of order `nu`, ``H^{(2)}_\\nu(x)``.
733

734
External links: [DLMF](https://dlmf.nist.gov/10.2.6), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Hankel_functions:_H(1)%CE%B1,_H(2)%CE%B1)
735

736
See also: [`hankelh2x(nu,x)`](@ref SpecialFunctions.hankelh2x)
737
"""
738
hankelh2(nu, z) = besselh(nu, 2, z)
127×
739

740
"""
741
    hankelh1x(nu, x)
742

743
Scaled Bessel function of the third kind of order `nu`, ``H^{(1)}_\\nu(x) e^{-x i}``.
744

745
External links: [DLMF](https://dlmf.nist.gov/10.2.5), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Hankel_functions:_H(1)%CE%B1,_H(2)%CE%B1)
746

747
See also: [`hankelh1`](@ref SpecialFunctions.hankelh1)
748
"""
749
hankelh1x(nu, z) = besselhx(nu, 1, z)
107×
750

751
"""
752
    hankelh2x(nu, x)
753

754
Scaled Bessel function of the third kind of order `nu`, ``H^{(2)}_\\nu(x) e^{x i}``.
755

756
External links: [DLMF](https://dlmf.nist.gov/10.2.6), [Wikipedia](https://en.wikipedia.org/wiki/Bessel_function#Hankel_functions:_H(1)%CE%B1,_H(2)%CE%B1)
757

758
See also: [`hankelh2(nu,x)`](@ref SpecialFunctions.hankelh2)
759
"""
760
hankelh2x(nu, z) = besselhx(nu, 2, z)
107×
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc