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