-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding an extension for SymPy -> Symbolics.jl using PythonCall.jl #957
Draft
jClugstor
wants to merge
10
commits into
JuliaSymbolics:master
Choose a base branch
from
jClugstor:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
24ea67d
Sympy to symbolics file added
jClugstor adf62f5
got rid of extra lines
jClugstor cb7e959
Fixed functions
jClugstor d9cd245
Added sympy_to_symbolics, added test file
jClugstor 1b6f2af
Added extension deps
jClugstor 254eb83
Functions shouldn't need to know what sp is
jClugstor 1fa67c9
Merge branch 'JuliaSymbolics:master' into master
jClugstor afa2903
added test set to runtests
jClugstor 8b40632
Merge remote-tracking branch 'refs/remotes/origin/master'
jClugstor 48968c0
added an end
jClugstor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
module SymPySymbolics | ||
if isdefined(Base,:get_extension) | ||
using Symbolics | ||
using PythonCall | ||
using CondaPkg | ||
else | ||
using ..Symbolics | ||
using ..PythonCall | ||
using ..CondaPkg | ||
end | ||
|
||
CondaPkg.add("sympy") | ||
|
||
sp = pyimport("sympy") | ||
|
||
# rule functions | ||
function pyconvert_rule_sympy_symbol(::Type{Symbolics.Num}, x::Py) | ||
if !pyisinstance(x,sp.Symbol) | ||
return PythonCall.pyconvert_unconverted() | ||
end | ||
name = PythonCall.pyconvert(Symbol,x.name) | ||
return PythonCall.pyconvert_return(Symbolics.variable(name)) | ||
end | ||
|
||
function pyconvert_rule_sympy_pow(::Type{Symbolics.Num}, x::Py) | ||
if !pyisinstance(x,sp.Pow) | ||
return PythonCall.pyconvert_unconverted() | ||
end | ||
expbase = pyconvert(Symbolics.Num,x.base) | ||
exp = pyconvert(Symbolics.Num,x.exp) | ||
return PythonCall.pyconvert_return(expbase^exp) | ||
end | ||
|
||
function pyconvert_rule_sympy_mul(::Type{Symbolics.Num}, x::Py) | ||
if !pyisinstance(x,sp.Mul) | ||
return PythonCall.pyconvert_unconverted() | ||
end | ||
mult = reduce(*,PythonCall.pyconvert.(Symbolics.Num,x.args)) | ||
return PythonCall.pyconvert_return(mult) | ||
end | ||
|
||
function pyconvert_rule_sympy_add(::Type{Symbolics.Num}, x::Py) | ||
if !pyisinstance(x,sp.Add) | ||
return PythonCall.pyconvert_unconverted() | ||
end | ||
sum = reduce(+, PythonCall.pyconvert.(Symbolics.Num,x.args)) | ||
return PythonCall.pyconvert_return(sum) | ||
end | ||
|
||
function pyconvert_rule_sympy_equality(::Type{Symbolics.Equation}, x::Py) | ||
if !pyisinstance(x,sp.Equality) | ||
return PythonCall.pyconvert_unconverted() | ||
end | ||
rhs = pyconvert(Symbolics.Num,x.rhs) | ||
lhs = pyconvert(Symbolics.Num,x.lhs) | ||
return PythonCall.pyconvert_return(rhs ~ lhs) | ||
end | ||
|
||
function pyconvert_rule_sympy_derivative(::Type{Symbolics.Num}, x::Py) | ||
if !pyisinstance(x,sp.Derivative) | ||
return PythonCall.pyconvert_unconverted() | ||
end | ||
variables = pyconvert.(Symbolics.Num,x.variables) | ||
derivatives = prod(var -> Differential(var), variables) | ||
expr = pyconvert(Symbolics.Num, x.expr) | ||
return PythonCall.pyconvert_return(derivatives(expr)) | ||
end | ||
|
||
|
||
function pyconvert_rule_sympy_function(::Type{Symbolics.Num}, x::Py) | ||
if !pyisinstance(x,sp.Function) | ||
return PythonCall.pyconvert_unconverted() | ||
end | ||
name = pyconvert(String,x.name) | ||
args = pyconvert.(Symbolics.Num,x.args) | ||
symbolexpr = Expr(:call,Symbol(name),args...) | ||
symbolicexpr = Num(parse_expr_to_symbolic(symbolexpr,Main)) | ||
return PythonCall.pyconvert_return(symbolicexpr) | ||
end | ||
|
||
# added rules | ||
PythonCall.pyconvert_add_rule("sympy.core.power:Pow", Symbolics.Num, pyconvert_rule_sympy_pow) | ||
|
||
PythonCall.pyconvert_add_rule("sympy.core.symbol:Symbol", Symbolics.Num, pyconvert_rule_sympy_symbol) | ||
|
||
PythonCall.pyconvert_add_rule("sympy.core.mul:Mul", Symbolics.Num, pyconvert_rule_sympy_mul) | ||
|
||
PythonCall.pyconvert_add_rule("sympy.core.add:Add", Symbolics.Num, pyconvert_rule_sympy_add) | ||
|
||
PythonCall.pyconvert_add_rule("sympy.core.relational:Equality", Symbolics.Equation, pyconvert_rule_sympy_equality) | ||
|
||
PythonCall.pyconvert_add_rule("sympy.core.function:Derivative",Symbolics.Num, pyconvert_rule_sympy_derivative) | ||
|
||
PythonCall.pyconvert_add_rule("sympy.core.function:Function",Symbolics.Num, pyconvert_rule_sympy_function) | ||
|
||
# little tests | ||
PythonCall.pyconvert(Symbolics.Num, sp.sympify("t")) | ||
|
||
PythonCall.pyconvert(Symbolics.Num, sp.sympify("t**t")) | ||
|
||
PythonCall.pyconvert(Symbolics.Num, sp.sympify("t + z + d")) | ||
|
||
PythonCall.pyconvert(Symbolics.Num, sp.sympify("t*z*9")) | ||
|
||
PythonCall.pyconvert(Symbolics.Num, sp.sympify("5*t*z + 3*d + h/(b*5)")) | ||
|
||
PythonCall.pyconvert(Symbolics.Num, sp.sympify("t * n/z * t**4 * h**z + l*h - j")) | ||
|
||
PythonCall.pyconvert(Symbolics.Equation, sp.sympify("Eq(2,5, evaluate = False)")) | ||
|
||
PythonCall.pyconvert(Symbolics.Equation, sp.sympify("Eq(t*x + 5**x + 20/t, 90/t + t**4 - t*z)")) | ||
|
||
PythonCall.pyconvert(Symbolics.Num, sp.sympify("Function('f')(x)")) | ||
|
||
PythonCall.pyconvert(Symbolics.Num, sp.sympify("f(x,y)")) | ||
|
||
PythonCall.pyconvert(Symbolics.Equation, sp.sympify("Eq(f(x), 2*x +1)")) | ||
|
||
PythonCall.pyconvert(Symbolics.Num,sp.sympify("Derivative(f(x),x)")) | ||
|
||
PythonCall.pyconvert(Symbolics.Num,sp.sympify("Eq(Derivative(f(x),x), x")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add to the tests? |
||
|
||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That shouldn't happen in an extension