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