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