Ask Your Question
0

Sage's Python not giving the expected result

asked 2023-10-03 01:38:40 +0100

JC gravatar image

Hello to everyone, When I run the code

import sympy as sp
x=sp.symbols('x')
y=sp.Function('y')(x)
eq = sp.Eq(sp.diff(y,x,x)+sp.diff(y,x)+9.25*y,0)  
y=sp.dsolve(eq,y)
y

in a Jupyter notebook launched from Sage, but with the kernel specified to be a Python kernel, I do not get the result I expect.

The same code in a jupyter notebook from anaconda-navigator, of in a jupyter notebook at CoCalc gives the correct result though. The error message I get is quite cryptic, but it starts as in the attached figure.

I am running sage 10.0 on a ubuntu 20.04 machine.

Any hint?

image description

Thanks in advance, JC

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-10-03 08:29:15 +0100

Emmanuel Charpentier gravatar image

updated 2023-10-03 21:35:51 +0100

WorksForMe(TM) on 10.2.beta5 :

sage: y=function("y")
sage: de=diff(y(x),x,2)+diff(y(x),x)+37/4*y(x)==0
sage: desolve(de, y(x), ivar=x)
(_K2*cos(3*x) + _K1*sin(3*x))*e^(-1/2*x)
sage: import sympy as sp
sage: sp.dsolve(*map(sp.sympify, (de, y(x))))
Eq(y(x), (C1*sin(3*x) + C2*cos(3*x))*exp(-x/2))

What version of Sage do you use ?

Note : check your sympy's syntax, possibly by working in a Python REPL...

EDIT : As pointed out in a comment, your Python code works in a Python REPL (modulo choice of floats vs rationals) :

Python 3.11.5 (main, Aug 29 2023, 15:31:31) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sympy as sp
x=sp.symbols('x')
y=sp.Function('y')(x)
eq = sp.Eq(sp.diff(y,x,x)+sp.diff(y,x)+9.25*y,0)  
y=sp.dsolve(eq,y)
y
>>> >>> >>> >>> >>> Eq(y(x), (C1*sin(3.0*x) + C2*cos(3.0*x))*exp(-0.5*x))
>>>

HTH,

edit flag offensive delete link more

Comments

Hello, As I pointed out, I am using sage 10.0. However, my problem is not with sage, instead with a python-equipped Jupyter notebook. I have a python code problem, but it only arises when I run it from the sage provided jupyter noteook. What is a Python REPL?

JC gravatar imageJC ( 2023-10-03 16:50:34 +0100 )edit

What is a Python REPL?

Any program goiving you access to a Python read-eval-print loop. Such as the command line interpreter, a Python notebook, emacs' inferior Python, etc...

BTW, cut 'n paste of your code in a Python REPL gives the same result as in Sage (modulo the replacement of exact fractions by floatoing point numers) :

Python 3.11.5 (main, Aug 29 2023, 15:31:31) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sympy as sp
x=sp.symbols('x')
y=sp.Function('y')(x)
eq = sp.Eq(sp.diff(y,x,x)+sp.diff(y,x)+9.25*y,0)  
y=sp.dsolve(eq,y)
y
>>> >>> >>> >>> >>> Eq(y(x), (C1*sin(3.0*x) + C2*cos(3.0*x))*exp(-0.5*x))
>>>

HTH,

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-10-03 21:31:39 +0100 )edit

This fails for me with Sage's Python, both from the command line and from the notebook. The error at the end is TypeError: cannot create mpf from mpz(4).

John Palmieri gravatar imageJohn Palmieri ( 2023-10-03 21:52:16 +0100 )edit

So we must conclude that Sage is not a Python REPL? This does not sound very good to me, but is highly subjective, and disappointing : I was trying to sell to my enginnering students the idea that they could do many things in sage, inlcuding what they know in Python... Too bad.

JC gravatar imageJC ( 2023-10-05 02:35:18 +0100 )edit

No, we might conclude that there is a bug in Sage. Sage's Python should be a fully functional version of Python. Possibly related: https://github.com/sagemath/sage/issu....

John Palmieri gravatar imageJohn Palmieri ( 2023-10-05 05:30:53 +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: 2023-10-03 01:38:40 +0100

Seen: 176 times

Last updated: Oct 03 '23