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 { |
4552
|
39 F77_RET_T |
|
40 F77_FUNC (dgeqpf, DGEQPF) (const int&, const int&, double*, |
|
41 const int&, int*, double*, double*, int&); |
567
|
42 |
4552
|
43 F77_RET_T |
|
44 F77_FUNC (dorgqr, DORGQR) (const int&, const int&, const int&, |
|
45 double*, const int&, double*, double*, |
|
46 const int&, int&); |
538
|
47 } |
|
48 |
|
49 // It would be best to share some of this code with QR class... |
|
50 |
|
51 QRP::QRP (const Matrix& a, QR::type qr_type) |
2763
|
52 : QR (), p () |
|
53 { |
|
54 init (a, qr_type); |
|
55 } |
|
56 |
|
57 void |
|
58 QRP::init (const Matrix& a, QR::type qr_type) |
538
|
59 { |
|
60 assert (qr_type != QR::raw); |
|
61 |
|
62 int m = a.rows (); |
|
63 int n = a.cols (); |
|
64 |
|
65 if (m == 0 || n == 0) |
|
66 { |
|
67 (*current_liboctave_error_handler) ("QR must have non-empty matrix"); |
|
68 return; |
|
69 } |
|
70 |
3883
|
71 int min_mn = m < n ? m : n; |
|
72 Array<double> tau (min_mn); |
1928
|
73 double *ptau = tau.fortran_vec (); |
1922
|
74 |
2763
|
75 int lwork = 3*n > 32*m ? 3*n : 32*m; |
1928
|
76 Array<double> work (lwork); |
|
77 double *pwork = work.fortran_vec (); |
1922
|
78 |
538
|
79 int info = 0; |
|
80 |
2763
|
81 Matrix A_fact = a; |
3883
|
82 if (m > n && qr_type != QR::economy) |
2763
|
83 A_fact.resize (m, m, 0.0); |
538
|
84 |
1928
|
85 double *tmp_data = A_fact.fortran_vec (); |
538
|
86 |
1928
|
87 Array<int> jpvt (n, 0); |
|
88 int *pjpvt = jpvt.fortran_vec (); |
538
|
89 |
1928
|
90 // Code to enforce a certain permutation could go here... |
538
|
91 |
1928
|
92 F77_XFCN (dgeqpf, DGEQPF, (m, n, tmp_data, m, pjpvt, ptau, pwork, info)); |
1922
|
93 |
|
94 if (f77_exception_encountered) |
|
95 (*current_liboctave_error_handler) ("unrecoverable error in dgeqpf"); |
538
|
96 else |
|
97 { |
1922
|
98 // Form Permutation matrix (if economy is requested, return the |
|
99 // indices only!) |
|
100 |
2763
|
101 if (qr_type == QR::economy) |
1922
|
102 { |
|
103 p.resize (1, n, 0.0); |
|
104 for (int j = 0; j < n; j++) |
1928
|
105 p.elem (0, j) = jpvt.elem (j); |
1922
|
106 } |
|
107 else |
|
108 { |
|
109 p.resize (n, n, 0.0); |
|
110 for (int j = 0; j < n; j++) |
1928
|
111 p.elem (jpvt.elem (j) - 1, j) = 1.0; |
1922
|
112 } |
|
113 |
3883
|
114 int n2 = (qr_type == QR::economy) ? min_mn : m; |
|
115 |
2763
|
116 if (qr_type == QR::economy && m > n) |
|
117 r.resize (n, n, 0.0); |
|
118 else |
|
119 r.resize (m, n, 0.0); |
1922
|
120 |
538
|
121 for (int j = 0; j < n; j++) |
1922
|
122 { |
|
123 int limit = j < min_mn-1 ? j : min_mn-1; |
|
124 for (int i = 0; i <= limit; i++) |
1928
|
125 r.elem (i, j) = A_fact.elem (i, j); |
1922
|
126 } |
|
127 |
2763
|
128 F77_XFCN (dorgqr, DORGQR, (m, n2, min_mn, tmp_data, m, ptau, |
1928
|
129 pwork, lwork, info)); |
1922
|
130 |
|
131 if (f77_exception_encountered) |
|
132 (*current_liboctave_error_handler) ("unrecoverable error in dorgqr"); |
|
133 else |
|
134 { |
2468
|
135 q = A_fact; |
1922
|
136 q.resize (m, n2); |
|
137 } |
538
|
138 } |
|
139 } |
|
140 |
|
141 /* |
|
142 ;;; Local Variables: *** |
|
143 ;;; mode: C++ *** |
|
144 ;;; End: *** |
|
145 */ |