Homework 2

This homework covers the material in Lessons 3 & 4. It is due Thurday, June 15. Please submit your answers as a PDF.

1. Modify the LU method (without pivoting) to work in-place. That is, it should not allocate any new memory for L or U, but instead overwrite A.

In [ ]:
def LU(A):
    U = np.copy(A)
    m, n = A.shape
    L = np.eye(n)
    for k in range(n-1):
        for j in range(k+1,n):
            L[j,k] = U[j,k]/U[k,k]
            U[j,k:n] -= L[j,k] * U[k,k:n]
    return L, U

2. Modify our LU method from class to add pivoting, as described in the lesson. Hint: the swap method below will be useful

In [ ]:
def swap(a,b):
    temp = np.copy(a)
    a[:] = b
    b[:] = temp

3. For each of the following sets of dimensions, either

  • give the dimensions of the output of an operation on A and B, or
  • answer incompatible if the dimensions are incompatible according to the numpy rules of broadcasting.
a.  A      (2d array):  3 x 3
    B      (1d array):      1

b.  A      (2d array):      2 x 1
    B      (3d array):  6 x 4 x 2

c.  A      (2d array):  5 x 4
    B      (1d array):      4

d.  A      (3d array):  32 x 64 x 8
    B      (3d array):   32 x 1 x 8

e.  A      (3d array):       64 x 1
    B      (3d array):  32 x 1 x 16

f.  A      (3d array):  32 x 64 x 2
    B      (3d array):   32 x 1 x 8

4. Write how this matrix would be stored in compressed row format:

\begin{pmatrix} 1 & & & & -2 & -3 \\ & 3 & & & & -9 \\ & & & -7 & 4 & \\ -1 & 2 & & 7 & & \\ -3 & & & 26 & & \end{pmatrix}