538
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
538
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
538
|
20 |
|
21 */ |
|
22 |
1296
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
538
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
538
|
29 #endif |
|
30 |
1367
|
31 #include <cassert> |
538
|
32 |
|
33 #include "dbleQRP.h" |
1847
|
34 #include "f77-fcn.h" |
538
|
35 #include "lo-error.h" |
|
36 |
|
37 extern "C" |
|
38 { |
1253
|
39 int F77_FCN (dgeqpf, DGEQPF) (const int&, const int&, double*, |
|
40 const int&, int*, double*, double*, |
|
41 int&); |
567
|
42 |
1253
|
43 int F77_FCN (dorgqr, DORGQR) (const int&, const int&, const int&, |
|
44 double*, const int&, double*, double*, |
|
45 const int&, int&); |
538
|
46 } |
|
47 |
|
48 // It would be best to share some of this code with QR class... |
|
49 |
|
50 QRP::QRP (const Matrix& a, QR::type qr_type) |
2763
|
51 : QR (), p () |
|
52 { |
|
53 init (a, qr_type); |
|
54 } |
|
55 |
|
56 void |
|
57 QRP::init (const Matrix& a, QR::type qr_type) |
538
|
58 { |
|
59 assert (qr_type != QR::raw); |
|
60 |
|
61 int m = a.rows (); |
|
62 int n = a.cols (); |
|
63 |
|
64 if (m == 0 || n == 0) |
|
65 { |
|
66 (*current_liboctave_error_handler) ("QR must have non-empty matrix"); |
|
67 return; |
|
68 } |
|
69 |
2763
|
70 Array<double> tau (m < n ? m : n); |
1928
|
71 double *ptau = tau.fortran_vec (); |
1922
|
72 |
2763
|
73 int lwork = 3*n > 32*m ? 3*n : 32*m; |
1928
|
74 Array<double> work (lwork); |
|
75 double *pwork = work.fortran_vec (); |
1922
|
76 |
538
|
77 int info = 0; |
|
78 |
2763
|
79 Matrix A_fact = a; |
538
|
80 if (m > n) |
2763
|
81 A_fact.resize (m, m, 0.0); |
538
|
82 |
1928
|
83 double *tmp_data = A_fact.fortran_vec (); |
538
|
84 |
1928
|
85 Array<int> jpvt (n, 0); |
|
86 int *pjpvt = jpvt.fortran_vec (); |
538
|
87 |
1928
|
88 // Code to enforce a certain permutation could go here... |
538
|
89 |
1928
|
90 F77_XFCN (dgeqpf, DGEQPF, (m, n, tmp_data, m, pjpvt, ptau, pwork, info)); |
1922
|
91 |
|
92 if (f77_exception_encountered) |
|
93 (*current_liboctave_error_handler) ("unrecoverable error in dgeqpf"); |
538
|
94 else |
|
95 { |
1922
|
96 // Form Permutation matrix (if economy is requested, return the |
|
97 // indices only!) |
|
98 |
2763
|
99 if (qr_type == QR::economy) |
1922
|
100 { |
|
101 p.resize (1, n, 0.0); |
|
102 for (int j = 0; j < n; j++) |
1928
|
103 p.elem (0, j) = jpvt.elem (j); |
1922
|
104 } |
|
105 else |
|
106 { |
|
107 p.resize (n, n, 0.0); |
|
108 for (int j = 0; j < n; j++) |
1928
|
109 p.elem (jpvt.elem (j) - 1, j) = 1.0; |
1922
|
110 } |
|
111 |
2763
|
112 if (qr_type == QR::economy && m > n) |
|
113 r.resize (n, n, 0.0); |
|
114 else |
|
115 r.resize (m, n, 0.0); |
1922
|
116 |
2763
|
117 int min_mn = m < n ? m : n; |
1922
|
118 |
538
|
119 for (int j = 0; j < n; j++) |
1922
|
120 { |
|
121 int limit = j < min_mn-1 ? j : min_mn-1; |
|
122 for (int i = 0; i <= limit; i++) |
1928
|
123 r.elem (i, j) = A_fact.elem (i, j); |
1922
|
124 } |
|
125 |
2763
|
126 int n2 = m; |
|
127 if (qr_type == QR::economy) |
|
128 n2 = min_mn; |
1922
|
129 |
2763
|
130 F77_XFCN (dorgqr, DORGQR, (m, n2, min_mn, tmp_data, m, ptau, |
1928
|
131 pwork, lwork, info)); |
1922
|
132 |
|
133 if (f77_exception_encountered) |
|
134 (*current_liboctave_error_handler) ("unrecoverable error in dorgqr"); |
|
135 else |
|
136 { |
2468
|
137 q = A_fact; |
1922
|
138 q.resize (m, n2); |
|
139 } |
538
|
140 } |
|
141 } |
|
142 |
|
143 /* |
|
144 ;;; Local Variables: *** |
|
145 ;;; mode: C++ *** |
|
146 ;;; End: *** |
|
147 */ |