2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
2928
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
2928
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include "CmplxQR.h" |
|
28 #include "CmplxQRP.h" |
|
29 #include "dbleQR.h" |
|
30 #include "dbleQRP.h" |
|
31 |
|
32 #include "defun-dld.h" |
|
33 #include "error.h" |
|
34 #include "gripes.h" |
|
35 #include "oct-obj.h" |
|
36 #include "utils.h" |
|
37 |
3372
|
38 // [Q, R] = qr (X): form Q unitary and R upper triangular such |
|
39 // that Q * R = X |
|
40 // |
|
41 // [Q, R] = qr (X, 0): form the economy decomposition such that if X is |
|
42 // m by n then only the first n columns of Q are |
|
43 // computed. |
|
44 // |
|
45 // [Q, R, P] = qr (X): form QRP factorization of X where |
|
46 // P is a permutation matrix such that |
|
47 // A * P = Q * R |
|
48 // |
|
49 // [Q, R, P] = qr (X, 0): form the economy decomposition with |
|
50 // permutation vector P such that Q * R = X (:, P) |
|
51 // |
|
52 // qr (X) alone returns the output of the LAPACK routine dgeqrf, such |
|
53 // that R = triu (qr (X)) |
|
54 |
2928
|
55 DEFUN_DLD (qr, args, nargout, |
3548
|
56 "-*- texinfo -*-\n\ |
3372
|
57 @deftypefn {Loadable Function} {[@var{q}, @var{r}, @var{p}] =} qr (@var{a})\n\ |
|
58 @cindex QR factorization\n\ |
|
59 Compute the QR factorization of @var{a}, using standard @sc{Lapack}\n\ |
|
60 subroutines. For example, given the matrix @code{a = [1, 2; 3, 4]},\n\ |
2928
|
61 \n\ |
3372
|
62 @example\n\ |
|
63 [q, r] = qr (a)\n\ |
|
64 @end example\n\ |
|
65 \n\ |
|
66 @noindent\n\ |
|
67 returns\n\ |
|
68 \n\ |
|
69 @example\n\ |
|
70 q =\n\ |
|
71 \n\ |
|
72 -0.31623 -0.94868\n\ |
|
73 -0.94868 0.31623\n\ |
|
74 \n\ |
|
75 r =\n\ |
|
76 \n\ |
|
77 -3.16228 -4.42719\n\ |
|
78 0.00000 -0.63246\n\ |
|
79 @end example\n\ |
|
80 \n\ |
|
81 The @code{qr} factorization has applications in the solution of least\n\ |
|
82 squares problems\n\ |
|
83 @iftex\n\ |
|
84 @tex\n\ |
|
85 $$\n\ |
|
86 \\min_x \\left\\Vert A x - b \\right\\Vert_2\n\ |
|
87 $$\n\ |
|
88 @end tex\n\ |
|
89 @end iftex\n\ |
|
90 @ifinfo\n\ |
2928
|
91 \n\ |
3372
|
92 @example\n\ |
|
93 @code{min norm(A x - b)}\n\ |
|
94 @end example\n\ |
|
95 \n\ |
|
96 @end ifinfo\n\ |
|
97 for overdetermined systems of equations (i.e.,\n\ |
|
98 @iftex\n\ |
|
99 @tex\n\ |
|
100 $A$\n\ |
|
101 @end tex\n\ |
|
102 @end iftex\n\ |
|
103 @ifinfo\n\ |
|
104 @code{a}\n\ |
|
105 @end ifinfo\n\ |
|
106 is a tall, thin matrix). The QR factorization is\n\ |
|
107 @iftex\n\ |
|
108 @tex\n\ |
|
109 $QR = A$ where $Q$ is an orthogonal matrix and $R$ is upper triangular.\n\ |
|
110 @end tex\n\ |
|
111 @end iftex\n\ |
|
112 @ifinfo\n\ |
|
113 @code{q * r = a} where @code{q} is an orthogonal matrix and @code{r} is\n\ |
|
114 upper triangular.\n\ |
|
115 @end ifinfo\n\ |
2928
|
116 \n\ |
3372
|
117 The permuted QR factorization @code{[@var{q}, @var{r}, @var{p}] =\n\ |
|
118 qr (@var{a})} forms the QR factorization such that the diagonal\n\ |
|
119 entries of @code{r} are decreasing in magnitude order. For example,\n\ |
|
120 given the matrix @code{a = [1, 2; 3, 4]},\n\ |
|
121 \n\ |
|
122 @example\n\ |
3600
|
123 [q, r, p] = qr(a)\n\ |
3372
|
124 @end example\n\ |
|
125 \n\ |
|
126 @noindent\n\ |
|
127 returns\n\ |
|
128 \n\ |
|
129 @example\n\ |
|
130 q = \n\ |
2928
|
131 \n\ |
3372
|
132 -0.44721 -0.89443\n\ |
|
133 -0.89443 0.44721\n\ |
|
134 \n\ |
|
135 r =\n\ |
|
136 \n\ |
|
137 -4.47214 -3.13050\n\ |
|
138 0.00000 0.44721\n\ |
|
139 \n\ |
|
140 p =\n\ |
|
141 \n\ |
|
142 0 1\n\ |
|
143 1 0\n\ |
|
144 @end example\n\ |
|
145 \n\ |
|
146 The permuted @code{qr} factorization @code{[q, r, p] = qr (a)}\n\ |
|
147 factorization allows the construction of an orthogonal basis of\n\ |
|
148 @code{span (a)}.\n\ |
|
149 @end deftypefn") |
2928
|
150 { |
|
151 octave_value_list retval; |
|
152 |
|
153 int nargin = args.length (); |
|
154 |
6484
|
155 if (nargin < 1 || nargin > 2 || nargout > 3) |
2928
|
156 { |
5823
|
157 print_usage (); |
2928
|
158 return retval; |
|
159 } |
|
160 |
|
161 octave_value arg = args(0); |
|
162 |
|
163 int arg_is_empty = empty_arg ("qr", arg.rows (), arg.columns ()); |
|
164 |
|
165 if (arg_is_empty < 0) |
|
166 return retval; |
|
167 else if (arg_is_empty > 0) |
|
168 return octave_value_list (3, Matrix ()); |
|
169 |
3068
|
170 QR::type type = (nargout == 0 || nargout == 1) ? QR::raw |
2928
|
171 : (nargin == 2 ? QR::economy : QR::std); |
|
172 |
|
173 if (arg.is_real_type ()) |
|
174 { |
|
175 Matrix m = arg.matrix_value (); |
|
176 |
|
177 if (! error_state) |
|
178 { |
3068
|
179 switch (nargout) |
2928
|
180 { |
3068
|
181 case 0: |
|
182 case 1: |
|
183 { |
|
184 QR fact (m, type); |
3069
|
185 retval(0) = fact.R (); |
3068
|
186 } |
|
187 break; |
|
188 |
|
189 case 2: |
|
190 { |
|
191 QR fact (m, type); |
|
192 retval(1) = fact.R (); |
|
193 retval(0) = fact.Q (); |
|
194 } |
|
195 break; |
|
196 |
|
197 default: |
|
198 { |
|
199 QRP fact (m, type); |
|
200 retval(2) = fact.P (); |
|
201 retval(1) = fact.R (); |
|
202 retval(0) = fact.Q (); |
|
203 } |
|
204 break; |
2928
|
205 } |
|
206 } |
|
207 } |
|
208 else if (arg.is_complex_type ()) |
|
209 { |
|
210 ComplexMatrix m = arg.complex_matrix_value (); |
|
211 |
|
212 if (! error_state) |
|
213 { |
3068
|
214 switch (nargout) |
2928
|
215 { |
3068
|
216 case 0: |
|
217 case 1: |
|
218 { |
|
219 ComplexQR fact (m, type); |
3069
|
220 retval(0) = fact.R (); |
3068
|
221 } |
|
222 break; |
|
223 |
|
224 case 2: |
|
225 { |
|
226 ComplexQR fact (m, type); |
|
227 retval(1) = fact.R (); |
|
228 retval(0) = fact.Q (); |
|
229 } |
|
230 break; |
|
231 |
|
232 default: |
|
233 { |
|
234 ComplexQRP fact (m, type); |
|
235 retval(2) = fact.P (); |
|
236 retval(1) = fact.R (); |
|
237 retval(0) = fact.Q (); |
|
238 } |
|
239 break; |
2928
|
240 } |
|
241 } |
|
242 } |
|
243 else |
|
244 { |
|
245 gripe_wrong_type_arg ("qr", arg); |
|
246 } |
|
247 |
|
248 return retval; |
|
249 } |
|
250 |
|
251 /* |
|
252 ;;; Local Variables: *** |
|
253 ;;; mode: C++ *** |
|
254 ;;; End: *** |
|
255 */ |