Bird
Raised Fist0
SciPydata~20 mins

LU decomposition in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does LU decomposition do to a square matrix A?
easy
A. Splits A into three matrices: P, L, and U
B. Calculates the determinant of A
C. Finds the inverse of A directly
D. Transforms A into a diagonal matrix only

Solution

  1. Step 1: Understand LU decomposition purpose

    LU decomposition breaks a matrix into three matrices: a permutation matrix P, a lower triangular matrix L, and an upper triangular matrix U.
  2. Step 2: Compare options with definition

    Only Splits A into three matrices: P, L, and U correctly describes this splitting. Other options describe different matrix operations.
  3. Final Answer:

    Splits A into three matrices: P, L, and U -> Option A
  4. Quick Check:

    LU decomposition = P, L, U matrices [OK]
Hint: LU always produces P, L, U matrices from A [OK]
Common Mistakes:
  • Confusing LU with determinant calculation
  • Thinking LU gives inverse directly
  • Assuming LU produces only diagonal matrix
2. Which of the following is the correct way to import the LU decomposition function from SciPy?
easy
A. import lu from scipy.linalg
B. import scipy.lu
C. from scipy import lu_decompose
D. from scipy.linalg import lu

Solution

  1. Step 1: Recall SciPy LU import syntax

    The LU decomposition function is in the scipy.linalg module and imported as lu.
  2. Step 2: Check each option's syntax

    from scipy.linalg import lu uses correct Python import syntax. Options B, C, and D are invalid or incorrect module/function names.
  3. Final Answer:

    from scipy.linalg import lu -> Option D
  4. Quick Check:

    Correct import = from scipy.linalg import lu [OK]
Hint: Use 'from scipy.linalg import lu' to import LU [OK]
Common Mistakes:
  • Trying to import lu directly from scipy
  • Using wrong function name like lu_decompose
  • Incorrect import syntax like 'import lu from ...'
3. What is the output of this code?
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))
medium
A. [[1. 0. ] [0.67 1. ]]
B. [[4. 3. ] [6. 3. ]]
C. [[1.5 0. ] [0. 1. ]]
D. [[0.67 1. ] [1. 0. ]]

Solution

  1. Step 1: Perform LU decomposition on matrix A

    Using scipy.linalg.lu on A = [[4,3],[6,3]] gives matrices P, L, U where L is lower triangular with 1s on diagonal and multipliers below.
  2. Step 2: Calculate L matrix values

    L = [[1, 0], [6/4, 1]] = [[1, 0], [1.5, 1]]. But scipy.linalg.lu applies row permutations, so actual L is [[1,0],[0.67,1]] after permutation.
  3. Final Answer:

    [[1. 0. ] [0.67 1. ]] -> Option A
  4. Quick Check:

    L matrix lower triangular with 1 diagonal = [[1,0],[0.67,1]] [OK]
Hint: L is lower triangular with 1s on diagonal [OK]
Common Mistakes:
  • Confusing L with original matrix A
  • Ignoring row permutations affecting L
  • Rounding errors in L matrix values
4. Identify the error in this LU decomposition code:
import numpy as np
from scipy.linalg import lu
A = np.array([[1, 2], [3, 4]])
P, L, U = lu(A)
print(L * U)
medium
A. Function lu returns only two matrices, not three
B. Matrix multiplication should use @, not *
C. LU decomposition requires square matrix, but A is not square
D. Numpy array A must be float type for LU

Solution

  1. Step 1: Understand matrix multiplication in numpy

    The code uses L * U, which does element-wise multiplication, not matrix multiplication.
  2. Step 2: Correct multiplication operator

    Matrix multiplication requires L @ U or np.dot(L, U) to multiply matrices properly.
  3. Final Answer:

    Matrix multiplication should use @, not * -> Option B
  4. Quick Check:

    Use @ for matrix multiply, not * [OK]
Hint: Use @ for matrix multiply, * is element-wise [OK]
Common Mistakes:
  • Using * instead of @ for matrix multiplication
  • Thinking LU returns only two matrices
  • Assuming matrix must be float type always
5. You want to solve the system Ax = b using LU decomposition with SciPy. Which steps correctly describe the process?
hard
A. Decompose A into L, U only; solve Lx = b; solve Ux = b
B. Decompose A into P, L, U; solve Ux = Pb; solve Ly = x
C. Decompose A into P, L, U; solve Ly = Pb; solve Ux = y
D. Decompose A into P, L, U; solve Px = b; solve Ly = x; solve Uz = y

Solution

  1. Step 1: Perform LU decomposition with permutation

    LU decomposition gives P, L, U such that PA = LU.
  2. Step 2: Solve system using forward and backward substitution

    First solve Ly = Pb (forward substitution), then solve Ux = y (backward substitution) to find x.
  3. Final Answer:

    Decompose A into P, L, U; solve Ly = Pb; solve Ux = y -> Option C
  4. Quick Check:

    Use P, L, U and solve Ly=Pb then Ux=y [OK]
Hint: Solve Ly=Pb then Ux=y after LU [OK]
Common Mistakes:
  • Ignoring permutation matrix P in solving
  • Solving equations in wrong order
  • Confusing variables in substitution steps
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard