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