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