Qm
Advanced

Matrix Exponentiation for Recurrences

A technique for computing the n-th term of a linear recurrence, like the Fibonacci sequence, in logarithmic time by expressing the recurrence as a matrix and repeatedly squaring it instead of iterating n times.

A linear recurrence like the Fibonacci rule Fn=Fn1+Fn2F_n = F_{n-1} + F_{n-2} can be rewritten as a matrix multiplication: stacking consecutive terms into a vector, (FnFn1)=M(Fn1Fn2)\begin{pmatrix} F_n \\ F_{n-1} \end{pmatrix} = M \begin{pmatrix} F_{n-1} \\ F_{n-2} \end{pmatrix} for a fixed matrix MM. Applying MM once advances the sequence by one step, so applying it nn times from the starting vector produces FnF_n, but computing MnM^n doesn't require nn separate multiplications. Repeated squaring computes MnM^n in about log2n\log_2 n matrix multiplications by writing nn in binary and combining powers of MM that double at each stage (M,M2,M4,M8,M, M^2, M^4, M^8, \dots), the same trick used for fast integer exponentiation.

For Fibonacci, M=(1110)M = \begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}. To find F10F_{10}, instead of 10 sequential additions, compute M2M^2, M4M^4, M8M^8 by three squarings, then combine M8M2M^8 \cdot M^2 to get M10M^{10}, about 4 matrix multiplications total rather than 10 scalar steps. The gap widens enormously for large nn: computing F109F_{10^9} takes about 30 matrix multiplications by this method versus a billion additions by direct iteration.

The technique generalizes to any order-kk linear recurrence with constant coefficients, using a k×kk \times k companion matrix, and is a standard tool whenever a coding interview or a simulation needs a huge recurrence term without simulating every intermediate step. It also composes naturally with modular arithmetic, since matrix multiplication only needs addition and multiplication, taking every entry modulo some large prime at each step keeps the numbers bounded, which is exactly what's needed when a problem asks for a huge Fibonacci-style term modulo 109+710^9+7.

Any linear recurrence can be advanced nn steps via MnM^n for a fixed matrix MM, and repeated squaring computes MnM^n in O(logn)O(\log n) multiplications instead of O(n)O(n) sequential steps.

Discussion

Sign in to join the discussion · reading is open to everyone

💡 Discussion rules

  1. Ask and answer about this concept. Off-topic gets removed.
  2. No homework dumps. Show what you tried first.
  3. Corrections are welcome. Cite a source when you claim an error.

Loading discussion…

Related concepts

Practice in interviews

Further reading

  • Cormen et al., Introduction to Algorithms, ch. 4
ShareTwitterLinkedIn