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 |
712
|
63 if (nargin != 1 && nargin != 2 || nargout > 3) |
519
|
64 { |
|
65 print_usage ("qr"); |
|
66 return retval; |
|
67 } |
1
|
68 |
712
|
69 tree_constant arg = args(0); |
718
|
70 |
|
71 int arg_is_empty = empty_arg ("qr", arg.rows (), arg.columns ()); |
|
72 |
|
73 if (arg_is_empty < 0) |
636
|
74 return retval; |
718
|
75 else if (arg_is_empty > 0) |
|
76 return Octave_object (3, Matrix ()); |
1
|
77 |
539
|
78 QR::type type = nargout == 1 ? QR::raw |
712
|
79 : (nargin == 2 ? QR::economy : QR::std); |
539
|
80 |
636
|
81 if (arg.is_real_type ()) |
620
|
82 { |
636
|
83 Matrix m = arg.matrix_value (); |
|
84 |
|
85 if (! error_state) |
620
|
86 { |
636
|
87 if (nargout < 3) |
|
88 { |
|
89 QR fact (m, type); |
|
90 retval(1) = fact.R (); |
|
91 retval(0) = fact.Q (); |
|
92 } |
|
93 else |
|
94 { |
|
95 QRP fact (m, type); |
|
96 retval(2) = fact.P (); |
|
97 retval(1) = fact.R (); |
|
98 retval(0) = fact.Q (); |
|
99 } |
620
|
100 } |
|
101 } |
636
|
102 else if (arg.is_complex_type ()) |
620
|
103 { |
636
|
104 ComplexMatrix m = arg.complex_matrix_value (); |
|
105 |
|
106 if (! error_state) |
620
|
107 { |
636
|
108 if (nargout < 3) |
|
109 { |
|
110 ComplexQR fact (m, type); |
|
111 retval(1) = fact.R (); |
|
112 retval(0) = fact.Q (); |
|
113 } |
|
114 else |
|
115 { |
|
116 ComplexQRP fact (m, type); |
|
117 retval(2) = fact.P (); |
|
118 retval(1) = fact.R (); |
|
119 retval(0) = fact.Q (); |
|
120 } |
620
|
121 } |
|
122 } |
|
123 else |
|
124 { |
636
|
125 gripe_wrong_type_arg ("qr", arg); |
620
|
126 } |
|
127 |
1
|
128 return retval; |
|
129 } |
|
130 |
|
131 /* |
|
132 ;;; Local Variables: *** |
|
133 ;;; mode: C++ *** |
|
134 ;;; page-delimiter: "^/\\*" *** |
|
135 ;;; End: *** |
|
136 */ |