QR decomposition in SciPy - Time & Space Complexity
We want to understand how the time needed to perform QR decomposition changes as the size of the matrix grows.
Specifically, how does the work increase when we have bigger matrices?
Analyze the time complexity of the following code snippet.
import numpy as np
from scipy.linalg import qr
A = np.random.rand(100, 100)
Q, R = qr(A)
This code creates a 100x100 matrix and computes its QR decomposition using SciPy.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Multiplying and subtracting vectors and matrices during the decomposition process.
- How many times: These operations happen repeatedly for each column and row, roughly proportional to the cube of the matrix size.
As the matrix size grows, the number of calculations grows much faster.
| Input Size (n x n) | Approx. Operations |
|---|---|
| 10 x 10 | About 1,000 operations |
| 100 x 100 | About 1,000,000 operations |
| 1000 x 1000 | About 1,000,000,000 operations |
Pattern observation: When the matrix size doubles, the work increases roughly by eight times.
Time Complexity: O(n^3)
This means the time needed grows roughly with the cube of the matrix size, so bigger matrices take much longer to process.
[X] Wrong: "QR decomposition takes time proportional to the number of elements in the matrix (n squared)."
[OK] Correct: The process involves many matrix multiplications and vector operations that multiply the work, making it grow faster than just the number of elements.
Understanding how QR decomposition scales helps you explain the cost of matrix operations clearly, a useful skill in data science and engineering roles.
"What if we only decomposed a rectangular matrix with many more rows than columns? How would the time complexity change?"
Practice
A?Solution
Step 1: Understand QR decomposition definition
QR decomposition splits a matrix into two parts: an orthogonal matrixQand an upper triangular matrixR.Step 2: Compare options with definition
Only SplitsAinto an orthogonal matrixQand an upper triangular matrixRcorrectly describes this split. Others describe different matrix operations.Final Answer:
Splits A into an orthogonal matrix Q and an upper triangular matrix R -> Option CQuick Check:
QR decomposition = Q and R matrices [OK]
- Confusing QR with LU decomposition
- Thinking QR produces diagonal matrices
- Assuming QR finds matrix inverse
Solution
Step 1: Recall scipy QR import syntax
The correct import is fromscipy.linalgimportqr.Step 2: Check usage correctness
Callingqr(A)returnsQandR. from scipy.linalg import qr Q, R = qr(A) matches this exactly.Final Answer:
from scipy.linalg import qr Q, R = qr(A) -> Option AQuick Check:
Correct import and call = from scipy.linalg import qr Q, R = qr(A) [OK]
- Trying to import qr directly from scipy
- Using incorrect import syntax
- Calling qr without importing
A = [[1, 2], [3, 4]], what is the shape of Q and R after Q, R = qr(A)?Solution
Step 1: Check input matrix shape
MatrixAhas shape (2, 2).Step 2: Understand QR output shapes
For a square matrix,QandRboth have the same shape asA, which is (2, 2).Final Answer:
Q is (2, 2) and R is (2, 2) -> Option DQuick Check:
Square matrix QR outputs same shape matrices [OK]
- Assuming R has fewer columns
- Confusing Q shape with R shape
- Thinking Q or R is 1D
from scipy.linalg import qr A = [[1, 2], [3, 4]] Q, R = qr(A) print(Q)
Solution
Step 1: Verify import statement
The importfrom scipy.linalg import qris correct.Step 2: Check qr function returns
qr(A)returns two valuesQandR.Step 3: Validate input type
List of lists forAworks asqraccepts array-like inputs and converts internally.Step 4: Check print statement
print(Q)correctly outputs theQmatrix.Final Answer:
Nothing is wrong with this code snippet -> Option AQuick Check:
Code runs successfully, produces correct Q and R [OK]
- Thinking Python lists cannot be passed to qr
- Misunderstanding qr return values
- Incorrect import statements
Ax = b using QR decomposition where A is a 3x3 matrix and b is a vector. Which step correctly describes how to find x after computing Q, R = qr(A)?Solution
Step 1: Use QR decomposition to rewrite Ax = b
SinceA = QR, rewrite asQRx = b.Step 2: Multiply both sides by
Multiply byQ.TQ.Tto getRx = Q.T b. Definey = Q.T b.Step 3: Solve upper triangular system
Now solveRx = yusing back substitution to findx.Final Answer:
Compute y = Q.T @ b then solve Rx = y using back substitution -> Option BQuick Check:
Use Q.T b and solve Rx = y [OK]
- Multiplying b by Q instead of Q.T
- Trying to solve Qx = y instead of Rx = y
- Ignoring that R is upper triangular
Ready to prove it?
Pass the test and this pattern moves unknown
