538
|
1 /* |
|
2 |
1882
|
3 Copyright (C) 1996 John W. Eaton |
538
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
538
|
20 |
|
21 */ |
|
22 |
1296
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
538
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
538
|
29 #endif |
|
30 |
1367
|
31 #include <cassert> |
538
|
32 |
|
33 #include "CmplxQRP.h" |
1847
|
34 #include "f77-fcn.h" |
538
|
35 #include "lo-error.h" |
1367
|
36 #include "mx-inlines.cc" |
538
|
37 |
|
38 extern "C" |
|
39 { |
1253
|
40 int F77_FCN (zgeqpf, ZGEQPF) (const int&, const int&, Complex*, |
|
41 const int&, int*, Complex*, Complex*, |
|
42 double*, int&); |
567
|
43 |
1253
|
44 int F77_FCN (zungqr, ZUNGQR) (const int&, const int&, const int&, |
|
45 Complex*, const int&, Complex*, |
|
46 Complex*, const int&, int&); |
538
|
47 } |
|
48 |
|
49 // It would be best to share some of this code with ComplexQR class... |
|
50 |
|
51 ComplexQRP::ComplexQRP (const ComplexMatrix& a, QR::type qr_type) |
|
52 { |
|
53 assert (qr_type != QR::raw); |
|
54 |
|
55 int m = a.rows (); |
|
56 int n = a.cols (); |
|
57 |
|
58 if (m == 0 || n == 0) |
|
59 { |
|
60 (*current_liboctave_error_handler) |
|
61 ("ComplexQR must have non-empty matrix"); |
|
62 return; |
|
63 } |
|
64 |
|
65 int min_mn = m < n ? m : n; |
1928
|
66 Array<Complex> tau (min_mn); |
|
67 Complex *ptau = tau.fortran_vec (); |
1922
|
68 |
538
|
69 int lwork = n; |
1928
|
70 Array<Complex> work (lwork); |
|
71 Complex *pwork = work.fortran_vec (); |
1922
|
72 |
538
|
73 int info = 0; |
|
74 |
1928
|
75 ComplexMatrix A_fact; |
538
|
76 if (m > n) |
|
77 { |
1928
|
78 A_fact.resize (m, m); |
|
79 A_fact.insert (a, 0, 0); |
538
|
80 } |
|
81 else |
1928
|
82 A_fact = a; |
538
|
83 |
1928
|
84 Complex *tmp_data = A_fact.fortran_vec (); |
538
|
85 |
1928
|
86 Array<double> rwork (2*n); |
|
87 double *prwork = rwork.fortran_vec (); |
538
|
88 |
1928
|
89 Array<int> jpvt (n, 0); |
|
90 int *pjpvt = jpvt.fortran_vec (); |
538
|
91 |
1928
|
92 // Code to enforce a certain permutation could go here... |
538
|
93 |
1928
|
94 F77_XFCN (zgeqpf, ZGEQPF, (m, n, tmp_data, m, pjpvt, ptau, pwork, |
|
95 prwork, info)); |
1922
|
96 |
|
97 if (f77_exception_encountered) |
|
98 (*current_liboctave_error_handler) ("unrecoverable error in zgeqpf"); |
538
|
99 else |
|
100 { |
1922
|
101 // Form Permutation matrix (if economy is requested, return the |
|
102 // indices only!) |
|
103 |
|
104 if (qr_type == QR::economy && m > n) |
|
105 { |
|
106 p.resize (1, n, 0.0); |
|
107 for (int j = 0; j < n; j++) |
1928
|
108 p.elem (0, j) = jpvt.elem (j); |
1922
|
109 } |
|
110 else |
|
111 { |
|
112 p.resize (n, n, 0.0); |
|
113 for (int j = 0; j < n; j++) |
1928
|
114 p.elem (jpvt.elem (j) - 1, j) = 1.0; |
1922
|
115 } |
|
116 |
|
117 volatile int n2; |
|
118 |
|
119 if (qr_type == QR::economy && m > n) |
|
120 { |
|
121 n2 = n; |
|
122 r.resize (n, n, 0.0); |
|
123 } |
|
124 else |
|
125 { |
|
126 n2 = m; |
|
127 r.resize (m, n, 0.0); |
|
128 } |
|
129 |
538
|
130 for (int j = 0; j < n; j++) |
1922
|
131 { |
|
132 int limit = j < min_mn-1 ? j : min_mn-1; |
|
133 for (int i = 0; i <= limit; i++) |
1928
|
134 r.elem (i, j) = A_fact.elem (i, j); |
1922
|
135 } |
|
136 |
|
137 lwork = 32*m; |
1928
|
138 work.resize (lwork); |
|
139 Complex *pwork = work.fortran_vec (); |
1922
|
140 |
1928
|
141 F77_XFCN (zungqr, ZUNGQR, (m, m, min_mn, tmp_data, m, ptau, |
|
142 pwork, lwork, info)); |
1922
|
143 |
|
144 if (f77_exception_encountered) |
|
145 (*current_liboctave_error_handler) ("unrecoverable error in zungqr"); |
|
146 else |
|
147 { |
2468
|
148 q = A_fact; |
1922
|
149 q.resize (m, n2); |
|
150 } |
538
|
151 } |
|
152 } |
|
153 |
|
154 /* |
|
155 ;;; Local Variables: *** |
|
156 ;;; mode: C++ *** |
|
157 ;;; End: *** |
|
158 */ |