0
0
SciPydata~10 mins

Sparse linear algebra solvers in SciPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the sparse linear solver from scipy.

SciPy
from scipy.sparse.linalg import [1]
Drag options to blanks, or click blank then click option'
Asolve
Bsolve_sparse
Csparse_solve
Dspsolve
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'solve' which is for dense matrices.
Trying to import a non-existent function like 'solve_sparse'.
2fill in blank
medium

Complete the code to create a sparse matrix in CSR format.

SciPy
from scipy.sparse import [1]

matrix = [1]((data, (row_indices, col_indices)), shape=(4, 4))
Drag options to blanks, or click blank then click option'
Acsr_matrix
Bcsc_matrix
Ccoo_matrix
Ddia_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'csc_matrix' which is Compressed Sparse Column format.
Using 'coo_matrix' which is coordinate format.
3fill in blank
hard

Fix the error in solving a sparse linear system using spsolve.

SciPy
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])
Drag options to blanks, or click blank then click option'
Ax
BA
Cb
D[6, 8]
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the matrix A instead of vector b.
Passing the solution variable x before it is defined.
4fill in blank
hard

Fill both blanks to create a sparse diagonal matrix and solve the system.

SciPy
from scipy.sparse import [1]
from scipy.sparse.linalg import [2]

D = [1]([1, 2, 3, 4])
b = [1, 4, 9, 16]
x = [2](D, b)
Drag options to blanks, or click blank then click option'
Adiags
Bspsolve
Ccsr_matrix
Dsolve
Attempts:
3 left
💡 Hint
Common Mistakes
Using csr_matrix to create diagonal matrix without specifying diagonals.
Using solve which is for dense matrices.
5fill in blank
hard

Fill all three blanks to create a sparse matrix, convert it to CSR format, and solve the system.

SciPy
from scipy.sparse import [1]
from scipy.sparse.linalg import [2]

coo = [1]((data, (rows, cols)), shape=(3, 3))
csr = coo.[3]()
b = [1, 2, 3]
x = [2](csr, b)
Drag options to blanks, or click blank then click option'
Acoo_matrix
Bspsolve
Ctocsr
Dcsr_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to solve directly with COO format without conversion.
Using csr_matrix constructor instead of conversion method.