Tutorial Brief

SymPy is symbolic mathematics library written completely in Python and doesn't require any dependencies.

Finding Help:

NumPy

Base N-dimensional array package

SciPy

Fundamental library for scientific computing

Matplotlib

Comprehensive 2D Plotting

IPython

Enhanced Interactive Console

SymPy

Symbolic mathematics

Pandas

Data structures & analysis

http://sympy.org/

Import SymPy

In [1]:
from sympy import *
In [2]:
3 + math.sqrt(3)
Out[2]:
4.732050807568877
In [3]:
expr = 3 * sqrt(3)
expr
Out[3]:
3*sqrt(3)
In [4]:
init_printing(use_latex='mathjax')
In [5]:
expr
Out[5]:
$$3 \sqrt{3}$$
In [6]:
expr = sqrt(8)
expr
Out[6]:
$$2 \sqrt{2}$$

symbols() & Symbol()

In [7]:
x, y = symbols("x y")
In [8]:
expr = x**2 + y**2
expr
Out[8]:
$$x^{2} + y^{2}$$
In [9]:
expr = (x+y)**3
expr
Out[9]:
$$\left(x + y\right)^{3}$$

Assumptions for symbols

In [10]:
a = Symbol("a")
In [11]:
a.is_imaginary
In [12]:
b = Symbol("b", integer=True)
In [13]:
b.is_imaginary
Out[13]:
False
In [14]:
c = Symbol("c", positive=True)
In [15]:
c.is_positive
Out[15]:
True
In [16]:
c.is_imaginary
Out[16]:
False

Imaginary Numbers

In [17]:
I
Out[17]:
$$i$$
In [18]:
I ** 2
Out[18]:
$$-1$$

Rational()

In [19]:
Rational(1,3)
Out[19]:
$$\frac{1}{3}$$
In [20]:
Rational(1,3) + Rational(1,2)
Out[20]:
$$\frac{5}{6}$$

Numerical evaluation

In [21]:
expr = Rational(1,3) + Rational(1,2)
N(expr)
Out[21]:
$$0.833333333333333$$
In [22]:
N(pi, 100)
Out[22]:
$$3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068$$
In [23]:
pi.evalf(100)
Out[23]:
$$3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068$$

subs()

In [24]:
expr = x**2 + 2*x + 1
expr
Out[24]:
$$x^{2} + 2 x + 1$$
In [25]:
expr.subs(x, 1)
Out[25]:
$$4$$
In [26]:
expr = pi * x**2
expr
Out[26]:
$$\pi x^{2}$$
In [27]:
expr.subs(x, 3)
Out[27]:
$$9 \pi$$
In [28]:
N(_)
Out[28]:
$$28.2743338823081$$

factor() and expand()

In [29]:
expr = (x + y) ** 2
expr
Out[29]:
$$\left(x + y\right)^{2}$$
In [30]:
expand(expr)
Out[30]:
$$x^{2} + 2 x y + y^{2}$$
In [31]:
factor(_)
Out[31]:
$$\left(x + y\right)^{2}$$

simplify()

In [32]:
expr = (2*x + Rational(1,3)*x + 4) / x
expr
Out[32]:
$$\frac{1}{x} \left(\frac{7 x}{3} + 4\right)$$
In [33]:
simplify(expr)
Out[33]:
$$\frac{7}{3} + \frac{4}{x}$$
In [34]:
expr = "(2*x + 1/3*x + 4)/x"
simplify(expr)
Out[34]:
$$\frac{7}{3} + \frac{4}{x}$$
In [35]:
expr = sin(x)/cos(x)
expr
Out[35]:
$$\frac{\sin{\left (x \right )}}{\cos{\left (x \right )}}$$
In [36]:
simplify(expr)
Out[36]:
$$\tan{\left (x \right )}$$

apart() and together()

In [37]:
expr = 1/(x**2 + 2*x)
expr
Out[37]:
$$\frac{1}{x^{2} + 2 x}$$
In [38]:
apart(expr)
Out[38]:
$$- \frac{1}{2 x + 4} + \frac{1}{2 x}$$
In [39]:
together(_)
Out[39]:
$$\frac{1}{x \left(x + 2\right)}$$

Calculus

In [40]:
diff(sin(x), x)
Out[40]:
$$\cos{\left (x \right )}$$
In [41]:
diff(log(x**2 + 1) + 2*x, x)
Out[41]:
$$\frac{2 x}{x^{2} + 1} + 2$$
In [42]:
integrate(cos(x), x)
Out[42]:
$$\sin{\left (x \right )}$$
In [43]:
Integral(sin(x), (x,0,pi))
Out[43]:
$$\int_{0}^{\pi} \sin{\left (x \right )}\, dx$$
In [44]:
N(_)
Out[44]:
$$2.0$$

Sum()

In [45]:
expr = Sum(1/(x**2 + 2*x), (x, 1, 10))
expr
Out[45]:
$$\sum_{x=1}^{10} \frac{1}{x^{2} + 2 x}$$
In [46]:
expr.doit()
Out[46]:
$$\frac{175}{264}$$

Product()

In [47]:
expr = Product(1/(x**2 + 2*x), (x, 1, 10))
expr
Out[47]:
$$\prod_{x=1}^{10} \frac{1}{x^{2} + 2 x}$$
In [48]:
expr.doit()
Out[48]:
$$\frac{1}{869100503040000}$$

Solve()

In [49]:
expr = 2*x + 1
solve(expr)
Out[49]:
$$\begin{bmatrix}- \frac{1}{2}\end{bmatrix}$$
In [50]:
expr = x**2 - 1
solve(expr)
Out[50]:
$$\begin{bmatrix}-1, & 1\end{bmatrix}$$
In [51]:
expr_1 = 2*x + y + 3
expr_2 = 2*y - x
solve([expr_1, expr_2],(x,y))
Out[51]:
$$\begin{Bmatrix}x : - \frac{6}{5}, & y : - \frac{3}{5}\end{Bmatrix}$$

Units

In [52]:
from sympy.physics import units as u
In [53]:
5. * u.milligram
Out[53]:
$$5.0 \cdot 10^{-6} kg$$
In [54]:
1./2 * u.inch
Out[54]:
$$0.0127 m$$
In [55]:
1. * u.nano
Out[55]:
$$1.0 \cdot 10^{-9}$$
In [56]:
u.watt
Out[56]:
$$\frac{kg m^{2}}{s^{3}}$$
In [57]:
u.ohm
Out[57]:
$$\frac{kg m^{2}}{A^{2} s^{3}}$$

Converting from Kilometers/hours to Miles/hours

In [58]:
kmph = u.km / u.hour
mph = u.mile / u.hour

N(mph / kmph)
Out[58]:
$$1.609344$$
In [69]:
80 * N(mph / kmph)
Out[69]:
$$128.74752$$

Working with NumPy / Pandas and Matplotlib

In [59]:
def sympy_expr(x_val):
    expr = x**2 + sqrt(3)*x - Rational(1,3)
    return expr.subs(x, x_val)
In [60]:
sympy_expr(3)
Out[60]:
$$3 \sqrt{3} + \frac{26}{3}$$
In [61]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [62]:
list1 = np.arange(1,1000)
list2 = pd.Series(list1)
In [63]:
%timeit [sympy_expr(item) for item in list1]
%timeit [sympy_expr(item) for item in list2]
1 loops, best of 3: 103 ms per loop
10 loops, best of 3: 105 ms per loop
In [64]:
%timeit np.vectorize(sympy_expr)(list1)
%timeit list2.apply(sympy_expr)
10 loops, best of 3: 776 ms per loop
1 loops, best of 3: 1.09 s per loop
In [65]:
expr = x**2 + sqrt(3)*x - Rational(1,3)

lf = lambdify(x, expr)
In [66]:
%timeit lf(list1)
%timeit lf(list2)
1000 loops, best of 3: 242 ┬Ás per loop
100 loops, best of 3: 4.8 ms per loop
In [67]:
fig = plt.figure()
axes = fig.add_subplot(111)

x_vals = np.linspace(-5.,5.)
y_vals = lf(x_vals)

axes.grid()
axes.plot(x_vals, y_vals)

plt.show();