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" |
636
|
37 #include "utils.h" |
544
|
38 #include "help.h" |
519
|
39 #include "defun-dld.h" |
1
|
40 |
701
|
41 DEFUN_DLD_BUILTIN ("qr", Fqr, Sqr, 2, 2, |
539
|
42 "[Q, R] = qr (X): form Q unitary and R upper triangular such\n\ |
|
43 that Q * R = X\n\ |
|
44 \n\ |
|
45 [Q, R] = qr (X, 0): form the economy decomposition such that if X is\n\ |
|
46 if X is m by n then only the first n columns of Q\n\ |
|
47 are computed.\n\ |
|
48 \n\ |
|
49 [Q, R, P] = qr (X): form QRP factorization of X where\n\ |
|
50 P is a permutation matrix such that\n\ |
|
51 A * P = Q * R\n\ |
|
52 \n\ |
|
53 [Q, R, P] = qr (X, 0): form the economy decomposition with \n\ |
|
54 permutation vector P such that Q * R = X (:, P)\n\ |
|
55 \n\ |
|
56 qr (X) alone returns the output of the LAPACK routine dgeqrf, such\n\ |
|
57 that R = triu (qr (X))") |
1
|
58 { |
519
|
59 Octave_object retval; |
|
60 |
|
61 int nargin = args.length (); |
1
|
62 |
539
|
63 if (nargin != 2 && nargin != 3 || nargout > 3) |
519
|
64 { |
|
65 print_usage ("qr"); |
|
66 return retval; |
|
67 } |
1
|
68 |
636
|
69 tree_constant arg = args(1); |
1
|
70 |
636
|
71 if (empty_arg ("qr", arg.rows (), arg.columns ()) < 0) |
|
72 return retval; |
1
|
73 |
539
|
74 QR::type type = nargout == 1 ? QR::raw |
|
75 : (nargin == 3 ? QR::economy : QR::std); |
|
76 |
636
|
77 if (arg.is_real_type ()) |
620
|
78 { |
636
|
79 Matrix m = arg.matrix_value (); |
|
80 |
|
81 if (! error_state) |
620
|
82 { |
636
|
83 if (nargout < 3) |
|
84 { |
|
85 QR fact (m, type); |
|
86 retval(1) = fact.R (); |
|
87 retval(0) = fact.Q (); |
|
88 } |
|
89 else |
|
90 { |
|
91 QRP fact (m, type); |
|
92 retval(2) = fact.P (); |
|
93 retval(1) = fact.R (); |
|
94 retval(0) = fact.Q (); |
|
95 } |
620
|
96 } |
|
97 } |
636
|
98 else if (arg.is_complex_type ()) |
620
|
99 { |
636
|
100 ComplexMatrix m = arg.complex_matrix_value (); |
|
101 |
|
102 if (! error_state) |
620
|
103 { |
636
|
104 if (nargout < 3) |
|
105 { |
|
106 ComplexQR fact (m, type); |
|
107 retval(1) = fact.R (); |
|
108 retval(0) = fact.Q (); |
|
109 } |
|
110 else |
|
111 { |
|
112 ComplexQRP fact (m, type); |
|
113 retval(2) = fact.P (); |
|
114 retval(1) = fact.R (); |
|
115 retval(0) = fact.Q (); |
|
116 } |
620
|
117 } |
|
118 } |
|
119 else |
|
120 { |
636
|
121 gripe_wrong_type_arg ("qr", arg); |
620
|
122 } |
|
123 |
1
|
124 return retval; |
|
125 } |
|
126 |
|
127 /* |
|
128 ;;; Local Variables: *** |
|
129 ;;; mode: C++ *** |
|
130 ;;; page-delimiter: "^/\\*" *** |
|
131 ;;; End: *** |
|
132 */ |