538
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 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 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
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" |
|
36 |
|
37 extern "C" |
|
38 { |
4552
|
39 F77_RET_T |
|
40 F77_FUNC (zgeqpf, ZGEQPF) (const int&, const int&, Complex*, |
|
41 const int&, int*, Complex*, Complex*, |
|
42 double*, int&); |
567
|
43 |
4552
|
44 F77_RET_T |
|
45 F77_FUNC (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) |
2763
|
53 : ComplexQR (), p () |
|
54 { |
|
55 init (a, qr_type); |
|
56 } |
|
57 |
|
58 void |
|
59 ComplexQRP::init (const ComplexMatrix& a, QR::type qr_type) |
538
|
60 { |
|
61 assert (qr_type != QR::raw); |
|
62 |
|
63 int m = a.rows (); |
|
64 int n = a.cols (); |
|
65 |
|
66 if (m == 0 || n == 0) |
|
67 { |
|
68 (*current_liboctave_error_handler) |
|
69 ("ComplexQR must have non-empty matrix"); |
|
70 return; |
|
71 } |
|
72 |
3883
|
73 int min_mn = m < n ? m : n; |
|
74 Array<Complex> tau (min_mn); |
1928
|
75 Complex *ptau = tau.fortran_vec (); |
1922
|
76 |
2763
|
77 int lwork = 3*n > 32*m ? 3*n : 32*m; |
1928
|
78 Array<Complex> work (lwork); |
|
79 Complex *pwork = work.fortran_vec (); |
1922
|
80 |
538
|
81 int info = 0; |
|
82 |
2763
|
83 ComplexMatrix A_fact = a; |
3883
|
84 if (m > n && qr_type != QR::economy) |
2763
|
85 A_fact.resize (m, m, 0.0); |
538
|
86 |
1928
|
87 Complex *tmp_data = A_fact.fortran_vec (); |
538
|
88 |
1928
|
89 Array<double> rwork (2*n); |
|
90 double *prwork = rwork.fortran_vec (); |
538
|
91 |
1928
|
92 Array<int> jpvt (n, 0); |
|
93 int *pjpvt = jpvt.fortran_vec (); |
538
|
94 |
1928
|
95 // Code to enforce a certain permutation could go here... |
538
|
96 |
1928
|
97 F77_XFCN (zgeqpf, ZGEQPF, (m, n, tmp_data, m, pjpvt, ptau, pwork, |
|
98 prwork, info)); |
1922
|
99 |
|
100 if (f77_exception_encountered) |
|
101 (*current_liboctave_error_handler) ("unrecoverable error in zgeqpf"); |
538
|
102 else |
|
103 { |
1922
|
104 // Form Permutation matrix (if economy is requested, return the |
|
105 // indices only!) |
|
106 |
2763
|
107 if (qr_type == QR::economy) |
1922
|
108 { |
|
109 p.resize (1, n, 0.0); |
|
110 for (int j = 0; j < n; j++) |
1928
|
111 p.elem (0, j) = jpvt.elem (j); |
1922
|
112 } |
|
113 else |
|
114 { |
|
115 p.resize (n, n, 0.0); |
|
116 for (int j = 0; j < n; j++) |
1928
|
117 p.elem (jpvt.elem (j) - 1, j) = 1.0; |
1922
|
118 } |
|
119 |
3883
|
120 int n2 = (qr_type == QR::economy) ? min_mn : m; |
|
121 |
2763
|
122 if (qr_type == QR::economy && m > n) |
|
123 r.resize (n, n, 0.0); |
|
124 else |
|
125 r.resize (m, n, 0.0); |
1922
|
126 |
538
|
127 for (int j = 0; j < n; j++) |
1922
|
128 { |
|
129 int limit = j < min_mn-1 ? j : min_mn-1; |
|
130 for (int i = 0; i <= limit; i++) |
1928
|
131 r.elem (i, j) = A_fact.elem (i, j); |
1922
|
132 } |
|
133 |
2763
|
134 F77_XFCN (zungqr, ZUNGQR, (m, n2, min_mn, tmp_data, m, ptau, |
1928
|
135 pwork, lwork, info)); |
1922
|
136 |
|
137 if (f77_exception_encountered) |
|
138 (*current_liboctave_error_handler) ("unrecoverable error in zungqr"); |
|
139 else |
|
140 { |
2468
|
141 q = A_fact; |
1922
|
142 q.resize (m, n2); |
|
143 } |
538
|
144 } |
|
145 } |
|
146 |
|
147 /* |
|
148 ;;; Local Variables: *** |
|
149 ;;; mode: C++ *** |
|
150 ;;; End: *** |
|
151 */ |