Challenge - 5 Problems
LU Decomposition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of LU decomposition matrices
Given the matrix
A = [[4, 3], [6, 3]], what is the output of the LU decomposition using scipy.linalg.lu?SciPy
import numpy as np from scipy.linalg import lu A = np.array([[4, 3], [6, 3]]) P, L, U = lu(A) print(np.round(L, 2)) print(np.round(U, 2))
Attempts:
2 left
💡 Hint
Remember that LU decomposition with pivoting returns P, L, U matrices where P is a permutation matrix.
✗ Incorrect
The LU decomposition with pivoting rearranges rows to maximize numerical stability. The matrix P permutes rows, L is lower triangular with 1s on the diagonal, and U is upper triangular. For the given matrix, the correct L and U matrices correspond to option D.
🧠 Conceptual
intermediate1:30remaining
Purpose of permutation matrix in LU decomposition
What is the main purpose of the permutation matrix
P in the LU decomposition P * A = L * U?Attempts:
2 left
💡 Hint
Think about why row swaps might be needed in matrix factorization.
✗ Incorrect
The permutation matrix
P reorders the rows of A to place the largest pivot elements on the diagonal, which improves numerical stability during decomposition.🔧 Debug
advanced1:30remaining
Identify the error in LU decomposition code
What error will this code raise?
import numpy as np from scipy.linalg import lu A = np.array([[1, 2], [3, 4], [5, 6]]) P, L, U = lu(A)
Attempts:
2 left
💡 Hint
Check the shape requirements for LU decomposition.
✗ Incorrect
LU decomposition requires a square matrix. The input matrix has shape (3, 2), which is not square, so scipy.linalg.lu raises a ValueError.
❓ data_output
advanced2:00remaining
Number of non-zero elements in L and U matrices
After performing LU decomposition on the matrix
A = [[2, 1, 1], [4, -6, 0], [-2, 7, 2]], how many non-zero elements are there in the L and U matrices combined?SciPy
import numpy as np from scipy.linalg import lu A = np.array([[2, 1, 1], [4, -6, 0], [-2, 7, 2]]) P, L, U = lu(A) nonzero_count = np.count_nonzero(L) + np.count_nonzero(U) print(nonzero_count)
Attempts:
2 left
💡 Hint
Count all non-zero elements in both L and U matrices after decomposition.
✗ Incorrect
The L matrix is lower triangular with ones on the diagonal, and U is upper triangular. Counting their non-zero elements for this 3x3 matrix results in 11.
🚀 Application
expert2:30remaining
Using LU decomposition to solve a linear system
Given the system
Ax = b where A = [[3, 1], [1, 2]] and b = [9, 8], which code snippet correctly uses LU decomposition from scipy.linalg to find x?Attempts:
2 left
💡 Hint
Use lu_factor and lu_solve functions for efficient solving.
✗ Incorrect
The recommended way to solve linear systems using LU decomposition in scipy is to use lu_factor to get LU and pivot arrays, then lu_solve to solve for x. Option C correctly implements this.