Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

linalg.qr

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.

How it works

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.

Syntax

scipy.linalg.qr(a, overwrite_a=False, lwork=None, mode='full', pivoting=False, check_finite=True)

Parameters

NameTypeDescription
aarray_likeInput matrix to factorize.
overwrite_abool, optionalAllow the factorization to overwrite the input array when possible.
lworkint, optionalWorkspace size; the default lets the implementation choose it.
mode{'full', 'r', 'economic', 'raw'}, optionalSelects the returned QR representation: 'full', 'r', 'economic', or 'raw'.
pivotingbool, optionalRequest rank-revealing column pivoting.
check_finitebool, optionalCheck that the input contains only finite values.

Returns

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.

Key points

  • qr decomposes a matrix into orthogonal/unitary and upper-triangular factors.
  • The unpivoted mathematical relation is A = Q @ R, with Q orthogonal or unitary and R upper triangular.
  • The mode accepts full, r, economic, or raw.
  • Full returns both factors, r returns only R, and economic returns both in compact form.
  • pivoting enables a rank-revealing QR factorization.

Common mistakes

  • With pivoting enabled, reconstruct the original factorization using the permuted columns A[:, P], not A directly.
  • Mode 'r' returns only R, so code must not unpack a Q, R pair.
  • Mode 'raw' does not return the ordinary Q matrix; it returns a raw-factor representation as Q and TAU.

Example: Economic QR factorization

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.

When to use it

Orthogonal-triangular factorization

Solving or analyzing linear systems

Compact QR outputs

Reducing storage for rectangular matrices

Rank-revealing QR with pivoting

Estimating numerical rank

Interview questions

QWhat does scipy.linalg.qr compute?

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.

QHow do the QR modes differ?

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.

QWhat does QR pivoting return?

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_multiply
  • scipy.linalg.orthogonal_procrustes
  • scipy.linalg.rq

Source: qr — SciPy v1.16.0 Manual. Checked 2026-10-15.