Generating functions and recurrences

Recurrences

A linear recurrence an=c1an−1+⋯+ckan−ka_n = c_1 a_{n-1} + \dots + c_k a_{n-k} is solved by the roots of its characteristic polynomial. Here the roots are exact algebraic numbers, so the closed form is exact too, and it is checked against the terms before it is returned.

Fibonacci, three ways

import { recurrence } from "@ratmath/discrete";

const fib = recurrence({ coefficients: [1, 1], initial: [0, 1] });
fib.terms(10).join(" ");            // "0 1 1 2 3 5 8 13 21 34"
fib.term(100);                      // 354224848179261915075, by a power of the companion matrix
String(fib.generatingFunction());   // "x/(1 − x − x^2)"
fib.closedForm().toString();
// "a(n) = −(√5/5)·((1 − √5)/2)^n + (√5/5)·((1 + √5)/2)^n"

The characteristic polynomial of an=c1an−1+⋯+ckan−ka_n = c_1 a_{n-1} + \dots + c_k a_{n-k} is χ(x)=xk−c1xk−1−⋯−ck\chi(x) = x^k - c_1 x^{k-1} - \dots - c_k. If its distinct roots are r1,…,rsr_1, \dots, r_s with multiplicities mim_i, every solution has the form

an=∑i=1s(bi,0+bi,1n+⋯+bi,mi−1nmi−1) ri n, a_n = \sum_{i=1}^{s} \bigl(b_{i,0} + b_{i,1} n + \dots + b_{i,m_i-1} n^{m_i-1}\bigr)\, r_i^{\,n},

and the kk unknowns bi,jb_{i,j} are fixed by the kk initial values. closedForm() finds the roots with @ratmath/algebraic, solves that linear system over the algebraic numbers, and compares the formula with further terms; checked records that it did.

Repeated and complex roots

recurrence({ coefficients: [4, -4], initial: [1, 4] }).closedForm().toString();
// "a(n) = 2^n + n·2^n": χ = (x − 2)², a double root
recurrence({ coefficients: [1, 1, 1], initial: [0, 0, 1] }).closedForm().toString();
// tribonacci: one real root 1.839286755214… and a complex pair, as certified digits

A root of degree two is written with square roots; higher degrees are shown by certified digits, but the value underneath stays exact, so at(n) still gives the integer term.

Inhomogeneous recurrences

A term p(n) bnp(n)\,b^n added on the right is absorbed by multiplying χ\chi by (x−b)deg⁡p+1(x - b)^{\deg p + 1}: the result is a longer homogeneous recurrence with the same terms. The original is kept in given.

const towers = recurrence({ coefficients: [2], initial: [0], plus: [1] });   // a(n) = 2a(n−1) + 1
towers.terms(8).join(" ");             // "0 1 3 7 15 31 63 127"
towers.closedForm().toString();        // "a(n) = −1 + 2^n"

Guessing a recurrence

Given terms, the Berlekamp–Massey algorithm finds the shortest linear recurrence that produces them, over the rationals. If none is shorter than half the data, the answer is null: Catalan numbers, for example, satisfy no linear recurrence with constant coefficients.

import { guessRecurrence } from "@ratmath/discrete";
String(guessRecurrence([0, 1, 1, 2, 3, 5, 8, 13]));   // "a(n) = a(n−1) + a(n−2), from a(0) = 0, a(1) = 1"
guessRecurrence([1, 1, 2, 5, 14, 42, 132, 429, 1430]);   // null

Sums and divide-and-conquer

import { closedSum, masterTheorem, divideAndConquer } from "@ratmath/discrete";
closedSum([0, 0, 1]).toString();       // "Σ_{k=0}^{n} k^2 = (1/3)·n^3 + (1/2)·n^2 + (1/6)·n"
closedSum([0, 1], 2).toString();       // "Σ_{k=0}^{n} (k)·2^k = (n − 1)·2^(n+1) + 2"
masterTheorem({ a: 2, b: 2, d: 1 }).bound;   // "Θ(n log n)": merge sort
masterTheorem({ a: 7, b: 2, d: 2 }).bound;   // "Θ(n^(log_2 7))": Strassen

∑k≤np(k)\sum_{k \le n} p(k) is a polynomial of one degree more, found from Newton's forward differences; ∑k≤np(k)rk=Q(n)rn+1−Q(−1)\sum_{k\le n} p(k) r^k = Q(n) r^{n+1} - Q(-1) for a polynomial QQ of the same degree as pp. The master theorem compares f(n)=ndlog⁡jnf(n) = n^d \log^j n with nlog⁡ban^{\log_b a}; the critical exponent is reported exactly when it is rational. divideAndConquer lists exact values of T(bm)T(b^m) so a bound can be looked at in numbers.

Try itEdit the code and run it with @ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs