SymPy –> Sage conversion - Interpreter Interfaces

Skip to content
Back to top
View this page
Edit this page

# SymPy –> Sage conversion

The file consists of `_sage_()` methods that are added lazily to the respective SymPy objects. Any call of the `_sympy_()` method of a symbolic expression will trigger the addition. See `sage.symbolic.expression_conversion.SymPyConverter` for the conversion to SymPy.

Only `Function` objects where the names differ need their own `_sage()_` method. There are several functions with differing name that have an alias in Sage that is the same as the name in SymPy, so no explicit translation is needed for them:

Sage

    sage: from sympy import Symbol, Si, Ci, Shi, Chi, sign
    sage: sx = Symbol('x')
    sage: assert sin_integral(x)._sympy_() == Si(sx)
    sage: assert sin_integral(x) == Si(sx)._sage_()
    sage: assert sinh_integral(x)._sympy_() == Shi(sx)
    sage: assert sinh_integral(x) == Shi(sx)._sage_()
    sage: assert cos_integral(x)._sympy_() == Ci(sx)
    sage: assert cos_integral(x) == Ci(sx)._sage_()
    sage: assert cosh_integral(x)._sympy_() == Chi(sx)
    sage: assert cosh_integral(x) == Chi(sx)._sage_()
    sage: assert sgn(x)._sympy_() == sign(sx)
    sage: assert sgn(x) == sign(sx)._sage_()

Python

    >>> from sage.all import *
    >>> from sympy import Symbol, Si, Ci, Shi, Chi, sign
    >>> sx = Symbol('x')
    >>> assert sin_integral(x)._sympy_() == Si(sx)
    >>> assert sin_integral(x) == Si(sx)._sage_()
    >>> assert sinh_integral(x)._sympy_() == Shi(sx)
    >>> assert sinh_integral(x) == Shi(sx)._sage_()
    >>> assert cos_integral(x)._sympy_() == Ci(sx)
    >>> assert cos_integral(x) == Ci(sx)._sage_()
    >>> assert cosh_integral(x)._sympy_() == Chi(sx)
    >>> assert cosh_integral(x) == Chi(sx)._sage_()
    >>> assert sgn(x)._sympy_() == sign(sx)
    >>> assert sgn(x) == sign(sx)._sage_()

AUTHORS:

- Ralf Stephan (2017-10)

class sage.interfaces.sympy.UndefSageHelper

  Bases: `object`

  Helper class to convert sympy function objects to sage functions

  EXAMPLES:

  Sage

      sage: from sympy import Function
      sage: f = function('f')
      sage: F = Function('f')
      sage: assert f._sympy_() == F
      sage: assert f == F._sage_()

  Python

      >>> from sage.all import *
      >>> from sympy import Function
      >>> f = function('f')
      >>> F = Function('f')
      >>> assert f._sympy_() == F
      >>> assert f == F._sage_()

sage.interfaces.sympy.check_all()

  Call some tests that were originally in SymPy.

  EXAMPLES:

  Sage

      sage: from sage.interfaces.sympy import check_all
      sage: check_all()

  Python

      >>> from sage.all import *
      >>> from sage.interfaces.sympy import check_all
      >>> check_all()

sage.interfaces.sympy.check_expression(expr, var_symbols, only_from_sympy=False)

  Do `eval(expr)` both in Sage and SymPy and other checks.

  EXAMPLES:

  Sage

      sage: from sage.interfaces.sympy import check_expression
      sage: check_expression("1.123*x", "x")

  Python

      >>> from sage.all import *
      >>> from sage.interfaces.sympy import check_expression
      >>> check_expression("1.123*x", "x")

sage.interfaces.sympy.sympy_init(*args, **kwargs)

  Add `_sage_()` methods to SymPy objects where needed.

  This gets called with every call to `Expression._sympy_()` so there is only need to call it if you bypass `_sympy_()` to create SymPy objects. Note that SymPy objects have `_sage_()` methods hard installed but having them inside Sage as one file makes them easier to maintain for Sage developers.

  EXAMPLES:

  Sage

      sage: from sage.interfaces.sympy import sympy_init
      sage: from sympy import Symbol, Abs
      sage: sympy_init()
      sage: assert abs(x) == Abs(Symbol('x'))._sage_()

  Python

      >>> from sage.all import *
      >>> from sage.interfaces.sympy import sympy_init
      >>> from sympy import Symbol, Abs
      >>> sympy_init()
      >>> assert abs(x) == Abs(Symbol('x'))._sage_()

sage.interfaces.sympy.sympy_set_to_list(set, vars)

  Convert all set objects that can be returned by SymPy’s solvers.