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++) |
458
|
1003 tmp_data[j] = tmp_data[j] / (double) npts; |
|
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++) |
677
|
1103 tmp_data[j] = tmp_data[j] / (double) npts; |
|
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++) |
1948
|
1125 tmp_data[i*nr + j] = prow[i] / (double) 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 |
|
1582 int sqpow = (int) (inf_norm > 0.0 |
|
1583 ? (1.0 + log (inf_norm) / log (2.0)) |
|
1584 : 0.0); |
|
1585 |
|
1586 // Check whether we need to square at all. |
|
1587 |
|
1588 if (sqpow < 0) |
|
1589 sqpow = 0; |
|
1590 |
|
1591 if (sqpow > 0) |
|
1592 { |
|
1593 double scale_factor = 1.0; |
|
1594 for (int i = 0; i < sqpow; i++) |
|
1595 scale_factor *= 2.0; |
|
1596 |
|
1597 m = m / scale_factor; |
|
1598 } |
|
1599 |
|
1600 // npp, dpp: pade' approx polynomial matrices. |
|
1601 |
|
1602 ComplexMatrix npp (nc, nc, 0.0); |
|
1603 ComplexMatrix dpp = npp; |
|
1604 |
|
1605 // Now powers a^8 ... a^1. |
|
1606 |
|
1607 int minus_one_j = -1; |
|
1608 for (int j = 7; j >= 0; j--) |
|
1609 { |
|
1610 npp = m * npp + m * padec[j]; |
|
1611 dpp = m * dpp + m * (minus_one_j * padec[j]); |
|
1612 minus_one_j *= -1; |
|
1613 } |
|
1614 |
|
1615 // Zero power. |
|
1616 |
|
1617 dpp = -dpp; |
|
1618 for (int j = 0; j < nc; j++) |
|
1619 { |
|
1620 npp.elem (j, j) += 1.0; |
|
1621 dpp.elem (j, j) += 1.0; |
|
1622 } |
|
1623 |
|
1624 // Compute pade approximation = inverse (dpp) * npp. |
|
1625 |
|
1626 retval = dpp.solve (npp); |
|
1627 |
|
1628 // Reverse preconditioning step 3: repeated squaring. |
|
1629 |
|
1630 while (sqpow) |
|
1631 { |
|
1632 retval = retval * retval; |
|
1633 sqpow--; |
|
1634 } |
|
1635 |
|
1636 // Reverse preconditioning step 2: inverse balancing. |
|
1637 // XXX FIXME XXX -- should probably do this with Lapack calls |
|
1638 // instead of a complete matrix inversion. |
|
1639 |
|
1640 retval = retval.transpose (); |
|
1641 d = d.transpose (); |
|
1642 retval = retval * d; |
|
1643 retval = d.solve (retval); |
|
1644 retval = retval.transpose (); |
|
1645 |
|
1646 // Reverse preconditioning step 1: fix trace normalization. |
|
1647 |
|
1648 return retval * exp (trshift); |
|
1649 } |
|
1650 |
1205
|
1651 // column vector by row vector -> matrix operations |
|
1652 |
|
1653 ComplexMatrix |
|
1654 operator * (const ColumnVector& v, const ComplexRowVector& a) |
|
1655 { |
|
1656 ComplexColumnVector tmp (v); |
|
1657 return tmp * a; |
|
1658 } |
|
1659 |
|
1660 ComplexMatrix |
|
1661 operator * (const ComplexColumnVector& a, const RowVector& b) |
|
1662 { |
|
1663 ComplexRowVector tmp (b); |
|
1664 return a * tmp; |
|
1665 } |
|
1666 |
|
1667 ComplexMatrix |
|
1668 operator * (const ComplexColumnVector& v, const ComplexRowVector& a) |
|
1669 { |
1948
|
1670 ComplexMatrix retval; |
|
1671 |
1205
|
1672 int len = v.length (); |
|
1673 int a_len = a.length (); |
1948
|
1674 |
1205
|
1675 if (len != a_len) |
2384
|
1676 gripe_nonconformant ("operator *", len, 1, 1, a_len); |
1948
|
1677 else |
1205
|
1678 { |
1948
|
1679 if (len != 0) |
|
1680 { |
|
1681 retval.resize (len, a_len); |
|
1682 Complex *c = retval.fortran_vec (); |
|
1683 |
|
1684 F77_XFCN (zgemm, ZGEMM, ("N", "N", len, a_len, 1, 1.0, |
|
1685 v.data (), len, a.data (), 1, 0.0, |
|
1686 c, len, 1L, 1L)); |
|
1687 |
|
1688 if (f77_exception_encountered) |
|
1689 (*current_liboctave_error_handler) |
|
1690 ("unrecoverable error in zgemm"); |
|
1691 } |
1205
|
1692 } |
|
1693 |
1948
|
1694 return retval; |
1205
|
1695 } |
|
1696 |
|
1697 // diagonal matrix by scalar -> matrix operations |
|
1698 |
|
1699 ComplexMatrix |
|
1700 operator + (const DiagMatrix& a, const Complex& s) |
|
1701 { |
|
1702 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1703 return a + tmp; |
|
1704 } |
|
1705 |
|
1706 ComplexMatrix |
|
1707 operator - (const DiagMatrix& a, const Complex& s) |
|
1708 { |
|
1709 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1710 return a + tmp; |
|
1711 } |
|
1712 |
|
1713 ComplexMatrix |
|
1714 operator + (const ComplexDiagMatrix& a, double s) |
|
1715 { |
|
1716 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1717 return a + tmp; |
|
1718 } |
|
1719 |
|
1720 ComplexMatrix |
|
1721 operator - (const ComplexDiagMatrix& a, double s) |
|
1722 { |
|
1723 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1724 return a + tmp; |
|
1725 } |
|
1726 |
|
1727 ComplexMatrix |
|
1728 operator + (const ComplexDiagMatrix& a, const Complex& s) |
|
1729 { |
|
1730 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1731 return a + tmp; |
|
1732 } |
|
1733 |
|
1734 ComplexMatrix |
|
1735 operator - (const ComplexDiagMatrix& a, const Complex& s) |
|
1736 { |
|
1737 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1738 return a + tmp; |
|
1739 } |
|
1740 |
|
1741 // scalar by diagonal matrix -> matrix operations |
|
1742 |
|
1743 ComplexMatrix |
|
1744 operator + (const Complex& s, const DiagMatrix& a) |
|
1745 { |
|
1746 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1747 return tmp + a; |
|
1748 } |
|
1749 |
|
1750 ComplexMatrix |
|
1751 operator - (const Complex& s, const DiagMatrix& a) |
|
1752 { |
|
1753 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1754 return tmp - a; |
|
1755 } |
|
1756 |
|
1757 ComplexMatrix |
|
1758 operator + (double s, const ComplexDiagMatrix& a) |
|
1759 { |
|
1760 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1761 return tmp + a; |
|
1762 } |
|
1763 |
|
1764 ComplexMatrix |
|
1765 operator - (double s, const ComplexDiagMatrix& a) |
|
1766 { |
|
1767 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1768 return tmp - a; |
|
1769 } |
|
1770 |
|
1771 ComplexMatrix |
|
1772 operator + (const Complex& s, const ComplexDiagMatrix& a) |
|
1773 { |
|
1774 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1775 return tmp + a; |
|
1776 } |
|
1777 |
|
1778 ComplexMatrix |
|
1779 operator - (const Complex& s, const ComplexDiagMatrix& a) |
|
1780 { |
|
1781 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1782 return tmp - a; |
|
1783 } |
|
1784 |
458
|
1785 // matrix by diagonal matrix -> matrix operations |
|
1786 |
|
1787 ComplexMatrix& |
|
1788 ComplexMatrix::operator += (const DiagMatrix& a) |
|
1789 { |
|
1790 int nr = rows (); |
|
1791 int nc = cols (); |
2384
|
1792 |
|
1793 int a_nr = rows (); |
|
1794 int a_nc = cols (); |
|
1795 |
|
1796 if (nr != a_nr || nc != a_nc) |
458
|
1797 { |
2384
|
1798 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889
|
1799 return *this; |
458
|
1800 } |
|
1801 |
|
1802 for (int i = 0; i < a.length (); i++) |
|
1803 elem (i, i) += a.elem (i, i); |
|
1804 |
|
1805 return *this; |
|
1806 } |
|
1807 |
|
1808 ComplexMatrix& |
|
1809 ComplexMatrix::operator -= (const DiagMatrix& a) |
|
1810 { |
|
1811 int nr = rows (); |
|
1812 int nc = cols (); |
2384
|
1813 |
|
1814 int a_nr = rows (); |
|
1815 int a_nc = cols (); |
|
1816 |
|
1817 if (nr != a_nr || nc != a_nc) |
458
|
1818 { |
2384
|
1819 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889
|
1820 return *this; |
458
|
1821 } |
|
1822 |
|
1823 for (int i = 0; i < a.length (); i++) |
|
1824 elem (i, i) -= a.elem (i, i); |
|
1825 |
|
1826 return *this; |
|
1827 } |
|
1828 |
|
1829 ComplexMatrix& |
|
1830 ComplexMatrix::operator += (const ComplexDiagMatrix& a) |
|
1831 { |
|
1832 int nr = rows (); |
|
1833 int nc = cols (); |
2384
|
1834 |
|
1835 int a_nr = rows (); |
|
1836 int a_nc = cols (); |
|
1837 |
|
1838 if (nr != a_nr || nc != a_nc) |
458
|
1839 { |
2384
|
1840 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889
|
1841 return *this; |
458
|
1842 } |
|
1843 |
|
1844 for (int i = 0; i < a.length (); i++) |
|
1845 elem (i, i) += a.elem (i, i); |
|
1846 |
|
1847 return *this; |
|
1848 } |
|
1849 |
|
1850 ComplexMatrix& |
|
1851 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) |
|
1852 { |
|
1853 int nr = rows (); |
|
1854 int nc = cols (); |
2384
|
1855 |
|
1856 int a_nr = rows (); |
|
1857 int a_nc = cols (); |
|
1858 |
|
1859 if (nr != a_nr || nc != a_nc) |
458
|
1860 { |
2384
|
1861 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889
|
1862 return *this; |
458
|
1863 } |
|
1864 |
|
1865 for (int i = 0; i < a.length (); i++) |
|
1866 elem (i, i) -= a.elem (i, i); |
|
1867 |
|
1868 return *this; |
|
1869 } |
|
1870 |
1205
|
1871 ComplexMatrix |
|
1872 operator + (const Matrix& m, const ComplexDiagMatrix& a) |
|
1873 { |
|
1874 int nr = m.rows (); |
|
1875 int nc = m.cols (); |
2384
|
1876 |
|
1877 int a_nr = a.rows (); |
|
1878 int a_nc = a.cols (); |
|
1879 |
|
1880 if (nr != a_nr || nc != a_nc) |
1205
|
1881 { |
2384
|
1882 gripe_nonconformant ("operator +", nr, nc, a_nr, a_nc); |
1205
|
1883 return ComplexMatrix (); |
|
1884 } |
|
1885 |
|
1886 if (nr == 0 || nc == 0) |
|
1887 return ComplexMatrix (nr, nc); |
|
1888 |
|
1889 ComplexMatrix result (m); |
|
1890 for (int i = 0; i < a.length (); i++) |
|
1891 result.elem (i, i) += a.elem (i, i); |
|
1892 |
|
1893 return result; |
|
1894 } |
|
1895 |
|
1896 ComplexMatrix |
|
1897 operator - (const Matrix& m, const ComplexDiagMatrix& a) |
|
1898 { |
|
1899 int nr = m.rows (); |
|
1900 int nc = m.cols (); |
2384
|
1901 |
|
1902 int a_nr = a.rows (); |
|
1903 int a_nc = a.cols (); |
|
1904 |
|
1905 if (nr != a_nr || nc != a_nc) |
1205
|
1906 { |
2384
|
1907 gripe_nonconformant ("operator -", nr, nc, a_nr, a_nc); |
1205
|
1908 return ComplexMatrix (); |
|
1909 } |
|
1910 |
|
1911 if (nr == 0 || nc == 0) |
|
1912 return ComplexMatrix (nr, nc); |
|
1913 |
|
1914 ComplexMatrix result (m); |
|
1915 for (int i = 0; i < a.length (); i++) |
|
1916 result.elem (i, i) -= a.elem (i, i); |
|
1917 |
|
1918 return result; |
|
1919 } |
|
1920 |
|
1921 ComplexMatrix |
|
1922 operator * (const Matrix& m, const ComplexDiagMatrix& a) |
|
1923 { |
1948
|
1924 ComplexMatrix retval; |
|
1925 |
1205
|
1926 int nr = m.rows (); |
|
1927 int nc = m.cols (); |
1948
|
1928 |
1205
|
1929 int a_nr = a.rows (); |
|
1930 int a_nc = a.cols (); |
1948
|
1931 |
1205
|
1932 if (nc != a_nr) |
2384
|
1933 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
1948
|
1934 else |
1205
|
1935 { |
1948
|
1936 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1937 retval.resize (nr, a_nc, 0.0); |
1205
|
1938 else |
|
1939 { |
1948
|
1940 retval.resize (nr, a_nc); |
|
1941 Complex *c = retval.fortran_vec (); |
|
1942 |
|
1943 Complex *ctmp = 0; |
|
1944 |
|
1945 for (int j = 0; j < a.length (); j++) |
|
1946 { |
|
1947 int idx = j * nr; |
|
1948 ctmp = c + idx; |
|
1949 if (a.elem (j, j) == 1.0) |
|
1950 { |
|
1951 for (int i = 0; i < nr; i++) |
|
1952 ctmp[i] = m.elem (i, j); |
|
1953 } |
|
1954 else if (a.elem (j, j) == 0.0) |
|
1955 { |
|
1956 for (int i = 0; i < nr; i++) |
|
1957 ctmp[i] = 0.0; |
|
1958 } |
|
1959 else |
|
1960 { |
|
1961 for (int i = 0; i < nr; i++) |
|
1962 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
1963 } |
|
1964 } |
|
1965 |
|
1966 if (a_nr < a_nc) |
|
1967 { |
|
1968 for (int i = nr * nc; i < nr * a_nc; i++) |
|
1969 ctmp[i] = 0.0; |
|
1970 } |
1205
|
1971 } |
|
1972 } |
|
1973 |
1948
|
1974 return retval; |
1205
|
1975 } |
|
1976 |
|
1977 // diagonal matrix by matrix -> matrix operations |
|
1978 |
|
1979 ComplexMatrix |
|
1980 operator + (const DiagMatrix& m, const ComplexMatrix& a) |
|
1981 { |
|
1982 int nr = m.rows (); |
|
1983 int nc = m.cols (); |
2384
|
1984 |
|
1985 int a_nr = a.rows (); |
|
1986 int a_nc = a.cols (); |
|
1987 |
|
1988 if (nr != a_nr || nc != a_nc) |
1205
|
1989 { |
2384
|
1990 gripe_nonconformant ("operator +", nr, nc, a_nr, a_nc); |
1205
|
1991 return ComplexMatrix (); |
|
1992 } |
|
1993 |
|
1994 if (nr == 0 || nc == 0) |
|
1995 return ComplexMatrix (nr, nc); |
|
1996 |
|
1997 ComplexMatrix result (a); |
|
1998 for (int i = 0; i < m.length (); i++) |
|
1999 result.elem (i, i) += m.elem (i, i); |
|
2000 |
|
2001 return result; |
|
2002 } |
|
2003 |
|
2004 ComplexMatrix |
|
2005 operator - (const DiagMatrix& m, const ComplexMatrix& a) |
|
2006 { |
|
2007 int nr = m.rows (); |
|
2008 int nc = m.cols (); |
2384
|
2009 |
|
2010 int a_nr = a.rows (); |
|
2011 int a_nc = a.cols (); |
|
2012 |
|
2013 if (nr != a_nr || nc != a_nc) |
1205
|
2014 { |
2384
|
2015 gripe_nonconformant ("operator -", nr, nc, a_nr, a_nc); |
1205
|
2016 return ComplexMatrix (); |
|
2017 } |
|
2018 |
|
2019 if (nr == 0 || nc == 0) |
|
2020 return ComplexMatrix (nr, nc); |
|
2021 |
|
2022 ComplexMatrix result (-a); |
|
2023 for (int i = 0; i < m.length (); i++) |
|
2024 result.elem (i, i) += m.elem (i, i); |
|
2025 |
|
2026 return result; |
|
2027 } |
|
2028 |
|
2029 ComplexMatrix |
|
2030 operator * (const DiagMatrix& m, const ComplexMatrix& a) |
|
2031 { |
|
2032 int nr = m.rows (); |
|
2033 int nc = m.cols (); |
2384
|
2034 |
1205
|
2035 int a_nr = a.rows (); |
|
2036 int a_nc = a.cols (); |
2384
|
2037 |
1205
|
2038 if (nc != a_nr) |
|
2039 { |
2384
|
2040 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
1205
|
2041 return ComplexMatrix (); |
|
2042 } |
|
2043 |
|
2044 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2045 return ComplexMatrix (nr, nc, 0.0); |
|
2046 |
|
2047 ComplexMatrix c (nr, a_nc); |
|
2048 |
|
2049 for (int i = 0; i < m.length (); i++) |
|
2050 { |
|
2051 if (m.elem (i, i) == 1.0) |
|
2052 { |
|
2053 for (int j = 0; j < a_nc; j++) |
|
2054 c.elem (i, j) = a.elem (i, j); |
|
2055 } |
|
2056 else if (m.elem (i, i) == 0.0) |
|
2057 { |
|
2058 for (int j = 0; j < a_nc; j++) |
|
2059 c.elem (i, j) = 0.0; |
|
2060 } |
|
2061 else |
|
2062 { |
|
2063 for (int j = 0; j < a_nc; j++) |
|
2064 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
2065 } |
|
2066 } |
|
2067 |
|
2068 if (nr > nc) |
|
2069 { |
|
2070 for (int j = 0; j < a_nc; j++) |
|
2071 for (int i = a_nr; i < nr; i++) |
|
2072 c.elem (i, j) = 0.0; |
|
2073 } |
|
2074 |
|
2075 return c; |
|
2076 } |
|
2077 |
|
2078 ComplexMatrix |
|
2079 operator + (const ComplexDiagMatrix& m, const Matrix& a) |
|
2080 { |
|
2081 int nr = m.rows (); |
|
2082 int nc = m.cols (); |
2384
|
2083 |
|
2084 int a_nr = a.rows (); |
|
2085 int a_nc = a.cols (); |
|
2086 |
|
2087 if (nr != a_nr || nc != a_nc) |
1205
|
2088 { |
2384
|
2089 gripe_nonconformant ("operator +", nr, nc, a_nr, a_nc); |
1205
|
2090 return ComplexMatrix (); |
|
2091 } |
|
2092 |
|
2093 if (nr == 0 || nc == 0) |
|
2094 return ComplexMatrix (nr, nc); |
|
2095 |
|
2096 ComplexMatrix result (a); |
|
2097 for (int i = 0; i < m.length (); i++) |
|
2098 result.elem (i, i) += m.elem (i, i); |
|
2099 |
|
2100 return result; |
|
2101 } |
|
2102 |
|
2103 ComplexMatrix |
|
2104 operator - (const ComplexDiagMatrix& m, const Matrix& a) |
|
2105 { |
|
2106 int nr = m.rows (); |
|
2107 int nc = m.cols (); |
2384
|
2108 |
|
2109 int a_nr = a.rows (); |
|
2110 int a_nc = a.cols (); |
|
2111 |
|
2112 if (nr != a_nr || nc != a_nc) |
1205
|
2113 { |
2384
|
2114 gripe_nonconformant ("operator -", nr, nc, a_nr, a_nc); |
1205
|
2115 return ComplexMatrix (); |
|
2116 } |
|
2117 |
|
2118 if (nr == 0 || nc == 0) |
|
2119 return ComplexMatrix (nr, nc); |
|
2120 |
|
2121 ComplexMatrix result (-a); |
|
2122 for (int i = 0; i < m.length (); i++) |
|
2123 result.elem (i, i) += m.elem (i, i); |
|
2124 |
|
2125 return result; |
|
2126 } |
|
2127 |
|
2128 ComplexMatrix |
|
2129 operator * (const ComplexDiagMatrix& m, const Matrix& a) |
|
2130 { |
|
2131 int nr = m.rows (); |
|
2132 int nc = m.cols (); |
2384
|
2133 |
1205
|
2134 int a_nr = a.rows (); |
|
2135 int a_nc = a.cols (); |
2384
|
2136 |
1205
|
2137 if (nc != a_nr) |
|
2138 { |
2384
|
2139 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
1205
|
2140 return ComplexMatrix (); |
|
2141 } |
|
2142 |
|
2143 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2144 return ComplexMatrix (nr, a_nc, 0.0); |
|
2145 |
|
2146 ComplexMatrix c (nr, a_nc); |
|
2147 |
|
2148 for (int i = 0; i < m.length (); i++) |
|
2149 { |
|
2150 if (m.elem (i, i) == 1.0) |
|
2151 { |
|
2152 for (int j = 0; j < a_nc; j++) |
|
2153 c.elem (i, j) = a.elem (i, j); |
|
2154 } |
|
2155 else if (m.elem (i, i) == 0.0) |
|
2156 { |
|
2157 for (int j = 0; j < a_nc; j++) |
|
2158 c.elem (i, j) = 0.0; |
|
2159 } |
|
2160 else |
|
2161 { |
|
2162 for (int j = 0; j < a_nc; j++) |
|
2163 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
2164 } |
|
2165 } |
|
2166 |
|
2167 if (nr > nc) |
|
2168 { |
|
2169 for (int j = 0; j < a_nc; j++) |
|
2170 for (int i = a_nr; i < nr; i++) |
|
2171 c.elem (i, j) = 0.0; |
|
2172 } |
|
2173 |
|
2174 return c; |
|
2175 } |
|
2176 |
|
2177 ComplexMatrix |
|
2178 operator + (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
2179 { |
|
2180 int nr = m.rows (); |
|
2181 int nc = m.cols (); |
2384
|
2182 |
|
2183 int a_nr = a.rows (); |
|
2184 int a_nc = a.cols (); |
|
2185 |
|
2186 if (nr != a_nr || nc != a_nc) |
1205
|
2187 { |
2384
|
2188 gripe_nonconformant ("operator +", nr, nc, a_nr, a_nc); |
1205
|
2189 return ComplexMatrix (); |
|
2190 } |
|
2191 |
|
2192 if (nr == 0 || nc == 0) |
|
2193 return ComplexMatrix (nr, nc); |
|
2194 |
|
2195 ComplexMatrix result (a); |
|
2196 for (int i = 0; i < m.length (); i++) |
|
2197 result.elem (i, i) += m.elem (i, i); |
|
2198 |
|
2199 return result; |
|
2200 } |
|
2201 |
|
2202 ComplexMatrix |
|
2203 operator - (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
2204 { |
|
2205 int nr = m.rows (); |
|
2206 int nc = m.cols (); |
2384
|
2207 |
|
2208 int a_nr = a.rows (); |
|
2209 int a_nc = a.cols (); |
|
2210 |
|
2211 if (nr != a_nr || nc != a_nc) |
1205
|
2212 { |
2384
|
2213 gripe_nonconformant ("operator -", nr, nc, a_nr, a_nc); |
1205
|
2214 return ComplexMatrix (); |
|
2215 } |
|
2216 |
|
2217 if (nr == 0 || nc == 0) |
|
2218 return ComplexMatrix (nr, nc); |
|
2219 |
|
2220 ComplexMatrix result (-a); |
|
2221 for (int i = 0; i < m.length (); i++) |
|
2222 result.elem (i, i) += m.elem (i, i); |
|
2223 |
|
2224 return result; |
|
2225 } |
|
2226 |
|
2227 ComplexMatrix |
|
2228 operator * (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
2229 { |
|
2230 int nr = m.rows (); |
|
2231 int nc = m.cols (); |
2384
|
2232 |
1205
|
2233 int a_nr = a.rows (); |
|
2234 int a_nc = a.cols (); |
2384
|
2235 |
1205
|
2236 if (nc != a_nr) |
|
2237 { |
2384
|
2238 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
1205
|
2239 return ComplexMatrix (); |
|
2240 } |
|
2241 |
|
2242 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2243 return ComplexMatrix (nr, a_nc, 0.0); |
|
2244 |
|
2245 ComplexMatrix c (nr, a_nc); |
|
2246 |
|
2247 for (int i = 0; i < m.length (); i++) |
|
2248 { |
|
2249 if (m.elem (i, i) == 1.0) |
|
2250 { |
|
2251 for (int j = 0; j < a_nc; j++) |
|
2252 c.elem (i, j) = a.elem (i, j); |
|
2253 } |
|
2254 else if (m.elem (i, i) == 0.0) |
|
2255 { |
|
2256 for (int j = 0; j < a_nc; j++) |
|
2257 c.elem (i, j) = 0.0; |
|
2258 } |
|
2259 else |
|
2260 { |
|
2261 for (int j = 0; j < a_nc; j++) |
|
2262 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
2263 } |
|
2264 } |
|
2265 |
|
2266 if (nr > nc) |
|
2267 { |
|
2268 for (int j = 0; j < a_nc; j++) |
|
2269 for (int i = a_nr; i < nr; i++) |
|
2270 c.elem (i, j) = 0.0; |
|
2271 } |
|
2272 |
|
2273 return c; |
|
2274 } |
|
2275 |
458
|
2276 // matrix by matrix -> matrix operations |
|
2277 |
|
2278 ComplexMatrix& |
|
2279 ComplexMatrix::operator += (const Matrix& a) |
|
2280 { |
|
2281 int nr = rows (); |
|
2282 int nc = cols (); |
2384
|
2283 |
|
2284 int a_nr = a.rows (); |
|
2285 int a_nc = a.cols (); |
|
2286 |
|
2287 if (nr != a_nr || nc != a_nc) |
458
|
2288 { |
2384
|
2289 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
2290 return *this; |
|
2291 } |
|
2292 |
|
2293 if (nr == 0 || nc == 0) |
|
2294 return *this; |
|
2295 |
|
2296 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2297 |
|
2298 add2 (d, a.data (), length ()); |
|
2299 return *this; |
|
2300 } |
|
2301 |
|
2302 ComplexMatrix& |
|
2303 ComplexMatrix::operator -= (const Matrix& a) |
|
2304 { |
|
2305 int nr = rows (); |
|
2306 int nc = cols (); |
2384
|
2307 |
|
2308 int a_nr = a.rows (); |
|
2309 int a_nc = a.cols (); |
|
2310 |
|
2311 if (nr != a_nr || nc != a_nc) |
458
|
2312 { |
2384
|
2313 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
2314 return *this; |
|
2315 } |
|
2316 |
|
2317 if (nr == 0 || nc == 0) |
|
2318 return *this; |
|
2319 |
|
2320 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2321 |
|
2322 subtract2 (d, a.data (), length ()); |
|
2323 return *this; |
|
2324 } |
|
2325 |
|
2326 ComplexMatrix& |
|
2327 ComplexMatrix::operator += (const ComplexMatrix& a) |
|
2328 { |
|
2329 int nr = rows (); |
|
2330 int nc = cols (); |
2384
|
2331 |
|
2332 int a_nr = a.rows (); |
|
2333 int a_nc = a.cols (); |
|
2334 |
|
2335 if (nr != a_nr || nc != a_nc) |
458
|
2336 { |
2384
|
2337 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
2338 return *this; |
|
2339 } |
|
2340 |
|
2341 if (nr == 0 || nc == 0) |
|
2342 return *this; |
|
2343 |
|
2344 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2345 |
|
2346 add2 (d, a.data (), length ()); |
|
2347 return *this; |
|
2348 } |
|
2349 |
|
2350 ComplexMatrix& |
|
2351 ComplexMatrix::operator -= (const ComplexMatrix& a) |
|
2352 { |
|
2353 int nr = rows (); |
|
2354 int nc = cols (); |
2384
|
2355 |
|
2356 int a_nr = a.rows (); |
|
2357 int a_nc = a.cols (); |
|
2358 |
|
2359 if (nr != a_nr || nc != a_nc) |
458
|
2360 { |
2384
|
2361 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
2362 return *this; |
|
2363 } |
|
2364 |
|
2365 if (nr == 0 || nc == 0) |
|
2366 return *this; |
|
2367 |
|
2368 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2369 |
|
2370 subtract2 (d, a.data (), length ()); |
|
2371 return *this; |
|
2372 } |
|
2373 |
|
2374 // unary operations |
|
2375 |
|
2376 Matrix |
|
2377 ComplexMatrix::operator ! (void) const |
|
2378 { |
|
2379 return Matrix (not (data (), length ()), rows (), cols ()); |
|
2380 } |
|
2381 |
|
2382 // matrix by scalar -> matrix operations |
|
2383 |
|
2384 ComplexMatrix |
1205
|
2385 operator + (const Matrix& a, const Complex& s) |
|
2386 { |
|
2387 return ComplexMatrix (add (a.data (), a.length (), s), |
|
2388 a.rows (), a.cols ()); |
|
2389 } |
|
2390 |
|
2391 ComplexMatrix |
|
2392 operator - (const Matrix& a, const Complex& s) |
|
2393 { |
|
2394 return ComplexMatrix (subtract (a.data (), a.length (), s), |
|
2395 a.rows (), a.cols ()); |
|
2396 } |
|
2397 |
|
2398 ComplexMatrix |
|
2399 operator * (const Matrix& a, const Complex& s) |
|
2400 { |
|
2401 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2402 a.rows (), a.cols ()); |
|
2403 } |
|
2404 |
|
2405 ComplexMatrix |
|
2406 operator / (const Matrix& a, const Complex& s) |
|
2407 { |
|
2408 return ComplexMatrix (divide (a.data (), a.length (), s), |
|
2409 a.rows (), a.cols ()); |
|
2410 } |
|
2411 |
|
2412 ComplexMatrix |
458
|
2413 operator + (const ComplexMatrix& a, double s) |
|
2414 { |
|
2415 return ComplexMatrix (add (a.data (), a.length (), s), |
|
2416 a.rows (), a.cols ()); |
|
2417 } |
|
2418 |
|
2419 ComplexMatrix |
|
2420 operator - (const ComplexMatrix& a, double s) |
|
2421 { |
|
2422 return ComplexMatrix (subtract (a.data (), a.length (), s), |
|
2423 a.rows (), a.cols ()); |
|
2424 } |
|
2425 |
|
2426 ComplexMatrix |
|
2427 operator * (const ComplexMatrix& a, double s) |
|
2428 { |
|
2429 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2430 a.rows (), a.cols ()); |
|
2431 } |
|
2432 |
|
2433 ComplexMatrix |
|
2434 operator / (const ComplexMatrix& a, double s) |
|
2435 { |
|
2436 return ComplexMatrix (divide (a.data (), a.length (), s), |
|
2437 a.rows (), a.cols ()); |
|
2438 } |
|
2439 |
|
2440 // scalar by matrix -> matrix operations |
|
2441 |
|
2442 ComplexMatrix |
|
2443 operator + (double s, const ComplexMatrix& a) |
|
2444 { |
|
2445 return ComplexMatrix (add (a.data (), a.length (), s), a.rows (), |
|
2446 a.cols ()); |
|
2447 } |
|
2448 |
|
2449 ComplexMatrix |
|
2450 operator - (double s, const ComplexMatrix& a) |
|
2451 { |
|
2452 return ComplexMatrix (subtract (s, a.data (), a.length ()), |
|
2453 a.rows (), a.cols ()); |
|
2454 } |
|
2455 |
|
2456 ComplexMatrix |
|
2457 operator * (double s, const ComplexMatrix& a) |
|
2458 { |
|
2459 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2460 a.rows (), a.cols ()); |
|
2461 } |
|
2462 |
|
2463 ComplexMatrix |
|
2464 operator / (double s, const ComplexMatrix& a) |
|
2465 { |
|
2466 return ComplexMatrix (divide (s, a.data (), a.length ()), |
|
2467 a.rows (), a.cols ()); |
|
2468 } |
|
2469 |
1205
|
2470 ComplexMatrix |
|
2471 operator + (const Complex& s, const Matrix& a) |
458
|
2472 { |
1205
|
2473 return ComplexMatrix (add (s, a.data (), a.length ()), |
|
2474 a.rows (), a.cols ()); |
458
|
2475 } |
|
2476 |
1205
|
2477 ComplexMatrix |
|
2478 operator - (const Complex& s, const Matrix& a) |
458
|
2479 { |
1205
|
2480 return ComplexMatrix (subtract (s, a.data (), a.length ()), |
|
2481 a.rows (), a.cols ()); |
|
2482 } |
|
2483 |
|
2484 ComplexMatrix |
|
2485 operator * (const Complex& s, const Matrix& a) |
|
2486 { |
|
2487 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2488 a.rows (), a.cols ()); |
|
2489 } |
|
2490 |
|
2491 ComplexMatrix |
|
2492 operator / (const Complex& s, const Matrix& a) |
|
2493 { |
|
2494 return ComplexMatrix (divide (s, a.data (), a.length ()), |
|
2495 a.rows (), a.cols ()); |
458
|
2496 } |
|
2497 |
|
2498 // matrix by diagonal matrix -> matrix operations |
|
2499 |
|
2500 ComplexMatrix |
|
2501 operator + (const ComplexMatrix& m, const DiagMatrix& a) |
|
2502 { |
|
2503 int nr = m.rows (); |
|
2504 int nc = m.cols (); |
2384
|
2505 |
|
2506 int a_nr = a.rows (); |
|
2507 int a_nc = a.cols (); |
|
2508 |
|
2509 if (nr != a_nr || nc != a_nc) |
458
|
2510 { |
2384
|
2511 gripe_nonconformant ("operator +", nr, nc, a_nr, a_nc); |
458
|
2512 return ComplexMatrix (); |
|
2513 } |
|
2514 |
|
2515 if (nr == 0 || nc == 0) |
|
2516 return ComplexMatrix (nr, nc); |
|
2517 |
|
2518 ComplexMatrix result (m); |
|
2519 for (int i = 0; i < a.length (); i++) |
|
2520 result.elem (i, i) += a.elem (i, i); |
|
2521 |
|
2522 return result; |
|
2523 } |
|
2524 |
|
2525 ComplexMatrix |
|
2526 operator - (const ComplexMatrix& m, const DiagMatrix& a) |
|
2527 { |
|
2528 int nr = m.rows (); |
|
2529 int nc = m.cols (); |
2384
|
2530 |
|
2531 int a_nr = a.rows (); |
|
2532 int a_nc = a.cols (); |
|
2533 |
|
2534 if (nr != a_nr || nc != a_nc) |
458
|
2535 { |
2384
|
2536 gripe_nonconformant ("operator -", nr, nc, a_nr, a_nc); |
458
|
2537 return ComplexMatrix (); |
|
2538 } |
|
2539 |
|
2540 if (nr == 0 || nc == 0) |
|
2541 return ComplexMatrix (nr, nc); |
|
2542 |
|
2543 ComplexMatrix result (m); |
|
2544 for (int i = 0; i < a.length (); i++) |
|
2545 result.elem (i, i) -= a.elem (i, i); |
|
2546 |
|
2547 return result; |
|
2548 } |
|
2549 |
|
2550 ComplexMatrix |
|
2551 operator * (const ComplexMatrix& m, const DiagMatrix& a) |
|
2552 { |
1948
|
2553 ComplexMatrix retval; |
|
2554 |
458
|
2555 int nr = m.rows (); |
|
2556 int nc = m.cols (); |
1948
|
2557 |
2384
|
2558 int a_nr = a.rows (); |
458
|
2559 int a_nc = a.cols (); |
1948
|
2560 |
2384
|
2561 if (nc != a_nr) |
|
2562 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
1948
|
2563 else |
458
|
2564 { |
1948
|
2565 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2566 retval.resize (nr, nc, 0.0); |
458
|
2567 else |
|
2568 { |
1948
|
2569 retval.resize (nr, a_nc); |
|
2570 Complex *c = retval.fortran_vec (); |
|
2571 Complex *ctmp = 0; |
|
2572 |
|
2573 for (int j = 0; j < a.length (); j++) |
|
2574 { |
|
2575 int idx = j * nr; |
|
2576 ctmp = c + idx; |
|
2577 if (a.elem (j, j) == 1.0) |
|
2578 { |
|
2579 for (int i = 0; i < nr; i++) |
|
2580 ctmp[i] = m.elem (i, j); |
|
2581 } |
|
2582 else if (a.elem (j, j) == 0.0) |
|
2583 { |
|
2584 for (int i = 0; i < nr; i++) |
|
2585 ctmp[i] = 0.0; |
|
2586 } |
|
2587 else |
|
2588 { |
|
2589 for (int i = 0; i < nr; i++) |
|
2590 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
2591 } |
|
2592 } |
|
2593 |
|
2594 if (a.rows () < a_nc) |
|
2595 { |
|
2596 for (int i = nr * nc; i < nr * a_nc; i++) |
|
2597 ctmp[i] = 0.0; |
|
2598 } |
458
|
2599 } |
|
2600 } |
|
2601 |
1948
|
2602 return retval; |
458
|
2603 } |
|
2604 |
|
2605 ComplexMatrix |
|
2606 operator + (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2607 { |
|
2608 int nr = m.rows (); |
|
2609 int nc = m.cols (); |
2384
|
2610 |
|
2611 int a_nr = a.rows (); |
|
2612 int a_nc = a.cols (); |
|
2613 |
|
2614 if (nr != a_nr || nc != a_nc) |
458
|
2615 { |
2384
|
2616 gripe_nonconformant ("operator +", nr, nc, a_nr, a_nc); |
458
|
2617 return ComplexMatrix (); |
|
2618 } |
|
2619 |
|
2620 if (nr == 0 || nc == 0) |
|
2621 return ComplexMatrix (nr, nc); |
|
2622 |
|
2623 ComplexMatrix result (m); |
|
2624 for (int i = 0; i < a.length (); i++) |
|
2625 result.elem (i, i) += a.elem (i, i); |
|
2626 |
|
2627 return result; |
|
2628 } |
|
2629 |
|
2630 ComplexMatrix |
|
2631 operator - (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2632 { |
|
2633 int nr = m.rows (); |
|
2634 int nc = m.cols (); |
2384
|
2635 |
|
2636 int a_nr = a.rows (); |
|
2637 int a_nc = a.cols (); |
|
2638 |
|
2639 if (nr != a_nr || nc != a_nc) |
458
|
2640 { |
2384
|
2641 gripe_nonconformant ("operator -", nr, nc, a_nr, a_nc); |
458
|
2642 return ComplexMatrix (); |
|
2643 } |
|
2644 |
|
2645 if (nr == 0 || nc == 0) |
|
2646 return ComplexMatrix (nr, nc); |
|
2647 |
|
2648 ComplexMatrix result (m); |
|
2649 for (int i = 0; i < a.length (); i++) |
|
2650 result.elem (i, i) -= a.elem (i, i); |
|
2651 |
|
2652 return result; |
|
2653 } |
|
2654 |
|
2655 ComplexMatrix |
|
2656 operator * (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2657 { |
1948
|
2658 ComplexMatrix retval; |
|
2659 |
458
|
2660 int nr = m.rows (); |
|
2661 int nc = m.cols (); |
1948
|
2662 |
2384
|
2663 int a_nr = a.rows (); |
458
|
2664 int a_nc = a.cols (); |
1948
|
2665 |
2384
|
2666 if (nc != a_nr) |
|
2667 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
1948
|
2668 else |
458
|
2669 { |
1948
|
2670 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2671 retval.resize (nr, nc, 0.0); |
458
|
2672 else |
|
2673 { |
1948
|
2674 retval.resize (nr, nc); |
|
2675 Complex *c = retval.fortran_vec (); |
|
2676 Complex *ctmp = 0; |
|
2677 |
|
2678 for (int j = 0; j < a.length (); j++) |
|
2679 { |
|
2680 int idx = j * nr; |
|
2681 ctmp = c + idx; |
|
2682 if (a.elem (j, j) == 1.0) |
|
2683 { |
|
2684 for (int i = 0; i < nr; i++) |
|
2685 ctmp[i] = m.elem (i, j); |
|
2686 } |
|
2687 else if (a.elem (j, j) == 0.0) |
|
2688 { |
|
2689 for (int i = 0; i < nr; i++) |
|
2690 ctmp[i] = 0.0; |
|
2691 } |
|
2692 else |
|
2693 { |
|
2694 for (int i = 0; i < nr; i++) |
|
2695 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
2696 } |
|
2697 } |
|
2698 |
|
2699 if (a.rows () < a_nc) |
|
2700 { |
|
2701 for (int i = nr * nc; i < nr * a_nc; i++) |
|
2702 ctmp[i] = 0.0; |
|
2703 } |
458
|
2704 } |
|
2705 } |
|
2706 |
1948
|
2707 return retval; |
458
|
2708 } |
|
2709 |
|
2710 // matrix by matrix -> matrix operations |
|
2711 |
|
2712 ComplexMatrix |
|
2713 operator + (const ComplexMatrix& m, const Matrix& a) |
|
2714 { |
|
2715 int nr = m.rows (); |
|
2716 int nc = m.cols (); |
2384
|
2717 |
|
2718 int a_nr = a.rows (); |
|
2719 int a_nc = a.cols (); |
|
2720 |
|
2721 if (nr != a_nr || nc != a_nc) |
458
|
2722 { |
2384
|
2723 gripe_nonconformant ("operator +", nr, nc, a_nr, a_nc); |
458
|
2724 return ComplexMatrix (); |
|
2725 } |
|
2726 |
|
2727 if (nr == 0 || nc == 0) |
|
2728 return ComplexMatrix (nr, nc); |
|
2729 |
|
2730 return ComplexMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
2731 } |
|
2732 |
|
2733 ComplexMatrix |
|
2734 operator - (const ComplexMatrix& m, const Matrix& a) |
|
2735 { |
|
2736 int nr = m.rows (); |
|
2737 int nc = m.cols (); |
2384
|
2738 |
|
2739 int a_nr = a.rows (); |
|
2740 int a_nc = a.cols (); |
|
2741 |
|
2742 if (nr != a_nr || nc != a_nc) |
458
|
2743 { |
2384
|
2744 gripe_nonconformant ("operator -", nr, nc, a_nr, a_nc); |
458
|
2745 return ComplexMatrix (); |
|
2746 } |
|
2747 |
|
2748 if (nr == 0 || nc == 0) |
|
2749 return ComplexMatrix (nr, nc); |
|
2750 |
|
2751 return ComplexMatrix (subtract (m.data (), a.data (), m.length ()), nr, nc); |
|
2752 } |
|
2753 |
|
2754 ComplexMatrix |
1205
|
2755 operator + (const Matrix& m, const ComplexMatrix& a) |
|
2756 { |
|
2757 int nr = m.rows (); |
|
2758 int nc = m.cols (); |
2384
|
2759 |
|
2760 int a_nr = a.rows (); |
|
2761 int a_nc = a.cols (); |
|
2762 |
|
2763 if (nr != a_nr || nc != a_nc) |
1205
|
2764 { |
2384
|
2765 gripe_nonconformant ("operator +", nr, nc, a_nr, a_nc); |
1205
|
2766 return ComplexMatrix (); |
|
2767 } |
|
2768 |
|
2769 return ComplexMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
2770 } |
|
2771 |
|
2772 ComplexMatrix |
|
2773 operator - (const Matrix& m, const ComplexMatrix& a) |
|
2774 { |
|
2775 int nr = m.rows (); |
|
2776 int nc = m.cols (); |
2384
|
2777 |
|
2778 int a_nr = a.rows (); |
|
2779 int a_nc = a.cols (); |
|
2780 |
|
2781 if (nr != a_nr || nc != a_nc) |
1205
|
2782 { |
2384
|
2783 gripe_nonconformant ("operator -", nr, nc, a_nr, a_nc); |
1205
|
2784 return ComplexMatrix (); |
|
2785 } |
|
2786 |
|
2787 if (nr == 0 || nc == 0) |
|
2788 return ComplexMatrix (nr, nc); |
|
2789 |
|
2790 return ComplexMatrix (subtract (m.data (), a.data (), m.length ()), nr, nc); |
|
2791 } |
|
2792 |
|
2793 ComplexMatrix |
458
|
2794 operator * (const ComplexMatrix& m, const Matrix& a) |
|
2795 { |
|
2796 ComplexMatrix tmp (a); |
|
2797 return m * tmp; |
|
2798 } |
|
2799 |
|
2800 ComplexMatrix |
1205
|
2801 operator * (const Matrix& m, const ComplexMatrix& a) |
|
2802 { |
|
2803 ComplexMatrix tmp (m); |
|
2804 return tmp * a; |
|
2805 } |
|
2806 |
|
2807 ComplexMatrix |
458
|
2808 operator * (const ComplexMatrix& m, const ComplexMatrix& a) |
|
2809 { |
1948
|
2810 ComplexMatrix retval; |
|
2811 |
458
|
2812 int nr = m.rows (); |
|
2813 int nc = m.cols (); |
1948
|
2814 |
2384
|
2815 int a_nr = a.rows (); |
458
|
2816 int a_nc = a.cols (); |
1948
|
2817 |
2384
|
2818 if (nc != a_nr) |
|
2819 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
1948
|
2820 else |
458
|
2821 { |
1948
|
2822 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2823 retval.resize (nr, nc, 0.0); |
|
2824 else |
|
2825 { |
|
2826 int ld = nr; |
|
2827 int lda = a.rows (); |
|
2828 |
|
2829 retval.resize (nr, a_nc); |
|
2830 Complex *c = retval.fortran_vec (); |
|
2831 |
|
2832 F77_XFCN (zgemm, ZGEMM, ("N", "N", nr, a_nc, nc, 1.0, |
|
2833 m.data (), ld, a.data (), lda, 0.0, |
|
2834 c, nr, 1L, 1L)); |
|
2835 |
|
2836 if (f77_exception_encountered) |
|
2837 (*current_liboctave_error_handler) |
|
2838 ("unrecoverable error in zgemm"); |
|
2839 } |
458
|
2840 } |
|
2841 |
1948
|
2842 return retval; |
458
|
2843 } |
|
2844 |
|
2845 ComplexMatrix |
|
2846 product (const ComplexMatrix& m, const Matrix& a) |
|
2847 { |
|
2848 int nr = m.rows (); |
|
2849 int nc = m.cols (); |
2384
|
2850 |
|
2851 int a_nr = a.rows (); |
|
2852 int a_nc = a.cols (); |
|
2853 |
|
2854 if (nr != a_nr || nc != a_nc) |
458
|
2855 { |
2384
|
2856 gripe_nonconformant ("product", nr, nc, a_nr, a_nc); |
458
|
2857 return ComplexMatrix (); |
|
2858 } |
|
2859 |
|
2860 if (nr == 0 || nc == 0) |
|
2861 return ComplexMatrix (nr, nc); |
|
2862 |
|
2863 return ComplexMatrix (multiply (m.data (), a.data (), m.length ()), nr, nc); |
|
2864 } |
|
2865 |
|
2866 ComplexMatrix |
|
2867 quotient (const ComplexMatrix& m, const Matrix& a) |
|
2868 { |
|
2869 int nr = m.rows (); |
|
2870 int nc = m.cols (); |
2384
|
2871 |
|
2872 int a_nr = a.rows (); |
|
2873 int a_nc = a.cols (); |
|
2874 |
|
2875 if (nr != a_nr || nc != a_nc) |
458
|
2876 { |
2384
|
2877 gripe_nonconformant ("quotient", nr, nc, a_nr, a_nc); |
458
|
2878 return ComplexMatrix (); |
|
2879 } |
|
2880 |
|
2881 if (nr == 0 || nc == 0) |
|
2882 return ComplexMatrix (nr, nc); |
|
2883 |
|
2884 return ComplexMatrix (divide (m.data (), a.data (), m.length ()), nr, nc); |
|
2885 } |
|
2886 |
1205
|
2887 ComplexMatrix |
|
2888 product (const Matrix& m, const ComplexMatrix& a) |
|
2889 { |
|
2890 int nr = m.rows (); |
|
2891 int nc = m.cols (); |
2384
|
2892 |
|
2893 int a_nr = a.rows (); |
|
2894 int a_nc = a.cols (); |
|
2895 |
|
2896 if (nr != a_nr || nc != a_nc) |
1205
|
2897 { |
2384
|
2898 gripe_nonconformant ("product", nr, nc, a_nr, a_nc); |
1205
|
2899 return ComplexMatrix (); |
|
2900 } |
|
2901 |
|
2902 if (nr == 0 || nc == 0) |
|
2903 return ComplexMatrix (nr, nc); |
|
2904 |
|
2905 return ComplexMatrix (multiply (m.data (), a.data (), m.length ()), nr, nc); |
|
2906 } |
|
2907 |
|
2908 ComplexMatrix |
|
2909 quotient (const Matrix& m, const ComplexMatrix& a) |
|
2910 { |
|
2911 int nr = m.rows (); |
|
2912 int nc = m.cols (); |
2384
|
2913 |
|
2914 int a_nr = a.rows (); |
|
2915 int a_nc = a.cols (); |
|
2916 |
|
2917 if (nr != a_nr || nc != a_nc) |
1205
|
2918 { |
2384
|
2919 gripe_nonconformant ("quotient", nr, nc, a_nr, a_nc); |
1205
|
2920 return ComplexMatrix (); |
|
2921 } |
|
2922 |
|
2923 if (nr == 0 || nc == 0) |
|
2924 return ComplexMatrix (nr, nc); |
|
2925 |
|
2926 return ComplexMatrix (divide (m.data (), a.data (), m.length ()), nr, nc); |
|
2927 } |
|
2928 |
458
|
2929 // other operations |
|
2930 |
|
2931 ComplexMatrix |
2676
|
2932 ComplexMatrix::map (c_c_Mapper f) const |
458
|
2933 { |
2676
|
2934 ComplexMatrix b (*this); |
|
2935 return b.apply (f); |
458
|
2936 } |
|
2937 |
2676
|
2938 Matrix |
|
2939 ComplexMatrix::map (d_c_Mapper f) const |
458
|
2940 { |
2676
|
2941 const Complex *d = data (); |
|
2942 |
|
2943 Matrix retval (rows (), columns ()); |
|
2944 |
|
2945 double *r = retval.fortran_vec (); |
|
2946 |
|
2947 for (int i = 0; i < length (); i++) |
|
2948 r[i] = f (d[i]); |
|
2949 |
|
2950 return retval; |
|
2951 } |
|
2952 |
|
2953 ComplexMatrix& |
|
2954 ComplexMatrix::apply (c_c_Mapper f) |
|
2955 { |
|
2956 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2957 |
|
2958 for (int i = 0; i < length (); i++) |
|
2959 d[i] = f (d[i]); |
|
2960 |
|
2961 return *this; |
458
|
2962 } |
|
2963 |
2384
|
2964 bool |
|
2965 ComplexMatrix::any_element_is_inf_or_nan (void) const |
|
2966 { |
|
2967 int nr = rows (); |
|
2968 int nc = cols (); |
|
2969 |
|
2970 for (int j = 0; j < nc; j++) |
|
2971 for (int i = 0; i < nr; i++) |
|
2972 { |
|
2973 Complex val = elem (i, j); |
|
2974 if (xisinf (val) || xisnan (val)) |
|
2975 return true; |
|
2976 } |
|
2977 |
|
2978 return false; |
|
2979 } |
|
2980 |
2408
|
2981 // Return true if no elements have imaginary components. |
|
2982 |
|
2983 bool |
|
2984 ComplexMatrix::all_elements_are_real (void) const |
|
2985 { |
|
2986 int nr = rows (); |
|
2987 int nc = cols (); |
|
2988 |
|
2989 for (int j = 0; j < nc; j++) |
|
2990 for (int i = 0; i < nr; i++) |
|
2991 if (imag (elem (i, j)) != 0.0) |
|
2992 return false; |
|
2993 |
|
2994 return true; |
|
2995 } |
|
2996 |
1968
|
2997 // Return nonzero if any element of CM has a non-integer real or |
|
2998 // imaginary part. Also extract the largest and smallest (real or |
|
2999 // imaginary) values and return them in MAX_VAL and MIN_VAL. |
|
3000 |
2384
|
3001 bool |
1968
|
3002 ComplexMatrix::all_integers (double& max_val, double& min_val) const |
|
3003 { |
|
3004 int nr = rows (); |
2384
|
3005 int nc = cols (); |
1968
|
3006 |
|
3007 if (nr > 0 && nc > 0) |
|
3008 { |
|
3009 Complex val = elem (0, 0); |
|
3010 |
|
3011 double r_val = real (val); |
|
3012 double i_val = imag (val); |
|
3013 |
|
3014 max_val = r_val; |
|
3015 min_val = r_val; |
|
3016 |
|
3017 if (i_val > max_val) |
|
3018 max_val = i_val; |
|
3019 |
|
3020 if (i_val < max_val) |
|
3021 min_val = i_val; |
|
3022 } |
|
3023 else |
2384
|
3024 return false; |
1968
|
3025 |
|
3026 for (int j = 0; j < nc; j++) |
|
3027 for (int i = 0; i < nr; i++) |
|
3028 { |
|
3029 Complex val = elem (i, j); |
|
3030 |
|
3031 double r_val = real (val); |
|
3032 double i_val = imag (val); |
|
3033 |
|
3034 if (r_val > max_val) |
|
3035 max_val = r_val; |
|
3036 |
|
3037 if (i_val > max_val) |
|
3038 max_val = i_val; |
|
3039 |
|
3040 if (r_val < min_val) |
|
3041 min_val = r_val; |
|
3042 |
|
3043 if (i_val < min_val) |
|
3044 min_val = i_val; |
|
3045 |
|
3046 if (D_NINT (r_val) != r_val || D_NINT (i_val) != i_val) |
2384
|
3047 return false; |
1968
|
3048 } |
2384
|
3049 |
|
3050 return true; |
1968
|
3051 } |
|
3052 |
2384
|
3053 bool |
1968
|
3054 ComplexMatrix::too_large_for_float (void) const |
|
3055 { |
|
3056 int nr = rows (); |
2384
|
3057 int nc = cols (); |
1968
|
3058 |
|
3059 for (int j = 0; j < nc; j++) |
|
3060 for (int i = 0; i < nr; i++) |
|
3061 { |
|
3062 Complex val = elem (i, j); |
|
3063 |
|
3064 double r_val = real (val); |
|
3065 double i_val = imag (val); |
|
3066 |
|
3067 if (r_val > FLT_MAX |
|
3068 || i_val > FLT_MAX |
|
3069 || r_val < FLT_MIN |
|
3070 || i_val < FLT_MIN) |
2384
|
3071 return true; |
1968
|
3072 } |
|
3073 |
2384
|
3074 return false; |
1968
|
3075 } |
|
3076 |
458
|
3077 Matrix |
|
3078 ComplexMatrix::all (void) const |
|
3079 { |
|
3080 int nr = rows (); |
|
3081 int nc = cols (); |
|
3082 Matrix retval; |
|
3083 if (nr > 0 && nc > 0) |
|
3084 { |
|
3085 if (nr == 1) |
|
3086 { |
|
3087 retval.resize (1, 1); |
|
3088 retval.elem (0, 0) = 1.0; |
|
3089 for (int j = 0; j < nc; j++) |
|
3090 { |
|
3091 if (elem (0, j) == 0.0) |
|
3092 { |
|
3093 retval.elem (0, 0) = 0.0; |
|
3094 break; |
|
3095 } |
|
3096 } |
|
3097 } |
|
3098 else if (nc == 1) |
|
3099 { |
|
3100 retval.resize (1, 1); |
|
3101 retval.elem (0, 0) = 1.0; |
|
3102 for (int i = 0; i < nr; i++) |
|
3103 { |
|
3104 if (elem (i, 0) == 0.0) |
|
3105 { |
|
3106 retval.elem (0, 0) = 0.0; |
|
3107 break; |
|
3108 } |
|
3109 } |
|
3110 } |
|
3111 else |
|
3112 { |
|
3113 retval.resize (1, nc); |
|
3114 for (int j = 0; j < nc; j++) |
|
3115 { |
|
3116 retval.elem (0, j) = 1.0; |
|
3117 for (int i = 0; i < nr; i++) |
|
3118 { |
|
3119 if (elem (i, j) == 0.0) |
|
3120 { |
|
3121 retval.elem (0, j) = 0.0; |
|
3122 break; |
|
3123 } |
|
3124 } |
|
3125 } |
|
3126 } |
|
3127 } |
|
3128 return retval; |
|
3129 } |
|
3130 |
|
3131 Matrix |
|
3132 ComplexMatrix::any (void) const |
|
3133 { |
|
3134 int nr = rows (); |
|
3135 int nc = cols (); |
|
3136 Matrix retval; |
|
3137 if (nr > 0 && nc > 0) |
|
3138 { |
|
3139 if (nr == 1) |
|
3140 { |
|
3141 retval.resize (1, 1); |
|
3142 retval.elem (0, 0) = 0.0; |
|
3143 for (int j = 0; j < nc; j++) |
|
3144 { |
|
3145 if (elem (0, j) != 0.0) |
|
3146 { |
|
3147 retval.elem (0, 0) = 1.0; |
|
3148 break; |
|
3149 } |
|
3150 } |
|
3151 } |
|
3152 else if (nc == 1) |
|
3153 { |
|
3154 retval.resize (1, 1); |
|
3155 retval.elem (0, 0) = 0.0; |
|
3156 for (int i = 0; i < nr; i++) |
|
3157 { |
|
3158 if (elem (i, 0) != 0.0) |
|
3159 { |
|
3160 retval.elem (0, 0) = 1.0; |
|
3161 break; |
|
3162 } |
|
3163 } |
|
3164 } |
|
3165 else |
|
3166 { |
|
3167 retval.resize (1, nc); |
|
3168 for (int j = 0; j < nc; j++) |
|
3169 { |
|
3170 retval.elem (0, j) = 0.0; |
|
3171 for (int i = 0; i < nr; i++) |
|
3172 { |
|
3173 if (elem (i, j) != 0.0) |
|
3174 { |
|
3175 retval.elem (0, j) = 1.0; |
|
3176 break; |
|
3177 } |
|
3178 } |
|
3179 } |
|
3180 } |
|
3181 } |
|
3182 return retval; |
|
3183 } |
|
3184 |
|
3185 ComplexMatrix |
|
3186 ComplexMatrix::cumprod (void) const |
|
3187 { |
|
3188 int nr = rows (); |
|
3189 int nc = cols (); |
|
3190 ComplexMatrix retval; |
|
3191 if (nr > 0 && nc > 0) |
|
3192 { |
|
3193 if (nr == 1) |
|
3194 { |
|
3195 retval.resize (1, nc); |
|
3196 Complex prod = elem (0, 0); |
|
3197 for (int j = 0; j < nc; j++) |
|
3198 { |
|
3199 retval.elem (0, j) = prod; |
|
3200 if (j < nc - 1) |
|
3201 prod *= elem (0, j+1); |
|
3202 } |
|
3203 } |
|
3204 else if (nc == 1) |
|
3205 { |
|
3206 retval.resize (nr, 1); |
|
3207 Complex prod = elem (0, 0); |
|
3208 for (int i = 0; i < nr; i++) |
|
3209 { |
|
3210 retval.elem (i, 0) = prod; |
|
3211 if (i < nr - 1) |
|
3212 prod *= elem (i+1, 0); |
|
3213 } |
|
3214 } |
|
3215 else |
|
3216 { |
|
3217 retval.resize (nr, nc); |
|
3218 for (int j = 0; j < nc; j++) |
|
3219 { |
|
3220 Complex prod = elem (0, j); |
|
3221 for (int i = 0; i < nr; i++) |
|
3222 { |
|
3223 retval.elem (i, j) = prod; |
|
3224 if (i < nr - 1) |
|
3225 prod *= elem (i+1, j); |
|
3226 } |
|
3227 } |
|
3228 } |
|
3229 } |
|
3230 return retval; |
|
3231 } |
|
3232 |
|
3233 ComplexMatrix |
|
3234 ComplexMatrix::cumsum (void) const |
|
3235 { |
|
3236 int nr = rows (); |
|
3237 int nc = cols (); |
|
3238 ComplexMatrix retval; |
|
3239 if (nr > 0 && nc > 0) |
|
3240 { |
|
3241 if (nr == 1) |
|
3242 { |
|
3243 retval.resize (1, nc); |
|
3244 Complex sum = elem (0, 0); |
|
3245 for (int j = 0; j < nc; j++) |
|
3246 { |
|
3247 retval.elem (0, j) = sum; |
|
3248 if (j < nc - 1) |
|
3249 sum += elem (0, j+1); |
|
3250 } |
|
3251 } |
|
3252 else if (nc == 1) |
|
3253 { |
|
3254 retval.resize (nr, 1); |
|
3255 Complex sum = elem (0, 0); |
|
3256 for (int i = 0; i < nr; i++) |
|
3257 { |
|
3258 retval.elem (i, 0) = sum; |
|
3259 if (i < nr - 1) |
|
3260 sum += elem (i+1, 0); |
|
3261 } |
|
3262 } |
|
3263 else |
|
3264 { |
|
3265 retval.resize (nr, nc); |
|
3266 for (int j = 0; j < nc; j++) |
|
3267 { |
|
3268 Complex sum = elem (0, j); |
|
3269 for (int i = 0; i < nr; i++) |
|
3270 { |
|
3271 retval.elem (i, j) = sum; |
|
3272 if (i < nr - 1) |
|
3273 sum += elem (i+1, j); |
|
3274 } |
|
3275 } |
|
3276 } |
|
3277 } |
|
3278 return retval; |
|
3279 } |
|
3280 |
|
3281 ComplexMatrix |
|
3282 ComplexMatrix::prod (void) const |
|
3283 { |
|
3284 int nr = rows (); |
|
3285 int nc = cols (); |
|
3286 ComplexMatrix retval; |
|
3287 if (nr > 0 && nc > 0) |
|
3288 { |
|
3289 if (nr == 1) |
|
3290 { |
|
3291 retval.resize (1, 1); |
|
3292 retval.elem (0, 0) = 1.0; |
|
3293 for (int j = 0; j < nc; j++) |
|
3294 retval.elem (0, 0) *= elem (0, j); |
|
3295 } |
|
3296 else if (nc == 1) |
|
3297 { |
|
3298 retval.resize (1, 1); |
|
3299 retval.elem (0, 0) = 1.0; |
|
3300 for (int i = 0; i < nr; i++) |
|
3301 retval.elem (0, 0) *= elem (i, 0); |
|
3302 } |
|
3303 else |
|
3304 { |
|
3305 retval.resize (1, nc); |
|
3306 for (int j = 0; j < nc; j++) |
|
3307 { |
|
3308 retval.elem (0, j) = 1.0; |
|
3309 for (int i = 0; i < nr; i++) |
|
3310 retval.elem (0, j) *= elem (i, j); |
|
3311 } |
|
3312 } |
|
3313 } |
|
3314 return retval; |
|
3315 } |
|
3316 |
|
3317 ComplexMatrix |
|
3318 ComplexMatrix::sum (void) const |
|
3319 { |
|
3320 int nr = rows (); |
|
3321 int nc = cols (); |
|
3322 ComplexMatrix retval; |
|
3323 if (nr > 0 && nc > 0) |
|
3324 { |
|
3325 if (nr == 1) |
|
3326 { |
|
3327 retval.resize (1, 1); |
|
3328 retval.elem (0, 0) = 0.0; |
|
3329 for (int j = 0; j < nc; j++) |
|
3330 retval.elem (0, 0) += elem (0, j); |
|
3331 } |
|
3332 else if (nc == 1) |
|
3333 { |
|
3334 retval.resize (1, 1); |
|
3335 retval.elem (0, 0) = 0.0; |
|
3336 for (int i = 0; i < nr; i++) |
|
3337 retval.elem (0, 0) += elem (i, 0); |
|
3338 } |
|
3339 else |
|
3340 { |
|
3341 retval.resize (1, nc); |
|
3342 for (int j = 0; j < nc; j++) |
|
3343 { |
|
3344 retval.elem (0, j) = 0.0; |
|
3345 for (int i = 0; i < nr; i++) |
|
3346 retval.elem (0, j) += elem (i, j); |
|
3347 } |
|
3348 } |
|
3349 } |
|
3350 return retval; |
|
3351 } |
|
3352 |
|
3353 ComplexMatrix |
|
3354 ComplexMatrix::sumsq (void) const |
|
3355 { |
|
3356 int nr = rows (); |
|
3357 int nc = cols (); |
|
3358 ComplexMatrix retval; |
|
3359 if (nr > 0 && nc > 0) |
|
3360 { |
|
3361 if (nr == 1) |
|
3362 { |
|
3363 retval.resize (1, 1); |
|
3364 retval.elem (0, 0) = 0.0; |
|
3365 for (int j = 0; j < nc; j++) |
|
3366 { |
|
3367 Complex d = elem (0, j); |
|
3368 retval.elem (0, 0) += d * d; |
|
3369 } |
|
3370 } |
|
3371 else if (nc == 1) |
|
3372 { |
|
3373 retval.resize (1, 1); |
|
3374 retval.elem (0, 0) = 0.0; |
|
3375 for (int i = 0; i < nr; i++) |
|
3376 { |
|
3377 Complex d = elem (i, 0); |
|
3378 retval.elem (0, 0) += d * d; |
|
3379 } |
|
3380 } |
|
3381 else |
|
3382 { |
|
3383 retval.resize (1, nc); |
|
3384 for (int j = 0; j < nc; j++) |
|
3385 { |
|
3386 retval.elem (0, j) = 0.0; |
|
3387 for (int i = 0; i < nr; i++) |
|
3388 { |
|
3389 Complex d = elem (i, j); |
|
3390 retval.elem (0, j) += d * d; |
|
3391 } |
|
3392 } |
|
3393 } |
|
3394 } |
|
3395 return retval; |
|
3396 } |
|
3397 |
|
3398 ComplexColumnVector |
|
3399 ComplexMatrix::diag (void) const |
|
3400 { |
|
3401 return diag (0); |
|
3402 } |
|
3403 |
|
3404 ComplexColumnVector |
|
3405 ComplexMatrix::diag (int k) const |
|
3406 { |
|
3407 int nnr = rows (); |
|
3408 int nnc = cols (); |
|
3409 if (k > 0) |
|
3410 nnc -= k; |
|
3411 else if (k < 0) |
|
3412 nnr += k; |
|
3413 |
|
3414 ComplexColumnVector d; |
|
3415 |
|
3416 if (nnr > 0 && nnc > 0) |
|
3417 { |
|
3418 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
3419 |
|
3420 d.resize (ndiag); |
|
3421 |
|
3422 if (k > 0) |
|
3423 { |
|
3424 for (int i = 0; i < ndiag; i++) |
|
3425 d.elem (i) = elem (i, i+k); |
|
3426 } |
|
3427 else if ( k < 0) |
|
3428 { |
|
3429 for (int i = 0; i < ndiag; i++) |
|
3430 d.elem (i) = elem (i-k, i); |
|
3431 } |
|
3432 else |
|
3433 { |
|
3434 for (int i = 0; i < ndiag; i++) |
|
3435 d.elem (i) = elem (i, i); |
|
3436 } |
|
3437 } |
|
3438 else |
|
3439 cerr << "diag: requested diagonal out of range\n"; |
|
3440 |
|
3441 return d; |
|
3442 } |
|
3443 |
2354
|
3444 bool |
|
3445 ComplexMatrix::row_is_real_only (int i) const |
|
3446 { |
|
3447 bool retval = true; |
|
3448 |
|
3449 int nc = columns (); |
|
3450 |
|
3451 for (int j = 0; j < nc; j++) |
|
3452 { |
|
3453 if (imag (elem (i, j)) != 0.0) |
|
3454 { |
|
3455 retval = false; |
|
3456 break; |
|
3457 } |
|
3458 } |
|
3459 |
|
3460 return retval; |
|
3461 } |
|
3462 |
|
3463 bool |
|
3464 ComplexMatrix::column_is_real_only (int j) const |
|
3465 { |
|
3466 bool retval = true; |
|
3467 |
|
3468 int nr = rows (); |
|
3469 |
|
3470 for (int i = 0; i < nr; i++) |
|
3471 { |
|
3472 if (imag (elem (i, j)) != 0.0) |
|
3473 { |
|
3474 retval = false; |
|
3475 break; |
|
3476 } |
|
3477 } |
|
3478 |
|
3479 return retval; |
|
3480 } |
891
|
3481 |
458
|
3482 ComplexColumnVector |
|
3483 ComplexMatrix::row_min (void) const |
|
3484 { |
2354
|
3485 Array<int> index; |
|
3486 return row_min (index); |
458
|
3487 } |
|
3488 |
|
3489 ComplexColumnVector |
2354
|
3490 ComplexMatrix::row_min (Array<int>& index) const |
458
|
3491 { |
|
3492 ComplexColumnVector result; |
|
3493 |
|
3494 int nr = rows (); |
|
3495 int nc = cols (); |
|
3496 |
|
3497 if (nr > 0 && nc > 0) |
|
3498 { |
|
3499 result.resize (nr); |
2354
|
3500 index.resize (nr); |
458
|
3501 |
|
3502 for (int i = 0; i < nr; i++) |
|
3503 { |
2354
|
3504 int idx = 0; |
|
3505 |
|
3506 Complex tmp_min = elem (i, idx); |
|
3507 |
|
3508 bool real_only = row_is_real_only (i); |
|
3509 |
|
3510 double abs_min = real_only ? real (tmp_min) : abs (tmp_min); |
|
3511 |
|
3512 if (xisnan (tmp_min)) |
|
3513 idx = -1; |
891
|
3514 else |
|
3515 { |
|
3516 for (int j = 1; j < nc; j++) |
2354
|
3517 { |
|
3518 Complex tmp = elem (i, j); |
|
3519 |
|
3520 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
3521 |
|
3522 if (xisnan (tmp)) |
|
3523 { |
|
3524 idx = -1; |
|
3525 break; |
|
3526 } |
|
3527 else if (abs_tmp < abs_min) |
|
3528 { |
|
3529 idx = j; |
|
3530 tmp_min = tmp; |
|
3531 abs_min = abs_tmp; |
|
3532 } |
|
3533 } |
|
3534 |
|
3535 result.elem (i) = (idx < 0) ? Complex_NaN_result : tmp_min; |
|
3536 index.elem (i) = idx; |
891
|
3537 } |
458
|
3538 } |
|
3539 } |
|
3540 |
|
3541 return result; |
|
3542 } |
|
3543 |
|
3544 ComplexColumnVector |
|
3545 ComplexMatrix::row_max (void) const |
|
3546 { |
2354
|
3547 Array<int> index; |
|
3548 return row_max (index); |
458
|
3549 } |
|
3550 |
|
3551 ComplexColumnVector |
2354
|
3552 ComplexMatrix::row_max (Array<int>& index) const |
458
|
3553 { |
|
3554 ComplexColumnVector result; |
|
3555 |
|
3556 int nr = rows (); |
|
3557 int nc = cols (); |
|
3558 |
|
3559 if (nr > 0 && nc > 0) |
|
3560 { |
|
3561 result.resize (nr); |
2354
|
3562 index.resize (nr); |
458
|
3563 |
|
3564 for (int i = 0; i < nr; i++) |
|
3565 { |
2354
|
3566 int idx = 0; |
|
3567 |
|
3568 Complex tmp_max = elem (i, idx); |
|
3569 |
|
3570 bool real_only = row_is_real_only (i); |
|
3571 |
|
3572 double abs_max = real_only ? real (tmp_max) : abs (tmp_max); |
|
3573 |
|
3574 if (xisnan (tmp_max)) |
|
3575 idx = -1; |
891
|
3576 else |
|
3577 { |
|
3578 for (int j = 1; j < nc; j++) |
2354
|
3579 { |
|
3580 Complex tmp = elem (i, j); |
|
3581 |
|
3582 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
3583 |
|
3584 if (xisnan (tmp)) |
|
3585 { |
|
3586 idx = -1; |
|
3587 break; |
|
3588 } |
|
3589 else if (abs_tmp > abs_max) |
|
3590 { |
|
3591 idx = j; |
|
3592 tmp_max = tmp; |
|
3593 abs_max = abs_tmp; |
|
3594 } |
|
3595 } |
|
3596 |
|
3597 result.elem (i) = (idx < 0) ? Complex_NaN_result : tmp_max; |
|
3598 index.elem (i) = idx; |
891
|
3599 } |
458
|
3600 } |
|
3601 } |
|
3602 |
|
3603 return result; |
|
3604 } |
|
3605 |
|
3606 ComplexRowVector |
|
3607 ComplexMatrix::column_min (void) const |
|
3608 { |
2354
|
3609 Array<int> index; |
|
3610 return column_min (index); |
458
|
3611 } |
|
3612 |
|
3613 ComplexRowVector |
2354
|
3614 ComplexMatrix::column_min (Array<int>& index) const |
458
|
3615 { |
|
3616 ComplexRowVector result; |
|
3617 |
|
3618 int nr = rows (); |
|
3619 int nc = cols (); |
|
3620 |
|
3621 if (nr > 0 && nc > 0) |
|
3622 { |
|
3623 result.resize (nc); |
2354
|
3624 index.resize (nc); |
458
|
3625 |
|
3626 for (int j = 0; j < nc; j++) |
|
3627 { |
2354
|
3628 int idx = 0; |
|
3629 |
|
3630 Complex tmp_min = elem (idx, j); |
|
3631 |
|
3632 bool real_only = column_is_real_only (j); |
|
3633 |
|
3634 double abs_min = real_only ? real (tmp_min) : abs (tmp_min); |
|
3635 |
|
3636 if (xisnan (tmp_min)) |
|
3637 idx = -1; |
891
|
3638 else |
|
3639 { |
|
3640 for (int i = 1; i < nr; i++) |
2354
|
3641 { |
|
3642 Complex tmp = elem (i, j); |
|
3643 |
|
3644 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
3645 |
|
3646 if (xisnan (tmp)) |
|
3647 { |
|
3648 idx = -1; |
|
3649 break; |
|
3650 } |
|
3651 else if (abs_tmp < abs_min) |
|
3652 { |
|
3653 idx = i; |
|
3654 tmp_min = tmp; |
|
3655 abs_min = abs_tmp; |
|
3656 } |
|
3657 } |
|
3658 |
|
3659 result.elem (j) = (idx < 0) ? Complex_NaN_result : tmp_min; |
|
3660 index.elem (j) = idx; |
891
|
3661 } |
458
|
3662 } |
|
3663 } |
|
3664 |
|
3665 return result; |
|
3666 } |
|
3667 |
|
3668 ComplexRowVector |
|
3669 ComplexMatrix::column_max (void) const |
|
3670 { |
2354
|
3671 Array<int> index; |
|
3672 return column_max (index); |
458
|
3673 } |
|
3674 |
|
3675 ComplexRowVector |
2354
|
3676 ComplexMatrix::column_max (Array<int>& index) const |
458
|
3677 { |
|
3678 ComplexRowVector result; |
|
3679 |
|
3680 int nr = rows (); |
|
3681 int nc = cols (); |
|
3682 |
|
3683 if (nr > 0 && nc > 0) |
|
3684 { |
|
3685 result.resize (nc); |
2354
|
3686 index.resize (nc); |
458
|
3687 |
|
3688 for (int j = 0; j < nc; j++) |
|
3689 { |
2354
|
3690 int idx = 0; |
|
3691 |
|
3692 Complex tmp_max = elem (idx, j); |
|
3693 |
|
3694 bool real_only = column_is_real_only (j); |
|
3695 |
|
3696 double abs_max = real_only ? real (tmp_max) : abs (tmp_max); |
|
3697 |
|
3698 if (xisnan (tmp_max)) |
|
3699 idx = -1; |
891
|
3700 else |
|
3701 { |
|
3702 for (int i = 1; i < nr; i++) |
2354
|
3703 { |
|
3704 Complex tmp = elem (i, j); |
|
3705 |
|
3706 double abs_tmp = real_only ? real (tmp) : abs (tmp); |
|
3707 |
|
3708 if (xisnan (tmp)) |
|
3709 { |
|
3710 idx = -1; |
|
3711 break; |
|
3712 } |
|
3713 else if (abs_tmp > abs_max) |
|
3714 { |
|
3715 idx = i; |
|
3716 tmp_max = tmp; |
|
3717 abs_max = abs_tmp; |
|
3718 } |
|
3719 } |
|
3720 |
|
3721 result.elem (j) = (idx < 0) ? Complex_NaN_result : tmp_max; |
|
3722 index.elem (j) = idx; |
891
|
3723 } |
458
|
3724 } |
|
3725 } |
|
3726 |
|
3727 return result; |
|
3728 } |
|
3729 |
|
3730 // i/o |
|
3731 |
|
3732 ostream& |
|
3733 operator << (ostream& os, const ComplexMatrix& a) |
|
3734 { |
|
3735 // int field_width = os.precision () + 7; |
|
3736 for (int i = 0; i < a.rows (); i++) |
|
3737 { |
|
3738 for (int j = 0; j < a.cols (); j++) |
|
3739 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
3740 os << "\n"; |
|
3741 } |
|
3742 return os; |
|
3743 } |
|
3744 |
|
3745 istream& |
|
3746 operator >> (istream& is, ComplexMatrix& a) |
|
3747 { |
|
3748 int nr = a.rows (); |
|
3749 int nc = a.cols (); |
|
3750 |
|
3751 if (nr < 1 || nc < 1) |
|
3752 is.clear (ios::badbit); |
|
3753 else |
|
3754 { |
|
3755 Complex tmp; |
|
3756 for (int i = 0; i < nr; i++) |
|
3757 for (int j = 0; j < nc; j++) |
|
3758 { |
|
3759 is >> tmp; |
|
3760 if (is) |
|
3761 a.elem (i, j) = tmp; |
|
3762 else |
|
3763 break; |
|
3764 } |
|
3765 } |
|
3766 |
|
3767 return is; |
|
3768 } |
|
3769 |
1819
|
3770 ComplexMatrix |
|
3771 Givens (const Complex& x, const Complex& y) |
|
3772 { |
|
3773 double cc; |
|
3774 Complex cs, temp_r; |
|
3775 |
|
3776 F77_FCN (zlartg, ZLARTG) (x, y, cc, cs, temp_r); |
|
3777 |
|
3778 ComplexMatrix g (2, 2); |
|
3779 |
|
3780 g.elem (0, 0) = cc; |
|
3781 g.elem (1, 1) = cc; |
|
3782 g.elem (0, 1) = cs; |
|
3783 g.elem (1, 0) = -conj (cs); |
|
3784 |
|
3785 return g; |
|
3786 } |
|
3787 |
|
3788 ComplexMatrix |
|
3789 Sylvester (const ComplexMatrix& a, const ComplexMatrix& b, |
|
3790 const ComplexMatrix& c) |
|
3791 { |
|
3792 ComplexMatrix retval; |
|
3793 |
|
3794 // XXX FIXME XXX -- need to check that a, b, and c are all the same |
|
3795 // size. |
|
3796 |
|
3797 // Compute Schur decompositions |
|
3798 |
|
3799 ComplexSCHUR as (a, "U"); |
|
3800 ComplexSCHUR bs (b, "U"); |
|
3801 |
|
3802 // Transform c to new coordinates. |
|
3803 |
|
3804 ComplexMatrix ua = as.unitary_matrix (); |
|
3805 ComplexMatrix sch_a = as.schur_matrix (); |
|
3806 |
|
3807 ComplexMatrix ub = bs.unitary_matrix (); |
|
3808 ComplexMatrix sch_b = bs.schur_matrix (); |
|
3809 |
|
3810 ComplexMatrix cx = ua.hermitian () * c * ub; |
|
3811 |
|
3812 // Solve the sylvester equation, back-transform, and return the |
|
3813 // solution. |
|
3814 |
|
3815 int a_nr = a.rows (); |
|
3816 int b_nr = b.rows (); |
|
3817 |
|
3818 double scale; |
|
3819 int info; |
1950
|
3820 |
|
3821 Complex *pa = sch_a.fortran_vec (); |
|
3822 Complex *pb = sch_b.fortran_vec (); |
|
3823 Complex *px = cx.fortran_vec (); |
1819
|
3824 |
1950
|
3825 F77_XFCN (ztrsyl, ZTRSYL, ("N", "N", 1, a_nr, b_nr, pa, a_nr, pb, |
|
3826 b_nr, px, a_nr, scale, |
|
3827 info, 1L, 1L)); |
|
3828 |
|
3829 if (f77_exception_encountered) |
|
3830 (*current_liboctave_error_handler) ("unrecoverable error in ztrsyl"); |
|
3831 else |
|
3832 { |
|
3833 // XXX FIXME XXX -- check info? |
|
3834 |
|
3835 retval = -ua * cx * ub.hermitian (); |
|
3836 } |
1819
|
3837 |
|
3838 return retval; |
|
3839 } |
|
3840 |
458
|
3841 /* |
|
3842 ;;; Local Variables: *** |
|
3843 ;;; mode: C++ *** |
|
3844 ;;; End: *** |
|
3845 */ |