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