    # Non-Rectangular Convolutions and (Sub-)Cadences with Three Elements

The discrete acyclic convolution computes the 2n-1 sums sum_i+j=k; (i,j) in [0,1,2,...,n-1]^2 (a_i b_j) in O(n log n) time. By using suitable offsets and setting some of the variables to zero, this method provides a tool to calculate all non-zero sums sum_i+j=k; (i,j) in (P cap Z^2) (a_i b_j) in a rectangle P with perimeter p in O(p log p) time. This paper extends this geometric interpretation in order to allow arbitrary convex polygons P with k vertices and perimeter p. Also, this extended algorithm only needs O(k + p(log p)^2 log k) time. Additionally, this paper presents fast algorithms for counting sub-cadences and cadences with 3 elements using this extended method.

## Authors

##### This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

## 1 Introduction

The convolution is a well-known and very useful method, which is not only closely linked to signal processing (e.g. ) but is also used to multiply polynomials (see [5, p. 905]) and large numbers (e.g. 

(written in German)) in quasi-linear time. The convolution can be efficiently computed with the fast Fourier transform or its counterpart in residue class rings, the number theoretic transform:

###### Theorem 1.

Let and be two sequences. The sequence with can be computed in operations.

The most well-known proofs use additions and multiplications of arbitrary complex numbers. However, with the finite register lengths of real-world computers, one must either cope with the roundoff errors or do all calculations in a different ring. In Appendix A.1, we show that a suitable ring is only dependent on and can be found in time if the generalized Riemann hypothesis is true.

The convolution can also be interpreted geometrically: Let and be sequences. Then the convolution calculates the partial sums

 ∑i+j=k(i,j)∈P∩Z2aibj,

where is the square given by .

This paper extends this geometric interpretation and shows that if is an arbitrary convex polygon with vertices and perimeter , the partial sums can be calculated in time.

We also use this extended method to solve an open problem of a string pattern called cadence. A cadence is given by an arithmetic progression of occurrences of the same character in a string such that the progression can not be extended to either side without extending the string as well. For example, in the string the indices corresponding to the “1”s form a -cadence. On the other hand, in the string the indices corresponding to the “1”s do not form a -cadence since, for example, the index is still inside of the string.

-cadences can be found naïvely in quadratic time. In the paper , a quasi-linear time algorithm for detecting the existence of -cadences was proposed, but this algorithm also detects false positives as the aforementioned string .

This paper fixes this issue and also extends the algorithm to the slightly more general notion of -partial--cadences. The resulting extended algorithm also allows counting those partial-cadences and only needs time. Using a method presented by Amir et al. in , this implies that all -partial--cadences can be counted in time.

Furthermore, we show that the output of the counting algorithm also allows for finding partial-cadences in time.

This paper also gives similar results for -sub-cadences.

For the time complexity, we assume that arithmetic operations with bits can be done in constant time. In particular, we want to be able to get the remainder of a division by a prime in constant time.

Also, in this paper, we assume a suitable alphabet. I.e. the characters are given by sufficiently small integers in order to allow constant time reading of a given character in the string and in order to allow sorting the characters.

## 2 (Sub-)Cadences and Their Definitions

The term cadence in the context of strings dates back to 1964 and was first introduced by Gardelle and Guilbaud in  (written in French). Since then, there were at least two other, slightly different and non-equivalent definitions given by Lothaire in  and Amir et al. in .

This paper uses the most restrictive definition of the cadence, which was introduced by Amir et al. in , and also uses their definition of the sub-cadence, which is equivalent to Gardelle’s cadence in  and Lothaire’s arithmetic cadence in .

A string of length is the concatenation of characters from an alphabet .

###### Definition 1.

A -sub-cadence is a triple of positive integers such that

 S[i]=S[i+d]=S[i+2d]=⋯=S[i+(k−1)d]

holds.

In this paper, cadences are additionally required to start and end close to the boundaries of the string:

###### Definition 2.

Since for any -sub-cadence the inequality holds, for any -cadence holds. This implies and thereby . It is therefore sufficient to omit the variable of a -cadence and just denote this -cadence by the pair .

###### Remark 1 (Comparison of the Definitions).
• The cadence as defined by Lothaire is just an ordered sequence of unequal indices such that the corresponding characters are equal.

• The cadence as defined by Gardelle and Guilbaud additionally requires the sequence to be an arithmetic sequence.

• The cadence as defined by Amir et al. and as used in this paper additionally requires that the cadence can not be extended in any direction without extending the string as well.

For the analysis of cadences with errors, we need two more definitions:

###### Definition 3.

A -cadence with at most errors is a tuple of integers such that and and hold and such that there are different integers with and

 S[i+π1d]=S[i+π2d]=S[i+π3d]⋯=S[i+πk−md].

A particularly interesting case of cadences with errors is given by the partial-cadences in which we know all positions where an error is allowed:

###### Definition 4.

For some different integers with , a -partial--cadence is a triple of positive integers with and such that

 S[i+π1d]=S[i+π2d]=S[i+π3d]⋯=S[i+πpd]

hold.

## 3 3-Sub-Cadences and Rectangular Convolutions

Lothaire showed over 20 years ago that sufficiently large strings are guaranteed to have sub-cadences of a given length:

###### Theorem 2 (Existence of Sub-Cadences (Lothaire )).

Let be an alphabet and an integer. There exists an integer such that every string containing at least characters has at least one -sub-cadence

However, this theorem does not provide the number of -sub-cadences of a given string.

In this section, we will show that -sub-cadences with a given character of a string of length can be efficiently counted in time. We will also show that arbitrary -sub-cadences of a string of length can be counted in time and that both counting algorithms allow to output different -sub-cadences in additional time if at least different -sub-cadences exist.

Let be a character. We will now count all -sub-cadences with character .

Let be a -sub-cadence. Since holds, the position of the middle occurrence of only depends on the sum of the index of first occurrence and the index of the third occurrence but does not depend on the individual indices of those two positions. Therefore, it is possible to determine the candidates for the middle occurrences with the convolution of the candidates of the first occurrence and the candidates of the third occurrence.

Let the sequence be given by the indicator function for in :

 δi:={1if$S[i]=σ$0if$S[i]≠σ$(thisincludes$i=0$)

With this definition, the product is if and only if and otherwise is . Therefore counts in how many ways the index lies in the middle of two . These partial sums can be calculated in time by convolution.

If

is odd or

holds, the index can not be the middle index of a -sub-cadence. If holds, the indicator function is , and therefore holds as well. Since is not a -sub-cadence, the output element contains one false positive. Additionally, for with and , the output element counts the combination as well as .

Therefore,

 sk:={c2k−12if$S[k]=σ$0if$S[k]≠σ$

counts exactly the number of -sub-cadences with character such that the second occurrence of has index . The sum of the is the number of total -sub-cadences with character .

Also, for each , all those -sub-cadences can be found in time by checking for each index whether holds.

If the character is rare, we can also follow the idea of Amir et al. in  for detecting -cadences with rare characters: If all occurrences of the character are known, the can be computed in time by computing every pair of those occurrences. Therefore:

###### Theorem 3.

For every character , the -sub-cadences with can be counted in time. Also, if all occurrences of are known, the -sub-cadences with can be counted in time.

Following the proof in , we can get all occurrences of every character by sorting the input string in time. This implies that the algorithm needs at most
time.

###### Theorem 4.

The number of all -sub-cadences can be counted in

 O(min(|Σ|nlogn,n3/2(logn)1/2))time.
###### Theorem 5.

After counting at least -sub-cadences, it is possible to output -sub-cadences in time.

## 4 Non-Rectangular Convolutions

In this section, we will extend the geometric interpretation of the convolution and show that for convex polygons with vertices and perimeter it is possible to calculate the partial sums

 ck=∑i+j=k(i,j)∈P∩Z2aibj

in time.

Let’s imagine a graph where all integer-coordinates have the value . We don’t need the convolution in order to determine the sum of the function values in a given rectangle since we can use the simple factorization in time. However, the convolution provides the partial sums on the -diagonals in almost the same time of .

We will now extend this geometric interpretation firstly to triangles with a vertical cathetus and a horizontal cathetus, then to arbitrary triangles and lastly to convex polygons. In order to do this, we will divide the given polygon in polygons and such that for each integer point the equality

holds, and we define

 (cp)k:=∑i+j=k(i,j)∈P+p∩Z2aibjand(cm)k:=−∑i+j=k(i,j)∈P−m∩Z2aibj.

By construction, holds. However, if the edges and vertices of the polygons and contain integer-points, we need to carefully decide for every of these polygons, which edges and vertices are supposed to be included in the polygons and which are excluded from the polygons.

###### Lemma 1.

Let be a triangle with a vertical cathetus and a horizontal cathetus and perimeter . Let also the sequences and be given.

Then the partial sums

 ck=∑i+j=k(i,j)∈P∩Z2aibj

can be calculated in time.

###### Proof.

The proof will be symmetrical with regard to horizontal and vertical mirroring. Therefore, without loss of generality, we will assume that is oriented as in Figure 1.

We first initialize the output vector

with zero. This takes time.

In the following proof, we assume that both catheti are included in the polygon and that the hypotenuse as well as its endpoints are excluded. If this is not the expected behavior, we can traverse the edges in time and for each integer-point on the edge, we can decrease/increase the corresponding by if necessary.

If is at most one, there is at most one integer-point in the triangle, and this point can be found in constant time. In this case, we only have to increase by .

If is bigger than one, we will separate the triangle into three disjoint parts as seen in Figure 1.

• The triangle of points with x-coordinate of at least ,

• the triangle of points with y-coordinate of at least and

• the red rectangle of points with x-coordinate of at most and y-coordinate of at most .

There are no integers bigger than but smaller than nor integers bigger than but smaller than . Therefore, each integer-point in is in exactly one of the three parts.

For the red rectangle, we can calculate the convolution and thereby get the corresponding partial sums in time. The partial sums corresponding to the sub-triangles are calculated recursively. Increasing the by the partial results leads to the final result.

Hence, the algorithm takes

 O(p+(log2p∑i=02i(p2ilogp2i))+2log2p)⊆O(logp∑i=0plogp)=O(p(logp)2)

time. ∎

We will now further extend this result to arbitrary triangles:

###### Lemma 2.

Let a triangle with perimeter and sequences and be given.

Then the partial sums

 ck=∑i+j=k(i,j)∈P∩Z2aibj

can be calculated in time.

###### Proof.

Let be the minimal and maximal x-coordinates and y-coordinates of the three vertices of the polygon . As in the last lemma, we first initialize the output vector .

Similarly to the last lemma, we can remove/add edges and vertices in linear time with respect to . Since the number of edges and vertices is constant, we ignore them for the sake of simplicity.

Let be the rectangle . Since has four edges but only has three vertices, at least one of the vertices of is also a vertex of . Without loss of generality, this vertex is .

Case 1:

The opposing vertex in also coincides with a vertex of (as in the left hand side of Figure 2):

Without loss of generality, we can assume that the third vertex of is above the diagonal from to . In this case, the partial sums corresponding to are given by the sum of the partial sums of the red triangles and the partial sums of the blue rectangle minus the partial sums of the lighter triangle.

There are only three triangles and one rectangle involved, and each of those polygons has perimeter . Furthermore, all triangles have a vertical cathetus and a horizontal cathetus. Therefore, using Lemma 1, we can calculate all partial sums in time.

Case 2:

The opposing vertex in does not coincide with a vertex of (as in the right hand side of Figure 2):

In this case, one vertex of lies on the right edge of and one vertex of lies on the upper edge of .

The wanted partial sums are in this case the difference of the partial sums of the rectangle and of the partial sums of the three red triangles. Again, we can calculate all partial sums in time.

Since both cases require time, this concludes the proof. ∎

Now we will extend this algorithm to convex polygons by dissecting them into triangles with sufficiently small perimeter.

###### Theorem 6.

Let be a convex polygon with vertices and perimeter . Let also the sequences and be given.

Then the partial sums

 ck=∑i+j=k(i,j)∈P∩Z2aibj

can be calculated in time.

###### Proof.

As in the last two Lemmata, we define to be the minimal and maximal x-coordinates and y-coordinates of the vertices of . Also, we first initialize the output vector . We further assume that none of the edges and vertices of is included in .

If is a triangle, then this Lemma simplifies to Lemma 2 and there is nothing left to prove.

If is a quadrilateral , as in the left hand side of Figure 3, then it can be partitioned into the triangles and where the edge is included in exactly one triangle and all other edges are excluded. The triangle inequality proves that and hold. Therefore, both triangles have a perimeter of at most . This implies that the partial sums can be calculated in

If is a polygon with more than four vertices, as in the right hand side of Figure 3, it can be partitioned into

• the polygon , which is given by the odd vertices without its edges,

• the red triangles with including the edge but excluding the other edges and the vertices,

• if is even, the triangle including the edge but excluding the other edges and the vertices.

By construction and triangle inequality, the perimeter of is at most . This, however, also implies that the total perimeter of the triangles is at most . The inequality

 ∑min(1,pi(logpi)2)≤k+∑(pi(logp)2)≤k+p(logp)2

implies that the algorithm needs time plus the time we need for processing . Since each step almost halves the number of vertices, we need steps. This results in a total time complexity of . ∎

In this section, we will show how the non-rectangular convolution helps counting the -partial--cadences with a given character in . We will further show that all -partial--cadences can be counted in time and that both counting algorithms allow to output of those partial-cadences in time.

As a special case, these results also hold for -cadences.

We further conclude from these results that the existence of -cadences with at most errors can be detected in time.

Without loss of generality, we will only deal with the case in this section.

###### Lemma 3.

Three positions , and form a -partial--cadence if and only if

• the equation holds,

• the equation holds and

• the inequalities

 0 ≥(b+1)x−(a+1)yb−a, (1) 0
###### Proof.

Define and . Then and . Furthermore, the equation holds if and only if and holds if and only if is an integer.

Additionally, using and , the four inequalities can be simplified to , , and .

Therefore, the lemma follows from the definition of the partial-cadence. ∎

The four inequalities hold if the points lie inside the convex quadrilateral given, as shown in Figure 4, by the corners

 A =(ank,bnk) B =((a+1)nk+1,(b+1)nk+1) C =((a+1)nk,(b+1)nk) D =(ank−1,bnk−1)

including the vertex and the edges between and as well as between and but excluding all other vertices and the edges between and as well as between and .

For given and , the third occurrence can be calculated with the equation directly without calculating and first. The corresponding partial sums

 ck=∑i+j=k(i,j)∈P∩Z2ai(b−c)bj(c−a)

can be calculated by using the partial sums

 ck=∑i+j=k(i,j)∈P′∩Z2a′ib′j

with and a polygon , which is derived from by stretching the first coordinate by and the second coordinate by . The perimeter of is at most times the perimeter of . Using the quadrilateral with perimeter

the polygon has perimeter . This proves the following three theorems.

###### Theorem 7.

For every character , the -partial--cadences with can be counted in time. Also, if all occurrences of are known, the -partial--cadences with can be counted in time.

###### Theorem 8.

The number of all -partial--cadences can be counted in

 O(min(|Σ|n(logn)2,n3/2logn))time.
###### Theorem 9.

After counting at least -partial--cadences, it is possible to output -partial--cadences in time.

Since every -cadence is an -partial--cadence, we also obtain the special case:

###### Corollary 1.

For every character , the -cadences with can be counted in time. Also, if all occurrences of are known, the -cadences with can be counted in time.

Therefore, the number of all -cadences can be counted in

 O(min(|Σ|n(logn)2,n3/2logn))time.

Also, after counting at least -cadences, it is possible to output -cadences in time.

Taking the sum over all possible triples , we can also search for -cadences with at most errors. It can be checked in

 O(min(|Σ|k3n(logn)2,k3n3/2logn))

time whether the given string has a -cadence with at most errors. However, since -cadences with less than errors are counted more than once, it seems to be difficult to determine the exact number of -cadences with at most errors.

## 6 Conclusion

This paper extends convolutions to arbitrary convex polygons. One might wonder whether these convolutions could be speed up or be further extended to non-convex polynomials.

Instead of just partitioning the interior of the polygon into triangles, it is also possible to identify polygons by the difference of a slightly bigger but less complex polygon and a triangle. However, if the algorithm presented in this paper is adapted to non-convex polygons, it can generate self-intersecting polygons. While the time-complexity stays the same for these polygons, it becomes hard to ensure that every vertex and every edge of the polygon is counted exactly once.

Another approach is given by Levcopoulos and Lingas in . This paper shows that any simple polygon can be decomposed into convex components in quasilinear time with only logarithmic blow-up. This paper also shows that if the input polygon is rectilinear, this partition only contains axis-aligned rectangles. Since the convolution handles rectangles quicker and more easily than triangles, this saves a logarithm. However, in general, it is not obvious how to transform arbitrary polygons into equivalent simple rectilinear polygons in quasilinear time without blowing-up the number of vertices too much.

The non-rectangular convolution, unlike the usual convolution, allows to define a dependence between the indices of the convoluted sequences. This dependence is not usable in applications like the multiplication of polynomials, and for many signal processing applications this extended method does not seem to bring any benefits either. However, in order to count the partial-cadences this dependence was essential. The non-rectangular convolution may also have future applications in image processing and convolutional neural networks.

In terms of cadences, this paper presents algorithms to count and find sub-cadences, cadences and partial-cadences with three elements. However, if there are linearly many -positions of -partial--cadences, the knowledge of those partial-cadences does not lead to a sub-quadratic-time-algorithm for determining the existence -cadences. On the other hand, it is also not shown that this problem needs quadratic time.

Also, the time-complexity for finding -cadences is quite pessimistic. If there are many -cadences, it is very likely that quite a few of these -cadences share one of their occurrences. These occurrences can be found in time. On the other hand, in the string , for example, there are linearly many -cadences but every second occurrence and every third occurrence only occurs in at most one of those -cadences.

## 7 acknowledgements

The first author discovered an error in the algorithm for determining the existence of 3-cadences in ”String cadences” of Amir et al., which led to false-positives. Travis Gagie explained this error to the second author at the CPM-Conference in Pisa. He also claimed that this problem should be solvable. Juliusz Straszyński showed that -sub-cadences beginning and ending in given intervals can efficiently be detected by convolution. Amihood Amir noted that we can also efficiently count these sub-cadences, which allows “subtractive” methods as used for arbitrary triangles.

## Appendix A appendix

### a.1 Convolutions

It is well-known that the discrete convolution can be calculated with complex arithmetic operations. However, if the convolution is calculated with the fast Fourier transform, the finite register lengths introduce roundoff errors. These errors can propagate and accumulate throughout the calculation.

Therefore, in order to calculate the convolution of integer sequences, it seems more convenient to use the number theoretic transform, which is the generalization of the fast Fourier transform from the field of the complex numbers to certain residue class rings.

In this section, we will show that after some precomputation in time it is possible to calculate these convolutions in time.

Agarwal and Burrus show in  that the cyclic convolution of two integer-vectors of length can be efficiently computed modulo a prime if is a multiple of .

Linnik proves in  that there are constants and such that for each , with , there is a prime of the form with . While Linnik himself did not provide the values of and , there are some upper bounds: For example, Xylouris proves in  that there is a such that for each , with , there is a prime of the form with . More explicitly, Bach and Sorenson present in  that if the generalized Riemann hypothesis holds, for each , with , there is a prime of the form with .

As a result, for each , there is a prime with . This also implies that the length of is at most times the length of . Therefore, such a prime number is a good modulus for the convolution of length or any of its divisors. It is left to show that such a prime can be efficiently found.

###### Theorem 10.

Let be an integer. A prime with can be found in time.

###### Proof.

The main idea is to use the sieve of Eratosthenes to first find all primes up to and then sieve only the numbers up to that are congruent to modulo with these primes.

On the one hand, since holds, all numbers left after the second sieving are primes. On the other hand, the result of Bach and Sorenson in  guarantees that if the generalized Riemann hypothesis holds, there is a prime left. Also, by construction, all primes left fulfill this theorem.

It remains to be shown that this algorithm can be done in time.

For the usual sieve of Eratosthenes, one prepares a Boolean array for the first numbers. Then, for each number that has not been marked as non-prime, every multiple is marked as non-prime. Afterwards, all non-marked numbers are returned. The majority of the time is spend for the marking. This takes

 O⎛⎜ ⎜⎝2nlogn∑p=2pisprime2nlognp⎞⎟ ⎟⎠=O⎛⎜ ⎜⎝nlogn2nlogn∑p=2pisprime1p⎞⎟ ⎟⎠=O(n(logn)(loglogn))

time. The last equality is given by Mertens in [10, p. 46] (written in German) and the inequality .

For the second part, we have a much larger interval of numbers. However, since we only have to consider the first residue class, only every -th number has to be considered. Therefore we need

 O⎛⎜ ⎜⎝2nlogn∑p=2pisprime2(nlogn)2np⎞⎟ ⎟⎠=O⎛⎜ ⎜⎝n(logn)22nlogn∑p=2pisprime1p⎞⎟ ⎟⎠=O(n(logn)2(loglogn))

markings. Using the extended Euclidean algorithm, for every prime , we can find the smallest such that in time. Summing up over all primes, this takes

 O⎛⎜ ⎜⎝2nlogn∑p=2pisprimelogn⎞⎟ ⎟⎠⊆O(n(logn)2)

time.

This concludes the proof. ∎

###### Remark 2.

The prime number theorem states that the number of primes smaller than asymptotically behaves like . Dirichlet’s prime number theorem states that for a given and a sufficiently large , the prime numbers are evenly distributed in all residue classes with .

Therefore, for a given and sufficiently large , we should expect circa prime numbers of the form that are smaller than . One might therefore hope that it is possible to guess logarithmically many numbers smaller than in the right residue class, and then test in time whether this number is prime.

However, the “sufficient largeness” of depends on . Therefore, these theorems do not provide the number of suitable primes smaller than, for example, . Also, since the generation of suitable primes can be done in quasilinear time, the randomized shortcut is not necessary.

It is not only possible to find a suitable modulus for the number theoretic transform, but we can also find a suitable -th root:

###### Theorem 11.

Let be a prime with and .

A -th root of unity modulo can be found in time.

###### Proof.

Let for an odd number .

Firstly, we will show that a residue is a -th root of unity modulo if and only if is a quadratic nonresidue modulo .

Since is prime, there is a primitive root modulo .

Let . Then has the order . Therefore, has order if and only if is odd. On the other hand, if is even, then is a quadratic residue, and if is odd, then is a quadratic nonresidue. This implies that is a -th root of unity modulo if and only if is a quadratic nonresidue modulo .

Ankeny shows in  that if the generalized Riemann hypothesis holds, there is a quadratic nonresidue in the first residue classes. For any residue it can be tested with multiplications and modulo operations whether has order . As byproduct we get . If and only if has order , the power has order .

Therefore, a -th root of unity modulo can be found in time. ∎

Therefore, we can efficiently compute the integer-convolution with the help of the number theoretic transform.

###### Theorem 12.

For a given integer , we can find a modulus and a suitable root in time such that it is possible to calculate the acyclic convolution modulo of two sequences of length in time afterwards.

###### Proof.

The acyclic convolution of sequences of length can be derived from a cyclic convolution of sequences with lengths of at least . Therefore, it is sufficient to prepare with .

For this length, the last two theorems state that a suitable modulus and a suitable -th root of unity can be found in .

Afterwards, for every