linalg.qr
Description
Compute the QR factorization of a matrix.
Factor the matrix a as q and r matrices, where q is orthonormal and r is upper-triangular.
Mandatory Input Parameters
Parameter |
Type |
Description |
|---|---|---|
a |
array_like, shape (…, M, N) |
An array-like object with the dimensionality of at least 2 |
Optional Input Parameters
Parameter |
Type |
Default Value |
Description |
|---|---|---|---|
mode |
{'reduced', 'complete', 'r', 'raw'} |
'reduce' |
If K = min(M, N), then:
|
Return Value
Parameter |
Type |
Description |
|---|---|---|
q |
ndarray of float or complex |
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, a stack of the matrices with above properties is returned. |
r |
ndarray of float or complex |
An 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 |
The array h contains the Householder reflectors that generate q along with r. The tau array contains scaling factors for the reflectors. |
Examples
>>> import numpy as np
>>> a = np.random.randn(9, 6)
>>> q, r = np.linalg.qr(a)
>>>
>>> np.allclose(a, np.dot(q,r))
True
>>>
>>> r2 = np.linalg.qr(a, mode='r')
>>> np.allclose(r, r2)
True
>>>
>>> a = np.random.normal(size=(3,2,2))
>>> q, r = np.linalg.qr(a)
>>> q.shape
(3, 2, 2)
>>> r.shape
(3, 2, 2)
>>> np.allclose(a, np.matmul(q,r))
True
>>>
>>> # Ax = b. If A = qr, then x = inv(r) * (q.T) * b.
>>>
>>> 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([2.62287285e-16, 1.00000000e+00])
>>>