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 |
1296
|
24 #if defined (__GNUG__) |
|
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 { |
3331
|
66 int F77_FCN (zgebal, ZGEBAL) (const char*, const int&, Complex*, |
|
67 const int&, int&, int&, double*, int&, |
|
68 long, long); |
|
69 |
|
70 int F77_FCN (dgebak, DGEBAK) (const char*, const char*, const int&, |
|
71 const int&, const int&, double*, |
|
72 const int&, double*, const int&, |
|
73 int&, long, long); |
|
74 |
1253
|
75 int F77_FCN (zgemm, ZGEMM) (const char*, const char*, const int&, |
|
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 |
|
82 int F77_FCN (zgeco, ZGECO) (Complex*, const int&, const int&, int*, |
|
83 double&, Complex*); |
|
84 |
|
85 int F77_FCN (zgedi, ZGEDI) (Complex*, const int&, const int&, int*, |
|
86 Complex*, Complex*, const int&); |
|
87 |
|
88 int F77_FCN (zgesl, ZGESL) (Complex*, const int&, const int&, int*, |
|
89 Complex*, const int&); |
|
90 |
|
91 int F77_FCN (zgelss, ZGELSS) (const int&, const int&, const int&, |
|
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 |
1253
|
101 int F77_FCN (cffti, CFFTI) (const int&, Complex*); |
|
102 |
|
103 int F77_FCN (cfftf, CFFTF) (const int&, Complex*, Complex*); |
|
104 |
|
105 int F77_FCN (cfftb, CFFTB) (const int&, Complex*, Complex*); |
1819
|
106 |
|
107 int F77_FCN (zlartg, ZLARTG) (const Complex&, const Complex&, |
|
108 double&, Complex&, Complex&); |
|
109 |
|
110 int F77_FCN (ztrsyl, ZTRSYL) (const char*, const char*, const int&, |
|
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 |
3130
|
117 int F77_FCN (xzlange, XZLANGE) (const char*, const int&, |
|
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 { |
|
182 for (int i = 0; i < a.cols (); i++) |
|
183 for (int j = 0; j < a.rows (); j++) |
|
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 { |
|
190 for (int i = 0; i < a.cols (); i++) |
|
191 for (int j = 0; j < a.rows (); j++) |
|
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 { |
|
989 octave_fftw::fft (&in[npts * i], &out[npts * i], npts); |
|
990 } |
|
991 |
|
992 return retval; |
|
993 } |
|
994 |
|
995 ComplexMatrix |
|
996 ComplexMatrix::ifourier (void) const |
|
997 { |
|
998 size_t nr = rows (); |
|
999 size_t nc = cols (); |
|
1000 |
|
1001 ComplexMatrix retval (nr, nc); |
|
1002 |
|
1003 size_t npts, nsamples; |
|
1004 |
|
1005 if (nr == 1 || nc == 1) |
|
1006 { |
|
1007 npts = nr > nc ? nr : nc; |
|
1008 nsamples = 1; |
|
1009 } |
|
1010 else |
|
1011 { |
|
1012 npts = nr; |
|
1013 nsamples = nc; |
|
1014 } |
|
1015 |
|
1016 const Complex *in (data ()); |
|
1017 Complex *out (retval.fortran_vec ()); |
|
1018 |
|
1019 for (size_t i = 0; i < nsamples; i++) |
|
1020 { |
|
1021 octave_fftw::ifft (&in[npts * i], &out[npts * i], npts); |
|
1022 } |
|
1023 |
|
1024 return retval; |
|
1025 } |
|
1026 |
|
1027 ComplexMatrix |
|
1028 ComplexMatrix::fourier2d (void) const |
|
1029 { |
|
1030 int nr = rows (); |
|
1031 int nc = cols (); |
|
1032 |
|
1033 ComplexMatrix retval (*this); |
|
1034 // Note the order of passing the rows and columns to account for |
|
1035 // column-major storage. |
|
1036 octave_fftw::fft2d (retval.fortran_vec (), nc, nr); |
|
1037 |
|
1038 return retval; |
|
1039 } |
|
1040 |
|
1041 ComplexMatrix |
|
1042 ComplexMatrix::ifourier2d (void) const |
|
1043 { |
|
1044 int nr = rows (); |
|
1045 int nc = cols (); |
|
1046 |
|
1047 ComplexMatrix retval (*this); |
|
1048 // Note the order of passing the rows and columns to account for |
|
1049 // column-major storage. |
|
1050 octave_fftw::ifft2d (retval.fortran_vec (), nc, nr); |
|
1051 |
|
1052 return retval; |
|
1053 } |
|
1054 |
|
1055 #else |
|
1056 |
740
|
1057 ComplexMatrix |
458
|
1058 ComplexMatrix::fourier (void) const |
|
1059 { |
1948
|
1060 ComplexMatrix retval; |
|
1061 |
458
|
1062 int nr = rows (); |
|
1063 int nc = cols (); |
1948
|
1064 |
458
|
1065 int npts, nsamples; |
1948
|
1066 |
458
|
1067 if (nr == 1 || nc == 1) |
|
1068 { |
|
1069 npts = nr > nc ? nr : nc; |
|
1070 nsamples = 1; |
|
1071 } |
|
1072 else |
|
1073 { |
|
1074 npts = nr; |
|
1075 nsamples = nc; |
|
1076 } |
|
1077 |
|
1078 int nn = 4*npts+15; |
1948
|
1079 |
|
1080 Array<Complex> wsave (nn); |
|
1081 Complex *pwsave = wsave.fortran_vec (); |
|
1082 |
|
1083 retval = *this; |
|
1084 Complex *tmp_data = retval.fortran_vec (); |
|
1085 |
|
1086 F77_FCN (cffti, CFFTI) (npts, pwsave); |
458
|
1087 |
|
1088 for (int j = 0; j < nsamples; j++) |
1948
|
1089 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
1090 |
|
1091 return retval; |
458
|
1092 } |
|
1093 |
|
1094 ComplexMatrix |
|
1095 ComplexMatrix::ifourier (void) const |
|
1096 { |
1948
|
1097 ComplexMatrix retval; |
|
1098 |
458
|
1099 int nr = rows (); |
|
1100 int nc = cols (); |
1948
|
1101 |
458
|
1102 int npts, nsamples; |
1948
|
1103 |
458
|
1104 if (nr == 1 || nc == 1) |
|
1105 { |
|
1106 npts = nr > nc ? nr : nc; |
|
1107 nsamples = 1; |
|
1108 } |
|
1109 else |
|
1110 { |
|
1111 npts = nr; |
|
1112 nsamples = nc; |
|
1113 } |
|
1114 |
|
1115 int nn = 4*npts+15; |
1948
|
1116 |
|
1117 Array<Complex> wsave (nn); |
|
1118 Complex *pwsave = wsave.fortran_vec (); |
|
1119 |
|
1120 retval = *this; |
|
1121 Complex *tmp_data = retval.fortran_vec (); |
|
1122 |
|
1123 F77_FCN (cffti, CFFTI) (npts, pwsave); |
458
|
1124 |
|
1125 for (int j = 0; j < nsamples; j++) |
1948
|
1126 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
458
|
1127 |
1321
|
1128 for (int j = 0; j < npts*nsamples; j++) |
3572
|
1129 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
458
|
1130 |
1948
|
1131 return retval; |
458
|
1132 } |
|
1133 |
677
|
1134 ComplexMatrix |
|
1135 ComplexMatrix::fourier2d (void) const |
|
1136 { |
1948
|
1137 ComplexMatrix retval; |
|
1138 |
677
|
1139 int nr = rows (); |
|
1140 int nc = cols (); |
1948
|
1141 |
677
|
1142 int npts, nsamples; |
1948
|
1143 |
677
|
1144 if (nr == 1 || nc == 1) |
|
1145 { |
|
1146 npts = nr > nc ? nr : nc; |
|
1147 nsamples = 1; |
|
1148 } |
|
1149 else |
|
1150 { |
|
1151 npts = nr; |
|
1152 nsamples = nc; |
|
1153 } |
|
1154 |
|
1155 int nn = 4*npts+15; |
1948
|
1156 |
|
1157 Array<Complex> wsave (nn); |
|
1158 Complex *pwsave = wsave.fortran_vec (); |
|
1159 |
|
1160 retval = *this; |
|
1161 Complex *tmp_data = retval.fortran_vec (); |
|
1162 |
|
1163 F77_FCN (cffti, CFFTI) (npts, pwsave); |
677
|
1164 |
|
1165 for (int j = 0; j < nsamples; j++) |
1948
|
1166 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
677
|
1167 |
|
1168 npts = nc; |
|
1169 nsamples = nr; |
|
1170 nn = 4*npts+15; |
1948
|
1171 |
|
1172 wsave.resize (nn); |
|
1173 pwsave = wsave.fortran_vec (); |
|
1174 |
|
1175 Array<Complex> row (npts); |
|
1176 Complex *prow = row.fortran_vec (); |
|
1177 |
|
1178 F77_FCN (cffti, CFFTI) (npts, pwsave); |
677
|
1179 |
1321
|
1180 for (int j = 0; j < nsamples; j++) |
677
|
1181 { |
|
1182 for (int i = 0; i < npts; i++) |
1948
|
1183 prow[i] = tmp_data[i*nr + j]; |
|
1184 |
|
1185 F77_FCN (cfftf, CFFTF) (npts, prow, pwsave); |
677
|
1186 |
1321
|
1187 for (int i = 0; i < npts; i++) |
1948
|
1188 tmp_data[i*nr + j] = prow[i]; |
677
|
1189 } |
|
1190 |
1948
|
1191 return retval; |
677
|
1192 } |
|
1193 |
|
1194 ComplexMatrix |
|
1195 ComplexMatrix::ifourier2d (void) const |
|
1196 { |
1948
|
1197 ComplexMatrix retval; |
|
1198 |
677
|
1199 int nr = rows (); |
|
1200 int nc = cols (); |
1948
|
1201 |
677
|
1202 int npts, nsamples; |
1948
|
1203 |
677
|
1204 if (nr == 1 || nc == 1) |
|
1205 { |
|
1206 npts = nr > nc ? nr : nc; |
|
1207 nsamples = 1; |
|
1208 } |
|
1209 else |
|
1210 { |
|
1211 npts = nr; |
|
1212 nsamples = nc; |
|
1213 } |
|
1214 |
|
1215 int nn = 4*npts+15; |
1948
|
1216 |
|
1217 Array<Complex> wsave (nn); |
|
1218 Complex *pwsave = wsave.fortran_vec (); |
|
1219 |
|
1220 retval = *this; |
|
1221 Complex *tmp_data = retval.fortran_vec (); |
|
1222 |
|
1223 F77_FCN (cffti, CFFTI) (npts, pwsave); |
677
|
1224 |
|
1225 for (int j = 0; j < nsamples; j++) |
1948
|
1226 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
677
|
1227 |
1321
|
1228 for (int j = 0; j < npts*nsamples; j++) |
3572
|
1229 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
677
|
1230 |
|
1231 npts = nc; |
|
1232 nsamples = nr; |
|
1233 nn = 4*npts+15; |
1948
|
1234 |
|
1235 wsave.resize (nn); |
|
1236 pwsave = wsave.fortran_vec (); |
|
1237 |
|
1238 Array<Complex> row (npts); |
|
1239 Complex *prow = row.fortran_vec (); |
|
1240 |
|
1241 F77_FCN (cffti, CFFTI) (npts, pwsave); |
677
|
1242 |
1321
|
1243 for (int j = 0; j < nsamples; j++) |
677
|
1244 { |
|
1245 for (int i = 0; i < npts; i++) |
1948
|
1246 prow[i] = tmp_data[i*nr + j]; |
|
1247 |
|
1248 F77_FCN (cfftb, CFFTB) (npts, prow, pwsave); |
677
|
1249 |
1321
|
1250 for (int i = 0; i < npts; i++) |
3572
|
1251 tmp_data[i*nr + j] = prow[i] / static_cast<double> (npts); |
677
|
1252 } |
|
1253 |
1948
|
1254 return retval; |
677
|
1255 } |
|
1256 |
3827
|
1257 #endif |
|
1258 |
458
|
1259 ComplexDET |
|
1260 ComplexMatrix::determinant (void) const |
|
1261 { |
|
1262 int info; |
|
1263 double rcond; |
|
1264 return determinant (info, rcond); |
|
1265 } |
|
1266 |
|
1267 ComplexDET |
|
1268 ComplexMatrix::determinant (int& info) const |
|
1269 { |
|
1270 double rcond; |
|
1271 return determinant (info, rcond); |
|
1272 } |
|
1273 |
|
1274 ComplexDET |
532
|
1275 ComplexMatrix::determinant (int& info, double& rcond) const |
458
|
1276 { |
|
1277 ComplexDET retval; |
|
1278 |
|
1279 int nr = rows (); |
|
1280 int nc = cols (); |
|
1281 |
|
1282 if (nr == 0 || nc == 0) |
|
1283 { |
|
1284 Complex d[2]; |
|
1285 d[0] = 1.0; |
|
1286 d[1] = 0.0; |
|
1287 retval = ComplexDET (d); |
|
1288 } |
|
1289 else |
|
1290 { |
|
1291 info = 0; |
1948
|
1292 |
|
1293 Array<int> ipvt (nr); |
|
1294 int *pipvt = ipvt.fortran_vec (); |
|
1295 |
|
1296 Array<Complex> z (nr); |
|
1297 Complex *pz = z.fortran_vec (); |
|
1298 |
|
1299 ComplexMatrix atmp = *this; |
|
1300 Complex *tmp_data = atmp.fortran_vec (); |
|
1301 |
|
1302 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nr, pipvt, rcond, pz)); |
|
1303 |
|
1304 if (f77_exception_encountered) |
|
1305 (*current_liboctave_error_handler) ("unrecoverable error in zgeco"); |
458
|
1306 else |
|
1307 { |
1948
|
1308 volatile double rcond_plus_one = rcond + 1.0; |
|
1309 |
3838
|
1310 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
1948
|
1311 { |
|
1312 info = -1; |
|
1313 retval = ComplexDET (); |
|
1314 } |
|
1315 else |
|
1316 { |
|
1317 Complex d[2]; |
|
1318 |
|
1319 F77_XFCN (zgedi, ZGEDI, (tmp_data, nr, nr, pipvt, d, pz, 10)); |
|
1320 |
|
1321 if (f77_exception_encountered) |
|
1322 (*current_liboctave_error_handler) |
|
1323 ("unrecoverable error in dgedi"); |
|
1324 else |
|
1325 retval = ComplexDET (d); |
|
1326 } |
458
|
1327 } |
|
1328 } |
|
1329 |
|
1330 return retval; |
|
1331 } |
|
1332 |
|
1333 ComplexMatrix |
|
1334 ComplexMatrix::solve (const Matrix& b) const |
|
1335 { |
|
1336 int info; |
|
1337 double rcond; |
3480
|
1338 return solve (b, info, rcond, 0); |
458
|
1339 } |
|
1340 |
|
1341 ComplexMatrix |
|
1342 ComplexMatrix::solve (const Matrix& b, int& info) const |
|
1343 { |
|
1344 double rcond; |
3480
|
1345 return solve (b, info, rcond, 0); |
458
|
1346 } |
|
1347 |
|
1348 ComplexMatrix |
|
1349 ComplexMatrix::solve (const Matrix& b, int& info, double& rcond) const |
|
1350 { |
3480
|
1351 return solve (b, info, rcond, 0); |
|
1352 } |
|
1353 |
|
1354 ComplexMatrix |
|
1355 ComplexMatrix::solve (const Matrix& b, int& info, double& rcond, |
|
1356 solve_singularity_handler sing_handler) const |
|
1357 { |
458
|
1358 ComplexMatrix tmp (b); |
3480
|
1359 return solve (tmp, info, rcond, sing_handler); |
458
|
1360 } |
|
1361 |
|
1362 ComplexMatrix |
|
1363 ComplexMatrix::solve (const ComplexMatrix& b) const |
|
1364 { |
|
1365 int info; |
|
1366 double rcond; |
3480
|
1367 return solve (b, info, rcond, 0); |
458
|
1368 } |
|
1369 |
|
1370 ComplexMatrix |
|
1371 ComplexMatrix::solve (const ComplexMatrix& b, int& info) const |
|
1372 { |
|
1373 double rcond; |
3480
|
1374 return solve (b, info, rcond, 0); |
458
|
1375 } |
3480
|
1376 |
458
|
1377 ComplexMatrix |
532
|
1378 ComplexMatrix::solve (const ComplexMatrix& b, int& info, double& rcond) const |
458
|
1379 { |
3480
|
1380 return solve (b, info, rcond, 0); |
|
1381 } |
|
1382 |
|
1383 ComplexMatrix |
|
1384 ComplexMatrix::solve (const ComplexMatrix& b, int& info, double& rcond, |
|
1385 solve_singularity_handler sing_handler) const |
|
1386 { |
458
|
1387 ComplexMatrix retval; |
|
1388 |
|
1389 int nr = rows (); |
|
1390 int nc = cols (); |
1948
|
1391 |
|
1392 if (nr == 0 || nc == 0 || nr != nc || nr != b.rows ()) |
|
1393 (*current_liboctave_error_handler) |
|
1394 ("matrix dimension mismatch in solution of linear equations"); |
458
|
1395 else |
|
1396 { |
1948
|
1397 info = 0; |
|
1398 |
|
1399 Array<int> ipvt (nr); |
|
1400 int *pipvt = ipvt.fortran_vec (); |
|
1401 |
|
1402 Array<Complex> z (nr); |
|
1403 Complex *pz = z.fortran_vec (); |
|
1404 |
|
1405 ComplexMatrix atmp = *this; |
|
1406 Complex *tmp_data = atmp.fortran_vec (); |
|
1407 |
|
1408 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nr, pipvt, rcond, pz)); |
|
1409 |
|
1410 if (f77_exception_encountered) |
|
1411 (*current_liboctave_error_handler) ("unrecoverable error in zgeco"); |
|
1412 else |
|
1413 { |
|
1414 volatile double rcond_plus_one = rcond + 1.0; |
|
1415 |
3838
|
1416 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
1948
|
1417 { |
|
1418 info = -2; |
3480
|
1419 |
|
1420 if (sing_handler) |
|
1421 sing_handler (rcond); |
|
1422 else |
|
1423 (*current_liboctave_error_handler) |
|
1424 ("matrix singular to machine precision, rcond = %g", |
|
1425 rcond); |
1948
|
1426 } |
|
1427 else |
|
1428 { |
|
1429 retval = b; |
|
1430 Complex *result = retval.fortran_vec (); |
|
1431 |
|
1432 int b_nc = b.cols (); |
|
1433 |
|
1434 for (volatile int j = 0; j < b_nc; j++) |
|
1435 { |
|
1436 F77_XFCN (zgesl, ZGESL, (tmp_data, nr, nr, pipvt, |
|
1437 &result[nr*j], 0)); |
|
1438 |
|
1439 if (f77_exception_encountered) |
|
1440 { |
|
1441 (*current_liboctave_error_handler) |
|
1442 ("unrecoverable error in dgesl"); |
|
1443 |
|
1444 break; |
|
1445 } |
|
1446 } |
|
1447 } |
|
1448 } |
458
|
1449 } |
|
1450 |
|
1451 return retval; |
|
1452 } |
|
1453 |
|
1454 ComplexColumnVector |
3585
|
1455 ComplexMatrix::solve (const ColumnVector& b) const |
|
1456 { |
|
1457 int info; |
|
1458 double rcond; |
|
1459 return solve (ComplexColumnVector (b), info, rcond, 0); |
|
1460 } |
|
1461 |
|
1462 ComplexColumnVector |
|
1463 ComplexMatrix::solve (const ColumnVector& b, int& info) const |
|
1464 { |
|
1465 double rcond; |
|
1466 return solve (ComplexColumnVector (b), info, rcond, 0); |
|
1467 } |
|
1468 |
|
1469 ComplexColumnVector |
|
1470 ComplexMatrix::solve (const ColumnVector& b, int& info, double& rcond) const |
|
1471 { |
|
1472 return solve (ComplexColumnVector (b), info, rcond, 0); |
|
1473 } |
|
1474 |
|
1475 ComplexColumnVector |
|
1476 ComplexMatrix::solve (const ColumnVector& b, int& info, double& rcond, |
|
1477 solve_singularity_handler sing_handler) const |
|
1478 { |
|
1479 return solve (ComplexColumnVector (b), info, rcond, sing_handler); |
|
1480 } |
|
1481 |
|
1482 ComplexColumnVector |
458
|
1483 ComplexMatrix::solve (const ComplexColumnVector& b) const |
|
1484 { |
|
1485 int info; |
|
1486 double rcond; |
3480
|
1487 return solve (b, info, rcond, 0); |
458
|
1488 } |
|
1489 |
|
1490 ComplexColumnVector |
|
1491 ComplexMatrix::solve (const ComplexColumnVector& b, int& info) const |
|
1492 { |
|
1493 double rcond; |
3480
|
1494 return solve (b, info, rcond, 0); |
458
|
1495 } |
|
1496 |
|
1497 ComplexColumnVector |
|
1498 ComplexMatrix::solve (const ComplexColumnVector& b, int& info, |
532
|
1499 double& rcond) const |
458
|
1500 { |
3480
|
1501 return solve (b, info, rcond, 0); |
|
1502 } |
|
1503 |
|
1504 ComplexColumnVector |
|
1505 ComplexMatrix::solve (const ComplexColumnVector& b, int& info, |
|
1506 double& rcond, |
|
1507 solve_singularity_handler sing_handler) const |
|
1508 { |
458
|
1509 ComplexColumnVector retval; |
|
1510 |
|
1511 int nr = rows (); |
|
1512 int nc = cols (); |
1948
|
1513 |
|
1514 if (nr == 0 || nc == 0 || nr != nc || nr != b.length ()) |
|
1515 (*current_liboctave_error_handler) |
|
1516 ("matrix dimension mismatch in solution of linear equations"); |
458
|
1517 else |
|
1518 { |
1948
|
1519 info = 0; |
|
1520 |
|
1521 Array<int> ipvt (nr); |
|
1522 int *pipvt = ipvt.fortran_vec (); |
|
1523 |
|
1524 Array<Complex> z (nr); |
|
1525 Complex *pz = z.fortran_vec (); |
|
1526 |
|
1527 ComplexMatrix atmp = *this; |
|
1528 Complex *tmp_data = atmp.fortran_vec (); |
|
1529 |
|
1530 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nr, pipvt, rcond, pz)); |
|
1531 |
|
1532 if (f77_exception_encountered) |
|
1533 (*current_liboctave_error_handler) |
3838
|
1534 ("unrecoverable error in zgeco"); |
1948
|
1535 else |
|
1536 { |
|
1537 volatile double rcond_plus_one = rcond + 1.0; |
|
1538 |
3838
|
1539 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
1948
|
1540 { |
|
1541 info = -2; |
3480
|
1542 |
|
1543 if (sing_handler) |
|
1544 sing_handler (rcond); |
|
1545 else |
|
1546 (*current_liboctave_error_handler) |
|
1547 ("matrix singular to machine precision, rcond = %g", |
|
1548 rcond); |
1948
|
1549 } |
|
1550 else |
|
1551 { |
|
1552 retval = b; |
|
1553 Complex *result = retval.fortran_vec (); |
|
1554 |
|
1555 F77_XFCN (zgesl, ZGESL, (tmp_data, nr, nr, pipvt, result, 0)); |
|
1556 |
|
1557 if (f77_exception_encountered) |
|
1558 (*current_liboctave_error_handler) |
|
1559 ("unrecoverable error in dgesl"); |
|
1560 } |
|
1561 } |
458
|
1562 } |
|
1563 |
|
1564 return retval; |
|
1565 } |
|
1566 |
|
1567 ComplexMatrix |
3585
|
1568 ComplexMatrix::lssolve (const Matrix& b) const |
|
1569 { |
|
1570 int info; |
|
1571 int rank; |
|
1572 return lssolve (ComplexMatrix (b), info, rank); |
|
1573 } |
|
1574 |
|
1575 ComplexMatrix |
|
1576 ComplexMatrix::lssolve (const Matrix& b, int& info) const |
|
1577 { |
|
1578 int rank; |
|
1579 return lssolve (ComplexMatrix (b), info, rank); |
|
1580 } |
|
1581 |
|
1582 ComplexMatrix |
|
1583 ComplexMatrix::lssolve (const Matrix& b, int& info, int& rank) const |
|
1584 { |
|
1585 return lssolve (ComplexMatrix (b), info, rank); |
|
1586 } |
|
1587 |
|
1588 ComplexMatrix |
458
|
1589 ComplexMatrix::lssolve (const ComplexMatrix& b) const |
|
1590 { |
|
1591 int info; |
|
1592 int rank; |
|
1593 return lssolve (b, info, rank); |
|
1594 } |
|
1595 |
|
1596 ComplexMatrix |
|
1597 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info) const |
|
1598 { |
|
1599 int rank; |
|
1600 return lssolve (b, info, rank); |
|
1601 } |
|
1602 |
|
1603 ComplexMatrix |
|
1604 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info, int& rank) const |
|
1605 { |
1948
|
1606 ComplexMatrix retval; |
|
1607 |
458
|
1608 int nrhs = b.cols (); |
|
1609 |
|
1610 int m = rows (); |
|
1611 int n = cols (); |
|
1612 |
|
1613 if (m == 0 || n == 0 || m != b.rows ()) |
1948
|
1614 (*current_liboctave_error_handler) |
|
1615 ("matrix dimension mismatch solution of linear equations"); |
|
1616 else |
458
|
1617 { |
1948
|
1618 ComplexMatrix atmp = *this; |
|
1619 Complex *tmp_data = atmp.fortran_vec (); |
|
1620 |
|
1621 int nrr = m > n ? m : n; |
|
1622 ComplexMatrix result (nrr, nrhs); |
|
1623 |
|
1624 for (int j = 0; j < nrhs; j++) |
|
1625 for (int i = 0; i < m; i++) |
|
1626 result.elem (i, j) = b.elem (i, j); |
|
1627 |
|
1628 Complex *presult = result.fortran_vec (); |
|
1629 |
|
1630 int len_s = m < n ? m : n; |
|
1631 Array<double> s (len_s); |
|
1632 double *ps = s.fortran_vec (); |
2563
|
1633 |
1948
|
1634 double rcond = -1.0; |
2563
|
1635 |
1948
|
1636 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1637 lrwork = lrwork > 1 ? lrwork : 1; |
|
1638 Array<double> rwork (lrwork); |
|
1639 double *prwork = rwork.fortran_vec (); |
|
1640 |
3752
|
1641 // Ask ZGELSS what the dimension of WORK should be. |
|
1642 |
|
1643 int lwork = -1; |
|
1644 |
|
1645 Array<Complex> work (1); |
|
1646 |
1948
|
1647 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
3752
|
1648 nrr, ps, rcond, rank, |
|
1649 work.fortran_vec (), lwork, prwork, |
|
1650 info)); |
1948
|
1651 |
|
1652 if (f77_exception_encountered) |
|
1653 (*current_liboctave_error_handler) ("unrecoverable error in zgelss"); |
|
1654 else |
|
1655 { |
3752
|
1656 lwork = static_cast<int> (real (work(0))); |
|
1657 work.resize (lwork); |
|
1658 |
|
1659 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
1660 nrr, ps, rcond, rank, |
|
1661 work.fortran_vec (), lwork, |
|
1662 prwork, info)); |
|
1663 |
|
1664 if (f77_exception_encountered) |
|
1665 (*current_liboctave_error_handler) |
|
1666 ("unrecoverable error in zgelss"); |
|
1667 else |
|
1668 { |
|
1669 retval.resize (n, nrhs); |
|
1670 for (int j = 0; j < nrhs; j++) |
|
1671 for (int i = 0; i < n; i++) |
|
1672 retval.elem (i, j) = result.elem (i, j); |
|
1673 } |
1948
|
1674 } |
458
|
1675 } |
|
1676 |
|
1677 return retval; |
|
1678 } |
|
1679 |
|
1680 ComplexColumnVector |
3585
|
1681 ComplexMatrix::lssolve (const ColumnVector& b) const |
|
1682 { |
|
1683 int info; |
|
1684 int rank; |
|
1685 return lssolve (ComplexColumnVector (b), info, rank); |
|
1686 } |
|
1687 |
|
1688 ComplexColumnVector |
|
1689 ComplexMatrix::lssolve (const ColumnVector& b, int& info) const |
|
1690 { |
|
1691 int rank; |
|
1692 return lssolve (ComplexColumnVector (b), info, rank); |
|
1693 } |
|
1694 |
|
1695 ComplexColumnVector |
|
1696 ComplexMatrix::lssolve (const ColumnVector& b, int& info, int& rank) const |
|
1697 { |
|
1698 return lssolve (ComplexColumnVector (b), info, rank); |
|
1699 } |
|
1700 |
|
1701 ComplexColumnVector |
458
|
1702 ComplexMatrix::lssolve (const ComplexColumnVector& b) const |
|
1703 { |
|
1704 int info; |
|
1705 int rank; |
|
1706 return lssolve (b, info, rank); |
|
1707 } |
|
1708 |
|
1709 ComplexColumnVector |
|
1710 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info) const |
|
1711 { |
|
1712 int rank; |
|
1713 return lssolve (b, info, rank); |
|
1714 } |
|
1715 |
|
1716 ComplexColumnVector |
|
1717 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info, |
|
1718 int& rank) const |
|
1719 { |
1948
|
1720 ComplexColumnVector retval; |
|
1721 |
458
|
1722 int nrhs = 1; |
|
1723 |
|
1724 int m = rows (); |
|
1725 int n = cols (); |
|
1726 |
|
1727 if (m == 0 || n == 0 || m != b.length ()) |
1948
|
1728 (*current_liboctave_error_handler) |
|
1729 ("matrix dimension mismatch solution of least squares problem"); |
|
1730 else |
458
|
1731 { |
1948
|
1732 ComplexMatrix atmp = *this; |
|
1733 Complex *tmp_data = atmp.fortran_vec (); |
|
1734 |
|
1735 int nrr = m > n ? m : n; |
|
1736 ComplexColumnVector result (nrr); |
|
1737 |
|
1738 for (int i = 0; i < m; i++) |
|
1739 result.elem (i) = b.elem (i); |
|
1740 |
|
1741 Complex *presult = result.fortran_vec (); |
|
1742 |
|
1743 int len_s = m < n ? m : n; |
|
1744 Array<double> s (len_s); |
|
1745 double *ps = s.fortran_vec (); |
|
1746 |
|
1747 double rcond = -1.0; |
|
1748 |
|
1749 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1750 lrwork = lrwork > 1 ? lrwork : 1; |
|
1751 Array<double> rwork (lrwork); |
|
1752 double *prwork = rwork.fortran_vec (); |
|
1753 |
3752
|
1754 // Ask ZGELSS what the dimension of WORK should be. |
|
1755 |
|
1756 int lwork = -1; |
|
1757 |
|
1758 Array<Complex> work (1); |
|
1759 |
1948
|
1760 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
3752
|
1761 nrr, ps, rcond, rank, |
|
1762 work.fortran_vec (), lwork, prwork, |
|
1763 info)); |
1948
|
1764 |
|
1765 if (f77_exception_encountered) |
|
1766 (*current_liboctave_error_handler) ("unrecoverable error in zgelss"); |
|
1767 else |
|
1768 { |
3752
|
1769 lwork = static_cast<int> (real (work(0))); |
|
1770 work.resize (lwork); |
|
1771 |
|
1772 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
1773 nrr, ps, rcond, rank, |
|
1774 work.fortran_vec (), lwork, |
|
1775 prwork, info)); |
|
1776 |
|
1777 if (f77_exception_encountered) |
|
1778 (*current_liboctave_error_handler) |
|
1779 ("unrecoverable error in zgelss"); |
|
1780 else |
|
1781 { |
|
1782 retval.resize (n); |
|
1783 for (int i = 0; i < n; i++) |
|
1784 retval.elem (i) = result.elem (i); |
|
1785 } |
1948
|
1786 } |
458
|
1787 } |
|
1788 |
|
1789 return retval; |
|
1790 } |
|
1791 |
1819
|
1792 // Constants for matrix exponential calculation. |
|
1793 |
|
1794 static double padec [] = |
|
1795 { |
|
1796 5.0000000000000000e-1, |
|
1797 1.1666666666666667e-1, |
|
1798 1.6666666666666667e-2, |
|
1799 1.6025641025641026e-3, |
|
1800 1.0683760683760684e-4, |
|
1801 4.8562548562548563e-6, |
|
1802 1.3875013875013875e-7, |
|
1803 1.9270852604185938e-9, |
|
1804 }; |
|
1805 |
|
1806 ComplexMatrix |
|
1807 ComplexMatrix::expm (void) const |
|
1808 { |
|
1809 ComplexMatrix retval; |
|
1810 |
|
1811 ComplexMatrix m = *this; |
|
1812 |
|
1813 int nc = columns (); |
|
1814 |
3130
|
1815 // Preconditioning step 1: trace normalization to reduce dynamic |
|
1816 // range of poles, but avoid making stable eigenvalues unstable. |
|
1817 |
1819
|
1818 // trace shift value |
|
1819 Complex trshift = 0.0; |
|
1820 |
|
1821 for (int i = 0; i < nc; i++) |
|
1822 trshift += m.elem (i, i); |
|
1823 |
|
1824 trshift /= nc; |
|
1825 |
3130
|
1826 if (trshift.real () < 0.0) |
|
1827 trshift = trshift.imag (); |
|
1828 |
1819
|
1829 for (int i = 0; i < nc; i++) |
|
1830 m.elem (i, i) -= trshift; |
|
1831 |
|
1832 // Preconditioning step 2: eigenvalue balancing. |
3331
|
1833 // code follows development in AEPBAL |
|
1834 |
|
1835 Complex *mp = m.fortran_vec (); |
3467
|
1836 |
|
1837 int info, ilo, ihi,ilos,ihis; |
3468
|
1838 Array<double> dpermute (nc); |
|
1839 Array<double> dscale (nc); |
|
1840 |
|
1841 // XXX FIXME XXX -- should pass job as a parameter in expm |
|
1842 |
|
1843 // Permute first |
|
1844 char job = 'P'; |
3467
|
1845 F77_XFCN (zgebal, ZGEBAL, (&job, nc, mp, nc, ilo, ihi, |
3469
|
1846 dpermute.fortran_vec (), info, 1L, 1L)); |
3331
|
1847 |
|
1848 if (f77_exception_encountered) |
|
1849 { |
|
1850 (*current_liboctave_error_handler) ("unrecoverable error in zgebal"); |
|
1851 return retval; |
|
1852 } |
|
1853 |
3468
|
1854 // then scale |
|
1855 job = 'S'; |
3467
|
1856 F77_XFCN (zgebal, ZGEBAL, (&job, nc, mp, nc, ilos, ihis, |
3469
|
1857 dscale.fortran_vec (), info, 1L, 1L)); |
3331
|
1858 |
|
1859 if (f77_exception_encountered) |
|
1860 { |
3467
|
1861 (*current_liboctave_error_handler) ("unrecoverable error in zgebal"); |
3331
|
1862 return retval; |
|
1863 } |
1819
|
1864 |
|
1865 // Preconditioning step 3: scaling. |
|
1866 |
|
1867 ColumnVector work (nc); |
3130
|
1868 double inf_norm; |
|
1869 |
3331
|
1870 F77_XFCN (xzlange, XZLANGE, ("I", nc, nc, m.fortran_vec (), nc, |
|
1871 work.fortran_vec (), inf_norm)); |
|
1872 |
|
1873 if (f77_exception_encountered) |
|
1874 { |
|
1875 (*current_liboctave_error_handler) ("unrecoverable error in zlange"); |
|
1876 return retval; |
|
1877 } |
1819
|
1878 |
2800
|
1879 int sqpow = (inf_norm > 0.0 |
|
1880 ? static_cast<int> (1.0 + log (inf_norm) / log (2.0)) : 0); |
1819
|
1881 |
|
1882 // Check whether we need to square at all. |
|
1883 |
|
1884 if (sqpow < 0) |
|
1885 sqpow = 0; |
|
1886 |
|
1887 if (sqpow > 0) |
|
1888 { |
|
1889 double scale_factor = 1.0; |
|
1890 for (int i = 0; i < sqpow; i++) |
|
1891 scale_factor *= 2.0; |
|
1892 |
|
1893 m = m / scale_factor; |
|
1894 } |
|
1895 |
|
1896 // npp, dpp: pade' approx polynomial matrices. |
|
1897 |
|
1898 ComplexMatrix npp (nc, nc, 0.0); |
|
1899 ComplexMatrix dpp = npp; |
|
1900 |
|
1901 // Now powers a^8 ... a^1. |
|
1902 |
|
1903 int minus_one_j = -1; |
|
1904 for (int j = 7; j >= 0; j--) |
|
1905 { |
|
1906 npp = m * npp + m * padec[j]; |
|
1907 dpp = m * dpp + m * (minus_one_j * padec[j]); |
|
1908 minus_one_j *= -1; |
|
1909 } |
|
1910 |
|
1911 // Zero power. |
|
1912 |
|
1913 dpp = -dpp; |
|
1914 for (int j = 0; j < nc; j++) |
|
1915 { |
|
1916 npp.elem (j, j) += 1.0; |
|
1917 dpp.elem (j, j) += 1.0; |
|
1918 } |
|
1919 |
|
1920 // Compute pade approximation = inverse (dpp) * npp. |
|
1921 |
|
1922 retval = dpp.solve (npp); |
|
1923 |
|
1924 // Reverse preconditioning step 3: repeated squaring. |
|
1925 |
|
1926 while (sqpow) |
|
1927 { |
|
1928 retval = retval * retval; |
|
1929 sqpow--; |
|
1930 } |
|
1931 |
|
1932 // Reverse preconditioning step 2: inverse balancing. |
3467
|
1933 // Done in two steps: inverse scaling, then inverse permutation |
|
1934 |
|
1935 // inverse scaling (diagonal transformation) |
3468
|
1936 for (int i = 0; i < nc; i++) |
|
1937 for (int j = 0; j < nc; j++) |
|
1938 retval(i,j) *= dscale(i) / dscale(j); |
3467
|
1939 |
|
1940 // construct balancing permutation vector |
3468
|
1941 Array<int> ipermute (nc); |
|
1942 for (int i = 0; i < nc; i++) |
|
1943 ipermute(i) = i; // initialize to identity permutation |
3467
|
1944 |
|
1945 // leading permutations in forward order |
3468
|
1946 for (int i = 0; i < (ilo-1); i++) |
|
1947 { |
|
1948 int swapidx = static_cast<int> (dpermute(i)) - 1; |
|
1949 int tmp = ipermute(i); |
|
1950 ipermute(i) = ipermute(swapidx); |
|
1951 ipermute(swapidx) = tmp; |
|
1952 } |
3467
|
1953 |
|
1954 // trailing permutations must be done in reverse order |
3468
|
1955 for (int i = nc - 1; i >= ihi; i--) |
|
1956 { |
|
1957 int swapidx = static_cast<int> (dpermute(i)) - 1; |
|
1958 int tmp = ipermute(i); |
|
1959 ipermute(i) = ipermute(swapidx); |
|
1960 ipermute(swapidx) = tmp; |
|
1961 } |
3467
|
1962 |
|
1963 // construct inverse balancing permutation vector |
3468
|
1964 Array<int> invpvec (nc); |
|
1965 for (int i = 0; i < nc; i++) |
|
1966 invpvec(ipermute(i)) = i; // Thanks to R. A. Lippert for this method |
3467
|
1967 |
|
1968 ComplexMatrix tmpMat = retval; |
3468
|
1969 for (int i = 0; i < nc; i++) |
|
1970 for (int j = 0; j < nc; j++) |
|
1971 retval(i,j) = tmpMat(invpvec(i),invpvec(j)); |
1819
|
1972 |
|
1973 // Reverse preconditioning step 1: fix trace normalization. |
|
1974 |
3130
|
1975 return exp (trshift) * retval; |
1819
|
1976 } |
|
1977 |
1205
|
1978 // column vector by row vector -> matrix operations |
|
1979 |
|
1980 ComplexMatrix |
|
1981 operator * (const ColumnVector& v, const ComplexRowVector& a) |
|
1982 { |
|
1983 ComplexColumnVector tmp (v); |
|
1984 return tmp * a; |
|
1985 } |
|
1986 |
|
1987 ComplexMatrix |
|
1988 operator * (const ComplexColumnVector& a, const RowVector& b) |
|
1989 { |
|
1990 ComplexRowVector tmp (b); |
|
1991 return a * tmp; |
|
1992 } |
|
1993 |
|
1994 ComplexMatrix |
|
1995 operator * (const ComplexColumnVector& v, const ComplexRowVector& a) |
|
1996 { |
1948
|
1997 ComplexMatrix retval; |
|
1998 |
1205
|
1999 int len = v.length (); |
3233
|
2000 |
|
2001 if (len != 0) |
1205
|
2002 { |
3233
|
2003 int a_len = a.length (); |
|
2004 |
|
2005 retval.resize (len, a_len); |
|
2006 Complex *c = retval.fortran_vec (); |
|
2007 |
|
2008 F77_XFCN (zgemm, ZGEMM, ("N", "N", len, a_len, 1, 1.0, |
|
2009 v.data (), len, a.data (), 1, 0.0, |
|
2010 c, len, 1L, 1L)); |
|
2011 |
|
2012 if (f77_exception_encountered) |
|
2013 (*current_liboctave_error_handler) |
|
2014 ("unrecoverable error in zgemm"); |
1205
|
2015 } |
|
2016 |
1948
|
2017 return retval; |
1205
|
2018 } |
|
2019 |
458
|
2020 // matrix by diagonal matrix -> matrix operations |
|
2021 |
|
2022 ComplexMatrix& |
|
2023 ComplexMatrix::operator += (const DiagMatrix& a) |
|
2024 { |
|
2025 int nr = rows (); |
|
2026 int nc = cols (); |
2384
|
2027 |
|
2028 int a_nr = rows (); |
|
2029 int a_nc = cols (); |
|
2030 |
|
2031 if (nr != a_nr || nc != a_nc) |
458
|
2032 { |
2384
|
2033 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889
|
2034 return *this; |
458
|
2035 } |
|
2036 |
|
2037 for (int i = 0; i < a.length (); i++) |
|
2038 elem (i, i) += a.elem (i, i); |
|
2039 |
|
2040 return *this; |
|
2041 } |
|
2042 |
|
2043 ComplexMatrix& |
|
2044 ComplexMatrix::operator -= (const DiagMatrix& a) |
|
2045 { |
|
2046 int nr = rows (); |
|
2047 int nc = cols (); |
2384
|
2048 |
|
2049 int a_nr = rows (); |
|
2050 int a_nc = cols (); |
|
2051 |
|
2052 if (nr != a_nr || nc != a_nc) |
458
|
2053 { |
2384
|
2054 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889
|
2055 return *this; |
458
|
2056 } |
|
2057 |
|
2058 for (int i = 0; i < a.length (); i++) |
|
2059 elem (i, i) -= a.elem (i, i); |
|
2060 |
|
2061 return *this; |
|
2062 } |
|
2063 |
|
2064 ComplexMatrix& |
|
2065 ComplexMatrix::operator += (const ComplexDiagMatrix& a) |
|
2066 { |
|
2067 int nr = rows (); |
|
2068 int nc = cols (); |
2384
|
2069 |
|
2070 int a_nr = rows (); |
|
2071 int a_nc = cols (); |
|
2072 |
|
2073 if (nr != a_nr || nc != a_nc) |
458
|
2074 { |
2384
|
2075 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889
|
2076 return *this; |
458
|
2077 } |
|
2078 |
|
2079 for (int i = 0; i < a.length (); i++) |
|
2080 elem (i, i) += a.elem (i, i); |
|
2081 |
|
2082 return *this; |
|
2083 } |
|
2084 |
|
2085 ComplexMatrix& |
|
2086 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) |
|
2087 { |
|
2088 int nr = rows (); |
|
2089 int nc = cols (); |
2384
|
2090 |
|
2091 int a_nr = rows (); |
|
2092 int a_nc = cols (); |
|
2093 |
|
2094 if (nr != a_nr || nc != a_nc) |
458
|
2095 { |
2384
|
2096 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889
|
2097 return *this; |
458
|
2098 } |
|
2099 |
|
2100 for (int i = 0; i < a.length (); i++) |
|
2101 elem (i, i) -= a.elem (i, i); |
|
2102 |
|
2103 return *this; |
|
2104 } |
|
2105 |
|
2106 // matrix by matrix -> matrix operations |
|
2107 |
|
2108 ComplexMatrix& |
|
2109 ComplexMatrix::operator += (const Matrix& a) |
|
2110 { |
|
2111 int nr = rows (); |
|
2112 int nc = cols (); |
2384
|
2113 |
|
2114 int a_nr = a.rows (); |
|
2115 int a_nc = a.cols (); |
|
2116 |
|
2117 if (nr != a_nr || nc != a_nc) |
458
|
2118 { |
2384
|
2119 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
2120 return *this; |
|
2121 } |
|
2122 |
|
2123 if (nr == 0 || nc == 0) |
|
2124 return *this; |
|
2125 |
|
2126 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2127 |
3769
|
2128 mx_inline_add2 (d, a.data (), length ()); |
458
|
2129 return *this; |
|
2130 } |
|
2131 |
|
2132 ComplexMatrix& |
|
2133 ComplexMatrix::operator -= (const Matrix& a) |
|
2134 { |
|
2135 int nr = rows (); |
|
2136 int nc = cols (); |
2384
|
2137 |
|
2138 int a_nr = a.rows (); |
|
2139 int a_nc = a.cols (); |
|
2140 |
|
2141 if (nr != a_nr || nc != a_nc) |
458
|
2142 { |
2384
|
2143 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
2144 return *this; |
|
2145 } |
|
2146 |
|
2147 if (nr == 0 || nc == 0) |
|
2148 return *this; |
|
2149 |
|
2150 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2151 |
3769
|
2152 mx_inline_subtract2 (d, a.data (), length ()); |
458
|
2153 return *this; |
|
2154 } |
|
2155 |
|
2156 // unary operations |
|
2157 |
2964
|
2158 boolMatrix |
458
|
2159 ComplexMatrix::operator ! (void) const |
|
2160 { |
2964
|
2161 int nr = rows (); |
|
2162 int nc = cols (); |
|
2163 |
|
2164 boolMatrix b (nr, nc); |
|
2165 |
|
2166 for (int j = 0; j < nc; j++) |
|
2167 for (int i = 0; i < nr; i++) |
|
2168 b.elem (i, j) = elem (i, j) != 0.0; |
|
2169 |
|
2170 return b; |
458
|
2171 } |
|
2172 |
|
2173 // other operations |
|
2174 |
|
2175 ComplexMatrix |
2676
|
2176 ComplexMatrix::map (c_c_Mapper f) const |
458
|
2177 { |
2676
|
2178 ComplexMatrix b (*this); |
|
2179 return b.apply (f); |
458
|
2180 } |
|
2181 |
2676
|
2182 Matrix |
|
2183 ComplexMatrix::map (d_c_Mapper f) const |
458
|
2184 { |
3248
|
2185 int nr = rows (); |
|
2186 int nc = cols (); |
|
2187 |
|
2188 Matrix retval (nr, nc); |
|
2189 |
|
2190 for (int j = 0; j < nc; j++) |
|
2191 for (int i = 0; i < nr; i++) |
|
2192 retval(i,j) = f (elem(i,j)); |
|
2193 |
|
2194 return retval; |
|
2195 } |
|
2196 |
|
2197 boolMatrix |
|
2198 ComplexMatrix::map (b_c_Mapper f) const |
|
2199 { |
|
2200 int nr = rows (); |
|
2201 int nc = cols (); |
|
2202 |
|
2203 boolMatrix retval (nr, nc); |
|
2204 |
|
2205 for (int j = 0; j < nc; j++) |
|
2206 for (int i = 0; i < nr; i++) |
|
2207 retval(i,j) = f (elem(i,j)); |
2676
|
2208 |
|
2209 return retval; |
|
2210 } |
|
2211 |
|
2212 ComplexMatrix& |
|
2213 ComplexMatrix::apply (c_c_Mapper f) |
|
2214 { |
|
2215 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2216 |
|
2217 for (int i = 0; i < length (); i++) |
|
2218 d[i] = f (d[i]); |
|
2219 |
|
2220 return *this; |
458
|
2221 } |
|
2222 |
2384
|
2223 bool |
|
2224 ComplexMatrix::any_element_is_inf_or_nan (void) const |
|
2225 { |
|
2226 int nr = rows (); |
|
2227 int nc = cols (); |
|
2228 |
|
2229 for (int j = 0; j < nc; j++) |
|
2230 for (int i = 0; i < nr; i++) |
|
2231 { |
|
2232 Complex val = elem (i, j); |
|
2233 if (xisinf (val) || xisnan (val)) |
|
2234 return true; |
|
2235 } |
|
2236 |
|
2237 return false; |
|
2238 } |
|
2239 |
2408
|
2240 // Return true if no elements have imaginary components. |
|
2241 |
|
2242 bool |
|
2243 ComplexMatrix::all_elements_are_real (void) const |
|
2244 { |
|
2245 int nr = rows (); |
|
2246 int nc = cols (); |
|
2247 |
|
2248 for (int j = 0; j < nc; j++) |
|
2249 for (int i = 0; i < nr; i++) |
|
2250 if (imag (elem (i, j)) != 0.0) |
|
2251 return false; |
|
2252 |
|
2253 return true; |
|
2254 } |
|
2255 |
1968
|
2256 // Return nonzero if any element of CM has a non-integer real or |
|
2257 // imaginary part. Also extract the largest and smallest (real or |
|
2258 // imaginary) values and return them in MAX_VAL and MIN_VAL. |
|
2259 |
2384
|
2260 bool |
1968
|
2261 ComplexMatrix::all_integers (double& max_val, double& min_val) const |
|
2262 { |
|
2263 int nr = rows (); |
2384
|
2264 int nc = cols (); |
1968
|
2265 |
|
2266 if (nr > 0 && nc > 0) |
|
2267 { |
|
2268 Complex val = elem (0, 0); |
|
2269 |
|
2270 double r_val = real (val); |
|
2271 double i_val = imag (val); |
|
2272 |
|
2273 max_val = r_val; |
|
2274 min_val = r_val; |
|
2275 |
|
2276 if (i_val > max_val) |
|
2277 max_val = i_val; |
|
2278 |
|
2279 if (i_val < max_val) |
|
2280 min_val = i_val; |
|
2281 } |
|
2282 else |
2384
|
2283 return false; |
1968
|
2284 |
|
2285 for (int j = 0; j < nc; j++) |
|
2286 for (int i = 0; i < nr; i++) |
|
2287 { |
|
2288 Complex val = elem (i, j); |
|
2289 |
|
2290 double r_val = real (val); |
|
2291 double i_val = imag (val); |
|
2292 |
|
2293 if (r_val > max_val) |
|
2294 max_val = r_val; |
|
2295 |
|
2296 if (i_val > max_val) |
|
2297 max_val = i_val; |
|
2298 |
|
2299 if (r_val < min_val) |
|
2300 min_val = r_val; |
|
2301 |
|
2302 if (i_val < min_val) |
|
2303 min_val = i_val; |
|
2304 |
|
2305 if (D_NINT (r_val) != r_val || D_NINT (i_val) != i_val) |
2384
|
2306 return false; |
1968
|
2307 } |
2384
|
2308 |
|
2309 return true; |
1968
|
2310 } |
|
2311 |
2384
|
2312 bool |
1968
|
2313 ComplexMatrix::too_large_for_float (void) const |
|
2314 { |
|
2315 int nr = rows (); |
2384
|
2316 int nc = cols (); |
1968
|
2317 |
|
2318 for (int j = 0; j < nc; j++) |
|
2319 for (int i = 0; i < nr; i++) |
|
2320 { |
|
2321 Complex val = elem (i, j); |
|
2322 |
|
2323 double r_val = real (val); |
|
2324 double i_val = imag (val); |
|
2325 |
|
2326 if (r_val > FLT_MAX |
|
2327 || i_val > FLT_MAX |
|
2328 || r_val < FLT_MIN |
|
2329 || i_val < FLT_MIN) |
2384
|
2330 return true; |
1968
|
2331 } |
|
2332 |
2384
|
2333 return false; |
1968
|
2334 } |
|
2335 |
2832
|
2336 boolMatrix |
458
|
2337 ComplexMatrix::all (void) const |
|
2338 { |
|
2339 int nr = rows (); |
|
2340 int nc = cols (); |
2832
|
2341 boolMatrix retval; |
458
|
2342 if (nr > 0 && nc > 0) |
|
2343 { |
|
2344 if (nr == 1) |
|
2345 { |
|
2346 retval.resize (1, 1); |
2832
|
2347 retval.elem (0, 0) = true; |
458
|
2348 for (int j = 0; j < nc; j++) |
|
2349 { |
|
2350 if (elem (0, j) == 0.0) |
|
2351 { |
2832
|
2352 retval.elem (0, 0) = false; |
458
|
2353 break; |
|
2354 } |
|
2355 } |
|
2356 } |
|
2357 else if (nc == 1) |
|
2358 { |
|
2359 retval.resize (1, 1); |
2832
|
2360 retval.elem (0, 0) = true; |
458
|
2361 for (int i = 0; i < nr; i++) |
|
2362 { |
|
2363 if (elem (i, 0) == 0.0) |
|
2364 { |
2832
|
2365 retval.elem (0, 0) = false; |
458
|
2366 break; |
|
2367 } |
|
2368 } |
|
2369 } |
|
2370 else |
|
2371 { |
|
2372 retval.resize (1, nc); |
|
2373 for (int j = 0; j < nc; j++) |
|
2374 { |
2832
|
2375 retval.elem (0, j) = true; |
458
|
2376 for (int i = 0; i < nr; i++) |
|
2377 { |
|
2378 if (elem (i, j) == 0.0) |
|
2379 { |
2832
|
2380 retval.elem (0, j) = false; |
458
|
2381 break; |
|
2382 } |
|
2383 } |
|
2384 } |
|
2385 } |
|
2386 } |
|
2387 return retval; |
|
2388 } |
|
2389 |
2832
|
2390 boolMatrix |
458
|
2391 ComplexMatrix::any (void) const |
|
2392 { |
|
2393 int nr = rows (); |
|
2394 int nc = cols (); |
2832
|
2395 boolMatrix retval; |
458
|
2396 if (nr > 0 && nc > 0) |
|
2397 { |
|
2398 if (nr == 1) |
|
2399 { |
|
2400 retval.resize (1, 1); |
2832
|
2401 retval.elem (0, 0) = false; |
458
|
2402 for (int j = 0; j < nc; j++) |
|
2403 { |
|
2404 if (elem (0, j) != 0.0) |
|
2405 { |
2832
|
2406 retval.elem (0, 0) = true; |
458
|
2407 break; |
|
2408 } |
|
2409 } |
|
2410 } |
|
2411 else if (nc == 1) |
|
2412 { |
|
2413 retval.resize (1, 1); |
2832
|
2414 retval.elem (0, 0) = false; |
458
|
2415 for (int i = 0; i < nr; i++) |
|
2416 { |
|
2417 if (elem (i, 0) != 0.0) |
|
2418 { |
2832
|
2419 retval.elem (0, 0) = true; |
458
|
2420 break; |
|
2421 } |
|
2422 } |
|
2423 } |
|
2424 else |
|
2425 { |
|
2426 retval.resize (1, nc); |
|
2427 for (int j = 0; j < nc; j++) |
|
2428 { |
2832
|
2429 retval.elem (0, j) = false; |
458
|
2430 for (int i = 0; i < nr; i++) |
|
2431 { |
|
2432 if (elem (i, j) != 0.0) |
|
2433 { |
2832
|
2434 retval.elem (0, j) = true; |
458
|
2435 break; |
|
2436 } |
|
2437 } |
|
2438 } |
|
2439 } |
|
2440 } |
|
2441 return retval; |
|
2442 } |
|
2443 |
|
2444 ComplexMatrix |
3723
|
2445 ComplexMatrix::cumprod (int dim) const |
458
|
2446 { |
|
2447 int nr = rows (); |
|
2448 int nc = cols (); |
3723
|
2449 ComplexMatrix retval (nr, nc); |
458
|
2450 if (nr > 0 && nc > 0) |
|
2451 { |
3723
|
2452 if ((nr == 1 && dim == 0) || dim == 1) |
458
|
2453 { |
|
2454 for (int i = 0; i < nr; i++) |
|
2455 { |
3726
|
2456 Complex prod = elem (i, 0); |
3723
|
2457 for (int j = 0; j < nc; j++) |
|
2458 { |
3726
|
2459 retval.elem (i, j) = prod; |
3723
|
2460 if (j < nc - 1) |
|
2461 prod *= elem (i, j+1); |
|
2462 } |
458
|
2463 } |
|
2464 } |
|
2465 else |
|
2466 { |
|
2467 for (int j = 0; j < nc; j++) |
|
2468 { |
|
2469 Complex prod = elem (0, j); |
|
2470 for (int i = 0; i < nr; i++) |
|
2471 { |
|
2472 retval.elem (i, j) = prod; |
|
2473 if (i < nr - 1) |
|
2474 prod *= elem (i+1, j); |
|
2475 } |
|
2476 } |
|
2477 } |
|
2478 } |
|
2479 return retval; |
|
2480 } |
|
2481 |
|
2482 ComplexMatrix |
3723
|
2483 ComplexMatrix::cumsum (int dim) const |
458
|
2484 { |
|
2485 int nr = rows (); |
|
2486 int nc = cols (); |
3723
|
2487 ComplexMatrix retval (nr, nc); |
458
|
2488 if (nr > 0 && nc > 0) |
|
2489 { |
3723
|
2490 if ((nr == 1 && dim == 0) || dim == 1) |
458
|
2491 { |
|
2492 for (int i = 0; i < nr; i++) |
|
2493 { |
3726
|
2494 Complex sum = elem (i, 0); |
3723
|
2495 for (int j = 0; j < nc; j++) |
|
2496 { |
|
2497 retval.elem (i, j) = sum; |
|
2498 if (j < nc - 1) |
|
2499 sum += elem (i, j+1); |
|
2500 } |
458
|
2501 } |
|
2502 } |
|
2503 else |
|
2504 { |
|
2505 for (int j = 0; j < nc; j++) |
|
2506 { |
|
2507 Complex sum = elem (0, j); |
|
2508 for (int i = 0; i < nr; i++) |
|
2509 { |
|
2510 retval.elem (i, j) = sum; |
|
2511 if (i < nr - 1) |
|
2512 sum += elem (i+1, j); |
|
2513 } |
|
2514 } |
|
2515 } |
|
2516 } |
|
2517 return retval; |
|
2518 } |
|
2519 |
|
2520 ComplexMatrix |
3723
|
2521 ComplexMatrix::prod (int dim) const |
458
|
2522 { |
|
2523 int nr = rows (); |
|
2524 int nc = cols (); |
|
2525 ComplexMatrix retval; |
|
2526 if (nr > 0 && nc > 0) |
|
2527 { |
3723
|
2528 if ((nr == 1 && dim == 0) || dim == 1) |
458
|
2529 { |
3723
|
2530 retval.resize(nr, 1); |
458
|
2531 for (int i = 0; i < nr; i++) |
3723
|
2532 { |
|
2533 retval.elem (i, 0) = 1.0; |
|
2534 for (int j = 0; j < nc; j++) |
|
2535 retval.elem (i, 0) *= elem (i, j); |
|
2536 } |
458
|
2537 } |
|
2538 else |
|
2539 { |
|
2540 retval.resize (1, nc); |
|
2541 for (int j = 0; j < nc; j++) |
|
2542 { |
|
2543 retval.elem (0, j) = 1.0; |
|
2544 for (int i = 0; i < nr; i++) |
|
2545 retval.elem (0, j) *= elem (i, j); |
|
2546 } |
|
2547 } |
|
2548 } |
3723
|
2549 else |
|
2550 { |
|
2551 retval.resize (1,1); |
|
2552 retval.elem (0,0) = 1.0; |
|
2553 } |
458
|
2554 return retval; |
|
2555 } |
|
2556 |
|
2557 ComplexMatrix |
3723
|
2558 ComplexMatrix::sum (int dim) const |
458
|
2559 { |
|
2560 int nr = rows (); |
|
2561 int nc = cols (); |
|
2562 ComplexMatrix retval; |
|
2563 if (nr > 0 && nc > 0) |
|
2564 { |
3723
|
2565 if ((nr == 1 && dim == 0) || dim == 1) |
458
|
2566 { |
3723
|
2567 retval.resize (nr, 1); |
458
|
2568 for (int i = 0; i < nr; i++) |
3723
|
2569 { |
|
2570 retval.elem (i, 0) = 0.0; |
|
2571 for (int j = 0; j < nc; j++) |
|
2572 retval.elem (i, 0) += elem (i, j); |
|
2573 } |
458
|
2574 } |
|
2575 else |
|
2576 { |
|
2577 retval.resize (1, nc); |
|
2578 for (int j = 0; j < nc; j++) |
|
2579 { |
|
2580 retval.elem (0, j) = 0.0; |
|
2581 for (int i = 0; i < nr; i++) |
|
2582 retval.elem (0, j) += elem (i, j); |
|
2583 } |
|
2584 } |
|
2585 } |
3723
|
2586 else |
|
2587 { |
|
2588 retval.resize (1, 1); |
|
2589 retval.elem (0, 0) = 0.0; |
|
2590 } |
458
|
2591 return retval; |
|
2592 } |
|
2593 |
|
2594 ComplexMatrix |
3723
|
2595 ComplexMatrix::sumsq (int dim) const |
458
|
2596 { |
|
2597 int nr = rows (); |
|
2598 int nc = cols (); |
|
2599 ComplexMatrix retval; |
|
2600 if (nr > 0 && nc > 0) |
|
2601 { |
3723
|
2602 if ((nr == 1 && dim == 0) || dim == 1) |
458
|
2603 { |
3723
|
2604 retval.resize (nr, 1); |
458
|
2605 for (int i = 0; i < nr; i++) |
|
2606 { |
3723
|
2607 retval.elem (i, 0) = 0.0; |
|
2608 for (int j = 0; j < nc; j++) |
|
2609 { |
|
2610 Complex d = elem (i, j); |
|
2611 retval.elem (i, 0) += d * conj (d); |
|
2612 } |
458
|
2613 } |
|
2614 } |
|
2615 else |
|
2616 { |
|
2617 retval.resize (1, nc); |
|
2618 for (int j = 0; j < nc; j++) |
|
2619 { |
|
2620 retval.elem (0, j) = 0.0; |
|
2621 for (int i = 0; i < nr; i++) |
|
2622 { |
|
2623 Complex d = elem (i, j); |
3095
|
2624 retval.elem (0, j) += d * conj (d); |
458
|
2625 } |
|
2626 } |
|
2627 } |
|
2628 } |
3723
|
2629 else |
|
2630 { |
|
2631 retval.resize (1, 1); |
|
2632 retval.elem (0, 0) = 0.0; |
|
2633 } |
|
2634 |
458
|
2635 return retval; |
|
2636 } |
|
2637 |
|
2638 ComplexColumnVector |
|
2639 ComplexMatrix::diag (void) const |
|
2640 { |
|
2641 return diag (0); |
|
2642 } |
|
2643 |
|
2644 ComplexColumnVector |
|
2645 ComplexMatrix::diag (int k) const |
|
2646 { |
|
2647 int nnr = rows (); |
|
2648 int nnc = cols (); |
|
2649 if (k > 0) |
|
2650 nnc -= k; |
|
2651 else if (k < 0) |
|
2652 nnr += k; |
|
2653 |
|
2654 ComplexColumnVector d; |
|
2655 |
|
2656 if (nnr > 0 && nnc > 0) |
|
2657 { |
|
2658 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
2659 |
|
2660 d.resize (ndiag); |
|
2661 |
|
2662 if (k > 0) |
|
2663 { |
|
2664 for (int i = 0; i < ndiag; i++) |
|
2665 d.elem (i) = elem (i, i+k); |
|
2666 } |
|
2667 else if ( k < 0) |
|
2668 { |
|
2669 for (int i = 0; i < ndiag; i++) |
|
2670 d.elem (i) = elem (i-k, i); |
|
2671 } |
|
2672 else |
|
2673 { |
|
2674 for (int i = 0; i < ndiag; i++) |
|
2675 d.elem (i) = elem (i, i); |
|
2676 } |
|
2677 } |
|
2678 else |
3504
|
2679 std::cerr << "diag: requested diagonal out of range\n"; |
458
|
2680 |
|
2681 return d; |
|
2682 } |
|
2683 |
2354
|
2684 bool |
|
2685 ComplexMatrix::row_is_real_only (int i) const |
|
2686 { |
|
2687 bool retval = true; |
|
2688 |
|
2689 int nc = columns (); |
|
2690 |
|
2691 for (int j = 0; j < nc; j++) |
|
2692 { |
|
2693 if (imag (elem (i, j)) != 0.0) |
|
2694 { |
|
2695 retval = false; |
|
2696 break; |
|
2697 } |
|
2698 } |
|
2699 |
|
2700 return retval; |
|
2701 } |
|
2702 |
|
2703 bool |
|
2704 ComplexMatrix::column_is_real_only (int j) const |
|
2705 { |
|
2706 bool retval = true; |
|
2707 |
|
2708 int nr = rows (); |
|
2709 |
|
2710 for (int i = 0; i < nr; i++) |
|
2711 { |
|
2712 if (imag (elem (i, j)) != 0.0) |
|
2713 { |
|
2714 retval = false; |
|
2715 break; |
|
2716 } |
|
2717 } |
|
2718 |
|
2719 return retval; |
|
2720 } |
891
|
2721 |
458
|
2722 ComplexColumnVector |
|
2723 ComplexMatrix::row_min (void) const |
|
2724 { |
2354
|
2725 Array<int> index; |
|
2726 return row_min (index); |
458
|
2727 } |
|
2728 |
|
2729 ComplexColumnVector |
2354
|
2730 ComplexMatrix::row_min (Array<int>& index) const |
458
|
2731 { |
|
2732 ComplexColumnVector result; |
|
2733 |
|
2734 int nr = rows (); |
|
2735 int nc = cols (); |
|
2736 |
|
2737 if (nr > 0 && nc > 0) |
|
2738 { |
|
2739 result.resize (nr); |
2354
|
2740 index.resize (nr); |
458
|
2741 |
|
2742 for (int i = 0; i < nr; i++) |
|
2743 { |
3504
|
2744 int idx_j = 0; |
|
2745 |
|
2746 Complex tmp_min = elem (i, idx_j); |
2354
|
2747 |
|
2748 bool real_only = row_is_real_only (i); |
|
2749 |
|
2750 double abs_min = real_only ? real (tmp_min) : abs (tmp_min); |
|
2751 |
|
2752 if (xisnan (tmp_min)) |
3504
|
2753 idx_j = -1; |
891
|
2754 else |
|
2755 { |
|
2756 for (int j = 1; j < nc; j++) |
2354
|
2757 { |
|
2758 Complex tmp = elem (i, j); |
|
2759 |
|
2760 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
2761 |
|
2762 if (xisnan (tmp)) |
|
2763 { |
3504
|
2764 idx_j = -1; |
2354
|
2765 break; |
|
2766 } |
|
2767 else if (abs_tmp < abs_min) |
|
2768 { |
3504
|
2769 idx_j = j; |
2354
|
2770 tmp_min = tmp; |
|
2771 abs_min = abs_tmp; |
|
2772 } |
|
2773 } |
|
2774 |
3504
|
2775 result.elem (i) = (idx_j < 0) ? Complex_NaN_result : tmp_min; |
|
2776 index.elem (i) = idx_j; |
891
|
2777 } |
458
|
2778 } |
|
2779 } |
|
2780 |
|
2781 return result; |
|
2782 } |
|
2783 |
|
2784 ComplexColumnVector |
|
2785 ComplexMatrix::row_max (void) const |
|
2786 { |
2354
|
2787 Array<int> index; |
|
2788 return row_max (index); |
458
|
2789 } |
|
2790 |
|
2791 ComplexColumnVector |
2354
|
2792 ComplexMatrix::row_max (Array<int>& index) const |
458
|
2793 { |
|
2794 ComplexColumnVector result; |
|
2795 |
|
2796 int nr = rows (); |
|
2797 int nc = cols (); |
|
2798 |
|
2799 if (nr > 0 && nc > 0) |
|
2800 { |
|
2801 result.resize (nr); |
2354
|
2802 index.resize (nr); |
458
|
2803 |
|
2804 for (int i = 0; i < nr; i++) |
|
2805 { |
3504
|
2806 int idx_j = 0; |
|
2807 |
|
2808 Complex tmp_max = elem (i, idx_j); |
2354
|
2809 |
|
2810 bool real_only = row_is_real_only (i); |
|
2811 |
|
2812 double abs_max = real_only ? real (tmp_max) : abs (tmp_max); |
|
2813 |
|
2814 if (xisnan (tmp_max)) |
3504
|
2815 idx_j = -1; |
891
|
2816 else |
|
2817 { |
|
2818 for (int j = 1; j < nc; j++) |
2354
|
2819 { |
|
2820 Complex tmp = elem (i, j); |
|
2821 |
|
2822 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
2823 |
|
2824 if (xisnan (tmp)) |
|
2825 { |
3504
|
2826 idx_j = -1; |
2354
|
2827 break; |
|
2828 } |
|
2829 else if (abs_tmp > abs_max) |
|
2830 { |
3504
|
2831 idx_j = j; |
2354
|
2832 tmp_max = tmp; |
|
2833 abs_max = abs_tmp; |
|
2834 } |
|
2835 } |
|
2836 |
3504
|
2837 result.elem (i) = (idx_j < 0) ? Complex_NaN_result : tmp_max; |
|
2838 index.elem (i) = idx_j; |
891
|
2839 } |
458
|
2840 } |
|
2841 } |
|
2842 |
|
2843 return result; |
|
2844 } |
|
2845 |
|
2846 ComplexRowVector |
|
2847 ComplexMatrix::column_min (void) const |
|
2848 { |
2354
|
2849 Array<int> index; |
|
2850 return column_min (index); |
458
|
2851 } |
|
2852 |
|
2853 ComplexRowVector |
2354
|
2854 ComplexMatrix::column_min (Array<int>& index) const |
458
|
2855 { |
|
2856 ComplexRowVector result; |
|
2857 |
|
2858 int nr = rows (); |
|
2859 int nc = cols (); |
|
2860 |
|
2861 if (nr > 0 && nc > 0) |
|
2862 { |
|
2863 result.resize (nc); |
2354
|
2864 index.resize (nc); |
458
|
2865 |
|
2866 for (int j = 0; j < nc; j++) |
|
2867 { |
3504
|
2868 int idx_i = 0; |
|
2869 |
|
2870 Complex tmp_min = elem (idx_i, j); |
2354
|
2871 |
|
2872 bool real_only = column_is_real_only (j); |
|
2873 |
|
2874 double abs_min = real_only ? real (tmp_min) : abs (tmp_min); |
|
2875 |
|
2876 if (xisnan (tmp_min)) |
3504
|
2877 idx_i = -1; |
891
|
2878 else |
|
2879 { |
|
2880 for (int i = 1; i < nr; i++) |
2354
|
2881 { |
|
2882 Complex tmp = elem (i, j); |
|
2883 |
|
2884 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
2885 |
|
2886 if (xisnan (tmp)) |
|
2887 { |
3504
|
2888 idx_i = -1; |
2354
|
2889 break; |
|
2890 } |
|
2891 else if (abs_tmp < abs_min) |
|
2892 { |
3504
|
2893 idx_i = i; |
2354
|
2894 tmp_min = tmp; |
|
2895 abs_min = abs_tmp; |
|
2896 } |
|
2897 } |
|
2898 |
3504
|
2899 result.elem (j) = (idx_i < 0) ? Complex_NaN_result : tmp_min; |
|
2900 index.elem (j) = idx_i; |
891
|
2901 } |
458
|
2902 } |
|
2903 } |
|
2904 |
|
2905 return result; |
|
2906 } |
|
2907 |
|
2908 ComplexRowVector |
|
2909 ComplexMatrix::column_max (void) const |
|
2910 { |
2354
|
2911 Array<int> index; |
|
2912 return column_max (index); |
458
|
2913 } |
|
2914 |
|
2915 ComplexRowVector |
2354
|
2916 ComplexMatrix::column_max (Array<int>& index) const |
458
|
2917 { |
|
2918 ComplexRowVector result; |
|
2919 |
|
2920 int nr = rows (); |
|
2921 int nc = cols (); |
|
2922 |
|
2923 if (nr > 0 && nc > 0) |
|
2924 { |
|
2925 result.resize (nc); |
2354
|
2926 index.resize (nc); |
458
|
2927 |
|
2928 for (int j = 0; j < nc; j++) |
|
2929 { |
3504
|
2930 int idx_i = 0; |
|
2931 |
|
2932 Complex tmp_max = elem (idx_i, j); |
2354
|
2933 |
|
2934 bool real_only = column_is_real_only (j); |
|
2935 |
|
2936 double abs_max = real_only ? real (tmp_max) : abs (tmp_max); |
|
2937 |
|
2938 if (xisnan (tmp_max)) |
3504
|
2939 idx_i = -1; |
891
|
2940 else |
|
2941 { |
|
2942 for (int i = 1; i < nr; i++) |
2354
|
2943 { |
|
2944 Complex tmp = elem (i, j); |
|
2945 |
|
2946 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
2947 |
|
2948 if (xisnan (tmp)) |
|
2949 { |
3504
|
2950 idx_i = -1; |
2354
|
2951 break; |
|
2952 } |
|
2953 else if (abs_tmp > abs_max) |
|
2954 { |
3504
|
2955 idx_i = i; |
2354
|
2956 tmp_max = tmp; |
|
2957 abs_max = abs_tmp; |
|
2958 } |
|
2959 } |
|
2960 |
3504
|
2961 result.elem (j) = (idx_i < 0) ? Complex_NaN_result : tmp_max; |
|
2962 index.elem (j) = idx_i; |
891
|
2963 } |
458
|
2964 } |
|
2965 } |
|
2966 |
|
2967 return result; |
|
2968 } |
|
2969 |
|
2970 // i/o |
|
2971 |
3504
|
2972 std::ostream& |
|
2973 operator << (std::ostream& os, const ComplexMatrix& a) |
458
|
2974 { |
|
2975 // int field_width = os.precision () + 7; |
|
2976 for (int i = 0; i < a.rows (); i++) |
|
2977 { |
|
2978 for (int j = 0; j < a.cols (); j++) |
|
2979 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
2980 os << "\n"; |
|
2981 } |
|
2982 return os; |
|
2983 } |
|
2984 |
3504
|
2985 std::istream& |
|
2986 operator >> (std::istream& is, ComplexMatrix& a) |
458
|
2987 { |
|
2988 int nr = a.rows (); |
|
2989 int nc = a.cols (); |
|
2990 |
|
2991 if (nr < 1 || nc < 1) |
3504
|
2992 is.clear (std::ios::badbit); |
458
|
2993 else |
|
2994 { |
|
2995 Complex tmp; |
|
2996 for (int i = 0; i < nr; i++) |
|
2997 for (int j = 0; j < nc; j++) |
|
2998 { |
|
2999 is >> tmp; |
|
3000 if (is) |
|
3001 a.elem (i, j) = tmp; |
|
3002 else |
2993
|
3003 goto done; |
458
|
3004 } |
|
3005 } |
|
3006 |
2993
|
3007 done: |
|
3008 |
458
|
3009 return is; |
|
3010 } |
|
3011 |
1819
|
3012 ComplexMatrix |
|
3013 Givens (const Complex& x, const Complex& y) |
|
3014 { |
|
3015 double cc; |
|
3016 Complex cs, temp_r; |
|
3017 |
|
3018 F77_FCN (zlartg, ZLARTG) (x, y, cc, cs, temp_r); |
|
3019 |
|
3020 ComplexMatrix g (2, 2); |
|
3021 |
|
3022 g.elem (0, 0) = cc; |
|
3023 g.elem (1, 1) = cc; |
|
3024 g.elem (0, 1) = cs; |
|
3025 g.elem (1, 0) = -conj (cs); |
|
3026 |
|
3027 return g; |
|
3028 } |
|
3029 |
|
3030 ComplexMatrix |
|
3031 Sylvester (const ComplexMatrix& a, const ComplexMatrix& b, |
|
3032 const ComplexMatrix& c) |
|
3033 { |
|
3034 ComplexMatrix retval; |
|
3035 |
|
3036 // XXX FIXME XXX -- need to check that a, b, and c are all the same |
|
3037 // size. |
|
3038 |
|
3039 // Compute Schur decompositions |
|
3040 |
|
3041 ComplexSCHUR as (a, "U"); |
|
3042 ComplexSCHUR bs (b, "U"); |
|
3043 |
|
3044 // Transform c to new coordinates. |
|
3045 |
|
3046 ComplexMatrix ua = as.unitary_matrix (); |
|
3047 ComplexMatrix sch_a = as.schur_matrix (); |
|
3048 |
|
3049 ComplexMatrix ub = bs.unitary_matrix (); |
|
3050 ComplexMatrix sch_b = bs.schur_matrix (); |
|
3051 |
|
3052 ComplexMatrix cx = ua.hermitian () * c * ub; |
|
3053 |
|
3054 // Solve the sylvester equation, back-transform, and return the |
|
3055 // solution. |
|
3056 |
|
3057 int a_nr = a.rows (); |
|
3058 int b_nr = b.rows (); |
|
3059 |
|
3060 double scale; |
|
3061 int info; |
1950
|
3062 |
|
3063 Complex *pa = sch_a.fortran_vec (); |
|
3064 Complex *pb = sch_b.fortran_vec (); |
|
3065 Complex *px = cx.fortran_vec (); |
1819
|
3066 |
1950
|
3067 F77_XFCN (ztrsyl, ZTRSYL, ("N", "N", 1, a_nr, b_nr, pa, a_nr, pb, |
|
3068 b_nr, px, a_nr, scale, |
|
3069 info, 1L, 1L)); |
|
3070 |
|
3071 if (f77_exception_encountered) |
|
3072 (*current_liboctave_error_handler) ("unrecoverable error in ztrsyl"); |
|
3073 else |
|
3074 { |
|
3075 // XXX FIXME XXX -- check info? |
|
3076 |
|
3077 retval = -ua * cx * ub.hermitian (); |
|
3078 } |
1819
|
3079 |
|
3080 return retval; |
|
3081 } |
|
3082 |
2828
|
3083 ComplexMatrix |
|
3084 operator * (const ComplexMatrix& m, const Matrix& a) |
|
3085 { |
|
3086 ComplexMatrix tmp (a); |
|
3087 return m * tmp; |
|
3088 } |
|
3089 |
|
3090 ComplexMatrix |
|
3091 operator * (const Matrix& m, const ComplexMatrix& a) |
|
3092 { |
|
3093 ComplexMatrix tmp (m); |
|
3094 return tmp * a; |
|
3095 } |
|
3096 |
|
3097 ComplexMatrix |
|
3098 operator * (const ComplexMatrix& m, const ComplexMatrix& a) |
|
3099 { |
|
3100 ComplexMatrix retval; |
|
3101 |
|
3102 int nr = m.rows (); |
|
3103 int nc = m.cols (); |
|
3104 |
|
3105 int a_nr = a.rows (); |
|
3106 int a_nc = a.cols (); |
|
3107 |
|
3108 if (nc != a_nr) |
|
3109 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
|
3110 else |
|
3111 { |
|
3112 if (nr == 0 || nc == 0 || a_nc == 0) |
3760
|
3113 retval.resize (nr, a_nc, 0.0); |
2828
|
3114 else |
|
3115 { |
|
3116 int ld = nr; |
|
3117 int lda = a.rows (); |
|
3118 |
|
3119 retval.resize (nr, a_nc); |
|
3120 Complex *c = retval.fortran_vec (); |
|
3121 |
|
3122 F77_XFCN (zgemm, ZGEMM, ("N", "N", nr, a_nc, nc, 1.0, |
|
3123 m.data (), ld, a.data (), lda, 0.0, |
|
3124 c, nr, 1L, 1L)); |
|
3125 |
|
3126 if (f77_exception_encountered) |
|
3127 (*current_liboctave_error_handler) |
|
3128 ("unrecoverable error in zgemm"); |
|
3129 } |
|
3130 } |
|
3131 |
|
3132 return retval; |
|
3133 } |
|
3134 |
2870
|
3135 MS_CMP_OPS(ComplexMatrix, real, Complex, real) |
3504
|
3136 MS_BOOL_OPS(ComplexMatrix, Complex, 0.0) |
2870
|
3137 |
|
3138 SM_CMP_OPS(Complex, real, ComplexMatrix, real) |
3504
|
3139 SM_BOOL_OPS(Complex, ComplexMatrix, 0.0) |
2870
|
3140 |
|
3141 MM_CMP_OPS(ComplexMatrix, real, ComplexMatrix, real) |
3504
|
3142 MM_BOOL_OPS(ComplexMatrix, ComplexMatrix, 0.0) |
2870
|
3143 |
458
|
3144 /* |
|
3145 ;;; Local Variables: *** |
|
3146 ;;; mode: C++ *** |
|
3147 ;;; End: *** |
|
3148 */ |