Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Merge branch 'public/22802_sympy_abstract_function' of git://trac.sag…
Browse files Browse the repository at this point in the history
…emath.org/sage into public/22802_sympy_abstract_function
  • Loading branch information
Travis Scrimshaw committed Sep 7, 2017
2 parents f34394d + 0be1d90 commit 7a6cb0a
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build/pkgs/sympy/package-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.p2
1.0.p3
59 changes: 59 additions & 0 deletions build/pkgs/sympy/patches/02_undeffun_sage.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
diff --git a/sympy/core/function.py b/sympy/core/function.py
index 4c4ce9c..63c52da 100644
--- a/sympy/core/function.py
+++ b/sympy/core/function.py
@@ -746,8 +746,21 @@ def _eval_as_leading_term(self, x):
def _sage_(self):
import sage.all as sage
fname = self.func.__name__
- func = getattr(sage, fname)
+ func = getattr(sage, fname,None)
args = [arg._sage_() for arg in self.args]
+
+ # In the case the function is not known in sage:
+ if func is None:
+ import sympy
+ if getattr(sympy, fname,None) is None:
+ # abstract function
+ return sage.function(fname)(*args)
+
+ else:
+ # the function defined in sympy is not known in sage
+ # this exception is catched in sage
+ raise AttributeError
+
return func(*args)


diff --git a/sympy/external/tests/test_sage.py b/sympy/external/tests/test_sage.py
index bc404b6..ed41d42 100644
--- a/sympy/external/tests/test_sage.py
+++ b/sympy/external/tests/test_sage.py
@@ -222,6 +222,19 @@ def test_undefined_function():
assert bool(sf(sx) == f(x)._sage_())
#assert bool(f == sympy.sympify(sf))

+def test_abstract_function():
+ from sage.symbolic.expression import Expression
+ x,y = sympy.symbols('x y')
+ f = sympy.Function('f')
+ expr = f(x,y)
+ sexpr = expr._sage_()
+ assert isinstance(sexpr,Expression), "converted expression %r is not sage expression" % sexpr
+ # This test has to be uncommented in the future: it depends on the sage ticket #22802 (https://trac.sagemath.org/ticket/22802)
+ # invexpr = sexpr._sympy_()
+ # assert invexpr == expr, "inverse coversion %r is not correct " % invexpr
+
+
+
# This string contains Sage doctests, that execute all the functions above.
# When you add a new function, please add it here as well.
"""
@@ -244,6 +257,7 @@ def test_undefined_function():
sage: test_issue_4023()
sage: test_integral()
sage: test_undefined_function()
+ sage test_abstract_function()

Sage has no symbolic Lucas function at the moment::

65 changes: 59 additions & 6 deletions src/sage/symbolic/expression_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,15 +603,15 @@ def derivative(self, ex, operator):
subs = ["%s = %s"%(t._maxima_init_(),a._maxima_init_()) for t,a in zip(temp_args,args)]
outstr = "at(diff(%s, %s), [%s])"%(f._maxima_init_(),
", ".join(params),
", ".join(subs))
", ".join(subs))
else:
f = operator.function()(*args)
params = operator.parameter_set()
params = ["%s, %s"%(args[i]._maxima_init_(), params.count(i)) for i in set(params)]
outstr = "diff(%s, %s)"%(f._maxima_init_(),
", ".join(params))
return outstr

def arithmetic(self, ex, operator):
"""
EXAMPLES::
Expand Down Expand Up @@ -674,7 +674,7 @@ class SympyConverter(Converter):
sage: sympy.sympify(x) # indirect doctest
x
TESTS:
TESTS::
Make sure we can convert I (:trac:`6424`)::
Expand Down Expand Up @@ -764,7 +764,61 @@ def composition(self, ex, operator):
if f_sympy:
return f_sympy(*sympy.sympify(g, evaluate=False))
else:
raise NotImplementedError("SymPy function '%s' doesn't exist" % f)
from sage.symbolic.function_factory import SymbolicFunction
if isinstance(ex.operator(), SymbolicFunction):
return sympy.Function(str(f))(*g, evaluate=False)


def derivative(self, ex, operator):
"""
Convert the derivative in sympy
INPUT:
- ``ex`` -- a symbolic expression
- ``operator`` -- operator
TESTS::
sage: import sympy
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 aa in range(len(_args)):
gg = order.count(aa)
if gg > 0 :
sympy_arg.append(_args[aa])
sympy_arg.append(gg)

return f_sympy.diff(*sympy_arg)



sympy = SympyConverter()

Expand Down Expand Up @@ -1769,7 +1823,7 @@ def arithmetic(self, ex, operator):
if operator == add_vararg:
operator = _operator.add
elif operator == mul_vararg:
operator = _operator.mul
operator = _operator.mul
return reduce(operator, map(self, operands))

def composition(self, ex, operator):
Expand Down Expand Up @@ -2026,4 +2080,3 @@ def composition(self, ex, operator):
return operator(*map(self, ex.operands()), hold=True)
else:
return operator(*map(self, ex.operands()))

4 changes: 1 addition & 3 deletions src/sage/symbolic/function.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,7 @@ cdef class Function(SageObject):
sage: g._sympy_init_()
'gg'
sage: g(x)._sympy_()
Traceback (most recent call last):
...
NotImplementedError: SymPy function 'gg' doesn't exist
gg(x)
"""
return self._conversions.get('sympy', self._name)

Expand Down
6 changes: 3 additions & 3 deletions src/sage/tests/french_book/recequadiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,10 @@
sage: from sympy import Function, Symbol
sage: u = Function('u'); n = Symbol('n', integer=True)
Sage example in ./recequadiff.tex, line 1208::
Sage example in ./recequadiff.tex, line 1208 (WARNING: the order of factors is
inverted, see :trac:`23496` )::
sage: f = u(n+2)-(3/2)*u(n+1)+(1/2)*u(n)
sage: f = u(n+2)-u(n+1)*(3/2)+u(n)*(1/2)
Sage example in ./recequadiff.tex, line 1214::
Expand All @@ -396,4 +397,3 @@
2**n*C0 + 2**(n + 2)*(C0 + n/2)
"""

0 comments on commit 7a6cb0a

Please sign in to comment.