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