@@ -3,104 +3,94 @@ module SymPyPythonCallSymbolicsExt
3
3
# from https://github.com/JuliaSymbolics/Symbolics.jl/pull/957/
4
4
# by @jClugstor
5
5
import SymPyPythonCall
6
- # const sp = SymPyPythonCall.sympy.py
6
+ sp = SymPyPythonCall. sympy. py
7
7
const PythonCall = SymPyPythonCall. PythonCall
8
- import PythonCall: pyconvert
8
+ import PythonCall: pyconvert, pyimport, pyisinstance
9
9
10
10
import Symbolics
11
11
import Symbolics: @variables
12
12
13
13
# rule functions
14
+ function pyconvert_rule_sympy_symbolX (:: Type{Symbolics.Num} , x)
15
+ end
14
16
function pyconvert_rule_sympy_symbol (:: Type{Symbolics.Num} , x)
15
- #=
16
17
if ! pyisinstance (x,sp. Symbol)
17
18
return PythonCall. pyconvert_unconverted ()
18
19
end
19
- =#
20
20
name = PythonCall. pyconvert (Symbol,x. name)
21
21
return PythonCall. pyconvert_return (Symbolics. variable (name))
22
22
end
23
23
24
24
function pyconvert_rule_sympy_pow (:: Type{Symbolics.Num} , x)
25
- #=
26
25
if ! pyisinstance (x,sp. Pow)
27
26
return PythonCall. pyconvert_unconverted ()
28
27
end
29
- =#
30
28
expbase = pyconvert (Symbolics. Num,x. base)
31
29
exp = pyconvert (Symbolics. Num,x. exp)
32
30
return PythonCall. pyconvert_return (expbase^ exp)
33
31
end
34
32
35
33
function pyconvert_rule_sympy_mul (:: Type{Symbolics.Num} , x)
36
- #=
37
34
if ! pyisinstance (x,sp. Mul)
38
35
return PythonCall. pyconvert_unconverted ()
39
36
end
40
- =#
41
37
mult = reduce (* ,PythonCall. pyconvert .(Symbolics. Num,x. args))
42
38
return PythonCall. pyconvert_return (mult)
43
39
end
44
40
45
41
function pyconvert_rule_sympy_add (:: Type{Symbolics.Num} , x)
46
- #=
47
42
if ! pyisinstance (x,sp. Add)
48
43
return PythonCall. pyconvert_unconverted ()
49
44
end
50
- =#
51
45
sum = reduce (+ , PythonCall. pyconvert .(Symbolics. Num,x. args))
52
46
return PythonCall. pyconvert_return (sum)
53
47
end
54
48
55
49
function pyconvert_rule_sympy_equality (:: Type{Symbolics.Equation} , x)
56
- #=
57
50
if ! pyisinstance (x,sp. Equality)
58
51
return PythonCall. pyconvert_unconverted ()
59
52
end
60
- =#
61
53
rhs = pyconvert (Symbolics. Num,x. rhs)
62
54
lhs = pyconvert (Symbolics. Num,x. lhs)
63
55
return PythonCall. pyconvert_return (rhs ~ lhs)
64
56
end
65
57
function pyconvert_rule_sympy_derivative (:: Type{Symbolics.Num} , x)
66
- #=
67
58
if ! pyisinstance (x,sp. Derivative)
68
59
return PythonCall. pyconvert_unconverted ()
69
60
end
70
- =#
71
61
variables = pyconvert .(Symbolics. Num,x. variables)
72
62
derivatives = prod (var -> Differential (var), variables)
73
63
expr = pyconvert (Symbolics. Num, x. expr)
74
64
return PythonCall. pyconvert_return (derivatives (expr))
75
65
end
76
66
77
67
function pyconvert_rule_sympy_function (:: Type{Symbolics.Num} , x)
78
- #=
79
68
if ! pyisinstance (x,sp. Function)
80
69
return PythonCall. pyconvert_unconverted ()
81
70
end
82
- =#
83
71
name = pyconvert (Symbol,x. name)
84
72
args = pyconvert .(Symbolics. Num,x. args)
85
73
func = @variables $ name (.. )
86
74
return PythonCall. pyconvert_return (first (func)(args... ))
87
75
end
88
76
89
- # added rules
90
- PythonCall. pyconvert_add_rule (" sympy.core.symbol:Symbol" , Symbolics. Num, pyconvert_rule_sympy_symbol)
91
77
92
- PythonCall. pyconvert_add_rule (" sympy.core.power:Pow" , Symbolics. Num, pyconvert_rule_sympy_pow)
78
+ function __init__ ()
79
+ # added rules
80
+ PythonCall. pyconvert_add_rule (" sympy.core.symbol:Symbol" , Symbolics. Num, pyconvert_rule_sympy_symbol)
93
81
94
- PythonCall. pyconvert_add_rule (" sympy.core.mul:Mul " , Symbolics. Num, pyconvert_rule_sympy_mul )
82
+ PythonCall. pyconvert_add_rule (" sympy.core.power:Pow " , Symbolics. Num, pyconvert_rule_sympy_pow )
95
83
96
- PythonCall. pyconvert_add_rule (" sympy.core.add:Add " , Symbolics. Num, pyconvert_rule_sympy_add )
84
+ PythonCall. pyconvert_add_rule (" sympy.core.mul:Mul " , Symbolics. Num, pyconvert_rule_sympy_mul )
97
85
98
- PythonCall. pyconvert_add_rule (" sympy.core.relational:Equality " , Symbolics. Equation, pyconvert_rule_sympy_equality )
86
+ PythonCall. pyconvert_add_rule (" sympy.core.add:Add " , Symbolics. Num, pyconvert_rule_sympy_add )
99
87
100
- PythonCall. pyconvert_add_rule (" sympy.core.function:Derivative " , Symbolics. Num, pyconvert_rule_sympy_derivative )
88
+ PythonCall. pyconvert_add_rule (" sympy.core.relational:Equality " , Symbolics. Equation, pyconvert_rule_sympy_equality )
101
89
102
- PythonCall. pyconvert_add_rule (" sympy.core.function:Function " ,Symbolics. Num, pyconvert_rule_sympy_function )
90
+ PythonCall. pyconvert_add_rule (" sympy.core.function:Derivative " ,Symbolics. Num, pyconvert_rule_sympy_derivative )
103
91
92
+ PythonCall. pyconvert_add_rule (" sympy.core.function:Function" ,Symbolics. Num, pyconvert_rule_sympy_function)
93
+ end
104
94
105
95
106
96
end
0 commit comments