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