457
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
457
|
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. |
457
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
24 #pragma implementation |
|
25 #endif |
|
26 |
457
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
457
|
29 #endif |
|
30 |
|
31 #include "CmplxQR.h" |
1847
|
32 #include "f77-fcn.h" |
457
|
33 #include "lo-error.h" |
|
34 |
|
35 extern "C" |
|
36 { |
3887
|
37 int F77_FUNC (zgeqrf, ZGEQRF) (const int&, const int&, Complex*, |
1253
|
38 const int&, Complex*, Complex*, |
|
39 const int&, int&); |
457
|
40 |
3887
|
41 int F77_FUNC (zungqr, ZUNGQR) (const int&, const int&, const int&, |
1253
|
42 Complex*, const int&, Complex*, |
|
43 Complex*, const int&, int&); |
457
|
44 } |
|
45 |
539
|
46 ComplexQR::ComplexQR (const ComplexMatrix& a, QR::type qr_type) |
2763
|
47 : q (), r () |
|
48 { |
|
49 init (a, qr_type); |
|
50 } |
|
51 |
|
52 void |
|
53 ComplexQR::init (const ComplexMatrix& a, QR::type qr_type) |
457
|
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; |
1927
|
66 |
|
67 Array<Complex> tau (min_mn); |
|
68 Complex *ptau = tau.fortran_vec (); |
1921
|
69 |
457
|
70 int lwork = 32*n; |
1927
|
71 Array<Complex> work (lwork); |
|
72 Complex *pwork = work.fortran_vec (); |
1921
|
73 |
457
|
74 int info = 0; |
|
75 |
1927
|
76 ComplexMatrix A_fact; |
3883
|
77 if (m > n && qr_type != QR::economy) |
457
|
78 { |
1927
|
79 A_fact.resize (m, m); |
|
80 A_fact.insert (a, 0, 0); |
457
|
81 } |
|
82 else |
1927
|
83 A_fact = a; |
457
|
84 |
1927
|
85 Complex *tmp_data = A_fact.fortran_vec (); |
457
|
86 |
1927
|
87 F77_XFCN (zgeqrf, ZGEQRF, (m, n, tmp_data, m, ptau, pwork, lwork, info)); |
457
|
88 |
1921
|
89 if (f77_exception_encountered) |
|
90 (*current_liboctave_error_handler) ("unrecoverable error in zgeqrf"); |
539
|
91 else |
457
|
92 { |
1921
|
93 if (qr_type == QR::raw) |
539
|
94 { |
1921
|
95 for (int j = 0; j < min_mn; j++) |
|
96 { |
|
97 int limit = j < min_mn - 1 ? j : min_mn - 1; |
|
98 for (int i = limit + 1; i < m; i++) |
1927
|
99 A_fact.elem (i, j) *= tau.elem (j); |
1921
|
100 } |
3068
|
101 |
3069
|
102 r = A_fact; |
|
103 |
|
104 if (m > n) |
|
105 r.resize (m, n); |
539
|
106 } |
|
107 else |
|
108 { |
3883
|
109 int n2 = (qr_type == QR::economy) ? min_mn : m; |
457
|
110 |
1921
|
111 if (qr_type == QR::economy && m > n) |
3883
|
112 r.resize (n, n, 0.0); |
1921
|
113 else |
3883
|
114 r.resize (m, n, 0.0); |
1921
|
115 |
|
116 for (int j = 0; j < n; j++) |
|
117 { |
|
118 int limit = j < min_mn-1 ? j : min_mn-1; |
|
119 for (int i = 0; i <= limit; i++) |
1927
|
120 r.elem (i, j) = A_fact.elem (i, j); |
1921
|
121 } |
|
122 |
3883
|
123 lwork = 32 * n2; |
1927
|
124 work.resize (lwork); |
|
125 Complex *pwork = work.fortran_vec (); |
457
|
126 |
3883
|
127 F77_XFCN (zungqr, ZUNGQR, (m, n2, min_mn, tmp_data, m, ptau, |
1927
|
128 pwork, lwork, info)); |
457
|
129 |
1921
|
130 if (f77_exception_encountered) |
|
131 (*current_liboctave_error_handler) |
|
132 ("unrecoverable error in zungqr"); |
|
133 else |
|
134 { |
2244
|
135 q = A_fact; |
1921
|
136 q.resize (m, n2); |
|
137 } |
|
138 } |
|
139 } |
457
|
140 } |
|
141 |
|
142 /* |
|
143 ;;; Local Variables: *** |
|
144 ;;; mode: C++ *** |
|
145 ;;; End: *** |
|
146 */ |