{ } Raw JSON

bundles / scipy latest / scipy / linalg / _decomp_qr / qr

function

scipy.linalg._decomp_qr:qr

source: /scipy/linalg/_decomp_qr.py :27

Signature

def   qr ( a overwrite_a = False lwork = None mode = full pivoting = False check_finite = True )

Summary

Compute QR decomposition of a matrix.

Extended Summary

Calculate the decomposition A = Q R where Q is unitary/orthogonal and R upper triangular.

The documentation is written assuming array arguments are of specified "core" shapes. However, array argument(s) of this function may have additional "batch" dimensions prepended to the core shape. In this case, the array is treated as a batch of lower-dimensional slices; see linalg_batch for details. Note that calls with zero-size batches are unsupported and will raise a ValueError.

Parameters

a : (M, N) array_like

Matrix to be decomposed

overwrite_a : bool, optional

Whether data in a is overwritten (may improve performance if overwrite_a is set to True by reusing the existing input data structure rather than creating a new one.)

lwork : int, optional

Work array size, lwork >= a.shape[1]. If None or -1, an optimal size is computed.

mode : {'full', 'r', 'economic', 'raw'}, optional

Determines what information is to be returned: either both Q and R ('full', default), only R ('r') or both Q and R but computed in economy-size ('economic', see Notes). The final option 'raw' (added in SciPy 0.11) makes the function return two matrices (Q, TAU) in the internal format used by LAPACK.

pivoting : bool, optional

Whether or not factorization should include pivoting for rank-revealing qr decomposition. If pivoting, compute the decomposition A[:, P] = Q @ R as above, but where P is chosen such that the diagonal of R is non-increasing. Equivalently, albeit less efficiently, an explicit P matrix may be formed explicitly by permuting the rows or columns (depending on the side of the equation on which it is to be used) of an identity matrix. See Examples.

check_finite : bool, optional

Whether to check that the input matrix contains only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination) if the inputs do contain infinities or NaNs.

Returns

Q : float or complex ndarray

Of shape (M, M), or (M, K) for mode='economic'. Not returned if mode='r'. Replaced by tuple (Q, TAU) if mode='raw'.

R : float or complex ndarray

Of shape (M, N), or (K, N) for mode in ['economic', 'raw']. K = min(M, N).

P : int ndarray

Of shape (N,) for pivoting=True. Not returned if pivoting=False.

Raises

: LinAlgError

Raised if decomposition fails

Notes

This is an interface to the LAPACK routines dgeqrf, zgeqrf, dorgqr, zungqr, dgeqp3, and zgeqp3.

If mode=economic, the shapes of Q and R are (M, K) and (K, N) instead of (M,M) and (M,N), with K=min(M,N).

Examples

import numpy as np
from scipy import linalg
rng = np.random.default_rng()
a = rng.standard_normal((9, 6))
q, r = linalg.qr(a)
np.allclose(a, np.dot(q, r))
q.shape, r.shape
r2 = linalg.qr(a, mode='r')
np.allclose(r, r2)
q3, r3 = linalg.qr(a, mode='economic')
q3.shape, r3.shape
q4, r4, p4 = linalg.qr(a, pivoting=True)
d = np.abs(np.diag(r4))
np.all(d[1:] <= d[:-1])
np.allclose(a[:, p4], np.dot(q4, r4))
P = np.eye(p4.size)[p4]
np.allclose(a, np.dot(q4, r4) @ P)
np.allclose(a @ P.T, np.dot(q4, r4))
q4.shape, r4.shape, p4.shape
q5, r5, p5 = linalg.qr(a, mode='economic', pivoting=True)
q5.shape, r5.shape, p5.shape
P = np.eye(6)[:, p5]
np.allclose(a @ P, np.dot(q5, r5))

Aliases

  • scipy.linalg.qr