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 |
|
1024 ComplexDET |
|
1025 ComplexMatrix::determinant (void) const |
|
1026 { |
|
1027 int info; |
|
1028 double rcond; |
|
1029 return determinant (info, rcond); |
|
1030 } |
|
1031 |
|
1032 ComplexDET |
|
1033 ComplexMatrix::determinant (int& info) const |
|
1034 { |
|
1035 double rcond; |
|
1036 return determinant (info, rcond); |
|
1037 } |
|
1038 |
|
1039 ComplexDET |
532
|
1040 ComplexMatrix::determinant (int& info, double& rcond) const |
458
|
1041 { |
|
1042 ComplexDET retval; |
|
1043 |
|
1044 int nr = rows (); |
|
1045 int nc = cols (); |
|
1046 |
|
1047 if (nr == 0 || nc == 0) |
|
1048 { |
|
1049 Complex d[2]; |
|
1050 d[0] = 1.0; |
|
1051 d[1] = 0.0; |
|
1052 retval = ComplexDET (d); |
|
1053 } |
|
1054 else |
|
1055 { |
|
1056 info = 0; |
|
1057 int *ipvt = new int [nr]; |
|
1058 |
|
1059 Complex *z = new Complex [nr]; |
|
1060 Complex *tmp_data = dup (data (), length ()); |
|
1061 |
|
1062 F77_FCN (zgeco) (tmp_data, &nr, &nr, ipvt, &rcond, z); |
|
1063 |
532
|
1064 volatile double tmp_rcond = rcond; |
|
1065 if (tmp_rcond + 1.0 == 1.0) |
458
|
1066 { |
|
1067 info = -1; |
|
1068 retval = ComplexDET (); |
|
1069 } |
|
1070 else |
|
1071 { |
|
1072 int job = 10; |
|
1073 Complex d[2]; |
|
1074 F77_FCN (zgedi) (tmp_data, &nr, &nr, ipvt, d, z, &job); |
|
1075 retval = ComplexDET (d); |
|
1076 } |
|
1077 |
|
1078 delete [] tmp_data; |
|
1079 delete [] ipvt; |
|
1080 delete [] z; |
|
1081 } |
|
1082 |
|
1083 return retval; |
|
1084 } |
|
1085 |
|
1086 ComplexMatrix |
|
1087 ComplexMatrix::solve (const Matrix& b) const |
|
1088 { |
|
1089 int info; |
|
1090 double rcond; |
|
1091 return solve (b, info, rcond); |
|
1092 } |
|
1093 |
|
1094 ComplexMatrix |
|
1095 ComplexMatrix::solve (const Matrix& b, int& info) const |
|
1096 { |
|
1097 double rcond; |
|
1098 return solve (b, info, rcond); |
|
1099 } |
|
1100 |
|
1101 ComplexMatrix |
|
1102 ComplexMatrix::solve (const Matrix& b, int& info, double& rcond) const |
|
1103 { |
|
1104 ComplexMatrix tmp (b); |
|
1105 return solve (tmp, info, rcond); |
|
1106 } |
|
1107 |
|
1108 ComplexMatrix |
|
1109 ComplexMatrix::solve (const ComplexMatrix& b) const |
|
1110 { |
|
1111 int info; |
|
1112 double rcond; |
|
1113 return solve (b, info, rcond); |
|
1114 } |
|
1115 |
|
1116 ComplexMatrix |
|
1117 ComplexMatrix::solve (const ComplexMatrix& b, int& info) const |
|
1118 { |
|
1119 double rcond; |
|
1120 return solve (b, info, rcond); |
|
1121 } |
|
1122 ComplexMatrix |
532
|
1123 ComplexMatrix::solve (const ComplexMatrix& b, int& info, double& rcond) const |
458
|
1124 { |
|
1125 ComplexMatrix retval; |
|
1126 |
|
1127 int nr = rows (); |
|
1128 int nc = cols (); |
|
1129 int b_nr = b.rows (); |
|
1130 int b_nc = b.cols (); |
|
1131 if (nr == 0 || nc == 0 || nr != nc || nr != b_nr) |
|
1132 { |
|
1133 (*current_liboctave_error_handler) |
|
1134 ("matrix dimension mismatch in solution of linear equations"); |
|
1135 return ComplexMatrix (); |
|
1136 } |
|
1137 |
|
1138 info = 0; |
|
1139 int *ipvt = new int [nr]; |
|
1140 |
|
1141 Complex *z = new Complex [nr]; |
|
1142 Complex *tmp_data = dup (data (), length ()); |
|
1143 |
|
1144 F77_FCN (zgeco) (tmp_data, &nr, &nr, ipvt, &rcond, z); |
|
1145 |
532
|
1146 volatile double tmp_rcond = rcond; |
|
1147 if (tmp_rcond + 1.0 == 1.0) |
458
|
1148 { |
|
1149 info = -2; |
|
1150 } |
|
1151 else |
|
1152 { |
|
1153 int job = 0; |
|
1154 |
|
1155 Complex *result = dup (b.data (), b.length ()); |
|
1156 |
|
1157 for (int j = 0; j < b_nc; j++) |
|
1158 F77_FCN (zgesl) (tmp_data, &nr, &nr, ipvt, &result[nr*j], &job); |
|
1159 |
|
1160 retval = ComplexMatrix (result, b_nr, b_nc); |
|
1161 } |
|
1162 |
|
1163 delete [] tmp_data; |
|
1164 delete [] ipvt; |
|
1165 delete [] z; |
|
1166 |
|
1167 return retval; |
|
1168 } |
|
1169 |
|
1170 ComplexColumnVector |
|
1171 ComplexMatrix::solve (const ComplexColumnVector& b) const |
|
1172 { |
|
1173 int info; |
|
1174 double rcond; |
|
1175 return solve (b, info, rcond); |
|
1176 } |
|
1177 |
|
1178 ComplexColumnVector |
|
1179 ComplexMatrix::solve (const ComplexColumnVector& b, int& info) const |
|
1180 { |
|
1181 double rcond; |
|
1182 return solve (b, info, rcond); |
|
1183 } |
|
1184 |
|
1185 ComplexColumnVector |
|
1186 ComplexMatrix::solve (const ComplexColumnVector& b, int& info, |
532
|
1187 double& rcond) const |
458
|
1188 { |
|
1189 ComplexColumnVector retval; |
|
1190 |
|
1191 int nr = rows (); |
|
1192 int nc = cols (); |
|
1193 int b_len = b.length (); |
|
1194 if (nr == 0 || nc == 0 || nr != nc || nr != b_len) |
|
1195 { |
|
1196 (*current_liboctave_error_handler) |
|
1197 ("matrix dimension mismatch in solution of linear equations"); |
|
1198 return ComplexColumnVector (); |
|
1199 } |
|
1200 |
|
1201 info = 0; |
|
1202 int *ipvt = new int [nr]; |
|
1203 |
|
1204 Complex *z = new Complex [nr]; |
|
1205 Complex *tmp_data = dup (data (), length ()); |
|
1206 |
|
1207 F77_FCN (zgeco) (tmp_data, &nr, &nr, ipvt, &rcond, z); |
|
1208 |
532
|
1209 volatile double tmp_rcond = rcond; |
|
1210 if (tmp_rcond + 1.0 == 1.0) |
458
|
1211 { |
|
1212 info = -2; |
|
1213 } |
|
1214 else |
|
1215 { |
|
1216 int job = 0; |
|
1217 |
|
1218 Complex *result = dup (b.data (), b_len); |
|
1219 |
|
1220 F77_FCN (zgesl) (tmp_data, &nr, &nr, ipvt, result, &job); |
|
1221 |
|
1222 retval = ComplexColumnVector (result, b_len); |
|
1223 } |
|
1224 |
|
1225 delete [] tmp_data; |
|
1226 delete [] ipvt; |
|
1227 delete [] z; |
|
1228 |
|
1229 return retval; |
|
1230 } |
|
1231 |
|
1232 ComplexMatrix |
|
1233 ComplexMatrix::lssolve (const ComplexMatrix& b) const |
|
1234 { |
|
1235 int info; |
|
1236 int rank; |
|
1237 return lssolve (b, info, rank); |
|
1238 } |
|
1239 |
|
1240 ComplexMatrix |
|
1241 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info) const |
|
1242 { |
|
1243 int rank; |
|
1244 return lssolve (b, info, rank); |
|
1245 } |
|
1246 |
|
1247 ComplexMatrix |
|
1248 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info, int& rank) const |
|
1249 { |
|
1250 int nrhs = b.cols (); |
|
1251 |
|
1252 int m = rows (); |
|
1253 int n = cols (); |
|
1254 |
|
1255 if (m == 0 || n == 0 || m != b.rows ()) |
|
1256 { |
|
1257 (*current_liboctave_error_handler) |
|
1258 ("matrix dimension mismatch solution of linear equations"); |
|
1259 return Matrix (); |
|
1260 } |
|
1261 |
|
1262 Complex *tmp_data = dup (data (), length ()); |
|
1263 |
|
1264 int nrr = m > n ? m : n; |
|
1265 ComplexMatrix result (nrr, nrhs); |
|
1266 |
|
1267 int i, j; |
|
1268 for (j = 0; j < nrhs; j++) |
|
1269 for (i = 0; i < m; i++) |
|
1270 result.elem (i, j) = b.elem (i, j); |
|
1271 |
|
1272 Complex *presult = result.fortran_vec (); |
|
1273 |
|
1274 int len_s = m < n ? m : n; |
|
1275 double *s = new double [len_s]; |
|
1276 double rcond = -1.0; |
|
1277 int lwork; |
|
1278 if (m < n) |
|
1279 lwork = 2*m + (nrhs > n ? nrhs : n); |
|
1280 else |
|
1281 lwork = 2*n + (nrhs > m ? nrhs : m); |
|
1282 |
|
1283 Complex *work = new Complex [lwork]; |
|
1284 |
|
1285 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1286 lrwork = lrwork > 1 ? lrwork : 1; |
|
1287 double *rwork = new double [lrwork]; |
|
1288 |
|
1289 F77_FCN (zgelss) (&m, &n, &nrhs, tmp_data, &m, presult, &nrr, s, |
|
1290 &rcond, &rank, work, &lwork, rwork, &info); |
|
1291 |
|
1292 ComplexMatrix retval (n, nrhs); |
|
1293 for (j = 0; j < nrhs; j++) |
|
1294 for (i = 0; i < n; i++) |
|
1295 retval.elem (i, j) = result.elem (i, j); |
|
1296 |
|
1297 delete [] tmp_data; |
|
1298 delete [] s; |
|
1299 delete [] work; |
|
1300 delete [] rwork; |
|
1301 |
|
1302 return retval; |
|
1303 } |
|
1304 |
|
1305 ComplexColumnVector |
|
1306 ComplexMatrix::lssolve (const ComplexColumnVector& b) const |
|
1307 { |
|
1308 int info; |
|
1309 int rank; |
|
1310 return lssolve (b, info, rank); |
|
1311 } |
|
1312 |
|
1313 ComplexColumnVector |
|
1314 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info) const |
|
1315 { |
|
1316 int rank; |
|
1317 return lssolve (b, info, rank); |
|
1318 } |
|
1319 |
|
1320 ComplexColumnVector |
|
1321 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info, |
|
1322 int& rank) const |
|
1323 { |
|
1324 int nrhs = 1; |
|
1325 |
|
1326 int m = rows (); |
|
1327 int n = cols (); |
|
1328 |
|
1329 if (m == 0 || n == 0 || m != b.length ()) |
|
1330 { |
|
1331 (*current_liboctave_error_handler) |
|
1332 ("matrix dimension mismatch solution of least squares problem"); |
|
1333 return ComplexColumnVector (); |
|
1334 } |
|
1335 |
|
1336 Complex *tmp_data = dup (data (), length ()); |
|
1337 |
|
1338 int nrr = m > n ? m : n; |
|
1339 ComplexColumnVector result (nrr); |
|
1340 |
|
1341 int i; |
|
1342 for (i = 0; i < m; i++) |
|
1343 result.elem (i) = b.elem (i); |
|
1344 |
|
1345 Complex *presult = result.fortran_vec (); |
|
1346 |
|
1347 int len_s = m < n ? m : n; |
|
1348 double *s = new double [len_s]; |
|
1349 double rcond = -1.0; |
|
1350 int lwork; |
|
1351 if (m < n) |
|
1352 lwork = 2*m + (nrhs > n ? nrhs : n); |
|
1353 else |
|
1354 lwork = 2*n + (nrhs > m ? nrhs : m); |
|
1355 |
|
1356 Complex *work = new Complex [lwork]; |
|
1357 |
|
1358 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1359 lrwork = lrwork > 1 ? lrwork : 1; |
|
1360 double *rwork = new double [lrwork]; |
|
1361 |
|
1362 F77_FCN (zgelss) (&m, &n, &nrhs, tmp_data, &m, presult, &nrr, s, |
|
1363 &rcond, &rank, work, &lwork, rwork, &info); |
|
1364 |
|
1365 ComplexColumnVector retval (n); |
|
1366 for (i = 0; i < n; i++) |
|
1367 retval.elem (i) = result.elem (i); |
|
1368 |
|
1369 delete [] tmp_data; |
|
1370 delete [] s; |
|
1371 delete [] work; |
|
1372 delete [] rwork; |
|
1373 |
|
1374 return retval; |
|
1375 } |
|
1376 |
|
1377 // matrix by diagonal matrix -> matrix operations |
|
1378 |
|
1379 ComplexMatrix& |
|
1380 ComplexMatrix::operator += (const DiagMatrix& a) |
|
1381 { |
|
1382 int nr = rows (); |
|
1383 int nc = cols (); |
|
1384 if (nr != a.rows () || nc != a.cols ()) |
|
1385 { |
|
1386 (*current_liboctave_error_handler) |
|
1387 ("nonconformant matrix += operation attempted"); |
|
1388 return ComplexMatrix (); |
|
1389 } |
|
1390 |
|
1391 for (int i = 0; i < a.length (); i++) |
|
1392 elem (i, i) += a.elem (i, i); |
|
1393 |
|
1394 return *this; |
|
1395 } |
|
1396 |
|
1397 ComplexMatrix& |
|
1398 ComplexMatrix::operator -= (const DiagMatrix& a) |
|
1399 { |
|
1400 int nr = rows (); |
|
1401 int nc = cols (); |
|
1402 if (nr != a.rows () || nc != a.cols ()) |
|
1403 { |
|
1404 (*current_liboctave_error_handler) |
|
1405 ("nonconformant matrix -= operation attempted"); |
|
1406 return ComplexMatrix (); |
|
1407 } |
|
1408 |
|
1409 for (int i = 0; i < a.length (); i++) |
|
1410 elem (i, i) -= a.elem (i, i); |
|
1411 |
|
1412 return *this; |
|
1413 } |
|
1414 |
|
1415 ComplexMatrix& |
|
1416 ComplexMatrix::operator += (const ComplexDiagMatrix& a) |
|
1417 { |
|
1418 int nr = rows (); |
|
1419 int nc = cols (); |
|
1420 if (nr != a.rows () || nc != a.cols ()) |
|
1421 { |
|
1422 (*current_liboctave_error_handler) |
|
1423 ("nonconformant matrix += operation attempted"); |
|
1424 return ComplexMatrix (); |
|
1425 } |
|
1426 |
|
1427 for (int i = 0; i < a.length (); i++) |
|
1428 elem (i, i) += a.elem (i, i); |
|
1429 |
|
1430 return *this; |
|
1431 } |
|
1432 |
|
1433 ComplexMatrix& |
|
1434 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) |
|
1435 { |
|
1436 int nr = rows (); |
|
1437 int nc = cols (); |
|
1438 if (nr != a.rows () || nc != a.cols ()) |
|
1439 { |
|
1440 (*current_liboctave_error_handler) |
|
1441 ("nonconformant matrix -= operation attempted"); |
|
1442 return ComplexMatrix (); |
|
1443 } |
|
1444 |
|
1445 for (int i = 0; i < a.length (); i++) |
|
1446 elem (i, i) -= a.elem (i, i); |
|
1447 |
|
1448 return *this; |
|
1449 } |
|
1450 |
|
1451 // matrix by matrix -> matrix operations |
|
1452 |
|
1453 ComplexMatrix& |
|
1454 ComplexMatrix::operator += (const Matrix& a) |
|
1455 { |
|
1456 int nr = rows (); |
|
1457 int nc = cols (); |
|
1458 if (nr != a.rows () || nc != a.cols ()) |
|
1459 { |
|
1460 (*current_liboctave_error_handler) |
|
1461 ("nonconformant matrix += operation attempted"); |
|
1462 return *this; |
|
1463 } |
|
1464 |
|
1465 if (nr == 0 || nc == 0) |
|
1466 return *this; |
|
1467 |
|
1468 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1469 |
|
1470 add2 (d, a.data (), length ()); |
|
1471 return *this; |
|
1472 } |
|
1473 |
|
1474 ComplexMatrix& |
|
1475 ComplexMatrix::operator -= (const Matrix& a) |
|
1476 { |
|
1477 int nr = rows (); |
|
1478 int nc = cols (); |
|
1479 if (nr != a.rows () || nc != a.cols ()) |
|
1480 { |
|
1481 (*current_liboctave_error_handler) |
|
1482 ("nonconformant matrix -= operation attempted"); |
|
1483 return *this; |
|
1484 } |
|
1485 |
|
1486 if (nr == 0 || nc == 0) |
|
1487 return *this; |
|
1488 |
|
1489 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1490 |
|
1491 subtract2 (d, a.data (), length ()); |
|
1492 return *this; |
|
1493 } |
|
1494 |
|
1495 ComplexMatrix& |
|
1496 ComplexMatrix::operator += (const ComplexMatrix& a) |
|
1497 { |
|
1498 int nr = rows (); |
|
1499 int nc = cols (); |
|
1500 if (nr != a.rows () || nc != a.cols ()) |
|
1501 { |
|
1502 (*current_liboctave_error_handler) |
|
1503 ("nonconformant matrix += operation attempted"); |
|
1504 return *this; |
|
1505 } |
|
1506 |
|
1507 if (nr == 0 || nc == 0) |
|
1508 return *this; |
|
1509 |
|
1510 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1511 |
|
1512 add2 (d, a.data (), length ()); |
|
1513 return *this; |
|
1514 } |
|
1515 |
|
1516 ComplexMatrix& |
|
1517 ComplexMatrix::operator -= (const ComplexMatrix& a) |
|
1518 { |
|
1519 int nr = rows (); |
|
1520 int nc = cols (); |
|
1521 if (nr != a.rows () || nc != a.cols ()) |
|
1522 { |
|
1523 (*current_liboctave_error_handler) |
|
1524 ("nonconformant matrix -= operation attempted"); |
|
1525 return *this; |
|
1526 } |
|
1527 |
|
1528 if (nr == 0 || nc == 0) |
|
1529 return *this; |
|
1530 |
|
1531 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1532 |
|
1533 subtract2 (d, a.data (), length ()); |
|
1534 return *this; |
|
1535 } |
|
1536 |
|
1537 // unary operations |
|
1538 |
|
1539 Matrix |
|
1540 ComplexMatrix::operator ! (void) const |
|
1541 { |
|
1542 return Matrix (not (data (), length ()), rows (), cols ()); |
|
1543 } |
|
1544 |
|
1545 // matrix by scalar -> matrix operations |
|
1546 |
|
1547 ComplexMatrix |
|
1548 operator + (const ComplexMatrix& a, double s) |
|
1549 { |
|
1550 return ComplexMatrix (add (a.data (), a.length (), s), |
|
1551 a.rows (), a.cols ()); |
|
1552 } |
|
1553 |
|
1554 ComplexMatrix |
|
1555 operator - (const ComplexMatrix& a, double s) |
|
1556 { |
|
1557 return ComplexMatrix (subtract (a.data (), a.length (), s), |
|
1558 a.rows (), a.cols ()); |
|
1559 } |
|
1560 |
|
1561 ComplexMatrix |
|
1562 operator * (const ComplexMatrix& a, double s) |
|
1563 { |
|
1564 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
1565 a.rows (), a.cols ()); |
|
1566 } |
|
1567 |
|
1568 ComplexMatrix |
|
1569 operator / (const ComplexMatrix& a, double s) |
|
1570 { |
|
1571 return ComplexMatrix (divide (a.data (), a.length (), s), |
|
1572 a.rows (), a.cols ()); |
|
1573 } |
|
1574 |
|
1575 // scalar by matrix -> matrix operations |
|
1576 |
|
1577 ComplexMatrix |
|
1578 operator + (double s, const ComplexMatrix& a) |
|
1579 { |
|
1580 return ComplexMatrix (add (a.data (), a.length (), s), a.rows (), |
|
1581 a.cols ()); |
|
1582 } |
|
1583 |
|
1584 ComplexMatrix |
|
1585 operator - (double s, const ComplexMatrix& a) |
|
1586 { |
|
1587 return ComplexMatrix (subtract (s, a.data (), a.length ()), |
|
1588 a.rows (), a.cols ()); |
|
1589 } |
|
1590 |
|
1591 ComplexMatrix |
|
1592 operator * (double s, const ComplexMatrix& a) |
|
1593 { |
|
1594 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
1595 a.rows (), a.cols ()); |
|
1596 } |
|
1597 |
|
1598 ComplexMatrix |
|
1599 operator / (double s, const ComplexMatrix& a) |
|
1600 { |
|
1601 return ComplexMatrix (divide (s, a.data (), a.length ()), |
|
1602 a.rows (), a.cols ()); |
|
1603 } |
|
1604 |
|
1605 // matrix by column vector -> column vector operations |
|
1606 |
|
1607 ComplexColumnVector |
|
1608 operator * (const ComplexMatrix& m, const ColumnVector& a) |
|
1609 { |
|
1610 ComplexColumnVector tmp (a); |
|
1611 return m * tmp; |
|
1612 } |
|
1613 |
|
1614 ComplexColumnVector |
|
1615 operator * (const ComplexMatrix& m, const ComplexColumnVector& a) |
|
1616 { |
|
1617 int nr = m.rows (); |
|
1618 int nc = m.cols (); |
|
1619 if (nc != a.length ()) |
|
1620 { |
|
1621 (*current_liboctave_error_handler) |
|
1622 ("nonconformant matrix multiplication attempted"); |
|
1623 return ComplexColumnVector (); |
|
1624 } |
|
1625 |
|
1626 if (nc == 0 || nr == 0) |
|
1627 return ComplexColumnVector (0); |
|
1628 |
|
1629 char trans = 'N'; |
|
1630 int ld = nr; |
|
1631 Complex alpha (1.0); |
|
1632 Complex beta (0.0); |
|
1633 int i_one = 1; |
|
1634 |
|
1635 Complex *y = new Complex [nr]; |
|
1636 |
|
1637 F77_FCN (zgemv) (&trans, &nr, &nc, &alpha, m.data (), &ld, a.data (), |
|
1638 &i_one, &beta, y, &i_one, 1L); |
|
1639 |
|
1640 return ComplexColumnVector (y, nr); |
|
1641 } |
|
1642 |
|
1643 // matrix by diagonal matrix -> matrix operations |
|
1644 |
|
1645 ComplexMatrix |
|
1646 operator + (const ComplexMatrix& m, const DiagMatrix& a) |
|
1647 { |
|
1648 int nr = m.rows (); |
|
1649 int nc = m.cols (); |
|
1650 if (nr != a.rows () || nc != a.cols ()) |
|
1651 { |
|
1652 (*current_liboctave_error_handler) |
|
1653 ("nonconformant matrix addition attempted"); |
|
1654 return ComplexMatrix (); |
|
1655 } |
|
1656 |
|
1657 if (nr == 0 || nc == 0) |
|
1658 return ComplexMatrix (nr, nc); |
|
1659 |
|
1660 ComplexMatrix result (m); |
|
1661 for (int i = 0; i < a.length (); i++) |
|
1662 result.elem (i, i) += a.elem (i, i); |
|
1663 |
|
1664 return result; |
|
1665 } |
|
1666 |
|
1667 ComplexMatrix |
|
1668 operator - (const ComplexMatrix& m, const DiagMatrix& a) |
|
1669 { |
|
1670 int nr = m.rows (); |
|
1671 int nc = m.cols (); |
|
1672 if (nr != a.rows () || nc != a.cols ()) |
|
1673 { |
|
1674 (*current_liboctave_error_handler) |
|
1675 ("nonconformant matrix subtraction attempted"); |
|
1676 return ComplexMatrix (); |
|
1677 } |
|
1678 |
|
1679 if (nr == 0 || nc == 0) |
|
1680 return ComplexMatrix (nr, nc); |
|
1681 |
|
1682 ComplexMatrix result (m); |
|
1683 for (int i = 0; i < a.length (); i++) |
|
1684 result.elem (i, i) -= a.elem (i, i); |
|
1685 |
|
1686 return result; |
|
1687 } |
|
1688 |
|
1689 ComplexMatrix |
|
1690 operator * (const ComplexMatrix& m, const DiagMatrix& a) |
|
1691 { |
|
1692 int nr = m.rows (); |
|
1693 int nc = m.cols (); |
|
1694 int a_nc = a.cols (); |
|
1695 if (nc != a.rows ()) |
|
1696 { |
|
1697 (*current_liboctave_error_handler) |
|
1698 ("nonconformant matrix multiplication attempted"); |
|
1699 return ComplexMatrix (); |
|
1700 } |
|
1701 |
|
1702 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1703 return ComplexMatrix (nr, nc, 0.0); |
|
1704 |
|
1705 Complex *c = new Complex [nr*a_nc]; |
533
|
1706 Complex *ctmp = 0; |
458
|
1707 |
|
1708 for (int j = 0; j < a.length (); j++) |
|
1709 { |
|
1710 int idx = j * nr; |
|
1711 ctmp = c + idx; |
|
1712 if (a.elem (j, j) == 1.0) |
|
1713 { |
|
1714 for (int i = 0; i < nr; i++) |
|
1715 ctmp[i] = m.elem (i, j); |
|
1716 } |
|
1717 else if (a.elem (j, j) == 0.0) |
|
1718 { |
|
1719 for (int i = 0; i < nr; i++) |
|
1720 ctmp[i] = 0.0; |
|
1721 } |
|
1722 else |
|
1723 { |
|
1724 for (int i = 0; i < nr; i++) |
|
1725 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
1726 } |
|
1727 } |
|
1728 |
|
1729 if (a.rows () < a_nc) |
|
1730 { |
|
1731 for (int i = nr * nc; i < nr * a_nc; i++) |
|
1732 ctmp[i] = 0.0; |
|
1733 } |
|
1734 |
|
1735 return ComplexMatrix (c, nr, a_nc); |
|
1736 } |
|
1737 |
|
1738 ComplexMatrix |
|
1739 operator + (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
1740 { |
|
1741 int nr = m.rows (); |
|
1742 int nc = m.cols (); |
|
1743 if (nr != a.rows () || nc != a.cols ()) |
|
1744 { |
|
1745 (*current_liboctave_error_handler) |
|
1746 ("nonconformant matrix addition attempted"); |
|
1747 return ComplexMatrix (); |
|
1748 } |
|
1749 |
|
1750 if (nr == 0 || nc == 0) |
|
1751 return ComplexMatrix (nr, nc); |
|
1752 |
|
1753 ComplexMatrix result (m); |
|
1754 for (int i = 0; i < a.length (); i++) |
|
1755 result.elem (i, i) += a.elem (i, i); |
|
1756 |
|
1757 return result; |
|
1758 } |
|
1759 |
|
1760 ComplexMatrix |
|
1761 operator - (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
1762 { |
|
1763 int nr = m.rows (); |
|
1764 int nc = m.cols (); |
|
1765 if (nr != a.rows () || nc != a.cols ()) |
|
1766 { |
|
1767 (*current_liboctave_error_handler) |
|
1768 ("nonconformant matrix subtraction attempted"); |
|
1769 return ComplexMatrix (); |
|
1770 } |
|
1771 |
|
1772 if (nr == 0 || nc == 0) |
|
1773 return ComplexMatrix (nr, nc); |
|
1774 |
|
1775 ComplexMatrix result (m); |
|
1776 for (int i = 0; i < a.length (); i++) |
|
1777 result.elem (i, i) -= a.elem (i, i); |
|
1778 |
|
1779 return result; |
|
1780 } |
|
1781 |
|
1782 ComplexMatrix |
|
1783 operator * (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
1784 { |
|
1785 int nr = m.rows (); |
|
1786 int nc = m.cols (); |
|
1787 int a_nc = a.cols (); |
|
1788 if (nc != a.rows ()) |
|
1789 { |
|
1790 (*current_liboctave_error_handler) |
|
1791 ("nonconformant matrix multiplication attempted"); |
|
1792 return ComplexMatrix (); |
|
1793 } |
|
1794 |
|
1795 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1796 return ComplexMatrix (nr, nc, 0.0); |
|
1797 |
|
1798 Complex *c = new Complex [nr*a_nc]; |
533
|
1799 Complex *ctmp = 0; |
458
|
1800 |
|
1801 for (int j = 0; j < a.length (); j++) |
|
1802 { |
|
1803 int idx = j * nr; |
|
1804 ctmp = c + idx; |
|
1805 if (a.elem (j, j) == 1.0) |
|
1806 { |
|
1807 for (int i = 0; i < nr; i++) |
|
1808 ctmp[i] = m.elem (i, j); |
|
1809 } |
|
1810 else if (a.elem (j, j) == 0.0) |
|
1811 { |
|
1812 for (int i = 0; i < nr; i++) |
|
1813 ctmp[i] = 0.0; |
|
1814 } |
|
1815 else |
|
1816 { |
|
1817 for (int i = 0; i < nr; i++) |
|
1818 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
1819 } |
|
1820 } |
|
1821 |
|
1822 if (a.rows () < a_nc) |
|
1823 { |
|
1824 for (int i = nr * nc; i < nr * a_nc; i++) |
|
1825 ctmp[i] = 0.0; |
|
1826 } |
|
1827 |
|
1828 return ComplexMatrix (c, nr, a_nc); |
|
1829 } |
|
1830 |
|
1831 // matrix by matrix -> matrix operations |
|
1832 |
|
1833 ComplexMatrix |
|
1834 operator + (const ComplexMatrix& m, const Matrix& a) |
|
1835 { |
|
1836 int nr = m.rows (); |
|
1837 int nc = m.cols (); |
|
1838 if (nr != a.rows () || nc != a.cols ()) |
|
1839 { |
|
1840 (*current_liboctave_error_handler) |
|
1841 ("nonconformant matrix addition attempted"); |
|
1842 return ComplexMatrix (); |
|
1843 } |
|
1844 |
|
1845 if (nr == 0 || nc == 0) |
|
1846 return ComplexMatrix (nr, nc); |
|
1847 |
|
1848 return ComplexMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
1849 } |
|
1850 |
|
1851 ComplexMatrix |
|
1852 operator - (const ComplexMatrix& m, const Matrix& a) |
|
1853 { |
|
1854 int nr = m.rows (); |
|
1855 int nc = m.cols (); |
|
1856 if (nr != a.rows () || nc != a.cols ()) |
|
1857 { |
|
1858 (*current_liboctave_error_handler) |
|
1859 ("nonconformant matrix subtraction attempted"); |
|
1860 return ComplexMatrix (); |
|
1861 } |
|
1862 |
|
1863 if (nr == 0 || nc == 0) |
|
1864 return ComplexMatrix (nr, nc); |
|
1865 |
|
1866 return ComplexMatrix (subtract (m.data (), a.data (), m.length ()), nr, nc); |
|
1867 } |
|
1868 |
|
1869 ComplexMatrix |
|
1870 operator * (const ComplexMatrix& m, const Matrix& a) |
|
1871 { |
|
1872 ComplexMatrix tmp (a); |
|
1873 return m * tmp; |
|
1874 } |
|
1875 |
|
1876 ComplexMatrix |
|
1877 operator * (const ComplexMatrix& m, const ComplexMatrix& a) |
|
1878 { |
|
1879 int nr = m.rows (); |
|
1880 int nc = m.cols (); |
|
1881 int a_nc = a.cols (); |
|
1882 if (nc != a.rows ()) |
|
1883 { |
|
1884 (*current_liboctave_error_handler) |
|
1885 ("nonconformant matrix multiplication attempted"); |
|
1886 return ComplexMatrix (); |
|
1887 } |
|
1888 |
|
1889 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1890 return ComplexMatrix (nr, nc, 0.0); |
|
1891 |
|
1892 char trans = 'N'; |
|
1893 char transa = 'N'; |
|
1894 |
|
1895 int ld = nr; |
|
1896 int lda = a.rows (); |
|
1897 |
|
1898 Complex alpha (1.0); |
|
1899 Complex beta (0.0); |
|
1900 |
|
1901 Complex *c = new Complex [nr*a_nc]; |
|
1902 |
|
1903 F77_FCN (zgemm) (&trans, &transa, &nr, &a_nc, &nc, &alpha, m.data (), |
|
1904 &ld, a.data (), &lda, &beta, c, &nr, 1L, 1L); |
|
1905 |
|
1906 return ComplexMatrix (c, nr, a_nc); |
|
1907 } |
|
1908 |
|
1909 ComplexMatrix |
|
1910 product (const ComplexMatrix& m, const Matrix& a) |
|
1911 { |
|
1912 int nr = m.rows (); |
|
1913 int nc = m.cols (); |
|
1914 if (nr != a.rows () || nc != a.cols ()) |
|
1915 { |
|
1916 (*current_liboctave_error_handler) |
|
1917 ("nonconformant matrix product attempted"); |
|
1918 return ComplexMatrix (); |
|
1919 } |
|
1920 |
|
1921 if (nr == 0 || nc == 0) |
|
1922 return ComplexMatrix (nr, nc); |
|
1923 |
|
1924 return ComplexMatrix (multiply (m.data (), a.data (), m.length ()), nr, nc); |
|
1925 } |
|
1926 |
|
1927 ComplexMatrix |
|
1928 quotient (const ComplexMatrix& m, const Matrix& a) |
|
1929 { |
|
1930 int nr = m.rows (); |
|
1931 int nc = m.cols (); |
|
1932 if (nr != a.rows () || nc != a.cols ()) |
|
1933 { |
|
1934 (*current_liboctave_error_handler) |
|
1935 ("nonconformant matrix quotient attempted"); |
|
1936 return ComplexMatrix (); |
|
1937 } |
|
1938 |
|
1939 if (nr == 0 || nc == 0) |
|
1940 return ComplexMatrix (nr, nc); |
|
1941 |
|
1942 return ComplexMatrix (divide (m.data (), a.data (), m.length ()), nr, nc); |
|
1943 } |
|
1944 |
|
1945 // other operations |
|
1946 |
|
1947 ComplexMatrix |
|
1948 map (c_c_Mapper f, const ComplexMatrix& a) |
|
1949 { |
|
1950 ComplexMatrix b (a); |
|
1951 b.map (f); |
|
1952 return b; |
|
1953 } |
|
1954 |
|
1955 Matrix |
|
1956 map (d_c_Mapper f, const ComplexMatrix& a) |
|
1957 { |
|
1958 int a_nc = a.cols (); |
|
1959 int a_nr = a.rows (); |
|
1960 Matrix b (a_nr, a_nc); |
|
1961 for (int j = 0; j < a_nc; j++) |
|
1962 for (int i = 0; i < a_nr; i++) |
|
1963 b.elem (i, j) = f (a.elem (i, j)); |
|
1964 return b; |
|
1965 } |
|
1966 |
|
1967 void |
|
1968 ComplexMatrix::map (c_c_Mapper f) |
|
1969 { |
|
1970 for (int j = 0; j < cols (); j++) |
|
1971 for (int i = 0; i < rows (); i++) |
|
1972 elem (i, j) = f (elem (i, j)); |
|
1973 } |
|
1974 |
|
1975 Matrix |
|
1976 ComplexMatrix::all (void) const |
|
1977 { |
|
1978 int nr = rows (); |
|
1979 int nc = cols (); |
|
1980 Matrix retval; |
|
1981 if (nr > 0 && nc > 0) |
|
1982 { |
|
1983 if (nr == 1) |
|
1984 { |
|
1985 retval.resize (1, 1); |
|
1986 retval.elem (0, 0) = 1.0; |
|
1987 for (int j = 0; j < nc; j++) |
|
1988 { |
|
1989 if (elem (0, j) == 0.0) |
|
1990 { |
|
1991 retval.elem (0, 0) = 0.0; |
|
1992 break; |
|
1993 } |
|
1994 } |
|
1995 } |
|
1996 else if (nc == 1) |
|
1997 { |
|
1998 retval.resize (1, 1); |
|
1999 retval.elem (0, 0) = 1.0; |
|
2000 for (int i = 0; i < nr; i++) |
|
2001 { |
|
2002 if (elem (i, 0) == 0.0) |
|
2003 { |
|
2004 retval.elem (0, 0) = 0.0; |
|
2005 break; |
|
2006 } |
|
2007 } |
|
2008 } |
|
2009 else |
|
2010 { |
|
2011 retval.resize (1, nc); |
|
2012 for (int j = 0; j < nc; j++) |
|
2013 { |
|
2014 retval.elem (0, j) = 1.0; |
|
2015 for (int i = 0; i < nr; i++) |
|
2016 { |
|
2017 if (elem (i, j) == 0.0) |
|
2018 { |
|
2019 retval.elem (0, j) = 0.0; |
|
2020 break; |
|
2021 } |
|
2022 } |
|
2023 } |
|
2024 } |
|
2025 } |
|
2026 return retval; |
|
2027 } |
|
2028 |
|
2029 Matrix |
|
2030 ComplexMatrix::any (void) const |
|
2031 { |
|
2032 int nr = rows (); |
|
2033 int nc = cols (); |
|
2034 Matrix retval; |
|
2035 if (nr > 0 && nc > 0) |
|
2036 { |
|
2037 if (nr == 1) |
|
2038 { |
|
2039 retval.resize (1, 1); |
|
2040 retval.elem (0, 0) = 0.0; |
|
2041 for (int j = 0; j < nc; j++) |
|
2042 { |
|
2043 if (elem (0, j) != 0.0) |
|
2044 { |
|
2045 retval.elem (0, 0) = 1.0; |
|
2046 break; |
|
2047 } |
|
2048 } |
|
2049 } |
|
2050 else if (nc == 1) |
|
2051 { |
|
2052 retval.resize (1, 1); |
|
2053 retval.elem (0, 0) = 0.0; |
|
2054 for (int i = 0; i < nr; i++) |
|
2055 { |
|
2056 if (elem (i, 0) != 0.0) |
|
2057 { |
|
2058 retval.elem (0, 0) = 1.0; |
|
2059 break; |
|
2060 } |
|
2061 } |
|
2062 } |
|
2063 else |
|
2064 { |
|
2065 retval.resize (1, nc); |
|
2066 for (int j = 0; j < nc; j++) |
|
2067 { |
|
2068 retval.elem (0, j) = 0.0; |
|
2069 for (int i = 0; i < nr; i++) |
|
2070 { |
|
2071 if (elem (i, j) != 0.0) |
|
2072 { |
|
2073 retval.elem (0, j) = 1.0; |
|
2074 break; |
|
2075 } |
|
2076 } |
|
2077 } |
|
2078 } |
|
2079 } |
|
2080 return retval; |
|
2081 } |
|
2082 |
|
2083 ComplexMatrix |
|
2084 ComplexMatrix::cumprod (void) const |
|
2085 { |
|
2086 int nr = rows (); |
|
2087 int nc = cols (); |
|
2088 ComplexMatrix retval; |
|
2089 if (nr > 0 && nc > 0) |
|
2090 { |
|
2091 if (nr == 1) |
|
2092 { |
|
2093 retval.resize (1, nc); |
|
2094 Complex prod = elem (0, 0); |
|
2095 for (int j = 0; j < nc; j++) |
|
2096 { |
|
2097 retval.elem (0, j) = prod; |
|
2098 if (j < nc - 1) |
|
2099 prod *= elem (0, j+1); |
|
2100 } |
|
2101 } |
|
2102 else if (nc == 1) |
|
2103 { |
|
2104 retval.resize (nr, 1); |
|
2105 Complex prod = elem (0, 0); |
|
2106 for (int i = 0; i < nr; i++) |
|
2107 { |
|
2108 retval.elem (i, 0) = prod; |
|
2109 if (i < nr - 1) |
|
2110 prod *= elem (i+1, 0); |
|
2111 } |
|
2112 } |
|
2113 else |
|
2114 { |
|
2115 retval.resize (nr, nc); |
|
2116 for (int j = 0; j < nc; j++) |
|
2117 { |
|
2118 Complex prod = elem (0, j); |
|
2119 for (int i = 0; i < nr; i++) |
|
2120 { |
|
2121 retval.elem (i, j) = prod; |
|
2122 if (i < nr - 1) |
|
2123 prod *= elem (i+1, j); |
|
2124 } |
|
2125 } |
|
2126 } |
|
2127 } |
|
2128 return retval; |
|
2129 } |
|
2130 |
|
2131 ComplexMatrix |
|
2132 ComplexMatrix::cumsum (void) const |
|
2133 { |
|
2134 int nr = rows (); |
|
2135 int nc = cols (); |
|
2136 ComplexMatrix retval; |
|
2137 if (nr > 0 && nc > 0) |
|
2138 { |
|
2139 if (nr == 1) |
|
2140 { |
|
2141 retval.resize (1, nc); |
|
2142 Complex sum = elem (0, 0); |
|
2143 for (int j = 0; j < nc; j++) |
|
2144 { |
|
2145 retval.elem (0, j) = sum; |
|
2146 if (j < nc - 1) |
|
2147 sum += elem (0, j+1); |
|
2148 } |
|
2149 } |
|
2150 else if (nc == 1) |
|
2151 { |
|
2152 retval.resize (nr, 1); |
|
2153 Complex sum = elem (0, 0); |
|
2154 for (int i = 0; i < nr; i++) |
|
2155 { |
|
2156 retval.elem (i, 0) = sum; |
|
2157 if (i < nr - 1) |
|
2158 sum += elem (i+1, 0); |
|
2159 } |
|
2160 } |
|
2161 else |
|
2162 { |
|
2163 retval.resize (nr, nc); |
|
2164 for (int j = 0; j < nc; j++) |
|
2165 { |
|
2166 Complex sum = elem (0, j); |
|
2167 for (int i = 0; i < nr; i++) |
|
2168 { |
|
2169 retval.elem (i, j) = sum; |
|
2170 if (i < nr - 1) |
|
2171 sum += elem (i+1, j); |
|
2172 } |
|
2173 } |
|
2174 } |
|
2175 } |
|
2176 return retval; |
|
2177 } |
|
2178 |
|
2179 ComplexMatrix |
|
2180 ComplexMatrix::prod (void) const |
|
2181 { |
|
2182 int nr = rows (); |
|
2183 int nc = cols (); |
|
2184 ComplexMatrix retval; |
|
2185 if (nr > 0 && nc > 0) |
|
2186 { |
|
2187 if (nr == 1) |
|
2188 { |
|
2189 retval.resize (1, 1); |
|
2190 retval.elem (0, 0) = 1.0; |
|
2191 for (int j = 0; j < nc; j++) |
|
2192 retval.elem (0, 0) *= elem (0, j); |
|
2193 } |
|
2194 else if (nc == 1) |
|
2195 { |
|
2196 retval.resize (1, 1); |
|
2197 retval.elem (0, 0) = 1.0; |
|
2198 for (int i = 0; i < nr; i++) |
|
2199 retval.elem (0, 0) *= elem (i, 0); |
|
2200 } |
|
2201 else |
|
2202 { |
|
2203 retval.resize (1, nc); |
|
2204 for (int j = 0; j < nc; j++) |
|
2205 { |
|
2206 retval.elem (0, j) = 1.0; |
|
2207 for (int i = 0; i < nr; i++) |
|
2208 retval.elem (0, j) *= elem (i, j); |
|
2209 } |
|
2210 } |
|
2211 } |
|
2212 return retval; |
|
2213 } |
|
2214 |
|
2215 ComplexMatrix |
|
2216 ComplexMatrix::sum (void) const |
|
2217 { |
|
2218 int nr = rows (); |
|
2219 int nc = cols (); |
|
2220 ComplexMatrix retval; |
|
2221 if (nr > 0 && nc > 0) |
|
2222 { |
|
2223 if (nr == 1) |
|
2224 { |
|
2225 retval.resize (1, 1); |
|
2226 retval.elem (0, 0) = 0.0; |
|
2227 for (int j = 0; j < nc; j++) |
|
2228 retval.elem (0, 0) += elem (0, j); |
|
2229 } |
|
2230 else if (nc == 1) |
|
2231 { |
|
2232 retval.resize (1, 1); |
|
2233 retval.elem (0, 0) = 0.0; |
|
2234 for (int i = 0; i < nr; i++) |
|
2235 retval.elem (0, 0) += elem (i, 0); |
|
2236 } |
|
2237 else |
|
2238 { |
|
2239 retval.resize (1, nc); |
|
2240 for (int j = 0; j < nc; j++) |
|
2241 { |
|
2242 retval.elem (0, j) = 0.0; |
|
2243 for (int i = 0; i < nr; i++) |
|
2244 retval.elem (0, j) += elem (i, j); |
|
2245 } |
|
2246 } |
|
2247 } |
|
2248 return retval; |
|
2249 } |
|
2250 |
|
2251 ComplexMatrix |
|
2252 ComplexMatrix::sumsq (void) const |
|
2253 { |
|
2254 int nr = rows (); |
|
2255 int nc = cols (); |
|
2256 ComplexMatrix retval; |
|
2257 if (nr > 0 && nc > 0) |
|
2258 { |
|
2259 if (nr == 1) |
|
2260 { |
|
2261 retval.resize (1, 1); |
|
2262 retval.elem (0, 0) = 0.0; |
|
2263 for (int j = 0; j < nc; j++) |
|
2264 { |
|
2265 Complex d = elem (0, j); |
|
2266 retval.elem (0, 0) += d * d; |
|
2267 } |
|
2268 } |
|
2269 else if (nc == 1) |
|
2270 { |
|
2271 retval.resize (1, 1); |
|
2272 retval.elem (0, 0) = 0.0; |
|
2273 for (int i = 0; i < nr; i++) |
|
2274 { |
|
2275 Complex d = elem (i, 0); |
|
2276 retval.elem (0, 0) += d * d; |
|
2277 } |
|
2278 } |
|
2279 else |
|
2280 { |
|
2281 retval.resize (1, nc); |
|
2282 for (int j = 0; j < nc; j++) |
|
2283 { |
|
2284 retval.elem (0, j) = 0.0; |
|
2285 for (int i = 0; i < nr; i++) |
|
2286 { |
|
2287 Complex d = elem (i, j); |
|
2288 retval.elem (0, j) += d * d; |
|
2289 } |
|
2290 } |
|
2291 } |
|
2292 } |
|
2293 return retval; |
|
2294 } |
|
2295 |
|
2296 ComplexColumnVector |
|
2297 ComplexMatrix::diag (void) const |
|
2298 { |
|
2299 return diag (0); |
|
2300 } |
|
2301 |
|
2302 ComplexColumnVector |
|
2303 ComplexMatrix::diag (int k) const |
|
2304 { |
|
2305 int nnr = rows (); |
|
2306 int nnc = cols (); |
|
2307 if (k > 0) |
|
2308 nnc -= k; |
|
2309 else if (k < 0) |
|
2310 nnr += k; |
|
2311 |
|
2312 ComplexColumnVector d; |
|
2313 |
|
2314 if (nnr > 0 && nnc > 0) |
|
2315 { |
|
2316 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
2317 |
|
2318 d.resize (ndiag); |
|
2319 |
|
2320 if (k > 0) |
|
2321 { |
|
2322 for (int i = 0; i < ndiag; i++) |
|
2323 d.elem (i) = elem (i, i+k); |
|
2324 } |
|
2325 else if ( k < 0) |
|
2326 { |
|
2327 for (int i = 0; i < ndiag; i++) |
|
2328 d.elem (i) = elem (i-k, i); |
|
2329 } |
|
2330 else |
|
2331 { |
|
2332 for (int i = 0; i < ndiag; i++) |
|
2333 d.elem (i) = elem (i, i); |
|
2334 } |
|
2335 } |
|
2336 else |
|
2337 cerr << "diag: requested diagonal out of range\n"; |
|
2338 |
|
2339 return d; |
|
2340 } |
|
2341 |
|
2342 ComplexColumnVector |
|
2343 ComplexMatrix::row_min (void) const |
|
2344 { |
|
2345 ComplexColumnVector result; |
|
2346 |
|
2347 int nr = rows (); |
|
2348 int nc = cols (); |
|
2349 if (nr > 0 && nc > 0) |
|
2350 { |
|
2351 result.resize (nr); |
|
2352 |
|
2353 for (int i = 0; i < nr; i++) |
|
2354 { |
|
2355 Complex res = elem (i, 0); |
|
2356 double absres = abs (res); |
|
2357 for (int j = 1; j < nc; j++) |
|
2358 if (abs (elem (i, j)) < absres) |
|
2359 { |
|
2360 res = elem (i, j); |
|
2361 absres = abs (res); |
|
2362 } |
|
2363 result.elem (i) = res; |
|
2364 } |
|
2365 } |
|
2366 |
|
2367 return result; |
|
2368 } |
|
2369 |
|
2370 ComplexColumnVector |
|
2371 ComplexMatrix::row_min_loc (void) const |
|
2372 { |
|
2373 ComplexColumnVector result; |
|
2374 |
|
2375 int nr = rows (); |
|
2376 int nc = cols (); |
|
2377 |
|
2378 if (nr > 0 && nc > 0) |
|
2379 { |
|
2380 result.resize (nr); |
|
2381 |
|
2382 for (int i = 0; i < nr; i++) |
|
2383 { |
|
2384 Complex res = 0; |
|
2385 double absres = abs (elem (i, 0)); |
|
2386 for (int j = 0; j < nc; j++) |
|
2387 if (abs (elem (i, j)) < absres) |
|
2388 { |
|
2389 res = j; |
|
2390 absres = abs (elem (i, j)); |
|
2391 } |
|
2392 result.elem (i) = res + 1; |
|
2393 } |
|
2394 } |
|
2395 |
|
2396 return result; |
|
2397 } |
|
2398 |
|
2399 ComplexColumnVector |
|
2400 ComplexMatrix::row_max (void) const |
|
2401 { |
|
2402 ComplexColumnVector result; |
|
2403 |
|
2404 int nr = rows (); |
|
2405 int nc = cols (); |
|
2406 |
|
2407 if (nr > 0 && nc > 0) |
|
2408 { |
|
2409 result.resize (nr); |
|
2410 |
|
2411 for (int i = 0; i < nr; i++) |
|
2412 { |
|
2413 Complex res = elem (i, 0); |
|
2414 double absres = abs (res); |
|
2415 for (int j = 1; j < nc; j++) |
|
2416 if (abs (elem (i, j)) > absres) |
|
2417 { |
|
2418 res = elem (i, j); |
|
2419 absres = abs (res); |
|
2420 } |
|
2421 result.elem (i) = res; |
|
2422 } |
|
2423 } |
|
2424 |
|
2425 return result; |
|
2426 } |
|
2427 |
|
2428 ComplexColumnVector |
|
2429 ComplexMatrix::row_max_loc (void) const |
|
2430 { |
|
2431 ComplexColumnVector result; |
|
2432 |
|
2433 int nr = rows (); |
|
2434 int nc = cols (); |
|
2435 |
|
2436 if (nr > 0 && nc > 0) |
|
2437 { |
|
2438 result.resize (nr); |
|
2439 |
|
2440 for (int i = 0; i < nr; i++) |
|
2441 { |
|
2442 Complex res = 0; |
|
2443 double absres = abs (elem (i, 0)); |
|
2444 for (int j = 0; j < nc; j++) |
|
2445 if (abs (elem (i, j)) > absres) |
|
2446 { |
|
2447 res = j; |
|
2448 absres = abs (elem (i, j)); |
|
2449 } |
|
2450 result.elem (i) = res + 1; |
|
2451 } |
|
2452 } |
|
2453 |
|
2454 return result; |
|
2455 } |
|
2456 |
|
2457 ComplexRowVector |
|
2458 ComplexMatrix::column_min (void) const |
|
2459 { |
|
2460 ComplexRowVector result; |
|
2461 |
|
2462 int nr = rows (); |
|
2463 int nc = cols (); |
|
2464 |
|
2465 if (nr > 0 && nc > 0) |
|
2466 { |
|
2467 result.resize (nc); |
|
2468 |
|
2469 for (int j = 0; j < nc; j++) |
|
2470 { |
|
2471 Complex res = elem (0, j); |
|
2472 double absres = abs (res); |
|
2473 for (int i = 1; i < nr; i++) |
|
2474 if (abs (elem (i, j)) < absres) |
|
2475 { |
|
2476 res = elem (i, j); |
|
2477 absres = abs (res); |
|
2478 } |
|
2479 result.elem (j) = res; |
|
2480 } |
|
2481 } |
|
2482 |
|
2483 return result; |
|
2484 } |
|
2485 |
|
2486 ComplexRowVector |
|
2487 ComplexMatrix::column_min_loc (void) const |
|
2488 { |
|
2489 ComplexRowVector result; |
|
2490 |
|
2491 int nr = rows (); |
|
2492 int nc = cols (); |
|
2493 |
|
2494 if (nr > 0 && nc > 0) |
|
2495 { |
|
2496 result.resize (nc); |
|
2497 |
|
2498 for (int j = 0; j < nc; j++) |
|
2499 { |
|
2500 Complex res = 0; |
|
2501 double absres = abs (elem (0, j)); |
|
2502 for (int i = 0; i < nr; i++) |
|
2503 if (abs (elem (i, j)) < absres) |
|
2504 { |
|
2505 res = i; |
|
2506 absres = abs (elem (i, j)); |
|
2507 } |
|
2508 result.elem (j) = res + 1; |
|
2509 } |
|
2510 } |
|
2511 |
|
2512 return result; |
|
2513 } |
|
2514 |
|
2515 ComplexRowVector |
|
2516 ComplexMatrix::column_max (void) const |
|
2517 { |
|
2518 ComplexRowVector result; |
|
2519 |
|
2520 int nr = rows (); |
|
2521 int nc = cols (); |
|
2522 |
|
2523 if (nr > 0 && nc > 0) |
|
2524 { |
|
2525 result.resize (nc); |
|
2526 |
|
2527 for (int j = 0; j < nc; j++) |
|
2528 { |
|
2529 Complex res = elem (0, j); |
|
2530 double absres = abs (res); |
|
2531 for (int i = 1; i < nr; i++) |
|
2532 if (abs (elem (i, j)) > absres) |
|
2533 { |
|
2534 res = elem (i, j); |
|
2535 absres = abs (res); |
|
2536 } |
|
2537 result.elem (j) = res; |
|
2538 } |
|
2539 } |
|
2540 |
|
2541 return result; |
|
2542 } |
|
2543 |
|
2544 ComplexRowVector |
|
2545 ComplexMatrix::column_max_loc (void) const |
|
2546 { |
|
2547 ComplexRowVector result; |
|
2548 |
|
2549 int nr = rows (); |
|
2550 int nc = cols (); |
|
2551 |
|
2552 if (nr > 0 && nc > 0) |
|
2553 { |
|
2554 result.resize (nc); |
|
2555 |
|
2556 for (int j = 0; j < nc; j++) |
|
2557 { |
|
2558 Complex res = 0; |
|
2559 double absres = abs (elem (0, j)); |
|
2560 for (int i = 0; i < nr; i++) |
|
2561 if (abs (elem (i, j)) > absres) |
|
2562 { |
|
2563 res = i; |
|
2564 absres = abs (elem (i, j)); |
|
2565 } |
|
2566 result.elem (j) = res + 1; |
|
2567 } |
|
2568 } |
|
2569 |
|
2570 return result; |
|
2571 } |
|
2572 |
|
2573 // i/o |
|
2574 |
|
2575 ostream& |
|
2576 operator << (ostream& os, const ComplexMatrix& a) |
|
2577 { |
|
2578 // int field_width = os.precision () + 7; |
|
2579 for (int i = 0; i < a.rows (); i++) |
|
2580 { |
|
2581 for (int j = 0; j < a.cols (); j++) |
|
2582 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
2583 os << "\n"; |
|
2584 } |
|
2585 return os; |
|
2586 } |
|
2587 |
|
2588 istream& |
|
2589 operator >> (istream& is, ComplexMatrix& a) |
|
2590 { |
|
2591 int nr = a.rows (); |
|
2592 int nc = a.cols (); |
|
2593 |
|
2594 if (nr < 1 || nc < 1) |
|
2595 is.clear (ios::badbit); |
|
2596 else |
|
2597 { |
|
2598 Complex tmp; |
|
2599 for (int i = 0; i < nr; i++) |
|
2600 for (int j = 0; j < nc; j++) |
|
2601 { |
|
2602 is >> tmp; |
|
2603 if (is) |
|
2604 a.elem (i, j) = tmp; |
|
2605 else |
|
2606 break; |
|
2607 } |
|
2608 } |
|
2609 |
|
2610 return is; |
|
2611 } |
|
2612 |
|
2613 /* |
|
2614 ;;; Local Variables: *** |
|
2615 ;;; mode: C++ *** |
|
2616 ;;; page-delimiter: "^/\\*" *** |
|
2617 ;;; End: *** |
|
2618 */ |