Skip to content

Commit

Permalink
Trac #22802: Symbolic to SymPy convertion for generic function
Browse files Browse the repository at this point in the history
Implement generic function coversion from Symbolic to SymPy

{{{
sage: a = function('A')(x,y)

sage: type(A)
<class 'sage.symbolic.function_factory.NewSymbolicFunction'>

sage: type(a)
<type 'sage.symbolic.expression.Expression'>

sage: a._sympy_()
------------------------------------------------------------------------
---
NotImplementedError                       Traceback (most recent call
last)
<ipython-input-101-eeb95aeda6e6> in <module>()
----> 1 a._sympy_()

}}}

URL: https://trac.sagemath.org/22802
Reported by: mmancini
Ticket author(s): Marco Mancini
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager authored and vbraun committed Sep 10, 2017
2 parents 7de1a99 + 5a53e12 commit 90aa5a8
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions src/sage/symbolic/expression_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,10 +764,53 @@ def composition(self, ex, operator):
if f_sympy:
return f_sympy(*sympy.sympify(g, evaluate=False))
else:
from sage.symbolic.function_factory import SymbolicFunction
if isinstance(ex.operator(), SymbolicFunction):
return sympy.Function(str(f))(*g, evaluate=False)
raise NotImplementedError("SymPy function '%s' doesn't exist" % f)
return sympy.Function(str(f))(*g, evaluate=False)

def derivative(self, ex, operator):
"""
Convert the derivative of ``self`` in sympy.
INPUT:
- ``ex`` -- a symbolic expression
- ``operator`` -- operator
TESTS::
sage: var('x','y')
(x, y)
sage: f_sage = function('f_sage')(x, y)
sage: f_sympy = f_sage._sympy_()
sage: df_sage = f_sage.diff(x, 2, y, 1); df_sage
diff(f_sage(x, y), x, x, y)
sage: df_sympy = df_sage._sympy_(); df_sympy
Derivative(f_sage(x, y), x, x, y)
sage: df_sympy == f_sympy.diff(x, 2, y, 1)
True
"""
import sympy

# retrive derivated function
f = operator.function()
f_sympy = self.composition(ex, f)

# retrive order
order = operator._parameter_set
# arguments
_args = ex.arguments()

sympy_arg = []
for i, a in enumerate(_args):
gg = order.count(i)
if gg > 0:
sympy_arg.append(a)
sympy_arg.append(gg)

return f_sympy.diff(*sympy_arg)


sympy = SympyConverter()

Expand Down

0 comments on commit 90aa5a8

Please sign in to comment.