Installing Python 3.10 and sympy 1.10.1 was very tricky. First Ubuntu 20.04 came with Python 3.8 and it was struggle to make it use Python 3.10 instead. After 2 hrs trying many commands, finally it seems to be using 3.10 now. Next did the following
>which python /bin/python >python --version Python 3.10.4
Now
python -m pip install sympy
Gives
Defaulting to user installation because normal site-packages is not writeable Collecting sympy Using cached sympy-1.10.1-py3-none-any.whl (6.4 MB) Collecting mpmath>=0.19 Using cached mpmath-1.2.1-py3-none-any.whl (532 kB) Installing collected packages: mpmath, sympy Successfully installed mpmath-1.2.1 sympy-1.10.1
And now
>python Python 3.10.4 (main, Mar 24 2022, 16:12:56) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sympy >>> sympy.__version__ '1.10.1'
Solve
from sympy import * x = symbols('x') y = Function('y') ode = Eq(Derivative(y(x),x),1+2*x) sol = dsolve(ode,y(x)) # Eq(y(x), C1 + x**2 + x) checkodesol(ode,sol) # (True, 0) if checkodesol(ode,sol)[0]==True: print ('verified solution OK')
Solve
import sympy x = sympy.symbols('x') y = sympy.Function('y') ode = sympy.Eq(sympy.Derivative(y(x),x),1+2*x) sol = sympy.dsolve(ode,y(x),ics={y(0):3}) # Eq(y(x), x**2 + x + 3) sympy.checkodesol(ode,sol) # (True, 0)
Solve
from sympy import Function,dsolve,Derivative,Eq x=sympy.symbols('x') y=sympy.symbols('y', cls=Function) ode=Eq(Derivative(y(x), x, x) + 9*y(x),0) dsolve(ode, y(x))
gives
Solve
import sympy x = sympy.symbols('x') y = sympy.Function('y') ode = sympy.Eq(sympy.Derivative(y(x),x),1+2*x) sol = sympy.dsolve(ode,y(x),ics={y(0):3}) # Eq(y(x), x**2 + x + 3) sympy.latex(sol)
PDE solving is still limited in sympy. Here is how to solve first order pde
Solve
import sympy as sp x,t = sp.symbols('x t') u = sp.Function('u') pde = sp.Eq( sp.diff(u(x,t),t) , sp.diff(u(x,t),x)) sol = sp.pdsolve(pde) sp.latex(sol)
import sympy x = sympy.symbols('x') y = sympy.Function('y') expr = sympy.Derivative(y(x),x) type(expr) is sympy.Derivative #True if type(expr) is sympy.Derivative: print("yes") #yes
This also works, which seems to be the more prefered way
isinstance(expr,sympy.Derivative) #True
Suppose one passes
def process(the_function): print("the function argument is ", the_function.args[0]) print("the function name itself is ", the_function.name) import sympy x = sympy.symbols('x') y = sympy.Function('y') process(y(x))
This prints
the function argument is x the function name itself is y