Skip to content

Commit 3ab988a

Browse files
committed
add core numbers
1 parent 1359575 commit 3ab988a

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
3838
SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
3939

4040
[targets]
41-
test = ["Test"]
41+
test = ["Symbolics", "Test"]

ext/SymPyPythonCallSymbolicsExt.jl

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,19 @@ function pyconvert_rule_sympy_function(::Type{Symbolics.Num}, x)
6868
if !pyisinstance(x,sp.Function)
6969
return PythonCall.pyconvert_unconverted()
7070
end
71-
name = pyconvert(Symbol,x.name)
72-
args = pyconvert.(Symbolics.Num,x.args)
71+
nm = PythonCall.pygetattr(x, "func", nothing)
72+
isnothing(nm) && return PythonCall.pyconvert_unconverted() # XXX
73+
name = pyconvert(Symbol, nm)
74+
args = pyconvert.(Symbolics.Num, x.args)
7375
func = @variables $name(..)
7476
return PythonCall.pyconvert_return(first(func)(args...))
7577
end
7678

7779

7880
function __init__()
7981
# added rules
82+
add_pyconvert_rule(f, cls) = PythonCall.pyconvert_add_rule(cls, Symbolics.Num, f)
83+
8084
PythonCall.pyconvert_add_rule("sympy.core.symbol:Symbol", Symbolics.Num, pyconvert_rule_sympy_symbol)
8185

8286
PythonCall.pyconvert_add_rule("sympy.core.power:Pow", Symbolics.Num, pyconvert_rule_sympy_pow)
@@ -90,7 +94,25 @@ function __init__()
9094
PythonCall.pyconvert_add_rule("sympy.core.function:Derivative",Symbolics.Num, pyconvert_rule_sympy_derivative)
9195

9296
PythonCall.pyconvert_add_rule("sympy.core.function:Function",Symbolics.Num, pyconvert_rule_sympy_function)
93-
end
9497

98+
# core numbers
99+
add_pyconvert_rule("sympy.core.numbers:Pi") do T::Type{Symbolics.Num}, x
100+
PythonCall.pyconvert_return(Symbolics.Num(pi))
101+
end
102+
add_pyconvert_rule("sympy.core.numbers:Exp1") do T::Type{Symbolics.Num}, x
103+
PythonCall.pyconvert_return(Symbolics.Num(ℯ))
104+
end
105+
add_pyconvert_rule("sympy.core.numbers:Infinity") do T::Type{Symbolics.Num}, x
106+
PythonCall.pyconvert_return(Symbolics.Num(Inf))
107+
end
108+
#= complex numbers and Num needs some workaround
109+
add_pyconvert_rule("sympy.core.numbers:ImaginaryUnit") do T::Type{Symbolics.Num}, x
110+
PythonCall.pyconvert_return(Symbolics.Num(im))
111+
end
112+
add_pyconvert_rule("sympy.core.numbers:ComplexInfinity") do T::Type{Symbolics.Num}, x
113+
PythonCall.pyconvert_return(Symbolics.Num(Inf)) # errors: Complex(Inf,Inf)))
114+
end
115+
=#
116+
end
95117

96118
end

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ include("test-specialfuncs.jl")
1313
#include("test-physics.jl")
1414
#include("test-external-module.jl")
1515
include("test-latexify.jl")
16+
17+
if VERSION >= v"1.9.0-"
18+
@testset "Symbolics integration" begin include("symbolics-integration.jl") end
19+
end

test/symbolics-integration.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using SymPyPythonCall
2+
using Symbolics
3+
4+
@test isa(SymPyPythonCall.PythonCall.pyconvert(Symbolics.Num, sympy.sympify("x")), Symbolics.Num)

0 commit comments

Comments
 (0)