Skip to content

Commit 1ee62c4

Browse files
committed
lambertwbp: simplify series coeffs code
1 parent b2ed0db commit 1ee62c4

File tree

1 file changed

+22
-34
lines changed

1 file changed

+22
-34
lines changed

src/lambertw.jl

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -201,45 +201,33 @@ Base.BigFloat(::Irrational{:ω}) = omega_const(BigFloat)
201201
# (4.23) and (4.24) for all μ are also given. This code implements the
202202
# recursion relations.
203203

204-
# (4.23) and (4.24) give zero based coefficients.
205-
cset(a, i, v) = a[i+1] = v
206-
cget(a, i) = a[i+1]
207-
208-
# (4.24)
209-
function compute_a_coeffs(k, m, a)
210-
sum0 = zero(eltype(m))
211-
for j in 2:(k - 1)
212-
sum0 += cget(m, j) * cget(m, k + 1 - j)
213-
end
214-
cset(a, k, sum0)
215-
return sum0
216-
end
217-
218-
# (4.23)
219-
function compute_m_coefficients(k, m, a)
220-
kt = convert(eltype(m), k)
221-
mk = (kt - 1) / (kt + 1) *(cget(m, k - 2) / 2 + cget(a, k - 2) / 4) -
222-
cget(a, k) / 2 - cget(m, k - 1) / (kt + 1)
223-
cset(m, k, mk)
224-
return mk
225-
end
226-
227204
# We plug the known value μ₂ == -1//3 for (4.22) into (4.23) and
228205
# solve for α₂. We get α₂ = 0.
229206
# Compute array of coefficients μ in (4.22).
230207
# m[1] is μ₀
231208
function compute_branch_point_coeffs(T::DataType, n::Int)
232-
a = Array{T}(undef, n)
233-
m = Array{T}(undef, n)
234-
cset(a, 0, 2) # α₀ literal in paper
235-
cset(a, 1, -1) # α₁ literal in paper
236-
cset(a, 2, 0) # α₂ get this by solving (4.23) for alpha_2 with values printed in paper
237-
cset(m, 0, -1) # μ₀ literal in paper
238-
cset(m, 1, 1) # μ₁ literal in paper
239-
cset(m, 2, -1//3) # μ₂ literal in paper, but only in (4.22)
240-
for i in 3:(n - 1) # coeffs are zero indexed
241-
compute_a_coeffs(i, m, a)
242-
compute_m_coefficients(i, m, a)
209+
a = Vector{T}(undef, n)
210+
m = Vector{T}(undef, n)
211+
212+
a[1] = 2 # α₀ literal in paper
213+
a[2] = -1 # α₁ literal in paper
214+
a[3] = 0 # α₂ get this by solving (4.23) for alpha_2 with values printed in paper
215+
m[1] = -1 # μ₀ literal in paper
216+
m[2] = 1 # μ₁ literal in paper
217+
m[3] = -1//3 # μ₂ literal in paper, but only in (4.22)
218+
219+
for i in 4:n
220+
# (4.24)
221+
msum = zero(T)
222+
@inbounds for j in 2:(i - 2)
223+
msum += m[j + 1] * m[i + 1 - j]
224+
end
225+
a[i] = msum
226+
227+
# (4.23)
228+
it = convert(T, i)
229+
m[i] = (it - 2) / it *(m[i - 2] / 2 + a[i - 2] / 4) -
230+
a[i] / 2 - m[i - 1] / it
243231
end
244232
return m
245233
end

0 commit comments

Comments
 (0)