457
|
1 // -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
457
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
457
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
457
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
457
|
30 #endif |
|
31 |
|
32 #include "dbleQR.h" |
1368
|
33 #include "f77-uscore.h" |
457
|
34 #include "lo-error.h" |
1368
|
35 #include "mx-inlines.cc" |
457
|
36 |
|
37 extern "C" |
|
38 { |
1253
|
39 int F77_FCN (dgeqrf, DGEQRF) (const int&, const int&, double*, |
|
40 const int&, double*, double*, |
|
41 const int&, int&); |
457
|
42 |
1253
|
43 int F77_FCN (dorgqr, DORGQR) (const int&, const int&, const int&, |
|
44 double*, const int&, double*, double*, |
|
45 const int&, int&); |
457
|
46 } |
|
47 |
539
|
48 QR::QR (const Matrix& a, QR::type qr_type) |
457
|
49 { |
|
50 int m = a.rows (); |
|
51 int n = a.cols (); |
|
52 |
|
53 if (m == 0 || n == 0) |
|
54 { |
|
55 (*current_liboctave_error_handler) ("QR must have non-empty matrix"); |
|
56 return; |
|
57 } |
|
58 |
|
59 double *tmp_data; |
|
60 int min_mn = m < n ? m : n; |
|
61 double *tau = new double[min_mn]; |
|
62 int lwork = 32*n; |
|
63 double *work = new double[lwork]; |
|
64 int info = 0; |
|
65 |
|
66 if (m > n) |
|
67 { |
|
68 tmp_data = new double [m*m]; |
|
69 copy (tmp_data, a.data (), a.length ()); |
|
70 } |
|
71 else |
|
72 tmp_data = dup (a.data (), a.length ()); |
|
73 |
1253
|
74 F77_FCN (dgeqrf, DGEQRF) (m, n, tmp_data, m, tau, work, lwork, info); |
457
|
75 |
|
76 delete [] work; |
|
77 |
539
|
78 if (qr_type == QR::raw) |
|
79 { |
|
80 for (int j = 0; j < min_mn; j++) |
|
81 { |
|
82 int limit = j < min_mn - 1 ? j : min_mn - 1; |
|
83 for (int i = limit + 1; i < m; i++) |
|
84 tmp_data[m*j+i] *= tau[j]; |
|
85 } |
|
86 } |
|
87 else |
457
|
88 { |
1011
|
89 int n2; |
539
|
90 if (qr_type == QR::economy && m > n) |
|
91 { |
1011
|
92 n2 = n; |
539
|
93 r.resize (n, n, 0.0); |
|
94 } |
|
95 else |
|
96 { |
1011
|
97 n2 = m; |
539
|
98 r.resize (m, n, 0.0); |
|
99 } |
457
|
100 |
539
|
101 for (int j = 0; j < n; j++) |
|
102 { |
|
103 int limit = j < min_mn-1 ? j : min_mn-1; |
|
104 for (int i = 0; i <= limit; i++) |
|
105 r.elem (i, j) = tmp_data[m*j+i]; |
|
106 } |
457
|
107 |
539
|
108 lwork = 32*m; |
|
109 work = new double[lwork]; |
|
110 |
1253
|
111 F77_FCN (dorgqr, DORGQR) (m, m, min_mn, tmp_data, m, tau, work, |
|
112 lwork, info); |
457
|
113 |
539
|
114 q = Matrix (tmp_data, m, m); |
1011
|
115 q.resize (m, n2); |
539
|
116 |
|
117 delete [] tau; |
|
118 delete [] work; |
|
119 } |
457
|
120 } |
|
121 |
|
122 /* |
|
123 ;;; Local Variables: *** |
|
124 ;;; mode: C++ *** |
|
125 ;;; page-delimiter: "^/\\*" *** |
|
126 ;;; End: *** |
|
127 */ |