From 1b1c50149fa8f0591f100af67dbfb4d92ec139bd Mon Sep 17 00:00:00 2001 From: songjhaha Date: Wed, 28 Feb 2024 10:21:43 +0800 Subject: [PATCH 1/4] improve type stable for expint --- src/expint.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/expint.jl b/src/expint.jl index 7a9f4267..dba922e1 100644 --- a/src/expint.jl +++ b/src/expint.jl @@ -131,7 +131,7 @@ end function _expint(z::Complex{Float64}, ::Val{expscaled}=Val{false}()) where {expscaled} if real(z) < 0 - return _expint(1, z, 1000, Val{expscaled}()) + return _expint(one(z), z, 1000, Val{expscaled}()) else return expint_opt(z, Val{expscaled}()) end @@ -208,9 +208,10 @@ En_safe_gamma_term(ν::Integer, z::Real) = (z ≥ 0 || isodd(ν) ? 1 : -1) * exp # returns the two terms from the above equation separately function En_cf_gamma(ν::Number, z::Number, n::Int=1000) A = float(1 - ν) - B::typeof(A) = 1 - Bprev::typeof(A) = 0 - Aprev::typeof(A) = 1 + A, _ = promote(A, z) + B = one(A) + Bprev = zero(B) + Aprev = one(A) ϵ = 10*eps(real(B)) scale = sqrt(floatmax(real(A))) From bac854b96ec8aaf6b8ad7e2ee50e060fbadb2176 Mon Sep 17 00:00:00 2001 From: songjhaha Date: Thu, 29 Feb 2024 10:29:23 +0800 Subject: [PATCH 2/4] add infer test && improve promote --- src/expint.jl | 9 ++++----- test/expint.jl | 2 ++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/expint.jl b/src/expint.jl index dba922e1..50dd6f07 100644 --- a/src/expint.jl +++ b/src/expint.jl @@ -131,7 +131,7 @@ end function _expint(z::Complex{Float64}, ::Val{expscaled}=Val{false}()) where {expscaled} if real(z) < 0 - return _expint(one(z), z, 1000, Val{expscaled}()) + return _expint(1, z, 1000, Val{expscaled}()) else return expint_opt(z, Val{expscaled}()) end @@ -207,11 +207,10 @@ En_safe_gamma_term(ν::Integer, z::Real) = (z ≥ 0 || isodd(ν) ? 1 : -1) * exp # https://functions.wolfram.com/GammaBetaErf/ExpIntegralE/10/0005/ # returns the two terms from the above equation separately function En_cf_gamma(ν::Number, z::Number, n::Int=1000) - A = float(1 - ν) - A, _ = promote(A, z) - B = one(A) + A, z = map(float, promote(float(1 - ν), z)) + B = oneunit(A) Bprev = zero(B) - Aprev = one(A) + Aprev = oneunit(A) ϵ = 10*eps(real(B)) scale = sqrt(floatmax(real(A))) diff --git a/test/expint.jl b/test/expint.jl index fe034d07..afc7cd49 100644 --- a/test/expint.jl +++ b/test/expint.jl @@ -158,6 +158,8 @@ using Base.MathConstants Γ, cf, iter = SpecialFunctions.En_cf_gamma(1 / 2, x) @test Γ + cf*exp(-x) ≈ y end + # type stability + @inferred SpecialFunctions.En_cf_gamma(1, 1.0 + 2.1im, 1000) end @testset "En_expand_origin" begin for (x, y) in zip(xs, ys) From 7ff5ee14be4cd5d9fcca012a0f4ed42585af089f Mon Sep 17 00:00:00 2001 From: songjhaha Date: Thu, 29 Feb 2024 10:50:16 +0800 Subject: [PATCH 3/4] fix promote for Float32 case --- src/expint.jl | 2 +- test/expint.jl | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/expint.jl b/src/expint.jl index 50dd6f07..a0acd81d 100644 --- a/src/expint.jl +++ b/src/expint.jl @@ -207,7 +207,7 @@ En_safe_gamma_term(ν::Integer, z::Real) = (z ≥ 0 || isodd(ν) ? 1 : -1) * exp # https://functions.wolfram.com/GammaBetaErf/ExpIntegralE/10/0005/ # returns the two terms from the above equation separately function En_cf_gamma(ν::Number, z::Number, n::Int=1000) - A, z = map(float, promote(float(1 - ν), z)) + A, z = map(float, promote(1 - ν, z)) B = oneunit(A) Bprev = zero(B) Aprev = oneunit(A) diff --git a/test/expint.jl b/test/expint.jl index afc7cd49..ea59db82 100644 --- a/test/expint.jl +++ b/test/expint.jl @@ -160,6 +160,9 @@ using Base.MathConstants end # type stability @inferred SpecialFunctions.En_cf_gamma(1, 1.0 + 2.1im, 1000) + @test SpecialFunctions.En_cf_gamma(1, 1.0 + 2.1im, 1000) isa Tuple{ComplexF64,ComplexF64,Int} + @inferred SpecialFunctions.En_cf_gamma(1, 1.0f0, 1000) + @test SpecialFunctions.En_cf_gamma(1, 1.0f0, 1000) isa Tuple{Float32,Float32,Int} end @testset "En_expand_origin" begin for (x, y) in zip(xs, ys) From 913efcb6071a46c33c9addc39927088ae065995c Mon Sep 17 00:00:00 2001 From: songjhaha Date: Thu, 29 Feb 2024 18:10:16 +0800 Subject: [PATCH 4/4] improve inferred test --- test/expint.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/expint.jl b/test/expint.jl index ea59db82..d25e92a2 100644 --- a/test/expint.jl +++ b/test/expint.jl @@ -159,10 +159,8 @@ using Base.MathConstants @test Γ + cf*exp(-x) ≈ y end # type stability - @inferred SpecialFunctions.En_cf_gamma(1, 1.0 + 2.1im, 1000) - @test SpecialFunctions.En_cf_gamma(1, 1.0 + 2.1im, 1000) isa Tuple{ComplexF64,ComplexF64,Int} - @inferred SpecialFunctions.En_cf_gamma(1, 1.0f0, 1000) - @test SpecialFunctions.En_cf_gamma(1, 1.0f0, 1000) isa Tuple{Float32,Float32,Int} + @test @inferred(SpecialFunctions.En_cf_gamma(1, 1.0 + 2.1im, 1000)) isa Tuple{ComplexF64,ComplexF64,Int} + @test @inferred(SpecialFunctions.En_cf_gamma(1, 1.0f0, 1000)) isa Tuple{Float32,Float32,Int} end @testset "En_expand_origin" begin for (x, y) in zip(xs, ys)