Description
Suppos you are given an upper-triangular π Γ π matrix U, i.e. all elements below the main diagonal are known to be 0. We wish to solve Ux = b for the vector x.
1. Find the analytical solution for the components π₯π of x in terms of the ππ,π and ππ using backsubstitution. (Hint: Where should you start?)
2. Write a function backsubstitution(U, b) that implements this.
3. What is the approximate operation count to perform backsubstitution?
4. Repeat [1]β[3] for forward substitution to solve Lx = b for a lowertriangular matrix L.
5. Write down what happens at a general step in the Gaussian elimination process for a matrix π΄, in terms of the old coefficients π΄π,π and the new coefficients π΄β²π,π.
6. Implement Gaussian elimination for an π Γ π square matrix π΄ to calculate the πΏπ factorization without pivoting. Your function LU_factorization should return L and U.
7. What is the operation count of this algorithm?
8. Write a function solve_linear_system(A, b) that uses the functions you have written to solve the linear system Ax = b.
9. Consider the matrix A built by the following Julia code. Please use exactly this code so that the results are consistent:
using Random
rng = MersenneTwister(1234) A = randn(rng, Float64, (10, 10)) b = randn(rng, Float64, 10) Solve Ax = b for this A and b.
Print out the full x vector using jl show(stdout, “text/plain”, x) (This prevents the ellipses, …, that can occur in compressed output.)
Compare this to the result from Juliaβs (backslash) operator and comment.
10. Find the size of the residual Ax β b using the maximum norm (i.e. find the largest entry of the vector) for the x you found using your solve_linear_system(A, b) function.
11. Plot the residual as a function of π for matrices of sizes between π = 10 and π = 1000 (or however high your computer can manage) on suitable axes.
How does the residual grow as the size grows?
Exercise 2: Exploiting structure in matrices 1 : Banded LU
In this exercise we will exploit structure in a matrix, i.e. the location of zero elements. If we know in advance that there are zeros in a certain part of the matrix (βstructural zerosβ), we can exploit that information to define more efficient operations on matrices of that type.
π΄ππ β 0 βΉ π β π β€ π β€ π + π
For example, a diagonal matrix is (0,0)-banded (only the main diagonal is present) and a tridiagonal matrix is (1,1)-banded.
As an example, think of a system of π particles in a line, each of which interacts only with its nearest neighbour on each side. This could lead to an interaction matrix that is known in advance to be tridiagonal, hence leading to the possibility of more efficiently solving the dynamics. Finite difference matrices are also banded. The matrices without structure that we discussed in the lectures on LU and QR are called dense matrices. We will define a new type Banded as follows:
struct Banded{T} <: AbstractMatrix{T} bands::Matrix{T} p::Int q::Int
end
Here, bands is a matrix that stores only the bands (diagonals) of the original matrix, numbered (βπ,β(πβ1),β¦,0,1,β¦π), where 0 corresponds to the main diagonal.
Note that there are more terms in bands than we need to store. We will store the lower bands justified to the bottom and the upper bands justified to the top. For example, consider the general (3, 2) banded matrix, i.e. with 3 diagonals below and 2 diagonals above the main diagonal.
π11 π12 π13
β‘π21 π22 π23 β’π31 π32 π33
π΅ = β’π41 π42 π43 β’ π52 π53
β£ π63
This would be stored as follows in bands: π24
π34
π44
π54
π64 π35
π45
π55
π65 β€
β₯
π46β₯β₯
π56
π66β¦
π41 π31 π21
β‘π52 π42 π32
π΅ = β’β’π063 ππ6453 ππ5443
β’ 0 0 π65
β£ 0 0 0 π11
π22
π33
π44
π55 π66 0
π12
π23
π34
π45
π56 0
0 β€
π13β₯
π24β₯β₯
π35
π46β¦
The columns of the new matrix correspond to the diagonals of the old matrix.
Although this seems not to be more efficient in terms of storage, it will be for a larger matrix.
1. Write a function mymul(B::Banded, v::Vector) that returns the matrixβ vector product B*v. The function should be optimized in the sense that terms we know are 0 are not calculated. Compare the result to the built-in dense matrix multiply using the code Array(B) * v. (Array(B) produces a dense matrix with the same bands as B).
2. Consider the LU factorization. Show, using the formula for matrix multiplication, that if πΏ is (π,0)-banded and π is (0,π)-banded then A = LU is (π,π)-banded.
3. Now show, by thinking about how the elimination algorithm works, that if A is (π,π)-banded then the factor L is (π,0)-banded and U is (0,π)banded.
4. We have seen that the structure of bandedness is maintained during an LU factorization. We saw in Exercise 1 that the operation count for a dense LU factorization is πͺ(π3). What happens to the operation count when we exploit the banded structure of a (π,π)-banded matrix of size π Γ π? Find the approximate operation count in terms of π, π and π.
5. Implement LU factorization for a banded matrix, LU_factorization(B::Banded), making sure to operate only on non-zero entries.
6. Given the new operation count, is the naive back/forward substitution or the LU factorization the bottleneck when solving a banded system? If LU is no longer the bottleneck, add methods for your substitution functions so that they are specialized for banded matrices.
(Hint for parts 5 & 6: if you fully understand what is happening here, modifying your LU / substitution codes from Exercise 1 should be simple for banded matrices, changing at most 1 or two lines of code.)
7. As an example of how necessary it is to exploit structure for efficiency, create a random tridiagonal matrix A of size π Γ π and a random vector b of length π, taking π = 10,000 (or whatever your computer can safely handle. How many times faster does your structured LU code run than if you were to use dense (standard) linear algebra (after you convert your banded matrix to a dense matrix)? (Remember to run your code once before timing it.)
8. Check numerically the dependence on π that you found in question [4] by solving random tridiagonal systems of sizes πΓπ for π between 10 and 10,000 and plotting the time taken as a function of π. (If the times are too fast, use larger values of π!)
[You can use @elapsed (or @belapsed from BenchmarkTools.jl) to capture the time taken by a Julia operation, so that you can automate this in a loop.]
Exercise 3: Exploiting structure in matrices 2 : Tridiagonal QR
In this exercise we will see how to exploit structure (zeros again) to make a more efficient QR algorithm. (However, we do not ask you to code the algorithm this time.)
Consider a general tridiagonal matrix (i.e. a (1,1)-banded matrix):
π1 π1
β‘π2 π2 π2 β€
π = β’β’ π3 πβ±3 πβ±3 β± β₯β₯
β’ ππβ1 ππβ1 ππβ1β₯
β£ ππ ππ β¦
Since there is only a single subdiagonal, it should be easier to make the matrix upper-triangular by operating on it.
1. Consider the following rotation matrix:
π
(π) = [cossin ππ βcossinππ]
How should you choose π so that
π π
π
(π)[π] = [0]
where π2 = π2 + π2 ? 2. Consider the matrix
π
(π) 0 π0 = [ 0 πΌπβ2]
where πΌπ is the π Γπ identity matrix, and where π is chosen as above for the terms π = π1 and π = π2. What does the resulting matrix π΄1Μ = π0π΄ look like? Show that the first column is now upper-triangular.
To be more concrete consider the matrix
1 4 0 0
β‘2 1 3 0β€ π = β’0 5 6 7β₯.
β£0 0 1 2β¦
Show the resulting matrix when you multiply by π0 β is it what you expected?
3. What matrix should you multiply π΄1Μ by to make the second column uppertriangular? (Hint: it will have a similar structure to π0) Check that the first column is still upper triangular. Call this matrix π1. Multiply the result of π0π by π1 and show the result.
4. Generalize to the rotation matrix that makes the πth column of a tridiagonal matrix upper-triangular. Call this matrix ππβ1. Show that ππβ1 is orthogonal, i.e. ππππβ€ = πΌ.
5. We can now write a tridiagonal matrix π in upper-triangular form by forming the product ππβ2ππβ3 β―π1π0π . From the above we see that the result of this will be an upper-triangular matrix π
.
Show that the product of orthogonal matrices is orthogonal, and hence that this procedure gives a QR decomposition of π .
6. What is the approximate operation count for a full QR factorization on a dense matrix π΄?
7. What is the approximate operation count for this reduced tridiagonal QR?
8. Extra credit Implement this algorithm in an efficient way.
Reviews
There are no reviews yet.