458
|
1 // Matrix manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
458
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
458
|
26 #endif |
|
27 |
|
28 #include <sys/types.h> |
|
29 #include <iostream.h> |
740
|
30 #include <float.h> |
458
|
31 |
|
32 #include <Complex.h> |
|
33 |
|
34 #include "mx-base.h" |
|
35 #include "CmplxDET.h" |
740
|
36 #include "CmplxSVD.h" |
458
|
37 #include "mx-inlines.cc" |
|
38 #include "lo-error.h" |
|
39 #include "f77-uscore.h" |
|
40 |
|
41 // Fortran functions we call. |
|
42 |
|
43 extern "C" |
|
44 { |
|
45 int F77_FCN (zgemm) (const char*, const char*, const int*, |
|
46 const int*, const int*, const Complex*, |
|
47 const Complex*, const int*, const Complex*, |
|
48 const int*, const Complex*, Complex*, const int*, |
|
49 long, long); |
|
50 |
|
51 int F77_FCN (zgeco) (Complex*, const int*, const int*, int*, |
|
52 double*, Complex*); |
|
53 |
|
54 int F77_FCN (zgedi) (Complex*, const int*, const int*, int*, |
|
55 Complex*, Complex*, const int*); |
|
56 |
|
57 int F77_FCN (zgesl) (Complex*, const int*, const int*, int*, |
|
58 Complex*, const int*); |
|
59 |
|
60 int F77_FCN (zgelss) (const int*, const int*, const int*, Complex*, |
|
61 const int*, Complex*, const int*, double*, |
|
62 const double*, int*, Complex*, const int*, |
|
63 double*, int*); |
|
64 |
|
65 // Note that the original complex fft routines were not written for |
|
66 // double complex arguments. They have been modified by adding an |
|
67 // implicit double precision (a-h,o-z) statement at the beginning of |
|
68 // each subroutine. |
|
69 |
|
70 int F77_FCN (cffti) (const int*, Complex*); |
|
71 |
|
72 int F77_FCN (cfftf) (const int*, Complex*, Complex*); |
|
73 |
|
74 int F77_FCN (cfftb) (const int*, Complex*, Complex*); |
|
75 } |
|
76 |
|
77 /* |
|
78 * Complex Matrix class |
|
79 */ |
|
80 |
|
81 ComplexMatrix::ComplexMatrix (const Matrix& a) |
1214
|
82 : MArray2<Complex> (a.rows (), a.cols ()) |
458
|
83 { |
|
84 for (int j = 0; j < cols (); j++) |
|
85 for (int i = 0; i < rows (); i++) |
|
86 elem (i, j) = a.elem (i, j); |
|
87 } |
|
88 |
|
89 ComplexMatrix::ComplexMatrix (const DiagMatrix& a) |
1214
|
90 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
458
|
91 { |
|
92 for (int i = 0; i < a.length (); i++) |
|
93 elem (i, i) = a.elem (i, i); |
|
94 } |
|
95 |
|
96 ComplexMatrix::ComplexMatrix (const ComplexDiagMatrix& a) |
1214
|
97 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
458
|
98 { |
|
99 for (int i = 0; i < a.length (); i++) |
|
100 elem (i, i) = a.elem (i, i); |
|
101 } |
|
102 |
|
103 int |
|
104 ComplexMatrix::operator == (const ComplexMatrix& a) const |
|
105 { |
|
106 if (rows () != a.rows () || cols () != a.cols ()) |
|
107 return 0; |
|
108 |
|
109 return equal (data (), a.data (), length ()); |
|
110 } |
|
111 |
|
112 int |
|
113 ComplexMatrix::operator != (const ComplexMatrix& a) const |
|
114 { |
|
115 return !(*this == a); |
|
116 } |
|
117 |
|
118 // destructive insert/delete/reorder operations |
|
119 |
|
120 ComplexMatrix& |
|
121 ComplexMatrix::insert (const Matrix& a, int r, int c) |
|
122 { |
|
123 int a_nr = a.rows (); |
|
124 int a_nc = a.cols (); |
|
125 if (r < 0 || r + a_nr - 1 > rows () || c < 0 || c + a_nc - 1 > cols ()) |
|
126 { |
|
127 (*current_liboctave_error_handler) ("range error for insert"); |
|
128 return *this; |
|
129 } |
|
130 |
|
131 for (int j = 0; j < a_nc; j++) |
|
132 for (int i = 0; i < a_nr; i++) |
|
133 elem (r+i, c+j) = a.elem (i, j); |
|
134 |
|
135 return *this; |
|
136 } |
|
137 |
|
138 ComplexMatrix& |
|
139 ComplexMatrix::insert (const RowVector& a, int r, int c) |
|
140 { |
|
141 int a_len = a.length (); |
|
142 if (r < 0 || r >= rows () || c < 0 || c + a_len - 1 > cols ()) |
|
143 { |
|
144 (*current_liboctave_error_handler) ("range error for insert"); |
|
145 return *this; |
|
146 } |
|
147 |
|
148 for (int i = 0; i < a_len; i++) |
|
149 elem (r, c+i) = a.elem (i); |
|
150 |
|
151 return *this; |
|
152 } |
|
153 |
|
154 ComplexMatrix& |
|
155 ComplexMatrix::insert (const ColumnVector& a, int r, int c) |
|
156 { |
|
157 int a_len = a.length (); |
|
158 if (r < 0 || r + a_len - 1 > rows () || c < 0 || c >= cols ()) |
|
159 { |
|
160 (*current_liboctave_error_handler) ("range error for insert"); |
|
161 return *this; |
|
162 } |
|
163 |
|
164 for (int i = 0; i < a_len; i++) |
|
165 elem (r+i, c) = a.elem (i); |
|
166 |
|
167 return *this; |
|
168 } |
|
169 |
|
170 ComplexMatrix& |
|
171 ComplexMatrix::insert (const DiagMatrix& a, int r, int c) |
|
172 { |
|
173 if (r < 0 || r + a.rows () - 1 > rows () |
|
174 || c < 0 || c + a.cols () - 1 > cols ()) |
|
175 { |
|
176 (*current_liboctave_error_handler) ("range error for insert"); |
|
177 return *this; |
|
178 } |
|
179 |
|
180 for (int i = 0; i < a.length (); i++) |
|
181 elem (r+i, c+i) = a.elem (i, i); |
|
182 |
|
183 return *this; |
|
184 } |
|
185 |
|
186 ComplexMatrix& |
|
187 ComplexMatrix::insert (const ComplexMatrix& a, int r, int c) |
|
188 { |
|
189 int a_nr = a.rows (); |
|
190 int a_nc = a.cols (); |
|
191 if (r < 0 || r + a_nr - 1 > rows () || c < 0 || c + a_nc - 1 > cols ()) |
|
192 { |
|
193 (*current_liboctave_error_handler) ("range error for insert"); |
|
194 return *this; |
|
195 } |
|
196 |
|
197 for (int j = 0; j < a_nc; j++) |
|
198 for (int i = 0; i < a_nr; i++) |
|
199 elem (r+i, c+j) = a.elem (i, j); |
|
200 |
|
201 return *this; |
|
202 } |
|
203 |
|
204 ComplexMatrix& |
|
205 ComplexMatrix::insert (const ComplexRowVector& a, int r, int c) |
|
206 { |
|
207 int a_len = a.length (); |
|
208 if (r < 0 || r >= rows () || c < 0 || c + a_len - 1 > cols ()) |
|
209 { |
|
210 (*current_liboctave_error_handler) ("range error for insert"); |
|
211 return *this; |
|
212 } |
|
213 |
|
214 for (int i = 0; i < a_len; i++) |
|
215 elem (r, c+i) = a.elem (i); |
|
216 |
|
217 return *this; |
|
218 } |
|
219 |
|
220 ComplexMatrix& |
|
221 ComplexMatrix::insert (const ComplexColumnVector& a, int r, int c) |
|
222 { |
|
223 int a_len = a.length (); |
|
224 if (r < 0 || r + a_len - 1 > rows () || c < 0 || c >= cols ()) |
|
225 { |
|
226 (*current_liboctave_error_handler) ("range error for insert"); |
|
227 return *this; |
|
228 } |
|
229 |
|
230 for (int i = 0; i < a_len; i++) |
|
231 elem (r+i, c) = a.elem (i); |
|
232 |
|
233 return *this; |
|
234 } |
|
235 |
|
236 ComplexMatrix& |
|
237 ComplexMatrix::insert (const ComplexDiagMatrix& a, int r, int c) |
|
238 { |
|
239 if (r < 0 || r + a.rows () - 1 > rows () |
|
240 || c < 0 || c + a.cols () - 1 > cols ()) |
|
241 { |
|
242 (*current_liboctave_error_handler) ("range error for insert"); |
|
243 return *this; |
|
244 } |
|
245 |
|
246 for (int i = 0; i < a.length (); i++) |
|
247 elem (r+i, c+i) = a.elem (i, i); |
|
248 |
|
249 return *this; |
|
250 } |
|
251 |
|
252 ComplexMatrix& |
|
253 ComplexMatrix::fill (double val) |
|
254 { |
|
255 int nr = rows (); |
|
256 int nc = cols (); |
|
257 if (nr > 0 && nc > 0) |
|
258 for (int j = 0; j < nc; j++) |
|
259 for (int i = 0; i < nr; i++) |
|
260 elem (i, j) = val; |
|
261 |
|
262 return *this; |
|
263 } |
|
264 |
|
265 ComplexMatrix& |
|
266 ComplexMatrix::fill (const Complex& val) |
|
267 { |
|
268 int nr = rows (); |
|
269 int nc = cols (); |
|
270 if (nr > 0 && nc > 0) |
|
271 for (int j = 0; j < nc; j++) |
|
272 for (int i = 0; i < nr; i++) |
|
273 elem (i, j) = val; |
|
274 |
|
275 return *this; |
|
276 } |
|
277 |
|
278 ComplexMatrix& |
|
279 ComplexMatrix::fill (double val, int r1, int c1, int r2, int c2) |
|
280 { |
|
281 int nr = rows (); |
|
282 int nc = cols (); |
|
283 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
|
284 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) |
|
285 { |
|
286 (*current_liboctave_error_handler) ("range error for fill"); |
|
287 return *this; |
|
288 } |
|
289 |
|
290 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
291 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
292 |
|
293 for (int j = c1; j <= c2; j++) |
|
294 for (int i = r1; i <= r2; i++) |
|
295 elem (i, j) = val; |
|
296 |
|
297 return *this; |
|
298 } |
|
299 |
|
300 ComplexMatrix& |
|
301 ComplexMatrix::fill (const Complex& val, int r1, int c1, int r2, int c2) |
|
302 { |
|
303 int nr = rows (); |
|
304 int nc = cols (); |
|
305 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
|
306 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) |
|
307 { |
|
308 (*current_liboctave_error_handler) ("range error for fill"); |
|
309 return *this; |
|
310 } |
|
311 |
|
312 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
313 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
314 |
|
315 for (int j = c1; j <= c2; j++) |
|
316 for (int i = r1; i <= r2; i++) |
|
317 elem (i, j) = val; |
|
318 |
|
319 return *this; |
|
320 } |
|
321 |
|
322 ComplexMatrix |
|
323 ComplexMatrix::append (const Matrix& a) const |
|
324 { |
|
325 int nr = rows (); |
|
326 int nc = cols (); |
|
327 if (nr != a.rows ()) |
|
328 { |
|
329 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
330 return *this; |
|
331 } |
|
332 |
|
333 int nc_insert = nc; |
|
334 ComplexMatrix retval (nr, nc + a.cols ()); |
|
335 retval.insert (*this, 0, 0); |
|
336 retval.insert (a, 0, nc_insert); |
|
337 return retval; |
|
338 } |
|
339 |
|
340 ComplexMatrix |
|
341 ComplexMatrix::append (const RowVector& a) const |
|
342 { |
|
343 int nr = rows (); |
|
344 int nc = cols (); |
|
345 if (nr != 1) |
|
346 { |
|
347 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
348 return *this; |
|
349 } |
|
350 |
|
351 int nc_insert = nc; |
|
352 ComplexMatrix retval (nr, nc + a.length ()); |
|
353 retval.insert (*this, 0, 0); |
|
354 retval.insert (a, 0, nc_insert); |
|
355 return retval; |
|
356 } |
|
357 |
|
358 ComplexMatrix |
|
359 ComplexMatrix::append (const ColumnVector& a) const |
|
360 { |
|
361 int nr = rows (); |
|
362 int nc = cols (); |
|
363 if (nr != a.length ()) |
|
364 { |
|
365 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
366 return *this; |
|
367 } |
|
368 |
|
369 int nc_insert = nc; |
|
370 ComplexMatrix retval (nr, nc + 1); |
|
371 retval.insert (*this, 0, 0); |
|
372 retval.insert (a, 0, nc_insert); |
|
373 return retval; |
|
374 } |
|
375 |
|
376 ComplexMatrix |
|
377 ComplexMatrix::append (const DiagMatrix& a) const |
|
378 { |
|
379 int nr = rows (); |
|
380 int nc = cols (); |
|
381 if (nr != a.rows ()) |
|
382 { |
|
383 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
384 return *this; |
|
385 } |
|
386 |
|
387 int nc_insert = nc; |
|
388 ComplexMatrix retval (nr, nc + a.cols ()); |
|
389 retval.insert (*this, 0, 0); |
|
390 retval.insert (a, 0, nc_insert); |
|
391 return retval; |
|
392 } |
|
393 |
|
394 ComplexMatrix |
|
395 ComplexMatrix::append (const ComplexMatrix& a) const |
|
396 { |
|
397 int nr = rows (); |
|
398 int nc = cols (); |
|
399 if (nr != a.rows ()) |
|
400 { |
|
401 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
402 return *this; |
|
403 } |
|
404 |
|
405 int nc_insert = nc; |
|
406 ComplexMatrix retval (nr, nc + a.cols ()); |
|
407 retval.insert (*this, 0, 0); |
|
408 retval.insert (a, 0, nc_insert); |
|
409 return retval; |
|
410 } |
|
411 |
|
412 ComplexMatrix |
|
413 ComplexMatrix::append (const ComplexRowVector& a) const |
|
414 { |
|
415 int nr = rows (); |
|
416 int nc = cols (); |
|
417 if (nr != 1) |
|
418 { |
|
419 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
420 return *this; |
|
421 } |
|
422 |
|
423 int nc_insert = nc; |
|
424 ComplexMatrix retval (nr, nc + a.length ()); |
|
425 retval.insert (*this, 0, 0); |
|
426 retval.insert (a, 0, nc_insert); |
|
427 return retval; |
|
428 } |
|
429 |
|
430 ComplexMatrix |
|
431 ComplexMatrix::append (const ComplexColumnVector& a) const |
|
432 { |
|
433 int nr = rows (); |
|
434 int nc = cols (); |
|
435 if (nr != a.length ()) |
|
436 { |
|
437 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
438 return *this; |
|
439 } |
|
440 |
|
441 int nc_insert = nc; |
|
442 ComplexMatrix retval (nr, nc + 1); |
|
443 retval.insert (*this, 0, 0); |
|
444 retval.insert (a, 0, nc_insert); |
|
445 return retval; |
|
446 } |
|
447 |
|
448 ComplexMatrix |
|
449 ComplexMatrix::append (const ComplexDiagMatrix& 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::stack (const Matrix& a) const |
|
468 { |
|
469 int nr = rows (); |
|
470 int nc = cols (); |
|
471 if (nc != a.cols ()) |
|
472 { |
|
473 (*current_liboctave_error_handler) |
|
474 ("column dimension mismatch for stack"); |
|
475 return *this; |
|
476 } |
|
477 |
|
478 int nr_insert = nr; |
|
479 ComplexMatrix retval (nr + a.rows (), nc); |
|
480 retval.insert (*this, 0, 0); |
|
481 retval.insert (a, nr_insert, 0); |
|
482 return retval; |
|
483 } |
|
484 |
|
485 ComplexMatrix |
|
486 ComplexMatrix::stack (const RowVector& a) const |
|
487 { |
|
488 int nr = rows (); |
|
489 int nc = cols (); |
|
490 if (nc != a.length ()) |
|
491 { |
|
492 (*current_liboctave_error_handler) |
|
493 ("column dimension mismatch for stack"); |
|
494 return *this; |
|
495 } |
|
496 |
|
497 int nr_insert = nr; |
|
498 ComplexMatrix retval (nr + 1, nc); |
|
499 retval.insert (*this, 0, 0); |
|
500 retval.insert (a, nr_insert, 0); |
|
501 return retval; |
|
502 } |
|
503 |
|
504 ComplexMatrix |
|
505 ComplexMatrix::stack (const ColumnVector& a) const |
|
506 { |
|
507 int nr = rows (); |
|
508 int nc = cols (); |
|
509 if (nc != 1) |
|
510 { |
|
511 (*current_liboctave_error_handler) |
|
512 ("column dimension mismatch for stack"); |
|
513 return *this; |
|
514 } |
|
515 |
|
516 int nr_insert = nr; |
|
517 ComplexMatrix retval (nr + a.length (), nc); |
|
518 retval.insert (*this, 0, 0); |
|
519 retval.insert (a, nr_insert, 0); |
|
520 return retval; |
|
521 } |
|
522 |
|
523 ComplexMatrix |
|
524 ComplexMatrix::stack (const DiagMatrix& a) const |
|
525 { |
|
526 int nr = rows (); |
|
527 int nc = cols (); |
|
528 if (nc != a.cols ()) |
|
529 { |
|
530 (*current_liboctave_error_handler) |
|
531 ("column dimension mismatch for stack"); |
|
532 return *this; |
|
533 } |
|
534 |
|
535 int nr_insert = nr; |
|
536 ComplexMatrix retval (nr + a.rows (), nc); |
|
537 retval.insert (*this, 0, 0); |
|
538 retval.insert (a, nr_insert, 0); |
|
539 return retval; |
|
540 } |
|
541 |
|
542 ComplexMatrix |
|
543 ComplexMatrix::stack (const ComplexMatrix& a) const |
|
544 { |
|
545 int nr = rows (); |
|
546 int nc = cols (); |
|
547 if (nc != a.cols ()) |
|
548 { |
|
549 (*current_liboctave_error_handler) |
|
550 ("column dimension mismatch for stack"); |
|
551 return *this; |
|
552 } |
|
553 |
|
554 int nr_insert = nr; |
|
555 ComplexMatrix retval (nr + a.rows (), nc); |
|
556 retval.insert (*this, 0, 0); |
|
557 retval.insert (a, nr_insert, 0); |
|
558 return retval; |
|
559 } |
|
560 |
|
561 ComplexMatrix |
|
562 ComplexMatrix::stack (const ComplexRowVector& a) const |
|
563 { |
|
564 int nr = rows (); |
|
565 int nc = cols (); |
|
566 if (nc != a.length ()) |
|
567 { |
|
568 (*current_liboctave_error_handler) |
|
569 ("column dimension mismatch for stack"); |
|
570 return *this; |
|
571 } |
|
572 |
|
573 int nr_insert = nr; |
|
574 ComplexMatrix retval (nr + 1, nc); |
|
575 retval.insert (*this, 0, 0); |
|
576 retval.insert (a, nr_insert, 0); |
|
577 return retval; |
|
578 } |
|
579 |
|
580 ComplexMatrix |
|
581 ComplexMatrix::stack (const ComplexColumnVector& a) const |
|
582 { |
|
583 int nr = rows (); |
|
584 int nc = cols (); |
|
585 if (nc != 1) |
|
586 { |
|
587 (*current_liboctave_error_handler) |
|
588 ("column dimension mismatch for stack"); |
|
589 return *this; |
|
590 } |
|
591 |
|
592 int nr_insert = nr; |
|
593 ComplexMatrix retval (nr + a.length (), nc); |
|
594 retval.insert (*this, 0, 0); |
|
595 retval.insert (a, nr_insert, 0); |
|
596 return retval; |
|
597 } |
|
598 |
|
599 ComplexMatrix |
|
600 ComplexMatrix::stack (const ComplexDiagMatrix& a) const |
|
601 { |
|
602 int nr = rows (); |
|
603 int nc = cols (); |
|
604 if (nc != a.cols ()) |
|
605 { |
|
606 (*current_liboctave_error_handler) |
|
607 ("column dimension mismatch for stack"); |
|
608 return *this; |
|
609 } |
|
610 |
|
611 int nr_insert = nr; |
|
612 ComplexMatrix retval (nr + a.rows (), nc); |
|
613 retval.insert (*this, 0, 0); |
|
614 retval.insert (a, nr_insert, 0); |
|
615 return retval; |
|
616 } |
|
617 |
|
618 ComplexMatrix |
|
619 ComplexMatrix::hermitian (void) const |
|
620 { |
|
621 int nr = rows (); |
|
622 int nc = cols (); |
|
623 ComplexMatrix result; |
|
624 if (length () > 0) |
|
625 { |
|
626 result.resize (nc, nr); |
|
627 for (int j = 0; j < nc; j++) |
|
628 for (int i = 0; i < nr; i++) |
|
629 result.elem (j, i) = conj (elem (i, j)); |
|
630 } |
|
631 return result; |
|
632 } |
|
633 |
|
634 ComplexMatrix |
|
635 ComplexMatrix::transpose (void) const |
|
636 { |
|
637 int nr = rows (); |
|
638 int nc = cols (); |
|
639 ComplexMatrix result (nc, nr); |
|
640 if (length () > 0) |
|
641 { |
|
642 for (int j = 0; j < nc; j++) |
|
643 for (int i = 0; i < nr; i++) |
|
644 result.elem (j, i) = elem (i, j); |
|
645 } |
|
646 return result; |
|
647 } |
|
648 |
|
649 ComplexMatrix |
|
650 conj (const ComplexMatrix& a) |
|
651 { |
|
652 int a_len = a.length (); |
|
653 ComplexMatrix retval; |
|
654 if (a_len > 0) |
|
655 retval = ComplexMatrix (conj_dup (a.data (), a_len), a.rows (), |
|
656 a.cols ()); |
|
657 return retval; |
|
658 } |
|
659 |
|
660 // resize is the destructive equivalent for this one |
|
661 |
|
662 ComplexMatrix |
|
663 ComplexMatrix::extract (int r1, int c1, int r2, int c2) const |
|
664 { |
|
665 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
666 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
667 |
|
668 int new_r = r2 - r1 + 1; |
|
669 int new_c = c2 - c1 + 1; |
|
670 |
|
671 ComplexMatrix result (new_r, new_c); |
|
672 |
|
673 for (int j = 0; j < new_c; j++) |
|
674 for (int i = 0; i < new_r; i++) |
|
675 result.elem (i, j) = elem (r1+i, c1+j); |
|
676 |
|
677 return result; |
|
678 } |
|
679 |
|
680 // extract row or column i. |
|
681 |
|
682 ComplexRowVector |
|
683 ComplexMatrix::row (int i) const |
|
684 { |
|
685 int nc = cols (); |
|
686 if (i < 0 || i >= rows ()) |
|
687 { |
|
688 (*current_liboctave_error_handler) ("invalid row selection"); |
|
689 return ComplexRowVector (); |
|
690 } |
|
691 |
|
692 ComplexRowVector retval (nc); |
|
693 for (int j = 0; j < cols (); j++) |
|
694 retval.elem (j) = elem (i, j); |
|
695 |
|
696 return retval; |
|
697 } |
|
698 |
|
699 ComplexRowVector |
|
700 ComplexMatrix::row (char *s) const |
|
701 { |
533
|
702 if (! s) |
458
|
703 { |
|
704 (*current_liboctave_error_handler) ("invalid row selection"); |
|
705 return ComplexRowVector (); |
|
706 } |
|
707 |
|
708 char c = *s; |
|
709 if (c == 'f' || c == 'F') |
|
710 return row (0); |
|
711 else if (c == 'l' || c == 'L') |
|
712 return row (rows () - 1); |
|
713 else |
|
714 { |
|
715 (*current_liboctave_error_handler) ("invalid row selection"); |
|
716 return ComplexRowVector (); |
|
717 } |
|
718 } |
|
719 |
|
720 ComplexColumnVector |
|
721 ComplexMatrix::column (int i) const |
|
722 { |
|
723 int nr = rows (); |
|
724 if (i < 0 || i >= cols ()) |
|
725 { |
|
726 (*current_liboctave_error_handler) ("invalid column selection"); |
|
727 return ComplexColumnVector (); |
|
728 } |
|
729 |
|
730 ComplexColumnVector retval (nr); |
|
731 for (int j = 0; j < nr; j++) |
|
732 retval.elem (j) = elem (j, i); |
|
733 |
|
734 return retval; |
|
735 } |
|
736 |
|
737 ComplexColumnVector |
|
738 ComplexMatrix::column (char *s) const |
|
739 { |
533
|
740 if (! s) |
458
|
741 { |
|
742 (*current_liboctave_error_handler) ("invalid column selection"); |
|
743 return ComplexColumnVector (); |
|
744 } |
|
745 |
|
746 char c = *s; |
|
747 if (c == 'f' || c == 'F') |
|
748 return column (0); |
|
749 else if (c == 'l' || c == 'L') |
|
750 return column (cols () - 1); |
|
751 else |
|
752 { |
|
753 (*current_liboctave_error_handler) ("invalid column selection"); |
|
754 return ComplexColumnVector (); |
|
755 } |
|
756 } |
|
757 |
|
758 ComplexMatrix |
|
759 ComplexMatrix::inverse (void) const |
|
760 { |
|
761 int info; |
479
|
762 double rcond; |
|
763 return inverse (info, rcond); |
458
|
764 } |
|
765 |
|
766 ComplexMatrix |
|
767 ComplexMatrix::inverse (int& info) const |
|
768 { |
|
769 double rcond; |
|
770 return inverse (info, rcond); |
|
771 } |
|
772 |
|
773 ComplexMatrix |
532
|
774 ComplexMatrix::inverse (int& info, double& rcond) const |
458
|
775 { |
|
776 int nr = rows (); |
|
777 int nc = cols (); |
|
778 int len = length (); |
|
779 if (nr != nc) |
|
780 { |
|
781 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
782 return ComplexMatrix (); |
|
783 } |
|
784 |
|
785 info = 0; |
|
786 |
|
787 int *ipvt = new int [nr]; |
|
788 Complex *z = new Complex [nr]; |
|
789 Complex *tmp_data = dup (data (), len); |
|
790 |
|
791 F77_FCN (zgeco) (tmp_data, &nr, &nc, ipvt, &rcond, z); |
|
792 |
1195
|
793 volatile double rcond_plus_one = rcond + 1.0; |
|
794 if (rcond_plus_one == 1.0) |
458
|
795 { |
|
796 info = -1; |
|
797 copy (tmp_data, data (), len); // Restore contents. |
|
798 } |
|
799 else |
|
800 { |
|
801 int job = 1; |
|
802 Complex dummy; |
|
803 |
|
804 F77_FCN (zgedi) (tmp_data, &nr, &nc, ipvt, &dummy, z, &job); |
|
805 } |
|
806 |
|
807 delete [] ipvt; |
|
808 delete [] z; |
|
809 |
|
810 return ComplexMatrix (tmp_data, nr, nc); |
|
811 } |
|
812 |
|
813 ComplexMatrix |
740
|
814 ComplexMatrix::pseudo_inverse (double tol) |
|
815 { |
|
816 ComplexSVD result (*this); |
|
817 |
|
818 DiagMatrix S = result.singular_values (); |
|
819 ComplexMatrix U = result.left_singular_matrix (); |
|
820 ComplexMatrix V = result.right_singular_matrix (); |
|
821 |
|
822 ColumnVector sigma = S.diag (); |
|
823 |
|
824 int r = sigma.length () - 1; |
|
825 int nr = rows (); |
|
826 int nc = cols (); |
|
827 |
|
828 if (tol <= 0.0) |
|
829 { |
|
830 if (nr > nc) |
|
831 tol = nr * sigma.elem (0) * DBL_EPSILON; |
|
832 else |
|
833 tol = nc * sigma.elem (0) * DBL_EPSILON; |
|
834 } |
|
835 |
|
836 while (r >= 0 && sigma.elem (r) < tol) |
|
837 r--; |
|
838 |
|
839 if (r < 0) |
|
840 return ComplexMatrix (nc, nr, 0.0); |
|
841 else |
|
842 { |
|
843 ComplexMatrix Ur = U.extract (0, 0, nr-1, r); |
|
844 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); |
|
845 ComplexMatrix Vr = V.extract (0, 0, nc-1, r); |
|
846 return Vr * D * Ur.hermitian (); |
|
847 } |
|
848 } |
|
849 |
|
850 ComplexMatrix |
458
|
851 ComplexMatrix::fourier (void) const |
|
852 { |
|
853 int nr = rows (); |
|
854 int nc = cols (); |
|
855 int npts, nsamples; |
|
856 if (nr == 1 || nc == 1) |
|
857 { |
|
858 npts = nr > nc ? nr : nc; |
|
859 nsamples = 1; |
|
860 } |
|
861 else |
|
862 { |
|
863 npts = nr; |
|
864 nsamples = nc; |
|
865 } |
|
866 |
|
867 int nn = 4*npts+15; |
|
868 Complex *wsave = new Complex [nn]; |
|
869 Complex *tmp_data = dup (data (), length ()); |
|
870 |
|
871 F77_FCN (cffti) (&npts, wsave); |
|
872 |
|
873 for (int j = 0; j < nsamples; j++) |
|
874 F77_FCN (cfftf) (&npts, &tmp_data[npts*j], wsave); |
|
875 |
|
876 delete [] wsave; |
|
877 |
|
878 return ComplexMatrix (tmp_data, nr, nc); |
|
879 } |
|
880 |
|
881 ComplexMatrix |
|
882 ComplexMatrix::ifourier (void) const |
|
883 { |
|
884 int nr = rows (); |
|
885 int nc = cols (); |
|
886 int npts, nsamples; |
|
887 if (nr == 1 || nc == 1) |
|
888 { |
|
889 npts = nr > nc ? nr : nc; |
|
890 nsamples = 1; |
|
891 } |
|
892 else |
|
893 { |
|
894 npts = nr; |
|
895 nsamples = nc; |
|
896 } |
|
897 |
|
898 int nn = 4*npts+15; |
|
899 Complex *wsave = new Complex [nn]; |
|
900 Complex *tmp_data = dup (data (), length ()); |
|
901 |
|
902 F77_FCN (cffti) (&npts, wsave); |
|
903 |
|
904 for (int j = 0; j < nsamples; j++) |
|
905 F77_FCN (cfftb) (&npts, &tmp_data[npts*j], wsave); |
|
906 |
|
907 for (j = 0; j < npts*nsamples; j++) |
|
908 tmp_data[j] = tmp_data[j] / (double) npts; |
|
909 |
|
910 delete [] wsave; |
|
911 |
|
912 return ComplexMatrix (tmp_data, nr, nc); |
|
913 } |
|
914 |
677
|
915 ComplexMatrix |
|
916 ComplexMatrix::fourier2d (void) const |
|
917 { |
|
918 int nr = rows (); |
|
919 int nc = cols (); |
|
920 int npts, nsamples; |
|
921 if (nr == 1 || nc == 1) |
|
922 { |
|
923 npts = nr > nc ? nr : nc; |
|
924 nsamples = 1; |
|
925 } |
|
926 else |
|
927 { |
|
928 npts = nr; |
|
929 nsamples = nc; |
|
930 } |
|
931 |
|
932 int nn = 4*npts+15; |
|
933 Complex *wsave = new Complex [nn]; |
|
934 Complex *tmp_data = dup (data (), length ()); |
|
935 |
|
936 F77_FCN (cffti) (&npts, wsave); |
|
937 |
|
938 for (int j = 0; j < nsamples; j++) |
|
939 F77_FCN (cfftf) (&npts, &tmp_data[npts*j], wsave); |
|
940 |
|
941 delete [] wsave; |
|
942 |
|
943 npts = nc; |
|
944 nsamples = nr; |
|
945 nn = 4*npts+15; |
|
946 wsave = new Complex [nn]; |
|
947 Complex *row = new Complex[npts]; |
|
948 |
|
949 F77_FCN (cffti) (&npts, wsave); |
|
950 |
|
951 for (j = 0; j < nsamples; j++) |
|
952 { |
|
953 for (int i = 0; i < npts; i++) |
|
954 row[i] = tmp_data[i*nr + j]; |
|
955 |
|
956 F77_FCN (cfftf) (&npts, row, wsave); |
|
957 |
|
958 for (i = 0; i < npts; i++) |
|
959 tmp_data[i*nr + j] = row[i]; |
|
960 } |
|
961 |
|
962 delete [] wsave; |
|
963 delete [] row; |
|
964 |
|
965 return ComplexMatrix (tmp_data, nr, nc); |
|
966 } |
|
967 |
|
968 ComplexMatrix |
|
969 ComplexMatrix::ifourier2d (void) const |
|
970 { |
|
971 int nr = rows (); |
|
972 int nc = cols (); |
|
973 int npts, nsamples; |
|
974 if (nr == 1 || nc == 1) |
|
975 { |
|
976 npts = nr > nc ? nr : nc; |
|
977 nsamples = 1; |
|
978 } |
|
979 else |
|
980 { |
|
981 npts = nr; |
|
982 nsamples = nc; |
|
983 } |
|
984 |
|
985 int nn = 4*npts+15; |
|
986 Complex *wsave = new Complex [nn]; |
|
987 Complex *tmp_data = dup (data (), length ()); |
|
988 |
|
989 F77_FCN (cffti) (&npts, wsave); |
|
990 |
|
991 for (int j = 0; j < nsamples; j++) |
|
992 F77_FCN (cfftb) (&npts, &tmp_data[npts*j], wsave); |
|
993 |
|
994 delete [] wsave; |
|
995 |
|
996 for (j = 0; j < npts*nsamples; j++) |
|
997 tmp_data[j] = tmp_data[j] / (double) npts; |
|
998 |
|
999 npts = nc; |
|
1000 nsamples = nr; |
|
1001 nn = 4*npts+15; |
|
1002 wsave = new Complex [nn]; |
|
1003 Complex *row = new Complex[npts]; |
|
1004 |
|
1005 F77_FCN (cffti) (&npts, wsave); |
|
1006 |
|
1007 for (j = 0; j < nsamples; j++) |
|
1008 { |
|
1009 for (int i = 0; i < npts; i++) |
|
1010 row[i] = tmp_data[i*nr + j]; |
|
1011 |
|
1012 F77_FCN (cfftb) (&npts, row, wsave); |
|
1013 |
|
1014 for (i = 0; i < npts; i++) |
|
1015 tmp_data[i*nr + j] = row[i] / (double) npts; |
|
1016 } |
|
1017 |
|
1018 delete [] wsave; |
|
1019 delete [] row; |
|
1020 |
|
1021 return ComplexMatrix (tmp_data, nr, nc); |
|
1022 } |
|
1023 |
458
|
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 |
1195
|
1064 volatile double rcond_plus_one = rcond + 1.0; |
|
1065 if (rcond_plus_one == 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 |
1195
|
1146 volatile double rcond_plus_one = rcond + 1.0; |
|
1147 if (rcond_plus_one == 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 |
1195
|
1209 volatile double rcond_plus_one = rcond + 1.0; |
|
1210 if (rcond_plus_one == 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 |
1205
|
1377 // column vector by row vector -> matrix operations |
|
1378 |
|
1379 ComplexMatrix |
|
1380 operator * (const ColumnVector& v, const ComplexRowVector& a) |
|
1381 { |
|
1382 ComplexColumnVector tmp (v); |
|
1383 return tmp * a; |
|
1384 } |
|
1385 |
|
1386 ComplexMatrix |
|
1387 operator * (const ComplexColumnVector& a, const RowVector& b) |
|
1388 { |
|
1389 ComplexRowVector tmp (b); |
|
1390 return a * tmp; |
|
1391 } |
|
1392 |
|
1393 ComplexMatrix |
|
1394 operator * (const ComplexColumnVector& v, const ComplexRowVector& a) |
|
1395 { |
|
1396 int len = v.length (); |
|
1397 int a_len = a.length (); |
|
1398 if (len != a_len) |
|
1399 { |
|
1400 (*current_liboctave_error_handler) |
|
1401 ("nonconformant vector multiplication attempted"); |
|
1402 return ComplexMatrix (); |
|
1403 } |
|
1404 |
|
1405 if (len == 0) |
|
1406 return ComplexMatrix (len, len, 0.0); |
|
1407 |
|
1408 char transa = 'N'; |
|
1409 char transb = 'N'; |
|
1410 Complex alpha (1.0); |
|
1411 Complex beta (0.0); |
|
1412 int anr = 1; |
|
1413 |
|
1414 Complex *c = new Complex [len * a_len]; |
|
1415 |
|
1416 F77_FCN (zgemm) (&transa, &transb, &len, &a_len, &anr, &alpha, |
|
1417 v.data (), &len, a.data (), &anr, &beta, c, &len, |
|
1418 1L, 1L); |
|
1419 |
|
1420 return ComplexMatrix (c, len, a_len); |
|
1421 } |
|
1422 |
|
1423 // diagonal matrix by scalar -> matrix operations |
|
1424 |
|
1425 ComplexMatrix |
|
1426 operator + (const DiagMatrix& a, const Complex& s) |
|
1427 { |
|
1428 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1429 return a + tmp; |
|
1430 } |
|
1431 |
|
1432 ComplexMatrix |
|
1433 operator - (const DiagMatrix& a, const Complex& s) |
|
1434 { |
|
1435 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1436 return a + tmp; |
|
1437 } |
|
1438 |
|
1439 ComplexMatrix |
|
1440 operator + (const ComplexDiagMatrix& a, double s) |
|
1441 { |
|
1442 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1443 return a + tmp; |
|
1444 } |
|
1445 |
|
1446 ComplexMatrix |
|
1447 operator - (const ComplexDiagMatrix& a, double s) |
|
1448 { |
|
1449 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1450 return a + tmp; |
|
1451 } |
|
1452 |
|
1453 ComplexMatrix |
|
1454 operator + (const ComplexDiagMatrix& a, const Complex& s) |
|
1455 { |
|
1456 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1457 return a + tmp; |
|
1458 } |
|
1459 |
|
1460 ComplexMatrix |
|
1461 operator - (const ComplexDiagMatrix& a, const Complex& s) |
|
1462 { |
|
1463 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1464 return a + tmp; |
|
1465 } |
|
1466 |
|
1467 // scalar by diagonal matrix -> matrix operations |
|
1468 |
|
1469 ComplexMatrix |
|
1470 operator + (const Complex& s, const DiagMatrix& a) |
|
1471 { |
|
1472 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1473 return tmp + a; |
|
1474 } |
|
1475 |
|
1476 ComplexMatrix |
|
1477 operator - (const Complex& s, const DiagMatrix& a) |
|
1478 { |
|
1479 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1480 return tmp - a; |
|
1481 } |
|
1482 |
|
1483 ComplexMatrix |
|
1484 operator + (double s, const ComplexDiagMatrix& a) |
|
1485 { |
|
1486 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1487 return tmp + a; |
|
1488 } |
|
1489 |
|
1490 ComplexMatrix |
|
1491 operator - (double s, const ComplexDiagMatrix& a) |
|
1492 { |
|
1493 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1494 return tmp - a; |
|
1495 } |
|
1496 |
|
1497 ComplexMatrix |
|
1498 operator + (const Complex& s, const ComplexDiagMatrix& a) |
|
1499 { |
|
1500 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1501 return tmp + a; |
|
1502 } |
|
1503 |
|
1504 ComplexMatrix |
|
1505 operator - (const Complex& s, const ComplexDiagMatrix& a) |
|
1506 { |
|
1507 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1508 return tmp - a; |
|
1509 } |
|
1510 |
458
|
1511 // matrix by diagonal matrix -> matrix operations |
|
1512 |
|
1513 ComplexMatrix& |
|
1514 ComplexMatrix::operator += (const DiagMatrix& 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"); |
889
|
1522 return *this; |
458
|
1523 } |
|
1524 |
|
1525 for (int i = 0; i < a.length (); i++) |
|
1526 elem (i, i) += a.elem (i, i); |
|
1527 |
|
1528 return *this; |
|
1529 } |
|
1530 |
|
1531 ComplexMatrix& |
|
1532 ComplexMatrix::operator -= (const DiagMatrix& a) |
|
1533 { |
|
1534 int nr = rows (); |
|
1535 int nc = cols (); |
|
1536 if (nr != a.rows () || nc != a.cols ()) |
|
1537 { |
|
1538 (*current_liboctave_error_handler) |
|
1539 ("nonconformant matrix -= operation attempted"); |
889
|
1540 return *this; |
458
|
1541 } |
|
1542 |
|
1543 for (int i = 0; i < a.length (); i++) |
|
1544 elem (i, i) -= a.elem (i, i); |
|
1545 |
|
1546 return *this; |
|
1547 } |
|
1548 |
|
1549 ComplexMatrix& |
|
1550 ComplexMatrix::operator += (const ComplexDiagMatrix& a) |
|
1551 { |
|
1552 int nr = rows (); |
|
1553 int nc = cols (); |
|
1554 if (nr != a.rows () || nc != a.cols ()) |
|
1555 { |
|
1556 (*current_liboctave_error_handler) |
|
1557 ("nonconformant matrix += operation attempted"); |
889
|
1558 return *this; |
458
|
1559 } |
|
1560 |
|
1561 for (int i = 0; i < a.length (); i++) |
|
1562 elem (i, i) += a.elem (i, i); |
|
1563 |
|
1564 return *this; |
|
1565 } |
|
1566 |
|
1567 ComplexMatrix& |
|
1568 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) |
|
1569 { |
|
1570 int nr = rows (); |
|
1571 int nc = cols (); |
|
1572 if (nr != a.rows () || nc != a.cols ()) |
|
1573 { |
|
1574 (*current_liboctave_error_handler) |
|
1575 ("nonconformant matrix -= operation attempted"); |
889
|
1576 return *this; |
458
|
1577 } |
|
1578 |
|
1579 for (int i = 0; i < a.length (); i++) |
|
1580 elem (i, i) -= a.elem (i, i); |
|
1581 |
|
1582 return *this; |
|
1583 } |
|
1584 |
1205
|
1585 ComplexMatrix |
|
1586 operator + (const Matrix& m, const ComplexDiagMatrix& a) |
|
1587 { |
|
1588 int nr = m.rows (); |
|
1589 int nc = m.cols (); |
|
1590 if (nr != a.rows () || nc != a.cols ()) |
|
1591 { |
|
1592 (*current_liboctave_error_handler) |
|
1593 ("nonconformant matrix addition attempted"); |
|
1594 return ComplexMatrix (); |
|
1595 } |
|
1596 |
|
1597 if (nr == 0 || nc == 0) |
|
1598 return ComplexMatrix (nr, nc); |
|
1599 |
|
1600 ComplexMatrix result (m); |
|
1601 for (int i = 0; i < a.length (); i++) |
|
1602 result.elem (i, i) += a.elem (i, i); |
|
1603 |
|
1604 return result; |
|
1605 } |
|
1606 |
|
1607 ComplexMatrix |
|
1608 operator - (const Matrix& m, const ComplexDiagMatrix& a) |
|
1609 { |
|
1610 int nr = m.rows (); |
|
1611 int nc = m.cols (); |
|
1612 if (nr != a.rows () || nc != a.cols ()) |
|
1613 { |
|
1614 (*current_liboctave_error_handler) |
|
1615 ("nonconformant matrix subtraction attempted"); |
|
1616 return ComplexMatrix (); |
|
1617 } |
|
1618 |
|
1619 if (nr == 0 || nc == 0) |
|
1620 return ComplexMatrix (nr, nc); |
|
1621 |
|
1622 ComplexMatrix result (m); |
|
1623 for (int i = 0; i < a.length (); i++) |
|
1624 result.elem (i, i) -= a.elem (i, i); |
|
1625 |
|
1626 return result; |
|
1627 } |
|
1628 |
|
1629 ComplexMatrix |
|
1630 operator * (const Matrix& m, const ComplexDiagMatrix& a) |
|
1631 { |
|
1632 int nr = m.rows (); |
|
1633 int nc = m.cols (); |
|
1634 int a_nr = a.rows (); |
|
1635 int a_nc = a.cols (); |
|
1636 if (nc != a_nr) |
|
1637 { |
|
1638 (*current_liboctave_error_handler) |
|
1639 ("nonconformant matrix multiplication attempted"); |
|
1640 return ComplexMatrix (); |
|
1641 } |
|
1642 |
|
1643 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1644 return ComplexMatrix (nr, a_nc, 0.0); |
|
1645 |
|
1646 Complex *c = new Complex [nr*a_nc]; |
|
1647 Complex *ctmp = 0; |
|
1648 |
|
1649 for (int j = 0; j < a.length (); j++) |
|
1650 { |
|
1651 int idx = j * nr; |
|
1652 ctmp = c + idx; |
|
1653 if (a.elem (j, j) == 1.0) |
|
1654 { |
|
1655 for (int i = 0; i < nr; i++) |
|
1656 ctmp[i] = m.elem (i, j); |
|
1657 } |
|
1658 else if (a.elem (j, j) == 0.0) |
|
1659 { |
|
1660 for (int i = 0; i < nr; i++) |
|
1661 ctmp[i] = 0.0; |
|
1662 } |
|
1663 else |
|
1664 { |
|
1665 for (int i = 0; i < nr; i++) |
|
1666 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
1667 } |
|
1668 } |
|
1669 |
|
1670 if (a_nr < a_nc) |
|
1671 { |
|
1672 for (int i = nr * nc; i < nr * a_nc; i++) |
|
1673 ctmp[i] = 0.0; |
|
1674 } |
|
1675 |
|
1676 return ComplexMatrix (c, nr, a_nc); |
|
1677 } |
|
1678 |
|
1679 // diagonal matrix by matrix -> matrix operations |
|
1680 |
|
1681 ComplexMatrix |
|
1682 operator + (const DiagMatrix& m, const ComplexMatrix& a) |
|
1683 { |
|
1684 int nr = m.rows (); |
|
1685 int nc = m.cols (); |
|
1686 if (nr != a.rows () || nc != a.cols ()) |
|
1687 { |
|
1688 (*current_liboctave_error_handler) |
|
1689 ("nonconformant matrix addition attempted"); |
|
1690 return ComplexMatrix (); |
|
1691 } |
|
1692 |
|
1693 if (nr == 0 || nc == 0) |
|
1694 return ComplexMatrix (nr, nc); |
|
1695 |
|
1696 ComplexMatrix result (a); |
|
1697 for (int i = 0; i < m.length (); i++) |
|
1698 result.elem (i, i) += m.elem (i, i); |
|
1699 |
|
1700 return result; |
|
1701 } |
|
1702 |
|
1703 ComplexMatrix |
|
1704 operator - (const DiagMatrix& m, const ComplexMatrix& a) |
|
1705 { |
|
1706 int nr = m.rows (); |
|
1707 int nc = m.cols (); |
|
1708 if (nr != a.rows () || nc != a.cols ()) |
|
1709 { |
|
1710 (*current_liboctave_error_handler) |
|
1711 ("nonconformant matrix subtraction attempted"); |
|
1712 return ComplexMatrix (); |
|
1713 } |
|
1714 |
|
1715 if (nr == 0 || nc == 0) |
|
1716 return ComplexMatrix (nr, nc); |
|
1717 |
|
1718 ComplexMatrix result (-a); |
|
1719 for (int i = 0; i < m.length (); i++) |
|
1720 result.elem (i, i) += m.elem (i, i); |
|
1721 |
|
1722 return result; |
|
1723 } |
|
1724 |
|
1725 ComplexMatrix |
|
1726 operator * (const DiagMatrix& m, const ComplexMatrix& a) |
|
1727 { |
|
1728 int nr = m.rows (); |
|
1729 int nc = m.cols (); |
|
1730 int a_nr = a.rows (); |
|
1731 int a_nc = a.cols (); |
|
1732 if (nc != a_nr) |
|
1733 { |
|
1734 (*current_liboctave_error_handler) |
|
1735 ("nonconformant matrix multiplication attempted"); |
|
1736 return ComplexMatrix (); |
|
1737 } |
|
1738 |
|
1739 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1740 return ComplexMatrix (nr, nc, 0.0); |
|
1741 |
|
1742 ComplexMatrix c (nr, a_nc); |
|
1743 |
|
1744 for (int i = 0; i < m.length (); i++) |
|
1745 { |
|
1746 if (m.elem (i, i) == 1.0) |
|
1747 { |
|
1748 for (int j = 0; j < a_nc; j++) |
|
1749 c.elem (i, j) = a.elem (i, j); |
|
1750 } |
|
1751 else if (m.elem (i, i) == 0.0) |
|
1752 { |
|
1753 for (int j = 0; j < a_nc; j++) |
|
1754 c.elem (i, j) = 0.0; |
|
1755 } |
|
1756 else |
|
1757 { |
|
1758 for (int j = 0; j < a_nc; j++) |
|
1759 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
1760 } |
|
1761 } |
|
1762 |
|
1763 if (nr > nc) |
|
1764 { |
|
1765 for (int j = 0; j < a_nc; j++) |
|
1766 for (int i = a_nr; i < nr; i++) |
|
1767 c.elem (i, j) = 0.0; |
|
1768 } |
|
1769 |
|
1770 return c; |
|
1771 } |
|
1772 |
|
1773 ComplexMatrix |
|
1774 operator + (const ComplexDiagMatrix& m, const Matrix& a) |
|
1775 { |
|
1776 int nr = m.rows (); |
|
1777 int nc = m.cols (); |
|
1778 if (nr != a.rows () || nc != a.cols ()) |
|
1779 { |
|
1780 (*current_liboctave_error_handler) |
|
1781 ("nonconformant matrix addition attempted"); |
|
1782 return ComplexMatrix (); |
|
1783 } |
|
1784 |
|
1785 if (nr == 0 || nc == 0) |
|
1786 return ComplexMatrix (nr, nc); |
|
1787 |
|
1788 ComplexMatrix result (a); |
|
1789 for (int i = 0; i < m.length (); i++) |
|
1790 result.elem (i, i) += m.elem (i, i); |
|
1791 |
|
1792 return result; |
|
1793 } |
|
1794 |
|
1795 ComplexMatrix |
|
1796 operator - (const ComplexDiagMatrix& m, const Matrix& a) |
|
1797 { |
|
1798 int nr = m.rows (); |
|
1799 int nc = m.cols (); |
|
1800 if (nr != a.rows () || nc != a.cols ()) |
|
1801 { |
|
1802 (*current_liboctave_error_handler) |
|
1803 ("nonconformant matrix subtraction attempted"); |
|
1804 return ComplexMatrix (); |
|
1805 } |
|
1806 |
|
1807 if (nr == 0 || nc == 0) |
|
1808 return ComplexMatrix (nr, nc); |
|
1809 |
|
1810 ComplexMatrix result (-a); |
|
1811 for (int i = 0; i < m.length (); i++) |
|
1812 result.elem (i, i) += m.elem (i, i); |
|
1813 |
|
1814 return result; |
|
1815 } |
|
1816 |
|
1817 ComplexMatrix |
|
1818 operator * (const ComplexDiagMatrix& m, const Matrix& a) |
|
1819 { |
|
1820 int nr = m.rows (); |
|
1821 int nc = m.cols (); |
|
1822 int a_nr = a.rows (); |
|
1823 int a_nc = a.cols (); |
|
1824 if (nc != a_nr) |
|
1825 { |
|
1826 (*current_liboctave_error_handler) |
|
1827 ("nonconformant matrix multiplication attempted"); |
|
1828 return ComplexMatrix (); |
|
1829 } |
|
1830 |
|
1831 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1832 return ComplexMatrix (nr, a_nc, 0.0); |
|
1833 |
|
1834 ComplexMatrix c (nr, a_nc); |
|
1835 |
|
1836 for (int i = 0; i < m.length (); i++) |
|
1837 { |
|
1838 if (m.elem (i, i) == 1.0) |
|
1839 { |
|
1840 for (int j = 0; j < a_nc; j++) |
|
1841 c.elem (i, j) = a.elem (i, j); |
|
1842 } |
|
1843 else if (m.elem (i, i) == 0.0) |
|
1844 { |
|
1845 for (int j = 0; j < a_nc; j++) |
|
1846 c.elem (i, j) = 0.0; |
|
1847 } |
|
1848 else |
|
1849 { |
|
1850 for (int j = 0; j < a_nc; j++) |
|
1851 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
1852 } |
|
1853 } |
|
1854 |
|
1855 if (nr > nc) |
|
1856 { |
|
1857 for (int j = 0; j < a_nc; j++) |
|
1858 for (int i = a_nr; i < nr; i++) |
|
1859 c.elem (i, j) = 0.0; |
|
1860 } |
|
1861 |
|
1862 return c; |
|
1863 } |
|
1864 |
|
1865 ComplexMatrix |
|
1866 operator + (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
1867 { |
|
1868 int nr = m.rows (); |
|
1869 int nc = m.cols (); |
|
1870 if (nr != a.rows () || nc != a.cols ()) |
|
1871 { |
|
1872 (*current_liboctave_error_handler) |
|
1873 ("nonconformant matrix addition attempted"); |
|
1874 return ComplexMatrix (); |
|
1875 } |
|
1876 |
|
1877 if (nr == 0 || nc == 0) |
|
1878 return ComplexMatrix (nr, nc); |
|
1879 |
|
1880 ComplexMatrix result (a); |
|
1881 for (int i = 0; i < m.length (); i++) |
|
1882 result.elem (i, i) += m.elem (i, i); |
|
1883 |
|
1884 return result; |
|
1885 } |
|
1886 |
|
1887 ComplexMatrix |
|
1888 operator - (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
1889 { |
|
1890 int nr = m.rows (); |
|
1891 int nc = m.cols (); |
|
1892 if (nr != a.rows () || nc != a.cols ()) |
|
1893 { |
|
1894 (*current_liboctave_error_handler) |
|
1895 ("nonconformant matrix subtraction attempted"); |
|
1896 return ComplexMatrix (); |
|
1897 } |
|
1898 |
|
1899 if (nr == 0 || nc == 0) |
|
1900 return ComplexMatrix (nr, nc); |
|
1901 |
|
1902 ComplexMatrix result (-a); |
|
1903 for (int i = 0; i < m.length (); i++) |
|
1904 result.elem (i, i) += m.elem (i, i); |
|
1905 |
|
1906 return result; |
|
1907 } |
|
1908 |
|
1909 ComplexMatrix |
|
1910 operator * (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
1911 { |
|
1912 int nr = m.rows (); |
|
1913 int nc = m.cols (); |
|
1914 int a_nr = a.rows (); |
|
1915 int a_nc = a.cols (); |
|
1916 if (nc != a_nr) |
|
1917 { |
|
1918 (*current_liboctave_error_handler) |
|
1919 ("nonconformant matrix multiplication attempted"); |
|
1920 return ComplexMatrix (); |
|
1921 } |
|
1922 |
|
1923 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1924 return ComplexMatrix (nr, a_nc, 0.0); |
|
1925 |
|
1926 ComplexMatrix c (nr, a_nc); |
|
1927 |
|
1928 for (int i = 0; i < m.length (); i++) |
|
1929 { |
|
1930 if (m.elem (i, i) == 1.0) |
|
1931 { |
|
1932 for (int j = 0; j < a_nc; j++) |
|
1933 c.elem (i, j) = a.elem (i, j); |
|
1934 } |
|
1935 else if (m.elem (i, i) == 0.0) |
|
1936 { |
|
1937 for (int j = 0; j < a_nc; j++) |
|
1938 c.elem (i, j) = 0.0; |
|
1939 } |
|
1940 else |
|
1941 { |
|
1942 for (int j = 0; j < a_nc; j++) |
|
1943 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
1944 } |
|
1945 } |
|
1946 |
|
1947 if (nr > nc) |
|
1948 { |
|
1949 for (int j = 0; j < a_nc; j++) |
|
1950 for (int i = a_nr; i < nr; i++) |
|
1951 c.elem (i, j) = 0.0; |
|
1952 } |
|
1953 |
|
1954 return c; |
|
1955 } |
|
1956 |
458
|
1957 // matrix by matrix -> matrix operations |
|
1958 |
|
1959 ComplexMatrix& |
|
1960 ComplexMatrix::operator += (const Matrix& a) |
|
1961 { |
|
1962 int nr = rows (); |
|
1963 int nc = cols (); |
|
1964 if (nr != a.rows () || nc != a.cols ()) |
|
1965 { |
|
1966 (*current_liboctave_error_handler) |
|
1967 ("nonconformant matrix += operation attempted"); |
|
1968 return *this; |
|
1969 } |
|
1970 |
|
1971 if (nr == 0 || nc == 0) |
|
1972 return *this; |
|
1973 |
|
1974 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1975 |
|
1976 add2 (d, a.data (), length ()); |
|
1977 return *this; |
|
1978 } |
|
1979 |
|
1980 ComplexMatrix& |
|
1981 ComplexMatrix::operator -= (const Matrix& a) |
|
1982 { |
|
1983 int nr = rows (); |
|
1984 int nc = cols (); |
|
1985 if (nr != a.rows () || nc != a.cols ()) |
|
1986 { |
|
1987 (*current_liboctave_error_handler) |
|
1988 ("nonconformant matrix -= operation attempted"); |
|
1989 return *this; |
|
1990 } |
|
1991 |
|
1992 if (nr == 0 || nc == 0) |
|
1993 return *this; |
|
1994 |
|
1995 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1996 |
|
1997 subtract2 (d, a.data (), length ()); |
|
1998 return *this; |
|
1999 } |
|
2000 |
|
2001 ComplexMatrix& |
|
2002 ComplexMatrix::operator += (const ComplexMatrix& a) |
|
2003 { |
|
2004 int nr = rows (); |
|
2005 int nc = cols (); |
|
2006 if (nr != a.rows () || nc != a.cols ()) |
|
2007 { |
|
2008 (*current_liboctave_error_handler) |
|
2009 ("nonconformant matrix += operation attempted"); |
|
2010 return *this; |
|
2011 } |
|
2012 |
|
2013 if (nr == 0 || nc == 0) |
|
2014 return *this; |
|
2015 |
|
2016 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2017 |
|
2018 add2 (d, a.data (), length ()); |
|
2019 return *this; |
|
2020 } |
|
2021 |
|
2022 ComplexMatrix& |
|
2023 ComplexMatrix::operator -= (const ComplexMatrix& a) |
|
2024 { |
|
2025 int nr = rows (); |
|
2026 int nc = cols (); |
|
2027 if (nr != a.rows () || nc != a.cols ()) |
|
2028 { |
|
2029 (*current_liboctave_error_handler) |
|
2030 ("nonconformant matrix -= operation attempted"); |
|
2031 return *this; |
|
2032 } |
|
2033 |
|
2034 if (nr == 0 || nc == 0) |
|
2035 return *this; |
|
2036 |
|
2037 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2038 |
|
2039 subtract2 (d, a.data (), length ()); |
|
2040 return *this; |
|
2041 } |
|
2042 |
|
2043 // unary operations |
|
2044 |
|
2045 Matrix |
|
2046 ComplexMatrix::operator ! (void) const |
|
2047 { |
|
2048 return Matrix (not (data (), length ()), rows (), cols ()); |
|
2049 } |
|
2050 |
|
2051 // matrix by scalar -> matrix operations |
|
2052 |
|
2053 ComplexMatrix |
1205
|
2054 operator + (const Matrix& a, const Complex& s) |
|
2055 { |
|
2056 return ComplexMatrix (add (a.data (), a.length (), s), |
|
2057 a.rows (), a.cols ()); |
|
2058 } |
|
2059 |
|
2060 ComplexMatrix |
|
2061 operator - (const Matrix& a, const Complex& s) |
|
2062 { |
|
2063 return ComplexMatrix (subtract (a.data (), a.length (), s), |
|
2064 a.rows (), a.cols ()); |
|
2065 } |
|
2066 |
|
2067 ComplexMatrix |
|
2068 operator * (const Matrix& a, const Complex& s) |
|
2069 { |
|
2070 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2071 a.rows (), a.cols ()); |
|
2072 } |
|
2073 |
|
2074 ComplexMatrix |
|
2075 operator / (const Matrix& a, const Complex& s) |
|
2076 { |
|
2077 return ComplexMatrix (divide (a.data (), a.length (), s), |
|
2078 a.rows (), a.cols ()); |
|
2079 } |
|
2080 |
|
2081 ComplexMatrix |
458
|
2082 operator + (const ComplexMatrix& a, double s) |
|
2083 { |
|
2084 return ComplexMatrix (add (a.data (), a.length (), s), |
|
2085 a.rows (), a.cols ()); |
|
2086 } |
|
2087 |
|
2088 ComplexMatrix |
|
2089 operator - (const ComplexMatrix& a, double s) |
|
2090 { |
|
2091 return ComplexMatrix (subtract (a.data (), a.length (), s), |
|
2092 a.rows (), a.cols ()); |
|
2093 } |
|
2094 |
|
2095 ComplexMatrix |
|
2096 operator * (const ComplexMatrix& a, double s) |
|
2097 { |
|
2098 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2099 a.rows (), a.cols ()); |
|
2100 } |
|
2101 |
|
2102 ComplexMatrix |
|
2103 operator / (const ComplexMatrix& a, double s) |
|
2104 { |
|
2105 return ComplexMatrix (divide (a.data (), a.length (), s), |
|
2106 a.rows (), a.cols ()); |
|
2107 } |
|
2108 |
|
2109 // scalar by matrix -> matrix operations |
|
2110 |
|
2111 ComplexMatrix |
|
2112 operator + (double s, const ComplexMatrix& a) |
|
2113 { |
|
2114 return ComplexMatrix (add (a.data (), a.length (), s), a.rows (), |
|
2115 a.cols ()); |
|
2116 } |
|
2117 |
|
2118 ComplexMatrix |
|
2119 operator - (double s, const ComplexMatrix& a) |
|
2120 { |
|
2121 return ComplexMatrix (subtract (s, a.data (), a.length ()), |
|
2122 a.rows (), a.cols ()); |
|
2123 } |
|
2124 |
|
2125 ComplexMatrix |
|
2126 operator * (double s, const ComplexMatrix& a) |
|
2127 { |
|
2128 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2129 a.rows (), a.cols ()); |
|
2130 } |
|
2131 |
|
2132 ComplexMatrix |
|
2133 operator / (double s, const ComplexMatrix& a) |
|
2134 { |
|
2135 return ComplexMatrix (divide (s, a.data (), a.length ()), |
|
2136 a.rows (), a.cols ()); |
|
2137 } |
|
2138 |
1205
|
2139 ComplexMatrix |
|
2140 operator + (const Complex& s, const Matrix& a) |
458
|
2141 { |
1205
|
2142 return ComplexMatrix (add (s, a.data (), a.length ()), |
|
2143 a.rows (), a.cols ()); |
458
|
2144 } |
|
2145 |
1205
|
2146 ComplexMatrix |
|
2147 operator - (const Complex& s, const Matrix& a) |
458
|
2148 { |
1205
|
2149 return ComplexMatrix (subtract (s, a.data (), a.length ()), |
|
2150 a.rows (), a.cols ()); |
|
2151 } |
|
2152 |
|
2153 ComplexMatrix |
|
2154 operator * (const Complex& s, const Matrix& a) |
|
2155 { |
|
2156 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2157 a.rows (), a.cols ()); |
|
2158 } |
|
2159 |
|
2160 ComplexMatrix |
|
2161 operator / (const Complex& s, const Matrix& a) |
|
2162 { |
|
2163 return ComplexMatrix (divide (s, a.data (), a.length ()), |
|
2164 a.rows (), a.cols ()); |
458
|
2165 } |
|
2166 |
|
2167 // matrix by diagonal matrix -> matrix operations |
|
2168 |
|
2169 ComplexMatrix |
|
2170 operator + (const ComplexMatrix& m, const DiagMatrix& a) |
|
2171 { |
|
2172 int nr = m.rows (); |
|
2173 int nc = m.cols (); |
|
2174 if (nr != a.rows () || nc != a.cols ()) |
|
2175 { |
|
2176 (*current_liboctave_error_handler) |
|
2177 ("nonconformant matrix addition attempted"); |
|
2178 return ComplexMatrix (); |
|
2179 } |
|
2180 |
|
2181 if (nr == 0 || nc == 0) |
|
2182 return ComplexMatrix (nr, nc); |
|
2183 |
|
2184 ComplexMatrix result (m); |
|
2185 for (int i = 0; i < a.length (); i++) |
|
2186 result.elem (i, i) += a.elem (i, i); |
|
2187 |
|
2188 return result; |
|
2189 } |
|
2190 |
|
2191 ComplexMatrix |
|
2192 operator - (const ComplexMatrix& m, const DiagMatrix& a) |
|
2193 { |
|
2194 int nr = m.rows (); |
|
2195 int nc = m.cols (); |
|
2196 if (nr != a.rows () || nc != a.cols ()) |
|
2197 { |
|
2198 (*current_liboctave_error_handler) |
|
2199 ("nonconformant matrix subtraction attempted"); |
|
2200 return ComplexMatrix (); |
|
2201 } |
|
2202 |
|
2203 if (nr == 0 || nc == 0) |
|
2204 return ComplexMatrix (nr, nc); |
|
2205 |
|
2206 ComplexMatrix result (m); |
|
2207 for (int i = 0; i < a.length (); i++) |
|
2208 result.elem (i, i) -= a.elem (i, i); |
|
2209 |
|
2210 return result; |
|
2211 } |
|
2212 |
|
2213 ComplexMatrix |
|
2214 operator * (const ComplexMatrix& m, const DiagMatrix& a) |
|
2215 { |
|
2216 int nr = m.rows (); |
|
2217 int nc = m.cols (); |
|
2218 int a_nc = a.cols (); |
|
2219 if (nc != a.rows ()) |
|
2220 { |
|
2221 (*current_liboctave_error_handler) |
|
2222 ("nonconformant matrix multiplication attempted"); |
|
2223 return ComplexMatrix (); |
|
2224 } |
|
2225 |
|
2226 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2227 return ComplexMatrix (nr, nc, 0.0); |
|
2228 |
|
2229 Complex *c = new Complex [nr*a_nc]; |
533
|
2230 Complex *ctmp = 0; |
458
|
2231 |
|
2232 for (int j = 0; j < a.length (); j++) |
|
2233 { |
|
2234 int idx = j * nr; |
|
2235 ctmp = c + idx; |
|
2236 if (a.elem (j, j) == 1.0) |
|
2237 { |
|
2238 for (int i = 0; i < nr; i++) |
|
2239 ctmp[i] = m.elem (i, j); |
|
2240 } |
|
2241 else if (a.elem (j, j) == 0.0) |
|
2242 { |
|
2243 for (int i = 0; i < nr; i++) |
|
2244 ctmp[i] = 0.0; |
|
2245 } |
|
2246 else |
|
2247 { |
|
2248 for (int i = 0; i < nr; i++) |
|
2249 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
2250 } |
|
2251 } |
|
2252 |
|
2253 if (a.rows () < a_nc) |
|
2254 { |
|
2255 for (int i = nr * nc; i < nr * a_nc; i++) |
|
2256 ctmp[i] = 0.0; |
|
2257 } |
|
2258 |
|
2259 return ComplexMatrix (c, nr, a_nc); |
|
2260 } |
|
2261 |
|
2262 ComplexMatrix |
|
2263 operator + (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2264 { |
|
2265 int nr = m.rows (); |
|
2266 int nc = m.cols (); |
|
2267 if (nr != a.rows () || nc != a.cols ()) |
|
2268 { |
|
2269 (*current_liboctave_error_handler) |
|
2270 ("nonconformant matrix addition attempted"); |
|
2271 return ComplexMatrix (); |
|
2272 } |
|
2273 |
|
2274 if (nr == 0 || nc == 0) |
|
2275 return ComplexMatrix (nr, nc); |
|
2276 |
|
2277 ComplexMatrix result (m); |
|
2278 for (int i = 0; i < a.length (); i++) |
|
2279 result.elem (i, i) += a.elem (i, i); |
|
2280 |
|
2281 return result; |
|
2282 } |
|
2283 |
|
2284 ComplexMatrix |
|
2285 operator - (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2286 { |
|
2287 int nr = m.rows (); |
|
2288 int nc = m.cols (); |
|
2289 if (nr != a.rows () || nc != a.cols ()) |
|
2290 { |
|
2291 (*current_liboctave_error_handler) |
|
2292 ("nonconformant matrix subtraction attempted"); |
|
2293 return ComplexMatrix (); |
|
2294 } |
|
2295 |
|
2296 if (nr == 0 || nc == 0) |
|
2297 return ComplexMatrix (nr, nc); |
|
2298 |
|
2299 ComplexMatrix result (m); |
|
2300 for (int i = 0; i < a.length (); i++) |
|
2301 result.elem (i, i) -= a.elem (i, i); |
|
2302 |
|
2303 return result; |
|
2304 } |
|
2305 |
|
2306 ComplexMatrix |
|
2307 operator * (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2308 { |
|
2309 int nr = m.rows (); |
|
2310 int nc = m.cols (); |
|
2311 int a_nc = a.cols (); |
|
2312 if (nc != a.rows ()) |
|
2313 { |
|
2314 (*current_liboctave_error_handler) |
|
2315 ("nonconformant matrix multiplication attempted"); |
|
2316 return ComplexMatrix (); |
|
2317 } |
|
2318 |
|
2319 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2320 return ComplexMatrix (nr, nc, 0.0); |
|
2321 |
|
2322 Complex *c = new Complex [nr*a_nc]; |
533
|
2323 Complex *ctmp = 0; |
458
|
2324 |
|
2325 for (int j = 0; j < a.length (); j++) |
|
2326 { |
|
2327 int idx = j * nr; |
|
2328 ctmp = c + idx; |
|
2329 if (a.elem (j, j) == 1.0) |
|
2330 { |
|
2331 for (int i = 0; i < nr; i++) |
|
2332 ctmp[i] = m.elem (i, j); |
|
2333 } |
|
2334 else if (a.elem (j, j) == 0.0) |
|
2335 { |
|
2336 for (int i = 0; i < nr; i++) |
|
2337 ctmp[i] = 0.0; |
|
2338 } |
|
2339 else |
|
2340 { |
|
2341 for (int i = 0; i < nr; i++) |
|
2342 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
2343 } |
|
2344 } |
|
2345 |
|
2346 if (a.rows () < a_nc) |
|
2347 { |
|
2348 for (int i = nr * nc; i < nr * a_nc; i++) |
|
2349 ctmp[i] = 0.0; |
|
2350 } |
|
2351 |
|
2352 return ComplexMatrix (c, nr, a_nc); |
|
2353 } |
|
2354 |
|
2355 // matrix by matrix -> matrix operations |
|
2356 |
|
2357 ComplexMatrix |
|
2358 operator + (const ComplexMatrix& m, const Matrix& a) |
|
2359 { |
|
2360 int nr = m.rows (); |
|
2361 int nc = m.cols (); |
|
2362 if (nr != a.rows () || nc != a.cols ()) |
|
2363 { |
|
2364 (*current_liboctave_error_handler) |
|
2365 ("nonconformant matrix addition attempted"); |
|
2366 return ComplexMatrix (); |
|
2367 } |
|
2368 |
|
2369 if (nr == 0 || nc == 0) |
|
2370 return ComplexMatrix (nr, nc); |
|
2371 |
|
2372 return ComplexMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
2373 } |
|
2374 |
|
2375 ComplexMatrix |
|
2376 operator - (const ComplexMatrix& m, const Matrix& a) |
|
2377 { |
|
2378 int nr = m.rows (); |
|
2379 int nc = m.cols (); |
|
2380 if (nr != a.rows () || nc != a.cols ()) |
|
2381 { |
|
2382 (*current_liboctave_error_handler) |
|
2383 ("nonconformant matrix subtraction attempted"); |
|
2384 return ComplexMatrix (); |
|
2385 } |
|
2386 |
|
2387 if (nr == 0 || nc == 0) |
|
2388 return ComplexMatrix (nr, nc); |
|
2389 |
|
2390 return ComplexMatrix (subtract (m.data (), a.data (), m.length ()), nr, nc); |
|
2391 } |
|
2392 |
|
2393 ComplexMatrix |
1205
|
2394 operator + (const Matrix& m, const ComplexMatrix& a) |
|
2395 { |
|
2396 int nr = m.rows (); |
|
2397 int nc = m.cols (); |
|
2398 if (nr != a.rows () || nc != a.cols ()) |
|
2399 { |
|
2400 (*current_liboctave_error_handler) |
|
2401 ("nonconformant matrix addition attempted"); |
|
2402 return ComplexMatrix (); |
|
2403 } |
|
2404 |
|
2405 return ComplexMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
2406 } |
|
2407 |
|
2408 ComplexMatrix |
|
2409 operator - (const Matrix& m, const ComplexMatrix& a) |
|
2410 { |
|
2411 int nr = m.rows (); |
|
2412 int nc = m.cols (); |
|
2413 if (nr != a.rows () || nc != a.cols ()) |
|
2414 { |
|
2415 (*current_liboctave_error_handler) |
|
2416 ("nonconformant matrix subtraction attempted"); |
|
2417 return ComplexMatrix (); |
|
2418 } |
|
2419 |
|
2420 if (nr == 0 || nc == 0) |
|
2421 return ComplexMatrix (nr, nc); |
|
2422 |
|
2423 return ComplexMatrix (subtract (m.data (), a.data (), m.length ()), nr, nc); |
|
2424 } |
|
2425 |
|
2426 ComplexMatrix |
458
|
2427 operator * (const ComplexMatrix& m, const Matrix& a) |
|
2428 { |
|
2429 ComplexMatrix tmp (a); |
|
2430 return m * tmp; |
|
2431 } |
|
2432 |
|
2433 ComplexMatrix |
1205
|
2434 operator * (const Matrix& m, const ComplexMatrix& a) |
|
2435 { |
|
2436 ComplexMatrix tmp (m); |
|
2437 return tmp * a; |
|
2438 } |
|
2439 |
|
2440 ComplexMatrix |
458
|
2441 operator * (const ComplexMatrix& m, const ComplexMatrix& a) |
|
2442 { |
|
2443 int nr = m.rows (); |
|
2444 int nc = m.cols (); |
|
2445 int a_nc = a.cols (); |
|
2446 if (nc != a.rows ()) |
|
2447 { |
|
2448 (*current_liboctave_error_handler) |
|
2449 ("nonconformant matrix multiplication attempted"); |
|
2450 return ComplexMatrix (); |
|
2451 } |
|
2452 |
|
2453 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2454 return ComplexMatrix (nr, nc, 0.0); |
|
2455 |
|
2456 char trans = 'N'; |
|
2457 char transa = 'N'; |
|
2458 |
|
2459 int ld = nr; |
|
2460 int lda = a.rows (); |
|
2461 |
|
2462 Complex alpha (1.0); |
|
2463 Complex beta (0.0); |
|
2464 |
|
2465 Complex *c = new Complex [nr*a_nc]; |
|
2466 |
|
2467 F77_FCN (zgemm) (&trans, &transa, &nr, &a_nc, &nc, &alpha, m.data (), |
|
2468 &ld, a.data (), &lda, &beta, c, &nr, 1L, 1L); |
|
2469 |
|
2470 return ComplexMatrix (c, nr, a_nc); |
|
2471 } |
|
2472 |
|
2473 ComplexMatrix |
|
2474 product (const ComplexMatrix& m, const Matrix& a) |
|
2475 { |
|
2476 int nr = m.rows (); |
|
2477 int nc = m.cols (); |
|
2478 if (nr != a.rows () || nc != a.cols ()) |
|
2479 { |
|
2480 (*current_liboctave_error_handler) |
|
2481 ("nonconformant matrix product attempted"); |
|
2482 return ComplexMatrix (); |
|
2483 } |
|
2484 |
|
2485 if (nr == 0 || nc == 0) |
|
2486 return ComplexMatrix (nr, nc); |
|
2487 |
|
2488 return ComplexMatrix (multiply (m.data (), a.data (), m.length ()), nr, nc); |
|
2489 } |
|
2490 |
|
2491 ComplexMatrix |
|
2492 quotient (const ComplexMatrix& m, const Matrix& a) |
|
2493 { |
|
2494 int nr = m.rows (); |
|
2495 int nc = m.cols (); |
|
2496 if (nr != a.rows () || nc != a.cols ()) |
|
2497 { |
|
2498 (*current_liboctave_error_handler) |
|
2499 ("nonconformant matrix quotient attempted"); |
|
2500 return ComplexMatrix (); |
|
2501 } |
|
2502 |
|
2503 if (nr == 0 || nc == 0) |
|
2504 return ComplexMatrix (nr, nc); |
|
2505 |
|
2506 return ComplexMatrix (divide (m.data (), a.data (), m.length ()), nr, nc); |
|
2507 } |
|
2508 |
1205
|
2509 ComplexMatrix |
|
2510 product (const Matrix& m, const ComplexMatrix& a) |
|
2511 { |
|
2512 int nr = m.rows (); |
|
2513 int nc = m.cols (); |
|
2514 if (nr != a.rows () || nc != a.cols ()) |
|
2515 { |
|
2516 (*current_liboctave_error_handler) |
|
2517 ("nonconformant matrix product attempted"); |
|
2518 return ComplexMatrix (); |
|
2519 } |
|
2520 |
|
2521 if (nr == 0 || nc == 0) |
|
2522 return ComplexMatrix (nr, nc); |
|
2523 |
|
2524 return ComplexMatrix (multiply (m.data (), a.data (), m.length ()), nr, nc); |
|
2525 } |
|
2526 |
|
2527 ComplexMatrix |
|
2528 quotient (const Matrix& m, const ComplexMatrix& a) |
|
2529 { |
|
2530 int nr = m.rows (); |
|
2531 int nc = m.cols (); |
|
2532 if (nr != a.rows () || nc != a.cols ()) |
|
2533 { |
|
2534 (*current_liboctave_error_handler) |
|
2535 ("nonconformant matrix quotient attempted"); |
|
2536 return ComplexMatrix (); |
|
2537 } |
|
2538 |
|
2539 if (nr == 0 || nc == 0) |
|
2540 return ComplexMatrix (nr, nc); |
|
2541 |
|
2542 return ComplexMatrix (divide (m.data (), a.data (), m.length ()), nr, nc); |
|
2543 } |
|
2544 |
458
|
2545 // other operations |
|
2546 |
|
2547 ComplexMatrix |
|
2548 map (c_c_Mapper f, const ComplexMatrix& a) |
|
2549 { |
|
2550 ComplexMatrix b (a); |
|
2551 b.map (f); |
|
2552 return b; |
|
2553 } |
|
2554 |
|
2555 void |
|
2556 ComplexMatrix::map (c_c_Mapper f) |
|
2557 { |
|
2558 for (int j = 0; j < cols (); j++) |
|
2559 for (int i = 0; i < rows (); i++) |
|
2560 elem (i, j) = f (elem (i, j)); |
|
2561 } |
|
2562 |
|
2563 Matrix |
|
2564 ComplexMatrix::all (void) const |
|
2565 { |
|
2566 int nr = rows (); |
|
2567 int nc = cols (); |
|
2568 Matrix retval; |
|
2569 if (nr > 0 && nc > 0) |
|
2570 { |
|
2571 if (nr == 1) |
|
2572 { |
|
2573 retval.resize (1, 1); |
|
2574 retval.elem (0, 0) = 1.0; |
|
2575 for (int j = 0; j < nc; j++) |
|
2576 { |
|
2577 if (elem (0, j) == 0.0) |
|
2578 { |
|
2579 retval.elem (0, 0) = 0.0; |
|
2580 break; |
|
2581 } |
|
2582 } |
|
2583 } |
|
2584 else if (nc == 1) |
|
2585 { |
|
2586 retval.resize (1, 1); |
|
2587 retval.elem (0, 0) = 1.0; |
|
2588 for (int i = 0; i < nr; i++) |
|
2589 { |
|
2590 if (elem (i, 0) == 0.0) |
|
2591 { |
|
2592 retval.elem (0, 0) = 0.0; |
|
2593 break; |
|
2594 } |
|
2595 } |
|
2596 } |
|
2597 else |
|
2598 { |
|
2599 retval.resize (1, nc); |
|
2600 for (int j = 0; j < nc; j++) |
|
2601 { |
|
2602 retval.elem (0, j) = 1.0; |
|
2603 for (int i = 0; i < nr; i++) |
|
2604 { |
|
2605 if (elem (i, j) == 0.0) |
|
2606 { |
|
2607 retval.elem (0, j) = 0.0; |
|
2608 break; |
|
2609 } |
|
2610 } |
|
2611 } |
|
2612 } |
|
2613 } |
|
2614 return retval; |
|
2615 } |
|
2616 |
|
2617 Matrix |
|
2618 ComplexMatrix::any (void) const |
|
2619 { |
|
2620 int nr = rows (); |
|
2621 int nc = cols (); |
|
2622 Matrix retval; |
|
2623 if (nr > 0 && nc > 0) |
|
2624 { |
|
2625 if (nr == 1) |
|
2626 { |
|
2627 retval.resize (1, 1); |
|
2628 retval.elem (0, 0) = 0.0; |
|
2629 for (int j = 0; j < nc; j++) |
|
2630 { |
|
2631 if (elem (0, j) != 0.0) |
|
2632 { |
|
2633 retval.elem (0, 0) = 1.0; |
|
2634 break; |
|
2635 } |
|
2636 } |
|
2637 } |
|
2638 else if (nc == 1) |
|
2639 { |
|
2640 retval.resize (1, 1); |
|
2641 retval.elem (0, 0) = 0.0; |
|
2642 for (int i = 0; i < nr; i++) |
|
2643 { |
|
2644 if (elem (i, 0) != 0.0) |
|
2645 { |
|
2646 retval.elem (0, 0) = 1.0; |
|
2647 break; |
|
2648 } |
|
2649 } |
|
2650 } |
|
2651 else |
|
2652 { |
|
2653 retval.resize (1, nc); |
|
2654 for (int j = 0; j < nc; j++) |
|
2655 { |
|
2656 retval.elem (0, j) = 0.0; |
|
2657 for (int i = 0; i < nr; i++) |
|
2658 { |
|
2659 if (elem (i, j) != 0.0) |
|
2660 { |
|
2661 retval.elem (0, j) = 1.0; |
|
2662 break; |
|
2663 } |
|
2664 } |
|
2665 } |
|
2666 } |
|
2667 } |
|
2668 return retval; |
|
2669 } |
|
2670 |
|
2671 ComplexMatrix |
|
2672 ComplexMatrix::cumprod (void) const |
|
2673 { |
|
2674 int nr = rows (); |
|
2675 int nc = cols (); |
|
2676 ComplexMatrix retval; |
|
2677 if (nr > 0 && nc > 0) |
|
2678 { |
|
2679 if (nr == 1) |
|
2680 { |
|
2681 retval.resize (1, nc); |
|
2682 Complex prod = elem (0, 0); |
|
2683 for (int j = 0; j < nc; j++) |
|
2684 { |
|
2685 retval.elem (0, j) = prod; |
|
2686 if (j < nc - 1) |
|
2687 prod *= elem (0, j+1); |
|
2688 } |
|
2689 } |
|
2690 else if (nc == 1) |
|
2691 { |
|
2692 retval.resize (nr, 1); |
|
2693 Complex prod = elem (0, 0); |
|
2694 for (int i = 0; i < nr; i++) |
|
2695 { |
|
2696 retval.elem (i, 0) = prod; |
|
2697 if (i < nr - 1) |
|
2698 prod *= elem (i+1, 0); |
|
2699 } |
|
2700 } |
|
2701 else |
|
2702 { |
|
2703 retval.resize (nr, nc); |
|
2704 for (int j = 0; j < nc; j++) |
|
2705 { |
|
2706 Complex prod = elem (0, j); |
|
2707 for (int i = 0; i < nr; i++) |
|
2708 { |
|
2709 retval.elem (i, j) = prod; |
|
2710 if (i < nr - 1) |
|
2711 prod *= elem (i+1, j); |
|
2712 } |
|
2713 } |
|
2714 } |
|
2715 } |
|
2716 return retval; |
|
2717 } |
|
2718 |
|
2719 ComplexMatrix |
|
2720 ComplexMatrix::cumsum (void) const |
|
2721 { |
|
2722 int nr = rows (); |
|
2723 int nc = cols (); |
|
2724 ComplexMatrix retval; |
|
2725 if (nr > 0 && nc > 0) |
|
2726 { |
|
2727 if (nr == 1) |
|
2728 { |
|
2729 retval.resize (1, nc); |
|
2730 Complex sum = elem (0, 0); |
|
2731 for (int j = 0; j < nc; j++) |
|
2732 { |
|
2733 retval.elem (0, j) = sum; |
|
2734 if (j < nc - 1) |
|
2735 sum += elem (0, j+1); |
|
2736 } |
|
2737 } |
|
2738 else if (nc == 1) |
|
2739 { |
|
2740 retval.resize (nr, 1); |
|
2741 Complex sum = elem (0, 0); |
|
2742 for (int i = 0; i < nr; i++) |
|
2743 { |
|
2744 retval.elem (i, 0) = sum; |
|
2745 if (i < nr - 1) |
|
2746 sum += elem (i+1, 0); |
|
2747 } |
|
2748 } |
|
2749 else |
|
2750 { |
|
2751 retval.resize (nr, nc); |
|
2752 for (int j = 0; j < nc; j++) |
|
2753 { |
|
2754 Complex sum = elem (0, j); |
|
2755 for (int i = 0; i < nr; i++) |
|
2756 { |
|
2757 retval.elem (i, j) = sum; |
|
2758 if (i < nr - 1) |
|
2759 sum += elem (i+1, j); |
|
2760 } |
|
2761 } |
|
2762 } |
|
2763 } |
|
2764 return retval; |
|
2765 } |
|
2766 |
|
2767 ComplexMatrix |
|
2768 ComplexMatrix::prod (void) const |
|
2769 { |
|
2770 int nr = rows (); |
|
2771 int nc = cols (); |
|
2772 ComplexMatrix retval; |
|
2773 if (nr > 0 && nc > 0) |
|
2774 { |
|
2775 if (nr == 1) |
|
2776 { |
|
2777 retval.resize (1, 1); |
|
2778 retval.elem (0, 0) = 1.0; |
|
2779 for (int j = 0; j < nc; j++) |
|
2780 retval.elem (0, 0) *= elem (0, j); |
|
2781 } |
|
2782 else if (nc == 1) |
|
2783 { |
|
2784 retval.resize (1, 1); |
|
2785 retval.elem (0, 0) = 1.0; |
|
2786 for (int i = 0; i < nr; i++) |
|
2787 retval.elem (0, 0) *= elem (i, 0); |
|
2788 } |
|
2789 else |
|
2790 { |
|
2791 retval.resize (1, nc); |
|
2792 for (int j = 0; j < nc; j++) |
|
2793 { |
|
2794 retval.elem (0, j) = 1.0; |
|
2795 for (int i = 0; i < nr; i++) |
|
2796 retval.elem (0, j) *= elem (i, j); |
|
2797 } |
|
2798 } |
|
2799 } |
|
2800 return retval; |
|
2801 } |
|
2802 |
|
2803 ComplexMatrix |
|
2804 ComplexMatrix::sum (void) const |
|
2805 { |
|
2806 int nr = rows (); |
|
2807 int nc = cols (); |
|
2808 ComplexMatrix retval; |
|
2809 if (nr > 0 && nc > 0) |
|
2810 { |
|
2811 if (nr == 1) |
|
2812 { |
|
2813 retval.resize (1, 1); |
|
2814 retval.elem (0, 0) = 0.0; |
|
2815 for (int j = 0; j < nc; j++) |
|
2816 retval.elem (0, 0) += elem (0, j); |
|
2817 } |
|
2818 else if (nc == 1) |
|
2819 { |
|
2820 retval.resize (1, 1); |
|
2821 retval.elem (0, 0) = 0.0; |
|
2822 for (int i = 0; i < nr; i++) |
|
2823 retval.elem (0, 0) += elem (i, 0); |
|
2824 } |
|
2825 else |
|
2826 { |
|
2827 retval.resize (1, nc); |
|
2828 for (int j = 0; j < nc; j++) |
|
2829 { |
|
2830 retval.elem (0, j) = 0.0; |
|
2831 for (int i = 0; i < nr; i++) |
|
2832 retval.elem (0, j) += elem (i, j); |
|
2833 } |
|
2834 } |
|
2835 } |
|
2836 return retval; |
|
2837 } |
|
2838 |
|
2839 ComplexMatrix |
|
2840 ComplexMatrix::sumsq (void) const |
|
2841 { |
|
2842 int nr = rows (); |
|
2843 int nc = cols (); |
|
2844 ComplexMatrix retval; |
|
2845 if (nr > 0 && nc > 0) |
|
2846 { |
|
2847 if (nr == 1) |
|
2848 { |
|
2849 retval.resize (1, 1); |
|
2850 retval.elem (0, 0) = 0.0; |
|
2851 for (int j = 0; j < nc; j++) |
|
2852 { |
|
2853 Complex d = elem (0, j); |
|
2854 retval.elem (0, 0) += d * d; |
|
2855 } |
|
2856 } |
|
2857 else if (nc == 1) |
|
2858 { |
|
2859 retval.resize (1, 1); |
|
2860 retval.elem (0, 0) = 0.0; |
|
2861 for (int i = 0; i < nr; i++) |
|
2862 { |
|
2863 Complex d = elem (i, 0); |
|
2864 retval.elem (0, 0) += d * d; |
|
2865 } |
|
2866 } |
|
2867 else |
|
2868 { |
|
2869 retval.resize (1, nc); |
|
2870 for (int j = 0; j < nc; j++) |
|
2871 { |
|
2872 retval.elem (0, j) = 0.0; |
|
2873 for (int i = 0; i < nr; i++) |
|
2874 { |
|
2875 Complex d = elem (i, j); |
|
2876 retval.elem (0, j) += d * d; |
|
2877 } |
|
2878 } |
|
2879 } |
|
2880 } |
|
2881 return retval; |
|
2882 } |
|
2883 |
|
2884 ComplexColumnVector |
|
2885 ComplexMatrix::diag (void) const |
|
2886 { |
|
2887 return diag (0); |
|
2888 } |
|
2889 |
|
2890 ComplexColumnVector |
|
2891 ComplexMatrix::diag (int k) const |
|
2892 { |
|
2893 int nnr = rows (); |
|
2894 int nnc = cols (); |
|
2895 if (k > 0) |
|
2896 nnc -= k; |
|
2897 else if (k < 0) |
|
2898 nnr += k; |
|
2899 |
|
2900 ComplexColumnVector d; |
|
2901 |
|
2902 if (nnr > 0 && nnc > 0) |
|
2903 { |
|
2904 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
2905 |
|
2906 d.resize (ndiag); |
|
2907 |
|
2908 if (k > 0) |
|
2909 { |
|
2910 for (int i = 0; i < ndiag; i++) |
|
2911 d.elem (i) = elem (i, i+k); |
|
2912 } |
|
2913 else if ( k < 0) |
|
2914 { |
|
2915 for (int i = 0; i < ndiag; i++) |
|
2916 d.elem (i) = elem (i-k, i); |
|
2917 } |
|
2918 else |
|
2919 { |
|
2920 for (int i = 0; i < ndiag; i++) |
|
2921 d.elem (i) = elem (i, i); |
|
2922 } |
|
2923 } |
|
2924 else |
|
2925 cerr << "diag: requested diagonal out of range\n"; |
|
2926 |
|
2927 return d; |
|
2928 } |
|
2929 |
891
|
2930 // XXX FIXME XXX -- it would be nice to share some code among all the |
|
2931 // min/max functions below. It would also be nice to combine the |
|
2932 // min/max and min_loc/max_loc functions. |
|
2933 |
458
|
2934 ComplexColumnVector |
|
2935 ComplexMatrix::row_min (void) const |
|
2936 { |
|
2937 ComplexColumnVector result; |
|
2938 |
|
2939 int nr = rows (); |
|
2940 int nc = cols (); |
|
2941 if (nr > 0 && nc > 0) |
|
2942 { |
|
2943 result.resize (nr); |
|
2944 |
|
2945 for (int i = 0; i < nr; i++) |
|
2946 { |
891
|
2947 int row_is_real_only = 1; |
|
2948 for (int j = 0; j < nc; j++) |
|
2949 if (imag (elem (i, j)) != 0.0) |
458
|
2950 { |
891
|
2951 row_is_real_only = 0; |
|
2952 break; |
458
|
2953 } |
891
|
2954 |
|
2955 if (row_is_real_only) |
|
2956 { |
|
2957 double res = real (elem (i, 0)); |
|
2958 for (int j = 1; j < nc; j++) |
|
2959 { |
|
2960 double tmp = real (elem (i, j)); |
|
2961 if (tmp < res) |
|
2962 res = tmp; |
|
2963 } |
|
2964 result.elem (i) = res; |
|
2965 } |
|
2966 else |
|
2967 { |
|
2968 Complex res = elem (i, 0); |
|
2969 double absres = abs (res); |
|
2970 for (int j = 1; j < nc; j++) |
|
2971 if (abs (elem (i, j)) < absres) |
|
2972 { |
|
2973 res = elem (i, j); |
|
2974 absres = abs (res); |
|
2975 } |
|
2976 result.elem (i) = res; |
|
2977 } |
458
|
2978 } |
|
2979 } |
|
2980 |
|
2981 return result; |
|
2982 } |
|
2983 |
|
2984 ComplexColumnVector |
|
2985 ComplexMatrix::row_min_loc (void) const |
|
2986 { |
|
2987 ComplexColumnVector result; |
|
2988 |
|
2989 int nr = rows (); |
|
2990 int nc = cols (); |
|
2991 |
|
2992 if (nr > 0 && nc > 0) |
|
2993 { |
|
2994 result.resize (nr); |
|
2995 |
|
2996 for (int i = 0; i < nr; i++) |
|
2997 { |
891
|
2998 int column_is_real_only = 1; |
|
2999 for (int j = 0; j < nc; j++) |
|
3000 if (imag (elem (i, j)) != 0.0) |
|
3001 { |
|
3002 column_is_real_only = 0; |
|
3003 break; |
|
3004 } |
|
3005 |
|
3006 if (column_is_real_only) |
|
3007 { |
|
3008 double res = 0; |
|
3009 double tmp = real (elem (i, 0)); |
|
3010 for (int j = 1; j < nc; j++) |
|
3011 if (real (elem (i, j)) < tmp) |
|
3012 res = j; |
|
3013 |
|
3014 result.elem (i) = res + 1; |
|
3015 } |
|
3016 else |
|
3017 { |
|
3018 Complex res = 0; |
|
3019 double absres = abs (elem (i, 0)); |
|
3020 for (int j = 1; j < nc; j++) |
|
3021 if (abs (elem (i, j)) < absres) |
|
3022 { |
|
3023 res = j; |
|
3024 absres = abs (elem (i, j)); |
|
3025 } |
|
3026 result.elem (i) = res + 1; |
|
3027 } |
458
|
3028 } |
|
3029 } |
|
3030 |
|
3031 return result; |
|
3032 } |
|
3033 |
|
3034 ComplexColumnVector |
|
3035 ComplexMatrix::row_max (void) const |
|
3036 { |
|
3037 ComplexColumnVector result; |
|
3038 |
|
3039 int nr = rows (); |
|
3040 int nc = cols (); |
|
3041 |
|
3042 if (nr > 0 && nc > 0) |
|
3043 { |
|
3044 result.resize (nr); |
|
3045 |
|
3046 for (int i = 0; i < nr; i++) |
|
3047 { |
891
|
3048 int row_is_real_only = 1; |
|
3049 for (int j = 0; j < nc; j++) |
|
3050 if (imag (elem (i, j)) != 0.0) |
458
|
3051 { |
891
|
3052 row_is_real_only = 0; |
|
3053 break; |
458
|
3054 } |
891
|
3055 |
|
3056 if (row_is_real_only) |
|
3057 { |
|
3058 double res = real (elem (i, 0)); |
|
3059 for (int j = 1; j < nc; j++) |
|
3060 { |
|
3061 double tmp = real (elem (i, j)); |
|
3062 if (tmp > res) |
|
3063 res = tmp; |
|
3064 } |
|
3065 result.elem (i) = res; |
|
3066 } |
|
3067 else |
|
3068 { |
|
3069 Complex res = elem (i, 0); |
|
3070 double absres = abs (res); |
|
3071 for (int j = 1; j < nc; j++) |
|
3072 if (abs (elem (i, j)) > absres) |
|
3073 { |
|
3074 res = elem (i, j); |
|
3075 absres = abs (res); |
|
3076 } |
|
3077 result.elem (i) = res; |
|
3078 } |
458
|
3079 } |
|
3080 } |
|
3081 |
|
3082 return result; |
|
3083 } |
|
3084 |
|
3085 ComplexColumnVector |
|
3086 ComplexMatrix::row_max_loc (void) const |
|
3087 { |
|
3088 ComplexColumnVector result; |
|
3089 |
|
3090 int nr = rows (); |
|
3091 int nc = cols (); |
|
3092 |
|
3093 if (nr > 0 && nc > 0) |
|
3094 { |
|
3095 result.resize (nr); |
|
3096 |
|
3097 for (int i = 0; i < nr; i++) |
|
3098 { |
891
|
3099 int column_is_real_only = 1; |
|
3100 for (int j = 0; j < nc; j++) |
|
3101 if (imag (elem (i, j)) != 0.0) |
|
3102 { |
|
3103 column_is_real_only = 0; |
|
3104 break; |
|
3105 } |
|
3106 |
|
3107 if (column_is_real_only) |
|
3108 { |
|
3109 double res = 0; |
|
3110 double tmp = real (elem (i, 0)); |
|
3111 for (int j = 1; j < nc; j++) |
|
3112 if (real (elem (i, j)) > tmp) |
|
3113 res = j; |
|
3114 |
|
3115 result.elem (i) = res + 1; |
|
3116 } |
|
3117 else |
|
3118 { |
|
3119 Complex res = 0; |
|
3120 double absres = abs (elem (i, 0)); |
|
3121 for (int j = 1; j < nc; j++) |
|
3122 if (abs (elem (i, j)) > absres) |
|
3123 { |
|
3124 res = j; |
|
3125 absres = abs (elem (i, j)); |
|
3126 } |
|
3127 result.elem (i) = res + 1; |
|
3128 } |
458
|
3129 } |
|
3130 } |
|
3131 |
|
3132 return result; |
|
3133 } |
|
3134 |
|
3135 ComplexRowVector |
|
3136 ComplexMatrix::column_min (void) const |
|
3137 { |
|
3138 ComplexRowVector result; |
|
3139 |
|
3140 int nr = rows (); |
|
3141 int nc = cols (); |
|
3142 |
|
3143 if (nr > 0 && nc > 0) |
|
3144 { |
|
3145 result.resize (nc); |
|
3146 |
|
3147 for (int j = 0; j < nc; j++) |
|
3148 { |
891
|
3149 int column_is_real_only = 1; |
|
3150 for (int i = 0; i < nr; i++) |
|
3151 if (imag (elem (i, j)) != 0.0) |
458
|
3152 { |
891
|
3153 column_is_real_only = 0; |
|
3154 break; |
458
|
3155 } |
891
|
3156 |
|
3157 if (column_is_real_only) |
|
3158 { |
|
3159 double res = real (elem (0, j)); |
|
3160 for (int i = 1; i < nr; i++) |
|
3161 { |
|
3162 double tmp = real (elem (i, j)); |
|
3163 if (tmp < res) |
|
3164 res = tmp; |
|
3165 } |
|
3166 result.elem (j) = res; |
|
3167 } |
|
3168 else |
|
3169 { |
|
3170 Complex res = elem (0, j); |
|
3171 double absres = abs (res); |
|
3172 for (int i = 1; i < nr; i++) |
|
3173 if (abs (elem (i, j)) < absres) |
|
3174 { |
|
3175 res = elem (i, j); |
|
3176 absres = abs (res); |
|
3177 } |
|
3178 result.elem (j) = res; |
|
3179 } |
458
|
3180 } |
|
3181 } |
|
3182 |
|
3183 return result; |
|
3184 } |
|
3185 |
|
3186 ComplexRowVector |
|
3187 ComplexMatrix::column_min_loc (void) const |
|
3188 { |
|
3189 ComplexRowVector result; |
|
3190 |
|
3191 int nr = rows (); |
|
3192 int nc = cols (); |
|
3193 |
|
3194 if (nr > 0 && nc > 0) |
|
3195 { |
|
3196 result.resize (nc); |
|
3197 |
|
3198 for (int j = 0; j < nc; j++) |
|
3199 { |
891
|
3200 int column_is_real_only = 1; |
|
3201 for (int i = 0; i < nr; i++) |
|
3202 if (imag (elem (i, j)) != 0.0) |
|
3203 { |
|
3204 column_is_real_only = 0; |
|
3205 break; |
|
3206 } |
|
3207 |
|
3208 if (column_is_real_only) |
|
3209 { |
|
3210 double res = 0; |
892
|
3211 double tmp = real (elem (0, j)); |
891
|
3212 for (int i = 1; i < nr; i++) |
|
3213 if (real (elem (i, j)) < tmp) |
|
3214 res = i; |
|
3215 |
|
3216 result.elem (j) = res + 1; |
|
3217 } |
|
3218 else |
|
3219 { |
|
3220 Complex res = 0; |
|
3221 double absres = abs (elem (0, j)); |
|
3222 for (int i = 1; i < nr; i++) |
|
3223 if (abs (elem (i, j)) < absres) |
|
3224 { |
|
3225 res = i; |
|
3226 absres = abs (elem (i, j)); |
|
3227 } |
|
3228 result.elem (j) = res + 1; |
|
3229 } |
458
|
3230 } |
|
3231 } |
|
3232 |
|
3233 return result; |
|
3234 } |
|
3235 |
|
3236 ComplexRowVector |
|
3237 ComplexMatrix::column_max (void) const |
|
3238 { |
|
3239 ComplexRowVector result; |
|
3240 |
|
3241 int nr = rows (); |
|
3242 int nc = cols (); |
|
3243 |
|
3244 if (nr > 0 && nc > 0) |
|
3245 { |
|
3246 result.resize (nc); |
|
3247 |
|
3248 for (int j = 0; j < nc; j++) |
|
3249 { |
891
|
3250 int column_is_real_only = 1; |
|
3251 for (int i = 0; i < nr; i++) |
|
3252 if (imag (elem (i, j)) != 0.0) |
458
|
3253 { |
891
|
3254 column_is_real_only = 0; |
|
3255 break; |
458
|
3256 } |
891
|
3257 |
|
3258 if (column_is_real_only) |
|
3259 { |
|
3260 double res = real (elem (0, j)); |
|
3261 for (int i = 1; i < nr; i++) |
|
3262 { |
|
3263 double tmp = real (elem (i, j)); |
|
3264 if (tmp > res) |
|
3265 res = tmp; |
|
3266 } |
|
3267 result.elem (j) = res; |
|
3268 } |
|
3269 else |
|
3270 { |
|
3271 Complex res = elem (0, j); |
|
3272 double absres = abs (res); |
|
3273 for (int i = 1; i < nr; i++) |
|
3274 if (abs (elem (i, j)) > absres) |
|
3275 { |
|
3276 res = elem (i, j); |
|
3277 absres = abs (res); |
|
3278 } |
|
3279 result.elem (j) = res; |
|
3280 } |
458
|
3281 } |
|
3282 } |
|
3283 |
|
3284 return result; |
|
3285 } |
|
3286 |
|
3287 ComplexRowVector |
|
3288 ComplexMatrix::column_max_loc (void) const |
|
3289 { |
|
3290 ComplexRowVector result; |
|
3291 |
|
3292 int nr = rows (); |
|
3293 int nc = cols (); |
|
3294 |
|
3295 if (nr > 0 && nc > 0) |
|
3296 { |
|
3297 result.resize (nc); |
|
3298 |
|
3299 for (int j = 0; j < nc; j++) |
|
3300 { |
891
|
3301 int column_is_real_only = 1; |
|
3302 for (int i = 0; i < nr; i++) |
|
3303 if (imag (elem (i, j)) != 0.0) |
|
3304 { |
|
3305 column_is_real_only = 0; |
|
3306 break; |
|
3307 } |
|
3308 |
|
3309 if (column_is_real_only) |
|
3310 { |
|
3311 double res = 0; |
892
|
3312 double tmp = real (elem (0, j)); |
891
|
3313 for (int i = 1; i < nr; i++) |
|
3314 if (real (elem (i, j)) > tmp) |
|
3315 res = i; |
|
3316 |
|
3317 result.elem (j) = res + 1; |
|
3318 } |
|
3319 else |
|
3320 { |
|
3321 Complex res = 0; |
|
3322 double absres = abs (elem (0, j)); |
|
3323 for (int i = 1; i < nr; i++) |
|
3324 if (abs (elem (i, j)) > absres) |
|
3325 { |
|
3326 res = i; |
|
3327 absres = abs (elem (i, j)); |
|
3328 } |
|
3329 result.elem (j) = res + 1; |
|
3330 } |
458
|
3331 } |
|
3332 } |
|
3333 |
|
3334 return result; |
|
3335 } |
|
3336 |
|
3337 // i/o |
|
3338 |
|
3339 ostream& |
|
3340 operator << (ostream& os, const ComplexMatrix& a) |
|
3341 { |
|
3342 // int field_width = os.precision () + 7; |
|
3343 for (int i = 0; i < a.rows (); i++) |
|
3344 { |
|
3345 for (int j = 0; j < a.cols (); j++) |
|
3346 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
3347 os << "\n"; |
|
3348 } |
|
3349 return os; |
|
3350 } |
|
3351 |
|
3352 istream& |
|
3353 operator >> (istream& is, ComplexMatrix& a) |
|
3354 { |
|
3355 int nr = a.rows (); |
|
3356 int nc = a.cols (); |
|
3357 |
|
3358 if (nr < 1 || nc < 1) |
|
3359 is.clear (ios::badbit); |
|
3360 else |
|
3361 { |
|
3362 Complex tmp; |
|
3363 for (int i = 0; i < nr; i++) |
|
3364 for (int j = 0; j < nc; j++) |
|
3365 { |
|
3366 is >> tmp; |
|
3367 if (is) |
|
3368 a.elem (i, j) = tmp; |
|
3369 else |
|
3370 break; |
|
3371 } |
|
3372 } |
|
3373 |
|
3374 return is; |
|
3375 } |
|
3376 |
|
3377 /* |
|
3378 ;;; Local Variables: *** |
|
3379 ;;; mode: C++ *** |
|
3380 ;;; page-delimiter: "^/\\*" *** |
|
3381 ;;; End: *** |
|
3382 */ |