0
0
SciPydata~20 mins

QR decomposition in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
QR Decomposition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of QR decomposition matrices shapes
Given the following code using scipy.linalg.qr, what are the shapes of matrices Q and R?
SciPy
import numpy as np
from scipy.linalg import qr

A = np.array([[1, 2], [3, 4], [5, 6]])
Q, R = qr(A, mode='economic')
print(Q.shape, R.shape)
A(2, 2) (2, 2)
B(3, 3) (3, 2)
C(3, 2) (3, 3)
D(3, 2) (2, 2)
Attempts:
2 left
💡 Hint
The 'economic' mode returns reduced shapes for Q and R.
data_output
intermediate
2:00remaining
Result of reconstructing matrix from QR factors
What is the output of the following code that reconstructs matrix A from its QR decomposition?
SciPy
import numpy as np
from scipy.linalg import qr

A = np.array([[2, 0], [1, 3]])
Q, R = qr(A)
reconstructed = np.dot(Q, R)
print(np.round(reconstructed, decimals=5))
A
[[2. 0.]
 [1. 3.]]
B
[[2. 0.]
 [0. 3.]]
C
[[2. 1.]
 [0. 3.]]
D
[[1. 0.]
 [1. 3.]]
Attempts:
2 left
💡 Hint
Multiplying Q and R should give back the original matrix A.
🔧 Debug
advanced
2:00remaining
Identify the error in QR decomposition code
What error will this code raise when executed?
SciPy
import numpy as np
from scipy.linalg import qr

A = np.array([[1, 2], [3, 4]])
Q, R = qr(A, mode='invalid_mode')
AAttributeError: module 'scipy.linalg' has no attribute 'qr'
BTypeError: qr() missing required positional argument
CValueError: Unknown mode 'invalid_mode'
DNo error, code runs successfully
Attempts:
2 left
💡 Hint
Check the valid modes for the qr function.
🧠 Conceptual
advanced
1:30remaining
Properties of Q matrix from QR decomposition
Which statement about the Q matrix from QR decomposition is true?
AQ is always a diagonal matrix
BQ is an orthogonal matrix with Q.T @ Q = I
CQ is a lower triangular matrix
DQ is the inverse of R
Attempts:
2 left
💡 Hint
Think about what orthogonal means for matrices.
🚀 Application
expert
3:00remaining
Using QR decomposition to solve linear system
Given matrix A and vector b, which code correctly solves Ax = b using QR decomposition?
SciPy
import numpy as np
from scipy.linalg import qr

A = np.array([[1, 2], [3, 4]])
b = np.array([5, 11])
A
Q, R = qr(A)
x = np.linalg.solve(R, Q.T @ b)
print(x)
B
Q, R = qr(A)
x = np.linalg.solve(Q, R @ b)
print(x)
C
Q, R = qr(A)
x = np.dot(R, np.linalg.inv(Q)) @ b
print(x)
D
Q, R = qr(A)
x = np.dot(np.linalg.inv(R), np.dot(Q, b))
print(x)
Attempts:
2 left
💡 Hint
Recall that A = QR and solve Rx = Q^T b for x.