0
0
SciPydata~20 mins

LU decomposition in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LU Decomposition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
A
[[1.   0.  ]
 [0.67 1.  ]]
[[4.  3.]
 [0.  -1.5]]
B
[[1.   0.  ]
 [1.5  1.  ]]
[[4.  3.]
 [0.  -1.5]]
C
[[1.   0.  ]
 [0.5  1.  ]]
[[4.  3.]
 [0.  -1.5]]
D
[[1.   0.  ]
 [0.67 1.  ]]
[[6.   3.  ]
 [0.   -1.  ]]
Attempts:
2 left
💡 Hint
Remember that LU decomposition with pivoting returns P, L, U matrices where P is a permutation matrix.
🧠 Conceptual
intermediate
1: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?
ATo reorder rows of <code>A</code> to improve numerical stability
BTo reorder columns of <code>A</code> to reduce computation
CTo convert <code>A</code> into a diagonal matrix
DTo scale the matrix <code>A</code> before decomposition
Attempts:
2 left
💡 Hint
Think about why row swaps might be needed in matrix factorization.
🔧 Debug
advanced
1: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)
AValueError: Last 2 dimensions of the array must be square
BTypeError: lu() missing 1 required positional argument
CNo error, code runs successfully
DIndexError: index out of bounds
Attempts:
2 left
💡 Hint
Check the shape requirements for LU decomposition.
data_output
advanced
2: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)
A10
B11
C9
D12
Attempts:
2 left
💡 Hint
Count all non-zero elements in both L and U matrices after decomposition.
🚀 Application
expert
2: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?
A
import numpy as np
from scipy.linalg import lu
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
P, L, U = lu(A)
x = np.linalg.solve(U, np.linalg.solve(L, np.dot(P.T, b)))
print(x)
B
import numpy as np
from scipy.linalg import lu
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
P, L, U = lu(A)
x = np.linalg.solve(U, np.linalg.solve(L, np.dot(P, b)))
print(x)
C
import numpy as np
from scipy.linalg import lu_solve, lu_factor
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
lu, piv = lu_factor(A)
x = lu_solve((lu, piv), b)
print(x)
D
import numpy as np
from scipy.linalg import lu_factor, lu_solve
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
lu, piv = lu_factor(A)
x = np.dot(np.linalg.inv(L), b)
print(x)
Attempts:
2 left
💡 Hint
Use lu_factor and lu_solve functions for efficient solving.