1993
|
1 // Matrix manipulations. |
458
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
458
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
458
|
21 |
|
22 */ |
|
23 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
25 #pragma implementation |
|
26 #endif |
|
27 |
458
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
458
|
30 #endif |
|
31 |
1367
|
32 #include <cfloat> |
|
33 |
3503
|
34 #include <iostream> |
1367
|
35 |
2443
|
36 // XXX FIXME XXX |
|
37 #ifdef HAVE_SYS_TYPES_H |
|
38 #include <sys/types.h> |
|
39 #endif |
458
|
40 |
2828
|
41 #include "CMatrix.h" |
1819
|
42 #include "CmplxAEPBAL.h" |
458
|
43 #include "CmplxDET.h" |
1819
|
44 #include "CmplxSCHUR.h" |
740
|
45 #include "CmplxSVD.h" |
1847
|
46 #include "f77-fcn.h" |
458
|
47 #include "lo-error.h" |
2354
|
48 #include "lo-ieee.h" |
|
49 #include "lo-mappers.h" |
1968
|
50 #include "lo-utils.h" |
1367
|
51 #include "mx-base.h" |
2828
|
52 #include "mx-cm-dm.h" |
3176
|
53 #include "mx-dm-cm.h" |
2828
|
54 #include "mx-cm-s.h" |
1367
|
55 #include "mx-inlines.cc" |
1650
|
56 #include "oct-cmplx.h" |
458
|
57 |
3827
|
58 #ifdef HAVE_FFTW |
|
59 #include "oct-fftw.h" |
|
60 #endif |
|
61 |
458
|
62 // Fortran functions we call. |
|
63 |
|
64 extern "C" |
|
65 { |
3887
|
66 int F77_FUNC (zgebal, ZGEBAL) (const char*, const int&, Complex*, |
3331
|
67 const int&, int&, int&, double*, int&, |
|
68 long, long); |
|
69 |
3887
|
70 int F77_FUNC (dgebak, DGEBAK) (const char*, const char*, const int&, |
3331
|
71 const int&, const int&, double*, |
|
72 const int&, double*, const int&, |
|
73 int&, long, long); |
|
74 |
3887
|
75 int F77_FUNC (zgemm, ZGEMM) (const char*, const char*, const int&, |
1253
|
76 const int&, const int&, const Complex&, |
|
77 const Complex*, const int&, |
|
78 const Complex*, const int&, |
|
79 const Complex&, Complex*, const int&, |
|
80 long, long); |
|
81 |
3887
|
82 int F77_FUNC (zgeco, ZGECO) (Complex*, const int&, const int&, int*, |
1253
|
83 double&, Complex*); |
|
84 |
3887
|
85 int F77_FUNC (zgedi, ZGEDI) (Complex*, const int&, const int&, int*, |
1253
|
86 Complex*, Complex*, const int&); |
|
87 |
3887
|
88 int F77_FUNC (zgesl, ZGESL) (Complex*, const int&, const int&, int*, |
1253
|
89 Complex*, const int&); |
|
90 |
3887
|
91 int F77_FUNC (zgelss, ZGELSS) (const int&, const int&, const int&, |
1253
|
92 Complex*, const int&, Complex*, |
|
93 const int&, double*, double&, int&, |
|
94 Complex*, const int&, double*, int&); |
458
|
95 |
1360
|
96 // Note that the original complex fft routines were not written for |
|
97 // double complex arguments. They have been modified by adding an |
|
98 // implicit double precision (a-h,o-z) statement at the beginning of |
|
99 // each subroutine. |
458
|
100 |
3887
|
101 int F77_FUNC (cffti, CFFTI) (const int&, Complex*); |
|
102 |
|
103 int F77_FUNC (cfftf, CFFTF) (const int&, Complex*, Complex*); |
|
104 |
|
105 int F77_FUNC (cfftb, CFFTB) (const int&, Complex*, Complex*); |
|
106 |
|
107 int F77_FUNC (zlartg, ZLARTG) (const Complex&, const Complex&, |
1819
|
108 double&, Complex&, Complex&); |
|
109 |
3887
|
110 int F77_FUNC (ztrsyl, ZTRSYL) (const char*, const char*, const int&, |
1819
|
111 const int&, const int&, |
|
112 const Complex*, const int&, |
|
113 const Complex*, const int&, |
|
114 const Complex*, const int&, double&, |
|
115 int&, long, long); |
|
116 |
3887
|
117 int F77_FUNC (xzlange, XZLANGE) (const char*, const int&, |
3130
|
118 const int&, const Complex*, |
|
119 const int&, double*, double&); |
458
|
120 } |
|
121 |
2354
|
122 static const Complex Complex_NaN_result (octave_NaN, octave_NaN); |
|
123 |
1360
|
124 // Complex Matrix class |
458
|
125 |
|
126 ComplexMatrix::ComplexMatrix (const Matrix& a) |
1214
|
127 : MArray2<Complex> (a.rows (), a.cols ()) |
458
|
128 { |
|
129 for (int j = 0; j < cols (); j++) |
|
130 for (int i = 0; i < rows (); i++) |
|
131 elem (i, j) = a.elem (i, j); |
|
132 } |
|
133 |
2349
|
134 ComplexMatrix::ComplexMatrix (const RowVector& rv) |
|
135 : MArray2<Complex> (1, rv.length (), 0.0) |
|
136 { |
|
137 for (int i = 0; i < rv.length (); i++) |
|
138 elem (0, i) = rv.elem (i); |
|
139 } |
|
140 |
|
141 ComplexMatrix::ComplexMatrix (const ColumnVector& cv) |
|
142 : MArray2<Complex> (cv.length (), 1, 0.0) |
|
143 { |
|
144 for (int i = 0; i < cv.length (); i++) |
|
145 elem (i, 0) = cv.elem (i); |
|
146 } |
|
147 |
458
|
148 ComplexMatrix::ComplexMatrix (const DiagMatrix& a) |
1214
|
149 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
458
|
150 { |
|
151 for (int i = 0; i < a.length (); i++) |
|
152 elem (i, i) = a.elem (i, i); |
|
153 } |
|
154 |
2349
|
155 ComplexMatrix::ComplexMatrix (const ComplexRowVector& rv) |
|
156 : MArray2<Complex> (1, rv.length (), 0.0) |
|
157 { |
|
158 for (int i = 0; i < rv.length (); i++) |
|
159 elem (0, i) = rv.elem (i); |
|
160 } |
|
161 |
|
162 ComplexMatrix::ComplexMatrix (const ComplexColumnVector& cv) |
|
163 : MArray2<Complex> (cv.length (), 1, 0.0) |
|
164 { |
|
165 for (int i = 0; i < cv.length (); i++) |
|
166 elem (i, 0) = cv.elem (i); |
|
167 } |
|
168 |
458
|
169 ComplexMatrix::ComplexMatrix (const ComplexDiagMatrix& a) |
1214
|
170 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
458
|
171 { |
|
172 for (int i = 0; i < a.length (); i++) |
|
173 elem (i, i) = a.elem (i, i); |
|
174 } |
|
175 |
1574
|
176 // XXX FIXME XXX -- could we use a templated mixed-type copy function |
|
177 // here? |
|
178 |
2828
|
179 ComplexMatrix::ComplexMatrix (const boolMatrix& a) |
3180
|
180 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
2828
|
181 { |
3998
|
182 for (int i = 0; i < a.rows (); i++) |
|
183 for (int j = 0; j < a.cols (); j++) |
2828
|
184 elem (i, j) = a.elem (i, j); |
|
185 } |
|
186 |
1574
|
187 ComplexMatrix::ComplexMatrix (const charMatrix& a) |
3180
|
188 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
1574
|
189 { |
3998
|
190 for (int i = 0; i < a.rows (); i++) |
|
191 for (int j = 0; j < a.cols (); j++) |
1574
|
192 elem (i, j) = a.elem (i, j); |
|
193 } |
|
194 |
2384
|
195 bool |
458
|
196 ComplexMatrix::operator == (const ComplexMatrix& a) const |
|
197 { |
|
198 if (rows () != a.rows () || cols () != a.cols ()) |
2384
|
199 return false; |
458
|
200 |
3769
|
201 return mx_inline_equal (data (), a.data (), length ()); |
458
|
202 } |
|
203 |
2384
|
204 bool |
458
|
205 ComplexMatrix::operator != (const ComplexMatrix& a) const |
|
206 { |
|
207 return !(*this == a); |
|
208 } |
|
209 |
2815
|
210 bool |
|
211 ComplexMatrix::is_hermitian (void) const |
|
212 { |
|
213 int nr = rows (); |
|
214 int nc = cols (); |
|
215 |
|
216 if (is_square () && nr > 0) |
|
217 { |
|
218 for (int i = 0; i < nr; i++) |
|
219 for (int j = i; j < nc; j++) |
|
220 if (elem (i, j) != conj (elem (j, i))) |
|
221 return false; |
|
222 |
|
223 return true; |
|
224 } |
|
225 |
|
226 return false; |
|
227 } |
|
228 |
458
|
229 // destructive insert/delete/reorder operations |
|
230 |
|
231 ComplexMatrix& |
|
232 ComplexMatrix::insert (const Matrix& a, int r, int c) |
|
233 { |
|
234 int a_nr = a.rows (); |
|
235 int a_nc = a.cols (); |
1699
|
236 |
|
237 if (r < 0 || r + a_nr > rows () || c < 0 || c + a_nc > cols ()) |
458
|
238 { |
|
239 (*current_liboctave_error_handler) ("range error for insert"); |
|
240 return *this; |
|
241 } |
|
242 |
|
243 for (int j = 0; j < a_nc; j++) |
|
244 for (int i = 0; i < a_nr; i++) |
|
245 elem (r+i, c+j) = a.elem (i, j); |
|
246 |
|
247 return *this; |
|
248 } |
|
249 |
|
250 ComplexMatrix& |
|
251 ComplexMatrix::insert (const RowVector& a, int r, int c) |
|
252 { |
|
253 int a_len = a.length (); |
1699
|
254 if (r < 0 || r >= rows () || c < 0 || c + a_len > cols ()) |
458
|
255 { |
|
256 (*current_liboctave_error_handler) ("range error for insert"); |
|
257 return *this; |
|
258 } |
|
259 |
|
260 for (int i = 0; i < a_len; i++) |
|
261 elem (r, c+i) = a.elem (i); |
|
262 |
|
263 return *this; |
|
264 } |
|
265 |
|
266 ComplexMatrix& |
|
267 ComplexMatrix::insert (const ColumnVector& a, int r, int c) |
|
268 { |
|
269 int a_len = a.length (); |
1699
|
270 if (r < 0 || r + a_len > rows () || c < 0 || c >= cols ()) |
458
|
271 { |
|
272 (*current_liboctave_error_handler) ("range error for insert"); |
|
273 return *this; |
|
274 } |
|
275 |
|
276 for (int i = 0; i < a_len; i++) |
|
277 elem (r+i, c) = a.elem (i); |
|
278 |
|
279 return *this; |
|
280 } |
|
281 |
|
282 ComplexMatrix& |
|
283 ComplexMatrix::insert (const DiagMatrix& a, int r, int c) |
|
284 { |
1699
|
285 int a_nr = a.rows (); |
|
286 int a_nc = a.cols (); |
|
287 |
|
288 if (r < 0 || r + a_nr > rows () || c < 0 || c + a_nc > cols ()) |
458
|
289 { |
|
290 (*current_liboctave_error_handler) ("range error for insert"); |
|
291 return *this; |
|
292 } |
|
293 |
1699
|
294 fill (0.0, r, c, r + a_nr - 1, c + a_nc - 1); |
|
295 |
458
|
296 for (int i = 0; i < a.length (); i++) |
|
297 elem (r+i, c+i) = a.elem (i, i); |
|
298 |
|
299 return *this; |
|
300 } |
|
301 |
|
302 ComplexMatrix& |
|
303 ComplexMatrix::insert (const ComplexMatrix& a, int r, int c) |
|
304 { |
1561
|
305 Array2<Complex>::insert (a, r, c); |
458
|
306 return *this; |
|
307 } |
|
308 |
|
309 ComplexMatrix& |
|
310 ComplexMatrix::insert (const ComplexRowVector& a, int r, int c) |
|
311 { |
|
312 int a_len = a.length (); |
1699
|
313 if (r < 0 || r >= rows () || c < 0 || c + a_len > cols ()) |
458
|
314 { |
|
315 (*current_liboctave_error_handler) ("range error for insert"); |
|
316 return *this; |
|
317 } |
|
318 |
|
319 for (int i = 0; i < a_len; i++) |
|
320 elem (r, c+i) = a.elem (i); |
|
321 |
|
322 return *this; |
|
323 } |
|
324 |
|
325 ComplexMatrix& |
|
326 ComplexMatrix::insert (const ComplexColumnVector& a, int r, int c) |
|
327 { |
|
328 int a_len = a.length (); |
1699
|
329 if (r < 0 || r + a_len > rows () || c < 0 || c >= cols ()) |
458
|
330 { |
|
331 (*current_liboctave_error_handler) ("range error for insert"); |
|
332 return *this; |
|
333 } |
|
334 |
|
335 for (int i = 0; i < a_len; i++) |
|
336 elem (r+i, c) = a.elem (i); |
|
337 |
|
338 return *this; |
|
339 } |
|
340 |
|
341 ComplexMatrix& |
|
342 ComplexMatrix::insert (const ComplexDiagMatrix& a, int r, int c) |
|
343 { |
1699
|
344 int a_nr = a.rows (); |
|
345 int a_nc = a.cols (); |
|
346 |
|
347 if (r < 0 || r + a_nr > rows () || c < 0 || c + a_nc > cols ()) |
458
|
348 { |
|
349 (*current_liboctave_error_handler) ("range error for insert"); |
|
350 return *this; |
|
351 } |
|
352 |
1699
|
353 fill (0.0, r, c, r + a_nr - 1, c + a_nc - 1); |
|
354 |
458
|
355 for (int i = 0; i < a.length (); i++) |
|
356 elem (r+i, c+i) = a.elem (i, i); |
|
357 |
|
358 return *this; |
|
359 } |
|
360 |
|
361 ComplexMatrix& |
|
362 ComplexMatrix::fill (double val) |
|
363 { |
|
364 int nr = rows (); |
|
365 int nc = cols (); |
|
366 if (nr > 0 && nc > 0) |
|
367 for (int j = 0; j < nc; j++) |
|
368 for (int i = 0; i < nr; i++) |
|
369 elem (i, j) = val; |
|
370 |
|
371 return *this; |
|
372 } |
|
373 |
|
374 ComplexMatrix& |
|
375 ComplexMatrix::fill (const Complex& val) |
|
376 { |
|
377 int nr = rows (); |
|
378 int nc = cols (); |
|
379 if (nr > 0 && nc > 0) |
|
380 for (int j = 0; j < nc; j++) |
|
381 for (int i = 0; i < nr; i++) |
|
382 elem (i, j) = val; |
|
383 |
|
384 return *this; |
|
385 } |
|
386 |
|
387 ComplexMatrix& |
|
388 ComplexMatrix::fill (double val, int r1, int c1, int r2, int c2) |
|
389 { |
|
390 int nr = rows (); |
|
391 int nc = cols (); |
|
392 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
|
393 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) |
|
394 { |
|
395 (*current_liboctave_error_handler) ("range error for fill"); |
|
396 return *this; |
|
397 } |
|
398 |
|
399 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
400 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
401 |
|
402 for (int j = c1; j <= c2; j++) |
|
403 for (int i = r1; i <= r2; i++) |
|
404 elem (i, j) = val; |
|
405 |
|
406 return *this; |
|
407 } |
|
408 |
|
409 ComplexMatrix& |
|
410 ComplexMatrix::fill (const Complex& val, int r1, int c1, int r2, int c2) |
|
411 { |
|
412 int nr = rows (); |
|
413 int nc = cols (); |
|
414 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
|
415 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) |
|
416 { |
|
417 (*current_liboctave_error_handler) ("range error for fill"); |
|
418 return *this; |
|
419 } |
|
420 |
|
421 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
422 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
423 |
|
424 for (int j = c1; j <= c2; j++) |
|
425 for (int i = r1; i <= r2; i++) |
|
426 elem (i, j) = val; |
|
427 |
|
428 return *this; |
|
429 } |
|
430 |
|
431 ComplexMatrix |
|
432 ComplexMatrix::append (const Matrix& a) const |
|
433 { |
|
434 int nr = rows (); |
|
435 int nc = cols (); |
|
436 if (nr != a.rows ()) |
|
437 { |
|
438 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
439 return *this; |
|
440 } |
|
441 |
|
442 int nc_insert = nc; |
|
443 ComplexMatrix retval (nr, nc + a.cols ()); |
|
444 retval.insert (*this, 0, 0); |
|
445 retval.insert (a, 0, nc_insert); |
|
446 return retval; |
|
447 } |
|
448 |
|
449 ComplexMatrix |
|
450 ComplexMatrix::append (const RowVector& a) const |
|
451 { |
|
452 int nr = rows (); |
|
453 int nc = cols (); |
|
454 if (nr != 1) |
|
455 { |
|
456 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
457 return *this; |
|
458 } |
|
459 |
|
460 int nc_insert = nc; |
|
461 ComplexMatrix retval (nr, nc + a.length ()); |
|
462 retval.insert (*this, 0, 0); |
|
463 retval.insert (a, 0, nc_insert); |
|
464 return retval; |
|
465 } |
|
466 |
|
467 ComplexMatrix |
|
468 ComplexMatrix::append (const ColumnVector& a) const |
|
469 { |
|
470 int nr = rows (); |
|
471 int nc = cols (); |
|
472 if (nr != a.length ()) |
|
473 { |
|
474 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
475 return *this; |
|
476 } |
|
477 |
|
478 int nc_insert = nc; |
|
479 ComplexMatrix retval (nr, nc + 1); |
|
480 retval.insert (*this, 0, 0); |
|
481 retval.insert (a, 0, nc_insert); |
|
482 return retval; |
|
483 } |
|
484 |
|
485 ComplexMatrix |
|
486 ComplexMatrix::append (const DiagMatrix& a) const |
|
487 { |
|
488 int nr = rows (); |
|
489 int nc = cols (); |
|
490 if (nr != a.rows ()) |
|
491 { |
|
492 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
493 return *this; |
|
494 } |
|
495 |
|
496 int nc_insert = nc; |
|
497 ComplexMatrix retval (nr, nc + a.cols ()); |
|
498 retval.insert (*this, 0, 0); |
|
499 retval.insert (a, 0, nc_insert); |
|
500 return retval; |
|
501 } |
|
502 |
|
503 ComplexMatrix |
|
504 ComplexMatrix::append (const ComplexMatrix& a) const |
|
505 { |
|
506 int nr = rows (); |
|
507 int nc = cols (); |
|
508 if (nr != a.rows ()) |
|
509 { |
|
510 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
511 return *this; |
|
512 } |
|
513 |
|
514 int nc_insert = nc; |
|
515 ComplexMatrix retval (nr, nc + a.cols ()); |
|
516 retval.insert (*this, 0, 0); |
|
517 retval.insert (a, 0, nc_insert); |
|
518 return retval; |
|
519 } |
|
520 |
|
521 ComplexMatrix |
|
522 ComplexMatrix::append (const ComplexRowVector& a) const |
|
523 { |
|
524 int nr = rows (); |
|
525 int nc = cols (); |
|
526 if (nr != 1) |
|
527 { |
|
528 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
529 return *this; |
|
530 } |
|
531 |
|
532 int nc_insert = nc; |
|
533 ComplexMatrix retval (nr, nc + a.length ()); |
|
534 retval.insert (*this, 0, 0); |
|
535 retval.insert (a, 0, nc_insert); |
|
536 return retval; |
|
537 } |
|
538 |
|
539 ComplexMatrix |
|
540 ComplexMatrix::append (const ComplexColumnVector& a) const |
|
541 { |
|
542 int nr = rows (); |
|
543 int nc = cols (); |
|
544 if (nr != a.length ()) |
|
545 { |
|
546 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
547 return *this; |
|
548 } |
|
549 |
|
550 int nc_insert = nc; |
|
551 ComplexMatrix retval (nr, nc + 1); |
|
552 retval.insert (*this, 0, 0); |
|
553 retval.insert (a, 0, nc_insert); |
|
554 return retval; |
|
555 } |
|
556 |
|
557 ComplexMatrix |
|
558 ComplexMatrix::append (const ComplexDiagMatrix& a) const |
|
559 { |
|
560 int nr = rows (); |
|
561 int nc = cols (); |
|
562 if (nr != a.rows ()) |
|
563 { |
|
564 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
565 return *this; |
|
566 } |
|
567 |
|
568 int nc_insert = nc; |
|
569 ComplexMatrix retval (nr, nc + a.cols ()); |
|
570 retval.insert (*this, 0, 0); |
|
571 retval.insert (a, 0, nc_insert); |
|
572 return retval; |
|
573 } |
|
574 |
|
575 ComplexMatrix |
|
576 ComplexMatrix::stack (const Matrix& a) const |
|
577 { |
|
578 int nr = rows (); |
|
579 int nc = cols (); |
|
580 if (nc != a.cols ()) |
|
581 { |
|
582 (*current_liboctave_error_handler) |
|
583 ("column dimension mismatch for stack"); |
|
584 return *this; |
|
585 } |
|
586 |
|
587 int nr_insert = nr; |
|
588 ComplexMatrix retval (nr + a.rows (), nc); |
|
589 retval.insert (*this, 0, 0); |
|
590 retval.insert (a, nr_insert, 0); |
|
591 return retval; |
|
592 } |
|
593 |
|
594 ComplexMatrix |
|
595 ComplexMatrix::stack (const RowVector& a) const |
|
596 { |
|
597 int nr = rows (); |
|
598 int nc = cols (); |
|
599 if (nc != a.length ()) |
|
600 { |
|
601 (*current_liboctave_error_handler) |
|
602 ("column dimension mismatch for stack"); |
|
603 return *this; |
|
604 } |
|
605 |
|
606 int nr_insert = nr; |
|
607 ComplexMatrix retval (nr + 1, nc); |
|
608 retval.insert (*this, 0, 0); |
|
609 retval.insert (a, nr_insert, 0); |
|
610 return retval; |
|
611 } |
|
612 |
|
613 ComplexMatrix |
|
614 ComplexMatrix::stack (const ColumnVector& a) const |
|
615 { |
|
616 int nr = rows (); |
|
617 int nc = cols (); |
|
618 if (nc != 1) |
|
619 { |
|
620 (*current_liboctave_error_handler) |
|
621 ("column dimension mismatch for stack"); |
|
622 return *this; |
|
623 } |
|
624 |
|
625 int nr_insert = nr; |
|
626 ComplexMatrix retval (nr + a.length (), nc); |
|
627 retval.insert (*this, 0, 0); |
|
628 retval.insert (a, nr_insert, 0); |
|
629 return retval; |
|
630 } |
|
631 |
|
632 ComplexMatrix |
|
633 ComplexMatrix::stack (const DiagMatrix& a) const |
|
634 { |
|
635 int nr = rows (); |
|
636 int nc = cols (); |
|
637 if (nc != a.cols ()) |
|
638 { |
|
639 (*current_liboctave_error_handler) |
|
640 ("column dimension mismatch for stack"); |
|
641 return *this; |
|
642 } |
|
643 |
|
644 int nr_insert = nr; |
|
645 ComplexMatrix retval (nr + a.rows (), nc); |
|
646 retval.insert (*this, 0, 0); |
|
647 retval.insert (a, nr_insert, 0); |
|
648 return retval; |
|
649 } |
|
650 |
|
651 ComplexMatrix |
|
652 ComplexMatrix::stack (const ComplexMatrix& a) const |
|
653 { |
|
654 int nr = rows (); |
|
655 int nc = cols (); |
|
656 if (nc != a.cols ()) |
|
657 { |
|
658 (*current_liboctave_error_handler) |
|
659 ("column dimension mismatch for stack"); |
|
660 return *this; |
|
661 } |
|
662 |
|
663 int nr_insert = nr; |
|
664 ComplexMatrix retval (nr + a.rows (), nc); |
|
665 retval.insert (*this, 0, 0); |
|
666 retval.insert (a, nr_insert, 0); |
|
667 return retval; |
|
668 } |
|
669 |
|
670 ComplexMatrix |
|
671 ComplexMatrix::stack (const ComplexRowVector& a) const |
|
672 { |
|
673 int nr = rows (); |
|
674 int nc = cols (); |
|
675 if (nc != a.length ()) |
|
676 { |
|
677 (*current_liboctave_error_handler) |
|
678 ("column dimension mismatch for stack"); |
|
679 return *this; |
|
680 } |
|
681 |
|
682 int nr_insert = nr; |
|
683 ComplexMatrix retval (nr + 1, nc); |
|
684 retval.insert (*this, 0, 0); |
|
685 retval.insert (a, nr_insert, 0); |
|
686 return retval; |
|
687 } |
|
688 |
|
689 ComplexMatrix |
|
690 ComplexMatrix::stack (const ComplexColumnVector& a) const |
|
691 { |
|
692 int nr = rows (); |
|
693 int nc = cols (); |
|
694 if (nc != 1) |
|
695 { |
|
696 (*current_liboctave_error_handler) |
|
697 ("column dimension mismatch for stack"); |
|
698 return *this; |
|
699 } |
|
700 |
|
701 int nr_insert = nr; |
|
702 ComplexMatrix retval (nr + a.length (), nc); |
|
703 retval.insert (*this, 0, 0); |
|
704 retval.insert (a, nr_insert, 0); |
|
705 return retval; |
|
706 } |
|
707 |
|
708 ComplexMatrix |
|
709 ComplexMatrix::stack (const ComplexDiagMatrix& a) const |
|
710 { |
|
711 int nr = rows (); |
|
712 int nc = cols (); |
|
713 if (nc != a.cols ()) |
|
714 { |
|
715 (*current_liboctave_error_handler) |
|
716 ("column dimension mismatch for stack"); |
|
717 return *this; |
|
718 } |
|
719 |
|
720 int nr_insert = nr; |
|
721 ComplexMatrix retval (nr + a.rows (), nc); |
|
722 retval.insert (*this, 0, 0); |
|
723 retval.insert (a, nr_insert, 0); |
|
724 return retval; |
|
725 } |
|
726 |
|
727 ComplexMatrix |
|
728 ComplexMatrix::hermitian (void) const |
|
729 { |
|
730 int nr = rows (); |
|
731 int nc = cols (); |
|
732 ComplexMatrix result; |
|
733 if (length () > 0) |
|
734 { |
|
735 result.resize (nc, nr); |
|
736 for (int j = 0; j < nc; j++) |
|
737 for (int i = 0; i < nr; i++) |
|
738 result.elem (j, i) = conj (elem (i, j)); |
|
739 } |
|
740 return result; |
|
741 } |
|
742 |
|
743 ComplexMatrix |
|
744 conj (const ComplexMatrix& a) |
|
745 { |
|
746 int a_len = a.length (); |
|
747 ComplexMatrix retval; |
|
748 if (a_len > 0) |
3769
|
749 retval = ComplexMatrix (mx_inline_conj_dup (a.data (), a_len), |
|
750 a.rows (), a.cols ()); |
458
|
751 return retval; |
|
752 } |
|
753 |
|
754 // resize is the destructive equivalent for this one |
|
755 |
|
756 ComplexMatrix |
|
757 ComplexMatrix::extract (int r1, int c1, int r2, int c2) const |
|
758 { |
|
759 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
760 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
761 |
|
762 int new_r = r2 - r1 + 1; |
|
763 int new_c = c2 - c1 + 1; |
|
764 |
|
765 ComplexMatrix result (new_r, new_c); |
|
766 |
|
767 for (int j = 0; j < new_c; j++) |
|
768 for (int i = 0; i < new_r; i++) |
|
769 result.elem (i, j) = elem (r1+i, c1+j); |
|
770 |
|
771 return result; |
|
772 } |
|
773 |
|
774 // extract row or column i. |
|
775 |
|
776 ComplexRowVector |
|
777 ComplexMatrix::row (int i) const |
|
778 { |
|
779 int nc = cols (); |
|
780 if (i < 0 || i >= rows ()) |
|
781 { |
|
782 (*current_liboctave_error_handler) ("invalid row selection"); |
|
783 return ComplexRowVector (); |
|
784 } |
|
785 |
|
786 ComplexRowVector retval (nc); |
|
787 for (int j = 0; j < cols (); j++) |
|
788 retval.elem (j) = elem (i, j); |
|
789 |
|
790 return retval; |
|
791 } |
|
792 |
|
793 ComplexRowVector |
|
794 ComplexMatrix::row (char *s) const |
|
795 { |
533
|
796 if (! s) |
458
|
797 { |
|
798 (*current_liboctave_error_handler) ("invalid row selection"); |
|
799 return ComplexRowVector (); |
|
800 } |
|
801 |
|
802 char c = *s; |
|
803 if (c == 'f' || c == 'F') |
|
804 return row (0); |
|
805 else if (c == 'l' || c == 'L') |
|
806 return row (rows () - 1); |
|
807 else |
|
808 { |
|
809 (*current_liboctave_error_handler) ("invalid row selection"); |
|
810 return ComplexRowVector (); |
|
811 } |
|
812 } |
|
813 |
|
814 ComplexColumnVector |
|
815 ComplexMatrix::column (int i) const |
|
816 { |
|
817 int nr = rows (); |
|
818 if (i < 0 || i >= cols ()) |
|
819 { |
|
820 (*current_liboctave_error_handler) ("invalid column selection"); |
|
821 return ComplexColumnVector (); |
|
822 } |
|
823 |
|
824 ComplexColumnVector retval (nr); |
|
825 for (int j = 0; j < nr; j++) |
|
826 retval.elem (j) = elem (j, i); |
|
827 |
|
828 return retval; |
|
829 } |
|
830 |
|
831 ComplexColumnVector |
|
832 ComplexMatrix::column (char *s) const |
|
833 { |
533
|
834 if (! s) |
458
|
835 { |
|
836 (*current_liboctave_error_handler) ("invalid column selection"); |
|
837 return ComplexColumnVector (); |
|
838 } |
|
839 |
|
840 char c = *s; |
|
841 if (c == 'f' || c == 'F') |
|
842 return column (0); |
|
843 else if (c == 'l' || c == 'L') |
|
844 return column (cols () - 1); |
|
845 else |
|
846 { |
|
847 (*current_liboctave_error_handler) ("invalid column selection"); |
|
848 return ComplexColumnVector (); |
|
849 } |
|
850 } |
|
851 |
|
852 ComplexMatrix |
|
853 ComplexMatrix::inverse (void) const |
|
854 { |
|
855 int info; |
479
|
856 double rcond; |
|
857 return inverse (info, rcond); |
458
|
858 } |
|
859 |
|
860 ComplexMatrix |
|
861 ComplexMatrix::inverse (int& info) const |
|
862 { |
|
863 double rcond; |
|
864 return inverse (info, rcond); |
|
865 } |
|
866 |
|
867 ComplexMatrix |
1656
|
868 ComplexMatrix::inverse (int& info, double& rcond, int force) const |
458
|
869 { |
1948
|
870 ComplexMatrix retval; |
|
871 |
458
|
872 int nr = rows (); |
|
873 int nc = cols (); |
1948
|
874 |
458
|
875 if (nr != nc) |
1948
|
876 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
458
|
877 else |
|
878 { |
1948
|
879 info = 0; |
|
880 |
|
881 Array<int> ipvt (nr); |
|
882 int *pipvt = ipvt.fortran_vec (); |
|
883 |
|
884 Array<Complex> z (nr); |
|
885 Complex *pz = z.fortran_vec (); |
|
886 |
|
887 retval = *this; |
|
888 Complex *tmp_data = retval.fortran_vec (); |
|
889 |
|
890 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nc, pipvt, rcond, pz)); |
|
891 |
|
892 if (f77_exception_encountered) |
|
893 (*current_liboctave_error_handler) ("unrecoverable error in zgeco"); |
|
894 else |
|
895 { |
|
896 volatile double rcond_plus_one = rcond + 1.0; |
|
897 |
3838
|
898 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
1948
|
899 info = -1; |
|
900 |
|
901 if (info == -1 && ! force) |
|
902 retval = *this; // Restore contents. |
|
903 else |
|
904 { |
|
905 Complex *dummy = 0; |
|
906 |
|
907 F77_XFCN (zgedi, ZGEDI, (tmp_data, nr, nc, pipvt, dummy, |
|
908 pz, 1)); |
|
909 |
|
910 if (f77_exception_encountered) |
|
911 (*current_liboctave_error_handler) |
|
912 ("unrecoverable error in zgedi"); |
|
913 } |
|
914 } |
458
|
915 } |
|
916 |
1948
|
917 return retval; |
458
|
918 } |
|
919 |
|
920 ComplexMatrix |
740
|
921 ComplexMatrix::pseudo_inverse (double tol) |
|
922 { |
1549
|
923 ComplexMatrix retval; |
|
924 |
3480
|
925 ComplexSVD result (*this, SVD::economy); |
740
|
926 |
|
927 DiagMatrix S = result.singular_values (); |
|
928 ComplexMatrix U = result.left_singular_matrix (); |
|
929 ComplexMatrix V = result.right_singular_matrix (); |
|
930 |
|
931 ColumnVector sigma = S.diag (); |
|
932 |
|
933 int r = sigma.length () - 1; |
|
934 int nr = rows (); |
|
935 int nc = cols (); |
|
936 |
|
937 if (tol <= 0.0) |
|
938 { |
|
939 if (nr > nc) |
|
940 tol = nr * sigma.elem (0) * DBL_EPSILON; |
|
941 else |
|
942 tol = nc * sigma.elem (0) * DBL_EPSILON; |
|
943 } |
|
944 |
|
945 while (r >= 0 && sigma.elem (r) < tol) |
|
946 r--; |
|
947 |
|
948 if (r < 0) |
1549
|
949 retval = ComplexMatrix (nc, nr, 0.0); |
740
|
950 else |
|
951 { |
|
952 ComplexMatrix Ur = U.extract (0, 0, nr-1, r); |
|
953 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); |
|
954 ComplexMatrix Vr = V.extract (0, 0, nc-1, r); |
1549
|
955 retval = Vr * D * Ur.hermitian (); |
740
|
956 } |
1549
|
957 |
|
958 return retval; |
740
|
959 } |
|
960 |
3827
|
961 #ifdef HAVE_FFTW |
|
962 |
|
963 ComplexMatrix |
|
964 ComplexMatrix::fourier (void) const |
|
965 { |
|
966 size_t nr = rows (); |
|
967 size_t nc = cols (); |
|
968 |
|
969 ComplexMatrix retval (nr, nc); |
|
970 |
|
971 size_t npts, nsamples; |
|
972 |
|
973 if (nr == 1 || nc == 1) |
|
974 { |
|
975 npts = nr > nc ? nr : nc; |
|
976 nsamples = 1; |
|
977 } |
|
978 else |
|
979 { |
|
980 npts = nr; |
|
981 nsamples = nc; |
|
982 } |
|
983 |
|
984 const Complex *in (data ()); |
|
985 Complex *out (retval.fortran_vec ()); |
|
986 |
|
987 for (size_t i = 0; i < nsamples; i++) |
|
988 { |
4153
|
989 OCTAVE_QUIT; |
|
990 |
3827
|
991 octave_fftw::fft (&in[npts * i], &out[npts * i], npts); |
|
992 } |
|
993 |
|
994 return retval; |
|
995 } |
|
996 |
|
997 ComplexMatrix |
|
998 ComplexMatrix::ifourier (void) const |
|
999 { |
|
1000 size_t nr = rows (); |
|
1001 size_t nc = cols (); |
|
1002 |
|
1003 ComplexMatrix retval (nr, nc); |
|
1004 |
|
1005 size_t npts, nsamples; |
|
1006 |
|
1007 if (nr == 1 || nc == 1) |
|
1008 { |
|
1009 npts = nr > nc ? nr : nc; |
|
1010 nsamples = 1; |
|
1011 } |
|
1012 else |
|
1013 { |
|
1014 npts = nr; |
|
1015 nsamples = nc; |
|
1016 } |
|
1017 |
|
1018 const Complex *in (data ()); |
|
1019 Complex *out (retval.fortran_vec ()); |
|
1020 |
|
1021 for (size_t i = 0; i < nsamples; i++) |
|
1022 { |
4153
|
1023 OCTAVE_QUIT; |
|
1024 |
3827
|
1025 octave_fftw::ifft (&in[npts * i], &out[npts * i], npts); |
|
1026 } |
|
1027 |
|
1028 return retval; |
|
1029 } |
|
1030 |
|
1031 ComplexMatrix |
|
1032 ComplexMatrix::fourier2d (void) const |
|
1033 { |
|
1034 int nr = rows (); |
|
1035 int nc = cols (); |
|
1036 |
|
1037 ComplexMatrix retval (*this); |
|
1038 // Note the order of passing the rows and columns to account for |
|
1039 // column-major storage. |
|
1040 octave_fftw::fft2d (retval.fortran_vec (), nc, nr); |
|
1041 |
|
1042 return retval; |
|
1043 } |
|
1044 |
|
1045 ComplexMatrix |
|
1046 ComplexMatrix::ifourier2d (void) const |
|
1047 { |
|
1048 int nr = rows (); |
|
1049 int nc = cols (); |
|
1050 |
|
1051 ComplexMatrix retval (*this); |
|
1052 // Note the order of passing the rows and columns to account for |
|
1053 // column-major storage. |
|
1054 octave_fftw::ifft2d (retval.fortran_vec (), nc, nr); |
|
1055 |
|
1056 return retval; |
|
1057 } |
|
1058 |
|
1059 #else |
|
1060 |
740
|
1061 ComplexMatrix |
458
|
1062 ComplexMatrix::fourier (void) const |
|
1063 { |
1948
|
1064 ComplexMatrix retval; |
|
1065 |
458
|
1066 int nr = rows (); |
|
1067 int nc = cols (); |
1948
|
1068 |
458
|
1069 int npts, nsamples; |
1948
|
1070 |
458
|
1071 if (nr == 1 || nc == 1) |
|
1072 { |
|
1073 npts = nr > nc ? nr : nc; |
|
1074 nsamples = 1; |
|
1075 } |
|
1076 else |
|
1077 { |
|
1078 npts = nr; |
|
1079 nsamples = nc; |
|
1080 } |
|
1081 |
|
1082 int nn = 4*npts+15; |
1948
|
1083 |
|
1084 Array<Complex> wsave (nn); |
|
1085 Complex *pwsave = wsave.fortran_vec (); |
|
1086 |
|
1087 retval = *this; |
|
1088 Complex *tmp_data = retval.fortran_vec (); |
|
1089 |
3887
|
1090 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
458
|
1091 |
|
1092 for (int j = 0; j < nsamples; j++) |
4153
|
1093 { |
|
1094 OCTAVE_QUIT; |
|
1095 |
|
1096 F77_FUNC (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
1097 } |
1948
|
1098 |
|
1099 return retval; |
458
|
1100 } |
|
1101 |
|
1102 ComplexMatrix |
|
1103 ComplexMatrix::ifourier (void) const |
|
1104 { |
1948
|
1105 ComplexMatrix retval; |
|
1106 |
458
|
1107 int nr = rows (); |
|
1108 int nc = cols (); |
1948
|
1109 |
458
|
1110 int npts, nsamples; |
1948
|
1111 |
458
|
1112 if (nr == 1 || nc == 1) |
|
1113 { |
|
1114 npts = nr > nc ? nr : nc; |
|
1115 nsamples = 1; |
|
1116 } |
|
1117 else |
|
1118 { |
|
1119 npts = nr; |
|
1120 nsamples = nc; |
|
1121 } |
|
1122 |
|
1123 int nn = 4*npts+15; |
1948
|
1124 |
|
1125 Array<Complex> wsave (nn); |
|
1126 Complex *pwsave = wsave.fortran_vec (); |
|
1127 |
|
1128 retval = *this; |
|
1129 Complex *tmp_data = retval.fortran_vec (); |
|
1130 |
3887
|
1131 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
458
|
1132 |
|
1133 for (int j = 0; j < nsamples; j++) |
4153
|
1134 { |
|
1135 OCTAVE_QUIT; |
|
1136 |
|
1137 F77_FUNC (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
|
1138 } |
458
|
1139 |
1321
|
1140 for (int j = 0; j < npts*nsamples; j++) |
3572
|
1141 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
458
|
1142 |
1948
|
1143 return retval; |
458
|
1144 } |
|
1145 |
677
|
1146 ComplexMatrix |
|
1147 ComplexMatrix::fourier2d (void) const |
|
1148 { |
1948
|
1149 ComplexMatrix retval; |
|
1150 |
677
|
1151 int nr = rows (); |
|
1152 int nc = cols (); |
1948
|
1153 |
677
|
1154 int npts, nsamples; |
1948
|
1155 |
677
|
1156 if (nr == 1 || nc == 1) |
|
1157 { |
|
1158 npts = nr > nc ? nr : nc; |
|
1159 nsamples = 1; |
|
1160 } |
|
1161 else |
|
1162 { |
|
1163 npts = nr; |
|
1164 nsamples = nc; |
|
1165 } |
|
1166 |
|
1167 int nn = 4*npts+15; |
1948
|
1168 |
|
1169 Array<Complex> wsave (nn); |
|
1170 Complex *pwsave = wsave.fortran_vec (); |
|
1171 |
|
1172 retval = *this; |
|
1173 Complex *tmp_data = retval.fortran_vec (); |
|
1174 |
3887
|
1175 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1176 |
|
1177 for (int j = 0; j < nsamples; j++) |
4153
|
1178 { |
|
1179 OCTAVE_QUIT; |
|
1180 |
|
1181 F77_FUNC (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
1182 } |
677
|
1183 |
|
1184 npts = nc; |
|
1185 nsamples = nr; |
|
1186 nn = 4*npts+15; |
1948
|
1187 |
|
1188 wsave.resize (nn); |
|
1189 pwsave = wsave.fortran_vec (); |
|
1190 |
|
1191 Array<Complex> row (npts); |
|
1192 Complex *prow = row.fortran_vec (); |
|
1193 |
3887
|
1194 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1195 |
1321
|
1196 for (int j = 0; j < nsamples; j++) |
677
|
1197 { |
4153
|
1198 OCTAVE_QUIT; |
|
1199 |
677
|
1200 for (int i = 0; i < npts; i++) |
1948
|
1201 prow[i] = tmp_data[i*nr + j]; |
|
1202 |
3887
|
1203 F77_FUNC (cfftf, CFFTF) (npts, prow, pwsave); |
677
|
1204 |
1321
|
1205 for (int i = 0; i < npts; i++) |
1948
|
1206 tmp_data[i*nr + j] = prow[i]; |
677
|
1207 } |
|
1208 |
1948
|
1209 return retval; |
677
|
1210 } |
|
1211 |
|
1212 ComplexMatrix |
|
1213 ComplexMatrix::ifourier2d (void) const |
|
1214 { |
1948
|
1215 ComplexMatrix retval; |
|
1216 |
677
|
1217 int nr = rows (); |
|
1218 int nc = cols (); |
1948
|
1219 |
677
|
1220 int npts, nsamples; |
1948
|
1221 |
677
|
1222 if (nr == 1 || nc == 1) |
|
1223 { |
|
1224 npts = nr > nc ? nr : nc; |
|
1225 nsamples = 1; |
|
1226 } |
|
1227 else |
|
1228 { |
|
1229 npts = nr; |
|
1230 nsamples = nc; |
|
1231 } |
|
1232 |
|
1233 int nn = 4*npts+15; |
1948
|
1234 |
|
1235 Array<Complex> wsave (nn); |
|
1236 Complex *pwsave = wsave.fortran_vec (); |
|
1237 |
|
1238 retval = *this; |
|
1239 Complex *tmp_data = retval.fortran_vec (); |
|
1240 |
3887
|
1241 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1242 |
|
1243 for (int j = 0; j < nsamples; j++) |
4153
|
1244 { |
|
1245 OCTAVE_QUIT; |
|
1246 |
|
1247 F77_FUNC (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
|
1248 } |
677
|
1249 |
1321
|
1250 for (int j = 0; j < npts*nsamples; j++) |
3572
|
1251 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
677
|
1252 |
|
1253 npts = nc; |
|
1254 nsamples = nr; |
|
1255 nn = 4*npts+15; |
1948
|
1256 |
|
1257 wsave.resize (nn); |
|
1258 pwsave = wsave.fortran_vec (); |
|
1259 |
|
1260 Array<Complex> row (npts); |
|
1261 Complex *prow = row.fortran_vec (); |
|
1262 |
3887
|
1263 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1264 |
1321
|
1265 for (int j = 0; j < nsamples; j++) |
677
|
1266 { |
4153
|
1267 OCTAVE_QUIT; |
|
1268 |
677
|
1269 for (int i = 0; i < npts; i++) |
1948
|
1270 prow[i] = tmp_data[i*nr + j]; |
|
1271 |
3887
|
1272 F77_FUNC (cfftb, CFFTB) (npts, prow, pwsave); |
677
|
1273 |
1321
|
1274 for (int i = 0; i < npts; i++) |
3572
|
1275 tmp_data[i*nr + j] = prow[i] / static_cast<double> (npts); |
677
|
1276 } |
|
1277 |
1948
|
1278 return retval; |
677
|
1279 } |
|
1280 |
3827
|
1281 #endif |
|
1282 |
458
|
1283 ComplexDET |
|
1284 ComplexMatrix::determinant (void) const |
|
1285 { |
|
1286 int info; |
|
1287 double rcond; |
|
1288 return determinant (info, rcond); |
|
1289 } |
|
1290 |
|
1291 ComplexDET |
|
1292 ComplexMatrix::determinant (int& info) const |
|
1293 { |
|
1294 double rcond; |
|
1295 return determinant (info, rcond); |
|
1296 } |
|
1297 |
|
1298 ComplexDET |
532
|
1299 ComplexMatrix::determinant (int& info, double& rcond) const |
458
|
1300 { |
|
1301 ComplexDET retval; |
|
1302 |
|
1303 int nr = rows (); |
|
1304 int nc = cols (); |
|
1305 |
|
1306 if (nr == 0 || nc == 0) |
|
1307 { |
|
1308 Complex d[2]; |
|
1309 d[0] = 1.0; |
|
1310 d[1] = 0.0; |
|
1311 retval = ComplexDET (d); |
|
1312 } |
|
1313 else |
|
1314 { |
|
1315 info = 0; |
1948
|
1316 |
|
1317 Array<int> ipvt (nr); |
|
1318 int *pipvt = ipvt.fortran_vec (); |
|
1319 |
|
1320 Array<Complex> z (nr); |
|
1321 Complex *pz = z.fortran_vec (); |
|
1322 |
|
1323 ComplexMatrix atmp = *this; |
|
1324 Complex *tmp_data = atmp.fortran_vec (); |
|
1325 |
|
1326 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nr, pipvt, rcond, pz)); |
|
1327 |
|
1328 if (f77_exception_encountered) |
|
1329 (*current_liboctave_error_handler) ("unrecoverable error in zgeco"); |
458
|
1330 else |
|
1331 { |
1948
|
1332 volatile double rcond_plus_one = rcond + 1.0; |
|
1333 |
3838
|
1334 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
1948
|
1335 { |
|
1336 info = -1; |
|
1337 retval = ComplexDET (); |
|
1338 } |
|
1339 else |
|
1340 { |
|
1341 Complex d[2]; |
|
1342 |
|
1343 F77_XFCN (zgedi, ZGEDI, (tmp_data, nr, nr, pipvt, d, pz, 10)); |
|
1344 |
|
1345 if (f77_exception_encountered) |
|
1346 (*current_liboctave_error_handler) |
|
1347 ("unrecoverable error in dgedi"); |
|
1348 else |
|
1349 retval = ComplexDET (d); |
|
1350 } |
458
|
1351 } |
|
1352 } |
|
1353 |
|
1354 return retval; |
|
1355 } |
|
1356 |
|
1357 ComplexMatrix |
|
1358 ComplexMatrix::solve (const Matrix& b) const |
|
1359 { |
|
1360 int info; |
|
1361 double rcond; |
3480
|
1362 return solve (b, info, rcond, 0); |
458
|
1363 } |
|
1364 |
|
1365 ComplexMatrix |
|
1366 ComplexMatrix::solve (const Matrix& b, int& info) const |
|
1367 { |
|
1368 double rcond; |
3480
|
1369 return solve (b, info, rcond, 0); |
458
|
1370 } |
|
1371 |
|
1372 ComplexMatrix |
|
1373 ComplexMatrix::solve (const Matrix& b, int& info, double& rcond) const |
|
1374 { |
3480
|
1375 return solve (b, info, rcond, 0); |
|
1376 } |
|
1377 |
|
1378 ComplexMatrix |
|
1379 ComplexMatrix::solve (const Matrix& b, int& info, double& rcond, |
|
1380 solve_singularity_handler sing_handler) const |
|
1381 { |
458
|
1382 ComplexMatrix tmp (b); |
3480
|
1383 return solve (tmp, info, rcond, sing_handler); |
458
|
1384 } |
|
1385 |
|
1386 ComplexMatrix |
|
1387 ComplexMatrix::solve (const ComplexMatrix& b) const |
|
1388 { |
|
1389 int info; |
|
1390 double rcond; |
3480
|
1391 return solve (b, info, rcond, 0); |
458
|
1392 } |
|
1393 |
|
1394 ComplexMatrix |
|
1395 ComplexMatrix::solve (const ComplexMatrix& b, int& info) const |
|
1396 { |
|
1397 double rcond; |
3480
|
1398 return solve (b, info, rcond, 0); |
458
|
1399 } |
3480
|
1400 |
458
|
1401 ComplexMatrix |
532
|
1402 ComplexMatrix::solve (const ComplexMatrix& b, int& info, double& rcond) const |
458
|
1403 { |
3480
|
1404 return solve (b, info, rcond, 0); |
|
1405 } |
|
1406 |
|
1407 ComplexMatrix |
|
1408 ComplexMatrix::solve (const ComplexMatrix& b, int& info, double& rcond, |
|
1409 solve_singularity_handler sing_handler) const |
|
1410 { |
458
|
1411 ComplexMatrix retval; |
|
1412 |
|
1413 int nr = rows (); |
|
1414 int nc = cols (); |
1948
|
1415 |
|
1416 if (nr == 0 || nc == 0 || nr != nc || nr != b.rows ()) |
|
1417 (*current_liboctave_error_handler) |
|
1418 ("matrix dimension mismatch in solution of linear equations"); |
458
|
1419 else |
|
1420 { |
1948
|
1421 info = 0; |
|
1422 |
|
1423 Array<int> ipvt (nr); |
|
1424 int *pipvt = ipvt.fortran_vec (); |
|
1425 |
|
1426 Array<Complex> z (nr); |
|
1427 Complex *pz = z.fortran_vec (); |
|
1428 |
|
1429 ComplexMatrix atmp = *this; |
|
1430 Complex *tmp_data = atmp.fortran_vec (); |
|
1431 |
|
1432 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nr, pipvt, rcond, pz)); |
|
1433 |
|
1434 if (f77_exception_encountered) |
|
1435 (*current_liboctave_error_handler) ("unrecoverable error in zgeco"); |
|
1436 else |
|
1437 { |
|
1438 volatile double rcond_plus_one = rcond + 1.0; |
|
1439 |
3838
|
1440 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
1948
|
1441 { |
|
1442 info = -2; |
3480
|
1443 |
|
1444 if (sing_handler) |
|
1445 sing_handler (rcond); |
|
1446 else |
|
1447 (*current_liboctave_error_handler) |
|
1448 ("matrix singular to machine precision, rcond = %g", |
|
1449 rcond); |
1948
|
1450 } |
|
1451 else |
|
1452 { |
|
1453 retval = b; |
|
1454 Complex *result = retval.fortran_vec (); |
|
1455 |
|
1456 int b_nc = b.cols (); |
|
1457 |
|
1458 for (volatile int j = 0; j < b_nc; j++) |
|
1459 { |
|
1460 F77_XFCN (zgesl, ZGESL, (tmp_data, nr, nr, pipvt, |
|
1461 &result[nr*j], 0)); |
|
1462 |
|
1463 if (f77_exception_encountered) |
|
1464 { |
|
1465 (*current_liboctave_error_handler) |
|
1466 ("unrecoverable error in dgesl"); |
|
1467 |
|
1468 break; |
|
1469 } |
|
1470 } |
|
1471 } |
|
1472 } |
458
|
1473 } |
|
1474 |
|
1475 return retval; |
|
1476 } |
|
1477 |
|
1478 ComplexColumnVector |
3585
|
1479 ComplexMatrix::solve (const ColumnVector& b) const |
|
1480 { |
|
1481 int info; |
|
1482 double rcond; |
|
1483 return solve (ComplexColumnVector (b), info, rcond, 0); |
|
1484 } |
|
1485 |
|
1486 ComplexColumnVector |
|
1487 ComplexMatrix::solve (const ColumnVector& b, int& info) const |
|
1488 { |
|
1489 double rcond; |
|
1490 return solve (ComplexColumnVector (b), info, rcond, 0); |
|
1491 } |
|
1492 |
|
1493 ComplexColumnVector |
|
1494 ComplexMatrix::solve (const ColumnVector& b, int& info, double& rcond) const |
|
1495 { |
|
1496 return solve (ComplexColumnVector (b), info, rcond, 0); |
|
1497 } |
|
1498 |
|
1499 ComplexColumnVector |
|
1500 ComplexMatrix::solve (const ColumnVector& b, int& info, double& rcond, |
|
1501 solve_singularity_handler sing_handler) const |
|
1502 { |
|
1503 return solve (ComplexColumnVector (b), info, rcond, sing_handler); |
|
1504 } |
|
1505 |
|
1506 ComplexColumnVector |
458
|
1507 ComplexMatrix::solve (const ComplexColumnVector& b) const |
|
1508 { |
|
1509 int info; |
|
1510 double rcond; |
3480
|
1511 return solve (b, info, rcond, 0); |
458
|
1512 } |
|
1513 |
|
1514 ComplexColumnVector |
|
1515 ComplexMatrix::solve (const ComplexColumnVector& b, int& info) const |
|
1516 { |
|
1517 double rcond; |
3480
|
1518 return solve (b, info, rcond, 0); |
458
|
1519 } |
|
1520 |
|
1521 ComplexColumnVector |
|
1522 ComplexMatrix::solve (const ComplexColumnVector& b, int& info, |
532
|
1523 double& rcond) const |
458
|
1524 { |
3480
|
1525 return solve (b, info, rcond, 0); |
|
1526 } |
|
1527 |
|
1528 ComplexColumnVector |
|
1529 ComplexMatrix::solve (const ComplexColumnVector& b, int& info, |
|
1530 double& rcond, |
|
1531 solve_singularity_handler sing_handler) const |
|
1532 { |
458
|
1533 ComplexColumnVector retval; |
|
1534 |
|
1535 int nr = rows (); |
|
1536 int nc = cols (); |
1948
|
1537 |
|
1538 if (nr == 0 || nc == 0 || nr != nc || nr != b.length ()) |
|
1539 (*current_liboctave_error_handler) |
|
1540 ("matrix dimension mismatch in solution of linear equations"); |
458
|
1541 else |
|
1542 { |
1948
|
1543 info = 0; |
|
1544 |
|
1545 Array<int> ipvt (nr); |
|
1546 int *pipvt = ipvt.fortran_vec (); |
|
1547 |
|
1548 Array<Complex> z (nr); |
|
1549 Complex *pz = z.fortran_vec (); |
|
1550 |
|
1551 ComplexMatrix atmp = *this; |
|
1552 Complex *tmp_data = atmp.fortran_vec (); |
|
1553 |
|
1554 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nr, pipvt, rcond, pz)); |
|
1555 |
|
1556 if (f77_exception_encountered) |
|
1557 (*current_liboctave_error_handler) |
3838
|
1558 ("unrecoverable error in zgeco"); |
1948
|
1559 else |
|
1560 { |
|
1561 volatile double rcond_plus_one = rcond + 1.0; |
|
1562 |
3838
|
1563 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
1948
|
1564 { |
|
1565 info = -2; |
3480
|
1566 |
|
1567 if (sing_handler) |
|
1568 sing_handler (rcond); |
|
1569 else |
|
1570 (*current_liboctave_error_handler) |
|
1571 ("matrix singular to machine precision, rcond = %g", |
|
1572 rcond); |
1948
|
1573 } |
|
1574 else |
|
1575 { |
|
1576 retval = b; |
|
1577 Complex *result = retval.fortran_vec (); |
|
1578 |
|
1579 F77_XFCN (zgesl, ZGESL, (tmp_data, nr, nr, pipvt, result, 0)); |
|
1580 |
|
1581 if (f77_exception_encountered) |
|
1582 (*current_liboctave_error_handler) |
|
1583 ("unrecoverable error in dgesl"); |
|
1584 } |
|
1585 } |
458
|
1586 } |
|
1587 |
|
1588 return retval; |
|
1589 } |
|
1590 |
|
1591 ComplexMatrix |
3585
|
1592 ComplexMatrix::lssolve (const Matrix& b) const |
|
1593 { |
|
1594 int info; |
|
1595 int rank; |
|
1596 return lssolve (ComplexMatrix (b), info, rank); |
|
1597 } |
|
1598 |
|
1599 ComplexMatrix |
|
1600 ComplexMatrix::lssolve (const Matrix& b, int& info) const |
|
1601 { |
|
1602 int rank; |
|
1603 return lssolve (ComplexMatrix (b), info, rank); |
|
1604 } |
|
1605 |
|
1606 ComplexMatrix |
|
1607 ComplexMatrix::lssolve (const Matrix& b, int& info, int& rank) const |
|
1608 { |
|
1609 return lssolve (ComplexMatrix (b), info, rank); |
|
1610 } |
|
1611 |
|
1612 ComplexMatrix |
458
|
1613 ComplexMatrix::lssolve (const ComplexMatrix& b) const |
|
1614 { |
|
1615 int info; |
|
1616 int rank; |
|
1617 return lssolve (b, info, rank); |
|
1618 } |
|
1619 |
|
1620 ComplexMatrix |
|
1621 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info) const |
|
1622 { |
|
1623 int rank; |
|
1624 return lssolve (b, info, rank); |
|
1625 } |
|
1626 |
|
1627 ComplexMatrix |
|
1628 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info, int& rank) const |
|
1629 { |
1948
|
1630 ComplexMatrix retval; |
|
1631 |
458
|
1632 int nrhs = b.cols (); |
|
1633 |
|
1634 int m = rows (); |
|
1635 int n = cols (); |
|
1636 |
|
1637 if (m == 0 || n == 0 || m != b.rows ()) |
1948
|
1638 (*current_liboctave_error_handler) |
|
1639 ("matrix dimension mismatch solution of linear equations"); |
|
1640 else |
458
|
1641 { |
1948
|
1642 ComplexMatrix atmp = *this; |
|
1643 Complex *tmp_data = atmp.fortran_vec (); |
|
1644 |
|
1645 int nrr = m > n ? m : n; |
|
1646 ComplexMatrix result (nrr, nrhs); |
|
1647 |
|
1648 for (int j = 0; j < nrhs; j++) |
|
1649 for (int i = 0; i < m; i++) |
|
1650 result.elem (i, j) = b.elem (i, j); |
|
1651 |
|
1652 Complex *presult = result.fortran_vec (); |
|
1653 |
|
1654 int len_s = m < n ? m : n; |
|
1655 Array<double> s (len_s); |
|
1656 double *ps = s.fortran_vec (); |
2563
|
1657 |
1948
|
1658 double rcond = -1.0; |
2563
|
1659 |
1948
|
1660 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1661 lrwork = lrwork > 1 ? lrwork : 1; |
|
1662 Array<double> rwork (lrwork); |
|
1663 double *prwork = rwork.fortran_vec (); |
|
1664 |
3752
|
1665 // Ask ZGELSS what the dimension of WORK should be. |
|
1666 |
|
1667 int lwork = -1; |
|
1668 |
|
1669 Array<Complex> work (1); |
|
1670 |
1948
|
1671 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
3752
|
1672 nrr, ps, rcond, rank, |
|
1673 work.fortran_vec (), lwork, prwork, |
|
1674 info)); |
1948
|
1675 |
|
1676 if (f77_exception_encountered) |
|
1677 (*current_liboctave_error_handler) ("unrecoverable error in zgelss"); |
|
1678 else |
|
1679 { |
3752
|
1680 lwork = static_cast<int> (real (work(0))); |
|
1681 work.resize (lwork); |
|
1682 |
|
1683 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
1684 nrr, ps, rcond, rank, |
|
1685 work.fortran_vec (), lwork, |
|
1686 prwork, info)); |
|
1687 |
|
1688 if (f77_exception_encountered) |
|
1689 (*current_liboctave_error_handler) |
|
1690 ("unrecoverable error in zgelss"); |
|
1691 else |
|
1692 { |
|
1693 retval.resize (n, nrhs); |
|
1694 for (int j = 0; j < nrhs; j++) |
|
1695 for (int i = 0; i < n; i++) |
|
1696 retval.elem (i, j) = result.elem (i, j); |
|
1697 } |
1948
|
1698 } |
458
|
1699 } |
|
1700 |
|
1701 return retval; |
|
1702 } |
|
1703 |
|
1704 ComplexColumnVector |
3585
|
1705 ComplexMatrix::lssolve (const ColumnVector& b) const |
|
1706 { |
|
1707 int info; |
|
1708 int rank; |
|
1709 return lssolve (ComplexColumnVector (b), info, rank); |
|
1710 } |
|
1711 |
|
1712 ComplexColumnVector |
|
1713 ComplexMatrix::lssolve (const ColumnVector& b, int& info) const |
|
1714 { |
|
1715 int rank; |
|
1716 return lssolve (ComplexColumnVector (b), info, rank); |
|
1717 } |
|
1718 |
|
1719 ComplexColumnVector |
|
1720 ComplexMatrix::lssolve (const ColumnVector& b, int& info, int& rank) const |
|
1721 { |
|
1722 return lssolve (ComplexColumnVector (b), info, rank); |
|
1723 } |
|
1724 |
|
1725 ComplexColumnVector |
458
|
1726 ComplexMatrix::lssolve (const ComplexColumnVector& b) const |
|
1727 { |
|
1728 int info; |
|
1729 int rank; |
|
1730 return lssolve (b, info, rank); |
|
1731 } |
|
1732 |
|
1733 ComplexColumnVector |
|
1734 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info) const |
|
1735 { |
|
1736 int rank; |
|
1737 return lssolve (b, info, rank); |
|
1738 } |
|
1739 |
|
1740 ComplexColumnVector |
|
1741 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info, |
|
1742 int& rank) const |
|
1743 { |
1948
|
1744 ComplexColumnVector retval; |
|
1745 |
458
|
1746 int nrhs = 1; |
|
1747 |
|
1748 int m = rows (); |
|
1749 int n = cols (); |
|
1750 |
|
1751 if (m == 0 || n == 0 || m != b.length ()) |
1948
|
1752 (*current_liboctave_error_handler) |
|
1753 ("matrix dimension mismatch solution of least squares problem"); |
|
1754 else |
458
|
1755 { |
1948
|
1756 ComplexMatrix atmp = *this; |
|
1757 Complex *tmp_data = atmp.fortran_vec (); |
|
1758 |
|
1759 int nrr = m > n ? m : n; |
|
1760 ComplexColumnVector result (nrr); |
|
1761 |
|
1762 for (int i = 0; i < m; i++) |
|
1763 result.elem (i) = b.elem (i); |
|
1764 |
|
1765 Complex *presult = result.fortran_vec (); |
|
1766 |
|
1767 int len_s = m < n ? m : n; |
|
1768 Array<double> s (len_s); |
|
1769 double *ps = s.fortran_vec (); |
|
1770 |
|
1771 double rcond = -1.0; |
|
1772 |
|
1773 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1774 lrwork = lrwork > 1 ? lrwork : 1; |
|
1775 Array<double> rwork (lrwork); |
|
1776 double *prwork = rwork.fortran_vec (); |
|
1777 |
3752
|
1778 // Ask ZGELSS what the dimension of WORK should be. |
|
1779 |
|
1780 int lwork = -1; |
|
1781 |
|
1782 Array<Complex> work (1); |
|
1783 |
1948
|
1784 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
3752
|
1785 nrr, ps, rcond, rank, |
|
1786 work.fortran_vec (), lwork, prwork, |
|
1787 info)); |
1948
|
1788 |
|
1789 if (f77_exception_encountered) |
|
1790 (*current_liboctave_error_handler) ("unrecoverable error in zgelss"); |
|
1791 else |
|
1792 { |
3752
|
1793 lwork = static_cast<int> (real (work(0))); |
|
1794 work.resize (lwork); |
|
1795 |
|
1796 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
1797 nrr, ps, rcond, rank, |
|
1798 work.fortran_vec (), lwork, |
|
1799 prwork, info)); |
|
1800 |
|
1801 if (f77_exception_encountered) |
|
1802 (*current_liboctave_error_handler) |
|
1803 ("unrecoverable error in zgelss"); |
|
1804 else |
|
1805 { |
|
1806 retval.resize (n); |
|
1807 for (int i = 0; i < n; i++) |
|
1808 retval.elem (i) = result.elem (i); |
|
1809 } |
1948
|
1810 } |
458
|
1811 } |
|
1812 |
|
1813 return retval; |
|
1814 } |
|
1815 |
1819
|
1816 // Constants for matrix exponential calculation. |
|
1817 |
|
1818 static double padec [] = |
|
1819 { |
|
1820 5.0000000000000000e-1, |
|
1821 1.1666666666666667e-1, |
|
1822 1.6666666666666667e-2, |
|
1823 1.6025641025641026e-3, |
|
1824 1.0683760683760684e-4, |
|
1825 4.8562548562548563e-6, |
|
1826 1.3875013875013875e-7, |
|
1827 1.9270852604185938e-9, |
|
1828 }; |
|
1829 |
|
1830 ComplexMatrix |
|
1831 ComplexMatrix::expm (void) const |
|
1832 { |
|
1833 ComplexMatrix retval; |
|
1834 |
|
1835 ComplexMatrix m = *this; |
|
1836 |
|
1837 int nc = columns (); |
|
1838 |
3130
|
1839 // Preconditioning step 1: trace normalization to reduce dynamic |
|
1840 // range of poles, but avoid making stable eigenvalues unstable. |
|
1841 |
1819
|
1842 // trace shift value |
|
1843 Complex trshift = 0.0; |
|
1844 |
|
1845 for (int i = 0; i < nc; i++) |
|
1846 trshift += m.elem (i, i); |
|
1847 |
|
1848 trshift /= nc; |
|
1849 |
3130
|
1850 if (trshift.real () < 0.0) |
|
1851 trshift = trshift.imag (); |
|
1852 |
1819
|
1853 for (int i = 0; i < nc; i++) |
|
1854 m.elem (i, i) -= trshift; |
|
1855 |
|
1856 // Preconditioning step 2: eigenvalue balancing. |
3331
|
1857 // code follows development in AEPBAL |
|
1858 |
|
1859 Complex *mp = m.fortran_vec (); |
3467
|
1860 |
|
1861 int info, ilo, ihi,ilos,ihis; |
3468
|
1862 Array<double> dpermute (nc); |
|
1863 Array<double> dscale (nc); |
|
1864 |
|
1865 // XXX FIXME XXX -- should pass job as a parameter in expm |
|
1866 |
|
1867 // Permute first |
|
1868 char job = 'P'; |
3467
|
1869 F77_XFCN (zgebal, ZGEBAL, (&job, nc, mp, nc, ilo, ihi, |
3469
|
1870 dpermute.fortran_vec (), info, 1L, 1L)); |
3331
|
1871 |
|
1872 if (f77_exception_encountered) |
|
1873 { |
|
1874 (*current_liboctave_error_handler) ("unrecoverable error in zgebal"); |
|
1875 return retval; |
|
1876 } |
|
1877 |
3468
|
1878 // then scale |
|
1879 job = 'S'; |
3467
|
1880 F77_XFCN (zgebal, ZGEBAL, (&job, nc, mp, nc, ilos, ihis, |
3469
|
1881 dscale.fortran_vec (), info, 1L, 1L)); |
3331
|
1882 |
|
1883 if (f77_exception_encountered) |
|
1884 { |
3467
|
1885 (*current_liboctave_error_handler) ("unrecoverable error in zgebal"); |
3331
|
1886 return retval; |
|
1887 } |
1819
|
1888 |
|
1889 // Preconditioning step 3: scaling. |
|
1890 |
|
1891 ColumnVector work (nc); |
3130
|
1892 double inf_norm; |
|
1893 |
3331
|
1894 F77_XFCN (xzlange, XZLANGE, ("I", nc, nc, m.fortran_vec (), nc, |
|
1895 work.fortran_vec (), inf_norm)); |
|
1896 |
|
1897 if (f77_exception_encountered) |
|
1898 { |
|
1899 (*current_liboctave_error_handler) ("unrecoverable error in zlange"); |
|
1900 return retval; |
|
1901 } |
1819
|
1902 |
2800
|
1903 int sqpow = (inf_norm > 0.0 |
|
1904 ? static_cast<int> (1.0 + log (inf_norm) / log (2.0)) : 0); |
1819
|
1905 |
|
1906 // Check whether we need to square at all. |
|
1907 |
|
1908 if (sqpow < 0) |
|
1909 sqpow = 0; |
|
1910 |
|
1911 if (sqpow > 0) |
|
1912 { |
|
1913 double scale_factor = 1.0; |
|
1914 for (int i = 0; i < sqpow; i++) |
|
1915 scale_factor *= 2.0; |
|
1916 |
|
1917 m = m / scale_factor; |
|
1918 } |
|
1919 |
|
1920 // npp, dpp: pade' approx polynomial matrices. |
|
1921 |
|
1922 ComplexMatrix npp (nc, nc, 0.0); |
|
1923 ComplexMatrix dpp = npp; |
|
1924 |
|
1925 // Now powers a^8 ... a^1. |
|
1926 |
|
1927 int minus_one_j = -1; |
|
1928 for (int j = 7; j >= 0; j--) |
|
1929 { |
|
1930 npp = m * npp + m * padec[j]; |
|
1931 dpp = m * dpp + m * (minus_one_j * padec[j]); |
|
1932 minus_one_j *= -1; |
|
1933 } |
|
1934 |
|
1935 // Zero power. |
|
1936 |
|
1937 dpp = -dpp; |
|
1938 for (int j = 0; j < nc; j++) |
|
1939 { |
|
1940 npp.elem (j, j) += 1.0; |
|
1941 dpp.elem (j, j) += 1.0; |
|
1942 } |
|
1943 |
|
1944 // Compute pade approximation = inverse (dpp) * npp. |
|
1945 |
|
1946 retval = dpp.solve (npp); |
|
1947 |
|
1948 // Reverse preconditioning step 3: repeated squaring. |
|
1949 |
|
1950 while (sqpow) |
|
1951 { |
|
1952 retval = retval * retval; |
|
1953 sqpow--; |
|
1954 } |
|
1955 |
|
1956 // Reverse preconditioning step 2: inverse balancing. |
3467
|
1957 // Done in two steps: inverse scaling, then inverse permutation |
|
1958 |
|
1959 // inverse scaling (diagonal transformation) |
3468
|
1960 for (int i = 0; i < nc; i++) |
|
1961 for (int j = 0; j < nc; j++) |
|
1962 retval(i,j) *= dscale(i) / dscale(j); |
3467
|
1963 |
4153
|
1964 OCTAVE_QUIT; |
|
1965 |
3467
|
1966 // construct balancing permutation vector |
3468
|
1967 Array<int> ipermute (nc); |
|
1968 for (int i = 0; i < nc; i++) |
|
1969 ipermute(i) = i; // initialize to identity permutation |
3467
|
1970 |
|
1971 // leading permutations in forward order |
3468
|
1972 for (int i = 0; i < (ilo-1); i++) |
|
1973 { |
|
1974 int swapidx = static_cast<int> (dpermute(i)) - 1; |
|
1975 int tmp = ipermute(i); |
|
1976 ipermute(i) = ipermute(swapidx); |
|
1977 ipermute(swapidx) = tmp; |
|
1978 } |
3467
|
1979 |
|
1980 // trailing permutations must be done in reverse order |
3468
|
1981 for (int i = nc - 1; i >= ihi; i--) |
|
1982 { |
|
1983 int swapidx = static_cast<int> (dpermute(i)) - 1; |
|
1984 int tmp = ipermute(i); |
|
1985 ipermute(i) = ipermute(swapidx); |
|
1986 ipermute(swapidx) = tmp; |
|
1987 } |
3467
|
1988 |
|
1989 // construct inverse balancing permutation vector |
3468
|
1990 Array<int> invpvec (nc); |
|
1991 for (int i = 0; i < nc; i++) |
|
1992 invpvec(ipermute(i)) = i; // Thanks to R. A. Lippert for this method |
3467
|
1993 |
4153
|
1994 OCTAVE_QUIT; |
|
1995 |
3467
|
1996 ComplexMatrix tmpMat = retval; |
3468
|
1997 for (int i = 0; i < nc; i++) |
|
1998 for (int j = 0; j < nc; j++) |
|
1999 retval(i,j) = tmpMat(invpvec(i),invpvec(j)); |
1819
|
2000 |
|
2001 // Reverse preconditioning step 1: fix trace normalization. |
|
2002 |
3130
|
2003 return exp (trshift) * retval; |
1819
|
2004 } |
|
2005 |
1205
|
2006 // column vector by row vector -> matrix operations |
|
2007 |
|
2008 ComplexMatrix |
|
2009 operator * (const ColumnVector& v, const ComplexRowVector& a) |
|
2010 { |
|
2011 ComplexColumnVector tmp (v); |
|
2012 return tmp * a; |
|
2013 } |
|
2014 |
|
2015 ComplexMatrix |
|
2016 operator * (const ComplexColumnVector& a, const RowVector& b) |
|
2017 { |
|
2018 ComplexRowVector tmp (b); |
|
2019 return a * tmp; |
|
2020 } |
|
2021 |
|
2022 ComplexMatrix |
|
2023 operator * (const ComplexColumnVector& v, const ComplexRowVector& a) |
|
2024 { |
1948
|
2025 ComplexMatrix retval; |
|
2026 |
1205
|
2027 int len = v.length (); |
3233
|
2028 |
|
2029 if (len != 0) |
1205
|
2030 { |
3233
|
2031 int a_len = a.length (); |
|
2032 |
|
2033 retval.resize (len, a_len); |
|
2034 Complex *c = retval.fortran_vec (); |
|
2035 |
|
2036 F77_XFCN (zgemm, ZGEMM, ("N", "N", len, a_len, 1, 1.0, |
|
2037 v.data (), len, a.data (), 1, 0.0, |
|
2038 c, len, 1L, 1L)); |
|
2039 |
|
2040 if (f77_exception_encountered) |
|
2041 (*current_liboctave_error_handler) |
|
2042 ("unrecoverable error in zgemm"); |
1205
|
2043 } |
|
2044 |
1948
|
2045 return retval; |
1205
|
2046 } |
|
2047 |
458
|
2048 // matrix by diagonal matrix -> matrix operations |
|
2049 |
|
2050 ComplexMatrix& |
|
2051 ComplexMatrix::operator += (const DiagMatrix& a) |
|
2052 { |
|
2053 int nr = rows (); |
|
2054 int nc = cols (); |
2384
|
2055 |
|
2056 int a_nr = rows (); |
|
2057 int a_nc = cols (); |
|
2058 |
|
2059 if (nr != a_nr || nc != a_nc) |
458
|
2060 { |
2384
|
2061 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889
|
2062 return *this; |
458
|
2063 } |
|
2064 |
|
2065 for (int i = 0; i < a.length (); i++) |
|
2066 elem (i, i) += a.elem (i, i); |
|
2067 |
|
2068 return *this; |
|
2069 } |
|
2070 |
|
2071 ComplexMatrix& |
|
2072 ComplexMatrix::operator -= (const DiagMatrix& a) |
|
2073 { |
|
2074 int nr = rows (); |
|
2075 int nc = cols (); |
2384
|
2076 |
|
2077 int a_nr = rows (); |
|
2078 int a_nc = cols (); |
|
2079 |
|
2080 if (nr != a_nr || nc != a_nc) |
458
|
2081 { |
2384
|
2082 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889
|
2083 return *this; |
458
|
2084 } |
|
2085 |
|
2086 for (int i = 0; i < a.length (); i++) |
|
2087 elem (i, i) -= a.elem (i, i); |
|
2088 |
|
2089 return *this; |
|
2090 } |
|
2091 |
|
2092 ComplexMatrix& |
|
2093 ComplexMatrix::operator += (const ComplexDiagMatrix& a) |
|
2094 { |
|
2095 int nr = rows (); |
|
2096 int nc = cols (); |
2384
|
2097 |
|
2098 int a_nr = rows (); |
|
2099 int a_nc = cols (); |
|
2100 |
|
2101 if (nr != a_nr || nc != a_nc) |
458
|
2102 { |
2384
|
2103 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889
|
2104 return *this; |
458
|
2105 } |
|
2106 |
|
2107 for (int i = 0; i < a.length (); i++) |
|
2108 elem (i, i) += a.elem (i, i); |
|
2109 |
|
2110 return *this; |
|
2111 } |
|
2112 |
|
2113 ComplexMatrix& |
|
2114 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) |
|
2115 { |
|
2116 int nr = rows (); |
|
2117 int nc = cols (); |
2384
|
2118 |
|
2119 int a_nr = rows (); |
|
2120 int a_nc = cols (); |
|
2121 |
|
2122 if (nr != a_nr || nc != a_nc) |
458
|
2123 { |
2384
|
2124 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889
|
2125 return *this; |
458
|
2126 } |
|
2127 |
|
2128 for (int i = 0; i < a.length (); i++) |
|
2129 elem (i, i) -= a.elem (i, i); |
|
2130 |
|
2131 return *this; |
|
2132 } |
|
2133 |
|
2134 // matrix by matrix -> matrix operations |
|
2135 |
|
2136 ComplexMatrix& |
|
2137 ComplexMatrix::operator += (const Matrix& a) |
|
2138 { |
|
2139 int nr = rows (); |
|
2140 int nc = cols (); |
2384
|
2141 |
|
2142 int a_nr = a.rows (); |
|
2143 int a_nc = a.cols (); |
|
2144 |
|
2145 if (nr != a_nr || nc != a_nc) |
458
|
2146 { |
2384
|
2147 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
2148 return *this; |
|
2149 } |
|
2150 |
|
2151 if (nr == 0 || nc == 0) |
|
2152 return *this; |
|
2153 |
|
2154 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2155 |
3769
|
2156 mx_inline_add2 (d, a.data (), length ()); |
458
|
2157 return *this; |
|
2158 } |
|
2159 |
|
2160 ComplexMatrix& |
|
2161 ComplexMatrix::operator -= (const Matrix& a) |
|
2162 { |
|
2163 int nr = rows (); |
|
2164 int nc = cols (); |
2384
|
2165 |
|
2166 int a_nr = a.rows (); |
|
2167 int a_nc = a.cols (); |
|
2168 |
|
2169 if (nr != a_nr || nc != a_nc) |
458
|
2170 { |
2384
|
2171 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
2172 return *this; |
|
2173 } |
|
2174 |
|
2175 if (nr == 0 || nc == 0) |
|
2176 return *this; |
|
2177 |
|
2178 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2179 |
3769
|
2180 mx_inline_subtract2 (d, a.data (), length ()); |
458
|
2181 return *this; |
|
2182 } |
|
2183 |
|
2184 // unary operations |
|
2185 |
2964
|
2186 boolMatrix |
458
|
2187 ComplexMatrix::operator ! (void) const |
|
2188 { |
2964
|
2189 int nr = rows (); |
|
2190 int nc = cols (); |
|
2191 |
|
2192 boolMatrix b (nr, nc); |
|
2193 |
|
2194 for (int j = 0; j < nc; j++) |
|
2195 for (int i = 0; i < nr; i++) |
|
2196 b.elem (i, j) = elem (i, j) != 0.0; |
|
2197 |
|
2198 return b; |
458
|
2199 } |
|
2200 |
|
2201 // other operations |
|
2202 |
|
2203 ComplexMatrix |
2676
|
2204 ComplexMatrix::map (c_c_Mapper f) const |
458
|
2205 { |
2676
|
2206 ComplexMatrix b (*this); |
|
2207 return b.apply (f); |
458
|
2208 } |
|
2209 |
2676
|
2210 Matrix |
|
2211 ComplexMatrix::map (d_c_Mapper f) const |
458
|
2212 { |
3248
|
2213 int nr = rows (); |
|
2214 int nc = cols (); |
|
2215 |
|
2216 Matrix retval (nr, nc); |
|
2217 |
|
2218 for (int j = 0; j < nc; j++) |
|
2219 for (int i = 0; i < nr; i++) |
|
2220 retval(i,j) = f (elem(i,j)); |
|
2221 |
|
2222 return retval; |
|
2223 } |
|
2224 |
|
2225 boolMatrix |
|
2226 ComplexMatrix::map (b_c_Mapper f) const |
|
2227 { |
|
2228 int nr = rows (); |
|
2229 int nc = cols (); |
|
2230 |
|
2231 boolMatrix retval (nr, nc); |
|
2232 |
|
2233 for (int j = 0; j < nc; j++) |
|
2234 for (int i = 0; i < nr; i++) |
|
2235 retval(i,j) = f (elem(i,j)); |
2676
|
2236 |
|
2237 return retval; |
|
2238 } |
|
2239 |
|
2240 ComplexMatrix& |
|
2241 ComplexMatrix::apply (c_c_Mapper f) |
|
2242 { |
|
2243 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2244 |
|
2245 for (int i = 0; i < length (); i++) |
|
2246 d[i] = f (d[i]); |
|
2247 |
|
2248 return *this; |
458
|
2249 } |
|
2250 |
2384
|
2251 bool |
|
2252 ComplexMatrix::any_element_is_inf_or_nan (void) const |
|
2253 { |
|
2254 int nr = rows (); |
|
2255 int nc = cols (); |
|
2256 |
|
2257 for (int j = 0; j < nc; j++) |
|
2258 for (int i = 0; i < nr; i++) |
|
2259 { |
|
2260 Complex val = elem (i, j); |
|
2261 if (xisinf (val) || xisnan (val)) |
|
2262 return true; |
|
2263 } |
|
2264 |
|
2265 return false; |
|
2266 } |
|
2267 |
2408
|
2268 // Return true if no elements have imaginary components. |
|
2269 |
|
2270 bool |
|
2271 ComplexMatrix::all_elements_are_real (void) const |
|
2272 { |
|
2273 int nr = rows (); |
|
2274 int nc = cols (); |
|
2275 |
|
2276 for (int j = 0; j < nc; j++) |
|
2277 for (int i = 0; i < nr; i++) |
|
2278 if (imag (elem (i, j)) != 0.0) |
|
2279 return false; |
|
2280 |
|
2281 return true; |
|
2282 } |
|
2283 |
1968
|
2284 // Return nonzero if any element of CM has a non-integer real or |
|
2285 // imaginary part. Also extract the largest and smallest (real or |
|
2286 // imaginary) values and return them in MAX_VAL and MIN_VAL. |
|
2287 |
2384
|
2288 bool |
1968
|
2289 ComplexMatrix::all_integers (double& max_val, double& min_val) const |
|
2290 { |
|
2291 int nr = rows (); |
2384
|
2292 int nc = cols (); |
1968
|
2293 |
|
2294 if (nr > 0 && nc > 0) |
|
2295 { |
|
2296 Complex val = elem (0, 0); |
|
2297 |
|
2298 double r_val = real (val); |
|
2299 double i_val = imag (val); |
|
2300 |
|
2301 max_val = r_val; |
|
2302 min_val = r_val; |
|
2303 |
|
2304 if (i_val > max_val) |
|
2305 max_val = i_val; |
|
2306 |
|
2307 if (i_val < max_val) |
|
2308 min_val = i_val; |
|
2309 } |
|
2310 else |
2384
|
2311 return false; |
1968
|
2312 |
|
2313 for (int j = 0; j < nc; j++) |
|
2314 for (int i = 0; i < nr; i++) |
|
2315 { |
|
2316 Complex val = elem (i, j); |
|
2317 |
|
2318 double r_val = real (val); |
|
2319 double i_val = imag (val); |
|
2320 |
|
2321 if (r_val > max_val) |
|
2322 max_val = r_val; |
|
2323 |
|
2324 if (i_val > max_val) |
|
2325 max_val = i_val; |
|
2326 |
|
2327 if (r_val < min_val) |
|
2328 min_val = r_val; |
|
2329 |
|
2330 if (i_val < min_val) |
|
2331 min_val = i_val; |
|
2332 |
|
2333 if (D_NINT (r_val) != r_val || D_NINT (i_val) != i_val) |
2384
|
2334 return false; |
1968
|
2335 } |
2384
|
2336 |
|
2337 return true; |
1968
|
2338 } |
|
2339 |
2384
|
2340 bool |
1968
|
2341 ComplexMatrix::too_large_for_float (void) const |
|
2342 { |
|
2343 int nr = rows (); |
2384
|
2344 int nc = cols (); |
1968
|
2345 |
|
2346 for (int j = 0; j < nc; j++) |
|
2347 for (int i = 0; i < nr; i++) |
|
2348 { |
|
2349 Complex val = elem (i, j); |
|
2350 |
|
2351 double r_val = real (val); |
|
2352 double i_val = imag (val); |
|
2353 |
|
2354 if (r_val > FLT_MAX |
|
2355 || i_val > FLT_MAX |
|
2356 || r_val < FLT_MIN |
|
2357 || i_val < FLT_MIN) |
2384
|
2358 return true; |
1968
|
2359 } |
|
2360 |
2384
|
2361 return false; |
1968
|
2362 } |
|
2363 |
4015
|
2364 // XXX FIXME XXX Do these really belong here? Maybe they should be |
|
2365 // in a base class? |
|
2366 |
2832
|
2367 boolMatrix |
4015
|
2368 ComplexMatrix::all (int dim) const |
458
|
2369 { |
4015
|
2370 MX_ALL_OP (dim); |
458
|
2371 } |
|
2372 |
2832
|
2373 boolMatrix |
4015
|
2374 ComplexMatrix::any (int dim) const |
458
|
2375 { |
4015
|
2376 MX_ANY_OP (dim); |
458
|
2377 } |
|
2378 |
|
2379 ComplexMatrix |
3723
|
2380 ComplexMatrix::cumprod (int dim) const |
458
|
2381 { |
4015
|
2382 MX_CUMULATIVE_OP (ComplexMatrix, Complex, *=); |
458
|
2383 } |
|
2384 |
|
2385 ComplexMatrix |
3723
|
2386 ComplexMatrix::cumsum (int dim) const |
458
|
2387 { |
4015
|
2388 MX_CUMULATIVE_OP (ComplexMatrix, Complex, +=); |
458
|
2389 } |
|
2390 |
|
2391 ComplexMatrix |
3723
|
2392 ComplexMatrix::prod (int dim) const |
458
|
2393 { |
3864
|
2394 MX_REDUCTION_OP (ComplexMatrix, *=, 1.0, 1.0); |
458
|
2395 } |
|
2396 |
|
2397 ComplexMatrix |
3723
|
2398 ComplexMatrix::sum (int dim) const |
458
|
2399 { |
3864
|
2400 MX_REDUCTION_OP (ComplexMatrix, +=, 0.0, 0.0); |
458
|
2401 } |
|
2402 |
|
2403 ComplexMatrix |
3723
|
2404 ComplexMatrix::sumsq (int dim) const |
458
|
2405 { |
3864
|
2406 #define ROW_EXPR \ |
|
2407 Complex d = elem (i, j); \ |
|
2408 retval.elem (i, 0) += d * conj (d) |
|
2409 |
|
2410 #define COL_EXPR \ |
|
2411 Complex d = elem (i, j); \ |
|
2412 retval.elem (0, j) += d * conj (d) |
|
2413 |
|
2414 MX_BASE_REDUCTION_OP (ComplexMatrix, ROW_EXPR, COL_EXPR, 0.0, 0.0); |
|
2415 |
|
2416 #undef ROW_EXPR |
|
2417 #undef COL_EXPR |
458
|
2418 } |
|
2419 |
|
2420 ComplexColumnVector |
|
2421 ComplexMatrix::diag (void) const |
|
2422 { |
|
2423 return diag (0); |
|
2424 } |
|
2425 |
|
2426 ComplexColumnVector |
|
2427 ComplexMatrix::diag (int k) const |
|
2428 { |
|
2429 int nnr = rows (); |
|
2430 int nnc = cols (); |
|
2431 if (k > 0) |
|
2432 nnc -= k; |
|
2433 else if (k < 0) |
|
2434 nnr += k; |
|
2435 |
|
2436 ComplexColumnVector d; |
|
2437 |
|
2438 if (nnr > 0 && nnc > 0) |
|
2439 { |
|
2440 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
2441 |
|
2442 d.resize (ndiag); |
|
2443 |
|
2444 if (k > 0) |
|
2445 { |
|
2446 for (int i = 0; i < ndiag; i++) |
|
2447 d.elem (i) = elem (i, i+k); |
|
2448 } |
|
2449 else if ( k < 0) |
|
2450 { |
|
2451 for (int i = 0; i < ndiag; i++) |
|
2452 d.elem (i) = elem (i-k, i); |
|
2453 } |
|
2454 else |
|
2455 { |
|
2456 for (int i = 0; i < ndiag; i++) |
|
2457 d.elem (i) = elem (i, i); |
|
2458 } |
|
2459 } |
|
2460 else |
3504
|
2461 std::cerr << "diag: requested diagonal out of range\n"; |
458
|
2462 |
|
2463 return d; |
|
2464 } |
|
2465 |
2354
|
2466 bool |
|
2467 ComplexMatrix::row_is_real_only (int i) const |
|
2468 { |
|
2469 bool retval = true; |
|
2470 |
|
2471 int nc = columns (); |
|
2472 |
|
2473 for (int j = 0; j < nc; j++) |
|
2474 { |
|
2475 if (imag (elem (i, j)) != 0.0) |
|
2476 { |
|
2477 retval = false; |
|
2478 break; |
|
2479 } |
|
2480 } |
|
2481 |
|
2482 return retval; |
|
2483 } |
|
2484 |
|
2485 bool |
|
2486 ComplexMatrix::column_is_real_only (int j) const |
|
2487 { |
|
2488 bool retval = true; |
|
2489 |
|
2490 int nr = rows (); |
|
2491 |
|
2492 for (int i = 0; i < nr; i++) |
|
2493 { |
|
2494 if (imag (elem (i, j)) != 0.0) |
|
2495 { |
|
2496 retval = false; |
|
2497 break; |
|
2498 } |
|
2499 } |
|
2500 |
|
2501 return retval; |
|
2502 } |
891
|
2503 |
458
|
2504 ComplexColumnVector |
|
2505 ComplexMatrix::row_min (void) const |
|
2506 { |
2354
|
2507 Array<int> index; |
|
2508 return row_min (index); |
458
|
2509 } |
|
2510 |
|
2511 ComplexColumnVector |
2354
|
2512 ComplexMatrix::row_min (Array<int>& index) const |
458
|
2513 { |
|
2514 ComplexColumnVector result; |
|
2515 |
|
2516 int nr = rows (); |
|
2517 int nc = cols (); |
|
2518 |
|
2519 if (nr > 0 && nc > 0) |
|
2520 { |
|
2521 result.resize (nr); |
2354
|
2522 index.resize (nr); |
458
|
2523 |
|
2524 for (int i = 0; i < nr; i++) |
|
2525 { |
3504
|
2526 int idx_j = 0; |
|
2527 |
|
2528 Complex tmp_min = elem (i, idx_j); |
2354
|
2529 |
|
2530 bool real_only = row_is_real_only (i); |
|
2531 |
|
2532 double abs_min = real_only ? real (tmp_min) : abs (tmp_min); |
|
2533 |
|
2534 if (xisnan (tmp_min)) |
3504
|
2535 idx_j = -1; |
891
|
2536 else |
|
2537 { |
|
2538 for (int j = 1; j < nc; j++) |
2354
|
2539 { |
|
2540 Complex tmp = elem (i, j); |
|
2541 |
|
2542 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
2543 |
|
2544 if (xisnan (tmp)) |
|
2545 { |
3504
|
2546 idx_j = -1; |
2354
|
2547 break; |
|
2548 } |
|
2549 else if (abs_tmp < abs_min) |
|
2550 { |
3504
|
2551 idx_j = j; |
2354
|
2552 tmp_min = tmp; |
|
2553 abs_min = abs_tmp; |
|
2554 } |
|
2555 } |
|
2556 |
3504
|
2557 result.elem (i) = (idx_j < 0) ? Complex_NaN_result : tmp_min; |
|
2558 index.elem (i) = idx_j; |
891
|
2559 } |
458
|
2560 } |
|
2561 } |
|
2562 |
|
2563 return result; |
|
2564 } |
|
2565 |
|
2566 ComplexColumnVector |
|
2567 ComplexMatrix::row_max (void) const |
|
2568 { |
2354
|
2569 Array<int> index; |
|
2570 return row_max (index); |
458
|
2571 } |
|
2572 |
|
2573 ComplexColumnVector |
2354
|
2574 ComplexMatrix::row_max (Array<int>& index) const |
458
|
2575 { |
|
2576 ComplexColumnVector result; |
|
2577 |
|
2578 int nr = rows (); |
|
2579 int nc = cols (); |
|
2580 |
|
2581 if (nr > 0 && nc > 0) |
|
2582 { |
|
2583 result.resize (nr); |
2354
|
2584 index.resize (nr); |
458
|
2585 |
|
2586 for (int i = 0; i < nr; i++) |
|
2587 { |
3504
|
2588 int idx_j = 0; |
|
2589 |
|
2590 Complex tmp_max = elem (i, idx_j); |
2354
|
2591 |
|
2592 bool real_only = row_is_real_only (i); |
|
2593 |
|
2594 double abs_max = real_only ? real (tmp_max) : abs (tmp_max); |
|
2595 |
|
2596 if (xisnan (tmp_max)) |
3504
|
2597 idx_j = -1; |
891
|
2598 else |
|
2599 { |
|
2600 for (int j = 1; j < nc; j++) |
2354
|
2601 { |
|
2602 Complex tmp = elem (i, j); |
|
2603 |
|
2604 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
2605 |
|
2606 if (xisnan (tmp)) |
|
2607 { |
3504
|
2608 idx_j = -1; |
2354
|
2609 break; |
|
2610 } |
|
2611 else if (abs_tmp > abs_max) |
|
2612 { |
3504
|
2613 idx_j = j; |
2354
|
2614 tmp_max = tmp; |
|
2615 abs_max = abs_tmp; |
|
2616 } |
|
2617 } |
|
2618 |
3504
|
2619 result.elem (i) = (idx_j < 0) ? Complex_NaN_result : tmp_max; |
|
2620 index.elem (i) = idx_j; |
891
|
2621 } |
458
|
2622 } |
|
2623 } |
|
2624 |
|
2625 return result; |
|
2626 } |
|
2627 |
|
2628 ComplexRowVector |
|
2629 ComplexMatrix::column_min (void) const |
|
2630 { |
2354
|
2631 Array<int> index; |
|
2632 return column_min (index); |
458
|
2633 } |
|
2634 |
|
2635 ComplexRowVector |
2354
|
2636 ComplexMatrix::column_min (Array<int>& index) const |
458
|
2637 { |
|
2638 ComplexRowVector result; |
|
2639 |
|
2640 int nr = rows (); |
|
2641 int nc = cols (); |
|
2642 |
|
2643 if (nr > 0 && nc > 0) |
|
2644 { |
|
2645 result.resize (nc); |
2354
|
2646 index.resize (nc); |
458
|
2647 |
|
2648 for (int j = 0; j < nc; j++) |
|
2649 { |
3504
|
2650 int idx_i = 0; |
|
2651 |
|
2652 Complex tmp_min = elem (idx_i, j); |
2354
|
2653 |
|
2654 bool real_only = column_is_real_only (j); |
|
2655 |
|
2656 double abs_min = real_only ? real (tmp_min) : abs (tmp_min); |
|
2657 |
|
2658 if (xisnan (tmp_min)) |
3504
|
2659 idx_i = -1; |
891
|
2660 else |
|
2661 { |
|
2662 for (int i = 1; i < nr; i++) |
2354
|
2663 { |
|
2664 Complex tmp = elem (i, j); |
|
2665 |
|
2666 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
2667 |
|
2668 if (xisnan (tmp)) |
|
2669 { |
3504
|
2670 idx_i = -1; |
2354
|
2671 break; |
|
2672 } |
|
2673 else if (abs_tmp < abs_min) |
|
2674 { |
3504
|
2675 idx_i = i; |
2354
|
2676 tmp_min = tmp; |
|
2677 abs_min = abs_tmp; |
|
2678 } |
|
2679 } |
|
2680 |
3504
|
2681 result.elem (j) = (idx_i < 0) ? Complex_NaN_result : tmp_min; |
|
2682 index.elem (j) = idx_i; |
891
|
2683 } |
458
|
2684 } |
|
2685 } |
|
2686 |
|
2687 return result; |
|
2688 } |
|
2689 |
|
2690 ComplexRowVector |
|
2691 ComplexMatrix::column_max (void) const |
|
2692 { |
2354
|
2693 Array<int> index; |
|
2694 return column_max (index); |
458
|
2695 } |
|
2696 |
|
2697 ComplexRowVector |
2354
|
2698 ComplexMatrix::column_max (Array<int>& index) const |
458
|
2699 { |
|
2700 ComplexRowVector result; |
|
2701 |
|
2702 int nr = rows (); |
|
2703 int nc = cols (); |
|
2704 |
|
2705 if (nr > 0 && nc > 0) |
|
2706 { |
|
2707 result.resize (nc); |
2354
|
2708 index.resize (nc); |
458
|
2709 |
|
2710 for (int j = 0; j < nc; j++) |
|
2711 { |
3504
|
2712 int idx_i = 0; |
|
2713 |
|
2714 Complex tmp_max = elem (idx_i, j); |
2354
|
2715 |
|
2716 bool real_only = column_is_real_only (j); |
|
2717 |
|
2718 double abs_max = real_only ? real (tmp_max) : abs (tmp_max); |
|
2719 |
|
2720 if (xisnan (tmp_max)) |
3504
|
2721 idx_i = -1; |
891
|
2722 else |
|
2723 { |
|
2724 for (int i = 1; i < nr; i++) |
2354
|
2725 { |
|
2726 Complex tmp = elem (i, j); |
|
2727 |
|
2728 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
2729 |
|
2730 if (xisnan (tmp)) |
|
2731 { |
3504
|
2732 idx_i = -1; |
2354
|
2733 break; |
|
2734 } |
|
2735 else if (abs_tmp > abs_max) |
|
2736 { |
3504
|
2737 idx_i = i; |
2354
|
2738 tmp_max = tmp; |
|
2739 abs_max = abs_tmp; |
|
2740 } |
|
2741 } |
|
2742 |
3504
|
2743 result.elem (j) = (idx_i < 0) ? Complex_NaN_result : tmp_max; |
|
2744 index.elem (j) = idx_i; |
891
|
2745 } |
458
|
2746 } |
|
2747 } |
|
2748 |
|
2749 return result; |
|
2750 } |
|
2751 |
|
2752 // i/o |
|
2753 |
3504
|
2754 std::ostream& |
|
2755 operator << (std::ostream& os, const ComplexMatrix& a) |
458
|
2756 { |
|
2757 for (int i = 0; i < a.rows (); i++) |
|
2758 { |
|
2759 for (int j = 0; j < a.cols (); j++) |
4130
|
2760 { |
|
2761 os << " "; |
|
2762 octave_write_complex (os, a.elem (i, j)); |
|
2763 } |
458
|
2764 os << "\n"; |
|
2765 } |
|
2766 return os; |
|
2767 } |
|
2768 |
3504
|
2769 std::istream& |
|
2770 operator >> (std::istream& is, ComplexMatrix& a) |
458
|
2771 { |
|
2772 int nr = a.rows (); |
|
2773 int nc = a.cols (); |
|
2774 |
|
2775 if (nr < 1 || nc < 1) |
3504
|
2776 is.clear (std::ios::badbit); |
458
|
2777 else |
|
2778 { |
|
2779 Complex tmp; |
|
2780 for (int i = 0; i < nr; i++) |
|
2781 for (int j = 0; j < nc; j++) |
|
2782 { |
4130
|
2783 tmp = octave_read_complex (is); |
458
|
2784 if (is) |
|
2785 a.elem (i, j) = tmp; |
|
2786 else |
2993
|
2787 goto done; |
458
|
2788 } |
|
2789 } |
|
2790 |
2993
|
2791 done: |
|
2792 |
458
|
2793 return is; |
|
2794 } |
|
2795 |
1819
|
2796 ComplexMatrix |
|
2797 Givens (const Complex& x, const Complex& y) |
|
2798 { |
|
2799 double cc; |
|
2800 Complex cs, temp_r; |
|
2801 |
3887
|
2802 F77_FUNC (zlartg, ZLARTG) (x, y, cc, cs, temp_r); |
1819
|
2803 |
|
2804 ComplexMatrix g (2, 2); |
|
2805 |
|
2806 g.elem (0, 0) = cc; |
|
2807 g.elem (1, 1) = cc; |
|
2808 g.elem (0, 1) = cs; |
|
2809 g.elem (1, 0) = -conj (cs); |
|
2810 |
|
2811 return g; |
|
2812 } |
|
2813 |
|
2814 ComplexMatrix |
|
2815 Sylvester (const ComplexMatrix& a, const ComplexMatrix& b, |
|
2816 const ComplexMatrix& c) |
|
2817 { |
|
2818 ComplexMatrix retval; |
|
2819 |
|
2820 // XXX FIXME XXX -- need to check that a, b, and c are all the same |
|
2821 // size. |
|
2822 |
|
2823 // Compute Schur decompositions |
|
2824 |
|
2825 ComplexSCHUR as (a, "U"); |
|
2826 ComplexSCHUR bs (b, "U"); |
|
2827 |
|
2828 // Transform c to new coordinates. |
|
2829 |
|
2830 ComplexMatrix ua = as.unitary_matrix (); |
|
2831 ComplexMatrix sch_a = as.schur_matrix (); |
|
2832 |
|
2833 ComplexMatrix ub = bs.unitary_matrix (); |
|
2834 ComplexMatrix sch_b = bs.schur_matrix (); |
|
2835 |
|
2836 ComplexMatrix cx = ua.hermitian () * c * ub; |
|
2837 |
|
2838 // Solve the sylvester equation, back-transform, and return the |
|
2839 // solution. |
|
2840 |
|
2841 int a_nr = a.rows (); |
|
2842 int b_nr = b.rows (); |
|
2843 |
|
2844 double scale; |
|
2845 int info; |
1950
|
2846 |
|
2847 Complex *pa = sch_a.fortran_vec (); |
|
2848 Complex *pb = sch_b.fortran_vec (); |
|
2849 Complex *px = cx.fortran_vec (); |
1819
|
2850 |
1950
|
2851 F77_XFCN (ztrsyl, ZTRSYL, ("N", "N", 1, a_nr, b_nr, pa, a_nr, pb, |
|
2852 b_nr, px, a_nr, scale, |
|
2853 info, 1L, 1L)); |
|
2854 |
|
2855 if (f77_exception_encountered) |
|
2856 (*current_liboctave_error_handler) ("unrecoverable error in ztrsyl"); |
|
2857 else |
|
2858 { |
|
2859 // XXX FIXME XXX -- check info? |
|
2860 |
|
2861 retval = -ua * cx * ub.hermitian (); |
|
2862 } |
1819
|
2863 |
|
2864 return retval; |
|
2865 } |
|
2866 |
2828
|
2867 ComplexMatrix |
|
2868 operator * (const ComplexMatrix& m, const Matrix& a) |
|
2869 { |
|
2870 ComplexMatrix tmp (a); |
|
2871 return m * tmp; |
|
2872 } |
|
2873 |
|
2874 ComplexMatrix |
|
2875 operator * (const Matrix& m, const ComplexMatrix& a) |
|
2876 { |
|
2877 ComplexMatrix tmp (m); |
|
2878 return tmp * a; |
|
2879 } |
|
2880 |
|
2881 ComplexMatrix |
|
2882 operator * (const ComplexMatrix& m, const ComplexMatrix& a) |
|
2883 { |
|
2884 ComplexMatrix retval; |
|
2885 |
|
2886 int nr = m.rows (); |
|
2887 int nc = m.cols (); |
|
2888 |
|
2889 int a_nr = a.rows (); |
|
2890 int a_nc = a.cols (); |
|
2891 |
|
2892 if (nc != a_nr) |
|
2893 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
|
2894 else |
|
2895 { |
|
2896 if (nr == 0 || nc == 0 || a_nc == 0) |
3760
|
2897 retval.resize (nr, a_nc, 0.0); |
2828
|
2898 else |
|
2899 { |
|
2900 int ld = nr; |
|
2901 int lda = a.rows (); |
|
2902 |
|
2903 retval.resize (nr, a_nc); |
|
2904 Complex *c = retval.fortran_vec (); |
|
2905 |
|
2906 F77_XFCN (zgemm, ZGEMM, ("N", "N", nr, a_nc, nc, 1.0, |
|
2907 m.data (), ld, a.data (), lda, 0.0, |
|
2908 c, nr, 1L, 1L)); |
|
2909 |
|
2910 if (f77_exception_encountered) |
|
2911 (*current_liboctave_error_handler) |
|
2912 ("unrecoverable error in zgemm"); |
|
2913 } |
|
2914 } |
|
2915 |
|
2916 return retval; |
|
2917 } |
|
2918 |
4309
|
2919 // XXX FIXME XXX -- it would be nice to share code among the min/max |
|
2920 // functions below. |
|
2921 |
|
2922 #define EMPTY_RETURN_CHECK(T) \ |
|
2923 if (nr == 0 || nc == 0) \ |
|
2924 return T (nr, nc); |
|
2925 |
|
2926 ComplexMatrix |
|
2927 min (const Complex& c, const ComplexMatrix& m) |
|
2928 { |
|
2929 int nr = m.rows (); |
|
2930 int nc = m.columns (); |
|
2931 |
|
2932 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
2933 |
|
2934 ComplexMatrix result (nr, nc); |
|
2935 |
|
2936 for (int j = 0; j < nc; j++) |
|
2937 for (int i = 0; i < nr; i++) |
|
2938 { |
|
2939 OCTAVE_QUIT; |
|
2940 result (i, j) = xmin (c, m (i, j)); |
|
2941 } |
|
2942 |
|
2943 return result; |
|
2944 } |
|
2945 |
|
2946 ComplexMatrix |
|
2947 min (const ComplexMatrix& m, const Complex& c) |
|
2948 { |
|
2949 int nr = m.rows (); |
|
2950 int nc = m.columns (); |
|
2951 |
|
2952 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
2953 |
|
2954 ComplexMatrix result (nr, nc); |
|
2955 |
|
2956 for (int j = 0; j < nc; j++) |
|
2957 for (int i = 0; i < nr; i++) |
|
2958 { |
|
2959 OCTAVE_QUIT; |
|
2960 result (i, j) = xmin (m (i, j), c); |
|
2961 } |
|
2962 |
|
2963 return result; |
|
2964 } |
|
2965 |
|
2966 ComplexMatrix |
|
2967 min (const ComplexMatrix& a, const ComplexMatrix& b) |
|
2968 { |
|
2969 int nr = a.rows (); |
|
2970 int nc = a.columns (); |
|
2971 |
|
2972 if (nr != b.rows () || nc != b.columns ()) |
|
2973 { |
|
2974 (*current_liboctave_error_handler) |
|
2975 ("two-arg min expecting args of same size"); |
|
2976 return ComplexMatrix (); |
|
2977 } |
|
2978 |
|
2979 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
2980 |
|
2981 ComplexMatrix result (nr, nc); |
|
2982 |
|
2983 for (int j = 0; j < nc; j++) |
|
2984 { |
|
2985 int columns_are_real_only = 1; |
|
2986 for (int i = 0; i < nr; i++) |
|
2987 { |
|
2988 OCTAVE_QUIT; |
|
2989 if (imag (a (i, j)) != 0.0 || imag (b (i, j)) != 0.0) |
|
2990 { |
|
2991 columns_are_real_only = 0; |
|
2992 break; |
|
2993 } |
|
2994 } |
|
2995 |
|
2996 if (columns_are_real_only) |
|
2997 { |
|
2998 for (int i = 0; i < nr; i++) |
|
2999 result (i, j) = xmin (real (a (i, j)), real (b (i, j))); |
|
3000 } |
|
3001 else |
|
3002 { |
|
3003 for (int i = 0; i < nr; i++) |
|
3004 { |
|
3005 OCTAVE_QUIT; |
|
3006 result (i, j) = xmin (a (i, j), b (i, j)); |
|
3007 } |
|
3008 } |
|
3009 } |
|
3010 |
|
3011 return result; |
|
3012 } |
|
3013 |
|
3014 ComplexMatrix |
|
3015 max (const Complex& c, const ComplexMatrix& m) |
|
3016 { |
|
3017 int nr = m.rows (); |
|
3018 int nc = m.columns (); |
|
3019 |
|
3020 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3021 |
|
3022 ComplexMatrix result (nr, nc); |
|
3023 |
|
3024 for (int j = 0; j < nc; j++) |
|
3025 for (int i = 0; i < nr; i++) |
|
3026 { |
|
3027 OCTAVE_QUIT; |
|
3028 result (i, j) = xmax (c, m (i, j)); |
|
3029 } |
|
3030 |
|
3031 return result; |
|
3032 } |
|
3033 |
|
3034 ComplexMatrix |
|
3035 max (const ComplexMatrix& m, const Complex& c) |
|
3036 { |
|
3037 int nr = m.rows (); |
|
3038 int nc = m.columns (); |
|
3039 |
|
3040 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3041 |
|
3042 ComplexMatrix result (nr, nc); |
|
3043 |
|
3044 for (int j = 0; j < nc; j++) |
|
3045 for (int i = 0; i < nr; i++) |
|
3046 { |
|
3047 OCTAVE_QUIT; |
|
3048 result (i, j) = xmax (m (i, j), c); |
|
3049 } |
|
3050 |
|
3051 return result; |
|
3052 } |
|
3053 |
|
3054 ComplexMatrix |
|
3055 max (const ComplexMatrix& a, const ComplexMatrix& b) |
|
3056 { |
|
3057 int nr = a.rows (); |
|
3058 int nc = a.columns (); |
|
3059 |
|
3060 if (nr != b.rows () || nc != b.columns ()) |
|
3061 { |
|
3062 (*current_liboctave_error_handler) |
|
3063 ("two-arg max expecting args of same size"); |
|
3064 return ComplexMatrix (); |
|
3065 } |
|
3066 |
|
3067 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3068 |
|
3069 ComplexMatrix result (nr, nc); |
|
3070 |
|
3071 for (int j = 0; j < nc; j++) |
|
3072 { |
|
3073 int columns_are_real_only = 1; |
|
3074 for (int i = 0; i < nr; i++) |
|
3075 { |
|
3076 OCTAVE_QUIT; |
|
3077 if (imag (a (i, j)) != 0.0 || imag (b (i, j)) != 0.0) |
|
3078 { |
|
3079 columns_are_real_only = 0; |
|
3080 break; |
|
3081 } |
|
3082 } |
|
3083 |
|
3084 if (columns_are_real_only) |
|
3085 { |
|
3086 for (int i = 0; i < nr; i++) |
|
3087 { |
|
3088 OCTAVE_QUIT; |
|
3089 result (i, j) = xmax (real (a (i, j)), real (b (i, j))); |
|
3090 } |
|
3091 } |
|
3092 else |
|
3093 { |
|
3094 for (int i = 0; i < nr; i++) |
|
3095 { |
|
3096 OCTAVE_QUIT; |
|
3097 result (i, j) = xmax (a (i, j), b (i, j)); |
|
3098 } |
|
3099 } |
|
3100 } |
|
3101 |
|
3102 return result; |
|
3103 } |
|
3104 |
2870
|
3105 MS_CMP_OPS(ComplexMatrix, real, Complex, real) |
3504
|
3106 MS_BOOL_OPS(ComplexMatrix, Complex, 0.0) |
2870
|
3107 |
|
3108 SM_CMP_OPS(Complex, real, ComplexMatrix, real) |
3504
|
3109 SM_BOOL_OPS(Complex, ComplexMatrix, 0.0) |
2870
|
3110 |
|
3111 MM_CMP_OPS(ComplexMatrix, real, ComplexMatrix, real) |
3504
|
3112 MM_BOOL_OPS(ComplexMatrix, ComplexMatrix, 0.0) |
2870
|
3113 |
458
|
3114 /* |
|
3115 ;;; Local Variables: *** |
|
3116 ;;; mode: C++ *** |
|
3117 ;;; End: *** |
|
3118 */ |