538
|
1 // -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 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 "CmplxQRP.h" |
1367
|
35 #include "f77-uscore.h" |
538
|
36 #include "lo-error.h" |
1367
|
37 #include "mx-inlines.cc" |
538
|
38 |
|
39 extern "C" |
|
40 { |
1253
|
41 int F77_FCN (zgeqpf, ZGEQPF) (const int&, const int&, Complex*, |
|
42 const int&, int*, Complex*, Complex*, |
|
43 double*, int&); |
567
|
44 |
1253
|
45 int F77_FCN (zungqr, ZUNGQR) (const int&, const int&, const int&, |
|
46 Complex*, const int&, Complex*, |
|
47 Complex*, const int&, int&); |
538
|
48 } |
|
49 |
|
50 // It would be best to share some of this code with ComplexQR class... |
|
51 |
|
52 ComplexQRP::ComplexQRP (const ComplexMatrix& a, QR::type qr_type) |
|
53 { |
|
54 assert (qr_type != QR::raw); |
|
55 |
|
56 int m = a.rows (); |
|
57 int n = a.cols (); |
|
58 |
|
59 if (m == 0 || n == 0) |
|
60 { |
|
61 (*current_liboctave_error_handler) |
|
62 ("ComplexQR must have non-empty matrix"); |
|
63 return; |
|
64 } |
|
65 |
|
66 Complex *tmp_data; |
|
67 int min_mn = m < n ? m : n; |
|
68 Complex *tau = new Complex[min_mn]; |
|
69 int lwork = n; |
|
70 Complex *work = new Complex[lwork]; |
|
71 int info = 0; |
|
72 |
|
73 if (m > n) |
|
74 { |
|
75 tmp_data = new Complex [m*m]; |
|
76 copy (tmp_data, a.data (), a.length ()); |
|
77 } |
|
78 else |
|
79 tmp_data = dup (a.data (), a.length ()); |
|
80 |
|
81 |
|
82 work = new Complex[n]; |
|
83 double *rwork = new double[2*n]; |
|
84 int *jpvt = new int[n]; |
|
85 |
1360
|
86 // Clear Pivot vector (code to enforce a certain permutation would |
|
87 // go here...) |
538
|
88 |
|
89 for (int i = 0; i < n; i++) |
|
90 jpvt[i] = 0; |
|
91 |
1253
|
92 F77_FCN (zgeqpf, ZGEQPF) (m, n, tmp_data, m, jpvt, tau, work, rwork, |
|
93 info); |
538
|
94 |
1360
|
95 // Form Permutation matrix (if economy is requested, return the |
|
96 // indices only!) |
538
|
97 |
|
98 if (qr_type == QR::economy && m > n) |
|
99 { |
|
100 p.resize (1, n, 0.0); |
|
101 for (int j = 0; j < n; j++) |
|
102 p.elem (0, j) = jpvt[j]; |
|
103 } |
|
104 else |
|
105 { |
|
106 p.resize (n, n, 0.0); |
|
107 for (int j = 0; j < n; j++) |
|
108 p.elem (jpvt[j]-1, j) = 1.0; |
|
109 } |
|
110 |
|
111 delete [] jpvt; |
|
112 delete [] work; |
|
113 |
1011
|
114 int n2; |
538
|
115 if (qr_type == QR::economy && m > n) |
|
116 { |
1011
|
117 n2 = n; |
538
|
118 r.resize (n, n, 0.0); |
|
119 } |
|
120 else |
|
121 { |
1011
|
122 n2 = m; |
538
|
123 r.resize (m, n, 0.0); |
|
124 } |
|
125 |
|
126 for (int j = 0; j < n; j++) |
|
127 { |
|
128 int limit = j < min_mn-1 ? j : min_mn-1; |
|
129 for (int i = 0; i <= limit; i++) |
|
130 r.elem (i, j) = tmp_data[m*j+i]; |
|
131 } |
|
132 |
|
133 lwork = 32*m; |
|
134 work = new Complex[lwork]; |
|
135 |
1253
|
136 F77_FCN (zungqr, ZUNGQR) (m, m, min_mn, tmp_data, m, tau, work, |
|
137 lwork, info); |
538
|
138 |
|
139 q = ComplexMatrix (tmp_data, m, m); |
1011
|
140 q.resize (m, n2); |
538
|
141 |
|
142 delete [] tau; |
|
143 delete [] work; |
|
144 } |
|
145 |
|
146 /* |
|
147 ;;; Local Variables: *** |
|
148 ;;; mode: C++ *** |
|
149 ;;; page-delimiter: "^/\\*" *** |
|
150 ;;; End: *** |
|
151 */ |