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