Termination of Triangular Polynomial Loops

We consider the problem of proving termination for triangular weakly non-linear loops (twn-loops) over some ring $\mathcal{S}$ like $\mathbb{Z}$, $\mathbb{Q}$, or $\mathbb{R}$. The guard of such a loop is an arbitrary quantifier-free Boolean formula over (possibly non-linear) polynomial inequations, and the body is a single assignment of the form $(x_1, \ldots, x_d) \longleftarrow (c_1 \cdot x_1 + p_1, \ldots, c_d \cdot x_d + p_d)$ where each $x_i$ is a variable, $c_i \in \mathcal{S}$, and each $p_i$ is a (possibly non-linear) polynomial over $\mathcal{S}$ and the variables $x_{i+1},\ldots,x_{d}$. We show that the question of termination can be reduced to the existential fragment of the first-order theory of $\mathcal{S}$. For loops over $\mathbb{R}$, our reduction implies decidability of termination. For loops over $\mathbb{Z}$ and $\mathbb{Q}$, it proves semi-decidability of non-termination. Furthermore, we present a transformation to convert certain non-twn-loops into twn-form. Then the original loop terminates iff the transformed loop terminates over a specific subset of $\mathbb{R}$, which can also be checked via our reduction. Moreover, we formalize a technique to linearize (the updates of) twn-loops in our setting and analyze its complexity. Based on these results, we prove complexity bounds for the termination problem of twn-loops as well as tight bounds for two important classes of loops which can always be transformed into twn-loops. Finally, we show that there is an important class of linear loops where our decision procedure results in an efficient procedure for termination analysis, i.e., where the parameterized complexity of deciding termination is polynomial.


Introduction
Termination is one of the most important properties of a program. In this work, we study complete approaches for analyzing termination of certain classes of polynomial loops. Compared to incomplete techniques, such approaches have the advantage that they always yield a definite result. In particular, we investigate decidability of termination for our classes of loops.
In the following, we give a short overview on the contributions of our article and highlight how it extends our earlier conference paper [20]. There are already several decidability results for termination of linear loops [8,10,19,30,41,44,48,61,65], but only few results on the decidability of termination for certain forms of non-linear loops [42,43,46,63,64]. Moreover, these previous works only deal with loops whose guards only contain conjunctions, besides [46] which is restricted to guards defining compact sets. In this work, we regard possibly non-linear loops with arbitrary guards, i.e., they may also contain disjunctions and define non-compact sets. More precisely, we consider so-called twn-loops, where the update is mildly restricted to be "triangular" and "weakly non-linear" (see Sect. 2 for a formal definition). We study such loops over Z, Q, R A (the real algebraic numbers), and R, whereas existing decidability results for non-linear loops are restricted to loops over the reals.
Most techniques for proving termination of loops rely on polynomial ranking functions, see, e.g., [1,5,6,7,9,50]. However, such ranking functions are only sound for proving termination, i.e., in general, they cannot refute termination. In contrast to ranking functions, we use the computability of closed forms for the iterated update of the loop (Sect. 3). In this way, we can reduce termination of a loop to (in)validity of a certain formula. This reduction, which is a generalization of our earlier results for linear loops over Z with conjunctive guards [19], is sound and complete, i.e., validity of the resulting formula proves non-termination, whereas invalidity implies termination. Moreover, our reduction is computable. Analogously to our earlier work for linear loops [19], our decidability results on termination then follow from existing results on the decidability of certain theories. In this way, we show that termination of twn-loops is decidable over R A and R, and non-termination is semi-decidable over Z and Q (Sect. 4).
In Sect. 5 we use concepts from algebra to enlarge the classes of loops to which our reduction is applicable. This is done by transforming (certain) non-twn loops into twn-form without affecting their termination behavior (Sect. 5.1). In Sect. 5.2, we discuss for which loops our transformation is applicable. In this way, we generalize our results to a broader class of polynomial loops (Sect. 5.3).
In contrast to our earlier conference paper [20], in Sect. 6 we formalize the technique of [47] to linearize (the updates of) twn-loops in our setting. Using this formalization, we develop novel results on the complexity of linearization.
Afterwards, based on our decision procedure for termination in Sect. 4, on the transformation of Sect. 5, and on our complexity results for linearization from Sect. 6, we study the complexity of deciding termination in Sect. 7.
In Sect. 7.1 we show that deciding termination of linear loops with rational spectrum over Z, Q, R A , and R is Co-NP-complete. Moreover, we show that deciding termination of linear-update loops (where the update is linear but the guard may be non-linear) with real spectrum over R A and R is ∀R-complete. Here, a loop has rational or real spectrum, respectively, if its update matrix has rational or real eigenvalues only, and ∀R is the complexity class of problems which can be reduced to validity of a universally quantified formula of polynomial inequations over the reals. We also analyze the complexity of deciding termination for arbitrary twn-loops (with possibly non-linear updates) in Sect. 7.2. In our conference paper [20,Thm. 45], we had only analyzed this case for a bounded number of variables. In contrast, we now extend our analysis to the general case where the number of variables is not restricted (Thm. 7.6). To this end, we need our new results from Sect. 6 on the complexity of linearizing twn-loops.
Finally, in contrast to [20], we identify a class of linear loops (uniform loops) where termination can be interpreted as a parameterized problem which is decidable in polynomial time when fixing such a parameter (Sect. 7.3). Based on the transformation of Sect. 5, we show that the closed forms arising from uniform loops have a special structure. Therefore, here (in)validity of the formula from Sect. 4 which encodes termination can be checked in polynomial time.
Related work is discussed in Sect. 8 and all missing proofs can be found in App. A. So the current paper extends [20] by the following new material: • Sect. 6 on the linearization of twn-loops and its complexity.
• Thm. 7.6 on the complexity of deciding termination for arbitrary twn-loops where the number of variables is not restricted. • Sect. 7.3 on uniform loops where the parameterized complexity of deciding termination is polynomial. • Several additional explanations and remarks.

Preliminaries
A (polynomial) loop over a ring S has the form in Fig. 1, where Z ≤ S ≤ R A and ≤ denotes the subring relation. Here, x is a vector of d ≥ 1 pairwise different variables that range over S and u ∈ (S[ x]) d where S[ x] is the set of polynomials over x with coefficients in S. To improve readability, we use rowand column-vectors interchangeably. The guard ϕ is an arbitrary propositional (i.e., quantifier-free) formula over the atoms {p ⊲ 0 | p ∈ S[ x], ⊲ ∈ {≥,>}}. We denote the set of all such formulas by Th qf (S). In our setting, negation is syntactic sugar as, e.g., ¬(p > 0) is equivalent to −p ≥ 0. So w.l.o.g. the guard (or condition) ϕ of a loop is built from atoms, ∧, and ∨.
We require S ≤ R A instead of S ≤ R, as it is unclear how to represent transcendental numbers on computers. However, in Sect. 4 we will see that the loops considered in this work terminate over R iff they terminate over R A . Thus, our results immediately carry over to loops where the variables range while ( ϕ ) do x ← u

Fig. 2:
Loop L ex over Z over R. Hence, we sometimes also consider loops over S = R. However, even then we restrict ourselves to loops where all constants in ϕ and u are algebraic.
We often represent a loop as in Fig. 1 by the tuple (ϕ, u) of the condition ϕ and the update u = (u 1 , . . . , u d ). Unless stated otherwise, (ϕ, u) is always a loop over S using the variables x = (x 1 , . . . , x d ) where Z ≤ S ≤ R A . We use the following notions for certain classes of loops: A linear-update loop has the form (ϕ, A · x + b), and it has rational or real spectrum, respectively, if A has rational or real eigenvalues only. Then all eigenvalues of A are real algebraic numbers, since u ∈ (R A [ x]) d implies that all constants in u (and thus in A) are algebraic. A linear loop is a linear-update loop where ϕ is linear (i.e., its atoms are only linear inequations). Here, "linear" refers to "linear polynomials", i.e., of degree at most 1, so it includes affine functions. A conjunctive loop is a loop (ϕ, u) where ϕ does not contain disjunctions. d . To this end, we define ψ( p) = ψ[ x/ p] for first-order formulas ψ with the free variables x. As usual, function application associates to the left, i.e., u( b)( p) stands for ( u( b))( p). However, obviously ( u( b))( p) = u( b( p)) since applying polynomials only means that one instantiates variables.
Def. 2.1 formalizes the intuitive notion of termination for a loop (ϕ, u) and the related notion of eventual termination [10,61]. Here, u n denotes the n-fold application of u, i.e., u 0 ( e) = e and u n+1 ( e) = u( u n ( e)).
Definition 2.1 (Termination). Let (ϕ, u) be a loop over S and e ∈ S d . If ∀n ∈ N. ϕ( u n ( e)) holds, then e is a witness for non-termination. If (ϕ, u) does not have any witnesses for non-termination, it terminates (over S).
If u n0 ( e) is a witness for non-termination for some n 0 ∈ N, then e is called a witness for eventual non-termination.
For any entity s, V(s) is the set of all its free variables. Given an assignment x ← u, the relation ≻ u ∈ V( u) × V( u) is the transitive closure of {(x i , x j ) | i, j ∈ {1, . . . , d}, i = j, x j ∈ V(u i )}, i.e., x i ≻ u x j means that x i depends on x j . For example, if u = (x 1 + x 2 2 , x 2 + 1) then we have ≻ u = {(x 1 , x 2 )}. Now we can introduce the class of twn-loops. A loop (ϕ, u) is triangular if ≻ u is well founded. It is weakly non-linear if there is no 1 ≤ i ≤ d such that x i occurs in a non-linear monomial of u i , i.e., u i = c i · x i + p i where c i ∈ S and p i ∈ S[ x] does not contain x i . A twn-loop is triangular and weakly non-linear. We call a loop non-negative if it is weakly non-linear and the coefficient c i of the monomial x i in u i is non-negative for all 1 ≤ i ≤ d. A tnn-loop is triangular and non-negative, i.e., a tnn-loop is a special form of a twn-loop.
The restriction to triangular loops prohibits "cyclic dependencies" of variables (e.g., where the new values of x 1 and x 2 both depend on the old values of x 1 and x 2 ). For example, the loop whose body consists of the assignment (x 1 , x 2 ) ← (x 1 + x 2 2 , x 2 + 1) is triangular since ≻ = {(x 1 , x 2 )} is well founded, whereas a loop with the body (x 1 , x 2 ) ← (x 1 + x 2 2 , x 1 + 1) is not triangular. By the restriction to twn-loops we can compute a closed form for the n-fold application of the update u by handling one variable after the other. A vector q of d arithmetic expressions over x and a distinguished variable n is a closed form for u ∈ (S[ x]) d if q[ x/ e, n/n ′ ] = u n ′ ( e) for all e ∈ S d and n ′ ∈ N, i.e., both vectors of expressions evaluate to the same element of S d . Thus, q = u n .
Triangular loops are very common in practice. For example, in [21], 1511 polynomial loops were extracted from the Termination Problems Data Base (TPDB) [62], the benchmark collection used at the annual Termination and Complexity Competition [25], and only 26 of them were non-triangular.
A loop with the body (x 1 , x 2 ) ← (x 1 +x 2 2 , x 2 +1) is weakly non-linear, while a loop with (x 1 , x 2 ) ← (x 1 · x 2 , x 2 + 1) is not. In particular, weak non-linearity excludes assignments like x 1 ← x 2 1 that need exponential space, as x 1 grows doubly exponentially. By permuting variables, the update of every twn-loop can be transformed to the following form where c i ∈ S and p i ∈ S[x i+1 , . . . , x d ]: Consider the loop L ex over the ring Z in Fig. 2. This loop is triangular since ≻ u = {(x 1 , x 2 ), (x 1 , x 3 ), (x 2 , x 3 )} is well founded. Moreover, it is weakly non-linear. Since the coefficient of x i is 1 in the update of x i for all 1 ≤ i ≤ 3, this loop is also non-negative, i.e., L ex is tnn.
Our twn-loops are a special case of solvable loops.
The eigenvalues of a solvable loop are the union of the eigenvalues of all A i . So solvable loops allow for blocks of variables with linear dependencies, and twn-loops correspond to the case that all blocks have size 1. While our approach could be generalized to solvable loops with real eigenvalues, Thm. 5.15 (Sect. 5) shows that this generalization does not increase its applicability.
Here, y and z are pairwise disjoint vectors of variables. The free variables z range over R A and they are needed in Sect. 5 to characterize subsets of real (algebraic) numbers by formulas.
The existential fragment of the first-order theory of S and R A is the set Th ∃ (S, R A ) of all formulas ∃ y ′ ∈ R k ′ A , y ∈ S k . ψ, with a propositional formula ψ over {p ⊲ 0 | p ∈ Q[ y ′ , y, z], ⊲ ∈ {≥, >}} where k ′ , k ∈ N and the variables y ′ , y, and z are pairwise disjoint. A formula without free variables is closed.
In the following, we also consider formulas over inequations p ⊲ 0 where p's coefficients are from R A to be elements of Th ∃ (R A ) (resp. Th ∃ (S, R A )). The reason is that real algebraic numbers are Th ∃ (R A )-definable.
Validity of closed formulas from Th ∃ (S) or Th [13,60]. By undecidability of Hilbert's Tenth Problem [45], it is undecidable for S = Z. While validity of full first-order formulas (i.e., also containing universal quantifiers) over S = Q is undecidable [52], it is still open whether validity of closed formulas from Th ∃ (Q) or Th ∃ (Q, R A ) is decidable. However, validity of linear closed formulas from Th ∃ (S) or Th ∃ (S, R A ) is decidable for all S ∈ {Z, Q, R A , R} [15,26,32,36]. Here, a formula is linear if it only contains atoms p ⊲ 0 where p is linear.
So for example, chaining the loop ( [19] where chaining was used for triangular linear loops, we obtain the following theorem. (a) (ϕ ∧ ϕ( u), u( u)) is tnn, i.e., it is triangular and the coefficient of each As chaining is clearly computable, we get the following corollary. Corollary 3.3. Termination of twn-loops is reducible to termination of tnnloops.
It is well known that closed forms for tnn-loops are computable (see, e.g., [34,66]) since their bodies correspond to C-finite recurrences, which are known to be solvable [33]. The resulting closed forms may contain polynomial arithmetic and exponentiation w.r.t. n (as, e.g., x 1 ← 2 · x 1 has the closed form x 1 · 2 n ) as well as certain piecewise defined functions. For example, the closed form x We represent closed forms using poly-exponential expressions [19], where instead of handling piecewise defined functions via disjunctions (as in [34]), we simulate them via Iverson brackets. For a formula ψ over n, its Iverson bracket [ψ] : N → {0, 1} evaluates to 1 iff ψ is satisfied (i.e., [ψ] (e) = 1 if ψ[n/e] holds and [ψ] (e) = 0, otherwise). Later, Iverson brackets can be replaced by the constants 0 or 1, as we only use them for formulas ψ that are constantly false or true for large enough values of n, see Sect. 4. Poly-exponential expressions are sums of arithmetic terms over the variables x and the additional designated variable n, where it is always clear which addend determines the asymptotic growth of the expression when increasing n. This is crucial for our reducibility proof in Sect. 4. Def. 3.4 slightly generalizes the poly-exponential expressions from [19,Def. 9] by allowing arbitrary polynomials over x (instead of just linear expressions) as coefficients. In the following, for any set X ⊆ R, any k ∈ X, and ⊲ ∈ {≥, >}, let X ⊲k = {x ∈ X | x ⊲ k}.
Definition 3.4 (Poly-Exponential Expressions). Let C be the set of all finite conjunctions over {n = c, n = c | c ∈ N} where n is a designated variable and let Q S = r s r ∈ S, s ∈ S >0 be the quotient field of S. Then the set of all poly-exponential expressions with the variables x over S is . The restriction to tnn-loops ensures that for the closed form q of the update we indeed have q ∈ (PE S [ x]) d . For example, for arbitrary matrices A ∈ R d×d A , the update x ← A · x is known to admit a closed form as in Def. 3.4 with complex b j 's, whereas real numbers suffice for triangular matrices. Moreover, non-negativity is required to ensure b j > 0 (e.g., a non-tnn loop with the update x 1 ← −x 1 has the closed form x 1 · (−1) n ).

Reducing Termination of tnn-Loops to Th ∃ (S)
It is known that the bodies of tnn-loops can be linearized [47], i.e., one can reduce termination of a tnn-loop (ϕ, u) to termination of a linear-update tnnloop (ϕ ′ , u ′ ), where ϕ ′ may be non-linear. See Sect. 6 for a discussion of linearization and novel results on the linearization procedure. Moreover, [63,64] showed decidability of termination for certain classes of conjunctive linearupdate loops over R, which cover conjunctive linear-update tnn-loops. So, by combining the results of [47] and [63,64], one can conclude that termination of conjunctive tnn-loops over R is decidable.
In contrast, we now present a reduction of termination of tnn-loops to Th ∃ (S) which applies to tnn-loops over any ring Z ≤ S ≤ R and which can also handle disjunctions in the loop condition. Moreover, our reduction yields tight complexity results on termination of linear loops over Z, Q, R A , and R, and on termination of linear-update loops over R A and R (see Sect. 7.1).
Similar to [19], our reduction exploits that for tnn-loops (ϕ, u) there is a closed form q for u with q ∈ (PE S [ x]) d . However, in [19] we only considered conjunctive linear loops over Z. In contrast, we now analyze loops over S for any Z ≤ S ≤ R A and allow non-linearity and arbitrary propositional formulas in the condition. Thus, the correctness proofs differ substantially from [19].
More precisely, we show that there is a function with the following specification that is computable in polynomial time: We use the concept of eventual non-termination, i.e., the condition of the loop may be violated finitely often, see Def. 2.1. Clearly, (ϕ, u) is nonterminating iff it is eventually non-terminating [48]. The formula χ in (1) will encode the existence of a witness for eventual non-termination. By the definition of q, eventual non-termination of (ϕ, u) on S d is equivalent to validity of Example 4.1. Continuing Ex. 3.5, L ex is eventually non-terminating over Z iff there is a corresponding witness e ∈ Z 3 , i.e., iff 3 ) · n 2 + ( 4 3 · x 5 3 ) · n 3 . Let q norm be like q, but each factor [ψ] is replaced by 0 if it contains an equation and by 1, otherwise. The reason is that for large enough n, equations in ψ become false and negated equations become true. So (2) is equivalent to In this way, we obtain normalized poly-exponential expressions.
As ϕ is a propositional formula over S[ x]-inequations, ϕ( q norm ) is a propositional formula over NPE S [ x]-inequations. By (4), we need to check whether ∃ x ∈ S d . ϕ( q norm ) is valid for large enough n. To this end, we will examine the dominant terms in the inequations of ϕ( q norm ). [37]). A function g : N → R dominates a function f : N → R asymptotically (f ∈ o(g)) if for all m > 0 there is an n 0 ∈ N such that 1 |f (n)| < m · |g(n)| for all n ∈ N >n0 . Now we can state the following lemma which generalizes [19,Lemma 24].

Definition 4.3 (Asymptotic Domination
In the following, let p ≥ 0 or p > 0 occur in ϕ( q norm ). We can order the coefficients of p according to the asymptotic growth of their addends w.r.t. n.
, where α j = 0 for all 1 ≤ j ≤ ℓ. Then the marked coefficients of p are Note that p( e) ∈ NPE S for any e ∈ S d , i.e., the only variable in p( e) is n. Now the ≻ coef -maximal addend determines the asymptotic growth of p( e): o(p( e)) = o(k · n a · b n ) where k (b,a) = max ≻ coef (coefs (p( e))) .
can be reduced to validity of the closed formula ∃ x ∈ S d . red(p ⊲ 0) from Th ∃ (S). This reduction takes polynomially many steps in the size of p.
To gain intuition for the formula red(p ⊲ 0), note that by (6), we have p( e) > 0 for large enough values of n iff the coefficient of the asymptotically fastest growing addend α( e) · n a · b n that does not vanish (i.e., where α( e) = 0) is positive. Similarly, we have p( e) < 0 for large enough n iff α( e) < 0. If all addends of p vanish when instantiating x with e, then p( e) = 0. In other words, (8) holds iff there is an e ∈ S d such that unmark max ≻ coef (coefs (p( e))) ⊲ 0. The formula red(p ⊲ 0) expresses the latter in Th ∃ (S).
can be reduced to validity of the closed formula ∃ x ∈ S d . red(ξ) ∈ Th ∃ (S). This reduction takes polynomially many steps in the size of ξ.
The time needed to compute the formula (9) is polynomial in the sum of the sizes of all poly-exponential expressions in ξ. So the function (1) is computable in polynomial time w.r.t. the size of its input: q norm can clearly be computed in polynomial time from q and we can then apply Thm. 4.9 to ϕ( q norm ). Combining Cor. 3.3, (4), and Thm. 4.9 leads to the main result of this section. However, if the update contains non-linear terms, then its closed form and hence this reduction are not always computable in polynomial space (and thus, also not in polynomial time). Consider the following tnn-loop L non−pspace : The closed form for x i (i.e., the value of x i after n loop iterations) is for all n ≥ d. Thus, the closed form q 1 for x 1 contains constants like d (d d−1 ) whose logarithm grows faster than any polynomial in d. Hence, q 1 cannot be computed in polynomial space.
Instead of computing closed forms directly, one could first linearize the loop (see [47] and Sect. 6) and then compute the closed form for the resulting linearupdate loop. However, this approach cannot be computed in polynomial space either, because the linearization of L non−pspace contains the constant d (d d−1 ) as well (see Ex. 6.14 in Sect. 6). We refer to Sect. 7 for an analysis of the complexity of deciding termination for twn-loops.
Our reduction also works if S = R, i.e., termination over R is reducible to Th ∃ (R), since R and R A are elementary equivalent (i.e., a first-order formula is valid over R iff it is valid over R A , see, e.g., [2]). Thus, we get the following corollary by using that validity of closed formulas from Th ∃ (S) is decidable for S ∈ {R A , R} and semi-decidable for S ∈ {Z, Q} [13,60]. (a) The loop (ϕ, u) terminates over R A iff it terminates over R.
Our technique does not yield witnesses for non-termination, but the formula constructed by Thm. 4.9 describes the set of all witnesses for eventual nontermination. So this set can be characterized by a formula from Th ∃ (S) (i.e., it is Th ∃ (S)-definable, see Sect. 5.1), while in general the set of witnesses for non-termination cannot be characterized in this way (see [14]). Lemma 4.12. Let ξ = ϕ( q norm ). Then e ∈ S d witnesses eventual nontermination of (ϕ, u) on S d iff red(ξ)( e) holds.
In [28], we show how to compute witnesses for non-termination from witnesses for eventual non-termination of twn-loops. Thus, combining Lemma 4.12 with [28] shows that NT (ϕ, u) is recursively enumerable for twn-loops over Z ≤ S ≤ R A . Alg. 1 summarizes our technique to check termination of twn-loops.

Transformation to Triangular Weakly Non-Linear Form
In this section, we show how to handle loops that are not yet twn. To this end, we introduce a transformation of loops via polynomial automorphisms

Algorithm 1: Checking Termination
Input: a twn-loop (ϕ, u) Result: ⊤ resp. ⊥ if (non-)termination of (ϕ, u) on S d is proven, ? otherwise if (ϕ, u) is not tnn then (ϕ, u) ← (ϕ ∧ ϕ( u), u( u)) q ← closed form for u if (in)validity of ∃ x ∈ S d . red(ϕ( qnorm )) cannot be proven then return ? if ∃ x ∈ S d . red(ϕ( qnorm )) is valid then return ⊥ else return ⊤ in Sect. 5.1 and show that our transformation preserves (non-)termination (Thm. 5.10). In Sect. 5.2, we use results from algebraic geometry to show that the question whether a loop can be transformed into twn-form is reducible to validity of Th ∃ (R A )-formulas (Thm. 5.19). Moreover, we show that it is decidable whether a linear automorphism can transform a loop into a special case of the twn-form (Thm. 5.22). Finally, based on the transformation of Sect. 5.1 and 5.2 we generalize our results from Sect. 4 to certain non-twn loops in Sect. 5.3.

Transforming Loops
Clearly, the polynomials x 1 , . . . , x d are generators of the S-algebra S[ x], i.e., every polynomial from S[ x] can be obtained from x 1 , . . . , x d and the operations of the algebra (i.e., addition and multiplication). So far, we have implicitly chosen a special "representation" of the loop based on the generators x 1 , . . . , x d .
We now change this representation, i.e., we use d different polynomials which are also generators of S[ x]. Then the loop has to be modified accordingly to adapt it to this new representation. This modification does not affect the loop's termination behavior, but it may transform a non-twn-loop into twnform. This change of representation is described by S-automorphisms of S[ x].
if it is S-linear and multiplicative, i.e., for all c, c ′ ∈ S and all p, p ′ ∈ S[ x] we have η(c · p + c ′ · p ′ ) = c · η(p) + c ′ · η(p ′ ), η(1) = 1, and η(p · p ′ ) = η(p) · η(p ′ ). We denote the set of all S-endomorphisms of is free on the generators x, an endomorphism η ∈ End S (S[ x]) is uniquely determined by the images of the variables, i.e., by η(x 1 ), . . . , η(x d ). Hence, we have a one-to-one correspondence between elements of (S[ x]) d and ). In particular, every tuple u = (u 1 , . . . , u d ) ∈ (S[ x]) d corresponds to the unique endomorphism u ∈ End S (S[ x]) which is defined as follows: So for any p ∈ S[ x] we have u(p) = p( u). Thus, the update of a loop induces an endomorphism which operates on polynomials.
In other words, we have Example 5.5. We transform the loop L aut in (11) with the automorphism η from Ex. 5.2. We obtain Tr η (ϕ, u) = (ϕ ′ , u ′ ) where So the resulting transformed loop is (x 3 1 + x 2 > 0, (x 1 + x 2 2 , 2 · x 2 )). Note that while the original loop (ϕ, u) is neither triangular nor weakly non-linear, the resulting transformed loop is twn. Also note that we used a non-linear automorphism with η(x 2 ) = x 1 − x 2 2 for the transformation. While the above example shows that our transformation can indeed transform non-twn-loops into twn-loops, it remains to prove that this transformation preserves (non-)termination. Then we can use our techniques for termination analysis of twn-loops for twn-transformable-loops as well, i.e., for all loops (ϕ, u) where Tr η (ϕ, u) is twn for some automorphism η. (The question how to find such automorphisms will be addressed in Sect. 5.2.) As a first step, by Lemma 5.6, our transformation is "compatible" with the operation • of the group Aut S (S[ x]), i.e., it is an action.
The following lemma will enable us to generalize our results on witnesses for (eventual) non-termination to loops which can be transformed into twn-form.
Theorem 5.10 (Tr Preserves Termination). If η ∈ Aut S (S[ x]), then η is a bijection between the respective sets of witnesses for (eventual) non-termination, i.e., for Tr Up to now, we only transformed a loop (ϕ, u) on S d using elements of Aut S (S[ x]). To see that this is a limitation, consider a linear-update loop where u = A · x and A only has real eigenvalues. In Sect. 7.1 we will show that these loops can always be transformed into twn-form and a suitable automorphism η can be obtained by computing the Jordan normal form of A. This automorphism η is only an element of Aut S (S[ x]) if the eigenvalues of A are from S. So if S = Z, then this transformation is only applicable if all eigenvalues of A are integers.
However, we can also transform (ϕ, u) into the loop Tr η (ϕ, u) on R d A using an automorphism η ∈ Aut R A (R A [ x]). Nevertheless, our goal remains to prove termination on S d instead of R d A , which is not equivalent in general. Thus, in Sect. 5.3 we will show how to analyze termination of loops on certain subsets F of R d A . This allows us to analyze termination of (ϕ, u) on S d by checking termination of Tr η (ϕ, u) on the subset η(S d ) ⊆ R d A instead. By our definition of loops over a ring S, we have u( e) ∈ S d for all e ∈ S d , i.e., S d is u-invariant. This property is preserved by our transformation.
Our goal is to reduce termination to a Th ∃ (S, R A )-formula. Clearly, termination on F cannot be encoded with such a formula if F cannot be defined via Th ∃ (S, R A ). Thus, we require that F is Th ∃ (S, R A )-definable, i.e., that there is a ψ ∈ Th ∃ (S, R A ) with free variables x such that we have e ∈ F iff ψ( e) is valid. Then we also say that ψ defines F . An example for a Th ∃ (Z, To analyze termination of (ϕ, u) on S d , we can analyze termination of The reason is that e ∈ S d is a witness for (eventual) non-termination of (ϕ, u) iff η( e) is a witness for Tr η (ϕ, u) due to Cor. 5.8, i.e., S d contains a witness for (eventual) non-termination of (ϕ, u) iff η(S d ) contains a witness for Thm. 5.15 shows that instead of regarding solvable loops [55], w.l.o.g. we can restrict ourselves to twn-loops. The reason is that every solvable loop with real eigenvalues can be transformed into a twn-loop by a linear automorphism η, i.e., the degree deg(η) of η is 1, where deg(η) = max{deg(η(x i )) | 1 ≤ i ≤ d}.
Theorem 5.15 (twn-Transformability of Solvable Loops). Let (ϕ, u) be a solvable loop with real eigenvalues. Then one can compute a linear automorphism We recapitulate our most important results on Tr in the following corollary. Here, we generalize the result of Thm. 5.10 to the setting where we consider termination on some update-invariant and Th ∃ (S, R A )-definable set.

Finding Automorphisms to Transform Loops into twn-Form
The goal of Tr η from Sect. 5.1 is to transform (ϕ, u) into twn-form such that our techniques from Sect. 4 can be used to decide termination. So the two remaining challenges are to find a suitable automorphism η ∈ Aut R A (R A [ x]) such that Tr η (ϕ, u) is twn, and to adapt our techniques from Sect. 4 such that they can be applied to twn-loops where one only wants to show termination on an update-invariant Th ∃ (S, R A )-definable subset. We discuss the latter challenge in Sect. 5.3. In this section, we present two techniques to check the existence of automorphisms for the transformation into twn-form constructively, i.e., these techniques can also be used to compute such automorphisms.
The search for suitable automorphisms is closely related to the question if a polynomial automorphism can be conjugated into a "de Jonquiéres"automorphism, a difficult question from algebraic geometry [18]. So future advances in this field may help to improve the results of the current section.
The first technique (Thm. 5.19) reduces the search for a suitable automorphism of bounded degree to Th ∃ (R A ). For any automorphism, the degree of its inverse is bounded in terms of the length d of x.
By Thm. 5.17, checking if an endomorphism is an automorphism can be reduced to Th ∃ (R A ). To do so, one encodes the existence of coefficients for the polynomials Based on Lemma 5.18, we now present our first technique to find an automorphism η that transforms a loop into twn-form.
So if the degree of η is bounded a priori, it is decidable whether there exists ) such that Tr η (ϕ, u) is twn. By Thm. 5.19, twn-transformability is semi-decidable since one can increment δ until a suitable automorphism is found. So in other words, any loop which is transformable to a twn-loop can be transformed via Thm. 5.19.
We call our transformation Tr complete for a class of loops if every loop from this class is twn-transformable. For such classes, a suitable automorphism is computable by Thm. 5.19. Together with Thm. 5.15, we get Cor. 5.20.
Corollary 5.20. Tr is complete for solvable loops with real eigenvalues.
Note that for solvable loops (ϕ, u), instead of computing η using Thm. 5.19, the proof of Thm. 5.15 yields a more efficient way to compute a linear automorphism η such that Tr η (ϕ, u) is twn. For this, one computes the Jordan normal form of each A i (see Def. 2.3), which is possible in polynomial time (see, e.g., [23,53]).
Our second technique to find suitable automorphisms for our transformation is restricted to linear automorphisms. In this case, it is decidable whether a loop can be transformed into a twn-loop (ϕ ′ , u ′ ) where the monomial for x i has the coefficient 1 in each u ′ i . The decision procedure checks if a certain Jacobian matrix is strongly nilpotent, i.e., it is not based on a reduction to Th ∃ (R A ).
Our second technique is formulated in the following theorem which follows from an existing result in linear algebra [17, Thm. 1.6.]. and As strong nilpotence of the Jacobian matrix is clearly decidable, Thm. 5.22 gives rise to a decision procedure for the existence of a linear automorphism that transforms (ϕ, u) to the form (12).
Example 5.23. The loop L non−twn on S 3 in Fig. 3 shows how our results enlarge the class of loops where termination is reducible to Th ∃ (S, R A ). This loop is clearly not in twn-form. To transform it, we use Thm. 5.22. The Jacobian matrix J of ( One easily checks that J is strongly nilpotent. Thus, by Thm. 5.22 the loop can be transformed into twn-form by a linear automorphism. Indeed, consider η : If we transform L non−twn with η, we obtain the twn-loop L ex in Fig. 2. If S = R A , then L ex terminates on R 3 A iff L non−twn terminates on R 3 A by Thm. 5.10. Thus, as seen in Ex. 4.8, L non−twn does not terminate on R 3 A . Now assume S = Z, i.e., we analyze termination of L non−twn on Z 3 instead of R 3 A . Note that η maps Z 3 to the set of all Z-linear combinations of columns of M , i.e., We will discuss how to analyze termination of tnn-loops like L ex on updateinvariant and To summarize, if a loop is twn-transformable, then we can always find a suitable automorphism via Thm. 5.19. So whenever Thm. 5.22 is applicable, a suitable linear automorphism can also be obtained by Thm. 5.19. Hence, our first technique from Thm. 5.19 subsumes our second one from Thm. 5.22. However, while Thm. 5.19 is always applicable, Thm. 5.22 is easier to apply. The reason is that for Thm. 5.19 one has to check validity of a possibly nonlinear formula over the reals, where the degree of the occurring polynomials depends on δ and the update u of the loop, and the number of variables can be exponential in d, see Thm. 5.17 and Lemma 5.18. So even when searching for a linear automorphism, one may obtain a non-linear formula if the loop is nonlinear. In contrast, Thm. 5.22 only requires linear algebra. So it is preferable to first check whether the loop can be transformed into a twn-loop (ϕ ′ , (x 1 + p 1 , . . . , x d + p d )) with x i / ∈ V(p i ) via a linear automorphism. This check is decidable by Thm. 5.22. Note that the proof of Thm. 5.19 is constructive. Moreover, the proof of [17, Thm. 1.6.] which implies Thm. 5.22 is also constructive: the idea is to use basic results from linear algebra to compute an invertible matrix T ∈ R d×d A such that . Then η with η( x) = T · x transforms the loop into the form (12). Hence, Thm. 5.22 is also constructive. Thus, we can not only check the existence of a suitable automorphism, but we can also compute it whenever it exists.

Analyzing twn-Transformable Loops
In this section, we generalize our results from Sect. 4 to twn-transformable loops. Our transformation from Sect. 5.1 and 5.2 transforms twn-transformable loops over update-invariant and Th ∃ (S, R A )-definable sets into twn-loops over update-invariant and Th ∃ (S, R A )-definable sets. Thus, in this section we fix a twn-loop (ϕ, u) and such a set 4 we were concerned with the termination of loops on a set S d for a ring S, we now show that termination of (ϕ, u) on F can also be reduced to an existential formula (from Th ∃ (S, R A )). Here, we indeed rely on the update-invariance of F as otherwise eventual non-termination and nontermination of (ϕ, u) on F would not be equivalent. In Sect. 4, this equivalence is crucial since we reduce non-termination via eventual non-termination to Th ∃ (S, R A ).
Again, let q norm be the normalized closed form of u. Similar to (4), (ϕ, u) is eventually non-terminating on F iff In Thm. 4.9, we have seen that given a propositional formula ξ over the atoms ∈ Th ∃ (S) and the resulting formula can be computed in polynomial time from ξ. Thus, by using the formula can be reduced to validity of a closed formula in Th ∃ (S, R A ) in polynomial time.
Corollary 5.25 (Reducing Termination on Sets). Termination of tnn-or twnloops, respectively, on update-invariant Th ∃ (S, R A )-definable sets is reducible to Th ∃ (S, R A ).
In addition, we get the following refined version of Cor. 4.11.
Of course, Lemma 4.12 also holds in this setting.

Linearization of tnn-Loops
In [47], a technique was proposed to linearize polynomial loops. As mentioned, by combining the linearization with existing decidability results [63,64], one can conclude decidability of termination for conjunctive twn-loops over R (whereas Cor. 4.11 (b) extends this result also to non-conjunctive loops).
In this section, we adapt the linearization technique of [47] to our setting and formalize it. This allows us to obtain novel results on the complexity of linearization which we use to analyze the complexity of deciding termination for arbitrary twn-loops in Sect. 7.2. We start with the definition of linearization.
holds for all e ∈ S d , where u ′ ( w( e)) stands for u ′ [ y/ w( e)]. Instead of y i we often write y wi for all 1 ≤ i ≤ d ′ .
So y 1 , . . . , y d (i.e., y w1 , . . . , y w d ) correspond to the variables x 1 , . . . , x d , whereas y d+1 , . . . , y d ′ are used to mimic the non-linear part of u in a linear way in u ′ . This non-linear behavior is captured by the polynomials w d+1 , . . . , w d ′ .
, since for all e = (e 1 , e 2 , e 3 ) ∈ Z 3 we have: Here, the non-linear part of u is mimicked by the variables y x 2 2 , y x 2 3 , and y x 4 3 .
The linearization of Def. 6.1 also works when applying the update repeatedly.
Then for all e ∈ S d and all n ∈ N we have w( u n ( e)) = ( u ′ ) n ( w( e)).
We now define the linearization of a loop to be a linearization of its update where the loop guard is extended to ensure that the fresh variables y w d+1 , . . . , y w d ′ indeed correspond to w d+1 , . . . , w d ′ .
While Lemma 6.6 proves the soundness of linearization, we now show how to find u ′ and w automatically, where it suffices to only use monomials (instead of arbitrary polynomials) in w. A monomial over x has the form x z1 The original update u consists of polynomials u i to update the variable x i , for all 1 ≤ i ≤ d. The linearized update u ′ consists of polynomials u ′ m to update the variables y m for all monomials m in w. Here, for any monomial . . + c k · p k for monomials p 1 , . . . , p k and numbers c 1 , . . . , c k ∈ S, then u ′ m = c 1 · y p1 + . . . + c k · y p k . Alg. 3 summarizes the linearization procedure. The vector v always contains those monomials p for which we still have to define the linearized update u ′ p . So initially, v consists of the original variables, i.e., v = x. Whenever a new variable y p is introduced in the linearized update, p is inserted into v at the end.

Algorithm 3: Linearizing tnn-Loops
and we obtain v = (x 2 , x 3 , x 2 2 ). Next, we consider x 2 , where u 2 = x 2 3 . Thus, we obtain u ′ x2 = y x 2 3 and v = (x 3 , x 2 2 , x 2 3 ). Then we take x 3 , where u 3 = x 3 . Hence, u ′ x3 = y x3 , but we do not insert x 3 into v again, since we just computed u ′ x3 . So we have v = (x 2 2 , x 2 3 ). We now handle x 2 2 . For the linearized update, we take u 2 2 = x 4 3 but replace the monomial x 4 3 by a fresh variable y x 4 3 . Hence, u ′ = y x 4 3 and v = (x 2 3 , x 4 3 ). Next, we take the monomial x 2 3 and in u 2 3 = x 2 3 we have to replace the monomial by y x 2 3 . This leads to the linearized update u ′ . Hence, we have v = (x 4 3 ). Finally, we treat x 4 3 and in u 4 3 = x 4 3 we replace the monomial by y x 4 3 , i.e., u ′ 3 . Now v = ( ). Hence, Alg. 3 terminates and returns the loop with the guard ϕ ′ from Ex. 6.5 and the (linear) update (y x 2 2 , y x 2 3 , y x3 , y x 4 3 , y x 2 3 , y x 4 3 ) over the variables (y x1 , y x2 , y x3 , y x 2 2 , y x 2 3 , y x 4 3 ), as in Ex. 6.2. Now we infer an upper bound on Alg. 3's complexity. To this end, we will show that the degrees of the monomials in w which are used for the linearization can be bounded by the maximal dependency degree of the loop's update u. For 1 ≤ i ≤ d, the dependency degree depdeg u (x i ) is the degree of u i , but this degree is expressed in terms of those variables that are minimal w.r.t. ≻ u . Recall that u i has the form c i · x i + p i where p i is a polynomial which only contains variables that are smaller than x i w.r.t. ≻ u . W.l.o.g. we may assume that The dependency degree of p i is the maximal dependency degree of its monomials. Definition 6.8 (Dependency Degree). Let (ϕ, u) be a twn-loop with u i = c i · x i + p i for all 1 ≤ i ≤ d, where p i only contains variables that are smaller than x i w.r.t. ≻ u . We define the dependency degree w.r.t. u as follows: Since ≻ u is well founded by the triangularity of the loop, depdeg u is well defined: for the variables x i which are minimal w.r.t. ≻ u , p i is a constant and thus, depdeg u (x i ) = 1. For other variables x i with p i = 0, we can compute depdeg u (p i ) because depdeg u (x j ) is already known for all variables x j occurring in p i . Lemma 6.9 states three easy observations on depdeg. Here, deg denotes the degree of monomials or polynomials, i.e., deg(x z1 Example 6.10. Again, we consider a loop as in Ex. 6.7 with update u = (x 2 2 , x 2 3 , x 3 ). Then, depdeg u (x 3 ) = max{1, depdeg u (0)} = max{1, −∞} = 1, depdeg u (x 2 ) = depdeg u (x 2 3 ) = 2 · depdeg u (x 3 ) = 2 · 1 = 2, and depdeg u (x 1 ) = depdeg u (x 2 2 ) = 2 · depdeg u (x 2 ) = 2 · 2 = 4. So intuitively, the update of x 1 has degree 4 in terms of the ≻ u -minimal variable x 3 since the update of x 2 is quadratic in x 3 and x 2 2 then has degree 4 w.r.t. x 3 . Here, mdepdeg = 4 and for the maximal degree deg = 2 occurring in the update, we indeed have mdepdeg = deg d−1 = 2 3−1 . So the bound on mdepdeg in Lemma 6.9 (c) is tight.
As mentioned, the technique in this section is based on the linearization method of [47], where instead of tnn-loops as in Thm. 6.12, [47] works in the setting of solvable loops (Def. 2.3). But [47] has no notion like the dependency degree of Def. 6.8. Instead they only consider the degree of the polynomials in the update u. However, Ex. 6.7 shows that the polynomials in w that are used for the linearization may have a higher degree than the ones in u. Here, the polynomials in u = (x 2 2 , x 2 3 , x 3 ) only have degree 2. However, x 1 is (eventually) updated to x 4 3 . Thus, to linearize this loop, polynomials up to degree 2 do not suffice, but w must contain a polynomial of degree 4 like x 4 3 . As we showed in Thm. 6.11 (a), the dependency degree (and hence, also the degree) of the polynomials in w is bounded by mdepdeg = max{depdeg u (x i ) | 1 ≤ i ≤ d}. Indeed, in Ex. 6.7 we have depdeg u (x 1 ) = 4. Hence, our new concept of the dependency degree was needed for the upper bound on the number of iterations of the linearization algorithm in Thm. 6.11 (b). Based on this, we can now infer the asymptotic complexity of Alg. 3. As mentioned, we will need this in Sect. 7.2 to analyze the complexity of deciding termination of twn-loops.
By Thm. 6.11 (b), the while-loop of Alg. 3 is executed at most d+mdepdeg mdepdeg − 1 times. Since n k ∈ O(n k ) for any natural numbers n ≥ k, we have By Lemma 6.9 (c), we have mdepdeg ≤ deg d−1 where deg is the maximum of 1 and the highest degree of any polynomial in the update u. Hence, For the expression (d + deg d−1 ) d we have (see also App. A.23): Here, as usual, ld denotes the logarithm to the base 2. Thus, (d+deg d−1 ) d is at most exponential in d, i.e., the number of iterations of Alg. 3 is at most exponential in d. In each such iteration, one has to compute a new polynomial u z1 1 · . . . · u z d d . By Thm and the corresponding linearized update u ′ instantiates So in particular, the update contains the constant d (d d−1 ) which shows that this linearization requires exponential space if d is not bounded.

Complexity of Deciding Termination
In this section, we study the complexity of deciding termination for different classes of loops by using our results from Sect. 4 to 6. We first regard linearupdate loops in Sect. 7.1, where the update is of the form

Fig. 5: Homogeneous Linear-Update Loop
S d×d and b ∈ S d . The reason for this restriction is that such loops can always be transformed into twn-form by our transformation Tr from Sect. 5. More precisely, we show that termination of linear loops with rational spectrum is Co-NP-complete if S ∈ {Z, Q, R A } and that termination of linear-update loops with real spectrum is ∀R-complete if S = R A . Since our proof is based on a reduction to Th ∃ (S, R A ), and R A and R are elementary equivalent, our results also hold if the program variables range over R. By combining these results with our observations on the complexity of linearization from Sect. 6, we then analyze the complexity of deciding termination for arbitrary twn-loops in Sect. 7.2. In Sect. 7.3, we show that there is an important subclass of linear loops where our decision procedure for termination works efficiently, i.e., when the number of eigenvalues of the update matrix is bounded, then termination can be decided in polynomial time. Here, we again use our transformation Tr from Sect. 5.
For our complexity results, we assume the usual dense encoding of univariate polynomials, i.e., a polynomial of degree k is represented as a list of k + 1 coefficients. As discussed in [54], many problems which are considered to be efficiently solvable become intractable if polynomials are encoded sparsely (e.g., as lists of monomials where each monomial is a pair of its non-zero coefficient and its degree). With densely encoded polynomials, all common representations of algebraic numbers can be converted into each other in polynomial time [3].

Complexity of Deciding Termination for Linear-Update Loops
When analyzing linear-update loops, w.l.o.g. we can assume b = 0 since a loop of the form in Fig. 4 terminates iff the loop in Fig. 5 terminates, where x b is a fresh variable (see [30,48]). Moreover, e witnesses (eventual) non-termination for the loop in Fig. 4 iff e 1 witnesses (eventual) non-termination for the loop in Fig. 5. Note that the only eigenvalue of A b 0 T 1 whose multiplicity increases in comparison to A is 1. Thus, to decide termination of linear-update loops with rational or real spectrum, respectively, it suffices to decide termination of homogeneous loops of the form (ϕ, A · x) where A has only rational or real eigenvalues.
Such loops can always be transformed into twn-form using our transformation Tr from Sect. 5. To compute the required automorphism η, we compute the Jordan normal form Q of A together with the corresponding transformation matrix T , i.e., T is an invertible real matrix such that A = T −1 ·Q·T . Then Q is a triangular real matrix whose diagonal consists of the eigenvalues To analyze termination of the loop on S d , we have to consider termination of the transformed loop on F = η(S d ) = T · S d (see Cor. 5.16).
The Jordan normal form Q as well as the matrices T and T −1 can be computed in polynomial time [23,53]. Hence, we can decide whether all eigenvalues are rational or real numbers in polynomial time by checking the diagonal entries of Q. Thus, we obtain the following lemma. So every linear(-update) loop with real spectrum can be transformed into a linear(-update) twn-loop, i.e., the transformation Tr from Sect. 5 is complete for such linear(-update) loops. Note that Lemma 7.1 (a) yields an efficient check whether a given linear(-update) loop has rational or real spectrum.
As chaining (Def. 3.1) can clearly be done in polynomial time, w.l.o.g. we may assume that Tr η (ϕ, A· x) = (ϕ ′ , Q· x) is tnn. Next, to analyze termination of a tnn-loop, our technique of Sect. 4 (resp. Sect. 5.3) uses a closed form for the update. For tnn-loops (ϕ ′ , Q · x) where Q is a triangular matrix with nonnegative diagonal entries, a suitable (i.e., poly-exponential) closed form can be computed in polynomial time analogously to [34,Prop. 5.2]. This closed form is linear in x (we will discuss this closed form in Sect. 7.3, see Eq. (22)).
According to our approach in Sect. 5.3, we now proceed as in Alg. 2 and construct the formula ∃ x ∈ R d A . ψ F ∧ red(ϕ( q norm )) ∈ Th ∃ (S, R A ) in polynomial time due to Cor. 5.24. Hence, we get the following lemma.
Lemma 7.2. Let (ϕ, A · x) be a linear-update loop with real spectrum. Then we can compute a closed formula ψ ∈ Th ∃ (S, R A ) in polynomial time such that ψ is valid iff the loop is non-terminating. If ϕ is linear, then so is ψ.
Note that ψ is existentially quantified. Hence, if the loop has rational spectrum and coefficients, ϕ (and thus also ψ) is linear, and S ∈ {Z, Q, R A , R}, then invalidity of ψ is in Co-NP as validity of such formulas is in NP, see [49]. Thus, we get the first main result of this section. Here, we fix an inaccuracy in [20,Thm. 42], where we also allowed irrational eigenvalues and coefficients, and thus ψ may contain irrational coefficients. However, to the best of our knowledge, it is not known whether validity of linear formulas from Th ∃ (S, R A ) with irrational algebraic coefficients is in NP.

Theorem 7.3 (Co-NP-Completeness).
For linear loops (ϕ, A · x + b) with rational spectrum where ϕ ∈ Th qf (Q), A ∈ Q d×d , and b ∈ Q d , termination over Z, Q, R A , and R is Co-NP-complete.
Co-NP-hardness follows from Co-NP-hardness of unsatisfiability of propositional formulas: let ξ be a propositional formula over the variables x. Then the We now consider linear-update loops with real spectrum (and possibly nonlinear conditions) on R d A and R d . Here, non-termination is ∃R-complete. Definition 7.4 (∃R [56,57]). Let Th ∃ (R) ⊤ = {ψ ∈ Th ∃ (R) | ψ closed and valid}.
The complexity class ∃R is the closure of Th ∃ (R) ⊤ under poly-time-reductions.
We have NP ⊆ ∃R ⊆ PSPACE (see [12]). By Lemma 7.2, non-termination of linear-update loops over R A with real spectrum is in ∃R. It is also ∃R-hard since (ϕ, x) is non-terminating iff ∃ x ∈ R d A . ϕ is valid. So non-termination is ∃R-complete, i.e., termination is Co-∃R-complete (where Co-∃R = ∀R [57]).
Theorem 7.5 (∀R-Completeness). Termination of linear-update loops with real spectrum over R A and R is ∀R-complete.

Complexity of Deciding Termination for twn-Loops over S ∈ {R A , R}
By Cor. 4.11 (b) termination is decidable for arbitrary twn-loops over R A and R. So in this section, we discuss the complexity of this decision problem. First of all, deciding termination of arbitrary twn-loops over R A and R is ∀R-hard since termination of linear-update loops with real spectrum over R A and R is ∀R-hard by Thm. 7.5 and any such linear-update loop can be transformed into a twn-loop in polynomial time by Lemma 7.1 (b). Thus, we have a lower bound on the complexity of deciding termination for arbitrary twn-loops.
To find an upper asymptotic bound for deciding termination of twn-loops, first note that we can restrict ourselves to tnn-loops again, as any twn-loop can be transformed into a tnn-loop by chaining (Def. 3.1) in polynomial time.
The next step of our decision procedure in Alg. 1 is to compute closed forms for the update. However, the loop L non−pspace in (10) showed that in general, the computation of closed forms cannot be done in polynomial space. On the other hand, as mentioned in Sect. 7.1, for linear-update loops, closed forms can be computed in polynomial time. To benefit from this upper bound, we therefore do not proceed directly as in Alg. 1, but instead we first linearize the tnn-loop. While linearization cannot be computed in polynomial space either (see Ex. 6.14), in Sect. 6 we formalized and analyzed the complexity of the linearization technique from [47]. Given a tnn-loop (ϕ, u) we can compute a linear-update tnn-loop (ϕ ′ , u ′ ) such that (ϕ, u) terminates if and only if (ϕ ′ , u ′ ) does (Thm. 6.12). Clearly, linear-update tnn-loops over R A and R always have real spectrum as the eigenvalues of a triangular matrix are its diagonal entries. So by using this linearization, we can give an upper complexity bound for deciding termination of arbitrary twn-loops.
As linearization can be computed in double exponential time and thus, also in double exponential space, and termination of linear-update loops is in ∀R ⊆ PSPACE ⊆ EXPTIME by Lemma 7.2 (where the size of the linear-update loop may be at most doubly exponential), we obtain that deciding termination of twn-loops is in 3-EXPTIME (i.e., it is between ∀R and 3-EXPTIME). Moreover, if the number of variables is bounded, checking validity of a formula in Th ∃ (R A ) is in P (see [2]). In this case, combining linearization which can be computed in exponential time and thus, also in exponential space when d is bounded (Lemma 6.13 (b)), and deciding termination of linear-update loops which is polynomial in this case (where the size of the linear-update loop may be at most exponential), we obtain Thm. 7.6 (b). Theorem 7.6 (Membership in 3-EXPTIME). Let D ∈ N be fixed. Termination of twn-loops over R A and R (a) is in 3-EXPTIME. (b) is in EXPTIME if the number of variables d is at most D.

Complexity of Deciding Termination for Uniform Loops
In Sect. 7.1, we showed that termination of linear loops with rational spectrum is Co-NP-complete. For proving Co-NP-hardness, we used the trivial update x ← x induced by the identity matrix. Therefore, the question arises whether imposing suitable restrictions to the update matrix (which exclude the identity matrix) leads to a "more efficient" decision procedure for termination (assuming P = NP). We now analyze a special case of linear loops (so-called uniform loops) and show that for these loops deciding termination is polynomial, if one fixes the number of eigenvalues of the update matrix.
In Sect. 7.3.1, we introduce uniform loops and parameterized decision problems, and state the main result of Sect. 7.3 (Thm. 7.10). To prove it, we show that for uniform loops, instantiating the variables in the loop guard by q norm (as required by our decision procedure from Sect. 4) results in formulas of a special structure (so-called interval conditions, see Sect. 7.3.2 and 7.3.3). Validity of these formulas can be checked in polynomial time (Sect. 7.3.4) which proves Thm. 7.10 for uniform loops over Q, R A , and R. In Sect. 7.3.5 we show that our result holds for uniform loops over Z as well.

Uniform Loops and the Parameterized Complexity Class XP
Definition 7.7 (Uniform Loop). A linear loop (ϕ, A · x) over S ∈ {Z, Q, R A } is uniform if each eigenvalue λ of A is a non-negative number from S whose eigenspace w.r.t. A is one-dimensional, i.e., λ has geometric multiplicity 1.
The latter property is equivalent to requiring that there is exactly one Jordan block for each eigenvalue in A's Jordan normal form. To grasp uniform loops intuitively, consider triangular linear loops with updates x i ← λ · x i + p i for all 1 ≤ i ≤ d, where the factor λ ≥ 0 is the same for all i. These loops are uniform iff the relation ≻ u is total (or equivalently, iff the variables can be ordered such that the super-diagonal of A does not contain zeros).  · x) where all diagonal entries are identical and non-negative is uniform iff ≻ u is a total ordering.
Thus, loops like the leading example from [7] in Fig. 6 which is equivalent to Fig. 7 are uniform. In contrast, a loop is not uniform if each x i is updated to λ·x i + c i for constants c i ∈ S. The reason is that the x i do not occur in each other's updates. Hence, we have x i ≻ u x j and x j ≻ u x i for all 1 ≤ i, j ≤ d.
So in particular, a uniform loop cannot have more than one update of the form x i ← x i . However, the loop condition can still be an arbitrary Boolean formula over linear inequations. Thus, our complexity result is quite surprising since it shows that for this class of loops, termination is easier to decide than satisfiability of the condition (e.g., unsatisfiability of linear formulas over R A is Co-NP-complete). Intuitively, the reason is that our class prohibits multiple updates like x i ← x i where variables "stabilize" and where termination is essentially equivalent to unsatisfiability of the condition.
To give an intuition how hard the restriction to uniform loops is, we analyzed the TPDB [62] used at the Termination and Complexity Competition [25]. In the category for "Termination of Integer Transition Systems (ITSs)" we identified 467 polynomial loops with non-constant guard (i.e., termination is not trivial) and 290 (62 %) of them are uniform loops over Z. Similarly, in the category for "Complexity Analysis of ITSs" we found 1,258 such polynomial loops and 452 (36 %) are uniform. In fact, in practice one is often interested in termination of triangular loops where after chaining, all variables belong to the eigenvalues 0 or 1. The reason is that termination is usually easy to show if there are eigenvalues greater than 1, because they lead to exponential growth. Thus, if the loop terminates, termination is usually reached after few steps (e.g., consider a loop (x ≤ c, 2 · x) for any constant c). Hence, the number of eigenvalues k is usually smaller than the number d of program variables. In this section we show that the complexity for deciding termination of uniform loops is exponential in k but not in d. More precisely, termination of uniform loops is in the parameterized complexity class XP, where the parameter is the number k of eigenvalues. A parameterized problem L is slicewise polynomial if the time needed for deciding the question "(x, k) ∈ L?" is in O(|x| f (k) ) where f is a computable function depending only on k. The corresponding complexity class is called XP.
In the remainder of this section, we prove that for any fixed k ∈ N, termination of uniform loops with k eigenvalues is decidable in polynomial time. Fig. 9: Multiplication of Jordan Block Theorem 7.10 (Parameterized Complexity of k-Termination). We define the parameterized decision problem k-termination as follows: ((ϕ, A · x), k) ∈ L k-termination iff the loop (ϕ, A · x) terminates over S and A has k eigenvalues. For uniform loops, k-termination is in XP. Moreover, for such loops, ktermination over R is in XP as well.

Hierarchical Expressions and Partitions
We now elaborate on the closed forms arising from uniform loops. To this end, we fix a uniform loop (ϕ, A · x). Let spec(A) = {λ 1 , . . . , λ k } be A's eigenvalues where 0 ≤ λ 1 < . . . < λ k , let Q be A's Jordan normal form where the Jordan blocks are ordered such that the numbers on the diagonal are weakly monotonically increasing, and let T be the corresponding transformation matrix, i.e., A = T −1 · Q · T . Moreover, let η be the automorphism defined by η( x) = T · x and let Tr η (ϕ, Instead of termination of the original loop on S d , we now have to prove termination of the transformed loop on η(S d ) = T · S d . For S ∈ {Q, R A }, if the eigenvalues of A are from S, then the transformation matrix T is an invertible matrix over S. Therefore, we obtain T · S d = S d . Hence, we now have to analyze termination of (ϕ ′ , Q · x) over S. In contrast, if S = Z, then even if the eigenvalues of A are integers, the transformation matrix T or its inverse may contain non-integer rational numbers. Thus, we first regard uniform loops over S ∈ {Q, R A } and discuss the case S = Z in Sect. 7.3.5.
By uniformity, Q = diag(Q λ1 , . . . , Q λ k ) has k Jordan blocks Q λ1 , . . . , Q λ k where Q λ is as in Fig. 8. For each eigenvalue λ, let ν(λ) be the dimension of Q λ . Since each eigenvalue has geometric multiplicity 1, ν(λ) is the algebraic multiplicity of λ, i.e., the multiplicity as a root of the characteristic polynomial of A. For ν = ν(λ), Fig. 9 shows the form of Q n λ as in, e.g., [34,48], where n s = 0 if n < s. This directly yields a closed form q for the n-fold application of the update Q · x of the transformed loop. Since our approach from Sect. 4 works by analyzing eventual non-termination, we are only interested in validity of formulas for large enough n. Thus, we may assume that n is larger than the algebraic multiplicities ν(λ) of all eigenvalues λ ∈ spec(A). Then one obtains a resulting normalized closed form q norm which consists of normalized polyexponential expressions of a special form, so-called hierarchical expressions.
Here, for any α ∈ Q S [ x], deg xi (α) is the highest power of x i occurring in a monomial of α, i.e., it is the degree of α when interpreting all variables besides x i as constants.
For q norm in Fig. 10, h = q 4 = x 4 ·2 n + x5 2 ·n·2 n is a hierarchical expression over the indices i 1 = 3, i 2 = 4, i 3 = 5 with offset 2, order 3, and base 2, as h = 3 s=2 α s · n s−2 · 2 n for α 2 = x 4 ∈ R A [x 4 , x 5 ] and α 3 = x5 2 ∈ R A [x 5 ]. To describe the form of the whole vector q norm , we now introduce hierarchical partitions. To this end, similar to the concept of solvable loops in Def. 2.3, we consider a partitioning of {1, . . . , d}. The hierarchical expressions h 1 , . . . , h d are a hierarchical k-partition via ν 1 , . . . , ν k with bases 0 < λ 1 < . . . < λ k from S if for all 1 ≤ i ≤ k: (a) h j is a hierarchical expression over the indices B i for all j ∈ B i , (b) base (h j ) = λ i for all j ∈ B i , (c) h j has order ν i for all j ∈ B i , (d) off(h min(Bi) ) = 1, and (e) if j, j + 1 ∈ B i , then off(h j ) + 1 = off(h j+1 ).
Indeed, when transforming the update of a uniform loop to Jordan normal form, then the normalized closed form is always a hierarchical partition.
Corollary 7.15 ( q norm is Hierarchical Partition). Let A be the update matrix of a uniform loop with eigenvalues 0 < λ 1 < . . . < λ k and algebraic multiplicities ν 1 , . . . , ν k , and let Q be its Jordan normal form where the numbers on the diagonal are weakly monotonically increasing. Then the normalized closed form q norm of the update Q is a hierarchical k-partition via ν 1 , . . . , ν k and bases λ 1 < . . . < λ k . Here the i-th block is

Interval Conditions
Our decision procedure in Sect. 4 instantiates the variables in the polynomials f of the loop guard by q norm = (h 1 , . . . , h d ), resulting in poly-exponential expressions p = f (h 1 , . . . , h d ). We now prove that in the setting of a hierarchical k-partition q norm , the atoms in red(p ⊲ 0) (see Lemma 4.7 and (7)) are equivalent to so-called interval conditions, whose satisfiability is particularly easy to check (Sect. 7.3.4). In Lemma 7.19, Lemma 7.21, and Cor. 7.23, we introduce the different subformulas occurring in red(p⊲0). Here, it is convenient to define the active variables of polynomials. From the addends α j · n aj · b n j of p = f (h 1 , . . . , h d ) with α j ∈ Q S [ x] lin , a j ∈ N, and b j ∈ S >0 , we again compute the set coefs(p) of marked coefficients as in Def. 4.5, which have the form α (bj ,aj ) j . For any b ∈ S >0 and a ∈ N, we now define a formula zero(b, a) which is equivalent to requiring that all addends α j · n aj · b n j vanish where b j = b and where a j ≥ a. Lemma 7.19 (Formulas for Vanishing of Addends). Let h 1 , . . . , h d be a hierarchical k-partition via ν 1 , . . . , ν k with bases 0 < λ 1 < . . . < λ k , and let For any b ∈ S >0 and a ∈ N, we define zero(b, a): (a) If λ i = 1 for all 1 ≤ i ≤ k or λ i = 1 for some 1 ≤ i ≤ k and F (i) = ∅, then zero(1, 0) is the formula f (0, . . . , 0) = 0. (b) If λ i = 1 for some 1 ≤ i ≤ k and F (i) = ∅, then zero(1, 0) is the formula ∅, and (b, a) = (1, 0), then zero(b, a) Then zero(b, a) is equivalent to the requirement that α s = 0 holds for all α (bs,as) s ∈ coefs(p) with b s = b and a s ≥ a.
In the notation of Lemma 7.19, since So v ∈ R 5 A satisfies zero(1, 0) or zero(2, 0), respectively, iff all terms with base After introducing zero(b, a), we are now ready to show that the formulas in Lemma 4.7 and Thm. 4.9 have a special form when considering uniform loops. In the following lemmas, corollaries, and definitions, let h 1 , . . . , h d , ν 1 , . . . , ν k , f , F (i), p, coefs (p), and ℓ always be as in Lemma 7.19.
The formulas ρ f,s in Lemma 7.21 and Cor. 7.23 are so-called interval conditions.

Checking Satisfiability of Interval Conditions
We now show that to decide satisfiability of the formulas ic(p⊲ 0), we only have to regard instantiations of the variables with values from {0, 1, -1, ⋆}, where ⋆ stands for one additional non-zero value. There are only polynomially many such instantiations and the particular value for ⋆ is later determined by SMT solving. This SMT solving only takes polynomial time, because the resulting SMT problem only contains a single variable. Def. 7.29 instantiates variables accordingly and performs Boolean simplifications as much as possible. For example, if ρ is the formula (x 1 − 5 2 > 0) ∧ (x 2 = 0) and v = (⋆, 0), then ρ( v)↓ is ⋆ − 5 2 > 0. Hence, v ? ρ. So in general, v ? ρ means that ρ( v)↓ = true or that there could be a value w for ⋆ such that ρ[ x/ v, ⋆ /w]↓ = true. Now we define candidate assignments cndAssg(ρ f,s ) for the formulas ρ f,s in Lemma 7.21 and Cor. 7.23 which contain all v ∈ {0, 1, -1, ⋆} d that may satisfy ρ f,s (if a suitable value for ⋆ is found). However, for each Block B i , at most one variable x j with j ∈ B i may be assigned a non-zero value (i.e., 1, -1, or ⋆). Moreover, the value ⋆ may only be used in the block for the eigenvalue λ i = 1.
Definition 7.30 (Sets of Candidate Assignments). For all 0 ≤ s ≤ ℓ, we define: Example 7.31. In Ex. 7.22 and 7.24, for ρ f,4 = (x 4 > 0)∧(x 5 = 0), v ? ρ f,4 implies v 4 = 1 and v 5 = 0. Here, v 4 = ⋆ is not possible, because 4 does not belong to the block B 1 = {1, 2} for the eigenvalue 1. Since at most one value for each block may be non-zero, we have v 3 = 0. In contrast, v 1 and v 2 can be arbitrary, but at most one of them may be non-zero. Hence, we obtain the following for ρ f, 4 and for ρ f,0 = ( Lemma 7.32 is crucial for our algorithm to decide k-termination for uniform loops: |cndAssg(ρ f,s )| is bounded by a polynomial in d if k is assumed to be a parameter. This is because the ν i form a k-partition of d, i.e., ν i ≤ d for 1 ≤ i ≤ k. Hence, computing cndAssg(ρ f,s ) can be done in polynomial time for fixed k.

Then we have
For a uniform loop with condition ϕ and normalized closed form q norm = h, let ϕ( h) contain the atoms f ( h) ⊲ 0, where f ∈ S[ x] lin . To decide termination, our algorithm computes cndAssg(ic(f ( h) ⊲ 0)) for all these atoms f ( h) ⊲ 0, and then checks for each of the candidate assignments whether it is a witness for eventual non-termination. We first lift ic and cndAssg to linear formulas.
To analyze termination of uniform loops, we now present an algorithm to decide whether for a hierarchical k-partition h and a linear formula ϕ, ∃ x ∈ S d , n 0 ∈ N. ∀n ∈ N >n0 . ϕ( h) (see (9)) is valid. Our algorithm calls a method SMT(ψ,V,S) which checks whether the linear formula ψ in the variables V is satisfiable. Here, the variables V range over S ∈ {Z, Q, R A } and the coefficients of the polynomials are from Q S . (So for S = Z, one can first multiply all inequations in ψ by the main denominator to result in coefficients from Z.) In our case, V = {⋆} and thus, |V| = 1. With these restrictions, the method SMT has polynomial runtime (see [2,40]). More precisely, SMT is called in Alg. 4 to determine whether ⋆ can be assigned a non-zero value such that ψ( v)↓ is satisfiable. Here, we have to assign all occurrences of ⋆ in the formula ψ( v)↓ the same value.

Algorithm 4: Checking Interval Conditions
Input: a formula ϕ over the atoms Let us discuss the complexity of Alg. 4. The formula ic(ϕ( h)) and each element of cndAssg(ic(ϕ( h))) can be computed in polynomial time. By Cor. 7.34, cndAssg(ic(ϕ( h))) has at most |ϕ| · (d + 2) · (3 · max{ν i | 1 ≤ i ≤ k} + 1) k elements, where |ϕ| is the number of atoms in ϕ and ν i ≤ d for all 1 ≤ i ≤ k. Thus, when considering k to be a parameter, cndAssg(ic(ϕ( h))) can be computed in polynomial time. Moreover, evaluating a formula w.r.t. v according to Def. 7.29 is possible in polynomial time, too. Finally, SMT has polynomial runtime as discussed before. So the runtime of the algorithm is polynomial when regarding k as a parameter. We now prove that Alg. 4 is sound and complete.
So for a uniform loop over S ∈ {Q, R A }, non-termination is equivalent to validity of ∃ x ∈ S d . ∀n ∈ N >n0 . ϕ( q norm ), which in turn is equivalent to a formula only containing interval conditions. This insight reduces the search space for proving validity drastically. Thus, we can now prove Thm. 7.10 for S ∈ {Q, R A , R}. As the computation of the equivalent interval conditions in Cor. 7.25 clearly works in polynomial time and we have discussed that Alg. 4 runs in polynomial time when k is assumed to be a parameter, this proves the statement. Finally, these loops terminate over R A iff they terminate over R by Cor. 4.11.
For Thm. 7.10, it was crucial to transform the loop such that the update matrix is in Jordan normal form. Here we relied on a special closed form for the Jordan normal form, while in Sect. 7.1 we only used the transformation to argue why the closed form is computable in polynomial time. Thus, the transformation from Sect. 5 does not only generalize our results from Sect. 4 to a wider class of loops but it also gives rise to novel results like Thm. 7.10.
The approach in the proof of Thm. 7.10 also works for uniform loops over Z if the update matrix is already in Jordan normal form. But otherwise, in addition to ϕ( q norm ) we also have an update-invariant and Th ∃ (S, R A )definable subset F which stems from the transformation into Jordan normal form (see Sect. 5). Thus, to decide termination we have to decide validity of ∃ x ∈ R d A . ∀n ∈ N >n0 . ϕ( q norm ) ∧ ψ F . We will discuss this in the next section.

Termination of Uniform Loops Over the Integers
Now we show that deciding termination of uniform loops (ϕ, A · x) over the integers is also in XP. Let A ∈ Z d×d with k integer eigenvalues each of geometric multiplicity one. Then there is a matrix T ∈ Q d×d such that A = T −1 · Q · T for a matrix Q in Jordan normal form. However, in general we do not have T, T −1 ∈ Z d×d (see, e.g., [58]). As before, let η( x) = T · x and ϕ ′ = η −1 (ϕ). Then termination of (ϕ, A · x) on Z d is equivalent to termination of (ϕ ′ , Q · x) on η(Z d ) = T · Z d by Cor. 5.16. Here, L T = T · Z d is the set of all integer linear combinations of T 's columns, i.e., their lattice. In general, we have L T = Z d . Hence, we now call Alg. 4 with the input (ϕ ′ , h, Q), where h = q norm is the normalized closed form for the update Q · x. For ψ = ic(ϕ ′ ( h)), we want to find out if ψ( v) holds for some v ∈ L T . Such a v ∈ L T would witness eventual non-termination of (ϕ ′ , Q · x), and since L T is update-invariant under Q · x by Lemma 5.12, this is equivalent to non-termination of (ϕ ′ , Q · x) on L T .
We modify Alg. 4 such that it computes all v ∈ cndAssg(ψ) where ψ( v)↓ ∧ ⋆ = 0 is satisfied by some v ′ that results from v by instantiating ⋆ with a suitable number from Q. As shown in Cor. 7.34, for a fixed number of eigenvalues k, there are only polynomially many such candidate assignments v. Note that the formula ϕ ′ is only built from the connectives ∧ and ∨, and ψ results from ϕ ′ by replacing each atom f ⊲ 0 by ic(f ( h) ⊲ 0). Hence, for every such v there is a subset {ic(f 1 ( h) ⊲ 1 0), . . . , ic(f e ( h) ⊲ e 0)} of these formulas such that v ′ satisfies them all and such that satisfying these formulas is sufficient for satisfying ψ. For each 1 ≤ r ≤ e, let ℓ r = | coefs(f r ( h))|. By Cor. 7.25, ic(f r ( h) ⊲ r 0) has the form ℓr s=1 ρ fr ,s or ℓr s=0 ρ fr ,s . So for every r there is at least one s where ρ fr ,s ( v ′ ) is true. But due to the construction of ρ fr ,s in Lemma 7.21 and Cor. 7.23, there is at most one 0 ≤ s ≤ ℓ r where ρ fr ,s ( v ′ ) is true. Thus, for every 1 ≤ r ≤ e, there is a unique 0 ≤ s r ≤ ℓ r where ρ fr,sr ( v ′ ) is true. By Lemma 7.21 and Cor. 7.23, all ρ fr ,sr are interval conditions. Thus, for each entry v j of v ′ we can find out whether x j = v j is required by some ρ fr,sr , or whether v j = 0 is just due to setting variables to zero by default, i.e., the formula would still hold when assigning an arbitrary value from Q to v j . So every v ∈ cndAssg(ψ) gives rise to a certain set of formulas {ρ f1,s1 , . . . , ρ fe,se }, which in turn results in a certain abstract assignment that indicates for each entry of v ′ whether its actual value is necessary to be a model. For each of the obtained abstract assignments, we now have to check whether it is satisfied by some value from L T . Let N ⊆ {1, . . . , d} be those indices where j ∈ N iff the j-th component of the abstract assignment is [0, 0], i.e., iff the j-th component must be 0 in order to satisfy all ρ fs,ms . Then we compute a basis of the sublattice L N = { w ∈ L T | w j = 0 for j ∈ N }. To this end, we solve the system of linear equations j∈N (T · x) j = 0 where x ∈ Z d . Here as usual, (T · x) j denotes the j-th component of the vector T · x. This problem can be solved in polynomial time (see, e.g., [22,59]). Since in general this system contains more variables than equations, the solutions yield a certain linear dependence between the variables. This dependence can then be used to reduce the number of variables in the system, i.e., it gives rise to a basis of L N , where each basis vector is represented by a Z-linear combination of the columns of T . Let d ′ ≤ d be the rank of the sublattice L N (i.e., the number of its basis vectors) and let P ∈ Q d×d ′ be the matrix whose columns form the basis of L N .
Let N ′ ⊆ {1, . . . , d} be those indices where j ∈ N ′ iff the j-th component of the abstract assignment is neither [0, 0] nor (−∞, ∞). So the j-th component must be from a certain interval in order to satisfy all ρ fs,ms . To ease notation, we define I j = (−∞, ∞) if j / ∈ N ′ and let K = d j=1 I j ⊆ Q d . Now we have to decide whether there exists an X ∈ Z d ′ such that P · X ∈ K.
Let B 1 , . . . , B k again be the blocks from the k-partition h 1 , . . . , h d . Note that if there is a block B i where some ρ fr,sr requires x j with j ∈ B i to be non-zero (i.e., j ∈ N ′ ), then ρ fr,sr requires all x j ′ with j ′ > j and j ′ ∈ B i to be zero (i.e., j ′ ∈ N ). Thus, since v ′ satisfies all formulas ρ fr ,sr for 1 ≤ r ≤ e, for each block B i there can be at most one r ∈ B i where some ρ fr ,sr requires x j to be non-zero. Hence, for each block B i there is at most one j ∈ B i where j ∈ N ′ . Note that containment in an interval can be described by at most 2 inequations, where the strict inequations can be turned into weak ones since the variables only range over the integers. Thus, to describe the required containment in the intervals for all x j with j ∈ N ′ , we need at most 2 · k inequations. In other words, the requirement P · X ∈ K can be described by 2 · k linear inequations where the coefficients are from Q. Since linear integer programming with rational coefficients and a fixed number of constraints is possible in polynomial time (see [40]), this shows that checking whether a candidate assignment v gives rise to a solution in L T can be done in XP. As |cndAssg(ψ)| is also polynomial for fixed k, k-termination of uniform loops over Z is in XP as well.

Conclusion and Related Work
In this work, we studied termination of twn-loops, i.e., loops where the update x ← u is a triangular system of polynomial equations and the use of non-linearity in u is mildly restricted. We first presented a reduction from termination of twn-loops to Th ∃ (S) in Sect. 3 and 4. This implies decidability of termination over S ∈ {R A , R} and semi-decidability of non-termination over S ∈ {Z, Q}.
In addition, we showed how to transform certain non-twn-loops into twnform in Sect. 5, and discussed how this generalizes our results to a wider class of loops. We also showed that twn-transformability is semi-decidable.
Afterwards, we analyzed the complexity of deciding termination for different subclasses of twn-loops. In Sect. 6, we first showed that linearizing twnloops can be done in double exponential time. In Sect. 7, we used our transformation and decision procedure to prove Co-NP-completeness (∀R-completeness) of termination of linear (linear-update) loops with rational (real) spectrum, and based on linearization, that deciding termination of arbitrary twn-loops over R A or R is in 3-EXPTIME.
Finally, we showed that for the subclass of uniform loops over S ∈ {Z, Q, R A , R}, termination can be decided in polynomial time, if the number of eigenval-ues of the update matrix is fixed. So here our decision procedure can be used as an efficient technique for termination analysis.

Related Work
In contrast to automated termination analysis (see e.g., [1,6,7,9,11,24,25,38,39,50]), we investigated decidability of termination for certain classes of loops in Sect. 4. As termination is undecidable in general, decidability results can only be obtained for very restricted classes of programs.
Nevertheless, many techniques used in automated tools for termination analysis (e.g., ranking functions [1,5,6,7,9,50]) focus on similar classes of loops, since such loops occur as sub-programs in (abstractions of) real programs. Tools based on these techniques have turned out to be very successful, also for larger classes of programs. Thus, these tools could benefit from integrating our (semi-)decision procedures and applying them instead of incomplete techniques for any sub-program that can be transformed into a twn-loop.
Related work on decidability of termination also considers similar (and often more restricted) classes of loops. For linear conjunctive loops, termination over R [41,44,61,65] and Q [10] is decidable. Decidability of termination of linear conjunctive loops over Z was conjectured to be decidable in [61]. After several partial results [8,19,48] this conjecture was confirmed recently in [30]. However, [4] shows that for slight generalizations of linear conjunctive loops over Z, where a non-deterministic update or a single piecewise update of a variable are allowed, termination is undecidable. Tiwari [61] uses the special case of our twn-transformation from Sect. 5 where the loop and the automorphism are linear. In contrast to these results, our approach applies to non-linear loops with arbitrary conditions over various rings.
Linearization is another attempt to handle non-linearity, see Sect. 6. While the update of solvable loops can be linearized [47], the condition cannot. Otherwise, one could linearize any loop (p = 0, x), which terminates over Z iff p has no integer root. By [30], this would imply decidability of Hilbert's Tenth Problem.
In the non-linear case, [42] proves decidability of termination for conjunctive loops on R d for the case that the condition defines a compact and connected subset of R d . In [64], decidability of termination of conjunctive linearupdate loops on R d with the non-zero minimum property is shown, which covers conjunctive linear-update loops with real spectrum. For general conjunctive linear-update loops on R d undecidability is conjectured. Moreover, [63] shows that termination of conjunctive linear-update loops where the update matrix has only periodic real eigenvalues is decidable, which also covers conjunctive linear-update loops with real spectrum. Here, a special case of our transformation from Sect. 5 with linear automorphisms is used. In combination with [47], [63,64] both yield a decision procedure for termination of conjunctive twn-loops over R. Furthermore, [46] proves that termination of (not necessarily conjunctive) linear-update loops is decidable if the condition describes a compact set. Finally, [66] gives sufficient criteria for (non-)termination of solvable loops and [43] presents sufficient conditions under which termination of non-deterministic non-linear loops on R d can be reduced to satisfiability of a semi-algebraic system.
For linear-update loops with real spectrum over R, we prove ∀R-completeness of termination, whereas [63,64] do not give tight complexity results. The approach from [66] is incomplete, whereas we present a complete reduction from termination to the respective existential fragment of the first-order theory. The work in [43] is orthogonal to ours as it only applies to loops that satisfy certain non-trivial conditions. Moreover, we consider loops with arbitrary conditions over various rings, while [42,43,63,64] only consider conjunctive loops over R and [46] only considers loops over R where the condition defines a compact set.
Regarding complexity, [48] proves that termination of conjunctive linear loops over Z with update [5] it is shown that existence of a linear (lexicographic) ranking function for linear conjunctive loops over Q or Z is Co-NP-complete.
Our Co-NP-completeness result is orthogonal to those results as we allow disjunctions in the condition. Moreover, Co-NP-completeness also holds for termination over Z, while [10,61] only consider termination over Q resp. R. Additionally, we showed that k-termination of uniform loops over Z, Q, R A , and R is in XP, where the parameter k is the number of eigenvalues. This result is also orthogonal to [10,61] since we again allow disjunctions in the condition. Furthermore, existence of a linear (lexicographic) ranking function is not necessary for termination of linear loops. We refer to [29] for further discussion on possible extensions of our results to uniform loops over S, where however the eigenvalues are not from S.
Several works exploit the existence of closed forms for solvable (or similar classes of) loops, e.g., to analyze termination on a given input, to infer runtime bounds, or to reason about invariants [28,31,34,35,47,55]. While our approach covers solvable loops with real eigenvalues (by Cor. 5.20), it also applies to loops which are not solvable, see Ex. 5.23. Our transformation of Sect. 5 may also be of interest for other techniques for solvable or other subclasses of polynomial loops, as it may be used to extend the applicability of such approaches.

Hence, (8) is equivalent to
The time needed to compute and sort coefs (p) is polynomial. Furthermore, red(p ⊲ 0) is a disjunction of at most ℓ + 1 subformulas, where each subformula is a conjunction of at most ℓ (in-)equations over coefs (p). Thus, the time needed to compute red(p ⊲ 0) resp. (16) is polynomial in the size of p.
A.6 Proof of Thm. 4.9 Proof. We have to prove (9) ⇐⇒ ∃ x ∈ S d . red(ξ) (17) where red(ξ) results from replacing each atom p ⊲ 0 in ξ by red(p ⊲ 0). Since each red(p ⊲ 0) can be computed in polynomial time due to Lemma 4.7, the computation of the formula "∃ x ∈ S d . red(ξ)" clearly works in polynomial time, too. To prove (17), we introduce the notion of a fundamental set. Let p 1 ⊲ 1 0, . . . , p k ⊲ k 0 denote the atoms in ξ. We call a subset I ⊆ {1, . . . , k} fundamental if i∈I p i ⊲ i 0 =⇒ ξ. Recall that w.l.o.g., we can assume that ξ does not contain any connectives except ∧ and ∨. Thus, whenever ξ = false, the formula ξ must have fundamental sets. Clearly, we have Thus, to prove (17), it suffices to show the following: For the " ⇐= "-direction, assume there is such a fundamental set and e ∈ S d , i.e., is valid. Then as in the proof of Lemma 4.7, for each i ∈ I, there is an n i ∈ N such that ∃ x ∈ S d . ∀n ∈ N>n i . p i ⊲ i 0.
As I is finite, nmax = max {n i | i ∈ I} exists. Hence, we get Since I is fundamental, this implies (9). For the " =⇒ "-direction, assume (9). Then there is an e ∈ S d and an n 0 ∈ N such that for each n ∈ N>n 0 , there is a fundamental set In such that i∈In p i ( e) ⊲ i 0 holds. As there are only finitely many fundamental sets, there is some fundamental set I that occurs infinitely often in (In) n∈N>n 0 . Hence we get ∃n 0 ∈ N. ∃ ∞ n ∈ N>n 0 . i∈I p i ( e) ⊲ i 0.
By definition of poly-exponential expressions, each p i ( e) is weakly monotonic in n for large enough n. Thus, (19) implies 5 ∃n 0 ∈ N. ∀n ∈ N>n 0 . i∈I p i ( e) ⊲ i 0.
As e ∈ S d , there is a fundamental set I such that ∃ x ∈ S d . i∈I red(p i ⊲ i 0) holds. 5 This corresponds to the observation that if the loop condition is a disjunction (and hence also ξ is a disjunction of the form ξ 1 ∨ ξ 2 ), then non-termination of the original loop implies non-termination of one of the loops where instead of the disjunction one only takes one of the disjuncts as the loop guard. The reason is that for every fundamental set I and every e ∈ S d , i∈I p i ( e) ⊲ i 0 =⇒ (ξ 1 ( e) ∨ ξ 2 ( e)) implies i∈I p i ( e) ⊲ i 0 =⇒ ξ 1 ( e) or i∈I p i ( e) ⊲ i 0 =⇒ ξ 2 ( e). The restriction to tnn-loops implies that the closed forms are poly-exponential expressions and hence, that the p i ( e) are weakly monotonic in n for large enough n. Therefore, the above argumentation in the proof shows that there is a fundamental set I such that i∈I p i ( e) ⊲ i 0 holds for all large enough n and thus, for some j ∈ {1, 2}, ξ j ( e) holds for all large enough n as well. Hence, the loop would also be non-terminating if one only takes the corresponding disjunct as the loop guard.
A.8 Proof of Cor. 4.11 Proof. Again, by Cor. 3.3, termination of twn-loops is reducible to termination of tnnloops. By Thm. 4.10, termination of tnn-loops is reducible to invalidity of a closed formula χ ∈ Th ∃ (S). If S = R A , then validity of χ is decidable, and if S = Z or S = Q, then validity of χ is semi-decidable [13,60]. But χ is valid iff the loop is non-terminating. Hence, non-termination is decidable for S = R A and semi-decidable if S = Z or S = Q. The claim (b) for S = R A follows since deciding non-termination is equivalent to deciding termination. Finally, (a) and the claim (b) for S = R follow due to elementary equivalence of R A and R.
A.14 Proof of Lemma 5.13 Proof. Let F be defined by ψ F ∈ Th ∃ (S, R A ). Consider the following formula ψ ∈ Th ∃ (S, R A ): ∃ y ∈ R d A . ψ F ( y) ∧ x = (η( x)) ( y). Then ψ( e) holds for e ∈ R d A iff e = η( u) for some u ∈ R d A with ψ F ( u), i.e., with u ∈ F .
For each A i , let Q i = T i ·A i ·T −1 i be its Jordan normal form, where T i is the corresponding transformation matrix. Since A i has only real eigenvectors, this means that the entries of Q i , T i , and T −1 i are real algebraic numbers. Let η be the endomorphism defined by η( x J i ) = T i · x J i . This means that η is induced by the block diagonal matrix Diag(T 1 , T 2 , . . . , T k ). Then η is an automorphism and its inverse satisfies η −1 ( x J i ) = T −1 i · x J i . Furthermore, the degree of η is obviously 1. Moreover, η and η −1 are compatible with the partition, i.e., the images of the variables in x J i under η and η −1 are polynomials only using the variables x J i . For each 1 ≤ i ≤ k we have: We have T i · p i ∈ S[ x J i+1 , . . . , x J k ] d i . Therefore, (T i · p i ) [ x/η −1 ( x)] ∈ S[ x J i+1 , . . . , x J k ] d i as well, since η −1 is compatible with the partitioning. This implies that Tr η (ϕ, u) is weakly non-linear. As we assumed that x is ordered w.r.t. the partitioning and each Q i is triangular, Tr η (ϕ, u) is triangular, too. Thus, Tr η (ϕ, u) is in twn-form.

A.16 Proof of Lemma 5.18
Proof. Let δ = deg(η). For any k ∈ N, there is only a finite number of monomials over x of degree k. (The number of monomials of exactly degree k is d+k−1 k , see the proof of Thm. 6.11 (b).) Hence, for any 1 ≤ i ≤ d we can construct the following term that stands for η −1 (x i ): m is a monomial of (at most) degree δ d−1 a i,m · m Here, the monomials m contain the variables x and the a i,m are variables that stand for the unknown coefficients of the polynomial η −1 (x i ).
Hence, for any 1 ≤ i ≤ d we now build a formula ρ r,i which stands for the requirement " η • η −1 (x i ) = η −1 (x i ) (η( x)) = x i " (i.e., that η −1 is a right inverse of η): ρ r,i : m is a monomial of (at most) degree δ d−1 a i,m · η(m) = x i Similarly, for any 1 ≤ i ≤ d we construct a formula ρ l,i which stands for the requirement " η −1 • η (x i ) = (η(x i )) η −1 ( x) = x i " (i.e., that η −1 is a left inverse of η): ρ l,i : η(x i ) m is a monomial of (at most) degree δ d−1 a i,m · m = x i Thus, the formula is valid iff η has an inverse of degree at most δ d−1 . By Thm. 5.17, this is equivalent to the question whether η has an inverse, i.e., whether η is an automorphism. Unfortunately, (20) ∈ Th ∃ (R A ). However, d i=1 ρ r,i ∧ d i=1 ρ l,i has to hold for all x ∈ R d A . So, we can reduce this formula to a system of equations: one simply has to check whether there is an instantiation of the unknown coefficients a i,m such that all monomials in ρ r,i and ρ l,i except x i get the coefficient 0 and the monomial x i gets the coefficient 1. When building the conjunction of these equations and existentially quantifying the unknown coefficients a i,m , one indeed obtains a formula from Th ∃ (R A ).
A.17 Proof of Thm. 5.19 Proof. For every 1 ≤ i ≤ d, let η(x i ) = m is a monomial of (at most) degree δ b i,m · m, where the b i,m are variables that stand for unknown coefficients. By Lemma 5.18 there is a Th ∃ (R A )-formula that contains both b i,m and the variables a i,m (for the coefficients of η −1 ) which expresses that η is an automorphism.
Furthermore, using these coefficients we can construct a formula from Th ∃ (R A ) which expresses that the update u ′ = (u ′ 1 , . . . , So there is a bound on the degree of the polynomials in the transformed loop Tr η (ϕ, u). For every 1 ≤ i ≤ d, let where the variables c i,m stand again for unknown coefficients. Now we can build a Th ∃ (R A )formula which is valid iff u ′ is in twn-form by requiring that certain coefficients c i,m are zero. Moreover, we can construct a Th ∃ (R A )-formula which is valid iff u ′ = (η −1 • u • η)( x).
Now we over-approximate the cardinality of cA. If v ∈ cA, then for every 1 ≤ i ≤ k, we have v j = 0 for at most one j ∈ B i . So for the values v j with j ∈ B i , there are 3 · |B i | + 1 possibilities: either exactly one of them is 1, -1, or ⋆, or we have v j = 0 for all j ∈ B i . When combining this result for all 1 ≤ i ≤ k by multiplication, we obtain cndAssg(ρ f,m ) ≤ A.29 Proof of Cor. 7.34 Proof. We show that ℓ ≤ d + 1 for |coefs(p)| = ℓ. Then the result follows from Lemma 7.32 and the definition of cndAssg, since cndAssg(ic(p ⊲ 0)) ⊆ ℓ s=0 cndAssg(ρ f,s ). The number of coefficients of p is determined by the number of terms of the form α · n a · b n occurring in p. But these terms are connected to the bases 0 < λ 1 < . . . < λ k of the hierarchical k-partition: either b = λ i for some 1 ≤ i ≤ k and 0 ≤ a ≤ ν i − 1 or a = 0 and b = 1. So we have at most 1 + k i=1 ν i = 1 + d such terms in p, i.e., ℓ ≤ d + 1.
Completeness: Let ψ = ic(ϕ( h)) and let ∃ x ∈ S d . ψ be valid, i.e., there is a v ∈ S d such that ψ( v) holds. We have to prove that there is a v ′ ∈ cndAssg(ic(ϕ( h))) where ψ( v ′ )↓ is satisfiable. Then the claim follows as the algorithm calls SMT on ψ( v ′ )↓ and thus returns ⊤.
Note that ϕ is a propositional formula only built from the connectives ∧ and ∨, i.e., it does not contain ¬. By construction, ic(ϕ( h)) results from ϕ by replacing each atom f ⊲ 0 by ic(f ( h) ⊲ 0). So similar to the concept of fundamental sets in the proof of Thm. 4.9, there is a subset {ic(f 1 ( h) ⊲ 1 0), . . . , ic(fe( h) ⊲e 0)} of these formulas such that v satisfies them all and such that satisfying these formulas is sufficient for satisfying ψ.
Let ℓ 1 , . . . , ℓe be the numbers of coefficients in the poly-exponential expressions f 1 ( h), . . . , fe( h). By Cor. 7.25, ic(fr( h) ⊲r 0) has the form ℓr s=1 ρ fr ,s or ℓr s=0 ρ fr ,s for each 1 ≤ r ≤ e. So for every r there is at least one s where ρ fr ,s ( v) is true. But due to the construction of ρ fr ,s in Lemma 7.21 and Cor. 7.23, for every v there is at most one 0 ≤ s ≤ ℓr where ρ fr ,s ( v) is true. Thus, for every 1 ≤ r ≤ e, there is a unique 0 ≤ sr ≤ ℓr where ρ fr ,sr ( v) is true.
Let B 1 , . . . , B k again be the blocks from the k-partition h 1 , . . . , h d . Note that if there is a block B i and some ρ fr ,sr requires x j with j ∈ B i to be non-zero, then ρ fr ,sr requires all x j ′ with j ′ > j and j ′ ∈ B i to be zero. Thus, since v satisfies all formulas ρ fr ,sr for 1 ≤ r ≤ e, for each B i there is at most one r ∈ B i where some ρ fr ,sr requires x j to be non-zero. Hence, we can assume that for each block B i there is at most one j ∈ B i where v j = 0.