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