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