Question: Python - please show code 3) In the implementation of Gaussian elimination in the form of LU composition), we zero out the coefficient A[i,j] below

Python - please show code

Python - please show code 3) In the implementation of Gaussian eliminationin the form of LU composition), we zero out the coefficient A[i,j]

3) In the implementation of Gaussian elimination in the form of LU composition), we zero out the coefficient A[i,j] below the pivot A[i,i] by performing a row-operation with the scalar multiplier m = A[i,j]/A[i,i] so, if that coefficient has larger magnitude than the pivot, the multiplier has magnitude greater than 1. As illustrated in Problem 2, a large multiplier can cause numerical errors to grow leading to loss of precision in the solution. Having implemented swap_rows() in Problem 1, we can now avoid this problem with partial pivoting. The basic idea is to swap the pivot row with whichever row beneath it has the largest coefficient in the current pivot column. After swapping this row into the pivot position, all multipliers in the row ops to zero out the entries below the diagonal will have magnitude less than 1. To implement a partial pivoting approach in the language of matrices, we will again think in terms of matrix factorization. We saw that basic Gaussian elimination can be thought of in terms of an equivalent LU factorization where the Upper triangular matrix U holds the triangular matrix obtained by elimination, and the lower triangular matrix L records the multipliers in the elementary row operations. For partial pivoting, we also need to record row swaps leading to the factorization A = PLU where P is the permutation matrix (a square matrix that is all zeros except for a single 1 in each row and column - alternatively think of P as an identity matrix that has been subjected to row swaps). Your task in this problem is to implement PLU factorization of a square input matrix A. The approach is very similar to what was done in LU factorization, but now with the additional factor P to record swaps. Here are the steps: 0. Get the size of the A matrix and check that it is square. 1. Initialize P and L as identity matrices and U as a copy of A 2. Loop over the diagonal elements A[i,i] for i in range(m): a. Identify row imax having the largest magnitude entry in sub-column i (at or below the main diagonal) b. Swap rows imax and i in U and in P d. Perform row ops on U to zero out the column below the pivot and record multipliers in L. 3. Return P, L, U Insert your code for PLU factorization in the cell below. (The code for LU_factor is included for reference.) In [ ]: def LU_factor(A): m, n = A. shape if m != n: print("WARNING: Non-square input matrix") return mult = 0 U = np. copy(A) #make a copy of the array #Note that U=A just makes another name for A, not a new copy of the array L = np.eye(n) #numpy's name for the identity matrix is "eye" for i in range(n): # for each row i for j in range(i+1, n): # for each row below row i mult = U[j,i]/U[i,i] L[j,i] = mult for k in range(i,n): # for each entry beyond the i'th diagonal entry U[j,k] = U[j, k] - mult*U[i,k] # for entries to the right, subtract multiple of term in row i return L,U def PLU_factor(A): perform elementary row operation to swap rows i0 and il Args: A: 2D numpy array representing a matrix i0, il: integer row indices Returns: P, L,U: nxn numpy arrays permutation, lower triangular, upper triangular = In [ ]: A = np.array([[2,3,4],[1,1,1],[7,5,1]]) P,L,U PLU_factor(A) assert_(np.allclose(A, np.dot(P, np.dot (L,U)))) #verify factorization assert (np.allclose(np.dot(P, P), np.eye(3))) #verify P.P - I assert (np.allclose(P, np.array([[0,1,01, [1,0,0], [0,0,1]]))) assert (np.allclose(L, np.array([[1,0,0], [2,1,01, 17, -2,1]]))) assert_(np.allclose(U, np.array( [[1,1,1],[0,1,2], [0,0,-2]]))) 3) In the implementation of Gaussian elimination in the form of LU composition), we zero out the coefficient A[i,j] below the pivot A[i,i] by performing a row-operation with the scalar multiplier m = A[i,j]/A[i,i] so, if that coefficient has larger magnitude than the pivot, the multiplier has magnitude greater than 1. As illustrated in Problem 2, a large multiplier can cause numerical errors to grow leading to loss of precision in the solution. Having implemented swap_rows() in Problem 1, we can now avoid this problem with partial pivoting. The basic idea is to swap the pivot row with whichever row beneath it has the largest coefficient in the current pivot column. After swapping this row into the pivot position, all multipliers in the row ops to zero out the entries below the diagonal will have magnitude less than 1. To implement a partial pivoting approach in the language of matrices, we will again think in terms of matrix factorization. We saw that basic Gaussian elimination can be thought of in terms of an equivalent LU factorization where the Upper triangular matrix U holds the triangular matrix obtained by elimination, and the lower triangular matrix L records the multipliers in the elementary row operations. For partial pivoting, we also need to record row swaps leading to the factorization A = PLU where P is the permutation matrix (a square matrix that is all zeros except for a single 1 in each row and column - alternatively think of P as an identity matrix that has been subjected to row swaps). Your task in this problem is to implement PLU factorization of a square input matrix A. The approach is very similar to what was done in LU factorization, but now with the additional factor P to record swaps. Here are the steps: 0. Get the size of the A matrix and check that it is square. 1. Initialize P and L as identity matrices and U as a copy of A 2. Loop over the diagonal elements A[i,i] for i in range(m): a. Identify row imax having the largest magnitude entry in sub-column i (at or below the main diagonal) b. Swap rows imax and i in U and in P d. Perform row ops on U to zero out the column below the pivot and record multipliers in L. 3. Return P, L, U Insert your code for PLU factorization in the cell below. (The code for LU_factor is included for reference.) In [ ]: def LU_factor(A): m, n = A. shape if m != n: print("WARNING: Non-square input matrix") return mult = 0 U = np. copy(A) #make a copy of the array #Note that U=A just makes another name for A, not a new copy of the array L = np.eye(n) #numpy's name for the identity matrix is "eye" for i in range(n): # for each row i for j in range(i+1, n): # for each row below row i mult = U[j,i]/U[i,i] L[j,i] = mult for k in range(i,n): # for each entry beyond the i'th diagonal entry U[j,k] = U[j, k] - mult*U[i,k] # for entries to the right, subtract multiple of term in row i return L,U def PLU_factor(A): perform elementary row operation to swap rows i0 and il Args: A: 2D numpy array representing a matrix i0, il: integer row indices Returns: P, L,U: nxn numpy arrays permutation, lower triangular, upper triangular = In [ ]: A = np.array([[2,3,4],[1,1,1],[7,5,1]]) P,L,U PLU_factor(A) assert_(np.allclose(A, np.dot(P, np.dot (L,U)))) #verify factorization assert (np.allclose(np.dot(P, P), np.eye(3))) #verify P.P - I assert (np.allclose(P, np.array([[0,1,01, [1,0,0], [0,0,1]]))) assert (np.allclose(L, np.array([[1,0,0], [2,1,01, 17, -2,1]]))) assert_(np.allclose(U, np.array( [[1,1,1],[0,1,2], [0,0,-2]])))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!