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" |
519
|
37 #include "defun-dld.h" |
1
|
38 |
519
|
39 DEFUN_DLD ("qr", Fqr, Sqr, 2, 2, |
539
|
40 "[Q, R] = qr (X): form Q unitary and R upper triangular such\n\ |
|
41 that Q * R = X\n\ |
|
42 \n\ |
|
43 [Q, R] = qr (X, 0): form the economy decomposition such that if X is\n\ |
|
44 if X is m by n then only the first n columns of Q\n\ |
|
45 are computed.\n\ |
|
46 \n\ |
|
47 [Q, R, P] = qr (X): form QRP factorization of X where\n\ |
|
48 P is a permutation matrix such that\n\ |
|
49 A * P = Q * R\n\ |
|
50 \n\ |
|
51 [Q, R, P] = qr (X, 0): form the economy decomposition with \n\ |
|
52 permutation vector P such that Q * R = X (:, P)\n\ |
|
53 \n\ |
|
54 qr (X) alone returns the output of the LAPACK routine dgeqrf, such\n\ |
|
55 that R = triu (qr (X))") |
1
|
56 { |
519
|
57 Octave_object retval; |
|
58 |
|
59 int nargin = args.length (); |
1
|
60 |
539
|
61 if (nargin != 2 && nargin != 3 || nargout > 3) |
519
|
62 { |
|
63 print_usage ("qr"); |
|
64 return retval; |
|
65 } |
1
|
66 |
519
|
67 tree_constant tmp = args(1).make_numeric (); |
1
|
68 |
539
|
69 if (tmp.rows () == 0 || tmp.columns () == 0) |
1
|
70 { |
|
71 int flag = user_pref.propagate_empty_matrices; |
|
72 if (flag != 0) |
|
73 { |
|
74 if (flag < 0) |
|
75 gripe_empty_arg ("qr", 0); |
|
76 Matrix m; |
539
|
77 retval(2) = m; |
519
|
78 retval(1) = m; |
516
|
79 retval(0) = m; |
1
|
80 } |
|
81 else |
|
82 gripe_empty_arg ("qr", 1); |
|
83 |
|
84 return retval; |
|
85 } |
|
86 |
539
|
87 QR::type type = nargout == 1 ? QR::raw |
|
88 : (nargin == 3 ? QR::economy : QR::std); |
|
89 |
1
|
90 switch (tmp.const_type ()) |
|
91 { |
|
92 case tree_constant_rep::matrix_constant: |
|
93 { |
|
94 Matrix m = tmp.matrix_value (); |
539
|
95 if (nargout < 3) |
|
96 { |
|
97 QR fact (m, type); |
|
98 retval(1) = fact.R (); |
|
99 retval(0) = fact.Q (); |
|
100 } |
|
101 else |
|
102 { |
|
103 QRP fact (m, type); |
|
104 retval(2) = fact.P (); |
|
105 retval(1) = fact.R (); |
|
106 retval(0) = fact.Q (); |
|
107 } |
1
|
108 } |
|
109 break; |
|
110 case tree_constant_rep::complex_matrix_constant: |
|
111 { |
|
112 ComplexMatrix m = tmp.complex_matrix_value (); |
539
|
113 if (nargout < 3) |
|
114 { |
|
115 ComplexQR fact (m, type); |
|
116 retval(1) = fact.R (); |
|
117 retval(0) = fact.Q (); |
|
118 } |
|
119 else |
|
120 { |
|
121 ComplexQRP fact (m, type); |
|
122 retval(2) = fact.P (); |
|
123 retval(1) = fact.R (); |
|
124 retval(0) = fact.Q (); |
|
125 } |
1
|
126 } |
|
127 break; |
|
128 case tree_constant_rep::scalar_constant: |
|
129 { |
|
130 double d = tmp.double_value (); |
539
|
131 if (nargout == 1) |
|
132 retval(0) = d; |
|
133 else |
|
134 { |
|
135 retval(2) = 1.0; |
|
136 retval(1) = d; |
|
137 retval(0) = 1.0; |
|
138 } |
1
|
139 } |
|
140 break; |
|
141 case tree_constant_rep::complex_scalar_constant: |
|
142 { |
|
143 Complex c = tmp.complex_value (); |
539
|
144 if (nargout == 1) |
|
145 retval(0) = c; |
|
146 else |
|
147 { |
|
148 retval(2) = 1.0; |
|
149 retval(1) = c; |
|
150 retval(0) = 1.0; |
|
151 } |
1
|
152 } |
|
153 break; |
|
154 default: |
|
155 break; |
|
156 } |
|
157 return retval; |
|
158 } |
|
159 |
|
160 /* |
|
161 ;;; Local Variables: *** |
|
162 ;;; mode: C++ *** |
|
163 ;;; page-delimiter: "^/\\*" *** |
|
164 ;;; End: *** |
|
165 */ |