538
|
1 // -*- C++ -*- |
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 John W. Eaton |
538
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
538
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
538
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
538
|
30 #endif |
|
31 |
1367
|
32 #include <cassert> |
538
|
33 |
|
34 #include "dbleQRP.h" |
1847
|
35 #include "f77-fcn.h" |
538
|
36 #include "lo-error.h" |
1367
|
37 #include "mx-inlines.cc" |
538
|
38 |
|
39 extern "C" |
|
40 { |
1253
|
41 int F77_FCN (dgeqpf, DGEQPF) (const int&, const int&, double*, |
|
42 const int&, int*, double*, double*, |
|
43 int&); |
567
|
44 |
1253
|
45 int F77_FCN (dorgqr, DORGQR) (const int&, const int&, const int&, |
|
46 double*, const int&, double*, double*, |
|
47 const int&, int&); |
538
|
48 } |
|
49 |
|
50 // It would be best to share some of this code with QR class... |
|
51 |
|
52 QRP::QRP (const Matrix& a, QR::type qr_type) |
|
53 { |
|
54 assert (qr_type != QR::raw); |
|
55 |
1922
|
56 tau = 0; |
|
57 work = 0; |
|
58 tmp_data = 0; |
|
59 jpvt = 0; |
|
60 |
538
|
61 int m = a.rows (); |
|
62 int n = a.cols (); |
|
63 |
|
64 if (m == 0 || n == 0) |
|
65 { |
|
66 (*current_liboctave_error_handler) ("QR must have non-empty matrix"); |
|
67 return; |
|
68 } |
|
69 |
|
70 int min_mn = m < n ? m : n; |
1922
|
71 tau = new double[min_mn]; |
|
72 |
538
|
73 int lwork = 3*n; |
1922
|
74 work = new double[lwork]; |
|
75 |
538
|
76 int info = 0; |
|
77 |
|
78 if (m > n) |
|
79 { |
|
80 tmp_data = new double [m*m]; |
|
81 copy (tmp_data, a.data (), a.length ()); |
|
82 } |
|
83 else |
|
84 tmp_data = dup (a.data (), a.length ()); |
|
85 |
1922
|
86 jpvt = new int[n]; |
538
|
87 |
1360
|
88 // Clear Pivot vector (code to enforce a certain permutation would |
|
89 // go here...) |
538
|
90 |
|
91 for (int i = 0; i < n; i++) |
|
92 jpvt[i] = 0; |
|
93 |
1922
|
94 F77_XFCN (dgeqpf, DGEQPF, (m, n, tmp_data, m, jpvt, tau, work, info)); |
538
|
95 |
1922
|
96 delete [] work; |
|
97 work = 0; |
|
98 |
|
99 if (f77_exception_encountered) |
|
100 (*current_liboctave_error_handler) ("unrecoverable error in dgeqpf"); |
538
|
101 else |
|
102 { |
1922
|
103 // Form Permutation matrix (if economy is requested, return the |
|
104 // indices only!) |
|
105 |
|
106 if (qr_type == QR::economy && m > n) |
|
107 { |
|
108 p.resize (1, n, 0.0); |
|
109 for (int j = 0; j < n; j++) |
|
110 p.elem (0, j) = jpvt[j]; |
|
111 } |
|
112 else |
|
113 { |
|
114 p.resize (n, n, 0.0); |
|
115 for (int j = 0; j < n; j++) |
|
116 p.elem (jpvt[j]-1, j) = 1.0; |
|
117 } |
|
118 |
|
119 delete [] jpvt; |
|
120 jpvt = 0; |
|
121 |
|
122 volatile int n2; |
|
123 |
|
124 if (qr_type == QR::economy && m > n) |
|
125 { |
|
126 n2 = n; |
|
127 r.resize (n, n, 0.0); |
|
128 } |
|
129 else |
|
130 { |
|
131 n2 = m; |
|
132 r.resize (m, n, 0.0); |
|
133 } |
|
134 |
538
|
135 for (int j = 0; j < n; j++) |
1922
|
136 { |
|
137 int limit = j < min_mn-1 ? j : min_mn-1; |
|
138 for (int i = 0; i <= limit; i++) |
|
139 r.elem (i, j) = tmp_data[m*j+i]; |
|
140 } |
|
141 |
|
142 lwork = 32*m; |
|
143 work = new double[lwork]; |
|
144 |
|
145 F77_XFCN (dorgqr, DORGQR, (m, m, min_mn, tmp_data, m, tau, work, |
|
146 lwork, info)); |
|
147 |
|
148 if (f77_exception_encountered) |
|
149 (*current_liboctave_error_handler) ("unrecoverable error in dorgqr"); |
|
150 else |
|
151 { |
|
152 q = Matrix (tmp_data, m, m); |
|
153 tmp_data = 0; |
|
154 q.resize (m, n2); |
|
155 } |
538
|
156 } |
|
157 |
1922
|
158 delete [] tau; |
|
159 tau = 0; |
|
160 |
|
161 delete [] work; |
|
162 work = 0; |
|
163 |
|
164 delete [] tmp_data; |
|
165 tmp_data = 0; |
|
166 |
538
|
167 delete [] jpvt; |
1922
|
168 jpvt = 0; |
538
|
169 } |
|
170 |
|
171 /* |
|
172 ;;; Local Variables: *** |
|
173 ;;; mode: C++ *** |
|
174 ;;; page-delimiter: "^/\\*" *** |
|
175 ;;; End: *** |
|
176 */ |