CCS Concepts: • Do Not Use This Code → Generate the Correct Terms for Your Paper; Generate the Correct Terms for Your Paper ; Generate the Correct Terms for Your Paper; Generate the Correct Terms for Your Paper.
Authors’ Contact Information: Yihong Zhang, yz489@cs.washington.edu, University of Washington, Seattle, USA; Bala Vinaithirthan, Stanford University, Stanford, USA; Karima Ma, Adobe, USA; Zachary Tatlock, ztatlock@cs.washington.edu, University of Washington, Seattle, USA; Maaz Bin Safeer Ahmad; Andrew Adams, Adobe, USA.
Introduction
A recurrence defines a sequence of values, where each value f ( x ) f(x) f ( x ) depends on previous values (e.g., f ( x − 1 ) f(x-1) f ( x − 1 ) ). Recurrences are ubiquitous in many computational domains. The simplest form of a recurrence is a prefix sum of an input sequence g g g :
f ( x ) = f ( x − 1 ) + g ( x ) . f(x) = f(x-1) + g(x). f ( x ) = f ( x − 1 ) + g ( x ) .
(Prefix Sum)
In signal processing, many classical filters can be expressed as recurrences. They are known as infinite-impulse response (IIR) filters. For example,
f ( x ) = 1.8 ⋅ f ( x − 1 ) − 0.9 ⋅ f ( x − 2 ) + g ( x ) . f(x) = 1.8 \cdot f(x-1) - 0.9 \cdot f(x-2) + g(x). f ( x ) = 1.8 ⋅ f ( x − 1 ) − 0.9 ⋅ f ( x − 2 ) + g ( x ) .
(A Band-Pass Filter)
In computer graphics, an approximate distance transform is a recurrence with min and +:
f ( x , y ) = min ( f ( x − 1 , y ) + 1 , f ( x , y − 1 ) + 1 , f ( x − 1 , y − 1 ) + 2 , g ( x , y ) ) . f(x,y) = \min\left(f(x-1,y)+1, f(x,y-1)+1, f(x-1,y-1)+\sqrt{2}, g(x,y)\right). f ( x , y ) = min ( f ( x − 1 , y ) + 1 , f ( x , y − 1 ) + 1 , f ( x − 1 , y − 1 ) + 2 , g ( x , y ) ) .
(Distance Transform)
Sequence alignment algorithms in bioinformatics have a similar pattern, where s s s is a score matrix and c g a p c_{\mathrm{gap}} c gap is a constant gap penalty:
f ( x , y ) = max ( f ( x − 1 , y − 1 ) + s ( x , y ) , f ( x − 1 , y ) + c g a p , f ( x , y − 1 ) + c g a p ) . f(x,y) = \max\left(f(x-1,y-1)+s(x,y), f(x-1,y)+c_{\mathrm{gap}}, f(x,y-1)+c_{\mathrm{gap}}\right). f ( x , y ) = max ( f ( x − 1 , y − 1 ) + s ( x , y ) , f ( x − 1 , y ) + c gap , f ( x , y − 1 ) + c gap ) .
(Needleman–Wunsch)
Even dynamic programming problems on LeetCode often reduce to computing recurrences:
f 1 ( x , y ) = max ( f 1 ( x , y − 1 ) , f 2 ( x − 1 , y − 1 ) − g ( y ) ) , f 2 ( x , y ) = max ( f 1 ( x , y − 1 ) + g ( y ) , f 2 ( x , y − 1 ) ) . \begin{aligned}
f_1(x,y) &= \max\left(f_1(x,y-1), f_2(x-1,y-1)-g(y)\right),\\
f_2(x,y) &= \max\left(f_1(x,y-1)+g(y), f_2(x,y-1)\right).
\end{aligned} f 1 ( x , y ) f 2 ( x , y ) = max ( f 1 ( x , y − 1 ) , f 2 ( x − 1 , y − 1 ) − g ( y ) ) , = max ( f 1 ( x , y − 1 ) + g ( y ) , f 2 ( x , y − 1 ) ) .
(LeetCode 188. Buy/Sell Stock) These tasks from signal processing, graphics, bioinformatics, and LeetCode are all instances of the same problem: computing an F F F that satisfies
F = A ⊗ F ⊕ G F = A \otimes F \oplus G F = A ⊗ F ⊕ G
over a semiring τ \tau τ . In this definition, A A A is a convolution kernel (a finite array of coefficients, or taps ), G G G is the input signal (a stream of values), F F F is the output signal, and convolution ⊗ \otimes ⊗ computes a sliding weighted sum of the signal by the kernel.1 For example, by instantiating the recurrence over the arithmetic (semi)ring and letting A A A be the kernel [ 0 , 1.8 , − 0.9 ] [0, 1.8, -0.9] [ 0 , 1.8 , − 0.9 ] , we obtain the band-pass filter above. Similarly, if we use the (min, +)-semiring and set the (2D) kernel to [ ∞ 1 1 2 ] \begin{bmatrix} \infty& 1 \\ 1 & \sqrt{2} \end{bmatrix} [ ∞ 1 1 2 ] , we obtain the distance transform.
These recurrence relations are fundamental building blocks in their respective domains. Computing them efficiently is important, yet difficult. While modern vector instruction sets like AVX-512 allow computing 16 values at a time, these recurrences enforce dependencies on previous output values (e.g., f ( x ) f(x) f ( x ) depends on f ( x − 1 ) f(x-1) f ( x − 1 ) ), forcing a serial implementation.
Previous work has studied different aspects of speeding up these recurrences, such as multithreading [4 , 29 ] , tiling [33 ] , and data layout transformations [11 ] . This work takes a different approach: we vectorize recurrences through algebraic transformations. Unlike prior approaches that are tailored to specific application domains, our approach is general: we can vectorize a wide class of recurrences, including all those in our examples, with speedups of up to an order of magnitude.
Core to our approach are the algebraic structures of the recurrences F = A ⊗ F ⊕ G F = A \otimes F \oplus G F = A ⊗ F ⊕ G . We define an algebra for recurrences parametrized by a semiring τ \tau τ . This algebra abstracts over the orders of recurrences,2 the numerical values of coefficients, and the concrete operators, retaining only their algebraic properties. We then present algebraic identities for recurrences over different semirings that enable vectorization for a wide range of real-world recurrences.
We instantiate the recurrence algebra and vectorizing transformations in a compiler dubbed VecRec. Given an input expression, VecRec exhaustively applies the transformation schemes to derive a set of vectorization-friendly alternative forms. It generates an efficient vectorized C++ kernel for each form and picks the best-performing kernel based on runtime. The generated kernels are significantly faster on a diverse set of recurrences and, in the bioinformatic domain, outperform state-of-the-art vectorized libraries.
Overview
To convey the vectorizing transformations this paper considers, we first (painfully) vectorize a recurrence by manual term manipulation. We then sketch our recurrence algebra and show how a handful of simple identities can transform the same example into the same vectorized form.
Vectorizing a second-order digital filter
Consider the following band-pass filter again:
f ( x ) = a f ( x − 1 ) + b f ( x − 2 ) + g ( x ) a = 1.8 , b = − 0.9. (1) f(x) = af(x-1) + bf(x-2) + g(x) \qquad a = 1.8, b = -0.9. \tag*{(1)} f ( x ) = a f ( x − 1 ) + b f ( x − 2 ) + g ( x ) a = 1.8 , b = − 0.9. (1)
As written, this recurrence is not directly vectorizable: the data dependence of f ( x ) f(x) f ( x ) on f ( x − 1 ) f(x-1) f ( x − 1 ) prevents computing consecutive outputs in a single SIMD vector.
To break this dependence, we expand f ( x − 1 ) f(x-1) f ( x − 1 ) and simplify:
f ( x ) = ( a 2 + b ) f ( x − 2 ) + a b f ( x − 3 ) + g ( x ) + a g ( x − 1 ) . (2) f(x) = (a^2 + b)f(x-2) + abf(x-3) + g(x) + ag(x-1). \tag*{(2)} f ( x ) = ( a 2 + b ) f ( x − 2 ) + ab f ( x − 3 ) + g ( x ) + a g ( x − 1 ) . (2)
After this substitution, f ( x ) f(x) f ( x ) no longer depends on f ( x − 1 ) f(x-1) f ( x − 1 ) , so we can compute two consecutive outputs in a single vector, as the following “vectorized” pseudocode3 shows:
f[x:x+2] = (a*a+b) * f[x-2:x] + a*b * f[x-3:x-1] + g[x:x+2] + a * g[x-1:x+1]
Repeating this process for f ( x − 2 ) f(x-2) f ( x − 2 ) , f ( x − 3 ) f(x-3) f ( x − 3 ) , … \ldots … , f ( x − n ) f(x-n) f ( x − n ) lets us compute n n n elements in parallel.
This approach, however, creates too much extra work. Each substitution lets us compute one more element in parallel, but it also incurs one fused multiply-add (fma) with a non-recursive g g g term (e.g., g ( x − 1 ) g(x-1) g ( x − 1 ) ). The original recurrence takes 2 fmas to compute one element, while the n n n -times-substituted version takes n + 2 n+2 n + 2 vectorized fmas to compute n + 1 n+1 n + 1 elements in parallel, so we reduce the number of fmas used by ( 2 / 1 ) / ( ( n + 2 ) / ( n + 1 ) ) ≤ 2 (2/1)/((n+2)/(n+1)) \le2 ( 2/1 ) / (( n + 2 ) / ( n + 1 )) ≤ 2 times.
To do better, observe that by rearranging Equation (1) , f ( x − 1 ) f(x-1) f ( x − 1 ) can be defined using f ( x ) f(x) f ( x ) and f ( x − 2 ) f(x-2) f ( x − 2 ) :
f ( x − 1 ) = f ( x ) − b f ( x − 2 ) − g ( x ) a (3) f(x-1) = \frac{f(x)-bf(x-2)-g(x)}{a} \tag*{(3)} f ( x − 1 ) = a f ( x ) − b f ( x − 2 ) − g ( x ) (3)
Using this to eliminate f ( x − 3 ) f(x-3) f ( x − 3 ) in Equation (2) , we obtain a definition of f ( x ) f(x) f ( x ) in terms of f ( x − 2 ) f(x-2) f ( x − 2 ) and f ( x − 4 ) f(x-4) f ( x − 4 ) ,
f ( x ) = ( a 2 + b ) f ( x − 2 ) + a b ( f ( x − 2 ) − b f ( x − 4 ) − g ( x − 2 ) a ) + g ( x ) + a g ( x − 1 ) (4) f(x) = (a^2+b)f(x-2) + ab\left(\frac{f(x-2)-bf(x-4)-g(x-2)}{a}\right) + g(x) + ag(x-1) \tag*{(4)} f ( x ) = ( a 2 + b ) f ( x − 2 ) + ab ( a f ( x − 2 ) − b f ( x − 4 ) − g ( x − 2 ) ) + g ( x ) + a g ( x − 1 ) (4)
= a ′ f ( x − 2 ) + b ′ f ( x − 4 ) + g ′ ( x ) (5) = a'f(x-2) + b'f(x-4) + g'(x) \tag*{(5)} = a ′ f ( x − 2 ) + b ′ f ( x − 4 ) + g ′ ( x ) (5)
where a ′ = a 2 + 2 b , b ′ = − b 2 , g ′ ( x ) = g ( x ) + a g ( x − 1 ) − b g ( x − 2 ) . (6) \text{where}\quad a'=a^2+2b,\quad b'=-b^2,\quad g'(x)=g(x)+ag(x-1)-bg(x-2). \tag*{(6)} where a ′ = a 2 + 2 b , b ′ = − b 2 , g ′ ( x ) = g ( x ) + a g ( x − 1 ) − b g ( x − 2 ) . (6)
Computing the auxiliary g ′ g' g ′ costs 2 additional fmas. We can apply the trick again to f ( x ) = a ′ f ( x − 2 ) + b ′ f ( x − 4 ) + g ′ ( x ) f(x)=a'f(x-2)+b'f(x-4)+g'(x) f ( x ) = a ′ f ( x − 2 ) + b ′ f ( x − 4 ) + g ′ ( x ) : expand f ( x − 2 ) f(x-2) f ( x − 2 ) , define f ( x − 6 ) f(x-6) f ( x − 6 ) in terms of f ( x − 4 ) f(x-4) f ( x − 4 ) and f ( x − 8 ) f(x-8) f ( x − 8 ) , and substitute. We now define f ( x ) f(x) f ( x ) in terms of f ( x − 4 ) f(x-4) f ( x − 4 ) and f ( x − 8 ) f(x-8) f ( x − 8 ) , moving the self-dependency from 2 to 4 elements away. In effect, this procedure doubles the distances between a recurrence’s coefficients. We call it dilation .
By repeatedly applying dilation, we obtain a tower of definitions:
g 1 ( x ) = a 0 g ( x − 1 ) + b 0 g ( x − 2 ) + g ( x ) , … g n ( x ) = a n − 1 g n − 1 ( x − 2 n − 1 ) + b n − 1 g n − 1 ( x − 2 n ) + g n − 1 ( x ) , f ( x ) = a n f ( x − 2 n ) + b n f ( x − 2 n + 1 ) + g n ( x ) . \begin{aligned}
g_1(x) &= a_0g(x-1)+b_0g(x-2)+g(x),\\
&\ldots\\
g_n(x) &= a_{n-1}g_{n-1}(x-2^{n-1})+b_{n-1}g_{n-1}(x-2^n)+g_{n-1}(x),\\
f(x) &= a_nf(x-2^n)+b_nf(x-2^{n+1})+g_n(x).
\end{aligned} g 1 ( x ) g n ( x ) f ( x ) = a 0 g ( x − 1 ) + b 0 g ( x − 2 ) + g ( x ) , … = a n − 1 g n − 1 ( x − 2 n − 1 ) + b n − 1 g n − 1 ( x − 2 n ) + g n − 1 ( x ) , = a n f ( x − 2 n ) + b n f ( x − 2 n + 1 ) + g n ( x ) .
Each layer of the tower doubles the distance between coefficients. Moreover, since the definition of each g i g_i g i does not depend on g i g_i g i itself, we can compute the elements of g i g_i g i in parallel, for any i = 1 , … , n i=1,\ldots,n i = 1 , … , n . In total, this tower of definitions requires 2 ⋅ ( n + 1 ) 2\cdot(n+1) 2 ⋅ ( n + 1 ) vectorized fmas, but it can compute 2 n 2^n 2 n elements at once. In other words, n n n steps of dilation reduce the number of fmas by
2 / 1 2 ⋅ ( n + 1 ) / 2 n × = 2 n n + 1 × . \frac{2/1}{2\cdot(n+1)/2^n}\times=\frac{2^n}{n+1}\times. 2 ⋅ ( n + 1 ) / 2 n 2/1 × = n + 1 2 n × .
This trick is known as scattered-lookahead [25 ] in digital circuit design but is largely overlooked in software, where it can yield significant speedups, as we will show: on an AMD Zen 5 CPU, dilating the above filter four times achieves a 4.86× speedup.
In this work, we systematically study such vectorization techniques. Dilation for arithmetic recurrences is just one of many transformations that enable vectorization; Figure 1 organizes the full set used in this paper by the algebraic property each exploits.
Figure 1. Vectorization techniques proposed in this paper. Fast associative scan and path-based decomposition apply to any recurrence, while dilation requires a time-invariant kernel over a ring or an idempotent semiring. VecRec use all applicable transformations and use profiling to find the fastest kernel.
The recurrence algebra
We propose an algebra of linear, fixed-order recurrences, inspired by the Z-transform [24 ] . Central to our recurrence algebra are two types of values, kernels and signals , and two types of operations, convolution (⊗ \otimes ⊗ ) and (pointwise) addition (⊕ \oplus ⊕ ).
Under this algebra, a recurrence F F F is defined as a signal that satisfies F = A ⊗ F ⊕ G F=A\otimes F\oplus G F = A ⊗ F ⊕ G . Here, G G G and F F F are the input and output signals, and A A A is a convolution kernel. That is, F F F is defined as the convolution of itself with a kernel A A A , plus an external input G G G . To avoid self-dependency (i.e., f ( x ) f(x) f ( x ) depending on f ( x ) f(x) f ( x ) ), we require the kernel A A A to be causal : it is zero at the origin, i.e., A [ 0 ] = 0 τ A[0]=0_\tau A [ 0 ] = 0 τ .
The recurrence algebra is parametrized by the semiring it operates over. For instance, we can also interpret F = A ⊗ F ⊕ G F=A\otimes F\oplus G F = A ⊗ F ⊕ G over the tropical semiring ( R ∪ { ∞ } , min , + , ∞ , 0 ) (\mathbb{R}\cup\{\infty\},\min,+,\infty,0) ( R ∪ { ∞ } , min , + , ∞ , 0 ) , where ⊕ \oplus ⊕ is minimum and ⊗ \otimes ⊗ is addition. Tropical recurrences capture DP-style optimization problems such as the distance transform in image processing and sequence alignment in bioinformatics.
Identities of the recurrence algebra
The identities of the recurrence algebra express the vectorizing transformations in § 2.1 cleanly. For instance, arithmetic is not just a semiring but a ring, so it has an additive inverse (subtraction), denoted as ⊖ \ominus ⊖ . This allows us to perform the following derivation.
Given a time-invariant arithmetic kernel A = [ 0 , a 1 , a 2 , … ] A=[0,a_1,a_2,\ldots] A = [ 0 , a 1 , a 2 , … ] , let us define E E E and O O O as the even and odd terms of A A A , i.e., E = [ 0 , 0 , a 2 , 0 , a 4 , … ] E=[0,0,a_2,0,a_4,\ldots] E = [ 0 , 0 , a 2 , 0 , a 4 , … ] and O = [ 0 , a 1 , 0 , a 3 , … ] O=[0,a_1,0,a_3,\ldots] O = [ 0 , a 1 , 0 , a 3 , … ] , such that A = E ⊕ O A=E\oplus O A = E ⊕ O . We have
F = A ⊗ F ⊕ G ( I ⊖ A ) ⊗ F = G ( I ⊖ E ⊖ O ) ⊗ F = G ( I ⊖ E ⊕ O ) ⊗ ( I ⊖ E ⊖ O ) ⊗ F = ( I ⊖ E ⊕ O ) ⊗ G ( ( I ⊖ E ) 2 ⊖ O 2 ) ⊗ F = ( I ⊖ E ⊕ O ) ⊗ G F = ( O 2 ⊕ E ⊕ E ⊕ O 2 ) ⊗ F ⊕ ( I ⊖ E ⊕ O ) ⊗ G \begin{aligned}
F &= A\otimes F\oplus G\\
(I\ominus A)\otimes F &= G\\
(I\ominus E\ominus O)\otimes F &= G\\
(I\ominus E\oplus O)\otimes(I\ominus E\ominus O)\otimes F &= (I\ominus E\oplus O)\otimes G\\
((I\ominus E)^2\ominus O^2)\otimes F &= (I\ominus E\oplus O)\otimes G\\
F &= (O^2\oplus E\oplus E\oplus O^2)\otimes F\oplus(I\ominus E\oplus O)\otimes G
\end{aligned} F ( I ⊖ A ) ⊗ F ( I ⊖ E ⊖ O ) ⊗ F ( I ⊖ E ⊕ O ) ⊗ ( I ⊖ E ⊖ O ) ⊗ F (( I ⊖ E ) 2 ⊖ O 2 ) ⊗ F F = A ⊗ F ⊕ G = G = G = ( I ⊖ E ⊕ O ) ⊗ G = ( I ⊖ E ⊕ O ) ⊗ G = ( O 2 ⊕ E ⊕ E ⊕ O 2 ) ⊗ F ⊕ ( I ⊖ E ⊕ O ) ⊗ G
where I I I is the identity kernel I = [ 1 ] I=[1] I = [ 1 ] . This is exactly the dilation of § 2.1 : for A = [ 0 , a , b ] A=[0,a,b] A = [ 0 , a , b ] , E = [ 0 , 0 , b ] E=[0,0,b] E = [ 0 , 0 , b ] , O = [ 0 , a ] O=[0,a] O = [ 0 , a ] , the new kernel ⊖ E 2 ⊕ E ⊕ E ⊕ O 2 = [ 0 , 0 , 0 , 0 , − b 2 ] + [ 0 , 0 , 2 b ] + [ 0 , 0 , a 2 ] = [ 0 , 0 , a 2 + 2 b , 0 , − b 2 ] \ominus E^2\oplus E\oplus E\oplus O^2=[0,0,0,0,-b^2]+[0,0,2b]+[0,0,a^2]=[0,0,a^2+2b,0,-b^2] ⊖ E 2 ⊕ E ⊕ E ⊕ O 2 = [ 0 , 0 , 0 , 0 , − b 2 ] + [ 0 , 0 , 2 b ] + [ 0 , 0 , a 2 ] = [ 0 , 0 , a 2 + 2 b , 0 , − b 2 ] matches the coefficients in Equation (5) , but the algebraic form generalizes to kernels with any number of taps.
Similarly, we derive vectorizing transformations for idempotent, time-varying, and mutual recurrences from identities in our algebraic framework.
A vectorizing compiler for recurrences
We instantiate the recurrence algebra and the vectorizing transformations in a compiler, dubbed VecRec. Given an input recurrence, VecRec automatically synthesizes several equivalent, vectorization-friendly forms, emits efficient vectorized C++ code for each form, and selects the best-performing form by throughput. The generated kernel is efficient and pluggable into existing systems.
Paper organization
The rest of the paper is organized as follows. § 3 introduces the syntax and semantics of the recurrence algebra and discusses the properties of ⊕ \oplus ⊕ and ⊗ \otimes ⊗ . § 4 discusses the algebraic identities of the recurrence operator. § 5 presents the vectorizing transformations of recurrences and extensions to two-dimensional recurrences and mutual recurrences. § 6 describes our compiler implementation that generates vectorized kernels for recurrences. § 7 evaluates our compiler on a diverse range of recurrences and presents a case study on bioinformatic recurrences.
Syntax and Semantics
This section formally defines the recurrence algebra, which captures convolutions and (linear) recurrences over different semirings and lays the foundation for our transformations. After defining its syntax and semantics, we present the properties of its non-recursive fragment.
Semirings and rings
The recurrence algebra is parametrized by a commutative semiring τ \tau τ . A semiring is a tuple ( τ , ⊕ τ , ⊗ τ , 0 τ , 1 τ ) (\tau, \oplus_{\tau}, \otimes_{\tau}, 0_{\tau}, 1_{\tau}) ( τ , ⊕ τ , ⊗ τ , 0 τ , 1 τ ) such that ( τ , ⊕ τ , 0 τ ) (\tau, \oplus_{\tau}, 0_{\tau}) ( τ , ⊕ τ , 0 τ ) is a commutative monoid, ( τ , ⊗ τ , 1 τ ) (\tau, \otimes_{\tau}, 1_{\tau}) ( τ , ⊗ τ , 1 τ ) is a monoid, ⊗ τ \otimes_{\tau} ⊗ τ distributes over ⊕ τ \oplus_{\tau} ⊕ τ on both sides, and 0 τ 0_{\tau} 0 τ is absorbing for ⊗ τ \otimes_{\tau} ⊗ τ ; that is:
identity: 0 τ ⊕ τ a = a ⊕ τ 0 τ = a 1 τ ⊗ τ a = a ⊗ τ 1 τ = a associativity: ( a ⊕ τ b ) ⊕ τ c = a ⊕ τ ( b ⊕ τ c ) ( a ⊗ τ b ) ⊗ τ c = a ⊗ τ ( b ⊗ τ c ) commutativity: a ⊕ τ b = b ⊕ τ a distributivity: ( a ⊕ τ b ) ⊗ τ c = a ⊗ τ c ⊕ τ b ⊗ τ c a ⊗ τ ( b ⊕ τ c ) = a ⊗ τ b ⊕ τ a ⊗ τ c annihilation: 0 τ ⊗ τ a = a ⊗ τ 0 τ = 0 τ \begin{aligned}
\textit{identity:}\quad&0_{\tau} \oplus_{\tau} a = a \oplus_{\tau} 0_{\tau} = a && 1_{\tau} \otimes_{\tau} a = a \otimes_{\tau} 1_{\tau} = a \\
\textit{associativity:}\quad&(a \oplus_{\tau} b) \oplus_{\tau} c = a \oplus_{\tau} (b \oplus_{\tau} c) && (a \otimes_{\tau} b) \otimes_{\tau} c = a \otimes_{\tau} (b \otimes_{\tau} c) \\
\textit{commutativity:}\quad&a \oplus_{\tau} b = b \oplus_{\tau} a \\
\textit{distributivity:}\quad&(a \oplus_{\tau} b) \otimes_{\tau} c = a \otimes_{\tau} c \oplus_{\tau} b \otimes_{\tau} c && a \otimes_{\tau} (b \oplus_{\tau} c) = a \otimes_{\tau} b \oplus_{\tau} a \otimes_{\tau} c \\
\textit{annihilation:}\quad&0_{\tau} \otimes_{\tau} a = a \otimes_{\tau} 0_{\tau} = 0_{\tau}
\end{aligned} identity: associativity: commutativity: distributivity: annihilation: 0 τ ⊕ τ a = a ⊕ τ 0 τ = a ( a ⊕ τ b ) ⊕ τ c = a ⊕ τ ( b ⊕ τ c ) a ⊕ τ b = b ⊕ τ a ( a ⊕ τ b ) ⊗ τ c = a ⊗ τ c ⊕ τ b ⊗ τ c 0 τ ⊗ τ a = a ⊗ τ 0 τ = 0 τ 1 τ ⊗ τ a = a ⊗ τ 1 τ = a ( a ⊗ τ b ) ⊗ τ c = a ⊗ τ ( b ⊗ τ c ) a ⊗ τ ( b ⊕ τ c ) = a ⊗ τ b ⊕ τ a ⊗ τ c
An idempotent semiring , also known as a dioid, is a semiring that satisfies a ⊕ τ a = a a \oplus_{\tau} a = a a ⊕ τ a = a . A commutative semiring is a semiring that satisfies a ⊗ τ b = b ⊗ τ a a \otimes_{\tau} b = b \otimes_{\tau} a a ⊗ τ b = b ⊗ τ a . A ring is a semiring in which every element a a a has an additive inverse ⊖ τ a \ominus_{\tau} a ⊖ τ a with a ⊕ τ ( ⊖ τ a ) = 0 τ a \oplus_{\tau} (\ominus_{\tau} a) = 0_{\tau} a ⊕ τ ( ⊖ τ a ) = 0 τ .
Example 1 (Arithmetic ring). The real numbers ( R , + , × , 0 , 1 ) (\mathbb{R}, +, \times, 0, 1) ( R , + , × , 0 , 1 ) form a commutative ring, with ⊕ τ = + \oplus_{\tau} = + ⊕ τ = + , ⊗ τ = × \otimes_{\tau} = \times ⊗ τ = × , 0 τ = 0 0_{\tau} = 0 0 τ = 0 , 1 τ = 1 1_{\tau} = 1 1 τ = 1 , and additive inverse ⊖ τ a = − a \ominus_{\tau} a = -a ⊖ τ a = − a . Recurrences over the arithmetic ring capture many recursive filters in signal processing. □
Example 2 (Tropical semiring). The min-plus tropical semiring is ( R ∪ { + ∞ } , min , + , + ∞ , 0 ) (\mathbb{R} \cup\{+\infty\}, \min, +, +\infty, 0) ( R ∪ { + ∞ } , min , + , + ∞ , 0 ) , with ⊕ τ = min \oplus_{\tau} = \min ⊕ τ = min , ⊗ τ = + \otimes_{\tau} = + ⊗ τ = + , 0 τ = + ∞ 0_{\tau} = +\infty 0 τ = + ∞ , and 1 τ = 0 1_{\tau} = 0 1 τ = 0 . Addition is idempotent ( min ( a , a ) = a ) (\min(a, a) = a) ( min ( a , a ) = a ) , so this is an idempotent commutative semiring. It is not a ring because min \min min has no inverse. The tropical semiring appears in many combinatorial optimization problems, e.g., in bioinformatics. Similarly, the max-plus tropical semiring is defined as ( R ∪ { − ∞ } , max , + , − ∞ , 0 ) (\mathbb{R} \cup\{-\infty\}, \max, +, -\infty, 0) ( R ∪ { − ∞ } , max , + , − ∞ , 0 ) . □
A (left) semimodule over a semiring ( τ , ⊕ τ , ⊗ τ , 0 τ , 1 τ ) (\tau, \oplus_{\tau}, \otimes_{\tau}, 0_{\tau}, 1_{\tau}) ( τ , ⊕ τ , ⊗ τ , 0 τ , 1 τ ) consists of a set M M M with an addition ⊕ M \oplus_M ⊕ M , a zero 0 M 0_M 0 M , and a scalar multiplication ⊗ M : τ × M → M \otimes_M : \tau\times M \to M ⊗ M : τ × M → M such that ( M , ⊕ M , 0 M ) (M, \oplus_M, 0_M) ( M , ⊕ M , 0 M ) is a commutative monoid and the scalar multiplication is compatible with the semiring structure; that is, for all r , s ∈ τ r,s \in\tau r , s ∈ τ and m , n , p ∈ M m,n,p \in M m , n , p ∈ M :
identity: $0_M \oplus_M m = m \oplus_M 0_M = m$ 1 τ ⊗ M m = m \$1_\tau\otimes_M m = m $ 1 τ ⊗ M m = m
associativity: ( m ⊕ M n ) ⊕ M p = m ⊕ M ( n ⊕ M p ) (m \oplus_M n) \oplus_M p = m \oplus_M (n \oplus_M p) ( m ⊕ M n ) ⊕ M p = m ⊕ M ( n ⊕ M p ) ( r ⊗ τ s ) ⊗ M m = r ⊗ M ( s ⊗ M m ) (r \otimes_\tau s) \otimes_M m = r \otimes_M (s \otimes_M m) ( r ⊗ τ s ) ⊗ M m = r ⊗ M ( s ⊗ M m )
commutativity: m ⊕ M n = n ⊕ M m m \oplus_M n = n \oplus_M m m ⊕ M n = n ⊕ M m
distributivity: r ⊗ M ( m ⊕ M n ) = ( r ⊗ M m ) ⊕ M ( r ⊗ M n ) r \otimes_M (m \oplus_M n) = (r \otimes_M m) \oplus_M (r \otimes_M n) r ⊗ M ( m ⊕ M n ) = ( r ⊗ M m ) ⊕ M ( r ⊗ M n ) ( r ⊕ τ s ) ⊗ M m = ( r ⊗ M m ) ⊕ M ( s ⊗ M m ) (r \oplus_\tau s) \otimes_M m = (r \otimes_M m) \oplus_M (s \otimes_M m) ( r ⊕ τ s ) ⊗ M m = ( r ⊗ M m ) ⊕ M ( s ⊗ M m )
annihilation: 0 τ ⊗ M m = 0 M 0_\tau\otimes_M m = 0_M 0 τ ⊗ M m = 0 M r ⊗ M 0 M = 0 M r \otimes_M 0_M = 0_M r ⊗ M 0 M = 0 M
When τ \tau τ is commutative, we call M M M a commutative semimodule .
Syntax
Fix a commutative semiring τ \tau τ . The syntax of kernels and signals is shown in Figure 2 . For the purpose of this section, kernels and signals are d d d -dimensional. § 6 specializes to 1D and 2D kernels and signals and discusses their efficient code generation. Throughout the paper we use F , G , H F,G,H F , G , H as metavariables for signals and K , A , B , C K,A,B,C K , A , B , C for kernels. We often denote by G G G the external input signal and by F F F the computed output signal.
Figure 2. Syntax for signals and kernels.
A signal F F F is either an input variable x x x , a constant signal c c c for c ∈ τ c \in\tau c ∈ τ (with slightly abused notation), an impulse δ c \delta_c δ c , a pointwise addition F ⊕ G F \oplus G F ⊕ G , a pointwise multiplication F ⊙ G F \odot G F ⊙ G , a convolution K ⊗ F K \otimes F K ⊗ F , a recurrence Rec ( K , G ) \operatorname{Rec}(K,G) Rec ( K , G ) , or ⊖ F \ominus F ⊖ F if τ \tau τ is a ring. Note that convolutions and recurrences are defined between kernels and signals.
A kernel is a finite d d d -dimensional array of signals [ F i ] 0 ≤ i ≤ n [F_i]_{0 \le i \le n} [ F i ] 0 ≤ i ≤ n . We call the signals of a kernel its taps . A kernel is called a singleton if it only has one tap. Kernels can be tap-wise added ( K 1 ⊕ K 2 ) (K_1 \oplus K_2) ( K 1 ⊕ K 2 ) or convolved ( K 1 ⊗ K 2 ) (K_1 \otimes K_2) ( K 1 ⊗ K 2 ) . A kernel can be additively inverted ( ⊖ K ) (\ominus K) ( ⊖ K ) if τ \tau τ is a ring. We call [ 1 τ ] [1_\tau] [ 1 τ ] the identity kernel I I I and [ 0 τ ] [0_\tau] [ 0 τ ] the zero kernel 0 0 0 . The order defines the domain of a kernel and is shown in Figure 3 . We define the time-delay kernel z − k z^{-k} z − k so that ( z − k ) k = 1 τ (z^{-k})_k = 1_\tau ( z − k ) k = 1 τ and is 0 τ 0_\tau 0 τ everywhere else. For instance, the one-dimensional delay kernel z − 1 z^{-1} z − 1 is defined as [ 0 τ , 1 τ ] [0_\tau,1_\tau] [ 0 τ , 1 τ ] , and in the degenerate case z 0 = I z^0 = I z 0 = I . We use A k A^k A k to denote A A A convolved with itself k k k times, A ⊗ A ⊗ … ⊗ A A \otimes A \otimes\ldots\otimes A A ⊗ A ⊗ … ⊗ A , and A 0 = I A^0 = I A 0 = I . This notation will be justified later as we show convolution is associative (Section 3.4).
Figure 3. The order defines the domain of a kernel. In the context of kernel orders, max \max max is interpreted as the dimension-wise maximum of two vectors. Similarly, we say i ≤ n i \le n i ≤ n if i i i is less than or equal to n n n in every dimension.
Semantics
The semantics of a signal over semiring τ \tau τ is a mathematical function Z d → τ \mathbb{Z}^d \to\tau Z d → τ that maps each time index n \mathbf{n} n to a value in τ \tau τ , and the semantics of a kernel is a finite, multi-dimensional array of signals, indexable by an offset i ∈ Z d \mathbf{i} \in\mathbb{Z}^d i ∈ Z d . We say the signals in the array the taps of the kernel. Fix an environment σ \sigma σ that maps variables to semantic signal Z d → τ \mathbb{Z}^d \to\tau Z d → τ . We write ⟦ F ⟧ σ \llbracket F \rrbracket^\sigma [ [ F ] ] σ and ⟦ K ⟧ σ \llbracket K \rrbracket^\sigma [ [ K ] ] σ for the semantics of signal F F F and kernel K K K , respectively. Figure 4 shows the definitions of ⟦ F ⟧ σ \llbracket F \rrbracket^\sigma [ [ F ] ] σ and ⟦ K ⟧ σ \llbracket K \rrbracket^\sigma [ [ K ] ] σ .
⟦ x ⟧ σ = λ n . if n ≥ 0 then σ [ x ] ( n ) else 0 τ \llbracket x \rrbracket^\sigma= \lambda n.\ \text{if } n \ge0\ \text{then }\sigma[x](n)\ \text{else }0_\tau [ [ x ] ] σ = λn . if n ≥ 0 then σ [ x ] ( n ) else 0 τ
⟦ c ⟧ σ = λ n . if n ≥ 0 then c else 0 τ \llbracket c \rrbracket^\sigma= \lambda n.\ \text{if } n \ge0\ \text{then }c\ \text{else }0_\tau [ [ c ] ] σ = λn . if n ≥ 0 then c else 0 τ
⟦ δ c ⟧ σ = λ n . if n = 0 then c else 0 τ \llbracket\delta_c \rrbracket^\sigma= \lambda n.\ \text{if } n = 0\ \text{then }c\ \text{else }0_\tau [ [ δ c ] ] σ = λn . if n = 0 then c else 0 τ
⟦ K [ i ] ⟧ σ = { ⟦ K [ i ] ⟧ σ if 0 ≤ i ≤ ord ( K ) ⟦ 0 τ ⟧ σ otherwise \llbracket K[i] \rrbracket^\sigma=
\begin{cases}
\llbracket K[i] \rrbracket^\sigma& \text{if } 0 \le i \le\operatorname{ord}(K) \\
\llbracket0_\tau\rrbracket^\sigma& \text{otherwise}
\end{cases} [ [ K [ i ] ] ] σ = { [ [ K [ i ] ] ] σ [ [ 0 τ ] ] σ if 0 ≤ i ≤ ord ( K ) otherwise
⟦ F ⊕ G ⟧ σ = λ n . ⟦ F ⟧ σ ( n ) ⊕ τ ⟦ G ⟧ σ ( n ) \llbracket F \oplus G \rrbracket^\sigma= \lambda n.\llbracket F \rrbracket^\sigma(n) \oplus_\tau\llbracket G \rrbracket^\sigma(n) [ [ F ⊕ G ] ] σ = λn . [ [ F ] ] σ ( n ) ⊕ τ [ [ G ] ] σ ( n )
⟦ F ⊙ G ⟧ σ = λ n . ⟦ F ⟧ σ ( n ) ⊗ τ ⟦ G ⟧ σ ( n ) \llbracket F \odot G \rrbracket^\sigma= \lambda n.\llbracket F \rrbracket^\sigma(n) \otimes_\tau\llbracket G \rrbracket^\sigma(n) [ [ F ⊙ G ] ] σ = λn . [ [ F ] ] σ ( n ) ⊗ τ [ [ G ] ] σ ( n )
⟦ K ⊗ F ⟧ σ = λ n . ⨁ 0 ≤ i ≤ ord ( K ) ⟦ K [ i ] ⟧ σ ( n ) ⊗ τ ⟦ F ⟧ σ ( n − i ) = [ ⨁ 0 ≤ i ≤ ord ( K ) K [ i ] ⊙ ( z − i ⊗ F ) ] σ \llbracket K \otimes F \rrbracket^\sigma
= \lambda n.\bigoplus_{0 \le i \le\operatorname{ord}(K)} \llbracket K[i] \rrbracket^\sigma(n) \otimes_\tau\llbracket F \rrbracket^\sigma(n-i)
= \left[\bigoplus_{0 \le i \le\operatorname{ord}(K)} K[i] \odot(z^{-i} \otimes F)\right]^\sigma [ [ K ⊗ F ] ] σ = λn . 0 ≤ i ≤ ord ( K ) ⨁ [ [ K [ i ] ] ] σ ( n ) ⊗ τ [ [ F ] ] σ ( n − i ) = 0 ≤ i ≤ ord ( K ) ⨁ K [ i ] ⊙ ( z − i ⊗ F ) σ
⟦ Rec ( K , G ) ⟧ σ = λ n . ⨁ k ≥ 0 ⟦ K k ⊗ G ⟧ σ ( n ) = [ ⨁ k = 0 ∞ K k ⊗ G ] σ \llbracket\operatorname{Rec}(K,G) \rrbracket^\sigma
= \lambda n.\bigoplus_{k \ge0}\llbracket K^k \otimes G \rrbracket^\sigma(n)
= \left[\bigoplus_{k=0}^{\infty}K^k \otimes G\right]^\sigma [ [ Rec ( K , G ) ] ] σ = λn . k ≥ 0 ⨁ [ [ K k ⊗ G ] ] σ ( n ) = [ k = 0 ⨁ ∞ K k ⊗ G ] σ
⟦ ⊖ F ⟧ σ = λ n . ⊖ τ ⟦ F ⟧ σ ( n ) \llbracket\ominus F \rrbracket^\sigma= \lambda n.\ominus_\tau\llbracket F \rrbracket^\sigma(n) [ [ ⊖ F ] ] σ = λn . ⊖ τ [ [ F ] ] σ ( n )
⟦ [ F k ] 0 ≤ k ≤ n ⟧ σ = [ ⟦ F k ⟧ σ ] 0 ≤ k ≤ n \llbracket[F_k]_{0 \le k \le n} \rrbracket^\sigma
= \left[\llbracket F_k \rrbracket^\sigma\right]_{0 \le k \le n} [ [ [ F k ] 0 ≤ k ≤ n ] ] σ = [ [ [ F k ] ] σ ] 0 ≤ k ≤ n
⟦ K 1 ⊕ K 2 ⟧ σ = ( ⟦ K 1 [ k ] ⊕ K 2 [ k ] ⟧ σ ) 0 ≤ k ≤ ord ( K 1 ⊕ K 2 ) \llbracket K_1 \oplus K_2 \rrbracket^\sigma
= \left(\llbracket K_1[k] \oplus K_2[k] \rrbracket^\sigma\right)_{0 \le k \le\operatorname{ord}(K_1 \oplus K_2)} [ [ K 1 ⊕ K 2 ] ] σ = ( [ [ K 1 [ k ] ⊕ K 2 [ k ] ] ] σ ) 0 ≤ k ≤ ord ( K 1 ⊕ K 2 )
⟦ K 1 ⊗ K 2 ⟧ σ = [ λ n . ⨁ 0 ≤ i ≤ k ⟦ K 1 [ i ] ⟧ σ ( n ) ⊗ τ ⟦ K 2 [ k − i ] ⟧ σ ( n − i ) ] 0 ≤ k ≤ ord ( K 1 ⊗ K 2 ) = [ ( ⨁ 0 ≤ i ≤ k K 1 [ i ] ⊙ ( z − i ⊗ K 2 [ k − i ] ) ) σ ] 0 ≤ k ≤ ord ( K 1 ⊗ K 2 ) \begin{aligned}
\llbracket K_1 \otimes K_2 \rrbracket^\sigma
= \left[\lambda n.\bigoplus_{0 \le i \le k}\llbracket K_1[i] \rrbracket^\sigma(n)\otimes_\tau\llbracket K_2[k-i] \rrbracket^\sigma(n-i)\right]_{0 \le k \le\operatorname{ord}(K_1\otimes K_2)} \\
&= \left[\left(\bigoplus_{0 \le i \le k}K_1[i]\odot(z^{-i}\otimes K_2[k-i])\right)^\sigma\right]_{0 \le k \le\operatorname{ord}(K_1\otimes K_2)}
\end{aligned} [ [ K 1 ⊗ K 2 ] ] σ = [ λn . 0 ≤ i ≤ k ⨁ [ [ K 1 [ i ] ] ] σ ( n ) ⊗ τ [ [ K 2 [ k − i ] ] ] σ ( n − i ) ] 0 ≤ k ≤ ord ( K 1 ⊗ K 2 ) = [ ( 0 ≤ i ≤ k ⨁ K 1 [ i ] ⊙ ( z − i ⊗ K 2 [ k − i ]) ) σ ] 0 ≤ k ≤ ord ( K 1 ⊗ K 2 )
⟦ ⊖ K ⟧ σ = [ ⟦ ⊖ K [ k ] ⟧ σ ] 0 ≤ k ≤ ord ( K ) \llbracket\ominus K \rrbracket^\sigma
= \left[\llbracket\ominus K[k]\rrbracket^\sigma\right]_{0 \le k \le\operatorname{ord}(K)} [ [ ⊖ K ] ] σ = [ [ [ ⊖ K [ k ] ] ] σ ] 0 ≤ k ≤ ord ( K )
Fig. 4. Semantics for signals and kernels.
We say a signal F F F is time-invariant if its semantics is a constant function, i.e., ∀ n 1 , n 2 . ⟦ F ⟧ σ ( n 1 ) = ⟦ F ⟧ σ ( n 2 ) \forall n_1,n_2.\ \llbracket F\rrbracket^\sigma(n_1)=\llbracket F\rrbracket^\sigma(n_2) ∀ n 1 , n 2 . [ [ F ] ] σ ( n 1 ) = [ [ F ] ] σ ( n 2 ) . We say a kernel K K K is time-invariant if all of its taps are time-invariant. Otherwise the kernel is called time-varying .
Signals. Signals evaluate to functions from time indices to values in τ \tau τ . They evaluate to 0 τ 0_\tau 0 τ at any negative index. A signal variable is evaluated by looking up the environment σ \sigma σ , and a constant signal c c c is evaluated to c c c at every non-negative index. The impulse δ c \delta_c δ c is c c c at the origin and 0 τ 0_\tau 0 τ elsewhere. Tap selection K [ i ] K[i] K [ i ] returns the taps of kernel K K K at offset i i i if defined and the zero signal 0 τ 0_\tau 0 τ otherwise. The operators F ⊕ G F \oplus G F ⊕ G and F ⊙ G F \odot G F ⊙ G lift ⊕ τ \oplus_\tau ⊕ τ and ⊗ τ \otimes_\tau ⊗ τ to signals pointwise.
The expression A ⊗ F A \otimes F A ⊗ F convolves kernel A A A with signal F F F : it slides A A A across F F F , multiplying each tap by the correspondingly shifted signal and summing. At time n n n ,
⟦ A ⊗ F ⟧ σ ( n ) = ⨁ 0 ≤ i ≤ ord ( A ) ⟦ A [ i ] ⟧ σ ( n ) ⊗ τ ⟦ F ⟧ σ ( n − i ) . \llbracket A \otimes F \rrbracket^\sigma(n)
= \bigoplus_{0 \le i \le\operatorname{ord}(A)}
\llbracket A[i]\rrbracket^\sigma(n)\otimes_\tau\llbracket F\rrbracket^\sigma(n-i). [ [ A ⊗ F ] ] σ ( n ) = 0 ≤ i ≤ ord ( A ) ⨁ [ [ A [ i ] ] ] σ ( n ) ⊗ τ [ [ F ] ] σ ( n − i ) .
Convolving with the delay kernel z − i z^{-i} z − i delays the signal by i i i time steps, so z − i ⊗ F z^{-i}\otimes F z − i ⊗ F is F F F shifted forward in time by i i i . In fact, any convolution A ⊗ F A\otimes F A ⊗ F can be written in terms of convolution with the delay kernel, i.e., ⨁ i A [ i ] ⊙ ( z − i ⊗ F ) \bigoplus_i A[i]\odot(z^{-i}\otimes F) ⨁ i A [ i ] ⊙ ( z − i ⊗ F ) .
Example 3. A finite impulse response (FIR) filter in signal processing is a convolution A ⊗ G A\otimes G A ⊗ G between a time-invariant kernel A A A and a signal G G G over the arithmetic ring. Consider the moving sum f ( x ) = g ( x ) + g ( x − 1 ) + g ( x − 2 ) + g ( x − 3 ) f(x)=g(x)+g(x-1)+g(x-2)+g(x-3) f ( x ) = g ( x ) + g ( x − 1 ) + g ( x − 2 ) + g ( x − 3 ) . This is represented in the recurrence algebra as F = [ 1 , 1 , 1 , 1 ] ⊗ G F=[1,1,1,1]\otimes G F = [ 1 , 1 , 1 , 1 ] ⊗ G .
The recurrence R e c ( A , G ) Rec(A,G) R ec ( A , G ) denotes the unbounded sum ⨁ k ≥ 0 A k ⊗ G \bigoplus_{k\ge0}A^k\otimes G ⨁ k ≥ 0 A k ⊗ G . Equivalently, it computes the signal F F F satisfying F = A ⊗ F ⊕ G F=A\otimes F\oplus G F = A ⊗ F ⊕ G . The infinite sum is well-defined when ⟦ A ⟧ σ \llbracket A\rrbracket^\sigma [ [ A ] ] σ is 0 τ 0_\tau 0 τ at offset 0 0 0 . We call this condition causality , and it ensures that the recurrence does not depend on its own current value. We defer a detailed discussion of R e c Rec R ec operator to § 4 .
Example 4. An infinite impulse response (IIR) filter in signal processing is a recurrence R e c ( K , G ) Rec(K,G) R ec ( K , G ) between a time-invariant kernel K K K and an input signal G G G over the arithmetic ring. Our band-pass IIR filter f ( x ) = 1.8 ⋅ f ( x − 1 ) − 0.9 ⋅ f ( x − 2 ) + g ( x ) f(x)=1.8\cdot f(x-1)-0.9\cdot f(x-2)+g(x) f ( x ) = 1.8 ⋅ f ( x − 1 ) − 0.9 ⋅ f ( x − 2 ) + g ( x ) is represented in the recurrence algebra as F = R e c ( [ 0 , 1.8 , − 0.9 ] , G ) F=Rec([0,1.8,-0.9],G) F = R ec ([ 0 , 1.8 , − 0.9 ] , G ) , which is a second-order, time-invariant recurrence.
Kernels. A kernel denotes an array of signals: a literal [ F i ] 0 ≤ i ≤ n [F_i]_{0\le i\le n} [ F i ] 0 ≤ i ≤ n denotes the array of denotations of its taps F i F_i F i , K 1 ⊕ K 2 K_1\oplus K_2 K 1 ⊕ K 2 adds the kernels tap-wise (widening to the higher order), and ⊖ K \ominus K ⊖ K negates K K K tap-wise.
K 1 ⊗ K 2 K_1\otimes K_2 K 1 ⊗ K 2 convolves the two kernels. The k-th tap of the convolution collects every way of splitting k k k across the two operands; it pairs a tap ⟦ K 1 ⟧ i σ \llbracket K_1\rrbracket^\sigma_i [ [ K 1 ] ] i σ at delay i i i with a tap ⟦ K 2 ⟧ k − i σ \llbracket K_2\rrbracket^\sigma_{k-i} [ [ K 2 ] ] k − i σ at delay k − i k-i k − i for 0 ≤ i ≤ k 0\le i\le k 0 ≤ i ≤ k . This splitting also shifts when the second coefficient is read. Recall that a tap at index i i i denotes a contribution from i i i steps back. Since the tap of K 1 K_1 K 1 already sits i i i steps in the past, the coefficient of K 2 K_2 K 2 that meets it is taken from i i i steps earlier as well, hence ⟦ K 1 ⟧ σ ( n ) ⊗ τ ⟦ K 2 ⟧ k − i σ ( n − i ) \llbracket K_1\rrbracket^\sigma(n)\otimes_\tau\llbracket K_2\rrbracket^\sigma_{k-i}(n-i) [ [ K 1 ] ] σ ( n ) ⊗ τ [ [ K 2 ] ] k − i σ ( n − i ) . If K 2 K_2 K 2 is time-invariant, its coefficients do not depend on time, so the delay has no effect and the expression recovers the familiar convolution sum. Putting everything together, we have the convolution evaluates to λ n . ⨁ 0 ≤ i ≤ k ⟦ K 1 ⟧ i σ ( n ) ⊗ τ ⟦ K 2 ⟧ k − i σ ( n − i ) \lambda n.\bigoplus_{0\le i\le k}\llbracket K_1\rrbracket^\sigma_i(n)\otimes_\tau\llbracket K_2\rrbracket^\sigma_{k-i}(n-i) λn . ⨁ 0 ≤ i ≤ k [ [ K 1 ] ] i σ ( n ) ⊗ τ [ [ K 2 ] ] k − i σ ( n − i ) at tap k k k . Equivalently, we can also use the delay kernel to denote the time shift and denote the tap k k k of the convolution as ⟦ ⨁ 0 ≤ i ≤ k K 1 [ i ] ⊙ ( z − i ⊗ K 2 [ k − i ] ) ⟧ σ \llbracket\bigoplus_{0\le i\le k}K_1[i]\odot(z^{-i}\otimes K_2[k-i])\rrbracket^\sigma [ [ ⨁ 0 ≤ i ≤ k K 1 [ i ] ⊙ ( z − i ⊗ K 2 [ k − i ]) ] ] σ . This form is handy for algebraic simplification.
Example 5. Consider time-varying kernels A = [ S 1 , S 2 ] A=[S_1,S_2] A = [ S 1 , S 2 ] and B = [ S 3 , 0 τ , S 4 ] B=[S_3,0_\tau,S_4] B = [ S 3 , 0 τ , S 4 ] . The convolution A ⊗ B A\otimes B A ⊗ B is equivalent to
A ⊗ B = [ S 1 ⊙ S 3 , S 1 ⊙ 0 τ ⊕ S 2 ⊙ ( z − 1 ⊗ S 3 ) , S 1 ⊙ S 4 ⊕ S 2 ⊙ ( z − 1 ⊗ 0 τ ) , S 2 ⊙ ( z − 1 ⊗ S 4 ) ] A\otimes B=[S_1\odot S_3,\ S_1\odot0_\tau\oplus S_2\odot(z^{-1}\otimes S_3),\ S_1\odot S_4\oplus S_2\odot(z^{-1}\otimes0_\tau),\ S_2\odot(z^{-1}\otimes S_4)] A ⊗ B = [ S 1 ⊙ S 3 , S 1 ⊙ 0 τ ⊕ S 2 ⊙ ( z − 1 ⊗ S 3 ) , S 1 ⊙ S 4 ⊕ S 2 ⊙ ( z − 1 ⊗ 0 τ ) , S 2 ⊙ ( z − 1 ⊗ S 4 )] = [ S 1 ⊙ S 3 , S 2 ⊙ ( z − 1 ⊗ S 3 ) , S 1 ⊙ S 4 , S 2 ⊙ ( z − 1 ⊗ S 4 ) ] =[S_1\odot S_3,\ S_2\odot(z^{-1}\otimes S_3),\ S_1\odot S_4,\ S_2\odot(z^{-1}\otimes S_4)] = [ S 1 ⊙ S 3 , S 2 ⊙ ( z − 1 ⊗ S 3 ) , S 1 ⊙ S 4 , S 2 ⊙ ( z − 1 ⊗ S 4 )]
Properties of ⊕ \oplus ⊕ and ⊗ \otimes ⊗
From the semantics, we can derive the algebraic properties of ⊕ \oplus ⊕ and ⊗ \otimes ⊗ .
Proposition 3.1. The operators ⊕ \oplus ⊕ and ⊗ \otimes ⊗ have the following properties:
associativity: ( A ⊗ B ) ⊗ C = A ⊗ ( B ⊗ C ) ( A ⊗ B ) ⊗ G = A ⊗ ( B ⊗ G ) distributivity: ( A ⊕ B ) ⊗ G = A ⊗ G ⊕ B ⊗ G A ⊗ ( G ⊕ H ) = A ⊗ G ⊕ A ⊗ H \begin{aligned}
\text{associativity:}\quad&(A\otimes B)\otimes C=A\otimes(B\otimes C) \qquad(A\otimes B)\otimes G=A\otimes(B\otimes G)\\
\text{distributivity:}\quad&(A\oplus B)\otimes G=A\otimes G\oplus B\otimes G \qquad A\otimes(G\oplus H)=A\otimes G\oplus A\otimes H
\end{aligned} associativity: distributivity: ( A ⊗ B ) ⊗ C = A ⊗ ( B ⊗ C ) ( A ⊗ B ) ⊗ G = A ⊗ ( B ⊗ G ) ( A ⊕ B ) ⊗ G = A ⊗ G ⊕ B ⊗ G A ⊗ ( G ⊕ H ) = A ⊗ G ⊕ A ⊗ H If A A A and B B B are time-invariant, we additionally have commutativity A ⊗ B = B ⊗ A A\otimes B=B\otimes A A ⊗ B = B ⊗ A .
Convolution is associative across both kernel-kernel and kernel-signal products, and it distributes over addition on both sides. Convolution is in general not commutative, because the order in which a tap is applied determines the time index at which its coefficient is read; when both kernels are time-invariant, however, convolution becomes commutative.
In fact, kernels form a semiring, and the time-invariant kernels form a commutative sub-semiring.
Proposition 3.2. Let K \mathcal{K} K be the set of all kernels. ( K , ⊕ , ⊗ , 0 , I ) (\mathcal{K},\oplus,\otimes,0,I) ( K , ⊕ , ⊗ , 0 , I ) is a semiring. Additionally, let K T I \mathcal{K}_{\mathrm{TI}} K TI be the set of time-invariant kernels. Then ( K T I , ⊕ , ⊗ , 0 , I ) (\mathcal{K}_{\mathrm{TI}},\oplus,\otimes,0,I) ( K TI , ⊕ , ⊗ , 0 , I ) is a commutative semiring.
Kernel-signal convolution connects kernels with signals, and it is naturally compatible with the kernel semiring. Indeed, the signals form a semimodule over the kernel semiring.
Proposition 3.3. Let S S S be the set of all signals. ( S , 0 τ , ⊕ , ⊗ ) (S, 0_{\tau}, \oplus, \otimes) ( S , 0 τ , ⊕ , ⊗ ) forms a semimodule over the semiring ( K , ⊕ , ⊗ , 0 , I ) (K, \oplus, \otimes, 0, I) ( K , ⊕ , ⊗ , 0 , I ) , and it forms a commutative semimodule over the semiring ( K T I , ⊕ , ⊗ , 0 , I ) (K_{TI}, \oplus, \otimes, 0, I) ( K T I , ⊕ , ⊗ , 0 , I ) .
To summarize, semirings and semimodules capture the algebraic structure of kernels and signals. The following example demonstrates the algebraic properties.
Example 6 (Convolving with time-invariant kernels). Consider again the 4-tap moving sum F = K ⊗ G F = K \otimes G F = K ⊗ G with K = [ 1 , 1 , 1 , 1 ] K = [1, 1, 1, 1] K = [ 1 , 1 , 1 , 1 ] . We can factor the kernel K K K into A = [ 1 , 0 , 1 ] A = [1, 0, 1] A = [ 1 , 0 , 1 ] and B = [ 1 , 1 ] B = [1, 1] B = [ 1 , 1 ] , and K = A ⊗ B K = A \otimes B K = A ⊗ B . By associativity, we have F = K ⊗ G = ( A ⊗ B ) ⊗ G = A ⊗ ( B ⊗ G ) F = K \otimes G = (A \otimes B) \otimes G = A \otimes(B \otimes G) F = K ⊗ G = ( A ⊗ B ) ⊗ G = A ⊗ ( B ⊗ G ) . Additionally, because A A A and B B B are time-invariant, we also have F = ( B ⊗ A ) ⊗ G = B ⊗ ( A ⊗ G ) F = (B \otimes A) \otimes G = B \otimes(A \otimes G) F = ( B ⊗ A ) ⊗ G = B ⊗ ( A ⊗ G ) by commutativity. They correspond to different implementations of the moving sum. For instance, A ⊗ ( B ⊗ G ) A \otimes(B \otimes G) A ⊗ ( B ⊗ G ) corresponds to a cascade of two filters: the inner filter B ⊗ G B \otimes G B ⊗ G first computes the pointwise sums h ( x ) = g ( x ) + g ( x − 1 ) h(x) = g(x) + g(x-1) h ( x ) = g ( x ) + g ( x − 1 ) , and the outer filter A A A then adds pairs of h h h that are two elements apart, i.e., f ( x ) = h ( x ) + h ( x − 2 ) f(x) = h(x) + h(x-2) f ( x ) = h ( x ) + h ( x − 2 ) .
A one-dimensional time-invariant kernel corresponds to a polynomial: A = [ a 0 , … , a n ] A = [a_0, \ldots, a_n] A = [ a 0 , … , a n ] is the coefficient list of p A ( x ) = ∑ i a i x i p_A(x) = \sum_i a_i x^i p A ( x ) = ∑ i a i x i , and convolution of time-invariant kernels is multiplication of their polynomials. By the fundamental theorem of algebra, any real polynomial factors into real factors of degree at most two, which bounds the order of a kernel’s factors.
Lemma 3.4. Under the arithmetic ring over real numbers, any one-dimensional time-invariant kernel A A A of order n n n can be factored as A = A 1 ⊗ … ⊗ A k A = A_1 \otimes\ldots\otimes A_k A = A 1 ⊗ … ⊗ A k where each A i A_i A i has order less than or equal to 2.
See Appendix A for the proof. Signal processing often uses this factoring to trade off numerical stability and performance in filter design. For example, the moving-sum kernel [ 1 , 1 , 1 , 1 ] [1, 1, 1, 1] [ 1 , 1 , 1 , 1 ] is the polynomial 1 + x + x 2 + x 3 = ( 1 + x 2 ) ( 1 + x ) 1 + x + x^2 + x^3 = (1 + x^2)(1 + x) 1 + x + x 2 + x 3 = ( 1 + x 2 ) ( 1 + x ) , which is exactly the factorization [ 1 , 0 , 1 ] ⊗ [ 1 , 1 ] [1, 0, 1] \otimes[1, 1] [ 1 , 0 , 1 ] ⊗ [ 1 , 1 ] above.
The following example shows why convolutions in general do not commute.
Example 7 (Convolving with time-varying kernels). Consider again time-varying kernels A = [ S 1 , S 2 ] A = [S_1, S_2] A = [ S 1 , S 2 ] and B = [ S 3 , 0 τ , S 4 ] B = [S_3, 0_{\tau}, S_4] B = [ S 3 , 0 τ , S 4 ] . Let F = A ⊗ ( B ⊗ G ) F = A \otimes(B \otimes G) F = A ⊗ ( B ⊗ G ) . By associativity, F = ( A ⊗ B ) ⊗ G F = (A \otimes B) \otimes G F = ( A ⊗ B ) ⊗ G . Meanwhile, ( B ⊗ A ) ⊗ G (B \otimes A) \otimes G ( B ⊗ A ) ⊗ G computes a different signal.
B ⊗ A = [ S 3 ⊙ S 1 , S 3 ⊙ S 2 , S 4 ⊙ ( z − 2 ⊗ S 1 ) , S 4 ⊙ ( z − 2 ⊗ S 2 ) ] ≠ A ⊗ B B \otimes A = [S_3 \odot S_1,\ S_3 \odot S_2,\ S_4 \odot(z^{-2} \otimes S_1),\ S_4 \odot(z^{-2} \otimes S_2)] \ne A \otimes B B ⊗ A = [ S 3 ⊙ S 1 , S 3 ⊙ S 2 , S 4 ⊙ ( z − 2 ⊗ S 1 ) , S 4 ⊙ ( z − 2 ⊗ S 2 )] = A ⊗ B To confirm this in the explicit indexed form, let S 1 , S 2 , S 3 , S 4 S_1, S_2, S_3, S_4 S 1 , S 2 , S 3 , S 4 denote [ S 1 ] σ [S_1]^\sigma [ S 1 ] σ , [ S 2 ] σ [S_2]^\sigma [ S 2 ] σ , [ S 3 ] σ [S_3]^\sigma [ S 3 ] σ , [ S 4 ] σ [S_4]^\sigma [ S 4 ] σ .
h ( n ) = s 3 ( n ) g ( n ) + s 4 ( n ) g ( n − 2 ) h ′ ( n ) = s 1 ( n ) g ( n ) + s 2 ( n ) g ( n − 1 ) f ( n ) = s 1 ( n ) h ( n ) + s 2 ( n ) h ( n − 1 ) f ′ ( n ) = s 3 ( n ) h ′ ( n ) + s 4 ( n ) h ′ ( n − 2 ) = s 1 ( n ) s 3 ( n ) ⋅ g ( n ) + s 2 ( n ) s 3 ( n − 1 ) ⋅ g ( n − 1 ) = s 3 ( n ) s 1 ( n ) ⋅ g ( n ) + s 3 ( n ) s 2 ( n ) ⋅ g ( n − 1 ) + s 1 ( n ) s 4 ( n ) ⋅ g ( n − 2 ) + s 2 ( n ) s 4 ( n − 1 ) ⋅ g ( n − 3 ) + s 4 ( n ) s 1 ( n − 2 ) ⋅ g ( n − 2 ) + s 4 ( n ) s 2 ( n − 2 ) ⋅ g ( n − 3 ) . \begin{aligned}
h(n) &= s_3(n)g(n) + s_4(n)g(n-2) & h'(n) &= s_1(n)g(n) + s_2(n)g(n-1) \\
f(n) &= s_1(n)h(n) + s_2(n)h(n-1) & f'(n) &= s_3(n)h'(n) + s_4(n)h'(n-2) \\
&= s_1(n)s_3(n)\cdot g(n) + s_2(n)s_3(n-1)\cdot g(n-1) & &= s_3(n)s_1(n)\cdot g(n) + s_3(n)s_2(n)\cdot g(n-1) \\
&\quad+ s_1(n)s_4(n)\cdot g(n-2) + s_2(n)s_4(n-1)\cdot g(n-3) & &\quad+ s_4(n)s_1(n-2)\cdot g(n-2) + s_4(n)s_2(n-2)\cdot g(n-3).
\end{aligned} h ( n ) f ( n ) = s 3 ( n ) g ( n ) + s 4 ( n ) g ( n − 2 ) = s 1 ( n ) h ( n ) + s 2 ( n ) h ( n − 1 ) = s 1 ( n ) s 3 ( n ) ⋅ g ( n ) + s 2 ( n ) s 3 ( n − 1 ) ⋅ g ( n − 1 ) + s 1 ( n ) s 4 ( n ) ⋅ g ( n − 2 ) + s 2 ( n ) s 4 ( n − 1 ) ⋅ g ( n − 3 ) h ′ ( n ) f ′ ( n ) = s 1 ( n ) g ( n ) + s 2 ( n ) g ( n − 1 ) = s 3 ( n ) h ′ ( n ) + s 4 ( n ) h ′ ( n − 2 ) = s 3 ( n ) s 1 ( n ) ⋅ g ( n ) + s 3 ( n ) s 2 ( n ) ⋅ g ( n − 1 ) + s 4 ( n ) s 1 ( n − 2 ) ⋅ g ( n − 2 ) + s 4 ( n ) s 2 ( n − 2 ) ⋅ g ( n − 3 ) . The functions f f f and f ′ f' f ′ match the definitions of F = ( A ⊗ B ) ⊗ G F = (A \otimes B) \otimes G F = ( A ⊗ B ) ⊗ G and F ′ = ( B ⊗ A ) ⊗ G F' = (B \otimes A) \otimes G F ′ = ( B ⊗ A ) ⊗ G , and they are not equivalent.
To summarize, this section presents the syntax and semantics of the recurrence algebra together with the properties of ⊗ \otimes ⊗ and ⊕ \oplus ⊕ . Next, we turn to the recurrence operator.
The recurrence operator
The recurrence operator R e c Rec R ec reifies the fixed point computation of recurrences into the language of kernels and signals. We first show that the semantics of R e c ( A , G ) Rec(A, G) R ec ( A , G ) is well defined and is equivalent to finding an F F F that satisfies F = A ⊗ F ⊕ G F = A \otimes F \oplus G F = A ⊗ F ⊕ G .
Lemma 4.1 (Equivalent definitions of R e c Rec R ec ). Suppose A A A is causal. ⨁ i ≥ 0 A i ⊗ G \bigoplus_{i\ge0} A^i \otimes G ⨁ i ≥ 0 A i ⊗ G is well-defined, and F = ⨁ i ≥ 0 A i ⊗ G F=\bigoplus_{i\ge0} A^i \otimes G F = ⨁ i ≥ 0 A i ⊗ G iff F = A ⊗ F ⊕ G F=A\otimes F\oplus G F = A ⊗ F ⊕ G .
In the rest of the section, we present the algebraic identities of the R e c Rec R ec operator. We start with identities that hold for any semiring and then specialize to rings and idempotent semirings. Some of these identities directly enable vectorization, while others capture and give insight into transformations that practitioners use. Figure 5 summarizes these identities. We comment on their contexts and implications when applicable. The proofs are algebraic and deferred to the appendix.
Figure 5. Recurrence identities grouped by the class of semirings. “t.i.” abbreviates time-invariant.
Identities for recurrences over any semiring
We start with identities that hold over any semiring. First, we can unroll a recurrence.
Lemma 4.2 (Unrolling). For kernel A A A and signal G G G over any semiring,
R e c ( A , G ) = R e c ( A k , ⨁ i = 0 k − 1 A i ⊗ G ) . Rec(A,G)=Rec\left(A^k,\bigoplus_{i=0}^{k-1}A^i\otimes G\right). R ec ( A , G ) = R ec ( A k , i = 0 ⨁ k − 1 A i ⊗ G ) . Intuitively, instead of advancing the recurrence one step at a time with A A A , we may take k k k steps at once using the unrolled kernel A k A^k A k , after pre-accumulating the contributions of the k k k intermediate steps, ⨁ i = 0 k − 1 A i ⊗ G \bigoplus_{i=0}^{k-1}A^i\otimes G ⨁ i = 0 k − 1 A i ⊗ G , into the input.
The next two lemmas concern the commutativity of recurrences. Recall that time-invariant convolutions commute with each other. Indeed, time-invariant convolutions also commute with time-invariant recurrences, and time-invariant recurrences commute with each other.
Lemma 4.3 (Commutativity of recurrences). For time-invariant kernels A A A and B B B and signal G G G over any semiring,
R e c ( A , B ⊗ G ) = B ⊗ R e c ( A , G ) . Rec(A,B\otimes G)=B\otimes Rec(A,G). R ec ( A , B ⊗ G ) = B ⊗ R ec ( A , G ) . R e c ( A , R e c ( B , G ) ) = R e c ( B , R e c ( A , G ) ) . Rec(A,Rec(B,G))=Rec(B,Rec(A,G)). R ec ( A , R ec ( B , G )) = R ec ( B , R ec ( A , G )) . These identities are helpful when implementing cascades of convolutions and recurrences.
Example 8. In image processing, repeated applications of box filters [8 ] are often used to approximate Gaussian filters. A box filter can be efficiently implemented as a cascade of a recurrence and a convolution: Box ( G ) = B ⊗ Rec ( A , G ) \operatorname{Box}(G) = B \otimes\operatorname{Rec}(A,G) Box ( G ) = B ⊗ Rec ( A , G ) , where the 2D kernel A A A computes the summed area table of the input image, and the 2D kernel B B B computes the difference of the summed area table. Therefore, a Gaussian filter can be approximated as Gauss ( G ) = B ⊗ Rec ( A , B ⊗ Rec ( A , B ⊗ Rec ( A , G ) ) ) \operatorname{Gauss}(G) = B \otimes\operatorname{Rec}(A, B \otimes\operatorname{Rec}(A, B \otimes\operatorname{Rec}(A,G))) Gauss ( G ) = B ⊗ Rec ( A , B ⊗ Rec ( A , B ⊗ Rec ( A , G ))) . Using commutativity, we can freely reorder the recurrences and convolutions in the cascade. For instance, we can fuse the convolutions together and derive Gauss ( G ) = B ⊗ B ⊗ B ⊗ Rec ( A , Rec ( A , Rec ( A , G ) ) ) \operatorname{Gauss}(G) = B \otimes B \otimes B \otimes\operatorname{Rec}(A, \operatorname{Rec}(A, \operatorname{Rec}(A,G))) Gauss ( G ) = B ⊗ B ⊗ B ⊗ Rec ( A , Rec ( A , Rec ( A , G ))) .
Identities for recurrences over a ring
This section presents the identities that hold for recurrences over a ring.
Lemma 4.4 (Inverse of Recurrence). For kernel A A A and signal G G G over a ring,
( I ⊖ A ) ⊗ Rec ( A , G ) = G . (I \ominus A) \otimes\operatorname{Rec}(A,G) = G. ( I ⊖ A ) ⊗ Rec ( A , G ) = G . The above lemma shows that when restricted to rings, convolution by I ⊖ A I \ominus A I ⊖ A is precisely the inverse to the recurrence Rec ( A , ⋅ ) \operatorname{Rec}(A,\cdot) Rec ( A , ⋅ ) . As an analogy, we can view Rec ( A , G ) \operatorname{Rec}(A,G) Rec ( A , G ) as algebraically similar to computing the geometric series ∑ i ≥ 0 a r i \sum_{i \ge0} ar^i ∑ i ≥ 0 a r i , and it holds that ( 1 − r ) ⋅ ∑ i ≥ 0 a r i = a (1-r) \cdot\sum_{i \ge0} ar^i = a ( 1 − r ) ⋅ ∑ i ≥ 0 a r i = a when the series converges. Similarly, applying I ⊖ A I \ominus A I ⊖ A undoes the recurrence and recovers the original input.
The next identity flattens a stack of ring recurrences into a single recurrence with a larger kernel.
Lemma 4.5 (Fusion of Stacked Ring Recurrences). For kernels A A A and B B B and signal G G G over a ring,
Rec ( A , Rec ( B , G ) ) = Rec ( A ⊕ B ⊖ B ⊗ A , G ) . \operatorname{Rec}(A,\operatorname{Rec}(B,G)) = \operatorname{Rec}(A \oplus B \ominus B \otimes A, G). Rec ( A , Rec ( B , G )) = Rec ( A ⊕ B ⊖ B ⊗ A , G ) . Sometimes we apply this identity in the opposite direction, decomposing a single recurrence into a stack of recurrences with smaller kernels.
Example 9. In the repeated box filter example, the recurrence kernel A = [ 0 1 1 − 1 ] A = \begin{bmatrix} 0 & 1 \\ 1 & -1 \end{bmatrix} A = [ 0 1 1 − 1 ] can be decomposed into a horizontal kernel A x = [ 0 , 1 ] A_x = [0,1] A x = [ 0 , 1 ] and a vertical kernel A y = [ 0 1 ] A_y = \begin{bmatrix} 0 \\ 1 \end{bmatrix} A y = [ 0 1 ] using Lemma 4.5. Therefore, the implementation space of the repeated box filter consists of permutations of repeated convolutions with B B B and repeated recurrences with A x A_x A x and A y A_y A y .
As a small digression, by the fundamental theorem of algebra, we can always decompose a one-dimensional time-invariant recurrence over the reals.
Lemma 4.6. Under the arithmetic ring over real numbers, the recurrence Rec ( A , G ) \operatorname{Rec}(A,G) Rec ( A , G ) for which A A A is a 1D time-invariant kernel of order n n n can be decomposed into a stack of recurrences Rec ( A 1 , Rec ( A 2 , … Rec ( A k , G ) ) ) \operatorname{Rec}(A_1, \operatorname{Rec}(A_2, \ldots\operatorname{Rec}(A_k,G))) Rec ( A 1 , Rec ( A 2 , … Rec ( A k , G ))) where each A i A_i A i has order at most 2.
For time-invariant kernels, we have a similar identity for fusing a parallel sum of recurrences.
Lemma 4.7 (Fusion of Parallel Ring Recurrences). For time-invariant kernels A A A and B B B and signals G G G and H H H over a ring,
Rec ( A , G ) ⊕ Rec ( B , H ) = Rec ( A ⊕ B ⊖ A ⊗ B , ( I ⊖ B ) ⊗ G ⊕ ( I ⊖ A ) ⊗ H ) . \operatorname{Rec}(A,G) \oplus\operatorname{Rec}(B,H) = \operatorname{Rec}(A \oplus B \ominus A \otimes B, (I \ominus B) \otimes G \oplus(I \ominus A) \otimes H). Rec ( A , G ) ⊕ Rec ( B , H ) = Rec ( A ⊕ B ⊖ A ⊗ B , ( I ⊖ B ) ⊗ G ⊕ ( I ⊖ A ) ⊗ H ) . Applying the above lemma in the opposite direction decomposes a time-invariant recurrence into a parallel sum of recurrences. This is known as partial fraction expansion. Partial fraction expansion can help parallelize a high-order recurrence by decomposing it into simpler independent recurrences. Practitioners use the fusion and decomposition of stacked and parallel recurrences to trade off performance, parallelism, and numerical stability in digital filter design. The next identity is the foundation of dilation for time-invariant ring recurrences.
Lemma 4.8 (Dilation with Additive Inverse). For time-invariant kernels A A A and B B B and signal G G G over a ring,
Rec ( A ⊕ B , G ) = Rec ( A ⊕ A ⊕ A 2 ⊕ B 2 , ( I ⊖ A ⊕ B ) ⊗ G ) . \operatorname{Rec}(A \oplus B,G) = \operatorname{Rec}(A \oplus A \oplus A^{2} \oplus B^{2},\ (I \ominus A \oplus B) \otimes G). Rec ( A ⊕ B , G ) = Rec ( A ⊕ A ⊕ A 2 ⊕ B 2 , ( I ⊖ A ⊕ B ) ⊗ G ) . This identity proves the transformation in the motivating example in § 2.1 by treating A A A and B B B as the even and odd splits of the original kernel.
The next two identities show how to decompose a recurrence with A 2 A^{2} A 2 and how to change the kernel used in a recurrence; both follow from the identities above.
Lemma 4.9 (Decomposition of Squared Kernels). For kernel A A A and signal G G G over a ring,
Rec ( A 2 , G ) = Rec ( A , Rec ( ⊖ A , G ) ) . \operatorname{Rec}(A^{2},G) = \operatorname{Rec}(A,\operatorname{Rec}(\ominus A,G)). Rec ( A 2 , G ) = Rec ( A , Rec ( ⊖ A , G )) .
Lemma 4.10 (Change of Basis). For kernels A A A and B B B and signal G G G over a ring,
Rec ( A , G ) = Rec ( A ⊖ B ⊗ ( I ⊖ A ) , ( I ⊕ B ) ⊗ G ) \operatorname{Rec}(A,G) = \operatorname{Rec}(A \ominus B \otimes(I \ominus A),\ (I \oplus B) \otimes G) Rec ( A , G ) = Rec ( A ⊖ B ⊗ ( I ⊖ A ) , ( I ⊕ B ) ⊗ G )
Identities for recurrences over an idempotent semiring
The idempotence of addition, a ⊕ τ a = a a \oplus_{\tau} a = a a ⊕ τ a = a , enables a different set of identities. The identity below dilates recurrences over idempotent semirings.
Lemma 4.11 (Dilation with Idempotence). For time-invariant kernels A A A and B B B and signal G G G over an idempotent semiring,
Rec ( A ⊕ B , G ) = Rec ( A ⊕ B 2 , ( I ⊕ B ) ⊗ G ) . \operatorname{Rec}(A \oplus B,G) = \operatorname{Rec}(A \oplus B^{2},\ (I \oplus B) \otimes G). Rec ( A ⊕ B , G ) = Rec ( A ⊕ B 2 , ( I ⊕ B ) ⊗ G ) .
This identity allows us to break a kernel into a sum of sub-kernels and dilate them independently.
Example 10. Consider distance transform F = Rec ( [ ∞ ∞ 1 2 ] , G ) F = \operatorname{Rec}\left(\begin{bmatrix} \infty& \infty\\ 1 & \sqrt{2} \end{bmatrix},G\right) F = Rec ( [ ∞ 1 ∞ 2 ] , G ) over the (min, +)-semiring. Let
A = [ ∞ ∞ 1 2 ] A = \begin{bmatrix} \infty& \infty\\ 1 & \sqrt{2} \end{bmatrix} A = [ ∞ 1 ∞ 2 ] and
B = [ ∞ ∞ 0 1 ] , B = \begin{bmatrix} \infty& \infty\\ 0 & 1 \end{bmatrix}, B = [ ∞ 0 ∞ 1 ] , the dilation identity gives us
F = Rec ( [ ∞ ∞ 2 1 2 ∞ ] , [ 0 1 ∞ ∞ ] ⊗ G ) . F = \operatorname{Rec}\left(\begin{bmatrix} \infty& \infty& 2 \\ 1 & \sqrt{2} & \infty\end{bmatrix},\begin{bmatrix} 0 & 1 \\ \infty& \infty\end{bmatrix} \otimes G\right). F = Rec ( [ ∞ 1 ∞ 2 2 ∞ ] , [ 0 ∞ 1 ∞ ] ⊗ G ) . Applying the identity again by setting B B B to be the two other taps of the original kernel, we get
F = Rec ( [ ∞ ∞ 2 ∞ ∞ ∞ 2 ∞ ∞ ] , [ 0 ∞ ∞ 2 ] ⊗ [ 0 ∞ 1 ∞ ] ⊗ [ 0 1 ∞ ∞ ] ⊗ G ) . F = \operatorname{Rec}\left(\begin{bmatrix} \infty& \infty& 2 \\ \infty& \infty& \infty\\ 2 & \infty& \infty\end{bmatrix},\begin{bmatrix} 0 & \infty\\ \infty& \sqrt{2} \end{bmatrix} \otimes\begin{bmatrix} 0 & \infty\\ 1 & \infty\end{bmatrix} \otimes\begin{bmatrix} 0 & 1 \\ \infty& \infty\end{bmatrix} \otimes G\right). F = Rec ∞ ∞ 2 ∞ ∞ ∞ 2 ∞ ∞ , [ 0 ∞ ∞ 2 ] ⊗ [ 0 1 ∞ ∞ ] ⊗ [ 0 ∞ 1 ∞ ] ⊗ G . This computes two consecutive elements (i.e., f ( x , y ) f(x,y) f ( x , y ) and f ( x , y + 1 ) f(x,y+1) f ( x , y + 1 ) ) at a time. Moreover, because the new f f f no longer depends on itself from the previous row, two rows can be computed in parallel with multi-cores. By choosing which tap to dilate, we obtain the desired shape of parallelism. For instance, dilating the tap at the zeroth row again computes four horizontal elements at a time with vectorization, while leaving the multi-core parallelism unchanged. □
We can also fuse stacked recurrences by simply summing the kernels in the idempotent semirings.
Lemma 4.12 (Fusion of Idempotent Recurrences). For time-invariant kernels A A A and B B B and signal G G G over an idempotent semiring,
Rec ( A , Rec ( B , G ) ) = Rec ( A ⊕ B , G ) \operatorname{Rec}(A,\operatorname{Rec}(B,G)) = \operatorname{Rec}(A \oplus B,G) Rec ( A , Rec ( B , G )) = Rec ( A ⊕ B , G )
Compare this with fusion of stacked recurrence over a ring. In general, a stack of recurrences can double count the contribution of an algebraic path. For rings, this is corrected using subtraction, whereas for idempotent rings, double counting does not change the result.
Techniques for Vectorizing Recurrences
We now present techniques for vectorizing different types of recurrences. This section focuses on 1D recurrences and defers higher-dimensional recurrences to § 5.4 . We present four techniques: dilation for ring recurrences, dilation for idempotent recurrences, fast associative scan, and path-based decomposition. The first two follow from the algebraic identities of the previous section, and they apply only when the kernel is time-invariant and the semiring satisfies certain properties. Fast associative scan builds on a general transformation for prefix scans over associative operations [2 , 17 ] . Path-based decomposition views convolution as aggregating over algebraic paths, and decomposes those paths by divide-and-conquer to expose vectorizable work. Unlike dilations, the latter two techniques apply to any type of recurrence, at the cost of smaller speedup.
Some of these techniques are not new. They have appeared in various forms in the literature, for example in multi-core parallelism or digital signal processing, usually within a specific domain. In relation to these works, our contribution is twofold: (1) we show that these techniques apply to SIMD vectorization, and (2) we distill them into an algebraic framework and, in some cases, generalize them. § 9 discusses related work and our extensions to it.
Dilations for Time-invariant Recurrences
We present two dilation techniques, one for recurrences over rings and the other over idempotent semirings. Both require the recurrence to be time-invariant, because the underlying identities rely on commutativity. We call these techniques dilation because both transform a recurrence into another that doubles the strides between, or “dilates”, the taps of the kernel. Formally, we define the stride s s s of a 1D kernel K K K as the largest integer such that K [ i ] K[i] K [ i ] evaluates to the constant signal 0 τ \mathbf{0}_{\tau} 0 τ for all i i i not divisible by s s s . For instance, the stride of kernel [ 0 , a , b ] [0, a, b] [ 0 , a , b ] is 1, and the stride of kernel [ 0 , 0 , a , 0 , b ] [0, 0, a, 0, b] [ 0 , 0 , a , 0 , b ] is 2.
Ring recurrences. We use the identity in Lemma 4.8 to dilate ring recurrences. Recall that Rec ( A ⊕ B , G ) = Rec ( A ⊕ A ⊖ A 2 ⊕ B 2 , ( I ⊖ A ⊕ B ) ⊗ G ) \operatorname{Rec}(A \oplus B, G) = \operatorname{Rec}(A \oplus A \ominus A^2 \oplus B^2, (I \ominus A \oplus B) \otimes G) Rec ( A ⊕ B , G ) = Rec ( A ⊕ A ⊖ A 2 ⊕ B 2 , ( I ⊖ A ⊕ B ) ⊗ G ) for time-invariant kernels A A A and B B B . Let K K K be a kernel with stride s s s and F = Rec ( K , G ) F = \operatorname{Rec}(K, G) F = Rec ( K , G ) . Define A A A as the kernel that only contains the taps of K K K at multiples of 2 s 2s 2 s , and B B B as the kernel that only contains the taps of K K K at indices that are s s s modulo 2 s 2s 2 s . We have K = A ⊕ B K = A \oplus B K = A ⊕ B . Applying the dilation identity yields F = Rec ( A ⊕ A ⊖ A 2 ⊕ B 2 , ( I ⊖ A ⊕ B ) ⊗ G ) F = \operatorname{Rec}(A \oplus A \ominus A^2 \oplus B^2, (I \ominus A \oplus B) \otimes G) F = Rec ( A ⊕ A ⊖ A 2 ⊕ B 2 , ( I ⊖ A ⊕ B ) ⊗ G ) . We claim that the new kernel A ⊕ A ⊖ A 2 ⊕ B 2 A \oplus A \ominus A^2 \oplus B^2 A ⊕ A ⊖ A 2 ⊕ B 2 has a stride of 2 ⋅ s 2 \cdot s 2 ⋅ s . To see this, first, A A A has a stride of 2 ⋅ s 2 \cdot s 2 ⋅ s , and so does A ⊕ A ⊖ A 2 A \oplus A \ominus A^2 A ⊕ A ⊖ A 2 . Second, suppose B 2 B^2 B 2 has a non-zero tap at index k k k . There must exist k 1 , k 2 k_1, k_2 k 1 , k 2 with k 1 + k 2 = k k_1 + k_2 = k k 1 + k 2 = k such that B [ k 1 ] , B [ k 2 ] B[k_1], B[k_2] B [ k 1 ] , B [ k 2 ] are non-zero. As k 1 m o d 2 s = s k_1 \bmod2s = s k 1 mod 2 s = s and k 2 m o d 2 s = s k_2 \bmod2s = s k 2 mod 2 s = s , it follows that k m o d 2 s = 0 k \bmod2s = 0 k mod 2 s = 0 , so B 2 B^2 B 2 also has a stride of 2 ⋅ s 2 \cdot s 2 ⋅ s . Therefore, the new recurrence kernel A ⊕ A ⊖ A 2 ⊕ B 2 A \oplus A \ominus A^2 \oplus B^2 A ⊕ A ⊖ A 2 ⊕ B 2 has a stride of 2 s 2s 2 s , which doubles the stride of the original kernel K K K .
We can likewise analyze the number of taps in the new term. If the original kernel K K K has taps at indices s , … , k ⋅ s s, \ldots, k \cdot s s , … , k ⋅ s , then the new recurrence kernel A ⊕ A ⊖ A 2 ⊕ B 2 A \oplus A \ominus A^2 \oplus B^2 A ⊕ A ⊖ A 2 ⊕ B 2 also has k k k taps in general, at indices 2 s , … , 2 ⋅ k ⋅ s 2s, \ldots, 2 \cdot k \cdot s 2 s , … , 2 ⋅ k ⋅ s , and the new convolution kernel I ⊕ A ⊕ B I \oplus A \oplus B I ⊕ A ⊕ B has k + 1 k + 1 k + 1 taps.
If we apply dilation n n n times, we obtain a new recurrence of the form F = Rec ( K n + 1 , K n ⊗ ⋯ ⊗ K 1 ⊗ G ) F = \operatorname{Rec}(K_{n+1}, K_n \otimes\cdots\otimes K_1 \otimes G) F = Rec ( K n + 1 , K n ⊗ ⋯ ⊗ K 1 ⊗ G ) where K n + 1 K_{n+1} K n + 1 has k k k taps that are strided by 2 n 2^n 2 n and K i K_i K i has k + 1 k + 1 k + 1 taps for i ≠ n + 1 i \ne n + 1 i = n + 1 . This allows us to compute 2 n 2^n 2 n elements of the recurrence in parallel.
A delay can further aid the dilation scheme. Consider the change of basis Rec ( A , G ) = Rec ( A ⊖ B ⊗ ( I ⊕ A ) , ( I ⊕ B ) ⊗ G ) \operatorname{Rec}(A, G) = \operatorname{Rec}(A \ominus B \otimes(I \oplus A), (I \oplus B) \otimes G) Rec ( A , G ) = Rec ( A ⊖ B ⊗ ( I ⊕ A ) , ( I ⊕ B ) ⊗ G ) again. Let B B B be the singleton kernel containing the first tap of A A A . Then A ⊕ B ⊗ ( I ⊖ A ) A \oplus B \otimes(I \ominus A) A ⊕ B ⊗ ( I ⊖ A ) effectively removes the first tap of A A A while respecting the stride of A A A . In other words, the new recurrence kernel keeps the same number of taps and the same stride, but widens the vectorizable work from s s s to 2 ⋅ s 2 \cdot s 2 ⋅ s , as dilation does. Unlike dilation, however, the new convolution kernel I ⊕ B I \oplus B I ⊕ B has only two taps.
Example 11. Consider the recurrence F = R e c ( A , G ) F = Rec(A, G) F = R ec ( A , G ) , where A = [ 0 , 0 , a , 0 , b , 0 , c ] A = [0, 0, a, 0, b, 0, c] A = [ 0 , 0 , a , 0 , b , 0 , c ] . Let B = [ 0 , 0 , a ] B = [0, 0, a] B = [ 0 , 0 , a ] . Applying the change of basis, the recurrent recurrence kernel is A ∅ B = [ 0 , 0 , 0 , b + a 2 , 0 , c + a b , 0 , a c ] A \mathbin{\varnothing} B = [0, 0, 0, b + a^{2}, 0, c + ab, 0, ac] A ∅ B = [ 0 , 0 , 0 , b + a 2 , 0 , c + ab , 0 , a c ] , and the incurred convolution kernel is I ∅ B = [ 1 , 0 , a ] I \mathbin{\varnothing} B = [1, 0, a] I ∅ B = [ 1 , 0 , a ] . This lets us compute four values of F F F at a time, at the cost of an additional two-tap convolution. Dilating this kernel instead would yield a convolution with I ∅ A ∅ B I \mathbin{\varnothing} A \mathbin{\varnothing} B I ∅ A ∅ B , which has three taps. □
Unlike dilation, applying delaying again does not double the vectorizable work, so it is best used as a replacement for the last step of dilation, saving taps in the convolution. In practice, the work saved by delaying may be offset by the less regular vector-shuffling patterns, so our compiler tries both dilation and delaying as the last step and picks the faster implementation based on profiling.
Idempotent recurrences. To dilate time-invariant idempotent recurrences, we use Lemma 4.11 , which states that R e c ( A ∅ B , G ) = R e c ( A ∅ B 2 , ( I ∅ B ) ⊗ G ) Rec(A \mathbin{\varnothing} B, G) = Rec(A \mathbin{\varnothing} B^{2}, (I \mathbin{\varnothing} B) \otimes G) R ec ( A ∅ B , G ) = R ec ( A ∅ B 2 , ( I ∅ B ) ⊗ G ) for time-invariant kernels A A A and B B B . Generalizing this identity, we have R e c ( A ∅ ⨁ i = 1 n B i , G ) = R e c ( A ∅ ⨁ i = 1 n B i 2 , ⨂ i = 1 n ( I ∅ B i ) ⊗ G ) Rec(A \mathbin{\varnothing} \bigoplus_{i=1}^{n} B_i, G) = Rec(A \mathbin{\varnothing} \bigoplus_{i=1}^{n} B_i^{2}, \bigotimes_{i=1}^{n}(I \mathbin{\varnothing} B_i) \otimes G) R ec ( A ∅ ⨁ i = 1 n B i , G ) = R ec ( A ∅ ⨁ i = 1 n B i 2 , ⨂ i = 1 n ( I ∅ B i ) ⊗ G ) . This identity lets us extract a set of singleton kernels B i B_i B i from a kernel and dilate them independently.
Unlike the dilation for ring recurrences, which works only in the 1D case, the dilation for idempotent recurrences extends naturally to 2D. For example, Example 10 showed how dilation can expose opportunities for multi-core parallelism across rows. Our compiler currently focuses only on vectorization and does not exploit such opportunities.
General Recurrences
We now describe techniques for vectorizing general recurrences. Unlike the previous section, we assume neither time invariance nor any additional property of the semiring. The two techniques below vectorize time-varying recurrences as well as time-invariant recurrences over semirings that lack additive inverses or are not idempotent. Neither technique dominates the other, and the best choice depends on the target architecture. For example, in our experience, Apple Silicon tends to prefer fast associative scan while AMD Zen family tends to prefer path decomposition.
Fast Associative Scan
Kogge and Stone [17 ] first observed that an m m m -th order linear, time-invariant recurrence can be computed in parallel in logarithmic steps. The key idea is a general transformation that parallelizes prefix scans of arbitrary associative operators [2 ] ; we show that it can also be used to vectorize recurrence relations. To illustrate, let F = R e c ( K , G ) F = Rec(K, G) F = R ec ( K , G ) where K K K is a k k k -tap kernel [ σ τ , S 1 , … , S k ] [\sigma_{\tau}, S_1, \ldots, S_k] [ σ τ , S 1 , … , S k ] . Let us define @ \mathbin{@} @ as the matrix-vector and matrix-matrix multiply using ⊙ \odot ⊙ , i.e., ( M @ N ) i = ⨁ k M i , k ⊙ N k (\mathcal{M} \mathbin{@} N)_i = \bigoplus_k \mathcal{M}_{i,k} \odot N_k ( M @ N ) i = ⨁ k M i , k ⊙ N k , and ( M @ V ) i = ⨁ k M i , k ⊙ V k (\mathcal{M} \mathbin{@} \mathcal{V})_i = \bigoplus_k \mathcal{M}_{i,k} \odot\mathcal{V}_k ( M @ V ) i = ⨁ k M i , k ⊙ V k . One step of the recurrence computation can be modeled as multiplication by a ( k + 1 ) × ( k + 1 ) (k+1) \times(k+1) ( k + 1 ) × ( k + 1 ) matrix:
[ F z − 1 ⊗ F ⋮ z − k + 1 ⊗ F 1 τ ] = [ S 1 S 2 ⋯ S k G 1 τ 0 τ ⋯ 0 τ 0 τ 0 τ 1 τ ⋯ 0 τ 0 τ ⋮ ⋮ ⋱ ⋮ ⋮ 0 τ ⋯ 1 τ 0 τ 0 τ 0 τ ⋯ 0 τ 1 τ 1 τ ] @ [ z − 1 ⊗ F z − 2 ⊗ F ⋮ z − k ⊗ F 1 τ ] \begin{bmatrix}
F \\
z^{-1} \otimes F \\
\vdots\\
z^{-k+1} \otimes F \\
\mathbf{1}_{\tau}
\end{bmatrix}
=
\begin{bmatrix}
S_1 & S_2 & \cdots& S_k & G \\
\mathbf{1}_{\tau} & \mathbf{0}_{\tau} & \cdots& \mathbf{0}_{\tau} & \mathbf{0}_{\tau} \\
\mathbf{0}_{\tau} & \mathbf{1}_{\tau} & \cdots& \mathbf{0}_{\tau} & \mathbf{0}_{\tau} \\
\vdots& \vdots& \ddots& \vdots& \vdots\\
\mathbf{0}_{\tau} & \cdots& \mathbf{1}_{\tau} & \mathbf{0}_{\tau} & \mathbf{0}_{\tau} \\
\mathbf{0}_{\tau} & \cdots& \mathbf{0}_{\tau} & \mathbf{1}_{\tau} & \mathbf{1}_{\tau}
\end{bmatrix}
\mathbin{@}
\begin{bmatrix}
z^{-1} \otimes F \\
z^{-2} \otimes F \\
\vdots\\
z^{-k} \otimes F \\
\mathbf{1}_{\tau}
\end{bmatrix} F z − 1 ⊗ F ⋮ z − k + 1 ⊗ F 1 τ = S 1 1 τ 0 τ ⋮ 0 τ 0 τ S 2 0 τ 1 τ ⋮ ⋯ ⋯ ⋯ ⋯ ⋯ ⋱ 1 τ 0 τ S k 0 τ 0 τ ⋮ 0 τ 1 τ G 0 τ 0 τ ⋮ 0 τ 1 τ @ z − 1 ⊗ F z − 2 ⊗ F ⋮ z − k ⊗ F 1 τ
Denote this ( k + 1 ) × ( k + 1 ) (k+1) \times(k+1) ( k + 1 ) × ( k + 1 ) matrix by M \mathcal{M} M . Applying the transformation n n n times, we obtain
[ F , … , z − k + 1 ⊗ F , 1 τ ] T = M @ ( … @ ( M @ [ z − n ⊗ F , … , z − n − k + 1 ⊗ F , 1 τ ] ) … ) . [F,\ldots,z^{-k+1}\otimes F,\mathbf{1}_{\tau}]^{\mathsf{T}}
=
\mathcal{M} \mathbin{@} (\ldots\mathbin{@} (\mathcal{M} \mathbin{@} [z^{-n}\otimes F,\ldots,z^{-n-k+1}\otimes F,\mathbf{1}_{\tau}])\ldots). [ F , … , z − k + 1 ⊗ F , 1 τ ] T = M @ ( … @ ( M @ [ z − n ⊗ F , … , z − n − k + 1 ⊗ F , 1 τ ]) … ) .
The @ \mathbin{@} @ operator is associative, which follows because ⨁ \bigoplus ⨁ distributes over ⊙ \odot ⊙ and ⊙ \odot ⊙ is associative. Hence, the identity above can be written as $[F,\ldots,z^{-k+1}\otimes F,\mathbf{1}_{\tau}]^{\mathsf{T}} = \mathcal{M}^{n} \mathbin{@} [z^{-n}\otimes F,\ldots,z^{-n-k+1}\otimes F , 1 τ T ] F, \mathbf{1}_{\tau}^{\mathsf T}] F , 1 τ T ] , and the repeated @-product M n \mathcal{M}^{n} M n can be computed in log n \log n log n steps using the standard fast exponentiation trick (i.e., M 2 n = M n @ M n \mathcal{M}^{2n} = \mathcal{M}^{n} @ \mathcal{M}^{n} M 2 n = M n @ M n ).
The power of M \mathcal{M} M can be used for vectorization in different ways. For example, on an architecture with l l l vector lanes, a naïve approach computes M l \mathcal{M}^{l} M l in log l \log l log l steps and then computes F F F as the dot product of the first row of M l \mathcal{M}^{l} M l with [ z − l ⊗ F , … , z − l − k + 1 ⊗ F , 1 τ ] [z^{-l} \otimes F,\ldots,z^{-l-k+1} \otimes F,\mathbf{1}_{\tau}] [ z − l ⊗ F , … , z − l − k + 1 ⊗ F , 1 τ ] . Since the nearest self-dependency in the dot product is z − n ⊗ F z^{-n} \otimes F z − n ⊗ F , vector instructions can compute l l l values of F F F at once. In pseudocode, this is expressed as
f [ x : x + l ] = ( ⨁ i = 1 k ( M l ) 1 , i [ x : x + l ] ⊗ τ f [ x − l − i + 1 : x − i + 1 ] ) ⊕ τ ( M l ) 1 , k + 1 [ x : x + l ] . f[x:x+l] = \left(\bigoplus_{i=1}^{k} \left(\mathcal{M}^{l}\right)_{1,i}[x:x+l] \otimes_{\tau} f[x-l-i+1:x-i+1]\right) \oplus_{\tau} \left(\mathcal{M}^{l}\right)_{1,k+1}[x:x+l]. f [ x : x + l ] = ( i = 1 ⨁ k ( M l ) 1 , i [ x : x + l ] ⊗ τ f [ x − l − i + 1 : x − i + 1 ] ) ⊕ τ ( M l ) 1 , k + 1 [ x : x + l ] .
This form, however, uses only the first row of M l \mathcal{M}^{l} M l , even though each row of M l \mathcal{M}^{l} M l can compute F F F at a different index. Ideally, with l l l vector lanes, the power matrix should compute k × l k \times l k × l outputs in parallel4 .
We propose a more efficient scheme that interleaves the output computation. We compute only every k k k -th value of the power matrix M l ⋅ k \mathcal{M}^{l \cdot k} M l ⋅ k , which suffices to produce the full stream of F F F . Specifically, we first de-interleave the stream of the initial tap matrix M \mathcal{M} M into k k k streams, each produced at 1 / k 1/k 1/ k the rate of the original stream. We then fold over the k k k de-interleaved streams to compute M k \mathcal{M}^{k} M k , and use fast exponentiation to compute M 2 k , M 4 k , … \mathcal{M}^{2k}, \mathcal{M}^{4k},\ldots M 2 k , M 4 k , … . All of M k , M 2 k , … \mathcal{M}^{k}, \mathcal{M}^{2k},\ldots M k , M 2 k , … are produced at rate 1 / k 1/k 1/ k , so this does only 1 / k 1/k 1/ k the work of computing the full stream of M l ⋅ k \mathcal{M}^{l \cdot k} M l ⋅ k . Finally, each matrix M l ⋅ k \mathcal{M}^{l \cdot k} M l ⋅ k computes l l l values of F F F in parallel, and we interleave these to produce the stream of output values F F F at the original rate. Figure 6 illustrates this computation scheme. Our compiler uses the WindowMap operator to encapsulate this interleave/de-interleave scheme (§ 6 ).
Figure 6. Vectorization using fast associative scan.
Path-based Decomposition
The second approach to vectorizing general recurrences rests on the following insight: a recurrence F = Rec ( K , G ) F = \operatorname{Rec}(K,G) F = Rec ( K , G ) computes the sums of all algebraic paths formed by the taps of K K K . We can model this computation as a transition graph, in which each tap of the kernel K K K is an edge, or a “jump”, and the values of G G G are the initial weights. Specifically, the k k k -th tap of K K K represents a jump of length k k k and weight [ K [ i ] ] [K[i]] [ K [ i ]] at time n n n .
To formalize this notion, let K 1 , … , K k K_1,\ldots,K_k K 1 , … , K k be the singleton kernels of K K K , each containing one tap of K K K . Define B 0 = 1 B_0 = 1 B 0 = 1 and B n = ⨁ 1 ≤ m ≤ n a 1 + ⋯ + a m = n ∀ i . 1 ≤ a i ≤ k K a 1 ⊗ … ⊗ K a m B_n = \bigoplus_{\substack{1 \le m \le n \\ a_1+\cdots+a_m=n \\ \forall i.\ 1 \le a_i \le k}} K_{a_1} \otimes\ldots\otimes K_{a_m} B n = ⨁ 1 ≤ m ≤ n a 1 + ⋯ + a m = n ∀ i . 1 ≤ a i ≤ k K a 1 ⊗ … ⊗ K a m for n ≥ 1 n \ge1 n ≥ 1 . Each B n B_n B n is a singleton kernel with its tap at index n n n , and intuitively represents the sum of all algebraic paths of length n n n . Define G p G_p G p as the prefix scan of B 0 , … , B p − 1 B_0,\ldots,B_{p-1} B 0 , … , B p − 1 convolved with the input signal G G G , i.e., G p = ⨁ 0 ≤ n < p B n ⊗ G G_p = \bigoplus_{0 \le n < p} B_n \otimes G G p = ⨁ 0 ≤ n < p B n ⊗ G . Intuitively, G p G_p G p aggregates all algebraic paths of length up to p p p , weighting each path by the sum of its weight and the initial weight from G G G .
Example 12. Consider the kernel K = [ 0 , 2 , 3 ] K = [0, 2, 3] K = [ 0 , 2 , 3 ] over the arithmetic ring. The singleton kernels are K 1 = [ 0 , 2 ] K_1 = [0, 2] K 1 = [ 0 , 2 ] and K 2 = [ 0 , 0 , 3 ] K_2 = [0, 0, 3] K 2 = [ 0 , 0 , 3 ] . We have B 0 = [ 1 ] B_0 = [1] B 0 = [ 1 ] , B 1 = K 1 = [ 0 , 2 ] B_1 = K_1 = [0, 2] B 1 = K 1 = [ 0 , 2 ] , B 2 = K 1 ⊗ K 1 ⊕ K 2 = [ 0 , 0 , 3 + 2 × 2 ] = [ 0 , 0 , 7 ] B_2 = K_1 \otimes K_1 \oplus K_2 = [0, 0, 3 + 2 \times2] = [0, 0, 7] B 2 = K 1 ⊗ K 1 ⊕ K 2 = [ 0 , 0 , 3 + 2 × 2 ] = [ 0 , 0 , 7 ] , and B 3 = [ 0 , 0 , 0 , 2 × 3 + 3 × 2 + 2 × 2 × 2 ] = [ 0 , 0 , 0 , 20 ] B_3 = [0, 0, 0, 2 \times3 + 3 \times2 + 2 \times2 \times2] = [0, 0, 0, 20] B 3 = [ 0 , 0 , 0 , 2 × 3 + 3 × 2 + 2 × 2 × 2 ] = [ 0 , 0 , 0 , 20 ] . B 3 B_3 B 3 aggregates the combinations of K K K ’s taps that sum to 3: a two-jump followed by a one-jump (K 1 ⊗ K 2 K_1 \otimes K_2 K 1 ⊗ K 2 ), a one-jump followed by a two-jump (K 2 ⊗ K 1 K_2 \otimes K_1 K 2 ⊗ K 1 ), and three one-jumps (K 1 ⊗ K 1 ⊗ K 1 K_1 \otimes K_1 \otimes K_1 K 1 ⊗ K 1 ⊗ K 1 ).
Let G G G be an input signal [ 1 , − 1 , 1 , − 1 ] [1, -1, 1, -1] [ 1 , − 1 , 1 , − 1 ] . G 4 = ( B 0 ⊕ B 1 ⊕ B 2 ⊕ B 3 ) ⊗ G = [ 1 , 1 , 6 , 14 ] G_4 = (B_0 \oplus B_1 \oplus B_2 \oplus B_3) \otimes G = [1, 1, 6, 14] G 4 = ( B 0 ⊕ B 1 ⊕ B 2 ⊕ B 3 ) ⊗ G = [ 1 , 1 , 6 , 14 ] . The n n n -th value of G 4 G_4 G 4 is the sum of paths ending at n n n . For instance, ∥ G 4 ∥ ( 3 ) = 14 \lVert G_4 \rVert(3) = 14 ∥ G 4 ∥ ( 3 ) = 14 , the sum of 0-paths (1 × − 1 1 \times-1 1 × − 1 ), 1-paths (2 × 1 2 \times1 2 × 1 ), 2-paths (7 × − 1 7 \times-1 7 × − 1 ), and 3-paths (20 × 1 20 \times1 20 × 1 ). □
The following lemma captures the intuition that the recurrence F = R e c ( K , G ) F = Rec(K, G) F = R ec ( K , G ) computes the sum of algebraic paths of every length.
Lemma 5.1. Let K K K be a kernel and G G G be a signal over any semiring. Define F = R e c ( K , G ) F = Rec(K, G) F = R ec ( K , G ) . We have
F = ⨁ n ≥ 0 B n ⊗ G . F = \bigoplus_{n \ge0} B_n \otimes G. F = n ≥ 0 ⨁ B n ⊗ G .
The lemma follows by induction on the time index i i i , and the unbounded sum is well-defined by the same argument as for the well-definedness of R e c ( K , G ) Rec(K, G) R ec ( K , G ) .
:::
Under this path-based interpretation, the following lemma states that we can decompose the computation of F F F into two parts: a local contribution from paths of length less than p p p , and a global contribution from paths that start outside the local p p p -sized window. Only the global contribution has far self-references to F F F .
Lemma 5.2. For any F = R e c ( K , G ) F = Rec(K, G) F = R ec ( K , G ) , we have
F = G p ⊕ ( ⨁ 1 ≤ q ≤ k ⨁ 0 ≤ r < p , p ≤ r + q B r ⊗ K q ⊗ F ) . F = G_p \oplus\left( \bigoplus_{1 \le q \le k} \bigoplus_{\substack{0 \le r < p,\\ p \le r+q}} B_r \otimes K_q \otimes F \right). F = G p ⊕ 1 ≤ q ≤ k ⨁ 0 ≤ r < p , p ≤ r + q ⨁ B r ⊗ K q ⊗ F . In this lemma, G p G_p G p gives the local contribution, and each global contribution has the form B r ⊗ K q ⊗ F B_r \otimes K_q \otimes F B r ⊗ K q ⊗ F : B r B_r B r aggregates the paths that traverse the local window, K q K_q K q is the jump that leaves the local window, and F F F is the continuation of the path. To enumerate the global contributions, we classify paths by the first jump out of the local window: we first enumerate q q q , the length of that jump, and then r r r , the position where it is made. The position r r r must lie within the local window, i.e., 0 ≤ r < p 0 \le r < p 0 ≤ r < p , and the jump must leave it, i.e., p ≤ r + q p \le r + q p ≤ r + q .
The following two lemmas show how to divide-and-conquer the computation of B n B_n B n and G p G_p G p . Both take a similar form, classifying paths by the first jump out of the local window.
Lemma 5.3. For any n , p n, p n , p where p < n p < n p < n ,
B n = ⨁ 1 ≤ q ≤ k ⨁ 0 ≤ r < p , p ≤ r + q ≤ n B r ⊗ K q ⊗ B n − r − q . B_n = \bigoplus_{1 \le q \le k} \bigoplus_{\substack{0 \le r < p,\\ p \le r+q \le n}} B_r \otimes K_q \otimes B_{n-r-q}. B n = 1 ≤ q ≤ k ⨁ 0 ≤ r < p , p ≤ r + q ≤ n ⨁ B r ⊗ K q ⊗ B n − r − q .
Lemma 5.4. For any p , l p, l p , l where l < p l < p l < p ,
G p = G l ⊕ ( ⨁ 1 ≤ q ≤ k ⨁ 0 ≤ r < l , l ≤ r + q B r ⊗ K q ⊗ G p − r − q ) . G_p = G_l \oplus\left( \bigoplus_{1 \le q \le k} \bigoplus_{\substack{0 \le r < l,\\ l \le r+q}} B_r \otimes K_q \otimes G_{p-r-q} \right). G p = G l ⊕ 1 ≤ q ≤ k ⨁ 0 ≤ r < l , l ≤ r + q ⨁ B r ⊗ K q ⊗ G p − r − q .
Taken together, these lemmas let us compute the recurrence F F F in terms of B n B_n B n and G n G_n G n , and divide-and-conquer B n B_n B n and G p G_p G p into smaller pieces. Below, we give examples illustrating how to use these lemmas to vectorize F F F . Figure 7 visualizes the used path decompositions.
Figure 7. Visual illustration of path decompositions for F F F , G 16 G_{16} G 16 , B 16 B_{16} B 16 , B 15 B_{15} B 15 . Dashed line denotes a subtracted path.
Example 13. Consider a second-order time-varying recurrence F = R e c ( A , G ) F = Rec(A, G) F = R ec ( A , G ) where A = [ 0 τ , S 1 , S 2 ] A = [0_{\tau}, S_1, S_2] A = [ 0 τ , S 1 , S 2 ] . Define the singleton kernels A 1 = [ 0 τ , S 1 ] A_1 = [0_{\tau}, S_1] A 1 = [ 0 τ , S 1 ] , and A 2 = [ 0 τ , 0 τ , S 2 ] A_2 = [0_{\tau}, 0_{\tau}, S_2] A 2 = [ 0 τ , 0 τ , S 2 ] . We can compute F F F in terms of G 16 G_{16} G 16 , B 16 B_{16} B 16 , B 15 B_{15} B 15 :
F = R e c ( B 15 ⊗ A 1 ⊕ B 14 ⊗ A 2 ⊕ B 15 ⊗ A 2 , G 16 ) = R e c ( B 16 ⊕ B 15 ⊗ A 2 , G 16 ) \begin{aligned}
F &= Rec(B_{15} \otimes A_1 \oplus B_{14} \otimes A_2 \oplus B_{15} \otimes A_2, G_{16}) \\
&= Rec(B_{16} \oplus B_{15} \otimes A_2, G_{16})
\end{aligned} F = R ec ( B 15 ⊗ A 1 ⊕ B 14 ⊗ A 2 ⊕ B 15 ⊗ A 2 , G 16 ) = R ec ( B 16 ⊕ B 15 ⊗ A 2 , G 16 ) This uses the identity B i = B i − 1 ⊗ A 1 ⊕ B i − 2 ⊗ A 2 B_i = B_{i-1} \otimes A_1 \oplus B_{i-2} \otimes A_2 B i = B i − 1 ⊗ A 1 ⊕ B i − 2 ⊗ A 2 to simplify the computation. The corresponding visualization shows that all algebraic paths of F F F can be classified as paths from the local window
(i.e., G 16 G_{16} G 16 ), paths hitting x − 16 x{-}16 x − 16 (i.e., B 16 ⊗ F B_{16} \otimes F B 16 ⊗ F ), and paths skipping x − 16 x{-}16 x − 16 via a two-jump (i.e., B 15 ⊗ K 2 ⊗ F B_{15} \otimes K_2 \otimes F B 15 ⊗ K 2 ⊗ F ). Similarly, we can compute G 16 G_{16} G 16 , B 16 B_{16} B 16 , B 15 B_{15} B 15 from B 8 B_8 B 8 , B 7 B_7 B 7 , B 6 B_6 B 6 , G 8 G_8 G 8 , G 7 G_7 G 7 .
G 16 = G 8 ⊕ B 8 ⊗ G 8 ⊕ B 7 ⊗ A 2 ⊗ G 7 B 16 = B 8 ⊗ B 8 ⊕ B 7 ⊗ A 2 ⊗ B 7 B 15 = B 7 ⊗ B 8 ⊕ B 6 ⊗ A 2 ⊗ B 7 \begin{aligned}
G_{16} &= G_8 \oplus B_8 \otimes G_8 \oplus B_7 \otimes A_2 \otimes G_7 \\
B_{16} &= B_8 \otimes B_8 \oplus B_7 \otimes A_2 \otimes B_7 \\
B_{15} &= B_7 \otimes B_8 \oplus B_6 \otimes A_2 \otimes B_7
\end{aligned} G 16 B 16 B 15 = G 8 ⊕ B 8 ⊗ G 8 ⊕ B 7 ⊗ A 2 ⊗ G 7 = B 8 ⊗ B 8 ⊕ B 7 ⊗ A 2 ⊗ B 7 = B 7 ⊗ B 8 ⊕ B 6 ⊗ A 2 ⊗ B 7
Take G 16 G_{16} G 16 for example. The visualization suggests the local paths that G 16 G_{16} G 16 aggregates can be categorized into three classes: from a local window of G 8 G_8 G 8 , from outside the window and hitting x − 8 x{-}8 x − 8 , and from outside the window and skipping x − 8 x{-}8 x − 8 via a two-jump.
Example 14. If we specialize the recurrence to rings, we can use the fact that B 6 ⊗ A 2 ⊗ B 7 = B 8 ⊗ B 7 ⊖ B 7 ⊗ A 1 ⊗ B 7 B_6 \otimes A_2 \otimes B_7 = B_8 \otimes B_7 \ominus B_7 \otimes A_1 \otimes B_7 B 6 ⊗ A 2 ⊗ B 7 = B 8 ⊗ B 7 ⊖ B 7 ⊗ A 1 ⊗ B 7 to eliminate the dependence on B 6 B_6 B 6 .
B 15 = B 7 ⊗ B 8 ⊕ B 8 ⊗ B 7 ⊖ B 7 ⊗ A 1 ⊗ B 7 B_{15} = B_7 \otimes B_8 \oplus B_8 \otimes B_7 \ominus B_7 \otimes A_1 \otimes B_7 B 15 = B 7 ⊗ B 8 ⊕ B 8 ⊗ B 7 ⊖ B 7 ⊗ A 1 ⊗ B 7
This saves the work of computing G 6 G_6 G 6 and its dependencies. Intuitively, the decomposition says that to aggregate over all paths of length 15, we can consider paths that hit x − 7 x{-}7 x − 7 and paths that hit x − 8 x{-}8 x − 8 , and then subtract paths that hit both x − 7 x{-}7 x − 7 and x − 8 x{-}8 x − 8 .
Example 15. If we specialize the recurrence to idempotent semirings instead, we can again avoid B 6 B_6 B 6 , this time by relying on idempotence and intentionally double-counting the path B 7 ⊗ A 1 ⊗ B 7 B_7 \otimes A_1 \otimes B_7 B 7 ⊗ A 1 ⊗ B 7 .
B 15 = B 7 ⊗ B 8 ⊕ B 8 ⊗ B 7 B_{15} = B_7 \otimes B_8 \oplus B_8 \otimes B_7 B 15 = B 7 ⊗ B 8 ⊕ B 8 ⊗ B 7
There are several ways to divide and conquer a recurrence, and algebraic identities can rearrange it into forms that save even more work. The general forms in Lemma 5.3 and Lemma 5.4 are often not the most efficient, as they yield quadratically many terms and can introduce too many dependencies. Our compiler hard-codes the optimal decomposition for orders of 2 or fewer over rings and idempotent semirings, which covers the most common recurrences. Scaling to higher order recurrences is left as future work.
Extensions
We now extend the techniques of § 5 to two-dimensional and mutually recursive recurrences.
Two-dimensional recurrences. Marginalization lets us treat a higher-dimensional recurrence as a one-dimensional one. For example, consider the 2D recurrence f ( x , y ) = f ( x − 1 , y ) + f ( x , y − 1 ) + g ( x , y ) f(x,y) = f(x{-}1,y) + f(x,y{-}1) + g(x,y) f ( x , y ) = f ( x − 1 , y ) + f ( x , y − 1 ) + g ( x , y ) . Although f ( x , y ) f(x,y) f ( x , y ) depends on itself in the preceding row and column, we can marginalize f ( x , y ) f(x,y) f ( x , y ) (resp. g ( x , y ) g(x,y) g ( x , y ) ) over x x x into a family of 1D signals f x ( y ) f_x(y) f x ( y ) (resp. g x ( y ) g_x(y) g x ( y ) ) indexed by x x x . This gives f x ( y ) = f x ( y − 1 ) + f x − 1 ( y ) + g x ( y ) f_x(y) = f_x(y{-}1) + f_{x-1}(y) + g_x(y) f x ( y ) = f x ( y − 1 ) + f x − 1 ( y ) + g x ( y ) , which is a prefix sum of f x − 1 ( y ) + g x ( y ) f_{x-1}(y) + g_x(y) f x − 1 ( y ) + g x ( y ) and we know how to vectorize. This also matches how 2D recurrences are computed in practice: row by row, where all earlier rows have been computed when computing the current one.
In § 6 we discuss how to generate code for 2D recurrences using a repeater operator Repeater μ x . e \mu x.e μx . e , which lets e e e refer to already-computed rows via x x x .
Mutual recurrences. Mutual recurrences are a group of recurrences that depend on each other. For example, double exponential smoothing is a method that computes the interdependent smoothed value and trend value of a time series, and it can be expressed as a mutual recurrence:
l ( x ) = ( 1 − α ) ⋅ l ( x − 1 ) + ( 1 − α ) ⋅ b ( x − 1 ) + α ⋅ g ( x ) l(x) = (1-\alpha) \cdot l(x-1) + (1-\alpha) \cdot b(x-1) + \alpha\cdot g(x) l ( x ) = ( 1 − α ) ⋅ l ( x − 1 ) + ( 1 − α ) ⋅ b ( x − 1 ) + α ⋅ g ( x )
b ( x ) = − α β ⋅ l ( x − 1 ) + ( 1 − α β ) ⋅ b ( x − 1 ) + α β ⋅ g ( x ) b(x) = -\alpha\beta\cdot l(x-1) + (1-\alpha\beta) \cdot b(x-1) + \alpha\beta\cdot g(x) b ( x ) = − α β ⋅ l ( x − 1 ) + ( 1 − α β ) ⋅ b ( x − 1 ) + α β ⋅ g ( x )
The LeetCode problem from the introduction is also mutually recursive.
Mutual recurrences fit naturally into our framework: we lift signals to vectors of signals, and kernels to matrices of kernels. For instance, the double exponential smoothing recurrence can be expressed as a recurrence vector:
[ L B ] = [ [ 0 1 − α 0 − α β ] [ 0 1 − α 0 1 − α β ] ] ⊗ [ L B ] [ α ⋅ G α β ⋅ G ] \begin{bmatrix} L \\ B \end{bmatrix}
=
\begin{bmatrix}
\begin{bmatrix} 0 & 1-\alpha\\ 0 & -\alpha\beta\end{bmatrix} &
\begin{bmatrix} 0 & 1-\alpha\\ 0 & 1-\alpha\beta\end{bmatrix}
\end{bmatrix}
\otimes
\begin{bmatrix} L \\ B \end{bmatrix}
\begin{bmatrix} \alpha\cdot G \\ \alpha\beta\cdot G \end{bmatrix} [ L B ] = [ [ 0 0 1 − α − α β ] [ 0 0 1 − α 1 − α β ] ] ⊗ [ L B ] [ α ⋅ G α β ⋅ G ]
The same algebraic transformations and vectorizing techniques that work for standard recurrences also apply to recurrence vectors. Matrix multiplication, however, is not commutative in general, so we no longer have A ⊗ B = B ⊗ A A \otimes B = B \otimes A A ⊗ B = B ⊗ A for kernel matrices A A A and B B B , even when the kernels within A A A and B B B are time-invariant. Therefore, we can apply only those vectorizing transformations that do not rely on commutativity, which excludes dilation.
The VecRec Compiler
In this section, we introduce VecRec, our compiler instantiation of the recurrence algebra. The recurrence algebra itself leaves many details of physical implementation unspecified. We first describe VecRec’s domain-specific language (DSL), which exposes details relevant to physical execution, and then the codegen process that lowers the DSL into efficient vectorized C++ implementations.
VecRec uses a streaming computation model and exposes a run method that, when called, returns the current output vector(s) and advances the internal state. the input nor the output is stored in memory; instead, it stores internally only the vectors needed to compute the output vectors.
VecRec's domain-specific language
Figure 8 shows a fragment of the VecRec syntax; the full syntax is available in the appendix. Here we omit some pointwise operators and the syntax for mutual recurrences. Mutual recurrences are supported with kernel matrix types and signal vector types, and operators like Convolve and Recurse are overloaded to accept kernel matrices and signal vectors.
type Kernel1D, Signal1D, Kernel2D, Signal2D
TimeInv : [Number] -> Kernel1D; TimeInv2D : [[Number]] -> Kernel2D
TimeVar : [Signal1D] -> Kernel1D; TimeVar2D : [[Signal1D]] -> Kernel2D
Var : String -> Signal1D; Var2D : String -> Signal2D
Convolve : (Kernel1D, Signal1D) -> Signal1D
Recurse : (Kernel1D, Signal1D) -> Signal1D
Convolve2D : (Kernel2D, Signal2D) -> Signal1D
Recurse2D : (Kernel2D, Signal1D) -> Signal2D
WindowMap : (n: Number, [Signal1D], [[Signal1D; n]] -> Signal1D) -> Signal1D
Repeater : (Signal2D -> Signal1D) -> Signal2D
Ith : (Signal2D, Int) -> Signal1D
ConvertLanes : forall T. (T, Int) -> TFigure 8. The domain-specific language used in VecRec. The full version is available in the appendix.
Unlike in the recurrence algebra, signal and kernel types in VecRec are refined by dimension. VecRec considers only one- and two-dimensional kernels and signals, which suffice for our applications. Kernel1D and Kernel2D represent a list of taps and a matrix of signal taps, respectively, and each tap is a 1D signal. The signal types are more interesting. The one-dimensional signal type Signal1D streams one value at a time, while the two-dimensional signal type Signal2D streams a tuple of not only the current value but also the values from the previous rows in the same column. To motivate this distinction, consider the convolution A ⊗ G A \otimes G A ⊗ G for a 2D kernel A A A . To compute its value at a point ( x , y ) (x,y) ( x , y ) , we must fetch the values of G G G at not only the current row x x x but also the previous rows x − 1 x-1 x − 1 , x − 2 x-2 x − 2 , … \ldots … , based on the window defined by A A A . In the streaming model, we must buffer the last n n n rows of G G G when G G G is not already materialized (e.g., a 2D input signal).
TimeInv (resp. TimeVar) constructs 1D time-invariant (resp. time-varying) kernels and takes numbers (resp. 1D signals) as inputs. TimeInv2D and TimeVar2D are the analogues for 2D kernels. Var and Var2D construct 1D and 2D input signals.
Convolve and Recurse express 1D convolutions and recurrences, and they take and return 1D signals. The 2D convolution and recurrence operators have interesting type signatures. Convolve2D takes a 2D signal but returns a 1D signal. This is because a physical implementation of convolution reads a 2D window of rows from the input but does not buffer by itself. Recurse2D, in contrast, takes a 1D signal but returns a 2D signal. This is because a 2D recurrence convolves with its own outputs from previous rows and must cache these rows. Meanwhile, it does not require the input signal to be buffered (hence a 1D input signal).
A Repeater turns a procedure that produces 1D signals into a 2D signal. It computes the given 1D signal one row at a time, buffers the rows already produced, and feeds them as non-recursive inputs to the current row. Repeater (λ \lambda λ s. e) binds s to the 2D signal that represents the previously computed rows, which e uses to compute the current row. The Repeater expression returns a 2D signal that streams both the current row and the buffered rows. It destructs a 2D signal and extracts a 1D signal at a given row offset. Together they enable marginalization and let us express arbitrary 2D recurrences as 1D recurrences. In fact, VecRec desugars a Recurse2D into a Repeater expression: Recurse2D(K K K , S S S ) ≡ \equiv ≡ Repeater(λ \lambda λ S'. Recurse(K 0 K_0 K 0 , Add(Convolve(K 1 K_1 K 1 , Ith(S ′ S' S ′ , 0)), ...))), where K i K_i K i is a 1D kernel representing the i i i -th row of the 2D kernel K K K .
WindowMap de-interleaves lists of signals and is used by the fast associative scan. Given interleave factor n n n , it splits each input into n n n substreams, where substream j j j takes every n n n -th sample from offset j j j ; it then runs its body on the substreams and interleaves the results back to full rate.
Each operator in VecRec is implicitly annotated with the number of lanes it operates over at a time. By default, an operator is annotated with the maximum number of vector lanes available to the hardware. The number of vector lanes in a recurrence, however, is bounded by the index of the first non-zero tap of the kernel. This bound can cause the vector lanes of a node to differ from those of its child or parent. We use ConvertLanes(e e e , l l l ) to explicitly convert the vector lanes of e e e to l l l lanes. The number of lanes of e e e must be either a divisor or a multiple of l l l . In the physical implementation, if e e e has fewer lanes than the specified l l l , we repeat the physical execution of e e e for l / l e l/l_e l / l e times, where l e l_e l e is the number of lanes of e e e . Otherwise, we compute e e e on every l e / l l_e/l l e / l -th invocation of the operator and split the output vector among l e / l l_e/l l e / l calls.
Compiler implementation
We implement the recurrence identities of § 4 and the vectorizing transformations of § 5 as compiler passes. These passes enumerate combinations of transformations and vectorization factors to produce a set of distinct vectorized forms. We call each form a schedule . Among the set of schedules, the compiler uses runtime profiling to select the best one.
Given a schedule, the code generator must first infer (1) the number of buffered rows for 2D signals and (2) the number of lanes for each operator. We obtain the number of buffered rows needed for a 2D signal by collecting the Ith references to it and taking the maximum. For vector lane annotations, we use the maximum available lanes for non-recurrence nodes. For recurrence operators, the vector lanes are given by the position of the first non-zero tap. If the vector lane annotation of a node does not match that of its child, we fork the expression into two schedules: either we wrap the child with ConvertLanes, so that the child’s annotation remains unchanged, or we recursively downgrade the vector lane annotations of the children to match the parent.
Example 16. Consider the recurrence Recurse(K1, Convolve(K2, Convolve(K3, g))), where the first non-zero tap of K1 is at offset 8 and the hardware supports up to 16 vector lanes. Initially, the Recurse node is annotated with 8 lanes and the two Convolves with 16 lanes, so the lanes of Recurse and Convolve differ. The compiler generates two versions of the annotations. In the first, it inserts a ConvertLanes node between Recurse and Convolve that converts from 16 lanes to 8 lanes. In the second, it recursively pushes Recurse’s annotation down to its descendants. Both versions are considered for runtime profiling. However, the compiler never inserts a ConvertLanes node between two non-recursive nodes (e.g., between two Convolve nodes). □
Once a schedule is fully annotated with buffered rows and vector lanes, we lower it into a C++ struct. The code generator keeps a C++ template library of operator structs such as Recurse and Convolve. These structs operate over a register state struct, which wraps an array of vector types5 . Operators read input vectors and write output vectors at given offsets of this array, and maintain internal state. For example, a Convolve keeps a window of the last n n n input vectors that is enough for the next convolution, and a Recurse similarly keeps previously computed output vectors. The code generator traverses the tree bottom up and emits an operator for each distinct node. The resulting C++ struct is initialized and called from user code.
Determining the optimal unrolling factors
For each generated kernel program, we measure its throughput on random input signals. For the top 10% of kernels, we perform an additional sweep to find the optimal unrolling factors. A VecRec program can be unrolled horizontally and, for 2D recurrences, vertically. An unrolled program produces a tile of output vectors at a time. For 2D recurrences, vertical unrolling saves memory roundtrips that horizontal unrolling cannot, because the vertical dependencies already reside in registers and need not be loaded from memory6 . Not all 2D recurrences benefit from vertical unrolling on all architectures, however, because vectorized kernels are often bound by register pressure (from the introduced convolutions) rather than memory bandwidth. It helps most on ARM, which offers more instruction-level parallelism.
Evaluation
We evaluate VecRec in two parts. First, we benchmark VecRec-generated kernels against real-world recurrence applications and LeetCode DP problems. To the best of our knowledge, most real-world recurrence implementations do not vectorize7 , except for Intel IPP and Apple Accelerate, which have vectorized implementations for biquad filters. Therefore, we compare against Intel IPP and Apple Accelerate on biquad filters. For the rest of real-world applications and all LeetCode problems, we compare against performant, unvectorized baselines. In both cases, we demonstrate significant speedups. Second, we conduct a case study of bioinformatic recurrences, which many studies have sought to speed up with vectorization [6 , 11 , 28 , 35 ] . To that end, we compare the kernels with vectorized implementations from state-of-the-art libraries, and integrate them into existing systems, showing up to 28% speedup on end-to-end pipelines. Together, our evaluation shows VecRec works on a broad range of recurrences, beats hand-tuned algorithms for specific ones, and provides state-of-the-art performance on diverse hardware.
We evaluate VecRec on three platforms that span different processor generations, microarchitectures, and instruction sets: Intel Skylake (Core i9-9960X, 2018), AMD Zen 5 (Ryzen Threadripper 9970X, 2025), and Apple Silicon (M2, 2022). The first two architectures support the AVX-512 instruction set with 512-bit operations. The M2’s NEON instruction set supports only 128-bit operations, but it offers better instruction-level parallelism and can issue more instructions simultaneously.
Evaluation on recurrences in the wild
Most high-performance libraries for signal processing just run recurrences as scalar code, so we compare VecRec-generated kernels with Intel IPP and Apple Accelerate, two libraries with vectorized implementations for biquad filters. Both libraries’s vectorization schemes incur the additional work that grows linearly with the exposed parallelism, while in ours it grows only logarithmically. Both libraries vectorize biquad filters with a fixed width of 4. The result is shown in Table 1 . VecRec is faster than both vendor libraries by up to 5.38×.
Table 1. Speedup over baselines from vendor libraries on biquad filters. We use IPP’s ippsIIR_32f and Apple Accelerate’s vDSP_deq22 for the comparison.
Next, we evaluate on a large class of recurrences that arise naturally in signal processing and numerical computing, many of which are critical in their respective domains. Here we highlight two rather unexpected applications of recurrences:
h ( x ) = a ( x ) ⋅ h ( x − 1 ) + b ( x ) ⋅ h ( x − 2 ) f ( x ) = h ( x ) / h ( x − 1 ) . \begin{aligned}
h(x) &= a(x) \cdot h(x-1) + b(x) \cdot h(x-2) \\
f(x) &= h(x)/h(x-1).
\end{aligned} h ( x ) f ( x ) = a ( x ) ⋅ h ( x − 1 ) + b ( x ) ⋅ h ( x − 2 ) = h ( x ) / h ( x − 1 ) .
As x x x goes to infinity, f f f converges to the continued fraction. An example of a continued fraction is to use the ratio of two consecutive Fibonacci numbers to approximate the golden ratio: 1/(1 + 1/(1 + …)). In general the coefficients are time-varying, and VecRec can vectorize such recurrences.
M e a n ( x ) = ( 1 − 1 / x ) ⋅ M e a n ( x − 1 ) + 1 / x ⋅ a ( x ) M 2 ( x ) = M 2 ( x − 1 ) + ( a ( x ) − M e a n ( x ) ) ⋅ ( a ( x ) − M e a n ( x − 1 ) ) V a r ( x ) = 1 / x ⋅ M 2 ( x ) . \begin{aligned}
\mathit{Mean}(x) &= (1 - 1/x) \cdot\mathit{Mean}(x-1) + 1/x \cdot a(x) \\
M_2(x) &= M_2(x-1) + (a(x) - \mathit{Mean}(x)) \cdot(a(x) - \mathit{Mean}(x-1)) \\
\mathit{Var}(x) &= 1/x \cdot M_2(x).
\end{aligned} Mean ( x ) M 2 ( x ) Var ( x ) = ( 1 − 1/ x ) ⋅ Mean ( x − 1 ) + 1/ x ⋅ a ( x ) = M 2 ( x − 1 ) + ( a ( x ) − Mean ( x )) ⋅ ( a ( x ) − Mean ( x − 1 )) = 1/ x ⋅ M 2 ( x ) .
VecRec can represent this recurrence and generate an efficient vectorized kernel, as M e a n \mathit{Mean} Mean is a first-order time-varying recurrence and M 2 M_2 M 2 is a prefix scan.
For these, our baseline is ‘Serial’, a modular C++ implementation that computes the recurrences in serial loops, and ‘Base’, a kernel that VecRec produces but with vectorization disabled. We set the problem size to 2 22 2^{22} 2 22 and measure the throughput. Table 2 shows the runtime on recurrences that arise naturally in signal processing and numerical computing. Base sometimes achieves higher throughput than Serial because VecRec unrolls the recurrence computation using a runtime profile. Serial can be faster if it hits a better compilation path in clang. The speedup is reported as VecRec over the higher throughput of Serial and Base.
Table 2. Benchmarked throughputs for recurrence applications on the serial, baseline, and vectorized implementations across architectures. Speedup is Vec over the faster of Serial and Base.
Overall, VecRec achieves a speedup of 1.04× to 15.85×. The speedup tends to be bigger for time-invariant recurrences where dilation applies. Additionally, many benchmarks, including continued fraction and Welford variance mentioned above, consist of both recurrence operators and expensive pointwise operators like float division. Recurrence’s self-dependencies prevent the rest of the kernel from being vectorized, because packing/unpacking values in and out of a vector register is often uneconomical. By widening the available parallelism for the recurrence operators, our transformations let the entire kernel use vectorized instructions, so even non-recursive operators in a recurrence kernel benefit from our vectorization techniques.
We also collect a set of 12 recurrences from dynamic programming problems on LeetCode. These problems often exhibit diverse patterns, and no baselines are available. Table 3 shows the result of VecRec-generated kernels against manual C++ implementations and unvectorized baselines.
Table 3. Benchmarked throughputs on LeetCode problems.
Case study on bioinformatic recurrences
Sequence alignment is built on four core recurrences that share the same basic structure: Edit Distance, Needleman–Wunsch, Smith–Waterman, and Affine Gap. Among them, Affine Gap is the most widely used and most complex [13 , 16 ] , consisting of three mutually recursive recurrences. These recurrences take time quadratic in the sequence length, and genetic data spans billions of characters [6 , 11 , 21 , 38 ] , so they are often the primary bottleneck in genomics pipelines [9 , 18 , 19 , 20 ] .
Runtime comparison. We implement all four recurrences in VecRec and compare them with existing libraries that use hand-tuned optimizations. Our baselines include ksw2, which compresses the recurrence using score differences [20 , 32 ] , parasail’s striped and scan strategies, which vectorize cells from the previous row, parasail’s diagonal strategy, which vectorizes independent cells along an antidiagonal [6 ] , and seqan3, an optimized scalar implementation [26 ] . We also compare against WFA [21 ] , which is algorithmically distinct and has performance proportional to sequence similarity, and Edlib, which is specialized to edit distance and uses bit-packing to compute multiple cells within a single machine word [38 ] .
Table 4 shows the evaluation results. Vectorized methods trade parallelism against memory locality. Ksw2 and diagonal vectorize fully independent antidiagonals at the cost of scattered memory access. Striped and scan keep access contiguous within a row but don’t resolve the column’s serial dependency. Using dilation, VecRec can both parallelize the serial dependency and preserve memory locality, providing up to 2.4× speedup over these libraries. On the other hand, edlib and WFA can outperform VecRec under specific conditions. Edlib’s bitpacking optimization applies only to edit distance. WFA is faster at high sequence similarity and with small scoring parameters, but it requires scores to be strictly positive and does not support Smith-Waterman. In practice, however, sequences are often divergent [20 , 23 , 32 ] , and scores can be large and negative [7 , 15 ] .
Table 4. Comparing VecRec with state-of-the-art vectorized libraries on sequence alignment algorithms of matrix size 2048 × 2048. The speedup is measured against the best-performing baselines. Ksw2 is run on Apple M2 via sse2neon. 8 For WFA2, runtime is proportional to sequence similarity and the 85% similarity reports WFA2’s speedup over VecRec.
Optimizing end-to-end pipelines
Genomics pipelines use sequence alignment recurrences in performance-critical inner loops [9 , 18 , 19 , 20 ] . Minimap2, a widely used long-read alignment tool, issues thousands to millions of small affine-gap recurrence calls in parallel via its alignment library ksw2 [20 ] .
Ksw2 exposes several alignment modes trading accuracy for speed: exact alignment, which computes the full recurrence and guarantees the optimal score, and approximations such as early termination and reduced-precision scores, which users select based on their workload’s error tolerance [20 ] . The approximations are tied to ksw2’s reformulated recurrence and antidiagonal traversal, so VecRec cannot support them directly; instead, it serves as a drop-in accelerator for the exact mode.
We compare VecRec with minimap2’s exact mode. Exact computation is useful for noisy reads9 such as Oxford Nanopore Technologies (ONT) long reads [20 ] . On ONT datasets, minimap2 integrated with VecRec achieves up to 1.28× speedup over stock Minimap2 Table 6 .
Table 6. End-to-end alignment time on ONT workloads with approximate maximum, banding, and z-drop flags disabled. ecoli-ultralong uses ultra-long E. coli reads, ecoli-strains uses E. coli strain sequences, and pa-strains uses Pseudomonas aeruginosa strain sequences. These results were obtained on the Zen 5 microarchitecture.
Comparison to Reptile [33 ] . We compare VecRec against Reptile, a tiling framework for recurrences. VecRec outperforms Reptile by 2.37×, because Reptile generates only scalar code Table 5 . Reptile also lets an external kernel replace its inner loop as a black box, so we composed it with VecRec for sequence alignment, but this yielded no additional speedup. Reptile’s optimal tile sizes are small and is covered by the unrolling VecRec already applies.
Table 5. Smith–Waterman runtime for N × N N \times N N × N matrices, comparing untiled code, Reptile’s best tiling, and VecRec. Tile sizes were 4–512 with the best being col 16, col 16, col 16, col 16.
Understanding vectorization schedules
To understand the characteristics of individual transformations, we vectorize one representative second-order filter with each of them. Table 7 shows their achieved speedup and accuracy. We measure the accuracy using three different configurations of the radius r r r of the filter’s poles. A larger radius r r r makes the filter harder to evaluate accurately.
Table 7. Vectorization schedules for the second-order filter in float32, with poles at r e ± i θ r e^{\pm i\theta} r e ± i θ for a fixed θ \theta θ . Speedup is over the scalar baseline. Error is measured as the relative L 2 L_2 L 2 distance from a scalar float64 evaluation on 2 22 2^{22} 2 22 uniform random inputs, measured on Zen 5; ∞ \infty ∞ means the kernel overflows.
The result shows that, when applicable, dilation (possibly with delay) achieves the best speedup compared to other more general transformations. Dilation makes the kernel more accurate than scalar evaluation. While dilation + delay may provide additional speedups, it can become numerically unstable as r r r gets closer to 1.
We also evaluate the two general transformations on two time-varying applications (Table 8 ). Neither dominates the other, so the best schedule can only be determined through run-time profiling.
Table 8. Fast associative scan vs. path-based decomposition on two applications based on time-varying recurrences. Speedup is measured over a scalar implementation.
Related Work
Several lines of work inspired this paper. Parallel algorithms for associative scans date back to Kogge–Stone adders [17 , 30 ] and were popularized by Blelloch [2 ] . Our fast associative scan (§ 5.2.1 ) is essentially a reformulation of recursive doubling [2 , 17 ] in the recurrence algebra. To turn it into efficient vectorized instructions, however, we propose a hardware-efficient scheme that interleaves the computation of vector lanes.
In digital signal processing (DSP), Robelly et al. [27 ] applies Blelloch [2 ] -style parallelism in digital signal processors, and Feinberg [12 ] vectorize IIR filters by unrolling the recurrence definitions, i.e., delaying. Our dilation for ring recurrences is known as scattered look-ahead in DSP literature[25 ] ; we have shown the algebraic identities behind it.
In tropical semirings, Rong and Tan [29 ] described jump flooding for parallelizing distance transforms. This corresponds to our dilation transformation for idempotent recurrences (Lemma 4.11 ). Our dilation transformation slightly generalizes jump flooding by allowing different taps to be dilated by different amounts. For instance, a variant of the distance transform used in practice is
F = Rec ( [ ∞ 1 ∞ 1 2 5 ∞ 5 ∞ ] , G ) . F = \operatorname{Rec}\left(\begin{bmatrix} \infty& 1 & \infty\\ 1 & \sqrt{2} & \sqrt{5} \\ \infty& \sqrt{5} & \infty\end{bmatrix}, G\right). F = Rec ∞ 1 ∞ 1 2 5 ∞ 5 ∞ , G .
To compute a 2 × 2 2 \times2 2 × 2 grid in parallel, we can let the two taps with coefficients 5 \sqrt{5} 5 stay undilated and only dilate the rest, leading to a recurrence kernel [ ∞ , ∞ , 2 ∞ , ∞ , 5 2 , 5 , 2 ] \begin{bmatrix} \infty,\infty,2 \\ \infty,\infty,\sqrt{5} \\ 2,\sqrt{5},\sqrt{2} \end{bmatrix} ∞ , ∞ , 2 ∞ , ∞ , 5 2 , 5 , 2 . This is more efficient than classical jump flooding, which dilates every term.
Beyond the generalizations to prior work, we treat vectorization schemes as generic transformations over algebraic structures (e.g., rings and tropical semirings), so they are not confined to a particular domain: jump flooding, for instance, had never been applied to bioinformatics or many other DP problems, yet we show that it achieves significant speedups there. We also build a compiler to show that these approaches yield efficient SIMD vectorization, whereas prior work largely targeted DSP design or multicore parallelism.
In signal and image processing, prior work has developed parallel algorithms for computing time-invariant recursive filters [22 ] . We draw inspiration from RecFilter [4 ] , a compiler that automatically generates high-performance parallel implementations of multi-dimensional time-invariant IIR filters. Their approaches to multi-core parallelism, however, do not directly extend to vectorization, because they require simultaneously loading from non-contiguous dimensions. Multi-core parallelism for recurrences (outer-loop) is orthogonal to this work (inner-loop). In fact, they complement each other: Table 6 shows that minimap2, a multi-threaded alignment library, is further sped up by VecRec’s vectorization.
Recent domain-specific compilers for recurrences include RecUMA [31 ] , which compiles recurrences to efficient implementations over dense and sparse arrays, and Reptile [33 ] , which explores tiling strategies. They handle a more general class of recurrences, including ones that aggregate over all previous values, for which auto-vectorization suffices. We focus on fixed-order recurrences, which require further techniques to vectorize efficiently.
Our recurrence algebra is inspired by recent semiring-based database languages [1 , 3 , 14 ] . Interestingly, DBSP [3 ] , a language for incremental computation, is also inspired by signal processing and uses terminology from Z-transform. Unlike these works, our algebra is a semimodule rather than a semiring, because convolution does not apply between signals. Similar to our work, Elliott [10 ] casts convolution in terms of semirings and semimodules, unifying signal processing, polynomial multiplication, probability, and language recognition.
Hand-optimized sequence alignment libraries use two primary vectorization paradigms. Row-based approaches (e.g., Parasail’s striped and scan [6 , 11 ] ) vectorize the dependency on the previous row, whereas antidiagonal approaches (e.g., Parasail’s diagonal, ksw2 [32 ] , and WFA [21 ] ) vectorize along independent diagonals. To maximize performance, these libraries employ specialized optimizations: ksw2 leverages score differences for int8 compression, WFA uses search-based recurrences to accelerate highly similar sequences, and Edlib [38 ] packs into machine words. While these frameworks are restricted to specific scoring strategies, VecRec is scoring-agnostic and supports diverse alignment and score variants. End-to-end genomic pipelines such as minimap2 [20 ] , BWA-MEM [19 ] , Bowtie2 [18 ] , and STAR [9 ] embed these libraries to process large-scale sequence data.
Our work is also inspired by Zhang et al. [36 ] , who sped up recursive filters by first applying scattered lookahead and then accelerating the resulting convolution using Tensor Cores. In a similar vein, our techniques may be adapted to other hardware, including SIMD GPUs and matrix accelerators.
Conclusion and Future Work
In this paper, we present the recurrence algebra over semirings, which captures a wide variety of recurrences in the wild, we show a set of identities and transformations that effectively vectorize them, and we build the VecRec compiler based on these ideas. Experiments show that the compiler-generated kernels can be an order of magnitude faster than non-vectorized implementations and are competitive with expert-optimized vectorized libraries.
This paper focuses on linear recurrences over semirings, so natural next steps are nonlinear recurrences, such as the Mandelbrot set or Floyd-Steinberg dithering, and recurrences not over semirings, as in certain LeetCode problems. On the implementation side, we would like to apply our framework to the GPU’s SIMT model. VecRec currently supports only transformations expressible in the recurrence algebra, which are shift-invariant (e.g., f ( x ) f(x) f ( x ) and f ( x − 1 ) f(x-1) f ( x − 1 ) are computed using the same scheme), yet many transformations in domain-specific libraries are not, such as bitpacking in edit distance and parasail’s wavefront parallelism. Future work is thus a compiler that supports and combines different vectorization and multi-core parallelism schemes, as well as other transformations like loop reordering [31 ] and multithreading.
Acknowledgments
We thank the anonymous reviewers for their valuable feedback, Shoaib Kamil and Dan Suciu for their feedback on early versions of this work, and Shiv Sundram and Muhammad Usman Tariq for helping with the REPTILE experiment. This material is based upon work supported by the National Science Foundation under Grant Nos. 2232339 and 2312195. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.
Disclosure of Use of AI in Research
AI tools assisted with implementing the compiler, particularly features needed for the bioinformatics-related case studies, developing the evaluation harness, and implementing C++ scalar baselines for the LeetCode and signal processing problems. AI tools were used for formatting and proofreading prose and for producing some initial paragraph drafts; these drafts were subsequently heavily edited by the authors.
Data Availability Statement
The source code of the VecRec compiler is available at https://github.com/yihozhang/vec-rec . The artifact additionally includes the evaluation harness necessary to reproduce the experiment in this paper, and it is available at https://doi.org/10.5281/zenodo.23330549 [37 ] .
References
A Proof of theorems concerning the recurrence algebra
Lemma A.1. In the arithmetic ring over real numbers, any one-dimensional time-invariant kernel A A A of order n n n can be factored as A = A 1 ⊗ ⋯ ⊗ A k A=A_1\otimes\cdots\otimes A_k A = A 1 ⊗ ⋯ ⊗ A k where each A i A_i A i has order less than or equal to 2.
Proof. We can write A A A as coefficients of a generating function p A ( x ) = ∑ i = 0 n a i x i p_A(x)=\sum_{i=0}^{n}a_i x^i p A ( x ) = ∑ i = 0 n a i x i , where a i a_i a i is the coefficient of the i i i -th tap of A A A . The fundamental theorem of algebra states that p A p_A p A can be factored into a product of real polynomials of degree at most 2, so we have p A ( x ) = p A 1 ( x ) ⋅ … ⋅ p A k ( x ) p_A(x)=p_{A_1}(x)\cdot\ldots\cdot p_{A_k}(x) p A ( x ) = p A 1 ( x ) ⋅ … ⋅ p A k ( x ) , where each p A i p_{A_i} p A i is either linear ( a x + b ) (ax+b) ( a x + b ) or quadratic ( a x 2 + b x + c ) (ax^2+bx+c) ( a x 2 + b x + c ) .
B Proof of theorems concerning recurrences
Lemma B.1 (Equivalent definitions of Rec). Suppose A A A is causal. ⨁ i ≥ 0 A i ⊗ G \bigoplus_{i\geq0}A^i\otimes G ⨁ i ≥ 0 A i ⊗ G is well-defined, and F = ⨁ i ≥ 0 A i ⊗ G F=\bigoplus_{i\geq0}A^i\otimes G F = ⨁ i ≥ 0 A i ⊗ G iff F = A ⊗ F ⊕ G F=A\otimes F\oplus G F = A ⊗ F ⊕ G .
Proof. We assert ⟦ A k ⟧ σ = 0 τ \llbracket A^k\rrbracket^\sigma=0_\tau [ [ A k ] ] σ = 0 τ if the L1 norm ∣ i ∣ 1 |i|_1 ∣ i ∣ 1 is smaller than k k k for any k k k . This can be proved by induction on k k k . Therefore, for a given index n n n , we can pick any k k k satisfying ∣ n ∣ 1 ≥ k |n|_1\geq k ∣ n ∣ 1 ≥ k , and ⟦ A k ⊗ F ⟧ σ ( n ) = 0 τ \llbracket A^k\otimes F\rrbracket^\sigma(n)=0_\tau [ [ A k ⊗ F ] ] σ ( n ) = 0 τ .
Fix any such k k k . Then \left\llbracket\bigoplus_{i\geq0}A^i\otimes G\right\rrbracket^\sigma(n) is well-defined and equal to \left\llbracket\bigoplus_{i=0}^{k-1}A^i\otimes G\right\rrbracket^\sigma(n) . Therefore, the semantics of the unbounded sum ⨁ i ≥ 0 A i ⊗ G \bigoplus_{i\geq0}A^i\otimes G ⨁ i ≥ 0 A i ⊗ G is well-defined for any index n n n .
( ⇐ ) (\Leftarrow) ( ⇐ ) Suppose F = A ⊗ F ⊕ G F=A\otimes F\oplus G F = A ⊗ F ⊕ G . For any k k k , we have
F = A ⊗ F ⊕ G = A ⊗ ( A ⊗ F ⊕ G ) ⊕ G = A 2 ⊗ F ⊕ A ⊗ G ⊕ G ⋮ = A k ⊗ F ⊕ ⨁ i = 0 k − 1 A i ⊗ G . \begin{aligned}
F&=A\otimes F\oplus G\\
&=A\otimes(A\otimes F\oplus G)\oplus G\\
&=A^2\otimes F\oplus A\otimes G\oplus G\\
&\ \vdots\\
&=A^k\otimes F\oplus\bigoplus_{i=0}^{k-1}A^i\otimes G.
\end{aligned} F = A ⊗ F ⊕ G = A ⊗ ( A ⊗ F ⊕ G ) ⊕ G = A 2 ⊗ F ⊕ A ⊗ G ⊕ G ⋮ = A k ⊗ F ⊕ i = 0 ⨁ k − 1 A i ⊗ G . For any n n n , we can choose a k k k with k ⋅ 1 > n k\cdot1>n k ⋅ 1 > n so that
\llbracket F\rrbracket^\sigma(n)=\llbracket A^k\otimes F\rrbracket^\sigma(n)\oplus_\tau\left\llbracket\bigoplus_{i=0}^{k-1}A^i\otimes G\right\rrbracket^\sigma(n)=\left\llbracket\bigoplus_{i=0}^{k-1}A^i\otimes G\right\rrbracket^\sigma(n)=\left\llbracket\bigoplus_{i\geq0}A^i\otimes G\right\rrbracket^\sigma(n).
( ⇒ ) (\Rightarrow) ( ⇒ ) Conversely, if F = ⨁ i ≥ 0 A i ⊗ G F=\bigoplus_{i\geq0}A^i\otimes G F = ⨁ i ≥ 0 A i ⊗ G then
A ⊗ F ⊕ G = A ⊗ ( ⨁ i ≥ 0 A i ⊗ G ) ⊕ G = ⨁ i ≥ 0 A i + 1 ⊗ G ⊕ A 0 ⊗ G = ⨁ i ≥ 0 A i ⊗ G = F . \begin{aligned}
A\otimes F\oplus G&=A\otimes\left(\bigoplus_{i\geq0}A^i\otimes G\right)\oplus G\\
&=\bigoplus_{i\geq0}A^{i+1}\otimes G\oplus A^0\otimes G\\
&=\bigoplus_{i\geq0}A^i\otimes G=F.
\end{aligned} A ⊗ F ⊕ G = A ⊗ ( i ≥ 0 ⨁ A i ⊗ G ) ⊕ G = i ≥ 0 ⨁ A i + 1 ⊗ G ⊕ A 0 ⊗ G = i ≥ 0 ⨁ A i ⊗ G = F .
Algebraic identities over any semiring
Lemma B.2 (Unrolling). For kernel A A A and signal G G G over any semiring,
Rec ( A , G ) = Rec ( A k , ⨁ i = 0 k − 1 A i ⊗ G ) . \operatorname{Rec}(A,G)=\operatorname{Rec}\left(A^k,\bigoplus_{i=0}^{k-1}A^i\otimes G\right). Rec ( A , G ) = Rec ( A k , i = 0 ⨁ k − 1 A i ⊗ G ) .
Proof.
Rec ( A , G ) = ⨁ j ≥ 0 A j ⊗ G = ⨁ j ≥ 0 ⨁ i = 0 k − 1 A j ⋅ k + i ⊗ G = ⨁ j ≥ 0 ( A j ⋅ k ⊗ ⨁ i = 0 k − 1 A i ⊗ G ) = Rec ( A k , ⨁ i = 0 k − 1 A i ⊗ G ) . \begin{aligned}
\operatorname{Rec}(A,G)=\bigoplus_{j\ge0} A^j\otimes G \\
&=\bigoplus_{j\ge0}\bigoplus_{i=0}^{k-1} A^{j\cdot k+i}\otimes G \\
&=\bigoplus_{j\ge0}\left(A^{j\cdot k}\otimes\bigoplus_{i=0}^{k-1} A^i\otimes G\right) \\
&=\operatorname{Rec}\left(A^k,\bigoplus_{i=0}^{k-1} A^i\otimes G\right).
\end{aligned} Rec ( A , G ) = j ≥ 0 ⨁ A j ⊗ G = j ≥ 0 ⨁ i = 0 ⨁ k − 1 A j ⋅ k + i ⊗ G = j ≥ 0 ⨁ ( A j ⋅ k ⊗ i = 0 ⨁ k − 1 A i ⊗ G ) = Rec ( A k , i = 0 ⨁ k − 1 A i ⊗ G ) .
Lemma B.3 (Commuting recurrence with convolution). For time-invariant kernels A A A and B B B and signal G G G over any semiring,
Rec ( A , B ⊗ G ) = B ⊗ Rec ( A , G ) . \operatorname{Rec}(A,B\otimes G)=B\otimes\operatorname{Rec}(A,G). Rec ( A , B ⊗ G ) = B ⊗ Rec ( A , G ) . Proof.
Rec ( A , B ⊗ G ) = ⨁ k = 0 ∞ A k ⊗ B ⊗ G \operatorname{Rec}(A,B\otimes G)=\bigoplus_{k=0}^{\infty} A^k\otimes B\otimes G Rec ( A , B ⊗ G ) = k = 0 ⨁ ∞ A k ⊗ B ⊗ G = ⨁ k = 0 ∞ B ⊗ A k ⊗ G =\bigoplus_{k=0}^{\infty} B\otimes A^k\otimes G = k = 0 ⨁ ∞ B ⊗ A k ⊗ G = B ⊗ Rec ( A , G ) . =B\otimes\operatorname{Rec}(A,G). = B ⊗ Rec ( A , G ) .
Lemma B.4 (Commuting recurrences). For time-invariant kernels A A A and B B B and signal G G G over any semiring,
Rec ( A , Rec ( B , G ) ) = Rec ( B , Rec ( A , G ) ) . \operatorname{Rec}(A,\operatorname{Rec}(B,G))=\operatorname{Rec}(B,\operatorname{Rec}(A,G)). Rec ( A , Rec ( B , G )) = Rec ( B , Rec ( A , G )) . Proof.
Rec ( A , Rec ( B , G ) ) = ⨁ i ≥ 0 A i ⊗ ( ⨁ j ≥ 0 B j ⊗ G ) \operatorname{Rec}(A,\operatorname{Rec}(B,G))=\bigoplus_{i\ge0} A^i\otimes\left(\bigoplus_{j\ge0} B^j\otimes G\right) Rec ( A , Rec ( B , G )) = i ≥ 0 ⨁ A i ⊗ ( j ≥ 0 ⨁ B j ⊗ G ) = ⨁ i ≥ 0 ⨁ j ≥ 0 ( A i ⊗ B j ⊗ G ) =\bigoplus_{i\ge0}\bigoplus_{j\ge0}\left(A^i\otimes B^j\otimes G\right) = i ≥ 0 ⨁ j ≥ 0 ⨁ ( A i ⊗ B j ⊗ G ) = ⨁ j ≥ 0 ⨁ i ≥ 0 ( A i ⊗ B j ⊗ G ) =\bigoplus_{j\ge0}\bigoplus_{i\ge0}\left(A^i\otimes B^j\otimes G\right) = j ≥ 0 ⨁ i ≥ 0 ⨁ ( A i ⊗ B j ⊗ G ) = Rec ( B , Rec ( A , G ) ) . =\operatorname{Rec}(B,\operatorname{Rec}(A,G)). = Rec ( B , Rec ( A , G )) .
Algebraic identities over rings
Lemma B.5 (Inverse of Recurrence). For kernel A A A and signal G G G over a ring,
( I ⊖ A ) ⊗ Rec ( A , G ) = G . (I\ominus A)\otimes\operatorname{Rec}(A,G)=G. ( I ⊖ A ) ⊗ Rec ( A , G ) = G . Proof. Let F = Rec ( A , G ) F=\operatorname{Rec}(A,G) F = Rec ( A , G ) , so F = A ⊗ F ⊕ G F=A\otimes F\oplus G F = A ⊗ F ⊕ G . Then
( I ⊖ A ) ⊗ F = F ⊖ A ⊗ F = G . (I\ominus A)\otimes F=F\ominus A\otimes F=G. ( I ⊖ A ) ⊗ F = F ⊖ A ⊗ F = G .
Lemma B.6 (Fusion of Stacked Ring Recurrences). For kernels A A A and B B B and signal G G G over a ring,
Rec ( A , Rec ( B , G ) ) = Rec ( A ⊕ B ⊖ B ⊗ A , G ) . \operatorname{Rec}(A,\operatorname{Rec}(B,G))=\operatorname{Rec}(A\oplus B\ominus B\otimes A,G). Rec ( A , Rec ( B , G )) = Rec ( A ⊕ B ⊖ B ⊗ A , G ) .
Proof. Let F = Rec ( A , Rec ( B , G ) ) F=\operatorname{Rec}(A,\operatorname{Rec}(B,G)) F = Rec ( A , Rec ( B , G )) , so F = A ⊗ F ⊕ Rec ( B , G ) F=A\otimes F\oplus\operatorname{Rec}(B,G) F = A ⊗ F ⊕ Rec ( B , G ) and hence ( I ⊖ A ) ⊗ F = Rec ( B , G ) (I\ominus A)\otimes F=\operatorname{Rec}(B,G) ( I ⊖ A ) ⊗ F = Rec ( B , G ) . Therefore
( I ⊖ A ) ⊗ F = B ⊗ ( I ⊖ A ) ⊗ F ⊕ G (I\ominus A)\otimes F=B\otimes(I\ominus A)\otimes F\oplus G ( I ⊖ A ) ⊗ F = B ⊗ ( I ⊖ A ) ⊗ F ⊕ G
( I ⊖ B ) ⊗ ( I ⊖ A ) ⊗ F = G (I\ominus B)\otimes(I\ominus A)\otimes F=G ( I ⊖ B ) ⊗ ( I ⊖ A ) ⊗ F = G
( I ⊖ A ⊖ B ⊕ B ⊗ A ) ⊗ F = G (I\ominus A\ominus B\oplus B\otimes A)\otimes F=G ( I ⊖ A ⊖ B ⊕ B ⊗ A ) ⊗ F = G
F = Rec ( A ⊕ B ⊖ B ⊗ A , G ) . F=\operatorname{Rec}(A\oplus B\ominus B\otimes A,G). F = Rec ( A ⊕ B ⊖ B ⊗ A , G ) .
Lemma B.7 (Fusion of Parallel Ring Recurrences). For time-invariant kernels A A A and B B B and signals G G G and H H H over a ring,
Rec ( A , G ) ⊕ Rec ( B , H ) = Rec ( A ⊕ B ⊖ A ⊗ B , ( I ⊖ B ) ⊗ G ⊕ ( I ⊖ A ) ⊗ H ) . \operatorname{Rec}(A,G)\oplus\operatorname{Rec}(B,H)=\operatorname{Rec}(A\oplus B\ominus A\otimes B,(I\ominus B)\otimes G\oplus(I\ominus A)\otimes H). Rec ( A , G ) ⊕ Rec ( B , H ) = Rec ( A ⊕ B ⊖ A ⊗ B , ( I ⊖ B ) ⊗ G ⊕ ( I ⊖ A ) ⊗ H ) . Proof. Let F 1 = Rec ( A , G ) F_1=\operatorname{Rec}(A,G) F 1 = Rec ( A , G ) and F 2 = Rec ( B , H ) F_2=\operatorname{Rec}(B,H) F 2 = Rec ( B , H ) , so ( I ⊖ A ) ⊗ F 1 = G (I\ominus A)\otimes F_1=G ( I ⊖ A ) ⊗ F 1 = G and ( I ⊖ B ) ⊗ F 2 = H (I\ominus B)\otimes F_2=H ( I ⊖ B ) ⊗ F 2 = H . Setting F = F 1 ⊕ F 2 F=F_1\oplus F_2 F = F 1 ⊕ F 2 :
( I ⊖ A ) ⊗ ( I ⊖ B ) ⊗ F = ( I ⊖ B ) ⊗ G ⊕ ( I ⊖ A ) ⊗ H (I\ominus A)\otimes(I\ominus B)\otimes F=(I\ominus B)\otimes G\oplus(I\ominus A)\otimes H ( I ⊖ A ) ⊗ ( I ⊖ B ) ⊗ F = ( I ⊖ B ) ⊗ G ⊕ ( I ⊖ A ) ⊗ H ( I ⊖ A ⊖ B ⊕ A ⊗ B ) ⊗ F = ( I ⊖ B ) ⊗ G ⊕ ( I ⊖ A ) ⊗ H (I\ominus A\ominus B\oplus A\otimes B)\otimes F=(I\ominus B)\otimes G\oplus(I\ominus A)\otimes H ( I ⊖ A ⊖ B ⊕ A ⊗ B ) ⊗ F = ( I ⊖ B ) ⊗ G ⊕ ( I ⊖ A ) ⊗ H F = Rec ( A ⊕ B ⊖ A ⊗ B , ( I ⊖ B ) ⊗ G ⊕ ( I ⊖ A ) ⊗ H ) . F=\operatorname{Rec}(A\oplus B\ominus A\otimes B,(I\ominus B)\otimes G\oplus(I\ominus A)\otimes H). F = Rec ( A ⊕ B ⊖ A ⊗ B , ( I ⊖ B ) ⊗ G ⊕ ( I ⊖ A ) ⊗ H ) .
Lemma B.8 (Dilation with Additive Inverse). For time-invariant kernels A A A and B B B and signal G G G over a ring,
Rec ( A ⊕ B , G ) = Rec ( A ⊕ A ⊖ A 2 ⊕ B 2 , ( I ⊖ A ⊕ B ) ⊗ G ) . \operatorname{Rec}(A\oplus B,G)=\operatorname{Rec}(A\oplus A\ominus A^2\oplus B^2,(I\ominus A\oplus B)\otimes G). Rec ( A ⊕ B , G ) = Rec ( A ⊕ A ⊖ A 2 ⊕ B 2 , ( I ⊖ A ⊕ B ) ⊗ G ) . Proof. Let F = Rec ( A ⊕ B , G ) F=\operatorname{Rec}(A\oplus B,G) F = Rec ( A ⊕ B , G ) , so ( I ⊖ A ⊖ B ) ⊗ F = G (I\ominus A\ominus B)\otimes F=G ( I ⊖ A ⊖ B ) ⊗ F = G . Multiplying by ( I ⊖ A ⊕ B ) (I\ominus A\oplus B) ( I ⊖ A ⊕ B ) :
( I ⊖ A ⊕ B ) ⊗ ( I ⊖ A ⊖ B ) ⊗ F = ( I ⊖ A ⊕ B ) ⊗ G (I\ominus A\oplus B)\otimes(I\ominus A\ominus B)\otimes F=(I\ominus A\oplus B)\otimes G ( I ⊖ A ⊕ B ) ⊗ ( I ⊖ A ⊖ B ) ⊗ F = ( I ⊖ A ⊕ B ) ⊗ G ( ( I ⊖ A ) 2 ⊖ B 2 ) ⊗ F = ( I ⊖ A ⊕ B ) ⊗ G ((I\ominus A)^2\ominus B^2)\otimes F=(I\ominus A\oplus B)\otimes G (( I ⊖ A ) 2 ⊖ B 2 ) ⊗ F = ( I ⊖ A ⊕ B ) ⊗ G ( I ⊖ A ⊖ A ⊕ A 2 ⊖ B 2 ) ⊗ F = ( I ⊖ A ⊕ B ) ⊗ G (I\ominus A\ominus A\oplus A^2\ominus B^2)\otimes F=(I\ominus A\oplus B)\otimes G ( I ⊖ A ⊖ A ⊕ A 2 ⊖ B 2 ) ⊗ F = ( I ⊖ A ⊕ B ) ⊗ G F = Rec ( A ⊕ A ⊖ A 2 ⊕ B 2 , ( I ⊖ A ⊕ B ) ⊗ G ) . F=\operatorname{Rec}(A\oplus A\ominus A^2\oplus B^2,(I\ominus A\oplus B)\otimes G). F = Rec ( A ⊕ A ⊖ A 2 ⊕ B 2 , ( I ⊖ A ⊕ B ) ⊗ G ) .
Lemma B.9 (Decomposition of Squared Kernels). For kernel A A A and signal G G G over a ring,
Rec ( A 2 , G ) = Rec ( A , Rec ( ⊖ A , G ) ) . \operatorname{Rec}(A^2,G)=\operatorname{Rec}(A,\operatorname{Rec}(\ominus A,G)). Rec ( A 2 , G ) = Rec ( A , Rec ( ⊖ A , G )) . Proof. By Lemma 4.5 with B ≔ ⊖ A B\coloneqq\ominus A B : = ⊖ A :
Rec ( A , Rec ( ⊖ A , G ) ) = Rec ( A ⊕ ( ⊖ A ) ⊖ ( ⊖ A ) ⊗ A , G ) \operatorname{Rec}(A,\operatorname{Rec}(\ominus A,G))=\operatorname{Rec}(A\oplus(\ominus A)\ominus(\ominus A)\otimes A,G) Rec ( A , Rec ( ⊖ A , G )) = Rec ( A ⊕ ( ⊖ A ) ⊖ ( ⊖ A ) ⊗ A , G ) = Rec ( 0 τ ⊕ A 2 , G ) =\operatorname{Rec}(0_\tau\oplus A^2,G) = Rec ( 0 τ ⊕ A 2 , G ) = Rec ( A 2 , G ) . =\operatorname{Rec}(A^2,G). = Rec ( A 2 , G ) .
Lemma B.10 (Change of Basis). For kernels A A A and B B B and signal G G G over a ring,
Rec ( A , G ) = Rec ( A ⊖ B ⊗ ( I ⊖ A ) , ( I ⊕ B ) ⊗ G ) . \operatorname{Rec}(A,G)=\operatorname{Rec}(A\ominus B\otimes(I\ominus A),(I\oplus B)\otimes G). Rec ( A , G ) = Rec ( A ⊖ B ⊗ ( I ⊖ A ) , ( I ⊕ B ) ⊗ G ) . Proof. Let F = Rec ( A , G ) F=\operatorname{Rec}(A,G) F = Rec ( A , G ) , so ( I ⊖ A ) ⊗ F = G (I\ominus A)\otimes F=G ( I ⊖ A ) ⊗ F = G . Multiplying by ( I ⊕ B ) (I\oplus B) ( I ⊕ B ) :
( I ⊕ B ) ⊗ ( I ⊖ A ) ⊗ F = ( I ⊕ B ) ⊗ G (I\oplus B)\otimes(I\ominus A)\otimes F=(I\oplus B)\otimes G ( I ⊕ B ) ⊗ ( I ⊖ A ) ⊗ F = ( I ⊕ B ) ⊗ G ( I ⊖ A ⊕ B ⊖ B ⊗ A ) ⊗ F = ( I ⊕ B ) ⊗ G (I\ominus A\oplus B\ominus B\otimes A)\otimes F=(I\oplus B)\otimes G ( I ⊖ A ⊕ B ⊖ B ⊗ A ) ⊗ F = ( I ⊕ B ) ⊗ G F = Rec ( A ⊖ B ⊗ ( I ⊖ A ) , ( I ⊕ B ) ⊗ G ) . F=\operatorname{Rec}(A\ominus B\otimes(I\ominus A),(I\oplus B)\otimes G). F = Rec ( A ⊖ B ⊗ ( I ⊖ A ) , ( I ⊕ B ) ⊗ G ) .
Algebraic identities over idempotent semirings
Lemma B.11 (Dilation with Idempotence). For time-invariant kernels A A A and B B B and signal G G G over an idempotent semiring,
Rec ( A ⊕ B , G ) = Rec ( A ⊕ B 2 , ( I ⊕ B ) ⊗ G ) . \operatorname{Rec}(A \oplus B,G)=\operatorname{Rec}(A\oplus B^{2},(I\oplus B)\otimes G). Rec ( A ⊕ B , G ) = Rec ( A ⊕ B 2 , ( I ⊕ B ) ⊗ G ) . Proof. Since A A A and B B B are time-invariant and ⊕ \oplus ⊕ is idempotent, the binomial expansion collapses to
( A ⊕ B ) k = ⨁ i , j ≥ 0 i + j = k A i ⊗ B j , (A\oplus B)^{k}=\bigoplus_{\substack{i,j\geq0\\i+j=k}} A^{i}\otimes B^{j}, ( A ⊕ B ) k = i , j ≥ 0 i + j = k ⨁ A i ⊗ B j , and therefore
Rec ( A ⊕ B , G ) = ⨁ k > 0 ( A ⊕ B ) k ⊗ G = ⨁ i , j ≥ 0 A i ⊗ B j ⊗ G . \operatorname{Rec}(A\oplus B,G)=\bigoplus_{k>0}(A\oplus B)^{k}\otimes G=\bigoplus_{i,j\geq0}A^{i}\otimes B^{j}\otimes G. Rec ( A ⊕ B , G ) = k > 0 ⨁ ( A ⊕ B ) k ⊗ G = i , j ≥ 0 ⨁ A i ⊗ B j ⊗ G . Reindexing j j j by parity:
Rec ( A ⊕ B , G ) = ⨁ i , j ≥ 0 A i ⊗ B 2 j ⊗ G ⊕ ⨁ i , j ≥ 0 A i ⊗ B 2 j + 1 ⊗ G \operatorname{Rec}(A\oplus B,G)=\bigoplus_{i,j\geq0}A^{i}\otimes B^{2j}\otimes G\ \oplus\ \bigoplus_{i,j\geq0}A^{i}\otimes B^{2j+1}\otimes G Rec ( A ⊕ B , G ) = i , j ≥ 0 ⨁ A i ⊗ B 2 j ⊗ G ⊕ i , j ≥ 0 ⨁ A i ⊗ B 2 j + 1 ⊗ G = ⨁ i , j ≥ 0 A i ⊗ B 2 j ⊗ ( I ⊕ B ) ⊗ G =\bigoplus_{i,j\geq0}A^{i}\otimes B^{2j}\otimes(I\oplus B)\otimes G = i , j ≥ 0 ⨁ A i ⊗ B 2 j ⊗ ( I ⊕ B ) ⊗ G = ⨁ k > 0 ( A ⊕ B 2 ) k ⊗ ( I ⊕ B ) ⊗ G =\bigoplus_{k>0}(A\oplus B^{2})^{k}\otimes(I\oplus B)\otimes G = k > 0 ⨁ ( A ⊕ B 2 ) k ⊗ ( I ⊕ B ) ⊗ G = Rec ( A ⊕ B 2 , ( I ⊕ B ) ⊗ G ) . =\operatorname{Rec}(A\oplus B^{2},(I\oplus B)\otimes G). = Rec ( A ⊕ B 2 , ( I ⊕ B ) ⊗ G ) .
Lemma B.12 (Fusion of Idempotent Recurrences). For time-invariant kernels A A A and B B B and signal G G G over an idempotent semiring,
Rec ( A , Rec ( B , G ) ) = Rec ( A ⊕ B , G ) \operatorname{Rec}(A,\operatorname{Rec}(B,G))=\operatorname{Rec}(A\oplus B,G) Rec ( A , Rec ( B , G )) = Rec ( A ⊕ B , G ) Proof. Using the same expansion as in the proof of Lemma 4.11 :
Rec ( A ⊕ B , G ) = ⨁ i , j ≥ 0 A i ⊗ B j ⊗ G \operatorname{Rec}(A\oplus B,G)=\bigoplus_{i,j\geq0}A^{i}\otimes B^{j}\otimes G Rec ( A ⊕ B , G ) = i , j ≥ 0 ⨁ A i ⊗ B j ⊗ G = ⨁ i > 0 A i ⊗ ( ⨁ j > 0 B j ⊗ G ) =\bigoplus_{i>0}A^{i}\otimes\left(\bigoplus_{j>0}B^{j}\otimes G\right) = i > 0 ⨁ A i ⊗ ( j > 0 ⨁ B j ⊗ G ) = ⨁ i > 0 A i ⊗ Rec ( B , G ) =\bigoplus_{i>0}A^{i}\otimes\operatorname{Rec}(B,G) = i > 0 ⨁ A i ⊗ Rec ( B , G ) = Rec ( A , Rec ( B , G ) ) . =\operatorname{Rec}(A,\operatorname{Rec}(B,G)). = Rec ( A , Rec ( B , G )) .
Proof of path-based decomposition
Lemma C.1. Let F F F be a recurrence defined by F = Rec ( A , G ) F=\operatorname{Rec}(A,G) F = Rec ( A , G ) . We have
F = G p ⊕ ( ⨁ 1 ≤ q ≤ k 0 ≤ r < p p ≤ r + q B r ⊗ K q ⊗ F ) F=G_{p}\oplus\left(\bigoplus_{\substack{1\leq q\leq k\\0\leq r<p\\p\leq r+q}}B_{r}\otimes K_{q}\otimes F\right) F = G p ⊕ 1 ≤ q ≤ k 0 ≤ r < p p ≤ r + q ⨁ B r ⊗ K q ⊗ F Proof.
F = ⨁ n ≥ 0 B n ⊗ G F=\bigoplus_{n\geq0}B_{n}\otimes G F = n ≥ 0 ⨁ B n ⊗ G
= ( ⨁ 0 ≤ n < p B n ⊗ G ) ⊕ ( ⨁ n ≥ p B n ⊗ G ) = G p ⊕ ( ⨁ n ≥ p ⨁ 1 ≤ q ≤ k ⨁ 0 ≤ r < p p ≤ r + q ≤ n B r ⊗ K q ⊗ B n − r − q ⊗ G ) = G p ⊕ ( ⨁ 1 ≤ q ≤ k ⨁ n ≥ p ⨁ 0 ≤ r < p p ≤ r + q ≤ n B r ⊗ K q ⊗ B n − r − q ⊗ G ) = G p ⊕ ( ⨁ 1 ≤ q ≤ k ⨁ 0 ≤ r < p ⨁ p ≤ r + q n ≥ r + q B r ⊗ K q ⊗ B n − r − q ⊗ G ) = G p ⊕ ( ⨁ 1 ≤ q ≤ k ⨁ 0 ≤ r < p ⨁ n ′ ≥ 0 B r ⊗ K q ⊗ B n ′ ⊗ G ) = G p ⊕ ( ⨁ 1 ≤ q ≤ k ⨁ 0 ≤ r < p ⨁ p ≤ r + q B r ⊗ K q ⊗ F ) . \begin{aligned}
&= \left(\bigoplus_{0\le n<p} B_n\otimes G\right)\oplus\left(\bigoplus_{n\ge p} B_n\otimes G\right)\\
&=G_p\oplus\left(\bigoplus_{n\ge p}\bigoplus_{1\le q\le k}\bigoplus_{\substack{0\le r<p\\p\le r+q\le n}}B_r\otimes K_q\otimes B_{n-r-q}\otimes G\right)\\
&=G_p\oplus\left(\bigoplus_{1\le q\le k}\bigoplus_{n\ge p}\bigoplus_{\substack{0\le r<p\\p\le r+q\le n}}B_r\otimes K_q\otimes B_{n-r-q}\otimes G\right)\\
&=G_p\oplus\left(\bigoplus_{1\le q\le k}\bigoplus_{0\le r<p}\bigoplus_{\substack{p\le r+q\\n\ge r+q}}B_r\otimes K_q\otimes B_{n-r-q}\otimes G\right)\\
&=G_p\oplus\left(\bigoplus_{1\le q\le k}\bigoplus_{0\le r<p}\bigoplus_{n'\ge0}B_r\otimes K_q\otimes B_{n'}\otimes G\right)\\
&=G_p\oplus\left(\bigoplus_{1\le q\le k}\bigoplus_{0\le r<p}\bigoplus_{p\le r+q}B_r\otimes K_q\otimes F\right).
\end{aligned} = ( 0 ≤ n < p ⨁ B n ⊗ G ) ⊕ ( n ≥ p ⨁ B n ⊗ G ) = G p ⊕ n ≥ p ⨁ 1 ≤ q ≤ k ⨁ 0 ≤ r < p p ≤ r + q ≤ n ⨁ B r ⊗ K q ⊗ B n − r − q ⊗ G = G p ⊕ 1 ≤ q ≤ k ⨁ n ≥ p ⨁ 0 ≤ r < p p ≤ r + q ≤ n ⨁ B r ⊗ K q ⊗ B n − r − q ⊗ G = G p ⊕ 1 ≤ q ≤ k ⨁ 0 ≤ r < p ⨁ p ≤ r + q n ≥ r + q ⨁ B r ⊗ K q ⊗ B n − r − q ⊗ G = G p ⊕ 1 ≤ q ≤ k ⨁ 0 ≤ r < p ⨁ n ′ ≥ 0 ⨁ B r ⊗ K q ⊗ B n ′ ⊗ G = G p ⊕ 1 ≤ q ≤ k ⨁ 0 ≤ r < p ⨁ p ≤ r + q ⨁ B r ⊗ K q ⊗ F .
Lemma C.2. For any n , p n,p n , p where p < n p<n p < n ,
B n = ⨁ 1 ≤ q ≤ k 0 ≤ r < p p ≤ r + q ≤ n B r ⊗ K q ⊗ B n − r − q B_n=\bigoplus_{\substack{1\le q\le k\\0\le r<p\\p\le r+q\le n}}B_r\otimes K_q\otimes B_{n-r-q} B n = 1 ≤ q ≤ k 0 ≤ r < p p ≤ r + q ≤ n ⨁ B r ⊗ K q ⊗ B n − r − q
Proof.
B n = ⨁ 1 ≤ m ≤ n a 1 + ⋯ + a m = n ∀ i . 1 ≤ a i ≤ k K a 1 ⊗ ⋯ ⊗ K a m = ⨁ 1 ≤ q ≤ k ⨁ 1 ≤ j ≤ m ≤ n a j = q a 1 + ⋯ + a m = n a 1 + ⋯ + a j − 1 ≤ p a 1 + ⋯ + a j − 1 + q > p ∀ i . 1 ≤ a i ≤ k K a 1 ⊗ ⋯ ⊗ K q ⊗ ⋯ ⊗ K a m = ⨁ 1 ≤ q ≤ k 0 ≤ r < p p ≤ r + q ≤ n ⨁ 1 ≤ j ≤ m ≤ n a j = q a 1 + ⋯ + a m = n a 1 + ⋯ + a j − 1 = r ∀ i . 1 ≤ a i ≤ k K a 1 ⊗ ⋯ ⊗ K q ⊗ ⋯ ⊗ K a m = ⨁ 1 ≤ q ≤ k 0 ≤ r < p p ≤ r + q ≤ n ( ⨁ 1 ≤ m ≤ r a 1 + ⋯ + a m = r ∀ i . 1 ≤ a i ≤ k K a 1 ⊗ ⋯ ⊗ K a m ⊗ K q ⊗ ⨁ 1 ≤ m ≤ n − r − q a 1 + ⋯ + a m = n − r − q ∀ i . 1 ≤ a i ≤ k K a 1 ⊗ ⋯ ⊗ K a m ) = ⨁ 1 ≤ q ≤ k 0 ≤ r < p p ≤ r + q ≤ n B r ⊗ K q ⊗ B n − r − q \begin{aligned}
B_n=\bigoplus_{\substack{1\le m\le n\\a_1+\cdots+a_m=n\\\forall i.\ 1\le a_i\le k}}K_{a_1}\otimes\cdots\otimes K_{a_m} \\
&=\bigoplus_{\substack{1\le q\le k}}\bigoplus_{\substack{1\le j\le m\le n\\a_j=q\\a_1+\cdots+a_m=n\\a_1+\cdots+a_{j-1}\le p\\a_1+\cdots+a_{j-1}+q>p\\\forall i.\ 1\le a_i\le k}}K_{a_1}\otimes\cdots\otimes K_q\otimes\cdots\otimes K_{a_m} \\
&=\bigoplus_{\substack{1\le q\le k\\0\le r<p\\p\le r+q\le n}}\bigoplus_{\substack{1\le j\le m\le n\\a_j=q\\a_1+\cdots+a_m=n\\a_1+\cdots+a_{j-1}=r\\\forall i.\ 1\le a_i\le k}}K_{a_1}\otimes\cdots\otimes K_q\otimes\cdots\otimes K_{a_m} \\
&= \bigoplus_{\substack{1\le q\le k\\0\le r<p\\p\le r+q\le n}}
\left(
\bigoplus_{\substack{1\le m\le r\\a_1+\cdots+a_m=r\\\forall i.\ 1\le a_i\le k}}
K_{a_1}\otimes\cdots\otimes K_{a_m}
\otimes K_q\otimes
\bigoplus_{\substack{1\le m\le n-r-q\\a_1+\cdots+a_m=n-r-q\\\forall i.\ 1\le a_i\le k}}
K_{a_1}\otimes\cdots\otimes K_{a_m}
\right) \\
&= \bigoplus_{\substack{1\le q\le k\\0\le r<p\\p\le r+q\le n}}
B_r\otimes K_q\otimes B_{n-r-q}
\end{aligned} B n = 1 ≤ m ≤ n a 1 + ⋯ + a m = n ∀ i . 1 ≤ a i ≤ k ⨁ K a 1 ⊗ ⋯ ⊗ K a m = 1 ≤ q ≤ k ⨁ 1 ≤ j ≤ m ≤ n a j = q a 1 + ⋯ + a m = n a 1 + ⋯ + a j − 1 ≤ p a 1 + ⋯ + a j − 1 + q > p ∀ i . 1 ≤ a i ≤ k ⨁ K a 1 ⊗ ⋯ ⊗ K q ⊗ ⋯ ⊗ K a m = 1 ≤ q ≤ k 0 ≤ r < p p ≤ r + q ≤ n ⨁ 1 ≤ j ≤ m ≤ n a j = q a 1 + ⋯ + a m = n a 1 + ⋯ + a j − 1 = r ∀ i . 1 ≤ a i ≤ k ⨁ K a 1 ⊗ ⋯ ⊗ K q ⊗ ⋯ ⊗ K a m = 1 ≤ q ≤ k 0 ≤ r < p p ≤ r + q ≤ n ⨁ 1 ≤ m ≤ r a 1 + ⋯ + a m = r ∀ i . 1 ≤ a i ≤ k ⨁ K a 1 ⊗ ⋯ ⊗ K a m ⊗ K q ⊗ 1 ≤ m ≤ n − r − q a 1 + ⋯ + a m = n − r − q ∀ i . 1 ≤ a i ≤ k ⨁ K a 1 ⊗ ⋯ ⊗ K a m = 1 ≤ q ≤ k 0 ≤ r < p p ≤ r + q ≤ n ⨁ B r ⊗ K q ⊗ B n − r − q
Lemma C.3. For any p , l p,l p , l where l < p l<p l < p
G p = G l ⊕ ⨁ 1 ≤ q ≤ k 0 ≤ r < l l ≤ r + q B r ⊗ K q ⊗ G p − r − q G_p=G_l\oplus
\bigoplus_{\substack{1\le q\le k\\0\le r<l\\l\le r+q}}
B_r\otimes K_q\otimes G_{p-r-q} G p = G l ⊕ 1 ≤ q ≤ k 0 ≤ r < l l ≤ r + q ⨁ B r ⊗ K q ⊗ G p − r − q
Proof.
G p = ⨁ 0 ≤ n < p B n ⊗ G = ( ⨁ 0 ≤ n < l B n ⊗ G ) ⊕ ( ⨁ l ≤ n < p 1 ≤ q ≤ k 0 ≤ r < l l ≤ r + q ≤ n B r ⊗ K q ⊗ B n − r − q ⊗ G ) = G l ⊕ ( ⨁ 1 ≤ q ≤ k 0 ≤ r < l l ≤ r + q B r ⊗ K q ⊗ ⨁ r + q ≤ n < p B n − r − q ⊗ G ) = G l ⊕ ( ⨁ 1 ≤ q ≤ k 0 ≤ r < l l ≤ r + q B r ⊗ K q ⊗ ⨁ 0 ≤ n ′ < p − r − q B n ′ ⊗ G ) = G l ⊕ ⨁ 1 ≤ q ≤ k 0 ≤ r < l l ≤ r + q B r ⊗ K q ⊗ G p − r − q \begin{aligned}
G_p=\bigoplus_{0\le n<p}B_n\otimes G \\
&=\left(\bigoplus_{0\le n<l}B_n\otimes G\right)\oplus
\left(
\bigoplus_{\substack{l\le n<p\\1\le q\le k\\0\le r<l\\l\le r+q\le n}}
B_r\otimes K_q\otimes B_{n-r-q}\otimes G
\right) \\
&=G_l\oplus
\left(
\bigoplus_{\substack{1\le q\le k\\0\le r<l\\l\le r+q}}
B_r\otimes K_q\otimes
\bigoplus_{r+q\le n<p}B_{n-r-q}\otimes G
\right) \\
&=G_l\oplus
\left(
\bigoplus_{\substack{1\le q\le k\\0\le r<l\\l\le r+q}}
B_r\otimes K_q\otimes
\bigoplus_{0\le n'<p-r-q}B_{n'}\otimes G
\right) \\
&=G_l\oplus
\bigoplus_{\substack{1\le q\le k\\0\le r<l\\l\le r+q}}
B_r\otimes K_q\otimes G_{p-r-q}
\end{aligned} G p = 0 ≤ n < p ⨁ B n ⊗ G = ( 0 ≤ n < l ⨁ B n ⊗ G ) ⊕ l ≤ n < p 1 ≤ q ≤ k 0 ≤ r < l l ≤ r + q ≤ n ⨁ B r ⊗ K q ⊗ B n − r − q ⊗ G = G l ⊕ 1 ≤ q ≤ k 0 ≤ r < l l ≤ r + q ⨁ B r ⊗ K q ⊗ r + q ≤ n < p ⨁ B n − r − q ⊗ G = G l ⊕ 1 ≤ q ≤ k 0 ≤ r < l l ≤ r + q ⨁ B r ⊗ K q ⊗ 0 ≤ n ′ < p − r − q ⨁ B n ′ ⊗ G = G l ⊕ 1 ≤ q ≤ k 0 ≤ r < l l ≤ r + q ⨁ B r ⊗ K q ⊗ G p − r − q
VecRec’s domain-specific language
Figure 9. The domain-specific language used in VecRec.