Skip to content

Commit 8b01863

Browse files
committed
try SymPy.jl
1 parent a0c888b commit 8b01863

File tree

3 files changed

+91
-102
lines changed

3 files changed

+91
-102
lines changed

Project.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@ SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
1212
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
1313

1414
[compat]
15+
Conda = "1"
1516
DataDrivenDiffEq = "0.8"
1617
DataStructures = "0.18"
1718
PyCall = "1"
1819
SymbolicUtils = "0.19"
1920
Symbolics = "4"
21+
SymPy = "1"
2022
julia = "1.6"
2123

2224
[extras]
23-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
25+
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
2426
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
27+
SymPy = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6"
28+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2529

2630
[targets]
27-
test = ["Test", "PyCall"]
31+
test = ["Conda", "PyCall", "SymPy", "Test"]

test/axiom.jl

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using SymbolicUtils
2-
using SymbolicUtils.Rewriters
3-
using Symbolics
4-
# using SymbolicNumericIntegration
51

6-
using PyCall
7-
sympy = pyimport("sympy")
82

93
@variables x a b c d e p t m n z
104

@@ -15,16 +9,18 @@ function convert_axiom(name::AbstractString)
159

1610
D = Dict{Any,Int}()
1711

18-
for (lineno,line) in enumerate(readlines(fd))
12+
for (lineno, line) in enumerate(readlines(fd))
1913
line = strip(line)
20-
if length(line) < 3 || line[1:2] == "--" continue end
14+
if length(line) < 3 || line[1:2] == "--"
15+
continue
16+
end
2117
ma = match(re, line)
2218

2319
if ma != nothing
2420
line = ma.match
2521
println(line)
2622
line = replace(line, "%e" => MathConstants.e)
27-
line = replace(line, "%i" => im)
23+
line = replace(line, "%i" => im)
2824

2925
try
3026
expr = Meta.parse(line)
@@ -41,7 +37,7 @@ function convert_axiom(name::AbstractString)
4137
P[1] = substitute(P[1], D)
4238
P[4] = substitute(P[4], D)
4339

44-
printstyled(length(L)+1, ": "; color=:red)
40+
printstyled(length(L) + 1, ": "; color=:red)
4541
println(P[1])
4642
push!(L, P)
4743
catch err
@@ -83,16 +79,16 @@ function convert_axiom(sym)
8379
"Wester Problems.input"
8480
end
8581

86-
name = joinpath("test/0", name)
87-
println(name)
88-
return convert_axiom(name)
82+
name = joinpath("test/0", name)
83+
println(name)
84+
return convert_axiom(name)
8985
end
9086

9187
function test_point(complex_plane, radius)
9288
if complex_plane
93-
return radius * sqrt(rand()) * cis(2π*rand())
89+
return radius * sqrt(rand()) * cis(2π * rand())
9490
else
95-
return Complex(radius * (2*rand() - 1))
91+
return Complex(radius * (2 * rand() - 1))
9692
end
9793
end
9894

@@ -116,36 +112,37 @@ function test_axiom(L, try_sympy=true; kwargs...)
116112
n_diff = 0
117113
n_catch = 0
118114

119-
for (i,p) in enumerate(L)
115+
for (i, p) in enumerate(L)
120116
# try
121-
printstyled(i, ": "; color=:yellow)
122-
eq = p[1]
123-
x = p[2]
124-
ans = p[4]
125-
126-
printstyled(eq, '\n'; color=:green)
127-
sol = integrate(eq, x; kwargs...)[1]
128-
129-
println(">>>>", sol)
130-
131-
if isequal(sol, 0)
132-
printstyled("\tFailure\n"; color=:red)
133-
n_fail += 1
134-
elseif accept_integral(value(sol), value(ans), x)
135-
printstyled("\tSuccess:\t", sol, '\n'; color=:cyan)
136-
n_ok += 1
137-
else
138-
printstyled("\tDiscrepancy:\t", sol, '\n'; color=:yellow)
139-
n_diff += 1
140-
end
117+
printstyled(i, ": "; color=:yellow)
118+
eq = p[1]
119+
x = p[2]
120+
ans = p[4]
121+
122+
printstyled(eq, '\n'; color=:green)
123+
sol = integrate(eq, x; kwargs...)[1]
124+
125+
println(">>>>", sol)
126+
127+
if isequal(sol, 0)
128+
printstyled("\tFailure\n"; color=:red)
129+
n_fail += 1
130+
elseif accept_integral(value(sol), value(ans), x)
131+
printstyled("\tSuccess:\t", sol, '\n'; color=:cyan)
132+
n_ok += 1
133+
else
134+
printstyled("\tDiscrepancy:\t", sol, '\n'; color=:yellow)
135+
n_diff += 1
136+
end
141137

142-
if try_sympy
143-
s = pythonize(eq)
144-
py = sympy.integrate(s, sympy.Symbol(string(x)))
145-
printstyled("\tSymPy :\t", string(py)[10:end], '\n'; color=:magenta)
146-
end
138+
if try_sympy
139+
s = Symbolics.symbolics_to_sympy(s)
140+
s_x = Symbolics.symbolics_to_sympy(x)
141+
py = SymPy.integrate(s, s_x)
142+
printstyled("\tSymPy :\t", string(py)[10:end], '\n'; color=:magenta)
143+
end
147144

148-
printstyled("\tAnswer: \t", ans, '\n'; color=:blue)
145+
printstyled("\tAnswer: \t", ans, '\n'; color=:blue)
149146
# catch e
150147
# printstyled(i, ": ", e, '\n'; color=:red)
151148
# n_catch += 1
@@ -158,22 +155,3 @@ function test_axiom(L, try_sympy=true; kwargs...)
158155
printstyled("Discrepancy = ", n_diff, '\n'; color=:yellow)
159156
printstyled("Exception = ", n_catch, '\n'; color=:cyan)
160157
end
161-
162-
#############################################################################
163-
164-
pythonize(eq::SymbolicUtils.Add) = "(" * join(pythonize.(arguments(eq)), ")+(") * ")"
165-
pythonize(eq::SymbolicUtils.Mul) = "(" * join(pythonize.(arguments(eq)), ")*(") * ")"
166-
167-
function pythonize(eq::SymbolicUtils.Pow)
168-
a = pythonize.(arguments(eq))
169-
"(" * a[1] * ")**(" * a[2] * ")"
170-
end
171-
172-
pythonize(eq::SymbolicUtils.Term) = string(operation(eq)) * '(' * pythonize(arguments(eq)[1]) * ')'
173-
174-
function pythonize(eq)
175-
s = string(eq)
176-
s = replace(s, "π" => "pi")
177-
s = replace(s, "im" => "j")
178-
s
179-
end

test/runtests.jl

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
using SymbolicNumericIntegration
22
using Symbolics
3+
4+
using SymbolicUtils
5+
using SymbolicUtils.Rewriters
6+
37
using Test
8+
using PyCall, Conda, SymPy
49

510
include("axiom.jl")
611

@@ -13,26 +18,26 @@ include("axiom.jl")
1318
based on http://integral-table.com/ with modifications
1419
"""
1520
basic_integrals = [
16-
# Basic Forms
21+
# Basic Forms
1722
1,
1823
x^2,
1924
4x^3,
20-
# Integrals of Rational Functions
21-
1/x,
22-
1/(2x + 5),
23-
1/(x + 1)^2,
25+
# Integrals of Rational Functions
26+
1 / x,
27+
1 / (2x + 5),
28+
1 / (x + 1)^2,
2429
(x + 3)^3,
25-
x*(x - 2)^4,
26-
1/(1 + x^2),
27-
1/(9 + x^2),
28-
x/(4 + x^2),
29-
x^2/(16 + x^2),
30-
x^3/(1 + x^2),
31-
1/(x^2 - 5x + 6),
32-
1/(x^2 + x + 1),
33-
x/(x + 4)^2,
34-
x/(x^2 + x + 1),
35-
# Integrals with Roots
30+
x * (x - 2)^4,
31+
1 / (1 + x^2),
32+
1 / (9 + x^2),
33+
x / (4 + x^2),
34+
x^2 / (16 + x^2),
35+
x^3 / (1 + x^2),
36+
1 / (x^2 - 5x + 6),
37+
1 / (x^2 + x + 1),
38+
x / (x + 4)^2,
39+
x / (x^2 + x + 1),
40+
# Integrals with Roots
3641
sqrt(x - 2),
3742
1 / sqrt(x - 1),
3843
1 / sqrt(x + 1),
@@ -45,8 +50,8 @@ basic_integrals = [
4550
sqrt(x / (4 - x)),
4651
sqrt(x / (4 + x)),
4752
x * sqrt(2x + 3),
48-
sqrt(x*(x+2)),
49-
sqrt(x^3*(x+3)),
53+
sqrt(x * (x + 2)),
54+
sqrt(x^3 * (x + 3)),
5055
sqrt(x^2 + 4),
5156
sqrt(x^2 - 4),
5257
sqrt(4 - x^2),
@@ -64,7 +69,7 @@ basic_integrals = [
6469
x * sqrt(x^2 - 5x + 6),
6570
1 / sqrt(x^2 - 5x + 6),
6671
1 / (4 + x^2)^1.5,
67-
# Integrals with Logarithms
72+
# Integrals with Logarithms
6873
log(x),
6974
x * log(x),
7075
x^2 * log(x),
@@ -80,7 +85,7 @@ basic_integrals = [
8085
log(x)^3,
8186
x * log(x)^2,
8287
x^2 * log(x)^2,
83-
# Integrals with Exponentials
88+
# Integrals with Exponentials
8489
exp(x),
8590
sqrt(x) * exp(x),
8691
x * exp(x),
@@ -91,7 +96,7 @@ basic_integrals = [
9196
x^3 * exp(2x),
9297
exp(x^2),
9398
x * exp(x^2),
94-
# Integrals with Trigonometric Functions
99+
# Integrals with Trigonometric Functions
95100
sin(4x),
96101
sin(x)^2,
97102
sin(x)^3,
@@ -116,7 +121,7 @@ basic_integrals = [
116121
sec(x)^2 * tan(x),
117122
csc(x),
118123
sec(x) * csc(x),
119-
# Products of Trigonometric Functions and Monomials
124+
# Products of Trigonometric Functions and Monomials
120125
x * cos(x),
121126
x * cos(3x),
122127
x^2 * cos(x),
@@ -132,14 +137,14 @@ basic_integrals = [
132137
x^3 * sin(x),
133138
x^4 * cos(2x),
134139
sin(x)^2 * cos(x)^3,
135-
# Products of Trigonometric Functions and Exponentials
140+
# Products of Trigonometric Functions and Exponentials
136141
exp(x) * sin(x),
137142
exp(3x) * sin(2x),
138143
exp(x) * cos(x),
139144
exp(2x) * cos(7x),
140145
x * exp(x) * sin(x),
141146
x * exp(x) * cos(x),
142-
# Integrals of Hyperbolic Functions
147+
# Integrals of Hyperbolic Functions
143148
cosh(x),
144149
exp(x) * cosh(x),
145150
sinh(3x),
@@ -152,15 +157,15 @@ basic_integrals = [
152157
sin(x) * sinh(x),
153158
sinh(x) * cosh(x),
154159
sinh(3x) * cosh(5x),
155-
# Misc
160+
# Misc
156161
exp(x) / (1 + exp(x)),
157162
cos(exp(x)) * sin(exp(x)) * exp(x),
158163
cos(exp(x))^2 * sin(exp(x)) * exp(x),
159-
1 / (x*log(x)),
164+
1 / (x * log(x)),
160165
(log(x - 1) + (x - 1)^-1) * log(x),
161166
1 / (exp(x) - 1),
162167
1 / (exp(x) + 5),
163-
sqrt(x)*log(x),
168+
sqrt(x) * log(x),
164169
log(log(x)) / x,
165170
x^3 * exp(x^2),
166171
sin(log(x)),
@@ -169,24 +174,24 @@ basic_integrals = [
169174
1 / (exp(2x) - 1),
170175
exp(x) / (exp(2x) - 1),
171176
x / (exp(2x) - 1),
172-
# derivative-divide examples (Lamangna 7.10.2)
177+
# derivative-divide examples (Lamangna 7.10.2)
173178
exp(x) * exp(exp(x)),
174179
exp(sqrt(x)) / sqrt(x),
175-
log(log(x)) / (x*log(x)),
180+
log(log(x)) / (x * log(x)),
176181
log(cos(x)) * tan(x),
177-
# rothstein-Trager examples (Lamangna 7.10.9)
182+
# rothstein-Trager examples (Lamangna 7.10.9)
178183
1 / (x^3 - x),
179184
1 / (x^3 + 1),
180185
1 / (x^2 - 8),
181186
(x + 1) / (x^2 + 1),
182187
x / (x^4 - 4),
183188
x^3 / (x^4 + 1),
184189
1 / (x^4 + 1),
185-
# bypass = true
190+
# bypass = true
186191
β, # turn of bypass = true
187-
exp(x)/x - exp(x)/x^2,
188-
cos(x)/x - sin(x)/x^2,
189-
1/log(x) - 1/log(x)^2,
192+
exp(x) / x - exp(x) / x^2,
193+
cos(x) / x - sin(x) / x^2,
194+
1 / log(x) - 1 / log(x)^2,
190195
]
191196

192197
function test_integrals(; kw...)
@@ -203,7 +208,7 @@ function test_integrals(; kw...)
203208
printstyled(k, ": "; color=:blue)
204209
k += 1
205210
printstyled(eq, " =>\n"; color=:green)
206-
solved, unsolved = integrate(eq; args...)
211+
solved, unsolved = SymbolicNumericIntegration.integrate(eq; args...)
207212
printstyled('\t', solved; color=:cyan)
208213
if isequal(unsolved, 0)
209214
println()
@@ -215,14 +220,16 @@ function test_integrals(; kw...)
215220
end
216221

217222
n = length(misses)
218-
if n > 0 println("**** missess (n=$n) *****") end
223+
if n > 0
224+
println("**** missess (n=$n) *****")
225+
end
219226
for eq in misses
220227
printstyled(eq, '\n'; color=:red)
221228
end
222229
return n
223230
end
224231

225-
@testset "integral" begin
226-
n = test_integrals(;symbolic=false, verbose=false, homotopy=true, num_steps=2, num_trials=10)
227-
@test n==11
232+
@testset "integral" begin
233+
n = test_integrals(; symbolic=false, verbose=false, homotopy=true, num_steps=2, num_trials=10)
234+
@test n == 10
228235
end

0 commit comments

Comments
 (0)