Question: Q4. (GMRES and CG) Write a python code to do the following operations. 1. Define a function create_sparse_rand_mat(N, zero_frac) for creating (1) A: a sparse

Q4. (GMRES and CG) Write a python code to do the following operations. 1. Define a function create_sparse_rand_mat(N, zero_frac) for creating (1) A: a sparse random symmetric matrix A of size N * N with the fraction of non-zero elements zero_frac stored in format of compressed sparse row matrix (CSR), (ii) A_arr : the same matrix A stored in format of numpy array, and (iii) b: a random matrix b of size N x 1 by A = scipy.sparse.random(N, N, zero_frac) # sparse random matrix in format of CSR A = A.toarray() # convert CSR format to numpy array format A_arr = A + np. transpose(A) + 4*np.eye(N) # addition of "4*np.eye(N)" guaranttes that A is well-conditioned A = scipy.sparse.csr_matrix(A_arr) # convert numpy array format to CSR format b = np.random.rand (N, 1) 2. Create the matrices A in both CSR format and array format and b by calling the function create_sparse_rand_mat(N, zero_frac) defined in Q4.1 with N = 100 and zero_frac 0.005. Plot the matrix A thus created by .imshow() in matplotlib. 3. Loop over the matrix size N = 100, 150, 200, ..., 750, 800. In each cycle with N, create random A and b by create_sparse_rand_mat(N, zero_frac) defined in Q4.1 with zero_frac = 0.005. Count the times spent on runing scipy.sparse. linalg.gmres(A, b, xo, tol, maxiter), scipy.sparse.linalg.cg(A, b, xo, tol, maxiter), and scipy.linalg.solve(a, b). The parameters are xo = 0, tol = 10-10 and maxiter = 1000000. You can learn how to count the time by time.time_ns() (in unit of nanosecond) from Page 13 of the note of lecture 2. For each cycle, append the values of N and the three times mentioned above to four lists. 4. Plot the computation times spent on three methods vs. N. Show the legend to label the meaning of each curve. Q4. (GMRES and CG) Write a python code to do the following operations. 1. Define a function create_sparse_rand_mat(N, zero_frac) for creating (1) A: a sparse random symmetric matrix A of size N * N with the fraction of non-zero elements zero_frac stored in format of compressed sparse row matrix (CSR), (ii) A_arr : the same matrix A stored in format of numpy array, and (iii) b: a random matrix b of size N x 1 by A = scipy.sparse.random(N, N, zero_frac) # sparse random matrix in format of CSR A = A.toarray() # convert CSR format to numpy array format A_arr = A + np. transpose(A) + 4*np.eye(N) # addition of "4*np.eye(N)" guaranttes that A is well-conditioned A = scipy.sparse.csr_matrix(A_arr) # convert numpy array format to CSR format b = np.random.rand (N, 1) 2. Create the matrices A in both CSR format and array format and b by calling the function create_sparse_rand_mat(N, zero_frac) defined in Q4.1 with N = 100 and zero_frac 0.005. Plot the matrix A thus created by .imshow() in matplotlib. 3. Loop over the matrix size N = 100, 150, 200, ..., 750, 800. In each cycle with N, create random A and b by create_sparse_rand_mat(N, zero_frac) defined in Q4.1 with zero_frac = 0.005. Count the times spent on runing scipy.sparse. linalg.gmres(A, b, xo, tol, maxiter), scipy.sparse.linalg.cg(A, b, xo, tol, maxiter), and scipy.linalg.solve(a, b). The parameters are xo = 0, tol = 10-10 and maxiter = 1000000. You can learn how to count the time by time.time_ns() (in unit of nanosecond) from Page 13 of the note of lecture 2. For each cycle, append the values of N and the three times mentioned above to four lists. 4. Plot the computation times spent on three methods vs. N. Show the legend to label the meaning of each curve
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
