Challenge - 5 Problems
Sparse Solver Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of sparse linear system solution
What is the output of the following code that solves a sparse linear system using
spsolve?SciPy
import numpy as np from scipy.sparse import csc_matrix from scipy.sparse.linalg import spsolve A = csc_matrix([[3, 0, 0], [0, 4, 0], [0, 0, 5]]) b = np.array([3, 8, 10]) x = spsolve(A, b) print(x)
Attempts:
2 left
💡 Hint
Remember that spsolve solves Ax = b for x.
✗ Incorrect
The matrix A is diagonal with entries 3, 4, and 5. Solving Ax = b means dividing each element of b by the corresponding diagonal element of A, resulting in [1, 2, 2].
🧠 Conceptual
intermediate1:30remaining
Understanding spsolve input requirements
Which of the following statements about the inputs to
spsolve is TRUE?Attempts:
2 left
💡 Hint
Check the documentation for the expected matrix type for A.
✗ Incorrect
spsolve requires A to be a sparse matrix in formats like CSC or CSR. The vector b can be a dense array or list, but A must be sparse.
🔧 Debug
advanced2:00remaining
Identify the error in sparse solver usage
What error will the following code raise when executed?
SciPy
import numpy as np from scipy.sparse import csr_matrix from scipy.sparse.linalg import spsolve A = csr_matrix([[1, 2], [3, 4], [5, 6]]) b = np.array([7, 8]) x = spsolve(A, b) print(x)
Attempts:
2 left
💡 Hint
Check the shapes of A and b before solving.
✗ Incorrect
Matrix A has shape (3, 2) but vector b has length 2, so dimensions do not align for solving Ax = b. This causes a ValueError.
❓ data_output
advanced2:00remaining
Resulting vector from sparse system with zero entries
Given the sparse matrix and vector below, what is the output vector after solving with
spsolve?SciPy
import numpy as np from scipy.sparse import csc_matrix from scipy.sparse.linalg import spsolve A = csc_matrix([[10, 0, 0], [0, 0, 0], [0, 0, 5]]) b = np.array([20, 0, 15]) x = spsolve(A, b) print(x)
Attempts:
2 left
💡 Hint
Check if the matrix A is invertible.
✗ Incorrect
The second row of A is all zeros, making A singular (not invertible). spsolve raises a LinAlgError in this case.
🚀 Application
expert3:00remaining
Using spsolve for a large sparse system
You have a large sparse matrix A of size 10000x10000 with mostly zeros and a vector b of length 10000. Which approach is MOST efficient to solve Ax = b using SciPy?
Attempts:
2 left
💡 Hint
Consider memory and computation time for large sparse matrices.
✗ Incorrect
spsolve is optimized for sparse matrices and large systems. Converting to dense wastes memory and time. Using spsolve directly is efficient.