Skip to content

Lambert's Omega constant #12

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

Closed
wants to merge 11 commits into from
6 changes: 5 additions & 1 deletion src/IrrationalConstants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ export
logten, # log(10)
logπ, # log(π)
log2π, # log(2π)
log4π # log(4π)
log4π, # log(4π)
invℯ, inve, # 1 / ℯ

lambertw_Ω, lambertw_Omega # Ω exp(Ω) = 1

include("stats.jl")
include("lambertw_omega.jl")

end # module
35 changes: 35 additions & 0 deletions src/lambertw_omega.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# lazy-initialized LambertW Omega at 256-bit precision
const lambertw_Omega_BigFloat256 = Ref{BigFloat}()

# compute BigFloat Omega constant at arbitrary precision
function compute_lambertw_Omega()
# initialize lambertw_Omega_BigFloat256
isassigned(lambertw_Omega_BigFloat256) ||
(lambertw_Omega_BigFloat256[] = BigFloat("0.5671432904097838729999686622103555497538157871865125081351310792230457930866845666932194", 256))
o = BigFloat(lambertw_Omega_BigFloat256[], precision(BigFloat)) # initial value with current precision
precision(o) <= 256 && return o
# iteratively improve the precision of the constant
myeps = eps(BigFloat)
for _ in 1:100
Copy link
Member

Choose a reason for hiding this comment

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

Is this enough for higher precision? It seems a bit dangerous to upper bound the number of iterations.

Suggested change
for _ in 1:100
while true

or alternatively move the stopping criterion here.

Copy link
Author

Choose a reason for hiding this comment

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

Wiki says (I've just added the reference) that this is the quadratic method, the number of correct digits is doubled at each iteration. So 100 should be safe for any reasonable precision.
while true sounds a bit dangerous.
I can increase it to 1000 and add a warning that the precision was not reached.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe just use the stopping criterion, i.e., while abs(next_o - o) > eps?

Copy link
Author

Choose a reason for hiding this comment

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

I want to avoid dead loop if there's some subtle bug in BigFloat implementation or something like that.

Copy link
Member

Choose a reason for hiding this comment

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

What bug could cause a dead loop? Generally, we should assume that BigFloat does not contain any bugs - and if we notice any, they should be fixed upstream.

BTW just came across the following while true loop, I still think it would be a simple and straightforward implementation here as well: https://github.com/JuliaLang/julia/blob/3d11f7db65a3461320542aee3b0f26619c4e65e3/base/irrationals.jl#L54

o_ = (1 + o) / (1 + exp(o))
abs(o - o_) <= myeps && break
o = o_
end
return o
end

@irrational lambertw_Ω 0.567143290409783872999968662210355 compute_lambertw_Omega()
const lambertw_Omega = lambertw_Ω # ASCII alias

"""
Lambert's Omega (*Ω*) constant.

Lambert's *Ω* is the solution to *W(Ω) = 1* equation,
where *W(t) = t exp(t)* is the
[Lambert's *W* function](https://en.wikipedia.org/wiki/Lambert_W_function).

# See also
* https://en.wikipedia.org/wiki/Omega_constant
* [`lambertw()`][@ref SpecialFunctions.lambertw]
"""
lambertw_Ω
3 changes: 3 additions & 0 deletions src/stats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@
@irrational logπ 1.1447298858494001741 log(big(π))
@irrational log2π 1.8378770664093454836 log(2 * big(π))
@irrational log4π 2.5310242469692907930 log(4 * big(π))

@irrational invℯ 0.367879441171442321595 inv(big(ℯ))
const inve = invℯ # ASCII alias
29 changes: 29 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,32 @@ end
@test isapprox(log(4pi), log4π)
end

@testset "1/e" begin
@test isapprox(invℯ, exp(-1))
@test isapprox(inve, exp(-1))
end

@testset "lambertw_Omega" begin
@test isapprox(lambertw_Ω * exp(lambertw_Ω), 1)
@test lambertw_Omega == lambertw_Ω

# lower than default precision
setprecision(BigFloat, 196) do
o = big(lambertw_Ω)
@test precision(o) == 196
@test isapprox(o * exp(o), 1, atol=eps(BigFloat))

oalias = big(lambertw_Omega)
@test o == oalias
end

# higher than default precision
setprecision(BigFloat, 2048) do
o = big(lambertw_Ω)
@test precision(o) == 2048
@test isapprox(o * exp(o), 1, atol=eps(BigFloat))

oalias = big(lambertw_Omega)
@test o == oalias
end
end