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 { |
|
35 int F77_FCN (dgeqrf) (const int*, const int*, double*, const int*, |
|
36 double*, double*, const int*, int*); |
|
37 |
|
38 int F77_FCN (dorgqr) (const int*, const int*, const int*, double*, |
|
39 const int*, double*, double*, const int*, int*); |
539
|
40 |
|
41 int F77_FCN (dgeqpf) (const int*, const int*, double*, const int*, |
|
42 int*, double*, double*, int*); |
457
|
43 } |
|
44 |
539
|
45 QR::QR (const Matrix& a, QR::type qr_type) |
457
|
46 { |
|
47 int m = a.rows (); |
|
48 int n = a.cols (); |
|
49 |
|
50 if (m == 0 || n == 0) |
|
51 { |
|
52 (*current_liboctave_error_handler) ("QR must have non-empty matrix"); |
|
53 return; |
|
54 } |
|
55 |
|
56 double *tmp_data; |
|
57 int min_mn = m < n ? m : n; |
|
58 double *tau = new double[min_mn]; |
|
59 int lwork = 32*n; |
|
60 double *work = new double[lwork]; |
|
61 int info = 0; |
|
62 |
|
63 if (m > n) |
|
64 { |
|
65 tmp_data = new double [m*m]; |
|
66 copy (tmp_data, a.data (), a.length ()); |
|
67 } |
|
68 else |
|
69 tmp_data = dup (a.data (), a.length ()); |
|
70 |
|
71 F77_FCN (dgeqrf) (&m, &n, tmp_data, &m, tau, work, &lwork, &info); |
|
72 |
|
73 delete [] work; |
|
74 |
539
|
75 if (qr_type == QR::raw) |
|
76 { |
|
77 for (int j = 0; j < min_mn; j++) |
|
78 { |
|
79 int limit = j < min_mn - 1 ? j : min_mn - 1; |
|
80 for (int i = limit + 1; i < m; i++) |
|
81 tmp_data[m*j+i] *= tau[j]; |
|
82 } |
|
83 } |
|
84 else |
457
|
85 { |
1011
|
86 int n2; |
539
|
87 if (qr_type == QR::economy && m > n) |
|
88 { |
1011
|
89 n2 = n; |
539
|
90 r.resize (n, n, 0.0); |
|
91 } |
|
92 else |
|
93 { |
1011
|
94 n2 = m; |
539
|
95 r.resize (m, n, 0.0); |
|
96 } |
457
|
97 |
539
|
98 for (int j = 0; j < n; j++) |
|
99 { |
|
100 int limit = j < min_mn-1 ? j : min_mn-1; |
|
101 for (int i = 0; i <= limit; i++) |
|
102 r.elem (i, j) = tmp_data[m*j+i]; |
|
103 } |
457
|
104 |
539
|
105 lwork = 32*m; |
|
106 work = new double[lwork]; |
|
107 |
|
108 F77_FCN (dorgqr) (&m, &m, &min_mn, tmp_data, &m, tau, work, |
|
109 &lwork, &info); |
457
|
110 |
539
|
111 q = Matrix (tmp_data, m, m); |
1011
|
112 q.resize (m, n2); |
539
|
113 |
|
114 delete [] tau; |
|
115 delete [] work; |
|
116 } |
457
|
117 } |
|
118 |
|
119 /* |
|
120 ;;; Local Variables: *** |
|
121 ;;; mode: C++ *** |
|
122 ;;; page-delimiter: "^/\\*" *** |
|
123 ;;; End: *** |
|
124 */ |