458
|
1 // Matrix manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
|
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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #if defined (__GNUG__) |
|
29 #pragma implementation |
|
30 #endif |
|
31 |
|
32 #include <sys/types.h> |
|
33 #include <iostream.h> |
740
|
34 #include <float.h> |
458
|
35 |
|
36 #include <Complex.h> |
|
37 |
|
38 #include "mx-base.h" |
|
39 #include "CmplxDET.h" |
740
|
40 #include "CmplxSVD.h" |
458
|
41 #include "mx-inlines.cc" |
|
42 #include "lo-error.h" |
|
43 #include "f77-uscore.h" |
|
44 |
|
45 // Fortran functions we call. |
|
46 |
|
47 extern "C" |
|
48 { |
|
49 int F77_FCN (zgemm) (const char*, const char*, const int*, |
|
50 const int*, const int*, const Complex*, |
|
51 const Complex*, const int*, const Complex*, |
|
52 const int*, const Complex*, Complex*, const int*, |
|
53 long, long); |
|
54 |
|
55 int F77_FCN (zgemv) (const char*, const int*, const int*, |
|
56 const Complex*, const Complex*, const int*, |
|
57 const Complex*, const int*, const Complex*, |
|
58 Complex*, const int*, long); |
|
59 |
|
60 int F77_FCN (zgeco) (Complex*, const int*, const int*, int*, |
|
61 double*, Complex*); |
|
62 |
|
63 int F77_FCN (zgedi) (Complex*, const int*, const int*, int*, |
|
64 Complex*, Complex*, const int*); |
|
65 |
|
66 int F77_FCN (zgesl) (Complex*, const int*, const int*, int*, |
|
67 Complex*, const int*); |
|
68 |
|
69 int F77_FCN (zgelss) (const int*, const int*, const int*, Complex*, |
|
70 const int*, Complex*, const int*, double*, |
|
71 const double*, int*, Complex*, const int*, |
|
72 double*, int*); |
|
73 |
|
74 // Note that the original complex fft routines were not written for |
|
75 // double complex arguments. They have been modified by adding an |
|
76 // implicit double precision (a-h,o-z) statement at the beginning of |
|
77 // each subroutine. |
|
78 |
|
79 int F77_FCN (cffti) (const int*, Complex*); |
|
80 |
|
81 int F77_FCN (cfftf) (const int*, Complex*, Complex*); |
|
82 |
|
83 int F77_FCN (cfftb) (const int*, Complex*, Complex*); |
|
84 } |
|
85 |
|
86 #define KLUDGE_MATRICES |
|
87 #define TYPE Complex |
|
88 #define KL_MAT_TYPE ComplexMatrix |
|
89 #include "mx-kludge.cc" |
|
90 #undef KLUDGE_MATRICES |
|
91 #undef TYPE |
|
92 #undef KL_MAT_TYPE |
|
93 |
|
94 /* |
|
95 * Complex Matrix class |
|
96 */ |
|
97 |
|
98 ComplexMatrix::ComplexMatrix (const Matrix& a) |
|
99 : Array2<Complex> (a.rows (), a.cols ()) |
|
100 { |
|
101 for (int j = 0; j < cols (); j++) |
|
102 for (int i = 0; i < rows (); i++) |
|
103 elem (i, j) = a.elem (i, j); |
|
104 } |
|
105 |
|
106 ComplexMatrix::ComplexMatrix (const DiagMatrix& a) |
|
107 : Array2<Complex> (a.rows (), a.cols (), 0.0) |
|
108 { |
|
109 for (int i = 0; i < a.length (); i++) |
|
110 elem (i, i) = a.elem (i, i); |
|
111 } |
|
112 |
|
113 ComplexMatrix::ComplexMatrix (const ComplexDiagMatrix& a) |
|
114 : Array2<Complex> (a.rows (), a.cols (), 0.0) |
|
115 { |
|
116 for (int i = 0; i < a.length (); i++) |
|
117 elem (i, i) = a.elem (i, i); |
|
118 } |
|
119 |
|
120 #if 0 |
|
121 ComplexMatrix& |
|
122 ComplexMatrix::resize (int r, int c) |
|
123 { |
|
124 if (r < 0 || c < 0) |
|
125 { |
|
126 (*current_liboctave_error_handler) |
|
127 ("can't resize to negative dimensions"); |
|
128 return *this; |
|
129 } |
|
130 |
|
131 int new_len = r * c; |
533
|
132 Complex* new_data = 0; |
458
|
133 if (new_len > 0) |
|
134 { |
|
135 new_data = new Complex [new_len]; |
|
136 |
|
137 int min_r = nr < r ? nr : r; |
|
138 int min_c = nc < c ? nc : c; |
|
139 |
|
140 for (int j = 0; j < min_c; j++) |
|
141 for (int i = 0; i < min_r; i++) |
|
142 new_data[r*j+i] = elem (i, j); |
|
143 } |
|
144 |
|
145 delete [] data; |
|
146 nr = r; |
|
147 nc = c; |
|
148 len = new_len; |
|
149 data = new_data; |
|
150 |
|
151 return *this; |
|
152 } |
|
153 |
|
154 ComplexMatrix& |
|
155 ComplexMatrix::resize (int r, int c, double val) |
|
156 { |
|
157 if (r < 0 || c < 0) |
|
158 { |
|
159 (*current_liboctave_error_handler) |
|
160 ("can't resize to negative dimensions"); |
|
161 return *this; |
|
162 } |
|
163 |
|
164 int new_len = r * c; |
533
|
165 Complex *new_data = 0; |
458
|
166 if (new_len > 0) |
|
167 { |
|
168 new_data = new Complex [new_len]; |
|
169 |
|
170 // There may be faster or cleaner ways to do this. |
|
171 |
|
172 if (r > nr || c > nc) |
|
173 copy (new_data, new_len, val); |
|
174 |
|
175 int min_r = nr < r ? nr : r; |
|
176 int min_c = nc < c ? nc : c; |
|
177 |
|
178 for (int j = 0; j < min_c; j++) |
|
179 for (int i = 0; i < min_r; i++) |
|
180 new_data[r*j+i] = elem (i, j); |
|
181 } |
|
182 |
|
183 delete [] data; |
|
184 nr = r; |
|
185 nc = c; |
|
186 len = new_len; |
|
187 data = new_data; |
|
188 |
|
189 return *this; |
|
190 } |
|
191 |
|
192 ComplexMatrix& |
|
193 ComplexMatrix::resize (int r, int c, const Complex& val) |
|
194 { |
|
195 if (r < 0 || c < 0) |
|
196 { |
|
197 (*current_liboctave_error_handler) |
|
198 ("can't resize to negative dimensions"); |
|
199 return *this; |
|
200 } |
|
201 |
|
202 int new_len = r * c; |
533
|
203 Complex *new_data = 0; |
458
|
204 if (new_len > 0) |
|
205 { |
|
206 new_data = new Complex [new_len]; |
|
207 |
|
208 // There may be faster or cleaner ways to do this. |
|
209 |
|
210 if (r > nr || c > nc) |
|
211 copy (new_data, new_len, val); |
|
212 |
|
213 int min_r = nr < r ? nr : r; |
|
214 int min_c = nc < c ? nc : c; |
|
215 |
|
216 for (int j = 0; j < min_c; j++) |
|
217 for (int i = 0; i < min_r; i++) |
|
218 new_data[r*j+i] = elem (i, j); |
|
219 } |
|
220 |
|
221 delete [] data; |
|
222 nr = r; |
|
223 nc = c; |
|
224 len = new_len; |
|
225 data = new_data; |
|
226 |
|
227 return *this; |
|
228 } |
|
229 #endif |
|
230 |
|
231 int |
|
232 ComplexMatrix::operator == (const ComplexMatrix& a) const |
|
233 { |
|
234 if (rows () != a.rows () || cols () != a.cols ()) |
|
235 return 0; |
|
236 |
|
237 return equal (data (), a.data (), length ()); |
|
238 } |
|
239 |
|
240 int |
|
241 ComplexMatrix::operator != (const ComplexMatrix& a) const |
|
242 { |
|
243 return !(*this == a); |
|
244 } |
|
245 |
|
246 // destructive insert/delete/reorder operations |
|
247 |
|
248 ComplexMatrix& |
|
249 ComplexMatrix::insert (const Matrix& a, int r, int c) |
|
250 { |
|
251 int a_nr = a.rows (); |
|
252 int a_nc = a.cols (); |
|
253 if (r < 0 || r + a_nr - 1 > rows () || c < 0 || c + a_nc - 1 > cols ()) |
|
254 { |
|
255 (*current_liboctave_error_handler) ("range error for insert"); |
|
256 return *this; |
|
257 } |
|
258 |
|
259 for (int j = 0; j < a_nc; j++) |
|
260 for (int i = 0; i < a_nr; i++) |
|
261 elem (r+i, c+j) = a.elem (i, j); |
|
262 |
|
263 return *this; |
|
264 } |
|
265 |
|
266 ComplexMatrix& |
|
267 ComplexMatrix::insert (const RowVector& a, int r, int c) |
|
268 { |
|
269 int a_len = a.length (); |
|
270 if (r < 0 || r >= rows () || c < 0 || c + a_len - 1 > cols ()) |
|
271 { |
|
272 (*current_liboctave_error_handler) ("range error for insert"); |
|
273 return *this; |
|
274 } |
|
275 |
|
276 for (int i = 0; i < a_len; i++) |
|
277 elem (r, c+i) = a.elem (i); |
|
278 |
|
279 return *this; |
|
280 } |
|
281 |
|
282 ComplexMatrix& |
|
283 ComplexMatrix::insert (const ColumnVector& a, int r, int c) |
|
284 { |
|
285 int a_len = a.length (); |
|
286 if (r < 0 || r + a_len - 1 > rows () || c < 0 || c >= cols ()) |
|
287 { |
|
288 (*current_liboctave_error_handler) ("range error for insert"); |
|
289 return *this; |
|
290 } |
|
291 |
|
292 for (int i = 0; i < a_len; i++) |
|
293 elem (r+i, c) = a.elem (i); |
|
294 |
|
295 return *this; |
|
296 } |
|
297 |
|
298 ComplexMatrix& |
|
299 ComplexMatrix::insert (const DiagMatrix& a, int r, int c) |
|
300 { |
|
301 if (r < 0 || r + a.rows () - 1 > rows () |
|
302 || c < 0 || c + a.cols () - 1 > cols ()) |
|
303 { |
|
304 (*current_liboctave_error_handler) ("range error for insert"); |
|
305 return *this; |
|
306 } |
|
307 |
|
308 for (int i = 0; i < a.length (); i++) |
|
309 elem (r+i, c+i) = a.elem (i, i); |
|
310 |
|
311 return *this; |
|
312 } |
|
313 |
|
314 ComplexMatrix& |
|
315 ComplexMatrix::insert (const ComplexMatrix& a, int r, int c) |
|
316 { |
|
317 int a_nr = a.rows (); |
|
318 int a_nc = a.cols (); |
|
319 if (r < 0 || r + a_nr - 1 > rows () || c < 0 || c + a_nc - 1 > cols ()) |
|
320 { |
|
321 (*current_liboctave_error_handler) ("range error for insert"); |
|
322 return *this; |
|
323 } |
|
324 |
|
325 for (int j = 0; j < a_nc; j++) |
|
326 for (int i = 0; i < a_nr; i++) |
|
327 elem (r+i, c+j) = a.elem (i, j); |
|
328 |
|
329 return *this; |
|
330 } |
|
331 |
|
332 ComplexMatrix& |
|
333 ComplexMatrix::insert (const ComplexRowVector& a, int r, int c) |
|
334 { |
|
335 int a_len = a.length (); |
|
336 if (r < 0 || r >= rows () || c < 0 || c + a_len - 1 > cols ()) |
|
337 { |
|
338 (*current_liboctave_error_handler) ("range error for insert"); |
|
339 return *this; |
|
340 } |
|
341 |
|
342 for (int i = 0; i < a_len; i++) |
|
343 elem (r, c+i) = a.elem (i); |
|
344 |
|
345 return *this; |
|
346 } |
|
347 |
|
348 ComplexMatrix& |
|
349 ComplexMatrix::insert (const ComplexColumnVector& a, int r, int c) |
|
350 { |
|
351 int a_len = a.length (); |
|
352 if (r < 0 || r + a_len - 1 > rows () || c < 0 || c >= cols ()) |
|
353 { |
|
354 (*current_liboctave_error_handler) ("range error for insert"); |
|
355 return *this; |
|
356 } |
|
357 |
|
358 for (int i = 0; i < a_len; i++) |
|
359 elem (r+i, c) = a.elem (i); |
|
360 |
|
361 return *this; |
|
362 } |
|
363 |
|
364 ComplexMatrix& |
|
365 ComplexMatrix::insert (const ComplexDiagMatrix& a, int r, int c) |
|
366 { |
|
367 if (r < 0 || r + a.rows () - 1 > rows () |
|
368 || c < 0 || c + a.cols () - 1 > cols ()) |
|
369 { |
|
370 (*current_liboctave_error_handler) ("range error for insert"); |
|
371 return *this; |
|
372 } |
|
373 |
|
374 for (int i = 0; i < a.length (); i++) |
|
375 elem (r+i, c+i) = a.elem (i, i); |
|
376 |
|
377 return *this; |
|
378 } |
|
379 |
|
380 ComplexMatrix& |
|
381 ComplexMatrix::fill (double val) |
|
382 { |
|
383 int nr = rows (); |
|
384 int nc = cols (); |
|
385 if (nr > 0 && nc > 0) |
|
386 for (int j = 0; j < nc; j++) |
|
387 for (int i = 0; i < nr; i++) |
|
388 elem (i, j) = val; |
|
389 |
|
390 return *this; |
|
391 } |
|
392 |
|
393 ComplexMatrix& |
|
394 ComplexMatrix::fill (const Complex& val) |
|
395 { |
|
396 int nr = rows (); |
|
397 int nc = cols (); |
|
398 if (nr > 0 && nc > 0) |
|
399 for (int j = 0; j < nc; j++) |
|
400 for (int i = 0; i < nr; i++) |
|
401 elem (i, j) = val; |
|
402 |
|
403 return *this; |
|
404 } |
|
405 |
|
406 ComplexMatrix& |
|
407 ComplexMatrix::fill (double val, int r1, int c1, int r2, int c2) |
|
408 { |
|
409 int nr = rows (); |
|
410 int nc = cols (); |
|
411 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
|
412 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) |
|
413 { |
|
414 (*current_liboctave_error_handler) ("range error for fill"); |
|
415 return *this; |
|
416 } |
|
417 |
|
418 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
419 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
420 |
|
421 for (int j = c1; j <= c2; j++) |
|
422 for (int i = r1; i <= r2; i++) |
|
423 elem (i, j) = val; |
|
424 |
|
425 return *this; |
|
426 } |
|
427 |
|
428 ComplexMatrix& |
|
429 ComplexMatrix::fill (const Complex& val, int r1, int c1, int r2, int c2) |
|
430 { |
|
431 int nr = rows (); |
|
432 int nc = cols (); |
|
433 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
|
434 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) |
|
435 { |
|
436 (*current_liboctave_error_handler) ("range error for fill"); |
|
437 return *this; |
|
438 } |
|
439 |
|
440 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
441 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
442 |
|
443 for (int j = c1; j <= c2; j++) |
|
444 for (int i = r1; i <= r2; i++) |
|
445 elem (i, j) = val; |
|
446 |
|
447 return *this; |
|
448 } |
|
449 |
|
450 ComplexMatrix |
|
451 ComplexMatrix::append (const Matrix& a) const |
|
452 { |
|
453 int nr = rows (); |
|
454 int nc = cols (); |
|
455 if (nr != a.rows ()) |
|
456 { |
|
457 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
458 return *this; |
|
459 } |
|
460 |
|
461 int nc_insert = nc; |
|
462 ComplexMatrix retval (nr, nc + a.cols ()); |
|
463 retval.insert (*this, 0, 0); |
|
464 retval.insert (a, 0, nc_insert); |
|
465 return retval; |
|
466 } |
|
467 |
|
468 ComplexMatrix |
|
469 ComplexMatrix::append (const RowVector& a) const |
|
470 { |
|
471 int nr = rows (); |
|
472 int nc = cols (); |
|
473 if (nr != 1) |
|
474 { |
|
475 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
476 return *this; |
|
477 } |
|
478 |
|
479 int nc_insert = nc; |
|
480 ComplexMatrix retval (nr, nc + a.length ()); |
|
481 retval.insert (*this, 0, 0); |
|
482 retval.insert (a, 0, nc_insert); |
|
483 return retval; |
|
484 } |
|
485 |
|
486 ComplexMatrix |
|
487 ComplexMatrix::append (const ColumnVector& a) const |
|
488 { |
|
489 int nr = rows (); |
|
490 int nc = cols (); |
|
491 if (nr != a.length ()) |
|
492 { |
|
493 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
494 return *this; |
|
495 } |
|
496 |
|
497 int nc_insert = nc; |
|
498 ComplexMatrix retval (nr, nc + 1); |
|
499 retval.insert (*this, 0, 0); |
|
500 retval.insert (a, 0, nc_insert); |
|
501 return retval; |
|
502 } |
|
503 |
|
504 ComplexMatrix |
|
505 ComplexMatrix::append (const DiagMatrix& a) const |
|
506 { |
|
507 int nr = rows (); |
|
508 int nc = cols (); |
|
509 if (nr != a.rows ()) |
|
510 { |
|
511 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
512 return *this; |
|
513 } |
|
514 |
|
515 int nc_insert = nc; |
|
516 ComplexMatrix retval (nr, nc + a.cols ()); |
|
517 retval.insert (*this, 0, 0); |
|
518 retval.insert (a, 0, nc_insert); |
|
519 return retval; |
|
520 } |
|
521 |
|
522 ComplexMatrix |
|
523 ComplexMatrix::append (const ComplexMatrix& a) const |
|
524 { |
|
525 int nr = rows (); |
|
526 int nc = cols (); |
|
527 if (nr != a.rows ()) |
|
528 { |
|
529 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
530 return *this; |
|
531 } |
|
532 |
|
533 int nc_insert = nc; |
|
534 ComplexMatrix retval (nr, nc + a.cols ()); |
|
535 retval.insert (*this, 0, 0); |
|
536 retval.insert (a, 0, nc_insert); |
|
537 return retval; |
|
538 } |
|
539 |
|
540 ComplexMatrix |
|
541 ComplexMatrix::append (const ComplexRowVector& a) const |
|
542 { |
|
543 int nr = rows (); |
|
544 int nc = cols (); |
|
545 if (nr != 1) |
|
546 { |
|
547 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
548 return *this; |
|
549 } |
|
550 |
|
551 int nc_insert = nc; |
|
552 ComplexMatrix retval (nr, nc + a.length ()); |
|
553 retval.insert (*this, 0, 0); |
|
554 retval.insert (a, 0, nc_insert); |
|
555 return retval; |
|
556 } |
|
557 |
|
558 ComplexMatrix |
|
559 ComplexMatrix::append (const ComplexColumnVector& a) const |
|
560 { |
|
561 int nr = rows (); |
|
562 int nc = cols (); |
|
563 if (nr != a.length ()) |
|
564 { |
|
565 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
566 return *this; |
|
567 } |
|
568 |
|
569 int nc_insert = nc; |
|
570 ComplexMatrix retval (nr, nc + 1); |
|
571 retval.insert (*this, 0, 0); |
|
572 retval.insert (a, 0, nc_insert); |
|
573 return retval; |
|
574 } |
|
575 |
|
576 ComplexMatrix |
|
577 ComplexMatrix::append (const ComplexDiagMatrix& a) const |
|
578 { |
|
579 int nr = rows (); |
|
580 int nc = cols (); |
|
581 if (nr != a.rows ()) |
|
582 { |
|
583 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
584 return *this; |
|
585 } |
|
586 |
|
587 int nc_insert = nc; |
|
588 ComplexMatrix retval (nr, nc + a.cols ()); |
|
589 retval.insert (*this, 0, 0); |
|
590 retval.insert (a, 0, nc_insert); |
|
591 return retval; |
|
592 } |
|
593 |
|
594 ComplexMatrix |
|
595 ComplexMatrix::stack (const Matrix& a) const |
|
596 { |
|
597 int nr = rows (); |
|
598 int nc = cols (); |
|
599 if (nc != a.cols ()) |
|
600 { |
|
601 (*current_liboctave_error_handler) |
|
602 ("column dimension mismatch for stack"); |
|
603 return *this; |
|
604 } |
|
605 |
|
606 int nr_insert = nr; |
|
607 ComplexMatrix retval (nr + a.rows (), nc); |
|
608 retval.insert (*this, 0, 0); |
|
609 retval.insert (a, nr_insert, 0); |
|
610 return retval; |
|
611 } |
|
612 |
|
613 ComplexMatrix |
|
614 ComplexMatrix::stack (const RowVector& a) const |
|
615 { |
|
616 int nr = rows (); |
|
617 int nc = cols (); |
|
618 if (nc != a.length ()) |
|
619 { |
|
620 (*current_liboctave_error_handler) |
|
621 ("column dimension mismatch for stack"); |
|
622 return *this; |
|
623 } |
|
624 |
|
625 int nr_insert = nr; |
|
626 ComplexMatrix retval (nr + 1, nc); |
|
627 retval.insert (*this, 0, 0); |
|
628 retval.insert (a, nr_insert, 0); |
|
629 return retval; |
|
630 } |
|
631 |
|
632 ComplexMatrix |
|
633 ComplexMatrix::stack (const ColumnVector& a) const |
|
634 { |
|
635 int nr = rows (); |
|
636 int nc = cols (); |
|
637 if (nc != 1) |
|
638 { |
|
639 (*current_liboctave_error_handler) |
|
640 ("column dimension mismatch for stack"); |
|
641 return *this; |
|
642 } |
|
643 |
|
644 int nr_insert = nr; |
|
645 ComplexMatrix retval (nr + a.length (), nc); |
|
646 retval.insert (*this, 0, 0); |
|
647 retval.insert (a, nr_insert, 0); |
|
648 return retval; |
|
649 } |
|
650 |
|
651 ComplexMatrix |
|
652 ComplexMatrix::stack (const DiagMatrix& a) const |
|
653 { |
|
654 int nr = rows (); |
|
655 int nc = cols (); |
|
656 if (nc != a.cols ()) |
|
657 { |
|
658 (*current_liboctave_error_handler) |
|
659 ("column dimension mismatch for stack"); |
|
660 return *this; |
|
661 } |
|
662 |
|
663 int nr_insert = nr; |
|
664 ComplexMatrix retval (nr + a.rows (), nc); |
|
665 retval.insert (*this, 0, 0); |
|
666 retval.insert (a, nr_insert, 0); |
|
667 return retval; |
|
668 } |
|
669 |
|
670 ComplexMatrix |
|
671 ComplexMatrix::stack (const ComplexMatrix& a) const |
|
672 { |
|
673 int nr = rows (); |
|
674 int nc = cols (); |
|
675 if (nc != a.cols ()) |
|
676 { |
|
677 (*current_liboctave_error_handler) |
|
678 ("column dimension mismatch for stack"); |
|
679 return *this; |
|
680 } |
|
681 |
|
682 int nr_insert = nr; |
|
683 ComplexMatrix retval (nr + a.rows (), nc); |
|
684 retval.insert (*this, 0, 0); |
|
685 retval.insert (a, nr_insert, 0); |
|
686 return retval; |
|
687 } |
|
688 |
|
689 ComplexMatrix |
|
690 ComplexMatrix::stack (const ComplexRowVector& a) const |
|
691 { |
|
692 int nr = rows (); |
|
693 int nc = cols (); |
|
694 if (nc != a.length ()) |
|
695 { |
|
696 (*current_liboctave_error_handler) |
|
697 ("column dimension mismatch for stack"); |
|
698 return *this; |
|
699 } |
|
700 |
|
701 int nr_insert = nr; |
|
702 ComplexMatrix retval (nr + 1, nc); |
|
703 retval.insert (*this, 0, 0); |
|
704 retval.insert (a, nr_insert, 0); |
|
705 return retval; |
|
706 } |
|
707 |
|
708 ComplexMatrix |
|
709 ComplexMatrix::stack (const ComplexColumnVector& a) const |
|
710 { |
|
711 int nr = rows (); |
|
712 int nc = cols (); |
|
713 if (nc != 1) |
|
714 { |
|
715 (*current_liboctave_error_handler) |
|
716 ("column dimension mismatch for stack"); |
|
717 return *this; |
|
718 } |
|
719 |
|
720 int nr_insert = nr; |
|
721 ComplexMatrix retval (nr + a.length (), nc); |
|
722 retval.insert (*this, 0, 0); |
|
723 retval.insert (a, nr_insert, 0); |
|
724 return retval; |
|
725 } |
|
726 |
|
727 ComplexMatrix |
|
728 ComplexMatrix::stack (const ComplexDiagMatrix& a) const |
|
729 { |
|
730 int nr = rows (); |
|
731 int nc = cols (); |
|
732 if (nc != a.cols ()) |
|
733 { |
|
734 (*current_liboctave_error_handler) |
|
735 ("column dimension mismatch for stack"); |
|
736 return *this; |
|
737 } |
|
738 |
|
739 int nr_insert = nr; |
|
740 ComplexMatrix retval (nr + a.rows (), nc); |
|
741 retval.insert (*this, 0, 0); |
|
742 retval.insert (a, nr_insert, 0); |
|
743 return retval; |
|
744 } |
|
745 |
|
746 ComplexMatrix |
|
747 ComplexMatrix::hermitian (void) const |
|
748 { |
|
749 int nr = rows (); |
|
750 int nc = cols (); |
|
751 ComplexMatrix result; |
|
752 if (length () > 0) |
|
753 { |
|
754 result.resize (nc, nr); |
|
755 for (int j = 0; j < nc; j++) |
|
756 for (int i = 0; i < nr; i++) |
|
757 result.elem (j, i) = conj (elem (i, j)); |
|
758 } |
|
759 return result; |
|
760 } |
|
761 |
|
762 ComplexMatrix |
|
763 ComplexMatrix::transpose (void) const |
|
764 { |
|
765 int nr = rows (); |
|
766 int nc = cols (); |
|
767 ComplexMatrix result (nc, nr); |
|
768 if (length () > 0) |
|
769 { |
|
770 for (int j = 0; j < nc; j++) |
|
771 for (int i = 0; i < nr; i++) |
|
772 result.elem (j, i) = elem (i, j); |
|
773 } |
|
774 return result; |
|
775 } |
|
776 |
|
777 Matrix |
|
778 real (const ComplexMatrix& a) |
|
779 { |
|
780 int a_len = a.length (); |
|
781 Matrix retval; |
|
782 if (a_len > 0) |
|
783 retval = Matrix (real_dup (a.data (), a_len), a.rows (), a.cols ()); |
|
784 return retval; |
|
785 } |
|
786 |
|
787 Matrix |
|
788 imag (const ComplexMatrix& a) |
|
789 { |
|
790 int a_len = a.length (); |
|
791 Matrix retval; |
|
792 if (a_len > 0) |
|
793 retval = Matrix (imag_dup (a.data (), a_len), a.rows (), a.cols ()); |
|
794 return retval; |
|
795 } |
|
796 |
|
797 ComplexMatrix |
|
798 conj (const ComplexMatrix& a) |
|
799 { |
|
800 int a_len = a.length (); |
|
801 ComplexMatrix retval; |
|
802 if (a_len > 0) |
|
803 retval = ComplexMatrix (conj_dup (a.data (), a_len), a.rows (), |
|
804 a.cols ()); |
|
805 return retval; |
|
806 } |
|
807 |
|
808 // resize is the destructive equivalent for this one |
|
809 |
|
810 ComplexMatrix |
|
811 ComplexMatrix::extract (int r1, int c1, int r2, int c2) const |
|
812 { |
|
813 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
814 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
815 |
|
816 int new_r = r2 - r1 + 1; |
|
817 int new_c = c2 - c1 + 1; |
|
818 |
|
819 ComplexMatrix result (new_r, new_c); |
|
820 |
|
821 for (int j = 0; j < new_c; j++) |
|
822 for (int i = 0; i < new_r; i++) |
|
823 result.elem (i, j) = elem (r1+i, c1+j); |
|
824 |
|
825 return result; |
|
826 } |
|
827 |
|
828 // extract row or column i. |
|
829 |
|
830 ComplexRowVector |
|
831 ComplexMatrix::row (int i) const |
|
832 { |
|
833 int nc = cols (); |
|
834 if (i < 0 || i >= rows ()) |
|
835 { |
|
836 (*current_liboctave_error_handler) ("invalid row selection"); |
|
837 return ComplexRowVector (); |
|
838 } |
|
839 |
|
840 ComplexRowVector retval (nc); |
|
841 for (int j = 0; j < cols (); j++) |
|
842 retval.elem (j) = elem (i, j); |
|
843 |
|
844 return retval; |
|
845 } |
|
846 |
|
847 ComplexRowVector |
|
848 ComplexMatrix::row (char *s) const |
|
849 { |
533
|
850 if (! s) |
458
|
851 { |
|
852 (*current_liboctave_error_handler) ("invalid row selection"); |
|
853 return ComplexRowVector (); |
|
854 } |
|
855 |
|
856 char c = *s; |
|
857 if (c == 'f' || c == 'F') |
|
858 return row (0); |
|
859 else if (c == 'l' || c == 'L') |
|
860 return row (rows () - 1); |
|
861 else |
|
862 { |
|
863 (*current_liboctave_error_handler) ("invalid row selection"); |
|
864 return ComplexRowVector (); |
|
865 } |
|
866 } |
|
867 |
|
868 ComplexColumnVector |
|
869 ComplexMatrix::column (int i) const |
|
870 { |
|
871 int nr = rows (); |
|
872 if (i < 0 || i >= cols ()) |
|
873 { |
|
874 (*current_liboctave_error_handler) ("invalid column selection"); |
|
875 return ComplexColumnVector (); |
|
876 } |
|
877 |
|
878 ComplexColumnVector retval (nr); |
|
879 for (int j = 0; j < nr; j++) |
|
880 retval.elem (j) = elem (j, i); |
|
881 |
|
882 return retval; |
|
883 } |
|
884 |
|
885 ComplexColumnVector |
|
886 ComplexMatrix::column (char *s) const |
|
887 { |
533
|
888 if (! s) |
458
|
889 { |
|
890 (*current_liboctave_error_handler) ("invalid column selection"); |
|
891 return ComplexColumnVector (); |
|
892 } |
|
893 |
|
894 char c = *s; |
|
895 if (c == 'f' || c == 'F') |
|
896 return column (0); |
|
897 else if (c == 'l' || c == 'L') |
|
898 return column (cols () - 1); |
|
899 else |
|
900 { |
|
901 (*current_liboctave_error_handler) ("invalid column selection"); |
|
902 return ComplexColumnVector (); |
|
903 } |
|
904 } |
|
905 |
|
906 ComplexMatrix |
|
907 ComplexMatrix::inverse (void) const |
|
908 { |
|
909 int info; |
479
|
910 double rcond; |
|
911 return inverse (info, rcond); |
458
|
912 } |
|
913 |
|
914 ComplexMatrix |
|
915 ComplexMatrix::inverse (int& info) const |
|
916 { |
|
917 double rcond; |
|
918 return inverse (info, rcond); |
|
919 } |
|
920 |
|
921 ComplexMatrix |
532
|
922 ComplexMatrix::inverse (int& info, double& rcond) const |
458
|
923 { |
|
924 int nr = rows (); |
|
925 int nc = cols (); |
|
926 int len = length (); |
|
927 if (nr != nc) |
|
928 { |
|
929 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
930 return ComplexMatrix (); |
|
931 } |
|
932 |
|
933 info = 0; |
|
934 |
|
935 int *ipvt = new int [nr]; |
|
936 Complex *z = new Complex [nr]; |
|
937 Complex *tmp_data = dup (data (), len); |
|
938 |
|
939 F77_FCN (zgeco) (tmp_data, &nr, &nc, ipvt, &rcond, z); |
|
940 |
532
|
941 volatile double tmp_rcond = rcond; |
|
942 if (tmp_rcond + 1.0 == 1.0) |
458
|
943 { |
|
944 info = -1; |
|
945 copy (tmp_data, data (), len); // Restore contents. |
|
946 } |
|
947 else |
|
948 { |
|
949 int job = 1; |
|
950 Complex dummy; |
|
951 |
|
952 F77_FCN (zgedi) (tmp_data, &nr, &nc, ipvt, &dummy, z, &job); |
|
953 } |
|
954 |
|
955 delete [] ipvt; |
|
956 delete [] z; |
|
957 |
|
958 return ComplexMatrix (tmp_data, nr, nc); |
|
959 } |
|
960 |
|
961 ComplexMatrix |
740
|
962 ComplexMatrix::pseudo_inverse (double tol) |
|
963 { |
|
964 ComplexSVD result (*this); |
|
965 |
|
966 DiagMatrix S = result.singular_values (); |
|
967 ComplexMatrix U = result.left_singular_matrix (); |
|
968 ComplexMatrix V = result.right_singular_matrix (); |
|
969 |
|
970 ColumnVector sigma = S.diag (); |
|
971 |
|
972 int r = sigma.length () - 1; |
|
973 int nr = rows (); |
|
974 int nc = cols (); |
|
975 |
|
976 if (tol <= 0.0) |
|
977 { |
|
978 if (nr > nc) |
|
979 tol = nr * sigma.elem (0) * DBL_EPSILON; |
|
980 else |
|
981 tol = nc * sigma.elem (0) * DBL_EPSILON; |
|
982 } |
|
983 |
|
984 while (r >= 0 && sigma.elem (r) < tol) |
|
985 r--; |
|
986 |
|
987 if (r < 0) |
|
988 return ComplexMatrix (nc, nr, 0.0); |
|
989 else |
|
990 { |
|
991 ComplexMatrix Ur = U.extract (0, 0, nr-1, r); |
|
992 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); |
|
993 ComplexMatrix Vr = V.extract (0, 0, nc-1, r); |
|
994 return Vr * D * Ur.hermitian (); |
|
995 } |
|
996 } |
|
997 |
|
998 ComplexMatrix |
458
|
999 ComplexMatrix::fourier (void) const |
|
1000 { |
|
1001 int nr = rows (); |
|
1002 int nc = cols (); |
|
1003 int npts, nsamples; |
|
1004 if (nr == 1 || nc == 1) |
|
1005 { |
|
1006 npts = nr > nc ? nr : nc; |
|
1007 nsamples = 1; |
|
1008 } |
|
1009 else |
|
1010 { |
|
1011 npts = nr; |
|
1012 nsamples = nc; |
|
1013 } |
|
1014 |
|
1015 int nn = 4*npts+15; |
|
1016 Complex *wsave = new Complex [nn]; |
|
1017 Complex *tmp_data = dup (data (), length ()); |
|
1018 |
|
1019 F77_FCN (cffti) (&npts, wsave); |
|
1020 |
|
1021 for (int j = 0; j < nsamples; j++) |
|
1022 F77_FCN (cfftf) (&npts, &tmp_data[npts*j], wsave); |
|
1023 |
|
1024 delete [] wsave; |
|
1025 |
|
1026 return ComplexMatrix (tmp_data, nr, nc); |
|
1027 } |
|
1028 |
|
1029 ComplexMatrix |
|
1030 ComplexMatrix::ifourier (void) const |
|
1031 { |
|
1032 int nr = rows (); |
|
1033 int nc = cols (); |
|
1034 int npts, nsamples; |
|
1035 if (nr == 1 || nc == 1) |
|
1036 { |
|
1037 npts = nr > nc ? nr : nc; |
|
1038 nsamples = 1; |
|
1039 } |
|
1040 else |
|
1041 { |
|
1042 npts = nr; |
|
1043 nsamples = nc; |
|
1044 } |
|
1045 |
|
1046 int nn = 4*npts+15; |
|
1047 Complex *wsave = new Complex [nn]; |
|
1048 Complex *tmp_data = dup (data (), length ()); |
|
1049 |
|
1050 F77_FCN (cffti) (&npts, wsave); |
|
1051 |
|
1052 for (int j = 0; j < nsamples; j++) |
|
1053 F77_FCN (cfftb) (&npts, &tmp_data[npts*j], wsave); |
|
1054 |
|
1055 for (j = 0; j < npts*nsamples; j++) |
|
1056 tmp_data[j] = tmp_data[j] / (double) npts; |
|
1057 |
|
1058 delete [] wsave; |
|
1059 |
|
1060 return ComplexMatrix (tmp_data, nr, nc); |
|
1061 } |
|
1062 |
677
|
1063 ComplexMatrix |
|
1064 ComplexMatrix::fourier2d (void) const |
|
1065 { |
|
1066 int nr = rows (); |
|
1067 int nc = cols (); |
|
1068 int npts, nsamples; |
|
1069 if (nr == 1 || nc == 1) |
|
1070 { |
|
1071 npts = nr > nc ? nr : nc; |
|
1072 nsamples = 1; |
|
1073 } |
|
1074 else |
|
1075 { |
|
1076 npts = nr; |
|
1077 nsamples = nc; |
|
1078 } |
|
1079 |
|
1080 int nn = 4*npts+15; |
|
1081 Complex *wsave = new Complex [nn]; |
|
1082 Complex *tmp_data = dup (data (), length ()); |
|
1083 |
|
1084 F77_FCN (cffti) (&npts, wsave); |
|
1085 |
|
1086 for (int j = 0; j < nsamples; j++) |
|
1087 F77_FCN (cfftf) (&npts, &tmp_data[npts*j], wsave); |
|
1088 |
|
1089 delete [] wsave; |
|
1090 |
|
1091 npts = nc; |
|
1092 nsamples = nr; |
|
1093 nn = 4*npts+15; |
|
1094 wsave = new Complex [nn]; |
|
1095 Complex *row = new Complex[npts]; |
|
1096 |
|
1097 F77_FCN (cffti) (&npts, wsave); |
|
1098 |
|
1099 for (j = 0; j < nsamples; j++) |
|
1100 { |
|
1101 for (int i = 0; i < npts; i++) |
|
1102 row[i] = tmp_data[i*nr + j]; |
|
1103 |
|
1104 F77_FCN (cfftf) (&npts, row, wsave); |
|
1105 |
|
1106 for (i = 0; i < npts; i++) |
|
1107 tmp_data[i*nr + j] = row[i]; |
|
1108 } |
|
1109 |
|
1110 delete [] wsave; |
|
1111 delete [] row; |
|
1112 |
|
1113 return ComplexMatrix (tmp_data, nr, nc); |
|
1114 } |
|
1115 |
|
1116 ComplexMatrix |
|
1117 ComplexMatrix::ifourier2d (void) const |
|
1118 { |
|
1119 int nr = rows (); |
|
1120 int nc = cols (); |
|
1121 int npts, nsamples; |
|
1122 if (nr == 1 || nc == 1) |
|
1123 { |
|
1124 npts = nr > nc ? nr : nc; |
|
1125 nsamples = 1; |
|
1126 } |
|
1127 else |
|
1128 { |
|
1129 npts = nr; |
|
1130 nsamples = nc; |
|
1131 } |
|
1132 |
|
1133 int nn = 4*npts+15; |
|
1134 Complex *wsave = new Complex [nn]; |
|
1135 Complex *tmp_data = dup (data (), length ()); |
|
1136 |
|
1137 F77_FCN (cffti) (&npts, wsave); |
|
1138 |
|
1139 for (int j = 0; j < nsamples; j++) |
|
1140 F77_FCN (cfftb) (&npts, &tmp_data[npts*j], wsave); |
|
1141 |
|
1142 delete [] wsave; |
|
1143 |
|
1144 for (j = 0; j < npts*nsamples; j++) |
|
1145 tmp_data[j] = tmp_data[j] / (double) npts; |
|
1146 |
|
1147 npts = nc; |
|
1148 nsamples = nr; |
|
1149 nn = 4*npts+15; |
|
1150 wsave = new Complex [nn]; |
|
1151 Complex *row = new Complex[npts]; |
|
1152 |
|
1153 F77_FCN (cffti) (&npts, wsave); |
|
1154 |
|
1155 for (j = 0; j < nsamples; j++) |
|
1156 { |
|
1157 for (int i = 0; i < npts; i++) |
|
1158 row[i] = tmp_data[i*nr + j]; |
|
1159 |
|
1160 F77_FCN (cfftb) (&npts, row, wsave); |
|
1161 |
|
1162 for (i = 0; i < npts; i++) |
|
1163 tmp_data[i*nr + j] = row[i] / (double) npts; |
|
1164 } |
|
1165 |
|
1166 delete [] wsave; |
|
1167 delete [] row; |
|
1168 |
|
1169 return ComplexMatrix (tmp_data, nr, nc); |
|
1170 } |
|
1171 |
458
|
1172 ComplexDET |
|
1173 ComplexMatrix::determinant (void) const |
|
1174 { |
|
1175 int info; |
|
1176 double rcond; |
|
1177 return determinant (info, rcond); |
|
1178 } |
|
1179 |
|
1180 ComplexDET |
|
1181 ComplexMatrix::determinant (int& info) const |
|
1182 { |
|
1183 double rcond; |
|
1184 return determinant (info, rcond); |
|
1185 } |
|
1186 |
|
1187 ComplexDET |
532
|
1188 ComplexMatrix::determinant (int& info, double& rcond) const |
458
|
1189 { |
|
1190 ComplexDET retval; |
|
1191 |
|
1192 int nr = rows (); |
|
1193 int nc = cols (); |
|
1194 |
|
1195 if (nr == 0 || nc == 0) |
|
1196 { |
|
1197 Complex d[2]; |
|
1198 d[0] = 1.0; |
|
1199 d[1] = 0.0; |
|
1200 retval = ComplexDET (d); |
|
1201 } |
|
1202 else |
|
1203 { |
|
1204 info = 0; |
|
1205 int *ipvt = new int [nr]; |
|
1206 |
|
1207 Complex *z = new Complex [nr]; |
|
1208 Complex *tmp_data = dup (data (), length ()); |
|
1209 |
|
1210 F77_FCN (zgeco) (tmp_data, &nr, &nr, ipvt, &rcond, z); |
|
1211 |
532
|
1212 volatile double tmp_rcond = rcond; |
|
1213 if (tmp_rcond + 1.0 == 1.0) |
458
|
1214 { |
|
1215 info = -1; |
|
1216 retval = ComplexDET (); |
|
1217 } |
|
1218 else |
|
1219 { |
|
1220 int job = 10; |
|
1221 Complex d[2]; |
|
1222 F77_FCN (zgedi) (tmp_data, &nr, &nr, ipvt, d, z, &job); |
|
1223 retval = ComplexDET (d); |
|
1224 } |
|
1225 |
|
1226 delete [] tmp_data; |
|
1227 delete [] ipvt; |
|
1228 delete [] z; |
|
1229 } |
|
1230 |
|
1231 return retval; |
|
1232 } |
|
1233 |
|
1234 ComplexMatrix |
|
1235 ComplexMatrix::solve (const Matrix& b) const |
|
1236 { |
|
1237 int info; |
|
1238 double rcond; |
|
1239 return solve (b, info, rcond); |
|
1240 } |
|
1241 |
|
1242 ComplexMatrix |
|
1243 ComplexMatrix::solve (const Matrix& b, int& info) const |
|
1244 { |
|
1245 double rcond; |
|
1246 return solve (b, info, rcond); |
|
1247 } |
|
1248 |
|
1249 ComplexMatrix |
|
1250 ComplexMatrix::solve (const Matrix& b, int& info, double& rcond) const |
|
1251 { |
|
1252 ComplexMatrix tmp (b); |
|
1253 return solve (tmp, info, rcond); |
|
1254 } |
|
1255 |
|
1256 ComplexMatrix |
|
1257 ComplexMatrix::solve (const ComplexMatrix& b) const |
|
1258 { |
|
1259 int info; |
|
1260 double rcond; |
|
1261 return solve (b, info, rcond); |
|
1262 } |
|
1263 |
|
1264 ComplexMatrix |
|
1265 ComplexMatrix::solve (const ComplexMatrix& b, int& info) const |
|
1266 { |
|
1267 double rcond; |
|
1268 return solve (b, info, rcond); |
|
1269 } |
|
1270 ComplexMatrix |
532
|
1271 ComplexMatrix::solve (const ComplexMatrix& b, int& info, double& rcond) const |
458
|
1272 { |
|
1273 ComplexMatrix retval; |
|
1274 |
|
1275 int nr = rows (); |
|
1276 int nc = cols (); |
|
1277 int b_nr = b.rows (); |
|
1278 int b_nc = b.cols (); |
|
1279 if (nr == 0 || nc == 0 || nr != nc || nr != b_nr) |
|
1280 { |
|
1281 (*current_liboctave_error_handler) |
|
1282 ("matrix dimension mismatch in solution of linear equations"); |
|
1283 return ComplexMatrix (); |
|
1284 } |
|
1285 |
|
1286 info = 0; |
|
1287 int *ipvt = new int [nr]; |
|
1288 |
|
1289 Complex *z = new Complex [nr]; |
|
1290 Complex *tmp_data = dup (data (), length ()); |
|
1291 |
|
1292 F77_FCN (zgeco) (tmp_data, &nr, &nr, ipvt, &rcond, z); |
|
1293 |
532
|
1294 volatile double tmp_rcond = rcond; |
|
1295 if (tmp_rcond + 1.0 == 1.0) |
458
|
1296 { |
|
1297 info = -2; |
|
1298 } |
|
1299 else |
|
1300 { |
|
1301 int job = 0; |
|
1302 |
|
1303 Complex *result = dup (b.data (), b.length ()); |
|
1304 |
|
1305 for (int j = 0; j < b_nc; j++) |
|
1306 F77_FCN (zgesl) (tmp_data, &nr, &nr, ipvt, &result[nr*j], &job); |
|
1307 |
|
1308 retval = ComplexMatrix (result, b_nr, b_nc); |
|
1309 } |
|
1310 |
|
1311 delete [] tmp_data; |
|
1312 delete [] ipvt; |
|
1313 delete [] z; |
|
1314 |
|
1315 return retval; |
|
1316 } |
|
1317 |
|
1318 ComplexColumnVector |
|
1319 ComplexMatrix::solve (const ComplexColumnVector& b) const |
|
1320 { |
|
1321 int info; |
|
1322 double rcond; |
|
1323 return solve (b, info, rcond); |
|
1324 } |
|
1325 |
|
1326 ComplexColumnVector |
|
1327 ComplexMatrix::solve (const ComplexColumnVector& b, int& info) const |
|
1328 { |
|
1329 double rcond; |
|
1330 return solve (b, info, rcond); |
|
1331 } |
|
1332 |
|
1333 ComplexColumnVector |
|
1334 ComplexMatrix::solve (const ComplexColumnVector& b, int& info, |
532
|
1335 double& rcond) const |
458
|
1336 { |
|
1337 ComplexColumnVector retval; |
|
1338 |
|
1339 int nr = rows (); |
|
1340 int nc = cols (); |
|
1341 int b_len = b.length (); |
|
1342 if (nr == 0 || nc == 0 || nr != nc || nr != b_len) |
|
1343 { |
|
1344 (*current_liboctave_error_handler) |
|
1345 ("matrix dimension mismatch in solution of linear equations"); |
|
1346 return ComplexColumnVector (); |
|
1347 } |
|
1348 |
|
1349 info = 0; |
|
1350 int *ipvt = new int [nr]; |
|
1351 |
|
1352 Complex *z = new Complex [nr]; |
|
1353 Complex *tmp_data = dup (data (), length ()); |
|
1354 |
|
1355 F77_FCN (zgeco) (tmp_data, &nr, &nr, ipvt, &rcond, z); |
|
1356 |
532
|
1357 volatile double tmp_rcond = rcond; |
|
1358 if (tmp_rcond + 1.0 == 1.0) |
458
|
1359 { |
|
1360 info = -2; |
|
1361 } |
|
1362 else |
|
1363 { |
|
1364 int job = 0; |
|
1365 |
|
1366 Complex *result = dup (b.data (), b_len); |
|
1367 |
|
1368 F77_FCN (zgesl) (tmp_data, &nr, &nr, ipvt, result, &job); |
|
1369 |
|
1370 retval = ComplexColumnVector (result, b_len); |
|
1371 } |
|
1372 |
|
1373 delete [] tmp_data; |
|
1374 delete [] ipvt; |
|
1375 delete [] z; |
|
1376 |
|
1377 return retval; |
|
1378 } |
|
1379 |
|
1380 ComplexMatrix |
|
1381 ComplexMatrix::lssolve (const ComplexMatrix& b) const |
|
1382 { |
|
1383 int info; |
|
1384 int rank; |
|
1385 return lssolve (b, info, rank); |
|
1386 } |
|
1387 |
|
1388 ComplexMatrix |
|
1389 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info) const |
|
1390 { |
|
1391 int rank; |
|
1392 return lssolve (b, info, rank); |
|
1393 } |
|
1394 |
|
1395 ComplexMatrix |
|
1396 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info, int& rank) const |
|
1397 { |
|
1398 int nrhs = b.cols (); |
|
1399 |
|
1400 int m = rows (); |
|
1401 int n = cols (); |
|
1402 |
|
1403 if (m == 0 || n == 0 || m != b.rows ()) |
|
1404 { |
|
1405 (*current_liboctave_error_handler) |
|
1406 ("matrix dimension mismatch solution of linear equations"); |
|
1407 return Matrix (); |
|
1408 } |
|
1409 |
|
1410 Complex *tmp_data = dup (data (), length ()); |
|
1411 |
|
1412 int nrr = m > n ? m : n; |
|
1413 ComplexMatrix result (nrr, nrhs); |
|
1414 |
|
1415 int i, j; |
|
1416 for (j = 0; j < nrhs; j++) |
|
1417 for (i = 0; i < m; i++) |
|
1418 result.elem (i, j) = b.elem (i, j); |
|
1419 |
|
1420 Complex *presult = result.fortran_vec (); |
|
1421 |
|
1422 int len_s = m < n ? m : n; |
|
1423 double *s = new double [len_s]; |
|
1424 double rcond = -1.0; |
|
1425 int lwork; |
|
1426 if (m < n) |
|
1427 lwork = 2*m + (nrhs > n ? nrhs : n); |
|
1428 else |
|
1429 lwork = 2*n + (nrhs > m ? nrhs : m); |
|
1430 |
|
1431 Complex *work = new Complex [lwork]; |
|
1432 |
|
1433 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1434 lrwork = lrwork > 1 ? lrwork : 1; |
|
1435 double *rwork = new double [lrwork]; |
|
1436 |
|
1437 F77_FCN (zgelss) (&m, &n, &nrhs, tmp_data, &m, presult, &nrr, s, |
|
1438 &rcond, &rank, work, &lwork, rwork, &info); |
|
1439 |
|
1440 ComplexMatrix retval (n, nrhs); |
|
1441 for (j = 0; j < nrhs; j++) |
|
1442 for (i = 0; i < n; i++) |
|
1443 retval.elem (i, j) = result.elem (i, j); |
|
1444 |
|
1445 delete [] tmp_data; |
|
1446 delete [] s; |
|
1447 delete [] work; |
|
1448 delete [] rwork; |
|
1449 |
|
1450 return retval; |
|
1451 } |
|
1452 |
|
1453 ComplexColumnVector |
|
1454 ComplexMatrix::lssolve (const ComplexColumnVector& b) const |
|
1455 { |
|
1456 int info; |
|
1457 int rank; |
|
1458 return lssolve (b, info, rank); |
|
1459 } |
|
1460 |
|
1461 ComplexColumnVector |
|
1462 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info) const |
|
1463 { |
|
1464 int rank; |
|
1465 return lssolve (b, info, rank); |
|
1466 } |
|
1467 |
|
1468 ComplexColumnVector |
|
1469 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info, |
|
1470 int& rank) const |
|
1471 { |
|
1472 int nrhs = 1; |
|
1473 |
|
1474 int m = rows (); |
|
1475 int n = cols (); |
|
1476 |
|
1477 if (m == 0 || n == 0 || m != b.length ()) |
|
1478 { |
|
1479 (*current_liboctave_error_handler) |
|
1480 ("matrix dimension mismatch solution of least squares problem"); |
|
1481 return ComplexColumnVector (); |
|
1482 } |
|
1483 |
|
1484 Complex *tmp_data = dup (data (), length ()); |
|
1485 |
|
1486 int nrr = m > n ? m : n; |
|
1487 ComplexColumnVector result (nrr); |
|
1488 |
|
1489 int i; |
|
1490 for (i = 0; i < m; i++) |
|
1491 result.elem (i) = b.elem (i); |
|
1492 |
|
1493 Complex *presult = result.fortran_vec (); |
|
1494 |
|
1495 int len_s = m < n ? m : n; |
|
1496 double *s = new double [len_s]; |
|
1497 double rcond = -1.0; |
|
1498 int lwork; |
|
1499 if (m < n) |
|
1500 lwork = 2*m + (nrhs > n ? nrhs : n); |
|
1501 else |
|
1502 lwork = 2*n + (nrhs > m ? nrhs : m); |
|
1503 |
|
1504 Complex *work = new Complex [lwork]; |
|
1505 |
|
1506 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1507 lrwork = lrwork > 1 ? lrwork : 1; |
|
1508 double *rwork = new double [lrwork]; |
|
1509 |
|
1510 F77_FCN (zgelss) (&m, &n, &nrhs, tmp_data, &m, presult, &nrr, s, |
|
1511 &rcond, &rank, work, &lwork, rwork, &info); |
|
1512 |
|
1513 ComplexColumnVector retval (n); |
|
1514 for (i = 0; i < n; i++) |
|
1515 retval.elem (i) = result.elem (i); |
|
1516 |
|
1517 delete [] tmp_data; |
|
1518 delete [] s; |
|
1519 delete [] work; |
|
1520 delete [] rwork; |
|
1521 |
|
1522 return retval; |
|
1523 } |
|
1524 |
|
1525 // matrix by diagonal matrix -> matrix operations |
|
1526 |
|
1527 ComplexMatrix& |
|
1528 ComplexMatrix::operator += (const DiagMatrix& a) |
|
1529 { |
|
1530 int nr = rows (); |
|
1531 int nc = cols (); |
|
1532 if (nr != a.rows () || nc != a.cols ()) |
|
1533 { |
|
1534 (*current_liboctave_error_handler) |
|
1535 ("nonconformant matrix += operation attempted"); |
|
1536 return ComplexMatrix (); |
|
1537 } |
|
1538 |
|
1539 for (int i = 0; i < a.length (); i++) |
|
1540 elem (i, i) += a.elem (i, i); |
|
1541 |
|
1542 return *this; |
|
1543 } |
|
1544 |
|
1545 ComplexMatrix& |
|
1546 ComplexMatrix::operator -= (const DiagMatrix& a) |
|
1547 { |
|
1548 int nr = rows (); |
|
1549 int nc = cols (); |
|
1550 if (nr != a.rows () || nc != a.cols ()) |
|
1551 { |
|
1552 (*current_liboctave_error_handler) |
|
1553 ("nonconformant matrix -= operation attempted"); |
|
1554 return ComplexMatrix (); |
|
1555 } |
|
1556 |
|
1557 for (int i = 0; i < a.length (); i++) |
|
1558 elem (i, i) -= a.elem (i, i); |
|
1559 |
|
1560 return *this; |
|
1561 } |
|
1562 |
|
1563 ComplexMatrix& |
|
1564 ComplexMatrix::operator += (const ComplexDiagMatrix& a) |
|
1565 { |
|
1566 int nr = rows (); |
|
1567 int nc = cols (); |
|
1568 if (nr != a.rows () || nc != a.cols ()) |
|
1569 { |
|
1570 (*current_liboctave_error_handler) |
|
1571 ("nonconformant matrix += operation attempted"); |
|
1572 return ComplexMatrix (); |
|
1573 } |
|
1574 |
|
1575 for (int i = 0; i < a.length (); i++) |
|
1576 elem (i, i) += a.elem (i, i); |
|
1577 |
|
1578 return *this; |
|
1579 } |
|
1580 |
|
1581 ComplexMatrix& |
|
1582 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) |
|
1583 { |
|
1584 int nr = rows (); |
|
1585 int nc = cols (); |
|
1586 if (nr != a.rows () || nc != a.cols ()) |
|
1587 { |
|
1588 (*current_liboctave_error_handler) |
|
1589 ("nonconformant matrix -= operation attempted"); |
|
1590 return ComplexMatrix (); |
|
1591 } |
|
1592 |
|
1593 for (int i = 0; i < a.length (); i++) |
|
1594 elem (i, i) -= a.elem (i, i); |
|
1595 |
|
1596 return *this; |
|
1597 } |
|
1598 |
|
1599 // matrix by matrix -> matrix operations |
|
1600 |
|
1601 ComplexMatrix& |
|
1602 ComplexMatrix::operator += (const Matrix& a) |
|
1603 { |
|
1604 int nr = rows (); |
|
1605 int nc = cols (); |
|
1606 if (nr != a.rows () || nc != a.cols ()) |
|
1607 { |
|
1608 (*current_liboctave_error_handler) |
|
1609 ("nonconformant matrix += operation attempted"); |
|
1610 return *this; |
|
1611 } |
|
1612 |
|
1613 if (nr == 0 || nc == 0) |
|
1614 return *this; |
|
1615 |
|
1616 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1617 |
|
1618 add2 (d, a.data (), length ()); |
|
1619 return *this; |
|
1620 } |
|
1621 |
|
1622 ComplexMatrix& |
|
1623 ComplexMatrix::operator -= (const Matrix& a) |
|
1624 { |
|
1625 int nr = rows (); |
|
1626 int nc = cols (); |
|
1627 if (nr != a.rows () || nc != a.cols ()) |
|
1628 { |
|
1629 (*current_liboctave_error_handler) |
|
1630 ("nonconformant matrix -= operation attempted"); |
|
1631 return *this; |
|
1632 } |
|
1633 |
|
1634 if (nr == 0 || nc == 0) |
|
1635 return *this; |
|
1636 |
|
1637 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1638 |
|
1639 subtract2 (d, a.data (), length ()); |
|
1640 return *this; |
|
1641 } |
|
1642 |
|
1643 ComplexMatrix& |
|
1644 ComplexMatrix::operator += (const ComplexMatrix& a) |
|
1645 { |
|
1646 int nr = rows (); |
|
1647 int nc = cols (); |
|
1648 if (nr != a.rows () || nc != a.cols ()) |
|
1649 { |
|
1650 (*current_liboctave_error_handler) |
|
1651 ("nonconformant matrix += operation attempted"); |
|
1652 return *this; |
|
1653 } |
|
1654 |
|
1655 if (nr == 0 || nc == 0) |
|
1656 return *this; |
|
1657 |
|
1658 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1659 |
|
1660 add2 (d, a.data (), length ()); |
|
1661 return *this; |
|
1662 } |
|
1663 |
|
1664 ComplexMatrix& |
|
1665 ComplexMatrix::operator -= (const ComplexMatrix& a) |
|
1666 { |
|
1667 int nr = rows (); |
|
1668 int nc = cols (); |
|
1669 if (nr != a.rows () || nc != a.cols ()) |
|
1670 { |
|
1671 (*current_liboctave_error_handler) |
|
1672 ("nonconformant matrix -= operation attempted"); |
|
1673 return *this; |
|
1674 } |
|
1675 |
|
1676 if (nr == 0 || nc == 0) |
|
1677 return *this; |
|
1678 |
|
1679 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1680 |
|
1681 subtract2 (d, a.data (), length ()); |
|
1682 return *this; |
|
1683 } |
|
1684 |
|
1685 // unary operations |
|
1686 |
|
1687 Matrix |
|
1688 ComplexMatrix::operator ! (void) const |
|
1689 { |
|
1690 return Matrix (not (data (), length ()), rows (), cols ()); |
|
1691 } |
|
1692 |
|
1693 // matrix by scalar -> matrix operations |
|
1694 |
|
1695 ComplexMatrix |
|
1696 operator + (const ComplexMatrix& a, double s) |
|
1697 { |
|
1698 return ComplexMatrix (add (a.data (), a.length (), s), |
|
1699 a.rows (), a.cols ()); |
|
1700 } |
|
1701 |
|
1702 ComplexMatrix |
|
1703 operator - (const ComplexMatrix& a, double s) |
|
1704 { |
|
1705 return ComplexMatrix (subtract (a.data (), a.length (), s), |
|
1706 a.rows (), a.cols ()); |
|
1707 } |
|
1708 |
|
1709 ComplexMatrix |
|
1710 operator * (const ComplexMatrix& a, double s) |
|
1711 { |
|
1712 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
1713 a.rows (), a.cols ()); |
|
1714 } |
|
1715 |
|
1716 ComplexMatrix |
|
1717 operator / (const ComplexMatrix& a, double s) |
|
1718 { |
|
1719 return ComplexMatrix (divide (a.data (), a.length (), s), |
|
1720 a.rows (), a.cols ()); |
|
1721 } |
|
1722 |
|
1723 // scalar by matrix -> matrix operations |
|
1724 |
|
1725 ComplexMatrix |
|
1726 operator + (double s, const ComplexMatrix& a) |
|
1727 { |
|
1728 return ComplexMatrix (add (a.data (), a.length (), s), a.rows (), |
|
1729 a.cols ()); |
|
1730 } |
|
1731 |
|
1732 ComplexMatrix |
|
1733 operator - (double s, const ComplexMatrix& a) |
|
1734 { |
|
1735 return ComplexMatrix (subtract (s, a.data (), a.length ()), |
|
1736 a.rows (), a.cols ()); |
|
1737 } |
|
1738 |
|
1739 ComplexMatrix |
|
1740 operator * (double s, const ComplexMatrix& a) |
|
1741 { |
|
1742 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
1743 a.rows (), a.cols ()); |
|
1744 } |
|
1745 |
|
1746 ComplexMatrix |
|
1747 operator / (double s, const ComplexMatrix& a) |
|
1748 { |
|
1749 return ComplexMatrix (divide (s, a.data (), a.length ()), |
|
1750 a.rows (), a.cols ()); |
|
1751 } |
|
1752 |
|
1753 // matrix by column vector -> column vector operations |
|
1754 |
|
1755 ComplexColumnVector |
|
1756 operator * (const ComplexMatrix& m, const ColumnVector& a) |
|
1757 { |
|
1758 ComplexColumnVector tmp (a); |
|
1759 return m * tmp; |
|
1760 } |
|
1761 |
|
1762 ComplexColumnVector |
|
1763 operator * (const ComplexMatrix& m, const ComplexColumnVector& a) |
|
1764 { |
|
1765 int nr = m.rows (); |
|
1766 int nc = m.cols (); |
|
1767 if (nc != a.length ()) |
|
1768 { |
|
1769 (*current_liboctave_error_handler) |
|
1770 ("nonconformant matrix multiplication attempted"); |
|
1771 return ComplexColumnVector (); |
|
1772 } |
|
1773 |
|
1774 if (nc == 0 || nr == 0) |
|
1775 return ComplexColumnVector (0); |
|
1776 |
|
1777 char trans = 'N'; |
|
1778 int ld = nr; |
|
1779 Complex alpha (1.0); |
|
1780 Complex beta (0.0); |
|
1781 int i_one = 1; |
|
1782 |
|
1783 Complex *y = new Complex [nr]; |
|
1784 |
|
1785 F77_FCN (zgemv) (&trans, &nr, &nc, &alpha, m.data (), &ld, a.data (), |
|
1786 &i_one, &beta, y, &i_one, 1L); |
|
1787 |
|
1788 return ComplexColumnVector (y, nr); |
|
1789 } |
|
1790 |
|
1791 // matrix by diagonal matrix -> matrix operations |
|
1792 |
|
1793 ComplexMatrix |
|
1794 operator + (const ComplexMatrix& m, const DiagMatrix& a) |
|
1795 { |
|
1796 int nr = m.rows (); |
|
1797 int nc = m.cols (); |
|
1798 if (nr != a.rows () || nc != a.cols ()) |
|
1799 { |
|
1800 (*current_liboctave_error_handler) |
|
1801 ("nonconformant matrix addition attempted"); |
|
1802 return ComplexMatrix (); |
|
1803 } |
|
1804 |
|
1805 if (nr == 0 || nc == 0) |
|
1806 return ComplexMatrix (nr, nc); |
|
1807 |
|
1808 ComplexMatrix result (m); |
|
1809 for (int i = 0; i < a.length (); i++) |
|
1810 result.elem (i, i) += a.elem (i, i); |
|
1811 |
|
1812 return result; |
|
1813 } |
|
1814 |
|
1815 ComplexMatrix |
|
1816 operator - (const ComplexMatrix& m, const DiagMatrix& a) |
|
1817 { |
|
1818 int nr = m.rows (); |
|
1819 int nc = m.cols (); |
|
1820 if (nr != a.rows () || nc != a.cols ()) |
|
1821 { |
|
1822 (*current_liboctave_error_handler) |
|
1823 ("nonconformant matrix subtraction attempted"); |
|
1824 return ComplexMatrix (); |
|
1825 } |
|
1826 |
|
1827 if (nr == 0 || nc == 0) |
|
1828 return ComplexMatrix (nr, nc); |
|
1829 |
|
1830 ComplexMatrix result (m); |
|
1831 for (int i = 0; i < a.length (); i++) |
|
1832 result.elem (i, i) -= a.elem (i, i); |
|
1833 |
|
1834 return result; |
|
1835 } |
|
1836 |
|
1837 ComplexMatrix |
|
1838 operator * (const ComplexMatrix& m, const DiagMatrix& a) |
|
1839 { |
|
1840 int nr = m.rows (); |
|
1841 int nc = m.cols (); |
|
1842 int a_nc = a.cols (); |
|
1843 if (nc != a.rows ()) |
|
1844 { |
|
1845 (*current_liboctave_error_handler) |
|
1846 ("nonconformant matrix multiplication attempted"); |
|
1847 return ComplexMatrix (); |
|
1848 } |
|
1849 |
|
1850 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1851 return ComplexMatrix (nr, nc, 0.0); |
|
1852 |
|
1853 Complex *c = new Complex [nr*a_nc]; |
533
|
1854 Complex *ctmp = 0; |
458
|
1855 |
|
1856 for (int j = 0; j < a.length (); j++) |
|
1857 { |
|
1858 int idx = j * nr; |
|
1859 ctmp = c + idx; |
|
1860 if (a.elem (j, j) == 1.0) |
|
1861 { |
|
1862 for (int i = 0; i < nr; i++) |
|
1863 ctmp[i] = m.elem (i, j); |
|
1864 } |
|
1865 else if (a.elem (j, j) == 0.0) |
|
1866 { |
|
1867 for (int i = 0; i < nr; i++) |
|
1868 ctmp[i] = 0.0; |
|
1869 } |
|
1870 else |
|
1871 { |
|
1872 for (int i = 0; i < nr; i++) |
|
1873 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
1874 } |
|
1875 } |
|
1876 |
|
1877 if (a.rows () < a_nc) |
|
1878 { |
|
1879 for (int i = nr * nc; i < nr * a_nc; i++) |
|
1880 ctmp[i] = 0.0; |
|
1881 } |
|
1882 |
|
1883 return ComplexMatrix (c, nr, a_nc); |
|
1884 } |
|
1885 |
|
1886 ComplexMatrix |
|
1887 operator + (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
1888 { |
|
1889 int nr = m.rows (); |
|
1890 int nc = m.cols (); |
|
1891 if (nr != a.rows () || nc != a.cols ()) |
|
1892 { |
|
1893 (*current_liboctave_error_handler) |
|
1894 ("nonconformant matrix addition attempted"); |
|
1895 return ComplexMatrix (); |
|
1896 } |
|
1897 |
|
1898 if (nr == 0 || nc == 0) |
|
1899 return ComplexMatrix (nr, nc); |
|
1900 |
|
1901 ComplexMatrix result (m); |
|
1902 for (int i = 0; i < a.length (); i++) |
|
1903 result.elem (i, i) += a.elem (i, i); |
|
1904 |
|
1905 return result; |
|
1906 } |
|
1907 |
|
1908 ComplexMatrix |
|
1909 operator - (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
1910 { |
|
1911 int nr = m.rows (); |
|
1912 int nc = m.cols (); |
|
1913 if (nr != a.rows () || nc != a.cols ()) |
|
1914 { |
|
1915 (*current_liboctave_error_handler) |
|
1916 ("nonconformant matrix subtraction attempted"); |
|
1917 return ComplexMatrix (); |
|
1918 } |
|
1919 |
|
1920 if (nr == 0 || nc == 0) |
|
1921 return ComplexMatrix (nr, nc); |
|
1922 |
|
1923 ComplexMatrix result (m); |
|
1924 for (int i = 0; i < a.length (); i++) |
|
1925 result.elem (i, i) -= a.elem (i, i); |
|
1926 |
|
1927 return result; |
|
1928 } |
|
1929 |
|
1930 ComplexMatrix |
|
1931 operator * (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
1932 { |
|
1933 int nr = m.rows (); |
|
1934 int nc = m.cols (); |
|
1935 int a_nc = a.cols (); |
|
1936 if (nc != a.rows ()) |
|
1937 { |
|
1938 (*current_liboctave_error_handler) |
|
1939 ("nonconformant matrix multiplication attempted"); |
|
1940 return ComplexMatrix (); |
|
1941 } |
|
1942 |
|
1943 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1944 return ComplexMatrix (nr, nc, 0.0); |
|
1945 |
|
1946 Complex *c = new Complex [nr*a_nc]; |
533
|
1947 Complex *ctmp = 0; |
458
|
1948 |
|
1949 for (int j = 0; j < a.length (); j++) |
|
1950 { |
|
1951 int idx = j * nr; |
|
1952 ctmp = c + idx; |
|
1953 if (a.elem (j, j) == 1.0) |
|
1954 { |
|
1955 for (int i = 0; i < nr; i++) |
|
1956 ctmp[i] = m.elem (i, j); |
|
1957 } |
|
1958 else if (a.elem (j, j) == 0.0) |
|
1959 { |
|
1960 for (int i = 0; i < nr; i++) |
|
1961 ctmp[i] = 0.0; |
|
1962 } |
|
1963 else |
|
1964 { |
|
1965 for (int i = 0; i < nr; i++) |
|
1966 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
1967 } |
|
1968 } |
|
1969 |
|
1970 if (a.rows () < a_nc) |
|
1971 { |
|
1972 for (int i = nr * nc; i < nr * a_nc; i++) |
|
1973 ctmp[i] = 0.0; |
|
1974 } |
|
1975 |
|
1976 return ComplexMatrix (c, nr, a_nc); |
|
1977 } |
|
1978 |
|
1979 // matrix by matrix -> matrix operations |
|
1980 |
|
1981 ComplexMatrix |
|
1982 operator + (const ComplexMatrix& m, const Matrix& a) |
|
1983 { |
|
1984 int nr = m.rows (); |
|
1985 int nc = m.cols (); |
|
1986 if (nr != a.rows () || nc != a.cols ()) |
|
1987 { |
|
1988 (*current_liboctave_error_handler) |
|
1989 ("nonconformant matrix addition attempted"); |
|
1990 return ComplexMatrix (); |
|
1991 } |
|
1992 |
|
1993 if (nr == 0 || nc == 0) |
|
1994 return ComplexMatrix (nr, nc); |
|
1995 |
|
1996 return ComplexMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
1997 } |
|
1998 |
|
1999 ComplexMatrix |
|
2000 operator - (const ComplexMatrix& m, const Matrix& a) |
|
2001 { |
|
2002 int nr = m.rows (); |
|
2003 int nc = m.cols (); |
|
2004 if (nr != a.rows () || nc != a.cols ()) |
|
2005 { |
|
2006 (*current_liboctave_error_handler) |
|
2007 ("nonconformant matrix subtraction attempted"); |
|
2008 return ComplexMatrix (); |
|
2009 } |
|
2010 |
|
2011 if (nr == 0 || nc == 0) |
|
2012 return ComplexMatrix (nr, nc); |
|
2013 |
|
2014 return ComplexMatrix (subtract (m.data (), a.data (), m.length ()), nr, nc); |
|
2015 } |
|
2016 |
|
2017 ComplexMatrix |
|
2018 operator * (const ComplexMatrix& m, const Matrix& a) |
|
2019 { |
|
2020 ComplexMatrix tmp (a); |
|
2021 return m * tmp; |
|
2022 } |
|
2023 |
|
2024 ComplexMatrix |
|
2025 operator * (const ComplexMatrix& m, const ComplexMatrix& a) |
|
2026 { |
|
2027 int nr = m.rows (); |
|
2028 int nc = m.cols (); |
|
2029 int a_nc = a.cols (); |
|
2030 if (nc != a.rows ()) |
|
2031 { |
|
2032 (*current_liboctave_error_handler) |
|
2033 ("nonconformant matrix multiplication attempted"); |
|
2034 return ComplexMatrix (); |
|
2035 } |
|
2036 |
|
2037 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2038 return ComplexMatrix (nr, nc, 0.0); |
|
2039 |
|
2040 char trans = 'N'; |
|
2041 char transa = 'N'; |
|
2042 |
|
2043 int ld = nr; |
|
2044 int lda = a.rows (); |
|
2045 |
|
2046 Complex alpha (1.0); |
|
2047 Complex beta (0.0); |
|
2048 |
|
2049 Complex *c = new Complex [nr*a_nc]; |
|
2050 |
|
2051 F77_FCN (zgemm) (&trans, &transa, &nr, &a_nc, &nc, &alpha, m.data (), |
|
2052 &ld, a.data (), &lda, &beta, c, &nr, 1L, 1L); |
|
2053 |
|
2054 return ComplexMatrix (c, nr, a_nc); |
|
2055 } |
|
2056 |
|
2057 ComplexMatrix |
|
2058 product (const ComplexMatrix& m, const Matrix& a) |
|
2059 { |
|
2060 int nr = m.rows (); |
|
2061 int nc = m.cols (); |
|
2062 if (nr != a.rows () || nc != a.cols ()) |
|
2063 { |
|
2064 (*current_liboctave_error_handler) |
|
2065 ("nonconformant matrix product attempted"); |
|
2066 return ComplexMatrix (); |
|
2067 } |
|
2068 |
|
2069 if (nr == 0 || nc == 0) |
|
2070 return ComplexMatrix (nr, nc); |
|
2071 |
|
2072 return ComplexMatrix (multiply (m.data (), a.data (), m.length ()), nr, nc); |
|
2073 } |
|
2074 |
|
2075 ComplexMatrix |
|
2076 quotient (const ComplexMatrix& m, const Matrix& a) |
|
2077 { |
|
2078 int nr = m.rows (); |
|
2079 int nc = m.cols (); |
|
2080 if (nr != a.rows () || nc != a.cols ()) |
|
2081 { |
|
2082 (*current_liboctave_error_handler) |
|
2083 ("nonconformant matrix quotient attempted"); |
|
2084 return ComplexMatrix (); |
|
2085 } |
|
2086 |
|
2087 if (nr == 0 || nc == 0) |
|
2088 return ComplexMatrix (nr, nc); |
|
2089 |
|
2090 return ComplexMatrix (divide (m.data (), a.data (), m.length ()), nr, nc); |
|
2091 } |
|
2092 |
|
2093 // other operations |
|
2094 |
|
2095 ComplexMatrix |
|
2096 map (c_c_Mapper f, const ComplexMatrix& a) |
|
2097 { |
|
2098 ComplexMatrix b (a); |
|
2099 b.map (f); |
|
2100 return b; |
|
2101 } |
|
2102 |
|
2103 Matrix |
|
2104 map (d_c_Mapper f, const ComplexMatrix& a) |
|
2105 { |
|
2106 int a_nc = a.cols (); |
|
2107 int a_nr = a.rows (); |
|
2108 Matrix b (a_nr, a_nc); |
|
2109 for (int j = 0; j < a_nc; j++) |
|
2110 for (int i = 0; i < a_nr; i++) |
|
2111 b.elem (i, j) = f (a.elem (i, j)); |
|
2112 return b; |
|
2113 } |
|
2114 |
|
2115 void |
|
2116 ComplexMatrix::map (c_c_Mapper f) |
|
2117 { |
|
2118 for (int j = 0; j < cols (); j++) |
|
2119 for (int i = 0; i < rows (); i++) |
|
2120 elem (i, j) = f (elem (i, j)); |
|
2121 } |
|
2122 |
|
2123 Matrix |
|
2124 ComplexMatrix::all (void) const |
|
2125 { |
|
2126 int nr = rows (); |
|
2127 int nc = cols (); |
|
2128 Matrix retval; |
|
2129 if (nr > 0 && nc > 0) |
|
2130 { |
|
2131 if (nr == 1) |
|
2132 { |
|
2133 retval.resize (1, 1); |
|
2134 retval.elem (0, 0) = 1.0; |
|
2135 for (int j = 0; j < nc; j++) |
|
2136 { |
|
2137 if (elem (0, j) == 0.0) |
|
2138 { |
|
2139 retval.elem (0, 0) = 0.0; |
|
2140 break; |
|
2141 } |
|
2142 } |
|
2143 } |
|
2144 else if (nc == 1) |
|
2145 { |
|
2146 retval.resize (1, 1); |
|
2147 retval.elem (0, 0) = 1.0; |
|
2148 for (int i = 0; i < nr; i++) |
|
2149 { |
|
2150 if (elem (i, 0) == 0.0) |
|
2151 { |
|
2152 retval.elem (0, 0) = 0.0; |
|
2153 break; |
|
2154 } |
|
2155 } |
|
2156 } |
|
2157 else |
|
2158 { |
|
2159 retval.resize (1, nc); |
|
2160 for (int j = 0; j < nc; j++) |
|
2161 { |
|
2162 retval.elem (0, j) = 1.0; |
|
2163 for (int i = 0; i < nr; i++) |
|
2164 { |
|
2165 if (elem (i, j) == 0.0) |
|
2166 { |
|
2167 retval.elem (0, j) = 0.0; |
|
2168 break; |
|
2169 } |
|
2170 } |
|
2171 } |
|
2172 } |
|
2173 } |
|
2174 return retval; |
|
2175 } |
|
2176 |
|
2177 Matrix |
|
2178 ComplexMatrix::any (void) const |
|
2179 { |
|
2180 int nr = rows (); |
|
2181 int nc = cols (); |
|
2182 Matrix retval; |
|
2183 if (nr > 0 && nc > 0) |
|
2184 { |
|
2185 if (nr == 1) |
|
2186 { |
|
2187 retval.resize (1, 1); |
|
2188 retval.elem (0, 0) = 0.0; |
|
2189 for (int j = 0; j < nc; j++) |
|
2190 { |
|
2191 if (elem (0, j) != 0.0) |
|
2192 { |
|
2193 retval.elem (0, 0) = 1.0; |
|
2194 break; |
|
2195 } |
|
2196 } |
|
2197 } |
|
2198 else if (nc == 1) |
|
2199 { |
|
2200 retval.resize (1, 1); |
|
2201 retval.elem (0, 0) = 0.0; |
|
2202 for (int i = 0; i < nr; i++) |
|
2203 { |
|
2204 if (elem (i, 0) != 0.0) |
|
2205 { |
|
2206 retval.elem (0, 0) = 1.0; |
|
2207 break; |
|
2208 } |
|
2209 } |
|
2210 } |
|
2211 else |
|
2212 { |
|
2213 retval.resize (1, nc); |
|
2214 for (int j = 0; j < nc; j++) |
|
2215 { |
|
2216 retval.elem (0, j) = 0.0; |
|
2217 for (int i = 0; i < nr; i++) |
|
2218 { |
|
2219 if (elem (i, j) != 0.0) |
|
2220 { |
|
2221 retval.elem (0, j) = 1.0; |
|
2222 break; |
|
2223 } |
|
2224 } |
|
2225 } |
|
2226 } |
|
2227 } |
|
2228 return retval; |
|
2229 } |
|
2230 |
|
2231 ComplexMatrix |
|
2232 ComplexMatrix::cumprod (void) const |
|
2233 { |
|
2234 int nr = rows (); |
|
2235 int nc = cols (); |
|
2236 ComplexMatrix retval; |
|
2237 if (nr > 0 && nc > 0) |
|
2238 { |
|
2239 if (nr == 1) |
|
2240 { |
|
2241 retval.resize (1, nc); |
|
2242 Complex prod = elem (0, 0); |
|
2243 for (int j = 0; j < nc; j++) |
|
2244 { |
|
2245 retval.elem (0, j) = prod; |
|
2246 if (j < nc - 1) |
|
2247 prod *= elem (0, j+1); |
|
2248 } |
|
2249 } |
|
2250 else if (nc == 1) |
|
2251 { |
|
2252 retval.resize (nr, 1); |
|
2253 Complex prod = elem (0, 0); |
|
2254 for (int i = 0; i < nr; i++) |
|
2255 { |
|
2256 retval.elem (i, 0) = prod; |
|
2257 if (i < nr - 1) |
|
2258 prod *= elem (i+1, 0); |
|
2259 } |
|
2260 } |
|
2261 else |
|
2262 { |
|
2263 retval.resize (nr, nc); |
|
2264 for (int j = 0; j < nc; j++) |
|
2265 { |
|
2266 Complex prod = elem (0, j); |
|
2267 for (int i = 0; i < nr; i++) |
|
2268 { |
|
2269 retval.elem (i, j) = prod; |
|
2270 if (i < nr - 1) |
|
2271 prod *= elem (i+1, j); |
|
2272 } |
|
2273 } |
|
2274 } |
|
2275 } |
|
2276 return retval; |
|
2277 } |
|
2278 |
|
2279 ComplexMatrix |
|
2280 ComplexMatrix::cumsum (void) const |
|
2281 { |
|
2282 int nr = rows (); |
|
2283 int nc = cols (); |
|
2284 ComplexMatrix retval; |
|
2285 if (nr > 0 && nc > 0) |
|
2286 { |
|
2287 if (nr == 1) |
|
2288 { |
|
2289 retval.resize (1, nc); |
|
2290 Complex sum = elem (0, 0); |
|
2291 for (int j = 0; j < nc; j++) |
|
2292 { |
|
2293 retval.elem (0, j) = sum; |
|
2294 if (j < nc - 1) |
|
2295 sum += elem (0, j+1); |
|
2296 } |
|
2297 } |
|
2298 else if (nc == 1) |
|
2299 { |
|
2300 retval.resize (nr, 1); |
|
2301 Complex sum = elem (0, 0); |
|
2302 for (int i = 0; i < nr; i++) |
|
2303 { |
|
2304 retval.elem (i, 0) = sum; |
|
2305 if (i < nr - 1) |
|
2306 sum += elem (i+1, 0); |
|
2307 } |
|
2308 } |
|
2309 else |
|
2310 { |
|
2311 retval.resize (nr, nc); |
|
2312 for (int j = 0; j < nc; j++) |
|
2313 { |
|
2314 Complex sum = elem (0, j); |
|
2315 for (int i = 0; i < nr; i++) |
|
2316 { |
|
2317 retval.elem (i, j) = sum; |
|
2318 if (i < nr - 1) |
|
2319 sum += elem (i+1, j); |
|
2320 } |
|
2321 } |
|
2322 } |
|
2323 } |
|
2324 return retval; |
|
2325 } |
|
2326 |
|
2327 ComplexMatrix |
|
2328 ComplexMatrix::prod (void) const |
|
2329 { |
|
2330 int nr = rows (); |
|
2331 int nc = cols (); |
|
2332 ComplexMatrix retval; |
|
2333 if (nr > 0 && nc > 0) |
|
2334 { |
|
2335 if (nr == 1) |
|
2336 { |
|
2337 retval.resize (1, 1); |
|
2338 retval.elem (0, 0) = 1.0; |
|
2339 for (int j = 0; j < nc; j++) |
|
2340 retval.elem (0, 0) *= elem (0, j); |
|
2341 } |
|
2342 else if (nc == 1) |
|
2343 { |
|
2344 retval.resize (1, 1); |
|
2345 retval.elem (0, 0) = 1.0; |
|
2346 for (int i = 0; i < nr; i++) |
|
2347 retval.elem (0, 0) *= elem (i, 0); |
|
2348 } |
|
2349 else |
|
2350 { |
|
2351 retval.resize (1, nc); |
|
2352 for (int j = 0; j < nc; j++) |
|
2353 { |
|
2354 retval.elem (0, j) = 1.0; |
|
2355 for (int i = 0; i < nr; i++) |
|
2356 retval.elem (0, j) *= elem (i, j); |
|
2357 } |
|
2358 } |
|
2359 } |
|
2360 return retval; |
|
2361 } |
|
2362 |
|
2363 ComplexMatrix |
|
2364 ComplexMatrix::sum (void) const |
|
2365 { |
|
2366 int nr = rows (); |
|
2367 int nc = cols (); |
|
2368 ComplexMatrix retval; |
|
2369 if (nr > 0 && nc > 0) |
|
2370 { |
|
2371 if (nr == 1) |
|
2372 { |
|
2373 retval.resize (1, 1); |
|
2374 retval.elem (0, 0) = 0.0; |
|
2375 for (int j = 0; j < nc; j++) |
|
2376 retval.elem (0, 0) += elem (0, j); |
|
2377 } |
|
2378 else if (nc == 1) |
|
2379 { |
|
2380 retval.resize (1, 1); |
|
2381 retval.elem (0, 0) = 0.0; |
|
2382 for (int i = 0; i < nr; i++) |
|
2383 retval.elem (0, 0) += elem (i, 0); |
|
2384 } |
|
2385 else |
|
2386 { |
|
2387 retval.resize (1, nc); |
|
2388 for (int j = 0; j < nc; j++) |
|
2389 { |
|
2390 retval.elem (0, j) = 0.0; |
|
2391 for (int i = 0; i < nr; i++) |
|
2392 retval.elem (0, j) += elem (i, j); |
|
2393 } |
|
2394 } |
|
2395 } |
|
2396 return retval; |
|
2397 } |
|
2398 |
|
2399 ComplexMatrix |
|
2400 ComplexMatrix::sumsq (void) const |
|
2401 { |
|
2402 int nr = rows (); |
|
2403 int nc = cols (); |
|
2404 ComplexMatrix retval; |
|
2405 if (nr > 0 && nc > 0) |
|
2406 { |
|
2407 if (nr == 1) |
|
2408 { |
|
2409 retval.resize (1, 1); |
|
2410 retval.elem (0, 0) = 0.0; |
|
2411 for (int j = 0; j < nc; j++) |
|
2412 { |
|
2413 Complex d = elem (0, j); |
|
2414 retval.elem (0, 0) += d * d; |
|
2415 } |
|
2416 } |
|
2417 else if (nc == 1) |
|
2418 { |
|
2419 retval.resize (1, 1); |
|
2420 retval.elem (0, 0) = 0.0; |
|
2421 for (int i = 0; i < nr; i++) |
|
2422 { |
|
2423 Complex d = elem (i, 0); |
|
2424 retval.elem (0, 0) += d * d; |
|
2425 } |
|
2426 } |
|
2427 else |
|
2428 { |
|
2429 retval.resize (1, nc); |
|
2430 for (int j = 0; j < nc; j++) |
|
2431 { |
|
2432 retval.elem (0, j) = 0.0; |
|
2433 for (int i = 0; i < nr; i++) |
|
2434 { |
|
2435 Complex d = elem (i, j); |
|
2436 retval.elem (0, j) += d * d; |
|
2437 } |
|
2438 } |
|
2439 } |
|
2440 } |
|
2441 return retval; |
|
2442 } |
|
2443 |
|
2444 ComplexColumnVector |
|
2445 ComplexMatrix::diag (void) const |
|
2446 { |
|
2447 return diag (0); |
|
2448 } |
|
2449 |
|
2450 ComplexColumnVector |
|
2451 ComplexMatrix::diag (int k) const |
|
2452 { |
|
2453 int nnr = rows (); |
|
2454 int nnc = cols (); |
|
2455 if (k > 0) |
|
2456 nnc -= k; |
|
2457 else if (k < 0) |
|
2458 nnr += k; |
|
2459 |
|
2460 ComplexColumnVector d; |
|
2461 |
|
2462 if (nnr > 0 && nnc > 0) |
|
2463 { |
|
2464 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
2465 |
|
2466 d.resize (ndiag); |
|
2467 |
|
2468 if (k > 0) |
|
2469 { |
|
2470 for (int i = 0; i < ndiag; i++) |
|
2471 d.elem (i) = elem (i, i+k); |
|
2472 } |
|
2473 else if ( k < 0) |
|
2474 { |
|
2475 for (int i = 0; i < ndiag; i++) |
|
2476 d.elem (i) = elem (i-k, i); |
|
2477 } |
|
2478 else |
|
2479 { |
|
2480 for (int i = 0; i < ndiag; i++) |
|
2481 d.elem (i) = elem (i, i); |
|
2482 } |
|
2483 } |
|
2484 else |
|
2485 cerr << "diag: requested diagonal out of range\n"; |
|
2486 |
|
2487 return d; |
|
2488 } |
|
2489 |
|
2490 ComplexColumnVector |
|
2491 ComplexMatrix::row_min (void) const |
|
2492 { |
|
2493 ComplexColumnVector result; |
|
2494 |
|
2495 int nr = rows (); |
|
2496 int nc = cols (); |
|
2497 if (nr > 0 && nc > 0) |
|
2498 { |
|
2499 result.resize (nr); |
|
2500 |
|
2501 for (int i = 0; i < nr; i++) |
|
2502 { |
|
2503 Complex res = elem (i, 0); |
|
2504 double absres = abs (res); |
|
2505 for (int j = 1; j < nc; j++) |
|
2506 if (abs (elem (i, j)) < absres) |
|
2507 { |
|
2508 res = elem (i, j); |
|
2509 absres = abs (res); |
|
2510 } |
|
2511 result.elem (i) = res; |
|
2512 } |
|
2513 } |
|
2514 |
|
2515 return result; |
|
2516 } |
|
2517 |
|
2518 ComplexColumnVector |
|
2519 ComplexMatrix::row_min_loc (void) const |
|
2520 { |
|
2521 ComplexColumnVector result; |
|
2522 |
|
2523 int nr = rows (); |
|
2524 int nc = cols (); |
|
2525 |
|
2526 if (nr > 0 && nc > 0) |
|
2527 { |
|
2528 result.resize (nr); |
|
2529 |
|
2530 for (int i = 0; i < nr; i++) |
|
2531 { |
|
2532 Complex res = 0; |
|
2533 double absres = abs (elem (i, 0)); |
|
2534 for (int j = 0; j < nc; j++) |
|
2535 if (abs (elem (i, j)) < absres) |
|
2536 { |
|
2537 res = j; |
|
2538 absres = abs (elem (i, j)); |
|
2539 } |
|
2540 result.elem (i) = res + 1; |
|
2541 } |
|
2542 } |
|
2543 |
|
2544 return result; |
|
2545 } |
|
2546 |
|
2547 ComplexColumnVector |
|
2548 ComplexMatrix::row_max (void) const |
|
2549 { |
|
2550 ComplexColumnVector result; |
|
2551 |
|
2552 int nr = rows (); |
|
2553 int nc = cols (); |
|
2554 |
|
2555 if (nr > 0 && nc > 0) |
|
2556 { |
|
2557 result.resize (nr); |
|
2558 |
|
2559 for (int i = 0; i < nr; i++) |
|
2560 { |
|
2561 Complex res = elem (i, 0); |
|
2562 double absres = abs (res); |
|
2563 for (int j = 1; j < nc; j++) |
|
2564 if (abs (elem (i, j)) > absres) |
|
2565 { |
|
2566 res = elem (i, j); |
|
2567 absres = abs (res); |
|
2568 } |
|
2569 result.elem (i) = res; |
|
2570 } |
|
2571 } |
|
2572 |
|
2573 return result; |
|
2574 } |
|
2575 |
|
2576 ComplexColumnVector |
|
2577 ComplexMatrix::row_max_loc (void) const |
|
2578 { |
|
2579 ComplexColumnVector result; |
|
2580 |
|
2581 int nr = rows (); |
|
2582 int nc = cols (); |
|
2583 |
|
2584 if (nr > 0 && nc > 0) |
|
2585 { |
|
2586 result.resize (nr); |
|
2587 |
|
2588 for (int i = 0; i < nr; i++) |
|
2589 { |
|
2590 Complex res = 0; |
|
2591 double absres = abs (elem (i, 0)); |
|
2592 for (int j = 0; j < nc; j++) |
|
2593 if (abs (elem (i, j)) > absres) |
|
2594 { |
|
2595 res = j; |
|
2596 absres = abs (elem (i, j)); |
|
2597 } |
|
2598 result.elem (i) = res + 1; |
|
2599 } |
|
2600 } |
|
2601 |
|
2602 return result; |
|
2603 } |
|
2604 |
|
2605 ComplexRowVector |
|
2606 ComplexMatrix::column_min (void) const |
|
2607 { |
|
2608 ComplexRowVector result; |
|
2609 |
|
2610 int nr = rows (); |
|
2611 int nc = cols (); |
|
2612 |
|
2613 if (nr > 0 && nc > 0) |
|
2614 { |
|
2615 result.resize (nc); |
|
2616 |
|
2617 for (int j = 0; j < nc; j++) |
|
2618 { |
|
2619 Complex res = elem (0, j); |
|
2620 double absres = abs (res); |
|
2621 for (int i = 1; i < nr; i++) |
|
2622 if (abs (elem (i, j)) < absres) |
|
2623 { |
|
2624 res = elem (i, j); |
|
2625 absres = abs (res); |
|
2626 } |
|
2627 result.elem (j) = res; |
|
2628 } |
|
2629 } |
|
2630 |
|
2631 return result; |
|
2632 } |
|
2633 |
|
2634 ComplexRowVector |
|
2635 ComplexMatrix::column_min_loc (void) const |
|
2636 { |
|
2637 ComplexRowVector result; |
|
2638 |
|
2639 int nr = rows (); |
|
2640 int nc = cols (); |
|
2641 |
|
2642 if (nr > 0 && nc > 0) |
|
2643 { |
|
2644 result.resize (nc); |
|
2645 |
|
2646 for (int j = 0; j < nc; j++) |
|
2647 { |
|
2648 Complex res = 0; |
|
2649 double absres = abs (elem (0, j)); |
|
2650 for (int i = 0; i < nr; i++) |
|
2651 if (abs (elem (i, j)) < absres) |
|
2652 { |
|
2653 res = i; |
|
2654 absres = abs (elem (i, j)); |
|
2655 } |
|
2656 result.elem (j) = res + 1; |
|
2657 } |
|
2658 } |
|
2659 |
|
2660 return result; |
|
2661 } |
|
2662 |
|
2663 ComplexRowVector |
|
2664 ComplexMatrix::column_max (void) const |
|
2665 { |
|
2666 ComplexRowVector result; |
|
2667 |
|
2668 int nr = rows (); |
|
2669 int nc = cols (); |
|
2670 |
|
2671 if (nr > 0 && nc > 0) |
|
2672 { |
|
2673 result.resize (nc); |
|
2674 |
|
2675 for (int j = 0; j < nc; j++) |
|
2676 { |
|
2677 Complex res = elem (0, j); |
|
2678 double absres = abs (res); |
|
2679 for (int i = 1; i < nr; i++) |
|
2680 if (abs (elem (i, j)) > absres) |
|
2681 { |
|
2682 res = elem (i, j); |
|
2683 absres = abs (res); |
|
2684 } |
|
2685 result.elem (j) = res; |
|
2686 } |
|
2687 } |
|
2688 |
|
2689 return result; |
|
2690 } |
|
2691 |
|
2692 ComplexRowVector |
|
2693 ComplexMatrix::column_max_loc (void) const |
|
2694 { |
|
2695 ComplexRowVector result; |
|
2696 |
|
2697 int nr = rows (); |
|
2698 int nc = cols (); |
|
2699 |
|
2700 if (nr > 0 && nc > 0) |
|
2701 { |
|
2702 result.resize (nc); |
|
2703 |
|
2704 for (int j = 0; j < nc; j++) |
|
2705 { |
|
2706 Complex res = 0; |
|
2707 double absres = abs (elem (0, j)); |
|
2708 for (int i = 0; i < nr; i++) |
|
2709 if (abs (elem (i, j)) > absres) |
|
2710 { |
|
2711 res = i; |
|
2712 absres = abs (elem (i, j)); |
|
2713 } |
|
2714 result.elem (j) = res + 1; |
|
2715 } |
|
2716 } |
|
2717 |
|
2718 return result; |
|
2719 } |
|
2720 |
|
2721 // i/o |
|
2722 |
|
2723 ostream& |
|
2724 operator << (ostream& os, const ComplexMatrix& a) |
|
2725 { |
|
2726 // int field_width = os.precision () + 7; |
|
2727 for (int i = 0; i < a.rows (); i++) |
|
2728 { |
|
2729 for (int j = 0; j < a.cols (); j++) |
|
2730 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
2731 os << "\n"; |
|
2732 } |
|
2733 return os; |
|
2734 } |
|
2735 |
|
2736 istream& |
|
2737 operator >> (istream& is, ComplexMatrix& a) |
|
2738 { |
|
2739 int nr = a.rows (); |
|
2740 int nc = a.cols (); |
|
2741 |
|
2742 if (nr < 1 || nc < 1) |
|
2743 is.clear (ios::badbit); |
|
2744 else |
|
2745 { |
|
2746 Complex tmp; |
|
2747 for (int i = 0; i < nr; i++) |
|
2748 for (int j = 0; j < nc; j++) |
|
2749 { |
|
2750 is >> tmp; |
|
2751 if (is) |
|
2752 a.elem (i, j) = tmp; |
|
2753 else |
|
2754 break; |
|
2755 } |
|
2756 } |
|
2757 |
|
2758 return is; |
|
2759 } |
|
2760 |
|
2761 /* |
|
2762 ;;; Local Variables: *** |
|
2763 ;;; mode: C++ *** |
|
2764 ;;; page-delimiter: "^/\\*" *** |
|
2765 ;;; End: *** |
|
2766 */ |