Skip to content

Commit 34e710d

Browse files
Merge pull request #9 from anandijain/aj/fix
try SymPy.jl
2 parents a0c888b + 57f4aad commit 34e710d

File tree

8 files changed

+123
-102
lines changed

8 files changed

+123
-102
lines changed

.github/workflows/CI.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ jobs:
1616
version:
1717
- '1'
1818
- '1.6'
19+
env:
20+
PYTHON: python3
21+
1922
steps:
2023
- uses: actions/checkout@v2
2124
- uses: julia-actions/setup-julia@v1
@@ -31,6 +34,9 @@ jobs:
3134
${{ runner.os }}-test-${{ env.cache-name }}-
3235
${{ runner.os }}-test-
3336
${{ runner.os }}-
37+
- run: >
38+
sudo apt-get install --no-install-recommends python3-setuptools python3-wheel
39+
&& pip3 install sympy
3440
- uses: julia-actions/julia-buildpkg@v1
3541
- uses: julia-actions/julia-runtest@v1
3642
- uses: julia-actions/julia-processcoverage@v1

Project.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ DataStructures = "0.18"
1717
PyCall = "1"
1818
SymbolicUtils = "0.19"
1919
Symbolics = "4"
20+
SymPy = "1"
2021
julia = "1.6"
2122

2223
[extras]
23-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2424
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
25+
SymPy = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6"
26+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2527

2628
[targets]
27-
test = ["Test", "PyCall"]
29+
test = ["PyCall", "SymPy", "Test"]

docs/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build/
2+
site/

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[deps]
2+
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"

docs/make.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Documenter
2+
using SymbolicNumericIntegration
3+
4+
makedocs(
5+
sitename="SymbolicNumericIntegration",
6+
format=Documenter.HTML(),
7+
modules=[SymbolicNumericIntegration]
8+
)
9+
10+
# Documenter can also automatically deploy documentation to gh-pages.
11+
# See "Hosting Documentation" and deploydocs() in the Documenter manual
12+
# for more information.
13+
deploydocs(
14+
repo="https://github.com/SciML/SymbolicNumericIntegration.jl";
15+
push_preview=true
16+
)

docs/src/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SymbolicNumericIntegration.jl
2+
3+
Documentation for SymbolicNumericIntegration.jl
4+
5+
```@autodocs
6+
Modules = [SymbolicNumericIntegration]
7+
```

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

0 commit comments

Comments
 (0)