In [ ]:
%%HTML
<style>
.container { width:100% } 
</style>

SymPy - An Appetizer

SymPyis a module that support symbolic mathematics i.e. things like

  • symbolic differentiation,
  • symbolic integration,
  • solving equations in closed form,
  • compute limits,
  • solve recurrence equations.

The listing given above is far from complete.


In [ ]:
import sympy as sym

To get started, we need to define some symbolic variables.


In [ ]:
x = sym.symbols('x')
y = sym.symbols('y')
n = sym.symbols('n')
i = sym.symbols('i')
q = sym.symbols('q')

We can also define a bunch of variables all at the same time:


In [ ]:
a, b, c, d = sym.symbols('a b c d')

Let us verify the first Binomial formula $$ (x + y)^2 = x^2 + 2 \cdot x \cdot y + y^2 $$ using the variables x and y.


In [ ]:
sym.expand((x + y) ** 2)

Let us generalize this:


In [ ]:
for k in range(2, 6):
    print(f'(x + y)**{k} = {sym.expand((x + y) ** k)}')

The opposite of the command expand is the command factor. Suppose we want to know the solution of the equation $$ x^2 - 8 \cdot x + 15 = 0 $$ and we suspect that the solutions are, in fact, integers. The following command does the trick:


In [ ]:
sym.factor(x ** 2 - 8 * x + 15)

We can also compute the partial fraction decomposition of an expression. The function apart comes in handy for this purpose:


In [ ]:
sym.apart(2*x/(x**2 - 8*x + 15))

This shows that $$ \frac{2 \cdot x}{x^2 - 8 \cdot x + 15} = \frac{5}{x - 5} - \frac{3}{x - 3}. $$

In order to compute the derivative of $$x \cdot \sin(x)$$ with respect to $x$ we can use the following command:


In [ ]:
sym.diff(x * sym.sin(x), x)

Symbolic integration is possible, too: To compute the indefinite integral $$\displaystyle\int x \cdot \sin(x)\; dx$$ we use the following command:


In [ ]:
sym.integrate(x * sym.sin(x), x)

SymPy can compute definite integrals. To compute the integral $$\displaystyle\int_{-\infty}^{+\infty} e^{-x^2} \; dx $$ we use the following command:


In [ ]:
sym.integrate(sym.exp(-x**2), (x, -sym.oo, sym.oo))

This shows that $$\displaystyle \int_{-\infty}^{+\infty} e^{-x^2} \; \mathrm{d}x = \sqrt{\pi}. $$

Let us compute the limit $$\lim\limits_{n \rightarrow \infty} \sqrt{n+1} - \sqrt{n}.$$


In [ ]:
sym.limit(sym.sqrt(n+1) - sym.sqrt(n), n, sym.oo)

SymPy can compute the Taylor series of a given function up to a given value of $n$. For example, to compute the Taylor series of the exponential function use the command


In [ ]:
sym.series(sym.exp(x), x, n=10)

In order to compute an analytical expression for the sum $$\displaystyle \sum\limits_{i=1}^n i^3 $$ we can use the following command:


In [ ]:
sym.summation(i**3, (i, 1, n))

Therefore, we have shown that $$\displaystyle \sum\limits_{i=1}^n i^3 = \frac{n^{4}}{4} + \frac{n^{3}}{2} + \frac{n^{2}}{4} $$ holds. Let's try to sum the first $n+1$ terms of the geometric series:


In [ ]:
sym.summation(q**i, (i, 0, n))

Note that sympy realizes that we need a case distinction for this formula, as the case $q=1$ is special.

The formula that $$\sum\limits_{i=0}^n q^i = \begin{cases} n + 1 & \text{if}\: q = 1\text{;} \\[0.2cm] \frac{\displaystyle\;1 - q^{n+1}\;}{\displaystyle 1 - q} & \text{otherwise.} \end{cases} $$

Lets try to compute $$ \sum\limits_{i=1}^\infty \frac{1}{i^2}: $$


In [ ]:
sym.summation(1 / n**2, (n, 1, sym.oo))

Let us compute the solution of the following quadratic equation: $$ x^2 - x - 1 = 0 $$ Note that the function solve only takes the term $x^2 - x - 1$ as its argument. It implicitly assumes that we search for that value of the variable $x$, that makes this expressions $0$.


In [ ]:
sym.solve(x**2 - x - 1, x)

This shows that the two solutions to the quadratic equation $x^2 - x - 1 = 0$ are $$ x_1 = \frac{1}{2} \cdot \bigl(1 - \sqrt{5}\bigr) \quad \mbox{and} \quad x_2 = \frac{1}{2} \cdot \bigl(1 + \sqrt{5}\bigr) $$

In order to solve a system of equations, we need to use sets of equations: $$ \begin{array}[t]{lcr} x + y & = & a \\ x - y & = & b \end{array} $$


In [ ]:
sym.solve({x + y - a, x - y - b}, {x, y})

Suppose we want to solve the recurrence equation $$ a(n+2) = a(n+1) + a(n) $$
with the initial values $a(0) = 0$ and $a(1) = 1$. First we have to declare that a is a function:


In [ ]:
a = sym.Function('a')

In [ ]:
sym.rsolve(a(n+2) - a(n+1) - a(n), a(n), {a(0): 0, a(1): 1})

The solution that is computed is $$\displaystyle a(n) = \frac{\sqrt{5}}{5} \cdot\left(\frac{1}{2} + \frac{\sqrt{5}}{2}\right)^{n} - \frac{\sqrt{5}}{5} \cdot\left(- \frac{\sqrt{5}}{2} + \frac{1}{2}\right)^{n}. $$

In order to solve the differential equation $$\displaystyle x \cdot \frac{d\hspace{0.1pt}f}{dx} - f(x) = x^2$$ we first need to declare $f$ as a function.


In [ ]:
f = sym.Function('f')

In [ ]:
sym.dsolve(sym.Eq(x * f(x).diff(x) - f(x), x**2), f(x))

This shows that for any $C_1 \in \mathbb{R}$ the function $$f(x) = x \cdot \bigl(C_{1} + x\bigr)$$ is a solution of the given differential equation.

Sometimes the output of SymPy is a little hard to read for the untrained eye:


In [ ]:
a = sym.symbols('a')
sym.solve(a*x**3 + b * x**2 + c * x + d, x)

The latex command can help out in cases like these:


In [ ]:
sym.latex(sym.solve(a*x**3 + b * x**2 + c * x + d, x))

If we typeset this in $\LaTeX$, we get $$ \left [ - \frac{- \frac{3 c}{a} + \frac{b^{2}}{a^{2}}}{3 \sqrt[3]{\frac{1}{2} \sqrt{- 4 \left(- \frac{3 c}{a} + \frac{b^{2}}{a^{2}}\right)^{3} + \left(\frac{27 d}{a} - \frac{9 b}{a^{2}} c + \frac{2 b^{3}}{a^{3}}\right)^{2}} + \frac{27 d}{2 a} - \frac{9 b c}{2 a^{2}} + \frac{b^{3}}{a^{3}}}} - \frac{1}{3} \sqrt[3]{\frac{1}{2} \sqrt{- 4 \left(- \frac{3 c}{a} + \frac{b^{2}}{a^{2}}\right)^{3} + \left(\frac{27 d}{a} - \frac{9 b}{a^{2}} c + \frac{2 b^{3}}{a^{3}}\right)^{2}} + \frac{27 d}{2 a} - \frac{9 b c}{2 a^{2}} + \frac{b^{3}}{a^{3}}} - \frac{b}{3 a}, \quad - \frac{- \frac{3 c}{a} + \frac{b^{2}}{a^{2}}}{3 \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{1}{2} \sqrt{- 4 \left(- \frac{3 c}{a} + \frac{b^{2}}{a^{2}}\right)^{3} + \left(\frac{27 d}{a} - \frac{9 b}{a^{2}} c + \frac{2 b^{3}}{a^{3}}\right)^{2}} + \frac{27 d}{2 a} - \frac{9 b c}{2 a^{2}} + \frac{b^{3}}{a^{3}}}} - \frac{1}{3} \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{1}{2} \sqrt{- 4 \left(- \frac{3 c}{a} + \frac{b^{2}}{a^{2}}\right)^{3} + \left(\frac{27 d}{a} - \frac{9 b}{a^{2}} c + \frac{2 b^{3}}{a^{3}}\right)^{2}} + \frac{27 d}{2 a} - \frac{9 b c}{2 a^{2}} + \frac{b^{3}}{a^{3}}} - \frac{b}{3 a}, \quad - \frac{- \frac{3 c}{a} + \frac{b^{2}}{a^{2}}}{3 \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{1}{2} \sqrt{- 4 \left(- \frac{3 c}{a} + \frac{b^{2}}{a^{2}}\right)^{3} + \left(\frac{27 d}{a} - \frac{9 b}{a^{2}} c + \frac{2 b^{3}}{a^{3}}\right)^{2}} + \frac{27 d}{2 a} - \frac{9 b c}{2 a^{2}} + \frac{b^{3}}{a^{3}}}} - \frac{1}{3} \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{1}{2} \sqrt{- 4 \left(- \frac{3 c}{a} + \frac{b^{2}}{a^{2}}\right)^{3} + \left(\frac{27 d}{a} - \frac{9 b}{a^{2}} c + \frac{2 b^{3}}{a^{3}}\right)^{2}} + \frac{27 d}{2 a} - \frac{9 b c}{2 a^{2}} + \frac{b^{3}}{a^{3}}} - \frac{b}{3 a}\right ] $$

This looks better.