457
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
457
|
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. |
457
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
24 #pragma implementation |
|
25 #endif |
|
26 |
457
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
457
|
29 #endif |
|
30 |
|
31 #include "dbleQR.h" |
1847
|
32 #include "f77-fcn.h" |
457
|
33 #include "lo-error.h" |
|
34 |
|
35 extern "C" |
|
36 { |
4552
|
37 F77_RET_T |
|
38 F77_FUNC (dgeqrf, DGEQRF) (const int&, const int&, double*, const int&, |
|
39 double*, double*, const int&, int&); |
457
|
40 |
4552
|
41 F77_RET_T |
|
42 F77_FUNC (dorgqr, DORGQR) (const int&, const int&, const int&, double*, |
|
43 const int&, double*, double*, const int&, int&); |
457
|
44 } |
|
45 |
539
|
46 QR::QR (const Matrix& a, QR::type qr_type) |
2763
|
47 : q (), r () |
|
48 { |
|
49 init (a, qr_type); |
|
50 } |
|
51 |
|
52 void |
|
53 QR::init (const Matrix& a, QR::type qr_type) |
457
|
54 { |
|
55 int m = a.rows (); |
|
56 int n = a.cols (); |
|
57 |
|
58 if (m == 0 || n == 0) |
|
59 { |
|
60 (*current_liboctave_error_handler) ("QR must have non-empty matrix"); |
|
61 return; |
|
62 } |
|
63 |
|
64 int min_mn = m < n ? m : n; |
1927
|
65 Array<double> tau (min_mn); |
|
66 double *ptau = tau.fortran_vec (); |
1921
|
67 |
457
|
68 int lwork = 32*n; |
1927
|
69 Array<double> work (lwork); |
|
70 double *pwork = work.fortran_vec (); |
1921
|
71 |
457
|
72 int info = 0; |
|
73 |
3883
|
74 Matrix A_fact = a; |
|
75 if (m > n && qr_type != QR::economy) |
|
76 A_fact.resize (m, m, 0.0); |
457
|
77 |
1927
|
78 double *tmp_data = A_fact.fortran_vec (); |
457
|
79 |
1927
|
80 F77_XFCN (dgeqrf, DGEQRF, (m, n, tmp_data, m, ptau, pwork, lwork, info)); |
457
|
81 |
1921
|
82 if (f77_exception_encountered) |
|
83 (*current_liboctave_error_handler) ("unrecoverable error in dgeqrf"); |
539
|
84 else |
457
|
85 { |
1921
|
86 if (qr_type == QR::raw) |
539
|
87 { |
1921
|
88 for (int j = 0; j < min_mn; j++) |
|
89 { |
|
90 int limit = j < min_mn - 1 ? j : min_mn - 1; |
|
91 for (int i = limit + 1; i < m; i++) |
1927
|
92 A_fact.elem (i, j) *= tau.elem (j); |
1921
|
93 } |
3068
|
94 |
3069
|
95 r = A_fact; |
|
96 |
|
97 if (m > n) |
|
98 r.resize (m, n); |
539
|
99 } |
|
100 else |
|
101 { |
3883
|
102 int n2 = (qr_type == QR::economy) ? min_mn : m; |
457
|
103 |
1921
|
104 if (qr_type == QR::economy && m > n) |
3883
|
105 r.resize (n, n, 0.0); |
1921
|
106 else |
3883
|
107 r.resize (m, n, 0.0); |
1921
|
108 |
|
109 for (int j = 0; j < n; j++) |
|
110 { |
|
111 int limit = j < min_mn-1 ? j : min_mn-1; |
|
112 for (int i = 0; i <= limit; i++) |
|
113 r.elem (i, j) = tmp_data[m*j+i]; |
|
114 } |
|
115 |
3883
|
116 lwork = 32 * n2; |
1927
|
117 work.resize (lwork); |
4663
|
118 double *pwork2 = work.fortran_vec (); |
457
|
119 |
3883
|
120 F77_XFCN (dorgqr, DORGQR, (m, n2, min_mn, tmp_data, m, ptau, |
4663
|
121 pwork2, lwork, info)); |
457
|
122 |
1921
|
123 if (f77_exception_encountered) |
|
124 (*current_liboctave_error_handler) |
|
125 ("unrecoverable error in dorgqr"); |
|
126 else |
|
127 { |
2244
|
128 q = A_fact; |
1921
|
129 q.resize (m, n2); |
|
130 } |
|
131 } |
|
132 } |
457
|
133 } |
|
134 |
|
135 /* |
|
136 ;;; Local Variables: *** |
|
137 ;;; mode: C++ *** |
|
138 ;;; End: *** |
|
139 */ |