What functionality from SymPy is incorporated in Sage

I notice that SymPy is a standard package that is installed with Sage: https://doc.sagemath.org/html/en/refe...

I am wondering what functionality in Sage is built on the SymPy library? Sage seems to have it's own variable system built from source: https://ask.sagemath.org/question/633...

I realize after I posted this, that there is probably many things included than can be listed. It looks like SymPy is an option for certain functionality.

Does anyone know if SymPy is used for any core features? As in, without SymPy these features would not exist?

Also, if there is anyway to search this out myself by knowing how to explore the source code and look for dependencies on SymPy, I would appreciate if someone could tell me how to do that.

edit retag close merge delete

Sort by » oldest newest most voted

Sympy is used as an option for integration, sums, solving equations, for the Laplace function (and inverse), and perhaps more. Usually, the way to use it is with adding a algorithm = 'sympy' option:

sage: integral(sin(x)^2, x)
1/2*x - 1/4*sin(2*x)
sage: integral(sin(x)^2, x, algorithm='sympy')
-1/2*cos(x)*sin(x) + 1/2*x

sage: var('k')
k
sage: sum(1/(1+k^2), k, -oo, oo)
-1/2*I*psi(I + 1) + 1/2*I*psi(-I + 1) - 1/2*I*psi(I) + 1/2*I*psi(-I)
sage: sum(1/(1+k^2), k, -oo, oo, algorithm = 'sympy')
pi/tanh(pi)

sage: solve(-2*x**3 + 4*x**2 - 2*x + 6 > 0, x)
[[x < 2.174559471365639]]
sage: solve(-2*x**3 + 4*x**2 - 2*x + 6 > 0, x, algorithm='sympy')
[x < 1/3*(1/2)^(1/3)*(9*sqrt(77) + 79)^(1/3) + 2/3*(1/2)^(2/3)/(9*sqrt(77) + 79)^(1/3) + 2/3]

sage: var('t, s')
(t, s)
sage: laplace(cos(t^2), t, s)
-(1/16*I - 1/16)*sqrt(2)*sqrt(pi)*cos(1/4*s^2)*erf(1/2*(-1)^(3/4)*s) - (1/16*I + 1/16)*sqrt(2)*sqrt(pi)*cos(1/4*s^2)*erf((1/4*I + 1/4)*sqrt(2)*s) - (1/16*I - 1/16)*sqrt(2)*sqrt(pi)*cos(1/4*s^2)*erf((1/4*I - 1/4)*sqrt(2)*s) - (1/16*I + 1/16)*sqrt(2)*sqrt(pi)*cos(1/4*s^2)*erf(1/2*I*sqrt(-I)*s) - (1/16*I + 1/16)*sqrt(2)*sqrt(pi)*erf(1/2*(-1)^(3/4)*s)*sin(1/4*s^2) - (1/16*I - 1/16)*sqrt(2)*sqrt(pi)*erf((1/4*I + 1/4)*sqrt(2)*s)*sin(1/4*s^2) - (1/16*I + 1/16)*sqrt(2)*sqrt(pi)*erf((1/4*I - 1/4)*sqrt(2)*s)*sin(1/4*s^2) - (1/16*I - 1/16)*sqrt(2)*sqrt(pi)*erf(1/2*I*sqrt(-I)*s)*sin(1/4*s^2) + 1/4*sqrt(2)*sqrt(pi)*cos(1/4*s^2) - 1/4*sqrt(2)*sqrt(pi)*sin(1/4*s^2)
sage: laplace(cos(t^2), t, s, algorithm='sympy')
(-1/2*sqrt(pi)*(sqrt(2)*cos(1/4*s^2)*fresnel_sin(1/2*sqrt(2)*s/sqrt(pi)) - sqrt(2)*fresnel_cos(1/2*sqrt(2)*s/sqrt(pi))*sin(1/4*s^2) - cos(1/4*pi + 1/4*s^2)),
0,
True)

Edit (answering some questions below)

• regarding integration, by default various "algorithms" (a more appropriate wording would be "libraries" or "backends") are tested one after another until some provides an answer, hence sympy can be involved in the computation of some integrals even if you do not specify algorithm='sympy' explicitely.

• to search where sympy is involved within Sage source code, you can open a terminal, go to the SAGE_ROOT/src/sage/ directory and run the command:

grep -R "sympy"

more

Thank you! I realize after I posted this, that there is probably more things included than can be listed. From what you shared, it looks like SymPy is an option for certain functionality.

Does anyone know if SymPy is used for any core features? As in, without SymPy these features would not exist?

Also, if there is anyway to search this out myself by knowing how to explore the source code and look for dependencies on SymPy, I would appreciate if someone could tell me how to do that.

( 2022-07-21 14:37:21 +0100 )edit

Regarding the last question, you can run grep -r 'from sympy' src/sage and grep -r '_sympy_' src/sage from the root of the Sage directory.

( 2022-07-22 14:24:41 +0100 )edit

In complement to @tmonteil 's answer, note that SymPy can be used as the symbolic backend for computations on manifolds:

sage: M = Manifold(2, 'M')
sage: X.<x,y> = M.chart()
sage: M.set_calculus_method('sympy')
sage: f = M.scalar_field(y*x^3)
sage: df = diff(f)
sage: df.display()
3*x**2*y dx + x**3 dy
sage: df[0].expr()
3*x**2*y
sage: type(df[0].expr())
<class 'sympy.core.mul.Mul'>

Back to Sage's default symbolic backend (SR):

sage: M.set_calculus_method('SR')
sage: w = df*df
sage: w.display()
9*x^4*y^2 dx⊗dx + 3*x^5*y dx⊗dy + 3*x^5*y dy⊗dx + x^6 dy⊗dy
sage: w[0,0].expr()
9*x^4*y^2
sage: type(w[0,0].expr())
<class 'sage.symbolic.expression.Expression'>
more

Indeed !

( 2022-07-21 18:54:21 +0100 )edit