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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
458
|
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 |
1360
|
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. |
458
|
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 |
1360
|
82 // Complex Matrix class |
458
|
83 |
|
84 ComplexMatrix::ComplexMatrix (const Matrix& a) |
1214
|
85 : MArray2<Complex> (a.rows (), a.cols ()) |
458
|
86 { |
|
87 for (int j = 0; j < cols (); j++) |
|
88 for (int i = 0; i < rows (); i++) |
|
89 elem (i, j) = a.elem (i, j); |
|
90 } |
|
91 |
|
92 ComplexMatrix::ComplexMatrix (const DiagMatrix& a) |
1214
|
93 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
458
|
94 { |
|
95 for (int i = 0; i < a.length (); i++) |
|
96 elem (i, i) = a.elem (i, i); |
|
97 } |
|
98 |
|
99 ComplexMatrix::ComplexMatrix (const ComplexDiagMatrix& a) |
1214
|
100 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
458
|
101 { |
|
102 for (int i = 0; i < a.length (); i++) |
|
103 elem (i, i) = a.elem (i, i); |
|
104 } |
|
105 |
|
106 int |
|
107 ComplexMatrix::operator == (const ComplexMatrix& a) const |
|
108 { |
|
109 if (rows () != a.rows () || cols () != a.cols ()) |
|
110 return 0; |
|
111 |
|
112 return equal (data (), a.data (), length ()); |
|
113 } |
|
114 |
|
115 int |
|
116 ComplexMatrix::operator != (const ComplexMatrix& a) const |
|
117 { |
|
118 return !(*this == a); |
|
119 } |
|
120 |
|
121 // destructive insert/delete/reorder operations |
|
122 |
|
123 ComplexMatrix& |
|
124 ComplexMatrix::insert (const Matrix& a, int r, int c) |
|
125 { |
|
126 int a_nr = a.rows (); |
|
127 int a_nc = a.cols (); |
|
128 if (r < 0 || r + a_nr - 1 > rows () || c < 0 || c + a_nc - 1 > cols ()) |
|
129 { |
|
130 (*current_liboctave_error_handler) ("range error for insert"); |
|
131 return *this; |
|
132 } |
|
133 |
|
134 for (int j = 0; j < a_nc; j++) |
|
135 for (int i = 0; i < a_nr; i++) |
|
136 elem (r+i, c+j) = a.elem (i, j); |
|
137 |
|
138 return *this; |
|
139 } |
|
140 |
|
141 ComplexMatrix& |
|
142 ComplexMatrix::insert (const RowVector& a, int r, int c) |
|
143 { |
|
144 int a_len = a.length (); |
|
145 if (r < 0 || r >= rows () || c < 0 || c + a_len - 1 > cols ()) |
|
146 { |
|
147 (*current_liboctave_error_handler) ("range error for insert"); |
|
148 return *this; |
|
149 } |
|
150 |
|
151 for (int i = 0; i < a_len; i++) |
|
152 elem (r, c+i) = a.elem (i); |
|
153 |
|
154 return *this; |
|
155 } |
|
156 |
|
157 ComplexMatrix& |
|
158 ComplexMatrix::insert (const ColumnVector& a, int r, int c) |
|
159 { |
|
160 int a_len = a.length (); |
|
161 if (r < 0 || r + a_len - 1 > rows () || c < 0 || c >= cols ()) |
|
162 { |
|
163 (*current_liboctave_error_handler) ("range error for insert"); |
|
164 return *this; |
|
165 } |
|
166 |
|
167 for (int i = 0; i < a_len; i++) |
|
168 elem (r+i, c) = a.elem (i); |
|
169 |
|
170 return *this; |
|
171 } |
|
172 |
|
173 ComplexMatrix& |
|
174 ComplexMatrix::insert (const DiagMatrix& a, int r, int c) |
|
175 { |
|
176 if (r < 0 || r + a.rows () - 1 > rows () |
|
177 || c < 0 || c + a.cols () - 1 > cols ()) |
|
178 { |
|
179 (*current_liboctave_error_handler) ("range error for insert"); |
|
180 return *this; |
|
181 } |
|
182 |
|
183 for (int i = 0; i < a.length (); i++) |
|
184 elem (r+i, c+i) = a.elem (i, i); |
|
185 |
|
186 return *this; |
|
187 } |
|
188 |
|
189 ComplexMatrix& |
|
190 ComplexMatrix::insert (const ComplexMatrix& a, int r, int c) |
|
191 { |
|
192 int a_nr = a.rows (); |
|
193 int a_nc = a.cols (); |
|
194 if (r < 0 || r + a_nr - 1 > rows () || c < 0 || c + a_nc - 1 > cols ()) |
|
195 { |
|
196 (*current_liboctave_error_handler) ("range error for insert"); |
|
197 return *this; |
|
198 } |
|
199 |
|
200 for (int j = 0; j < a_nc; j++) |
|
201 for (int i = 0; i < a_nr; i++) |
|
202 elem (r+i, c+j) = a.elem (i, j); |
|
203 |
|
204 return *this; |
|
205 } |
|
206 |
|
207 ComplexMatrix& |
|
208 ComplexMatrix::insert (const ComplexRowVector& a, int r, int c) |
|
209 { |
|
210 int a_len = a.length (); |
|
211 if (r < 0 || r >= rows () || c < 0 || c + a_len - 1 > cols ()) |
|
212 { |
|
213 (*current_liboctave_error_handler) ("range error for insert"); |
|
214 return *this; |
|
215 } |
|
216 |
|
217 for (int i = 0; i < a_len; i++) |
|
218 elem (r, c+i) = a.elem (i); |
|
219 |
|
220 return *this; |
|
221 } |
|
222 |
|
223 ComplexMatrix& |
|
224 ComplexMatrix::insert (const ComplexColumnVector& a, int r, int c) |
|
225 { |
|
226 int a_len = a.length (); |
|
227 if (r < 0 || r + a_len - 1 > rows () || c < 0 || c >= cols ()) |
|
228 { |
|
229 (*current_liboctave_error_handler) ("range error for insert"); |
|
230 return *this; |
|
231 } |
|
232 |
|
233 for (int i = 0; i < a_len; i++) |
|
234 elem (r+i, c) = a.elem (i); |
|
235 |
|
236 return *this; |
|
237 } |
|
238 |
|
239 ComplexMatrix& |
|
240 ComplexMatrix::insert (const ComplexDiagMatrix& a, int r, int c) |
|
241 { |
|
242 if (r < 0 || r + a.rows () - 1 > rows () |
|
243 || c < 0 || c + a.cols () - 1 > cols ()) |
|
244 { |
|
245 (*current_liboctave_error_handler) ("range error for insert"); |
|
246 return *this; |
|
247 } |
|
248 |
|
249 for (int i = 0; i < a.length (); i++) |
|
250 elem (r+i, c+i) = a.elem (i, i); |
|
251 |
|
252 return *this; |
|
253 } |
|
254 |
|
255 ComplexMatrix& |
|
256 ComplexMatrix::fill (double val) |
|
257 { |
|
258 int nr = rows (); |
|
259 int nc = cols (); |
|
260 if (nr > 0 && nc > 0) |
|
261 for (int j = 0; j < nc; j++) |
|
262 for (int i = 0; i < nr; i++) |
|
263 elem (i, j) = val; |
|
264 |
|
265 return *this; |
|
266 } |
|
267 |
|
268 ComplexMatrix& |
|
269 ComplexMatrix::fill (const Complex& val) |
|
270 { |
|
271 int nr = rows (); |
|
272 int nc = cols (); |
|
273 if (nr > 0 && nc > 0) |
|
274 for (int j = 0; j < nc; j++) |
|
275 for (int i = 0; i < nr; i++) |
|
276 elem (i, j) = val; |
|
277 |
|
278 return *this; |
|
279 } |
|
280 |
|
281 ComplexMatrix& |
|
282 ComplexMatrix::fill (double val, int r1, int c1, int r2, int c2) |
|
283 { |
|
284 int nr = rows (); |
|
285 int nc = cols (); |
|
286 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
|
287 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) |
|
288 { |
|
289 (*current_liboctave_error_handler) ("range error for fill"); |
|
290 return *this; |
|
291 } |
|
292 |
|
293 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
294 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
295 |
|
296 for (int j = c1; j <= c2; j++) |
|
297 for (int i = r1; i <= r2; i++) |
|
298 elem (i, j) = val; |
|
299 |
|
300 return *this; |
|
301 } |
|
302 |
|
303 ComplexMatrix& |
|
304 ComplexMatrix::fill (const Complex& val, int r1, int c1, int r2, int c2) |
|
305 { |
|
306 int nr = rows (); |
|
307 int nc = cols (); |
|
308 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
|
309 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) |
|
310 { |
|
311 (*current_liboctave_error_handler) ("range error for fill"); |
|
312 return *this; |
|
313 } |
|
314 |
|
315 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
316 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
317 |
|
318 for (int j = c1; j <= c2; j++) |
|
319 for (int i = r1; i <= r2; i++) |
|
320 elem (i, j) = val; |
|
321 |
|
322 return *this; |
|
323 } |
|
324 |
|
325 ComplexMatrix |
|
326 ComplexMatrix::append (const Matrix& a) const |
|
327 { |
|
328 int nr = rows (); |
|
329 int nc = cols (); |
|
330 if (nr != a.rows ()) |
|
331 { |
|
332 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
333 return *this; |
|
334 } |
|
335 |
|
336 int nc_insert = nc; |
|
337 ComplexMatrix retval (nr, nc + a.cols ()); |
|
338 retval.insert (*this, 0, 0); |
|
339 retval.insert (a, 0, nc_insert); |
|
340 return retval; |
|
341 } |
|
342 |
|
343 ComplexMatrix |
|
344 ComplexMatrix::append (const RowVector& a) const |
|
345 { |
|
346 int nr = rows (); |
|
347 int nc = cols (); |
|
348 if (nr != 1) |
|
349 { |
|
350 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
351 return *this; |
|
352 } |
|
353 |
|
354 int nc_insert = nc; |
|
355 ComplexMatrix retval (nr, nc + a.length ()); |
|
356 retval.insert (*this, 0, 0); |
|
357 retval.insert (a, 0, nc_insert); |
|
358 return retval; |
|
359 } |
|
360 |
|
361 ComplexMatrix |
|
362 ComplexMatrix::append (const ColumnVector& a) const |
|
363 { |
|
364 int nr = rows (); |
|
365 int nc = cols (); |
|
366 if (nr != a.length ()) |
|
367 { |
|
368 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
369 return *this; |
|
370 } |
|
371 |
|
372 int nc_insert = nc; |
|
373 ComplexMatrix retval (nr, nc + 1); |
|
374 retval.insert (*this, 0, 0); |
|
375 retval.insert (a, 0, nc_insert); |
|
376 return retval; |
|
377 } |
|
378 |
|
379 ComplexMatrix |
|
380 ComplexMatrix::append (const DiagMatrix& a) const |
|
381 { |
|
382 int nr = rows (); |
|
383 int nc = cols (); |
|
384 if (nr != a.rows ()) |
|
385 { |
|
386 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
387 return *this; |
|
388 } |
|
389 |
|
390 int nc_insert = nc; |
|
391 ComplexMatrix retval (nr, nc + a.cols ()); |
|
392 retval.insert (*this, 0, 0); |
|
393 retval.insert (a, 0, nc_insert); |
|
394 return retval; |
|
395 } |
|
396 |
|
397 ComplexMatrix |
|
398 ComplexMatrix::append (const ComplexMatrix& a) const |
|
399 { |
|
400 int nr = rows (); |
|
401 int nc = cols (); |
|
402 if (nr != a.rows ()) |
|
403 { |
|
404 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
405 return *this; |
|
406 } |
|
407 |
|
408 int nc_insert = nc; |
|
409 ComplexMatrix retval (nr, nc + a.cols ()); |
|
410 retval.insert (*this, 0, 0); |
|
411 retval.insert (a, 0, nc_insert); |
|
412 return retval; |
|
413 } |
|
414 |
|
415 ComplexMatrix |
|
416 ComplexMatrix::append (const ComplexRowVector& a) const |
|
417 { |
|
418 int nr = rows (); |
|
419 int nc = cols (); |
|
420 if (nr != 1) |
|
421 { |
|
422 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
423 return *this; |
|
424 } |
|
425 |
|
426 int nc_insert = nc; |
|
427 ComplexMatrix retval (nr, nc + a.length ()); |
|
428 retval.insert (*this, 0, 0); |
|
429 retval.insert (a, 0, nc_insert); |
|
430 return retval; |
|
431 } |
|
432 |
|
433 ComplexMatrix |
|
434 ComplexMatrix::append (const ComplexColumnVector& a) const |
|
435 { |
|
436 int nr = rows (); |
|
437 int nc = cols (); |
|
438 if (nr != a.length ()) |
|
439 { |
|
440 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
441 return *this; |
|
442 } |
|
443 |
|
444 int nc_insert = nc; |
|
445 ComplexMatrix retval (nr, nc + 1); |
|
446 retval.insert (*this, 0, 0); |
|
447 retval.insert (a, 0, nc_insert); |
|
448 return retval; |
|
449 } |
|
450 |
|
451 ComplexMatrix |
|
452 ComplexMatrix::append (const ComplexDiagMatrix& a) const |
|
453 { |
|
454 int nr = rows (); |
|
455 int nc = cols (); |
|
456 if (nr != a.rows ()) |
|
457 { |
|
458 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
459 return *this; |
|
460 } |
|
461 |
|
462 int nc_insert = nc; |
|
463 ComplexMatrix retval (nr, nc + a.cols ()); |
|
464 retval.insert (*this, 0, 0); |
|
465 retval.insert (a, 0, nc_insert); |
|
466 return retval; |
|
467 } |
|
468 |
|
469 ComplexMatrix |
|
470 ComplexMatrix::stack (const Matrix& a) const |
|
471 { |
|
472 int nr = rows (); |
|
473 int nc = cols (); |
|
474 if (nc != a.cols ()) |
|
475 { |
|
476 (*current_liboctave_error_handler) |
|
477 ("column dimension mismatch for stack"); |
|
478 return *this; |
|
479 } |
|
480 |
|
481 int nr_insert = nr; |
|
482 ComplexMatrix retval (nr + a.rows (), nc); |
|
483 retval.insert (*this, 0, 0); |
|
484 retval.insert (a, nr_insert, 0); |
|
485 return retval; |
|
486 } |
|
487 |
|
488 ComplexMatrix |
|
489 ComplexMatrix::stack (const RowVector& a) const |
|
490 { |
|
491 int nr = rows (); |
|
492 int nc = cols (); |
|
493 if (nc != a.length ()) |
|
494 { |
|
495 (*current_liboctave_error_handler) |
|
496 ("column dimension mismatch for stack"); |
|
497 return *this; |
|
498 } |
|
499 |
|
500 int nr_insert = nr; |
|
501 ComplexMatrix retval (nr + 1, nc); |
|
502 retval.insert (*this, 0, 0); |
|
503 retval.insert (a, nr_insert, 0); |
|
504 return retval; |
|
505 } |
|
506 |
|
507 ComplexMatrix |
|
508 ComplexMatrix::stack (const ColumnVector& a) const |
|
509 { |
|
510 int nr = rows (); |
|
511 int nc = cols (); |
|
512 if (nc != 1) |
|
513 { |
|
514 (*current_liboctave_error_handler) |
|
515 ("column dimension mismatch for stack"); |
|
516 return *this; |
|
517 } |
|
518 |
|
519 int nr_insert = nr; |
|
520 ComplexMatrix retval (nr + a.length (), nc); |
|
521 retval.insert (*this, 0, 0); |
|
522 retval.insert (a, nr_insert, 0); |
|
523 return retval; |
|
524 } |
|
525 |
|
526 ComplexMatrix |
|
527 ComplexMatrix::stack (const DiagMatrix& a) const |
|
528 { |
|
529 int nr = rows (); |
|
530 int nc = cols (); |
|
531 if (nc != a.cols ()) |
|
532 { |
|
533 (*current_liboctave_error_handler) |
|
534 ("column dimension mismatch for stack"); |
|
535 return *this; |
|
536 } |
|
537 |
|
538 int nr_insert = nr; |
|
539 ComplexMatrix retval (nr + a.rows (), nc); |
|
540 retval.insert (*this, 0, 0); |
|
541 retval.insert (a, nr_insert, 0); |
|
542 return retval; |
|
543 } |
|
544 |
|
545 ComplexMatrix |
|
546 ComplexMatrix::stack (const ComplexMatrix& a) const |
|
547 { |
|
548 int nr = rows (); |
|
549 int nc = cols (); |
|
550 if (nc != a.cols ()) |
|
551 { |
|
552 (*current_liboctave_error_handler) |
|
553 ("column dimension mismatch for stack"); |
|
554 return *this; |
|
555 } |
|
556 |
|
557 int nr_insert = nr; |
|
558 ComplexMatrix retval (nr + a.rows (), nc); |
|
559 retval.insert (*this, 0, 0); |
|
560 retval.insert (a, nr_insert, 0); |
|
561 return retval; |
|
562 } |
|
563 |
|
564 ComplexMatrix |
|
565 ComplexMatrix::stack (const ComplexRowVector& a) const |
|
566 { |
|
567 int nr = rows (); |
|
568 int nc = cols (); |
|
569 if (nc != a.length ()) |
|
570 { |
|
571 (*current_liboctave_error_handler) |
|
572 ("column dimension mismatch for stack"); |
|
573 return *this; |
|
574 } |
|
575 |
|
576 int nr_insert = nr; |
|
577 ComplexMatrix retval (nr + 1, nc); |
|
578 retval.insert (*this, 0, 0); |
|
579 retval.insert (a, nr_insert, 0); |
|
580 return retval; |
|
581 } |
|
582 |
|
583 ComplexMatrix |
|
584 ComplexMatrix::stack (const ComplexColumnVector& a) const |
|
585 { |
|
586 int nr = rows (); |
|
587 int nc = cols (); |
|
588 if (nc != 1) |
|
589 { |
|
590 (*current_liboctave_error_handler) |
|
591 ("column dimension mismatch for stack"); |
|
592 return *this; |
|
593 } |
|
594 |
|
595 int nr_insert = nr; |
|
596 ComplexMatrix retval (nr + a.length (), nc); |
|
597 retval.insert (*this, 0, 0); |
|
598 retval.insert (a, nr_insert, 0); |
|
599 return retval; |
|
600 } |
|
601 |
|
602 ComplexMatrix |
|
603 ComplexMatrix::stack (const ComplexDiagMatrix& a) const |
|
604 { |
|
605 int nr = rows (); |
|
606 int nc = cols (); |
|
607 if (nc != a.cols ()) |
|
608 { |
|
609 (*current_liboctave_error_handler) |
|
610 ("column dimension mismatch for stack"); |
|
611 return *this; |
|
612 } |
|
613 |
|
614 int nr_insert = nr; |
|
615 ComplexMatrix retval (nr + a.rows (), nc); |
|
616 retval.insert (*this, 0, 0); |
|
617 retval.insert (a, nr_insert, 0); |
|
618 return retval; |
|
619 } |
|
620 |
|
621 ComplexMatrix |
|
622 ComplexMatrix::hermitian (void) const |
|
623 { |
|
624 int nr = rows (); |
|
625 int nc = cols (); |
|
626 ComplexMatrix result; |
|
627 if (length () > 0) |
|
628 { |
|
629 result.resize (nc, nr); |
|
630 for (int j = 0; j < nc; j++) |
|
631 for (int i = 0; i < nr; i++) |
|
632 result.elem (j, i) = conj (elem (i, j)); |
|
633 } |
|
634 return result; |
|
635 } |
|
636 |
|
637 ComplexMatrix |
|
638 ComplexMatrix::transpose (void) const |
|
639 { |
|
640 int nr = rows (); |
|
641 int nc = cols (); |
|
642 ComplexMatrix result (nc, nr); |
|
643 if (length () > 0) |
|
644 { |
|
645 for (int j = 0; j < nc; j++) |
|
646 for (int i = 0; i < nr; i++) |
|
647 result.elem (j, i) = elem (i, j); |
|
648 } |
|
649 return result; |
|
650 } |
|
651 |
|
652 ComplexMatrix |
|
653 conj (const ComplexMatrix& a) |
|
654 { |
|
655 int a_len = a.length (); |
|
656 ComplexMatrix retval; |
|
657 if (a_len > 0) |
|
658 retval = ComplexMatrix (conj_dup (a.data (), a_len), a.rows (), |
|
659 a.cols ()); |
|
660 return retval; |
|
661 } |
|
662 |
|
663 // resize is the destructive equivalent for this one |
|
664 |
|
665 ComplexMatrix |
|
666 ComplexMatrix::extract (int r1, int c1, int r2, int c2) const |
|
667 { |
|
668 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
669 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
670 |
|
671 int new_r = r2 - r1 + 1; |
|
672 int new_c = c2 - c1 + 1; |
|
673 |
|
674 ComplexMatrix result (new_r, new_c); |
|
675 |
|
676 for (int j = 0; j < new_c; j++) |
|
677 for (int i = 0; i < new_r; i++) |
|
678 result.elem (i, j) = elem (r1+i, c1+j); |
|
679 |
|
680 return result; |
|
681 } |
|
682 |
|
683 // extract row or column i. |
|
684 |
|
685 ComplexRowVector |
|
686 ComplexMatrix::row (int i) const |
|
687 { |
|
688 int nc = cols (); |
|
689 if (i < 0 || i >= rows ()) |
|
690 { |
|
691 (*current_liboctave_error_handler) ("invalid row selection"); |
|
692 return ComplexRowVector (); |
|
693 } |
|
694 |
|
695 ComplexRowVector retval (nc); |
|
696 for (int j = 0; j < cols (); j++) |
|
697 retval.elem (j) = elem (i, j); |
|
698 |
|
699 return retval; |
|
700 } |
|
701 |
|
702 ComplexRowVector |
|
703 ComplexMatrix::row (char *s) const |
|
704 { |
533
|
705 if (! s) |
458
|
706 { |
|
707 (*current_liboctave_error_handler) ("invalid row selection"); |
|
708 return ComplexRowVector (); |
|
709 } |
|
710 |
|
711 char c = *s; |
|
712 if (c == 'f' || c == 'F') |
|
713 return row (0); |
|
714 else if (c == 'l' || c == 'L') |
|
715 return row (rows () - 1); |
|
716 else |
|
717 { |
|
718 (*current_liboctave_error_handler) ("invalid row selection"); |
|
719 return ComplexRowVector (); |
|
720 } |
|
721 } |
|
722 |
|
723 ComplexColumnVector |
|
724 ComplexMatrix::column (int i) const |
|
725 { |
|
726 int nr = rows (); |
|
727 if (i < 0 || i >= cols ()) |
|
728 { |
|
729 (*current_liboctave_error_handler) ("invalid column selection"); |
|
730 return ComplexColumnVector (); |
|
731 } |
|
732 |
|
733 ComplexColumnVector retval (nr); |
|
734 for (int j = 0; j < nr; j++) |
|
735 retval.elem (j) = elem (j, i); |
|
736 |
|
737 return retval; |
|
738 } |
|
739 |
|
740 ComplexColumnVector |
|
741 ComplexMatrix::column (char *s) const |
|
742 { |
533
|
743 if (! s) |
458
|
744 { |
|
745 (*current_liboctave_error_handler) ("invalid column selection"); |
|
746 return ComplexColumnVector (); |
|
747 } |
|
748 |
|
749 char c = *s; |
|
750 if (c == 'f' || c == 'F') |
|
751 return column (0); |
|
752 else if (c == 'l' || c == 'L') |
|
753 return column (cols () - 1); |
|
754 else |
|
755 { |
|
756 (*current_liboctave_error_handler) ("invalid column selection"); |
|
757 return ComplexColumnVector (); |
|
758 } |
|
759 } |
|
760 |
|
761 ComplexMatrix |
|
762 ComplexMatrix::inverse (void) const |
|
763 { |
|
764 int info; |
479
|
765 double rcond; |
|
766 return inverse (info, rcond); |
458
|
767 } |
|
768 |
|
769 ComplexMatrix |
|
770 ComplexMatrix::inverse (int& info) const |
|
771 { |
|
772 double rcond; |
|
773 return inverse (info, rcond); |
|
774 } |
|
775 |
|
776 ComplexMatrix |
532
|
777 ComplexMatrix::inverse (int& info, double& rcond) const |
458
|
778 { |
|
779 int nr = rows (); |
|
780 int nc = cols (); |
|
781 int len = length (); |
|
782 if (nr != nc) |
|
783 { |
|
784 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
785 return ComplexMatrix (); |
|
786 } |
|
787 |
|
788 info = 0; |
|
789 |
|
790 int *ipvt = new int [nr]; |
|
791 Complex *z = new Complex [nr]; |
|
792 Complex *tmp_data = dup (data (), len); |
|
793 |
1253
|
794 F77_FCN (zgeco, ZGECO) (tmp_data, nr, nc, ipvt, rcond, z); |
458
|
795 |
1195
|
796 volatile double rcond_plus_one = rcond + 1.0; |
|
797 if (rcond_plus_one == 1.0) |
458
|
798 { |
|
799 info = -1; |
|
800 copy (tmp_data, data (), len); // Restore contents. |
|
801 } |
|
802 else |
|
803 { |
1251
|
804 Complex *dummy; |
|
805 |
1253
|
806 F77_FCN (zgedi, ZGEDI) (tmp_data, nr, nc, ipvt, dummy, z, 1); |
458
|
807 } |
|
808 |
|
809 delete [] ipvt; |
|
810 delete [] z; |
|
811 |
|
812 return ComplexMatrix (tmp_data, nr, nc); |
|
813 } |
|
814 |
|
815 ComplexMatrix |
740
|
816 ComplexMatrix::pseudo_inverse (double tol) |
|
817 { |
|
818 ComplexSVD result (*this); |
|
819 |
|
820 DiagMatrix S = result.singular_values (); |
|
821 ComplexMatrix U = result.left_singular_matrix (); |
|
822 ComplexMatrix V = result.right_singular_matrix (); |
|
823 |
|
824 ColumnVector sigma = S.diag (); |
|
825 |
|
826 int r = sigma.length () - 1; |
|
827 int nr = rows (); |
|
828 int nc = cols (); |
|
829 |
|
830 if (tol <= 0.0) |
|
831 { |
|
832 if (nr > nc) |
|
833 tol = nr * sigma.elem (0) * DBL_EPSILON; |
|
834 else |
|
835 tol = nc * sigma.elem (0) * DBL_EPSILON; |
|
836 } |
|
837 |
|
838 while (r >= 0 && sigma.elem (r) < tol) |
|
839 r--; |
|
840 |
|
841 if (r < 0) |
|
842 return ComplexMatrix (nc, nr, 0.0); |
|
843 else |
|
844 { |
|
845 ComplexMatrix Ur = U.extract (0, 0, nr-1, r); |
|
846 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); |
|
847 ComplexMatrix Vr = V.extract (0, 0, nc-1, r); |
|
848 return Vr * D * Ur.hermitian (); |
|
849 } |
|
850 } |
|
851 |
|
852 ComplexMatrix |
458
|
853 ComplexMatrix::fourier (void) const |
|
854 { |
|
855 int nr = rows (); |
|
856 int nc = cols (); |
|
857 int npts, nsamples; |
|
858 if (nr == 1 || nc == 1) |
|
859 { |
|
860 npts = nr > nc ? nr : nc; |
|
861 nsamples = 1; |
|
862 } |
|
863 else |
|
864 { |
|
865 npts = nr; |
|
866 nsamples = nc; |
|
867 } |
|
868 |
|
869 int nn = 4*npts+15; |
|
870 Complex *wsave = new Complex [nn]; |
|
871 Complex *tmp_data = dup (data (), length ()); |
|
872 |
1253
|
873 F77_FCN (cffti, CFFTI) (npts, wsave); |
458
|
874 |
|
875 for (int j = 0; j < nsamples; j++) |
1253
|
876 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], wsave); |
458
|
877 |
|
878 delete [] wsave; |
|
879 |
|
880 return ComplexMatrix (tmp_data, nr, nc); |
|
881 } |
|
882 |
|
883 ComplexMatrix |
|
884 ComplexMatrix::ifourier (void) const |
|
885 { |
|
886 int nr = rows (); |
|
887 int nc = cols (); |
|
888 int npts, nsamples; |
|
889 if (nr == 1 || nc == 1) |
|
890 { |
|
891 npts = nr > nc ? nr : nc; |
|
892 nsamples = 1; |
|
893 } |
|
894 else |
|
895 { |
|
896 npts = nr; |
|
897 nsamples = nc; |
|
898 } |
|
899 |
|
900 int nn = 4*npts+15; |
|
901 Complex *wsave = new Complex [nn]; |
|
902 Complex *tmp_data = dup (data (), length ()); |
|
903 |
1253
|
904 F77_FCN (cffti, CFFTI) (npts, wsave); |
458
|
905 |
|
906 for (int j = 0; j < nsamples; j++) |
1253
|
907 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], wsave); |
458
|
908 |
1321
|
909 for (int j = 0; j < npts*nsamples; j++) |
458
|
910 tmp_data[j] = tmp_data[j] / (double) npts; |
|
911 |
|
912 delete [] wsave; |
|
913 |
|
914 return ComplexMatrix (tmp_data, nr, nc); |
|
915 } |
|
916 |
677
|
917 ComplexMatrix |
|
918 ComplexMatrix::fourier2d (void) const |
|
919 { |
|
920 int nr = rows (); |
|
921 int nc = cols (); |
|
922 int npts, nsamples; |
|
923 if (nr == 1 || nc == 1) |
|
924 { |
|
925 npts = nr > nc ? nr : nc; |
|
926 nsamples = 1; |
|
927 } |
|
928 else |
|
929 { |
|
930 npts = nr; |
|
931 nsamples = nc; |
|
932 } |
|
933 |
|
934 int nn = 4*npts+15; |
|
935 Complex *wsave = new Complex [nn]; |
|
936 Complex *tmp_data = dup (data (), length ()); |
|
937 |
1253
|
938 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
939 |
|
940 for (int j = 0; j < nsamples; j++) |
1253
|
941 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], wsave); |
677
|
942 |
|
943 delete [] wsave; |
|
944 |
|
945 npts = nc; |
|
946 nsamples = nr; |
|
947 nn = 4*npts+15; |
|
948 wsave = new Complex [nn]; |
|
949 Complex *row = new Complex[npts]; |
|
950 |
1253
|
951 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
952 |
1321
|
953 for (int j = 0; j < nsamples; j++) |
677
|
954 { |
|
955 for (int i = 0; i < npts; i++) |
|
956 row[i] = tmp_data[i*nr + j]; |
|
957 |
1253
|
958 F77_FCN (cfftf, CFFTF) (npts, row, wsave); |
677
|
959 |
1321
|
960 for (int i = 0; i < npts; i++) |
677
|
961 tmp_data[i*nr + j] = row[i]; |
|
962 } |
|
963 |
|
964 delete [] wsave; |
|
965 delete [] row; |
|
966 |
|
967 return ComplexMatrix (tmp_data, nr, nc); |
|
968 } |
|
969 |
|
970 ComplexMatrix |
|
971 ComplexMatrix::ifourier2d (void) const |
|
972 { |
|
973 int nr = rows (); |
|
974 int nc = cols (); |
|
975 int npts, nsamples; |
|
976 if (nr == 1 || nc == 1) |
|
977 { |
|
978 npts = nr > nc ? nr : nc; |
|
979 nsamples = 1; |
|
980 } |
|
981 else |
|
982 { |
|
983 npts = nr; |
|
984 nsamples = nc; |
|
985 } |
|
986 |
|
987 int nn = 4*npts+15; |
|
988 Complex *wsave = new Complex [nn]; |
|
989 Complex *tmp_data = dup (data (), length ()); |
|
990 |
1253
|
991 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
992 |
|
993 for (int j = 0; j < nsamples; j++) |
1253
|
994 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], wsave); |
677
|
995 |
|
996 delete [] wsave; |
|
997 |
1321
|
998 for (int j = 0; j < npts*nsamples; j++) |
677
|
999 tmp_data[j] = tmp_data[j] / (double) npts; |
|
1000 |
|
1001 npts = nc; |
|
1002 nsamples = nr; |
|
1003 nn = 4*npts+15; |
|
1004 wsave = new Complex [nn]; |
|
1005 Complex *row = new Complex[npts]; |
|
1006 |
1253
|
1007 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
1008 |
1321
|
1009 for (int j = 0; j < nsamples; j++) |
677
|
1010 { |
|
1011 for (int i = 0; i < npts; i++) |
|
1012 row[i] = tmp_data[i*nr + j]; |
|
1013 |
1253
|
1014 F77_FCN (cfftb, CFFTB) (npts, row, wsave); |
677
|
1015 |
1321
|
1016 for (int i = 0; i < npts; i++) |
677
|
1017 tmp_data[i*nr + j] = row[i] / (double) npts; |
|
1018 } |
|
1019 |
|
1020 delete [] wsave; |
|
1021 delete [] row; |
|
1022 |
|
1023 return ComplexMatrix (tmp_data, nr, nc); |
|
1024 } |
|
1025 |
458
|
1026 ComplexDET |
|
1027 ComplexMatrix::determinant (void) const |
|
1028 { |
|
1029 int info; |
|
1030 double rcond; |
|
1031 return determinant (info, rcond); |
|
1032 } |
|
1033 |
|
1034 ComplexDET |
|
1035 ComplexMatrix::determinant (int& info) const |
|
1036 { |
|
1037 double rcond; |
|
1038 return determinant (info, rcond); |
|
1039 } |
|
1040 |
|
1041 ComplexDET |
532
|
1042 ComplexMatrix::determinant (int& info, double& rcond) const |
458
|
1043 { |
|
1044 ComplexDET retval; |
|
1045 |
|
1046 int nr = rows (); |
|
1047 int nc = cols (); |
|
1048 |
|
1049 if (nr == 0 || nc == 0) |
|
1050 { |
|
1051 Complex d[2]; |
|
1052 d[0] = 1.0; |
|
1053 d[1] = 0.0; |
|
1054 retval = ComplexDET (d); |
|
1055 } |
|
1056 else |
|
1057 { |
|
1058 info = 0; |
|
1059 int *ipvt = new int [nr]; |
|
1060 |
|
1061 Complex *z = new Complex [nr]; |
|
1062 Complex *tmp_data = dup (data (), length ()); |
|
1063 |
1253
|
1064 F77_FCN (zgeco, ZGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
1065 |
1195
|
1066 volatile double rcond_plus_one = rcond + 1.0; |
|
1067 if (rcond_plus_one == 1.0) |
458
|
1068 { |
|
1069 info = -1; |
|
1070 retval = ComplexDET (); |
|
1071 } |
|
1072 else |
|
1073 { |
|
1074 Complex d[2]; |
1253
|
1075 F77_FCN (zgedi, ZGEDI) (tmp_data, nr, nr, ipvt, d, z, 10); |
458
|
1076 retval = ComplexDET (d); |
|
1077 } |
|
1078 |
|
1079 delete [] tmp_data; |
|
1080 delete [] ipvt; |
|
1081 delete [] z; |
|
1082 } |
|
1083 |
|
1084 return retval; |
|
1085 } |
|
1086 |
|
1087 ComplexMatrix |
|
1088 ComplexMatrix::solve (const Matrix& b) const |
|
1089 { |
|
1090 int info; |
|
1091 double rcond; |
|
1092 return solve (b, info, rcond); |
|
1093 } |
|
1094 |
|
1095 ComplexMatrix |
|
1096 ComplexMatrix::solve (const Matrix& b, int& info) const |
|
1097 { |
|
1098 double rcond; |
|
1099 return solve (b, info, rcond); |
|
1100 } |
|
1101 |
|
1102 ComplexMatrix |
|
1103 ComplexMatrix::solve (const Matrix& b, int& info, double& rcond) const |
|
1104 { |
|
1105 ComplexMatrix tmp (b); |
|
1106 return solve (tmp, info, rcond); |
|
1107 } |
|
1108 |
|
1109 ComplexMatrix |
|
1110 ComplexMatrix::solve (const ComplexMatrix& b) const |
|
1111 { |
|
1112 int info; |
|
1113 double rcond; |
|
1114 return solve (b, info, rcond); |
|
1115 } |
|
1116 |
|
1117 ComplexMatrix |
|
1118 ComplexMatrix::solve (const ComplexMatrix& b, int& info) const |
|
1119 { |
|
1120 double rcond; |
|
1121 return solve (b, info, rcond); |
|
1122 } |
|
1123 ComplexMatrix |
532
|
1124 ComplexMatrix::solve (const ComplexMatrix& b, int& info, double& rcond) const |
458
|
1125 { |
|
1126 ComplexMatrix retval; |
|
1127 |
|
1128 int nr = rows (); |
|
1129 int nc = cols (); |
|
1130 int b_nr = b.rows (); |
|
1131 int b_nc = b.cols (); |
|
1132 if (nr == 0 || nc == 0 || nr != nc || nr != b_nr) |
|
1133 { |
|
1134 (*current_liboctave_error_handler) |
|
1135 ("matrix dimension mismatch in solution of linear equations"); |
|
1136 return ComplexMatrix (); |
|
1137 } |
|
1138 |
|
1139 info = 0; |
|
1140 int *ipvt = new int [nr]; |
|
1141 |
|
1142 Complex *z = new Complex [nr]; |
|
1143 Complex *tmp_data = dup (data (), length ()); |
|
1144 |
1253
|
1145 F77_FCN (zgeco, ZGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
1146 |
1195
|
1147 volatile double rcond_plus_one = rcond + 1.0; |
|
1148 if (rcond_plus_one == 1.0) |
458
|
1149 { |
|
1150 info = -2; |
|
1151 } |
|
1152 else |
|
1153 { |
|
1154 Complex *result = dup (b.data (), b.length ()); |
|
1155 |
|
1156 for (int j = 0; j < b_nc; j++) |
1253
|
1157 F77_FCN (zgesl, ZGESL) (tmp_data, nr, nr, ipvt, &result[nr*j], 0); |
458
|
1158 |
|
1159 retval = ComplexMatrix (result, b_nr, b_nc); |
|
1160 } |
|
1161 |
|
1162 delete [] tmp_data; |
|
1163 delete [] ipvt; |
|
1164 delete [] z; |
|
1165 |
|
1166 return retval; |
|
1167 } |
|
1168 |
|
1169 ComplexColumnVector |
|
1170 ComplexMatrix::solve (const ComplexColumnVector& b) const |
|
1171 { |
|
1172 int info; |
|
1173 double rcond; |
|
1174 return solve (b, info, rcond); |
|
1175 } |
|
1176 |
|
1177 ComplexColumnVector |
|
1178 ComplexMatrix::solve (const ComplexColumnVector& b, int& info) const |
|
1179 { |
|
1180 double rcond; |
|
1181 return solve (b, info, rcond); |
|
1182 } |
|
1183 |
|
1184 ComplexColumnVector |
|
1185 ComplexMatrix::solve (const ComplexColumnVector& b, int& info, |
532
|
1186 double& rcond) const |
458
|
1187 { |
|
1188 ComplexColumnVector retval; |
|
1189 |
|
1190 int nr = rows (); |
|
1191 int nc = cols (); |
|
1192 int b_len = b.length (); |
|
1193 if (nr == 0 || nc == 0 || nr != nc || nr != b_len) |
|
1194 { |
|
1195 (*current_liboctave_error_handler) |
|
1196 ("matrix dimension mismatch in solution of linear equations"); |
|
1197 return ComplexColumnVector (); |
|
1198 } |
|
1199 |
|
1200 info = 0; |
|
1201 int *ipvt = new int [nr]; |
|
1202 |
|
1203 Complex *z = new Complex [nr]; |
|
1204 Complex *tmp_data = dup (data (), length ()); |
|
1205 |
1253
|
1206 F77_FCN (zgeco, ZGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
1207 |
1195
|
1208 volatile double rcond_plus_one = rcond + 1.0; |
|
1209 if (rcond_plus_one == 1.0) |
458
|
1210 { |
|
1211 info = -2; |
|
1212 } |
|
1213 else |
|
1214 { |
|
1215 Complex *result = dup (b.data (), b_len); |
|
1216 |
1253
|
1217 F77_FCN (zgesl, ZGESL) (tmp_data, nr, nr, ipvt, result, 0); |
458
|
1218 |
|
1219 retval = ComplexColumnVector (result, b_len); |
|
1220 } |
|
1221 |
|
1222 delete [] tmp_data; |
|
1223 delete [] ipvt; |
|
1224 delete [] z; |
|
1225 |
|
1226 return retval; |
|
1227 } |
|
1228 |
|
1229 ComplexMatrix |
|
1230 ComplexMatrix::lssolve (const ComplexMatrix& b) const |
|
1231 { |
|
1232 int info; |
|
1233 int rank; |
|
1234 return lssolve (b, info, rank); |
|
1235 } |
|
1236 |
|
1237 ComplexMatrix |
|
1238 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info) const |
|
1239 { |
|
1240 int rank; |
|
1241 return lssolve (b, info, rank); |
|
1242 } |
|
1243 |
|
1244 ComplexMatrix |
|
1245 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info, int& rank) const |
|
1246 { |
|
1247 int nrhs = b.cols (); |
|
1248 |
|
1249 int m = rows (); |
|
1250 int n = cols (); |
|
1251 |
|
1252 if (m == 0 || n == 0 || m != b.rows ()) |
|
1253 { |
|
1254 (*current_liboctave_error_handler) |
|
1255 ("matrix dimension mismatch solution of linear equations"); |
|
1256 return Matrix (); |
|
1257 } |
|
1258 |
|
1259 Complex *tmp_data = dup (data (), length ()); |
|
1260 |
|
1261 int nrr = m > n ? m : n; |
|
1262 ComplexMatrix result (nrr, nrhs); |
|
1263 |
1321
|
1264 for (int j = 0; j < nrhs; j++) |
|
1265 for (int i = 0; i < m; i++) |
458
|
1266 result.elem (i, j) = b.elem (i, j); |
|
1267 |
|
1268 Complex *presult = result.fortran_vec (); |
|
1269 |
|
1270 int len_s = m < n ? m : n; |
|
1271 double *s = new double [len_s]; |
|
1272 double rcond = -1.0; |
|
1273 int lwork; |
|
1274 if (m < n) |
|
1275 lwork = 2*m + (nrhs > n ? nrhs : n); |
|
1276 else |
|
1277 lwork = 2*n + (nrhs > m ? nrhs : m); |
|
1278 |
|
1279 Complex *work = new Complex [lwork]; |
|
1280 |
|
1281 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1282 lrwork = lrwork > 1 ? lrwork : 1; |
|
1283 double *rwork = new double [lrwork]; |
|
1284 |
1253
|
1285 F77_FCN (zgelss, ZGELSS) (m, n, nrhs, tmp_data, m, presult, nrr, s, |
|
1286 rcond, rank, work, lwork, rwork, info); |
458
|
1287 |
|
1288 ComplexMatrix retval (n, nrhs); |
1321
|
1289 for (int j = 0; j < nrhs; j++) |
|
1290 for (int i = 0; i < n; i++) |
458
|
1291 retval.elem (i, j) = result.elem (i, j); |
|
1292 |
|
1293 delete [] tmp_data; |
|
1294 delete [] s; |
|
1295 delete [] work; |
|
1296 delete [] rwork; |
|
1297 |
|
1298 return retval; |
|
1299 } |
|
1300 |
|
1301 ComplexColumnVector |
|
1302 ComplexMatrix::lssolve (const ComplexColumnVector& b) const |
|
1303 { |
|
1304 int info; |
|
1305 int rank; |
|
1306 return lssolve (b, info, rank); |
|
1307 } |
|
1308 |
|
1309 ComplexColumnVector |
|
1310 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info) const |
|
1311 { |
|
1312 int rank; |
|
1313 return lssolve (b, info, rank); |
|
1314 } |
|
1315 |
|
1316 ComplexColumnVector |
|
1317 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info, |
|
1318 int& rank) const |
|
1319 { |
|
1320 int nrhs = 1; |
|
1321 |
|
1322 int m = rows (); |
|
1323 int n = cols (); |
|
1324 |
|
1325 if (m == 0 || n == 0 || m != b.length ()) |
|
1326 { |
|
1327 (*current_liboctave_error_handler) |
|
1328 ("matrix dimension mismatch solution of least squares problem"); |
|
1329 return ComplexColumnVector (); |
|
1330 } |
|
1331 |
|
1332 Complex *tmp_data = dup (data (), length ()); |
|
1333 |
|
1334 int nrr = m > n ? m : n; |
|
1335 ComplexColumnVector result (nrr); |
|
1336 |
1321
|
1337 for (int i = 0; i < m; i++) |
458
|
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); |
1321
|
1361 for (int i = 0; i < n; i++) |
458
|
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 */ |