Recurrences
A linear recurrence 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 is . If its distinct roots are with multiplicities , every solution has the form
and the unknowns are fixed by the 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 added on the right is absorbed by multiplying by : 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
is a polynomial of one degree more, found from Newton's forward differences; for a polynomial of the same degree as . The master theorem compares with ; the critical exponent is reported exactly when it is rational. divideAndConquer lists exact values of so a bound can be looked at in numbers.
@ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.