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