Keywords

1 Introduction

A sequence is called D-finite (or P-recursive or holonomic), if it satisfies a linear recurrence with polynomial coefficients. These sequences appear in many applications, e.g., in combinatorics or as coefficient sequences of special functions [7, 32]. They are interesting from the symbolic computation point of view, as they can be represented by a finite amount of data – the recurrence coefficients and sufficiently many initial values. Several closure properties hold for holonomic sequences and there exist summation algorithms that work with this representation for input and output. These methods are used to automatically prove and derive identities for holonomic sequences. When it comes to automatic proving of inequalities on holonomic sequences, there are not many algorithms available. Gerhold and Kauers [10] introduced a method in 2005 that can be used for sequences satisfying (a system of) recurrences including in particular holonomic sequences. This method (together with variations of it) has been applied successfully on several examples [17, 29, 31]. Still, a priori it is not known in general whether the procedure terminates [22].

In this paper, we restrict our study to C-finite sequences, i.e., holonomic sequences with constant coefficients, and the problem of proving positivity. This is known to be decidable for integer linear recurrences of order 2 [12], order 3 [23], order at most 5 and is related to difficult number theoretic problems for higher order [28]. We give an overview on some methods which can be used to prove the positivity of C-finite sequences, including the Gerhold–Kauers method and the most used variation (Algorithms 1 and 2 below). Other methods are based on theoretical results that, as far as we know, have not yet been implemented and tested on practical input on a larger scale. For testing the effectiveness of these different algorithms, we use input from the Online Encyclopedia of Integer Sequences (OEIS) [27] that are likely candidates for positive sequences. Our implementations are done both in SageMath and Mathematica and the source files as well as testing data are made available online (see links in Sect. 4).

2 Preliminaries

We introduce some notations and definitions that will be used throughout the paper. We always assume that \(\mathbb {Q}\subseteq \mathbb {K}\subsetneq \mathbb {R}\) is some number field. We denote the field of algebraic numbers by \(\overline{\mathbb {Q}}\) and the field of real algebraic numbers by \(\mathbb {A}{:}{=}\overline{\mathbb {Q}}\cap \mathbb {R}\). We denote the \(\mathbb {K}\)-vector space of sequences by \(\mathbb {K}^\mathbb {N}\) and let \(\sigma \) denote the shift operator \(\sigma ((c(n))_{n \in \mathbb {N}}) {:}{=}(c(n+1))_{n \in \mathbb {N}}\).

2.1 Linear Recurrence Sequences

We denote the Ore algebra of shift operators by \(\mathbb {K}[x]\langle \sigma \rangle \). Let \(\mathcal {A}= \sum _{i=0}^r p_i (x) \sigma ^i \in \mathbb {K}[x]\langle \sigma \rangle \). If \(p_r \ne 0\), then r is called the order of \(\mathcal {A}\) and \(\max _{i=0,\dots ,r} \deg (p_i)\) is called the degree of \(\mathcal {A}\). The operator \(\mathcal {A}\) acts on a sequence \(c \in \mathbb {K}^\mathbb {N}\) in the natural way as

$$\begin{aligned} \mathcal {A}c = (p_0(n) c(n) + \dots + p_r(n) c(n+r))_{n \in \mathbb {N}}. \end{aligned}$$

A sequence \(c \in \mathbb {K}^\mathbb {N}\) is called D-finite (or P-recursive or holonomic) if there is a non-zero operator \(\mathcal {A}\in \mathbb {K}[x]\langle \sigma \rangle \) with \(\mathcal {A}c = 0\), i.e., the sequence satisfies a linear recurrence with polynomial coefficients. We call \(\mathcal {A}\) an annihilating operator of c. It is well known that D-finite sequences form a computable difference ring [21]. The minimal possible order r of an annihilating operator is also called the order of the sequence c. The degree of c is then just defined as the degree of this operator.

A D-finite sequence c is called C-finite if it satisfies a linear recurrence with constant coefficients, i.e., if there are \(\gamma _0,\dots ,\gamma _r \in \mathbb {K}\) with \(\gamma _r \ne 0\) such that

$$\begin{aligned} \gamma _0 c(n) + \dots + \gamma _r c(n+r) = 0, \quad \text { for all } n \in \mathbb {N}. \end{aligned}$$
(1)

Again, the order of c as a C-finite sequence is the minimal r (note that the order of c considered as a C-finite sequence can be different from the order considered as a D-finite sequence, cf. Lemma 3). The set of C-finite sequences is again a computable difference ring. Every such sequence can be uniquely described by the coefficients of the recurrence \(\gamma _0,\dots ,\gamma _r\) and sufficiently many initial values \(c(0),\dots ,c(r-1)\).

2.2 Characteristic Polynomial

For an operator \(\mathcal {A}= \sum _{i=0}^r p_i (x) \sigma ^i \in \mathbb {K}[x]\langle \sigma \rangle \) the characteristic polynomial is defined as

$$\begin{aligned} \chi (\mathcal {A}) {:}{=}{{\,\mathrm{lc}\,}}_x \left( \sum _{i=0}^r p_i (x) y^i \right) \in \mathbb {K}[y]. \end{aligned}$$

The roots of \(\chi (\mathcal {A})\) are called eigenvalues and usually govern the asymptotic behavior of sequences which are annihilated by \(\mathcal {A}\) [22].

We can extend the notion of the characteristic polynomial to the left-Noetherian ring \(\mathbb {K}(x)\langle \sigma \rangle \). For a univariate polynomial \(p \in \mathbb {K}[x]\) we denote by \({{\,\mathrm{coeff}\,}}\left( p, i\right) \in \mathbb {K}\) the coefficient of \(x^i\) in p. For a rational function \(\tfrac{p(x)}{q(x)}\) with coprime \(p,q\in \mathbb {K}[x]\) we define the degree as \(\deg (p/q) = \deg (p)-\deg (q)\) and call

$$\begin{aligned} {{\,\mathrm{lc}\,}}(p/q) {:}{=}{{\,\mathrm{coeff}\,}}\left( p/q, \deg (p/q)\right) {:}{=}{{\,\mathrm{lc}\,}}(p)/{{\,\mathrm{lc}\,}}(q) \end{aligned}$$

the leading coefficient of p/q. Now, for an operator \(\mathcal {A}= \sum _{i=0}^r \tfrac{p_i(x)}{q_i(x)} \sigma ^i \in \mathbb {K}(x)\langle \sigma \rangle \) with \(\deg (\mathcal {A}) {:}{=}\max _{i=0,\dots ,r} \deg (p_i/q_i)\) we define the characteristic polynomial as

$$\begin{aligned} \chi (\mathcal {A}) {:}{=}\sum _{\begin{array}{c} i=0 \\ \deg (p_i/q_i) = \deg (\mathcal {A}) \end{array} }^r {{\,\mathrm{lc}\,}}(p_i/q_i) y^i \in \mathbb {K}[y]. \end{aligned}$$

Next, in Lemma 1 and Lemma 2, we state some basic properties of the characteristic polynomial. Since we could not find references for those, we add the proofs for sake of completeness.

Lemma 1

Let \(\mathcal {A},\mathcal {B}\in \mathbb {K}(x)\langle \sigma \rangle \). Then \(\chi (\mathcal {A}\mathcal {B}) = \chi (\mathcal {A}) \chi (\mathcal {B})\).

Proof

Let \(\mathcal {A}{:}{=}\sum _{i=0}^r p_i(x) \sigma ^i \in \mathbb {K}(x)\langle \sigma \rangle \) and \(\mathcal {B}{:}{=}\sum _{j=0}^s q_j(x) \sigma ^j \in \mathbb {K}(x)\langle \sigma \rangle \) and \(d_\mathcal {A}{:}{=}\max _{i=0,\dots ,r} \deg p_i, d_\mathcal {B}{:}{=}\max _{j=0,\dots ,s} \deg q_j \in \mathbb {Z}\) their respective degrees. We show that \(\mathcal {A}\mathcal {B}\) has degree \(d_\mathcal {A}+ d_\mathcal {B}\). By the definition of multiplication in  \(\mathbb {K}(x)\langle \sigma \rangle \) and the properties of the degree of a rational function, the degree of \(\mathcal {A}\mathcal {B}\) is certainly bounded by \(d_\mathcal {A}+ d_\mathcal {B}\). Let \(i',j'\) be maximal such that \(\deg p_{i'} = d_\mathcal {A}\) and \(\deg q_{j'} = d_\mathcal {B}\). We show that the coefficient of \(\sigma ^{i' + j'}\) of \(\mathcal {A}\mathcal {B}\) has degree \(d_\mathcal {A}+ d_\mathcal {B}\). This coefficient is given by \(\sum _{l=0}^{i'+j'} p_l(x) q_{i'+j'-l}(x+l)\). Because of the choice of \(i',j'\) we have

$$\begin{aligned} \deg (p_l(x) q_{i'+j'-l}(x)) = \deg (p_l(x)) + \deg (q_{i'+j'-l}(x+l)) < d_\mathcal {A}+ d_\mathcal {B}\end{aligned}$$

for all \(l \ne i'\). For \(l=i'\), we have \(\deg (p_l(x) q_{i'+j'-l}(x))=d_\mathcal {A}+ d_\mathcal {B}\), so by the properties of the degree we have

$$\begin{aligned} \deg \left( \sum _{l=0}^{i'+j'} p_l(x) q_{i'+j'-l}(x+l) \right)&= \max _{l=0,\dots ,i'+j'} \left( \deg \left( p_l(x)\right) + \deg \left( q_{i'+j'-l}(x+l) \right) \right) \\&= d_\mathcal {A}+ d_\mathcal {B}. \end{aligned}$$

Next, we show that all coefficients of \(\chi (\mathcal {A}) \chi (\mathcal {B})\) and \(\chi (\mathcal {A}\mathcal {B})\) agree. Let \(i \in \{ 0, \dots , r+s \}\). Then,

$$\begin{aligned} {{\,\mathrm{coeff}\,}}\left( \chi (\mathcal {A}), i\right) = {{\,\mathrm{coeff}\,}}\left( p_i(x), d_\mathcal {A}\right) , \quad {{\,\mathrm{coeff}\,}}\left( \chi (\mathcal {B}), i\right) = {{\,\mathrm{coeff}\,}}\left( q_i(x), d_\mathcal {B}\right) \end{aligned}$$

and therefore

$$\begin{aligned} {{\,\mathrm{coeff}\,}}\left( \chi (\mathcal {A}) \chi (\mathcal {B}), i\right) = \sum _{j=0}^i {{\,\mathrm{coeff}\,}}\left( p_j(x), d_\mathcal {A}\right) {{\,\mathrm{coeff}\,}}\left( q_{i-j}(x), d_\mathcal {B}\right) . \end{aligned}$$

In the first part of the proof, we have shown that \(\mathcal {A}\mathcal {B}\) has degree \(d_\mathcal {A}+ d_\mathcal {B}\). Therefore,

$$\begin{aligned} {{\,\mathrm{coeff}\,}}\left( \chi (\mathcal {A}\mathcal {B}), i\right)&= {{\,\mathrm{coeff}\,}}\left( \sum _{j=0}^i p_j(x) q_{i-j}(x+j), d_\mathcal {A}+ d_\mathcal {B}\right) \\&= \sum _{j=0}^i {{\,\mathrm{coeff}\,}}\left( p_j(x) q_{i-j}(x+j), d_\mathcal {A}+ d_\mathcal {B}\right) \\&= \sum _{j=0}^i {{\,\mathrm{coeff}\,}}\left( p_j(x), d_\mathcal {A}\right) {{\,\mathrm{coeff}\,}}\left( q_{i-j}(x+j), d_\mathcal {B}\right) \\&= \sum _{j=0}^i {{\,\mathrm{coeff}\,}}\left( p_j(x), d_\mathcal {A}\right) {{\,\mathrm{coeff}\,}}\left( q_{i-j}(x), d_\mathcal {B}\right) . \end{aligned}$$

   \(\square \)

Suppose \(\mathcal {A}\) is an annihilator of a and \(\mathcal {B}\) an annihilator of b. Then, the least common left multiple \({{\,\mathrm{lclm}\,}}(\mathcal {A},\mathcal {B})\) is an annihilator of \(a+b\) [19].

Lemma 2

Let \(\mathcal {A},\mathcal {B}\in \mathbb {K}[x]\langle \sigma \rangle \). Then

$$\begin{aligned} \chi (\mathcal {A}) \mid \chi ({{\,\mathrm{lclm}\,}}(\mathcal {A},\mathcal {B}))\text { and } \chi (\mathcal {B}) \mid \chi ({{\,\mathrm{lclm}\,}}(\mathcal {A},\mathcal {B})). \end{aligned}$$

In particular, we have

$$\begin{aligned} {{\,\mathrm{lcm}\,}}(\chi (\mathcal {A}), \chi (\mathcal {B})) \mid \chi ({{\,\mathrm{lclm}\,}}(\mathcal {A},\mathcal {B})). \end{aligned}$$

Proof

Let \(\mathcal {C}\in \mathbb {K}(x)\langle \sigma \rangle \) be such that \(\mathcal {C}\mathcal {A}= {{\,\mathrm{lclm}\,}}(\mathcal {A},\mathcal {B})\). Then, with Lemma 1 we have

$$\begin{aligned} \chi ({{\,\mathrm{lclm}\,}}(\mathcal {A},\mathcal {B})) = \chi (\mathcal {C}\mathcal {A}) = \chi (\mathcal {C}) \chi (\mathcal {A}). \end{aligned}$$

   \(\square \)

Example 1

In Lemma 2, divisibility cannot be replaced with equality. Consider \(\mathcal {A}{:}{=}1+\sigma \) and \(\mathcal {B}{:}{=}x + (x+1) \sigma \). Then,

$$\begin{aligned} \chi (\mathcal {A}) = \chi (\mathcal {B}) = 1+y, \end{aligned}$$

but

$$\begin{aligned} \chi ({{\,\mathrm{lclm}\,}}(\mathcal {A}, \mathcal {B})) = \chi (x+ (2 x + 2) \sigma + (x + 2) \sigma ^2) = 1 + 2y+y^2. \end{aligned}$$

An operator \(\mathcal {A}= \sum _{i=0}^r p_i \sigma ^i \in \mathbb {K}[x]\langle \sigma \rangle \) is called balanced if

$$\begin{aligned} \deg p_0 = \deg p_r = \max _{i=0,\dots ,r} \deg p_i. \end{aligned}$$

Equivalently, \(\mathcal {A}\) is balanced if and only if the degree of \(\chi (\mathcal {A}) \in \mathbb {K}[y]\) equals the order of \(\mathcal {A}\) and the trailing coefficient of \(\chi (\mathcal {A})\) is non-zero, i.e., \(y \not \mid \chi (\mathcal {A})\).

2.3 Positivity

Suppose we are given a C-finite sequence c in terms of a recurrence and sufficiently many initial values. Our goal is to prove \(c(n) > 0\) for all \(n \in \mathbb {N}\) (i.e., show that c is positive) or to find an index \(n_0 \in \mathbb {N}\) such that \(c(n_0) \le 0\). The very same methods can always be applied to show non-negativity instead of strict positivity of a sequence.

If bc are C-finite sequences, then the inequality \(b > c\) (or \(b \ge c\)) can easily be reduced to the positivity problem. The sequence \(b-c\) is again C-finite. Hence, proving the equivalent positivity problem \(b-c > 0\) (or \(b - c \ge 0\)) shows the original inequality.

Suppose c is C-finite satisfying the recurrence (1). Let \(k \in \mathbb {N}\) be minimal such that \(\gamma _k \ne 0\). Now, define \(\tilde{c}{:}{=}\sigma ^k c\). Then, \(\tilde{c}\) is again C-finite satisfying the recurrence

$$\begin{aligned} \gamma _k \tilde{c}(n) + \dots + \gamma _r \tilde{c}(n+r-k) = 0, \quad \text { for all } n \in \mathbb {N}. \end{aligned}$$

The sequence c is positive if and only if the sequence \(\tilde{c}\) and the initial values \(c(0),\dots ,c(k-1)\) are positive. Therefore, we can (and will) always assume that a C-finite sequence c is given by a recurrence with coefficients \(\gamma _0,\dots ,\gamma _r\) with \(\gamma _0,\gamma _r \ne 0\). Such a sequence c can then always be written as a polynomial-linear combination of exponential sequences. One can compute polynomials \(p_1,\dots ,p_m\) \(\in \overline{\mathbb {Q}}[x]\) and pairwise distinct non-zero constants \(\lambda _1,\dots ,\lambda _m \in \overline{\mathbb {Q}}\) such that

$$\begin{aligned} c(n) = \sum _{i=1}^m p_i(n) \lambda _i^n, \quad \text { for all } n \in \mathbb {N}. \end{aligned}$$
(2)

These \(\lambda _i\) are called the eigenvalues of c and they are the roots of the characteristic polynomial \(\sum _{i=0}^r \gamma _i y^i \in \mathbb {K}[y]\) of the minimal order recurrence of c. More precisely, if \(\lambda _i\) is a root of multiplicity \(d_i\), then \(\deg (p_i) = d_i-1\). Hence, \(r=\sum _{i=1}^m d_i\) [21].

Two sequences bc which are non-zero from some term on are called asymptotically equivalent if \(\lim _{n \rightarrow \infty } \tfrac{b(n)}{c(n)} = 1\). In this case, we write \(b \sim c\). The asymptotic behavior of c is governed by the k eigenvalues of maximal modulus, we call them the dominant eigenvalues. We assume \(| \lambda _1 | = \dots = | \lambda _k | > | \lambda _{k+1} | \ge \dots \ge | \lambda _m |\). Let \(d {:}{=}\max _{i=1,\dots ,k} \deg p_i\). Then, \(c(n) \sim n^d \sum _{i=1}^k {{\,\mathrm{coeff}\,}}\left( p_i, d\right) \lambda _i^n\) [21].

In the special case that we have a unique dominant eigenvalue (i.e., \(k=1\)) we have \(c(n) \sim \gamma n^d \lambda _1^n\) for some \(\gamma \) [21]. Hence, c can only be a positive sequence if \(\gamma ,\lambda _1 \in \mathbb {A}\) and \(\gamma>0,\lambda _1>0\). Then, c is positive if and only if \(c(n) / \lambda _1^n\) is positive. Therefore, it is sufficient to show positivity of a sequence

$$\begin{aligned} p(n) + \sum _{i=1}^s \left( o_i(n) \xi _i^n + \overline{o_i}(n) \overline{\xi _i}^n \right) + \sum _{i=1}^l q_i(n) \rho _i^n \end{aligned}$$
(3)

with \(p \in \mathbb {A}[x], o_1,\dots ,o_s \in \overline{\mathbb {Q}}[x],q_1,\dots ,q_l \in \mathbb {A}[x]\) and constants \(\xi _1,\dots ,\xi _s \in \overline{\mathbb {Q}},\rho _1,\dots ,\rho _l \in \mathbb {A}\) where the leading coefficient of p is positive [28].

3 Algorithms

In this section we give an overview over some methods which can be used to prove positivity of a C-finite sequence. Algorithms 1 and 2 introduced below in Sects. 3.13.2 can be applied to D-finite sequences. As such they can be used to prove positivity of C-finite sequences. However, sometimes C-finite sequences satisfy a D-finite recurrence of lower order, which is better suited as input for these methods. In Sect. 3.3, we discuss when such a D-finite recurrence exists. A method based on the combination of Algorithms 1 and 2 as well as on the closed form of a C-finite sequence is introduced in Sect. 3.5. The methods described in Sects. 3.4 and 3.6 also make use of the closed form of C-finite sequences. They are based on known results, but we believe that they had not been implemented so far.

3.1 Algorithm 1

In 2008 [10], a method based on cylindrical algebraic decomposition [1, 3, 5, 6] (CAD) was introduced which can be used to show positivity of sequences that can be defined recursively along some discrete parameter. This procedure, however, is not guaranteed to terminate. For D-finite sequences of small order conditions which guarantee the termination of the algorithm were found [22, 30].

We give a short description of Algorithm 1 from [22]. For a D-finite sequence c of order r, the \(\mathbb {Q}(x)\)-vector space which is generated by the shifts of c is finitely generated [21]. In fact, it is generated by \(c,\dots ,\sigma ^{r-1} c\), i.e.,

$$\begin{aligned} \langle \sigma ^i c \mid i \in \mathbb {N}\rangle _{\mathbb {Q}(x)} = \langle c , \dots , \sigma ^{r-1} c \rangle _{\mathbb {Q}(x)} . \end{aligned}$$

Hence, for all \(\rho \in \mathbb {N}\) there are rational functions \(q_{\rho ,0}(x),\dots ,q_{\rho , r-1}(x) \in \mathbb {K}(x)\) with \(c(n+\rho ) = \sum _{i=0}^{r-1} q_{\rho ,i}(n) c(n+i)\) for all \(n \in \mathbb {N}\). The idea now is to check with CAD whether \(c(n),\dots ,c(n+r-1) > 0\) implies \(c(n+r) > 0\) where \(c(n+r)\) can be written in terms of the \(c(n),\dots ,c(n+r-1)\). If this is true, then by induction it would be sufficient to check finitely many initial values to deduce positivity of the entire sequence. If, however, this cannot be shown, then we can add \(c(n+r)>0\) to the hypothesis and show \(c(n+r+1) > 0\). This process is iterated. In the iteration step \(\rho \ge r\) we try to show positivity of the formula

$$\begin{aligned} \varPhi (\rho , c) {:}{=}\forall y_0,\dots ,y_{r-1}, x \in \mathbb {R}:&\left( x \ge 0 \wedge \bigwedge \limits _{j=0}^{\rho -1}\sum _{i=0}^{r-1} q_{j,i}(x) y_i> 0 \right) \\&\implies \sum _{i=0}^{r-1} q_{\rho ,i}(x) y_i > 0. \end{aligned}$$

Formula \(\varPhi (\rho ,c)\) is a generalized induction formula over the reals. It is certainly sufficient to prove the initial induction step and has the advantage of being a valid input for CAD. Here, we give a slightly adjusted version which searches for an index \(n_0\) such that the sequence \(\sigma ^{n_0} c\) is positive, i.e., it checks whether the sequence is eventually positive (hence, we denote the algorithm by Algorithm 1e). If such an \(n_0\) can be found by the algorithm, then it is sufficient to check the initial values \(c(0),\dots ,c(n_0-1)\) of the sequence to prove positivity of c.

figure a

Clearly, Algorithm 1e is not guaranteed to terminate. E.g., if the input sequence c is negative, then the algorithm never terminates. Suppose the sequence c is eventually positive, i.e., there exists an \(n_0 \in \mathbb {N}\) such that \(\sigma ^{n_0} c\) is positive. Since \(\chi (c) = \chi (\sigma ^{n_0} c)\), the same termination conditions for Algorithm 1 in [22] now also apply to Algorithm .

Example 2

The sequence A001903 is C-finite of order 3 satisfying

$$\begin{aligned} c(n) - c(n+1) + c(n+2) - c(n+3) = 0 \end{aligned}$$

with initial values \(c(0)=1,c(1)=7,c(2)=9\). Algorithm 1e terminates for this sequence for \(n=4\) showing that c is positive.

3.2 Algorithm 2

Algorithm 2 in [22] again uses CAD to prove positivity of a D-finite sequence. The idea is to check whether there is a \(\mu > 0\) such that \(c(n+1) \ge \mu c(n)\) for all \(n \in \mathbb {N}\). By induction, if there is a \(\mu >0\) such that \(c(n+1)\ge \mu c(n),\dots ,c(n+r-1) \ge \mu c(n+r-2)\) implies \(c(n+r) \ge \mu c(n+r-1)\), then it is again sufficient to check finitely many initial values to prove positivity of c. Hence, the important step in the algorithm is to use CAD to check whether there exists a \(\mu > 0\) such that the formula

$$\begin{aligned} \varPsi (\xi , \mu , c) {:}{=}\forall y_0,\dots ,y_{r-1} \in \mathbb {R}\, \forall x \in \mathbb {R}_{\ge \xi } :&\left( y_0 > 0 \wedge \bigwedge \limits _{i=0}^{r-2} y_{i+1} \ge \mu y_i \right) \\&\implies \sum _{i=0}^{r-1} q_{i}(x) y_i \ge \mu y_{r-1} \end{aligned}$$

is valid where \(q_i \in \mathbb {K}(x)\) are such that \(c(n+r)=\sum _{i=0}^{r-1} q_i(n) c(n+i)\) for all \(n \in \mathbb {N}\).

Again, we give a slightly adjusted version which searches for an index \(n_0\) such that the sequence \(\sigma ^{n_0} c\) is positive. If the input sequence c is eventually positive, then the same termination conditions as for Algorithm 2 in [22] apply in this adjusted version.

figure b

Example 3

The sequence A005682 is C-finite of order 6 satisfying

$$\begin{aligned} c(n) + c(n+2) - 2 c(n+5) + c(n+6) = 0 \end{aligned}$$

with initial values \(c=\langle 1, 2, 4, 8, 15, 28, \dots \rangle \). Algorithm 2e terminates for this sequence at \(n=0\) showing that c is positive. Algorithm 1e cannot show positivity of c in 60 s.

3.3 D-finite Reduction

Clearly, every C-finite sequence is also D-finite. Sometimes, C-finite sequences satisfy shorter D-finite recurrences. In these cases, it can be helpful to use this shorter D-finite recurrence as the next example shows.

Example 4

Let c be the sequence defined by \(c(n)=n^2+1\) for all \(n \in \mathbb {N}\) (A002522). If c is considered as a C-finite sequence of order 3, then neither Algorithm 1e nor Algorithm 2e terminate in 60 s. If c is, however, considered as a D-finite sequence of order 1 and degree 2, then both algorithms terminate and show that c is indeed positive.

The next lemma shows that we can find a shorter D-finite recurrence of a C-finite sequence c if and only if c has eigenvalues of higher multiplicities or equivalently the characteristic polynomial of c is not squarefree.

Lemma 3

Let c be a C-finite sequence of order r with \(y \not \mid \chi (c)\). Then, c is D-finite of order \(m<r\) if and only if \(\chi (c)\) is not squarefree.

Proof

Suppose c is given as in (2).

\(\Longleftarrow \): The sequences \(p_i(n) \lambda _i^n\) are D-finite of order 1 and degree \(d_i\) over \(\overline{\mathbb {Q}}\). Hence, by the bounds for closure properties of D-finite sequences, c(n) is D-finite of order at most m over \(\overline{\mathbb {Q}}\) [21]. [9, Lemma 2] shows that the sequence is then also D-finite over \(\mathbb {K}\) with the same order and degree. In particular, if \(\chi (c)\) is not squarefree, then \(r = \sum _{i=1}^m d_i > m\).

\(\Longrightarrow \): Suppose c satisfies a D-finite recurrence of order \(m<r\) and degree d

$$\begin{aligned} \sum _{i=0}^m p_i(n) c(n+i) = 0 \quad \text { for all } n \in \mathbb {N}\end{aligned}$$
(4)

with \(p_i(n)=\sum _{k=0}^d p_{i,k} n^k\) where not all \(p_{i,k}\) are zero. Furthermore, suppose that c is C-finite of order r with pairwise distinct eigenvalues \(\lambda _1,\dots ,\lambda _r \in \overline{\mathbb {Q}}\), i.e., c(n) can be written as \(c(n) = \sum _{j=1}^r \gamma _j \lambda _j^n\) for some \(\gamma _j \in \overline{\mathbb {Q}}\). Using this closed form in (4) yields

$$\begin{aligned} \sum _{k=0}^d \left( \sum _{i=0}^m \sum _{j=1}^r p_{i,k} \gamma _j \lambda _j^{n+i} \right) n^k = 0. \end{aligned}$$
(5)

Let \(\gamma _{k,j} {:}{=}\sum _{i=0}^m p_{i,k} \gamma _j \lambda _j^i\), then (5) is equivalent to \(\sum _{k=0}^d \left( \sum _{j=1}^r \gamma _{k,j} \lambda _j^n \right) n^k = 0\). For \(n=0,\dots ,r (d+1)-1\) we get a homogeneous linear system for the \(\gamma _{k,j}\). The corresponding matrix is regular [24, Theorem 2.2.1],[13, Proposition 2.11], so \(\gamma _{k,j} = 0\) for all kj. Let k be such that \(p_{i,k} \ne 0\) for some i. Then,

$$\begin{aligned} 0 = \sum _{j=1}^r \lambda _j^n \sum _{i=0}^m p_{i,k} \gamma _j \lambda _j^{i} = \sum _{i=0}^m \sum _{j=1}^r p_{i,k} \gamma _j \lambda _j^{n+i} = \sum _{i=0}^m p_{i,k} c(n+i). \end{aligned}$$

Hence, c satisfies a C-finite recurrence of order \(m<r\), a contradiction to c being C-finite of order r.    \(\square \)

The proof of Lemma 3 shows that precisely the polynomial factors can be reduced in the D-finite recurrence, i.e., the m in the statement of Lemma 3 is the number of distinct eigenvalues of the sequence, which is also denoted by m in Eq. (2). The degree of the D-finite recurrence can be bounded by

$$\begin{aligned} (m(m+1)-m) \max _{i=1,\dots ,m} d_i = m^2 \max _{i=1,\dots ,m} d_i \le r^3 \end{aligned}$$

using [18, Theorem 2].

In practice, we can easily check whether \(\chi (c)\) is squarefree by checking whether \(\chi (c)\) and its derivative are coprime. The shorter D-finite recurrence can then be either found by guessing or by computing it explicitly from the closed form of c.

3.4 Classical Algorithm for Sequences with Unique Dominant Eigenvalue

If a C-finite sequence has a unique dominant eigenvalue, checking positivity of the sequence is known to be decidable [28]. In this section, we give a full description of such an algorithm based on that result.

As discussed in Sect. 2.3 we can assume that a C-finite sequence c is given in its closed form representation, i.e., as

$$\begin{aligned} c(n) = p(n) + r(n), \end{aligned}$$
(6)

where \(p \in \mathbb {A}[x]\) with \({{\,\mathrm{lc}\,}}(p) > 0\) and \(r(n) = \sum _{i=1}^m p_i(n) \lambda _i^n\) with \(p_i \in \overline{\mathbb {Q}}[x],\lambda _i \in \overline{\mathbb {Q}}\) and \(1 > | \lambda _1 | \ge | \lambda _2 | \ge \dots \ge | \lambda _m |\). The idea is now to compute an \(\varepsilon \in (0,1)\) and \(n_0,n_1 \in \mathbb {N}\) such that \(| r(n) | < (1-\varepsilon )^n\) for \(n \ge n_0\) and \(p(n) \ge (1-\varepsilon )^n\) for \(n \ge n_1\). Then, clearly c(n) is positive from \(\max (n_0, n_1)\) on. The initial values can be checked separately again.

figure c

For a polynomial \(p_i(x)=\sum _{j=0}^{d_i} \gamma _{i,j} x^j \in \mathbb {A}\) of degree \(d_i\) we can easily compute a constant \(c_i \in \mathbb {A}\) such that \(| p_i(n) | \le c_i n^{d_i}\) for all \(n \ge 1\). For example, we can choose \(c_i {:}{=}\sum _{i=0}^{d_i} | \gamma _{i,j} |\). Let \(c {:}{=}\sum _{i=1}^m c_i\) and \(d {:}{=}\max (d_1,\dots ,d_m)\), i.e., the maximal multiplicity of the eigenvalues \(\lambda _1,\dots ,\lambda _m\). Furthermore, let \(\varepsilon {:}{=}\tfrac{1-| \lambda _1 |}{2}\). Then, \(1-\varepsilon = | \lambda _1 | + \varepsilon \).

First, we show how \(n_0\) can be found such that \(| r(n) | < (1-\varepsilon )^n\) for \(n \ge n_0\). Let \(\mu {:}{=}\tfrac{| \lambda _1 | + \varepsilon }{| \lambda _1 |}\). If \(d=0\), then

$$\begin{aligned} | r(n) | \le c | \lambda _1 |^n< (1-\varepsilon )^n \iff \tfrac{\log (c)}{\log (\mu )} < n. \end{aligned}$$

Hence, we can choose \(n_0 {:}{=}\lceil \tfrac{\log (c)}{\log (\mu )} \rceil \) in this case. If \(d > 0\), then

$$\begin{aligned} | r(n) | \le c \, n^d | \lambda _1 |^n< (1-\varepsilon )^n \iff \log (c^{1/d}) < \tfrac{n}{d} \log (\mu ) - \log (n). \end{aligned}$$

The derivative of the right-hand side of this inequality is positive if \(n > \tfrac{d}{\log (\mu )}\), i.e., from \(\lceil \tfrac{d}{\log (\mu )} \rceil \) on the sequence on the right-hand side is monotonously increasing. Hence, if the inequality is true for some \(n_0 \ge \lceil \tfrac{d}{\log (\mu )} \rceil \), then it is true for all \(n \ge n_0\). Checking these values one by one, we will find a suitable \(n_0\) eventually.

If the polynomial \(p(x) = p_0\) is just constant, then \(p(n) \ge (1-\varepsilon )^n\) if and only if \(n \ge \tfrac{\log (p_0)}{\log (1-\varepsilon )}\). Otherwise, we can compute the largest real root \(x_1\) of the derivative of p(x). If \(p(n_1) \ge (1-\varepsilon )^{n_1}\) for any \(n_1 \ge \lceil x_1 \rceil \), then the inequality holds for all \(n \ge n_1\).

Example 5

The sequence A000126 is C-finite of order 4 satisfying

$$\begin{aligned} c(n) - c(n+1) - 2 c(n+2) + 3 c(n+3) - c(n+4) = 0 \end{aligned}$$

with initial values \(c=\langle 1, 2, 4, 8, \dots \rangle \). The sequence has the unique dominant root \(\tfrac{1+\sqrt{5}}{2}\). Algorithm 1e and Algorithm 2e do not terminate in 60 s whereas Algorithm C terminates after checking the first 14 terms.

3.5 Combination of Algorithm 1 and Algorithm 2

In the case that the C-finite sequence has a unique dominant eigenvalue, we can combine the closed form representation of the sequence together with Algorithm 1e and Algorithm 2e. As we know that the polynomial term p(n) in (3) certainly dominates the exponential terms, we can find indices \(n_i\) using Algorithm 1e and Algorithm 2e from which on the exponential sequences are dominated by the polynomial term. These input sequences have very low order (maximum order 3). Therefore, the termination criteria in [22] show that these algorithms terminate in most instances.

figure d

As Algorithm 2e terminates for essentially all sequences of order 2, the real algebraic part of Algorithm P certainly terminates.

Theorem 1

Algorithm P terminates if \(s=0\), i.e., if all eigenvalues of c are real algebraic.

Proof

Each sequence \(h(n) {:}{=}\tfrac{p(n)}{s+l}+ q_i(n) \rho _i^n\) is the sum of two balanced D-finite sequences gf over \(\mathbb {A}\) satisfying the recurrences

$$\begin{aligned} -p(n+1) g(n) + p(n) g(n+1) = 0, \quad -q_i(n+1) \rho _i f(n) + q_i(n) f(n+1) = 0 \end{aligned}$$

with characteristic polynomials

$$\begin{aligned} \chi (\mathcal {G}) = {{\,\mathrm{lc}\,}}(p) (y-1), \quad \chi (\mathcal {F}) = {{\,\mathrm{lc}\,}}(q_i)(y-\rho _i), \end{aligned}$$

where \(\mathcal {G},\mathcal {F}\) denote the annihilating operators of gf, respectively. As these characteristic polynomials are coprime, Lemma 2 yields

$$\begin{aligned} \chi (\mathcal {H}) = \chi (\mathcal {G}) \chi (\mathcal {F})= \gamma (y-1)(y-\rho _i) \end{aligned}$$

for some constant \(\gamma \) where \(\mathcal {H}\) denotes the annihilating operator of h. In particular, \(\mathcal {H}\) is balanced. Furthermore, \(h \sim p(n)\) by construction. With [22, Theorem 3], Algorithm 2e terminates with input h.    \(\square \)

It is conjectured that Algorithm 1e terminates for sequences of order 3 if the eigenvalues are complex. This is the case if we apply Algorithm 1e. Hence, if the conjecture is true, Algorithm P terminates for all C-finite sequences with a unique dominant eigenvalue.

Theorem 2

Assume Conjecture 1 from [22] is true. Then, Algorithm P terminates.

Proof

The proof of Theorem 1 already shows that the algorithm terminates for the real algebraic eigenvalues. Analogously, in the complex case, the sequences \(h(n) {:}{=}\tfrac{p(n)}{s+l} + o_i(n) \xi _i^n + \overline{o_i}(n) \overline{\xi _i}^n\) are D-finite of order 3 with a balanced annihilating operator \(\mathcal {H}\) with characteristic polynomial

$$\begin{aligned} \chi (\mathcal {H}) = \gamma (y-1)(y-\xi _i)(y-\overline{\xi _i}) \end{aligned}$$

for some constant \(\gamma \). With Conjecture 1, Algorithm 1e terminates on this input.    \(\square \)

Example 6

The sequence A002248 is C-finite of order 4 satisfying the recurrence

$$\begin{aligned} 4c(n) - 8 c(n+1) + 7 c(n+2) - 4 c(n+3) + c(n+4) = 0 \end{aligned}$$

with initial values \(c=\langle 2, 8, 14, 16, \dots \rangle \). The sequence has the unique dominant eigenvalue 2. Neither Algorithm 1e nor Algorithm 2e terminate in 60 s. However, both Algorithm C and Algorithm P terminate in negligible time.

3.6 Decomposition into Non-degenerate Sequences

A C-finite sequence c is called degenerate if the ratio \(\tfrac{\lambda _i}{\lambda _j}\) of two distinct eigenvalues \(\lambda _i,\lambda _j\) is a root of unity. Every C-finite sequence c can be written as the interlacing of non-degenerate and zero-sequences \(c_1,\dots ,c_k\) [8, Theorem 1.2]. For proving inequalities for C-finite sequences this decomposition often turned out useful [26, 28, 35]. For proving positivity of c we can compute this decomposition and prove positivity for every subsequence \(c_1,\dots ,c_k\).

One can explicitly compute the eigenvalues of a C-finite sequence and check whether the ratio of two eigenvalues is a root of unity [4]. Hence, a naive algorithm can decompose a sequence c into k subsequences

$$\begin{aligned} c_1(n)=c(kn),\dots ,c_k(n)=c(kn+k-1) \end{aligned}$$

and check whether all these subsequences are either zero or non-degenerate. Eventually, for large enough k, this is the case. This already works well in practice as we see in Sect. 4. A more efficient algorithm is given in [36].

If decomposition into subsequences is used together with Algorithm C or Algorithm P, then it is more efficient to check whether every subsequence has a unique dominant root (which can be done numerically with arbitrary-precision arithmetic) instead for checking degeneracy. The main bottleneck (cf. Example 8) is usually the computations of the subsequences. Hence, an efficient implementation should certainly aim to minimize the computations of these subsequences.

Example 7

The sequence A000115 is C-finite of order 8 and satisfies the recurrence

$$\begin{aligned} c(n) - c(n+1) - c(n+2) +&c(n+3) \\ -c(n+5)+c(n+6)+c(n+7)-&c(n+8) = 0. \end{aligned}$$

with initial values \(c=\langle 1, 1, 2, 2, 3, 4, 5, 6, \dots \rangle \). It has 6 dominant eigenvalues and is degenerate. It can be decomposed into 10 non-degenerate sequences with unique dominant eigenvalues. For these subsequences, Algorithm C and Algorithm P both have no problem showing positivity.

4 Comparison

As far as we are aware the only implementations of the algorithms presented in Sect. 3 are implementations of the Gerhold–Kauers method for Mathematica in the package SumCracker [16] and for SageMath [34]. We have implemented the presented algorithms in SageMath (using QEPCAD-B) and in Mathematica and tested them on C-finite sequences which could be obtained from the OEIS by guessing.

4.1 Test Set

We used guessing on the terms given in the OEIS to check for each sequence whether it is C-finite. To have reasonable certainty that the guessed recurrence is indeed correct we make sure that the corresponding linear systems are overdetermined with at least 15 more equations than variables. We take the first 1000 of these sequences for which the first 500 terms are strictly positive and are therefore highly likely to be positive altogetherFootnote 1.

The maximal order of these sequences is 42. The following table shows the number of sequences of each given order:

order

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

> 15

 

73

134

117

139

120

80

87

36

47

27

31

14

17

10

10

58

More than half of these sequences, 567, have a unique dominant eigenvalue. There are 102, 40, 70, 32 sequences with 2, 3, 4, 5 distinct dominant eigenvalues, respectively. Hence, there are 139 sequences with more than 6 distinct dominant eigenvalues.

About half of the sequences, 513, have a characteristic polynomial which is not squarefree. By Lemma 3 these are the sequences which have a shorter D-finite recurrence.

4.2 SageMath Implementation

The methods for proving inequalities for C-finite sequences (and in a limited way for D-finite sequences) are part of the rec_sequences package which is itself based on the ore_algebra package [20]. SageMath provides an interface to QEPCAD-B which allows CAD computations [2, 33]. This is used in the implementations of Algorithm 1 and Algorithm 2. For Algorithm C, we rely on fast arbitrary-precision arithmetic using the library Arb which is included in SageMath [15]. To decompose a sequence into subsequences with a unique dominant eigenvalue, we decompose the sequence into k subsequences and check, using arbitrary-precision arithmetic, whether all of these have a unique dominant eigenvalue. If they do not have a unique dominant eigenvalue, we increase k by one. The main bottleneck when decomposing is by far the computation of the subsequences. Checking whether a subsequence has a unique dominant eigenvalue or proving positivity of a sequence with a unique dominant eigenvalue using Algorithm C only takes negligible time in our examples.

The package is publicly availableFootnote 2. We give a list of the methods that can be used on C-finite sequences to show positivity. Every method has a parameter strict which is True by default and indicates whether strict positivity or non-negativity should be shown. The additional parameter time can be used to give an upper bound (in seconds) after which the algorithms should be terminated, the default value is \(-1\), indicating that they should not stop prematurely.

  • is_positive_algo1 implements Algorithm 1 from [22]. As an additional parameter bound can be specified which gives an upper bound on the number of iterations.

  • is_positive_algo2 implements Algorithm 2 from [22]. Again, bound can be specified. This method is also implemented for general D-finite sequences and can be called using is_positive on D-finite sequences.

  • is_positive_dominant_root implements Algorithm C for sequences with a unique dominant eigenvalue.

  • is_positive_dominant_root_decompose first tries to decompose the sequence into sequences with a unique dominant eigenvalue and zero sequences and calls Algorithm C on each of those.

  • is_positive is a combination of all these algorithms which additionally uses a reduction to D-finite sequences if possible. This method is also applied if the comparison operators > , \(\texttt {<}\), \(\texttt {>=}\), \(\texttt {<=}\) are used.

The following example session shows how the methods can be used.

figure e

Using the above mentioned methods, 987 out of the 1000 sequences from the test set could be proven to be positive where each method was given 60 s. The following table gives an overview on the number of sequences which could be proven to be positive by each method (“Comb.” stands for a combination of the algorithms and a “D” indicates that decomposition of the sequence is used):

Algo. 1

Algo. 2

Algo. C

D, Algo. C

Comb.

384

327

566

984

986

It is clear that decomposing the sequences and using Algorithm C is the most powerful method. The implementation of Algorithm C is very fast and takes at most 0.3 s for every example we considered.

Example 8

The sequence A008628 is C-finite of order 13 satisfying

$$\begin{aligned}&c(n)-c(n+1)-2c(n+2)+c(n+3)+2c(n+4)-c(n+6)\\ +&c(n+7)-2c(n+9)-c(n+10)+2c(n+11)+c(n+12)-c(n+13) = 0 \end{aligned}$$

with initial values = \(c=\langle 1, 1, 2, 3, 5, 7, 10, 13, 18, 23, 31, 38, 49 \rangle \). If the sequence is decomposed into 30 subsequences, then all of the subsequences have a unique dominant root and positivity of these subsequences can be shown easily with Algorithm C. It takes about 2 min to show positivity of the sequence c and 98% of the time is used to compute the subsequences in the decomposition.

Allowing more than 60 s for each sequence, all 1000 sequences can be shown to be positive using decomposition into subsequences with a unique dominant eigenvalue and Algorithm C for these subsequences.

4.3 Mathematica Implementation

The Mathematica package Positivity encompasses several of the algorithms described in Sect. 3. It is part of RISCErgoSum which is a collection of Mathematica packages developed at RISCFootnote 3. The package GeneratingFunctions is used to compute closure properties of C-finite sequences [25]. Our package, therefore, uses the same syntax as Mallinger’s package for defining sequences. For the quantifier elimination steps in Algorithm 1e and Algorithm 2e, we use the Mathematica method Resolve. It might be interesting to compare different quantifier elimination procedures for our concrete examples. Following, we give a list of the methods contained in the Positivity package. All methods can be used in a strict version to show strict positivity of a sequence (this is the default) or a non-strict version to show non-negativity of a sequence using the parameter Strict set to False. If the parameter Verbose is set to True, then more information about the different computation steps are printed.

  • KPAlgorithm1 implements Algorithm 1e, i.e., for a C-finite or D-finite sequence an index \(n_0\) is returned from which the sequence is guaranteed to be positive. If the parameter Eventual is set to False, then the traditional Algorithm 1 from [22] is executed which returns True if the sequence is positive or False if the sequence is not positive.

  • KPAlgorithm2 implements Algorithm 2e and Algorithm 2 from [22], analogous to KPAlgorithm1.

  • AlgorithmDominantRootClassic is an implementation of Algorithm C.

  • AlgorithmDominantRootCAD provides an implementation of Algorithm P.

  • AlgorithmClassic and AlgorithmCAD first decompose the sequence into non-degenerate and zero sequences and check positivity of these subsequences with AlgorithmDominantRootClassic and AlgorithmDominantRootCAD, respectively.

  • PositiveSequence combines some of the previous algorithms.

The methods can be used in the following way:

figure f

Comparing the different algorithms on the test set we see similar results as in the SageMath implementation. Every method was again aborted after 60 s. 980 out of the 1000 sequences could be shown to be positive by at least one of the methods. The following table shows the number of sequences which could be proven positive by each method:

Algo. 1

Algo. 2

Algo. C

Algo. P

D, Algo. C

D, Algo. P

Comb.

387

325

526

528

940

942

980

A more precise comparison of Algorithm C and Algorithm P shows that the two methods are not only equally powerful on the test set, but their runtime for the individual examples is also very similar. One can, however, expect that this is due to the specific implementation as the next example indicates. Hence, if provided by the computer algebra system, implementations based on numerical arbitrary-precision computations should be prefered over implementations based on algebraic number computations or quantifier elimination methods.

Example 9

The C-finite sequence A003520 is C-finite of order 5 satisfying

$$\begin{aligned} c(n) + c(n+4) - c(n+5) = 0 \end{aligned}$$

with initial values \(c(0)=\dots =c(4)=1\). The sequence has a unique dominant root. The Mathematica implementations of Algorithm C and Algorithm P both take several seconds. The SageMath implementation based on arbitrary-precision ball arithmetic instead of computations with algebraic numbers takes less than 0.1 s.

Increasing the time shows that the combined algorithm can show the positivity of 996 sequences with a time limit of 12 hours per sequence.

5 Conclusions

Summarizing, we have investigated some well known and new methods for showing positivity of C-finite sequences. To our knowledge, most of these algorithms were never implemented and it was not clear how well they perform on practical examples. It turned out that the methods are already powerful enough to prove the positivity of most C-finite sequences from the OEIS in reasonable time.

The given algorithms already cover most of the sequences appearing in combinatorial examples. One can, however, construct examples of non-degenerate sequences which have multiple dominant eigenvalues. For sequences with up to 5 dominant eigenvalues, positivity is still known to be decidable [28]. Other algorithms for showing positivity are given for instance in [11] and [14]. It would certainly be interesting to check whether and how these methods can be applied and implemented in practice and how their runtime compares to the algorithms presented here.