From 554f4c650d733a7f769d90d5e945dc39f1d7efec Mon Sep 17 00:00:00 2001 From: PaulXiCao <> Date: Sat, 20 Jun 2020 00:31:12 +0200 Subject: [PATCH 01/11] bessel j0,j1,y0,y1: test for type stability --- test/runtests.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 01f68774..c7ab0c60 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -246,6 +246,15 @@ end @test z(2.0 + im) ≈ f(0, 2.0 + im) @test o(2.0 + im) ≈ f(1, 2.0 + im) end + + @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 "besselj error throwing" begin @test_throws MethodError besselj(1.2,big(1.0)) @test_throws MethodError besselj(1,complex(big(1.0))) From d25b7b429f5414adc86598fc61bf84c595049abb Mon Sep 17 00:00:00 2001 From: PaulXiCao <> Date: Sat, 20 Jun 2020 00:32:27 +0200 Subject: [PATCH 02/11] bessel j0,j1,y0,y1: type stable for Complex{FloatXY} --- src/bessel.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bessel.jl b/src/bessel.jl index d70799dc..ca4a35b9 100644 --- a/src/bessel.jl +++ b/src/bessel.jl @@ -170,7 +170,9 @@ for jy in ("j","y"), nu in (0,1) end @eval begin $bjynu(x::Real) = $bjynu(float(x)) - $bjynu(x::Complex) = $(Symbol("bessel",jy))($nu,x) + $bjynu(x::Complex{Float64}) = $(Symbol("bessel",jy))($nu,x) + $bjynu(x::Complex{Float32}) = Complex{Float32}($bjynu(Complex{Float64}(x))) + $bjynu(x::Complex{Float16}) = Complex{Float16}($bjynu(Complex{Float64}(x))) end end From 74204c789ebe83506cb656eec1d4c2bd1103186c Mon Sep 17 00:00:00 2001 From: PaulXiCao <> Date: Sun, 21 Jun 2020 22:13:29 +0200 Subject: [PATCH 03/11] added: besselj(Int32, Float16) --- src/bessel.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bessel.jl b/src/bessel.jl index 78ff7d97..0c7e7881 100644 --- a/src/bessel.jl +++ b/src/bessel.jl @@ -393,6 +393,7 @@ end besselj(nu::Cint, x::Float64) = ccall((:jn, libm), Float64, (Cint, Float64), nu, x) besselj(nu::Cint, x::Float32) = ccall((:jnf, libm), Float32, (Cint, Float32), nu, x) +besselj(nu::Cint, x::Float16) = Float16(besselj(nu, Float32(x))) function besseljx(nu::Float64, z::Complex{Float64}) From 077e5e2dc0527a7acc2e75a541b3fe59e598eeaa Mon Sep 17 00:00:00 2001 From: PaulXiCao <> Date: Sun, 21 Jun 2020 22:14:31 +0200 Subject: [PATCH 04/11] type stability tests for besselj(Int32, FloatXY) --- test/bessel.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/bessel.jl b/test/bessel.jl index 47b15ff0..0a608c73 100644 --- a/test/bessel.jl +++ b/test/bessel.jl @@ -54,6 +54,11 @@ end end @test BigFloat == Base.return_types(f, Tuple{BigFloat})[] end + @testset "type stability: $f" for f in [besselj] + for F in [Float16, Float32, Float64] + @test F == Base.return_types(f, Tuple{Int32, F})[] + end + end @testset "besselj error throwing" begin @test_throws MethodError besselj(1.2,big(1.0)) From 4b7a51360a826b408259301e53e213750df70881 Mon Sep 17 00:00:00 2001 From: PaulXiCao <> Date: Sun, 21 Jun 2020 22:39:25 +0200 Subject: [PATCH 05/11] bessel j0,j1,y0,y1: testing Complex{Float16/32} --- test/bessel.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/bessel.jl b/test/bessel.jl index 0a608c73..1d7f376e 100644 --- a/test/bessel.jl +++ b/test/bessel.jl @@ -45,6 +45,10 @@ end @test o(2) ≈ o(2.0) @test z(2.0 + im) ≈ f(0, 2.0 + im) @test o(2.0 + im) ≈ f(1, 2.0 + im) + @test z(Complex{Float32}(2.0 + im)) ≈ z(2.0 + im) + @test o(Complex{Float32}(2.0 + im)) ≈ o(2.0 + im) + @test z(Complex{Float16}(2.0 + im)) ≈ z(2.0 + im) + @test o(Complex{Float16}(2.0 + im)) ≈ o(2.0 + im) end @testset "type stability: $f" for f in [bessely0, bessely1, besselj0, besselj1] From 2f5eef4b9719a87048c7af60cbec706e34d27aeb Mon Sep 17 00:00:00 2001 From: PaulXiCao <> Date: Fri, 26 Jun 2020 23:03:48 +0200 Subject: [PATCH 06/11] besselj,y: tests --- src/bessel.jl | 1 + test/bessel.jl | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/bessel.jl b/src/bessel.jl index 0c7e7881..a2d721cf 100644 --- a/src/bessel.jl +++ b/src/bessel.jl @@ -424,6 +424,7 @@ function bessely(nu::Cint, x::Float32) end ccall((:ynf, libm), Float32, (Cint, Float32), nu, x) end +bessely(nu::Cint, x::Float16) = Float16(bessely(nu, Float32(x))) function bessely(nu::Float64, z::Complex{Float64}) if nu < 0 diff --git a/test/bessel.jl b/test/bessel.jl index 1d7f376e..88695f56 100644 --- a/test/bessel.jl +++ b/test/bessel.jl @@ -49,6 +49,10 @@ end @test o(Complex{Float32}(2.0 + im)) ≈ o(2.0 + im) @test z(Complex{Float16}(2.0 + im)) ≈ z(2.0 + im) @test o(Complex{Float16}(2.0 + im)) ≈ o(2.0 + im) + @test f(3, Float32(2.0)) ≈ f(3, 2.0) + @test f(3, Float16(2.0)) ≈ f(3, 2.0) + @test f(3, Complex{Float32}(2.0 + im)) ≈ f(3, 2.0 + im) + @test f(3, Complex{Float16}(2.0 + im)) ≈ f(3, 2.0 + im) end @testset "type stability: $f" for f in [bessely0, bessely1, besselj0, besselj1] @@ -58,7 +62,7 @@ end end @test BigFloat == Base.return_types(f, Tuple{BigFloat})[] end - @testset "type stability: $f" for f in [besselj] + @testset "type stability: $f" for f in [besselj, bessely] for F in [Float16, Float32, Float64] @test F == Base.return_types(f, Tuple{Int32, F})[] end From d10542799b3e0c6a9ae5b425b6d28191beae9108 Mon Sep 17 00:00:00 2001 From: PaulXiCao <> Date: Tue, 22 Sep 2020 20:03:32 +0200 Subject: [PATCH 07/11] bessel{j,y} tests --- test/bessel.jl | 159 +++++++++++++++++++++++++------------------------ 1 file changed, 82 insertions(+), 77 deletions(-) diff --git a/test/bessel.jl b/test/bessel.jl index 88695f56..17b12691 100644 --- a/test/bessel.jl +++ b/test/bessel.jl @@ -35,44 +35,48 @@ end @testset "bessel functions" begin - bessel_funcs = [(bessely0, bessely1, bessely), (besselj0, besselj1, besselj)] - @testset "$z, $o" for (z, o, f) in bessel_funcs - @test z(Float32(2.0)) ≈ z(Float64(2.0)) - @test o(Float32(2.0)) ≈ o(Float64(2.0)) - @test z(Float16(2.0)) ≈ z(Float64(2.0)) - @test o(Float16(2.0)) ≈ o(Float64(2.0)) - @test z(2) ≈ z(2.0) - @test o(2) ≈ o(2.0) - @test z(2.0 + im) ≈ f(0, 2.0 + im) - @test o(2.0 + im) ≈ f(1, 2.0 + im) - @test z(Complex{Float32}(2.0 + im)) ≈ z(2.0 + im) - @test o(Complex{Float32}(2.0 + im)) ≈ o(2.0 + im) - @test z(Complex{Float16}(2.0 + im)) ≈ z(2.0 + im) - @test o(Complex{Float16}(2.0 + im)) ≈ o(2.0 + im) - @test f(3, Float32(2.0)) ≈ f(3, 2.0) - @test f(3, Float16(2.0)) ≈ f(3, 2.0) - @test f(3, Complex{Float32}(2.0 + im)) ≈ f(3, 2.0 + im) - @test f(3, Complex{Float16}(2.0 + im)) ≈ f(3, 2.0 + im) - end + @testset "bessel{j,y}{0,1}: check return value wrt bessel{j,y}" begin + for jy in ("j","y") + bjy = Symbol("bessel",jy) + for F in [Float16, Float32] + for nu in (0, 1) + bjynu = Symbol("bessel",jy,nu) - @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}})[] + @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 - @test BigFloat == Base.return_types(f, Tuple{BigFloat})[] end - @testset "type stability: $f" for f in [besselj, bessely] - for F in [Float16, Float32, Float64] - @test F == Base.return_types(f, Tuple{Int32, F})[] + + @testset "besselj, bessely: 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] + @test F == Base.return_types(f, Tuple{Int, F})[] + end end end - @testset "besselj error throwing" begin - @test_throws MethodError besselj(1.2,big(1.0)) - @test_throws MethodError besselj(1,complex(big(1.0))) - @test_throws MethodError besseljx(1,big(1.0)) - @test_throws MethodError besseljx(1,complex(big(1.0))) + @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 @@ -118,49 +122,49 @@ end @test_throws MethodError besselix(1,complex(big(1.0))) end end - @testset "besselj" begin - @test besselj(0,0) == 1 - for i in [-5 -3 -1 1 3 5] - @test besselj(i,0) == 0 - @test besselj(i,Float32(0)) == 0 - @test besselj(i,Complex{Float32}(0)) == 0.0 + @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 : 1) + end + for F in [Float16, Float32, Float64] + @test besselj(nu, zero( F )) == (nu == 0 ? 1 : 1) + @test besselj(nu, zero(Complex{F})) == (nu == 0 ? 1 : 1) + end end - j33 = besselj(3,3.) - @test besselj(3,3) == j33 - @test besselj(-3,-3) == j33 - @test besselj(-3,3) == -j33 - @test besselj(3,-3) == -j33 - @test besselj(3,3f0) ≈ j33 - @test besselj(3,complex(3.)) ≈ j33 - @test besselj(3,complex(3f0)) ≈ j33 - @test besselj(3,complex(3)) ≈ j33 - - j43 = besselj(4,3.) - @test besselj(4,3) == j43 - @test besselj(-4,-3) == j43 - @test besselj(-4,3) == j43 - @test besselj(4,-3) == j43 - @test besselj(4,3f0) ≈ j43 - @test besselj(4,complex(3.)) ≈ j43 - @test besselj(4,complex(3f0)) ≈ j43 - @test besselj(4,complex(3)) ≈ j43 + const 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 - @test j33 ≈ 0.30906272225525164362 - @test j43 ≈ 0.13203418392461221033 - @test besselj(0.1, complex(-0.4)) ≈ 0.820421842809028916 + 0.266571215948350899im - @test besselj(3.2, 1.3+0.6im) ≈ 0.01135309305831220201 + 0.03927719044393515275im - @test besselj(1, 3im) ≈ 3.953370217402609396im - @test besselj(1.0,3im) ≈ besselj(1,3im) + const 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 - true_jm3p1_3 = -0.45024252862270713882 - @test besselj(-3.1,3) ≈ true_jm3p1_3 - @test besselj(Float16(-3.1),Float16(3)) ≈ true_jm3p1_3 + @test besselj(1.0, 3im) ≈ besselj(1, 3im) - @testset "Error throwing" begin - @test_throws DomainError besselj(0.1, -0.4) - @test_throws AmosException besselj(20,1000im) - @test_throws MethodError besselj(big(1.0),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 @@ -185,14 +189,15 @@ end end end - @testset "bessely" begin - y33 = bessely(3,3.) - @test bessely(3,3) == y33 - @test bessely(3.,3.) == y33 - @test bessely(3,Float32(3.)) ≈ y33 + @testset "bessely: specific values and (domain) errors" begin + const y33 = bessely(3, 3.0) + @test bessely(3, 3) == y33 + @test bessely(3.0,3.0) == y33 + @test bessely(3,Float32(3.0)) ≈ y33 @test bessely(-3,3) ≈ -y33 @test y33 ≈ -0.53854161610503161800 @test bessely(3,complex(-3)) ≈ 0.53854161610503161800 - 0.61812544451050328724im + @testset "Error throwing" begin @test_throws AmosException bessely(200.5,0.1) @test_throws DomainError bessely(3,-3) @@ -201,8 +206,8 @@ end @test_throws DomainError bessely(1,Float32(-1.0)) @test_throws DomainError bessely(0.4,BigFloat(-1.0)) @test_throws DomainError bessely(1,BigFloat(-1.0)) - @test_throws DomainError bessely(Cint(3),Float32(-3.)) - @test_throws DomainError bessely(Cint(3),Float64(-3.)) + @test_throws DomainError bessely(Cint(3),Float32(-3.0)) + @test_throws DomainError bessely(Cint(3),Float64(-3.0)) @test_throws MethodError bessely(1.2,big(1.0)) @test_throws MethodError bessely(1,complex(big(1.0))) From e5d4d3237dd992982a48a27ebf3a251ccff2b66c Mon Sep 17 00:00:00 2001 From: PaulXiCao <> Date: Tue, 22 Sep 2020 22:02:18 +0200 Subject: [PATCH 08/11] bessel{j,y} tests --- test/bessel.jl | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/test/bessel.jl b/test/bessel.jl index 17b12691..3bfb3c3b 100644 --- a/test/bessel.jl +++ b/test/bessel.jl @@ -36,23 +36,26 @@ end @testset "bessel functions" begin @testset "bessel{j,y}{0,1}: check return value wrt bessel{j,y}" begin - for jy in ("j","y") - bjy = Symbol("bessel",jy) + # 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 = 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) + @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 "besselj, bessely: correct return type" begin + @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 })[] @@ -62,7 +65,14 @@ end end @testset "type stability: $f" for f in [besselj, bessely] for F in [Float16, Float32, Float64] - @test F == Base.return_types(f, Tuple{Int, F})[] + 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 @@ -127,15 +137,15 @@ end # { 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 : 1) + @test besselj(nu, zero( I )) == (nu == 0 ? 1 : 0) end for F in [Float16, Float32, Float64] - @test besselj(nu, zero( F )) == (nu == 0 ? 1 : 1) - @test besselj(nu, zero(Complex{F})) == (nu == 0 ? 1 : 1) + @test besselj(nu, zero( F )) == (nu == 0 ? 1 : 0) + @test besselj(nu, zero(Complex{F})) == (nu == 0 ? 1 : 0) end end - const j33 = besselj(3, 3.0) + j33 = besselj( 3, 3.0) @test besselj( 3, 3) == j33 @test besselj(-3, -3) == j33 @test besselj(-3, 3) == -j33 @@ -145,11 +155,11 @@ end @test besselj(3, Complex{F}(3)) ≈ j33 end - const 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 + 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 @@ -190,7 +200,7 @@ end end @testset "bessely: specific values and (domain) errors" begin - const y33 = bessely(3, 3.0) + y33 = bessely(3, 3.0) @test bessely(3, 3) == y33 @test bessely(3.0,3.0) == y33 @test bessely(3,Float32(3.0)) ≈ y33 From df24280f06d8a407ca0a1cb56be5d697c320ac58 Mon Sep 17 00:00:00 2001 From: PaulXiCao <> Date: Tue, 22 Sep 2020 22:02:54 +0200 Subject: [PATCH 09/11] code alignment --- src/bessel.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/bessel.jl b/src/bessel.jl index a2d721cf..e62df1bd 100644 --- a/src/bessel.jl +++ b/src/bessel.jl @@ -184,7 +184,7 @@ end # besselj0, besselj1, bessely0, bessely1 for jy in ("j","y"), nu in (0,1) - jynu = Expr(:quote, Symbol(jy,nu)) + jynu = Expr(:quote, Symbol(jy,nu)) jynuf = Expr(:quote, Symbol(jy,nu,"f")) bjynu = Symbol("bessel",jy,nu) if jy == "y" @@ -201,7 +201,7 @@ for jy in ("j","y"), nu in (0,1) end end @eval begin - $bjynu(x::Real) = $bjynu(float(x)) + $bjynu(x::Real ) = $bjynu(float(x)) $bjynu(x::Complex{Float64}) = $(Symbol("bessel",jy))($nu,x) $bjynu(x::Complex{Float32}) = Complex{Float32}($bjynu(Complex{Float64}(x))) $bjynu(x::Complex{Float16}) = Complex{Float16}($bjynu(Complex{Float64}(x))) @@ -391,9 +391,9 @@ function besselj(nu::Float64, z::Complex{Float64}) end end -besselj(nu::Cint, x::Float64) = ccall((:jn, libm), Float64, (Cint, Float64), nu, x) -besselj(nu::Cint, x::Float32) = ccall((:jnf, libm), Float32, (Cint, Float32), nu, x) -besselj(nu::Cint, x::Float16) = Float16(besselj(nu, Float32(x))) +besselj(nu::Cint, x::Float64) = ccall((:jn, libm), Float64, (Cint, Float64), nu, x) +besselj(nu::Cint, x::Float32) = ccall((:jnf, libm), Float32, (Cint, Float32), nu, x) +besselj(nu::Cint, x::Float16)::Float16 = besselj(nu, Float32(x)) function besseljx(nu::Float64, z::Complex{Float64}) From b131e8c87ac417105691c1c4732ad6d68a488360 Mon Sep 17 00:00:00 2001 From: PaulXiCao <> Date: Thu, 24 Sep 2020 21:21:02 +0200 Subject: [PATCH 10/11] bessely tests --- test/bessel.jl | 56 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/test/bessel.jl b/test/bessel.jl index 3bfb3c3b..5704d01f 100644 --- a/test/bessel.jl +++ b/test/bessel.jl @@ -199,30 +199,48 @@ end end end - @testset "bessely: specific values and (domain) errors" begin + @testset "bessely" begin y33 = bessely(3, 3.0) - @test bessely(3, 3) == y33 - @test bessely(3.0,3.0) == y33 - @test bessely(3,Float32(3.0)) ≈ y33 - @test bessely(-3,3) ≈ -y33 - @test y33 ≈ -0.53854161610503161800 - @test bessely(3,complex(-3)) ≈ 0.53854161610503161800 - 0.61812544451050328724im + + @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(0.4,-1.0) - @test_throws DomainError bessely(0.4,Float32(-1.0)) - @test_throws DomainError bessely(1,Float32(-1.0)) - @test_throws DomainError bessely(0.4,BigFloat(-1.0)) - @test_throws DomainError bessely(1,BigFloat(-1.0)) - @test_throws DomainError bessely(Cint(3),Float32(-3.0)) - @test_throws DomainError bessely(Cint(3),Float64(-3.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,complex(big(1.0))) + @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 From 5e23d1ca3ffe5c09f109d928fae39a40d4759b0e Mon Sep 17 00:00:00 2001 From: PaulXiCao <> Date: Wed, 7 Oct 2020 22:05:31 +0200 Subject: [PATCH 11/11] besselyx: first tests for type stability --- src/bessel.jl | 20 ++++++++++---------- test/bessel.jl | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/bessel.jl b/src/bessel.jl index e62df1bd..15c32b3a 100644 --- a/src/bessel.jl +++ b/src/bessel.jl @@ -434,14 +434,6 @@ function bessely(nu::Float64, z::Complex{Float64}) end end -function besselyx(nu::Float64, z::Complex{Float64}) - if nu < 0 - return _bessely(-nu,z,Int32(2))*cospi(nu) - _besselj(-nu,z,Int32(2))*sinpi(nu) - else - return _bessely(nu,z,Int32(2)) - end -end - """ besseli(nu, x) @@ -578,13 +570,21 @@ function besselyx(nu::Real, x::AbstractFloat) if x < 0 throw(DomainError(x, "`x` must be nonnegative.")) end - real(besselyx(float(nu), complex(x))) + convert(typeof(x), besselyx(float(nu), complex(x))) +end + +function besselyx(nu::Float64, z::Complex{Float64}) + if nu < 0 + return _bessely(-nu,z,Int32(2))*cospi(nu) - _besselj(-nu,z,Int32(2))*sinpi(nu) + else + return _bessely(nu,z,Int32(2)) + end end for f in ("i", "ix", "j", "jx", "k", "kx", "y", "yx") bfn = Symbol("bessel", f) @eval begin - $bfn(nu::Real, x::Real) = $bfn(nu, float(x)) + $bfn(nu::Real, x::Real) = typeof(float(x))($bfn(float(nu), float(x))) function $bfn(nu::Real, z::Complex) Tf = promote_type(float(typeof(nu)),float(typeof(real(z)))) $bfn(Tf(nu), Complex{Tf}(z)) diff --git a/test/bessel.jl b/test/bessel.jl index 5704d01f..c3a3af48 100644 --- a/test/bessel.jl +++ b/test/bessel.jl @@ -244,6 +244,27 @@ end 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