This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
qr.py
151 lines (125 loc) · 4.58 KB
/
qr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python
import numpy
from scipy.linalg.decomp import asarray_chkfinite, _datanotshared, \
get_lapack_funcs, basic, find_best_lapack_type
import cvxopt
from cvxopt import lapack
def qr(a, overwrite_a=0, lwork=None, mode='qr', pivoting = False):
"""Compute QR decomposition of a matrix.
Calculate the decomposition :lm:`A = Q R` where Q is unitary/orthogonal
and R upper triangular.
Parameters
----------
a : array, shape (M, N)
Matrix to be decomposed
overwrite_a : boolean
Whether data in a is overwritten (may improve performance)
lwork : integer
Work array size, lwork >= a.shape[1]. If None or -1, an optimal size
is computed.
mode : {'qr', 'r', 'economic'}
Determines what information is to be returned: either both Q and R
or only R, or Q and R and P, a permutation matrix. Any of these can
be combined with 'economic' using the '+' sign as a separator.
Economic mode means the following:
Compute the economy-size QR decomposition, making shapes
of Q and R (M, K) and (K, N) instead of (M,M) and (M,N).
K=min(M,N).
pivoting : bool, optional
Whether or not factorization should include pivoting for rank-revealing
qr decomposition. If pivoting, compute the decomposition
:lm:`A P = Q R` as above, but where P is chosen such that the diagonal
of R is non-increasing. P represents the new column order of A.
Returns
-------
(if mode == 'qr')
Q : double or complex array, shape (M, M) or (M, K) for econ==True
(for any mode)
R : double or complex array, shape (M, N) or (K, N) for econ==True
Size K = min(M, N)
P : integer ndarray
Of shape (N,) for ``pivoting=True``.
Not returned if ``pivoting=False``.
Raises LinAlgError if decomposition fails
Notes
-----
This is an interface to the LAPACK routines dgeqrf, zgeqrf,
dorgqr, and zungqr.
Examples
--------
>>> from scipy import random, linalg, dot
>>> a = random.randn(9, 6)
>>> q, r = linalg.qr(a)
>>> allclose(a, dot(q, r))
True
>>> q.shape, r.shape
((9, 9), (9, 6))
>>> r2 = linalg.qr(a, mode='r')
>>> allclose(r, r2)
>>> q3, r3 = linalg.qr(a, mode='economic')
>>> q3.shape, r3.shape
((9, 6), (6, 6))
"""
mode = mode.split("+")
if "economic" in mode:
econ = True
else:
econ = False
a1 = asarray_chkfinite(a)
if len(a1.shape) != 2:
raise ValueError("expected 2D array")
M, N = a1.shape
overwrite_a = overwrite_a or (_datanotshared(a1,a))
if pivoting:
qr = cvxopt.matrix(0, a1.shape, tc = 'd')
qr[:, :] = a1
tau = cvxopt.matrix(0, (min(M, N), 1), tc = 'd')
jpvt = cvxopt.matrix(0, (N, 1), tc = 'i')
lapack.geqp3(qr, jpvt, tau)
qr = numpy.asarray(qr)
tau = numpy.asarray(tau)
jpvt = (numpy.asarray(jpvt) - 1).ravel()
else:
geqrf, = get_lapack_funcs(('geqrf',),(a1,))
if lwork is None or lwork == -1:
# get optimal work array
qr,tau,work,info = geqrf(a1,lwork=-1,overwrite_a=1)
lwork = work[0]
qr,tau,work,info = geqrf(a1,lwork=lwork,overwrite_a=overwrite_a)
if info<0:
raise ValueError("illegal value in %d-th argument of internal geqrf"
% -info)
if not econ or M<N:
R = basic.triu(qr)
else:
R = basic.triu(qr[0:N,0:N])
if 'r' in mode:
return R
if find_best_lapack_type((a1,))[0]=='s' or find_best_lapack_type((a1,))[0]=='d':
gor_un_gqr, = get_lapack_funcs(('orgqr',),(qr,))
else:
gor_un_gqr, = get_lapack_funcs(('ungqr',),(qr,))
if M<N:
# get optimal work array
Q,work,info = gor_un_gqr(qr[:,0:M],tau,lwork=-1,overwrite_a=1)
lwork = work[0]
Q,work,info = gor_un_gqr(qr[:,0:M],tau,lwork=lwork,overwrite_a=1)
elif econ:
# get optimal work array
Q,work,info = gor_un_gqr(qr,tau,lwork=-1,overwrite_a=1)
lwork = work[0]
Q,work,info = gor_un_gqr(qr,tau,lwork=lwork,overwrite_a=1)
else:
t = qr.dtype.char
qqr = numpy.empty((M,M),dtype=t)
qqr[:,0:N]=qr
# get optimal work array
Q,work,info = gor_un_gqr(qqr,tau,lwork=-1,overwrite_a=1)
lwork = work[0]
Q,work,info = gor_un_gqr(qqr,tau,lwork=lwork,overwrite_a=1)
if info < 0:
raise ValueError("illegal value in %-th argument of internal gorgqr"
% -info)
if pivoting:
return Q, R, jpvt
return Q, R