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