Constant-coefficient Homogeneous Linear Recurrences
Introduction
A constant-coefficient homogeneous linear recurrence sequence (also called a C-finite or C-recursive sequence) is a common basic class of recurrence sequences.
For a sequence (aj)j≥0 and its recurrence
an=j=1∑dcjan−j,(n≥d)
where the cj are not all zero, our goal is to compute ak after the initial values a0,…,ad−1 and the coefficients c1,…,cd in the recurrence have been given. If k≫d, we want a faster algorithm.
The sequence (aj)j≥0 is called a constant-coefficient homogeneous linear recurrence sequence of order d.
Fiduccia Algorithm
The Fiduccia algorithm computes ak using polynomial modular reduction and fast exponentiation in time O(M(d)logk), where O(M(d)) denotes the time needed to multiply two polynomials of degree O(d).
Algorithm: Construct the polynomials Γ(x):=xd−∑j=0d−1cd−jxj and A(x):=∑j=0d−1ajxj. Then
ak=⟨xkmodΓ(x),A(x)⟩
where the inner product is defined by ⟨(∑j=0n−1fjxj),(∑j=0n−1gjxj)⟩:=∑j=0n−1fjgj.
It follows that the first row of ((CΓ)k)⊺ is BxkmodΓ, and the result follows from the definition of matrix multiplication.
Representation as a Rational Function
For the sequence (aj)j≥0 above, there always exists a rational function
Q(x)P(x)=j≥0∑ajxj
with Q(x)=xdΓ(x−1) and degP<d. We call it a rational function because P(x),Q(x) are polynomials.
Proof: For P(x)=∑j=0d−1pjxj and Q(x):=∑j=0dqjxj, consider the coefficient definition of Q(x)P(x)=∑j≥0q~jxj. This is almost exactly the definition of formal power series division:
q~N=⎩⎨⎧p0q0−1,(pN−∑j=1Nqjq~N−j)⋅q0−1,−q0−1∑j=1dqjq~N−j, if N=0, else if N<d, otherwise.
We only need to set
P(x)=((j≥0∑ajxj)⋅xdΓ(x−1))modxd
Then, by the definition of q~N, we necessarily have Q(x)P(x)=∑j≥0ajxj.
Bostan–Mori Algorithm
Computing a Single Term
Our goal is still to give the polynomials P(x),Q(x) and compute [xk]Q(x)P(x).
The Bostan–Mori algorithm is based on Graeffe iteration. For the polynomials P(x),Q(x) above,
Since the denominator V(x2) is an even function, the subproblem only needs to consider one side:
[xk]Q(x)P(x)=[x⌊k/2⌋]V(x)Ukmod2(x)
We pay the cost of two polynomial multiplications to reduce the problem to at least half its original size. When k=0, it is clear that [x0]Q(x)P(x)=Q(0)P(0), so the time complexity is as stated above.
Computing Several Consecutive Terms
The goal is to give the polynomials P(x),Q(x) and compute [x[L,R)]Q(x)P(x). In the following computation, we only need to consider the coefficients that affect the answer; this is the key to the Bostan–Mori algorithm.
We may assume that degP<degQ; otherwise, one polynomial long division can reduce the problem to this case.
Let us first consider the simpler problem:
[x[L,R)]Q(x)1=[x[L,R)]Q(x)Q(−x)1⋅Q(−x)
We need to compute [x[L−degQ,R)]Q(x)Q(−x)1, then perform one multiplication and extract the coefficients of xL,…,xR−1. Let V(x2)=Q(x)Q(−x). Then we only need to compute
[x[⌈2L−degQ⌉,⌈2R⌉)]V(x)1
which allows us to recover [x[L−degQ,R)]Q(x)Q(−x)1. Consequently, we only need to compute [x[L−degP,R)]Q(x)1 and then multiply by P(x) to obtain [x[L,R)]Q(x)P(x).
The algorithm above already works, but the time complexity of each recursive call depends on R−L. We would like to eliminate this dependence on R−L during the recursive computation. More specifically, first consider computing [x[L,L+degQ+1)]Q(x)1. Consider
123456Algorithm Slice-Coefficients(Q,L):Input: Q(x)∈C[x],L∈Z.Output: [x[L,L+degQ+1)]Q(x)−1.if L≤1 then return [x[L,L+degQ+1)]Q(x)−1Use other algorithm to compute Q(x)−1V(x2)←Q(x)Q(−x)k←⌈2L−degQ⌉(tk,…,tk+degQ)←Slice-Coefficients(V,k)T(x)←x(L−degQ)mod2∑j=0degQtj+kx2jreturn [x[degQ,2degQ+1)]T(x)Q(−x)
However, this algorithm alone is not sufficient; we need to find a rational function again and compute more coefficients.
Finding a New Rational Function Representation
We know Q(x) itself and a consecutive portion of the coefficients of Q(x)−1, such as [x[L,L+degQ)]Q(x)−1 for L≥0. We want to compute [x[L+degQ,L+2degQ)]Q(x)−1. This is equivalent to finding a P(x) with degP<degQ such that the first degQ terms of Q(x)P(x) are the same as [x[L,L+degQ)]Q(x)−1. In simple terms, the recurrence relation (the denominator of the rational function) remains unchanged; all we do is replace the initial values (the numerator of the rational function).
Specifically, consider
Q(x)P(x)=j≥0∑ajxj
We now want to advance the recurrence by n terms, which means
j≥n∑ajxj−n=Q(x)xnP(x)−Q(x)xnQ(x)∑j=0n−1ajxj
We first use one call to Slice-Coefficients(Q,L−degP) to compute [x[L−degP,L−degP+degQ+1)]Q(x)−1. We then extend and combine it to obtain [x[L−degP,L+degQ)]Q(x)−1, and recompute a numerator such that
Q(x)P(x)=j≥0∑([xL+j]Q(x)P(x))xj
Finally, we use formal power series division to compute [x[0,R−L)]Q(x)P(x), in time O(M(d)logL+M(R−L)).