458
|
1 // Matrix manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 John W. Eaton |
458
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
458
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
458
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
458
|
30 #endif |
|
31 |
1367
|
32 #include <cfloat> |
|
33 |
458
|
34 #include <iostream.h> |
1367
|
35 |
|
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" |
1847
|
42 #include "f77-fcn.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 { |
1948
|
804 ComplexMatrix retval; |
|
805 |
458
|
806 int nr = rows (); |
|
807 int nc = cols (); |
1948
|
808 |
458
|
809 if (nr != nc) |
1948
|
810 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
458
|
811 else |
|
812 { |
1948
|
813 info = 0; |
|
814 |
|
815 Array<int> ipvt (nr); |
|
816 int *pipvt = ipvt.fortran_vec (); |
|
817 |
|
818 Array<Complex> z (nr); |
|
819 Complex *pz = z.fortran_vec (); |
|
820 |
|
821 retval = *this; |
|
822 Complex *tmp_data = retval.fortran_vec (); |
|
823 |
|
824 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nc, pipvt, rcond, pz)); |
|
825 |
|
826 if (f77_exception_encountered) |
|
827 (*current_liboctave_error_handler) ("unrecoverable error in zgeco"); |
|
828 else |
|
829 { |
|
830 volatile double rcond_plus_one = rcond + 1.0; |
|
831 |
|
832 if (rcond_plus_one == 1.0) |
|
833 info = -1; |
|
834 |
|
835 if (info == -1 && ! force) |
|
836 retval = *this; // Restore contents. |
|
837 else |
|
838 { |
|
839 Complex *dummy = 0; |
|
840 |
|
841 F77_XFCN (zgedi, ZGEDI, (tmp_data, nr, nc, pipvt, dummy, |
|
842 pz, 1)); |
|
843 |
|
844 if (f77_exception_encountered) |
|
845 (*current_liboctave_error_handler) |
|
846 ("unrecoverable error in zgedi"); |
|
847 } |
|
848 } |
458
|
849 } |
|
850 |
1948
|
851 return retval; |
458
|
852 } |
|
853 |
|
854 ComplexMatrix |
740
|
855 ComplexMatrix::pseudo_inverse (double tol) |
|
856 { |
1549
|
857 ComplexMatrix retval; |
|
858 |
740
|
859 ComplexSVD result (*this); |
|
860 |
|
861 DiagMatrix S = result.singular_values (); |
|
862 ComplexMatrix U = result.left_singular_matrix (); |
|
863 ComplexMatrix V = result.right_singular_matrix (); |
|
864 |
|
865 ColumnVector sigma = S.diag (); |
|
866 |
|
867 int r = sigma.length () - 1; |
|
868 int nr = rows (); |
|
869 int nc = cols (); |
|
870 |
|
871 if (tol <= 0.0) |
|
872 { |
|
873 if (nr > nc) |
|
874 tol = nr * sigma.elem (0) * DBL_EPSILON; |
|
875 else |
|
876 tol = nc * sigma.elem (0) * DBL_EPSILON; |
|
877 } |
|
878 |
|
879 while (r >= 0 && sigma.elem (r) < tol) |
|
880 r--; |
|
881 |
|
882 if (r < 0) |
1549
|
883 retval = ComplexMatrix (nc, nr, 0.0); |
740
|
884 else |
|
885 { |
|
886 ComplexMatrix Ur = U.extract (0, 0, nr-1, r); |
|
887 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); |
|
888 ComplexMatrix Vr = V.extract (0, 0, nc-1, r); |
1549
|
889 retval = Vr * D * Ur.hermitian (); |
740
|
890 } |
1549
|
891 |
|
892 return retval; |
740
|
893 } |
|
894 |
|
895 ComplexMatrix |
458
|
896 ComplexMatrix::fourier (void) const |
|
897 { |
1948
|
898 ComplexMatrix retval; |
|
899 |
458
|
900 int nr = rows (); |
|
901 int nc = cols (); |
1948
|
902 |
458
|
903 int npts, nsamples; |
1948
|
904 |
458
|
905 if (nr == 1 || nc == 1) |
|
906 { |
|
907 npts = nr > nc ? nr : nc; |
|
908 nsamples = 1; |
|
909 } |
|
910 else |
|
911 { |
|
912 npts = nr; |
|
913 nsamples = nc; |
|
914 } |
|
915 |
|
916 int nn = 4*npts+15; |
1948
|
917 |
|
918 Array<Complex> wsave (nn); |
|
919 Complex *pwsave = wsave.fortran_vec (); |
|
920 |
|
921 retval = *this; |
|
922 Complex *tmp_data = retval.fortran_vec (); |
|
923 |
|
924 F77_FCN (cffti, CFFTI) (npts, pwsave); |
458
|
925 |
|
926 for (int j = 0; j < nsamples; j++) |
1948
|
927 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
928 |
|
929 return retval; |
458
|
930 } |
|
931 |
|
932 ComplexMatrix |
|
933 ComplexMatrix::ifourier (void) const |
|
934 { |
1948
|
935 ComplexMatrix retval; |
|
936 |
458
|
937 int nr = rows (); |
|
938 int nc = cols (); |
1948
|
939 |
458
|
940 int npts, nsamples; |
1948
|
941 |
458
|
942 if (nr == 1 || nc == 1) |
|
943 { |
|
944 npts = nr > nc ? nr : nc; |
|
945 nsamples = 1; |
|
946 } |
|
947 else |
|
948 { |
|
949 npts = nr; |
|
950 nsamples = nc; |
|
951 } |
|
952 |
|
953 int nn = 4*npts+15; |
1948
|
954 |
|
955 Array<Complex> wsave (nn); |
|
956 Complex *pwsave = wsave.fortran_vec (); |
|
957 |
|
958 retval = *this; |
|
959 Complex *tmp_data = retval.fortran_vec (); |
|
960 |
|
961 F77_FCN (cffti, CFFTI) (npts, pwsave); |
458
|
962 |
|
963 for (int j = 0; j < nsamples; j++) |
1948
|
964 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
458
|
965 |
1321
|
966 for (int j = 0; j < npts*nsamples; j++) |
458
|
967 tmp_data[j] = tmp_data[j] / (double) npts; |
|
968 |
1948
|
969 return retval; |
458
|
970 } |
|
971 |
677
|
972 ComplexMatrix |
|
973 ComplexMatrix::fourier2d (void) const |
|
974 { |
1948
|
975 ComplexMatrix retval; |
|
976 |
677
|
977 int nr = rows (); |
|
978 int nc = cols (); |
1948
|
979 |
677
|
980 int npts, nsamples; |
1948
|
981 |
677
|
982 if (nr == 1 || nc == 1) |
|
983 { |
|
984 npts = nr > nc ? nr : nc; |
|
985 nsamples = 1; |
|
986 } |
|
987 else |
|
988 { |
|
989 npts = nr; |
|
990 nsamples = nc; |
|
991 } |
|
992 |
|
993 int nn = 4*npts+15; |
1948
|
994 |
|
995 Array<Complex> wsave (nn); |
|
996 Complex *pwsave = wsave.fortran_vec (); |
|
997 |
|
998 retval = *this; |
|
999 Complex *tmp_data = retval.fortran_vec (); |
|
1000 |
|
1001 F77_FCN (cffti, CFFTI) (npts, pwsave); |
677
|
1002 |
|
1003 for (int j = 0; j < nsamples; j++) |
1948
|
1004 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
677
|
1005 |
|
1006 npts = nc; |
|
1007 nsamples = nr; |
|
1008 nn = 4*npts+15; |
1948
|
1009 |
|
1010 wsave.resize (nn); |
|
1011 pwsave = wsave.fortran_vec (); |
|
1012 |
|
1013 Array<Complex> row (npts); |
|
1014 Complex *prow = row.fortran_vec (); |
|
1015 |
|
1016 F77_FCN (cffti, CFFTI) (npts, pwsave); |
677
|
1017 |
1321
|
1018 for (int j = 0; j < nsamples; j++) |
677
|
1019 { |
|
1020 for (int i = 0; i < npts; i++) |
1948
|
1021 prow[i] = tmp_data[i*nr + j]; |
|
1022 |
|
1023 F77_FCN (cfftf, CFFTF) (npts, prow, pwsave); |
677
|
1024 |
1321
|
1025 for (int i = 0; i < npts; i++) |
1948
|
1026 tmp_data[i*nr + j] = prow[i]; |
677
|
1027 } |
|
1028 |
1948
|
1029 return retval; |
677
|
1030 } |
|
1031 |
|
1032 ComplexMatrix |
|
1033 ComplexMatrix::ifourier2d (void) const |
|
1034 { |
1948
|
1035 ComplexMatrix retval; |
|
1036 |
677
|
1037 int nr = rows (); |
|
1038 int nc = cols (); |
1948
|
1039 |
677
|
1040 int npts, nsamples; |
1948
|
1041 |
677
|
1042 if (nr == 1 || nc == 1) |
|
1043 { |
|
1044 npts = nr > nc ? nr : nc; |
|
1045 nsamples = 1; |
|
1046 } |
|
1047 else |
|
1048 { |
|
1049 npts = nr; |
|
1050 nsamples = nc; |
|
1051 } |
|
1052 |
|
1053 int nn = 4*npts+15; |
1948
|
1054 |
|
1055 Array<Complex> wsave (nn); |
|
1056 Complex *pwsave = wsave.fortran_vec (); |
|
1057 |
|
1058 retval = *this; |
|
1059 Complex *tmp_data = retval.fortran_vec (); |
|
1060 |
|
1061 F77_FCN (cffti, CFFTI) (npts, pwsave); |
677
|
1062 |
|
1063 for (int j = 0; j < nsamples; j++) |
1948
|
1064 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
677
|
1065 |
1321
|
1066 for (int j = 0; j < npts*nsamples; j++) |
677
|
1067 tmp_data[j] = tmp_data[j] / (double) npts; |
|
1068 |
|
1069 npts = nc; |
|
1070 nsamples = nr; |
|
1071 nn = 4*npts+15; |
1948
|
1072 |
|
1073 wsave.resize (nn); |
|
1074 pwsave = wsave.fortran_vec (); |
|
1075 |
|
1076 Array<Complex> row (npts); |
|
1077 Complex *prow = row.fortran_vec (); |
|
1078 |
|
1079 F77_FCN (cffti, CFFTI) (npts, pwsave); |
677
|
1080 |
1321
|
1081 for (int j = 0; j < nsamples; j++) |
677
|
1082 { |
|
1083 for (int i = 0; i < npts; i++) |
1948
|
1084 prow[i] = tmp_data[i*nr + j]; |
|
1085 |
|
1086 F77_FCN (cfftb, CFFTB) (npts, prow, pwsave); |
677
|
1087 |
1321
|
1088 for (int i = 0; i < npts; i++) |
1948
|
1089 tmp_data[i*nr + j] = prow[i] / (double) npts; |
677
|
1090 } |
|
1091 |
1948
|
1092 return retval; |
677
|
1093 } |
|
1094 |
458
|
1095 ComplexDET |
|
1096 ComplexMatrix::determinant (void) const |
|
1097 { |
|
1098 int info; |
|
1099 double rcond; |
|
1100 return determinant (info, rcond); |
|
1101 } |
|
1102 |
|
1103 ComplexDET |
|
1104 ComplexMatrix::determinant (int& info) const |
|
1105 { |
|
1106 double rcond; |
|
1107 return determinant (info, rcond); |
|
1108 } |
|
1109 |
|
1110 ComplexDET |
532
|
1111 ComplexMatrix::determinant (int& info, double& rcond) const |
458
|
1112 { |
|
1113 ComplexDET retval; |
|
1114 |
|
1115 int nr = rows (); |
|
1116 int nc = cols (); |
|
1117 |
|
1118 if (nr == 0 || nc == 0) |
|
1119 { |
|
1120 Complex d[2]; |
|
1121 d[0] = 1.0; |
|
1122 d[1] = 0.0; |
|
1123 retval = ComplexDET (d); |
|
1124 } |
|
1125 else |
|
1126 { |
|
1127 info = 0; |
1948
|
1128 |
|
1129 Array<int> ipvt (nr); |
|
1130 int *pipvt = ipvt.fortran_vec (); |
|
1131 |
|
1132 Array<Complex> z (nr); |
|
1133 Complex *pz = z.fortran_vec (); |
|
1134 |
|
1135 ComplexMatrix atmp = *this; |
|
1136 Complex *tmp_data = atmp.fortran_vec (); |
|
1137 |
|
1138 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nr, pipvt, rcond, pz)); |
|
1139 |
|
1140 if (f77_exception_encountered) |
|
1141 (*current_liboctave_error_handler) ("unrecoverable error in zgeco"); |
458
|
1142 else |
|
1143 { |
1948
|
1144 volatile double rcond_plus_one = rcond + 1.0; |
|
1145 |
|
1146 if (rcond_plus_one == 1.0) |
|
1147 { |
|
1148 info = -1; |
|
1149 retval = ComplexDET (); |
|
1150 } |
|
1151 else |
|
1152 { |
|
1153 Complex d[2]; |
|
1154 |
|
1155 F77_XFCN (zgedi, ZGEDI, (tmp_data, nr, nr, pipvt, d, pz, 10)); |
|
1156 |
|
1157 if (f77_exception_encountered) |
|
1158 (*current_liboctave_error_handler) |
|
1159 ("unrecoverable error in dgedi"); |
|
1160 else |
|
1161 retval = ComplexDET (d); |
|
1162 } |
458
|
1163 } |
|
1164 } |
|
1165 |
|
1166 return retval; |
|
1167 } |
|
1168 |
|
1169 ComplexMatrix |
|
1170 ComplexMatrix::solve (const Matrix& b) const |
|
1171 { |
|
1172 int info; |
|
1173 double rcond; |
|
1174 return solve (b, info, rcond); |
|
1175 } |
|
1176 |
|
1177 ComplexMatrix |
|
1178 ComplexMatrix::solve (const Matrix& b, int& info) const |
|
1179 { |
|
1180 double rcond; |
|
1181 return solve (b, info, rcond); |
|
1182 } |
|
1183 |
|
1184 ComplexMatrix |
|
1185 ComplexMatrix::solve (const Matrix& b, int& info, double& rcond) const |
|
1186 { |
|
1187 ComplexMatrix tmp (b); |
|
1188 return solve (tmp, info, rcond); |
|
1189 } |
|
1190 |
|
1191 ComplexMatrix |
|
1192 ComplexMatrix::solve (const ComplexMatrix& b) const |
|
1193 { |
|
1194 int info; |
|
1195 double rcond; |
|
1196 return solve (b, info, rcond); |
|
1197 } |
|
1198 |
|
1199 ComplexMatrix |
|
1200 ComplexMatrix::solve (const ComplexMatrix& b, int& info) const |
|
1201 { |
|
1202 double rcond; |
|
1203 return solve (b, info, rcond); |
|
1204 } |
|
1205 ComplexMatrix |
532
|
1206 ComplexMatrix::solve (const ComplexMatrix& b, int& info, double& rcond) const |
458
|
1207 { |
|
1208 ComplexMatrix retval; |
|
1209 |
|
1210 int nr = rows (); |
|
1211 int nc = cols (); |
1948
|
1212 |
|
1213 if (nr == 0 || nc == 0 || nr != nc || nr != b.rows ()) |
|
1214 (*current_liboctave_error_handler) |
|
1215 ("matrix dimension mismatch in solution of linear equations"); |
458
|
1216 else |
|
1217 { |
1948
|
1218 info = 0; |
|
1219 |
|
1220 Array<int> ipvt (nr); |
|
1221 int *pipvt = ipvt.fortran_vec (); |
|
1222 |
|
1223 Array<Complex> z (nr); |
|
1224 Complex *pz = z.fortran_vec (); |
|
1225 |
|
1226 ComplexMatrix atmp = *this; |
|
1227 Complex *tmp_data = atmp.fortran_vec (); |
|
1228 |
|
1229 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nr, pipvt, rcond, pz)); |
|
1230 |
|
1231 if (f77_exception_encountered) |
|
1232 (*current_liboctave_error_handler) ("unrecoverable error in zgeco"); |
|
1233 else |
|
1234 { |
|
1235 volatile double rcond_plus_one = rcond + 1.0; |
|
1236 |
|
1237 if (rcond_plus_one == 1.0) |
|
1238 { |
|
1239 info = -2; |
|
1240 } |
|
1241 else |
|
1242 { |
|
1243 retval = b; |
|
1244 Complex *result = retval.fortran_vec (); |
|
1245 |
|
1246 int b_nc = b.cols (); |
|
1247 |
|
1248 for (volatile int j = 0; j < b_nc; j++) |
|
1249 { |
|
1250 F77_XFCN (zgesl, ZGESL, (tmp_data, nr, nr, pipvt, |
|
1251 &result[nr*j], 0)); |
|
1252 |
|
1253 if (f77_exception_encountered) |
|
1254 { |
|
1255 (*current_liboctave_error_handler) |
|
1256 ("unrecoverable error in dgesl"); |
|
1257 |
|
1258 break; |
|
1259 } |
|
1260 } |
|
1261 } |
|
1262 } |
458
|
1263 } |
|
1264 |
|
1265 return retval; |
|
1266 } |
|
1267 |
|
1268 ComplexColumnVector |
|
1269 ComplexMatrix::solve (const ComplexColumnVector& b) const |
|
1270 { |
|
1271 int info; |
|
1272 double rcond; |
|
1273 return solve (b, info, rcond); |
|
1274 } |
|
1275 |
|
1276 ComplexColumnVector |
|
1277 ComplexMatrix::solve (const ComplexColumnVector& b, int& info) const |
|
1278 { |
|
1279 double rcond; |
|
1280 return solve (b, info, rcond); |
|
1281 } |
|
1282 |
|
1283 ComplexColumnVector |
|
1284 ComplexMatrix::solve (const ComplexColumnVector& b, int& info, |
532
|
1285 double& rcond) const |
458
|
1286 { |
|
1287 ComplexColumnVector retval; |
|
1288 |
|
1289 int nr = rows (); |
|
1290 int nc = cols (); |
1948
|
1291 |
|
1292 if (nr == 0 || nc == 0 || nr != nc || nr != b.length ()) |
|
1293 (*current_liboctave_error_handler) |
|
1294 ("matrix dimension mismatch in solution of linear equations"); |
458
|
1295 else |
|
1296 { |
1948
|
1297 info = 0; |
|
1298 |
|
1299 Array<int> ipvt (nr); |
|
1300 int *pipvt = ipvt.fortran_vec (); |
|
1301 |
|
1302 Array<Complex> z (nr); |
|
1303 Complex *pz = z.fortran_vec (); |
|
1304 |
|
1305 ComplexMatrix atmp = *this; |
|
1306 Complex *tmp_data = atmp.fortran_vec (); |
|
1307 |
|
1308 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nr, pipvt, rcond, pz)); |
|
1309 |
|
1310 if (f77_exception_encountered) |
|
1311 (*current_liboctave_error_handler) |
|
1312 ("unrecoverable error in dgeco"); |
|
1313 else |
|
1314 { |
|
1315 volatile double rcond_plus_one = rcond + 1.0; |
|
1316 |
|
1317 if (rcond_plus_one == 1.0) |
|
1318 { |
|
1319 info = -2; |
|
1320 } |
|
1321 else |
|
1322 { |
|
1323 retval = b; |
|
1324 Complex *result = retval.fortran_vec (); |
|
1325 |
|
1326 F77_XFCN (zgesl, ZGESL, (tmp_data, nr, nr, pipvt, result, 0)); |
|
1327 |
|
1328 if (f77_exception_encountered) |
|
1329 (*current_liboctave_error_handler) |
|
1330 ("unrecoverable error in dgesl"); |
|
1331 } |
|
1332 } |
458
|
1333 } |
|
1334 |
|
1335 return retval; |
|
1336 } |
|
1337 |
|
1338 ComplexMatrix |
|
1339 ComplexMatrix::lssolve (const ComplexMatrix& b) const |
|
1340 { |
|
1341 int info; |
|
1342 int rank; |
|
1343 return lssolve (b, info, rank); |
|
1344 } |
|
1345 |
|
1346 ComplexMatrix |
|
1347 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info) const |
|
1348 { |
|
1349 int rank; |
|
1350 return lssolve (b, info, rank); |
|
1351 } |
|
1352 |
|
1353 ComplexMatrix |
|
1354 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info, int& rank) const |
|
1355 { |
1948
|
1356 ComplexMatrix retval; |
|
1357 |
458
|
1358 int nrhs = b.cols (); |
|
1359 |
|
1360 int m = rows (); |
|
1361 int n = cols (); |
|
1362 |
|
1363 if (m == 0 || n == 0 || m != b.rows ()) |
1948
|
1364 (*current_liboctave_error_handler) |
|
1365 ("matrix dimension mismatch solution of linear equations"); |
|
1366 else |
458
|
1367 { |
1948
|
1368 ComplexMatrix atmp = *this; |
|
1369 Complex *tmp_data = atmp.fortran_vec (); |
|
1370 |
|
1371 int nrr = m > n ? m : n; |
|
1372 ComplexMatrix result (nrr, nrhs); |
|
1373 |
|
1374 for (int j = 0; j < nrhs; j++) |
|
1375 for (int i = 0; i < m; i++) |
|
1376 result.elem (i, j) = b.elem (i, j); |
|
1377 |
|
1378 Complex *presult = result.fortran_vec (); |
|
1379 |
|
1380 int len_s = m < n ? m : n; |
|
1381 Array<double> s (len_s); |
|
1382 double *ps = s.fortran_vec (); |
|
1383 double rcond = -1.0; |
|
1384 int lwork; |
|
1385 if (m < n) |
|
1386 lwork = 2*m + (nrhs > n ? nrhs : n); |
|
1387 else |
|
1388 lwork = 2*n + (nrhs > m ? nrhs : m); |
|
1389 |
|
1390 Array<Complex> work (lwork); |
|
1391 Complex *pwork = work.fortran_vec (); |
|
1392 |
|
1393 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1394 lrwork = lrwork > 1 ? lrwork : 1; |
|
1395 Array<double> rwork (lrwork); |
|
1396 double *prwork = rwork.fortran_vec (); |
|
1397 |
|
1398 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
1399 nrr, ps, rcond, rank, pwork, lwork, |
|
1400 prwork, info)); |
|
1401 |
|
1402 if (f77_exception_encountered) |
|
1403 (*current_liboctave_error_handler) ("unrecoverable error in zgelss"); |
|
1404 else |
|
1405 { |
|
1406 ComplexMatrix retval (n, nrhs); |
|
1407 for (int j = 0; j < nrhs; j++) |
|
1408 for (int i = 0; i < n; i++) |
|
1409 retval.elem (i, j) = result.elem (i, j); |
|
1410 } |
458
|
1411 } |
|
1412 |
|
1413 return retval; |
|
1414 } |
|
1415 |
|
1416 ComplexColumnVector |
|
1417 ComplexMatrix::lssolve (const ComplexColumnVector& b) const |
|
1418 { |
|
1419 int info; |
|
1420 int rank; |
|
1421 return lssolve (b, info, rank); |
|
1422 } |
|
1423 |
|
1424 ComplexColumnVector |
|
1425 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info) const |
|
1426 { |
|
1427 int rank; |
|
1428 return lssolve (b, info, rank); |
|
1429 } |
|
1430 |
|
1431 ComplexColumnVector |
|
1432 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info, |
|
1433 int& rank) const |
|
1434 { |
1948
|
1435 ComplexColumnVector retval; |
|
1436 |
458
|
1437 int nrhs = 1; |
|
1438 |
|
1439 int m = rows (); |
|
1440 int n = cols (); |
|
1441 |
|
1442 if (m == 0 || n == 0 || m != b.length ()) |
1948
|
1443 (*current_liboctave_error_handler) |
|
1444 ("matrix dimension mismatch solution of least squares problem"); |
|
1445 else |
458
|
1446 { |
1948
|
1447 ComplexMatrix atmp = *this; |
|
1448 Complex *tmp_data = atmp.fortran_vec (); |
|
1449 |
|
1450 int nrr = m > n ? m : n; |
|
1451 ComplexColumnVector result (nrr); |
|
1452 |
|
1453 for (int i = 0; i < m; i++) |
|
1454 result.elem (i) = b.elem (i); |
|
1455 |
|
1456 Complex *presult = result.fortran_vec (); |
|
1457 |
|
1458 int len_s = m < n ? m : n; |
|
1459 Array<double> s (len_s); |
|
1460 double *ps = s.fortran_vec (); |
|
1461 |
|
1462 double rcond = -1.0; |
|
1463 |
|
1464 int lwork; |
|
1465 if (m < n) |
|
1466 lwork = 2*m + (nrhs > n ? nrhs : n); |
|
1467 else |
|
1468 lwork = 2*n + (nrhs > m ? nrhs : m); |
|
1469 |
|
1470 Array<Complex> work (lwork); |
|
1471 Complex *pwork = work.fortran_vec (); |
|
1472 |
|
1473 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1474 lrwork = lrwork > 1 ? lrwork : 1; |
|
1475 Array<double> rwork (lrwork); |
|
1476 double *prwork = rwork.fortran_vec (); |
|
1477 |
|
1478 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
1479 nrr, ps, rcond, rank, pwork, lwork, |
|
1480 prwork, info)); |
|
1481 |
|
1482 if (f77_exception_encountered) |
|
1483 (*current_liboctave_error_handler) ("unrecoverable error in zgelss"); |
|
1484 else |
|
1485 { |
|
1486 ComplexColumnVector retval (n); |
|
1487 for (int i = 0; i < n; i++) |
|
1488 retval.elem (i) = result.elem (i); |
|
1489 } |
458
|
1490 } |
|
1491 |
|
1492 return retval; |
|
1493 } |
|
1494 |
1819
|
1495 // Constants for matrix exponential calculation. |
|
1496 |
|
1497 static double padec [] = |
|
1498 { |
|
1499 5.0000000000000000e-1, |
|
1500 1.1666666666666667e-1, |
|
1501 1.6666666666666667e-2, |
|
1502 1.6025641025641026e-3, |
|
1503 1.0683760683760684e-4, |
|
1504 4.8562548562548563e-6, |
|
1505 1.3875013875013875e-7, |
|
1506 1.9270852604185938e-9, |
|
1507 }; |
|
1508 |
|
1509 ComplexMatrix |
|
1510 ComplexMatrix::expm (void) const |
|
1511 { |
|
1512 ComplexMatrix retval; |
|
1513 |
|
1514 ComplexMatrix m = *this; |
|
1515 |
|
1516 int nc = columns (); |
|
1517 |
|
1518 // trace shift value |
|
1519 Complex trshift = 0.0; |
|
1520 |
|
1521 // Preconditioning step 1: trace normalization. |
|
1522 |
|
1523 for (int i = 0; i < nc; i++) |
|
1524 trshift += m.elem (i, i); |
|
1525 |
|
1526 trshift /= nc; |
|
1527 |
|
1528 for (int i = 0; i < nc; i++) |
|
1529 m.elem (i, i) -= trshift; |
|
1530 |
|
1531 // Preconditioning step 2: eigenvalue balancing. |
|
1532 |
|
1533 ComplexAEPBALANCE mbal (m, "B"); |
|
1534 m = mbal.balanced_matrix (); |
|
1535 ComplexMatrix d = mbal.balancing_matrix (); |
|
1536 |
|
1537 // Preconditioning step 3: scaling. |
|
1538 |
|
1539 ColumnVector work (nc); |
|
1540 double inf_norm |
|
1541 = F77_FCN (zlange, ZLANGE) ("I", nc, nc, m.fortran_vec (), nc, |
|
1542 work.fortran_vec ()); |
|
1543 |
|
1544 int sqpow = (int) (inf_norm > 0.0 |
|
1545 ? (1.0 + log (inf_norm) / log (2.0)) |
|
1546 : 0.0); |
|
1547 |
|
1548 // Check whether we need to square at all. |
|
1549 |
|
1550 if (sqpow < 0) |
|
1551 sqpow = 0; |
|
1552 |
|
1553 if (sqpow > 0) |
|
1554 { |
|
1555 double scale_factor = 1.0; |
|
1556 for (int i = 0; i < sqpow; i++) |
|
1557 scale_factor *= 2.0; |
|
1558 |
|
1559 m = m / scale_factor; |
|
1560 } |
|
1561 |
|
1562 // npp, dpp: pade' approx polynomial matrices. |
|
1563 |
|
1564 ComplexMatrix npp (nc, nc, 0.0); |
|
1565 ComplexMatrix dpp = npp; |
|
1566 |
|
1567 // Now powers a^8 ... a^1. |
|
1568 |
|
1569 int minus_one_j = -1; |
|
1570 for (int j = 7; j >= 0; j--) |
|
1571 { |
|
1572 npp = m * npp + m * padec[j]; |
|
1573 dpp = m * dpp + m * (minus_one_j * padec[j]); |
|
1574 minus_one_j *= -1; |
|
1575 } |
|
1576 |
|
1577 // Zero power. |
|
1578 |
|
1579 dpp = -dpp; |
|
1580 for (int j = 0; j < nc; j++) |
|
1581 { |
|
1582 npp.elem (j, j) += 1.0; |
|
1583 dpp.elem (j, j) += 1.0; |
|
1584 } |
|
1585 |
|
1586 // Compute pade approximation = inverse (dpp) * npp. |
|
1587 |
|
1588 retval = dpp.solve (npp); |
|
1589 |
|
1590 // Reverse preconditioning step 3: repeated squaring. |
|
1591 |
|
1592 while (sqpow) |
|
1593 { |
|
1594 retval = retval * retval; |
|
1595 sqpow--; |
|
1596 } |
|
1597 |
|
1598 // Reverse preconditioning step 2: inverse balancing. |
|
1599 // XXX FIXME XXX -- should probably do this with Lapack calls |
|
1600 // instead of a complete matrix inversion. |
|
1601 |
|
1602 retval = retval.transpose (); |
|
1603 d = d.transpose (); |
|
1604 retval = retval * d; |
|
1605 retval = d.solve (retval); |
|
1606 retval = retval.transpose (); |
|
1607 |
|
1608 // Reverse preconditioning step 1: fix trace normalization. |
|
1609 |
|
1610 return retval * exp (trshift); |
|
1611 } |
|
1612 |
1205
|
1613 // column vector by row vector -> matrix operations |
|
1614 |
|
1615 ComplexMatrix |
|
1616 operator * (const ColumnVector& v, const ComplexRowVector& a) |
|
1617 { |
|
1618 ComplexColumnVector tmp (v); |
|
1619 return tmp * a; |
|
1620 } |
|
1621 |
|
1622 ComplexMatrix |
|
1623 operator * (const ComplexColumnVector& a, const RowVector& b) |
|
1624 { |
|
1625 ComplexRowVector tmp (b); |
|
1626 return a * tmp; |
|
1627 } |
|
1628 |
|
1629 ComplexMatrix |
|
1630 operator * (const ComplexColumnVector& v, const ComplexRowVector& a) |
|
1631 { |
1948
|
1632 ComplexMatrix retval; |
|
1633 |
1205
|
1634 int len = v.length (); |
|
1635 int a_len = a.length (); |
1948
|
1636 |
1205
|
1637 if (len != a_len) |
1948
|
1638 (*current_liboctave_error_handler) |
|
1639 ("nonconformant vector multiplication attempted"); |
|
1640 else |
1205
|
1641 { |
1948
|
1642 if (len != 0) |
|
1643 { |
|
1644 retval.resize (len, a_len); |
|
1645 Complex *c = retval.fortran_vec (); |
|
1646 |
|
1647 F77_XFCN (zgemm, ZGEMM, ("N", "N", len, a_len, 1, 1.0, |
|
1648 v.data (), len, a.data (), 1, 0.0, |
|
1649 c, len, 1L, 1L)); |
|
1650 |
|
1651 if (f77_exception_encountered) |
|
1652 (*current_liboctave_error_handler) |
|
1653 ("unrecoverable error in zgemm"); |
|
1654 } |
1205
|
1655 } |
|
1656 |
1948
|
1657 return retval; |
1205
|
1658 } |
|
1659 |
|
1660 // diagonal matrix by scalar -> matrix operations |
|
1661 |
|
1662 ComplexMatrix |
|
1663 operator + (const DiagMatrix& a, const Complex& s) |
|
1664 { |
|
1665 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1666 return a + tmp; |
|
1667 } |
|
1668 |
|
1669 ComplexMatrix |
|
1670 operator - (const DiagMatrix& a, const Complex& s) |
|
1671 { |
|
1672 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1673 return a + tmp; |
|
1674 } |
|
1675 |
|
1676 ComplexMatrix |
|
1677 operator + (const ComplexDiagMatrix& a, double s) |
|
1678 { |
|
1679 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1680 return a + tmp; |
|
1681 } |
|
1682 |
|
1683 ComplexMatrix |
|
1684 operator - (const ComplexDiagMatrix& a, double s) |
|
1685 { |
|
1686 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1687 return a + tmp; |
|
1688 } |
|
1689 |
|
1690 ComplexMatrix |
|
1691 operator + (const ComplexDiagMatrix& a, const Complex& s) |
|
1692 { |
|
1693 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1694 return a + tmp; |
|
1695 } |
|
1696 |
|
1697 ComplexMatrix |
|
1698 operator - (const ComplexDiagMatrix& a, const Complex& s) |
|
1699 { |
|
1700 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1701 return a + tmp; |
|
1702 } |
|
1703 |
|
1704 // scalar by diagonal matrix -> matrix operations |
|
1705 |
|
1706 ComplexMatrix |
|
1707 operator + (const Complex& s, const DiagMatrix& a) |
|
1708 { |
|
1709 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1710 return tmp + a; |
|
1711 } |
|
1712 |
|
1713 ComplexMatrix |
|
1714 operator - (const Complex& s, const DiagMatrix& a) |
|
1715 { |
|
1716 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1717 return tmp - a; |
|
1718 } |
|
1719 |
|
1720 ComplexMatrix |
|
1721 operator + (double s, const ComplexDiagMatrix& a) |
|
1722 { |
|
1723 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1724 return tmp + a; |
|
1725 } |
|
1726 |
|
1727 ComplexMatrix |
|
1728 operator - (double s, const ComplexDiagMatrix& a) |
|
1729 { |
|
1730 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1731 return tmp - a; |
|
1732 } |
|
1733 |
|
1734 ComplexMatrix |
|
1735 operator + (const Complex& s, const ComplexDiagMatrix& a) |
|
1736 { |
|
1737 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1738 return tmp + a; |
|
1739 } |
|
1740 |
|
1741 ComplexMatrix |
|
1742 operator - (const Complex& s, const ComplexDiagMatrix& a) |
|
1743 { |
|
1744 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1745 return tmp - a; |
|
1746 } |
|
1747 |
458
|
1748 // matrix by diagonal matrix -> matrix operations |
|
1749 |
|
1750 ComplexMatrix& |
|
1751 ComplexMatrix::operator += (const DiagMatrix& a) |
|
1752 { |
|
1753 int nr = rows (); |
|
1754 int nc = cols (); |
|
1755 if (nr != a.rows () || nc != a.cols ()) |
|
1756 { |
|
1757 (*current_liboctave_error_handler) |
|
1758 ("nonconformant matrix += operation attempted"); |
889
|
1759 return *this; |
458
|
1760 } |
|
1761 |
|
1762 for (int i = 0; i < a.length (); i++) |
|
1763 elem (i, i) += a.elem (i, i); |
|
1764 |
|
1765 return *this; |
|
1766 } |
|
1767 |
|
1768 ComplexMatrix& |
|
1769 ComplexMatrix::operator -= (const DiagMatrix& a) |
|
1770 { |
|
1771 int nr = rows (); |
|
1772 int nc = cols (); |
|
1773 if (nr != a.rows () || nc != a.cols ()) |
|
1774 { |
|
1775 (*current_liboctave_error_handler) |
|
1776 ("nonconformant matrix -= operation attempted"); |
889
|
1777 return *this; |
458
|
1778 } |
|
1779 |
|
1780 for (int i = 0; i < a.length (); i++) |
|
1781 elem (i, i) -= a.elem (i, i); |
|
1782 |
|
1783 return *this; |
|
1784 } |
|
1785 |
|
1786 ComplexMatrix& |
|
1787 ComplexMatrix::operator += (const ComplexDiagMatrix& a) |
|
1788 { |
|
1789 int nr = rows (); |
|
1790 int nc = cols (); |
|
1791 if (nr != a.rows () || nc != a.cols ()) |
|
1792 { |
|
1793 (*current_liboctave_error_handler) |
|
1794 ("nonconformant matrix += operation attempted"); |
889
|
1795 return *this; |
458
|
1796 } |
|
1797 |
|
1798 for (int i = 0; i < a.length (); i++) |
|
1799 elem (i, i) += a.elem (i, i); |
|
1800 |
|
1801 return *this; |
|
1802 } |
|
1803 |
|
1804 ComplexMatrix& |
|
1805 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) |
|
1806 { |
|
1807 int nr = rows (); |
|
1808 int nc = cols (); |
|
1809 if (nr != a.rows () || nc != a.cols ()) |
|
1810 { |
|
1811 (*current_liboctave_error_handler) |
|
1812 ("nonconformant matrix -= operation attempted"); |
889
|
1813 return *this; |
458
|
1814 } |
|
1815 |
|
1816 for (int i = 0; i < a.length (); i++) |
|
1817 elem (i, i) -= a.elem (i, i); |
|
1818 |
|
1819 return *this; |
|
1820 } |
|
1821 |
1205
|
1822 ComplexMatrix |
|
1823 operator + (const Matrix& m, const ComplexDiagMatrix& a) |
|
1824 { |
|
1825 int nr = m.rows (); |
|
1826 int nc = m.cols (); |
|
1827 if (nr != a.rows () || nc != a.cols ()) |
|
1828 { |
|
1829 (*current_liboctave_error_handler) |
|
1830 ("nonconformant matrix addition attempted"); |
|
1831 return ComplexMatrix (); |
|
1832 } |
|
1833 |
|
1834 if (nr == 0 || nc == 0) |
|
1835 return ComplexMatrix (nr, nc); |
|
1836 |
|
1837 ComplexMatrix result (m); |
|
1838 for (int i = 0; i < a.length (); i++) |
|
1839 result.elem (i, i) += a.elem (i, i); |
|
1840 |
|
1841 return result; |
|
1842 } |
|
1843 |
|
1844 ComplexMatrix |
|
1845 operator - (const Matrix& m, const ComplexDiagMatrix& a) |
|
1846 { |
|
1847 int nr = m.rows (); |
|
1848 int nc = m.cols (); |
|
1849 if (nr != a.rows () || nc != a.cols ()) |
|
1850 { |
|
1851 (*current_liboctave_error_handler) |
|
1852 ("nonconformant matrix subtraction attempted"); |
|
1853 return ComplexMatrix (); |
|
1854 } |
|
1855 |
|
1856 if (nr == 0 || nc == 0) |
|
1857 return ComplexMatrix (nr, nc); |
|
1858 |
|
1859 ComplexMatrix result (m); |
|
1860 for (int i = 0; i < a.length (); i++) |
|
1861 result.elem (i, i) -= a.elem (i, i); |
|
1862 |
|
1863 return result; |
|
1864 } |
|
1865 |
|
1866 ComplexMatrix |
|
1867 operator * (const Matrix& m, const ComplexDiagMatrix& a) |
|
1868 { |
1948
|
1869 ComplexMatrix retval; |
|
1870 |
1205
|
1871 int nr = m.rows (); |
|
1872 int nc = m.cols (); |
1948
|
1873 |
1205
|
1874 int a_nr = a.rows (); |
|
1875 int a_nc = a.cols (); |
1948
|
1876 |
1205
|
1877 if (nc != a_nr) |
1948
|
1878 (*current_liboctave_error_handler) |
|
1879 ("nonconformant matrix multiplication attempted"); |
|
1880 else |
1205
|
1881 { |
1948
|
1882 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1883 retval.resize (nr, a_nc, 0.0); |
1205
|
1884 else |
|
1885 { |
1948
|
1886 retval.resize (nr, a_nc); |
|
1887 Complex *c = retval.fortran_vec (); |
|
1888 |
|
1889 Complex *ctmp = 0; |
|
1890 |
|
1891 for (int j = 0; j < a.length (); j++) |
|
1892 { |
|
1893 int idx = j * nr; |
|
1894 ctmp = c + idx; |
|
1895 if (a.elem (j, j) == 1.0) |
|
1896 { |
|
1897 for (int i = 0; i < nr; i++) |
|
1898 ctmp[i] = m.elem (i, j); |
|
1899 } |
|
1900 else if (a.elem (j, j) == 0.0) |
|
1901 { |
|
1902 for (int i = 0; i < nr; i++) |
|
1903 ctmp[i] = 0.0; |
|
1904 } |
|
1905 else |
|
1906 { |
|
1907 for (int i = 0; i < nr; i++) |
|
1908 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
1909 } |
|
1910 } |
|
1911 |
|
1912 if (a_nr < a_nc) |
|
1913 { |
|
1914 for (int i = nr * nc; i < nr * a_nc; i++) |
|
1915 ctmp[i] = 0.0; |
|
1916 } |
1205
|
1917 } |
|
1918 } |
|
1919 |
1948
|
1920 return retval; |
1205
|
1921 } |
|
1922 |
|
1923 // diagonal matrix by matrix -> matrix operations |
|
1924 |
|
1925 ComplexMatrix |
|
1926 operator + (const DiagMatrix& m, const ComplexMatrix& a) |
|
1927 { |
|
1928 int nr = m.rows (); |
|
1929 int nc = m.cols (); |
|
1930 if (nr != a.rows () || nc != a.cols ()) |
|
1931 { |
|
1932 (*current_liboctave_error_handler) |
|
1933 ("nonconformant matrix addition attempted"); |
|
1934 return ComplexMatrix (); |
|
1935 } |
|
1936 |
|
1937 if (nr == 0 || nc == 0) |
|
1938 return ComplexMatrix (nr, nc); |
|
1939 |
|
1940 ComplexMatrix result (a); |
|
1941 for (int i = 0; i < m.length (); i++) |
|
1942 result.elem (i, i) += m.elem (i, i); |
|
1943 |
|
1944 return result; |
|
1945 } |
|
1946 |
|
1947 ComplexMatrix |
|
1948 operator - (const DiagMatrix& m, const ComplexMatrix& a) |
|
1949 { |
|
1950 int nr = m.rows (); |
|
1951 int nc = m.cols (); |
|
1952 if (nr != a.rows () || nc != a.cols ()) |
|
1953 { |
|
1954 (*current_liboctave_error_handler) |
|
1955 ("nonconformant matrix subtraction attempted"); |
|
1956 return ComplexMatrix (); |
|
1957 } |
|
1958 |
|
1959 if (nr == 0 || nc == 0) |
|
1960 return ComplexMatrix (nr, nc); |
|
1961 |
|
1962 ComplexMatrix result (-a); |
|
1963 for (int i = 0; i < m.length (); i++) |
|
1964 result.elem (i, i) += m.elem (i, i); |
|
1965 |
|
1966 return result; |
|
1967 } |
|
1968 |
|
1969 ComplexMatrix |
|
1970 operator * (const DiagMatrix& m, const ComplexMatrix& a) |
|
1971 { |
|
1972 int nr = m.rows (); |
|
1973 int nc = m.cols (); |
|
1974 int a_nr = a.rows (); |
|
1975 int a_nc = a.cols (); |
|
1976 if (nc != a_nr) |
|
1977 { |
|
1978 (*current_liboctave_error_handler) |
|
1979 ("nonconformant matrix multiplication attempted"); |
|
1980 return ComplexMatrix (); |
|
1981 } |
|
1982 |
|
1983 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1984 return ComplexMatrix (nr, nc, 0.0); |
|
1985 |
|
1986 ComplexMatrix c (nr, a_nc); |
|
1987 |
|
1988 for (int i = 0; i < m.length (); i++) |
|
1989 { |
|
1990 if (m.elem (i, i) == 1.0) |
|
1991 { |
|
1992 for (int j = 0; j < a_nc; j++) |
|
1993 c.elem (i, j) = a.elem (i, j); |
|
1994 } |
|
1995 else if (m.elem (i, i) == 0.0) |
|
1996 { |
|
1997 for (int j = 0; j < a_nc; j++) |
|
1998 c.elem (i, j) = 0.0; |
|
1999 } |
|
2000 else |
|
2001 { |
|
2002 for (int j = 0; j < a_nc; j++) |
|
2003 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
2004 } |
|
2005 } |
|
2006 |
|
2007 if (nr > nc) |
|
2008 { |
|
2009 for (int j = 0; j < a_nc; j++) |
|
2010 for (int i = a_nr; i < nr; i++) |
|
2011 c.elem (i, j) = 0.0; |
|
2012 } |
|
2013 |
|
2014 return c; |
|
2015 } |
|
2016 |
|
2017 ComplexMatrix |
|
2018 operator + (const ComplexDiagMatrix& m, const Matrix& a) |
|
2019 { |
|
2020 int nr = m.rows (); |
|
2021 int nc = m.cols (); |
|
2022 if (nr != a.rows () || nc != a.cols ()) |
|
2023 { |
|
2024 (*current_liboctave_error_handler) |
|
2025 ("nonconformant matrix addition attempted"); |
|
2026 return ComplexMatrix (); |
|
2027 } |
|
2028 |
|
2029 if (nr == 0 || nc == 0) |
|
2030 return ComplexMatrix (nr, nc); |
|
2031 |
|
2032 ComplexMatrix result (a); |
|
2033 for (int i = 0; i < m.length (); i++) |
|
2034 result.elem (i, i) += m.elem (i, i); |
|
2035 |
|
2036 return result; |
|
2037 } |
|
2038 |
|
2039 ComplexMatrix |
|
2040 operator - (const ComplexDiagMatrix& m, const Matrix& a) |
|
2041 { |
|
2042 int nr = m.rows (); |
|
2043 int nc = m.cols (); |
|
2044 if (nr != a.rows () || nc != a.cols ()) |
|
2045 { |
|
2046 (*current_liboctave_error_handler) |
|
2047 ("nonconformant matrix subtraction attempted"); |
|
2048 return ComplexMatrix (); |
|
2049 } |
|
2050 |
|
2051 if (nr == 0 || nc == 0) |
|
2052 return ComplexMatrix (nr, nc); |
|
2053 |
|
2054 ComplexMatrix result (-a); |
|
2055 for (int i = 0; i < m.length (); i++) |
|
2056 result.elem (i, i) += m.elem (i, i); |
|
2057 |
|
2058 return result; |
|
2059 } |
|
2060 |
|
2061 ComplexMatrix |
|
2062 operator * (const ComplexDiagMatrix& m, const Matrix& a) |
|
2063 { |
|
2064 int nr = m.rows (); |
|
2065 int nc = m.cols (); |
|
2066 int a_nr = a.rows (); |
|
2067 int a_nc = a.cols (); |
|
2068 if (nc != a_nr) |
|
2069 { |
|
2070 (*current_liboctave_error_handler) |
|
2071 ("nonconformant matrix multiplication attempted"); |
|
2072 return ComplexMatrix (); |
|
2073 } |
|
2074 |
|
2075 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2076 return ComplexMatrix (nr, a_nc, 0.0); |
|
2077 |
|
2078 ComplexMatrix c (nr, a_nc); |
|
2079 |
|
2080 for (int i = 0; i < m.length (); i++) |
|
2081 { |
|
2082 if (m.elem (i, i) == 1.0) |
|
2083 { |
|
2084 for (int j = 0; j < a_nc; j++) |
|
2085 c.elem (i, j) = a.elem (i, j); |
|
2086 } |
|
2087 else if (m.elem (i, i) == 0.0) |
|
2088 { |
|
2089 for (int j = 0; j < a_nc; j++) |
|
2090 c.elem (i, j) = 0.0; |
|
2091 } |
|
2092 else |
|
2093 { |
|
2094 for (int j = 0; j < a_nc; j++) |
|
2095 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
2096 } |
|
2097 } |
|
2098 |
|
2099 if (nr > nc) |
|
2100 { |
|
2101 for (int j = 0; j < a_nc; j++) |
|
2102 for (int i = a_nr; i < nr; i++) |
|
2103 c.elem (i, j) = 0.0; |
|
2104 } |
|
2105 |
|
2106 return c; |
|
2107 } |
|
2108 |
|
2109 ComplexMatrix |
|
2110 operator + (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
2111 { |
|
2112 int nr = m.rows (); |
|
2113 int nc = m.cols (); |
|
2114 if (nr != a.rows () || nc != a.cols ()) |
|
2115 { |
|
2116 (*current_liboctave_error_handler) |
|
2117 ("nonconformant matrix addition attempted"); |
|
2118 return ComplexMatrix (); |
|
2119 } |
|
2120 |
|
2121 if (nr == 0 || nc == 0) |
|
2122 return ComplexMatrix (nr, nc); |
|
2123 |
|
2124 ComplexMatrix result (a); |
|
2125 for (int i = 0; i < m.length (); i++) |
|
2126 result.elem (i, i) += m.elem (i, i); |
|
2127 |
|
2128 return result; |
|
2129 } |
|
2130 |
|
2131 ComplexMatrix |
|
2132 operator - (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
2133 { |
|
2134 int nr = m.rows (); |
|
2135 int nc = m.cols (); |
|
2136 if (nr != a.rows () || nc != a.cols ()) |
|
2137 { |
|
2138 (*current_liboctave_error_handler) |
|
2139 ("nonconformant matrix subtraction attempted"); |
|
2140 return ComplexMatrix (); |
|
2141 } |
|
2142 |
|
2143 if (nr == 0 || nc == 0) |
|
2144 return ComplexMatrix (nr, nc); |
|
2145 |
|
2146 ComplexMatrix result (-a); |
|
2147 for (int i = 0; i < m.length (); i++) |
|
2148 result.elem (i, i) += m.elem (i, i); |
|
2149 |
|
2150 return result; |
|
2151 } |
|
2152 |
|
2153 ComplexMatrix |
|
2154 operator * (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
2155 { |
|
2156 int nr = m.rows (); |
|
2157 int nc = m.cols (); |
|
2158 int a_nr = a.rows (); |
|
2159 int a_nc = a.cols (); |
|
2160 if (nc != a_nr) |
|
2161 { |
|
2162 (*current_liboctave_error_handler) |
|
2163 ("nonconformant matrix multiplication attempted"); |
|
2164 return ComplexMatrix (); |
|
2165 } |
|
2166 |
|
2167 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2168 return ComplexMatrix (nr, a_nc, 0.0); |
|
2169 |
|
2170 ComplexMatrix c (nr, a_nc); |
|
2171 |
|
2172 for (int i = 0; i < m.length (); i++) |
|
2173 { |
|
2174 if (m.elem (i, i) == 1.0) |
|
2175 { |
|
2176 for (int j = 0; j < a_nc; j++) |
|
2177 c.elem (i, j) = a.elem (i, j); |
|
2178 } |
|
2179 else if (m.elem (i, i) == 0.0) |
|
2180 { |
|
2181 for (int j = 0; j < a_nc; j++) |
|
2182 c.elem (i, j) = 0.0; |
|
2183 } |
|
2184 else |
|
2185 { |
|
2186 for (int j = 0; j < a_nc; j++) |
|
2187 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
2188 } |
|
2189 } |
|
2190 |
|
2191 if (nr > nc) |
|
2192 { |
|
2193 for (int j = 0; j < a_nc; j++) |
|
2194 for (int i = a_nr; i < nr; i++) |
|
2195 c.elem (i, j) = 0.0; |
|
2196 } |
|
2197 |
|
2198 return c; |
|
2199 } |
|
2200 |
458
|
2201 // matrix by matrix -> matrix operations |
|
2202 |
|
2203 ComplexMatrix& |
|
2204 ComplexMatrix::operator += (const Matrix& a) |
|
2205 { |
|
2206 int nr = rows (); |
|
2207 int nc = cols (); |
|
2208 if (nr != a.rows () || nc != a.cols ()) |
|
2209 { |
|
2210 (*current_liboctave_error_handler) |
|
2211 ("nonconformant matrix += operation attempted"); |
|
2212 return *this; |
|
2213 } |
|
2214 |
|
2215 if (nr == 0 || nc == 0) |
|
2216 return *this; |
|
2217 |
|
2218 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2219 |
|
2220 add2 (d, a.data (), length ()); |
|
2221 return *this; |
|
2222 } |
|
2223 |
|
2224 ComplexMatrix& |
|
2225 ComplexMatrix::operator -= (const Matrix& a) |
|
2226 { |
|
2227 int nr = rows (); |
|
2228 int nc = cols (); |
|
2229 if (nr != a.rows () || nc != a.cols ()) |
|
2230 { |
|
2231 (*current_liboctave_error_handler) |
|
2232 ("nonconformant matrix -= operation attempted"); |
|
2233 return *this; |
|
2234 } |
|
2235 |
|
2236 if (nr == 0 || nc == 0) |
|
2237 return *this; |
|
2238 |
|
2239 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2240 |
|
2241 subtract2 (d, a.data (), length ()); |
|
2242 return *this; |
|
2243 } |
|
2244 |
|
2245 ComplexMatrix& |
|
2246 ComplexMatrix::operator += (const ComplexMatrix& a) |
|
2247 { |
|
2248 int nr = rows (); |
|
2249 int nc = cols (); |
|
2250 if (nr != a.rows () || nc != a.cols ()) |
|
2251 { |
|
2252 (*current_liboctave_error_handler) |
|
2253 ("nonconformant matrix += operation attempted"); |
|
2254 return *this; |
|
2255 } |
|
2256 |
|
2257 if (nr == 0 || nc == 0) |
|
2258 return *this; |
|
2259 |
|
2260 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2261 |
|
2262 add2 (d, a.data (), length ()); |
|
2263 return *this; |
|
2264 } |
|
2265 |
|
2266 ComplexMatrix& |
|
2267 ComplexMatrix::operator -= (const ComplexMatrix& a) |
|
2268 { |
|
2269 int nr = rows (); |
|
2270 int nc = cols (); |
|
2271 if (nr != a.rows () || nc != a.cols ()) |
|
2272 { |
|
2273 (*current_liboctave_error_handler) |
|
2274 ("nonconformant matrix -= operation attempted"); |
|
2275 return *this; |
|
2276 } |
|
2277 |
|
2278 if (nr == 0 || nc == 0) |
|
2279 return *this; |
|
2280 |
|
2281 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2282 |
|
2283 subtract2 (d, a.data (), length ()); |
|
2284 return *this; |
|
2285 } |
|
2286 |
|
2287 // unary operations |
|
2288 |
|
2289 Matrix |
|
2290 ComplexMatrix::operator ! (void) const |
|
2291 { |
|
2292 return Matrix (not (data (), length ()), rows (), cols ()); |
|
2293 } |
|
2294 |
|
2295 // matrix by scalar -> matrix operations |
|
2296 |
|
2297 ComplexMatrix |
1205
|
2298 operator + (const Matrix& a, const Complex& s) |
|
2299 { |
|
2300 return ComplexMatrix (add (a.data (), a.length (), s), |
|
2301 a.rows (), a.cols ()); |
|
2302 } |
|
2303 |
|
2304 ComplexMatrix |
|
2305 operator - (const Matrix& a, const Complex& s) |
|
2306 { |
|
2307 return ComplexMatrix (subtract (a.data (), a.length (), s), |
|
2308 a.rows (), a.cols ()); |
|
2309 } |
|
2310 |
|
2311 ComplexMatrix |
|
2312 operator * (const Matrix& a, const Complex& s) |
|
2313 { |
|
2314 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2315 a.rows (), a.cols ()); |
|
2316 } |
|
2317 |
|
2318 ComplexMatrix |
|
2319 operator / (const Matrix& a, const Complex& s) |
|
2320 { |
|
2321 return ComplexMatrix (divide (a.data (), a.length (), s), |
|
2322 a.rows (), a.cols ()); |
|
2323 } |
|
2324 |
|
2325 ComplexMatrix |
458
|
2326 operator + (const ComplexMatrix& a, double s) |
|
2327 { |
|
2328 return ComplexMatrix (add (a.data (), a.length (), s), |
|
2329 a.rows (), a.cols ()); |
|
2330 } |
|
2331 |
|
2332 ComplexMatrix |
|
2333 operator - (const ComplexMatrix& a, double s) |
|
2334 { |
|
2335 return ComplexMatrix (subtract (a.data (), a.length (), s), |
|
2336 a.rows (), a.cols ()); |
|
2337 } |
|
2338 |
|
2339 ComplexMatrix |
|
2340 operator * (const ComplexMatrix& a, double s) |
|
2341 { |
|
2342 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2343 a.rows (), a.cols ()); |
|
2344 } |
|
2345 |
|
2346 ComplexMatrix |
|
2347 operator / (const ComplexMatrix& a, double s) |
|
2348 { |
|
2349 return ComplexMatrix (divide (a.data (), a.length (), s), |
|
2350 a.rows (), a.cols ()); |
|
2351 } |
|
2352 |
|
2353 // scalar by matrix -> matrix operations |
|
2354 |
|
2355 ComplexMatrix |
|
2356 operator + (double s, const ComplexMatrix& a) |
|
2357 { |
|
2358 return ComplexMatrix (add (a.data (), a.length (), s), a.rows (), |
|
2359 a.cols ()); |
|
2360 } |
|
2361 |
|
2362 ComplexMatrix |
|
2363 operator - (double s, const ComplexMatrix& a) |
|
2364 { |
|
2365 return ComplexMatrix (subtract (s, a.data (), a.length ()), |
|
2366 a.rows (), a.cols ()); |
|
2367 } |
|
2368 |
|
2369 ComplexMatrix |
|
2370 operator * (double s, const ComplexMatrix& a) |
|
2371 { |
|
2372 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2373 a.rows (), a.cols ()); |
|
2374 } |
|
2375 |
|
2376 ComplexMatrix |
|
2377 operator / (double s, const ComplexMatrix& a) |
|
2378 { |
|
2379 return ComplexMatrix (divide (s, a.data (), a.length ()), |
|
2380 a.rows (), a.cols ()); |
|
2381 } |
|
2382 |
1205
|
2383 ComplexMatrix |
|
2384 operator + (const Complex& s, const Matrix& a) |
458
|
2385 { |
1205
|
2386 return ComplexMatrix (add (s, a.data (), a.length ()), |
|
2387 a.rows (), a.cols ()); |
458
|
2388 } |
|
2389 |
1205
|
2390 ComplexMatrix |
|
2391 operator - (const Complex& s, const Matrix& a) |
458
|
2392 { |
1205
|
2393 return ComplexMatrix (subtract (s, a.data (), a.length ()), |
|
2394 a.rows (), a.cols ()); |
|
2395 } |
|
2396 |
|
2397 ComplexMatrix |
|
2398 operator * (const Complex& s, const Matrix& a) |
|
2399 { |
|
2400 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2401 a.rows (), a.cols ()); |
|
2402 } |
|
2403 |
|
2404 ComplexMatrix |
|
2405 operator / (const Complex& s, const Matrix& a) |
|
2406 { |
|
2407 return ComplexMatrix (divide (s, a.data (), a.length ()), |
|
2408 a.rows (), a.cols ()); |
458
|
2409 } |
|
2410 |
|
2411 // matrix by diagonal matrix -> matrix operations |
|
2412 |
|
2413 ComplexMatrix |
|
2414 operator + (const ComplexMatrix& m, const DiagMatrix& a) |
|
2415 { |
|
2416 int nr = m.rows (); |
|
2417 int nc = m.cols (); |
|
2418 if (nr != a.rows () || nc != a.cols ()) |
|
2419 { |
|
2420 (*current_liboctave_error_handler) |
|
2421 ("nonconformant matrix addition attempted"); |
|
2422 return ComplexMatrix (); |
|
2423 } |
|
2424 |
|
2425 if (nr == 0 || nc == 0) |
|
2426 return ComplexMatrix (nr, nc); |
|
2427 |
|
2428 ComplexMatrix result (m); |
|
2429 for (int i = 0; i < a.length (); i++) |
|
2430 result.elem (i, i) += a.elem (i, i); |
|
2431 |
|
2432 return result; |
|
2433 } |
|
2434 |
|
2435 ComplexMatrix |
|
2436 operator - (const ComplexMatrix& m, const DiagMatrix& a) |
|
2437 { |
|
2438 int nr = m.rows (); |
|
2439 int nc = m.cols (); |
|
2440 if (nr != a.rows () || nc != a.cols ()) |
|
2441 { |
|
2442 (*current_liboctave_error_handler) |
|
2443 ("nonconformant matrix subtraction attempted"); |
|
2444 return ComplexMatrix (); |
|
2445 } |
|
2446 |
|
2447 if (nr == 0 || nc == 0) |
|
2448 return ComplexMatrix (nr, nc); |
|
2449 |
|
2450 ComplexMatrix result (m); |
|
2451 for (int i = 0; i < a.length (); i++) |
|
2452 result.elem (i, i) -= a.elem (i, i); |
|
2453 |
|
2454 return result; |
|
2455 } |
|
2456 |
|
2457 ComplexMatrix |
|
2458 operator * (const ComplexMatrix& m, const DiagMatrix& a) |
|
2459 { |
1948
|
2460 ComplexMatrix retval; |
|
2461 |
458
|
2462 int nr = m.rows (); |
|
2463 int nc = m.cols (); |
1948
|
2464 |
458
|
2465 int a_nc = a.cols (); |
1948
|
2466 |
458
|
2467 if (nc != a.rows ()) |
1948
|
2468 (*current_liboctave_error_handler) |
|
2469 ("nonconformant matrix multiplication attempted"); |
|
2470 else |
458
|
2471 { |
1948
|
2472 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2473 retval.resize (nr, nc, 0.0); |
458
|
2474 else |
|
2475 { |
1948
|
2476 retval.resize (nr, a_nc); |
|
2477 Complex *c = retval.fortran_vec (); |
|
2478 Complex *ctmp = 0; |
|
2479 |
|
2480 for (int j = 0; j < a.length (); j++) |
|
2481 { |
|
2482 int idx = j * nr; |
|
2483 ctmp = c + idx; |
|
2484 if (a.elem (j, j) == 1.0) |
|
2485 { |
|
2486 for (int i = 0; i < nr; i++) |
|
2487 ctmp[i] = m.elem (i, j); |
|
2488 } |
|
2489 else if (a.elem (j, j) == 0.0) |
|
2490 { |
|
2491 for (int i = 0; i < nr; i++) |
|
2492 ctmp[i] = 0.0; |
|
2493 } |
|
2494 else |
|
2495 { |
|
2496 for (int i = 0; i < nr; i++) |
|
2497 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
2498 } |
|
2499 } |
|
2500 |
|
2501 if (a.rows () < a_nc) |
|
2502 { |
|
2503 for (int i = nr * nc; i < nr * a_nc; i++) |
|
2504 ctmp[i] = 0.0; |
|
2505 } |
458
|
2506 } |
|
2507 } |
|
2508 |
1948
|
2509 return retval; |
458
|
2510 } |
|
2511 |
|
2512 ComplexMatrix |
|
2513 operator + (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2514 { |
|
2515 int nr = m.rows (); |
|
2516 int nc = m.cols (); |
|
2517 if (nr != a.rows () || nc != a.cols ()) |
|
2518 { |
|
2519 (*current_liboctave_error_handler) |
|
2520 ("nonconformant matrix addition attempted"); |
|
2521 return ComplexMatrix (); |
|
2522 } |
|
2523 |
|
2524 if (nr == 0 || nc == 0) |
|
2525 return ComplexMatrix (nr, nc); |
|
2526 |
|
2527 ComplexMatrix result (m); |
|
2528 for (int i = 0; i < a.length (); i++) |
|
2529 result.elem (i, i) += a.elem (i, i); |
|
2530 |
|
2531 return result; |
|
2532 } |
|
2533 |
|
2534 ComplexMatrix |
|
2535 operator - (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2536 { |
|
2537 int nr = m.rows (); |
|
2538 int nc = m.cols (); |
|
2539 if (nr != a.rows () || nc != a.cols ()) |
|
2540 { |
|
2541 (*current_liboctave_error_handler) |
|
2542 ("nonconformant matrix subtraction attempted"); |
|
2543 return ComplexMatrix (); |
|
2544 } |
|
2545 |
|
2546 if (nr == 0 || nc == 0) |
|
2547 return ComplexMatrix (nr, nc); |
|
2548 |
|
2549 ComplexMatrix result (m); |
|
2550 for (int i = 0; i < a.length (); i++) |
|
2551 result.elem (i, i) -= a.elem (i, i); |
|
2552 |
|
2553 return result; |
|
2554 } |
|
2555 |
|
2556 ComplexMatrix |
|
2557 operator * (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2558 { |
1948
|
2559 ComplexMatrix retval; |
|
2560 |
458
|
2561 int nr = m.rows (); |
|
2562 int nc = m.cols (); |
1948
|
2563 |
458
|
2564 int a_nc = a.cols (); |
1948
|
2565 |
458
|
2566 if (nc != a.rows ()) |
1948
|
2567 (*current_liboctave_error_handler) |
|
2568 ("nonconformant matrix multiplication attempted"); |
|
2569 else |
458
|
2570 { |
1948
|
2571 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2572 retval.resize (nr, nc, 0.0); |
458
|
2573 else |
|
2574 { |
1948
|
2575 retval.resize (nr, nc); |
|
2576 Complex *c = retval.fortran_vec (); |
|
2577 Complex *ctmp = 0; |
|
2578 |
|
2579 for (int j = 0; j < a.length (); j++) |
|
2580 { |
|
2581 int idx = j * nr; |
|
2582 ctmp = c + idx; |
|
2583 if (a.elem (j, j) == 1.0) |
|
2584 { |
|
2585 for (int i = 0; i < nr; i++) |
|
2586 ctmp[i] = m.elem (i, j); |
|
2587 } |
|
2588 else if (a.elem (j, j) == 0.0) |
|
2589 { |
|
2590 for (int i = 0; i < nr; i++) |
|
2591 ctmp[i] = 0.0; |
|
2592 } |
|
2593 else |
|
2594 { |
|
2595 for (int i = 0; i < nr; i++) |
|
2596 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
2597 } |
|
2598 } |
|
2599 |
|
2600 if (a.rows () < a_nc) |
|
2601 { |
|
2602 for (int i = nr * nc; i < nr * a_nc; i++) |
|
2603 ctmp[i] = 0.0; |
|
2604 } |
458
|
2605 } |
|
2606 } |
|
2607 |
1948
|
2608 return retval; |
458
|
2609 } |
|
2610 |
|
2611 // matrix by matrix -> matrix operations |
|
2612 |
|
2613 ComplexMatrix |
|
2614 operator + (const ComplexMatrix& m, const Matrix& a) |
|
2615 { |
|
2616 int nr = m.rows (); |
|
2617 int nc = m.cols (); |
|
2618 if (nr != a.rows () || nc != a.cols ()) |
|
2619 { |
|
2620 (*current_liboctave_error_handler) |
|
2621 ("nonconformant matrix addition attempted"); |
|
2622 return ComplexMatrix (); |
|
2623 } |
|
2624 |
|
2625 if (nr == 0 || nc == 0) |
|
2626 return ComplexMatrix (nr, nc); |
|
2627 |
|
2628 return ComplexMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
2629 } |
|
2630 |
|
2631 ComplexMatrix |
|
2632 operator - (const ComplexMatrix& m, const Matrix& a) |
|
2633 { |
|
2634 int nr = m.rows (); |
|
2635 int nc = m.cols (); |
|
2636 if (nr != a.rows () || nc != a.cols ()) |
|
2637 { |
|
2638 (*current_liboctave_error_handler) |
|
2639 ("nonconformant matrix subtraction attempted"); |
|
2640 return ComplexMatrix (); |
|
2641 } |
|
2642 |
|
2643 if (nr == 0 || nc == 0) |
|
2644 return ComplexMatrix (nr, nc); |
|
2645 |
|
2646 return ComplexMatrix (subtract (m.data (), a.data (), m.length ()), nr, nc); |
|
2647 } |
|
2648 |
|
2649 ComplexMatrix |
1205
|
2650 operator + (const Matrix& m, const ComplexMatrix& a) |
|
2651 { |
|
2652 int nr = m.rows (); |
|
2653 int nc = m.cols (); |
|
2654 if (nr != a.rows () || nc != a.cols ()) |
|
2655 { |
|
2656 (*current_liboctave_error_handler) |
|
2657 ("nonconformant matrix addition attempted"); |
|
2658 return ComplexMatrix (); |
|
2659 } |
|
2660 |
|
2661 return ComplexMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
2662 } |
|
2663 |
|
2664 ComplexMatrix |
|
2665 operator - (const Matrix& m, const ComplexMatrix& a) |
|
2666 { |
|
2667 int nr = m.rows (); |
|
2668 int nc = m.cols (); |
|
2669 if (nr != a.rows () || nc != a.cols ()) |
|
2670 { |
|
2671 (*current_liboctave_error_handler) |
|
2672 ("nonconformant matrix subtraction attempted"); |
|
2673 return ComplexMatrix (); |
|
2674 } |
|
2675 |
|
2676 if (nr == 0 || nc == 0) |
|
2677 return ComplexMatrix (nr, nc); |
|
2678 |
|
2679 return ComplexMatrix (subtract (m.data (), a.data (), m.length ()), nr, nc); |
|
2680 } |
|
2681 |
|
2682 ComplexMatrix |
458
|
2683 operator * (const ComplexMatrix& m, const Matrix& a) |
|
2684 { |
|
2685 ComplexMatrix tmp (a); |
|
2686 return m * tmp; |
|
2687 } |
|
2688 |
|
2689 ComplexMatrix |
1205
|
2690 operator * (const Matrix& m, const ComplexMatrix& a) |
|
2691 { |
|
2692 ComplexMatrix tmp (m); |
|
2693 return tmp * a; |
|
2694 } |
|
2695 |
|
2696 ComplexMatrix |
458
|
2697 operator * (const ComplexMatrix& m, const ComplexMatrix& a) |
|
2698 { |
1948
|
2699 ComplexMatrix retval; |
|
2700 |
458
|
2701 int nr = m.rows (); |
|
2702 int nc = m.cols (); |
1948
|
2703 |
458
|
2704 int a_nc = a.cols (); |
1948
|
2705 |
458
|
2706 if (nc != a.rows ()) |
1948
|
2707 (*current_liboctave_error_handler) |
|
2708 ("nonconformant matrix multiplication attempted"); |
|
2709 else |
458
|
2710 { |
1948
|
2711 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2712 retval.resize (nr, nc, 0.0); |
|
2713 else |
|
2714 { |
|
2715 int ld = nr; |
|
2716 int lda = a.rows (); |
|
2717 |
|
2718 retval.resize (nr, a_nc); |
|
2719 Complex *c = retval.fortran_vec (); |
|
2720 |
|
2721 F77_XFCN (zgemm, ZGEMM, ("N", "N", nr, a_nc, nc, 1.0, |
|
2722 m.data (), ld, a.data (), lda, 0.0, |
|
2723 c, nr, 1L, 1L)); |
|
2724 |
|
2725 if (f77_exception_encountered) |
|
2726 (*current_liboctave_error_handler) |
|
2727 ("unrecoverable error in zgemm"); |
|
2728 } |
458
|
2729 } |
|
2730 |
1948
|
2731 return retval; |
458
|
2732 } |
|
2733 |
|
2734 ComplexMatrix |
|
2735 product (const ComplexMatrix& m, const Matrix& a) |
|
2736 { |
|
2737 int nr = m.rows (); |
|
2738 int nc = m.cols (); |
|
2739 if (nr != a.rows () || nc != a.cols ()) |
|
2740 { |
|
2741 (*current_liboctave_error_handler) |
|
2742 ("nonconformant matrix product attempted"); |
|
2743 return ComplexMatrix (); |
|
2744 } |
|
2745 |
|
2746 if (nr == 0 || nc == 0) |
|
2747 return ComplexMatrix (nr, nc); |
|
2748 |
|
2749 return ComplexMatrix (multiply (m.data (), a.data (), m.length ()), nr, nc); |
|
2750 } |
|
2751 |
|
2752 ComplexMatrix |
|
2753 quotient (const ComplexMatrix& m, const Matrix& a) |
|
2754 { |
|
2755 int nr = m.rows (); |
|
2756 int nc = m.cols (); |
|
2757 if (nr != a.rows () || nc != a.cols ()) |
|
2758 { |
|
2759 (*current_liboctave_error_handler) |
|
2760 ("nonconformant matrix quotient attempted"); |
|
2761 return ComplexMatrix (); |
|
2762 } |
|
2763 |
|
2764 if (nr == 0 || nc == 0) |
|
2765 return ComplexMatrix (nr, nc); |
|
2766 |
|
2767 return ComplexMatrix (divide (m.data (), a.data (), m.length ()), nr, nc); |
|
2768 } |
|
2769 |
1205
|
2770 ComplexMatrix |
|
2771 product (const Matrix& m, const ComplexMatrix& a) |
|
2772 { |
|
2773 int nr = m.rows (); |
|
2774 int nc = m.cols (); |
|
2775 if (nr != a.rows () || nc != a.cols ()) |
|
2776 { |
|
2777 (*current_liboctave_error_handler) |
|
2778 ("nonconformant matrix product attempted"); |
|
2779 return ComplexMatrix (); |
|
2780 } |
|
2781 |
|
2782 if (nr == 0 || nc == 0) |
|
2783 return ComplexMatrix (nr, nc); |
|
2784 |
|
2785 return ComplexMatrix (multiply (m.data (), a.data (), m.length ()), nr, nc); |
|
2786 } |
|
2787 |
|
2788 ComplexMatrix |
|
2789 quotient (const Matrix& m, const ComplexMatrix& a) |
|
2790 { |
|
2791 int nr = m.rows (); |
|
2792 int nc = m.cols (); |
|
2793 if (nr != a.rows () || nc != a.cols ()) |
|
2794 { |
|
2795 (*current_liboctave_error_handler) |
|
2796 ("nonconformant matrix quotient attempted"); |
|
2797 return ComplexMatrix (); |
|
2798 } |
|
2799 |
|
2800 if (nr == 0 || nc == 0) |
|
2801 return ComplexMatrix (nr, nc); |
|
2802 |
|
2803 return ComplexMatrix (divide (m.data (), a.data (), m.length ()), nr, nc); |
|
2804 } |
|
2805 |
458
|
2806 // other operations |
|
2807 |
|
2808 ComplexMatrix |
|
2809 map (c_c_Mapper f, const ComplexMatrix& a) |
|
2810 { |
|
2811 ComplexMatrix b (a); |
|
2812 b.map (f); |
|
2813 return b; |
|
2814 } |
|
2815 |
|
2816 void |
|
2817 ComplexMatrix::map (c_c_Mapper f) |
|
2818 { |
|
2819 for (int j = 0; j < cols (); j++) |
|
2820 for (int i = 0; i < rows (); i++) |
|
2821 elem (i, j) = f (elem (i, j)); |
|
2822 } |
|
2823 |
|
2824 Matrix |
|
2825 ComplexMatrix::all (void) const |
|
2826 { |
|
2827 int nr = rows (); |
|
2828 int nc = cols (); |
|
2829 Matrix retval; |
|
2830 if (nr > 0 && nc > 0) |
|
2831 { |
|
2832 if (nr == 1) |
|
2833 { |
|
2834 retval.resize (1, 1); |
|
2835 retval.elem (0, 0) = 1.0; |
|
2836 for (int j = 0; j < nc; j++) |
|
2837 { |
|
2838 if (elem (0, j) == 0.0) |
|
2839 { |
|
2840 retval.elem (0, 0) = 0.0; |
|
2841 break; |
|
2842 } |
|
2843 } |
|
2844 } |
|
2845 else if (nc == 1) |
|
2846 { |
|
2847 retval.resize (1, 1); |
|
2848 retval.elem (0, 0) = 1.0; |
|
2849 for (int i = 0; i < nr; i++) |
|
2850 { |
|
2851 if (elem (i, 0) == 0.0) |
|
2852 { |
|
2853 retval.elem (0, 0) = 0.0; |
|
2854 break; |
|
2855 } |
|
2856 } |
|
2857 } |
|
2858 else |
|
2859 { |
|
2860 retval.resize (1, nc); |
|
2861 for (int j = 0; j < nc; j++) |
|
2862 { |
|
2863 retval.elem (0, j) = 1.0; |
|
2864 for (int i = 0; i < nr; i++) |
|
2865 { |
|
2866 if (elem (i, j) == 0.0) |
|
2867 { |
|
2868 retval.elem (0, j) = 0.0; |
|
2869 break; |
|
2870 } |
|
2871 } |
|
2872 } |
|
2873 } |
|
2874 } |
|
2875 return retval; |
|
2876 } |
|
2877 |
|
2878 Matrix |
|
2879 ComplexMatrix::any (void) const |
|
2880 { |
|
2881 int nr = rows (); |
|
2882 int nc = cols (); |
|
2883 Matrix retval; |
|
2884 if (nr > 0 && nc > 0) |
|
2885 { |
|
2886 if (nr == 1) |
|
2887 { |
|
2888 retval.resize (1, 1); |
|
2889 retval.elem (0, 0) = 0.0; |
|
2890 for (int j = 0; j < nc; j++) |
|
2891 { |
|
2892 if (elem (0, j) != 0.0) |
|
2893 { |
|
2894 retval.elem (0, 0) = 1.0; |
|
2895 break; |
|
2896 } |
|
2897 } |
|
2898 } |
|
2899 else if (nc == 1) |
|
2900 { |
|
2901 retval.resize (1, 1); |
|
2902 retval.elem (0, 0) = 0.0; |
|
2903 for (int i = 0; i < nr; i++) |
|
2904 { |
|
2905 if (elem (i, 0) != 0.0) |
|
2906 { |
|
2907 retval.elem (0, 0) = 1.0; |
|
2908 break; |
|
2909 } |
|
2910 } |
|
2911 } |
|
2912 else |
|
2913 { |
|
2914 retval.resize (1, nc); |
|
2915 for (int j = 0; j < nc; j++) |
|
2916 { |
|
2917 retval.elem (0, j) = 0.0; |
|
2918 for (int i = 0; i < nr; i++) |
|
2919 { |
|
2920 if (elem (i, j) != 0.0) |
|
2921 { |
|
2922 retval.elem (0, j) = 1.0; |
|
2923 break; |
|
2924 } |
|
2925 } |
|
2926 } |
|
2927 } |
|
2928 } |
|
2929 return retval; |
|
2930 } |
|
2931 |
|
2932 ComplexMatrix |
|
2933 ComplexMatrix::cumprod (void) const |
|
2934 { |
|
2935 int nr = rows (); |
|
2936 int nc = cols (); |
|
2937 ComplexMatrix retval; |
|
2938 if (nr > 0 && nc > 0) |
|
2939 { |
|
2940 if (nr == 1) |
|
2941 { |
|
2942 retval.resize (1, nc); |
|
2943 Complex prod = elem (0, 0); |
|
2944 for (int j = 0; j < nc; j++) |
|
2945 { |
|
2946 retval.elem (0, j) = prod; |
|
2947 if (j < nc - 1) |
|
2948 prod *= elem (0, j+1); |
|
2949 } |
|
2950 } |
|
2951 else if (nc == 1) |
|
2952 { |
|
2953 retval.resize (nr, 1); |
|
2954 Complex prod = elem (0, 0); |
|
2955 for (int i = 0; i < nr; i++) |
|
2956 { |
|
2957 retval.elem (i, 0) = prod; |
|
2958 if (i < nr - 1) |
|
2959 prod *= elem (i+1, 0); |
|
2960 } |
|
2961 } |
|
2962 else |
|
2963 { |
|
2964 retval.resize (nr, nc); |
|
2965 for (int j = 0; j < nc; j++) |
|
2966 { |
|
2967 Complex prod = elem (0, j); |
|
2968 for (int i = 0; i < nr; i++) |
|
2969 { |
|
2970 retval.elem (i, j) = prod; |
|
2971 if (i < nr - 1) |
|
2972 prod *= elem (i+1, j); |
|
2973 } |
|
2974 } |
|
2975 } |
|
2976 } |
|
2977 return retval; |
|
2978 } |
|
2979 |
|
2980 ComplexMatrix |
|
2981 ComplexMatrix::cumsum (void) const |
|
2982 { |
|
2983 int nr = rows (); |
|
2984 int nc = cols (); |
|
2985 ComplexMatrix retval; |
|
2986 if (nr > 0 && nc > 0) |
|
2987 { |
|
2988 if (nr == 1) |
|
2989 { |
|
2990 retval.resize (1, nc); |
|
2991 Complex sum = elem (0, 0); |
|
2992 for (int j = 0; j < nc; j++) |
|
2993 { |
|
2994 retval.elem (0, j) = sum; |
|
2995 if (j < nc - 1) |
|
2996 sum += elem (0, j+1); |
|
2997 } |
|
2998 } |
|
2999 else if (nc == 1) |
|
3000 { |
|
3001 retval.resize (nr, 1); |
|
3002 Complex sum = elem (0, 0); |
|
3003 for (int i = 0; i < nr; i++) |
|
3004 { |
|
3005 retval.elem (i, 0) = sum; |
|
3006 if (i < nr - 1) |
|
3007 sum += elem (i+1, 0); |
|
3008 } |
|
3009 } |
|
3010 else |
|
3011 { |
|
3012 retval.resize (nr, nc); |
|
3013 for (int j = 0; j < nc; j++) |
|
3014 { |
|
3015 Complex sum = elem (0, j); |
|
3016 for (int i = 0; i < nr; i++) |
|
3017 { |
|
3018 retval.elem (i, j) = sum; |
|
3019 if (i < nr - 1) |
|
3020 sum += elem (i+1, j); |
|
3021 } |
|
3022 } |
|
3023 } |
|
3024 } |
|
3025 return retval; |
|
3026 } |
|
3027 |
|
3028 ComplexMatrix |
|
3029 ComplexMatrix::prod (void) const |
|
3030 { |
|
3031 int nr = rows (); |
|
3032 int nc = cols (); |
|
3033 ComplexMatrix retval; |
|
3034 if (nr > 0 && nc > 0) |
|
3035 { |
|
3036 if (nr == 1) |
|
3037 { |
|
3038 retval.resize (1, 1); |
|
3039 retval.elem (0, 0) = 1.0; |
|
3040 for (int j = 0; j < nc; j++) |
|
3041 retval.elem (0, 0) *= elem (0, j); |
|
3042 } |
|
3043 else if (nc == 1) |
|
3044 { |
|
3045 retval.resize (1, 1); |
|
3046 retval.elem (0, 0) = 1.0; |
|
3047 for (int i = 0; i < nr; i++) |
|
3048 retval.elem (0, 0) *= elem (i, 0); |
|
3049 } |
|
3050 else |
|
3051 { |
|
3052 retval.resize (1, nc); |
|
3053 for (int j = 0; j < nc; j++) |
|
3054 { |
|
3055 retval.elem (0, j) = 1.0; |
|
3056 for (int i = 0; i < nr; i++) |
|
3057 retval.elem (0, j) *= elem (i, j); |
|
3058 } |
|
3059 } |
|
3060 } |
|
3061 return retval; |
|
3062 } |
|
3063 |
|
3064 ComplexMatrix |
|
3065 ComplexMatrix::sum (void) const |
|
3066 { |
|
3067 int nr = rows (); |
|
3068 int nc = cols (); |
|
3069 ComplexMatrix retval; |
|
3070 if (nr > 0 && nc > 0) |
|
3071 { |
|
3072 if (nr == 1) |
|
3073 { |
|
3074 retval.resize (1, 1); |
|
3075 retval.elem (0, 0) = 0.0; |
|
3076 for (int j = 0; j < nc; j++) |
|
3077 retval.elem (0, 0) += elem (0, j); |
|
3078 } |
|
3079 else if (nc == 1) |
|
3080 { |
|
3081 retval.resize (1, 1); |
|
3082 retval.elem (0, 0) = 0.0; |
|
3083 for (int i = 0; i < nr; i++) |
|
3084 retval.elem (0, 0) += elem (i, 0); |
|
3085 } |
|
3086 else |
|
3087 { |
|
3088 retval.resize (1, nc); |
|
3089 for (int j = 0; j < nc; j++) |
|
3090 { |
|
3091 retval.elem (0, j) = 0.0; |
|
3092 for (int i = 0; i < nr; i++) |
|
3093 retval.elem (0, j) += elem (i, j); |
|
3094 } |
|
3095 } |
|
3096 } |
|
3097 return retval; |
|
3098 } |
|
3099 |
|
3100 ComplexMatrix |
|
3101 ComplexMatrix::sumsq (void) const |
|
3102 { |
|
3103 int nr = rows (); |
|
3104 int nc = cols (); |
|
3105 ComplexMatrix retval; |
|
3106 if (nr > 0 && nc > 0) |
|
3107 { |
|
3108 if (nr == 1) |
|
3109 { |
|
3110 retval.resize (1, 1); |
|
3111 retval.elem (0, 0) = 0.0; |
|
3112 for (int j = 0; j < nc; j++) |
|
3113 { |
|
3114 Complex d = elem (0, j); |
|
3115 retval.elem (0, 0) += d * d; |
|
3116 } |
|
3117 } |
|
3118 else if (nc == 1) |
|
3119 { |
|
3120 retval.resize (1, 1); |
|
3121 retval.elem (0, 0) = 0.0; |
|
3122 for (int i = 0; i < nr; i++) |
|
3123 { |
|
3124 Complex d = elem (i, 0); |
|
3125 retval.elem (0, 0) += d * d; |
|
3126 } |
|
3127 } |
|
3128 else |
|
3129 { |
|
3130 retval.resize (1, nc); |
|
3131 for (int j = 0; j < nc; j++) |
|
3132 { |
|
3133 retval.elem (0, j) = 0.0; |
|
3134 for (int i = 0; i < nr; i++) |
|
3135 { |
|
3136 Complex d = elem (i, j); |
|
3137 retval.elem (0, j) += d * d; |
|
3138 } |
|
3139 } |
|
3140 } |
|
3141 } |
|
3142 return retval; |
|
3143 } |
|
3144 |
|
3145 ComplexColumnVector |
|
3146 ComplexMatrix::diag (void) const |
|
3147 { |
|
3148 return diag (0); |
|
3149 } |
|
3150 |
|
3151 ComplexColumnVector |
|
3152 ComplexMatrix::diag (int k) const |
|
3153 { |
|
3154 int nnr = rows (); |
|
3155 int nnc = cols (); |
|
3156 if (k > 0) |
|
3157 nnc -= k; |
|
3158 else if (k < 0) |
|
3159 nnr += k; |
|
3160 |
|
3161 ComplexColumnVector d; |
|
3162 |
|
3163 if (nnr > 0 && nnc > 0) |
|
3164 { |
|
3165 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
3166 |
|
3167 d.resize (ndiag); |
|
3168 |
|
3169 if (k > 0) |
|
3170 { |
|
3171 for (int i = 0; i < ndiag; i++) |
|
3172 d.elem (i) = elem (i, i+k); |
|
3173 } |
|
3174 else if ( k < 0) |
|
3175 { |
|
3176 for (int i = 0; i < ndiag; i++) |
|
3177 d.elem (i) = elem (i-k, i); |
|
3178 } |
|
3179 else |
|
3180 { |
|
3181 for (int i = 0; i < ndiag; i++) |
|
3182 d.elem (i) = elem (i, i); |
|
3183 } |
|
3184 } |
|
3185 else |
|
3186 cerr << "diag: requested diagonal out of range\n"; |
|
3187 |
|
3188 return d; |
|
3189 } |
|
3190 |
891
|
3191 // XXX FIXME XXX -- it would be nice to share some code among all the |
|
3192 // min/max functions below. It would also be nice to combine the |
|
3193 // min/max and min_loc/max_loc functions. |
|
3194 |
458
|
3195 ComplexColumnVector |
|
3196 ComplexMatrix::row_min (void) const |
|
3197 { |
|
3198 ComplexColumnVector result; |
|
3199 |
|
3200 int nr = rows (); |
|
3201 int nc = cols (); |
|
3202 if (nr > 0 && nc > 0) |
|
3203 { |
|
3204 result.resize (nr); |
|
3205 |
|
3206 for (int i = 0; i < nr; i++) |
|
3207 { |
891
|
3208 int row_is_real_only = 1; |
|
3209 for (int j = 0; j < nc; j++) |
|
3210 if (imag (elem (i, j)) != 0.0) |
458
|
3211 { |
891
|
3212 row_is_real_only = 0; |
|
3213 break; |
458
|
3214 } |
891
|
3215 |
|
3216 if (row_is_real_only) |
|
3217 { |
|
3218 double res = real (elem (i, 0)); |
|
3219 for (int j = 1; j < nc; j++) |
|
3220 { |
|
3221 double tmp = real (elem (i, j)); |
|
3222 if (tmp < res) |
|
3223 res = tmp; |
|
3224 } |
|
3225 result.elem (i) = res; |
|
3226 } |
|
3227 else |
|
3228 { |
|
3229 Complex res = elem (i, 0); |
|
3230 double absres = abs (res); |
|
3231 for (int j = 1; j < nc; j++) |
|
3232 if (abs (elem (i, j)) < absres) |
|
3233 { |
|
3234 res = elem (i, j); |
|
3235 absres = abs (res); |
|
3236 } |
|
3237 result.elem (i) = res; |
|
3238 } |
458
|
3239 } |
|
3240 } |
|
3241 |
|
3242 return result; |
|
3243 } |
|
3244 |
|
3245 ComplexColumnVector |
|
3246 ComplexMatrix::row_min_loc (void) const |
|
3247 { |
|
3248 ComplexColumnVector result; |
|
3249 |
|
3250 int nr = rows (); |
|
3251 int nc = cols (); |
|
3252 |
|
3253 if (nr > 0 && nc > 0) |
|
3254 { |
|
3255 result.resize (nr); |
|
3256 |
|
3257 for (int i = 0; i < nr; i++) |
|
3258 { |
891
|
3259 int column_is_real_only = 1; |
|
3260 for (int j = 0; j < nc; j++) |
|
3261 if (imag (elem (i, j)) != 0.0) |
|
3262 { |
|
3263 column_is_real_only = 0; |
|
3264 break; |
|
3265 } |
|
3266 |
|
3267 if (column_is_real_only) |
|
3268 { |
|
3269 double res = 0; |
|
3270 double tmp = real (elem (i, 0)); |
|
3271 for (int j = 1; j < nc; j++) |
|
3272 if (real (elem (i, j)) < tmp) |
|
3273 res = j; |
|
3274 |
|
3275 result.elem (i) = res + 1; |
|
3276 } |
|
3277 else |
|
3278 { |
|
3279 Complex res = 0; |
|
3280 double absres = abs (elem (i, 0)); |
|
3281 for (int j = 1; j < nc; j++) |
|
3282 if (abs (elem (i, j)) < absres) |
|
3283 { |
|
3284 res = j; |
|
3285 absres = abs (elem (i, j)); |
|
3286 } |
|
3287 result.elem (i) = res + 1; |
|
3288 } |
458
|
3289 } |
|
3290 } |
|
3291 |
|
3292 return result; |
|
3293 } |
|
3294 |
|
3295 ComplexColumnVector |
|
3296 ComplexMatrix::row_max (void) const |
|
3297 { |
|
3298 ComplexColumnVector result; |
|
3299 |
|
3300 int nr = rows (); |
|
3301 int nc = cols (); |
|
3302 |
|
3303 if (nr > 0 && nc > 0) |
|
3304 { |
|
3305 result.resize (nr); |
|
3306 |
|
3307 for (int i = 0; i < nr; i++) |
|
3308 { |
891
|
3309 int row_is_real_only = 1; |
|
3310 for (int j = 0; j < nc; j++) |
|
3311 if (imag (elem (i, j)) != 0.0) |
458
|
3312 { |
891
|
3313 row_is_real_only = 0; |
|
3314 break; |
458
|
3315 } |
891
|
3316 |
|
3317 if (row_is_real_only) |
|
3318 { |
|
3319 double res = real (elem (i, 0)); |
|
3320 for (int j = 1; j < nc; j++) |
|
3321 { |
|
3322 double tmp = real (elem (i, j)); |
|
3323 if (tmp > res) |
|
3324 res = tmp; |
|
3325 } |
|
3326 result.elem (i) = res; |
|
3327 } |
|
3328 else |
|
3329 { |
|
3330 Complex res = elem (i, 0); |
|
3331 double absres = abs (res); |
|
3332 for (int j = 1; j < nc; j++) |
|
3333 if (abs (elem (i, j)) > absres) |
|
3334 { |
|
3335 res = elem (i, j); |
|
3336 absres = abs (res); |
|
3337 } |
|
3338 result.elem (i) = res; |
|
3339 } |
458
|
3340 } |
|
3341 } |
|
3342 |
|
3343 return result; |
|
3344 } |
|
3345 |
|
3346 ComplexColumnVector |
|
3347 ComplexMatrix::row_max_loc (void) const |
|
3348 { |
|
3349 ComplexColumnVector result; |
|
3350 |
|
3351 int nr = rows (); |
|
3352 int nc = cols (); |
|
3353 |
|
3354 if (nr > 0 && nc > 0) |
|
3355 { |
|
3356 result.resize (nr); |
|
3357 |
|
3358 for (int i = 0; i < nr; i++) |
|
3359 { |
891
|
3360 int column_is_real_only = 1; |
|
3361 for (int j = 0; j < nc; j++) |
|
3362 if (imag (elem (i, j)) != 0.0) |
|
3363 { |
|
3364 column_is_real_only = 0; |
|
3365 break; |
|
3366 } |
|
3367 |
|
3368 if (column_is_real_only) |
|
3369 { |
|
3370 double res = 0; |
|
3371 double tmp = real (elem (i, 0)); |
|
3372 for (int j = 1; j < nc; j++) |
|
3373 if (real (elem (i, j)) > tmp) |
|
3374 res = j; |
|
3375 |
|
3376 result.elem (i) = res + 1; |
|
3377 } |
|
3378 else |
|
3379 { |
|
3380 Complex res = 0; |
|
3381 double absres = abs (elem (i, 0)); |
|
3382 for (int j = 1; j < nc; j++) |
|
3383 if (abs (elem (i, j)) > absres) |
|
3384 { |
|
3385 res = j; |
|
3386 absres = abs (elem (i, j)); |
|
3387 } |
|
3388 result.elem (i) = res + 1; |
|
3389 } |
458
|
3390 } |
|
3391 } |
|
3392 |
|
3393 return result; |
|
3394 } |
|
3395 |
|
3396 ComplexRowVector |
|
3397 ComplexMatrix::column_min (void) const |
|
3398 { |
|
3399 ComplexRowVector result; |
|
3400 |
|
3401 int nr = rows (); |
|
3402 int nc = cols (); |
|
3403 |
|
3404 if (nr > 0 && nc > 0) |
|
3405 { |
|
3406 result.resize (nc); |
|
3407 |
|
3408 for (int j = 0; j < nc; j++) |
|
3409 { |
891
|
3410 int column_is_real_only = 1; |
|
3411 for (int i = 0; i < nr; i++) |
|
3412 if (imag (elem (i, j)) != 0.0) |
458
|
3413 { |
891
|
3414 column_is_real_only = 0; |
|
3415 break; |
458
|
3416 } |
891
|
3417 |
|
3418 if (column_is_real_only) |
|
3419 { |
|
3420 double res = real (elem (0, j)); |
|
3421 for (int i = 1; i < nr; i++) |
|
3422 { |
|
3423 double tmp = real (elem (i, j)); |
|
3424 if (tmp < res) |
|
3425 res = tmp; |
|
3426 } |
|
3427 result.elem (j) = res; |
|
3428 } |
|
3429 else |
|
3430 { |
|
3431 Complex res = elem (0, j); |
|
3432 double absres = abs (res); |
|
3433 for (int i = 1; i < nr; i++) |
|
3434 if (abs (elem (i, j)) < absres) |
|
3435 { |
|
3436 res = elem (i, j); |
|
3437 absres = abs (res); |
|
3438 } |
|
3439 result.elem (j) = res; |
|
3440 } |
458
|
3441 } |
|
3442 } |
|
3443 |
|
3444 return result; |
|
3445 } |
|
3446 |
|
3447 ComplexRowVector |
|
3448 ComplexMatrix::column_min_loc (void) const |
|
3449 { |
|
3450 ComplexRowVector result; |
|
3451 |
|
3452 int nr = rows (); |
|
3453 int nc = cols (); |
|
3454 |
|
3455 if (nr > 0 && nc > 0) |
|
3456 { |
|
3457 result.resize (nc); |
|
3458 |
|
3459 for (int j = 0; j < nc; j++) |
|
3460 { |
891
|
3461 int column_is_real_only = 1; |
|
3462 for (int i = 0; i < nr; i++) |
|
3463 if (imag (elem (i, j)) != 0.0) |
|
3464 { |
|
3465 column_is_real_only = 0; |
|
3466 break; |
|
3467 } |
|
3468 |
|
3469 if (column_is_real_only) |
|
3470 { |
|
3471 double res = 0; |
892
|
3472 double tmp = real (elem (0, j)); |
891
|
3473 for (int i = 1; i < nr; i++) |
|
3474 if (real (elem (i, j)) < tmp) |
|
3475 res = i; |
|
3476 |
|
3477 result.elem (j) = res + 1; |
|
3478 } |
|
3479 else |
|
3480 { |
|
3481 Complex res = 0; |
|
3482 double absres = abs (elem (0, j)); |
|
3483 for (int i = 1; i < nr; i++) |
|
3484 if (abs (elem (i, j)) < absres) |
|
3485 { |
|
3486 res = i; |
|
3487 absres = abs (elem (i, j)); |
|
3488 } |
|
3489 result.elem (j) = res + 1; |
|
3490 } |
458
|
3491 } |
|
3492 } |
|
3493 |
|
3494 return result; |
|
3495 } |
|
3496 |
|
3497 ComplexRowVector |
|
3498 ComplexMatrix::column_max (void) const |
|
3499 { |
|
3500 ComplexRowVector result; |
|
3501 |
|
3502 int nr = rows (); |
|
3503 int nc = cols (); |
|
3504 |
|
3505 if (nr > 0 && nc > 0) |
|
3506 { |
|
3507 result.resize (nc); |
|
3508 |
|
3509 for (int j = 0; j < nc; j++) |
|
3510 { |
891
|
3511 int column_is_real_only = 1; |
|
3512 for (int i = 0; i < nr; i++) |
|
3513 if (imag (elem (i, j)) != 0.0) |
458
|
3514 { |
891
|
3515 column_is_real_only = 0; |
|
3516 break; |
458
|
3517 } |
891
|
3518 |
|
3519 if (column_is_real_only) |
|
3520 { |
|
3521 double res = real (elem (0, j)); |
|
3522 for (int i = 1; i < nr; i++) |
|
3523 { |
|
3524 double tmp = real (elem (i, j)); |
|
3525 if (tmp > res) |
|
3526 res = tmp; |
|
3527 } |
|
3528 result.elem (j) = res; |
|
3529 } |
|
3530 else |
|
3531 { |
|
3532 Complex res = elem (0, j); |
|
3533 double absres = abs (res); |
|
3534 for (int i = 1; i < nr; i++) |
|
3535 if (abs (elem (i, j)) > absres) |
|
3536 { |
|
3537 res = elem (i, j); |
|
3538 absres = abs (res); |
|
3539 } |
|
3540 result.elem (j) = res; |
|
3541 } |
458
|
3542 } |
|
3543 } |
|
3544 |
|
3545 return result; |
|
3546 } |
|
3547 |
|
3548 ComplexRowVector |
|
3549 ComplexMatrix::column_max_loc (void) const |
|
3550 { |
|
3551 ComplexRowVector result; |
|
3552 |
|
3553 int nr = rows (); |
|
3554 int nc = cols (); |
|
3555 |
|
3556 if (nr > 0 && nc > 0) |
|
3557 { |
|
3558 result.resize (nc); |
|
3559 |
|
3560 for (int j = 0; j < nc; j++) |
|
3561 { |
891
|
3562 int column_is_real_only = 1; |
|
3563 for (int i = 0; i < nr; i++) |
|
3564 if (imag (elem (i, j)) != 0.0) |
|
3565 { |
|
3566 column_is_real_only = 0; |
|
3567 break; |
|
3568 } |
|
3569 |
|
3570 if (column_is_real_only) |
|
3571 { |
|
3572 double res = 0; |
892
|
3573 double tmp = real (elem (0, j)); |
891
|
3574 for (int i = 1; i < nr; i++) |
|
3575 if (real (elem (i, j)) > tmp) |
|
3576 res = i; |
|
3577 |
|
3578 result.elem (j) = res + 1; |
|
3579 } |
|
3580 else |
|
3581 { |
|
3582 Complex res = 0; |
|
3583 double absres = abs (elem (0, j)); |
|
3584 for (int i = 1; i < nr; i++) |
|
3585 if (abs (elem (i, j)) > absres) |
|
3586 { |
|
3587 res = i; |
|
3588 absres = abs (elem (i, j)); |
|
3589 } |
|
3590 result.elem (j) = res + 1; |
|
3591 } |
458
|
3592 } |
|
3593 } |
|
3594 |
|
3595 return result; |
|
3596 } |
|
3597 |
|
3598 // i/o |
|
3599 |
|
3600 ostream& |
|
3601 operator << (ostream& os, const ComplexMatrix& a) |
|
3602 { |
|
3603 // int field_width = os.precision () + 7; |
|
3604 for (int i = 0; i < a.rows (); i++) |
|
3605 { |
|
3606 for (int j = 0; j < a.cols (); j++) |
|
3607 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
3608 os << "\n"; |
|
3609 } |
|
3610 return os; |
|
3611 } |
|
3612 |
|
3613 istream& |
|
3614 operator >> (istream& is, ComplexMatrix& a) |
|
3615 { |
|
3616 int nr = a.rows (); |
|
3617 int nc = a.cols (); |
|
3618 |
|
3619 if (nr < 1 || nc < 1) |
|
3620 is.clear (ios::badbit); |
|
3621 else |
|
3622 { |
|
3623 Complex tmp; |
|
3624 for (int i = 0; i < nr; i++) |
|
3625 for (int j = 0; j < nc; j++) |
|
3626 { |
|
3627 is >> tmp; |
|
3628 if (is) |
|
3629 a.elem (i, j) = tmp; |
|
3630 else |
|
3631 break; |
|
3632 } |
|
3633 } |
|
3634 |
|
3635 return is; |
|
3636 } |
|
3637 |
1819
|
3638 ComplexMatrix |
|
3639 Givens (const Complex& x, const Complex& y) |
|
3640 { |
|
3641 double cc; |
|
3642 Complex cs, temp_r; |
|
3643 |
|
3644 F77_FCN (zlartg, ZLARTG) (x, y, cc, cs, temp_r); |
|
3645 |
|
3646 ComplexMatrix g (2, 2); |
|
3647 |
|
3648 g.elem (0, 0) = cc; |
|
3649 g.elem (1, 1) = cc; |
|
3650 g.elem (0, 1) = cs; |
|
3651 g.elem (1, 0) = -conj (cs); |
|
3652 |
|
3653 return g; |
|
3654 } |
|
3655 |
|
3656 ComplexMatrix |
|
3657 Sylvester (const ComplexMatrix& a, const ComplexMatrix& b, |
|
3658 const ComplexMatrix& c) |
|
3659 { |
|
3660 ComplexMatrix retval; |
|
3661 |
|
3662 // XXX FIXME XXX -- need to check that a, b, and c are all the same |
|
3663 // size. |
|
3664 |
|
3665 // Compute Schur decompositions |
|
3666 |
|
3667 ComplexSCHUR as (a, "U"); |
|
3668 ComplexSCHUR bs (b, "U"); |
|
3669 |
|
3670 // Transform c to new coordinates. |
|
3671 |
|
3672 ComplexMatrix ua = as.unitary_matrix (); |
|
3673 ComplexMatrix sch_a = as.schur_matrix (); |
|
3674 |
|
3675 ComplexMatrix ub = bs.unitary_matrix (); |
|
3676 ComplexMatrix sch_b = bs.schur_matrix (); |
|
3677 |
|
3678 ComplexMatrix cx = ua.hermitian () * c * ub; |
|
3679 |
|
3680 // Solve the sylvester equation, back-transform, and return the |
|
3681 // solution. |
|
3682 |
|
3683 int a_nr = a.rows (); |
|
3684 int b_nr = b.rows (); |
|
3685 |
|
3686 double scale; |
|
3687 int info; |
|
3688 |
|
3689 F77_FCN (ztrsyl, ZTRSYL) ("N", "N", 1, a_nr, b_nr, |
|
3690 sch_a.fortran_vec (), a_nr, |
|
3691 sch_b.fortran_vec (), b_nr, |
|
3692 cx.fortran_vec (), a_nr, scale, |
|
3693 info, 1L, 1L); |
|
3694 |
|
3695 // XXX FIXME XXX -- check info? |
|
3696 |
|
3697 retval = -ua * cx * ub.hermitian (); |
|
3698 |
|
3699 return retval; |
|
3700 } |
|
3701 |
458
|
3702 /* |
|
3703 ;;; Local Variables: *** |
|
3704 ;;; mode: C++ *** |
|
3705 ;;; page-delimiter: "^/\\*" *** |
|
3706 ;;; End: *** |
|
3707 */ |