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