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) |
457
|
48 { |
|
49 int m = a.rows (); |
|
50 int n = a.cols (); |
|
51 |
|
52 if (m == 0 || n == 0) |
|
53 { |
|
54 (*current_liboctave_error_handler) |
|
55 ("ComplexQR must have non-empty matrix"); |
|
56 return; |
|
57 } |
|
58 |
|
59 int min_mn = m < n ? m : n; |
1927
|
60 |
|
61 Array<Complex> tau (min_mn); |
|
62 Complex *ptau = tau.fortran_vec (); |
1921
|
63 |
457
|
64 int lwork = 32*n; |
1927
|
65 Array<Complex> work (lwork); |
|
66 Complex *pwork = work.fortran_vec (); |
1921
|
67 |
457
|
68 int info = 0; |
|
69 |
1927
|
70 ComplexMatrix A_fact; |
457
|
71 if (m > n) |
|
72 { |
1927
|
73 A_fact.resize (m, m); |
|
74 A_fact.insert (a, 0, 0); |
457
|
75 } |
|
76 else |
1927
|
77 A_fact = a; |
457
|
78 |
1927
|
79 Complex *tmp_data = A_fact.fortran_vec (); |
457
|
80 |
1927
|
81 F77_XFCN (zgeqrf, ZGEQRF, (m, n, tmp_data, m, ptau, pwork, lwork, info)); |
457
|
82 |
1921
|
83 if (f77_exception_encountered) |
|
84 (*current_liboctave_error_handler) ("unrecoverable error in zgeqrf"); |
539
|
85 else |
457
|
86 { |
1921
|
87 if (qr_type == QR::raw) |
539
|
88 { |
1921
|
89 for (int j = 0; j < min_mn; j++) |
|
90 { |
|
91 int limit = j < min_mn - 1 ? j : min_mn - 1; |
|
92 for (int i = limit + 1; i < m; i++) |
1927
|
93 A_fact.elem (i, j) *= tau.elem (j); |
1921
|
94 } |
539
|
95 } |
|
96 else |
|
97 { |
1921
|
98 volatile int n2; |
457
|
99 |
1921
|
100 if (qr_type == QR::economy && m > n) |
|
101 { |
|
102 n2 = n; |
|
103 r.resize (n, n, 0.0); |
|
104 } |
|
105 else |
|
106 { |
|
107 n2 = m; |
|
108 r.resize (m, n, 0.0); |
|
109 } |
|
110 |
|
111 for (int j = 0; j < n; j++) |
|
112 { |
|
113 int limit = j < min_mn-1 ? j : min_mn-1; |
|
114 for (int i = 0; i <= limit; i++) |
1927
|
115 r.elem (i, j) = A_fact.elem (i, j); |
1921
|
116 } |
|
117 |
|
118 lwork = 32*m; |
1927
|
119 work.resize (lwork); |
|
120 Complex *pwork = work.fortran_vec (); |
457
|
121 |
1927
|
122 F77_XFCN (zungqr, ZUNGQR, (m, m, min_mn, tmp_data, m, ptau, |
|
123 pwork, lwork, info)); |
457
|
124 |
1921
|
125 if (f77_exception_encountered) |
|
126 (*current_liboctave_error_handler) |
|
127 ("unrecoverable error in zungqr"); |
|
128 else |
|
129 { |
2244
|
130 q = A_fact; |
1921
|
131 q.resize (m, n2); |
|
132 } |
|
133 } |
|
134 } |
457
|
135 } |
|
136 |
|
137 /* |
|
138 ;;; Local Variables: *** |
|
139 ;;; mode: C++ *** |
|
140 ;;; End: *** |
|
141 */ |