0
0
SciPydata~30 mins

Sparse iterative solvers (gmres, cg) in SciPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Solving Sparse Linear Systems with GMRES and CG
📖 Scenario: You work as a data scientist helping engineers solve large systems of equations that come from real-world problems like network flows or physical simulations. These systems are often very large but have mostly zero values, called sparse systems.To solve these efficiently, you will use special methods called iterative solvers: GMRES and CG. These methods find approximate solutions quickly without using too much memory.
🎯 Goal: You will create a sparse matrix and a vector, then use the GMRES and CG solvers from scipy.sparse.linalg to find solutions. Finally, you will print the results to see how well the solvers worked.
📋 What You'll Learn
Create a sparse matrix using scipy.sparse
Create a vector with exact values
Set a tolerance level for the solver
Use gmres and cg solvers from scipy.sparse.linalg
Print the solution vectors
💡 Why This Matters
🌍 Real World
Sparse iterative solvers are used in engineering, physics, and computer science to solve large systems efficiently, such as in simulations, network analysis, and optimization.
💼 Career
Knowing how to use sparse solvers helps data scientists and engineers handle big data problems and scientific computations where memory and speed are critical.
Progress0 / 4 steps
1
Create a sparse matrix and vector
Create a sparse matrix called A using scipy.sparse.csr_matrix with these exact values: [[4, 1, 0], [1, 3, 1], [0, 1, 2]]. Also create a vector called b as a NumPy array with values [1, 2, 3].
SciPy
Need a hint?

Use csr_matrix to create the sparse matrix A with the exact 3x3 values. Use np.array to create vector b.

2
Set the tolerance for the solvers
Create a variable called tol and set it to 1e-5 to control the solver accuracy.
SciPy
Need a hint?

Simply assign 1e-5 to a variable named tol.

3
Solve the system using GMRES and CG
Import gmres and cg from scipy.sparse.linalg. Use gmres to solve Ax = b with tolerance tol and store the solution in x_gmres. Use cg to solve Ax = b with tolerance tol and store the solution in x_cg. Ignore the second returned value from both solvers.
SciPy
Need a hint?

Use gmres(A, b, tol=tol) and cg(A, b, tol=tol). Assign the first returned value to x_gmres and x_cg respectively, and ignore the second value with _.

4
Print the solutions
Print the solution vectors x_gmres and x_cg each on a separate line.
SciPy
Need a hint?

Use print(x_gmres) and print(x_cg) to show the solution vectors.