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

Symbolic computation 2: Linear recurrences

Table of contents

Warm-up

The aim of this Lab session is to use SymPy to solve automatically some simple recurrences. We first solve a particular case "by hand" and then use the SymPy function rsolve.

Exercise 1. Solving a recurrence (almost) by hand

We want to find an explicit formula for the sequence $(u_n)$ defined by \begin{equation} \begin{cases} u_0&=1,\\ u_{n}&=2u_{n-1} +3n^2. \qquad (\forall n\geq 1). \end{cases} \tag{$\star$} \end{equation}
  1. . Let $\alpha,a,b,c$ be four real parameters. Let $(w_n)_{n\geq 0}$ be the sequence defined by $$ w_n=\alpha 2^n +an^2+bn+c. $$ Use `SymPy` to find $\alpha,a,b,c$ such that for every $0\leq n\leq 3$, $$ w_n=u_n. $$ To solve a system of equations with `SymPy` with unknowns $x,y$, write something like ``` solve([x-y-2,3*y+x],[x,y]) ```
  2. . Prove with `SymPy` that $(w_n)$ defined as above is the unique solution of ($\star$). (It might be helpful to define a function `w(n,alpha,a,b,c)` which computes $w_n$.)
  1. We have four unknowns $\alpha,a,b,c$ and four equations \begin{align*} u_0&=1,\\ u_1&=2u_0+3\times 1^2,\\ u_2&=2u_1+3\times 2^2,\\ u_3&=2u_2+3\times 3^2,\\ \end{align*} With the above script we obtain that if the formula is true then necessarily $$ u_n=19\times 2^n -3n^2-12n-18. $$
  1. We first observe that eq.$(\star)$ has a unique solution since the sequence $(u_n)$ is a recurrence of order one: it is uniquely determined by $u_0$. Besides, set
    $$ w_n=19\times 2^n -3n^2-12n-18. $$
    Parameters are chosen so that $w_0=u_0=1$. The above script shows that for every $n$ we have that $w_n-2w_{n-1}-3n^2=0$. Therefore $(w_n)$ satisfies the recurrence $(\star)$ and starts with the same initial value $w_0=1$. Therefore $w_n=u_n$ for every $n$.

Solving recurrences with SymPy: rsolve

The function rsolve

We will use Sympy to obtain explicit formulas for some sequences defined by linear recurrences. More precisely, we will see how to obtain an explicit formula for $u_n$ in two cases:

  1. Linear recurrence of order one: this is a sequence $(u_n)_{n\geq 0}$ is defined by $$ \begin{cases} u_0&=a, \\ u_{n}&=\alpha u_{n-1}+f(n),\qquad (n\geq 1), \end{cases} $$ where $a,\alpha$ are some given constants and $f$ is an arbitrary function.
  2. Linear recurrence of order two: this is a sequence $(u_n)_{n\geq 0}$ is defined by $$ \begin{cases} u_0&=a, \\ u_1&=b,\\ u_{n}&=\alpha u_{n-1}+\beta u_{n-2}+f(n),\qquad (n\geq 2), \end{cases} $$ where $a,,b,\alpha,\beta$ are some given constants and $f$ is an arbitrary function.

Some known examples fit in this settings:

  1. Geometric sequences: $u_0=a,u_n=ru_{n-1}$.
  2. Arithmetic sequences: $u_0=a,u_n=u_{n-1}+r$.
  3. The Fibonacci sequence: $F_1=1,F_2=1,F_{n}=F_{n-1}+F_{n-2}$.

The following script shows how to solve the two recurrences \begin{align*} u_0=5,\qquad u_{n}&=3u_{n-1},\\ v_0=1,\qquad v_{n}&=2v_{n-1}+n,\\ \end{align*} using function rsolve.

Exercise 2. A first example with rsolve

  1. Use `SymPy` to solve the recurrence of the Fibonacci sequence and find an explicit formula for $F_n$.
    (To set up two initial conditions you must write `{u(1):1,u(2):1}`.)
  2. (Theory) Use the formula obtained at Question 1 to prove that $$ \lim_{n\to +\infty} \frac{F_n}{F_{n-1}}= \varphi, $$ where $\varphi=\frac{1+\sqrt{5}}{2}$ is the golden ratio.
  1. We put the above answer in $\texttt{LateX}$: $$ F_n=\frac{2^{- n}}{5} \sqrt{5} \left(\left(1 + \sqrt{5}\right)^{n} - \left(- \sqrt{5} + 1\right)^{n}\right). $$
  2. We obtain (by setting $\psi=(1-\sqrt{5})/2$): \begin{align*} \frac{F_{n}}{F_{n-1}}&= \frac{\tfrac{1}{\sqrt{5}}\varphi^{n} - \tfrac{1}{\sqrt{5}}\psi^n }{\tfrac{1}{\sqrt{5}}\varphi^{n-1} - \tfrac{1}{\sqrt{5}}\psi^{n-1} },\\ &= \frac{\tfrac{1}{\sqrt{5}}\varphi - \psi \tfrac{1}{\sqrt{5}}(\psi/\varphi)^{n-1} }{\tfrac{1}{\sqrt{5}} - \tfrac{1}{\sqrt{5}}(\psi/\varphi)^{n-1} },\qquad \text{(we divide everything by $\varphi^{n-1}$.)}\\ &\to \varphi, \end{align*} since $|\psi/\varphi|<1$.
The output of `rsolve` is an expression which depends on the symbolic variable `n`. If we want to evaluate this expression (for instance for $n=10$) we must write:

Two-dimensional recurrence: matrices with SymPy

Exercise 3. A double linear recurrence

Theory
  1. Prove by induction that there exist integers $a_n,b_n$ such that for every $n\geq 1$ $$ (1+\sqrt{2})^n=a_n+b_n\sqrt{2}. $$
  2. Find a $2\times 2$ matrix $A$ such that $$ \binom{a_{n+1}}{b_{n+1}}=A\times \binom{a_{n}}{b_{n}}. $$
  3. Use `SymPy` to find an explicit formula for $a_n$.
    (In `SymPy` matrices are defined by `A=Matrix([[a,b],[c,d]])`. To raise $A$ to some power just write `A**n`.)
  4. Find (with pen/paper) real numbers $c,R$ such that $$ a_n \stackrel{n\to +\infty}{\sim } cR^n. $$
  1. For $n=1$ this is true with $a_1=b_1=1$. Assume this holds for some $n\geq 1$. \begin{align*} (1+\sqrt{2})^{n+1}&=(1+\sqrt{2})\times(1+\sqrt{2})^{n}\\ &=(1+\sqrt{2})\times(a_n+b_n\sqrt{2})\\ &=a_n+b_n\sqrt{2}+a_n\sqrt{2}+2b_n\\ &=(a_n+2b_n)+(a_n+b_n)\sqrt{2}. \end{align*} Finally, one can write $$ (1+\sqrt{2})^{n+1}=a_{n+1}+b_{n+1}\sqrt{2}, $$ where $$ a_{n+1}=a_n+2b_n,\qquad b_{n+1}=a_n+b_n, $$ which are easily proved to be integers by induction.
  2. By the above computation we have $$ \begin{cases} a_{n+1}&=a_n+2b_n,\\ b_{n+1}&=a_n+b_n, \end{cases} $$ which can be written as: $$ \binom{a_{n+1}}{b_{n+1}}= \begin{pmatrix} 1 & 2\\ 1 & 1 \end{pmatrix} \times \binom{a_{n}}{b_{n}}. $$ By induction this implies that $$ \binom{a_{n}}{b_{n}}= \begin{pmatrix} 1 & 2\\ 1 & 1 \end{pmatrix}^{n-1} \times \binom{a_1}{b_1} =\begin{pmatrix} 1 & 2\\ 1 & 1 \end{pmatrix}^{n-1} \times \binom{1}{1}. $$
  1. We export the result in LateX: \begin{align*} a_n&=\frac{\left(1 - \sqrt{2}\right)^{n}}{2} + \frac{\left(1 + \sqrt{2}\right)^{n}}{2}\\ b_n&= \frac{\sqrt{2} \left(- \left(1 - \sqrt{2}\right)^{n} + \left(1 + \sqrt{2}\right)^{n}\right)}{4} \end{align*} (You can indeed check that $a_n+\sqrt{2}b_n=(1+\sqrt{2})^n$.)
  2. As $\left|-\sqrt{2}+1\right|<1$, we have that $(-\sqrt{2}+1)^n$ tends to zero. It follows that \begin{align*} a_n&=\frac{1}{2} \left(1 + \sqrt{2}\right)^{n} +\mathrm{o}(1)\\ &\sim \frac{1}{2} \left(1 + \sqrt{2}\right)^{n}. \end{align*}

Exercise 4. Rational fractions. (Taken from BX2023's test)

We set $$ u_1=\frac{1}{1+1}, \quad u_2=\frac{1}{1+\frac{1}{2+1}},\quad u_3=\frac{1}{1+\frac{1}{2+\frac{1}{3+1}}}, \quad u_4=\frac{1}{1+\frac{1}{2+\frac{1}{3+\frac{1}{4+1}}}}, \dots $$ Using SymPy, write $u_{20}$ as a rational fraction $a/b$. (To check your result: the approximate value is $0.69777...$.)
We set \begin{align*} v_0&=21\\ v_1&=20+\frac{1}{v_0}=20+\frac{1}{20+1}\\ v_2&=19+\frac{1}{v_1}=19+\frac{1}{20+\frac{1}{20+1}}\\ & \dots\\ v_{20}&=1+\frac{1}{v_{19}}=1+\frac{1}{2+\frac{1}{\dots+\frac{1}{{\dots+\frac{1}{20+1}}}}} \end{align*} And we finally put $u_{20}=\frac{1}{v_{20}}$. Therefore the above script computes: $$ u_{20}=\frac{3864956170297235421}{5538974690732597941}. $$

Exercise 5. Multivariate Polynomials (Taken from BX2024's Test)

Use SymPy to find the coefficient of $x^3y^2z^7$ in the polynomial $(x+2y+z)^{12}$.

Sympy finds $$ (x+2y+z)^{12}= x^{12}+ \dots + 31680x^3y^2z^7+\dots +z^{12}. $$