0
0
SciPydata~20 mins

Sparse iterative solvers (gmres, cg) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sparse Solver Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of CG solver on a simple sparse system
What is the output of the following code snippet that uses the Conjugate Gradient (CG) solver from scipy.sparse.linalg to solve a sparse linear system?
SciPy
import numpy as np
from scipy.sparse import diags
from scipy.sparse.linalg import cg

# Create a sparse diagonal matrix
n = 5
k = [-1*np.ones(n-1), 2*np.ones(n), -1*np.ones(n-1)]
offsets = [-1, 0, 1]
A = diags(k, offsets)

b = np.array([1, 2, 3, 4, 5])

x, info = cg(A, b)
print(x)
A[5.83 10.67 13.5 13.33 9.17]
B[2. 3. 4. 5. 6.]
C[0.75 1.5 2. 2.5 3. ]
D[1. 2. 3. 4. 5.]
Attempts:
2 left
💡 Hint
Think about how the CG solver solves Ax = b for a tridiagonal matrix with 2 on the diagonal and -1 on the off-diagonals.
Predict Output
intermediate
2:00remaining
GMRES solver output for a sparse system
What is the output of the following code that uses GMRES to solve a sparse linear system?
SciPy
import numpy as np
from scipy.sparse import diags
from scipy.sparse.linalg import gmres

n = 4
k = [np.ones(n), 2*np.ones(n), np.ones(n)]
offsets = [-1, 0, 1]
A = diags(k, offsets)

b = np.array([1, 2, 3, 4])

x, info = gmres(A, b)
print(x)
A[0.25 0.5 0.75 1. ]
B[0. 1. 0. 2. ]
C[1. 2. 3. 4.]
D[0.5 1. 1.5 2. ]
Attempts:
2 left
💡 Hint
GMRES solves Ax = b for a matrix with 2 on the diagonal and 1 on the off-diagonals.
data_output
advanced
2:00remaining
Number of iterations for CG solver convergence
Given the following code that solves a sparse system using CG with a tolerance of 1e-8, what is the number of iterations taken for convergence?
SciPy
import numpy as np
from scipy.sparse import diags
from scipy.sparse.linalg import cg

n = 10
k = [-1*np.ones(n-1), 2*np.ones(n), -1*np.ones(n-1)]
offsets = [-1, 0, 1]
A = diags(k, offsets)

b = np.arange(1, n+1)

x, info = cg(A, b, tol=1e-8, maxiter=1000)
print(info)
A0
B10
C1000
D5
Attempts:
2 left
💡 Hint
The info output from cg returns 0 if convergence is reached successfully.
🔧 Debug
advanced
2:00remaining
Identify the error in GMRES solver usage
What error will the following code raise when trying to solve a sparse system with GMRES?
SciPy
import numpy as np
from scipy.sparse import diags
from scipy.sparse.linalg import gmres

n = 3
k = [np.ones(n), 2*np.ones(n), np.ones(n)]
offsets = [-1, 0, 1]
A = diags(k, offsets)

b = np.array([1, 2])

x, info = gmres(A, b)
print(x)
AIndexError: index out of range
BValueError: dimension mismatch between A and b
CTypeError: unsupported operand type(s) for +: 'int' and 'str'
DNo error, prints solution vector
Attempts:
2 left
💡 Hint
Check the size of matrix A and vector b.
🚀 Application
expert
2:00remaining
Choosing solver based on matrix properties
You have a large sparse symmetric positive definite matrix A and vector b. Which solver is the best choice to efficiently solve Ax = b?
AUse GMRES solver because it works for any matrix
BUse Jacobi iterative method for faster convergence
CUse direct solver like LU decomposition for sparse matrices
DUse CG solver because it is optimized for symmetric positive definite matrices
Attempts:
2 left
💡 Hint
Consider matrix properties and solver strengths.