0% found this document useful (0 votes)
459 views

SymPy Cheatsheet

This document provides a cheat sheet for solving ordinary differential equations (ODEs) and partial differential equations (PDEs) using Sympy and Python. It gives code examples for how to solve first order ODEs both with and without initial conditions, convert ODE solutions to LaTeX format, check if an expression is a derivative, and extract the function name and arguments from a function object passed to a function. The cheat sheet covers common tasks for symbolic math and differential equations in Sympy.

Uploaded by

RTEFG DFGJU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
459 views

SymPy Cheatsheet

This document provides a cheat sheet for solving ordinary differential equations (ODEs) and partial differential equations (PDEs) using Sympy and Python. It gives code examples for how to solve first order ODEs both with and without initial conditions, convert ODE solutions to LaTeX format, check if an expression is a derivative, and extract the function name and arguments from a function object passed to a function. The cheat sheet covers common tasks for symbolic math and differential equations in Sympy.

Uploaded by

RTEFG DFGJU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

my sympy and python cheat sheet

Nasser M. Abbasi

July 19, 2019 Compiled on July 19, 2019 at 6:49pm

Contents
1 How to solve a first oder ODE? 1

2 How to solve a first oder ODE with initial condition? 2

3 How to solve and ODE and convert the result to latex string? 2

4 How to solve a PDE in sympy? 2

5 How to check if something is derivative? 3

6 How to find function name and its arguments in a proc? 3

1 How to solve a first oder ODE?


Solve y 0 (x) = 1 + 2x for y(x)
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))
# Eq(y(x), C1 + x**2 + x)
sympy.checkodesol(ode,sol)
# (True, 0)

1
2

2 How to solve a first oder ODE with initial


condition?
Solve y 0 (x) = 1 + 2x for y(x) with y(0) = 3
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)

3 How to solve and ODE and convert the result to


latex string?
Solve y 0 (x) = 1 + 2x for y(x) with y(0) = 3
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)

y(x) = x2 + x + 3

4 How to solve a PDE in sympy?


PDE solving is still limited in sympy. Here is how to solve first order pde
Solve ut (x, t) = ux (x, t)
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)

u(x, t) = F (t + x)
3

5 How to check if something is derivative?


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

6 How to find function name and its arguments in a


proc?
Suppose one passes y(x) to a function, and the function wants to find the name of this
function and its argument. Here is an example
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

You might also like