0
0
SciPydata~20 mins

Preconditioners in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Preconditioner Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of applying Jacobi preconditioner
Given the sparse matrix A and vector b, what is the output of applying the Jacobi preconditioner to b using scipy?
SciPy
import numpy as np
from scipy.sparse import diags
from scipy.sparse.linalg import LinearOperator

A = diags([4, -1, -1], [0, -1, 1], shape=(3,3))
b = np.array([1, 2, 3])

D_inv = diags(1 / A.diagonal())
M = LinearOperator(A.shape, matvec=lambda x: D_inv.dot(x))
result = M.matvec(b)
print(result)
A[0.25 0.5 0.75]
B[4 2 3]
C[0.5 1. 1.5]
D[1 2 3]
Attempts:
2 left
💡 Hint
Remember the Jacobi preconditioner uses the inverse of the diagonal elements of A.
🧠 Conceptual
intermediate
1:30remaining
Purpose of Preconditioners in Iterative Solvers
What is the main purpose of using a preconditioner in iterative methods for solving linear systems?
ATo convert the matrix into a diagonal matrix
BTo increase the size of the matrix for better accuracy
CTo change the solution vector to a random guess
DTo reduce the condition number of the matrix and speed up convergence
Attempts:
2 left
💡 Hint
Think about how preconditioners affect the matrix properties.
🔧 Debug
advanced
2:00remaining
Identify the error in incomplete LU preconditioner code
What error will this code raise when trying to create an incomplete LU preconditioner with scipy.sparse.linalg.spilu on a singular matrix?
SciPy
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.linalg import spilu

A = csr_matrix([[0, 1], [0, 0]])
ilu = spilu(A)
AValueError: matrix is not square
BTypeError: unsupported operand type(s)
CRuntimeError: factor is exactly singular
DNo error, returns a valid preconditioner
Attempts:
2 left
💡 Hint
Consider what happens if the matrix cannot be factorized.
data_output
advanced
1:30remaining
Output shape of preconditioned vector
If you apply an incomplete LU preconditioner created by spilu on a vector of length 5, what will be the shape of the output vector?
SciPy
import numpy as np
from scipy.sparse import diags
from scipy.sparse.linalg import spilu

A = diags([1, 2, 3, 4, 5], 0)
ilu = spilu(A)
v = np.array([1, 2, 3, 4, 5])
result = ilu.solve(v)
print(result.shape)
A(5,)
B(1, 5)
C(5, 1)
D(25,)
Attempts:
2 left
💡 Hint
The preconditioner solves a system of the same size as the input vector.
🚀 Application
expert
2:30remaining
Choosing a preconditioner for a large sparse symmetric positive definite matrix
You have a large sparse symmetric positive definite matrix A. Which preconditioner is generally the best choice to speed up conjugate gradient solver convergence?
AJacobi preconditioner
BIncomplete Cholesky factorization
CIncomplete LU factorization without symmetry
DNo preconditioner
Attempts:
2 left
💡 Hint
Consider the matrix properties and the solver used.