-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathbessel.jl
350 lines (318 loc) · 15.5 KB
/
bessel.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
@testset "airy" begin
@test_throws AmosException airyai(200im)
@test_throws AmosException airybi(200)
for T in [Float16, Float32, Float64,Complex{Float16}, Complex{Float32},Complex{Float64}]
@test airyai(T(1.8)) ≈ 0.0470362168668458052247
@test airyaiprime(T(1.8)) ≈ -0.0685247801186109345638
@test airybi(T(1.8)) ≈ 2.595869356743906290060
@test airybiprime(T(1.8)) ≈ 2.98554005084659907283
end
for T in [Complex{Float16}, Complex{Float32}, Complex{Float64}]
z = convert(T,1.8 + 1.0im)
@test airyaix(z) ≈ airyai(z) * exp(2/3 * z * sqrt(z))
@test airyaiprimex(z) ≈ airyaiprime(z) * exp(2/3 * z * sqrt(z))
@test airybix(z) ≈ airybi(z) * exp(-abs(real(2/3 * z * sqrt(z))))
@test airybiprimex(z) ≈ airybiprime(z) * exp(-abs(real(2/3 * z * sqrt(z))))
end
@test_throws MethodError airyai(complex(big(1.0)))
for x = -3:3
@test airyai(x) ≈ airyai(complex(x))
@test airyaiprime(x) ≈ airyaiprime(complex(x))
@test airybi(x) ≈ airybi(complex(x))
@test airybiprime(x) ≈ airybiprime(complex(x))
if x >= 0
@test airyaix(x) ≈ airyaix(complex(x))
@test airyaiprimex(x) ≈ airyaiprimex(complex(x))
else
@test_throws DomainError airyaix(x)
@test_throws DomainError airyaiprimex(x)
end
@test airybix(x) ≈ airybix(complex(x))
@test airybiprimex(x) ≈ airybiprimex(complex(x))
end
end
@testset "bessel functions" begin
@testset "bessel{j,y}{0,1}: check return value wrt bessel{j,y}" begin
# fixme can Symbol/eval combinations be simplified???
for jy in ("j", "y")
bjy = Symbol("bessel", jy)
bjy_ = @eval begin $bjy end
for F in [Float16, Float32]
for nu in (0, 1)
bjynu = Symbol("bessel", jy, nu)
bjynu_ = @eval begin $bjynu end
@test bjynu_( F(2.0) ) ≈ bjynu_(Float64(2.0) )
@test bjynu_( 2 ) ≈ bjynu_( 2.0 )
@test bjynu_( 2.0 ) ≈ bjy_(nu, 2.0 )
@test bjynu_( 2.0+im ) ≈ bjy_(nu, 2.0+im)
@test bjynu_(Complex{F}(2.0+im)) ≈ bjynu_( 2.0+im)
end
end
end
end
@testset "bessel{j,y}{,0,1}: correct return type" begin
@testset "type stability: $f" for f in [bessely0, bessely1, besselj0, besselj1]
for F in [Float16, Float32, Float64]
@test F == Base.return_types(f, Tuple{ F })[]
@test Complex{F} == Base.return_types(f, Tuple{Complex{F}})[]
end
@test BigFloat == Base.return_types(f, Tuple{BigFloat})[]
end
@testset "type stability: $f" for f in [besselj, bessely]
for F in [Float16, Float32, Float64]
for I in [Int16, Int32, Int64]
@test F == typeof(f(I(2), F( 2)))
@test Complex{promote_type(float(I),F)} == typeof(f(I(2), Complex{F}(2)))
for F2 in [Float16, Float32, Float64]
@test F == typeof(f(F2(2), F( 2)))
@test Complex{promote_type(F,F2)} == typeof(f(F2(2), Complex{F}(2)))
end
end
end
end
end
@testset "besselj: undefined argument types" begin
@test_throws MethodError besselj( 1.2 , big( 1.0 ))
@test_throws MethodError besselj( 1 , complex(big( 1.0 )))
@test_throws MethodError besselj(big( 1.0), 3im )
@test_throws DomainError besselj( 0.1 , -0.4 )
@test_throws AmosException besselj( 20 , 1000im )
@test_throws MethodError besseljx(1, big( 1.0) )
@test_throws MethodError besseljx(1, complex(big(1.0)))
end
@testset "besselh" begin
true_h133 = 0.30906272225525164362 - 0.53854161610503161800im
@test besselh(3,1,3) ≈ true_h133
@test besselh(-3,1,3) ≈ -true_h133
@test besselh(Float32(3),1,Float32(3)) ≈ true_h133
@test besselh(Float16(3),1,Float16(3)) ≈ true_h133
@test besselh(3,2,3) ≈ conj(true_h133)
@test besselh(-3,2,3) ≈ -conj(true_h133)
@test besselh(1,1,0) ≈ complex(0,-Inf)
@test besselh(1,2,0) ≈ complex(0,Inf)
@testset "Error throwing" begin
@test_throws AmosException besselh(1,5,0)
@test_throws MethodError besselh(1,big(1.0))
@test_throws MethodError besselh(1,complex(big(1.0)))
@test_throws MethodError besselhx(1,big(1.0))
@test_throws MethodError besselhx(1,complex(big(1.0)))
end
end
@testset "besseli" begin
true_i33 = 0.95975362949600785698
@test besseli(3,3) ≈ true_i33
@test besseli(-3,3) ≈ true_i33
@test besseli(3,-3) ≈ -true_i33
@test besseli(-3,-3) ≈ -true_i33
@test besseli(Float32(-3),Complex{Float32}(-3,0)) ≈ -true_i33
@test besseli(Float16(-3),Complex{Float16}(-3,0)) ≈ -true_i33
true_im3p1_3 = 0.84371226532586351965
@test besseli(-3.1,3) ≈ true_im3p1_3
for i in [-5 -3 -1 1 3 5]
@test besseli(i,0) == 0.0
@test besseli(i,Float32(0)) == 0
@test besseli(i,Complex{Float32}(0)) == 0
@test besseli(i,Float16(0)) == 0
@test besseli(i,Complex{Float16}(0)) == 0
end
@testset "Error throwing" begin
@test_throws AmosException besseli(1,1000)
@test_throws DomainError besseli(0.4,-1.0)
@test_throws MethodError besseli(1,big(1.0))
@test_throws MethodError besseli(1,complex(big(1.0)))
@test_throws MethodError besselix(1,big(1.0))
@test_throws MethodError besselix(1,complex(big(1.0)))
end
end
@testset "besselj: specific values and (domain) errors" begin
# besselj(nu, 0) == { 1 for nu == 0
# { 0 else
for nu in [-5 -3 -1 0 1 3 5]
for I in [Int16, Int32, Int64]
@test besselj(nu, zero( I )) == (nu == 0 ? 1 : 0)
end
for F in [Float16, Float32, Float64]
@test besselj(nu, zero( F )) == (nu == 0 ? 1 : 0)
@test besselj(nu, zero(Complex{F})) == (nu == 0 ? 1 : 0)
end
end
j33 = besselj( 3, 3.0)
@test besselj( 3, 3) == j33
@test besselj(-3, -3) == j33
@test besselj(-3, 3) == -j33
@test besselj( 3, -3) == -j33
for F in [Float16, Float32, Float64]
@test besselj(3, F( 3)) ≈ j33
@test besselj(3, Complex{F}(3)) ≈ j33
end
j43 = besselj( 4, 3.0)
@test besselj( 4, 3) == j43
@test besselj(-4, -3) == j43
@test besselj(-4, 3) == j43
@test besselj( 4, -3) == j43
for F in [Float16, Float32, Float64]
@test besselj(4, F( 3)) ≈ j43
@test besselj(4, Complex{F}(3)) ≈ j43
end
@test besselj(1.0, 3im) ≈ besselj(1, 3im)
# specific values
@test j33 ≈ 0.30906272225525164362
@test j43 ≈ 0.13203418392461221033
@test besselj(0.1, -0.4+ 0im) ≈ 0.820421842809028916 + 0.266571215948350899im
@test besselj(3.2, 1.3+0.6im) ≈ 0.01135309305831220201 + 0.03927719044393515275im
@test besselj(1, 3im) ≈ 3.953370217402609396im
for F in [Float16, Float32, Float64]
@test besselj(F(-3.1), F(3)) ≈ -0.45024252862270713882
end
end
@testset "besselk" begin
true_k33 = 0.12217037575718356792
@test besselk(3,3) ≈ true_k33
@test besselk(Float32(3),Float32(3)) ≈ true_k33
@test besselk(Float16(3),Float16(3)) ≈ true_k33
@test besselk(-3,3) ≈ true_k33
true_k3m3 = -0.1221703757571835679 - 3.0151549516807985776im
@test besselk(3,complex(-3)) ≈ true_k3m3
@test besselk(-3,complex(-3)) ≈ true_k3m3
# issue #6564
@test besselk(1.0,0.0) == Inf
@testset "Error throwing" begin
@test_throws AmosException besselk(200,0.01)
@test_throws DomainError besselk(3,-3)
@test_throws MethodError besselk(1,big(1.0))
@test_throws MethodError besselk(1,complex(big(1.0)))
@test_throws MethodError besselkx(1,big(1.0))
@test_throws MethodError besselkx(1,complex(big(1.0)))
end
end
@testset "bessely" begin
y33 = bessely(3, 3.0)
@testset "same arguments, different data types" begin
@test bessely(3, 3 ) == y33
@test bessely(3.0, 3.0) == y33
@test bessely(3.0, 3 ) == y33
for I in [Int16, Int32, Int64]
for F in [Float16, Float32, Float64]
@test bessely(I(3), F( 3)) ≈ y33
@test bessely(I(3), Complex{F}(3)) ≈ y33
end
end
end
@testset "symmetry" begin
@test bessely(-3, 3) == -y33
end
@testset "specific values" begin
@test y33 ≈ -0.53854161610503161800
@test bessely(3, complex(-3)) ≈ -y33 - 0.61812544451050328724im
end
@testset "Error throwing" begin
@test_throws AmosException bessely(200.5,0.1)
@test_throws DomainError bessely( 3, -3 )
@test_throws DomainError bessely(Cint(3), -3.0 )
@test_throws DomainError bessely(Cint(3), Float32(-3.0))
@test_throws DomainError bessely(0.4, -1.0 )
@test_throws DomainError bessely(0.4, Float32( -1.0))
@test_throws DomainError bessely(0.4, BigFloat(-1.0))
@test_throws DomainError bessely(1, -1.0 )
@test_throws DomainError bessely(1, Float32( -1.0))
@test_throws DomainError bessely(1, BigFloat(-1.0))
@test_throws MethodError bessely(1.2, big(1.0) )
@test_throws MethodError bessely(1, complex(big(1.0)))
@test_throws MethodError besselyx(1, big(1.0) )
@test_throws MethodError besselyx(1.2, big(1.0) )
@test_throws MethodError besselyx(1, complex(big(1.0)))
end
end
@testset "besselyx" begin
@testset "return type" begin
for I in [Int16, Int32, Int64], I2 in [Int16, Int32, Int64]
@test Float64 == typeof(besselyx(I(2), I2(2)))
end
for F in [Float16, Float32, Float64], F2 in [Float16, Float32, Float64]
@test F2 == typeof(besselyx(F(2), F2( 2)))
@test promote_type(F, Complex{F2}) == typeof(besselyx(F(2), Complex{F2}(2)))
end
for F in [Float16, Float32, Float64], I in [Int16, Int32, Int64]
@test float(I) == typeof(besselyx(F(2), I( 2)))
@test F == typeof(besselyx(I(2), F( 2)))
@test promote_type(float(I), Complex{F}) == typeof(besselyx(I(2), Complex{F}(2)))
end
end
end
@testset "sphericalbesselj" begin
@test sphericalbesselj(1, 1) ≈ 0.3011686789397568
@test sphericalbesselj(10, 5.5) ≈ 0.0009369210263385842
@test sphericalbesselj(1.25, 5.5) ≈ -0.1123558799930763
@test sphericalbesselj(1.25, -5.5+0im) ≈ 0.079447604649286 + 0.079447604649286im
@test sphericalbesselj(0, 0.01) ≈ 0.999983333416666
@test sphericalbesselj(0, 0) == 1.0
@test sphericalbesselj(1, 0) == 0.0
@test sphericalbesselj(1, 0.01) ≈ 0.003333300000119047
@test_throws DomainError sphericalbesselj(1.25, -5.5)
end
@testset "sphericalbessely" begin
@test sphericalbessely(1, 1) ≈ -1.381773290676036
@test sphericalbessely(10, 5.5) ≈ -10.89087037026398
@test sphericalbessely(1.25, 5.5) ≈ 0.148322390312228
@test sphericalbessely(1.25, -5.5+0im) ≈ -0.054015441306998 - 0.104879767991574im
@test sphericalbessely(0, 1e-5) ≈ -99999.9999950000000
@test sphericalbessely(1, 1e-5) ≈ -1e10
@test_throws DomainError sphericalbessely(1.25, -5.5)
@test_throws AmosException sphericalbessely(1, 0)
end
@testset "besselhx" begin
for elty in [Complex{Float16},Complex{Float32},Complex{Float64}]
z = convert(elty, 1.0 + 1.9im)
@test besselhx(1.0, 1, z) ≈ convert(elty,-0.5949634147786144 - 0.18451272807835967im)
@test besselhx(Float32(1.0), 1, z) ≈ convert(elty,-0.5949634147786144 - 0.18451272807835967im)
end
@testset "Error throwing" begin
@test_throws MethodError besselh(1,1,big(1.0))
@test_throws MethodError besselh(1,1,complex(big(1.0)))
@test_throws MethodError besselhx(1,1,big(1.0))
@test_throws MethodError besselhx(1,1,complex(big(1.0)))
end
end
@testset "scaled bessel[ijky] and hankelh[12]" begin
for x in (1.0, 0.0, -1.0), y in (1.0, 0.0, -1.0), nu in (1.0, 0.0, -1.0)
z = Complex{Float64}(x + y * im)
z == zero(z) || @test hankelh1x(nu, z) ≈ hankelh1(nu, z) * exp(-z * im)
z == zero(z) || @test hankelh2x(nu, z) ≈ hankelh2(nu, z) * exp(z * im)
(nu < 0 && z == zero(z)) || @test besselix(nu, z) ≈ besseli(nu, z) * exp(-abs(real(z)))
(nu < 0 && z == zero(z)) || @test besseljx(nu, z) ≈ besselj(nu, z) * exp(-abs(imag(z)))
z == zero(z) || @test besselkx(nu, z) ≈ besselk(nu, z) * exp(z)
z == zero(z) || @test besselyx(nu, z) ≈ bessely(nu, z) * exp(-abs(imag(z)))
end
@test besselkx(1, 0) == Inf
for i = [-5 -3 -1 1 3 5]
@test besseljx(i,0) == 0
@test besselix(i,0) == 0
@test besseljx(i,Float32(0)) == 0
@test besselix(i,Float32(0)) == 0
@test besseljx(i,Complex{Float32}(0)) == 0
@test besselix(i,Complex{Float32}(0)) == 0
@test besseljx(i,Float16(0)) == 0
@test besselix(i,Float16(0)) == 0
@test besseljx(i,Complex{Float16}(0)) == 0
@test besselix(i,Complex{Float16}(0)) == 0
end
@testset "Error throwing" begin
@test_throws AmosException hankelh1x(1, 0)
@test_throws AmosException hankelh2x(1, 0)
@test_throws AmosException besselix(-1.01, 0)
@test_throws AmosException besseljx(-1.01, 0)
@test_throws AmosException besselyx(1, 0)
@test_throws DomainError besselix(0.4,-1.0)
@test_throws DomainError besseljx(0.4, -1.0)
@test_throws DomainError besselkx(0.4,-1.0)
@test_throws DomainError besselyx(0.4,-1.0)
end
end
@testset "issue #6653" begin
@testset "$f" for f in (besselj,bessely,besseli,besselk,hankelh1,hankelh2)
@test f(0,1) ≈ f(0,Complex{Float64}(1))
@test f(0,1) ≈ f(0,Complex{Float32}(1))
@test f(0,1) ≈ f(0,Complex{Float16}(1))
end
end
end