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