7
|
1 // f-qr.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
453
|
4 Copyright (C) 1993, 1994 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
453
|
28 #include "dbleQR.h" |
|
29 #include "CmplxQR.h" |
1
|
30 |
539
|
31 #include "dbleQRP.h" |
|
32 #include "CmplxQRP.h" |
|
33 |
1
|
34 #include "tree-const.h" |
|
35 #include "user-prefs.h" |
|
36 #include "gripes.h" |
544
|
37 #include "help.h" |
519
|
38 #include "defun-dld.h" |
1
|
39 |
519
|
40 DEFUN_DLD ("qr", Fqr, Sqr, 2, 2, |
539
|
41 "[Q, R] = qr (X): form Q unitary and R upper triangular such\n\ |
|
42 that Q * R = X\n\ |
|
43 \n\ |
|
44 [Q, R] = qr (X, 0): form the economy decomposition such that if X is\n\ |
|
45 if X is m by n then only the first n columns of Q\n\ |
|
46 are computed.\n\ |
|
47 \n\ |
|
48 [Q, R, P] = qr (X): form QRP factorization of X where\n\ |
|
49 P is a permutation matrix such that\n\ |
|
50 A * P = Q * R\n\ |
|
51 \n\ |
|
52 [Q, R, P] = qr (X, 0): form the economy decomposition with \n\ |
|
53 permutation vector P such that Q * R = X (:, P)\n\ |
|
54 \n\ |
|
55 qr (X) alone returns the output of the LAPACK routine dgeqrf, such\n\ |
|
56 that R = triu (qr (X))") |
1
|
57 { |
519
|
58 Octave_object retval; |
|
59 |
|
60 int nargin = args.length (); |
1
|
61 |
539
|
62 if (nargin != 2 && nargin != 3 || nargout > 3) |
519
|
63 { |
|
64 print_usage ("qr"); |
|
65 return retval; |
|
66 } |
1
|
67 |
519
|
68 tree_constant tmp = args(1).make_numeric (); |
1
|
69 |
539
|
70 if (tmp.rows () == 0 || tmp.columns () == 0) |
1
|
71 { |
|
72 int flag = user_pref.propagate_empty_matrices; |
|
73 if (flag != 0) |
|
74 { |
|
75 if (flag < 0) |
|
76 gripe_empty_arg ("qr", 0); |
|
77 Matrix m; |
539
|
78 retval(2) = m; |
519
|
79 retval(1) = m; |
516
|
80 retval(0) = m; |
1
|
81 } |
|
82 else |
|
83 gripe_empty_arg ("qr", 1); |
|
84 |
|
85 return retval; |
|
86 } |
|
87 |
539
|
88 QR::type type = nargout == 1 ? QR::raw |
|
89 : (nargin == 3 ? QR::economy : QR::std); |
|
90 |
1
|
91 switch (tmp.const_type ()) |
|
92 { |
|
93 case tree_constant_rep::matrix_constant: |
|
94 { |
|
95 Matrix m = tmp.matrix_value (); |
539
|
96 if (nargout < 3) |
|
97 { |
|
98 QR fact (m, type); |
|
99 retval(1) = fact.R (); |
|
100 retval(0) = fact.Q (); |
|
101 } |
|
102 else |
|
103 { |
|
104 QRP fact (m, type); |
|
105 retval(2) = fact.P (); |
|
106 retval(1) = fact.R (); |
|
107 retval(0) = fact.Q (); |
|
108 } |
1
|
109 } |
|
110 break; |
|
111 case tree_constant_rep::complex_matrix_constant: |
|
112 { |
|
113 ComplexMatrix m = tmp.complex_matrix_value (); |
539
|
114 if (nargout < 3) |
|
115 { |
|
116 ComplexQR fact (m, type); |
|
117 retval(1) = fact.R (); |
|
118 retval(0) = fact.Q (); |
|
119 } |
|
120 else |
|
121 { |
|
122 ComplexQRP fact (m, type); |
|
123 retval(2) = fact.P (); |
|
124 retval(1) = fact.R (); |
|
125 retval(0) = fact.Q (); |
|
126 } |
1
|
127 } |
|
128 break; |
|
129 case tree_constant_rep::scalar_constant: |
|
130 { |
|
131 double d = tmp.double_value (); |
539
|
132 if (nargout == 1) |
|
133 retval(0) = d; |
|
134 else |
|
135 { |
|
136 retval(2) = 1.0; |
|
137 retval(1) = d; |
|
138 retval(0) = 1.0; |
|
139 } |
1
|
140 } |
|
141 break; |
|
142 case tree_constant_rep::complex_scalar_constant: |
|
143 { |
|
144 Complex c = tmp.complex_value (); |
539
|
145 if (nargout == 1) |
|
146 retval(0) = c; |
|
147 else |
|
148 { |
|
149 retval(2) = 1.0; |
|
150 retval(1) = c; |
|
151 retval(0) = 1.0; |
|
152 } |
1
|
153 } |
|
154 break; |
|
155 default: |
|
156 break; |
|
157 } |
|
158 return retval; |
|
159 } |
|
160 |
|
161 /* |
|
162 ;;; Local Variables: *** |
|
163 ;;; mode: C++ *** |
|
164 ;;; page-delimiter: "^/\\*" *** |
|
165 ;;; End: *** |
|
166 */ |