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 |
1367
|
32 #include <cfloat> |
|
33 |
458
|
34 #include <iostream.h> |
1367
|
35 |
|
36 #include <sys/types.h> // XXX FIXME XXX |
458
|
37 |
|
38 #include "CmplxDET.h" |
740
|
39 #include "CmplxSVD.h" |
1367
|
40 #include "f77-uscore.h" |
458
|
41 #include "lo-error.h" |
1367
|
42 #include "mx-base.h" |
|
43 #include "mx-inlines.cc" |
1650
|
44 #include "oct-cmplx.h" |
458
|
45 |
|
46 // Fortran functions we call. |
|
47 |
|
48 extern "C" |
|
49 { |
1253
|
50 int F77_FCN (zgemm, ZGEMM) (const char*, const char*, const int&, |
|
51 const int&, const int&, const Complex&, |
|
52 const Complex*, const int&, |
|
53 const Complex*, const int&, |
|
54 const Complex&, Complex*, const int&, |
|
55 long, long); |
|
56 |
|
57 int F77_FCN (zgeco, ZGECO) (Complex*, const int&, const int&, int*, |
|
58 double&, Complex*); |
|
59 |
|
60 int F77_FCN (zgedi, ZGEDI) (Complex*, const int&, const int&, int*, |
|
61 Complex*, Complex*, const int&); |
|
62 |
|
63 int F77_FCN (zgesl, ZGESL) (Complex*, const int&, const int&, int*, |
|
64 Complex*, const int&); |
|
65 |
|
66 int F77_FCN (zgelss, ZGELSS) (const int&, const int&, const int&, |
|
67 Complex*, const int&, Complex*, |
|
68 const int&, double*, double&, int&, |
|
69 Complex*, const int&, double*, int&); |
458
|
70 |
1360
|
71 // Note that the original complex fft routines were not written for |
|
72 // double complex arguments. They have been modified by adding an |
|
73 // implicit double precision (a-h,o-z) statement at the beginning of |
|
74 // each subroutine. |
458
|
75 |
1253
|
76 int F77_FCN (cffti, CFFTI) (const int&, Complex*); |
|
77 |
|
78 int F77_FCN (cfftf, CFFTF) (const int&, Complex*, Complex*); |
|
79 |
|
80 int F77_FCN (cfftb, CFFTB) (const int&, Complex*, Complex*); |
458
|
81 } |
|
82 |
1360
|
83 // Complex Matrix class |
458
|
84 |
|
85 ComplexMatrix::ComplexMatrix (const Matrix& a) |
1214
|
86 : MArray2<Complex> (a.rows (), a.cols ()) |
458
|
87 { |
|
88 for (int j = 0; j < cols (); j++) |
|
89 for (int i = 0; i < rows (); i++) |
|
90 elem (i, j) = a.elem (i, j); |
|
91 } |
|
92 |
|
93 ComplexMatrix::ComplexMatrix (const DiagMatrix& a) |
1214
|
94 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
458
|
95 { |
|
96 for (int i = 0; i < a.length (); i++) |
|
97 elem (i, i) = a.elem (i, i); |
|
98 } |
|
99 |
|
100 ComplexMatrix::ComplexMatrix (const ComplexDiagMatrix& a) |
1214
|
101 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
458
|
102 { |
|
103 for (int i = 0; i < a.length (); i++) |
|
104 elem (i, i) = a.elem (i, i); |
|
105 } |
|
106 |
1574
|
107 // XXX FIXME XXX -- could we use a templated mixed-type copy function |
|
108 // here? |
|
109 |
|
110 ComplexMatrix::ComplexMatrix (const charMatrix& a) |
|
111 { |
|
112 for (int i = 0; i < a.cols (); i++) |
|
113 for (int j = 0; j < a.rows (); j++) |
|
114 elem (i, j) = a.elem (i, j); |
|
115 } |
|
116 |
458
|
117 int |
|
118 ComplexMatrix::operator == (const ComplexMatrix& a) const |
|
119 { |
|
120 if (rows () != a.rows () || cols () != a.cols ()) |
|
121 return 0; |
|
122 |
|
123 return equal (data (), a.data (), length ()); |
|
124 } |
|
125 |
|
126 int |
|
127 ComplexMatrix::operator != (const ComplexMatrix& a) const |
|
128 { |
|
129 return !(*this == a); |
|
130 } |
|
131 |
|
132 // destructive insert/delete/reorder operations |
|
133 |
|
134 ComplexMatrix& |
|
135 ComplexMatrix::insert (const Matrix& a, int r, int c) |
|
136 { |
|
137 int a_nr = a.rows (); |
|
138 int a_nc = a.cols (); |
|
139 if (r < 0 || r + a_nr - 1 > rows () || c < 0 || c + a_nc - 1 > cols ()) |
|
140 { |
|
141 (*current_liboctave_error_handler) ("range error for insert"); |
|
142 return *this; |
|
143 } |
|
144 |
|
145 for (int j = 0; j < a_nc; j++) |
|
146 for (int i = 0; i < a_nr; i++) |
|
147 elem (r+i, c+j) = a.elem (i, j); |
|
148 |
|
149 return *this; |
|
150 } |
|
151 |
|
152 ComplexMatrix& |
|
153 ComplexMatrix::insert (const RowVector& a, int r, int c) |
|
154 { |
|
155 int a_len = a.length (); |
|
156 if (r < 0 || r >= rows () || c < 0 || c + a_len - 1 > cols ()) |
|
157 { |
|
158 (*current_liboctave_error_handler) ("range error for insert"); |
|
159 return *this; |
|
160 } |
|
161 |
|
162 for (int i = 0; i < a_len; i++) |
|
163 elem (r, c+i) = a.elem (i); |
|
164 |
|
165 return *this; |
|
166 } |
|
167 |
|
168 ComplexMatrix& |
|
169 ComplexMatrix::insert (const ColumnVector& a, int r, int c) |
|
170 { |
|
171 int a_len = a.length (); |
|
172 if (r < 0 || r + a_len - 1 > rows () || c < 0 || c >= cols ()) |
|
173 { |
|
174 (*current_liboctave_error_handler) ("range error for insert"); |
|
175 return *this; |
|
176 } |
|
177 |
|
178 for (int i = 0; i < a_len; i++) |
|
179 elem (r+i, c) = a.elem (i); |
|
180 |
|
181 return *this; |
|
182 } |
|
183 |
|
184 ComplexMatrix& |
|
185 ComplexMatrix::insert (const DiagMatrix& a, int r, int c) |
|
186 { |
|
187 if (r < 0 || r + a.rows () - 1 > rows () |
|
188 || c < 0 || c + a.cols () - 1 > cols ()) |
|
189 { |
|
190 (*current_liboctave_error_handler) ("range error for insert"); |
|
191 return *this; |
|
192 } |
|
193 |
|
194 for (int i = 0; i < a.length (); i++) |
|
195 elem (r+i, c+i) = a.elem (i, i); |
|
196 |
|
197 return *this; |
|
198 } |
|
199 |
|
200 ComplexMatrix& |
|
201 ComplexMatrix::insert (const ComplexMatrix& a, int r, int c) |
|
202 { |
1561
|
203 Array2<Complex>::insert (a, r, c); |
458
|
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 { |
1365
|
804 Complex *dummy = 0; |
1251
|
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 { |
1549
|
818 ComplexMatrix retval; |
|
819 |
740
|
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) |
1549
|
844 retval = ComplexMatrix (nc, nr, 0.0); |
740
|
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); |
1549
|
850 retval = Vr * D * Ur.hermitian (); |
740
|
851 } |
1549
|
852 |
|
853 return retval; |
740
|
854 } |
|
855 |
|
856 ComplexMatrix |
458
|
857 ComplexMatrix::fourier (void) const |
|
858 { |
|
859 int nr = rows (); |
|
860 int nc = cols (); |
|
861 int npts, nsamples; |
|
862 if (nr == 1 || nc == 1) |
|
863 { |
|
864 npts = nr > nc ? nr : nc; |
|
865 nsamples = 1; |
|
866 } |
|
867 else |
|
868 { |
|
869 npts = nr; |
|
870 nsamples = nc; |
|
871 } |
|
872 |
|
873 int nn = 4*npts+15; |
|
874 Complex *wsave = new Complex [nn]; |
|
875 Complex *tmp_data = dup (data (), length ()); |
|
876 |
1253
|
877 F77_FCN (cffti, CFFTI) (npts, wsave); |
458
|
878 |
|
879 for (int j = 0; j < nsamples; j++) |
1253
|
880 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], wsave); |
458
|
881 |
|
882 delete [] wsave; |
|
883 |
|
884 return ComplexMatrix (tmp_data, nr, nc); |
|
885 } |
|
886 |
|
887 ComplexMatrix |
|
888 ComplexMatrix::ifourier (void) const |
|
889 { |
|
890 int nr = rows (); |
|
891 int nc = cols (); |
|
892 int npts, nsamples; |
|
893 if (nr == 1 || nc == 1) |
|
894 { |
|
895 npts = nr > nc ? nr : nc; |
|
896 nsamples = 1; |
|
897 } |
|
898 else |
|
899 { |
|
900 npts = nr; |
|
901 nsamples = nc; |
|
902 } |
|
903 |
|
904 int nn = 4*npts+15; |
|
905 Complex *wsave = new Complex [nn]; |
|
906 Complex *tmp_data = dup (data (), length ()); |
|
907 |
1253
|
908 F77_FCN (cffti, CFFTI) (npts, wsave); |
458
|
909 |
|
910 for (int j = 0; j < nsamples; j++) |
1253
|
911 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], wsave); |
458
|
912 |
1321
|
913 for (int j = 0; j < npts*nsamples; j++) |
458
|
914 tmp_data[j] = tmp_data[j] / (double) npts; |
|
915 |
|
916 delete [] wsave; |
|
917 |
|
918 return ComplexMatrix (tmp_data, nr, nc); |
|
919 } |
|
920 |
677
|
921 ComplexMatrix |
|
922 ComplexMatrix::fourier2d (void) const |
|
923 { |
|
924 int nr = rows (); |
|
925 int nc = cols (); |
|
926 int npts, nsamples; |
|
927 if (nr == 1 || nc == 1) |
|
928 { |
|
929 npts = nr > nc ? nr : nc; |
|
930 nsamples = 1; |
|
931 } |
|
932 else |
|
933 { |
|
934 npts = nr; |
|
935 nsamples = nc; |
|
936 } |
|
937 |
|
938 int nn = 4*npts+15; |
|
939 Complex *wsave = new Complex [nn]; |
|
940 Complex *tmp_data = dup (data (), length ()); |
|
941 |
1253
|
942 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
943 |
|
944 for (int j = 0; j < nsamples; j++) |
1253
|
945 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], wsave); |
677
|
946 |
|
947 delete [] wsave; |
|
948 |
|
949 npts = nc; |
|
950 nsamples = nr; |
|
951 nn = 4*npts+15; |
|
952 wsave = new Complex [nn]; |
|
953 Complex *row = new Complex[npts]; |
|
954 |
1253
|
955 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
956 |
1321
|
957 for (int j = 0; j < nsamples; j++) |
677
|
958 { |
|
959 for (int i = 0; i < npts; i++) |
|
960 row[i] = tmp_data[i*nr + j]; |
|
961 |
1253
|
962 F77_FCN (cfftf, CFFTF) (npts, row, wsave); |
677
|
963 |
1321
|
964 for (int i = 0; i < npts; i++) |
677
|
965 tmp_data[i*nr + j] = row[i]; |
|
966 } |
|
967 |
|
968 delete [] wsave; |
|
969 delete [] row; |
|
970 |
|
971 return ComplexMatrix (tmp_data, nr, nc); |
|
972 } |
|
973 |
|
974 ComplexMatrix |
|
975 ComplexMatrix::ifourier2d (void) const |
|
976 { |
|
977 int nr = rows (); |
|
978 int nc = cols (); |
|
979 int npts, nsamples; |
|
980 if (nr == 1 || nc == 1) |
|
981 { |
|
982 npts = nr > nc ? nr : nc; |
|
983 nsamples = 1; |
|
984 } |
|
985 else |
|
986 { |
|
987 npts = nr; |
|
988 nsamples = nc; |
|
989 } |
|
990 |
|
991 int nn = 4*npts+15; |
|
992 Complex *wsave = new Complex [nn]; |
|
993 Complex *tmp_data = dup (data (), length ()); |
|
994 |
1253
|
995 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
996 |
|
997 for (int j = 0; j < nsamples; j++) |
1253
|
998 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], wsave); |
677
|
999 |
|
1000 delete [] wsave; |
|
1001 |
1321
|
1002 for (int j = 0; j < npts*nsamples; j++) |
677
|
1003 tmp_data[j] = tmp_data[j] / (double) npts; |
|
1004 |
|
1005 npts = nc; |
|
1006 nsamples = nr; |
|
1007 nn = 4*npts+15; |
|
1008 wsave = new Complex [nn]; |
|
1009 Complex *row = new Complex[npts]; |
|
1010 |
1253
|
1011 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
1012 |
1321
|
1013 for (int j = 0; j < nsamples; j++) |
677
|
1014 { |
|
1015 for (int i = 0; i < npts; i++) |
|
1016 row[i] = tmp_data[i*nr + j]; |
|
1017 |
1253
|
1018 F77_FCN (cfftb, CFFTB) (npts, row, wsave); |
677
|
1019 |
1321
|
1020 for (int i = 0; i < npts; i++) |
677
|
1021 tmp_data[i*nr + j] = row[i] / (double) npts; |
|
1022 } |
|
1023 |
|
1024 delete [] wsave; |
|
1025 delete [] row; |
|
1026 |
|
1027 return ComplexMatrix (tmp_data, nr, nc); |
|
1028 } |
|
1029 |
458
|
1030 ComplexDET |
|
1031 ComplexMatrix::determinant (void) const |
|
1032 { |
|
1033 int info; |
|
1034 double rcond; |
|
1035 return determinant (info, rcond); |
|
1036 } |
|
1037 |
|
1038 ComplexDET |
|
1039 ComplexMatrix::determinant (int& info) const |
|
1040 { |
|
1041 double rcond; |
|
1042 return determinant (info, rcond); |
|
1043 } |
|
1044 |
|
1045 ComplexDET |
532
|
1046 ComplexMatrix::determinant (int& info, double& rcond) const |
458
|
1047 { |
|
1048 ComplexDET retval; |
|
1049 |
|
1050 int nr = rows (); |
|
1051 int nc = cols (); |
|
1052 |
|
1053 if (nr == 0 || nc == 0) |
|
1054 { |
|
1055 Complex d[2]; |
|
1056 d[0] = 1.0; |
|
1057 d[1] = 0.0; |
|
1058 retval = ComplexDET (d); |
|
1059 } |
|
1060 else |
|
1061 { |
|
1062 info = 0; |
|
1063 int *ipvt = new int [nr]; |
|
1064 |
|
1065 Complex *z = new Complex [nr]; |
|
1066 Complex *tmp_data = dup (data (), length ()); |
|
1067 |
1253
|
1068 F77_FCN (zgeco, ZGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
1069 |
1195
|
1070 volatile double rcond_plus_one = rcond + 1.0; |
|
1071 if (rcond_plus_one == 1.0) |
458
|
1072 { |
|
1073 info = -1; |
|
1074 retval = ComplexDET (); |
|
1075 } |
|
1076 else |
|
1077 { |
|
1078 Complex d[2]; |
1253
|
1079 F77_FCN (zgedi, ZGEDI) (tmp_data, nr, nr, ipvt, d, z, 10); |
458
|
1080 retval = ComplexDET (d); |
|
1081 } |
|
1082 |
|
1083 delete [] tmp_data; |
|
1084 delete [] ipvt; |
|
1085 delete [] z; |
|
1086 } |
|
1087 |
|
1088 return retval; |
|
1089 } |
|
1090 |
|
1091 ComplexMatrix |
|
1092 ComplexMatrix::solve (const Matrix& b) const |
|
1093 { |
|
1094 int info; |
|
1095 double rcond; |
|
1096 return solve (b, info, rcond); |
|
1097 } |
|
1098 |
|
1099 ComplexMatrix |
|
1100 ComplexMatrix::solve (const Matrix& b, int& info) const |
|
1101 { |
|
1102 double rcond; |
|
1103 return solve (b, info, rcond); |
|
1104 } |
|
1105 |
|
1106 ComplexMatrix |
|
1107 ComplexMatrix::solve (const Matrix& b, int& info, double& rcond) const |
|
1108 { |
|
1109 ComplexMatrix tmp (b); |
|
1110 return solve (tmp, info, rcond); |
|
1111 } |
|
1112 |
|
1113 ComplexMatrix |
|
1114 ComplexMatrix::solve (const ComplexMatrix& b) const |
|
1115 { |
|
1116 int info; |
|
1117 double rcond; |
|
1118 return solve (b, info, rcond); |
|
1119 } |
|
1120 |
|
1121 ComplexMatrix |
|
1122 ComplexMatrix::solve (const ComplexMatrix& b, int& info) const |
|
1123 { |
|
1124 double rcond; |
|
1125 return solve (b, info, rcond); |
|
1126 } |
|
1127 ComplexMatrix |
532
|
1128 ComplexMatrix::solve (const ComplexMatrix& b, int& info, double& rcond) const |
458
|
1129 { |
|
1130 ComplexMatrix retval; |
|
1131 |
|
1132 int nr = rows (); |
|
1133 int nc = cols (); |
|
1134 int b_nr = b.rows (); |
|
1135 int b_nc = b.cols (); |
|
1136 if (nr == 0 || nc == 0 || nr != nc || nr != b_nr) |
|
1137 { |
|
1138 (*current_liboctave_error_handler) |
|
1139 ("matrix dimension mismatch in solution of linear equations"); |
|
1140 return ComplexMatrix (); |
|
1141 } |
|
1142 |
|
1143 info = 0; |
|
1144 int *ipvt = new int [nr]; |
|
1145 |
|
1146 Complex *z = new Complex [nr]; |
|
1147 Complex *tmp_data = dup (data (), length ()); |
|
1148 |
1253
|
1149 F77_FCN (zgeco, ZGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
1150 |
1195
|
1151 volatile double rcond_plus_one = rcond + 1.0; |
|
1152 if (rcond_plus_one == 1.0) |
458
|
1153 { |
|
1154 info = -2; |
|
1155 } |
|
1156 else |
|
1157 { |
|
1158 Complex *result = dup (b.data (), b.length ()); |
|
1159 |
|
1160 for (int j = 0; j < b_nc; j++) |
1253
|
1161 F77_FCN (zgesl, ZGESL) (tmp_data, nr, nr, ipvt, &result[nr*j], 0); |
458
|
1162 |
|
1163 retval = ComplexMatrix (result, b_nr, b_nc); |
|
1164 } |
|
1165 |
|
1166 delete [] tmp_data; |
|
1167 delete [] ipvt; |
|
1168 delete [] z; |
|
1169 |
|
1170 return retval; |
|
1171 } |
|
1172 |
|
1173 ComplexColumnVector |
|
1174 ComplexMatrix::solve (const ComplexColumnVector& b) const |
|
1175 { |
|
1176 int info; |
|
1177 double rcond; |
|
1178 return solve (b, info, rcond); |
|
1179 } |
|
1180 |
|
1181 ComplexColumnVector |
|
1182 ComplexMatrix::solve (const ComplexColumnVector& b, int& info) const |
|
1183 { |
|
1184 double rcond; |
|
1185 return solve (b, info, rcond); |
|
1186 } |
|
1187 |
|
1188 ComplexColumnVector |
|
1189 ComplexMatrix::solve (const ComplexColumnVector& b, int& info, |
532
|
1190 double& rcond) const |
458
|
1191 { |
|
1192 ComplexColumnVector retval; |
|
1193 |
|
1194 int nr = rows (); |
|
1195 int nc = cols (); |
|
1196 int b_len = b.length (); |
|
1197 if (nr == 0 || nc == 0 || nr != nc || nr != b_len) |
|
1198 { |
|
1199 (*current_liboctave_error_handler) |
|
1200 ("matrix dimension mismatch in solution of linear equations"); |
|
1201 return ComplexColumnVector (); |
|
1202 } |
|
1203 |
|
1204 info = 0; |
|
1205 int *ipvt = new int [nr]; |
|
1206 |
|
1207 Complex *z = new Complex [nr]; |
|
1208 Complex *tmp_data = dup (data (), length ()); |
|
1209 |
1253
|
1210 F77_FCN (zgeco, ZGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
1211 |
1195
|
1212 volatile double rcond_plus_one = rcond + 1.0; |
|
1213 if (rcond_plus_one == 1.0) |
458
|
1214 { |
|
1215 info = -2; |
|
1216 } |
|
1217 else |
|
1218 { |
|
1219 Complex *result = dup (b.data (), b_len); |
|
1220 |
1253
|
1221 F77_FCN (zgesl, ZGESL) (tmp_data, nr, nr, ipvt, result, 0); |
458
|
1222 |
|
1223 retval = ComplexColumnVector (result, b_len); |
|
1224 } |
|
1225 |
|
1226 delete [] tmp_data; |
|
1227 delete [] ipvt; |
|
1228 delete [] z; |
|
1229 |
|
1230 return retval; |
|
1231 } |
|
1232 |
|
1233 ComplexMatrix |
|
1234 ComplexMatrix::lssolve (const ComplexMatrix& b) const |
|
1235 { |
|
1236 int info; |
|
1237 int rank; |
|
1238 return lssolve (b, info, rank); |
|
1239 } |
|
1240 |
|
1241 ComplexMatrix |
|
1242 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info) const |
|
1243 { |
|
1244 int rank; |
|
1245 return lssolve (b, info, rank); |
|
1246 } |
|
1247 |
|
1248 ComplexMatrix |
|
1249 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info, int& rank) const |
|
1250 { |
|
1251 int nrhs = b.cols (); |
|
1252 |
|
1253 int m = rows (); |
|
1254 int n = cols (); |
|
1255 |
|
1256 if (m == 0 || n == 0 || m != b.rows ()) |
|
1257 { |
|
1258 (*current_liboctave_error_handler) |
|
1259 ("matrix dimension mismatch solution of linear equations"); |
|
1260 return Matrix (); |
|
1261 } |
|
1262 |
|
1263 Complex *tmp_data = dup (data (), length ()); |
|
1264 |
|
1265 int nrr = m > n ? m : n; |
|
1266 ComplexMatrix result (nrr, nrhs); |
|
1267 |
1321
|
1268 for (int j = 0; j < nrhs; j++) |
|
1269 for (int i = 0; i < m; i++) |
458
|
1270 result.elem (i, j) = b.elem (i, j); |
|
1271 |
|
1272 Complex *presult = result.fortran_vec (); |
|
1273 |
|
1274 int len_s = m < n ? m : n; |
|
1275 double *s = new double [len_s]; |
|
1276 double rcond = -1.0; |
|
1277 int lwork; |
|
1278 if (m < n) |
|
1279 lwork = 2*m + (nrhs > n ? nrhs : n); |
|
1280 else |
|
1281 lwork = 2*n + (nrhs > m ? nrhs : m); |
|
1282 |
|
1283 Complex *work = new Complex [lwork]; |
|
1284 |
|
1285 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1286 lrwork = lrwork > 1 ? lrwork : 1; |
|
1287 double *rwork = new double [lrwork]; |
|
1288 |
1253
|
1289 F77_FCN (zgelss, ZGELSS) (m, n, nrhs, tmp_data, m, presult, nrr, s, |
|
1290 rcond, rank, work, lwork, rwork, info); |
458
|
1291 |
|
1292 ComplexMatrix retval (n, nrhs); |
1321
|
1293 for (int j = 0; j < nrhs; j++) |
|
1294 for (int i = 0; i < n; i++) |
458
|
1295 retval.elem (i, j) = result.elem (i, j); |
|
1296 |
|
1297 delete [] tmp_data; |
|
1298 delete [] s; |
|
1299 delete [] work; |
|
1300 delete [] rwork; |
|
1301 |
|
1302 return retval; |
|
1303 } |
|
1304 |
|
1305 ComplexColumnVector |
|
1306 ComplexMatrix::lssolve (const ComplexColumnVector& b) const |
|
1307 { |
|
1308 int info; |
|
1309 int rank; |
|
1310 return lssolve (b, info, rank); |
|
1311 } |
|
1312 |
|
1313 ComplexColumnVector |
|
1314 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info) const |
|
1315 { |
|
1316 int rank; |
|
1317 return lssolve (b, info, rank); |
|
1318 } |
|
1319 |
|
1320 ComplexColumnVector |
|
1321 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info, |
|
1322 int& rank) const |
|
1323 { |
|
1324 int nrhs = 1; |
|
1325 |
|
1326 int m = rows (); |
|
1327 int n = cols (); |
|
1328 |
|
1329 if (m == 0 || n == 0 || m != b.length ()) |
|
1330 { |
|
1331 (*current_liboctave_error_handler) |
|
1332 ("matrix dimension mismatch solution of least squares problem"); |
|
1333 return ComplexColumnVector (); |
|
1334 } |
|
1335 |
|
1336 Complex *tmp_data = dup (data (), length ()); |
|
1337 |
|
1338 int nrr = m > n ? m : n; |
|
1339 ComplexColumnVector result (nrr); |
|
1340 |
1321
|
1341 for (int i = 0; i < m; i++) |
458
|
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); |
1321
|
1365 for (int i = 0; i < n; i++) |
458
|
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 */ |