Mercurial > hg > octave-nkf
annotate liboctave/numeric/dbleSCHUR.cc @ 20809:ffc6cdcd02c5 stable
Fix segfault when complex double matrix calls ZGETRF (bug #45577).
* CMatrix.cc (finverse, determinant, rcond, fsolve): Calculate norm of matrix
and if it is NaN, skip calling ZGETRF in LAPACK and set info to non-zero value
to signal an error.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 10 Oct 2015 16:46:00 -0700 |
parents | 4197fc428c7d |
children |
rev | line source |
---|---|
457 | 1 /* |
2 | |
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17769
diff
changeset
|
3 Copyright (C) 1994-2015 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
457 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
457 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
1192 | 24 #include <config.h> |
457 | 25 #endif |
26 | |
3503 | 27 #include <iostream> |
1631 | 28 |
457 | 29 #include "dbleSCHUR.h" |
1847 | 30 #include "f77-fcn.h" |
457 | 31 #include "lo-error.h" |
32 | |
33 extern "C" | |
34 { | |
4552 | 35 F77_RET_T |
36 F77_FUNC (dgeesx, DGEESX) (F77_CONST_CHAR_ARG_DECL, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
37 F77_CONST_CHAR_ARG_DECL, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
38 SCHUR::select_function, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
39 F77_CONST_CHAR_ARG_DECL, |
11495 | 40 const octave_idx_type&, double*, |
41 const octave_idx_type&, octave_idx_type&, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
42 double*, double*, double*, const octave_idx_type&, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
43 double&, double&, double*, const octave_idx_type&, |
11495 | 44 octave_idx_type*, const octave_idx_type&, |
45 octave_idx_type*, octave_idx_type& | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
46 F77_CHAR_ARG_LEN_DECL |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
47 F77_CHAR_ARG_LEN_DECL |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
48 F77_CHAR_ARG_LEN_DECL); |
457 | 49 } |
50 | |
5275 | 51 static octave_idx_type |
1486 | 52 select_ana (const double& a, const double&) |
457 | 53 { |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
54 return (a < 0.0); |
457 | 55 } |
56 | |
5275 | 57 static octave_idx_type |
1251 | 58 select_dig (const double& a, const double& b) |
457 | 59 { |
1251 | 60 return (hypot (a, b) < 1.0); |
457 | 61 } |
62 | |
5275 | 63 octave_idx_type |
5008 | 64 SCHUR::init (const Matrix& a, const std::string& ord, bool calc_unitary) |
457 | 65 { |
5275 | 66 octave_idx_type a_nr = a.rows (); |
67 octave_idx_type a_nc = a.cols (); | |
1929 | 68 |
457 | 69 if (a_nr != a_nc) |
70 { | |
71 (*current_liboctave_error_handler) ("SCHUR requires square matrix"); | |
72 return -1; | |
73 } | |
10607
f7501986e42d
make schur more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
74 else if (a_nr == 0) |
f7501986e42d
make schur more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
75 { |
f7501986e42d
make schur more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
76 schur_mat.clear (); |
f7501986e42d
make schur more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
77 unitary_mat.clear (); |
f7501986e42d
make schur more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
78 return 0; |
f7501986e42d
make schur more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
79 } |
457 | 80 |
3334 | 81 // Workspace requirements may need to be fixed if any of the |
82 // following change. | |
83 | |
5008 | 84 char jobvs; |
1930 | 85 char sense = 'N'; |
86 char sort = 'N'; | |
457 | 87 |
5008 | 88 if (calc_unitary) |
89 jobvs = 'V'; | |
90 else | |
91 jobvs = 'N'; | |
92 | |
1756 | 93 char ord_char = ord.empty () ? 'U' : ord[0]; |
94 | |
95 if (ord_char == 'A' || ord_char == 'D' || ord_char == 'a' || ord_char == 'd') | |
1930 | 96 sort = 'S'; |
457 | 97 |
1929 | 98 if (ord_char == 'A' || ord_char == 'a') |
99 selector = select_ana; | |
100 else if (ord_char == 'D' || ord_char == 'd') | |
101 selector = select_dig; | |
1930 | 102 else |
103 selector = 0; | |
457 | 104 |
5275 | 105 octave_idx_type n = a_nc; |
106 octave_idx_type lwork = 8 * n; | |
107 octave_idx_type liwork = 1; | |
108 octave_idx_type info; | |
109 octave_idx_type sdim; | |
457 | 110 double rconde; |
111 double rcondv; | |
112 | |
1929 | 113 schur_mat = a; |
5008 | 114 |
115 if (calc_unitary) | |
10607
f7501986e42d
make schur more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
116 unitary_mat.clear (n, n); |
1929 | 117 |
118 double *s = schur_mat.fortran_vec (); | |
119 double *q = unitary_mat.fortran_vec (); | |
457 | 120 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
121 Array<double> wr (dim_vector (n, 1)); |
1929 | 122 double *pwr = wr.fortran_vec (); |
123 | |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
124 Array<double> wi (dim_vector (n, 1)); |
1929 | 125 double *pwi = wi.fortran_vec (); |
126 | |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
127 Array<double> work (dim_vector (lwork, 1)); |
1929 | 128 double *pwork = work.fortran_vec (); |
457 | 129 |
3334 | 130 // BWORK is not referenced for the non-ordered Schur routine. |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
131 octave_idx_type ntmp = (ord_char == 'N' || ord_char == 'n') ? 0 : n; |
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
132 Array<octave_idx_type> bwork (dim_vector (ntmp, 1)); |
5275 | 133 octave_idx_type *pbwork = bwork.fortran_vec (); |
1929 | 134 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
135 Array<octave_idx_type> iwork (dim_vector (liwork, 1)); |
5275 | 136 octave_idx_type *piwork = iwork.fortran_vec (); |
1929 | 137 |
4552 | 138 F77_XFCN (dgeesx, DGEESX, (F77_CONST_CHAR_ARG2 (&jobvs, 1), |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
139 F77_CONST_CHAR_ARG2 (&sort, 1), |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
140 selector, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
141 F77_CONST_CHAR_ARG2 (&sense, 1), |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
142 n, s, n, sdim, pwr, pwi, q, n, rconde, rcondv, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
143 pwork, lwork, piwork, liwork, pbwork, info |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
144 F77_CHAR_ARG_LEN (1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
145 F77_CHAR_ARG_LEN (1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
146 F77_CHAR_ARG_LEN (1))); |
457 | 147 |
148 return info; | |
149 } | |
150 | |
3504 | 151 std::ostream& |
152 operator << (std::ostream& os, const SCHUR& a) | |
457 | 153 { |
154 os << a.schur_matrix () << "\n"; | |
155 os << a.unitary_matrix () << "\n"; | |
156 | |
157 return os; | |
158 } | |
10822 | 159 |
160 SCHUR::SCHUR (const Matrix& s, const Matrix& u) | |
11498
367bfee35ba0
data member initialization fixes
John W. Eaton <jwe@octave.org>
parents:
11495
diff
changeset
|
161 : schur_mat (s), unitary_mat (u), selector (0) |
10822 | 162 { |
163 octave_idx_type n = s.rows (); | |
164 if (s.columns () != n || u.rows () != n || u.columns () != n) | |
165 (*current_liboctave_error_handler) | |
166 ("schur: inconsistent matrix dimensions"); | |
167 } | |
168 |