Complete the code to import the sparse solver function from scipy.
from scipy.sparse.linalg import [1]
The function spsolve is the correct sparse direct solver imported from scipy.sparse.linalg.
Complete the code to create a sparse matrix using scipy's csr_matrix.
from scipy.sparse import [1] data = [1, 2, 3] rows = [0, 1, 2] cols = [0, 1, 2] sparse_matrix = [1]((data, (rows, cols)), shape=(3, 3))
csr_matrix is a common sparse matrix format used for efficient arithmetic and matrix vector operations.
Fix the error in the code to solve the sparse linear system Ax = b.
from scipy.sparse.linalg import spsolve from scipy.sparse import csr_matrix A = csr_matrix([[3, 0], [0, 4]]) b = [6, 8] x = spsolve(A, [1])
The right-hand side vector b must be passed to spsolve as the second argument.
Fill both blanks to create a sparse diagonal matrix and solve Ax = b.
from scipy.sparse import [1] from scipy.sparse.linalg import spsolve import numpy as np A = [2](np.array([1, 2, 3])) b = np.array([1, 4, 9]) x = spsolve(A, b)
diags creates a sparse diagonal matrix from the given array. We use it both to import and to create matrix A.
Fill all three blanks to solve a sparse system and print the solution.
from scipy.sparse import [1] from scipy.sparse.linalg import [2] import numpy as np A = [1]([[4, 1], [1, 3]]) b = np.array([1, 2]) x = [2](A, b) print(x)
We import csr_matrix to create the sparse matrix A, and spsolve to solve the system. The matrix A is created using csr_matrix.