Uploaded by PingAn Sun

hw4 (3)

advertisement
MATH-UA 252 - Spring ‘23 - Numerical Analysis - HW #4
Problems 1–5.
Exercises 4.1, 4.3, 5.3, 5.4, and 31.4 in Trefethen and Bau.
Problem 6. (Image compression using the SVD.) In this problem we will use the SVD as a tool for image
compression. Get ahold of a 1024 × 1024 grayscale image and load it into Python using NumPy or SciPy.
We will think of your image as a matrix A ∈ R1024×1024 with components in the range [0, 1] (so, make sure
to normalize your image). Do the following:
1. Let A = U ΣV ⊤ be the singular value decomposition of A. Let k > 0 and let Uk be the first k columns
of U , let Σk be the leading k × k block of Σ, and let Vk be the first columns of V . We can approximate
A with the truncated singular value decomposition Ak = Uk Σk Vk⊤ .
(a) Use plt.imshow to plot A, A2 , A4 , A6 , and A8 . Find the value of k where A and Ak appear
identical to you and plot the pointwise error (with a colorbar!). What is the average pointwise
error in this case?
(b) Check the pointwise error between A and Ak for k = 1, 2, . . . , 1024. Plot this pointwise error
using plt.semilogy with k on the horizontal axis and the absolute maximum pointwise error on
the vertical axis.
(c) Plot the size of the compressed image size versus k using plt.semilogy (use the .nbytes property
of a NumPy array).
2. Follow the same approach as before, but use a truncated singular value decomposition to approximate
subblocks of A. For example, if you subdivided A into a grid of 16 × 16 blocks, each block would be
64 × 64. So, the maximum k value will be smaller than 1024 in this case, but the same for each block.
Experiment with different block sizes to see if you can find the smallest compressed size which is close
to visually identical to the original image (you can use your average pointwise error from part 1(a) as
a target to aim for).
Hint: in this case, you can choose k differently for each block! Try coming up with an algorithm which
will automatically find k such that a target maximum or average pointwise error is achieved. Then
you will only need to search over the number of blocks to partition your image into in order to find
the best size.
1
Download