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 |
1296
|
23 #if defined (__GNUG__) |
|
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 { |
1253
|
37 int F77_FCN (dgeqrf, DGEQRF) (const int&, const int&, double*, |
|
38 const int&, double*, double*, |
|
39 const int&, int&); |
457
|
40 |
1253
|
41 int F77_FCN (dorgqr, DORGQR) (const int&, const int&, const int&, |
|
42 double*, const int&, double*, double*, |
|
43 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 |
1927
|
74 Matrix A_fact; |
457
|
75 if (m > n) |
|
76 { |
1927
|
77 A_fact.resize (m, m); |
|
78 A_fact.insert (a, 0, 0); |
457
|
79 } |
|
80 else |
1927
|
81 A_fact = a; |
457
|
82 |
1927
|
83 double *tmp_data = A_fact.fortran_vec (); |
457
|
84 |
1927
|
85 F77_XFCN (dgeqrf, DGEQRF, (m, n, tmp_data, m, ptau, pwork, lwork, info)); |
457
|
86 |
1921
|
87 if (f77_exception_encountered) |
|
88 (*current_liboctave_error_handler) ("unrecoverable error in dgeqrf"); |
539
|
89 else |
457
|
90 { |
1921
|
91 if (qr_type == QR::raw) |
539
|
92 { |
1921
|
93 for (int j = 0; j < min_mn; j++) |
|
94 { |
|
95 int limit = j < min_mn - 1 ? j : min_mn - 1; |
|
96 for (int i = limit + 1; i < m; i++) |
1927
|
97 A_fact.elem (i, j) *= tau.elem (j); |
1921
|
98 } |
3068
|
99 |
3069
|
100 r = A_fact; |
|
101 |
|
102 if (m > n) |
|
103 r.resize (m, n); |
539
|
104 } |
|
105 else |
|
106 { |
1921
|
107 volatile int n2; |
457
|
108 |
1921
|
109 if (qr_type == QR::economy && m > n) |
|
110 { |
|
111 n2 = n; |
|
112 r.resize (n, n, 0.0); |
|
113 } |
|
114 else |
|
115 { |
|
116 n2 = m; |
|
117 r.resize (m, n, 0.0); |
|
118 } |
|
119 |
|
120 for (int j = 0; j < n; j++) |
|
121 { |
|
122 int limit = j < min_mn-1 ? j : min_mn-1; |
|
123 for (int i = 0; i <= limit; i++) |
|
124 r.elem (i, j) = tmp_data[m*j+i]; |
|
125 } |
|
126 |
|
127 lwork = 32*m; |
1927
|
128 work.resize (lwork); |
|
129 double *pwork = work.fortran_vec (); |
457
|
130 |
1927
|
131 F77_XFCN (dorgqr, DORGQR, (m, m, min_mn, tmp_data, m, ptau, |
|
132 pwork, lwork, info)); |
457
|
133 |
1921
|
134 if (f77_exception_encountered) |
|
135 (*current_liboctave_error_handler) |
|
136 ("unrecoverable error in dorgqr"); |
|
137 else |
|
138 { |
2244
|
139 q = A_fact; |
1921
|
140 q.resize (m, n2); |
|
141 } |
|
142 } |
|
143 } |
457
|
144 } |
|
145 |
|
146 /* |
|
147 ;;; Local Variables: *** |
|
148 ;;; mode: C++ *** |
|
149 ;;; End: *** |
|
150 */ |