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