Skip to content

Commit fdcfa40

Browse files
committed
use sincos
1 parent 38f9a54 commit fdcfa40

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/besselj.jl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ function besselj0(x::T) where T
4242
p = (z - DR1) * (z - DR2)
4343
p = p * evalpoly(z, RP_j0(T)) / evalpoly(z, RQ_j0(T))
4444
return p
45-
elseif x < 80.0
45+
elseif x < 75.0
4646
w = 5.0 / x
4747
q = 25.0 / (x * x)
4848

4949
p = evalpoly(q, PP_j0(T)) / evalpoly(q, PQ_j0(T))
5050
q = evalpoly(q, QP_j0(T)) / evalpoly(q, QQ_j0(T))
5151
xn = x - PIO4(T)
52-
p = p * cos(xn) - w * q * sin(xn)
52+
sc = sincos(xn)
53+
p = p * sc[2] - w * q * sc[1]
5354
return p * SQ2OPI(T) / sqrt(x)
5455
else
5556
xinv = inv(x)
@@ -61,8 +62,13 @@ function besselj0(x::T) where T
6162

6263
q = (-1/8, 25/384, -1073/5120, 375733/229376, -55384775/2359296)
6364
xn = muladd(xinv, evalpoly(x2, q), - PIO4(T))
64-
b = cos(x)*cos(xn) - sin(x)*sin(xn)
65-
#b = cos(x + xn)
65+
66+
# the following computes b = cos(x + xn)
67+
# but uses cos(x)*cos(xn) - sin(x)*sin(xn)
68+
# to improve accuracy when x >> xn
69+
c1 = sincos(x)
70+
c2 = sincos(xn)
71+
b = c1[2] * c2[2] - c1[1] * c2[1]
6672
return a * b
6773
end
6874
end
@@ -120,7 +126,13 @@ function besselj1(x::Float64)
120126

121127
q = (3/8, -21/128, 1899/5120, -543483/229376, 8027901/262144)
122128
xn = muladd(xinv, evalpoly(x2, q), - 3 * PIO4(T))
123-
b = cos(x + xn)
129+
130+
# the following computes b = cos(x + xn)
131+
# but uses cos(x)*cos(xn) - sin(x)*sin(xn)
132+
# to improve accuracy when x >> xn
133+
c1 = sincos(x)
134+
c2 = sincos(xn)
135+
b = c1[2] * c2[2] - c1[1] * c2[1]
124136
return a * b
125137
end
126138
end

src/bessely.jl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function _bessely0_compute(x::Float64)
2323
w = evalpoly(z, YP_y0(T)) / evalpoly(z, YQ_y0(T))
2424
w += TWOOPI(T) * log(x) * besselj0(x)
2525
return w
26-
elseif x < 100.0
26+
elseif x < 75.0
2727
w = T(5) / x
2828
z = w*w
2929
p = evalpoly(z, PP_y0(T)) / evalpoly(z, PQ_y0(T))
@@ -41,7 +41,13 @@ function _bessely0_compute(x::Float64)
4141

4242
q = (-1/8, 25/384, -1073/5120, 375733/229376, -55384775/2359296)
4343
xn = muladd(xinv, evalpoly(x2, q), - PIO4(T))
44-
b = sin(x + xn)
44+
45+
# the following computes b = sin(x + xn)
46+
# but uses cos(x)*sin(xn) + sin(x)*cos(xn)
47+
# to improve accuracy when x >> xn
48+
c1 = sincos(x)
49+
c2 = sincos(xn)
50+
b = c1[2] * c2[1] + c1[1] * c2[2]
4551
return a * b
4652
end
4753
end
@@ -101,7 +107,13 @@ function _bessely1_compute(x::Float64)
101107

102108
q = (3/8, -21/128, 1899/5120, -543483/229376, 8027901/262144)
103109
xn = muladd(xinv, evalpoly(x2, q), - 3 * PIO4(T))
104-
b = sin(x + xn)
110+
111+
# the following computes b = sin(x + xn)
112+
# but uses cos(x)*sin(xn) + sin(x)*cos(xn)
113+
# to improve accuracy when x >> xn
114+
c1 = sincos(x)
115+
c2 = sincos(xn)
116+
b = c1[2] * c2[1] + c1[1] * c2[2]
105117
return a * b
106118
end
107119
end

0 commit comments

Comments
 (0)