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