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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
457
|
26 #endif |
|
27 |
|
28 #include "dbleQR.h" |
|
29 #include "mx-inlines.cc" |
|
30 #include "lo-error.h" |
|
31 #include "f77-uscore.h" |
|
32 |
|
33 extern "C" |
|
34 { |
1253
|
35 int F77_FCN (dgeqrf, DGEQRF) (const int&, const int&, double*, |
|
36 const int&, double*, double*, |
|
37 const int&, int&); |
457
|
38 |
1253
|
39 int F77_FCN (dorgqr, DORGQR) (const int&, const int&, const int&, |
|
40 double*, const int&, double*, double*, |
|
41 const int&, int&); |
457
|
42 } |
|
43 |
539
|
44 QR::QR (const Matrix& a, QR::type qr_type) |
457
|
45 { |
|
46 int m = a.rows (); |
|
47 int n = a.cols (); |
|
48 |
|
49 if (m == 0 || n == 0) |
|
50 { |
|
51 (*current_liboctave_error_handler) ("QR must have non-empty matrix"); |
|
52 return; |
|
53 } |
|
54 |
|
55 double *tmp_data; |
|
56 int min_mn = m < n ? m : n; |
|
57 double *tau = new double[min_mn]; |
|
58 int lwork = 32*n; |
|
59 double *work = new double[lwork]; |
|
60 int info = 0; |
|
61 |
|
62 if (m > n) |
|
63 { |
|
64 tmp_data = new double [m*m]; |
|
65 copy (tmp_data, a.data (), a.length ()); |
|
66 } |
|
67 else |
|
68 tmp_data = dup (a.data (), a.length ()); |
|
69 |
1253
|
70 F77_FCN (dgeqrf, DGEQRF) (m, n, tmp_data, m, tau, work, lwork, info); |
457
|
71 |
|
72 delete [] work; |
|
73 |
539
|
74 if (qr_type == QR::raw) |
|
75 { |
|
76 for (int j = 0; j < min_mn; j++) |
|
77 { |
|
78 int limit = j < min_mn - 1 ? j : min_mn - 1; |
|
79 for (int i = limit + 1; i < m; i++) |
|
80 tmp_data[m*j+i] *= tau[j]; |
|
81 } |
|
82 } |
|
83 else |
457
|
84 { |
1011
|
85 int n2; |
539
|
86 if (qr_type == QR::economy && m > n) |
|
87 { |
1011
|
88 n2 = n; |
539
|
89 r.resize (n, n, 0.0); |
|
90 } |
|
91 else |
|
92 { |
1011
|
93 n2 = m; |
539
|
94 r.resize (m, n, 0.0); |
|
95 } |
457
|
96 |
539
|
97 for (int j = 0; j < n; j++) |
|
98 { |
|
99 int limit = j < min_mn-1 ? j : min_mn-1; |
|
100 for (int i = 0; i <= limit; i++) |
|
101 r.elem (i, j) = tmp_data[m*j+i]; |
|
102 } |
457
|
103 |
539
|
104 lwork = 32*m; |
|
105 work = new double[lwork]; |
|
106 |
1253
|
107 F77_FCN (dorgqr, DORGQR) (m, m, min_mn, tmp_data, m, tau, work, |
|
108 lwork, info); |
457
|
109 |
539
|
110 q = Matrix (tmp_data, m, m); |
1011
|
111 q.resize (m, n2); |
539
|
112 |
|
113 delete [] tau; |
|
114 delete [] work; |
|
115 } |
457
|
116 } |
|
117 |
|
118 /* |
|
119 ;;; Local Variables: *** |
|
120 ;;; mode: C++ *** |
|
121 ;;; page-delimiter: "^/\\*" *** |
|
122 ;;; End: *** |
|
123 */ |