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