numpy 1.22.4 Pypi GitHub Homepage
Other Docs
NotesParametersRaisesReturnsBackRef
qr(a, mode='reduced')

Factor the matrix a as qr, where q is orthonormal and r is upper-triangular.

Notes

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

For more information on the qr factorization, see for example: https://en.wikipedia.org/wiki/QR_factorization

Subclasses of ndarray are preserved except for the 'raw' mode. So if a is of type matrix , all the return values will be matrices too.

New 'reduced', 'complete', and 'raw' options for mode were added in NumPy 1.8.0 and the old option 'full' was made an alias of 'reduced'. In addition the options 'full' and 'economic' were deprecated. Because 'full' was the previous default and 'reduced' is the new default, backward compatibility can be maintained by letting :None:None:`mode` default. The 'raw' option was added so that LAPACK routines that can multiply arrays by q using the Householder reflectors can be used. Note that in this case the returned arrays are of type np.double or np.cdouble and the h array is transposed to be FORTRAN compatible. No routines using the 'raw' return are currently exposed by numpy, but some are available in lapack_lite and just await the necessary work.

Parameters

a : array_like, shape (..., M, N)

An array-like object with the dimensionality of at least 2.

mode : {'reduced', 'complete', 'r', 'raw'}, optional

If K = min(M, N), then

Raises

LinAlgError

If factoring fails.

Returns

q : ndarray of float or complex, optional

A matrix with orthonormal columns. When mode = 'complete' the result is an orthogonal/unitary matrix depending on whether or not a is real/complex. The determinant may be either +/- 1 in that case. In case the number of dimensions in the input array is greater than 2 then a stack of the matrices with above properties is returned.

r : ndarray of float or complex, optional

The upper-triangular matrix or a stack of upper-triangular matrices if the number of dimensions in the input array is greater than 2.

(h, tau) : ndarrays of np.double or np.cdouble, optional

The array h contains the Householder reflectors that generate q along with r. The tau array contains scaling factors for the reflectors. In the deprecated 'economic' mode only h is returned.

Compute the qr factorization of a matrix.

See Also

scipy.linalg.qr

Similar function in SciPy.

scipy.linalg.rq

Compute RQ decomposition of a matrix.

Examples

>>> a = np.random.randn(9, 6)
... q, r = np.linalg.qr(a)
... np.allclose(a, np.dot(q, r)) # a does equal qr True
>>> r2 = np.linalg.qr(a, mode='r')
... np.allclose(r, r2) # mode='r' returns the same r as mode='full' True
>>> a = np.random.normal(size=(3, 2, 2)) # Stack of 2 x 2 matrices as input
... q, r = np.linalg.qr(a)
... q.shape (3, 2, 2)
>>> r.shape
(3, 2, 2)
>>> np.allclose(a, np.matmul(q, r))
True

Example illustrating a common use of qr : solving of least squares problems

A = array([[0, 1], [1, 1], [1, 1], [2, 1]]) x = array([[y0], [m]]) b = array([[1], [0], [2], [1]])

If A = qr such that q is orthonormal (which is always possible via Gram-Schmidt), then x = inv(r) * (q.T) * b . (In numpy practice, however, we simply use lstsq .)

>>> A = np.array([[0, 1], [1, 1], [1, 1], [2, 1]])
... A array([[0, 1], [1, 1], [1, 1], [2, 1]])
>>> b = np.array([1, 0, 2, 1])
... q, r = np.linalg.qr(A)
... p = np.dot(q.T, b)
... np.dot(np.linalg.inv(r), p) array([ 1.1e-16, 1.0e+00])
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

numpy.linalg.qr dask.array.linalg.qr

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /numpy/linalg/linalg.py#773
type: <class 'function'>
Commit: