Bird
Raised Fist0
SciPydata~10 mins

QR decomposition in SciPy - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - QR decomposition
Start with matrix A
↓
Apply QR decomposition
↓
Get Q (orthogonal matrix)
↓
Get R (upper triangular matrix)
↓
Verify A = Q * R
↓
End
We start with a matrix A, then split it into Q and R matrices using QR decomposition, and finally verify that multiplying Q and R returns the original matrix.
Execution Sample
SciPy
import numpy as np
from scipy.linalg import qr

A = np.array([[1, 2], [3, 4]])
Q, R = qr(A)
print(Q)
print(R)
This code computes the QR decomposition of matrix A and prints matrices Q and R.
Execution Table
StepActionMatrix/VariableValue/Result
1Define matrix AA[[1 2] [3 4]]
2Apply qr(A)Q[[-0.31622777 -0.9486833 ] [-0.9486833 0.31622777]]
3Apply qr(A)R[[-3.16227766 -4.42718872] [ 0. 0.63245553]]
4Verify A = Q * RQ @ R[[1. 2.] [3. 4.]]
5Print QOutput[[-0.31622777 -0.9486833 ] [-0.9486833 0.31622777]]
6Print ROutput[[-3.16227766 -4.42718872] [ 0. 0.63245553]]
7EndQR decomposition complete
💡 QR decomposition finished and verified that Q*R equals original matrix A
Variable Tracker
VariableStartAfter qr(A)Final
A[[1 2] [3 4]][[1 2] [3 4]][[1 2] [3 4]]
QNone[[-0.31622777 -0.9486833 ] [-0.9486833 0.31622777]][[-0.31622777 -0.9486833 ] [-0.9486833 0.31622777]]
RNone[[-3.16227766 -4.42718872] [ 0. 0.63245553]][[-3.16227766 -4.42718872] [ 0. 0.63245553]]
Key Moments - 3 Insights
Why are the values in Q negative?
The QR decomposition returns an orthogonal matrix Q which can have negative signs due to normalization; the important part is that Q is orthogonal, not that values are positive. See execution_table rows 2 and 5.
Why does R have zeros below the diagonal?
R is an upper triangular matrix by definition in QR decomposition, so all elements below the diagonal are zero. This is shown in execution_table row 3.
How do we know Q and R multiply back to A?
Row 4 in execution_table shows the multiplication Q @ R equals the original matrix A, confirming the decomposition is correct.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the shape of matrix R?
A2 rows and 3 columns
B2 rows and 2 columns
C3 rows and 2 columns
D1 row and 2 columns
💡 Hint
Check the printed matrix R in execution_table row 3; it has two rows and two columns.
At which step does the code verify that Q times R equals A?
AStep 2
BStep 6
CStep 4
DStep 7
💡 Hint
Look at execution_table row 4 where Q @ R is computed and compared to A.
If matrix A was changed to [[2, 0], [0, 2]], how would Q likely change?
AQ would be the identity matrix
BQ would be all zeros
CQ would be the same as before
DQ would be a zero matrix with negative values
💡 Hint
For a diagonal matrix like [[2,0],[0,2]], Q is usually the identity matrix as per QR decomposition properties.
Concept Snapshot
QR decomposition splits a matrix A into Q and R matrices.
Q is orthogonal (Q.T @ Q = I).
R is upper triangular.
A = Q * R exactly.
Use scipy.linalg.qr to compute.
Useful for solving linear systems and least squares.
Full Transcript
QR decomposition takes a matrix A and breaks it into two matrices Q and R. Q is orthogonal, meaning its transpose times itself is the identity matrix. R is upper triangular, meaning all elements below the diagonal are zero. We use scipy's qr function to get Q and R. After decomposition, multiplying Q and R returns the original matrix A, confirming the process. This is useful in many data science tasks like solving equations and regression.

Practice

(1/5)
1. What does QR decomposition do to a matrix A?
easy
A. Converts A into a lower triangular matrix only
B. Splits A into two diagonal matrices
C. Splits A into an orthogonal matrix Q and an upper triangular matrix R
D. Finds the inverse of A directly

Solution

  1. Step 1: Understand QR decomposition definition

    QR decomposition splits a matrix into two parts: an orthogonal matrix Q and an upper triangular matrix R.
  2. Step 2: Compare options with definition

    Only Splits A into an orthogonal matrix Q and an upper triangular matrix R correctly describes this split. Others describe different matrix operations.
  3. Final Answer:

    Splits A into an orthogonal matrix Q and an upper triangular matrix R -> Option C
  4. Quick Check:

    QR decomposition = Q and R matrices [OK]
Hint: QR always produces Q (orthogonal) and R (upper triangular) [OK]
Common Mistakes:
  • Confusing QR with LU decomposition
  • Thinking QR produces diagonal matrices
  • Assuming QR finds matrix inverse
2. Which of the following is the correct way to import and use QR decomposition from scipy?
easy
A. from scipy.linalg import qr Q, R = qr(A)
B. import scipy.qr Q, R = scipy.qr(A)
C. from scipy import qr Q, R = qr(A)
D. import qr from scipy.linalg Q, R = qr(A)

Solution

  1. Step 1: Recall scipy QR import syntax

    The correct import is from scipy.linalg import qr.
  2. Step 2: Check usage correctness

    Calling qr(A) returns Q and R. from scipy.linalg import qr Q, R = qr(A) matches this exactly.
  3. Final Answer:

    from scipy.linalg import qr Q, R = qr(A) -> Option A
  4. Quick Check:

    Correct import and call = from scipy.linalg import qr Q, R = qr(A) [OK]
Hint: Use 'from scipy.linalg import qr' to access QR function [OK]
Common Mistakes:
  • Trying to import qr directly from scipy
  • Using incorrect import syntax
  • Calling qr without importing
3. Given the matrix A = [[1, 2], [3, 4]], what is the shape of Q and R after Q, R = qr(A)?
medium
A. Q is (2, 2) and R is (2, 1)
B. Q is (1, 2) and R is (2, 2)
C. Q is (2, 1) and R is (1, 2)
D. Q is (2, 2) and R is (2, 2)

Solution

  1. Step 1: Check input matrix shape

    Matrix A has shape (2, 2).
  2. Step 2: Understand QR output shapes

    For a square matrix, Q and R both have the same shape as A, which is (2, 2).
  3. Final Answer:

    Q is (2, 2) and R is (2, 2) -> Option D
  4. Quick Check:

    Square matrix QR outputs same shape matrices [OK]
Hint: Q and R shapes match input matrix shape for square matrices [OK]
Common Mistakes:
  • Assuming R has fewer columns
  • Confusing Q shape with R shape
  • Thinking Q or R is 1D
4. What is wrong with this code snippet for QR decomposition?
from scipy.linalg import qr
A = [[1, 2], [3, 4]]
Q, R = qr(A)
print(Q)
medium
A. Nothing is wrong with this code snippet
B. The qr function does not return two values
C. Matrix A should be a numpy array, not a list of lists
D. Import statement is incorrect

Solution

  1. Step 1: Verify import statement

    The import from scipy.linalg import qr is correct.
  2. Step 2: Check qr function returns

    qr(A) returns two values Q and R.
  3. Step 3: Validate input type

    List of lists for A works as qr accepts array-like inputs and converts internally.
  4. Step 4: Check print statement

    print(Q) correctly outputs the Q matrix.
  5. Final Answer:

    Nothing is wrong with this code snippet -> Option A
  6. Quick Check:

    Code runs successfully, produces correct Q and R [OK]
Hint: scipy.linalg.qr accepts lists of lists as array-like input [OK]
Common Mistakes:
  • Thinking Python lists cannot be passed to qr
  • Misunderstanding qr return values
  • Incorrect import statements
5. You want to solve the system 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)?
hard
A. Compute x = Q @ b then solve Ry = x
B. Compute y = Q.T @ b then solve Rx = y using back substitution
C. Compute y = R @ b then solve Qx = y
D. Compute x = R.T @ Q @ b

Solution

  1. Step 1: Use QR decomposition to rewrite Ax = b

    Since A = QR, rewrite as QRx = b.
  2. Step 2: Multiply both sides by Q.T

    Multiply by Q.T to get Rx = Q.T b. Define y = Q.T b.
  3. Step 3: Solve upper triangular system

    Now solve Rx = y using back substitution to find x.
  4. Final Answer:

    Compute y = Q.T @ b then solve Rx = y using back substitution -> Option B
  5. Quick Check:

    Use Q.T b and solve Rx = y [OK]
Hint: Multiply b by Q.T, then solve Rx = y [OK]
Common Mistakes:
  • Multiplying b by Q instead of Q.T
  • Trying to solve Qx = y instead of Rx = y
  • Ignoring that R is upper triangular
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