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