1.MATH547 Homework 3

Topic: Math@UNC environment
Post date: May 20, 2020
Due date: May 21, 2020

1.1.Background

This homework investigates consequences of the fundamental theorem of algebra and application of the singular value decomposition.

1.2.Theoretical questions

Consider a linear mapping 𝒇:UV, from vector space 𝒰=(U,,+,) with basis {𝒖1,,𝒖n}, to 𝒱=(V,,+,), with basis {𝒗1,,𝒗m}.

  1. Is {𝒇(𝒖1),,𝒇(𝒖n)} a basis for 𝒱?

    Solution. Not necessarily, for example if 𝒇(𝒖j)=𝟎.

  2. If nullity(𝒇)=0 must m=n?

    Solution. No. Let 𝑨m×n be matrix associated with 𝒇. If nullity(𝒇)=0 then N(𝑨)={𝟎}, and the only solution to 𝒇(𝒙)=𝟎 is 𝒙=𝟎. Consider 𝒇:2,𝒇(x)=[ x 2x ]T=𝑨x, with 𝑨=[ 1 2 ]T, and m=1, n=2, mn.

  3. If m=n and 𝒖i=𝒗i for i=1,,m, what is the matrix 𝑨 representing 𝒇?

    Solution. The identity matrix 𝑨=𝑰. Apply the linear mapping 𝒗=𝒇(𝒖)=𝑨𝒖 to the basis vectors {𝒖1,,𝒖m}, to obtain

    𝑽=[ 𝒗1 𝒗2 𝒗m ]=𝑨𝑼=[ 𝑨𝒖1 𝑨𝒖2 𝑨𝒖m ].

    Since 𝑼 is a basis, the column vectors are linearly independent and the matrix is invertible,

    𝑨=𝑽𝑼-1.

    The statement 𝒖i=𝒗i can be interpreted in one of two ways (these descriptions are said to be duals of one another, and distinguish between a vector as a geometrical entity and its coordinates in some given basis):

    1. 𝒖i is a vector that contains the coordinates of the ith basis vector of U in the 𝑰 basis, 𝒖i=𝑰𝒖i. Similarly, 𝒗i is a vector that contains the coordinates of the ith basis vector of V in the 𝑰 basis, 𝒗i=𝑰𝒗i. In this case 𝒖i=𝒗i implies 𝑼=𝑽, and 𝑨=𝑰. This interpretation was emphasized in this course, and is appropriate for U=V=m.

    2. 𝒖i is the ith basis vector of U, whose coordinates in that basis are 𝒃i given by 𝒖i=𝑼𝒃i. Similarly 𝒗i=𝑽𝒄i. The statement 𝒖i=𝒗i now implies 𝑼𝒃i=𝑽𝒄i, such that

      𝒄i=𝑽-1𝑼𝒃i

      and the matrix associated with the mapping would be 𝑨=𝑽-1𝑼. This interpretation is appropriate when mapping between different vector spaces of the same dimension, UV, dim(U)=dim(V).

  4. Determine the singular value decomposition and pseudo-inverse of a matrix 𝑨1×n (i.e., a row vector).

    Solution. The SVD is 𝑨=𝑼𝚺𝑽T, 𝑼m×m orthogonal, 𝑽n×n orthogonal, 𝚺+m×n diagonal. For 𝑨 a row vector, m=1, and

    𝚺=[ σ1 0 0 ]

    with σ1=||𝑨||2. By definition of the matrix 2-norm

    σ1=sup||𝒙||=1||𝑨𝒙||.

    Since 𝑨=𝒂T,

    𝑨𝒙=𝒂T𝒙=i=1naixi,i=1nxi2=1.

    The largest possible value of ||𝑨𝒙|| when ||𝒙||=1, is σ1=||𝑨||=max1in|ai|. The SVD is

    𝑨=[1][ σ1 0 0 ][ 𝒂Tσ1 𝑽n-1T ]

    with 𝑽n-1T𝒂=0, 𝑽n-1T𝑽n-1=𝑰n-1 (orthogonal).

1.3.Ordered bases for the fundamental spaces and painting motifs

The fudamental theorem of linear algebra partitions the domain and codomain of a linear mapping. The singular value decomposition provides orthogonal bases for each of the subspaces arising in the partition. The bases are ordered according to the amplification behavior of the linear mapping, expressed through the norm of successive restrictions of the mapping. This approach is closely aligned with typical problems in data science, and can be used in a variety of scenarios. In this homework linear algebra methods will first be used in a field far removed from the physical sciences: extracting the quirks of painter style from the overall composition of a painting, and applying one artist's style to another artist's composition. This is often-encountered data science problem: distinguishing between small and large scale features of data.

First steps in solving the homework questions will be carried out in class. Each of the following subsections is a homework question, with 1 grade point awarded for a correct solution. Here are some initial Octave instructions to define a directory to save images into and define a function to read an image.

octave] 
cd homework; mkdir hw03; cd hw03
octave] 
im=imread("/home/student/courses/MATH547ML/data/paintings/Andy_Warhol_127.jpg");
octave] 
imshow(im)
octave] 
function im=pread(name)
  im=imread(strcat("/home/student/courses/MATH547ML/data/paintings/",name));
  im=rgb2gray(im);
end
octave] 
im=pread("Andy_Warhol_127.jpg");
octave] 
imshow(im); size(im)

ans =

360 357

octave] 

1.3.1.Data input mappings

Define a linear mapping that rescales data within an image file to some specified size px×py . Determine whether the mapping is data-preserving, and if not, quantify the amount of data loss.

Solution. One such linear mapping is to simply take a portion of the image. If the initial image was qx×qy the data loss is qx×qy-px×py.

octave] 
function im1=psize(im0,px,py)
  [mx,my]=size(im0);
  cx=floor(mx/2); cy=floor(my/2);
  px2=floor(px/2); py2=floor(py/2);
  im1 = im0(cx-px2:cx+px2,cy-py2:cy+py2);
end
octave] 
imshow(psize(im,256,256))
octave] 
imshow(psize(pread("Vincent_van_Gogh_167.jpg"),256,256))
octave] 

1.3.2.Images as data: change of basis

Take the largest possible portion of a painting of size p×p with p=2q. Interpret the resulting image as a vector 𝒃m with m=p2=22q. The image is thus specified as a linear combination of the columns of the identity matrix 𝑰m×m,

𝒃=𝑰𝒃=b1𝒆1+b2𝒆2++bm𝒆m,

and describes illumination on a pixel-by-pixel basis. A column vector of 𝑰 can be interpreted as the binary base representation of a natural number from the set P={1,2,3,,2m}, namely 2j-1g𝒆j.

𝒆jT=[ 0 0 1 0 0 ]m
nj=0001002=2j-1

Note that is one choice among the possible combinations of m objects chosen from a set with 2m elements, C(2m,m). Even for small m, the number of choices is enormous

C(2m,m)=2m!m!(2m-m)!,m=16C(216,16)=216!16!(216-16)!5.5×1063.

Construct a new basis by random choice of numbers from P and the g mapping. Check if the basis is orthogonal.

Solution. Let M be the largest representable integer on a computer. If M<m=p2 then the binary number representation would not construct a basis since the vector 𝒖 associated with binary number B𝒖>M is not within span{𝒗1,,𝒗m} associated with binary numbers {B1,,Bm}, all less than M, BiM. Choose m<M. Orthgonality of vectors associated with binary numbers Bi,Bj corresponds to a zero result from the bitwise and operation.

octave] 
m=4*4;
octave] 
B=randi(flintmax()-1,m,1);
octave] 
C=zeros(m,m);
for i=1:m
  for j=1:m
    C(i,j)=bitand(B(i),B(j));
  end;
end
octave] 
max(max(C))

ans = 8.9756e+15

octave] 

Linear dependence is the same as equality in this binary representation.

octave] 
D=zeros(m,m);
for i=1:m
  for j=i+1:m
    D(i,j) = B(i)==B(j);
  end;
end
octave] 
max(max(D))

ans = 0

octave] 

From above, a non-orthogonal basis is obtained.

1.3.3.Images as data: positive checkerboard basis

Construct another basis that corresponds to successive halving of image regions by factors (2k,2l) along the horizontal and vertical dimensions. Are basis vectors orthogonal?

Solution. There are many possible approaches to this problem. In the following a full solution is presented highlighting that application of linear algebra concepts to practical data science problems usually involves non-trivial preprocessing of the data. Since the solution does involve considerable technique from outside of linear algebra full credit is awarded to all attempts of this problem.

First consider the simplest possible case. A 1×1 image cannot be halved, hence start with 2×2 images for which

1 0 1 0 , 0 1 0 1 , 1 1 0 0 , 0 0 1 1 , 1 0 0 1 , 0 1 1 0

give 6 possible patterns encoded by row vectors

𝑩T=[ 1 0 1 0 0 1 0 1 1 1 0 0 0 0 1 1 1 0 0 1 0 1 1 0 ].

Only four basis vectors are required and can be chosen as

𝑪=[ 1 0 1 0 1 1 0 0 0 1 1 0 0 0 1 1 ].

octave] 
B=[1 0 1 0; 0 1 0 1; 1 1 0 0; 0 0 1 1; 1 0 0 1; 0 1 1 0];
C=[1 0 1 0; 1 1 0 0; 0 1 1 0; 0 0 1 1];
octave] 
[rank(B) rank(C)]

ans =

4 4

octave] 

The pattern suggested by above is alternating groups of n 1's and n 0's, that are then shifted right by 1,,n places. Define a function to generate alternating 1,0 bit groupings for images of size m=p×p, p=2q. There are 22q-1 such numbers.

octave] 
q=2; p=2^q; m=p^2; disp([q p m])

2 4 16

octave] 
g=zeros(2^(2*q-1),1); o=2^m-1;
for i=0:q
  k=2^i; l=2^k; disp([k l]);
  g(i+1) = bitshift(l-1,2^i);
  dec2bin(g(i+1))
end

1 2

ans = 10

2 4

ans = 1100

4 16

ans = 11110000

octave] 

1.3.4.Images as data: mixed-sign checkerboard basis

Map the binary digits {0,1} from the positive checkerboard basis to integers {-1,1}. Check if the newly obtained basis is orthogonal. Display approximations of the image that result from the first 2l basis vectors, l=2q,2q-2,2q-4,2q-6,2q-8.

Solution. As above, s.ince the solution does involve considerable technique from outside of linear algebra full credit is awarded to all attempts of this problem.

1.3.5.Images as mappings

Alternatively, an image can be interpreted as a matrix 𝑨, hence a mapping. From

𝑨=𝑨𝑰=𝑨[ 𝒆1 𝒆2 𝒆m ]=[ 𝑨𝒆1 𝑨𝒆2 𝑨𝒆m ],

the image can be interpreted as the transformation of the image encoded by 𝑰. Denote by 𝑨k the kth-rank approximation of 𝑨 from the singular value decomposition 𝑨=𝑼𝚺𝑽T

𝑨k=l=1kσl𝒖l𝒗lT.

Display the images that correspond to k=m,m/2,m/4,m/8.

Solution. Construct and represent each 𝑨k.

octave] 
function im=svdim(S,U,V,p)
  im=S(1,1)*U(:,1)*V(:,1)';
  for k=2:p
    im=im+S(k,k)*U(:,k)*V(:,k)';
  end;
end
octave] 
figure(3); clf; imshow(im)
octave] 
[U,S,V]=svd(im,1);
octave] 
figure(2); plot(log10(diag(S)),'o');
octave] 
figure(1); clf; imagesc(svdim(S,U,V,10)); colormap(gray); print -deps HW03Fig01.eps
octave] 
figure(1); clf; imagesc(svdim(S,U,V,20)); colormap(gray); print -deps HW03Fig02.eps
octave] 
figure(1); clf; imagesc(svdim(S,U,V,40)); colormap(gray); print -deps HW03Fig03.eps
octave] 

Figure 1. Successive SVD approximations of an image with k=10,20,40 rank-one updates.

1.3.6.Extracting and applying motifs

Consider images from two different artists, 𝑨,𝑩 and their singular value decompositions

𝑨=𝑺𝚲𝑻T,𝑩=𝑼𝚺𝑽T.

Let q=rank(𝑨), r=rank(𝑩). Construct and display images that take the large scale features from 𝑨 combined with small scale features from 𝑩,

𝑪=l=1min(k,q)λl𝒔l𝒕lT+l=max(1,r-k)rσl𝒖l𝒗lT,

for k=m/2,m/4,m/8,m/16where r=rank(𝑩).

Solution. Choose first 40 modes from the composition image, and mmodes 50 to 60 from the style image.

octave] 
ims=pread("Vincent_van_Gogh_50.jpg");
octave] 
figure(4); imshow(ims)
octave] 
imC=psize(im,256,256); imS=psize(ims,256,256);
octave] 
figure(1); clf; imshow(imC); print -deps Hw03Fig04.eps;
octave] 
figure(2); clf; imshow(imS); print -deps HW03Fig05.eps;
octave] 
[Uc,Sc,Vc]=svd(imC,1); [Us,Ss,Vs]=svd(imS,1);
octave] 
function im=svdimpq(S,U,V,p,q)
  im=S(p,p)*U(:,p)*V(:,p)';
  for k=p+1:q
    im=im+S(k,k)*U(:,k)*V(:,k)';
  end;
end
octave] 
imCS = svdim(Sc,Uc,Vc,40) + svdimpq(Ss,Us,Vs,50,60);
octave] 
figure(5); clf; imagesc(imCS); colormap(gray); print -deps Hw03Fig06.eps;
octave] 

Figure 2. Applying the style of Van Gogh to a work by Warhol.