0
0
SciPydata~20 mins

Sparse matrix factorizations in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sparse Matrix Factorization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of sparse LU factorization
What is the shape of the L and U factors after performing LU factorization on a 4x4 sparse matrix?
SciPy
import numpy as np
from scipy.sparse import csc_matrix
from scipy.sparse.linalg import splu

A = csc_matrix(np.array([[4, 0, 0, 0], [3, 5, 0, 0], [0, 1, 7, 0], [0, 0, 2, 6]]))
lu = splu(A)
L_shape = lu.L.shape
U_shape = lu.U.shape
print(L_shape, U_shape)
A(4, 4) (4, 4)
B(3, 3) (3, 3)
C(4, 3) (3, 4)
D(5, 5) (5, 5)
Attempts:
2 left
💡 Hint
LU factorization of an n x n matrix produces L and U matrices of the same shape as the original matrix.
data_output
intermediate
2:00remaining
Number of non-zero elements after Cholesky factorization
Given a 3x3 sparse positive definite matrix, what is the number of non-zero elements in the Cholesky factor?
SciPy
import numpy as np
from scipy.sparse import csc_matrix
from scipy.sparse.linalg import cholesky

A = csc_matrix(np.array([[4, 1, 0], [1, 3, 1], [0, 1, 2]]))
ch = cholesky(A)
L = ch.L()
nnz = L.nnz
print(nnz)
A6
B5
C4
D7
Attempts:
2 left
💡 Hint
Count the non-zero elements in the lower triangular Cholesky factor matrix.
🔧 Debug
advanced
2:00remaining
Identify the error in sparse QR factorization code
What error will this code raise when trying to perform QR factorization on a sparse matrix using scipy?
SciPy
import numpy as np
from scipy.sparse import csc_matrix
from scipy.sparse.linalg import qr

A = csc_matrix(np.array([[1, 2], [3, 4]]))
Q, R = qr(A)
print(Q.toarray())
ANo error, prints Q matrix
BTypeError: cannot unpack non-iterable NoneType object
CValueError: too many values to unpack (expected 2)
DAttributeError: module 'scipy.sparse.linalg' has no attribute 'qr'
Attempts:
2 left
💡 Hint
Check if scipy.sparse.linalg has a qr function.
🚀 Application
advanced
2:00remaining
Choosing factorization for solving sparse linear systems
You have a large sparse symmetric positive definite matrix. Which factorization method is best suited for solving linear systems efficiently?
ALU factorization
BSingular Value Decomposition (SVD)
CCholesky factorization
DQR factorization
Attempts:
2 left
💡 Hint
Consider matrix properties and factorization efficiency.
🧠 Conceptual
expert
3:00remaining
Effect of fill-in during sparse LU factorization
What does 'fill-in' mean in the context of sparse LU factorization, and why is it important?
AFill-in refers to the creation of new non-zero elements in L and U that were zero in the original matrix, increasing memory and computation cost.
BFill-in is the process of filling missing data in sparse matrices before factorization.
CFill-in means removing zero elements from the matrix to make it denser for better factorization.
DFill-in describes the compression of sparse matrices to reduce storage space.
Attempts:
2 left
💡 Hint
Think about how factorization changes the sparsity pattern.