0
0
SciPydata~10 mins

Sparse direct solvers (spsolve) 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 solver function from scipy.

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

Complete the code to create a sparse matrix using scipy's csr_matrix.

SciPy
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))
Drag options to blanks, or click blank then click option'
Adia_matrix
Bcsc_matrix
Ccoo_matrix
Dcsr_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 the code to solve the sparse linear system Ax = b.

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'
Ab
BA
C[6, 8]
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the matrix A instead of vector b.
Passing None or a list literal instead of the variable b.
4fill in blank
hard

Fill both blanks to create a sparse diagonal matrix and solve Ax = b.

SciPy
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)
Drag options to blanks, or click blank then click option'
Adiags
Bcsr_matrix
Ccsc_matrix
Deye
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'csr_matrix' or 'csc_matrix' directly with a 1D array.
Using 'eye' which creates an identity matrix, not diagonal from array.
5fill in blank
hard

Fill all three blanks to solve a sparse system and print the solution.

SciPy
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)
Drag options to blanks, or click blank then click option'
Acsr_matrix
Bspsolve
Csolve
Dcsc_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'solve' instead of 'spsolve' which is for dense matrices.
Using 'csc_matrix' to create A but not importing it.