APM_2F005 - Algorithms For Discrete Mathematics Bachelor 2
Lecturer: Lucas Gerin

Symbolic computing 1: Proofs with SymPy

Table of contents

A short tutorial in SymPy

Symbolic computation is a theory and a set of tools that enable manipulation of exact mathematical expressions without resorting to approximate calculations. In Python, a standard library for symbolic computation is SymPy. The objective of today's tutorial is to learn how to use SymPy to solve various problems typically addressed with pen and paper.

To begin with, run and compare the following scripts:

One can expand or simplify expressions:

When simplifying, SymPy uses formal rules and doesn't care about division by zero. As a result, you may occasionally obtain nonsensical results.

With Sympy one can also obtain Taylor expansions of functions with series:

SymPy can also compute with "big O's". (By default $O(x)$ is considered for $x\to 0$.)

A nice feature of `Sympy` is that you can export formulas in $\texttt{LateX}$. You also can get nice displays with `display`.

Warning: Fractions such as $1/4$ must be introduced with Rational(1,4) to keep Sympy from evaluating the expression. For example:

Let SymPy do the proofs

Exercise 1. A warm-up


Set $\phi=\frac{1+\sqrt{5}}{2}$. Use `SymPy` to simplify $F=\frac{\phi^4-\phi}{1+\phi^7}$. (Print a nice display.)

Exercise 2. A simple (?) recurrence

We will see how to use SymPy to prove a mathematical statement. Our aim is to make as rigorous proofs as possible, as long as we trust SymPy.


Let $a,b$ be two real numbers $\neq 0$, we define the sequence $(u_n)_{n\geq 0}$ as follows: $u_0=a,u_1=b$ and for $n\geq 2$ $$ u_{n}=\frac{1+u_{n-1}}{u_{n-2}}. $$
  1. Write a short program which returns the $15$ first values of $u_n$ in terms of symbolic variables $a,b$. The output should be something like: ``` u_0 = a u_1 = b u_2 = (b + 1)/a ... ```
  2. Use `SymPy` to make and prove a conjecture for the asymptotic behaviour of the sequence $(u_n)$, for every reals $a,b$.
  1. See the cell above.
  2. We conjecture that for every $n\geq 0$, $u_n$ is periodic with period $5$, i.e. \begin{align} u_{5n}=u_0,\\ u_{5n+1}=u_1,\\ u_{5n+2}=u_2,\\ u_{5n+3}=u_3,\\ u_{5n+4}=u_4. \end{align} For $n=0$ this is obvious. To prove the induction, it suffices to prove that \begin{align} u_{5n+5}=u_{5n},\\ u_{5n+6}=u_{5n+1},\\ u_{5n+7}=u_{5n+2},\\ u_{5n+8}=u_{5n+3},\\ u_{5n+9}=u_{5n+4}. \end{align} And this is exactly what the SymPy computation shows as we have done the computation for **every** reals $a,b$, and since $u_k$ only depends on $u_{k-1},u_{k-2}$. So if we trust Sympy the proof is done.

Exercise 3. What if Archimedes had known Sympy?

For $n\geq 1$, let $\mathcal{P}_n$ be a regular $3\times 2^n$-gon with radius $1$. Here is $\mathcal{P}_1$:

Roots
Archimedes (IIIrd century BC) used the fact that $\mathcal{P}_n$ gets closer and closer to the unit circle to obtain good approximations of $\pi$.
We will use SymPy to deduce nice formulas for approximations of $\pi$.

Let $L_n$ be the length of any side of $\mathcal{P}_n$. Compute $L_1$ and use the following picture to write $L_{n+1}$ as a function of $L_n$: * $O$ is the center of the circle, $OC=1$. * $(OB)$ is the bisector of $\widehat{DOC}$. * $\widehat{OAC}$ is a right angle.
Roots
As $OB$ is the bisector we have that $CB=BD$, which both are sides of $\mathcal{P}_{n+1}$.
Besides, $OAC$ is rectangle at $A$. By Pythagora's theorem $$ 1^2=OA^2+AC^2=OA^2+(L_n/2)^2. $$ $BAC$ is also rectangle at $A$, therefore \begin{align*} L_{n+1}^2=BC^2&=AB^2+AC^2\\ &=(1-OA)^2+(L_n/2)^2\\ &=\bigg(1-\sqrt{1-(L_n/2)^2}\bigg)^2+(L_n/2)^2\\ &=1+1-(L_n/2)^2-2\sqrt{1-(L_n/2)^2}+(L_n/2)^2\\ &=2-2\sqrt{1-(L_n/2)^2}. \end{align*} Finally we obtain \begin{align*} L_{n+1}=\sqrt{2-2\sqrt{1-(L_n/2)^2}}. \end{align*}
  1. Write a script which computes exact expressions for the first values $L_1,L_2,\dots, L_n$. (First try for small $n$'s.)
  2. Find a sequence $a_n$ such that $a_nL_n$ converges to $\pi$ (here we don't ask for a proof, this is quite complicated to prove rigorously). Deduce some good algebraic approximations of $\pi$. Export your results in $\texttt{Latex}$ in order to get nice formulas.
    (In order to check your formulas, you may compute numerical evaluations. With `SymPy`, a numerical evaluation is obtained with `N(expression)`.)
When $n$ goes large, $\mathcal{P}_n$ gets closer and closer to the unit circle. As the perimeter of $\mathcal{P}_n$ is $3\times 2^n L_n$, we expect that $$ 3\times 2^n L_n \to 2\pi, $$ therefore we choose $a_n=3\times 2^{n-1}$. For $n=8$ we obtain: $$ \pi \approx 384 \sqrt{- \sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{3} + 2} + 2} + 2} + 2} + 2} + 2} + 2} = 3.141583892... $$
Can you deduce a proof that $\pi >3$ ?
The length of a chord is always smaller than he corresponding arc. Then for every $n$ $$ 2\pi\geq 3\times 2^n L_n. $$ For $n=2$ we obtain $$ \pi \geq 3.10582854123... $$

Solving equations with SymPy

One can solve equations with Sympy. The following script shows how to solve $x^2=x+1$:

Exercise 4. Solving equations with Sympy: the easy case

Let $n\geq 1$ be an integer, we are interested in solving the equation \begin{equation} x^3 +nx =1.\tag{$\star$} \end{equation}

In the script below we plot $x \mapsto x^3$, and $x \mapsto 1-nx$ for $0\leq x\leq 1$ and for several (large) values of $n$. This suggests that Equation $(\star)$ has a unique real solution in the interval $[0,1]$, that we will denote by $S_n$.

(Theory)
  1. Prove that indeed for every $n\geq 1$, Equation $(\star)$ has a unique real solution in the interval $[0,1]$.
  2. According to the plot, what can we conjecture for the limit $S_n$?
  1. The map $x\mapsto f(x)=x^3+nx -1$ is continuous and increasing on $[0,1]$, since $$ f'(x)=3x^2 +n > n>0. $$ Besides, $$ f(0)=0^3-n\times 0-1=-1,\qquad f(1)=1^3+n\times 1-1=n>0. $$ This implies that there is a unique $S_n \in(0,1)$ such that $f(S_n)=0$, i.e. $$ (S_n)^3 +nS_n =1. $$
  2. On the figure above we observe that when $n\to +\infty$, the solution of Equation $(\star)$ seems to get closer and closer to zero.
    We therefore conjecture $$ \lim_{n\to+\infty} S_n=0. $$

  1. Use the function `solve()` to compute the exact expression of $S_n$.
  2. Use `SymPy` and the function `series`to get the asymptotic expansion of $S_n$ (up to $\mathcal{O}(1/n^5)$). Check your previous conjecture.
  1. According to the above script, $$ \frac{- 2 \sqrt[3]{18} n + \sqrt[3]{12} \left(\sqrt{3} \sqrt{4 n^{3} + 27} + 9\right)^{\frac{2}{3}}}{6 \sqrt[3]{\sqrt{3} \sqrt{4 n^{3} + 27} + 9}}. $$
  2. SymPy gives $$ S_n=\frac{1}{n}-\frac{1}{n^4}+\mathcal{O}(1/n^5). $$ Indeed, this goes to zero as expected.

Exercise 5. Solving equations: when SymPy needs help

We consider the following equation: \begin{equation} X^5-3\varepsilon X^4-1=0, \tag{$\star$} \end{equation} where $\varepsilon$ is a positive parameter. As in previous exercise a quick analysis shows that if $\varepsilon>0$ is small enough then ($\star$) has a unique real solution, that we denote by $S_\varepsilon$.

The degree of this equation is too high to be solved by SymPy:

Indeed, SymPy needs help to handle this equation.

In the above script we plotted the function $f(x)=x^5-3\varepsilon x^4-1$ for some small $\varepsilon$. This suggests that $\lim_{\varepsilon \to 0}S_\varepsilon=1$.


We admit that $S_\varepsilon$ can be written as \begin{equation} S_\varepsilon = 1+ r\varepsilon + s\varepsilon^2+ \mathcal{O}(\varepsilon^3), \end{equation} for some real $r,s$. Use `SymPy` to find $r,s$.
(You can use any `SymPy` function already seen in this notebook.)