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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
457
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
457
|
26 #endif |
|
27 |
|
28 #include "CmplxQR.h" |
1847
|
29 #include "f77-fcn.h" |
457
|
30 #include "lo-error.h" |
|
31 |
|
32 extern "C" |
|
33 { |
4552
|
34 F77_RET_T |
5275
|
35 F77_FUNC (zgeqrf, ZGEQRF) (const octave_idx_type&, const octave_idx_type&, Complex*, |
|
36 const octave_idx_type&, Complex*, Complex*, |
|
37 const octave_idx_type&, octave_idx_type&); |
457
|
38 |
4552
|
39 F77_RET_T |
5275
|
40 F77_FUNC (zungqr, ZUNGQR) (const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, |
|
41 Complex*, const octave_idx_type&, Complex*, |
|
42 Complex*, const octave_idx_type&, octave_idx_type&); |
457
|
43 } |
|
44 |
539
|
45 ComplexQR::ComplexQR (const ComplexMatrix& a, QR::type qr_type) |
2763
|
46 : q (), r () |
|
47 { |
|
48 init (a, qr_type); |
|
49 } |
|
50 |
|
51 void |
|
52 ComplexQR::init (const ComplexMatrix& a, QR::type qr_type) |
457
|
53 { |
5275
|
54 octave_idx_type m = a.rows (); |
|
55 octave_idx_type n = a.cols (); |
457
|
56 |
|
57 if (m == 0 || n == 0) |
|
58 { |
|
59 (*current_liboctave_error_handler) |
|
60 ("ComplexQR must have non-empty matrix"); |
|
61 return; |
|
62 } |
|
63 |
5275
|
64 octave_idx_type min_mn = m < n ? m : n; |
1927
|
65 |
|
66 Array<Complex> tau (min_mn); |
|
67 Complex *ptau = tau.fortran_vec (); |
1921
|
68 |
5275
|
69 octave_idx_type lwork = 32*n; |
1927
|
70 Array<Complex> work (lwork); |
|
71 Complex *pwork = work.fortran_vec (); |
1921
|
72 |
5275
|
73 octave_idx_type info = 0; |
457
|
74 |
1927
|
75 ComplexMatrix A_fact; |
3883
|
76 if (m > n && qr_type != QR::economy) |
457
|
77 { |
1927
|
78 A_fact.resize (m, m); |
|
79 A_fact.insert (a, 0, 0); |
457
|
80 } |
|
81 else |
1927
|
82 A_fact = a; |
457
|
83 |
1927
|
84 Complex *tmp_data = A_fact.fortran_vec (); |
457
|
85 |
1927
|
86 F77_XFCN (zgeqrf, ZGEQRF, (m, n, tmp_data, m, ptau, pwork, lwork, info)); |
457
|
87 |
1921
|
88 if (f77_exception_encountered) |
|
89 (*current_liboctave_error_handler) ("unrecoverable error in zgeqrf"); |
539
|
90 else |
457
|
91 { |
1921
|
92 if (qr_type == QR::raw) |
539
|
93 { |
5275
|
94 for (octave_idx_type j = 0; j < min_mn; j++) |
1921
|
95 { |
5275
|
96 octave_idx_type limit = j < min_mn - 1 ? j : min_mn - 1; |
|
97 for (octave_idx_type i = limit + 1; i < m; i++) |
1927
|
98 A_fact.elem (i, j) *= tau.elem (j); |
1921
|
99 } |
3068
|
100 |
3069
|
101 r = A_fact; |
|
102 |
|
103 if (m > n) |
|
104 r.resize (m, n); |
539
|
105 } |
|
106 else |
|
107 { |
5275
|
108 octave_idx_type n2 = (qr_type == QR::economy) ? min_mn : m; |
457
|
109 |
1921
|
110 if (qr_type == QR::economy && m > n) |
3883
|
111 r.resize (n, n, 0.0); |
1921
|
112 else |
3883
|
113 r.resize (m, n, 0.0); |
1921
|
114 |
5275
|
115 for (octave_idx_type j = 0; j < n; j++) |
1921
|
116 { |
5275
|
117 octave_idx_type limit = j < min_mn-1 ? j : min_mn-1; |
|
118 for (octave_idx_type i = 0; i <= limit; i++) |
1927
|
119 r.elem (i, j) = A_fact.elem (i, j); |
1921
|
120 } |
|
121 |
3883
|
122 lwork = 32 * n2; |
1927
|
123 work.resize (lwork); |
4663
|
124 Complex *pwork2 = work.fortran_vec (); |
457
|
125 |
3883
|
126 F77_XFCN (zungqr, ZUNGQR, (m, n2, min_mn, tmp_data, m, ptau, |
4664
|
127 pwork2, lwork, info)); |
457
|
128 |
1921
|
129 if (f77_exception_encountered) |
|
130 (*current_liboctave_error_handler) |
|
131 ("unrecoverable error in zungqr"); |
|
132 else |
|
133 { |
2244
|
134 q = A_fact; |
1921
|
135 q.resize (m, n2); |
|
136 } |
|
137 } |
|
138 } |
457
|
139 } |
|
140 |
|
141 /* |
|
142 ;;; Local Variables: *** |
|
143 ;;; mode: C++ *** |
|
144 ;;; End: *** |
|
145 */ |