Definition and Properties of Tensor Products
Definition and Properties of Tensor Products
212
(a) Original.
Figure 7.1: The results of smoothing an image with the filter {1/4, 1/2, 1/4}
horizontally, vertically, and both. The pixels are shown as disks with intensity
corresponding to the pixel values.
7.1
1
and EN = {ei }N
i=0
is a basis for LM,N (R), the set of M N -matrices. This basis is often referred
to as the standard basis for LM,N (R).
An image can simply be thought of as a matrix in LM,N (R). With this
definition of tensor products, we can define operations on images by extending
the one-dimensional filtering operations defined earlier.
Definition 7.3 (Tensor product of matrices). If S : RM RM and T : RN
RN are matrices, we define the linear mapping S T : LM,N (R) LM,N (R)
by linear extension of (S T )(ei ej ) = (Sei ) (T ej ). The linear mapping
S T is called the tensor product of the matrices S and T .
214
A couple of remarks are in order. First, from linear algebra we know that,
when T is linear mapping from V and T (v i ) is known for a basis {v i }i of V ,
T is uniquely determined. In particular, since the {ei ej }i,j form a basis,
there exists a unique linear transformation S T so that (S T )(ei ej ) =
(Sei ) (T ej ). This unique linear transformation is what we call the linear
extension from the values in the given basis.
Secondly S T also satisfies (S T )(x y) = (Sx) (T y). This follows
from
(S T )(x y) = (S T )((
xi ei ) (
yj ej )) = (S T )(
xi yj (ei ej ))
=
i,j
xi yj (S T )(ei ej ) =
xi yj Sei ((T ej ))T = S(
i,j
i,j
i,j
xi yj (Sei ) (T ej )
xi ei )(T (
yj ej ))T
Here we used the result from Exercise 5. Linear extension is necessary anyway,
since only rank 1 matrices have the form x y.
Example 7.4 (Smoothing an image). Assume that S and T are both filters,
and that S = T = {1/4, 1/2, 1/4}. Let us set M = 5 and N = 7, and let us
compute (S T )(e2 e3 ). We have that
(S T )(e2 e3 ) = (Se2 )(T e3 )T = (col2 S)(col3 T )T .
Since
2
1
1
0
S=
4
0
1
we find that
0
1
1
2 1 0
4
4
1
0
1
2
1
0
0
0
1
2
1
0
0
0
1
2
1
2
1
0
1
0
T =
4
0
0
1
1
0
1
2
0
0
1
0
0 =
16
0
0
1
2
1
0
0
0
0
0
0
0
0
0
0
1
2
1
0
0
0
0
1
2
1
0
0
0
1
2
1
0
0
0
2
4
2
0
0
0
0
1
2
1
0
0
1
2
1
0
0
0
0
0
1
2
1
0
0
0
0
0
0
0
0
,
0
1
2
0
0
0
0
We recognize here the computational molecule from Example 6.16 for smoothing
an image. More generally it is not hard to see that (S T )(ei ej ) is the
matrix where the same computational molecule is placed with its centre at (i, j).
215
Since this formula is valid when applied to any tensor product of two vectors,
it is also valid when applied to any matrix:
((T T )X)i,j
1
=
(Xi1,j1 + 2Xi,j1 + 2Xi1,j + 4Xi,j + 2Xi,j+1
16
+ 2Xi+1,j + Xi+1,j+1 )
This again confirms that the computational molecule given by Equation 6.3 in
Example 6.16 is the tensor product of the filter {1/4, 1/2, 1/4} with itself.
While we have seen that the computational molecules from Chapter 1 can
be written as tensor products, not all computational molecules can be written
as tensor products: we need of course that the molecule is a rank 1 matrix, since
matrices which can be written as a tensor product always have rank 1.
The tensor product can be expressed explicitly in terms of matrix products.
Theorem 7.5. If S : RM RM and T : RN RN are matrices, the action
of their tensor product on a matrix X is given by (S T )X = SXT T for any
X LM,N (R).
Proof. We have that
(S T )(ei ej ) = (Sei ) (T ej )
This means that (S T )X = SXT T for any X LM,N (R), since equality holds
on the basis vectors ei ej .
This leads to the following implementation for the tensor product of matrices:
Theorem 7.6 (Implementation of a tensor product of matrices). If S : RM
RM , T : RN RN are matrices, and X LM,N (R), we have that (S T )X
can be computed as follows:
1. Apply S to every column of X.
2. Transpose the resulting matrix.
3. Apply T to every column in the resulting matrix.
4. Transpose the resulting matrix.
This recipe for computing (S T )X is perhaps best seen if we write
(S T )X = SXT T = (T (SX)T )T .
(7.1)
In the first step above we compute SX, in the second step (SX)T , in the third
step T (SX)T , in the fourth step (T (SX)T )T . The reason for writing the tensor
product this way, as an operation column by column, has to do with with that
S and T are mostly filters for our purposes, and that we want to reuse ecient
implementations instead of performing full matrix multiplications, just as we
decided to express a wavelet transformation in terms of filters. The reason for
using columns instead of rows has to do with that we have expressed filtering
as a matrix by column multiplication. Note that this choice of using columns
instead of rows should be influenced by how the computer actually stores values
in a matrix. If these values are stored column by column, performing operations
columnwise may be a good idea, since then the values from the matrix are read
in the same order as they are stored. If matrix values are stored row by row,
it may be a good idea to rewrite the procedure above so that operations are
performed row by row also (see Exercise 7).
Theorem 7.6 leads to the following algorithm for computing the tensor product of matrices:
[M,N]=size(X);
for col=1:N
X(:,col)=S*X(:,col);
end
X=X
for col=1:M
X(:,col)=T*X(:,col);
end
X=X;
217
This algorithm replaces the rows and columns in X at each step. In the following, S = T in most cases. In this case we can replace with the following
algorithm, which is even simpler:
for k=1:2
for col=1:size(X,2)
X(:,col)=S*X(:,col);
end
X=X;
end
In an ecient algorithm, we would of course replace the matrix multiplications
with S and T with ecient implementations.
If we want to apply a sequence of tensor products of filters to a matrix, the
order of the operations does not matter. This will follow from the next result:
Corollary 7.7. If S1 T1 and S2 T2 are two tensor products of one dimensional filters, then (S1 T1 )(S2 T2 ) = (S1 S2 ) (T1 T2 ).
Proof. By Theorem 7.5 we have that
(S1 T1 )(S2 T2 )X = S1 (S2 XT2T )T1T = (S1 S2 )X(T1 T2 )T = ((S1 S2 )(T1 T2 ))X.
for any X LM,N (R). This proves the result.
Suppose that we want to apply the operation S T to an image. We can
write
S T = (S I)(I T ) = (I T )(S I).
(7.2)
Moreover, from Theorem 7.5 it follows that
(S I)X = SX
(I T )X = XT T = (T X T )T .
This means that S I corresponds to applying S to each column in X, and I T
corresponds to applying T to each row in X. When S and T are smoothing filters, this is what we refered to as vertical smoothing and horizontal smoothing,
respectively. The relations in Equation (7.2) thus have the following interpretation (alternatively note that the order of left or right multiplication does not
matter).
Observation 7.8. The order of vertical and horizontal smoothing does not
matter, and any tensor product of filters S T can be written as a horizontal
filtering operation I T followed by a vertical filtering operation S I.
218
2L+1 times
of T T .
7.2
In this section we will prove a specialization of our previous result to the case
where S and T are change of coordinate matrices. We start by proving the
following:
1
N 1
M
Theorem 7.10. If B1 = {v i }M
i=0 is a basis for R , and B2 = {w j }j=0 is
(M 1,N 1)
(M 1,N 1)
N 1
Proof. Suppose that (i,j)=(0,0) i,j (v i wj ) = 0. Setting hi = j=0 i,j wj
we get
N
1
N
1
i,j (v i wj ) = v i (
i,j wj ) = v i hi .
j=0
j=0
where we have used the bi-linearity of the tensor product mapping (x, y) xy
(Exercise 7.1.5). This means that
0=
(M 1,N 1)
(i,j)=(0,0)
i,j (v i wj ) =
M
1
i=0
v i hi =
M
1
v i hTi .
i=0
M 1
Column k in this matrix equation says 0 = i=0 hi,k v i , where hi,k are the
components in hi . By linear independence of the v i we must have that h0,k =
h1,k = = hM 1,k = 0. Since this applies for all k, we must have that all
N 1
hi = 0. This means that j=0 i,j wj = 0 for all i, from which it follows by
linear independence of the wj that i,j = 0 for all j, and for all i. This means
that B1 B2 is a basis.
In particular, as we have already seen, the standard basis for LM,N (R) can be
written EM,N = EM EN . This is the basis for a useful convention: For a tensor
product the bases are most naturally indexed in two dimensions, rather than
the usual sequential indexing. This dierence translates also to the meaning
of coordinate vectors, which now are more naturally thought of as coordinate
matrices:
220
1
N 1
Definition 7.11 (Coordinate matrix). Let {v i }M
i=0 , {w j }j=0 be bases for
M
N
R and R . By the coordinate matrix of k,l k,l (v k wl ) we will mean the
M N -matrix X with entries Xkl = k,l .
We will have use for the following theorem, which shows how change of
coordinates in RM and RN translate to a change of coordinates in the tensor
product:
Theorem 7.12 (Change of coordinates in tensor products). Assume that
B1 , C1 are bases for RM , and B2 , C2 are bases for RN , and that S is the change
of coordinates matrix from C1 to B1 , and that T is the change of coordinates
matrix from C2 to B2 . Both B1 B2 and C1 C2 are bases for LM,N (R), and if
X is the coordinate matrix in C1 C2 , and Y the coordinate matrix in B1 B2 ,
then
Y = SXT T .
(7.3)
Proof. Let cki be the ith basis vector in Ck , bki the ith basis vector in Bk ,
k = 1, 2. Since any change of coordinates is linear, it is enough to show that it
coincides with X SXT T on the basis C1 C2 . The basis vector c1i c2j has
coordinate vector X = ei ej in C1 C2 . With the mapping X SXT T this
is sent to
SXT T = S(ei ej )T T = coli (S)rowj (T T ).
On the other hand, since column i in S is the coordinates of c1i in the basis B1 ,
and column j in T is the coordinates of c2j in the basis B2 , we can write
c1i c2j =
Sk,i b1k
Tl,j b2l =
Sk,i Tl,j (b1k b2l )
k
k,l
k,l
k,l
we see that the coordinate vector of c1i c2j in the basis B1 B2 is coli (S)rowj (T T ).
In other words, change of coordinates coincides with X SXT T , and the proof
is done.
In both cases of tensor products of matrices and change of coordinates in
tensor products, we see that we need to compute the mapping X SXT T . This
means that we can restate Theorem 7.6 for change of coordinates as follows:
Theorem 7.13 (Implementation of change of coordinates in tensor products).
The change of coordinates from C1 C2 to B1 B2 can be implemented as
follows:
221
Figure 7.2: The eect on an image when it is transformed with the DFT, and
the DFT-coecients below a certain threshold were neglected.
Theorem 7.13. The JPEG standard actually applies a two-dimensional DCT to
the blocks of size 8 8, it does not apply the two-dimensional DFT.
If we follow the same strategy for the DCT as for the DFT, so that we
neglect DCT-coecients which are below a given threshold, we get the images
shown in Figure 7.3. We see similar eects as with the DFT, but it seems that
the latter images are a bit clearer, verifying that the DCT is a better choice
than the DFT. It is also interesting to compare with what happens when we
drop splitting the image into blocks. Of course, when we neglect many of the
DCT-coecients, we should see some artifacts, but there is no reason to believe
that these should be at the old block boundaries. The new artifacts can be seen
in Figure 7.4, where the same thresholds as before have been used. Clearly, the
new artifacts take a completely dierent shape.
In the exercises you will be asked to implement functions which generate the
images shown in these examples.
newx=FFT2Impl(x)
x=IFF2Impl(newx)
newx=DCT2Impl(x)
x=IDCT2Impl(newx)
which implement the two-dimensional DCT, FFT, and their inverses as described in this section. Base your code on the algorithm at the end of Section 7.1.
223
Figure 7.3: The eect on an image when it is transformed with the DCT, and
the DCT-coecients below a certain threshold were neglected.
Figure 7.4: The eect on an image when it is transformed with the DCT, and
the DCT-coecients below a certain threshold were neglected. The image has
not been split into blocks here.
224
7.3
Summary
We defined the tensor product, and saw how this could be used to define operations on images in a similar way to how we defined operations on sound.
It turned out that the tensor product construction could be used to construct
some of the operations on images we looked at in the previous chapter, which
now could be factorized into first filtering the columns in the image, and then
filtering the rows in the image. We went through an algorithm for computing
the tensor product, and established how we could perform change of coordinates in tensor products. This enables us to define two-dimensional extensions
of the DCT and the DFT and their inverses, and we used these extensions to
experiment on images.
225