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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
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 |
|
38 DEFUN_DLD (qr, args, nargout, |
|
39 "[Q, R] = qr (X): form Q unitary and R upper triangular such\n\ |
|
40 that Q * R = X\n\ |
|
41 \n\ |
|
42 [Q, R] = qr (X, 0): form the economy decomposition such that if X is\n\ |
|
43 m by n then only the first n columns of Q are\n\ |
|
44 computed.\n\ |
|
45 \n\ |
|
46 [Q, R, P] = qr (X): form QRP factorization of X where\n\ |
|
47 P is a permutation matrix such that\n\ |
|
48 A * P = Q * R\n\ |
|
49 \n\ |
|
50 [Q, R, P] = qr (X, 0): form the economy decomposition with \n\ |
|
51 permutation vector P such that Q * R = X (:, P)\n\ |
|
52 \n\ |
|
53 qr (X) alone returns the output of the LAPACK routine dgeqrf, such\n\ |
|
54 that R = triu (qr (X))") |
|
55 { |
|
56 octave_value_list retval; |
|
57 |
|
58 int nargin = args.length (); |
|
59 |
|
60 if (nargin != 1 && nargin != 2 || nargout > 3) |
|
61 { |
|
62 print_usage ("qr"); |
|
63 return retval; |
|
64 } |
|
65 |
|
66 octave_value arg = args(0); |
|
67 |
|
68 int arg_is_empty = empty_arg ("qr", arg.rows (), arg.columns ()); |
|
69 |
|
70 if (arg_is_empty < 0) |
|
71 return retval; |
|
72 else if (arg_is_empty > 0) |
|
73 return octave_value_list (3, Matrix ()); |
|
74 |
3068
|
75 QR::type type = (nargout == 0 || nargout == 1) ? QR::raw |
2928
|
76 : (nargin == 2 ? QR::economy : QR::std); |
|
77 |
|
78 if (arg.is_real_type ()) |
|
79 { |
|
80 Matrix m = arg.matrix_value (); |
|
81 |
|
82 if (! error_state) |
|
83 { |
3068
|
84 switch (nargout) |
2928
|
85 { |
3068
|
86 case 0: |
|
87 case 1: |
|
88 { |
|
89 QR fact (m, type); |
|
90 retval(0) = fact.Q (); |
|
91 } |
|
92 break; |
|
93 |
|
94 case 2: |
|
95 { |
|
96 QR fact (m, type); |
|
97 retval(1) = fact.R (); |
|
98 retval(0) = fact.Q (); |
|
99 } |
|
100 break; |
|
101 |
|
102 default: |
|
103 { |
|
104 QRP fact (m, type); |
|
105 retval(2) = fact.P (); |
|
106 retval(1) = fact.R (); |
|
107 retval(0) = fact.Q (); |
|
108 } |
|
109 break; |
2928
|
110 } |
|
111 } |
|
112 } |
|
113 else if (arg.is_complex_type ()) |
|
114 { |
|
115 ComplexMatrix m = arg.complex_matrix_value (); |
|
116 |
|
117 if (! error_state) |
|
118 { |
3068
|
119 switch (nargout) |
2928
|
120 { |
3068
|
121 case 0: |
|
122 case 1: |
|
123 { |
|
124 ComplexQR fact (m, type); |
|
125 retval(0) = fact.Q (); |
|
126 } |
|
127 break; |
|
128 |
|
129 case 2: |
|
130 { |
|
131 ComplexQR fact (m, type); |
|
132 retval(1) = fact.R (); |
|
133 retval(0) = fact.Q (); |
|
134 } |
|
135 break; |
|
136 |
|
137 default: |
|
138 { |
|
139 ComplexQRP fact (m, type); |
|
140 retval(2) = fact.P (); |
|
141 retval(1) = fact.R (); |
|
142 retval(0) = fact.Q (); |
|
143 } |
|
144 break; |
2928
|
145 } |
|
146 } |
|
147 } |
|
148 else |
|
149 { |
|
150 gripe_wrong_type_arg ("qr", arg); |
|
151 } |
|
152 |
|
153 return retval; |
|
154 } |
|
155 |
|
156 /* |
|
157 ;;; Local Variables: *** |
|
158 ;;; mode: C++ *** |
|
159 ;;; End: *** |
|
160 */ |