Mercurial > hg > octave-nkf
annotate liboctave/CMatrix.cc @ 9585:06b8b51dca48
also handle user-defined graphics properties in new property name validation scheme
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 28 Aug 2009 18:37:31 -0400 |
parents | 7dafdb8b062f |
children | a9b37bae1802 |
rev | line source |
---|---|
1993 | 1 // Matrix manipulations. |
458 | 2 /* |
3 | |
7017 | 4 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, |
5 2003, 2004, 2005, 2006, 2007 John W. Eaton | |
8920 | 6 Copyright (C) 2008, 2009 Jaroslav Hajek |
458 | 7 |
8 This file is part of Octave. | |
9 | |
10 Octave is free software; you can redistribute it and/or modify it | |
11 under the terms of the GNU General Public License as published by the | |
7016 | 12 Free Software Foundation; either version 3 of the License, or (at your |
13 option) any later version. | |
458 | 14 |
15 Octave is distributed in the hope that it will be useful, but WITHOUT | |
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 for more details. | |
19 | |
20 You should have received a copy of the GNU General Public License | |
7016 | 21 along with Octave; see the file COPYING. If not, see |
22 <http://www.gnu.org/licenses/>. | |
458 | 23 |
24 */ | |
25 | |
26 #ifdef HAVE_CONFIG_H | |
1192 | 27 #include <config.h> |
458 | 28 #endif |
29 | |
1367 | 30 #include <cfloat> |
31 | |
3503 | 32 #include <iostream> |
6209 | 33 #include <vector> |
1367 | 34 |
5775 | 35 // FIXME |
2443 | 36 #ifdef HAVE_SYS_TYPES_H |
37 #include <sys/types.h> | |
38 #endif | |
458 | 39 |
4669 | 40 #include "Array-util.h" |
2828 | 41 #include "CMatrix.h" |
1819 | 42 #include "CmplxAEPBAL.h" |
9523
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
43 #include "CmplxCHOL.h" |
1819 | 44 #include "CmplxSCHUR.h" |
740 | 45 #include "CmplxSVD.h" |
9523
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
46 #include "DET.h" |
1847 | 47 #include "f77-fcn.h" |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
48 #include "functor.h" |
458 | 49 #include "lo-error.h" |
2354 | 50 #include "lo-ieee.h" |
51 #include "lo-mappers.h" | |
1968 | 52 #include "lo-utils.h" |
1367 | 53 #include "mx-base.h" |
2828 | 54 #include "mx-cm-dm.h" |
9523
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
55 #include "mx-cm-s.h" |
3176 | 56 #include "mx-dm-cm.h" |
1367 | 57 #include "mx-inlines.cc" |
8774
b756ce0002db
split implementation and interface in mx-op-defs and MArray-defs
Jaroslav Hajek <highegg@gmail.com>
parents:
8743
diff
changeset
|
58 #include "mx-op-defs.h" |
1650 | 59 #include "oct-cmplx.h" |
9523
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
60 #include "oct-fftw.h" |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
61 #include "oct-locbuf.h" |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
62 #include "oct-norm.h" |
458 | 63 |
64 // Fortran functions we call. | |
65 | |
66 extern "C" | |
67 { | |
7477 | 68 F77_RET_T |
69 F77_FUNC (xilaenv, XILAENV) (const octave_idx_type&, F77_CONST_CHAR_ARG_DECL, | |
70 F77_CONST_CHAR_ARG_DECL, | |
71 const octave_idx_type&, const octave_idx_type&, | |
7478 | 72 const octave_idx_type&, const octave_idx_type&, |
73 octave_idx_type& | |
74 F77_CHAR_ARG_LEN_DECL F77_CHAR_ARG_LEN_DECL); | |
7476 | 75 |
4552 | 76 F77_RET_T |
77 F77_FUNC (zgebal, ZGEBAL) (F77_CONST_CHAR_ARG_DECL, | |
5275 | 78 const octave_idx_type&, Complex*, const octave_idx_type&, octave_idx_type&, |
79 octave_idx_type&, double*, octave_idx_type& | |
4552 | 80 F77_CHAR_ARG_LEN_DECL); |
81 | |
82 F77_RET_T | |
83 F77_FUNC (dgebak, DGEBAK) (F77_CONST_CHAR_ARG_DECL, | |
84 F77_CONST_CHAR_ARG_DECL, | |
5275 | 85 const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, double*, |
86 const octave_idx_type&, double*, const octave_idx_type&, octave_idx_type& | |
4552 | 87 F77_CHAR_ARG_LEN_DECL |
88 F77_CHAR_ARG_LEN_DECL); | |
89 | |
90 F77_RET_T | |
91 F77_FUNC (zgemm, ZGEMM) (F77_CONST_CHAR_ARG_DECL, | |
92 F77_CONST_CHAR_ARG_DECL, | |
5275 | 93 const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, |
94 const Complex&, const Complex*, const octave_idx_type&, | |
95 const Complex*, const octave_idx_type&, const Complex&, | |
96 Complex*, const octave_idx_type& | |
4552 | 97 F77_CHAR_ARG_LEN_DECL |
98 F77_CHAR_ARG_LEN_DECL); | |
99 | |
100 F77_RET_T | |
5983 | 101 F77_FUNC (zgemv, ZGEMV) (F77_CONST_CHAR_ARG_DECL, |
102 const octave_idx_type&, const octave_idx_type&, const Complex&, | |
103 const Complex*, const octave_idx_type&, const Complex*, | |
104 const octave_idx_type&, const Complex&, Complex*, const octave_idx_type& | |
105 F77_CHAR_ARG_LEN_DECL); | |
106 | |
107 F77_RET_T | |
108 F77_FUNC (xzdotu, XZDOTU) (const octave_idx_type&, const Complex*, const octave_idx_type&, | |
109 const Complex*, const octave_idx_type&, Complex&); | |
110 | |
111 F77_RET_T | |
7800
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
112 F77_FUNC (xzdotc, XZDOTC) (const octave_idx_type&, const Complex*, const octave_idx_type&, |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
113 const Complex*, const octave_idx_type&, Complex&); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
114 |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
115 F77_RET_T |
7801
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
116 F77_FUNC (zsyrk, ZSYRK) (F77_CONST_CHAR_ARG_DECL, |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
117 F77_CONST_CHAR_ARG_DECL, |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
118 const octave_idx_type&, const octave_idx_type&, |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
119 const Complex&, const Complex*, const octave_idx_type&, |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
120 const Complex&, Complex*, const octave_idx_type& |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
121 F77_CHAR_ARG_LEN_DECL |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
122 F77_CHAR_ARG_LEN_DECL); |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
123 |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
124 F77_RET_T |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
125 F77_FUNC (zherk, ZHERK) (F77_CONST_CHAR_ARG_DECL, |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
126 F77_CONST_CHAR_ARG_DECL, |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
127 const octave_idx_type&, const octave_idx_type&, |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
128 const Complex&, const Complex*, const octave_idx_type&, |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
129 const Complex&, Complex*, const octave_idx_type& |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
130 F77_CHAR_ARG_LEN_DECL |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
131 F77_CHAR_ARG_LEN_DECL); |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
132 |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
133 F77_RET_T |
5275 | 134 F77_FUNC (zgetrf, ZGETRF) (const octave_idx_type&, const octave_idx_type&, Complex*, const octave_idx_type&, |
135 octave_idx_type*, octave_idx_type&); | |
4552 | 136 |
137 F77_RET_T | |
138 F77_FUNC (zgetrs, ZGETRS) (F77_CONST_CHAR_ARG_DECL, | |
5275 | 139 const octave_idx_type&, const octave_idx_type&, Complex*, const octave_idx_type&, |
140 const octave_idx_type*, Complex*, const octave_idx_type&, octave_idx_type& | |
4552 | 141 F77_CHAR_ARG_LEN_DECL); |
142 | |
143 F77_RET_T | |
5275 | 144 F77_FUNC (zgetri, ZGETRI) (const octave_idx_type&, Complex*, const octave_idx_type&, const octave_idx_type*, |
145 Complex*, const octave_idx_type&, octave_idx_type&); | |
4552 | 146 |
147 F77_RET_T | |
148 F77_FUNC (zgecon, ZGECON) (F77_CONST_CHAR_ARG_DECL, | |
5275 | 149 const octave_idx_type&, Complex*, |
150 const octave_idx_type&, const double&, double&, | |
151 Complex*, double*, octave_idx_type& | |
4552 | 152 F77_CHAR_ARG_LEN_DECL); |
153 | |
154 F77_RET_T | |
7072 | 155 F77_FUNC (zgelsy, ZGELSY) (const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, |
156 Complex*, const octave_idx_type&, Complex*, | |
157 const octave_idx_type&, octave_idx_type*, double&, octave_idx_type&, | |
158 Complex*, const octave_idx_type&, double*, octave_idx_type&); | |
159 | |
160 F77_RET_T | |
161 F77_FUNC (zgelsd, ZGELSD) (const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, | |
5275 | 162 Complex*, const octave_idx_type&, Complex*, |
7071 | 163 const octave_idx_type&, double*, double&, octave_idx_type&, |
7072 | 164 Complex*, const octave_idx_type&, double*, |
165 octave_idx_type*, octave_idx_type&); | |
458 | 166 |
5785 | 167 F77_RET_T |
168 F77_FUNC (zpotrf, ZPOTRF) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, | |
169 Complex*, const octave_idx_type&, | |
170 octave_idx_type& F77_CHAR_ARG_LEN_DECL); | |
171 | |
172 F77_RET_T | |
173 F77_FUNC (zpocon, ZPOCON) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, | |
174 Complex*, const octave_idx_type&, const double&, | |
175 double&, Complex*, double*, | |
176 octave_idx_type& F77_CHAR_ARG_LEN_DECL); | |
177 | |
178 F77_RET_T | |
179 F77_FUNC (zpotrs, ZPOTRS) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, | |
180 const octave_idx_type&, const Complex*, | |
181 const octave_idx_type&, Complex*, | |
182 const octave_idx_type&, octave_idx_type& | |
183 F77_CHAR_ARG_LEN_DECL); | |
184 | |
185 F77_RET_T | |
6207 | 186 F77_FUNC (ztrtri, ZTRTRI) (F77_CONST_CHAR_ARG_DECL, F77_CONST_CHAR_ARG_DECL, |
187 const octave_idx_type&, const Complex*, | |
188 const octave_idx_type&, octave_idx_type& | |
189 F77_CHAR_ARG_LEN_DECL | |
190 F77_CHAR_ARG_LEN_DECL); | |
191 | |
192 F77_RET_T | |
5785 | 193 F77_FUNC (ztrcon, ZTRCON) (F77_CONST_CHAR_ARG_DECL, F77_CONST_CHAR_ARG_DECL, |
194 F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, | |
195 const Complex*, const octave_idx_type&, double&, | |
196 Complex*, double*, octave_idx_type& | |
197 F77_CHAR_ARG_LEN_DECL | |
198 F77_CHAR_ARG_LEN_DECL | |
199 F77_CHAR_ARG_LEN_DECL); | |
200 | |
201 F77_RET_T | |
202 F77_FUNC (ztrtrs, ZTRTRS) (F77_CONST_CHAR_ARG_DECL, F77_CONST_CHAR_ARG_DECL, | |
203 F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, | |
204 const octave_idx_type&, const Complex*, | |
205 const octave_idx_type&, Complex*, | |
206 const octave_idx_type&, octave_idx_type& | |
207 F77_CHAR_ARG_LEN_DECL | |
208 F77_CHAR_ARG_LEN_DECL | |
209 F77_CHAR_ARG_LEN_DECL); | |
210 | |
4552 | 211 F77_RET_T |
212 F77_FUNC (zlartg, ZLARTG) (const Complex&, const Complex&, | |
213 double&, Complex&, Complex&); | |
214 | |
215 F77_RET_T | |
216 F77_FUNC (ztrsyl, ZTRSYL) (F77_CONST_CHAR_ARG_DECL, | |
217 F77_CONST_CHAR_ARG_DECL, | |
5275 | 218 const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, |
219 const Complex*, const octave_idx_type&, | |
220 const Complex*, const octave_idx_type&, | |
221 const Complex*, const octave_idx_type&, double&, octave_idx_type& | |
4552 | 222 F77_CHAR_ARG_LEN_DECL |
223 F77_CHAR_ARG_LEN_DECL); | |
224 | |
225 F77_RET_T | |
226 F77_FUNC (xzlange, XZLANGE) (F77_CONST_CHAR_ARG_DECL, | |
5275 | 227 const octave_idx_type&, const octave_idx_type&, const Complex*, |
228 const octave_idx_type&, double*, double& | |
4552 | 229 F77_CHAR_ARG_LEN_DECL); |
458 | 230 } |
231 | |
2354 | 232 static const Complex Complex_NaN_result (octave_NaN, octave_NaN); |
233 | |
1360 | 234 // Complex Matrix class |
458 | 235 |
236 ComplexMatrix::ComplexMatrix (const Matrix& a) | |
1214 | 237 : MArray2<Complex> (a.rows (), a.cols ()) |
458 | 238 { |
5275 | 239 for (octave_idx_type j = 0; j < cols (); j++) |
240 for (octave_idx_type i = 0; i < rows (); i++) | |
458 | 241 elem (i, j) = a.elem (i, j); |
242 } | |
243 | |
2349 | 244 ComplexMatrix::ComplexMatrix (const RowVector& rv) |
245 : MArray2<Complex> (1, rv.length (), 0.0) | |
246 { | |
5275 | 247 for (octave_idx_type i = 0; i < rv.length (); i++) |
2349 | 248 elem (0, i) = rv.elem (i); |
249 } | |
250 | |
251 ComplexMatrix::ComplexMatrix (const ColumnVector& cv) | |
252 : MArray2<Complex> (cv.length (), 1, 0.0) | |
253 { | |
5275 | 254 for (octave_idx_type i = 0; i < cv.length (); i++) |
2349 | 255 elem (i, 0) = cv.elem (i); |
256 } | |
257 | |
458 | 258 ComplexMatrix::ComplexMatrix (const DiagMatrix& a) |
1214 | 259 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
458 | 260 { |
5275 | 261 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 262 elem (i, i) = a.elem (i, i); |
263 } | |
264 | |
2349 | 265 ComplexMatrix::ComplexMatrix (const ComplexRowVector& rv) |
8614
5114ea5a41b5
use shallow copying in Matrix/RowVector/ColumnVector conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8392
diff
changeset
|
266 : MArray2<Complex> (Array2<Complex> (rv, 1, rv.length ())) |
2349 | 267 { |
268 } | |
269 | |
270 ComplexMatrix::ComplexMatrix (const ComplexColumnVector& cv) | |
8614
5114ea5a41b5
use shallow copying in Matrix/RowVector/ColumnVector conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8392
diff
changeset
|
271 : MArray2<Complex> (Array2<Complex> (cv, cv.length (), 1)) |
2349 | 272 { |
273 } | |
274 | |
458 | 275 ComplexMatrix::ComplexMatrix (const ComplexDiagMatrix& a) |
1214 | 276 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
458 | 277 { |
5275 | 278 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 279 elem (i, i) = a.elem (i, i); |
280 } | |
281 | |
5775 | 282 // FIXME -- could we use a templated mixed-type copy function |
1574 | 283 // here? |
284 | |
2828 | 285 ComplexMatrix::ComplexMatrix (const boolMatrix& a) |
3180 | 286 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
2828 | 287 { |
5275 | 288 for (octave_idx_type i = 0; i < a.rows (); i++) |
289 for (octave_idx_type j = 0; j < a.cols (); j++) | |
2828 | 290 elem (i, j) = a.elem (i, j); |
291 } | |
292 | |
1574 | 293 ComplexMatrix::ComplexMatrix (const charMatrix& a) |
3180 | 294 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
1574 | 295 { |
5275 | 296 for (octave_idx_type i = 0; i < a.rows (); i++) |
297 for (octave_idx_type j = 0; j < a.cols (); j++) | |
8956
d91fa4b20bbb
ensure nonnegative char -> real conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
298 elem (i, j) = static_cast<unsigned char> (a.elem (i, j)); |
1574 | 299 } |
300 | |
2384 | 301 bool |
458 | 302 ComplexMatrix::operator == (const ComplexMatrix& a) const |
303 { | |
304 if (rows () != a.rows () || cols () != a.cols ()) | |
2384 | 305 return false; |
458 | 306 |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9528
diff
changeset
|
307 return mx_inline_equal (length (), data (), a.data ()); |
458 | 308 } |
309 | |
2384 | 310 bool |
458 | 311 ComplexMatrix::operator != (const ComplexMatrix& a) const |
312 { | |
313 return !(*this == a); | |
314 } | |
315 | |
2815 | 316 bool |
317 ComplexMatrix::is_hermitian (void) const | |
318 { | |
5275 | 319 octave_idx_type nr = rows (); |
320 octave_idx_type nc = cols (); | |
2815 | 321 |
322 if (is_square () && nr > 0) | |
323 { | |
5275 | 324 for (octave_idx_type i = 0; i < nr; i++) |
325 for (octave_idx_type j = i; j < nc; j++) | |
2815 | 326 if (elem (i, j) != conj (elem (j, i))) |
327 return false; | |
328 | |
329 return true; | |
330 } | |
331 | |
332 return false; | |
333 } | |
334 | |
458 | 335 // destructive insert/delete/reorder operations |
336 | |
337 ComplexMatrix& | |
5275 | 338 ComplexMatrix::insert (const Matrix& a, octave_idx_type r, octave_idx_type c) |
458 | 339 { |
5275 | 340 octave_idx_type a_nr = a.rows (); |
341 octave_idx_type a_nc = a.cols (); | |
1699 | 342 |
343 if (r < 0 || r + a_nr > rows () || c < 0 || c + a_nc > cols ()) | |
458 | 344 { |
345 (*current_liboctave_error_handler) ("range error for insert"); | |
346 return *this; | |
347 } | |
348 | |
4316 | 349 if (a_nr >0 && a_nc > 0) |
350 { | |
351 make_unique (); | |
352 | |
5275 | 353 for (octave_idx_type j = 0; j < a_nc; j++) |
354 for (octave_idx_type i = 0; i < a_nr; i++) | |
4316 | 355 xelem (r+i, c+j) = a.elem (i, j); |
356 } | |
458 | 357 |
358 return *this; | |
359 } | |
360 | |
361 ComplexMatrix& | |
5275 | 362 ComplexMatrix::insert (const RowVector& a, octave_idx_type r, octave_idx_type c) |
458 | 363 { |
5275 | 364 octave_idx_type a_len = a.length (); |
4316 | 365 |
1699 | 366 if (r < 0 || r >= rows () || c < 0 || c + a_len > cols ()) |
458 | 367 { |
368 (*current_liboctave_error_handler) ("range error for insert"); | |
369 return *this; | |
370 } | |
371 | |
4316 | 372 if (a_len > 0) |
373 { | |
374 make_unique (); | |
375 | |
5275 | 376 for (octave_idx_type i = 0; i < a_len; i++) |
4316 | 377 xelem (r, c+i) = a.elem (i); |
378 } | |
458 | 379 |
380 return *this; | |
381 } | |
382 | |
383 ComplexMatrix& | |
5275 | 384 ComplexMatrix::insert (const ColumnVector& a, octave_idx_type r, octave_idx_type c) |
458 | 385 { |
5275 | 386 octave_idx_type a_len = a.length (); |
4316 | 387 |
1699 | 388 if (r < 0 || r + a_len > rows () || c < 0 || c >= cols ()) |
458 | 389 { |
390 (*current_liboctave_error_handler) ("range error for insert"); | |
391 return *this; | |
392 } | |
393 | |
4316 | 394 if (a_len > 0) |
395 { | |
396 make_unique (); | |
397 | |
5275 | 398 for (octave_idx_type i = 0; i < a_len; i++) |
4316 | 399 xelem (r+i, c) = a.elem (i); |
400 } | |
458 | 401 |
402 return *this; | |
403 } | |
404 | |
405 ComplexMatrix& | |
5275 | 406 ComplexMatrix::insert (const DiagMatrix& a, octave_idx_type r, octave_idx_type c) |
458 | 407 { |
5275 | 408 octave_idx_type a_nr = a.rows (); |
409 octave_idx_type a_nc = a.cols (); | |
1699 | 410 |
411 if (r < 0 || r + a_nr > rows () || c < 0 || c + a_nc > cols ()) | |
458 | 412 { |
413 (*current_liboctave_error_handler) ("range error for insert"); | |
414 return *this; | |
415 } | |
416 | |
1699 | 417 fill (0.0, r, c, r + a_nr - 1, c + a_nc - 1); |
418 | |
5275 | 419 octave_idx_type a_len = a.length (); |
4316 | 420 |
421 if (a_len > 0) | |
422 { | |
423 make_unique (); | |
424 | |
5275 | 425 for (octave_idx_type i = 0; i < a_len; i++) |
4316 | 426 xelem (r+i, c+i) = a.elem (i, i); |
427 } | |
458 | 428 |
429 return *this; | |
430 } | |
431 | |
432 ComplexMatrix& | |
5275 | 433 ComplexMatrix::insert (const ComplexMatrix& a, octave_idx_type r, octave_idx_type c) |
458 | 434 { |
1561 | 435 Array2<Complex>::insert (a, r, c); |
458 | 436 return *this; |
437 } | |
438 | |
439 ComplexMatrix& | |
5275 | 440 ComplexMatrix::insert (const ComplexRowVector& a, octave_idx_type r, octave_idx_type c) |
458 | 441 { |
5275 | 442 octave_idx_type a_len = a.length (); |
1699 | 443 if (r < 0 || r >= rows () || c < 0 || c + a_len > cols ()) |
458 | 444 { |
445 (*current_liboctave_error_handler) ("range error for insert"); | |
446 return *this; | |
447 } | |
448 | |
5275 | 449 for (octave_idx_type i = 0; i < a_len; i++) |
458 | 450 elem (r, c+i) = a.elem (i); |
451 | |
452 return *this; | |
453 } | |
454 | |
455 ComplexMatrix& | |
5275 | 456 ComplexMatrix::insert (const ComplexColumnVector& a, octave_idx_type r, octave_idx_type c) |
458 | 457 { |
5275 | 458 octave_idx_type a_len = a.length (); |
4316 | 459 |
1699 | 460 if (r < 0 || r + a_len > rows () || c < 0 || c >= cols ()) |
458 | 461 { |
462 (*current_liboctave_error_handler) ("range error for insert"); | |
463 return *this; | |
464 } | |
465 | |
4316 | 466 if (a_len > 0) |
467 { | |
468 make_unique (); | |
469 | |
5275 | 470 for (octave_idx_type i = 0; i < a_len; i++) |
4316 | 471 xelem (r+i, c) = a.elem (i); |
472 } | |
458 | 473 |
474 return *this; | |
475 } | |
476 | |
477 ComplexMatrix& | |
5275 | 478 ComplexMatrix::insert (const ComplexDiagMatrix& a, octave_idx_type r, octave_idx_type c) |
458 | 479 { |
5275 | 480 octave_idx_type a_nr = a.rows (); |
481 octave_idx_type a_nc = a.cols (); | |
1699 | 482 |
483 if (r < 0 || r + a_nr > rows () || c < 0 || c + a_nc > cols ()) | |
458 | 484 { |
485 (*current_liboctave_error_handler) ("range error for insert"); | |
486 return *this; | |
487 } | |
488 | |
1699 | 489 fill (0.0, r, c, r + a_nr - 1, c + a_nc - 1); |
490 | |
5275 | 491 octave_idx_type a_len = a.length (); |
4316 | 492 |
493 if (a_len > 0) | |
494 { | |
495 make_unique (); | |
496 | |
5275 | 497 for (octave_idx_type i = 0; i < a_len; i++) |
4316 | 498 xelem (r+i, c+i) = a.elem (i, i); |
499 } | |
458 | 500 |
501 return *this; | |
502 } | |
503 | |
504 ComplexMatrix& | |
505 ComplexMatrix::fill (double val) | |
506 { | |
5275 | 507 octave_idx_type nr = rows (); |
508 octave_idx_type nc = cols (); | |
4316 | 509 |
458 | 510 if (nr > 0 && nc > 0) |
4316 | 511 { |
512 make_unique (); | |
513 | |
5275 | 514 for (octave_idx_type j = 0; j < nc; j++) |
515 for (octave_idx_type i = 0; i < nr; i++) | |
4316 | 516 xelem (i, j) = val; |
517 } | |
458 | 518 |
519 return *this; | |
520 } | |
521 | |
522 ComplexMatrix& | |
523 ComplexMatrix::fill (const Complex& val) | |
524 { | |
5275 | 525 octave_idx_type nr = rows (); |
526 octave_idx_type nc = cols (); | |
4316 | 527 |
458 | 528 if (nr > 0 && nc > 0) |
4316 | 529 { |
530 make_unique (); | |
531 | |
5275 | 532 for (octave_idx_type j = 0; j < nc; j++) |
533 for (octave_idx_type i = 0; i < nr; i++) | |
4316 | 534 xelem (i, j) = val; |
535 } | |
458 | 536 |
537 return *this; | |
538 } | |
539 | |
540 ComplexMatrix& | |
5275 | 541 ComplexMatrix::fill (double val, octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) |
458 | 542 { |
5275 | 543 octave_idx_type nr = rows (); |
544 octave_idx_type nc = cols (); | |
4316 | 545 |
458 | 546 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
547 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) | |
548 { | |
549 (*current_liboctave_error_handler) ("range error for fill"); | |
550 return *this; | |
551 } | |
552 | |
5275 | 553 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } |
554 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } | |
458 | 555 |
4316 | 556 if (r2 >= r1 && c2 >= c1) |
557 { | |
558 make_unique (); | |
559 | |
5275 | 560 for (octave_idx_type j = c1; j <= c2; j++) |
561 for (octave_idx_type i = r1; i <= r2; i++) | |
4316 | 562 xelem (i, j) = val; |
563 } | |
458 | 564 |
565 return *this; | |
566 } | |
567 | |
568 ComplexMatrix& | |
5275 | 569 ComplexMatrix::fill (const Complex& val, octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) |
458 | 570 { |
5275 | 571 octave_idx_type nr = rows (); |
572 octave_idx_type nc = cols (); | |
4316 | 573 |
458 | 574 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
575 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) | |
576 { | |
577 (*current_liboctave_error_handler) ("range error for fill"); | |
578 return *this; | |
579 } | |
580 | |
5275 | 581 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } |
582 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } | |
458 | 583 |
4316 | 584 if (r2 >= r1 && c2 >=c1) |
585 { | |
586 make_unique (); | |
587 | |
5275 | 588 for (octave_idx_type j = c1; j <= c2; j++) |
589 for (octave_idx_type i = r1; i <= r2; i++) | |
4316 | 590 xelem (i, j) = val; |
591 } | |
458 | 592 |
593 return *this; | |
594 } | |
595 | |
596 ComplexMatrix | |
597 ComplexMatrix::append (const Matrix& a) const | |
598 { | |
5275 | 599 octave_idx_type nr = rows (); |
600 octave_idx_type nc = cols (); | |
458 | 601 if (nr != a.rows ()) |
602 { | |
603 (*current_liboctave_error_handler) ("row dimension mismatch for append"); | |
604 return *this; | |
605 } | |
606 | |
5275 | 607 octave_idx_type nc_insert = nc; |
458 | 608 ComplexMatrix retval (nr, nc + a.cols ()); |
609 retval.insert (*this, 0, 0); | |
610 retval.insert (a, 0, nc_insert); | |
611 return retval; | |
612 } | |
613 | |
614 ComplexMatrix | |
615 ComplexMatrix::append (const RowVector& a) const | |
616 { | |
5275 | 617 octave_idx_type nr = rows (); |
618 octave_idx_type nc = cols (); | |
458 | 619 if (nr != 1) |
620 { | |
621 (*current_liboctave_error_handler) ("row dimension mismatch for append"); | |
622 return *this; | |
623 } | |
624 | |
5275 | 625 octave_idx_type nc_insert = nc; |
458 | 626 ComplexMatrix retval (nr, nc + a.length ()); |
627 retval.insert (*this, 0, 0); | |
628 retval.insert (a, 0, nc_insert); | |
629 return retval; | |
630 } | |
631 | |
632 ComplexMatrix | |
633 ComplexMatrix::append (const ColumnVector& a) const | |
634 { | |
5275 | 635 octave_idx_type nr = rows (); |
636 octave_idx_type nc = cols (); | |
458 | 637 if (nr != a.length ()) |
638 { | |
639 (*current_liboctave_error_handler) ("row dimension mismatch for append"); | |
640 return *this; | |
641 } | |
642 | |
5275 | 643 octave_idx_type nc_insert = nc; |
458 | 644 ComplexMatrix retval (nr, nc + 1); |
645 retval.insert (*this, 0, 0); | |
646 retval.insert (a, 0, nc_insert); | |
647 return retval; | |
648 } | |
649 | |
650 ComplexMatrix | |
651 ComplexMatrix::append (const DiagMatrix& a) const | |
652 { | |
5275 | 653 octave_idx_type nr = rows (); |
654 octave_idx_type nc = cols (); | |
458 | 655 if (nr != a.rows ()) |
656 { | |
657 (*current_liboctave_error_handler) ("row dimension mismatch for append"); | |
658 return *this; | |
659 } | |
660 | |
5275 | 661 octave_idx_type nc_insert = nc; |
458 | 662 ComplexMatrix retval (nr, nc + a.cols ()); |
663 retval.insert (*this, 0, 0); | |
664 retval.insert (a, 0, nc_insert); | |
665 return retval; | |
666 } | |
667 | |
668 ComplexMatrix | |
669 ComplexMatrix::append (const ComplexMatrix& a) const | |
670 { | |
5275 | 671 octave_idx_type nr = rows (); |
672 octave_idx_type nc = cols (); | |
458 | 673 if (nr != a.rows ()) |
674 { | |
675 (*current_liboctave_error_handler) ("row dimension mismatch for append"); | |
676 return *this; | |
677 } | |
678 | |
5275 | 679 octave_idx_type nc_insert = nc; |
458 | 680 ComplexMatrix retval (nr, nc + a.cols ()); |
681 retval.insert (*this, 0, 0); | |
682 retval.insert (a, 0, nc_insert); | |
683 return retval; | |
684 } | |
685 | |
686 ComplexMatrix | |
687 ComplexMatrix::append (const ComplexRowVector& a) const | |
688 { | |
5275 | 689 octave_idx_type nr = rows (); |
690 octave_idx_type nc = cols (); | |
458 | 691 if (nr != 1) |
692 { | |
693 (*current_liboctave_error_handler) ("row dimension mismatch for append"); | |
694 return *this; | |
695 } | |
696 | |
5275 | 697 octave_idx_type nc_insert = nc; |
458 | 698 ComplexMatrix retval (nr, nc + a.length ()); |
699 retval.insert (*this, 0, 0); | |
700 retval.insert (a, 0, nc_insert); | |
701 return retval; | |
702 } | |
703 | |
704 ComplexMatrix | |
705 ComplexMatrix::append (const ComplexColumnVector& a) const | |
706 { | |
5275 | 707 octave_idx_type nr = rows (); |
708 octave_idx_type nc = cols (); | |
458 | 709 if (nr != a.length ()) |
710 { | |
711 (*current_liboctave_error_handler) ("row dimension mismatch for append"); | |
712 return *this; | |
713 } | |
714 | |
5275 | 715 octave_idx_type nc_insert = nc; |
458 | 716 ComplexMatrix retval (nr, nc + 1); |
717 retval.insert (*this, 0, 0); | |
718 retval.insert (a, 0, nc_insert); | |
719 return retval; | |
720 } | |
721 | |
722 ComplexMatrix | |
723 ComplexMatrix::append (const ComplexDiagMatrix& a) const | |
724 { | |
5275 | 725 octave_idx_type nr = rows (); |
726 octave_idx_type nc = cols (); | |
458 | 727 if (nr != a.rows ()) |
728 { | |
729 (*current_liboctave_error_handler) ("row dimension mismatch for append"); | |
730 return *this; | |
731 } | |
732 | |
5275 | 733 octave_idx_type nc_insert = nc; |
458 | 734 ComplexMatrix retval (nr, nc + a.cols ()); |
735 retval.insert (*this, 0, 0); | |
736 retval.insert (a, 0, nc_insert); | |
737 return retval; | |
738 } | |
739 | |
740 ComplexMatrix | |
741 ComplexMatrix::stack (const Matrix& a) const | |
742 { | |
5275 | 743 octave_idx_type nr = rows (); |
744 octave_idx_type nc = cols (); | |
458 | 745 if (nc != a.cols ()) |
746 { | |
747 (*current_liboctave_error_handler) | |
748 ("column dimension mismatch for stack"); | |
749 return *this; | |
750 } | |
751 | |
5275 | 752 octave_idx_type nr_insert = nr; |
458 | 753 ComplexMatrix retval (nr + a.rows (), nc); |
754 retval.insert (*this, 0, 0); | |
755 retval.insert (a, nr_insert, 0); | |
756 return retval; | |
757 } | |
758 | |
759 ComplexMatrix | |
760 ComplexMatrix::stack (const RowVector& a) const | |
761 { | |
5275 | 762 octave_idx_type nr = rows (); |
763 octave_idx_type nc = cols (); | |
458 | 764 if (nc != a.length ()) |
765 { | |
766 (*current_liboctave_error_handler) | |
767 ("column dimension mismatch for stack"); | |
768 return *this; | |
769 } | |
770 | |
5275 | 771 octave_idx_type nr_insert = nr; |
458 | 772 ComplexMatrix retval (nr + 1, nc); |
773 retval.insert (*this, 0, 0); | |
774 retval.insert (a, nr_insert, 0); | |
775 return retval; | |
776 } | |
777 | |
778 ComplexMatrix | |
779 ComplexMatrix::stack (const ColumnVector& a) const | |
780 { | |
5275 | 781 octave_idx_type nr = rows (); |
782 octave_idx_type nc = cols (); | |
458 | 783 if (nc != 1) |
784 { | |
785 (*current_liboctave_error_handler) | |
786 ("column dimension mismatch for stack"); | |
787 return *this; | |
788 } | |
789 | |
5275 | 790 octave_idx_type nr_insert = nr; |
458 | 791 ComplexMatrix retval (nr + a.length (), nc); |
792 retval.insert (*this, 0, 0); | |
793 retval.insert (a, nr_insert, 0); | |
794 return retval; | |
795 } | |
796 | |
797 ComplexMatrix | |
798 ComplexMatrix::stack (const DiagMatrix& a) const | |
799 { | |
5275 | 800 octave_idx_type nr = rows (); |
801 octave_idx_type nc = cols (); | |
458 | 802 if (nc != a.cols ()) |
803 { | |
804 (*current_liboctave_error_handler) | |
805 ("column dimension mismatch for stack"); | |
806 return *this; | |
807 } | |
808 | |
5275 | 809 octave_idx_type nr_insert = nr; |
458 | 810 ComplexMatrix retval (nr + a.rows (), nc); |
811 retval.insert (*this, 0, 0); | |
812 retval.insert (a, nr_insert, 0); | |
813 return retval; | |
814 } | |
815 | |
816 ComplexMatrix | |
817 ComplexMatrix::stack (const ComplexMatrix& a) const | |
818 { | |
5275 | 819 octave_idx_type nr = rows (); |
820 octave_idx_type nc = cols (); | |
458 | 821 if (nc != a.cols ()) |
822 { | |
823 (*current_liboctave_error_handler) | |
824 ("column dimension mismatch for stack"); | |
825 return *this; | |
826 } | |
827 | |
5275 | 828 octave_idx_type nr_insert = nr; |
458 | 829 ComplexMatrix retval (nr + a.rows (), nc); |
830 retval.insert (*this, 0, 0); | |
831 retval.insert (a, nr_insert, 0); | |
832 return retval; | |
833 } | |
834 | |
835 ComplexMatrix | |
836 ComplexMatrix::stack (const ComplexRowVector& a) const | |
837 { | |
5275 | 838 octave_idx_type nr = rows (); |
839 octave_idx_type nc = cols (); | |
458 | 840 if (nc != a.length ()) |
841 { | |
842 (*current_liboctave_error_handler) | |
843 ("column dimension mismatch for stack"); | |
844 return *this; | |
845 } | |
846 | |
5275 | 847 octave_idx_type nr_insert = nr; |
458 | 848 ComplexMatrix retval (nr + 1, nc); |
849 retval.insert (*this, 0, 0); | |
850 retval.insert (a, nr_insert, 0); | |
851 return retval; | |
852 } | |
853 | |
854 ComplexMatrix | |
855 ComplexMatrix::stack (const ComplexColumnVector& a) const | |
856 { | |
5275 | 857 octave_idx_type nr = rows (); |
858 octave_idx_type nc = cols (); | |
458 | 859 if (nc != 1) |
860 { | |
861 (*current_liboctave_error_handler) | |
862 ("column dimension mismatch for stack"); | |
863 return *this; | |
864 } | |
865 | |
5275 | 866 octave_idx_type nr_insert = nr; |
458 | 867 ComplexMatrix retval (nr + a.length (), nc); |
868 retval.insert (*this, 0, 0); | |
869 retval.insert (a, nr_insert, 0); | |
870 return retval; | |
871 } | |
872 | |
873 ComplexMatrix | |
874 ComplexMatrix::stack (const ComplexDiagMatrix& a) const | |
875 { | |
5275 | 876 octave_idx_type nr = rows (); |
877 octave_idx_type nc = cols (); | |
458 | 878 if (nc != a.cols ()) |
879 { | |
880 (*current_liboctave_error_handler) | |
881 ("column dimension mismatch for stack"); | |
882 return *this; | |
883 } | |
884 | |
5275 | 885 octave_idx_type nr_insert = nr; |
458 | 886 ComplexMatrix retval (nr + a.rows (), nc); |
887 retval.insert (*this, 0, 0); | |
888 retval.insert (a, nr_insert, 0); | |
889 return retval; | |
890 } | |
891 | |
892 ComplexMatrix | |
893 conj (const ComplexMatrix& a) | |
894 { | |
8650
a1ae2aae903e
abs,real,imag,conj: use code from mx-inlines rather than the generic map
Jaroslav Hajek <highegg@gmail.com>
parents:
8614
diff
changeset
|
895 return ComplexMatrix (mx_inline_conj_dup (a.data (), a.length ()), |
a1ae2aae903e
abs,real,imag,conj: use code from mx-inlines rather than the generic map
Jaroslav Hajek <highegg@gmail.com>
parents:
8614
diff
changeset
|
896 a.rows (), a.cols ()); |
458 | 897 } |
898 | |
899 // resize is the destructive equivalent for this one | |
900 | |
901 ComplexMatrix | |
5275 | 902 ComplexMatrix::extract (octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const |
458 | 903 { |
5275 | 904 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } |
905 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } | |
906 | |
907 octave_idx_type new_r = r2 - r1 + 1; | |
908 octave_idx_type new_c = c2 - c1 + 1; | |
458 | 909 |
910 ComplexMatrix result (new_r, new_c); | |
911 | |
5275 | 912 for (octave_idx_type j = 0; j < new_c; j++) |
913 for (octave_idx_type i = 0; i < new_r; i++) | |
4316 | 914 result.xelem (i, j) = elem (r1+i, c1+j); |
915 | |
916 return result; | |
917 } | |
918 | |
919 ComplexMatrix | |
5275 | 920 ComplexMatrix::extract_n (octave_idx_type r1, octave_idx_type c1, octave_idx_type nr, octave_idx_type nc) const |
4316 | 921 { |
922 ComplexMatrix result (nr, nc); | |
923 | |
5275 | 924 for (octave_idx_type j = 0; j < nc; j++) |
925 for (octave_idx_type i = 0; i < nr; i++) | |
4316 | 926 result.xelem (i, j) = elem (r1+i, c1+j); |
458 | 927 |
928 return result; | |
929 } | |
930 | |
931 // extract row or column i. | |
932 | |
933 ComplexRowVector | |
5275 | 934 ComplexMatrix::row (octave_idx_type i) const |
458 | 935 { |
8614
5114ea5a41b5
use shallow copying in Matrix/RowVector/ColumnVector conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8392
diff
changeset
|
936 return MArray<Complex> (index (idx_vector (i), idx_vector::colon)); |
458 | 937 } |
938 | |
939 ComplexColumnVector | |
5275 | 940 ComplexMatrix::column (octave_idx_type i) const |
458 | 941 { |
8614
5114ea5a41b5
use shallow copying in Matrix/RowVector/ColumnVector conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8392
diff
changeset
|
942 return MArray<Complex> (index (idx_vector::colon, idx_vector (i))); |
458 | 943 } |
944 | |
945 ComplexMatrix | |
946 ComplexMatrix::inverse (void) const | |
947 { | |
5275 | 948 octave_idx_type info; |
7788 | 949 double rcon; |
6207 | 950 MatrixType mattype (*this); |
7788 | 951 return inverse (mattype, info, rcon, 0, 0); |
6207 | 952 } |
953 | |
954 ComplexMatrix | |
6479 | 955 ComplexMatrix::inverse (octave_idx_type& info) const |
956 { | |
7788 | 957 double rcon; |
6479 | 958 MatrixType mattype (*this); |
7788 | 959 return inverse (mattype, info, rcon, 0, 0); |
6479 | 960 } |
961 | |
962 ComplexMatrix | |
7788 | 963 ComplexMatrix::inverse (octave_idx_type& info, double& rcon, int force, |
6479 | 964 int calc_cond) const |
965 { | |
966 MatrixType mattype (*this); | |
7788 | 967 return inverse (mattype, info, rcon, force, calc_cond); |
6479 | 968 } |
969 | |
970 ComplexMatrix | |
6207 | 971 ComplexMatrix::inverse (MatrixType &mattype) const |
972 { | |
973 octave_idx_type info; | |
7788 | 974 double rcon; |
975 return inverse (mattype, info, rcon, 0, 0); | |
6207 | 976 } |
977 | |
978 ComplexMatrix | |
979 ComplexMatrix::inverse (MatrixType &mattype, octave_idx_type& info) const | |
980 { | |
7788 | 981 double rcon; |
982 return inverse (mattype, info, rcon, 0, 0); | |
458 | 983 } |
984 | |
985 ComplexMatrix | |
6207 | 986 ComplexMatrix::tinverse (MatrixType &mattype, octave_idx_type& info, |
7788 | 987 double& rcon, int force, int calc_cond) const |
458 | 988 { |
6207 | 989 ComplexMatrix retval; |
990 | |
991 octave_idx_type nr = rows (); | |
992 octave_idx_type nc = cols (); | |
993 | |
994 if (nr != nc || nr == 0 || nc == 0) | |
995 (*current_liboctave_error_handler) ("inverse requires square matrix"); | |
996 else | |
997 { | |
998 int typ = mattype.type (); | |
999 char uplo = (typ == MatrixType::Lower ? 'L' : 'U'); | |
1000 char udiag = 'N'; | |
1001 retval = *this; | |
1002 Complex *tmp_data = retval.fortran_vec (); | |
1003 | |
1004 F77_XFCN (ztrtri, ZTRTRI, (F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1005 F77_CONST_CHAR_ARG2 (&udiag, 1), | |
1006 nr, tmp_data, nr, info | |
1007 F77_CHAR_ARG_LEN (1) | |
1008 F77_CHAR_ARG_LEN (1))); | |
1009 | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1010 // Throw-away extra info LAPACK gives so as to not change output. |
7788 | 1011 rcon = 0.0; |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1012 if (info != 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1013 info = -1; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1014 else if (calc_cond) |
6207 | 1015 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1016 octave_idx_type ztrcon_info = 0; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1017 char job = '1'; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1018 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1019 OCTAVE_LOCAL_BUFFER (Complex, cwork, 2*nr); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1020 OCTAVE_LOCAL_BUFFER (double, rwork, nr); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1021 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1022 F77_XFCN (ztrcon, ZTRCON, (F77_CONST_CHAR_ARG2 (&job, 1), |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1023 F77_CONST_CHAR_ARG2 (&uplo, 1), |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1024 F77_CONST_CHAR_ARG2 (&udiag, 1), |
7788 | 1025 nr, tmp_data, nr, rcon, |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1026 cwork, rwork, ztrcon_info |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1027 F77_CHAR_ARG_LEN (1) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1028 F77_CHAR_ARG_LEN (1) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1029 F77_CHAR_ARG_LEN (1))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1030 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1031 if (ztrcon_info != 0) |
6207 | 1032 info = -1; |
1033 } | |
1034 | |
1035 if (info == -1 && ! force) | |
1036 retval = *this; // Restore matrix contents. | |
1037 } | |
1038 | |
1039 return retval; | |
458 | 1040 } |
1041 | |
1042 ComplexMatrix | |
6207 | 1043 ComplexMatrix::finverse (MatrixType &mattype, octave_idx_type& info, |
7788 | 1044 double& rcon, int force, int calc_cond) const |
458 | 1045 { |
1948 | 1046 ComplexMatrix retval; |
1047 | |
5275 | 1048 octave_idx_type nr = rows (); |
1049 octave_idx_type nc = cols (); | |
1948 | 1050 |
458 | 1051 if (nr != nc) |
1948 | 1052 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
458 | 1053 else |
1054 { | |
5275 | 1055 Array<octave_idx_type> ipvt (nr); |
1056 octave_idx_type *pipvt = ipvt.fortran_vec (); | |
1948 | 1057 |
1058 retval = *this; | |
1059 Complex *tmp_data = retval.fortran_vec (); | |
1060 | |
4329 | 1061 Array<Complex> z(1); |
5275 | 1062 octave_idx_type lwork = -1; |
4330 | 1063 |
1064 // Query the optimum work array size. | |
4329 | 1065 |
1066 F77_XFCN (zgetri, ZGETRI, (nc, tmp_data, nr, pipvt, | |
1067 z.fortran_vec (), lwork, info)); | |
1068 | |
5315 | 1069 lwork = static_cast<octave_idx_type> (std::real(z(0))); |
4329 | 1070 lwork = (lwork < 2 *nc ? 2*nc : lwork); |
1071 z.resize (lwork); | |
1072 Complex *pz = z.fortran_vec (); | |
1073 | |
1074 info = 0; | |
1075 | |
4330 | 1076 // Calculate the norm of the matrix, for later use. |
4329 | 1077 double anorm; |
1078 if (calc_cond) | |
5275 | 1079 anorm = retval.abs().sum().row(static_cast<octave_idx_type>(0)).max(); |
4329 | 1080 |
1081 F77_XFCN (zgetrf, ZGETRF, (nc, nc, tmp_data, nr, pipvt, info)); | |
1948 | 1082 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1083 // Throw-away extra info LAPACK gives so as to not change output. |
7788 | 1084 rcon = 0.0; |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1085 if (info != 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1086 info = -1; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1087 else if (calc_cond) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1088 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1089 // Now calculate the condition number for non-singular matrix. |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1090 octave_idx_type zgecon_info = 0; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1091 char job = '1'; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1092 Array<double> rz (2 * nc); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1093 double *prz = rz.fortran_vec (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1094 F77_XFCN (zgecon, ZGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1095 nc, tmp_data, nr, anorm, |
7788 | 1096 rcon, pz, prz, zgecon_info |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1097 F77_CHAR_ARG_LEN (1))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1098 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1099 if (zgecon_info != 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1100 info = -1; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1101 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1102 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1103 if (info == -1 && ! force) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1104 retval = *this; // Restore contents. |
1948 | 1105 else |
1106 { | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1107 octave_idx_type zgetri_info = 0; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1108 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1109 F77_XFCN (zgetri, ZGETRI, (nc, tmp_data, nr, pipvt, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1110 pz, lwork, zgetri_info)); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1111 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1112 if (zgetri_info != 0) |
1948 | 1113 info = -1; |
1114 } | |
6207 | 1115 |
1116 if (info != 0) | |
1117 mattype.mark_as_rectangular(); | |
458 | 1118 } |
4329 | 1119 |
1948 | 1120 return retval; |
458 | 1121 } |
1122 | |
1123 ComplexMatrix | |
6207 | 1124 ComplexMatrix::inverse (MatrixType &mattype, octave_idx_type& info, |
7788 | 1125 double& rcon, int force, int calc_cond) const |
6207 | 1126 { |
1127 int typ = mattype.type (false); | |
1128 ComplexMatrix ret; | |
1129 | |
1130 if (typ == MatrixType::Unknown) | |
1131 typ = mattype.type (*this); | |
1132 | |
1133 if (typ == MatrixType::Upper || typ == MatrixType::Lower) | |
7788 | 1134 ret = tinverse (mattype, info, rcon, force, calc_cond); |
6840 | 1135 else |
6207 | 1136 { |
1137 if (mattype.is_hermitian ()) | |
1138 { | |
6486 | 1139 ComplexCHOL chol (*this, info, calc_cond); |
6207 | 1140 if (info == 0) |
6486 | 1141 { |
1142 if (calc_cond) | |
7788 | 1143 rcon = chol.rcond(); |
6486 | 1144 else |
7788 | 1145 rcon = 1.0; |
6486 | 1146 ret = chol.inverse (); |
1147 } | |
6207 | 1148 else |
1149 mattype.mark_as_unsymmetric (); | |
1150 } | |
1151 | |
1152 if (!mattype.is_hermitian ()) | |
7788 | 1153 ret = finverse(mattype, info, rcon, force, calc_cond); |
1154 | |
1155 if ((mattype.is_hermitian () || calc_cond) && rcon == 0.) | |
6840 | 1156 ret = ComplexMatrix (rows (), columns (), Complex (octave_Inf, 0.)); |
6207 | 1157 } |
1158 | |
1159 return ret; | |
1160 } | |
1161 | |
1162 ComplexMatrix | |
4384 | 1163 ComplexMatrix::pseudo_inverse (double tol) const |
740 | 1164 { |
1549 | 1165 ComplexMatrix retval; |
1166 | |
3480 | 1167 ComplexSVD result (*this, SVD::economy); |
740 | 1168 |
1169 DiagMatrix S = result.singular_values (); | |
1170 ComplexMatrix U = result.left_singular_matrix (); | |
1171 ComplexMatrix V = result.right_singular_matrix (); | |
1172 | |
1173 ColumnVector sigma = S.diag (); | |
1174 | |
5275 | 1175 octave_idx_type r = sigma.length () - 1; |
1176 octave_idx_type nr = rows (); | |
1177 octave_idx_type nc = cols (); | |
740 | 1178 |
1179 if (tol <= 0.0) | |
1180 { | |
1181 if (nr > nc) | |
1182 tol = nr * sigma.elem (0) * DBL_EPSILON; | |
1183 else | |
1184 tol = nc * sigma.elem (0) * DBL_EPSILON; | |
1185 } | |
1186 | |
1187 while (r >= 0 && sigma.elem (r) < tol) | |
1188 r--; | |
1189 | |
1190 if (r < 0) | |
1549 | 1191 retval = ComplexMatrix (nc, nr, 0.0); |
740 | 1192 else |
1193 { | |
1194 ComplexMatrix Ur = U.extract (0, 0, nr-1, r); | |
1195 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); | |
1196 ComplexMatrix Vr = V.extract (0, 0, nc-1, r); | |
1549 | 1197 retval = Vr * D * Ur.hermitian (); |
740 | 1198 } |
1549 | 1199 |
1200 return retval; | |
740 | 1201 } |
1202 | |
9523
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1203 #if defined (HAVE_FFTW) |
3827 | 1204 |
1205 ComplexMatrix | |
1206 ComplexMatrix::fourier (void) const | |
1207 { | |
1208 size_t nr = rows (); | |
1209 size_t nc = cols (); | |
1210 | |
1211 ComplexMatrix retval (nr, nc); | |
1212 | |
1213 size_t npts, nsamples; | |
1214 | |
1215 if (nr == 1 || nc == 1) | |
1216 { | |
1217 npts = nr > nc ? nr : nc; | |
1218 nsamples = 1; | |
1219 } | |
1220 else | |
1221 { | |
1222 npts = nr; | |
1223 nsamples = nc; | |
1224 } | |
1225 | |
1226 const Complex *in (data ()); | |
1227 Complex *out (retval.fortran_vec ()); | |
1228 | |
4773 | 1229 octave_fftw::fft (in, out, npts, nsamples); |
3827 | 1230 |
1231 return retval; | |
1232 } | |
1233 | |
1234 ComplexMatrix | |
1235 ComplexMatrix::ifourier (void) const | |
1236 { | |
1237 size_t nr = rows (); | |
1238 size_t nc = cols (); | |
1239 | |
1240 ComplexMatrix retval (nr, nc); | |
1241 | |
1242 size_t npts, nsamples; | |
1243 | |
1244 if (nr == 1 || nc == 1) | |
1245 { | |
1246 npts = nr > nc ? nr : nc; | |
1247 nsamples = 1; | |
1248 } | |
1249 else | |
1250 { | |
1251 npts = nr; | |
1252 nsamples = nc; | |
1253 } | |
1254 | |
1255 const Complex *in (data ()); | |
1256 Complex *out (retval.fortran_vec ()); | |
1257 | |
4773 | 1258 octave_fftw::ifft (in, out, npts, nsamples); |
3827 | 1259 |
1260 return retval; | |
1261 } | |
1262 | |
1263 ComplexMatrix | |
1264 ComplexMatrix::fourier2d (void) const | |
1265 { | |
4773 | 1266 dim_vector dv(rows (), cols ()); |
1267 | |
1268 ComplexMatrix retval (rows (), cols ()); | |
1269 const Complex *in (data ()); | |
1270 Complex *out (retval.fortran_vec ()); | |
1271 | |
1272 octave_fftw::fftNd (in, out, 2, dv); | |
3827 | 1273 |
1274 return retval; | |
1275 } | |
1276 | |
1277 ComplexMatrix | |
1278 ComplexMatrix::ifourier2d (void) const | |
1279 { | |
4773 | 1280 dim_vector dv(rows (), cols ()); |
1281 | |
1282 ComplexMatrix retval (rows (), cols ()); | |
1283 const Complex *in (data ()); | |
1284 Complex *out (retval.fortran_vec ()); | |
1285 | |
1286 octave_fftw::ifftNd (in, out, 2, dv); | |
3827 | 1287 |
1288 return retval; | |
1289 } | |
1290 | |
1291 #else | |
1292 | |
9523
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1293 extern "C" |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1294 { |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1295 // Note that the original complex fft routines were not written for |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1296 // double complex arguments. They have been modified by adding an |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1297 // implicit double precision (a-h,o-z) statement at the beginning of |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1298 // each subroutine. |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1299 |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1300 F77_RET_T |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1301 F77_FUNC (zffti, ZFFTI) (const octave_idx_type&, Complex*); |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1302 |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1303 F77_RET_T |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1304 F77_FUNC (zfftf, ZFFTF) (const octave_idx_type&, Complex*, Complex*); |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1305 |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1306 F77_RET_T |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1307 F77_FUNC (zfftb, ZFFTB) (const octave_idx_type&, Complex*, Complex*); |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1308 } |
0ce82753dd72
more configure changes for libraries
John W. Eaton <jwe@octave.org>
parents:
9469
diff
changeset
|
1309 |
740 | 1310 ComplexMatrix |
458 | 1311 ComplexMatrix::fourier (void) const |
1312 { | |
1948 | 1313 ComplexMatrix retval; |
1314 | |
5275 | 1315 octave_idx_type nr = rows (); |
1316 octave_idx_type nc = cols (); | |
1317 | |
1318 octave_idx_type npts, nsamples; | |
1948 | 1319 |
458 | 1320 if (nr == 1 || nc == 1) |
1321 { | |
1322 npts = nr > nc ? nr : nc; | |
1323 nsamples = 1; | |
1324 } | |
1325 else | |
1326 { | |
1327 npts = nr; | |
1328 nsamples = nc; | |
1329 } | |
1330 | |
5275 | 1331 octave_idx_type nn = 4*npts+15; |
1948 | 1332 |
1333 Array<Complex> wsave (nn); | |
1334 Complex *pwsave = wsave.fortran_vec (); | |
1335 | |
1336 retval = *this; | |
1337 Complex *tmp_data = retval.fortran_vec (); | |
1338 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7788
diff
changeset
|
1339 F77_FUNC (zffti, ZFFTI) (npts, pwsave); |
458 | 1340 |
5275 | 1341 for (octave_idx_type j = 0; j < nsamples; j++) |
4153 | 1342 { |
1343 OCTAVE_QUIT; | |
1344 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7788
diff
changeset
|
1345 F77_FUNC (zfftf, ZFFTF) (npts, &tmp_data[npts*j], pwsave); |
4153 | 1346 } |
1948 | 1347 |
1348 return retval; | |
458 | 1349 } |
1350 | |
1351 ComplexMatrix | |
1352 ComplexMatrix::ifourier (void) const | |
1353 { | |
1948 | 1354 ComplexMatrix retval; |
1355 | |
5275 | 1356 octave_idx_type nr = rows (); |
1357 octave_idx_type nc = cols (); | |
1358 | |
1359 octave_idx_type npts, nsamples; | |
1948 | 1360 |
458 | 1361 if (nr == 1 || nc == 1) |
1362 { | |
1363 npts = nr > nc ? nr : nc; | |
1364 nsamples = 1; | |
1365 } | |
1366 else | |
1367 { | |
1368 npts = nr; | |
1369 nsamples = nc; | |
1370 } | |
1371 | |
5275 | 1372 octave_idx_type nn = 4*npts+15; |
1948 | 1373 |
1374 Array<Complex> wsave (nn); | |
1375 Complex *pwsave = wsave.fortran_vec (); | |
1376 | |
1377 retval = *this; | |
1378 Complex *tmp_data = retval.fortran_vec (); | |
1379 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7788
diff
changeset
|
1380 F77_FUNC (zffti, ZFFTI) (npts, pwsave); |
458 | 1381 |
5275 | 1382 for (octave_idx_type j = 0; j < nsamples; j++) |
4153 | 1383 { |
1384 OCTAVE_QUIT; | |
1385 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7788
diff
changeset
|
1386 F77_FUNC (zfftb, ZFFTB) (npts, &tmp_data[npts*j], pwsave); |
4153 | 1387 } |
458 | 1388 |
5275 | 1389 for (octave_idx_type j = 0; j < npts*nsamples; j++) |
3572 | 1390 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
458 | 1391 |
1948 | 1392 return retval; |
458 | 1393 } |
1394 | |
677 | 1395 ComplexMatrix |
1396 ComplexMatrix::fourier2d (void) const | |
1397 { | |
1948 | 1398 ComplexMatrix retval; |
1399 | |
5275 | 1400 octave_idx_type nr = rows (); |
1401 octave_idx_type nc = cols (); | |
1402 | |
1403 octave_idx_type npts, nsamples; | |
1948 | 1404 |
677 | 1405 if (nr == 1 || nc == 1) |
1406 { | |
1407 npts = nr > nc ? nr : nc; | |
1408 nsamples = 1; | |
1409 } | |
1410 else | |
1411 { | |
1412 npts = nr; | |
1413 nsamples = nc; | |
1414 } | |
1415 | |
5275 | 1416 octave_idx_type nn = 4*npts+15; |
1948 | 1417 |
1418 Array<Complex> wsave (nn); | |
1419 Complex *pwsave = wsave.fortran_vec (); | |
1420 | |
1421 retval = *this; | |
1422 Complex *tmp_data = retval.fortran_vec (); | |
1423 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7788
diff
changeset
|
1424 F77_FUNC (zffti, ZFFTI) (npts, pwsave); |
677 | 1425 |
5275 | 1426 for (octave_idx_type j = 0; j < nsamples; j++) |
4153 | 1427 { |
1428 OCTAVE_QUIT; | |
1429 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7788
diff
changeset
|
1430 F77_FUNC (zfftf, ZFFTF) (npts, &tmp_data[npts*j], pwsave); |
4153 | 1431 } |
677 | 1432 |
1433 npts = nc; | |
1434 nsamples = nr; | |
1435 nn = 4*npts+15; | |
1948 | 1436 |
1437 wsave.resize (nn); | |
1438 pwsave = wsave.fortran_vec (); | |
1439 | |
4773 | 1440 Array<Complex> tmp (npts); |
1441 Complex *prow = tmp.fortran_vec (); | |
1948 | 1442 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7788
diff
changeset
|
1443 F77_FUNC (zffti, ZFFTI) (npts, pwsave); |
677 | 1444 |
5275 | 1445 for (octave_idx_type j = 0; j < nsamples; j++) |
677 | 1446 { |
4153 | 1447 OCTAVE_QUIT; |
1448 | |
5275 | 1449 for (octave_idx_type i = 0; i < npts; i++) |
1948 | 1450 prow[i] = tmp_data[i*nr + j]; |
1451 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7788
diff
changeset
|
1452 F77_FUNC (zfftf, ZFFTF) (npts, prow, pwsave); |
677 | 1453 |
5275 | 1454 for (octave_idx_type i = 0; i < npts; i++) |
1948 | 1455 tmp_data[i*nr + j] = prow[i]; |
677 | 1456 } |
1457 | |
1948 | 1458 return retval; |
677 | 1459 } |
1460 | |
1461 ComplexMatrix | |
1462 ComplexMatrix::ifourier2d (void) const | |
1463 { | |
1948 | 1464 ComplexMatrix retval; |
1465 | |
5275 | 1466 octave_idx_type nr = rows (); |
1467 octave_idx_type nc = cols (); | |
1468 | |
1469 octave_idx_type npts, nsamples; | |
1948 | 1470 |
677 | 1471 if (nr == 1 || nc == 1) |
1472 { | |
1473 npts = nr > nc ? nr : nc; | |
1474 nsamples = 1; | |
1475 } | |
1476 else | |
1477 { | |
1478 npts = nr; | |
1479 nsamples = nc; | |
1480 } | |
1481 | |
5275 | 1482 octave_idx_type nn = 4*npts+15; |
1948 | 1483 |
1484 Array<Complex> wsave (nn); | |
1485 Complex *pwsave = wsave.fortran_vec (); | |
1486 | |
1487 retval = *this; | |
1488 Complex *tmp_data = retval.fortran_vec (); | |
1489 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7788
diff
changeset
|
1490 F77_FUNC (zffti, ZFFTI) (npts, pwsave); |
677 | 1491 |
5275 | 1492 for (octave_idx_type j = 0; j < nsamples; j++) |
4153 | 1493 { |
1494 OCTAVE_QUIT; | |
1495 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7788
diff
changeset
|
1496 F77_FUNC (zfftb, ZFFTB) (npts, &tmp_data[npts*j], pwsave); |
4153 | 1497 } |
677 | 1498 |
5275 | 1499 for (octave_idx_type j = 0; j < npts*nsamples; j++) |
3572 | 1500 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
677 | 1501 |
1502 npts = nc; | |
1503 nsamples = nr; | |
1504 nn = 4*npts+15; | |
1948 | 1505 |
1506 wsave.resize (nn); | |
1507 pwsave = wsave.fortran_vec (); | |
1508 | |
4773 | 1509 Array<Complex> tmp (npts); |
1510 Complex *prow = tmp.fortran_vec (); | |
1948 | 1511 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7788
diff
changeset
|
1512 F77_FUNC (zffti, ZFFTI) (npts, pwsave); |
677 | 1513 |
5275 | 1514 for (octave_idx_type j = 0; j < nsamples; j++) |
677 | 1515 { |
4153 | 1516 OCTAVE_QUIT; |
1517 | |
5275 | 1518 for (octave_idx_type i = 0; i < npts; i++) |
1948 | 1519 prow[i] = tmp_data[i*nr + j]; |
1520 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7788
diff
changeset
|
1521 F77_FUNC (zfftb, ZFFTB) (npts, prow, pwsave); |
677 | 1522 |
5275 | 1523 for (octave_idx_type i = 0; i < npts; i++) |
3572 | 1524 tmp_data[i*nr + j] = prow[i] / static_cast<double> (npts); |
677 | 1525 } |
1526 | |
1948 | 1527 return retval; |
677 | 1528 } |
1529 | |
3827 | 1530 #endif |
1531 | |
458 | 1532 ComplexDET |
1533 ComplexMatrix::determinant (void) const | |
1534 { | |
5275 | 1535 octave_idx_type info; |
7788 | 1536 double rcon; |
1537 return determinant (info, rcon, 0); | |
458 | 1538 } |
1539 | |
1540 ComplexDET | |
5275 | 1541 ComplexMatrix::determinant (octave_idx_type& info) const |
458 | 1542 { |
7788 | 1543 double rcon; |
1544 return determinant (info, rcon, 0); | |
458 | 1545 } |
1546 | |
1547 ComplexDET | |
7788 | 1548 ComplexMatrix::determinant (octave_idx_type& info, double& rcon, int calc_cond) const |
458 | 1549 { |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1550 MatrixType mattype (*this); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1551 return determinant (mattype, info, rcon, calc_cond); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1552 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1553 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1554 ComplexDET |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1555 ComplexMatrix::determinant (MatrixType& mattype, |
8806 | 1556 octave_idx_type& info, double& rcon, |
1557 int calc_cond) const | |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1558 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1559 ComplexDET retval (1.0); |
458 | 1560 |
5275 | 1561 octave_idx_type nr = rows (); |
1562 octave_idx_type nc = cols (); | |
458 | 1563 |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1564 if (nr != nc) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1565 (*current_liboctave_error_handler) ("matrix must be square"); |
458 | 1566 else |
1567 { | |
8806 | 1568 volatile int typ = mattype.type (); |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1569 |
8337
e02242c54c49
reuse matrix type detected in det
Jaroslav Hajek <highegg@gmail.com>
parents:
8336
diff
changeset
|
1570 if (typ == MatrixType::Unknown) |
e02242c54c49
reuse matrix type detected in det
Jaroslav Hajek <highegg@gmail.com>
parents:
8336
diff
changeset
|
1571 typ = mattype.type (*this); |
e02242c54c49
reuse matrix type detected in det
Jaroslav Hajek <highegg@gmail.com>
parents:
8336
diff
changeset
|
1572 |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1573 if (typ == MatrixType::Lower || typ == MatrixType::Upper) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1574 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1575 for (octave_idx_type i = 0; i < nc; i++) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1576 retval *= elem (i,i); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1577 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1578 else if (typ == MatrixType::Hermitian) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1579 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1580 ComplexMatrix atmp = *this; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1581 Complex *tmp_data = atmp.fortran_vec (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1582 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1583 info = 0; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1584 double anorm = 0; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1585 if (calc_cond) anorm = xnorm (*this, 1); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1586 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1587 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1588 char job = 'L'; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1589 F77_XFCN (zpotrf, ZPOTRF, (F77_CONST_CHAR_ARG2 (&job, 1), nr, |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1590 tmp_data, nr, info |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1591 F77_CHAR_ARG_LEN (1))); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1592 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1593 if (info != 0) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1594 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1595 rcon = 0.0; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1596 mattype.mark_as_unsymmetric (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1597 typ = MatrixType::Full; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1598 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1599 else |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1600 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1601 Array<Complex> z (2 * nc); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1602 Complex *pz = z.fortran_vec (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1603 Array<double> rz (nc); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1604 double *prz = rz.fortran_vec (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1605 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1606 F77_XFCN (zpocon, ZPOCON, (F77_CONST_CHAR_ARG2 (&job, 1), |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1607 nr, tmp_data, nr, anorm, |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1608 rcon, pz, prz, info |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1609 F77_CHAR_ARG_LEN (1))); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1610 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1611 if (info != 0) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1612 rcon = 0.0; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1613 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1614 for (octave_idx_type i = 0; i < nc; i++) |
8337
e02242c54c49
reuse matrix type detected in det
Jaroslav Hajek <highegg@gmail.com>
parents:
8336
diff
changeset
|
1615 retval *= atmp (i,i); |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1616 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1617 retval = retval.square (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1618 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1619 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1620 else if (typ != MatrixType::Full) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1621 (*current_liboctave_error_handler) ("det: invalid dense matrix type"); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1622 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1623 if (typ == MatrixType::Full) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1624 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1625 Array<octave_idx_type> ipvt (nr); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1626 octave_idx_type *pipvt = ipvt.fortran_vec (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1627 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1628 ComplexMatrix atmp = *this; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1629 Complex *tmp_data = atmp.fortran_vec (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1630 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1631 info = 0; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1632 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1633 // Calculate the norm of the matrix, for later use. |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1634 double anorm = 0; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1635 if (calc_cond) anorm = xnorm (*this, 1); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1636 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1637 F77_XFCN (zgetrf, ZGETRF, (nr, nr, tmp_data, nr, pipvt, info)); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1638 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1639 // Throw-away extra info LAPACK gives so as to not change output. |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1640 rcon = 0.0; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1641 if (info != 0) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1642 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1643 info = -1; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1644 retval = ComplexDET (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1645 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1646 else |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1647 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1648 if (calc_cond) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1649 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1650 // Now calc the condition number for non-singular matrix. |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1651 char job = '1'; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1652 Array<Complex> z (2 * nc); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1653 Complex *pz = z.fortran_vec (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1654 Array<double> rz (2 * nc); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1655 double *prz = rz.fortran_vec (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1656 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1657 F77_XFCN (zgecon, ZGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1658 nc, tmp_data, nr, anorm, |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1659 rcon, pz, prz, info |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1660 F77_CHAR_ARG_LEN (1))); |
8335 | 1661 } |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1662 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1663 if (info != 0) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1664 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1665 info = -1; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1666 retval = ComplexDET (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1667 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1668 else |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1669 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1670 for (octave_idx_type i = 0; i < nc; i++) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1671 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1672 Complex c = atmp(i,i); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1673 retval *= (ipvt(i) != (i+1)) ? -c : c; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1674 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1675 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1676 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1677 } |
458 | 1678 } |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1679 |
458 | 1680 return retval; |
1681 } | |
1682 | |
7788 | 1683 double |
1684 ComplexMatrix::rcond (void) const | |
1685 { | |
1686 MatrixType mattype (*this); | |
1687 return rcond (mattype); | |
1688 } | |
1689 | |
1690 double | |
1691 ComplexMatrix::rcond (MatrixType &mattype) const | |
1692 { | |
1693 double rcon; | |
1694 octave_idx_type nr = rows (); | |
1695 octave_idx_type nc = cols (); | |
1696 | |
1697 if (nr != nc) | |
1698 (*current_liboctave_error_handler) ("matrix must be square"); | |
1699 else if (nr == 0 || nc == 0) | |
1700 rcon = octave_Inf; | |
1701 else | |
1702 { | |
1703 int typ = mattype.type (); | |
1704 | |
1705 if (typ == MatrixType::Unknown) | |
1706 typ = mattype.type (*this); | |
1707 | |
1708 // Only calculate the condition number for LU/Cholesky | |
1709 if (typ == MatrixType::Upper) | |
1710 { | |
1711 const Complex *tmp_data = fortran_vec (); | |
1712 octave_idx_type info = 0; | |
1713 char norm = '1'; | |
1714 char uplo = 'U'; | |
1715 char dia = 'N'; | |
1716 | |
1717 Array<Complex> z (2 * nc); | |
1718 Complex *pz = z.fortran_vec (); | |
1719 Array<double> rz (nc); | |
1720 double *prz = rz.fortran_vec (); | |
1721 | |
1722 F77_XFCN (ztrcon, ZTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), | |
1723 F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1724 F77_CONST_CHAR_ARG2 (&dia, 1), | |
1725 nr, tmp_data, nr, rcon, | |
1726 pz, prz, info | |
1727 F77_CHAR_ARG_LEN (1) | |
1728 F77_CHAR_ARG_LEN (1) | |
1729 F77_CHAR_ARG_LEN (1))); | |
1730 | |
1731 if (info != 0) | |
1732 rcon = 0; | |
1733 } | |
1734 else if (typ == MatrixType::Permuted_Upper) | |
1735 (*current_liboctave_error_handler) | |
1736 ("permuted triangular matrix not implemented"); | |
1737 else if (typ == MatrixType::Lower) | |
1738 { | |
1739 const Complex *tmp_data = fortran_vec (); | |
1740 octave_idx_type info = 0; | |
1741 char norm = '1'; | |
1742 char uplo = 'L'; | |
1743 char dia = 'N'; | |
1744 | |
1745 Array<Complex> z (2 * nc); | |
1746 Complex *pz = z.fortran_vec (); | |
1747 Array<double> rz (nc); | |
1748 double *prz = rz.fortran_vec (); | |
1749 | |
1750 F77_XFCN (ztrcon, ZTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), | |
1751 F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1752 F77_CONST_CHAR_ARG2 (&dia, 1), | |
1753 nr, tmp_data, nr, rcon, | |
1754 pz, prz, info | |
1755 F77_CHAR_ARG_LEN (1) | |
1756 F77_CHAR_ARG_LEN (1) | |
1757 F77_CHAR_ARG_LEN (1))); | |
1758 | |
1759 if (info != 0) | |
1760 rcon = 0.0; | |
1761 } | |
1762 else if (typ == MatrixType::Permuted_Lower) | |
1763 (*current_liboctave_error_handler) | |
1764 ("permuted triangular matrix not implemented"); | |
1765 else if (typ == MatrixType::Full || typ == MatrixType::Hermitian) | |
1766 { | |
1767 double anorm = -1.0; | |
1768 ComplexMatrix atmp = *this; | |
1769 Complex *tmp_data = atmp.fortran_vec (); | |
1770 | |
1771 if (typ == MatrixType::Hermitian) | |
1772 { | |
1773 octave_idx_type info = 0; | |
1774 char job = 'L'; | |
1775 anorm = atmp.abs().sum(). | |
1776 row(static_cast<octave_idx_type>(0)).max(); | |
1777 | |
1778 F77_XFCN (zpotrf, ZPOTRF, (F77_CONST_CHAR_ARG2 (&job, 1), nr, | |
1779 tmp_data, nr, info | |
1780 F77_CHAR_ARG_LEN (1))); | |
1781 | |
1782 if (info != 0) | |
1783 { | |
1784 rcon = 0.0; | |
1785 | |
1786 mattype.mark_as_unsymmetric (); | |
1787 typ = MatrixType::Full; | |
1788 } | |
1789 else | |
1790 { | |
1791 Array<Complex> z (2 * nc); | |
1792 Complex *pz = z.fortran_vec (); | |
1793 Array<double> rz (nc); | |
1794 double *prz = rz.fortran_vec (); | |
1795 | |
1796 F77_XFCN (zpocon, ZPOCON, (F77_CONST_CHAR_ARG2 (&job, 1), | |
1797 nr, tmp_data, nr, anorm, | |
1798 rcon, pz, prz, info | |
1799 F77_CHAR_ARG_LEN (1))); | |
1800 | |
1801 if (info != 0) | |
1802 rcon = 0.0; | |
1803 } | |
1804 } | |
1805 | |
1806 | |
1807 if (typ == MatrixType::Full) | |
1808 { | |
1809 octave_idx_type info = 0; | |
1810 | |
1811 Array<octave_idx_type> ipvt (nr); | |
1812 octave_idx_type *pipvt = ipvt.fortran_vec (); | |
1813 | |
1814 if(anorm < 0.) | |
1815 anorm = atmp.abs().sum(). | |
1816 row(static_cast<octave_idx_type>(0)).max(); | |
1817 | |
1818 Array<Complex> z (2 * nc); | |
1819 Complex *pz = z.fortran_vec (); | |
1820 Array<double> rz (2 * nc); | |
1821 double *prz = rz.fortran_vec (); | |
1822 | |
1823 F77_XFCN (zgetrf, ZGETRF, (nr, nr, tmp_data, nr, pipvt, info)); | |
1824 | |
1825 if (info != 0) | |
1826 { | |
1827 rcon = 0.0; | |
1828 mattype.mark_as_rectangular (); | |
1829 } | |
1830 else | |
1831 { | |
1832 char job = '1'; | |
1833 F77_XFCN (zgecon, ZGECON, (F77_CONST_CHAR_ARG2 (&job, 1), | |
1834 nc, tmp_data, nr, anorm, | |
1835 rcon, pz, prz, info | |
1836 F77_CHAR_ARG_LEN (1))); | |
1837 | |
1838 if (info != 0) | |
1839 rcon = 0.0; | |
1840 } | |
1841 } | |
1842 } | |
1843 else | |
1844 rcon = 0.0; | |
1845 } | |
1846 | |
1847 return rcon; | |
1848 } | |
1849 | |
458 | 1850 ComplexMatrix |
5785 | 1851 ComplexMatrix::utsolve (MatrixType &mattype, const ComplexMatrix& b, |
7788 | 1852 octave_idx_type& info, double& rcon, |
5785 | 1853 solve_singularity_handler sing_handler, |
1854 bool calc_cond) const | |
1855 { | |
1856 ComplexMatrix retval; | |
1857 | |
1858 octave_idx_type nr = rows (); | |
1859 octave_idx_type nc = cols (); | |
1860 | |
6924 | 1861 if (nr != b.rows ()) |
5785 | 1862 (*current_liboctave_error_handler) |
1863 ("matrix dimension mismatch solution of linear equations"); | |
6924 | 1864 else if (nr == 0 || nc == 0 || b.cols () == 0) |
1865 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); | |
5785 | 1866 else |
1867 { | |
1868 volatile int typ = mattype.type (); | |
1869 | |
1870 if (typ == MatrixType::Permuted_Upper || | |
1871 typ == MatrixType::Upper) | |
1872 { | |
1873 octave_idx_type b_nc = b.cols (); | |
7788 | 1874 rcon = 1.; |
5785 | 1875 info = 0; |
1876 | |
1877 if (typ == MatrixType::Permuted_Upper) | |
1878 { | |
1879 (*current_liboctave_error_handler) | |
6390 | 1880 ("permuted triangular matrix not implemented"); |
5785 | 1881 } |
1882 else | |
1883 { | |
1884 const Complex *tmp_data = fortran_vec (); | |
1885 | |
1886 if (calc_cond) | |
1887 { | |
1888 char norm = '1'; | |
1889 char uplo = 'U'; | |
1890 char dia = 'N'; | |
1891 | |
1892 Array<Complex> z (2 * nc); | |
1893 Complex *pz = z.fortran_vec (); | |
1894 Array<double> rz (nc); | |
1895 double *prz = rz.fortran_vec (); | |
1896 | |
1897 F77_XFCN (ztrcon, ZTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), | |
1898 F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1899 F77_CONST_CHAR_ARG2 (&dia, 1), | |
7788 | 1900 nr, tmp_data, nr, rcon, |
5785 | 1901 pz, prz, info |
1902 F77_CHAR_ARG_LEN (1) | |
1903 F77_CHAR_ARG_LEN (1) | |
1904 F77_CHAR_ARG_LEN (1))); | |
1905 | |
1906 if (info != 0) | |
1907 info = -2; | |
1908 | |
7788 | 1909 volatile double rcond_plus_one = rcon + 1.0; |
1910 | |
1911 if (rcond_plus_one == 1.0 || xisnan (rcon)) | |
5785 | 1912 { |
1913 info = -2; | |
1914 | |
1915 if (sing_handler) | |
7788 | 1916 sing_handler (rcon); |
5785 | 1917 else |
1918 (*current_liboctave_error_handler) | |
1919 ("matrix singular to machine precision, rcond = %g", | |
7788 | 1920 rcon); |
5785 | 1921 } |
1922 } | |
1923 | |
1924 if (info == 0) | |
1925 { | |
1926 retval = b; | |
1927 Complex *result = retval.fortran_vec (); | |
1928 | |
1929 char uplo = 'U'; | |
1930 char trans = 'N'; | |
1931 char dia = 'N'; | |
1932 | |
1933 F77_XFCN (ztrtrs, ZTRTRS, (F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1934 F77_CONST_CHAR_ARG2 (&trans, 1), | |
1935 F77_CONST_CHAR_ARG2 (&dia, 1), | |
1936 nr, b_nc, tmp_data, nr, | |
1937 result, nr, info | |
1938 F77_CHAR_ARG_LEN (1) | |
1939 F77_CHAR_ARG_LEN (1) | |
1940 F77_CHAR_ARG_LEN (1))); | |
1941 } | |
1942 } | |
1943 } | |
1944 else | |
1945 (*current_liboctave_error_handler) ("incorrect matrix type"); | |
1946 } | |
1947 | |
1948 return retval; | |
1949 } | |
1950 | |
1951 ComplexMatrix | |
1952 ComplexMatrix::ltsolve (MatrixType &mattype, const ComplexMatrix& b, | |
7788 | 1953 octave_idx_type& info, double& rcon, |
5785 | 1954 solve_singularity_handler sing_handler, |
1955 bool calc_cond) const | |
1956 { | |
1957 ComplexMatrix retval; | |
1958 | |
1959 octave_idx_type nr = rows (); | |
1960 octave_idx_type nc = cols (); | |
1961 | |
6924 | 1962 if (nr != b.rows ()) |
5785 | 1963 (*current_liboctave_error_handler) |
1964 ("matrix dimension mismatch solution of linear equations"); | |
6924 | 1965 else if (nr == 0 || nc == 0 || b.cols () == 0) |
1966 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); | |
5785 | 1967 else |
1968 { | |
1969 volatile int typ = mattype.type (); | |
1970 | |
1971 if (typ == MatrixType::Permuted_Lower || | |
1972 typ == MatrixType::Lower) | |
1973 { | |
1974 octave_idx_type b_nc = b.cols (); | |
7788 | 1975 rcon = 1.; |
5785 | 1976 info = 0; |
1977 | |
1978 if (typ == MatrixType::Permuted_Lower) | |
1979 { | |
1980 (*current_liboctave_error_handler) | |
6390 | 1981 ("permuted triangular matrix not implemented"); |
5785 | 1982 } |
1983 else | |
1984 { | |
1985 const Complex *tmp_data = fortran_vec (); | |
1986 | |
1987 if (calc_cond) | |
1988 { | |
1989 char norm = '1'; | |
1990 char uplo = 'L'; | |
1991 char dia = 'N'; | |
1992 | |
1993 Array<Complex> z (2 * nc); | |
1994 Complex *pz = z.fortran_vec (); | |
1995 Array<double> rz (nc); | |
1996 double *prz = rz.fortran_vec (); | |
1997 | |
1998 F77_XFCN (ztrcon, ZTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), | |
1999 F77_CONST_CHAR_ARG2 (&uplo, 1), | |
2000 F77_CONST_CHAR_ARG2 (&dia, 1), | |
7788 | 2001 nr, tmp_data, nr, rcon, |
5785 | 2002 pz, prz, info |
2003 F77_CHAR_ARG_LEN (1) | |
2004 F77_CHAR_ARG_LEN (1) | |
2005 F77_CHAR_ARG_LEN (1))); | |
2006 | |
2007 if (info != 0) | |
2008 info = -2; | |
2009 | |
7788 | 2010 volatile double rcond_plus_one = rcon + 1.0; |
2011 | |
2012 if (rcond_plus_one == 1.0 || xisnan (rcon)) | |
5785 | 2013 { |
2014 info = -2; | |
2015 | |
2016 if (sing_handler) | |
7788 | 2017 sing_handler (rcon); |
5785 | 2018 else |
2019 (*current_liboctave_error_handler) | |
2020 ("matrix singular to machine precision, rcond = %g", | |
7788 | 2021 rcon); |
5785 | 2022 } |
2023 } | |
2024 | |
2025 if (info == 0) | |
2026 { | |
2027 retval = b; | |
2028 Complex *result = retval.fortran_vec (); | |
2029 | |
2030 char uplo = 'L'; | |
2031 char trans = 'N'; | |
2032 char dia = 'N'; | |
2033 | |
2034 F77_XFCN (ztrtrs, ZTRTRS, (F77_CONST_CHAR_ARG2 (&uplo, 1), | |
2035 F77_CONST_CHAR_ARG2 (&trans, 1), | |
2036 F77_CONST_CHAR_ARG2 (&dia, 1), | |
2037 nr, b_nc, tmp_data, nr, | |
2038 result, nr, info | |
2039 F77_CHAR_ARG_LEN (1) | |
2040 F77_CHAR_ARG_LEN (1) | |
2041 F77_CHAR_ARG_LEN (1))); | |
2042 } | |
2043 } | |
2044 } | |
2045 else | |
2046 (*current_liboctave_error_handler) ("incorrect matrix type"); | |
2047 } | |
2048 | |
2049 return retval; | |
2050 } | |
2051 | |
2052 ComplexMatrix | |
2053 ComplexMatrix::fsolve (MatrixType &mattype, const ComplexMatrix& b, | |
7788 | 2054 octave_idx_type& info, double& rcon, |
5785 | 2055 solve_singularity_handler sing_handler, |
2056 bool calc_cond) const | |
2057 { | |
2058 ComplexMatrix retval; | |
2059 | |
2060 octave_idx_type nr = rows (); | |
2061 octave_idx_type nc = cols (); | |
2062 | |
6924 | 2063 |
2064 if (nr != nc || nr != b.rows ()) | |
5785 | 2065 (*current_liboctave_error_handler) |
6924 | 2066 ("matrix dimension mismatch solution of linear equations"); |
2067 else if (nr == 0 || b.cols () == 0) | |
2068 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); | |
5785 | 2069 else |
2070 { | |
2071 volatile int typ = mattype.type (); | |
2072 | |
2073 // Calculate the norm of the matrix, for later use. | |
2074 double anorm = -1.; | |
2075 | |
2076 if (typ == MatrixType::Hermitian) | |
2077 { | |
2078 info = 0; | |
2079 char job = 'L'; | |
2080 ComplexMatrix atmp = *this; | |
2081 Complex *tmp_data = atmp.fortran_vec (); | |
2082 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); | |
2083 | |
2084 F77_XFCN (zpotrf, ZPOTRF, (F77_CONST_CHAR_ARG2 (&job, 1), nr, | |
2085 tmp_data, nr, info | |
2086 F77_CHAR_ARG_LEN (1))); | |
2087 | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2088 // Throw-away extra info LAPACK gives so as to not change output. |
7788 | 2089 rcon = 0.0; |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2090 if (info != 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2091 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2092 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2093 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2094 mattype.mark_as_unsymmetric (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2095 typ = MatrixType::Full; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2096 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2097 else |
5785 | 2098 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2099 if (calc_cond) |
5785 | 2100 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2101 Array<Complex> z (2 * nc); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2102 Complex *pz = z.fortran_vec (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2103 Array<double> rz (nc); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2104 double *prz = rz.fortran_vec (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2105 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2106 F77_XFCN (zpocon, ZPOCON, (F77_CONST_CHAR_ARG2 (&job, 1), |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2107 nr, tmp_data, nr, anorm, |
7788 | 2108 rcon, pz, prz, info |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2109 F77_CHAR_ARG_LEN (1))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2110 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2111 if (info != 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2112 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2113 |
7788 | 2114 volatile double rcond_plus_one = rcon + 1.0; |
2115 | |
2116 if (rcond_plus_one == 1.0 || xisnan (rcon)) | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2117 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2118 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2119 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2120 if (sing_handler) |
7788 | 2121 sing_handler (rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2122 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2123 (*current_liboctave_error_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2124 ("matrix singular to machine precision, rcond = %g", |
7788 | 2125 rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2126 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2127 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2128 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2129 if (info == 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2130 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2131 retval = b; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2132 Complex *result = retval.fortran_vec (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2133 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2134 octave_idx_type b_nc = b.cols (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2135 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2136 F77_XFCN (zpotrs, ZPOTRS, (F77_CONST_CHAR_ARG2 (&job, 1), |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2137 nr, b_nc, tmp_data, nr, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2138 result, b.rows(), info |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2139 F77_CHAR_ARG_LEN (1))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2140 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2141 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2142 { |
5785 | 2143 mattype.mark_as_unsymmetric (); |
2144 typ = MatrixType::Full; | |
2145 } | |
2146 } | |
2147 } | |
2148 | |
2149 if (typ == MatrixType::Full) | |
2150 { | |
2151 info = 0; | |
2152 | |
2153 Array<octave_idx_type> ipvt (nr); | |
2154 octave_idx_type *pipvt = ipvt.fortran_vec (); | |
2155 | |
2156 ComplexMatrix atmp = *this; | |
2157 Complex *tmp_data = atmp.fortran_vec (); | |
2158 | |
2159 Array<Complex> z (2 * nc); | |
2160 Complex *pz = z.fortran_vec (); | |
2161 Array<double> rz (2 * nc); | |
2162 double *prz = rz.fortran_vec (); | |
2163 | |
2164 // Calculate the norm of the matrix, for later use. | |
2165 if (anorm < 0.) | |
2166 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); | |
2167 | |
2168 F77_XFCN (zgetrf, ZGETRF, (nr, nr, tmp_data, nr, pipvt, info)); | |
2169 | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2170 // Throw-away extra info LAPACK gives so as to not change output. |
7788 | 2171 rcon = 0.0; |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2172 if (info != 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2173 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2174 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2175 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2176 if (sing_handler) |
7788 | 2177 sing_handler (rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2178 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2179 (*current_liboctave_error_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2180 ("matrix singular to machine precision"); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2181 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2182 mattype.mark_as_rectangular (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2183 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2184 else |
5785 | 2185 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2186 if (calc_cond) |
5785 | 2187 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2188 // Now calculate the condition number for |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2189 // non-singular matrix. |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2190 char job = '1'; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2191 F77_XFCN (zgecon, ZGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2192 nc, tmp_data, nr, anorm, |
7788 | 2193 rcon, pz, prz, info |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2194 F77_CHAR_ARG_LEN (1))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2195 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2196 if (info != 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2197 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2198 |
7788 | 2199 volatile double rcond_plus_one = rcon + 1.0; |
2200 | |
2201 if (rcond_plus_one == 1.0 || xisnan (rcon)) | |
5785 | 2202 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2203 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2204 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2205 if (sing_handler) |
7788 | 2206 sing_handler (rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2207 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2208 (*current_liboctave_error_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2209 ("matrix singular to machine precision, rcond = %g", |
7788 | 2210 rcon); |
5785 | 2211 } |
2212 } | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2213 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2214 if (info == 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2215 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2216 retval = b; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2217 Complex *result = retval.fortran_vec (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2218 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2219 octave_idx_type b_nc = b.cols (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2220 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2221 char job = 'N'; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2222 F77_XFCN (zgetrs, ZGETRS, (F77_CONST_CHAR_ARG2 (&job, 1), |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2223 nr, b_nc, tmp_data, nr, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2224 pipvt, result, b.rows(), info |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2225 F77_CHAR_ARG_LEN (1))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2226 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2227 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2228 mattype.mark_as_rectangular (); |
5785 | 2229 } |
2230 } | |
2231 } | |
2232 | |
2233 return retval; | |
2234 } | |
2235 | |
2236 ComplexMatrix | |
2237 ComplexMatrix::solve (MatrixType &typ, const Matrix& b) const | |
2238 { | |
2239 octave_idx_type info; | |
7788 | 2240 double rcon; |
2241 return solve (typ, b, info, rcon, 0); | |
5785 | 2242 } |
2243 | |
2244 ComplexMatrix | |
2245 ComplexMatrix::solve (MatrixType &typ, const Matrix& b, | |
2246 octave_idx_type& info) const | |
2247 { | |
7788 | 2248 double rcon; |
2249 return solve (typ, b, info, rcon, 0); | |
5785 | 2250 } |
2251 | |
2252 ComplexMatrix | |
2253 ComplexMatrix::solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, | |
7788 | 2254 double& rcon) const |
5785 | 2255 { |
7788 | 2256 return solve (typ, b, info, rcon, 0); |
5785 | 2257 } |
2258 | |
2259 ComplexMatrix | |
2260 ComplexMatrix::solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, | |
7788 | 2261 double& rcon, solve_singularity_handler sing_handler, |
5785 | 2262 bool singular_fallback) const |
2263 { | |
2264 ComplexMatrix tmp (b); | |
7788 | 2265 return solve (typ, tmp, info, rcon, sing_handler, singular_fallback); |
5785 | 2266 } |
2267 | |
2268 ComplexMatrix | |
2269 ComplexMatrix::solve (MatrixType &typ, const ComplexMatrix& b) const | |
2270 { | |
2271 octave_idx_type info; | |
7788 | 2272 double rcon; |
2273 return solve (typ, b, info, rcon, 0); | |
5785 | 2274 } |
2275 | |
2276 ComplexMatrix | |
2277 ComplexMatrix::solve (MatrixType &typ, const ComplexMatrix& b, | |
2278 octave_idx_type& info) const | |
2279 { | |
7788 | 2280 double rcon; |
2281 return solve (typ, b, info, rcon, 0); | |
5785 | 2282 } |
2283 | |
2284 ComplexMatrix | |
2285 ComplexMatrix::solve (MatrixType &typ, const ComplexMatrix& b, | |
7788 | 2286 octave_idx_type& info, double& rcon) const |
5785 | 2287 { |
7788 | 2288 return solve (typ, b, info, rcon, 0); |
5785 | 2289 } |
2290 | |
2291 ComplexMatrix | |
2292 ComplexMatrix::solve (MatrixType &mattype, const ComplexMatrix& b, | |
7788 | 2293 octave_idx_type& info, double& rcon, |
5785 | 2294 solve_singularity_handler sing_handler, |
2295 bool singular_fallback) const | |
2296 { | |
2297 ComplexMatrix retval; | |
2298 int typ = mattype.type (); | |
2299 | |
2300 if (typ == MatrixType::Unknown) | |
2301 typ = mattype.type (*this); | |
2302 | |
2303 // Only calculate the condition number for LU/Cholesky | |
2304 if (typ == MatrixType::Upper || typ == MatrixType::Permuted_Upper) | |
7788 | 2305 retval = utsolve (mattype, b, info, rcon, sing_handler, false); |
5785 | 2306 else if (typ == MatrixType::Lower || typ == MatrixType::Permuted_Lower) |
7788 | 2307 retval = ltsolve (mattype, b, info, rcon, sing_handler, false); |
5785 | 2308 else if (typ == MatrixType::Full || typ == MatrixType::Hermitian) |
7788 | 2309 retval = fsolve (mattype, b, info, rcon, sing_handler, true); |
5785 | 2310 else if (typ != MatrixType::Rectangular) |
2311 { | |
2312 (*current_liboctave_error_handler) ("unknown matrix type"); | |
2313 return ComplexMatrix (); | |
2314 } | |
2315 | |
2316 // Rectangular or one of the above solvers flags a singular matrix | |
2317 if (singular_fallback && mattype.type () == MatrixType::Rectangular) | |
2318 { | |
2319 octave_idx_type rank; | |
7788 | 2320 retval = lssolve (b, info, rank, rcon); |
5785 | 2321 } |
2322 | |
2323 return retval; | |
2324 } | |
2325 | |
2326 ComplexColumnVector | |
2327 ComplexMatrix::solve (MatrixType &typ, const ColumnVector& b) const | |
2328 { | |
2329 octave_idx_type info; | |
7788 | 2330 double rcon; |
2331 return solve (typ, ComplexColumnVector (b), info, rcon, 0); | |
5785 | 2332 } |
2333 | |
2334 ComplexColumnVector | |
2335 ComplexMatrix::solve (MatrixType &typ, const ColumnVector& b, | |
2336 octave_idx_type& info) const | |
2337 { | |
7788 | 2338 double rcon; |
2339 return solve (typ, ComplexColumnVector (b), info, rcon, 0); | |
5785 | 2340 } |
2341 | |
2342 ComplexColumnVector | |
2343 ComplexMatrix::solve (MatrixType &typ, const ColumnVector& b, | |
7788 | 2344 octave_idx_type& info, double& rcon) const |
5785 | 2345 { |
7788 | 2346 return solve (typ, ComplexColumnVector (b), info, rcon, 0); |
5785 | 2347 } |
2348 | |
2349 ComplexColumnVector | |
2350 ComplexMatrix::solve (MatrixType &typ, const ColumnVector& b, | |
7788 | 2351 octave_idx_type& info, double& rcon, |
5785 | 2352 solve_singularity_handler sing_handler) const |
2353 { | |
7788 | 2354 return solve (typ, ComplexColumnVector (b), info, rcon, sing_handler); |
5785 | 2355 } |
2356 | |
2357 ComplexColumnVector | |
2358 ComplexMatrix::solve (MatrixType &typ, const ComplexColumnVector& b) const | |
2359 { | |
2360 octave_idx_type info; | |
7788 | 2361 double rcon; |
2362 return solve (typ, b, info, rcon, 0); | |
5785 | 2363 } |
2364 | |
2365 ComplexColumnVector | |
2366 ComplexMatrix::solve (MatrixType &typ, const ComplexColumnVector& b, | |
2367 octave_idx_type& info) const | |
2368 { | |
7788 | 2369 double rcon; |
2370 return solve (typ, b, info, rcon, 0); | |
5785 | 2371 } |
2372 | |
2373 ComplexColumnVector | |
2374 ComplexMatrix::solve (MatrixType &typ, const ComplexColumnVector& b, | |
7788 | 2375 octave_idx_type& info, double& rcon) const |
5785 | 2376 { |
7788 | 2377 return solve (typ, b, info, rcon, 0); |
5785 | 2378 } |
2379 | |
2380 ComplexColumnVector | |
2381 ComplexMatrix::solve (MatrixType &typ, const ComplexColumnVector& b, | |
7788 | 2382 octave_idx_type& info, double& rcon, |
5785 | 2383 solve_singularity_handler sing_handler) const |
2384 { | |
2385 | |
2386 ComplexMatrix tmp (b); | |
7788 | 2387 return solve (typ, tmp, info, rcon, sing_handler).column(static_cast<octave_idx_type> (0)); |
5785 | 2388 } |
2389 | |
2390 ComplexMatrix | |
458 | 2391 ComplexMatrix::solve (const Matrix& b) const |
2392 { | |
5275 | 2393 octave_idx_type info; |
7788 | 2394 double rcon; |
2395 return solve (b, info, rcon, 0); | |
458 | 2396 } |
2397 | |
2398 ComplexMatrix | |
5275 | 2399 ComplexMatrix::solve (const Matrix& b, octave_idx_type& info) const |
458 | 2400 { |
7788 | 2401 double rcon; |
2402 return solve (b, info, rcon, 0); | |
458 | 2403 } |
2404 | |
2405 ComplexMatrix | |
7788 | 2406 ComplexMatrix::solve (const Matrix& b, octave_idx_type& info, double& rcon) const |
458 | 2407 { |
7788 | 2408 return solve (b, info, rcon, 0); |
3480 | 2409 } |
2410 | |
2411 ComplexMatrix | |
7788 | 2412 ComplexMatrix::solve (const Matrix& b, octave_idx_type& info, double& rcon, |
3480 | 2413 solve_singularity_handler sing_handler) const |
2414 { | |
458 | 2415 ComplexMatrix tmp (b); |
7788 | 2416 return solve (tmp, info, rcon, sing_handler); |
458 | 2417 } |
2418 | |
2419 ComplexMatrix | |
2420 ComplexMatrix::solve (const ComplexMatrix& b) const | |
2421 { | |
5275 | 2422 octave_idx_type info; |
7788 | 2423 double rcon; |
2424 return solve (b, info, rcon, 0); | |
458 | 2425 } |
2426 | |
2427 ComplexMatrix | |
5275 | 2428 ComplexMatrix::solve (const ComplexMatrix& b, octave_idx_type& info) const |
458 | 2429 { |
7788 | 2430 double rcon; |
2431 return solve (b, info, rcon, 0); | |
458 | 2432 } |
3480 | 2433 |
458 | 2434 ComplexMatrix |
7788 | 2435 ComplexMatrix::solve (const ComplexMatrix& b, octave_idx_type& info, double& rcon) const |
458 | 2436 { |
7788 | 2437 return solve (b, info, rcon, 0); |
3480 | 2438 } |
2439 | |
2440 ComplexMatrix | |
7788 | 2441 ComplexMatrix::solve (const ComplexMatrix& b, octave_idx_type& info, double& rcon, |
3480 | 2442 solve_singularity_handler sing_handler) const |
2443 { | |
5785 | 2444 MatrixType mattype (*this); |
7788 | 2445 return solve (mattype, b, info, rcon, sing_handler); |
458 | 2446 } |
2447 | |
2448 ComplexColumnVector | |
3585 | 2449 ComplexMatrix::solve (const ColumnVector& b) const |
2450 { | |
5275 | 2451 octave_idx_type info; |
7788 | 2452 double rcon; |
2453 return solve (ComplexColumnVector (b), info, rcon, 0); | |
3585 | 2454 } |
2455 | |
2456 ComplexColumnVector | |
5275 | 2457 ComplexMatrix::solve (const ColumnVector& b, octave_idx_type& info) const |
3585 | 2458 { |
7788 | 2459 double rcon; |
2460 return solve (ComplexColumnVector (b), info, rcon, 0); | |
3585 | 2461 } |
2462 | |
2463 ComplexColumnVector | |
5785 | 2464 ComplexMatrix::solve (const ColumnVector& b, octave_idx_type& info, |
7788 | 2465 double& rcon) const |
3585 | 2466 { |
7788 | 2467 return solve (ComplexColumnVector (b), info, rcon, 0); |
3585 | 2468 } |
2469 | |
2470 ComplexColumnVector | |
5785 | 2471 ComplexMatrix::solve (const ColumnVector& b, octave_idx_type& info, |
7788 | 2472 double& rcon, |
3585 | 2473 solve_singularity_handler sing_handler) const |
2474 { | |
7788 | 2475 return solve (ComplexColumnVector (b), info, rcon, sing_handler); |
3585 | 2476 } |
2477 | |
2478 ComplexColumnVector | |
458 | 2479 ComplexMatrix::solve (const ComplexColumnVector& b) const |
2480 { | |
5275 | 2481 octave_idx_type info; |
7788 | 2482 double rcon; |
2483 return solve (b, info, rcon, 0); | |
458 | 2484 } |
2485 | |
2486 ComplexColumnVector | |
5275 | 2487 ComplexMatrix::solve (const ComplexColumnVector& b, octave_idx_type& info) const |
458 | 2488 { |
7788 | 2489 double rcon; |
2490 return solve (b, info, rcon, 0); | |
458 | 2491 } |
2492 | |
2493 ComplexColumnVector | |
5275 | 2494 ComplexMatrix::solve (const ComplexColumnVector& b, octave_idx_type& info, |
7788 | 2495 double& rcon) const |
458 | 2496 { |
7788 | 2497 return solve (b, info, rcon, 0); |
3480 | 2498 } |
2499 | |
2500 ComplexColumnVector | |
5275 | 2501 ComplexMatrix::solve (const ComplexColumnVector& b, octave_idx_type& info, |
7788 | 2502 double& rcon, |
3480 | 2503 solve_singularity_handler sing_handler) const |
2504 { | |
5785 | 2505 MatrixType mattype (*this); |
7788 | 2506 return solve (mattype, b, info, rcon, sing_handler); |
458 | 2507 } |
2508 | |
2509 ComplexMatrix | |
3585 | 2510 ComplexMatrix::lssolve (const Matrix& b) const |
2511 { | |
5275 | 2512 octave_idx_type info; |
2513 octave_idx_type rank; | |
7788 | 2514 double rcon; |
2515 return lssolve (ComplexMatrix (b), info, rank, rcon); | |
3585 | 2516 } |
2517 | |
2518 ComplexMatrix | |
5275 | 2519 ComplexMatrix::lssolve (const Matrix& b, octave_idx_type& info) const |
3585 | 2520 { |
5275 | 2521 octave_idx_type rank; |
7788 | 2522 double rcon; |
2523 return lssolve (ComplexMatrix (b), info, rank, rcon); | |
3585 | 2524 } |
2525 | |
2526 ComplexMatrix | |
7076 | 2527 ComplexMatrix::lssolve (const Matrix& b, octave_idx_type& info, |
2528 octave_idx_type& rank) const | |
3585 | 2529 { |
7788 | 2530 double rcon; |
2531 return lssolve (ComplexMatrix (b), info, rank, rcon); | |
7076 | 2532 } |
2533 | |
2534 ComplexMatrix | |
2535 ComplexMatrix::lssolve (const Matrix& b, octave_idx_type& info, | |
7788 | 2536 octave_idx_type& rank, double& rcon) const |
7076 | 2537 { |
7788 | 2538 return lssolve (ComplexMatrix (b), info, rank, rcon); |
3585 | 2539 } |
2540 | |
2541 ComplexMatrix | |
458 | 2542 ComplexMatrix::lssolve (const ComplexMatrix& b) const |
2543 { | |
5275 | 2544 octave_idx_type info; |
2545 octave_idx_type rank; | |
7788 | 2546 double rcon; |
2547 return lssolve (b, info, rank, rcon); | |
458 | 2548 } |
2549 | |
2550 ComplexMatrix | |
5275 | 2551 ComplexMatrix::lssolve (const ComplexMatrix& b, octave_idx_type& info) const |
458 | 2552 { |
5275 | 2553 octave_idx_type rank; |
7788 | 2554 double rcon; |
2555 return lssolve (b, info, rank, rcon); | |
458 | 2556 } |
2557 | |
2558 ComplexMatrix | |
7076 | 2559 ComplexMatrix::lssolve (const ComplexMatrix& b, octave_idx_type& info, |
2560 octave_idx_type& rank) const | |
2561 { | |
7788 | 2562 double rcon; |
2563 return lssolve (b, info, rank, rcon); | |
7076 | 2564 } |
2565 | |
2566 ComplexMatrix | |
2567 ComplexMatrix::lssolve (const ComplexMatrix& b, octave_idx_type& info, | |
7788 | 2568 octave_idx_type& rank, double& rcon) const |
458 | 2569 { |
1948 | 2570 ComplexMatrix retval; |
2571 | |
5275 | 2572 octave_idx_type nrhs = b.cols (); |
2573 | |
2574 octave_idx_type m = rows (); | |
2575 octave_idx_type n = cols (); | |
458 | 2576 |
6924 | 2577 if (m != b.rows ()) |
1948 | 2578 (*current_liboctave_error_handler) |
2579 ("matrix dimension mismatch solution of linear equations"); | |
6924 | 2580 else if (m== 0 || n == 0 || b.cols () == 0) |
2581 retval = ComplexMatrix (n, b.cols (), Complex (0.0, 0.0)); | |
1948 | 2582 else |
458 | 2583 { |
7072 | 2584 volatile octave_idx_type minmn = (m < n ? m : n); |
2585 octave_idx_type maxmn = m > n ? m : n; | |
7788 | 2586 rcon = -1.0; |
7072 | 2587 |
2588 if (m != n) | |
2589 { | |
2590 retval = ComplexMatrix (maxmn, nrhs); | |
2591 | |
2592 for (octave_idx_type j = 0; j < nrhs; j++) | |
2593 for (octave_idx_type i = 0; i < m; i++) | |
2594 retval.elem (i, j) = b.elem (i, j); | |
2595 } | |
2596 else | |
2597 retval = b; | |
2598 | |
1948 | 2599 ComplexMatrix atmp = *this; |
2600 Complex *tmp_data = atmp.fortran_vec (); | |
2601 | |
7072 | 2602 Complex *pretval = retval.fortran_vec (); |
2603 Array<double> s (minmn); | |
7071 | 2604 double *ps = s.fortran_vec (); |
2563 | 2605 |
7072 | 2606 // Ask ZGELSD what the dimension of WORK should be. |
5275 | 2607 octave_idx_type lwork = -1; |
3752 | 2608 |
2609 Array<Complex> work (1); | |
7079 | 2610 |
7477 | 2611 octave_idx_type smlsiz; |
2612 F77_FUNC (xilaenv, XILAENV) (9, F77_CONST_CHAR_ARG2 ("ZGELSD", 6), | |
2613 F77_CONST_CHAR_ARG2 (" ", 1), | |
7478 | 2614 0, 0, 0, 0, smlsiz |
7477 | 2615 F77_CHAR_ARG_LEN (6) |
7478 | 2616 F77_CHAR_ARG_LEN (1)); |
7079 | 2617 |
7486
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2618 octave_idx_type mnthr; |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2619 F77_FUNC (xilaenv, XILAENV) (6, F77_CONST_CHAR_ARG2 ("ZGELSD", 6), |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2620 F77_CONST_CHAR_ARG2 (" ", 1), |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2621 m, n, nrhs, -1, mnthr |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2622 F77_CHAR_ARG_LEN (6) |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2623 F77_CHAR_ARG_LEN (1)); |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2624 |
7079 | 2625 // We compute the size of rwork and iwork because ZGELSD in |
2626 // older versions of LAPACK does not return them on a query | |
2627 // call. | |
7124 | 2628 double dminmn = static_cast<double> (minmn); |
2629 double dsmlsizp1 = static_cast<double> (smlsiz+1); | |
7079 | 2630 #if defined (HAVE_LOG2) |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2631 double tmp = log2 (dminmn / dsmlsizp1); |
7079 | 2632 #else |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2633 double tmp = log (dminmn / dsmlsizp1) / log (2.0); |
7079 | 2634 #endif |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2635 octave_idx_type nlvl = static_cast<octave_idx_type> (tmp) + 1; |
7079 | 2636 if (nlvl < 0) |
2637 nlvl = 0; | |
2638 | |
2639 octave_idx_type lrwork = minmn*(10 + 2*smlsiz + 8*nlvl) | |
2640 + 3*smlsiz*nrhs + (smlsiz+1)*(smlsiz+1); | |
2641 if (lrwork < 1) | |
2642 lrwork = 1; | |
2643 Array<double> rwork (lrwork); | |
2644 double *prwork = rwork.fortran_vec (); | |
2645 | |
2646 octave_idx_type liwork = 3 * minmn * nlvl + 11 * minmn; | |
2647 if (liwork < 1) | |
2648 liwork = 1; | |
2649 Array<octave_idx_type> iwork (liwork); | |
2650 octave_idx_type* piwork = iwork.fortran_vec (); | |
7072 | 2651 |
2652 F77_XFCN (zgelsd, ZGELSD, (m, n, nrhs, tmp_data, m, pretval, maxmn, | |
7788 | 2653 ps, rcon, rank, work.fortran_vec (), |
7079 | 2654 lwork, prwork, piwork, info)); |
1948 | 2655 |
7476 | 2656 // The workspace query is broken in at least LAPACK 3.0.0 |
7488
6470f946a425
another small xGELSD workspace fix
John W. Eaton <jwe@octave.org>
parents:
7486
diff
changeset
|
2657 // through 3.1.1 when n >= mnthr. The obtuse formula below |
7486
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2658 // should provide sufficient workspace for ZGELSD to operate |
7476 | 2659 // efficiently. |
7488
6470f946a425
another small xGELSD workspace fix
John W. Eaton <jwe@octave.org>
parents:
7486
diff
changeset
|
2660 if (n >= mnthr) |
7476 | 2661 { |
2662 octave_idx_type addend = m; | |
2663 | |
2664 if (2*m-4 > addend) | |
2665 addend = 2*m-4; | |
2666 | |
2667 if (nrhs > addend) | |
2668 addend = nrhs; | |
2669 | |
2670 if (n-3*m > addend) | |
2671 addend = n-3*m; | |
2672 | |
2673 const octave_idx_type lworkaround = 4*m + m*m + addend; | |
2674 | |
2675 if (std::real (work(0)) < lworkaround) | |
2676 work(0) = lworkaround; | |
2677 } | |
7532
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2678 else if (m >= n) |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2679 { |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2680 octave_idx_type lworkaround = 2*m + m*nrhs; |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2681 |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2682 if (std::real (work(0)) < lworkaround) |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2683 work(0) = lworkaround; |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2684 } |
7476 | 2685 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2686 lwork = static_cast<octave_idx_type> (std::real (work(0))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2687 work.resize (lwork); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2688 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2689 F77_XFCN (zgelsd, ZGELSD, (m, n, nrhs, tmp_data, m, pretval, |
7788 | 2690 maxmn, ps, rcon, rank, |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2691 work.fortran_vec (), lwork, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2692 prwork, piwork, info)); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2693 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2694 if (rank < minmn) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2695 (*current_liboctave_warning_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2696 ("zgelsd: rank deficient %dx%d matrix, rank = %d, tol = %e", |
7788 | 2697 m, n, rank, rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2698 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2699 if (s.elem (0) == 0.0) |
7788 | 2700 rcon = 0.0; |
1948 | 2701 else |
7788 | 2702 rcon = s.elem (minmn - 1) / s.elem (0); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2703 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2704 retval.resize (n, nrhs); |
458 | 2705 } |
2706 | |
2707 return retval; | |
2708 } | |
2709 | |
2710 ComplexColumnVector | |
3585 | 2711 ComplexMatrix::lssolve (const ColumnVector& b) const |
2712 { | |
5275 | 2713 octave_idx_type info; |
2714 octave_idx_type rank; | |
7788 | 2715 double rcon; |
2716 return lssolve (ComplexColumnVector (b), info, rank, rcon); | |
3585 | 2717 } |
2718 | |
2719 ComplexColumnVector | |
5275 | 2720 ComplexMatrix::lssolve (const ColumnVector& b, octave_idx_type& info) const |
3585 | 2721 { |
5275 | 2722 octave_idx_type rank; |
7788 | 2723 double rcon; |
2724 return lssolve (ComplexColumnVector (b), info, rank, rcon); | |
3585 | 2725 } |
2726 | |
2727 ComplexColumnVector | |
7076 | 2728 ComplexMatrix::lssolve (const ColumnVector& b, octave_idx_type& info, |
2729 octave_idx_type& rank) const | |
3585 | 2730 { |
7788 | 2731 double rcon; |
2732 return lssolve (ComplexColumnVector (b), info, rank, rcon); | |
7076 | 2733 } |
2734 | |
2735 ComplexColumnVector | |
2736 ComplexMatrix::lssolve (const ColumnVector& b, octave_idx_type& info, | |
7788 | 2737 octave_idx_type& rank, double& rcon) const |
7076 | 2738 { |
7788 | 2739 return lssolve (ComplexColumnVector (b), info, rank, rcon); |
3585 | 2740 } |
2741 | |
2742 ComplexColumnVector | |
458 | 2743 ComplexMatrix::lssolve (const ComplexColumnVector& b) const |
2744 { | |
5275 | 2745 octave_idx_type info; |
2746 octave_idx_type rank; | |
7788 | 2747 double rcon; |
2748 return lssolve (b, info, rank, rcon); | |
458 | 2749 } |
2750 | |
2751 ComplexColumnVector | |
5275 | 2752 ComplexMatrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info) const |
458 | 2753 { |
5275 | 2754 octave_idx_type rank; |
7788 | 2755 double rcon; |
2756 return lssolve (b, info, rank, rcon); | |
458 | 2757 } |
2758 | |
2759 ComplexColumnVector | |
5275 | 2760 ComplexMatrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info, |
2761 octave_idx_type& rank) const | |
458 | 2762 { |
7788 | 2763 double rcon; |
2764 return lssolve (b, info, rank, rcon); | |
7076 | 2765 |
2766 } | |
2767 | |
2768 ComplexColumnVector | |
2769 ComplexMatrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info, | |
7788 | 2770 octave_idx_type& rank, double& rcon) const |
7076 | 2771 { |
1948 | 2772 ComplexColumnVector retval; |
2773 | |
5275 | 2774 octave_idx_type nrhs = 1; |
2775 | |
2776 octave_idx_type m = rows (); | |
2777 octave_idx_type n = cols (); | |
458 | 2778 |
6924 | 2779 if (m != b.length ()) |
1948 | 2780 (*current_liboctave_error_handler) |
6924 | 2781 ("matrix dimension mismatch solution of linear equations"); |
2782 else if (m == 0 || n == 0 || b.cols () == 0) | |
2783 retval = ComplexColumnVector (n, Complex (0.0, 0.0)); | |
1948 | 2784 else |
458 | 2785 { |
7072 | 2786 volatile octave_idx_type minmn = (m < n ? m : n); |
2787 octave_idx_type maxmn = m > n ? m : n; | |
7788 | 2788 rcon = -1.0; |
7072 | 2789 |
2790 if (m != n) | |
2791 { | |
2792 retval = ComplexColumnVector (maxmn); | |
2793 | |
2794 for (octave_idx_type i = 0; i < m; i++) | |
2795 retval.elem (i) = b.elem (i); | |
2796 } | |
2797 else | |
2798 retval = b; | |
2799 | |
1948 | 2800 ComplexMatrix atmp = *this; |
2801 Complex *tmp_data = atmp.fortran_vec (); | |
2802 | |
7072 | 2803 Complex *pretval = retval.fortran_vec (); |
2804 Array<double> s (minmn); | |
7071 | 2805 double *ps = s.fortran_vec (); |
1948 | 2806 |
7072 | 2807 // Ask ZGELSD what the dimension of WORK should be. |
5275 | 2808 octave_idx_type lwork = -1; |
3752 | 2809 |
2810 Array<Complex> work (1); | |
7079 | 2811 |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2812 octave_idx_type smlsiz; |
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2813 F77_FUNC (xilaenv, XILAENV) (9, F77_CONST_CHAR_ARG2 ("ZGELSD", 6), |
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2814 F77_CONST_CHAR_ARG2 (" ", 1), |
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2815 0, 0, 0, 0, smlsiz |
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2816 F77_CHAR_ARG_LEN (6) |
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2817 F77_CHAR_ARG_LEN (1)); |
7079 | 2818 |
2819 // We compute the size of rwork and iwork because ZGELSD in | |
2820 // older versions of LAPACK does not return them on a query | |
2821 // call. | |
7124 | 2822 double dminmn = static_cast<double> (minmn); |
2823 double dsmlsizp1 = static_cast<double> (smlsiz+1); | |
7079 | 2824 #if defined (HAVE_LOG2) |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2825 double tmp = log2 (dminmn / dsmlsizp1); |
7079 | 2826 #else |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2827 double tmp = log (dminmn / dsmlsizp1) / log (2.0); |
7079 | 2828 #endif |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2829 octave_idx_type nlvl = static_cast<octave_idx_type> (tmp) + 1; |
7079 | 2830 if (nlvl < 0) |
2831 nlvl = 0; | |
2832 | |
2833 octave_idx_type lrwork = minmn*(10 + 2*smlsiz + 8*nlvl) | |
2834 + 3*smlsiz*nrhs + (smlsiz+1)*(smlsiz+1); | |
2835 if (lrwork < 1) | |
2836 lrwork = 1; | |
2837 Array<double> rwork (lrwork); | |
2838 double *prwork = rwork.fortran_vec (); | |
2839 | |
2840 octave_idx_type liwork = 3 * minmn * nlvl + 11 * minmn; | |
2841 if (liwork < 1) | |
2842 liwork = 1; | |
2843 Array<octave_idx_type> iwork (liwork); | |
2844 octave_idx_type* piwork = iwork.fortran_vec (); | |
7072 | 2845 |
2846 F77_XFCN (zgelsd, ZGELSD, (m, n, nrhs, tmp_data, m, pretval, maxmn, | |
7788 | 2847 ps, rcon, rank, work.fortran_vec (), |
7079 | 2848 lwork, prwork, piwork, info)); |
1948 | 2849 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2850 lwork = static_cast<octave_idx_type> (std::real (work(0))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2851 work.resize (lwork); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2852 rwork.resize (static_cast<octave_idx_type> (rwork(0))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2853 iwork.resize (iwork(0)); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2854 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2855 F77_XFCN (zgelsd, ZGELSD, (m, n, nrhs, tmp_data, m, pretval, |
7788 | 2856 maxmn, ps, rcon, rank, |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2857 work.fortran_vec (), lwork, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2858 prwork, piwork, info)); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2859 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2860 if (rank < minmn) |
1948 | 2861 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2862 if (rank < minmn) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2863 (*current_liboctave_warning_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2864 ("zgelsd: rank deficient %dx%d matrix, rank = %d, tol = %e", |
7788 | 2865 m, n, rank, rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2866 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2867 if (s.elem (0) == 0.0) |
7788 | 2868 rcon = 0.0; |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2869 else |
7788 | 2870 rcon = s.elem (minmn - 1) / s.elem (0); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2871 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2872 retval.resize (n, nrhs); |
1948 | 2873 } |
458 | 2874 } |
2875 | |
2876 return retval; | |
2877 } | |
2878 | |
1205 | 2879 // column vector by row vector -> matrix operations |
2880 | |
2881 ComplexMatrix | |
2882 operator * (const ColumnVector& v, const ComplexRowVector& a) | |
2883 { | |
2884 ComplexColumnVector tmp (v); | |
2885 return tmp * a; | |
2886 } | |
2887 | |
2888 ComplexMatrix | |
2889 operator * (const ComplexColumnVector& a, const RowVector& b) | |
2890 { | |
2891 ComplexRowVector tmp (b); | |
2892 return a * tmp; | |
2893 } | |
2894 | |
2895 ComplexMatrix | |
2896 operator * (const ComplexColumnVector& v, const ComplexRowVector& a) | |
2897 { | |
1948 | 2898 ComplexMatrix retval; |
2899 | |
5275 | 2900 octave_idx_type len = v.length (); |
3233 | 2901 |
2902 if (len != 0) | |
1205 | 2903 { |
5275 | 2904 octave_idx_type a_len = a.length (); |
3233 | 2905 |
9359
be6867ba8104
avoid useless zero initialization when doing matrix multiply
Jaroslav Hajek <highegg@gmail.com>
parents:
9227
diff
changeset
|
2906 retval = ComplexMatrix (len, a_len); |
3233 | 2907 Complex *c = retval.fortran_vec (); |
2908 | |
4552 | 2909 F77_XFCN (zgemm, ZGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
2910 F77_CONST_CHAR_ARG2 ("N", 1), | |
2911 len, a_len, 1, 1.0, v.data (), len, | |
2912 a.data (), 1, 0.0, c, len | |
2913 F77_CHAR_ARG_LEN (1) | |
2914 F77_CHAR_ARG_LEN (1))); | |
1205 | 2915 } |
2916 | |
1948 | 2917 return retval; |
1205 | 2918 } |
2919 | |
458 | 2920 // matrix by diagonal matrix -> matrix operations |
2921 | |
2922 ComplexMatrix& | |
2923 ComplexMatrix::operator += (const DiagMatrix& a) | |
2924 { | |
5275 | 2925 octave_idx_type nr = rows (); |
2926 octave_idx_type nc = cols (); | |
2927 | |
2928 octave_idx_type a_nr = rows (); | |
2929 octave_idx_type a_nc = cols (); | |
2384 | 2930 |
2931 if (nr != a_nr || nc != a_nc) | |
458 | 2932 { |
2384 | 2933 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889 | 2934 return *this; |
458 | 2935 } |
2936 | |
5275 | 2937 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 2938 elem (i, i) += a.elem (i, i); |
2939 | |
2940 return *this; | |
2941 } | |
2942 | |
2943 ComplexMatrix& | |
2944 ComplexMatrix::operator -= (const DiagMatrix& a) | |
2945 { | |
5275 | 2946 octave_idx_type nr = rows (); |
2947 octave_idx_type nc = cols (); | |
2948 | |
2949 octave_idx_type a_nr = rows (); | |
2950 octave_idx_type a_nc = cols (); | |
2384 | 2951 |
2952 if (nr != a_nr || nc != a_nc) | |
458 | 2953 { |
2384 | 2954 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889 | 2955 return *this; |
458 | 2956 } |
2957 | |
5275 | 2958 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 2959 elem (i, i) -= a.elem (i, i); |
2960 | |
2961 return *this; | |
2962 } | |
2963 | |
2964 ComplexMatrix& | |
2965 ComplexMatrix::operator += (const ComplexDiagMatrix& a) | |
2966 { | |
5275 | 2967 octave_idx_type nr = rows (); |
2968 octave_idx_type nc = cols (); | |
2969 | |
2970 octave_idx_type a_nr = rows (); | |
2971 octave_idx_type a_nc = cols (); | |
2384 | 2972 |
2973 if (nr != a_nr || nc != a_nc) | |
458 | 2974 { |
2384 | 2975 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889 | 2976 return *this; |
458 | 2977 } |
2978 | |
5275 | 2979 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 2980 elem (i, i) += a.elem (i, i); |
2981 | |
2982 return *this; | |
2983 } | |
2984 | |
2985 ComplexMatrix& | |
2986 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) | |
2987 { | |
5275 | 2988 octave_idx_type nr = rows (); |
2989 octave_idx_type nc = cols (); | |
2990 | |
2991 octave_idx_type a_nr = rows (); | |
2992 octave_idx_type a_nc = cols (); | |
2384 | 2993 |
2994 if (nr != a_nr || nc != a_nc) | |
458 | 2995 { |
2384 | 2996 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889 | 2997 return *this; |
458 | 2998 } |
2999 | |
5275 | 3000 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 3001 elem (i, i) -= a.elem (i, i); |
3002 | |
3003 return *this; | |
3004 } | |
3005 | |
3006 // matrix by matrix -> matrix operations | |
3007 | |
3008 ComplexMatrix& | |
3009 ComplexMatrix::operator += (const Matrix& a) | |
3010 { | |
5275 | 3011 octave_idx_type nr = rows (); |
3012 octave_idx_type nc = cols (); | |
3013 | |
3014 octave_idx_type a_nr = a.rows (); | |
3015 octave_idx_type a_nc = a.cols (); | |
2384 | 3016 |
3017 if (nr != a_nr || nc != a_nc) | |
458 | 3018 { |
2384 | 3019 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458 | 3020 return *this; |
3021 } | |
3022 | |
3023 if (nr == 0 || nc == 0) | |
3024 return *this; | |
3025 | |
3026 Complex *d = fortran_vec (); // Ensures only one reference to my privates! | |
3027 | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9528
diff
changeset
|
3028 mx_inline_add2 (length (), d, a.data ()); |
458 | 3029 return *this; |
3030 } | |
3031 | |
3032 ComplexMatrix& | |
3033 ComplexMatrix::operator -= (const Matrix& a) | |
3034 { | |
5275 | 3035 octave_idx_type nr = rows (); |
3036 octave_idx_type nc = cols (); | |
3037 | |
3038 octave_idx_type a_nr = a.rows (); | |
3039 octave_idx_type a_nc = a.cols (); | |
2384 | 3040 |
3041 if (nr != a_nr || nc != a_nc) | |
458 | 3042 { |
2384 | 3043 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458 | 3044 return *this; |
3045 } | |
3046 | |
3047 if (nr == 0 || nc == 0) | |
3048 return *this; | |
3049 | |
3050 Complex *d = fortran_vec (); // Ensures only one reference to my privates! | |
3051 | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9528
diff
changeset
|
3052 mx_inline_sub2 (length (), d, a.data ()); |
458 | 3053 return *this; |
3054 } | |
3055 | |
3056 // unary operations | |
3057 | |
2964 | 3058 boolMatrix |
458 | 3059 ComplexMatrix::operator ! (void) const |
3060 { | |
9553
0c72d9284087
further bool ops tweaks
Jaroslav Hajek <highegg@gmail.com>
parents:
9551
diff
changeset
|
3061 return do_mx_unary_op<boolMatrix, ComplexMatrix> (*this, mx_inline_not); |
458 | 3062 } |
3063 | |
3064 // other operations | |
3065 | |
2676 | 3066 Matrix |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3067 ComplexMatrix::map (dmapper fcn) const |
458 | 3068 { |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3069 return MArray2<Complex>::map<double> (func_ptr (fcn)); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3070 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3071 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3072 ComplexMatrix |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3073 ComplexMatrix::map (cmapper fcn) const |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3074 { |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3075 return MArray2<Complex>::map<Complex> (func_ptr (fcn)); |
3248 | 3076 } |
3077 | |
3078 boolMatrix | |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3079 ComplexMatrix::map (bmapper fcn) const |
3248 | 3080 { |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3081 return MArray2<Complex>::map<bool> (func_ptr (fcn)); |
458 | 3082 } |
3083 | |
2384 | 3084 bool |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3085 ComplexMatrix::any_element_is_nan (void) const |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3086 { |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3087 octave_idx_type nr = rows (); |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3088 octave_idx_type nc = cols (); |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3089 |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3090 for (octave_idx_type j = 0; j < nc; j++) |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3091 for (octave_idx_type i = 0; i < nr; i++) |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3092 { |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3093 Complex val = elem (i, j); |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3094 if (xisnan (val)) |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3095 return true; |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3096 } |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3097 |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3098 return false; |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3099 } |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3100 |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3101 bool |
2384 | 3102 ComplexMatrix::any_element_is_inf_or_nan (void) const |
3103 { | |
5275 | 3104 octave_idx_type nr = rows (); |
3105 octave_idx_type nc = cols (); | |
3106 | |
3107 for (octave_idx_type j = 0; j < nc; j++) | |
3108 for (octave_idx_type i = 0; i < nr; i++) | |
2384 | 3109 { |
3110 Complex val = elem (i, j); | |
3111 if (xisinf (val) || xisnan (val)) | |
3112 return true; | |
3113 } | |
3114 | |
3115 return false; | |
3116 } | |
3117 | |
2408 | 3118 // Return true if no elements have imaginary components. |
3119 | |
3120 bool | |
3121 ComplexMatrix::all_elements_are_real (void) const | |
3122 { | |
5275 | 3123 octave_idx_type nr = rows (); |
3124 octave_idx_type nc = cols (); | |
3125 | |
3126 for (octave_idx_type j = 0; j < nc; j++) | |
4349 | 3127 { |
5275 | 3128 for (octave_idx_type i = 0; i < nr; i++) |
4349 | 3129 { |
5315 | 3130 double ip = std::imag (elem (i, j)); |
4349 | 3131 |
3132 if (ip != 0.0 || lo_ieee_signbit (ip)) | |
3133 return false; | |
3134 } | |
3135 } | |
2408 | 3136 |
3137 return true; | |
3138 } | |
3139 | |
1968 | 3140 // Return nonzero if any element of CM has a non-integer real or |
3141 // imaginary part. Also extract the largest and smallest (real or | |
3142 // imaginary) values and return them in MAX_VAL and MIN_VAL. | |
3143 | |
2384 | 3144 bool |
1968 | 3145 ComplexMatrix::all_integers (double& max_val, double& min_val) const |
3146 { | |
5275 | 3147 octave_idx_type nr = rows (); |
3148 octave_idx_type nc = cols (); | |
1968 | 3149 |
3150 if (nr > 0 && nc > 0) | |
3151 { | |
3152 Complex val = elem (0, 0); | |
3153 | |
5315 | 3154 double r_val = std::real (val); |
3155 double i_val = std::imag (val); | |
1968 | 3156 |
3157 max_val = r_val; | |
3158 min_val = r_val; | |
3159 | |
3160 if (i_val > max_val) | |
3161 max_val = i_val; | |
3162 | |
3163 if (i_val < max_val) | |
3164 min_val = i_val; | |
3165 } | |
3166 else | |
2384 | 3167 return false; |
1968 | 3168 |
5275 | 3169 for (octave_idx_type j = 0; j < nc; j++) |
3170 for (octave_idx_type i = 0; i < nr; i++) | |
1968 | 3171 { |
3172 Complex val = elem (i, j); | |
3173 | |
5315 | 3174 double r_val = std::real (val); |
3175 double i_val = std::imag (val); | |
1968 | 3176 |
3177 if (r_val > max_val) | |
3178 max_val = r_val; | |
3179 | |
3180 if (i_val > max_val) | |
3181 max_val = i_val; | |
3182 | |
3183 if (r_val < min_val) | |
3184 min_val = r_val; | |
3185 | |
3186 if (i_val < min_val) | |
3187 min_val = i_val; | |
3188 | |
3189 if (D_NINT (r_val) != r_val || D_NINT (i_val) != i_val) | |
2384 | 3190 return false; |
1968 | 3191 } |
2384 | 3192 |
3193 return true; | |
1968 | 3194 } |
3195 | |
2384 | 3196 bool |
1968 | 3197 ComplexMatrix::too_large_for_float (void) const |
3198 { | |
5275 | 3199 octave_idx_type nr = rows (); |
3200 octave_idx_type nc = cols (); | |
3201 | |
3202 for (octave_idx_type j = 0; j < nc; j++) | |
3203 for (octave_idx_type i = 0; i < nr; i++) | |
1968 | 3204 { |
3205 Complex val = elem (i, j); | |
3206 | |
5315 | 3207 double r_val = std::real (val); |
3208 double i_val = std::imag (val); | |
1968 | 3209 |
5389 | 3210 if ((! (xisnan (r_val) || xisinf (r_val)) |
5387 | 3211 && fabs (r_val) > FLT_MAX) |
5389 | 3212 || (! (xisnan (i_val) || xisinf (i_val)) |
5387 | 3213 && fabs (i_val) > FLT_MAX)) |
2384 | 3214 return true; |
1968 | 3215 } |
3216 | |
2384 | 3217 return false; |
1968 | 3218 } |
3219 | |
5775 | 3220 // FIXME Do these really belong here? Maybe they should be |
4015 | 3221 // in a base class? |
3222 | |
2832 | 3223 boolMatrix |
4015 | 3224 ComplexMatrix::all (int dim) const |
458 | 3225 { |
9227
8145f2255276
use explicit template qualifs to please Intel C++ and MSVC++
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
3226 return do_mx_red_op<boolMatrix, Complex> (*this, dim, mx_inline_all); |
458 | 3227 } |
3228 | |
2832 | 3229 boolMatrix |
4015 | 3230 ComplexMatrix::any (int dim) const |
458 | 3231 { |
9227
8145f2255276
use explicit template qualifs to please Intel C++ and MSVC++
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
3232 return do_mx_red_op<boolMatrix, Complex> (*this, dim, mx_inline_any); |
458 | 3233 } |
3234 | |
3235 ComplexMatrix | |
3723 | 3236 ComplexMatrix::cumprod (int dim) const |
458 | 3237 { |
9227
8145f2255276
use explicit template qualifs to please Intel C++ and MSVC++
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
3238 return do_mx_cum_op<ComplexMatrix, Complex> (*this, dim, mx_inline_cumprod); |
458 | 3239 } |
3240 | |
3241 ComplexMatrix | |
3723 | 3242 ComplexMatrix::cumsum (int dim) const |
458 | 3243 { |
9227
8145f2255276
use explicit template qualifs to please Intel C++ and MSVC++
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
3244 return do_mx_cum_op<ComplexMatrix, Complex> (*this, dim, mx_inline_cumsum); |
458 | 3245 } |
3246 | |
3247 ComplexMatrix | |
3723 | 3248 ComplexMatrix::prod (int dim) const |
458 | 3249 { |
9227
8145f2255276
use explicit template qualifs to please Intel C++ and MSVC++
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
3250 return do_mx_red_op<ComplexMatrix, Complex> (*this, dim, mx_inline_prod); |
458 | 3251 } |
3252 | |
3253 ComplexMatrix | |
3723 | 3254 ComplexMatrix::sum (int dim) const |
458 | 3255 { |
9227
8145f2255276
use explicit template qualifs to please Intel C++ and MSVC++
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
3256 return do_mx_red_op<ComplexMatrix, Complex> (*this, dim, mx_inline_sum); |
458 | 3257 } |
3258 | |
3259 ComplexMatrix | |
3723 | 3260 ComplexMatrix::sumsq (int dim) const |
458 | 3261 { |
9227
8145f2255276
use explicit template qualifs to please Intel C++ and MSVC++
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
3262 return do_mx_red_op<Matrix, Complex> (*this, dim, mx_inline_sumsq); |
458 | 3263 } |
3264 | |
4329 | 3265 Matrix ComplexMatrix::abs (void) const |
3266 { | |
8650
a1ae2aae903e
abs,real,imag,conj: use code from mx-inlines rather than the generic map
Jaroslav Hajek <highegg@gmail.com>
parents:
8614
diff
changeset
|
3267 return Matrix (mx_inline_cabs_dup (data (), length ()), |
a1ae2aae903e
abs,real,imag,conj: use code from mx-inlines rather than the generic map
Jaroslav Hajek <highegg@gmail.com>
parents:
8614
diff
changeset
|
3268 rows (), cols ()); |
4329 | 3269 } |
3270 | |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7544
diff
changeset
|
3271 ComplexMatrix |
5275 | 3272 ComplexMatrix::diag (octave_idx_type k) const |
458 | 3273 { |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7544
diff
changeset
|
3274 return MArray2<Complex>::diag (k); |
458 | 3275 } |
3276 | |
2354 | 3277 bool |
5275 | 3278 ComplexMatrix::row_is_real_only (octave_idx_type i) const |
2354 | 3279 { |
3280 bool retval = true; | |
3281 | |
5275 | 3282 octave_idx_type nc = columns (); |
3283 | |
3284 for (octave_idx_type j = 0; j < nc; j++) | |
2354 | 3285 { |
5315 | 3286 if (std::imag (elem (i, j)) != 0.0) |
2354 | 3287 { |
3288 retval = false; | |
3289 break; | |
3290 } | |
3291 } | |
3292 | |
3293 return retval; | |
3294 } | |
3295 | |
3296 bool | |
5275 | 3297 ComplexMatrix::column_is_real_only (octave_idx_type j) const |
2354 | 3298 { |
3299 bool retval = true; | |
3300 | |
5275 | 3301 octave_idx_type nr = rows (); |
3302 | |
3303 for (octave_idx_type i = 0; i < nr; i++) | |
2354 | 3304 { |
5315 | 3305 if (std::imag (elem (i, j)) != 0.0) |
2354 | 3306 { |
3307 retval = false; | |
3308 break; | |
3309 } | |
3310 } | |
3311 | |
3312 return retval; | |
3313 } | |
891 | 3314 |
458 | 3315 ComplexColumnVector |
3316 ComplexMatrix::row_min (void) const | |
3317 { | |
5275 | 3318 Array<octave_idx_type> dummy_idx; |
4587 | 3319 return row_min (dummy_idx); |
458 | 3320 } |
3321 | |
3322 ComplexColumnVector | |
5275 | 3323 ComplexMatrix::row_min (Array<octave_idx_type>& idx_arg) const |
458 | 3324 { |
3325 ComplexColumnVector result; | |
3326 | |
5275 | 3327 octave_idx_type nr = rows (); |
3328 octave_idx_type nc = cols (); | |
458 | 3329 |
3330 if (nr > 0 && nc > 0) | |
3331 { | |
3332 result.resize (nr); | |
4587 | 3333 idx_arg.resize (nr); |
458 | 3334 |
5275 | 3335 for (octave_idx_type i = 0; i < nr; i++) |
458 | 3336 { |
2354 | 3337 bool real_only = row_is_real_only (i); |
3338 | |
5275 | 3339 octave_idx_type idx_j; |
4469 | 3340 |
3341 Complex tmp_min; | |
3342 | |
3343 double abs_min = octave_NaN; | |
3344 | |
3345 for (idx_j = 0; idx_j < nc; idx_j++) | |
3346 { | |
3347 tmp_min = elem (i, idx_j); | |
3348 | |
5389 | 3349 if (! xisnan (tmp_min)) |
4469 | 3350 { |
5315 | 3351 abs_min = real_only ? std::real (tmp_min) : std::abs (tmp_min); |
4469 | 3352 break; |
3353 } | |
3354 } | |
3355 | |
5275 | 3356 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469 | 3357 { |
3358 Complex tmp = elem (i, j); | |
3359 | |
5389 | 3360 if (xisnan (tmp)) |
4469 | 3361 continue; |
3362 | |
5315 | 3363 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469 | 3364 |
3365 if (abs_tmp < abs_min) | |
3366 { | |
3367 idx_j = j; | |
3368 tmp_min = tmp; | |
3369 abs_min = abs_tmp; | |
3370 } | |
3371 } | |
3372 | |
5389 | 3373 if (xisnan (tmp_min)) |
4469 | 3374 { |
3375 result.elem (i) = Complex_NaN_result; | |
4587 | 3376 idx_arg.elem (i) = 0; |
4469 | 3377 } |
891 | 3378 else |
3379 { | |
4469 | 3380 result.elem (i) = tmp_min; |
4587 | 3381 idx_arg.elem (i) = idx_j; |
891 | 3382 } |
458 | 3383 } |
3384 } | |
3385 | |
3386 return result; | |
3387 } | |
3388 | |
3389 ComplexColumnVector | |
3390 ComplexMatrix::row_max (void) const | |
3391 { | |
5275 | 3392 Array<octave_idx_type> dummy_idx; |
4587 | 3393 return row_max (dummy_idx); |
458 | 3394 } |
3395 | |
3396 ComplexColumnVector | |
5275 | 3397 ComplexMatrix::row_max (Array<octave_idx_type>& idx_arg) const |
458 | 3398 { |
3399 ComplexColumnVector result; | |
3400 | |
5275 | 3401 octave_idx_type nr = rows (); |
3402 octave_idx_type nc = cols (); | |
458 | 3403 |
3404 if (nr > 0 && nc > 0) | |
3405 { | |
3406 result.resize (nr); | |
4587 | 3407 idx_arg.resize (nr); |
458 | 3408 |
5275 | 3409 for (octave_idx_type i = 0; i < nr; i++) |
458 | 3410 { |
2354 | 3411 bool real_only = row_is_real_only (i); |
3412 | |
5275 | 3413 octave_idx_type idx_j; |
4469 | 3414 |
3415 Complex tmp_max; | |
3416 | |
3417 double abs_max = octave_NaN; | |
3418 | |
3419 for (idx_j = 0; idx_j < nc; idx_j++) | |
3420 { | |
3421 tmp_max = elem (i, idx_j); | |
3422 | |
5389 | 3423 if (! xisnan (tmp_max)) |
4469 | 3424 { |
5315 | 3425 abs_max = real_only ? std::real (tmp_max) : std::abs (tmp_max); |
4469 | 3426 break; |
3427 } | |
3428 } | |
3429 | |
5275 | 3430 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469 | 3431 { |
3432 Complex tmp = elem (i, j); | |
3433 | |
5389 | 3434 if (xisnan (tmp)) |
4469 | 3435 continue; |
3436 | |
5315 | 3437 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469 | 3438 |
3439 if (abs_tmp > abs_max) | |
3440 { | |
3441 idx_j = j; | |
3442 tmp_max = tmp; | |
3443 abs_max = abs_tmp; | |
3444 } | |
3445 } | |
3446 | |
5389 | 3447 if (xisnan (tmp_max)) |
4469 | 3448 { |
3449 result.elem (i) = Complex_NaN_result; | |
4587 | 3450 idx_arg.elem (i) = 0; |
4469 | 3451 } |
891 | 3452 else |
3453 { | |
4469 | 3454 result.elem (i) = tmp_max; |
4587 | 3455 idx_arg.elem (i) = idx_j; |
891 | 3456 } |
458 | 3457 } |
3458 } | |
3459 | |
3460 return result; | |
3461 } | |
3462 | |
3463 ComplexRowVector | |
3464 ComplexMatrix::column_min (void) const | |
3465 { | |
5275 | 3466 Array<octave_idx_type> dummy_idx; |
4587 | 3467 return column_min (dummy_idx); |
458 | 3468 } |
3469 | |
3470 ComplexRowVector | |
5275 | 3471 ComplexMatrix::column_min (Array<octave_idx_type>& idx_arg) const |
458 | 3472 { |
3473 ComplexRowVector result; | |
3474 | |
5275 | 3475 octave_idx_type nr = rows (); |
3476 octave_idx_type nc = cols (); | |
458 | 3477 |
3478 if (nr > 0 && nc > 0) | |
3479 { | |
3480 result.resize (nc); | |
4587 | 3481 idx_arg.resize (nc); |
458 | 3482 |
5275 | 3483 for (octave_idx_type j = 0; j < nc; j++) |
458 | 3484 { |
2354 | 3485 bool real_only = column_is_real_only (j); |
3486 | |
5275 | 3487 octave_idx_type idx_i; |
4469 | 3488 |
3489 Complex tmp_min; | |
3490 | |
3491 double abs_min = octave_NaN; | |
3492 | |
3493 for (idx_i = 0; idx_i < nr; idx_i++) | |
3494 { | |
3495 tmp_min = elem (idx_i, j); | |
3496 | |
5389 | 3497 if (! xisnan (tmp_min)) |
4469 | 3498 { |
5315 | 3499 abs_min = real_only ? std::real (tmp_min) : std::abs (tmp_min); |
4469 | 3500 break; |
3501 } | |
3502 } | |
3503 | |
5275 | 3504 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469 | 3505 { |
3506 Complex tmp = elem (i, j); | |
3507 | |
5389 | 3508 if (xisnan (tmp)) |
4469 | 3509 continue; |
3510 | |
5315 | 3511 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469 | 3512 |
3513 if (abs_tmp < abs_min) | |
3514 { | |
3515 idx_i = i; | |
3516 tmp_min = tmp; | |
3517 abs_min = abs_tmp; | |
3518 } | |
3519 } | |
3520 | |
5389 | 3521 if (xisnan (tmp_min)) |
4469 | 3522 { |
3523 result.elem (j) = Complex_NaN_result; | |
4587 | 3524 idx_arg.elem (j) = 0; |
4469 | 3525 } |
891 | 3526 else |
3527 { | |
4469 | 3528 result.elem (j) = tmp_min; |
4587 | 3529 idx_arg.elem (j) = idx_i; |
891 | 3530 } |
458 | 3531 } |
3532 } | |
3533 | |
3534 return result; | |
3535 } | |
3536 | |
3537 ComplexRowVector | |
3538 ComplexMatrix::column_max (void) const | |
3539 { | |
5275 | 3540 Array<octave_idx_type> dummy_idx; |
4587 | 3541 return column_max (dummy_idx); |
458 | 3542 } |
3543 | |
3544 ComplexRowVector | |
5275 | 3545 ComplexMatrix::column_max (Array<octave_idx_type>& idx_arg) const |
458 | 3546 { |
3547 ComplexRowVector result; | |
3548 | |
5275 | 3549 octave_idx_type nr = rows (); |
3550 octave_idx_type nc = cols (); | |
458 | 3551 |
3552 if (nr > 0 && nc > 0) | |
3553 { | |
3554 result.resize (nc); | |
4587 | 3555 idx_arg.resize (nc); |
458 | 3556 |
5275 | 3557 for (octave_idx_type j = 0; j < nc; j++) |
458 | 3558 { |
2354 | 3559 bool real_only = column_is_real_only (j); |
3560 | |
5275 | 3561 octave_idx_type idx_i; |
4469 | 3562 |
3563 Complex tmp_max; | |
3564 | |
3565 double abs_max = octave_NaN; | |
3566 | |
3567 for (idx_i = 0; idx_i < nr; idx_i++) | |
3568 { | |
3569 tmp_max = elem (idx_i, j); | |
3570 | |
5389 | 3571 if (! xisnan (tmp_max)) |
4469 | 3572 { |
5315 | 3573 abs_max = real_only ? std::real (tmp_max) : std::abs (tmp_max); |
4469 | 3574 break; |
3575 } | |
3576 } | |
3577 | |
5275 | 3578 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469 | 3579 { |
3580 Complex tmp = elem (i, j); | |
3581 | |
5389 | 3582 if (xisnan (tmp)) |
4469 | 3583 continue; |
3584 | |
5315 | 3585 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469 | 3586 |
3587 if (abs_tmp > abs_max) | |
3588 { | |
3589 idx_i = i; | |
3590 tmp_max = tmp; | |
3591 abs_max = abs_tmp; | |
3592 } | |
3593 } | |
3594 | |
5389 | 3595 if (xisnan (tmp_max)) |
4469 | 3596 { |
3597 result.elem (j) = Complex_NaN_result; | |
4587 | 3598 idx_arg.elem (j) = 0; |
4469 | 3599 } |
891 | 3600 else |
3601 { | |
4469 | 3602 result.elem (j) = tmp_max; |
4587 | 3603 idx_arg.elem (j) = idx_i; |
891 | 3604 } |
458 | 3605 } |
3606 } | |
3607 | |
3608 return result; | |
3609 } | |
3610 | |
3611 // i/o | |
3612 | |
3504 | 3613 std::ostream& |
3614 operator << (std::ostream& os, const ComplexMatrix& a) | |
458 | 3615 { |
5275 | 3616 for (octave_idx_type i = 0; i < a.rows (); i++) |
458 | 3617 { |
5275 | 3618 for (octave_idx_type j = 0; j < a.cols (); j++) |
4130 | 3619 { |
3620 os << " "; | |
3621 octave_write_complex (os, a.elem (i, j)); | |
3622 } | |
458 | 3623 os << "\n"; |
3624 } | |
3625 return os; | |
3626 } | |
3627 | |
3504 | 3628 std::istream& |
3629 operator >> (std::istream& is, ComplexMatrix& a) | |
458 | 3630 { |
5275 | 3631 octave_idx_type nr = a.rows (); |
3632 octave_idx_type nc = a.cols (); | |
458 | 3633 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8956
diff
changeset
|
3634 if (nr > 0 && nc > 0) |
458 | 3635 { |
3636 Complex tmp; | |
5275 | 3637 for (octave_idx_type i = 0; i < nr; i++) |
3638 for (octave_idx_type j = 0; j < nc; j++) | |
458 | 3639 { |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9359
diff
changeset
|
3640 tmp = octave_read_value<Complex> (is); |
458 | 3641 if (is) |
3642 a.elem (i, j) = tmp; | |
3643 else | |
2993 | 3644 goto done; |
458 | 3645 } |
3646 } | |
3647 | |
2993 | 3648 done: |
3649 | |
458 | 3650 return is; |
3651 } | |
3652 | |
1819 | 3653 ComplexMatrix |
3654 Givens (const Complex& x, const Complex& y) | |
3655 { | |
3656 double cc; | |
3657 Complex cs, temp_r; | |
3658 | |
3887 | 3659 F77_FUNC (zlartg, ZLARTG) (x, y, cc, cs, temp_r); |
1819 | 3660 |
3661 ComplexMatrix g (2, 2); | |
3662 | |
3663 g.elem (0, 0) = cc; | |
3664 g.elem (1, 1) = cc; | |
3665 g.elem (0, 1) = cs; | |
3666 g.elem (1, 0) = -conj (cs); | |
3667 | |
3668 return g; | |
3669 } | |
3670 | |
3671 ComplexMatrix | |
3672 Sylvester (const ComplexMatrix& a, const ComplexMatrix& b, | |
3673 const ComplexMatrix& c) | |
3674 { | |
3675 ComplexMatrix retval; | |
3676 | |
5775 | 3677 // FIXME -- need to check that a, b, and c are all the same |
1819 | 3678 // size. |
3679 | |
3680 // Compute Schur decompositions | |
3681 | |
3682 ComplexSCHUR as (a, "U"); | |
3683 ComplexSCHUR bs (b, "U"); | |
3684 | |
3685 // Transform c to new coordinates. | |
3686 | |
3687 ComplexMatrix ua = as.unitary_matrix (); | |
3688 ComplexMatrix sch_a = as.schur_matrix (); | |
3689 | |
3690 ComplexMatrix ub = bs.unitary_matrix (); | |
3691 ComplexMatrix sch_b = bs.schur_matrix (); | |
3692 | |
3693 ComplexMatrix cx = ua.hermitian () * c * ub; | |
3694 | |
3695 // Solve the sylvester equation, back-transform, and return the | |
3696 // solution. | |
3697 | |
5275 | 3698 octave_idx_type a_nr = a.rows (); |
3699 octave_idx_type b_nr = b.rows (); | |
1819 | 3700 |
3701 double scale; | |
5275 | 3702 octave_idx_type info; |
1950 | 3703 |
3704 Complex *pa = sch_a.fortran_vec (); | |
3705 Complex *pb = sch_b.fortran_vec (); | |
3706 Complex *px = cx.fortran_vec (); | |
1819 | 3707 |
4552 | 3708 F77_XFCN (ztrsyl, ZTRSYL, (F77_CONST_CHAR_ARG2 ("N", 1), |
3709 F77_CONST_CHAR_ARG2 ("N", 1), | |
3710 1, a_nr, b_nr, pa, a_nr, pb, | |
3711 b_nr, px, a_nr, scale, info | |
3712 F77_CHAR_ARG_LEN (1) | |
3713 F77_CHAR_ARG_LEN (1))); | |
1950 | 3714 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
3715 // FIXME -- check info? |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
3716 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
3717 retval = -ua * cx * ub.hermitian (); |
1819 | 3718 |
3719 return retval; | |
3720 } | |
3721 | |
2828 | 3722 ComplexMatrix |
3723 operator * (const ComplexMatrix& m, const Matrix& a) | |
3724 { | |
3725 ComplexMatrix tmp (a); | |
3726 return m * tmp; | |
3727 } | |
3728 | |
3729 ComplexMatrix | |
3730 operator * (const Matrix& m, const ComplexMatrix& a) | |
3731 { | |
3732 ComplexMatrix tmp (m); | |
3733 return tmp * a; | |
3734 } | |
3735 | |
6162 | 3736 /* Simple Dot Product, Matrix-Vector and Matrix-Matrix Unit tests |
3737 %!assert([1+i 2+i 3+i] * [ 4+i ; 5+i ; 6+i], 29+21i, 1e-14) | |
3738 %!assert([1+i 2+i ; 3+i 4+i ] * [5+i ; 6+i], [15 + 14i ; 37 + 18i], 1e-14) | |
3739 %!assert([1+i 2+i ; 3+i 4+i ] * [5+i 6+i ; 7+i 8+i], [17 + 15i 20 + 17i; 41 + 19i 48 + 21i], 1e-14) | |
9526
f3ebc728ffd7
fix typos in complex xgemm
Jaroslav Hajek <highegg@gmail.com>
parents:
9523
diff
changeset
|
3740 %!assert([1 i]*[i 0]', -i); |
6162 | 3741 */ |
3742 | |
3743 /* Test some simple identities | |
3744 %!shared M, cv, rv | |
3745 %! M = randn(10,10)+i*rand(10,10); | |
3746 %! cv = randn(10,1)+i*rand(10,1); | |
3747 %! rv = randn(1,10)+i*rand(1,10); | |
3748 %!assert([M*cv,M*cv],M*[cv,cv],1e-14) | |
9528
ec066ba012c8
more fixes & tests for matrix multiply
Jaroslav Hajek <highegg@gmail.com>
parents:
9526
diff
changeset
|
3749 %!assert([M.'*cv,M.'*cv],M.'*[cv,cv],1e-14) |
ec066ba012c8
more fixes & tests for matrix multiply
Jaroslav Hajek <highegg@gmail.com>
parents:
9526
diff
changeset
|
3750 %!assert([M'*cv,M'*cv],M'*[cv,cv],1e-14) |
6162 | 3751 %!assert([rv*M;rv*M],[rv;rv]*M,1e-14) |
9528
ec066ba012c8
more fixes & tests for matrix multiply
Jaroslav Hajek <highegg@gmail.com>
parents:
9526
diff
changeset
|
3752 %!assert([rv*M.';rv*M.'],[rv;rv]*M.',1e-14) |
ec066ba012c8
more fixes & tests for matrix multiply
Jaroslav Hajek <highegg@gmail.com>
parents:
9526
diff
changeset
|
3753 %!assert([rv*M';rv*M'],[rv;rv]*M',1e-14) |
6162 | 3754 %!assert(2*rv*cv,[rv,rv]*[cv;cv],1e-14) |
3755 */ | |
3756 | |
7800
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3757 static const char * |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3758 get_blas_trans_arg (bool trans, bool conj) |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3759 { |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3760 static char blas_notrans = 'N', blas_trans = 'T', blas_conj_trans = 'C'; |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3761 return trans ? (conj ? &blas_conj_trans : &blas_trans) : &blas_notrans; |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3762 } |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3763 |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3764 // the general GEMM operation |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3765 |
2828 | 3766 ComplexMatrix |
7800
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3767 xgemm (bool transa, bool conja, const ComplexMatrix& a, |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3768 bool transb, bool conjb, const ComplexMatrix& b) |
2828 | 3769 { |
3770 ComplexMatrix retval; | |
3771 | |
7800
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3772 // conjugacy is ignored if no transpose |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3773 conja = conja && transa; |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3774 conjb = conjb && transb; |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3775 |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3776 octave_idx_type a_nr = transa ? a.cols () : a.rows (); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3777 octave_idx_type a_nc = transa ? a.rows () : a.cols (); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3778 |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3779 octave_idx_type b_nr = transb ? b.cols () : b.rows (); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3780 octave_idx_type b_nc = transb ? b.rows () : b.cols (); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3781 |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3782 if (a_nc != b_nr) |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3783 gripe_nonconformant ("operator *", a_nr, a_nc, b_nr, b_nc); |
2828 | 3784 else |
3785 { | |
7800
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3786 if (a_nr == 0 || a_nc == 0 || b_nc == 0) |
9359
be6867ba8104
avoid useless zero initialization when doing matrix multiply
Jaroslav Hajek <highegg@gmail.com>
parents:
9227
diff
changeset
|
3787 retval = ComplexMatrix (a_nr, b_nc, 0.0); |
7801
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3788 else if (a.data () == b.data () && a_nr == b_nc && transa != transb) |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3789 { |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3790 octave_idx_type lda = a.rows (); |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3791 |
9359
be6867ba8104
avoid useless zero initialization when doing matrix multiply
Jaroslav Hajek <highegg@gmail.com>
parents:
9227
diff
changeset
|
3792 retval = ComplexMatrix (a_nr, b_nc); |
7801
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3793 Complex *c = retval.fortran_vec (); |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3794 |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3795 const char *ctransa = get_blas_trans_arg (transa, conja); |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3796 if (conja || conjb) |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3797 { |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3798 F77_XFCN (zherk, ZHERK, (F77_CONST_CHAR_ARG2 ("U", 1), |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3799 F77_CONST_CHAR_ARG2 (ctransa, 1), |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3800 a_nr, a_nc, 1.0, |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3801 a.data (), lda, 0.0, c, a_nr |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3802 F77_CHAR_ARG_LEN (1) |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3803 F77_CHAR_ARG_LEN (1))); |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3804 for (int j = 0; j < a_nr; j++) |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3805 for (int i = 0; i < j; i++) |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3806 retval.xelem (j,i) = std::conj (retval.xelem (i,j)); |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3807 } |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3808 else |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3809 { |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3810 F77_XFCN (zsyrk, ZSYRK, (F77_CONST_CHAR_ARG2 ("U", 1), |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3811 F77_CONST_CHAR_ARG2 (ctransa, 1), |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3812 a_nr, a_nc, 1.0, |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3813 a.data (), lda, 0.0, c, a_nr |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3814 F77_CHAR_ARG_LEN (1) |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3815 F77_CHAR_ARG_LEN (1))); |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3816 for (int j = 0; j < a_nr; j++) |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3817 for (int i = 0; i < j; i++) |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3818 retval.xelem (j,i) = retval.xelem (i,j); |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3819 |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3820 } |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3821 |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3822 } |
2828 | 3823 else |
3824 { | |
7800
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3825 octave_idx_type lda = a.rows (), tda = a.cols (); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3826 octave_idx_type ldb = b.rows (), tdb = b.cols (); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3827 |
9359
be6867ba8104
avoid useless zero initialization when doing matrix multiply
Jaroslav Hajek <highegg@gmail.com>
parents:
9227
diff
changeset
|
3828 retval = ComplexMatrix (a_nr, b_nc); |
2828 | 3829 Complex *c = retval.fortran_vec (); |
3830 | |
7800
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3831 if (b_nc == 1 && a_nr == 1) |
5983 | 3832 { |
7800
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3833 if (conja == conjb) |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3834 { |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3835 F77_FUNC (xzdotu, XZDOTU) (a_nc, a.data (), 1, b.data (), 1, *c); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3836 if (conja) *c = std::conj (*c); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3837 } |
9526
f3ebc728ffd7
fix typos in complex xgemm
Jaroslav Hajek <highegg@gmail.com>
parents:
9523
diff
changeset
|
3838 else if (conja) |
7800
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3839 F77_FUNC (xzdotc, XZDOTC) (a_nc, a.data (), 1, b.data (), 1, *c); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3840 else |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3841 F77_FUNC (xzdotc, XZDOTC) (a_nc, b.data (), 1, a.data (), 1, *c); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3842 } |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3843 else if (b_nc == 1 && ! conjb) |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3844 { |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3845 const char *ctransa = get_blas_trans_arg (transa, conja); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3846 F77_XFCN (zgemv, ZGEMV, (F77_CONST_CHAR_ARG2 (ctransa, 1), |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3847 lda, tda, 1.0, a.data (), lda, |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3848 b.data (), 1, 0.0, c, 1 |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3849 F77_CHAR_ARG_LEN (1))); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3850 } |
9528
ec066ba012c8
more fixes & tests for matrix multiply
Jaroslav Hajek <highegg@gmail.com>
parents:
9526
diff
changeset
|
3851 else if (a_nr == 1 && ! conja && ! conjb) |
7800
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3852 { |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3853 const char *crevtransb = get_blas_trans_arg (! transb, conjb); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3854 F77_XFCN (zgemv, ZGEMV, (F77_CONST_CHAR_ARG2 (crevtransb, 1), |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3855 ldb, tdb, 1.0, b.data (), ldb, |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3856 a.data (), 1, 0.0, c, 1 |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3857 F77_CHAR_ARG_LEN (1))); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3858 } |
5983 | 3859 else |
6390 | 3860 { |
7800
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3861 const char *ctransa = get_blas_trans_arg (transa, conja); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3862 const char *ctransb = get_blas_trans_arg (transb, conjb); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3863 F77_XFCN (zgemm, ZGEMM, (F77_CONST_CHAR_ARG2 (ctransa, 1), |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3864 F77_CONST_CHAR_ARG2 (ctransb, 1), |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3865 a_nr, b_nc, a_nc, 1.0, a.data (), |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3866 lda, b.data (), ldb, 0.0, c, a_nr |
6390 | 3867 F77_CHAR_ARG_LEN (1) |
3868 F77_CHAR_ARG_LEN (1))); | |
3869 } | |
2828 | 3870 } |
3871 } | |
3872 | |
3873 return retval; | |
3874 } | |
3875 | |
7800
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3876 ComplexMatrix |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3877 operator * (const ComplexMatrix& a, const ComplexMatrix& b) |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3878 { |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3879 return xgemm (false, false, a, false, false, b); |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3880 } |
5861b95e9879
support for compound operators, implement trans_mul, mul_trans, herm_mul and mul_herm
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
3881 |
5775 | 3882 // FIXME -- it would be nice to share code among the min/max |
4309 | 3883 // functions below. |
3884 | |
3885 #define EMPTY_RETURN_CHECK(T) \ | |
3886 if (nr == 0 || nc == 0) \ | |
3887 return T (nr, nc); | |
3888 | |
3889 ComplexMatrix | |
3890 min (const Complex& c, const ComplexMatrix& m) | |
3891 { | |
5275 | 3892 octave_idx_type nr = m.rows (); |
3893 octave_idx_type nc = m.columns (); | |
4309 | 3894 |
3895 EMPTY_RETURN_CHECK (ComplexMatrix); | |
3896 | |
3897 ComplexMatrix result (nr, nc); | |
3898 | |
5275 | 3899 for (octave_idx_type j = 0; j < nc; j++) |
3900 for (octave_idx_type i = 0; i < nr; i++) | |
4309 | 3901 { |
3902 OCTAVE_QUIT; | |
3903 result (i, j) = xmin (c, m (i, j)); | |
3904 } | |
3905 | |
3906 return result; | |
3907 } | |
3908 | |
3909 ComplexMatrix | |
3910 min (const ComplexMatrix& m, const Complex& c) | |
3911 { | |
5275 | 3912 octave_idx_type nr = m.rows (); |
3913 octave_idx_type nc = m.columns (); | |
4309 | 3914 |
3915 EMPTY_RETURN_CHECK (ComplexMatrix); | |
3916 | |
3917 ComplexMatrix result (nr, nc); | |
3918 | |
5275 | 3919 for (octave_idx_type j = 0; j < nc; j++) |
3920 for (octave_idx_type i = 0; i < nr; i++) | |
4309 | 3921 { |
3922 OCTAVE_QUIT; | |
3923 result (i, j) = xmin (m (i, j), c); | |
3924 } | |
3925 | |
3926 return result; | |
3927 } | |
3928 | |
3929 ComplexMatrix | |
3930 min (const ComplexMatrix& a, const ComplexMatrix& b) | |
3931 { | |
5275 | 3932 octave_idx_type nr = a.rows (); |
3933 octave_idx_type nc = a.columns (); | |
4309 | 3934 |
3935 if (nr != b.rows () || nc != b.columns ()) | |
3936 { | |
3937 (*current_liboctave_error_handler) | |
3938 ("two-arg min expecting args of same size"); | |
3939 return ComplexMatrix (); | |
3940 } | |
3941 | |
3942 EMPTY_RETURN_CHECK (ComplexMatrix); | |
3943 | |
3944 ComplexMatrix result (nr, nc); | |
3945 | |
5275 | 3946 for (octave_idx_type j = 0; j < nc; j++) |
4309 | 3947 { |
3948 int columns_are_real_only = 1; | |
5275 | 3949 for (octave_idx_type i = 0; i < nr; i++) |
4309 | 3950 { |
3951 OCTAVE_QUIT; | |
5315 | 3952 if (std::imag (a (i, j)) != 0.0 || std::imag (b (i, j)) != 0.0) |
4309 | 3953 { |
3954 columns_are_real_only = 0; | |
3955 break; | |
3956 } | |
3957 } | |
3958 | |
3959 if (columns_are_real_only) | |
3960 { | |
5275 | 3961 for (octave_idx_type i = 0; i < nr; i++) |
5315 | 3962 result (i, j) = xmin (std::real (a (i, j)), std::real (b (i, j))); |
4309 | 3963 } |
3964 else | |
3965 { | |
5275 | 3966 for (octave_idx_type i = 0; i < nr; i++) |
4309 | 3967 { |
3968 OCTAVE_QUIT; | |
3969 result (i, j) = xmin (a (i, j), b (i, j)); | |
3970 } | |
3971 } | |
3972 } | |
3973 | |
3974 return result; | |
3975 } | |
3976 | |
3977 ComplexMatrix | |
3978 max (const Complex& c, const ComplexMatrix& m) | |
3979 { | |
5275 | 3980 octave_idx_type nr = m.rows (); |
3981 octave_idx_type nc = m.columns (); | |
4309 | 3982 |
3983 EMPTY_RETURN_CHECK (ComplexMatrix); | |
3984 | |
3985 ComplexMatrix result (nr, nc); | |
3986 | |
5275 | 3987 for (octave_idx_type j = 0; j < nc; j++) |
3988 for (octave_idx_type i = 0; i < nr; i++) | |
4309 | 3989 { |
3990 OCTAVE_QUIT; | |
3991 result (i, j) = xmax (c, m (i, j)); | |
3992 } | |
3993 | |
3994 return result; | |
3995 } | |
3996 | |
3997 ComplexMatrix | |
3998 max (const ComplexMatrix& m, const Complex& c) | |
3999 { | |
5275 | 4000 octave_idx_type nr = m.rows (); |
4001 octave_idx_type nc = m.columns (); | |
4309 | 4002 |
4003 EMPTY_RETURN_CHECK (ComplexMatrix); | |
4004 | |
4005 ComplexMatrix result (nr, nc); | |
4006 | |
5275 | 4007 for (octave_idx_type j = 0; j < nc; j++) |
4008 for (octave_idx_type i = 0; i < nr; i++) | |
4309 | 4009 { |
4010 OCTAVE_QUIT; | |
4011 result (i, j) = xmax (m (i, j), c); | |
4012 } | |
4013 | |
4014 return result; | |
4015 } | |
4016 | |
4017 ComplexMatrix | |
4018 max (const ComplexMatrix& a, const ComplexMatrix& b) | |
4019 { | |
5275 | 4020 octave_idx_type nr = a.rows (); |
4021 octave_idx_type nc = a.columns (); | |
4309 | 4022 |
4023 if (nr != b.rows () || nc != b.columns ()) | |
4024 { | |
4025 (*current_liboctave_error_handler) | |
4026 ("two-arg max expecting args of same size"); | |
4027 return ComplexMatrix (); | |
4028 } | |
4029 | |
4030 EMPTY_RETURN_CHECK (ComplexMatrix); | |
4031 | |
4032 ComplexMatrix result (nr, nc); | |
4033 | |
5275 | 4034 for (octave_idx_type j = 0; j < nc; j++) |
4309 | 4035 { |
4036 int columns_are_real_only = 1; | |
5275 | 4037 for (octave_idx_type i = 0; i < nr; i++) |
4309 | 4038 { |
4039 OCTAVE_QUIT; | |
5315 | 4040 if (std::imag (a (i, j)) != 0.0 || std::imag (b (i, j)) != 0.0) |
4309 | 4041 { |
4042 columns_are_real_only = 0; | |
4043 break; | |
4044 } | |
4045 } | |
4046 | |
4047 if (columns_are_real_only) | |
4048 { | |
5275 | 4049 for (octave_idx_type i = 0; i < nr; i++) |
4309 | 4050 { |
4051 OCTAVE_QUIT; | |
5315 | 4052 result (i, j) = xmax (std::real (a (i, j)), std::real (b (i, j))); |
4309 | 4053 } |
4054 } | |
4055 else | |
4056 { | |
5275 | 4057 for (octave_idx_type i = 0; i < nr; i++) |
4309 | 4058 { |
4059 OCTAVE_QUIT; | |
4060 result (i, j) = xmax (a (i, j), b (i, j)); | |
4061 } | |
4062 } | |
4063 } | |
4064 | |
4065 return result; | |
4066 } | |
4067 | |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9553
diff
changeset
|
4068 MS_CMP_OPS (ComplexMatrix, Complex) |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9528
diff
changeset
|
4069 MS_BOOL_OPS (ComplexMatrix, Complex) |
2870 | 4070 |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9553
diff
changeset
|
4071 SM_CMP_OPS (Complex, ComplexMatrix) |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9528
diff
changeset
|
4072 SM_BOOL_OPS (Complex, ComplexMatrix) |
2870 | 4073 |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9553
diff
changeset
|
4074 MM_CMP_OPS (ComplexMatrix, ComplexMatrix) |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9528
diff
changeset
|
4075 MM_BOOL_OPS (ComplexMatrix, ComplexMatrix) |
2870 | 4076 |
458 | 4077 /* |
4078 ;;; Local Variables: *** | |
4079 ;;; mode: C++ *** | |
4080 ;;; End: *** | |
4081 */ |