Challenge - 5 Problems
Sparse Linear Algebra Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of sparse linear system solver using SciPy
What is the output of the following code snippet that solves a sparse linear system using SciPy's conjugate gradient solver?
SciPy
import numpy as np from scipy.sparse import diags from scipy.sparse.linalg import cg # Create a sparse diagonal matrix A diagonals = [np.ones(3)*2] A = diags(diagonals, offsets=[0], shape=(3, 3)) b = np.array([1, 2, 3]) x, info = cg(A, b) print(np.round(x, 2))
Attempts:
2 left
💡 Hint
Think about how the conjugate gradient solver solves Ax = b for sparse A.
✗ Incorrect
The conjugate gradient solver finds x such that Ax = b. Given the sparse matrix A and vector b, the solution x is approximately [0.5, 1.0, 1.5].
❓ data_output
intermediate1:30remaining
Number of non-zero elements after sparse matrix multiplication
Given two sparse matrices A and B, what is the number of non-zero elements in the product matrix C = A * B?
SciPy
import numpy as np from scipy.sparse import csr_matrix A = csr_matrix([[0, 1, 0], [2, 0, 0], [0, 0, 3]]) B = csr_matrix([[1, 0, 0], [0, 0, 4], [5, 0, 0]]) C = A.dot(B) print(C.nnz)
Attempts:
2 left
💡 Hint
Count the non-zero elements in the resulting sparse matrix after multiplication.
✗ Incorrect
Multiplying A and B results in a sparse matrix with 3 non-zero elements.
🔧 Debug
advanced1:30remaining
Identify the error in sparse linear solver usage
What error does the following code raise when trying to solve a sparse linear system?
SciPy
import numpy as np from scipy.sparse import csc_matrix from scipy.sparse.linalg import spsolve A = csc_matrix([[0, 1], [1, 0]]) b = np.array([1, 2]) x = spsolve(A, b) print(x)
Attempts:
2 left
💡 Hint
Check if the matrix A is invertible before solving.
✗ Incorrect
No error occurs because matrix A is invertible (determinant = -1). The solution is x = [2. 1.].
❓ visualization
advanced2:00remaining
Visualizing sparsity pattern of a matrix
Which option produces a plot showing the sparsity pattern of a sparse matrix A?
SciPy
import numpy as np import matplotlib.pyplot as plt from scipy.sparse import random A = random(10, 10, density=0.2, format='csr', random_state=42) # Choose the correct code to visualize sparsity pattern
Attempts:
2 left
💡 Hint
Use the function designed to visualize sparse matrix structure.
✗ Incorrect
plt.spy() is the correct function to visualize the sparsity pattern of a matrix.
🚀 Application
expert2:30remaining
Choosing the best solver for a large sparse symmetric positive definite matrix
You have a large sparse symmetric positive definite matrix A and vector b. Which solver is the best choice to efficiently solve Ax = b?
Attempts:
2 left
💡 Hint
Consider matrix properties and solver efficiency for symmetric positive definite matrices.
✗ Incorrect
The conjugate gradient solver (cg) is optimized for large sparse symmetric positive definite matrices and is more efficient than other solvers.