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