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