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 |
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" |
|
36 |
|
37 extern "C" |
|
38 { |
3887
|
39 int F77_FUNC (zgeqpf, ZGEQPF) (const int&, const int&, Complex*, |
1253
|
40 const int&, int*, Complex*, Complex*, |
|
41 double*, int&); |
567
|
42 |
3887
|
43 int F77_FUNC (zungqr, ZUNGQR) (const int&, const int&, const int&, |
1253
|
44 Complex*, const int&, Complex*, |
|
45 Complex*, const int&, int&); |
538
|
46 } |
|
47 |
|
48 // It would be best to share some of this code with ComplexQR class... |
|
49 |
|
50 ComplexQRP::ComplexQRP (const ComplexMatrix& a, QR::type qr_type) |
2763
|
51 : ComplexQR (), p () |
|
52 { |
|
53 init (a, qr_type); |
|
54 } |
|
55 |
|
56 void |
|
57 ComplexQRP::init (const ComplexMatrix& a, QR::type qr_type) |
538
|
58 { |
|
59 assert (qr_type != QR::raw); |
|
60 |
|
61 int m = a.rows (); |
|
62 int n = a.cols (); |
|
63 |
|
64 if (m == 0 || n == 0) |
|
65 { |
|
66 (*current_liboctave_error_handler) |
|
67 ("ComplexQR must have non-empty matrix"); |
|
68 return; |
|
69 } |
|
70 |
3883
|
71 int min_mn = m < n ? m : n; |
|
72 Array<Complex> tau (min_mn); |
1928
|
73 Complex *ptau = tau.fortran_vec (); |
1922
|
74 |
2763
|
75 int lwork = 3*n > 32*m ? 3*n : 32*m; |
1928
|
76 Array<Complex> work (lwork); |
|
77 Complex *pwork = work.fortran_vec (); |
1922
|
78 |
538
|
79 int info = 0; |
|
80 |
2763
|
81 ComplexMatrix A_fact = a; |
3883
|
82 if (m > n && qr_type != QR::economy) |
2763
|
83 A_fact.resize (m, m, 0.0); |
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 |
2763
|
105 if (qr_type == QR::economy) |
1922
|
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 |
3883
|
118 int n2 = (qr_type == QR::economy) ? min_mn : m; |
|
119 |
2763
|
120 if (qr_type == QR::economy && m > n) |
|
121 r.resize (n, n, 0.0); |
|
122 else |
|
123 r.resize (m, n, 0.0); |
1922
|
124 |
538
|
125 for (int j = 0; j < n; j++) |
1922
|
126 { |
|
127 int limit = j < min_mn-1 ? j : min_mn-1; |
|
128 for (int i = 0; i <= limit; i++) |
1928
|
129 r.elem (i, j) = A_fact.elem (i, j); |
1922
|
130 } |
|
131 |
2763
|
132 F77_XFCN (zungqr, ZUNGQR, (m, n2, min_mn, tmp_data, m, ptau, |
1928
|
133 pwork, lwork, info)); |
1922
|
134 |
|
135 if (f77_exception_encountered) |
|
136 (*current_liboctave_error_handler) ("unrecoverable error in zungqr"); |
|
137 else |
|
138 { |
2468
|
139 q = A_fact; |
1922
|
140 q.resize (m, n2); |
|
141 } |
538
|
142 } |
|
143 } |
|
144 |
|
145 /* |
|
146 ;;; Local Variables: *** |
|
147 ;;; mode: C++ *** |
|
148 ;;; End: *** |
|
149 */ |