Skip to content

Commit 904b577

Browse files
Merge pull request #40 from shahriariravanian/main
various performance improvements based of profiling
2 parents 9aa5ce3 + a404585 commit 904b577

File tree

6 files changed

+44
-55
lines changed

6 files changed

+44
-55
lines changed

Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
name = "SymbolicNumericIntegration"
22
uuid = "78aadeae-fbc0-11eb-17b6-c7ec0477ba9e"
33
authors = ["Shahriar Iravanian <[email protected]>"]
4-
version = "0.8.6"
4+
version = "0.9.0"
55

66
[deps]
77
DataDrivenDiffEq = "2445eb08-9709-466a-b3fc-47e12bd697a2"
88
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
9+
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
910
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1011
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1112
SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
@@ -15,9 +16,9 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
1516
DataDrivenDiffEq = "0.8"
1617
DataStructures = "0.18"
1718
PyCall = "1"
19+
SymPy = "1"
1820
SymbolicUtils = "0.19"
1921
Symbolics = "4"
20-
SymPy = "1"
2122
julia = "1.6"
2223

2324
[extras]

src/integral.jl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -280,16 +280,17 @@ function init_basis_matrix!(T, A, X, x, eq, Δbasis, radius, complex_plane; abst
280280
k = 1
281281
i = 1
282282

283+
eq_fun = build_function(eq, x; expression = false)
284+
Δbasis_fun = build_function.(Δbasis, x; expression = false)
285+
283286
while k <= n
284287
try
285-
x₀ = test_point(complex_plane, radius)
286-
X[k] = x₀
287-
d = Dict(x => x₀)
288+
X[k] = test_point(complex_plane, radius)
289+
b₀ = eq_fun(X[k])
288290

289-
b₀ = Complex{T}(substitute(eq, d))
290291
if is_proper(b₀)
291292
for j in 1:n
292-
A[k, j] = Complex{T}(substitute(Δbasis[j], d)) / b₀
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: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,42 +31,28 @@ 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-6) where {T <: Real}
36+
α = nice_parameter(real(u); abstol)
37+
β = nice_parameter(imag(u); abstol)
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-6) = small_rational(x; abstol)
42+
43+
nice_parameters(p; abstol = 1e-6) = nice_parameter.(p; abstol)
44+
45+
function small_rational(x::T; abstol = 1e-6, M = 20) where {T <: Real}
46+
a = floor(Int, x)
47+
r = x - a
48+
for den in 2:M
5749
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)
50+
if abs(round(r * den) - r * den) < abstol
51+
s = a + round(Int, r * den) // den
52+
return (denominator(s) == 1 ? numerator(s) : s)
6153
end
6254
catch e
6355
end
6456
end
65-
return u
66-
end
67-
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(α, β)
57+
return x
7258
end

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

test/axiom.jl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
@variables x a b c d e p t m n z
32

43
function convert_axiom(name::AbstractString)
@@ -104,7 +103,7 @@ function accept_integral(sol, ans, x; radius = 1.0, abstol = 1e-3, n = 5)
104103
return false
105104
end
106105

107-
function test_axiom(L, try_sympy = true; kwargs...)
106+
function test_axiom(L; kwargs...)
108107
n_ok = 0
109108
n_fail = 0
110109
n_diff = 0
@@ -120,7 +119,7 @@ function test_axiom(L, try_sympy = true; kwargs...)
120119
printstyled(eq, '\n'; color = :green)
121120
sol = integrate(eq, x; kwargs...)[1]
122121

123-
println(">>>>", sol)
122+
println(">>>>\t", sol)
124123

125124
if isequal(sol, 0)
126125
printstyled("\tFailure\n"; color = :red)
@@ -133,13 +132,6 @@ function test_axiom(L, try_sympy = true; kwargs...)
133132
n_diff += 1
134133
end
135134

136-
if try_sympy
137-
s = Symbolics.symbolics_to_sympy(s)
138-
s_x = Symbolics.symbolics_to_sympy(x)
139-
py = SymPy.integrate(s, s_x)
140-
printstyled("\tSymPy :\t", string(py)[10:end], '\n'; color = :magenta)
141-
end
142-
143135
printstyled("\tAnswer: \t", ans, '\n'; color = :blue)
144136
# catch e
145137
# printstyled(i, ": ", e, '\n'; color=:red)

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using SymbolicNumericIntegration
2+
using SymbolicNumericIntegration: value
23
using Symbolics
34

45
using SymbolicUtils
56
using SymbolicUtils.Rewriters
67

78
using Test
8-
using PyCall, SymPy
99

1010
include("axiom.jl")
1111

0 commit comments

Comments
 (0)