Ask Your Question
1

What functionality from SymPy is incorporated in Sage

asked 2022-07-21 05:15:29 +0100

Pickle gravatar image

updated 2022-07-21 18:25:46 +0100

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 flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2022-07-21 12:41:30 +0100

tmonteil gravatar image

updated 2022-07-21 18:55:15 +0100

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"
    
edit flag offensive delete link more

Comments

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.

Pickle gravatar imagePickle ( 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.

eric_g gravatar imageeric_g ( 2022-07-22 14:24:41 +0100 )edit
2

answered 2022-07-21 15:35:34 +0100

eric_g gravatar image

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'>
edit flag offensive delete link more

Comments

Indeed !

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-07-21 05:15:29 +0100

Seen: 235 times

Last updated: Jul 21 '22