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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
538
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
538
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
538
|
30 #endif |
|
31 |
|
32 #include <assert.h> |
|
33 |
|
34 #include "dbleQRP.h" |
|
35 #include "mx-inlines.cc" |
|
36 #include "lo-error.h" |
|
37 #include "f77-uscore.h" |
|
38 |
|
39 extern "C" |
|
40 { |
1253
|
41 int F77_FCN (dgeqpf, DGEQPF) (const int&, const int&, double*, |
|
42 const int&, int*, double*, double*, |
|
43 int&); |
567
|
44 |
1253
|
45 int F77_FCN (dorgqr, DORGQR) (const int&, const int&, const int&, |
|
46 double*, const int&, double*, double*, |
|
47 const int&, int&); |
538
|
48 } |
|
49 |
|
50 // It would be best to share some of this code with QR class... |
|
51 |
|
52 QRP::QRP (const Matrix& a, QR::type qr_type) |
|
53 { |
|
54 assert (qr_type != QR::raw); |
|
55 |
|
56 int m = a.rows (); |
|
57 int n = a.cols (); |
|
58 |
|
59 if (m == 0 || n == 0) |
|
60 { |
|
61 (*current_liboctave_error_handler) ("QR must have non-empty matrix"); |
|
62 return; |
|
63 } |
|
64 |
|
65 double *tmp_data; |
|
66 int min_mn = m < n ? m : n; |
|
67 double *tau = new double[min_mn]; |
|
68 int lwork = 3*n; |
|
69 double *work = new double[lwork]; |
|
70 int info = 0; |
|
71 |
|
72 if (m > n) |
|
73 { |
|
74 tmp_data = new double [m*m]; |
|
75 copy (tmp_data, a.data (), a.length ()); |
|
76 } |
|
77 else |
|
78 tmp_data = dup (a.data (), a.length ()); |
|
79 |
|
80 int *jpvt = new int[n]; |
|
81 |
|
82 // Clear Pivot vector (code to enforce a certain permutation would go |
|
83 // here...) |
|
84 |
|
85 for (int i = 0; i < n; i++) |
|
86 jpvt[i] = 0; |
|
87 |
1253
|
88 F77_FCN (dgeqpf, DGEQPF) (m, n, tmp_data, m, jpvt, tau, work, info); |
538
|
89 |
|
90 // Form Permutation matrix (if economy is requested, return the |
|
91 // indices only!) |
|
92 |
|
93 if (qr_type == QR::economy && m > n) |
|
94 { |
|
95 p.resize (1, n, 0.0); |
|
96 for (int j = 0; j < n; j++) |
|
97 p.elem (0, j) = jpvt[j]; |
|
98 } |
|
99 else |
|
100 { |
|
101 p.resize (n, n, 0.0); |
|
102 for (int j = 0; j < n; j++) |
|
103 p.elem (jpvt[j]-1, j) = 1.0; |
|
104 } |
|
105 |
|
106 delete [] jpvt; |
|
107 delete [] work; |
|
108 |
1011
|
109 int n2; |
538
|
110 if (qr_type == QR::economy && m > n) |
|
111 { |
1011
|
112 n2 = n; |
538
|
113 r.resize (n, n, 0.0); |
|
114 } |
|
115 else |
|
116 { |
1011
|
117 n2 = m; |
538
|
118 r.resize (m, n, 0.0); |
|
119 } |
|
120 |
|
121 for (int j = 0; j < n; j++) |
|
122 { |
|
123 int limit = j < min_mn-1 ? j : min_mn-1; |
|
124 for (int i = 0; i <= limit; i++) |
|
125 r.elem (i, j) = tmp_data[m*j+i]; |
|
126 } |
|
127 |
|
128 lwork = 32*m; |
|
129 work = new double[lwork]; |
|
130 |
1253
|
131 F77_FCN (dorgqr, DORGQR) (m, m, min_mn, tmp_data, m, tau, work, |
|
132 lwork, info); |
538
|
133 |
|
134 q = Matrix (tmp_data, m, m); |
1011
|
135 q.resize (m, n2); |
538
|
136 |
|
137 delete [] tau; |
|
138 delete [] work; |
|
139 } |
|
140 |
|
141 /* |
|
142 ;;; Local Variables: *** |
|
143 ;;; mode: C++ *** |
|
144 ;;; page-delimiter: "^/\\*" *** |
|
145 ;;; End: *** |
|
146 */ |