457
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1994, 1995, 1996, 1997, 2002, 2003, 2004, 2005, 2007 |
|
4 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 |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
457
|
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 |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
457
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
457
|
26 #endif |
|
27 |
|
28 #include "dbleQR.h" |
1847
|
29 #include "f77-fcn.h" |
457
|
30 #include "lo-error.h" |
|
31 |
|
32 extern "C" |
|
33 { |
4552
|
34 F77_RET_T |
5275
|
35 F77_FUNC (dgeqrf, DGEQRF) (const octave_idx_type&, const octave_idx_type&, double*, const octave_idx_type&, |
|
36 double*, double*, const octave_idx_type&, octave_idx_type&); |
457
|
37 |
4552
|
38 F77_RET_T |
5275
|
39 F77_FUNC (dorgqr, DORGQR) (const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, double*, |
|
40 const octave_idx_type&, double*, double*, const octave_idx_type&, octave_idx_type&); |
457
|
41 } |
|
42 |
539
|
43 QR::QR (const Matrix& a, QR::type qr_type) |
2763
|
44 : q (), r () |
|
45 { |
|
46 init (a, qr_type); |
|
47 } |
|
48 |
|
49 void |
|
50 QR::init (const Matrix& a, QR::type qr_type) |
457
|
51 { |
5275
|
52 octave_idx_type m = a.rows (); |
|
53 octave_idx_type n = a.cols (); |
457
|
54 |
|
55 if (m == 0 || n == 0) |
|
56 { |
|
57 (*current_liboctave_error_handler) ("QR must have non-empty matrix"); |
|
58 return; |
|
59 } |
|
60 |
5275
|
61 octave_idx_type min_mn = m < n ? m : n; |
1927
|
62 Array<double> tau (min_mn); |
|
63 double *ptau = tau.fortran_vec (); |
1921
|
64 |
5275
|
65 octave_idx_type lwork = 32*n; |
1927
|
66 Array<double> work (lwork); |
|
67 double *pwork = work.fortran_vec (); |
1921
|
68 |
5275
|
69 octave_idx_type info = 0; |
457
|
70 |
3883
|
71 Matrix A_fact = a; |
|
72 if (m > n && qr_type != QR::economy) |
|
73 A_fact.resize (m, m, 0.0); |
457
|
74 |
1927
|
75 double *tmp_data = A_fact.fortran_vec (); |
457
|
76 |
1927
|
77 F77_XFCN (dgeqrf, DGEQRF, (m, n, tmp_data, m, ptau, pwork, lwork, info)); |
457
|
78 |
1921
|
79 if (f77_exception_encountered) |
|
80 (*current_liboctave_error_handler) ("unrecoverable error in dgeqrf"); |
539
|
81 else |
457
|
82 { |
1921
|
83 if (qr_type == QR::raw) |
539
|
84 { |
5275
|
85 for (octave_idx_type j = 0; j < min_mn; j++) |
1921
|
86 { |
5275
|
87 octave_idx_type limit = j < min_mn - 1 ? j : min_mn - 1; |
|
88 for (octave_idx_type i = limit + 1; i < m; i++) |
1927
|
89 A_fact.elem (i, j) *= tau.elem (j); |
1921
|
90 } |
3068
|
91 |
3069
|
92 r = A_fact; |
|
93 |
|
94 if (m > n) |
|
95 r.resize (m, n); |
539
|
96 } |
|
97 else |
|
98 { |
5275
|
99 octave_idx_type n2 = (qr_type == QR::economy) ? min_mn : m; |
457
|
100 |
1921
|
101 if (qr_type == QR::economy && m > n) |
3883
|
102 r.resize (n, n, 0.0); |
1921
|
103 else |
3883
|
104 r.resize (m, n, 0.0); |
1921
|
105 |
5275
|
106 for (octave_idx_type j = 0; j < n; j++) |
1921
|
107 { |
5275
|
108 octave_idx_type limit = j < min_mn-1 ? j : min_mn-1; |
|
109 for (octave_idx_type i = 0; i <= limit; i++) |
1921
|
110 r.elem (i, j) = tmp_data[m*j+i]; |
|
111 } |
|
112 |
3883
|
113 lwork = 32 * n2; |
1927
|
114 work.resize (lwork); |
4663
|
115 double *pwork2 = work.fortran_vec (); |
457
|
116 |
3883
|
117 F77_XFCN (dorgqr, DORGQR, (m, n2, min_mn, tmp_data, m, ptau, |
4663
|
118 pwork2, lwork, info)); |
457
|
119 |
1921
|
120 if (f77_exception_encountered) |
|
121 (*current_liboctave_error_handler) |
|
122 ("unrecoverable error in dorgqr"); |
|
123 else |
|
124 { |
2244
|
125 q = A_fact; |
1921
|
126 q.resize (m, n2); |
|
127 } |
|
128 } |
|
129 } |
457
|
130 } |
|
131 |
|
132 /* |
|
133 ;;; Local Variables: *** |
|
134 ;;; mode: C++ *** |
|
135 ;;; End: *** |
|
136 */ |