pianofisica

Mathematics & Physics, Maxima, a bit Python & Wolfram, and Arts

How to use Python (SymPy) through examples (Basics)

Here I'd like to share how to use a Python library "SymPy",
which is for symbolic formula manipulation.

How to use SymPy

In the following I'll show you concrete examples of input of Python (SymPy).

Although I've checked them on JupyterNotebook,
please let me know if you found any trouble.

Copy, Paste and Do it! Modify the input as you like!

Let's get it started!

Import SymPy library

First of all, one must load SymPy library:

import sympy

Addition, Subtraction, Multiplication and Division

3+2 # letters after "#" is to be ignored for evaluation
3-2
3*2
sympy.Rational(3,2)

or

3/2

Power, Factorial, Double factorial, Square root

3**2
sympy.factorial(50)
sympy.factorial2(50)
sympy.factorial2(49)
sympy.sqrt(50)

Complex number

Imaginary unit
sympy.I**2   #  capital " I "
Complex conjugate
sympy.conjugate(2+3*sympy.I)
Real part, Imaginary part
sympy.re(2+sympy.I*3)
sympy.im(2+sympy.I*3)
Absolute value
sympy.Abs(2+sympy.I*3)
Argument
sympy.arg(2+sympy.I*3)

Algebraic manipulation

Prime factorization
sympy.factorint(sympy.factorial(10))
Factorization of polynomial
sympy.var('x')
sympy.factor(x**2-4*x+3)
Partial fraction decomposition
sympy.apart(1/(x**2-4*x+3))
sympy.apart(1/(x**6-1))
Expand expression
sympy.var('x, y')
sympy.expand((x+y)**6)
Simplify expression
sympy.simplify((2*x-2)/((x-1)**2*(x-2)))
sympy.simplify(1/(x-1)**2+1/((x-1)*(x-2)))
Simplify and expand expression
sympy.cancel((2*x-2)/((x-1)**2*(x-2)))
sympy.cancel(1/(x-1)**2+1/((x-1)*(x-2)))
Examine True or False
0==sympy.sqrt(2)-2**(sympy.Rational(1,2))
0==sympy.sqrt(2)-2**(sympy.Rational(1,3))
sympy.factorial(50)==sympy.factorial2(50)*sympy.factorial2(49)

sometimes it needs some manipulation on expressions

x**2-2*x+1==(x-1)**2
x**2-2*x+1==sympy.expand((x-1)**2)
Substitution
sympy.var('x, a, b, c')
(a*x**2+b*x+c).subs(x, 3)
sympy.expand((a*x**2+b*x+c).subs(x, (-b + sympy.sqrt(-4*a*c + b**2))/(2*a)))
Summation
k=sympy.symbols('k',integer=True)
sympy.summation(k, (k, 1, 10) )
k, N=sympy.symbols('k N',integer=True)
sympy.factor(sympy.summation(k, (k, 1, N) ))
sympy.factor(sympy.summation(k**3, (k, 1, N) ))
Product
k=sympy.symbols('k',integer=True)
sympy.product(k, (k, 1, 10) )
sympy.factorial(10)==sympy.product(k, (k, 1, 10) )
Extract coefficients of specific term
sympy.var('x, y')
sympy.expand((x+y)**6).coeff(x, 3)

Equation

Solve simultaneous equations
sympy.var('x, y')
sympy.solve ([3*x+4*y-5, 2*x+3*y-3], [x, y])
Find root of algebraic equation
sympy.var('x, a, b, c')
sympy.solve (a*x**2+b*x+c, x)

Function

Assign expression to letter, and evaluate it at a value
sympy.var('x, y')
u=x+y
v=(x+y)**3
u.subs(x,5)
(v.subs(x,5)).subs(y,3)
Use assigned letter: 4 operations
u+v
sympy.expand(u+v)
sympy.factor(u+v)
sympy.expand(u*v)
sympy.simplify(u/v)
Use assigned letter: differentiation
sympy.diff(v, x)
sympy.diff(v, y, 2)
0==sympy.diff(v, x, 1)-sympy.diff(v, x)
Use assigned letter: Integration
sympy.integrate(u, x)
sympy.integrate(v, (x, 0, 6))
Define a function
sympy.var('x')
def f(x):
    return sympy.log(1+x)/x
f(1)
Differential
sympy.diff(f(x), x, 1)
Integral
sympy.integrate(f(x), x)
Taylor expansion
sympy.series(f(x), x, 0, 7) 
Extract RHS, LHS, Denominator, Numerator
sympy.var('x')
F=x+1
G=x**2+x-2
E=sympy.Eq(x+1, F/G)
sympy.solve(E, x)
E.rhs
E.lhs
sympy.denom(E.rhs)
sympy.numer(E.rhs)

Trigonometric (Hyperbolic) function

Expand
sympy.var('a, b')
sympy.expand(sympy.sin(a+b), trig=True)
sympy.expand(sympy.cos(a+b), trig=True)
sympy.expand(sympy.tan(a+b), trig=True)
Simplify
sympy.trigsimp((sympy.sin(a))**2+(sympy.cos(a))**2)
sympy.trigsimp((sympy.sinh(a))**2+(sympy.cosh(a))**2)

List

Make List, Refer its element
S=list([a, b, c])
S[0]
S[0]+S[1]*S[2]
S[3]
sum(S)
FOR
def n(k):
    return k
N_list = []
for i in range(10):
    N = n(i)
    N_list.append(N)
print(N_list)

Display in TeX form, Output in TeX form

sympy.var('a, b, c, x')
E2=sympy.Eq(a*x**2+b*x+c, 0)
sol=sympy.solve(E2, x)
display(sol[1])
sympy.init_printing()
display(sol[1])
print(sympy.latex(sol[0]))


As we've seen so far, many objects in SymPy are associated with " sympy. "
we can shorten it by doing from the beginning

from sympy import sp.

then, for example

sp.I**2


Keywords: Python, SymPy, JupyterNotbook