1993
|
1 // Matrix manipulations. |
458
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 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 |
|
1457 Array<Complex> work (lwork); |
|
1458 Complex *pwork = work.fortran_vec (); |
|
1459 |
|
1460 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1461 lrwork = lrwork > 1 ? lrwork : 1; |
|
1462 Array<double> rwork (lrwork); |
|
1463 double *prwork = rwork.fortran_vec (); |
|
1464 |
|
1465 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
1466 nrr, ps, rcond, rank, pwork, lwork, |
|
1467 prwork, info)); |
|
1468 |
|
1469 if (f77_exception_encountered) |
|
1470 (*current_liboctave_error_handler) ("unrecoverable error in zgelss"); |
|
1471 else |
|
1472 { |
2563
|
1473 retval.resize (n, nrhs); |
1948
|
1474 for (int j = 0; j < nrhs; j++) |
|
1475 for (int i = 0; i < n; i++) |
|
1476 retval.elem (i, j) = result.elem (i, j); |
|
1477 } |
458
|
1478 } |
|
1479 |
|
1480 return retval; |
|
1481 } |
|
1482 |
|
1483 ComplexColumnVector |
|
1484 ComplexMatrix::lssolve (const ComplexColumnVector& b) const |
|
1485 { |
|
1486 int info; |
|
1487 int rank; |
|
1488 return lssolve (b, info, rank); |
|
1489 } |
|
1490 |
|
1491 ComplexColumnVector |
|
1492 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info) const |
|
1493 { |
|
1494 int rank; |
|
1495 return lssolve (b, info, rank); |
|
1496 } |
|
1497 |
|
1498 ComplexColumnVector |
|
1499 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info, |
|
1500 int& rank) const |
|
1501 { |
1948
|
1502 ComplexColumnVector retval; |
|
1503 |
458
|
1504 int nrhs = 1; |
|
1505 |
|
1506 int m = rows (); |
|
1507 int n = cols (); |
|
1508 |
|
1509 if (m == 0 || n == 0 || m != b.length ()) |
1948
|
1510 (*current_liboctave_error_handler) |
|
1511 ("matrix dimension mismatch solution of least squares problem"); |
|
1512 else |
458
|
1513 { |
1948
|
1514 ComplexMatrix atmp = *this; |
|
1515 Complex *tmp_data = atmp.fortran_vec (); |
|
1516 |
|
1517 int nrr = m > n ? m : n; |
|
1518 ComplexColumnVector result (nrr); |
|
1519 |
|
1520 for (int i = 0; i < m; i++) |
|
1521 result.elem (i) = b.elem (i); |
|
1522 |
|
1523 Complex *presult = result.fortran_vec (); |
|
1524 |
|
1525 int len_s = m < n ? m : n; |
|
1526 Array<double> s (len_s); |
|
1527 double *ps = s.fortran_vec (); |
|
1528 |
|
1529 double rcond = -1.0; |
|
1530 |
|
1531 int lwork; |
|
1532 if (m < n) |
|
1533 lwork = 2*m + (nrhs > n ? nrhs : n); |
|
1534 else |
|
1535 lwork = 2*n + (nrhs > m ? nrhs : m); |
|
1536 |
|
1537 Array<Complex> work (lwork); |
|
1538 Complex *pwork = work.fortran_vec (); |
|
1539 |
|
1540 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1541 lrwork = lrwork > 1 ? lrwork : 1; |
|
1542 Array<double> rwork (lrwork); |
|
1543 double *prwork = rwork.fortran_vec (); |
|
1544 |
|
1545 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
1546 nrr, ps, rcond, rank, pwork, lwork, |
|
1547 prwork, info)); |
|
1548 |
|
1549 if (f77_exception_encountered) |
|
1550 (*current_liboctave_error_handler) ("unrecoverable error in zgelss"); |
|
1551 else |
|
1552 { |
2563
|
1553 retval.resize (n); |
1948
|
1554 for (int i = 0; i < n; i++) |
|
1555 retval.elem (i) = result.elem (i); |
|
1556 } |
458
|
1557 } |
|
1558 |
|
1559 return retval; |
|
1560 } |
|
1561 |
1819
|
1562 // Constants for matrix exponential calculation. |
|
1563 |
|
1564 static double padec [] = |
|
1565 { |
|
1566 5.0000000000000000e-1, |
|
1567 1.1666666666666667e-1, |
|
1568 1.6666666666666667e-2, |
|
1569 1.6025641025641026e-3, |
|
1570 1.0683760683760684e-4, |
|
1571 4.8562548562548563e-6, |
|
1572 1.3875013875013875e-7, |
|
1573 1.9270852604185938e-9, |
|
1574 }; |
|
1575 |
|
1576 ComplexMatrix |
|
1577 ComplexMatrix::expm (void) const |
|
1578 { |
|
1579 ComplexMatrix retval; |
|
1580 |
|
1581 ComplexMatrix m = *this; |
|
1582 |
|
1583 int nc = columns (); |
|
1584 |
|
1585 // trace shift value |
|
1586 Complex trshift = 0.0; |
|
1587 |
|
1588 // Preconditioning step 1: trace normalization. |
|
1589 |
|
1590 for (int i = 0; i < nc; i++) |
|
1591 trshift += m.elem (i, i); |
|
1592 |
|
1593 trshift /= nc; |
|
1594 |
|
1595 for (int i = 0; i < nc; i++) |
|
1596 m.elem (i, i) -= trshift; |
|
1597 |
|
1598 // Preconditioning step 2: eigenvalue balancing. |
|
1599 |
|
1600 ComplexAEPBALANCE mbal (m, "B"); |
|
1601 m = mbal.balanced_matrix (); |
|
1602 ComplexMatrix d = mbal.balancing_matrix (); |
|
1603 |
|
1604 // Preconditioning step 3: scaling. |
|
1605 |
|
1606 ColumnVector work (nc); |
|
1607 double inf_norm |
|
1608 = F77_FCN (zlange, ZLANGE) ("I", nc, nc, m.fortran_vec (), nc, |
|
1609 work.fortran_vec ()); |
|
1610 |
2800
|
1611 int sqpow = (inf_norm > 0.0 |
|
1612 ? static_cast<int> (1.0 + log (inf_norm) / log (2.0)) : 0); |
1819
|
1613 |
|
1614 // Check whether we need to square at all. |
|
1615 |
|
1616 if (sqpow < 0) |
|
1617 sqpow = 0; |
|
1618 |
|
1619 if (sqpow > 0) |
|
1620 { |
|
1621 double scale_factor = 1.0; |
|
1622 for (int i = 0; i < sqpow; i++) |
|
1623 scale_factor *= 2.0; |
|
1624 |
|
1625 m = m / scale_factor; |
|
1626 } |
|
1627 |
|
1628 // npp, dpp: pade' approx polynomial matrices. |
|
1629 |
|
1630 ComplexMatrix npp (nc, nc, 0.0); |
|
1631 ComplexMatrix dpp = npp; |
|
1632 |
|
1633 // Now powers a^8 ... a^1. |
|
1634 |
|
1635 int minus_one_j = -1; |
|
1636 for (int j = 7; j >= 0; j--) |
|
1637 { |
|
1638 npp = m * npp + m * padec[j]; |
|
1639 dpp = m * dpp + m * (minus_one_j * padec[j]); |
|
1640 minus_one_j *= -1; |
|
1641 } |
|
1642 |
|
1643 // Zero power. |
|
1644 |
|
1645 dpp = -dpp; |
|
1646 for (int j = 0; j < nc; j++) |
|
1647 { |
|
1648 npp.elem (j, j) += 1.0; |
|
1649 dpp.elem (j, j) += 1.0; |
|
1650 } |
|
1651 |
|
1652 // Compute pade approximation = inverse (dpp) * npp. |
|
1653 |
|
1654 retval = dpp.solve (npp); |
|
1655 |
|
1656 // Reverse preconditioning step 3: repeated squaring. |
|
1657 |
|
1658 while (sqpow) |
|
1659 { |
|
1660 retval = retval * retval; |
|
1661 sqpow--; |
|
1662 } |
|
1663 |
|
1664 // Reverse preconditioning step 2: inverse balancing. |
|
1665 // XXX FIXME XXX -- should probably do this with Lapack calls |
|
1666 // instead of a complete matrix inversion. |
|
1667 |
|
1668 retval = retval.transpose (); |
|
1669 d = d.transpose (); |
|
1670 retval = retval * d; |
|
1671 retval = d.solve (retval); |
|
1672 retval = retval.transpose (); |
|
1673 |
|
1674 // Reverse preconditioning step 1: fix trace normalization. |
|
1675 |
|
1676 return retval * exp (trshift); |
|
1677 } |
|
1678 |
1205
|
1679 // column vector by row vector -> matrix operations |
|
1680 |
|
1681 ComplexMatrix |
|
1682 operator * (const ColumnVector& v, const ComplexRowVector& a) |
|
1683 { |
|
1684 ComplexColumnVector tmp (v); |
|
1685 return tmp * a; |
|
1686 } |
|
1687 |
|
1688 ComplexMatrix |
|
1689 operator * (const ComplexColumnVector& a, const RowVector& b) |
|
1690 { |
|
1691 ComplexRowVector tmp (b); |
|
1692 return a * tmp; |
|
1693 } |
|
1694 |
|
1695 ComplexMatrix |
|
1696 operator * (const ComplexColumnVector& v, const ComplexRowVector& a) |
|
1697 { |
1948
|
1698 ComplexMatrix retval; |
|
1699 |
1205
|
1700 int len = v.length (); |
|
1701 int a_len = a.length (); |
1948
|
1702 |
1205
|
1703 if (len != a_len) |
2384
|
1704 gripe_nonconformant ("operator *", len, 1, 1, a_len); |
1948
|
1705 else |
1205
|
1706 { |
1948
|
1707 if (len != 0) |
|
1708 { |
|
1709 retval.resize (len, a_len); |
|
1710 Complex *c = retval.fortran_vec (); |
|
1711 |
|
1712 F77_XFCN (zgemm, ZGEMM, ("N", "N", len, a_len, 1, 1.0, |
|
1713 v.data (), len, a.data (), 1, 0.0, |
|
1714 c, len, 1L, 1L)); |
|
1715 |
|
1716 if (f77_exception_encountered) |
|
1717 (*current_liboctave_error_handler) |
|
1718 ("unrecoverable error in zgemm"); |
|
1719 } |
1205
|
1720 } |
|
1721 |
1948
|
1722 return retval; |
1205
|
1723 } |
|
1724 |
458
|
1725 // matrix by diagonal matrix -> matrix operations |
|
1726 |
|
1727 ComplexMatrix& |
|
1728 ComplexMatrix::operator += (const DiagMatrix& a) |
|
1729 { |
|
1730 int nr = rows (); |
|
1731 int nc = cols (); |
2384
|
1732 |
|
1733 int a_nr = rows (); |
|
1734 int a_nc = cols (); |
|
1735 |
|
1736 if (nr != a_nr || nc != a_nc) |
458
|
1737 { |
2384
|
1738 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889
|
1739 return *this; |
458
|
1740 } |
|
1741 |
|
1742 for (int i = 0; i < a.length (); i++) |
|
1743 elem (i, i) += a.elem (i, i); |
|
1744 |
|
1745 return *this; |
|
1746 } |
|
1747 |
|
1748 ComplexMatrix& |
|
1749 ComplexMatrix::operator -= (const DiagMatrix& a) |
|
1750 { |
|
1751 int nr = rows (); |
|
1752 int nc = cols (); |
2384
|
1753 |
|
1754 int a_nr = rows (); |
|
1755 int a_nc = cols (); |
|
1756 |
|
1757 if (nr != a_nr || nc != a_nc) |
458
|
1758 { |
2384
|
1759 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889
|
1760 return *this; |
458
|
1761 } |
|
1762 |
|
1763 for (int i = 0; i < a.length (); i++) |
|
1764 elem (i, i) -= a.elem (i, i); |
|
1765 |
|
1766 return *this; |
|
1767 } |
|
1768 |
|
1769 ComplexMatrix& |
|
1770 ComplexMatrix::operator += (const ComplexDiagMatrix& a) |
|
1771 { |
|
1772 int nr = rows (); |
|
1773 int nc = cols (); |
2384
|
1774 |
|
1775 int a_nr = rows (); |
|
1776 int a_nc = cols (); |
|
1777 |
|
1778 if (nr != a_nr || nc != a_nc) |
458
|
1779 { |
2384
|
1780 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889
|
1781 return *this; |
458
|
1782 } |
|
1783 |
|
1784 for (int i = 0; i < a.length (); i++) |
|
1785 elem (i, i) += a.elem (i, i); |
|
1786 |
|
1787 return *this; |
|
1788 } |
|
1789 |
|
1790 ComplexMatrix& |
|
1791 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) |
|
1792 { |
|
1793 int nr = rows (); |
|
1794 int nc = cols (); |
2384
|
1795 |
|
1796 int a_nr = rows (); |
|
1797 int a_nc = cols (); |
|
1798 |
|
1799 if (nr != a_nr || nc != a_nc) |
458
|
1800 { |
2384
|
1801 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889
|
1802 return *this; |
458
|
1803 } |
|
1804 |
|
1805 for (int i = 0; i < a.length (); i++) |
|
1806 elem (i, i) -= a.elem (i, i); |
|
1807 |
|
1808 return *this; |
|
1809 } |
|
1810 |
|
1811 // matrix by matrix -> matrix operations |
|
1812 |
|
1813 ComplexMatrix& |
|
1814 ComplexMatrix::operator += (const Matrix& a) |
|
1815 { |
|
1816 int nr = rows (); |
|
1817 int nc = cols (); |
2384
|
1818 |
|
1819 int a_nr = a.rows (); |
|
1820 int a_nc = a.cols (); |
|
1821 |
|
1822 if (nr != a_nr || nc != a_nc) |
458
|
1823 { |
2384
|
1824 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
1825 return *this; |
|
1826 } |
|
1827 |
|
1828 if (nr == 0 || nc == 0) |
|
1829 return *this; |
|
1830 |
|
1831 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1832 |
|
1833 add2 (d, a.data (), length ()); |
|
1834 return *this; |
|
1835 } |
|
1836 |
|
1837 ComplexMatrix& |
|
1838 ComplexMatrix::operator -= (const Matrix& a) |
|
1839 { |
|
1840 int nr = rows (); |
|
1841 int nc = cols (); |
2384
|
1842 |
|
1843 int a_nr = a.rows (); |
|
1844 int a_nc = a.cols (); |
|
1845 |
|
1846 if (nr != a_nr || nc != a_nc) |
458
|
1847 { |
2384
|
1848 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
1849 return *this; |
|
1850 } |
|
1851 |
|
1852 if (nr == 0 || nc == 0) |
|
1853 return *this; |
|
1854 |
|
1855 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1856 |
|
1857 subtract2 (d, a.data (), length ()); |
|
1858 return *this; |
|
1859 } |
|
1860 |
|
1861 ComplexMatrix& |
|
1862 ComplexMatrix::operator += (const ComplexMatrix& a) |
|
1863 { |
|
1864 int nr = rows (); |
|
1865 int nc = cols (); |
2384
|
1866 |
|
1867 int a_nr = a.rows (); |
|
1868 int a_nc = a.cols (); |
|
1869 |
|
1870 if (nr != a_nr || nc != a_nc) |
458
|
1871 { |
2384
|
1872 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
1873 return *this; |
|
1874 } |
|
1875 |
|
1876 if (nr == 0 || nc == 0) |
|
1877 return *this; |
|
1878 |
|
1879 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1880 |
|
1881 add2 (d, a.data (), length ()); |
|
1882 return *this; |
|
1883 } |
|
1884 |
|
1885 ComplexMatrix& |
|
1886 ComplexMatrix::operator -= (const ComplexMatrix& a) |
|
1887 { |
|
1888 int nr = rows (); |
|
1889 int nc = cols (); |
2384
|
1890 |
|
1891 int a_nr = a.rows (); |
|
1892 int a_nc = a.cols (); |
|
1893 |
|
1894 if (nr != a_nr || nc != a_nc) |
458
|
1895 { |
2384
|
1896 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
1897 return *this; |
|
1898 } |
|
1899 |
|
1900 if (nr == 0 || nc == 0) |
|
1901 return *this; |
|
1902 |
|
1903 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1904 |
|
1905 subtract2 (d, a.data (), length ()); |
|
1906 return *this; |
|
1907 } |
|
1908 |
|
1909 // unary operations |
|
1910 |
|
1911 Matrix |
|
1912 ComplexMatrix::operator ! (void) const |
|
1913 { |
|
1914 return Matrix (not (data (), length ()), rows (), cols ()); |
|
1915 } |
|
1916 |
|
1917 // other operations |
|
1918 |
|
1919 ComplexMatrix |
2676
|
1920 ComplexMatrix::map (c_c_Mapper f) const |
458
|
1921 { |
2676
|
1922 ComplexMatrix b (*this); |
|
1923 return b.apply (f); |
458
|
1924 } |
|
1925 |
2676
|
1926 Matrix |
|
1927 ComplexMatrix::map (d_c_Mapper f) const |
458
|
1928 { |
2676
|
1929 const Complex *d = data (); |
|
1930 |
|
1931 Matrix retval (rows (), columns ()); |
|
1932 |
|
1933 double *r = retval.fortran_vec (); |
|
1934 |
|
1935 for (int i = 0; i < length (); i++) |
|
1936 r[i] = f (d[i]); |
|
1937 |
|
1938 return retval; |
|
1939 } |
|
1940 |
|
1941 ComplexMatrix& |
|
1942 ComplexMatrix::apply (c_c_Mapper f) |
|
1943 { |
|
1944 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1945 |
|
1946 for (int i = 0; i < length (); i++) |
|
1947 d[i] = f (d[i]); |
|
1948 |
|
1949 return *this; |
458
|
1950 } |
|
1951 |
2384
|
1952 bool |
|
1953 ComplexMatrix::any_element_is_inf_or_nan (void) const |
|
1954 { |
|
1955 int nr = rows (); |
|
1956 int nc = cols (); |
|
1957 |
|
1958 for (int j = 0; j < nc; j++) |
|
1959 for (int i = 0; i < nr; i++) |
|
1960 { |
|
1961 Complex val = elem (i, j); |
|
1962 if (xisinf (val) || xisnan (val)) |
|
1963 return true; |
|
1964 } |
|
1965 |
|
1966 return false; |
|
1967 } |
|
1968 |
2408
|
1969 // Return true if no elements have imaginary components. |
|
1970 |
|
1971 bool |
|
1972 ComplexMatrix::all_elements_are_real (void) const |
|
1973 { |
|
1974 int nr = rows (); |
|
1975 int nc = cols (); |
|
1976 |
|
1977 for (int j = 0; j < nc; j++) |
|
1978 for (int i = 0; i < nr; i++) |
|
1979 if (imag (elem (i, j)) != 0.0) |
|
1980 return false; |
|
1981 |
|
1982 return true; |
|
1983 } |
|
1984 |
1968
|
1985 // Return nonzero if any element of CM has a non-integer real or |
|
1986 // imaginary part. Also extract the largest and smallest (real or |
|
1987 // imaginary) values and return them in MAX_VAL and MIN_VAL. |
|
1988 |
2384
|
1989 bool |
1968
|
1990 ComplexMatrix::all_integers (double& max_val, double& min_val) const |
|
1991 { |
|
1992 int nr = rows (); |
2384
|
1993 int nc = cols (); |
1968
|
1994 |
|
1995 if (nr > 0 && nc > 0) |
|
1996 { |
|
1997 Complex val = elem (0, 0); |
|
1998 |
|
1999 double r_val = real (val); |
|
2000 double i_val = imag (val); |
|
2001 |
|
2002 max_val = r_val; |
|
2003 min_val = r_val; |
|
2004 |
|
2005 if (i_val > max_val) |
|
2006 max_val = i_val; |
|
2007 |
|
2008 if (i_val < max_val) |
|
2009 min_val = i_val; |
|
2010 } |
|
2011 else |
2384
|
2012 return false; |
1968
|
2013 |
|
2014 for (int j = 0; j < nc; j++) |
|
2015 for (int i = 0; i < nr; i++) |
|
2016 { |
|
2017 Complex val = elem (i, j); |
|
2018 |
|
2019 double r_val = real (val); |
|
2020 double i_val = imag (val); |
|
2021 |
|
2022 if (r_val > max_val) |
|
2023 max_val = r_val; |
|
2024 |
|
2025 if (i_val > max_val) |
|
2026 max_val = i_val; |
|
2027 |
|
2028 if (r_val < min_val) |
|
2029 min_val = r_val; |
|
2030 |
|
2031 if (i_val < min_val) |
|
2032 min_val = i_val; |
|
2033 |
|
2034 if (D_NINT (r_val) != r_val || D_NINT (i_val) != i_val) |
2384
|
2035 return false; |
1968
|
2036 } |
2384
|
2037 |
|
2038 return true; |
1968
|
2039 } |
|
2040 |
2384
|
2041 bool |
1968
|
2042 ComplexMatrix::too_large_for_float (void) const |
|
2043 { |
|
2044 int nr = rows (); |
2384
|
2045 int nc = cols (); |
1968
|
2046 |
|
2047 for (int j = 0; j < nc; j++) |
|
2048 for (int i = 0; i < nr; i++) |
|
2049 { |
|
2050 Complex val = elem (i, j); |
|
2051 |
|
2052 double r_val = real (val); |
|
2053 double i_val = imag (val); |
|
2054 |
|
2055 if (r_val > FLT_MAX |
|
2056 || i_val > FLT_MAX |
|
2057 || r_val < FLT_MIN |
|
2058 || i_val < FLT_MIN) |
2384
|
2059 return true; |
1968
|
2060 } |
|
2061 |
2384
|
2062 return false; |
1968
|
2063 } |
|
2064 |
2832
|
2065 boolMatrix |
458
|
2066 ComplexMatrix::all (void) const |
|
2067 { |
|
2068 int nr = rows (); |
|
2069 int nc = cols (); |
2832
|
2070 boolMatrix retval; |
458
|
2071 if (nr > 0 && nc > 0) |
|
2072 { |
|
2073 if (nr == 1) |
|
2074 { |
|
2075 retval.resize (1, 1); |
2832
|
2076 retval.elem (0, 0) = true; |
458
|
2077 for (int j = 0; j < nc; j++) |
|
2078 { |
|
2079 if (elem (0, j) == 0.0) |
|
2080 { |
2832
|
2081 retval.elem (0, 0) = false; |
458
|
2082 break; |
|
2083 } |
|
2084 } |
|
2085 } |
|
2086 else if (nc == 1) |
|
2087 { |
|
2088 retval.resize (1, 1); |
2832
|
2089 retval.elem (0, 0) = true; |
458
|
2090 for (int i = 0; i < nr; i++) |
|
2091 { |
|
2092 if (elem (i, 0) == 0.0) |
|
2093 { |
2832
|
2094 retval.elem (0, 0) = false; |
458
|
2095 break; |
|
2096 } |
|
2097 } |
|
2098 } |
|
2099 else |
|
2100 { |
|
2101 retval.resize (1, nc); |
|
2102 for (int j = 0; j < nc; j++) |
|
2103 { |
2832
|
2104 retval.elem (0, j) = true; |
458
|
2105 for (int i = 0; i < nr; i++) |
|
2106 { |
|
2107 if (elem (i, j) == 0.0) |
|
2108 { |
2832
|
2109 retval.elem (0, j) = false; |
458
|
2110 break; |
|
2111 } |
|
2112 } |
|
2113 } |
|
2114 } |
|
2115 } |
|
2116 return retval; |
|
2117 } |
|
2118 |
2832
|
2119 boolMatrix |
458
|
2120 ComplexMatrix::any (void) const |
|
2121 { |
|
2122 int nr = rows (); |
|
2123 int nc = cols (); |
2832
|
2124 boolMatrix retval; |
458
|
2125 if (nr > 0 && nc > 0) |
|
2126 { |
|
2127 if (nr == 1) |
|
2128 { |
|
2129 retval.resize (1, 1); |
2832
|
2130 retval.elem (0, 0) = false; |
458
|
2131 for (int j = 0; j < nc; j++) |
|
2132 { |
|
2133 if (elem (0, j) != 0.0) |
|
2134 { |
2832
|
2135 retval.elem (0, 0) = true; |
458
|
2136 break; |
|
2137 } |
|
2138 } |
|
2139 } |
|
2140 else if (nc == 1) |
|
2141 { |
|
2142 retval.resize (1, 1); |
2832
|
2143 retval.elem (0, 0) = false; |
458
|
2144 for (int i = 0; i < nr; i++) |
|
2145 { |
|
2146 if (elem (i, 0) != 0.0) |
|
2147 { |
2832
|
2148 retval.elem (0, 0) = true; |
458
|
2149 break; |
|
2150 } |
|
2151 } |
|
2152 } |
|
2153 else |
|
2154 { |
|
2155 retval.resize (1, nc); |
|
2156 for (int j = 0; j < nc; j++) |
|
2157 { |
2832
|
2158 retval.elem (0, j) = false; |
458
|
2159 for (int i = 0; i < nr; i++) |
|
2160 { |
|
2161 if (elem (i, j) != 0.0) |
|
2162 { |
2832
|
2163 retval.elem (0, j) = true; |
458
|
2164 break; |
|
2165 } |
|
2166 } |
|
2167 } |
|
2168 } |
|
2169 } |
|
2170 return retval; |
|
2171 } |
|
2172 |
|
2173 ComplexMatrix |
|
2174 ComplexMatrix::cumprod (void) const |
|
2175 { |
|
2176 int nr = rows (); |
|
2177 int nc = cols (); |
|
2178 ComplexMatrix retval; |
|
2179 if (nr > 0 && nc > 0) |
|
2180 { |
|
2181 if (nr == 1) |
|
2182 { |
|
2183 retval.resize (1, nc); |
|
2184 Complex prod = elem (0, 0); |
|
2185 for (int j = 0; j < nc; j++) |
|
2186 { |
|
2187 retval.elem (0, j) = prod; |
|
2188 if (j < nc - 1) |
|
2189 prod *= elem (0, j+1); |
|
2190 } |
|
2191 } |
|
2192 else if (nc == 1) |
|
2193 { |
|
2194 retval.resize (nr, 1); |
|
2195 Complex prod = elem (0, 0); |
|
2196 for (int i = 0; i < nr; i++) |
|
2197 { |
|
2198 retval.elem (i, 0) = prod; |
|
2199 if (i < nr - 1) |
|
2200 prod *= elem (i+1, 0); |
|
2201 } |
|
2202 } |
|
2203 else |
|
2204 { |
|
2205 retval.resize (nr, nc); |
|
2206 for (int j = 0; j < nc; j++) |
|
2207 { |
|
2208 Complex prod = elem (0, j); |
|
2209 for (int i = 0; i < nr; i++) |
|
2210 { |
|
2211 retval.elem (i, j) = prod; |
|
2212 if (i < nr - 1) |
|
2213 prod *= elem (i+1, j); |
|
2214 } |
|
2215 } |
|
2216 } |
|
2217 } |
|
2218 return retval; |
|
2219 } |
|
2220 |
|
2221 ComplexMatrix |
|
2222 ComplexMatrix::cumsum (void) const |
|
2223 { |
|
2224 int nr = rows (); |
|
2225 int nc = cols (); |
|
2226 ComplexMatrix retval; |
|
2227 if (nr > 0 && nc > 0) |
|
2228 { |
|
2229 if (nr == 1) |
|
2230 { |
|
2231 retval.resize (1, nc); |
|
2232 Complex sum = elem (0, 0); |
|
2233 for (int j = 0; j < nc; j++) |
|
2234 { |
|
2235 retval.elem (0, j) = sum; |
|
2236 if (j < nc - 1) |
|
2237 sum += elem (0, j+1); |
|
2238 } |
|
2239 } |
|
2240 else if (nc == 1) |
|
2241 { |
|
2242 retval.resize (nr, 1); |
|
2243 Complex sum = elem (0, 0); |
|
2244 for (int i = 0; i < nr; i++) |
|
2245 { |
|
2246 retval.elem (i, 0) = sum; |
|
2247 if (i < nr - 1) |
|
2248 sum += elem (i+1, 0); |
|
2249 } |
|
2250 } |
|
2251 else |
|
2252 { |
|
2253 retval.resize (nr, nc); |
|
2254 for (int j = 0; j < nc; j++) |
|
2255 { |
|
2256 Complex sum = elem (0, j); |
|
2257 for (int i = 0; i < nr; i++) |
|
2258 { |
|
2259 retval.elem (i, j) = sum; |
|
2260 if (i < nr - 1) |
|
2261 sum += elem (i+1, j); |
|
2262 } |
|
2263 } |
|
2264 } |
|
2265 } |
|
2266 return retval; |
|
2267 } |
|
2268 |
|
2269 ComplexMatrix |
|
2270 ComplexMatrix::prod (void) const |
|
2271 { |
|
2272 int nr = rows (); |
|
2273 int nc = cols (); |
|
2274 ComplexMatrix retval; |
|
2275 if (nr > 0 && nc > 0) |
|
2276 { |
|
2277 if (nr == 1) |
|
2278 { |
|
2279 retval.resize (1, 1); |
|
2280 retval.elem (0, 0) = 1.0; |
|
2281 for (int j = 0; j < nc; j++) |
|
2282 retval.elem (0, 0) *= elem (0, j); |
|
2283 } |
|
2284 else if (nc == 1) |
|
2285 { |
|
2286 retval.resize (1, 1); |
|
2287 retval.elem (0, 0) = 1.0; |
|
2288 for (int i = 0; i < nr; i++) |
|
2289 retval.elem (0, 0) *= elem (i, 0); |
|
2290 } |
|
2291 else |
|
2292 { |
|
2293 retval.resize (1, nc); |
|
2294 for (int j = 0; j < nc; j++) |
|
2295 { |
|
2296 retval.elem (0, j) = 1.0; |
|
2297 for (int i = 0; i < nr; i++) |
|
2298 retval.elem (0, j) *= elem (i, j); |
|
2299 } |
|
2300 } |
|
2301 } |
|
2302 return retval; |
|
2303 } |
|
2304 |
|
2305 ComplexMatrix |
|
2306 ComplexMatrix::sum (void) const |
|
2307 { |
|
2308 int nr = rows (); |
|
2309 int nc = cols (); |
|
2310 ComplexMatrix retval; |
|
2311 if (nr > 0 && nc > 0) |
|
2312 { |
|
2313 if (nr == 1) |
|
2314 { |
|
2315 retval.resize (1, 1); |
|
2316 retval.elem (0, 0) = 0.0; |
|
2317 for (int j = 0; j < nc; j++) |
|
2318 retval.elem (0, 0) += elem (0, j); |
|
2319 } |
|
2320 else if (nc == 1) |
|
2321 { |
|
2322 retval.resize (1, 1); |
|
2323 retval.elem (0, 0) = 0.0; |
|
2324 for (int i = 0; i < nr; i++) |
|
2325 retval.elem (0, 0) += elem (i, 0); |
|
2326 } |
|
2327 else |
|
2328 { |
|
2329 retval.resize (1, nc); |
|
2330 for (int j = 0; j < nc; j++) |
|
2331 { |
|
2332 retval.elem (0, j) = 0.0; |
|
2333 for (int i = 0; i < nr; i++) |
|
2334 retval.elem (0, j) += elem (i, j); |
|
2335 } |
|
2336 } |
|
2337 } |
|
2338 return retval; |
|
2339 } |
|
2340 |
|
2341 ComplexMatrix |
|
2342 ComplexMatrix::sumsq (void) const |
|
2343 { |
|
2344 int nr = rows (); |
|
2345 int nc = cols (); |
|
2346 ComplexMatrix retval; |
|
2347 if (nr > 0 && nc > 0) |
|
2348 { |
|
2349 if (nr == 1) |
|
2350 { |
|
2351 retval.resize (1, 1); |
|
2352 retval.elem (0, 0) = 0.0; |
|
2353 for (int j = 0; j < nc; j++) |
|
2354 { |
|
2355 Complex d = elem (0, j); |
|
2356 retval.elem (0, 0) += d * d; |
|
2357 } |
|
2358 } |
|
2359 else if (nc == 1) |
|
2360 { |
|
2361 retval.resize (1, 1); |
|
2362 retval.elem (0, 0) = 0.0; |
|
2363 for (int i = 0; i < nr; i++) |
|
2364 { |
|
2365 Complex d = elem (i, 0); |
|
2366 retval.elem (0, 0) += d * d; |
|
2367 } |
|
2368 } |
|
2369 else |
|
2370 { |
|
2371 retval.resize (1, nc); |
|
2372 for (int j = 0; j < nc; j++) |
|
2373 { |
|
2374 retval.elem (0, j) = 0.0; |
|
2375 for (int i = 0; i < nr; i++) |
|
2376 { |
|
2377 Complex d = elem (i, j); |
|
2378 retval.elem (0, j) += d * d; |
|
2379 } |
|
2380 } |
|
2381 } |
|
2382 } |
|
2383 return retval; |
|
2384 } |
|
2385 |
|
2386 ComplexColumnVector |
|
2387 ComplexMatrix::diag (void) const |
|
2388 { |
|
2389 return diag (0); |
|
2390 } |
|
2391 |
|
2392 ComplexColumnVector |
|
2393 ComplexMatrix::diag (int k) const |
|
2394 { |
|
2395 int nnr = rows (); |
|
2396 int nnc = cols (); |
|
2397 if (k > 0) |
|
2398 nnc -= k; |
|
2399 else if (k < 0) |
|
2400 nnr += k; |
|
2401 |
|
2402 ComplexColumnVector d; |
|
2403 |
|
2404 if (nnr > 0 && nnc > 0) |
|
2405 { |
|
2406 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
2407 |
|
2408 d.resize (ndiag); |
|
2409 |
|
2410 if (k > 0) |
|
2411 { |
|
2412 for (int i = 0; i < ndiag; i++) |
|
2413 d.elem (i) = elem (i, i+k); |
|
2414 } |
|
2415 else if ( k < 0) |
|
2416 { |
|
2417 for (int i = 0; i < ndiag; i++) |
|
2418 d.elem (i) = elem (i-k, i); |
|
2419 } |
|
2420 else |
|
2421 { |
|
2422 for (int i = 0; i < ndiag; i++) |
|
2423 d.elem (i) = elem (i, i); |
|
2424 } |
|
2425 } |
|
2426 else |
|
2427 cerr << "diag: requested diagonal out of range\n"; |
|
2428 |
|
2429 return d; |
|
2430 } |
|
2431 |
2354
|
2432 bool |
|
2433 ComplexMatrix::row_is_real_only (int i) const |
|
2434 { |
|
2435 bool retval = true; |
|
2436 |
|
2437 int nc = columns (); |
|
2438 |
|
2439 for (int j = 0; j < nc; j++) |
|
2440 { |
|
2441 if (imag (elem (i, j)) != 0.0) |
|
2442 { |
|
2443 retval = false; |
|
2444 break; |
|
2445 } |
|
2446 } |
|
2447 |
|
2448 return retval; |
|
2449 } |
|
2450 |
|
2451 bool |
|
2452 ComplexMatrix::column_is_real_only (int j) const |
|
2453 { |
|
2454 bool retval = true; |
|
2455 |
|
2456 int nr = rows (); |
|
2457 |
|
2458 for (int i = 0; i < nr; i++) |
|
2459 { |
|
2460 if (imag (elem (i, j)) != 0.0) |
|
2461 { |
|
2462 retval = false; |
|
2463 break; |
|
2464 } |
|
2465 } |
|
2466 |
|
2467 return retval; |
|
2468 } |
891
|
2469 |
458
|
2470 ComplexColumnVector |
|
2471 ComplexMatrix::row_min (void) const |
|
2472 { |
2354
|
2473 Array<int> index; |
|
2474 return row_min (index); |
458
|
2475 } |
|
2476 |
|
2477 ComplexColumnVector |
2354
|
2478 ComplexMatrix::row_min (Array<int>& index) const |
458
|
2479 { |
|
2480 ComplexColumnVector result; |
|
2481 |
|
2482 int nr = rows (); |
|
2483 int nc = cols (); |
|
2484 |
|
2485 if (nr > 0 && nc > 0) |
|
2486 { |
|
2487 result.resize (nr); |
2354
|
2488 index.resize (nr); |
458
|
2489 |
|
2490 for (int i = 0; i < nr; i++) |
|
2491 { |
2354
|
2492 int idx = 0; |
|
2493 |
|
2494 Complex tmp_min = elem (i, idx); |
|
2495 |
|
2496 bool real_only = row_is_real_only (i); |
|
2497 |
|
2498 double abs_min = real_only ? real (tmp_min) : abs (tmp_min); |
|
2499 |
|
2500 if (xisnan (tmp_min)) |
|
2501 idx = -1; |
891
|
2502 else |
|
2503 { |
|
2504 for (int j = 1; j < nc; j++) |
2354
|
2505 { |
|
2506 Complex tmp = elem (i, j); |
|
2507 |
|
2508 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
2509 |
|
2510 if (xisnan (tmp)) |
|
2511 { |
|
2512 idx = -1; |
|
2513 break; |
|
2514 } |
|
2515 else if (abs_tmp < abs_min) |
|
2516 { |
|
2517 idx = j; |
|
2518 tmp_min = tmp; |
|
2519 abs_min = abs_tmp; |
|
2520 } |
|
2521 } |
|
2522 |
|
2523 result.elem (i) = (idx < 0) ? Complex_NaN_result : tmp_min; |
|
2524 index.elem (i) = idx; |
891
|
2525 } |
458
|
2526 } |
|
2527 } |
|
2528 |
|
2529 return result; |
|
2530 } |
|
2531 |
|
2532 ComplexColumnVector |
|
2533 ComplexMatrix::row_max (void) const |
|
2534 { |
2354
|
2535 Array<int> index; |
|
2536 return row_max (index); |
458
|
2537 } |
|
2538 |
|
2539 ComplexColumnVector |
2354
|
2540 ComplexMatrix::row_max (Array<int>& index) const |
458
|
2541 { |
|
2542 ComplexColumnVector result; |
|
2543 |
|
2544 int nr = rows (); |
|
2545 int nc = cols (); |
|
2546 |
|
2547 if (nr > 0 && nc > 0) |
|
2548 { |
|
2549 result.resize (nr); |
2354
|
2550 index.resize (nr); |
458
|
2551 |
|
2552 for (int i = 0; i < nr; i++) |
|
2553 { |
2354
|
2554 int idx = 0; |
|
2555 |
|
2556 Complex tmp_max = elem (i, idx); |
|
2557 |
|
2558 bool real_only = row_is_real_only (i); |
|
2559 |
|
2560 double abs_max = real_only ? real (tmp_max) : abs (tmp_max); |
|
2561 |
|
2562 if (xisnan (tmp_max)) |
|
2563 idx = -1; |
891
|
2564 else |
|
2565 { |
|
2566 for (int j = 1; j < nc; j++) |
2354
|
2567 { |
|
2568 Complex tmp = elem (i, j); |
|
2569 |
|
2570 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
2571 |
|
2572 if (xisnan (tmp)) |
|
2573 { |
|
2574 idx = -1; |
|
2575 break; |
|
2576 } |
|
2577 else if (abs_tmp > abs_max) |
|
2578 { |
|
2579 idx = j; |
|
2580 tmp_max = tmp; |
|
2581 abs_max = abs_tmp; |
|
2582 } |
|
2583 } |
|
2584 |
|
2585 result.elem (i) = (idx < 0) ? Complex_NaN_result : tmp_max; |
|
2586 index.elem (i) = idx; |
891
|
2587 } |
458
|
2588 } |
|
2589 } |
|
2590 |
|
2591 return result; |
|
2592 } |
|
2593 |
|
2594 ComplexRowVector |
|
2595 ComplexMatrix::column_min (void) const |
|
2596 { |
2354
|
2597 Array<int> index; |
|
2598 return column_min (index); |
458
|
2599 } |
|
2600 |
|
2601 ComplexRowVector |
2354
|
2602 ComplexMatrix::column_min (Array<int>& index) const |
458
|
2603 { |
|
2604 ComplexRowVector result; |
|
2605 |
|
2606 int nr = rows (); |
|
2607 int nc = cols (); |
|
2608 |
|
2609 if (nr > 0 && nc > 0) |
|
2610 { |
|
2611 result.resize (nc); |
2354
|
2612 index.resize (nc); |
458
|
2613 |
|
2614 for (int j = 0; j < nc; j++) |
|
2615 { |
2354
|
2616 int idx = 0; |
|
2617 |
|
2618 Complex tmp_min = elem (idx, j); |
|
2619 |
|
2620 bool real_only = column_is_real_only (j); |
|
2621 |
|
2622 double abs_min = real_only ? real (tmp_min) : abs (tmp_min); |
|
2623 |
|
2624 if (xisnan (tmp_min)) |
|
2625 idx = -1; |
891
|
2626 else |
|
2627 { |
|
2628 for (int i = 1; i < nr; i++) |
2354
|
2629 { |
|
2630 Complex tmp = elem (i, j); |
|
2631 |
|
2632 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
2633 |
|
2634 if (xisnan (tmp)) |
|
2635 { |
|
2636 idx = -1; |
|
2637 break; |
|
2638 } |
|
2639 else if (abs_tmp < abs_min) |
|
2640 { |
|
2641 idx = i; |
|
2642 tmp_min = tmp; |
|
2643 abs_min = abs_tmp; |
|
2644 } |
|
2645 } |
|
2646 |
|
2647 result.elem (j) = (idx < 0) ? Complex_NaN_result : tmp_min; |
|
2648 index.elem (j) = idx; |
891
|
2649 } |
458
|
2650 } |
|
2651 } |
|
2652 |
|
2653 return result; |
|
2654 } |
|
2655 |
|
2656 ComplexRowVector |
|
2657 ComplexMatrix::column_max (void) const |
|
2658 { |
2354
|
2659 Array<int> index; |
|
2660 return column_max (index); |
458
|
2661 } |
|
2662 |
|
2663 ComplexRowVector |
2354
|
2664 ComplexMatrix::column_max (Array<int>& index) const |
458
|
2665 { |
|
2666 ComplexRowVector result; |
|
2667 |
|
2668 int nr = rows (); |
|
2669 int nc = cols (); |
|
2670 |
|
2671 if (nr > 0 && nc > 0) |
|
2672 { |
|
2673 result.resize (nc); |
2354
|
2674 index.resize (nc); |
458
|
2675 |
|
2676 for (int j = 0; j < nc; j++) |
|
2677 { |
2354
|
2678 int idx = 0; |
|
2679 |
|
2680 Complex tmp_max = elem (idx, j); |
|
2681 |
|
2682 bool real_only = column_is_real_only (j); |
|
2683 |
|
2684 double abs_max = real_only ? real (tmp_max) : abs (tmp_max); |
|
2685 |
|
2686 if (xisnan (tmp_max)) |
|
2687 idx = -1; |
891
|
2688 else |
|
2689 { |
|
2690 for (int i = 1; i < nr; i++) |
2354
|
2691 { |
|
2692 Complex tmp = elem (i, j); |
|
2693 |
|
2694 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
2695 |
|
2696 if (xisnan (tmp)) |
|
2697 { |
|
2698 idx = -1; |
|
2699 break; |
|
2700 } |
|
2701 else if (abs_tmp > abs_max) |
|
2702 { |
|
2703 idx = i; |
|
2704 tmp_max = tmp; |
|
2705 abs_max = abs_tmp; |
|
2706 } |
|
2707 } |
|
2708 |
|
2709 result.elem (j) = (idx < 0) ? Complex_NaN_result : tmp_max; |
|
2710 index.elem (j) = idx; |
891
|
2711 } |
458
|
2712 } |
|
2713 } |
|
2714 |
|
2715 return result; |
|
2716 } |
|
2717 |
|
2718 // i/o |
|
2719 |
|
2720 ostream& |
|
2721 operator << (ostream& os, const ComplexMatrix& a) |
|
2722 { |
|
2723 // int field_width = os.precision () + 7; |
|
2724 for (int i = 0; i < a.rows (); i++) |
|
2725 { |
|
2726 for (int j = 0; j < a.cols (); j++) |
|
2727 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
2728 os << "\n"; |
|
2729 } |
|
2730 return os; |
|
2731 } |
|
2732 |
|
2733 istream& |
|
2734 operator >> (istream& is, ComplexMatrix& a) |
|
2735 { |
|
2736 int nr = a.rows (); |
|
2737 int nc = a.cols (); |
|
2738 |
|
2739 if (nr < 1 || nc < 1) |
|
2740 is.clear (ios::badbit); |
|
2741 else |
|
2742 { |
|
2743 Complex tmp; |
|
2744 for (int i = 0; i < nr; i++) |
|
2745 for (int j = 0; j < nc; j++) |
|
2746 { |
|
2747 is >> tmp; |
|
2748 if (is) |
|
2749 a.elem (i, j) = tmp; |
|
2750 else |
2798
|
2751 return is; |
458
|
2752 } |
|
2753 } |
|
2754 |
|
2755 return is; |
|
2756 } |
|
2757 |
1819
|
2758 ComplexMatrix |
|
2759 Givens (const Complex& x, const Complex& y) |
|
2760 { |
|
2761 double cc; |
|
2762 Complex cs, temp_r; |
|
2763 |
|
2764 F77_FCN (zlartg, ZLARTG) (x, y, cc, cs, temp_r); |
|
2765 |
|
2766 ComplexMatrix g (2, 2); |
|
2767 |
|
2768 g.elem (0, 0) = cc; |
|
2769 g.elem (1, 1) = cc; |
|
2770 g.elem (0, 1) = cs; |
|
2771 g.elem (1, 0) = -conj (cs); |
|
2772 |
|
2773 return g; |
|
2774 } |
|
2775 |
|
2776 ComplexMatrix |
|
2777 Sylvester (const ComplexMatrix& a, const ComplexMatrix& b, |
|
2778 const ComplexMatrix& c) |
|
2779 { |
|
2780 ComplexMatrix retval; |
|
2781 |
|
2782 // XXX FIXME XXX -- need to check that a, b, and c are all the same |
|
2783 // size. |
|
2784 |
|
2785 // Compute Schur decompositions |
|
2786 |
|
2787 ComplexSCHUR as (a, "U"); |
|
2788 ComplexSCHUR bs (b, "U"); |
|
2789 |
|
2790 // Transform c to new coordinates. |
|
2791 |
|
2792 ComplexMatrix ua = as.unitary_matrix (); |
|
2793 ComplexMatrix sch_a = as.schur_matrix (); |
|
2794 |
|
2795 ComplexMatrix ub = bs.unitary_matrix (); |
|
2796 ComplexMatrix sch_b = bs.schur_matrix (); |
|
2797 |
|
2798 ComplexMatrix cx = ua.hermitian () * c * ub; |
|
2799 |
|
2800 // Solve the sylvester equation, back-transform, and return the |
|
2801 // solution. |
|
2802 |
|
2803 int a_nr = a.rows (); |
|
2804 int b_nr = b.rows (); |
|
2805 |
|
2806 double scale; |
|
2807 int info; |
1950
|
2808 |
|
2809 Complex *pa = sch_a.fortran_vec (); |
|
2810 Complex *pb = sch_b.fortran_vec (); |
|
2811 Complex *px = cx.fortran_vec (); |
1819
|
2812 |
1950
|
2813 F77_XFCN (ztrsyl, ZTRSYL, ("N", "N", 1, a_nr, b_nr, pa, a_nr, pb, |
|
2814 b_nr, px, a_nr, scale, |
|
2815 info, 1L, 1L)); |
|
2816 |
|
2817 if (f77_exception_encountered) |
|
2818 (*current_liboctave_error_handler) ("unrecoverable error in ztrsyl"); |
|
2819 else |
|
2820 { |
|
2821 // XXX FIXME XXX -- check info? |
|
2822 |
|
2823 retval = -ua * cx * ub.hermitian (); |
|
2824 } |
1819
|
2825 |
|
2826 return retval; |
|
2827 } |
|
2828 |
2828
|
2829 ComplexMatrix |
|
2830 operator * (const ComplexMatrix& m, const Matrix& a) |
|
2831 { |
|
2832 ComplexMatrix tmp (a); |
|
2833 return m * tmp; |
|
2834 } |
|
2835 |
|
2836 ComplexMatrix |
|
2837 operator * (const Matrix& m, const ComplexMatrix& a) |
|
2838 { |
|
2839 ComplexMatrix tmp (m); |
|
2840 return tmp * a; |
|
2841 } |
|
2842 |
|
2843 ComplexMatrix |
|
2844 operator * (const ComplexMatrix& m, const ComplexMatrix& a) |
|
2845 { |
|
2846 ComplexMatrix retval; |
|
2847 |
|
2848 int nr = m.rows (); |
|
2849 int nc = m.cols (); |
|
2850 |
|
2851 int a_nr = a.rows (); |
|
2852 int a_nc = a.cols (); |
|
2853 |
|
2854 if (nc != a_nr) |
|
2855 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
|
2856 else |
|
2857 { |
|
2858 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2859 retval.resize (nr, nc, 0.0); |
|
2860 else |
|
2861 { |
|
2862 int ld = nr; |
|
2863 int lda = a.rows (); |
|
2864 |
|
2865 retval.resize (nr, a_nc); |
|
2866 Complex *c = retval.fortran_vec (); |
|
2867 |
|
2868 F77_XFCN (zgemm, ZGEMM, ("N", "N", nr, a_nc, nc, 1.0, |
|
2869 m.data (), ld, a.data (), lda, 0.0, |
|
2870 c, nr, 1L, 1L)); |
|
2871 |
|
2872 if (f77_exception_encountered) |
|
2873 (*current_liboctave_error_handler) |
|
2874 ("unrecoverable error in zgemm"); |
|
2875 } |
|
2876 } |
|
2877 |
|
2878 return retval; |
|
2879 } |
|
2880 |
458
|
2881 /* |
|
2882 ;;; Local Variables: *** |
|
2883 ;;; mode: C++ *** |
|
2884 ;;; End: *** |
|
2885 */ |