Skip to content

Commit 751b13b

Browse files
performance improvements (especially in init_basis_matrix)
1 parent 9aa5ce3 commit 751b13b

File tree

3 files changed

+45
-44
lines changed

3 files changed

+45
-44
lines changed

src/integral.jl

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -279,17 +279,18 @@ function init_basis_matrix!(T, A, X, x, eq, Δbasis, radius, complex_plane; abst
279279
n = size(A, 1)
280280
k = 1
281281
i = 1
282+
283+
eq_fun = build_function(eq, x; expression=false)
284+
Δbasis_fun = build_function.(Δbasis, x; expression=false)
282285

283286
while k <= n
284-
try
285-
x₀ = test_point(complex_plane, radius)
286-
X[k] = x₀
287-
d = Dict(x => x₀)
288-
289-
b₀ = Complex{T}(substitute(eq, d))
287+
try
288+
X[k] = test_point(complex_plane, radius)
289+
b₀ = eq_fun(X[k])
290+
290291
if is_proper(b₀)
291-
for j in 1:n
292-
A[k, j] = Complex{T}(substitute(Δbasis[j], d)) / b₀
292+
for j in 1:n
293+
A[k, j] = Δbasis_fun[j](X[k]) / b₀
293294
end
294295
if all(is_proper, A[k, :])
295296
k += 1
@@ -304,17 +305,21 @@ end
304305
function modify_basis_matrix!(T, A, X, x, eq, ∂eq, Δbasis, radius; abstol = 1e-6)
305306
n = size(A, 1)
306307
k = 1
308+
309+
eq_fun = build_function(eq, x; expression=false)
310+
∂eq_fun = build_function(∂eq, x; expression=false)
311+
Δbasis_fun = build_function.(Δbasis, x; expression=false)
312+
307313
for k in 1:n
308-
d = Dict(x => X[k])
309314
# One Newton iteration toward the poles
310315
# note the + sign instead of the usual - in Newton-Raphson's method. This is
311316
# because we are moving toward the poles and not zeros.
312-
x₀ = X[k] + Complex{T}(substitute(eq, d)) / Complex{T}(substitute(∂eq, d))
317+
318+
x₀ = X[k] + eq_fun(X[k]) / ∂eq_fun(X[k])
313319
X[k] = x₀
314-
d = Dict(x => x₀)
315-
b₀ = Complex{T}(substitute(eq, d))
320+
b₀ = eq_fun(X[k])
316321
for j in 1:n
317-
A[k, j] = Complex{T}(substitute(Δbasis[j], d)) / b₀
322+
A[k, j] = Δbasis_fun[j](X[k]) / b₀
318323
end
319324
end
320325
end

src/numeric_utils.jl

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,42 +31,33 @@ end
3131
"""
3232
converts float to int or small rational numbers
3333
"""
34-
function nice_parameters(p; abstol = 1e-3)
35-
c = lcm(collect(1:10)...)
36-
n = length(p)
37-
q = Array{Any}(undef, n)
38-
for i in 1:n
39-
den = 1
40-
while den < 10
41-
if abs(round(p[i] * den) - p[i] * den) < abstol
42-
a = round(Int, p[i] * den) // den
43-
q[i] = (denominator(a) == 1 ? numerator(a) : a)
44-
den = 10
45-
else
46-
q[i] = Float64(p[i])
47-
end
48-
den += 1
49-
end
50-
end
51-
q
34+
35+
function nice_parameter(u::Complex{T}; abstol = 1e-3, M = 10) where {T <: Real}
36+
α = nice_parameter(real(u))
37+
β = nice_parameter(imag(u))
38+
return β 0 ? α : Complex(α, β)
5239
end
5340

54-
function nice_parameter(u::T; abstol = 1e-3, M = 10) where {T <: Real}
55-
c = lcm(collect(1:M)...)
56-
for den in 1:M
41+
nice_parameter(x; abstol=1e-7) = small_rational(x; abstol)
42+
43+
nice_parameters(p; abstol = 1e-3) = nice_parameter.(p)
44+
45+
function small_rational(x::T; abstol=1e-6, M=20) where {T <: Real}
46+
# c = lcm(collect(1:M)...)
47+
a = floor(Int, x)
48+
r = x - a
49+
for den in 2:M
5750
try
58-
if abs(round(u * den) - u * den) < abstol
59-
a = round(Int, u * den) // den
60-
return (denominator(a) == 1 ? numerator(a) : a)
51+
if abs(round(r * den) - r * den) < abstol
52+
s = a + round(Int, r * den) // den
53+
return (denominator(s) == 1 ? numerator(s) : s)
6154
end
6255
catch e
6356
end
6457
end
65-
return u
58+
return x
6659
end
6760

68-
function nice_parameter(u::Complex{T}; abstol = 1e-3, M = 10) where {T <: Real}
69-
α = nice_parameter(real(u))
70-
β = nice_parameter(imag(u))
71-
return β 0 ? α : Complex(α, β)
72-
end
61+
62+
63+

src/utils.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
"""
22
isdependent returns true if eq is dependent on x
33
"""
4-
isdependent(eq, x) = !isequal(expand_derivatives(Differential(x)(eq)), 0)
4+
#isdependent(eq, x) = !isequal(expand_derivatives(Differential(x)(eq)), 0)
5+
6+
function isdependent(eq, x)
7+
vars = get_variables(eq)
8+
return length(vars) == 1 && isequal(x, vars[1])
9+
end
510

611
"""
712
is_number(x) returns true if x is a concrete numerical type

0 commit comments

Comments
 (0)