Show Menu
Cheatography

Sympy Cheat Sheet (DRAFT) by

A cheat sheet for sympy library

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Basic Operations

expr.s­ubs­([(x, 2), (y, 4), (z, 0)])
substitute x with 2 etc.
sympif­y(s­tr_­expr)
convert strings into SymPy expres­sions
expr.e­val­f(15, chop=True)
evaluate a numerical expression into a floating point number
lambdi­fy(x, expr, ”numpy”)
converts the SymPy names to the names of the given numerical library
init_p­rin­ting()
This will automa­tically enable the best printer available in your enviro­nment.
simpli­fy(­expr)
simplify mathem­atical expres­sions
expand­(expr)
expand polynomial expres­sions
factor­(expr)
takes a polynomial and factors it into irredu­cible factors over the rational numbers
factor­_li­st(­expr)
returns a list with the factors. More struct­ured.
collec­t(expr, x)
collects common powers of a term in an expression
cancel­(expr)
take any rational function and put it into the standard canonical form
apart(­expr)
performs a partial fraction decomp­osition on a rational function

Matrices

Matrix([1, 2, 3])
matrix constr­uct­or(­mutable matrix)
shape(­expr)
shape of matrix
M.row(0)
get the first row
M.col(-1)
get the last column
M.col_­del(0)
delete first column
M.row_­del(1)
delete second row
M.row_­ins­ert(1, Matrix­([[0, 4]]))
insert a row
M.col_­ins­ert(0, Matrix([1, -2]))
insert a column
M**-1
inverse of M
M.T
transpose of M
eye(n)
create a nxn identity matrix
zeros(n,m)
creates a nxm matrix of zeroes
ones(n,m)
creates a nxm matrix of ones
diag(expr)
creates a matrix with expr in the diagonal
M.det()
computes the determ­inant of M
M.rref()
put a matrix into reduced row echelon form
M.null­space()
returns a list of column vectors that span the nullspace of the matrix
M.colu­mns­pace()
returns a list of column vectors that span the column­space of the matrix
M.eige­nvals()
eigenvals returns a dictionary of eigenv­alue: algebr­aic­_mu­lti­plicity pairs
M.eige­nve­cts()
returns a list of tuples of the form (eigen­value, algebr­aic­_mu­lti­pli­city, [eigen­vec­tors])
M.diag­ona­lize()
returns a tuple (P, D), where D is diagonal and M = P DP **−1
M.char­pol­y(l­amda)
return the charac­ter­istic polynomial
 

Trigon­ometric Simpli­fic­ation

trigsi­mp(­expr)
simplify expres­sions using trigon­ometric identities
expand­_tr­ig(­expr)
expand trigon­ometric functions

Powers

powsim­p(expr)
use power identities
expand­_po­wer­_ex­p(x**(a + b))
x**a * x**b
expand­_po­wer­_ba­se((xy)*a)
x**a * y**a
powden­est((xa)b)powd­ene­st((xa)b)
x**(a*b)

Expone­ntials and logarithms

expand­_lo­g(expr)
logcom­bin­e(expr)

Special Functions

factor­ial(n)
return the factorial of n
binomi­al(n, k)
return the binomial coeffi­cient of n and k
gamma(z)
return the gamma function
expr.r­ewr­ite­(fu­nction)
rewrite expr in terms of function
expand­_fu­nc(­expr)
expand special functions
hypere­xpa­nd(­expr)
rewrite hyper in terms of more standard functions
combsi­mp(­expr)
simplify combin­atorial expres­sions
gammas­imp­(expr)
simplify expres­sions with gamma functions or combin­atorial functions

Assump­tions

positive
negative
real
complex
integer
expr.a­ssu­mpt­ions0
The full set of known predicates for a symbol
posify­(expr)
replace all symbols in an expression with symbols that have the assumption positi­ve=True
 

Calculus

diff(expr, x, n)
nth order derivative of expr in terms of x
Deriva­tiv­e(expr, x, n)
create an uneval­uated derivative
deriv.d­oit()
evaluate an uneval­uated derivative
integr­ate­(expr, x, a, b)
integrate expr from a to b
Integr­al(­expr, x, n)
create an uneval­uated integral
limit(­expr, x, xo)
limit of expr to xo
Limit(­expr, x, xo)
create an uneval­uated limit
expr.s­eri­es(x, x0, n)
nth order series expansion of expr around x0
expr.s­eri­es(x, x0, n).rem­oveO()
remove O notation
differ­ent­iat­e_f­ini­te(­expr)
differ­entiate using finite differ­ences
expr.a­s_f­ini­te_­dif­fer­ence()
generate approx­ima­tions of the derivative to arbitrary order

Solvers

solves­et(­expr, x, domain­=S.C­om­plexes, dict=F­alse)
solve expr=0
linsol­ve(­[expr1, expr2, ...], (x, y, ...))
solve a linear system of equations
nonlin­sol­ve(­[expr1, expr2, ...], [x, y, ...])
solve a non linear system of equations
dsolve­(di­ffeq, f(x))
soves differ­ential equation diffeq
roots(­expr, x)
o get the solutions of a polynomial including multip­licity