qr#

rlinalg.qr(a: numpy.typing.ArrayLike, mode: Literal['full', 'r', 'economic', 'raw'] = 'full', tol: float = 1e-07, check_finite: bool = True, overwrite_a: bool = False) Union[Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, int], Tuple[Tuple[numpy.ndarray, numpy.ndarray], numpy.ndarray, numpy.ndarray, int], Tuple[numpy.ndarray, numpy.ndarray, int]]#

Compute QR decomposition of a matrix.

Calculate the decomposition A[:, P] = Q R where Q is unitary/orthogonal, R upper triangular, and P is a permutation such that columns with near-zero norm are moved towards the right-hand edge of A.

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.)

  • 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’ makes the function return two matrices (Q, TAU) in the internal format used by LAPACK and LINPACK.

  • 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.

  • tol (float) – The absolute tolerance to which each column norm is required. An column is considered negligible when its norm falls under this value.

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,). The column permutation.

  • rank (int) – The rank of the matrix.

Raises:

ValueError – When parameters are not compatible.

Notes

This is an interface to the R-modified LINPACK routine dqrdc2.

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).

See also

numpy.linalg.qr

The NumPy implementation based on LAPACK.

scipy.linalg.qr

The SciPy implementation based on LAPACK.

qr

Documentation of the equivalent R function qr.

Examples

>>> import numpy
>>> import rlinalg
>>> rng = numpy.random.default_rng()
>>> a = rng.standard_normal((9, 6))
>>> q, r, p, k = rlinalg.qr(a)
>>> numpy.allclose(a, numpy.dot(q, r))
True
>>> q.shape, r.shape
((9, 9), (9, 6))
>>> r2, p2, rank = rlinalg.qr(a, mode='r')
>>> numpy.allclose(r, r2)
True
>>> q3, r3, p3, rank = rlinalg.qr(a, mode='economic')
>>> q3.shape, r3.shape
((9, 6), (6, 6))
>>> q4, r4, p4, rank = rlinalg.qr(a)
>>> d = numpy.abs(numpy.diag(r4))
>>> numpy.allclose(a[:, p4], numpy.dot(q4, r4))
True
>>> q4.shape, r4.shape, p4.shape
((9, 9), (9, 6), (6,))
>>> q5, r5, p5, rank = rlinalg.qr(a, mode='economic')
>>> q5.shape, r5.shape, p5.shape
((9, 6), (6, 6), (6,))

References

Anderson., E., et al. (1999) LAPACK Users’ Guide. Third Edition. SIAM. Available on-line at https://netlib.org/lapack/lug/lapack_lug.html.

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

Dongarra, J. J., Bunch, J. R., Moler, C. B. and Stewart, G. W. (1978) LINPACK Users Guide. Philadelphia: SIAM Publications.