Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" 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 :class:`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: 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_()
AUTHORS:
- Ralf Stephan (2017-10) """ ################################################################ # Distributed under GNU GPL3, see www.gnu.org ################################################################
################# numbers and constants ##############
""" EXAMPLES::
sage: from sympy.core.numbers import RealNumber as RN sage: assert SR(-1.34)._sympy_() == RN('-1.34') sage: assert SR(-1.34) == RN('-1.34')._sage_() """
""" EXAMPLES::
sage: from sympy.core.numbers import Rational sage: assert SR(-5/7)._sympy_() == Rational(int(-5),int(7)) sage: assert SR(-5/7) == Rational(int(-5),int(7))._sage_() """
""" EXAMPLES::
sage: from sympy.core.numbers import oo as sinf sage: assert SR(oo)._sympy_() == sinf sage: assert SR(oo) == sinf._sage_() """
""" EXAMPLES::
sage: from sympy.core.numbers import oo as sinf sage: assert SR(-oo)._sympy_() == -sinf sage: assert SR(-oo) == (-sinf)._sage_() """
""" EXAMPLES::
sage: from sympy.core.numbers import zoo sage: assert unsigned_infinity._sympy_() == zoo sage: assert unsigned_infinity == zoo._sage_() """
""" EXAMPLES::
sage: from sympy.core.numbers import nan as snan sage: assert NaN._sympy_() == snan sage: assert NaN == snan._sage_() """
""" EXAMPLES::
sage: from sympy.core.numbers import E sage: assert e._sympy_() == E sage: assert e == E._sage_() """
""" EXAMPLES::
sage: from sympy.core.numbers import pi as spi sage: assert pi._sympy_() == spi sage: assert pi == spi._sage_() """
""" EXAMPLES::
sage: from sympy.core.singleton import S sage: assert golden_ratio._sympy_() == S.GoldenRatio sage: assert golden_ratio == S.GoldenRatio._sage_() """
""" EXAMPLES::
sage: from sympy.core.singleton import S sage: assert euler_gamma._sympy_() == S.EulerGamma sage: assert euler_gamma == S.EulerGamma._sage_() """
""" EXAMPLES::
sage: from sympy.core.singleton import S sage: assert catalan._sympy_() == S.Catalan sage: assert catalan == S.Catalan._sage_() """
""" EXAMPLES::
sage: from sympy.core.singleton import S sage: assert I._sympy_() == S.ImaginaryUnit sage: assert I == S.ImaginaryUnit._sage_() """
################## basic operators ##############
""" EXAMPLES::
sage: from sympy import Symbol sage: from sympy.core.singleton import S sage: assert (x-pi+1)._sympy_() == Symbol('x')-S.Pi+1 sage: assert x-pi+1 == (Symbol('x')-S.Pi+1)._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol sage: from sympy.core.singleton import S sage: assert (-x*pi*5)._sympy_() == -Symbol('x')*S.Pi*5 sage: assert -x*pi*5 == (-Symbol('x')*S.Pi*5)._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol sage: from sympy.core.singleton import S sage: assert (x^pi^5)._sympy_() == Symbol('x')**S.Pi**5 sage: assert x^pi^5 == (Symbol('x')**S.Pi**5)._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol sage: assert x._sympy_() == Symbol('x') sage: assert x == Symbol('x')._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol sage: from sympy.core.singleton import S """
args = self.args substi = dict([(args[1][i]._sage_(),args[2][i]._sage_()) for i in range(len(args[1]))])
return args[0]._sage_().subs(substi)
############## functions ###############
""" EXAMPLES::
sage: from sympy import Symbol, Function, sin as Sin sage: assert sin(x)._sympy_() == Sin(Symbol('x')) sage: assert sin(x) == Sin(Symbol('x'))._sage_()
sage: f = function('f') sage: F = Function('f') sage: assert f(x)._sympy_() == F(x) sage: assert f(x) == F(x)._sage_() sage: assert f(x+3)._sympy_() == F(x+3) sage: assert f(x+3) == F(x+3)._sage_() sage: assert (3*f(x))._sympy_() == 3*F(x) sage: assert 3*f(x) == (3*F(x))._sage_()
Test that functions unknown to Sage raise an exception::
sage: from sympy.functions.combinatorial.numbers import lucas sage: lucas(Symbol('x'))._sage_() Traceback (most recent call last): ... AttributeError... """
# In the case the function is not known in sage: # abstract function from sage.calculus.var import function return function(fname)(*args)
else: # the function defined in sympy is not known in sage
""" EXAMPLES::
sage: from sympy import Symbol, Integral sage: sx = Symbol('x') sage: assert integral(x, x, hold=True)._sympy_() == Integral(sx, sx) sage: assert integral(x, x, hold=True) == Integral(sx, sx)._sage_() sage: assert integral(x, x, 0, 1, hold=True)._sympy_() == Integral(sx, (sx,0,1)) sage: assert integral(x, x, 0, 1, hold=True) == Integral(sx, (sx,0,1))._sage_() """ x, b = limit f = integral(f, x._sage_(), b._sage_(), hold=True) else:
""" EXAMPLES::
sage: from sympy import Derivative sage: f = function('f') sage: sympy_diff = Derivative(f(x)._sympy_(), x._sympy_()) sage: assert diff(f(x),x)._sympy_() == sympy_diff sage: assert diff(f(x),x) == sympy_diff._sage_() """
""" EXAMPLES::
sage: from sage.functions.other import Order sage: from sympy.series import Order as SOrder sage: assert Order(1)._sympy_() == SOrder(1) sage: assert Order(1) == SOrder(1)._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, LambertW sage: assert lambert_w(x)._sympy_() == LambertW(0, Symbol('x')) sage: assert lambert_w(x) == LambertW(Symbol('x'))._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, rf sage: _ = var('x, y') sage: rfxy = rf(Symbol('x'), Symbol('y')) sage: assert rising_factorial(x,y)._sympy_() == rfxy.rewrite('gamma') sage: assert rising_factorial(x,y) == rfxy._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, ff sage: _ = var('x, y') sage: ffxy = ff(Symbol('x'), Symbol('y')) sage: assert falling_factorial(x,y)._sympy_() == ffxy.rewrite('gamma') # known bug sage: assert falling_factorial(x,y) == ffxy._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, loggamma sage: assert log_gamma(x)._sympy_() == loggamma(Symbol('x')) sage: assert log_gamma(x) == loggamma(Symbol('x'))._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, polygamma as pg sage: _ = var('x, y') sage: pgxy = pg(Symbol('x'), Symbol('y')) sage: assert psi(x)._sympy_() == pg(0, Symbol('x')) sage: assert psi(x) == pg(0, Symbol('x'))._sage_() sage: assert psi(x,y)._sympy_() == pgxy sage: assert psi(x,y) == pgxy._sage_() sage: integrate(psi(x), x, algorithm='sympy') integrate(psi(x), x) """
""" EXAMPLES::
sage: from sympy import Symbol, DiracDelta sage: assert dirac_delta(x)._sympy_() == DiracDelta(Symbol('x')) sage: assert dirac_delta(x) == DiracDelta(Symbol('x'))._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, Heaviside sage: assert heaviside(x)._sympy_() == Heaviside(Symbol('x')) sage: assert heaviside(x) == Heaviside(Symbol('x'))._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, expint sage: _ = var('x, y') sage: sy = expint(Symbol('x'), Symbol('y')) sage: assert exp_integral_e(x,y)._sympy_() == sy sage: assert exp_integral_e(x,y) == sy._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, hyper sage: _ = var('a,b,p,q,x') sage: sy = hyper((Symbol('a'), Symbol('b')), (Symbol('p'), Symbol('q')), Symbol('x')) sage: assert hypergeometric((a,b),(p,q),x)._sympy_() == sy sage: assert hypergeometric((a,b),(p,q),x) == sy._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, elliptic_k sage: assert elliptic_kc(x)._sympy_() == elliptic_k(Symbol('x')) sage: assert elliptic_kc(x) == elliptic_k(Symbol('x'))._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, KroneckerDelta sage: _ = var('x, y') sage: sy = KroneckerDelta(Symbol('x'), Symbol('y')) sage: assert kronecker_delta(x,y)._sympy_() == sy sage: assert kronecker_delta(x,y) == sy._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, ceiling sage: assert ceil(x)._sympy_() == ceiling(Symbol('x')) sage: assert ceil(x) == ceiling(Symbol('x'))._sage_() sage: integrate(ceil(x), x, 0, infinity, algorithm='sympy') integrate(ceil(x), x, 0, +Infinity) """
""" EXAMPLES::
sage: from sympy import Symbol, pi as spi, Eq, Lt, Piecewise sage: sx = Symbol('x') sage: sp = Piecewise((spi, Lt(sx,0)), (1, Eq(sx,1)), (0, True)) sage: ex = cases(((x<0, pi), (x==1, 1), (True, 0))) sage: assert ex._sympy_() == sp sage: assert ex == sp._sage_()
sage: _ = var('y, z') sage: (x^y - z).integrate(y, algorithm="sympy") -y*z + cases(((log(x) == 0, y), (1, x^y/log(x)))) """
""" EXAMPLES::
sage: from sympy import Symbol, besselj sage: _ = var('x, y') sage: sy = besselj(Symbol('x'), Symbol('y')) sage: assert bessel_J(x,y)._sympy_() == sy sage: assert bessel_J(x,y) == sy._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, bessely sage: _ = var('x, y') sage: sy = bessely(Symbol('x'), Symbol('y')) sage: assert bessel_Y(x,y)._sympy_() == sy sage: assert bessel_Y(x,y) == sy._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, besseli sage: _ = var('x, y') sage: sy = besseli(Symbol('x'), Symbol('y')) sage: assert bessel_I(x,y)._sympy_() == sy sage: assert bessel_I(x,y) == sy._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, besselk sage: _ = var('x, y') sage: sy = besselk(Symbol('x'), Symbol('y')) sage: assert bessel_K(x,y)._sympy_() == sy sage: assert bessel_K(x,y) == sy._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, Ynm sage: _ = var('n,m,t,p') sage: sy = Ynm(Symbol('n'), Symbol('m'), Symbol('t'), Symbol('p')) sage: assert spherical_harmonic(n,m,t,p)._sympy_() == sy sage: assert spherical_harmonic(n,m,t,p) == sy._sage_() """ self.args[1]._sage_(), self.args[2]._sage_(), self.args[3]._sage_())
""" EXAMPLES::
sage: from sympy import Symbol, re sage: assert real_part(x)._sympy_() == re(Symbol('x')) sage: assert real_part(x) == re(Symbol('x'))._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, im sage: assert imag_part(x)._sympy_() == im(Symbol('x')) sage: assert imag_part(x) == im(Symbol('x'))._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, Abs sage: assert abs(x)._sympy_() == Abs(Symbol('x')) sage: assert abs(x) == Abs(Symbol('x'))._sage_() """
""" EXAMPLES::
sage: from sympy import Symbol, CRootOf sage: sobj = CRootOf(Symbol('x')**2 - 2, 1) sage: assert complex_root_of(x^2-2, 1)._sympy_() == sobj sage: assert complex_root_of(x^2-2, 1) == sobj._sage_()
sage: from sympy import solve as ssolve sage: sols = ssolve(x^6+x+1, x) sage: (sols[0]+1)._sage_().n() 0.209332811185582 - 0.300506920309552*I """
""" EXAMPLES::
sage: from sympy import Eq, Ne, Gt, Ge, Lt, Le, Symbol sage: sx = Symbol('x') sage: assert (x == 0)._sympy_() == Eq(sx, 0) sage: assert (x == 0) == Eq(x, 0)._sage_() sage: assert (x != 0)._sympy_() == Ne(sx, 0) sage: assert (x != 0) == Ne(x, 0)._sage_() sage: assert (x > 0)._sympy_() == Gt(sx, 0) sage: assert (x > 0) == Gt(x, 0)._sage_() sage: assert (x >= 0)._sympy_() == Ge(sx, 0) sage: assert (x >= 0) == Ge(x, 0)._sage_() sage: assert (x < 0)._sympy_() == Lt(sx, 0) sage: assert (x < 0) == Lt(x, 0)._sage_() sage: assert (x <= 0)._sympy_() == Le(sx, 0) sage: assert (x <= 0) == Le(x, 0)._sage_() """
""" EXAMPLES::
sage: from sympy.logic.boolalg import BooleanFalse sage: assert SR(False)._sympy_() == BooleanFalse() # known bug sage: assert SR(False) == BooleanFalse()._sage_() """
""" EXAMPLES::
sage: from sympy.logic.boolalg import BooleanTrue sage: assert SR(True)._sympy_() == BooleanTrue() # known bug sage: assert SR(True) == BooleanTrue()._sage_() """
#------------------------------------------------------------------
def sympy_init(): """ 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: from sage.interfaces.sympy import sympy_init sage: from sympy import Symbol, Abs sage: sympy_init() sage: assert abs(x) == Abs(Symbol('x'))._sage_() """ return
NegativeInfinity, ComplexInfinity, Exp1, Pi, GoldenRatio, EulerGamma, Catalan, ImaginaryUnit) FallingFactorial)
""" Does ``eval(expr)`` both in Sage and SymPy and does other checks.
EXAMPLES::
sage: from sage.interfaces.sympy import check_expression sage: check_expression("1.123*x", "x") """ # evaluate the expression in the context of Sage:
# evaluate the expression in the context of SymPy:
# Sympy func may have specific _sage_ method
# Do the actual checks:
""" Call some tests that were originally in SymPy.
EXAMPLES::
sage: from sage.interfaces.sympy import test_all sage: test_all() """
# Sage doesn't properly implement _sympy_ on I
# Test at least one Function without own _sage_ method
#test Sympy-->Sage
# Note: sage may attempt to turn this into Integral(x, (x, x, 0)) check_expression("Integral(x, (x, 0))", "x", only_from_sympy=True) check_expression("Integral(x*y, (x,), (y, 0))", "x,y", only_from_sympy=True) check_expression("Integral(x*y, (x, 0, 1), (y, 0))", "x,y", only_from_sympy=True)
#test_integral_failing()
""" Convert all set objects that can be returned by SymPy's solvers. """ return [x._sage_() < oo for x in vars] return [x._sage_() != UnsignedInfinity for x in vars] return [] if isinstance(set, And): return [[item for rel in set._args[0] for item in sympy_set_to_list(rel, vars) ]] elif isinstance(set, Or): return [sympy_set_to_list(iv, vars) for iv in set._args[0]] elif isinstance(set, Relational): return [set._sage_()] else: rel1 = [x._sage_() >= left._sage_()] else: return [x._sage_() <= right._sage_()] return rel1 return [rel1, rel2]
|