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