Post · 7 min read· pp. 72–77

Multiplying a Toeplitz matrix by a vector

These are some notes on how to efficiently multiply a Toeplitz matrix by a vector. I was writing these for myself while implementing the new amortized KZG proofs by Feist and Khovratovich, but I thought they might be useful for you too.

Preliminaries

We use column vector notation for all vectors. If [a,b,c][a, b, c] is a row vector, then [a,b,c]T[a,b,c]^T denotes its transpose: i.e., the column vector [a b c]\begin{bmatrix}a \\\\\ b \\\\\ c\end{bmatrix}.

What’s a Toeplitz (and a circulant) matrix?

A Toeplitz matrix (e.g. of size $4\times 4$) looks like this:

[a0a−1a−2a−3a1a0a−1a−2a2a1a0a−1a3a2a1a0]\begin{bmatrix} a_0 & a_{-1} & a_{-2} & a_{-3}\\ a_1 & a_0 & a_{-1} & a_{-2}\\ a_2 & a_1 & a_0 & a_{-1}\\ a_3 & a_2 & a_1 & a_0 \end{bmatrix}

Note the odd use of negative indices here, since typically we usually use positive numbers to index. It’s just convenient for notation to use negative indices.

In other words, it’s a square matrix where the entries “repeat diagonally.” A concrete example would be:

[7115637115837111837]\begin{bmatrix} 7 & 11 & 5 & 6 \\ 3 & 7 & 11 & 5 \\ 8 & 3 & 7 & 11 \\ 1 & 8 & 3 & 7 \end{bmatrix}

A circulant matrix CC is a special form of Toeplitz matrix:

[a0a3a2a1a1a0a3a2a2a1a0a3a3a2a1a0]\begin{bmatrix} a_0 & a_3 & a_2 & a_1\\ a_1 & a_0 & a_3 & a_2\\ a_2 & a_1 & a_0 & a_3\\ a_3 & a_2 & a_1 & a_0 \end{bmatrix}

In other words, each row is shifted/rotated to the right by 1 entry. (Or, alternatively, each column is shifted/rotated down by 1 entry.)

In general, note that any circulant matrix CnC_n of size n×nn\times n has a vector representation:

an⃗=[a0,a1,…,an−1]\vec{a_n}=[a_0, a_1, \dots, a_{n-1}]

Also, note that a circulant matrix is a particular type of a Toeplitz matrix where a−i=an−i,∀i∈[n−1]a_{-i} = a_{n-i}, \forall i \in[n-1].

Here are two examples of circulant matrices:

C4=[71156 67115 56711 11567],C4′=[7183 3718 8371 1837]C_4=\begin{bmatrix} 7 & 11 & 5 & 6 \\\\\ 6 & 7 & 11 & 5 \\\\\ 5 & 6 & 7 & 11 \\\\\ 11 & 5 & 6 & 7 \end{bmatrix}, C_4'=\begin{bmatrix} 7 & 1 & 8 & 3 \\\\\ 3 & 7 & 1 & 8 \\\\\ 8 & 3 & 7 & 1 \\\\\ 1 & 8 & 3 & 7 \end{bmatrix}

Importantly, a circulant matrix is diagonalizable by the DFT matrix (although we won’t explain why).

First, recall an example of a DFT matrix (e.g., of size $4 \times 4$):

F4=[1111 1(w)1(w)2(w)3 1(w2)1(w2)2(w2)3 1(w3)1(w3)2(w3)3]F_4=\begin{bmatrix} 1 & 1 & 1 & 1 \\\\\ 1 & (w)^1 & (w)^2 & (w)^3 \\\\\ 1 & (w^2)^1 & (w^2)^2 & (w^2)^3 \\\\\ 1 & (w^3)^1 & (w^3)^2 & (w^3)^3 \end{bmatrix}

What we’re saying is that a circulant matrix CnC_n of size n×nn\times n can be written as:

Cn=(Fn)−1diag(Fnan⃗)FnC_n = (F_n)^{-1} \mathsf{diag}(F_n \vec{a_n}) F_n

Here, an⃗=[a0,…,an−1]\vec{a_n} = [a_0, \dots, a_{n-1}] is the vector representation of CnC_n as discussed before (see above). Also, diag(Fna⃗)\mathsf{diag}(F_n \vec{a}) is the n×nn\times n diagonal matrix whose diagonal entries are the entries from Fnan⃗F_n\vec{a_n} (i.e., the entry at position (i,i)(i,i) is the iith entry in Fnan⃗F_n\vec{a_n}) and all other entries are 0.

Multiplying a circulant matrix by a vector

Let y=DFT(x⃗)=Fnx⃗y=\mathsf{DFT}(\vec{x}) = F_n \vec{x} denote the DFT of a vector x⃗\vec{x} and let x⃗=DFT−1(y)=Fn−1y⃗\vec{x}=\mathsf{DFT}^{-1}(y)=F_n^{-1} \vec{y} denote the inverse DFT.

If CnC_n is circulant with vector representation an⃗\vec{a_n}, then multiplying it by a size-nn vector x⃗\vec{x} can be written as:

Cnx⃗=((Fn)−1diag(Fnan⃗)Fn)x⃗=(Fn)−1(diag(Fnan⃗)(Fnx⃗))=DFT−1(diag(DFT(an⃗))DFT(x⃗))=DFT−1(diag(v⃗)y⃗)=DFT−1(v⃗∘y⃗)=DFT−1(u⃗)\begin{align} C_n \vec{x} &= \left((F_n)^{-1} \mathsf{diag}(F_n\vec{a_n}) F_n\right)\vec{x}\\ &= (F_n)^{-1} (\mathsf{diag}(F_n\vec{a_n}) (F_n \vec{x}))\\ &= \mathsf{DFT}^{-1}(\mathsf{diag}(\mathsf{DFT}(\vec{a_n})) \mathsf{DFT}(\vec{x}))\\ &= \mathsf{DFT}^{-1}(\mathsf{diag}(\vec{v}) \vec{y})\\ &= \mathsf{DFT}^{-1}(\vec{v} \circ \vec{y})\\ &= \mathsf{DFT}^{-1}(\vec{u}) \end{align}

In other words, what we must do is:

  • Compute y⃗\vec{y} by doing a DFT on x⃗\vec{x} (in Θ(nlog⁡n)\Theta(n\log{n}) time)
  • Compute v⃗\vec{v} by doing a DFT on an⃗\vec{a_n} (in Θ(nlog⁡n)\Theta(n\log{n}) time)
  • Compute the Hadamard product u⃗=v⃗∘y⃗\vec{u}=\vec{v} \circ \vec{y},
    • (Since that’s what happens when you multiply a diagonal matrix by a vector.)
  • Do an inverse DFT on u⃗\vec{u} (in Θ(nlog⁡n)\Theta(n\log{n}) time).

Thus, we can compute Cnx⃗C_n \vec{x} in Θ(nlog⁡n)\Theta(n\log{n}) time.

Multiplying a Toeplitz matrix by a vector

To multiply a Toeplitz matrix TnT_n by a vector x⃗\vec{x}, we’ll embed the matrix in a circulant matrix C2nC_{2n} in such a manner that the first nn entries of C2nx⃗C_{2n}\vec{x} will equal exactly Tnx⃗T_n\vec{x}.

We’ll use T4T_4 as an example:

T4=[a0a−1a−2a−3 a1a0a−1a−2 a2a1a0a−1 a3a2a1a0]T_4 = \begin{bmatrix} a_0 & a_{-1} & a_{-2} & a_{-3}\\\\\ a_1 & a_0 & a_{-1} & a_{-2}\\\\\ a_2 & a_1 & a_0 & a_{-1}\\\\\ a_3 & a_2 & a_1 & a_0 \end{bmatrix}

We want to build a circulant matrix C8C_8 from T4T_4 such that:

C8[x⃗ 0⃗]=[T4x⃗ ?]C_8 \begin{bmatrix} \vec{x} \\\\\ \vec{0} \end{bmatrix} = \begin{bmatrix} T_4 \vec{x} \\\\\ ? \end{bmatrix}

Note that we don’t care what we get in the last nn entries of the result, which we denote with a question mark. Also, 0⃗\vec{0} denotes the vector of n=4n=4 zeros.

If we had such a C8C_8, then we could multiply it with [x⃗ 0⃗]\begin{bmatrix}\vec{x}\\\\\ \vec{0}\end{bmatrix} using the Θ(nlog⁡n)\Theta(n\log{n}) multiplication algorithm from the previous section and efficiently compute T4x⃗T_4\vec{x}.

We’ll build C8C_8 from T4T_4 and some other, to be determined matrix which we denote using B4B_4.

C8=[T4B4 B4T4]C_8 = \begin{bmatrix} T_4 & B_4 \\\\\ B_4 & T_4 \end{bmatrix}

Note that this gives us what we want: (C_8 \begin{bmatrix}\vec{x}\\\ \vec{0}\end{bmatrix} = \begin{bmatrix} T_4 & B_4 \\\ B_4 & T_4 \end{bmatrix} \begin{bmatrix}\vec{x}\\\ \vec{0}\end{bmatrix}= \begin{bmatrix}T_4\vec{x}\\\ B_4\vec{x}\end{bmatrix})

In other words, the first nn entries of the product are indeed equal to T4x⃗T_4 \vec{x}, independent of what we pick for B4B_4.

But for us to efficiently compute the product, we’ll need to pick a B4B_4 that makes C8C_8 circulant. So let’s look at what C8C_8 looks like with just the two T4T_4’s in it:

C8=[a0a−1a−2a−3???? a1a0a−1a−2???? a2a1a0a−1???? a3a2a1a0????????a0a−1a−2a−3 ????a1a0a−1a−2 ????a2a1a0a−1 ????a3a2a1a0]C_8 = \begin{bmatrix} a_0 & a_{-1} & a_{-2} & a_{-3} & ? & ? & ? & ? \\\\\ a_1 & a_0 & a_{-1} & a_{-2} & ? & ? & ? & ? \\\\\ a_2 & a_1 & a_0 & a_{-1} & ? & ? & ? & ? \\\\\ a_3 & a_2 & a_1 & a_0 & ? & ? & ? & ? \\\\ ? & ? & ? & ? & a_0 & a_{-1} & a_{-2} & a_{-3}\\\\\ ? & ? & ? & ? & a_1 & a_0 & a_{-1} & a_{-2}\\\\\ ? & ? & ? & ? & a_2 & a_1 & a_0 & a_{-1}\\\\\ ? & ? & ? & ? & a_3 & a_2 & a_1 & a_0 \end{bmatrix}

We can fill in part of the puzzle to keep C8C_8 circulant:

C8=[a0a−1a−2a−3???? a1a0a−1a−2a−3??? a2a1a0a−1a−2a−3?? a3a2a1a0a−1a−2a−3??a3a2a1a0a−1a−2a−3 ??a3a2a1a0a−1a−2 ???a3a2a1a0a−1 ????a3a2a1a0]C_8 = \begin{bmatrix} a_0 & a_{-1} & a_{-2} & a_{-3} & ? & ? & ? & ? \\\\\ a_1 & a_0 & a_{-1} & a_{-2} & \mathbf{a_{-3}} & ? & ? & ? \\\\\ a_2 & a_1 & a_0 & a_{-1} & \mathbf{a_{-2}} & \mathbf{a_{-3}} & ? & ? \\\\\ a_3 & a_2 & a_1 & a_0 & \mathbf{a_{-1}} & \mathbf{a_{-2}} & \mathbf{a_{-3}} & ? \\\\ ? & \mathbf{a_3} & \mathbf{a_2} & \mathbf{a_1} & a_0 & a_{-1} & a_{-2} & a_{-3}\\\\\ ? & ? & \mathbf{a_3} & \mathbf{a_2} & a_1 & a_0 & a_{-1} & a_{-2}\\\\\ ? & ? & ? & \mathbf{a_3} & a_2 & a_1 & a_0 & a_{-1}\\\\\ ? & ? & ? & ? & a_3 & a_2 & a_1 & a_0 \end{bmatrix}

By now, you can tell that B4B_4 can be set to:

B4=[?a3a2a1 a−3?a3a2 a−2a−3?a3 a−1a−2a−3?]B_4 = \begin{bmatrix} ? & \mathbf{a_3} & \mathbf{a_2} & \mathbf{a_1} \\\\\ \mathbf{a_{-3}} & ? &\mathbf{a_3} & \mathbf{a_2} \\\\\ \mathbf{a_{-2}} &\mathbf{a_{-3}} & ? &\mathbf{a_3} \\\\\ \mathbf{a_{-1}} & \mathbf{a_{-2}} & \mathbf{a_{-3}} & ? \end{bmatrix}

Since the only constraint for the diagonal elements is to be the same, we’ll set them to a0a_0. So, the final C8C_8 will be:

C8=[a0a−1a−2a−3a0a3a2a1 a1a0a−1a−2a−3a0a3a2 a2a1a0a−1a−2a−3a0a3 a3a2a1a0a−1a−2a−3a0a0a3a2a1a0a−1a−2a−3 a−3a0a3a2a1a0a−1a−2 a−2a−3a0a3a2a1a0a−1 a−1a−2a−3a0a3a2a1a0]C_8 = \begin{bmatrix} a_0 & a_{-1} & a_{-2} & a_{-3} & \mathbf{a_0} & \mathbf{a_3} & \mathbf{a_2} & \mathbf{a_1} \\\\\ a_1 & a_0 & a_{-1} & a_{-2} & \mathbf{a_{-3}} & \mathbf{a_0} & \mathbf{a_3} & \mathbf{a_2} \\\\\ a_2 & a_1 & a_0 & a_{-1} & \mathbf{a_{-2}} & \mathbf{a_{-3}} & \mathbf{a_0} & \mathbf{a_3} \\\\\ a_3 & a_2 & a_1 & a_0 & \mathbf{a_{-1}} & \mathbf{a_{-2}} & \mathbf{a_{-3}} & \mathbf{a_0}\\\\ \mathbf{a_0} & \mathbf{a_3} & \mathbf{a_2} & \mathbf{a_1} & a_0 & a_{-1} & a_{-2} & a_{-3}\\\\\ \mathbf{a_{-3}} & \mathbf{a_0} & \mathbf{a_3} & \mathbf{a_2} & a_1 & a_0 & a_{-1} & a_{-2}\\\\\ \mathbf{a_{-2}} & \mathbf{a_{-3}} & \mathbf{a_0} & \mathbf{a_3} & a_2 & a_1 & a_0 & a_{-1}\\\\\ \mathbf{a_{-1}} & \mathbf{a_{-2}} & \mathbf{a_{-3}} & \mathbf{a_0} & a_3 & a_2 & a_1 & a_0 \end{bmatrix}

The question that remains to be answered is what is the vector representation a8⃗\vec{a_8} of C8C_8, since that’s what we’ll need to efficiently evaluate C8x⃗C_8\vec{x} and thus T4x⃗T_4\vec{x}.

The answer is, as before, the elements in the first columns of C8C_8, which are:

a8⃗=[a0,a1,a2,a3,a0,a−3,a−2,a−1]T\vec{a_8}=[ a_0, a_1, a_2, a_3, a_0, a_{-3}, a_{-2}, a_{-1} ]^T

Thus, applying the algorithm for circulant matrices from before, what we must do is:

  • Build a2n⃗\vec{a_{2n}} from the entries {an−1,an−2,…,a1,a0,a−1,…,a−(n−1)}\{a_{n-1}, a_{n-2}, \dots, a_1, a_0, a_{-1}, \dots, a_{-(n-1)}\} of the Toeplitz matrix TnT_n
  • Compute y⃗\vec{y} by doing a DFT on [x⃗,0⃗]T[\vec{x}, \vec{0}]^T
  • Compute v⃗\vec{v} by doing a DFT on a2n⃗\vec{a_{2n}} (e.g., on a8⃗\vec{a_8} from above)
  • Compute the Hadamard product u⃗=v⃗∘y⃗\vec{u}=\vec{v} \circ \vec{y},
  • Do an inverse DFT on u⃗\vec{u}
  • The product Tnx⃗T_n \vec{x} consists of the first nn entries of the resulting vector