Movatterモバイル変換


[0]ホーム

URL:


SciPy

numpy.linalg.qr

numpy.linalg.qr(a,mode='reduced')[source]

Compute the qr factorization of a matrix.

Factor the matrixa asqr, whereq is orthonormal andr isupper-triangular.

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

Matrix to be factored.

mode:{‘reduced’, ‘complete’, ‘r’, ‘raw’, ‘full’, ‘economic’}, optional

If K = min(M, N), then

  • ‘reduced’ : returns q, r with dimensions (M, K), (K, N) (default)
  • ‘complete’ : returns q, r with dimensions (M, M), (M, N)
  • ‘r’ : returns r only with dimensions (K, N)
  • ‘raw’ : returns h, tau with dimensions (N, M), (K,)
  • ‘full’ : alias of ‘reduced’, deprecated
  • ‘economic’ : returns h from ‘raw’, deprecated.

The options ‘reduced’, ‘complete, and ‘raw’ are new in numpy 1.8,see the notes for more information. The default is ‘reduced’, and tomaintain backward compatibility with earlier versions of numpy bothit and the old default ‘full’ can be omitted. Note that array hreturned in ‘raw’ mode is transposed for calling Fortran. The‘economic’ mode is deprecated. The modes ‘full’ and ‘economic’ maybe passed using only the first letter for backwards compatibility,but all others must be spelled out. See the Notes for moreexplanation.

Returns:
q:ndarray of float or complex, optional

A matrix with orthonormal columns. When mode = ‘complete’ theresult is an orthogonal/unitary matrix depending on whether or nota is real/complex. The determinant may be either +/- 1 in thatcase.

r:ndarray of float or complex, optional

The upper-triangular matrix.

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

The array h contains the Householder reflectors that generate qalong with r. The tau array contains scaling factors for thereflectors. In the deprecated ‘economic’ mode only h is returned.

Raises:
LinAlgError

If factoring fails.

Notes

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

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

Subclasses ofndarray are preserved except for the ‘raw’ mode. So ifa is of typematrix, all the return values will be matrices too.

New ‘reduced’, ‘complete’, and ‘raw’ options for mode were added inNumPy 1.8.0 and the old option ‘full’ was made an alias of ‘reduced’. Inaddition 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 lettingmode default.The ‘raw’ option was added so that LAPACK routines that can multiplyarrays by q using the Householder reflectors can be used. Note that inthis case the returned arrays are of type np.double or np.cdouble andthe h array is transposed to be FORTRAN compatible. No routines usingthe ‘raw’ return are currently exposed by numpy, but some are availablein lapack_lite and just await the necessary work.

Examples

>>>a=np.random.randn(9,6)>>>q,r=np.linalg.qr(a)>>>np.allclose(a,np.dot(q,r))# a does equal qrTrue>>>r2=np.linalg.qr(a,mode='r')>>>r3=np.linalg.qr(a,mode='economic')>>>np.allclose(r,r2)# mode='r' returns the same r as mode='full'True>>># But only triu parts are guaranteed equal when mode='economic'>>>np.allclose(r,np.triu(r3[:6,:6],k=0))True

Example illustrating a common use ofqr: solving of least squaresproblems

What are the least-squares-bestm andy0 iny=y0+mx forthe following data: {(0,1), (1,0), (1,2), (2,1)}. (Graph the pointsand you’ll see that it should be y0 = 0, m = 1.) The answer is providedby solving the over-determined matrix equationAx=b, where:

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 viaGram-Schmidt), thenx=inv(r)*(q.T)*b. (In numpy practice,however, we simply uselstsq.)

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

Previous topic

numpy.linalg.cholesky

Next topic

numpy.linalg.svd

Quick search

  • © Copyright 2008-2018, The SciPy community.
  • Last updated on Jul 24, 2018.
  • Created usingSphinx 1.6.6.

[8]ページ先頭

©2009-2025 Movatter.jp