Mercurial > hg > octave-lyh
annotate liboctave/dbleQRP.cc @ 8335:64cf956a109c
templatize & fix DET
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 19 Nov 2008 11:23:07 +0100 |
parents | 29980c6b8604 |
children | 445d27d79f4e |
rev | line source |
---|---|
538 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 2002, 2003, 2004, 2005, 2007 |
4 John W. Eaton | |
538 | 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. | |
538 | 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/>. | |
538 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
1192 | 25 #include <config.h> |
538 | 26 #endif |
27 | |
1367 | 28 #include <cassert> |
538 | 29 |
30 #include "dbleQRP.h" | |
1847 | 31 #include "f77-fcn.h" |
538 | 32 #include "lo-error.h" |
33 | |
34 extern "C" | |
35 { | |
4552 | 36 F77_RET_T |
5275 | 37 F77_FUNC (dgeqpf, DGEQPF) (const octave_idx_type&, const octave_idx_type&, double*, |
38 const octave_idx_type&, octave_idx_type*, double*, double*, octave_idx_type&); | |
567 | 39 |
4552 | 40 F77_RET_T |
5275 | 41 F77_FUNC (dorgqr, DORGQR) (const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, |
42 double*, const octave_idx_type&, double*, double*, | |
43 const octave_idx_type&, octave_idx_type&); | |
538 | 44 } |
45 | |
46 // It would be best to share some of this code with QR class... | |
47 | |
48 QRP::QRP (const Matrix& a, QR::type qr_type) | |
2763 | 49 : QR (), p () |
50 { | |
51 init (a, qr_type); | |
52 } | |
53 | |
54 void | |
55 QRP::init (const Matrix& a, QR::type qr_type) | |
538 | 56 { |
57 assert (qr_type != QR::raw); | |
58 | |
5275 | 59 octave_idx_type m = a.rows (); |
60 octave_idx_type n = a.cols (); | |
538 | 61 |
62 if (m == 0 || n == 0) | |
63 { | |
64 (*current_liboctave_error_handler) ("QR must have non-empty matrix"); | |
65 return; | |
66 } | |
67 | |
5275 | 68 octave_idx_type min_mn = m < n ? m : n; |
3883 | 69 Array<double> tau (min_mn); |
1928 | 70 double *ptau = tau.fortran_vec (); |
1922 | 71 |
5275 | 72 octave_idx_type lwork = 3*n > 32*m ? 3*n : 32*m; |
1928 | 73 Array<double> work (lwork); |
74 double *pwork = work.fortran_vec (); | |
1922 | 75 |
5275 | 76 octave_idx_type info = 0; |
538 | 77 |
2763 | 78 Matrix A_fact = a; |
3883 | 79 if (m > n && qr_type != QR::economy) |
2763 | 80 A_fact.resize (m, m, 0.0); |
538 | 81 |
1928 | 82 double *tmp_data = A_fact.fortran_vec (); |
538 | 83 |
5275 | 84 Array<octave_idx_type> jpvt (n, 0); |
85 octave_idx_type *pjpvt = jpvt.fortran_vec (); | |
538 | 86 |
1928 | 87 // Code to enforce a certain permutation could go here... |
538 | 88 |
1928 | 89 F77_XFCN (dgeqpf, DGEQPF, (m, n, tmp_data, m, pjpvt, ptau, pwork, info)); |
1922 | 90 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
91 // Form Permutation matrix (if economy is requested, return the |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
92 // indices only!) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
93 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
94 if (qr_type == QR::economy) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
95 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
96 p.resize (1, n, 0.0); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
97 for (octave_idx_type j = 0; j < n; j++) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
98 p.elem (0, j) = jpvt.elem (j); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
99 } |
538 | 100 else |
101 { | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
102 p.resize (n, n, 0.0); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
103 for (octave_idx_type j = 0; j < n; j++) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
104 p.elem (jpvt.elem (j) - 1, j) = 1.0; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
105 } |
1922 | 106 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
107 octave_idx_type n2 = (qr_type == QR::economy) ? min_mn : m; |
1922 | 108 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
109 if (qr_type == QR::economy && m > n) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
110 r.resize (n, n, 0.0); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
111 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
112 r.resize (m, n, 0.0); |
3883 | 113 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
114 for (octave_idx_type j = 0; j < n; j++) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
115 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
116 octave_idx_type limit = j < min_mn-1 ? j : min_mn-1; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
117 for (octave_idx_type i = 0; i <= limit; i++) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
118 r.elem (i, j) = A_fact.elem (i, j); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
119 } |
1922 | 120 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
121 F77_XFCN (dorgqr, DORGQR, (m, n2, min_mn, tmp_data, m, ptau, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
122 pwork, lwork, info)); |
1922 | 123 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
124 q = A_fact; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
125 q.resize (m, n2); |
538 | 126 } |
127 | |
128 /* | |
129 ;;; Local Variables: *** | |
130 ;;; mode: C++ *** | |
131 ;;; End: *** | |
132 */ |