Skip to content

added Logarithmic integral function #500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/functions_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ ncF
expint
expinti
expintx
li
sinint
cosint
```
Expand Down
1 change: 1 addition & 0 deletions docs/src/functions_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Here the *Special Functions* are listed according to the structure of [NIST Digi
| [`expintx(x)`](@ref SpecialFunctions.expintx) | scaled [exponential integral](https://en.wikipedia.org/wiki/Exponential_integral) ``e^z \operatorname{E}_\nu(z)`` |
| [`sinint(x)`](@ref SpecialFunctions.sinint) | [sine integral](https://en.wikipedia.org/wiki/Trigonometric_integral#Sine_integral) ``\operatorname{Si}(x)`` |
| [`cosint(x)`](@ref SpecialFunctions.cosint) | [cosine integral](https://en.wikipedia.org/wiki/Trigonometric_integral#Cosine_integral) ``\operatorname{Ci}(x)`` |
| [`li(x)`](@ref SpecialFunctions.li) | [logarithmic integral function](https://en.wikipedia.org/wiki/Logarithmic_integral_function) ``\operatorname{li}(x)`` |


## Error Functions, Dawson’s and Fresnel Integrals
Expand Down
1 change: 1 addition & 0 deletions src/SpecialFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export
expintx,
sinint,
cosint,
li,
lbinomial

include("bessel.jl")
Expand Down
29 changes: 29 additions & 0 deletions src/expint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,32 @@ function expinti(x::BigFloat)
ccall((:mpfr_eint, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Base.MPFR.MPFRRoundingMode), z, x, Base.MPFR.ROUNDING_MODE[])
return z
end

##############################################################################
# Logarithmic integral function li

@doc raw"""
expinti(x::Real)

Computes the Logarithmic integral function
```math
\operatorname{li}(x) = \int_{0}^x \frac{1}{\ln{t}} \mathrm{d}t,
```
which is equivalent to ``\operatorname{Ei}(\ln{x})`` where
``\operatorname{Ei}`` is the `expinti` function.

External links: [Wikipedia](https://en.wikipedia.org/wiki/Logarithmic_integral_function)
"""
function li(x::Real)
if x < 0
throw(DomainError(x, "negative argument, convert to complex first"))
elseif x == 0
return 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a type instability.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this solve it?

    elseif x == 0
        return zero(typeof(x))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or simply zero(x)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but you probably want zero(float(x))

elseif x == 1
return -Inf
elseif x == Inf
return Inf
else
return expinti(log(x))
end
end
7 changes: 7 additions & 0 deletions test/expint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,10 @@ expinti_real(x) = invoke(expinti, Tuple{Real}, x)
end
end
end

@testset "Logarithmic integral function" begin
@test li(0.0) ≈ 0
@test li(1.0) ≈ -Inf
@test li(Inf) === Inf
@test li(2) ≈ 1.0451637801174927
end
Loading