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