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 "CmplxQRP.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 (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 int min_mn = m < n ? m : n; |
1928
|
67 Array<Complex> tau (min_mn); |
|
68 Complex *ptau = tau.fortran_vec (); |
1922
|
69 |
538
|
70 int lwork = n; |
1928
|
71 Array<Complex> work (lwork); |
|
72 Complex *pwork = work.fortran_vec (); |
1922
|
73 |
538
|
74 int info = 0; |
|
75 |
1928
|
76 ComplexMatrix A_fact; |
538
|
77 if (m > n) |
|
78 { |
1928
|
79 A_fact.resize (m, m); |
|
80 A_fact.insert (a, 0, 0); |
538
|
81 } |
|
82 else |
1928
|
83 A_fact = a; |
538
|
84 |
1928
|
85 Complex *tmp_data = A_fact.fortran_vec (); |
538
|
86 |
1928
|
87 Array<double> rwork (2*n); |
|
88 double *prwork = rwork.fortran_vec (); |
538
|
89 |
1928
|
90 Array<int> jpvt (n, 0); |
|
91 int *pjpvt = jpvt.fortran_vec (); |
538
|
92 |
1928
|
93 // Code to enforce a certain permutation could go here... |
538
|
94 |
1928
|
95 F77_XFCN (zgeqpf, ZGEQPF, (m, n, tmp_data, m, pjpvt, ptau, pwork, |
|
96 prwork, info)); |
1922
|
97 |
|
98 if (f77_exception_encountered) |
|
99 (*current_liboctave_error_handler) ("unrecoverable error in zgeqpf"); |
538
|
100 else |
|
101 { |
1922
|
102 // Form Permutation matrix (if economy is requested, return the |
|
103 // indices only!) |
|
104 |
|
105 if (qr_type == QR::economy && m > n) |
|
106 { |
|
107 p.resize (1, n, 0.0); |
|
108 for (int j = 0; j < n; j++) |
1928
|
109 p.elem (0, j) = jpvt.elem (j); |
1922
|
110 } |
|
111 else |
|
112 { |
|
113 p.resize (n, n, 0.0); |
|
114 for (int j = 0; j < n; j++) |
1928
|
115 p.elem (jpvt.elem (j) - 1, j) = 1.0; |
1922
|
116 } |
|
117 |
|
118 volatile int n2; |
|
119 |
|
120 if (qr_type == QR::economy && m > n) |
|
121 { |
|
122 n2 = n; |
|
123 r.resize (n, n, 0.0); |
|
124 } |
|
125 else |
|
126 { |
|
127 n2 = m; |
|
128 r.resize (m, n, 0.0); |
|
129 } |
|
130 |
538
|
131 for (int j = 0; j < n; j++) |
1922
|
132 { |
|
133 int limit = j < min_mn-1 ? j : min_mn-1; |
|
134 for (int i = 0; i <= limit; i++) |
1928
|
135 r.elem (i, j) = A_fact.elem (i, j); |
1922
|
136 } |
|
137 |
|
138 lwork = 32*m; |
1928
|
139 work.resize (lwork); |
|
140 Complex *pwork = work.fortran_vec (); |
1922
|
141 |
1928
|
142 F77_XFCN (zungqr, ZUNGQR, (m, m, min_mn, tmp_data, m, ptau, |
|
143 pwork, lwork, info)); |
1922
|
144 |
|
145 if (f77_exception_encountered) |
|
146 (*current_liboctave_error_handler) ("unrecoverable error in zungqr"); |
|
147 else |
|
148 { |
|
149 q = ComplexMatrix (tmp_data, m, m); |
|
150 q.resize (m, n2); |
|
151 } |
538
|
152 } |
|
153 } |
|
154 |
|
155 /* |
|
156 ;;; Local Variables: *** |
|
157 ;;; mode: C++ *** |
|
158 ;;; page-delimiter: "^/\\*" *** |
|
159 ;;; End: *** |
|
160 */ |