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 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
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 { |
3887
|
39 int F77_FUNC (dgeqpf, DGEQPF) (const int&, const int&, double*, |
1253
|
40 const int&, int*, double*, double*, |
|
41 int&); |
567
|
42 |
3887
|
43 int F77_FUNC (dorgqr, DORGQR) (const int&, const int&, const int&, |
1253
|
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 |
3883
|
70 int min_mn = m < n ? m : n; |
|
71 Array<double> tau (min_mn); |
1928
|
72 double *ptau = tau.fortran_vec (); |
1922
|
73 |
2763
|
74 int lwork = 3*n > 32*m ? 3*n : 32*m; |
1928
|
75 Array<double> work (lwork); |
|
76 double *pwork = work.fortran_vec (); |
1922
|
77 |
538
|
78 int info = 0; |
|
79 |
2763
|
80 Matrix A_fact = a; |
3883
|
81 if (m > n && qr_type != QR::economy) |
2763
|
82 A_fact.resize (m, m, 0.0); |
538
|
83 |
1928
|
84 double *tmp_data = A_fact.fortran_vec (); |
538
|
85 |
1928
|
86 Array<int> jpvt (n, 0); |
|
87 int *pjpvt = jpvt.fortran_vec (); |
538
|
88 |
1928
|
89 // Code to enforce a certain permutation could go here... |
538
|
90 |
1928
|
91 F77_XFCN (dgeqpf, DGEQPF, (m, n, tmp_data, m, pjpvt, ptau, pwork, info)); |
1922
|
92 |
|
93 if (f77_exception_encountered) |
|
94 (*current_liboctave_error_handler) ("unrecoverable error in dgeqpf"); |
538
|
95 else |
|
96 { |
1922
|
97 // Form Permutation matrix (if economy is requested, return the |
|
98 // indices only!) |
|
99 |
2763
|
100 if (qr_type == QR::economy) |
1922
|
101 { |
|
102 p.resize (1, n, 0.0); |
|
103 for (int j = 0; j < n; j++) |
1928
|
104 p.elem (0, j) = jpvt.elem (j); |
1922
|
105 } |
|
106 else |
|
107 { |
|
108 p.resize (n, n, 0.0); |
|
109 for (int j = 0; j < n; j++) |
1928
|
110 p.elem (jpvt.elem (j) - 1, j) = 1.0; |
1922
|
111 } |
|
112 |
3883
|
113 int n2 = (qr_type == QR::economy) ? min_mn : m; |
|
114 |
2763
|
115 if (qr_type == QR::economy && m > n) |
|
116 r.resize (n, n, 0.0); |
|
117 else |
|
118 r.resize (m, n, 0.0); |
1922
|
119 |
538
|
120 for (int j = 0; j < n; j++) |
1922
|
121 { |
|
122 int limit = j < min_mn-1 ? j : min_mn-1; |
|
123 for (int i = 0; i <= limit; i++) |
1928
|
124 r.elem (i, j) = A_fact.elem (i, j); |
1922
|
125 } |
|
126 |
2763
|
127 F77_XFCN (dorgqr, DORGQR, (m, n2, min_mn, tmp_data, m, ptau, |
1928
|
128 pwork, lwork, info)); |
1922
|
129 |
|
130 if (f77_exception_encountered) |
|
131 (*current_liboctave_error_handler) ("unrecoverable error in dorgqr"); |
|
132 else |
|
133 { |
2468
|
134 q = A_fact; |
1922
|
135 q.resize (m, n2); |
|
136 } |
538
|
137 } |
|
138 } |
|
139 |
|
140 /* |
|
141 ;;; Local Variables: *** |
|
142 ;;; mode: C++ *** |
|
143 ;;; End: *** |
|
144 */ |