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.