0
0
SciPydata~10 mins

QR decomposition in SciPy - Step-by-Step Execution

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