Orthogonal-triangular factorization
Solving or analyzing linear systems
SciPy · Function
Computes the QR decomposition of a matrix, expressing it as an orthogonal/unitary factor Q and an upper-triangular factor R. Modes select full, reduced, R-only, or raw outputs, and pivoting supports rank-revealing factorization.
The function factorizes a matrix into an orthogonal or unitary factor Q and an upper-triangular factor R. Its mode controls which factors and shapes are returned; optional pivoting produces a rank-revealing factorization with a permutation vector.
scipy.linalg.qr(a, overwrite_a=False, lwork=None, mode='full', pivoting=False, check_finite=True)| Name | Type | Description |
|---|---|---|
a | array_like | Input matrix to factorize. |
overwrite_a | bool, optional | Allow the factorization to overwrite the input array when possible. |
lwork | int, optional | Workspace size; the default lets the implementation choose it. |
mode | {'full', 'r', 'economic', 'raw'}, optional | Selects the returned QR representation: 'full', 'r', 'economic', or 'raw'. |
pivoting | bool, optional | Request rank-revealing column pivoting. |
check_finite | bool, optional | Check that the input contains only finite values. |
Depending on mode and pivoting, returns Q, R, and optionally P. Q is float or complex with shape (M, M), or (M, K) for economic mode; R has shape (M, N), or (K, N) for economic/raw mode; P has shape (N,) when pivoting=True.
import numpy as np
from scipy.linalg import qr
A = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
Q, R = qr(A, mode="economic")
print(np.allclose(Q @ R, A))
print(Q.shape, R.shape)Computes a compact QR factorization and verifies that multiplying Q by R reconstructs the input matrix.
Solving or analyzing linear systems
Reducing storage for rectangular matrices
Estimating numerical rank
The function computes A = Q R, where Q is unitary or orthogonal and R is upper triangular. By default, mode='full' returns both factors.
Follow-up: How can the factorization expose rank structure? Enable pivoting=True; the resulting column permutation is chosen so the diagonal of R is non-increasing.
mode='economic' returns both factors using reduced dimensions, with K = min(M, N). mode='r' returns only R, while mode='raw' returns Q and TAU instead of an explicit Q matrix.
Follow-up: Why use economic mode for a rectangular matrix? It avoids storing the extra orthogonal dimensions that are not needed for the compact factorization.
pivoting=True computes A[:, P] = Q @ R and returns P, an integer array of shape (N,). The permutation is intended for rank-revealing QR because the diagonal of R is ordered non-increasingly.
Follow-up: What changes when pivoting=False? No permutation vector is returned, and the ordinary relation A = Q @ R applies.
scipy.linalg.qr_multiplyscipy.linalg.orthogonal_procrustesscipy.linalg.rq