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