Answered step by step
Verified Expert Solution
Question
1 Approved Answer
( Python Question ) Please show steps by steps AND also the working proof which is python code. If giving explanation is better. Thanks. Q4.
(Python Question) Please show steps by steps AND also the working proof which is python code.
If giving explanation is better. Thanks.
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 (0) A: a sparse random symmetric matrix A of size Nx 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, xe, tol, maxiter), scipy.sparse. linalg.cg(A, b, xe, tol, maxiter), and scipy.linalg.solve(A, b). The parameters are xe = 0, tol = 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. 10 (25 marks) 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 (0) A: a sparse random symmetric matrix A of size Nx 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, xe, tol, maxiter), scipy.sparse. linalg.cg(A, b, xe, tol, maxiter), and scipy.linalg.solve(A, b). The parameters are xe = 0, tol = 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. 10 (25 marks)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started