Linear Recurrence with Constant Coefficients
Introduction
A constant-coefficient homogeneous linear recurrence sequence (also known as C-finite or C-recursive sequence) is a common type of fundamental recurrence sequence.
For a sequence \(\left(a_j\right)_{j\geq 0}\) and its recurrence relation
where not all \(c_j\) are zero, our goal is to compute \(a_k\) given the initial values \(a_0,\dots ,a_{d-1}\) and the coefficients \(c_1,\dots ,c_d\) in the recurrence. If \(k\gg d\), we desire a faster algorithm.
Here, \(\left(a_j\right)_{j\geq 0}\) is called a constant-coefficient homogeneous linear recurrence sequence of order \(d\).
Fiduccia's Algorithm
Fiduccia's algorithm uses polynomial modular reduction and fast exponentiation to compute \(a_k\) in \(O(\mathsf{M}(d)\log k)\) time, where \(O(\mathsf{M}(d))\) denotes the time to multiply two polynomials of degree \(O(d)\).
Algorithm: Construct the polynomials \(\Gamma(x):=x^d-\sum_{j=0}^{d-1}c_{d-j}x^j\) and \(A(x):=\sum_{j=0}^{d-1}a_jx^j\). Then
where the inner product is defined as \(\left\langle \left(\sum_{j=0}^{n-1}f_jx^j\right),\left(\sum_{j=0}^{n-1}g_jx^j\right) \right\rangle :=\sum_{j=0}^{n-1}f_jg_j\).
Proof: We define the companion matrix of \(\Gamma(x)\) as
We define a polynomial \(b(x):=\sum_{j=0}^{d-1}b_jx^j\) and
Observe that
and
Representing the recurrence in matrix form gives
It follows that the first row of \(\left(\left(C_\Gamma\right)^{k}\right)^{\intercal}\) is \(B_{x^k\bmod{\Gamma}}\), proving the claim by the definition of matrix multiplication.
Representation as a Rational Function
For the above sequence \(\left(a_j\right)_{j\geq 0}\), there always exists a rational function
with \(Q(x)=x^d\Gamma\left(x^{-1}\right)\) and \(\deg{P}<d\). We call it a "rational function" because \(P(x)\) and \(Q(x)\) are "polynomials".
Proof: For \(P(x)=\sum_{j=0}^{d-1}p_jx^j\) and \(Q(x):=\sum_{j=0}^{d}q_jx^j\), consider the coefficient definition of \(\dfrac{P(x)}{Q(x)}=\sum_{j\geq 0}\tilde{q}_jx^j\). This is essentially the definition of "division" for formal power series:
We only need to set
Then, by the definition of \(\tilde{q}_N\), we must have \(\dfrac{P(x)}{Q(x)}=\sum_{j\geq 0}a_jx^j\).
Bostan–Mori Algorithm
Computing a Single Term
Our goal is still, given the polynomials \(P(x),Q(x)\) above, to compute \(\left\lbrack x^k\right\rbrack\dfrac{P(x)}{Q(x)}\).
The Bostan–Mori algorithm is based on Graeffe iteration. For the polynomials \(P(x),Q(x)\) above, we have
Since the denominator \(V(x^2)\) is an even function, the subproblem only needs to consider one side:
We pay the cost of two polynomial multiplications to reduce the problem size by at least half. When \(k=0\), clearly \(\left\lbrack x^0\right\rbrack \dfrac{P(x)}{Q(x)}=\dfrac{P(0)}{Q(0)}\). The time complexity is the same as above.
Computing a Range of Consecutive Terms
The goal is, given the polynomials \(P(x),Q(x)\) above, to compute \(\left\lbrack x^{\left\lbrack L,R\right)}\right\rbrack\dfrac{P(x)}{Q(x)}\). In the following computation, we only need to consider coefficients that "affect" the answer, which is key to the Bostan–Mori algorithm.
We can assume \(\deg{P}<\deg{Q}\); otherwise, we can reduce the problem to this case via one polynomial division with remainder.
First, consider a simpler problem:
We need to compute \(\left\lbrack x^{\left\lbrack L-\deg{Q},R\right)}\right\rbrack\dfrac{1}{Q(x)Q(-x)}\), then perform one multiplication and extract the coefficients of \(x^L,\dots ,x^{R-1}\). Let \(V(x^2)=Q(x)Q(-x)\). Then we only need to compute
to recover \(\left\lbrack x^{\left\lbrack L-\deg{Q},R\right)}\right\rbrack\dfrac{1}{Q(x)Q(-x)}\). Subsequently, we only need to compute \(\left\lbrack x^{\left\lbrack L-\deg{P},R\right)}\right\rbrack\dfrac{1}{Q(x)}\) and then multiply by \(P(x)\) once to obtain \(\left\lbrack x^{\left\lbrack L,R\right)}\right\rbrack\dfrac{P(x)}{Q(x)}\).
The above algorithm already works, but the time complexity of each recursion depends on \(R-L\). We hope to eliminate the dependence on \(R-L\) during the recursive computation. More specifically, first consider computing \(\left\lbrack x^{\left\lbrack L,L+\deg Q+1\right)}\right\rbrack \dfrac{1}{Q(x)}\). Consider
We need to compute
Then for \(V(x^2)=Q(x)Q(-x)\), we only need to compute
This is because
We know \(L+\deg Q\) and \(L-\deg Q\) have the same parity, so
Thus, we can write the pseudocode:
However, this algorithm alone is insufficient; we need to find a new rational function representation and compute more coefficients.
Finding a New Rational Function Representation
We know \(Q(x)\) itself and a consecutive range of coefficients of \(Q(x)^{-1}\), e.g., \(\left\lbrack x^{\left\lbrack L,L+\deg Q\right)}\right\rbrack Q(x)^{-1}\) for \(L\geq 0\). We wish to compute \(\left\lbrack x^{\left\lbrack L+\deg Q,L+2\deg Q\right)}\right\rbrack Q(x)^{-1}\). This is equivalent to finding some \(P(x)\) with \(\deg P< \deg Q\) such that the first \(\deg Q\) terms of \(\dfrac{P(x)}{Q(x)}\) match \(\left\lbrack x^{\left\lbrack L,L+\deg Q\right)}\right\rbrack Q(x)^{-1}\). Simply put: the recurrence relation (the denominator of the rational function) remains unchanged; we are only changing the initial values (the numerator of the rational function).
Specifically, consider
Now we want to advance the recurrence by \(n\) terms, i.e.,
We first use \(\operatorname{Slice-Coefficients}(Q,L-\deg{P})\) once to compute \(\left\lbrack x^{\left\lbrack L-\deg{P},L-\deg{P}+\deg{Q}+1\right)}\right\rbrack Q(x)^{-1}\). Then we extend and merge to obtain \(\left\lbrack x^{\left\lbrack L-\deg{P},L+\deg{Q}\right)}\right\rbrack Q(x)^{-1}\), and recompute a numerator such that
Finally, we use formal power series division to compute \(\left\lbrack x^{\left\lbrack 0,R-L\right)}\right\rbrack\dfrac{\widetilde{P}(x)}{Q(x)}\) in time \(O(\mathsf{M}(d)\log L+\mathsf{M}(R-L))\).
References
- Alin Bostan, Ryuhei Mori. A Simple and Fast Algorithm for Computing the \(N\)-th Term of a Linearly Recurrent Sequence.
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用