Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

(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 (i) A: a sparse random symmetric matrix AA of size NNNN with the fraction of non-zero elements zero_frac stored in format of compressed sparse row matrix (CSR), (ii) A_arr: the same matrix AA stored in format of numpy array, and (iii) b: a random matrix bb of size N1N1 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 AA in both CSR format and array format and bb by calling the function create_sparse_rand_mat(N, zero_frac) defined in Q4.1 with N=100N=100 and zero_frac =0.005=0.005. Plot the matrix AA thus created by .imshow() in matplotlib.

  3. Loop over the matrix size N=100,150,200,,750,800N=100,150,200,,750,800. In each cycle with NN, create random AA and bb by create_sparse_rand_mat(N, zero_frac) defined in Q4.1 with zero_frac =0.005=0.005. Count the times spent on runing scipy.sparse.linalg.gmres(A, b, x0, tol, maxiter), scipy.sparse.linalg.cg(A, b, x0, tol, maxiter), and scipy.linalg.solve(A, b). The parameters are x0 =0=0, tol =1010=1010 and maxiter =1000000=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 NN and the three times mentioned above to four lists.

  4. Plot the computation times spent on three methods vs. NN. Show the legend to label the meaning of each curve.

image text in transcribed

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 Nx 1 by . scipy.sparse.random(N, N, zero_frac) # sparse random matrix i n 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 scipy.sparse.csr_matrix(A_arr) # convert numpy array format t O 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-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. (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 (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 Nx 1 by . scipy.sparse.random(N, N, zero_frac) # sparse random matrix i n 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 scipy.sparse.csr_matrix(A_arr) # convert numpy array format t O 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-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. (25 marks)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What are the similarities and differences among viral genomes?

Answered: 1 week ago