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