Skip to content

Commit

Permalink
remove global state (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored Feb 20, 2023
1 parent b1cf074 commit f14f023
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "LegendrePolynomials"
uuid = "3db4a2ba-fc88-11e8-3e01-49c72059a882"
version = "0.4.3"
version = "0.4.4"

[deps]
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Expand Down
30 changes: 3 additions & 27 deletions src/LegendrePolynomials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@ export dnPl
export collectdnPl
export collectdnPl!

# cache values of logfactorial to avoid expensive calls
const lfdictInt = Vector{Dict{Int, Float64}}()
const lfdictBigInt = Vector{Dict{BigInt, BigFloat}}()

function __init__()
empty!(lfdictInt)
empty!(lfdictBigInt)
resize!(lfdictInt, Threads.nthreads())
resize!(lfdictBigInt, Threads.nthreads())
for i in 1:Threads.nthreads()
lfdictInt[i] = Dict{Int, Float64}()
lfdictBigInt[i] = Dict{BigInt, BigFloat}()
end
end

checkdomain(x) = true
function checkdomain(x::Number)
f = abs(x) > 1
Expand Down Expand Up @@ -84,18 +69,9 @@ end
convert(T, P_n_l)
end

# Allow memoization
_logfactorial(n) = logfactorial(n)
_logfactorial(n::Int) = get!(lfdictInt[Threads.threadid()], n) do
logfactorial(n)
end
_logfactorial(n::BigInt) = get!(lfdictBigInt[Threads.threadid()], n) do
logfactorial(n)
end

function logplm_norm(l, m)
T = promote_type(typeof(l), typeof(m))
(log(T(2)) - log(2T(l)+1) + _logfactorial(l + m) - _logfactorial(l - m))/2
(log(T(2)) - log(2T(l)+1) + logfactorial(l + m) - logfactorial(l - m))/2
end
function _maybebigexp(t)
if t < log(prevfloat(typemax(t)))
Expand All @@ -111,12 +87,12 @@ end
function logabspll_prefactor(l, T = typeof(l))
lT = T(l)
a = (logfactorial(2lT-1) + log(2lT+1) - log(lT))/2
b = l*log(T(2)) + _logfactorial(lT-1)
b = l*log(T(2)) + logfactorial(lT-1)
a - b
end
neg1pow(l) = iseven(l) ? 1 : -1
function pll_prefactor(l, T = typeof(l); csphase::Bool = true)
l == 0 && return sqrt(oftype(_logfactorial(T(l)), 1/2))
l == 0 && return sqrt(oftype(logfactorial(T(l)), 1/2))
t = logabspll_prefactor(l, T)
(csphase ? neg1pow(l) : 1) * exp(t)
end
Expand Down

2 comments on commit f14f023

@jishnub
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/78084

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.4 -m "<description of version>" f14f023d2ebfc07a911fd4cb541a9985d8b5ed4d
git push origin v0.4.4

Please sign in to comment.