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