Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to wrap a sympy function as a sage function ?

Inspired by a recent question :

importing a function from sympy allows apparently to use a sympy function in sage:

sage: import sympy
sage: from sympy import sympify, sin as ssin
sage: ssin(sympify(a+b))
sin(a + b)

But this is purely cosmetic : this result does not have the methods of a Sage symbolic expression:

sage: ssin(sympify(a+b)).trig_expand()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-15-859c8e851702> in <module>()
----> 1 ssin(sympify(a+b)).trig_expand()

AttributeError: 'sin' object has no attribute 'trig_expand'
sage: ssin(sympify(a+b)).operator()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-d52ecf6bce28> in <module>()
----> 1 ssin(sympify(a+b)).operator()

AttributeError: 'sin' object has no attribute 'operator'

In fact, this is a sympy object :

sage: type(ssin(sympify(a+b)))
sin

whereas:

sage: type(sin(a+b))
<class 'sage.symbolic.expression.Expression'>

So, my question is :how toi create a Sage function that :

  • translates all its argument to the relevant sympy types

  • calls somehow the sympy functin on these arguments

  • translates back the result to Sage ?

This is done for some Sage functions, implemented by Maxima or sympy, in the Sage library. Is it possible to make this "lightly" at run time in some Sage source code without having to recompile the Sage library ?