Skip to content

Commit 69f936b

Browse files
committed
add besselk0, besseli0, cheb
1 parent d37b06b commit 69f936b

File tree

6 files changed

+202
-18
lines changed

6 files changed

+202
-18
lines changed

src/constants.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ const TWOOPI(::Type{Float64}) = 0.6366197723675814
99

1010
const PIO4(::Type{Float32}) = 0.78539816339744830962f0
1111
const TWOOPI(::Type{Float32}) = 0.636619772367581343075535f0
12-
const THPIO4(::Type{Float32}) = 2.35619449019234492885f0
12+
const THPIO4(::Type{Float32}) = 2.35619449019234492885f0

src/i0.jl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
function chbevl(x, coeff)
2+
b0 = coeff[1]
3+
b1 = zero(x)
4+
b2 = zero(x)
5+
6+
for i in 2:length(coeff)
7+
b2 = b1
8+
b1 = b0
9+
b0 = x * b1 - b2 + coeff[i]
10+
end
11+
12+
return 0.5 * (b0 - b2)
13+
end
14+
15+
const A_i0 = (
16+
-4.41534164647933937950E-18, 3.33079451882223809783E-17, -2.43127984654795469359E-16,
17+
1.71539128555513303061E-15, -1.16853328779934516808E-14, 7.67618549860493561688E-14,
18+
-4.85644678311192946090E-13, 2.95505266312963983461E-12, -1.72682629144155570723E-11,
19+
9.67580903537323691224E-11, -5.18979560163526290666E-10, 2.65982372468238665035E-9,
20+
-1.30002500998624804212E-8, 6.04699502254191894932E-8, -2.67079385394061173391E-7,
21+
1.11738753912010371815E-6, -4.41673835845875056359E-6, 1.64484480707288970893E-5,
22+
-5.75419501008210370398E-5, 1.88502885095841655729E-4, -5.76375574538582365885E-4,
23+
1.63947561694133579842E-3, -4.32430999505057594430E-3, 1.05464603945949983183E-2,
24+
-2.37374148058994688156E-2, 4.93052842396707084878E-2, -9.49010970480476444210E-2,
25+
1.71620901522208775349E-1, -3.04682672343198398683E-1, 6.76795274409476084995E-1
26+
)
27+
28+
const B_i0 = (
29+
-7.23318048787475395456E-18,
30+
-4.83050448594418207126E-18,
31+
4.46562142029675999901E-17,
32+
3.46122286769746109310E-17,
33+
-2.82762398051658348494E-16,
34+
-3.42548561967721913462E-16,
35+
1.77256013305652638360E-15,
36+
3.81168066935262242075E-15,
37+
-9.55484669882830764870E-15,
38+
-4.15056934728722208663E-14,
39+
1.54008621752140982691E-14,
40+
3.85277838274214270114E-13,
41+
7.18012445138366623367E-13,
42+
-1.79417853150680611778E-12,
43+
-1.32158118404477131188E-11,
44+
-3.14991652796324136454E-11,
45+
1.18891471078464383424E-11,
46+
4.94060238822496958910E-10,
47+
3.39623202570838634515E-9,
48+
2.26666899049817806459E-8,
49+
2.04891858946906374183E-7,
50+
2.89137052083475648297E-6,
51+
6.88975834691682398426E-5,
52+
3.36911647825569408990E-3,
53+
8.04490411014108831608E-1
54+
)
55+
56+
57+
function besseli0(x)
58+
x = abs(x)
59+
if x <= 8.0
60+
y = x / 2.0 - 2.0
61+
return exp(x) * chbevl(y, A_i0)
62+
else
63+
return exp(x) * chbevl(32.0 / x - 2.0, B_i0) / sqrt(x)
64+
end
65+
end
66+
67+
function besseli0e(x)
68+
x = abs(x)
69+
if x <= 8.0
70+
y = x / 2.0 - 2.0
71+
return chbevl(y, A_i0)
72+
else
73+
return chbevl(32.0 / x - 2.0, B_i0) / sqrt(x)
74+
end
75+
end

src/j1.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function besselj1(x::Float32)
22
x = abs(x)
33
if iszero(x)
4-
return one(x)
4+
return zero(x)
55
elseif isinf(x)
66
return zero(x)
77
end
@@ -25,7 +25,7 @@ end
2525
function besselj1(x::Float64)
2626
x = abs(x)
2727
if iszero(x)
28-
return one(x)
28+
return zero(x)
2929
elseif isinf(x)
3030
return zero(x)
3131
end

src/k0.jl

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
function besselk0(x)
2+
if x <= zero(x)
3+
return throw(DomainError(x, "NaN result for non-NaN input."))
4+
end
5+
6+
if x <= 2.0
7+
y = x * x - 2.0
8+
y = chbevl(y, A_k0) - log(0.5 * x) * besseli0(x)
9+
return y
10+
else
11+
z = 8.0 / x - 2.0
12+
y = exp(-x) * chbevl(z, B_k0) / sqrt(x)
13+
return y
14+
end
15+
end
16+
17+
function besselk0e(x)
18+
if x <= zero(x)
19+
return throw(DomainError(x, "NaN result for non-NaN input."))
20+
end
21+
if x <= 2.0
22+
y = x * x - 2.0
23+
y = chbevl(y, A_k0) - log(0.5 * x) * besseli0(x)
24+
return y * exp(x)
25+
else
26+
z = 8.0 / x - 2.0
27+
y = chbevl(z, B_k0) / sqrt(x)
28+
return y
29+
end
30+
end
31+
32+
const A_k0 = (
33+
1.37446543561352307156E-16,
34+
4.25981614279661018399E-14,
35+
1.03496952576338420167E-11,
36+
1.90451637722020886025E-9,
37+
2.53479107902614945675E-7,
38+
2.28621210311945178607E-5,
39+
1.26461541144692592338E-3,
40+
3.59799365153615016266E-2,
41+
3.44289899924628486886E-1,
42+
-5.35327393233902768720E-1
43+
)
44+
45+
const B_k0 = (
46+
5.30043377268626276149E-18,
47+
-1.64758043015242134646E-17,
48+
5.21039150503902756861E-17,
49+
-1.67823109680541210385E-16,
50+
5.51205597852431940784E-16,
51+
-1.84859337734377901440E-15,
52+
6.34007647740507060557E-15,
53+
-2.22751332699166985548E-14,
54+
8.03289077536357521100E-14,
55+
-2.98009692317273043925E-13,
56+
1.14034058820847496303E-12,
57+
-4.51459788337394416547E-12,
58+
1.85594911495471785253E-11,
59+
-7.95748924447710747776E-11,
60+
3.57739728140030116597E-10,
61+
-1.69753450938905987466E-9,
62+
8.57403401741422608519E-9,
63+
-4.66048989768794782956E-8,
64+
2.76681363944501510342E-7,
65+
-1.83175552271911948767E-6,
66+
1.39498137188764993662E-5,
67+
-1.28495495816278026384E-4,
68+
1.56988388573005337491E-3,
69+
-3.14481013119645005427E-2,
70+
2.44030308206595545468E0
71+
)

test/besselj_test.jl

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,60 @@
1-
@test besselj0(0.0) 1.0
2-
1+
# general array for testing input to SpecialFunctions.jl
32
x = 0.01:0.01:100.0
43

5-
j0_SpecialFunctions = SpecialFunctions.besselj0.(big.(x))
6-
@assert j0_SpecialFunctions[1] isa BigFloat
4+
### Tests for besselj0
5+
j0_SpecialFunctions = SpecialFunctions.besselj0.(big.(x)) # array to be tested against computed in BigFloats
6+
@assert j0_SpecialFunctions[1] isa BigFloat # just double check the higher precision
77

88
j0_64 = besselj0.(Float64.(x))
99
j0_32 = besselj0.(Float32.(x))
1010
j0_big = besselj0.(big.(x))
1111

12+
# make sure output types match input types
1213
@test j0_64[1] isa Float64
1314
@test j0_32[1] isa Float32
1415
@test j0_big[1] isa BigFloat
1516

16-
17+
# test against SpecialFunctions.jl
1718
@test j0_64 j0_SpecialFunctions
1819
@test j0_32 j0_SpecialFunctions
1920

21+
# BigFloat precision only computed to 128 bits
2022
@test isapprox(j0_big, j0_SpecialFunctions, atol=1.5e-34)
2123

24+
# NaN should return NaN
25+
@test isnan(besselj0(NaN))
2226

27+
# zero should return one
28+
@test isone(besselj0(zero(Float32)))
29+
@test isone(besselj0(zero(Float64)))
30+
@test isone(besselj0(zero(BigFloat)))
2331

24-
j1_SpecialFunctions = SpecialFunctions.besselj1.(big.(x))
25-
@assert j1_SpecialFunctions[1] isa BigFloat
32+
# test that Inf inputs go to zero
33+
@test besselj0(Inf32) == zero(Float32)
34+
@test besselj0(Inf64) == zero(Float64)
35+
36+
### Tests for besselj1
37+
j1_SpecialFunctions = SpecialFunctions.besselj1.(big.(x)) # array to be tested against computed in BigFloats
38+
@assert j1_SpecialFunctions[1] isa BigFloat # just double check the higher precision
2639

2740
j1_64 = besselj1.(Float64.(x))
2841
j1_32 = besselj1.(Float32.(x))
2942

30-
31-
43+
# make sure output types match input types
3244
@test j1_64[1] isa Float64
3345
@test j1_32[1] isa Float32
3446

35-
47+
# test against SpecialFunctions.jl
3648
@test j1_64 j1_SpecialFunctions
3749
@test j1_32 j1_SpecialFunctions
3850

51+
# NaN should return NaN
52+
@test isnan(besselj1(NaN))
53+
54+
# zero should return zero
55+
@test iszero(besselj1(zero(Float32)))
56+
@test iszero(besselj1(zero(Float64)))
3957

58+
# test that Inf inputs go to zero
59+
@test besselj1(Inf32) == zero(Float32)
60+
@test besselj1(Inf64) == zero(Float64)

test/bessely_test.jl

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,66 @@
1+
# general array for testing input to SpecialFunctions.jl
12
x = 0.01:0.01:100.0
23

3-
y0_SpecialFunctions = SpecialFunctions.bessely0.(big.(x))
4-
@assert y0_SpecialFunctions[1] isa BigFloat
4+
### Tests for bessely0
5+
y0_SpecialFunctions = SpecialFunctions.bessely0.(big.(x)) # array to be tested against computed in BigFloats
6+
@assert y0_SpecialFunctions[1] isa BigFloat # just double check the higher precision
57

68
y0_64 = bessely0.(Float64.(x))
79
y0_32 = bessely0.(Float32.(x))
810
y0_big = bessely0.(big.(x))
911

12+
# make sure output types match input types
1013
@test y0_64[1] isa Float64
1114
@test y0_32[1] isa Float32
1215
@test y0_big[1] isa BigFloat
1316

17+
# test against SpecialFunctions.jl
1418
@test y0_SpecialFunctions y0_64
1519
@test y0_SpecialFunctions y0_32
1620

21+
# BigFloat precision only computed to 128 bits
1722
@test isapprox(y0_big, y0_SpecialFunctions, atol=1.5e-34)
1823

24+
# negative numbers should result in a domain error
1925
@test_throws DomainError bessely0(-1.0)
2026

27+
# NaN should return NaN
28+
@test isnan(bessely0(NaN))
29+
30+
# test that zero inputs go to -Inf
2131
@test bessely0(zero(Float32)) == -Inf32
2232
@test bessely0(zero(Float64)) == -Inf64
2333
@test bessely0(zero(BigFloat)) == -Inf
2434

35+
# test that Inf inputs go to zero
2536
@test bessely0(Inf32) == zero(Float32)
2637
@test bessely0(Inf64) == zero(Float64)
2738

39+
### tests for bessely1
2840
y1_SpecialFunctions = SpecialFunctions.bessely1.(big.(x))
2941
@assert y1_SpecialFunctions[1] isa BigFloat
3042

3143
y1_64 = bessely1.(Float64.(x))
3244
y1_32 = bessely1.(Float32.(x))
3345

34-
46+
# make sure output types match input types
3547
@test y1_64[1] isa Float64
3648
@test y1_32[1] isa Float32
3749

38-
50+
# test against SpecialFunctions.jl
3951
@test y1_64 y1_SpecialFunctions
4052
@test y1_32 y1_SpecialFunctions
4153

54+
# negative numbers should result in a domain error
4255
@test_throws DomainError bessely1(-1.0)
4356

57+
# NaN should return NaN
58+
@test isnan(bessely1(NaN))
59+
60+
# test that zero inputs go to -Inf
4461
@test bessely1(zero(Float32)) == -Inf32
4562
@test bessely1(zero(Float64)) == -Inf64
4663

64+
# test that Inf inputs go to zero
4765
@test bessely1(Inf32) == zero(Float32)
4866
@test bessely1(Inf64) == zero(Float64)
49-

0 commit comments

Comments
 (0)