Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'solve' which is for dense matrices.
Importing 'linalg' instead of the solver function.
✗ Incorrect
The spsolve function is used to solve sparse linear systems efficiently.
2fill in blank
mediumComplete the code to create a sparse matrix in CSR format.
SciPy
from scipy.sparse import [1] matrix = [1]((data, (row, col)), shape=(4, 4))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dense_matrix' which is not a sparse format.
Confusing CSR with COO format.
✗ Incorrect
The csr_matrix format is efficient for arithmetic and matrix vector operations.
3fill in blank
hardFix the error in the code to solve a sparse linear system.
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([1], b)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the vector 'b' as the first argument.
Passing the function name instead of the matrix.
✗ Incorrect
The first argument to spsolve must be the sparse matrix A.
4fill in blank
hardFill both blanks to create a sparse diagonal matrix and solve it.
SciPy
from scipy.sparse import [1] from scipy.sparse.linalg import spsolve D = [2]([1, 2, 3], 0, shape=(3, 3)) b = [1, 4, 9] x = spsolve(D, b)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'csr_matrix' to create diagonal matrices directly.
Confusing 'diags' with 'csc_matrix'.
✗ Incorrect
The diags function creates a sparse diagonal matrix from the given diagonal values.
5fill in blank
hardFill all three blanks to create a sparse system and solve it efficiently.
SciPy
from scipy.sparse import [1] from scipy.sparse.linalg import [2] A = [1]([[10, 0], [0, 5]]) b = [20, 15] x = [2](A, b)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dense matrix formats.
Mixing up the order of arguments.
✗ Incorrect
Use csr_matrix to create the sparse matrix and spsolve to solve the system.