Skip to content

Commit 0527481

Browse files
committed
update docs and bessely0,1
1 parent 1d3bb36 commit 0527481

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

src/besselj.jl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
# Polynomial coefficients are from [1] which is based on [2]
1111
# For tiny arugments the power series expansion is used.
1212
#
13-
# Branch 2: 5.0 < x < 75.0
13+
# Branch 2: 5.0 < x < 25.0
1414
# besselj0 = sqrt(2/(pi*x))*(cos(x - pi/4)*R7(x) - sin(x - pi/4)*R8(x))
1515
# Hankel's asymptotic expansion is used
1616
# where R7 and R8 are rational functions (Pn(x)/Qn(x)) of degree 7 and 8 respectively
1717
# See section 4 of [3] for more details and [1] for coefficients of polynomials
1818
#
19-
# Branch 3: x >= 75.0
19+
# Branch 3: x >= 25.0
2020
# besselj0 = sqrt(2/(pi*x))*beta(x)*(cos(x - pi/4 - alpha(x))
2121
# See modified expansions given in [3]. Exact coefficients are used
2222
#
@@ -28,6 +28,12 @@
2828
# [3] Harrison, John. "Fast and accurate Bessel function computation."
2929
# 2009 19th IEEE Symposium on Computer Arithmetic. IEEE, 2009.
3030
#
31+
32+
"""
33+
besselj0(x::T) where T <: Union{Float32, Float64}
34+
35+
Bessel function of the first kind of order zero, ``J_0(x)``.
36+
"""
3137
function besselj0(x::Float64)
3238
T = Float64
3339
x = abs(x)
@@ -54,12 +60,12 @@ function besselj0(x::Float64)
5460
p = p * sc[2] - w * q * sc[1]
5561
return p * SQ2OPI(T) / sqrt(x)
5662
else
57-
if x < 75.0
63+
if x < 120.0
5864
p = (one(T), -1/16, 53/512, -4447/8192, 3066403/524288, -896631415/8388608, 796754802993/268435456, -500528959023471/4294967296)
5965
q = (-1/8, 25/384, -1073/5120, 375733/229376, -55384775/2359296, 24713030909/46137344, -7780757249041/436207616)
6066
else
61-
p = (one(T), -1/16, 53/512, -4447/8192, 3066403/524288)
62-
q = (-1/8, 25/384, -1073/5120, 375733/229376, -55384775/2359296)
67+
p = (one(T), -1/16, 53/512, -4447/8192)
68+
q = (-1/8, 25/384, -1073/5120, 375733/229376)
6369
end
6470
xinv = inv(x)
6571
x2 = xinv*xinv
@@ -74,8 +80,6 @@ function besselj0(x::Float64)
7480
return a * b
7581
end
7682
end
77-
78-
7983
function besselj0(x::Float32)
8084
T = Float32
8185
x = abs(x)
@@ -100,6 +104,11 @@ function besselj0(x::Float32)
100104
end
101105
end
102106

107+
"""
108+
besselj1(x::T) where T <: Union{Float32, Float64}
109+
110+
Bessel function of the first kind of order one, ``J_1(x)``.
111+
"""
103112
function besselj1(x::Float64)
104113
T = Float64
105114
x = abs(x)

src/bessely.jl

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
# Polynomial coefficients are from [1] which is based on [2].
1111
# For tiny arugments the power series expansion is used.
1212
#
13-
# Branch 2: 5.0 < x < 75.0
13+
# Branch 2: 5.0 < x < 25.0
1414
# bessely0 = sqrt(2/(pi*x))*(sin(x - pi/4)*R7(x) - cos(x - pi/4)*R8(x))
1515
# Hankel's asymptotic expansion is used
1616
# where R7 and R8 are rational functions (Pn(x)/Qn(x)) of degree 7 and 8 respectively
1717
# See section 4 of [3] for more details and [1] for coefficients of polynomials
1818
#
19-
# Branch 3: x >= 75.0
19+
# Branch 3: x >= 25.0
2020
# bessely0 = sqrt(2/(pi*x))*beta(x)*(sin(x - pi/4 - alpha(x))
2121
# See modified expansions given in [3]. Exact coefficients are used.
2222
#
@@ -28,6 +28,12 @@
2828
# [3] Harrison, John. "Fast and accurate Bessel function computation."
2929
# 2009 19th IEEE Symposium on Computer Arithmetic. IEEE, 2009.
3030
#
31+
32+
"""
33+
bessely0(x::T) where T <: Union{Float32, Float64}
34+
35+
Bessel function of the second kind of order zero, ``Y_0(x)``.
36+
"""
3137
function bessely0(x::T) where T <: Union{Float32, Float64}
3238
if x <= zero(x)
3339
if iszero(x)
@@ -42,29 +48,33 @@ function bessely0(x::T) where T <: Union{Float32, Float64}
4248
end
4349
function _bessely0_compute(x::Float64)
4450
T = Float64
45-
if x <= 5
51+
if x <= 5.0
4652
z = x * x
4753
w = evalpoly(z, YP_y0(T)) / evalpoly(z, YQ_y0(T))
4854
w += TWOOPI(T) * log(x) * besselj0(x)
4955
return w
50-
elseif x < 75.0
51-
w = T(5) / x
52-
z = w*w
56+
elseif x < 25.0
57+
w = 5.0 / x
58+
z = w * w
5359
p = evalpoly(z, PP_y0(T)) / evalpoly(z, PQ_y0(T))
5460
q = evalpoly(z, QP_y0(T)) / evalpoly(z, QQ_y0(T))
5561
xn = x - PIO4(T)
5662
sc = sincos(xn)
5763
p = p * sc[1] + w * q * sc[2]
5864
return p * SQ2OPI(T) / sqrt(x)
5965
else
66+
if x < 120.0
67+
p = (one(T), -1/16, 53/512, -4447/8192, 3066403/524288, -896631415/8388608, 796754802993/268435456, -500528959023471/4294967296)
68+
q = (-1/8, 25/384, -1073/5120, 375733/229376, -55384775/2359296, 24713030909/46137344, -7780757249041/436207616)
69+
else
70+
p = (one(T), -1/16, 53/512, -4447/8192)
71+
q = (-1/8, 25/384, -1073/5120, 375733/229376)
72+
end
6073
xinv = inv(x)
6174
x2 = xinv*xinv
6275

63-
p = (one(T), -1/16, 53/512, -4447/8192, 5066403/524288)
6476
p = evalpoly(x2, p)
6577
a = SQ2OPI(T) * sqrt(xinv) * p
66-
67-
q = (-1/8, 25/384, -1073/5120, 375733/229376, -55384775/2359296)
6878
xn = muladd(xinv, evalpoly(x2, q), - PIO4(T))
6979

7080
# the following computes b = sin(x + xn) more accurately
@@ -91,6 +101,12 @@ function _bessely0_compute(x::Float32)
91101
return p
92102
end
93103
end
104+
105+
"""
106+
bessely1(x::T) where T <: Union{Float32, Float64}
107+
108+
Bessel function of the second kind of order one, ``Y_1(x)``.
109+
"""
94110
function bessely1(x::T) where T <: Union{Float32, Float64}
95111
if x <= zero(x)
96112
if iszero(x)
@@ -103,16 +119,15 @@ function bessely1(x::T) where T <: Union{Float32, Float64}
103119
end
104120
return _bessely1_compute(x)
105121
end
106-
107122
function _bessely1_compute(x::Float64)
108123
T = Float64
109124
if x <= 5
110125
z = x * x
111126
w = x * (evalpoly(z, YP_y1(T)) / evalpoly(z, YQ_y1(T)))
112127
w += TWOOPI(T) * (besselj1(x) * log(x) - inv(x))
113128
return w
114-
elseif x < 75.0
115-
w = T(5) / x
129+
elseif x < 25.0
130+
w = 5.0 / x
116131
z = w * w
117132
p = evalpoly(z, PP_j1(T)) / evalpoly(z, PQ_j1(T))
118133
q = evalpoly(z, QP_j1(T)) / evalpoly(z, QQ_j1(T))
@@ -121,14 +136,18 @@ function _bessely1_compute(x::Float64)
121136
p = p * sc[1] + w * q * sc[2]
122137
return p * SQ2OPI(T) / sqrt(x)
123138
else
139+
if x < 120.0
140+
p = (one(T), 3/16, -99/512, 6597/8192, -4057965/524288, 1113686901/8388608, -951148335159/268435456, 581513783771781/4294967296)
141+
q = (3/8, -21/128, 1899/5120, -543483/229376, 8027901/262144, -30413055339/46137344, 9228545313147/436207616)
142+
else
143+
p = (one(T), 3/16, -99/512, 6597/8192)
144+
q = (3/8, -21/128, 1899/5120, -543483/229376)
145+
end
124146
xinv = inv(x)
125147
x2 = xinv*xinv
126148

127-
p = (one(T), 3/16, -99/512, 6597/8192, -4057965/524288)
128149
p = evalpoly(x2, p)
129150
a = SQ2OPI(T) * sqrt(xinv) * p
130-
131-
q = (3/8, -21/128, 1899/5120, -543483/229376, 8027901/262144)
132151
xn = muladd(xinv, evalpoly(x2, q), - 3 * PIO4(T))
133152

134153
# the following computes b = sin(x + xn) more accurately
@@ -137,7 +156,6 @@ function _bessely1_compute(x::Float64)
137156
return a * b
138157
end
139158
end
140-
141159
function _bessely1_compute(x::Float32)
142160
T = Float32
143161
if x <= 2.0f0

0 commit comments

Comments
 (0)