538
|
1 // -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
538
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
538
|
26 #endif |
|
27 |
|
28 #include <assert.h> |
|
29 |
|
30 #include "CmplxQRP.h" |
|
31 #include "mx-inlines.cc" |
|
32 #include "lo-error.h" |
|
33 #include "f77-uscore.h" |
|
34 |
|
35 extern "C" |
|
36 { |
|
37 int F77_FCN (zgeqpf) (const int*, const int*, Complex*, const int*, |
|
38 int*, Complex*, Complex*, double*, int*); |
567
|
39 |
|
40 int F77_FCN (zungqr) (const int*, const int*, const int*, Complex*, |
|
41 const int*, Complex*, Complex*, const int*, int*); |
538
|
42 } |
|
43 |
|
44 // It would be best to share some of this code with ComplexQR class... |
|
45 |
|
46 ComplexQRP::ComplexQRP (const ComplexMatrix& a, QR::type qr_type) |
|
47 { |
|
48 assert (qr_type != QR::raw); |
|
49 |
|
50 int m = a.rows (); |
|
51 int n = a.cols (); |
|
52 |
|
53 if (m == 0 || n == 0) |
|
54 { |
|
55 (*current_liboctave_error_handler) |
|
56 ("ComplexQR must have non-empty matrix"); |
|
57 return; |
|
58 } |
|
59 |
|
60 Complex *tmp_data; |
|
61 int min_mn = m < n ? m : n; |
|
62 Complex *tau = new Complex[min_mn]; |
|
63 int lwork = n; |
|
64 Complex *work = new Complex[lwork]; |
|
65 int info = 0; |
|
66 |
|
67 if (m > n) |
|
68 { |
|
69 tmp_data = new Complex [m*m]; |
|
70 copy (tmp_data, a.data (), a.length ()); |
|
71 } |
|
72 else |
|
73 tmp_data = dup (a.data (), a.length ()); |
|
74 |
|
75 |
|
76 work = new Complex[n]; |
|
77 double *rwork = new double[2*n]; |
|
78 int *jpvt = new int[n]; |
|
79 |
|
80 // Clear Pivot vector (code to enforce a certain permutation would go |
|
81 // here...) |
|
82 |
|
83 for (int i = 0; i < n; i++) |
|
84 jpvt[i] = 0; |
|
85 |
|
86 F77_FCN (zgeqpf) (&m, &n, tmp_data, &m, jpvt, tau, work, rwork, &info); |
|
87 |
|
88 // Form Permutation matrix (if economy is requested, return the |
|
89 // indices only!) |
|
90 |
|
91 if (qr_type == QR::economy && m > n) |
|
92 { |
|
93 p.resize (1, n, 0.0); |
|
94 for (int j = 0; j < n; j++) |
|
95 p.elem (0, j) = jpvt[j]; |
|
96 } |
|
97 else |
|
98 { |
|
99 p.resize (n, n, 0.0); |
|
100 for (int j = 0; j < n; j++) |
|
101 p.elem (jpvt[j]-1, j) = 1.0; |
|
102 } |
|
103 |
|
104 delete [] jpvt; |
|
105 delete [] work; |
|
106 |
1011
|
107 int n2; |
538
|
108 if (qr_type == QR::economy && m > n) |
|
109 { |
1011
|
110 n2 = n; |
538
|
111 r.resize (n, n, 0.0); |
|
112 } |
|
113 else |
|
114 { |
1011
|
115 n2 = m; |
538
|
116 r.resize (m, n, 0.0); |
|
117 } |
|
118 |
|
119 for (int j = 0; j < n; j++) |
|
120 { |
|
121 int limit = j < min_mn-1 ? j : min_mn-1; |
|
122 for (int i = 0; i <= limit; i++) |
|
123 r.elem (i, j) = tmp_data[m*j+i]; |
|
124 } |
|
125 |
|
126 lwork = 32*m; |
|
127 work = new Complex[lwork]; |
|
128 |
|
129 F77_FCN (zungqr) (&m, &m, &min_mn, tmp_data, &m, tau, work, |
|
130 &lwork, &info); |
|
131 |
|
132 q = ComplexMatrix (tmp_data, m, m); |
1011
|
133 q.resize (m, n2); |
538
|
134 |
|
135 delete [] tau; |
|
136 delete [] work; |
|
137 } |
|
138 |
|
139 /* |
|
140 ;;; Local Variables: *** |
|
141 ;;; mode: C++ *** |
|
142 ;;; page-delimiter: "^/\\*" *** |
|
143 ;;; End: *** |
|
144 */ |