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 { |
|
820 ComplexSVD result (*this); |
|
821 |
|
822 DiagMatrix S = result.singular_values (); |
|
823 ComplexMatrix U = result.left_singular_matrix (); |
|
824 ComplexMatrix V = result.right_singular_matrix (); |
|
825 |
|
826 ColumnVector sigma = S.diag (); |
|
827 |
|
828 int r = sigma.length () - 1; |
|
829 int nr = rows (); |
|
830 int nc = cols (); |
|
831 |
|
832 if (tol <= 0.0) |
|
833 { |
|
834 if (nr > nc) |
|
835 tol = nr * sigma.elem (0) * DBL_EPSILON; |
|
836 else |
|
837 tol = nc * sigma.elem (0) * DBL_EPSILON; |
|
838 } |
|
839 |
|
840 while (r >= 0 && sigma.elem (r) < tol) |
|
841 r--; |
|
842 |
|
843 if (r < 0) |
|
844 return ComplexMatrix (nc, nr, 0.0); |
|
845 else |
|
846 { |
|
847 ComplexMatrix Ur = U.extract (0, 0, nr-1, r); |
|
848 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); |
|
849 ComplexMatrix Vr = V.extract (0, 0, nc-1, r); |
|
850 return Vr * D * Ur.hermitian (); |
|
851 } |
|
852 } |
|
853 |
|
854 ComplexMatrix |
458
|
855 ComplexMatrix::fourier (void) const |
|
856 { |
|
857 int nr = rows (); |
|
858 int nc = cols (); |
|
859 int npts, nsamples; |
|
860 if (nr == 1 || nc == 1) |
|
861 { |
|
862 npts = nr > nc ? nr : nc; |
|
863 nsamples = 1; |
|
864 } |
|
865 else |
|
866 { |
|
867 npts = nr; |
|
868 nsamples = nc; |
|
869 } |
|
870 |
|
871 int nn = 4*npts+15; |
|
872 Complex *wsave = new Complex [nn]; |
|
873 Complex *tmp_data = dup (data (), length ()); |
|
874 |
1253
|
875 F77_FCN (cffti, CFFTI) (npts, wsave); |
458
|
876 |
|
877 for (int j = 0; j < nsamples; j++) |
1253
|
878 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], wsave); |
458
|
879 |
|
880 delete [] wsave; |
|
881 |
|
882 return ComplexMatrix (tmp_data, nr, nc); |
|
883 } |
|
884 |
|
885 ComplexMatrix |
|
886 ComplexMatrix::ifourier (void) const |
|
887 { |
|
888 int nr = rows (); |
|
889 int nc = cols (); |
|
890 int npts, nsamples; |
|
891 if (nr == 1 || nc == 1) |
|
892 { |
|
893 npts = nr > nc ? nr : nc; |
|
894 nsamples = 1; |
|
895 } |
|
896 else |
|
897 { |
|
898 npts = nr; |
|
899 nsamples = nc; |
|
900 } |
|
901 |
|
902 int nn = 4*npts+15; |
|
903 Complex *wsave = new Complex [nn]; |
|
904 Complex *tmp_data = dup (data (), length ()); |
|
905 |
1253
|
906 F77_FCN (cffti, CFFTI) (npts, wsave); |
458
|
907 |
|
908 for (int j = 0; j < nsamples; j++) |
1253
|
909 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], wsave); |
458
|
910 |
1321
|
911 for (int j = 0; j < npts*nsamples; j++) |
458
|
912 tmp_data[j] = tmp_data[j] / (double) npts; |
|
913 |
|
914 delete [] wsave; |
|
915 |
|
916 return ComplexMatrix (tmp_data, nr, nc); |
|
917 } |
|
918 |
677
|
919 ComplexMatrix |
|
920 ComplexMatrix::fourier2d (void) const |
|
921 { |
|
922 int nr = rows (); |
|
923 int nc = cols (); |
|
924 int npts, nsamples; |
|
925 if (nr == 1 || nc == 1) |
|
926 { |
|
927 npts = nr > nc ? nr : nc; |
|
928 nsamples = 1; |
|
929 } |
|
930 else |
|
931 { |
|
932 npts = nr; |
|
933 nsamples = nc; |
|
934 } |
|
935 |
|
936 int nn = 4*npts+15; |
|
937 Complex *wsave = new Complex [nn]; |
|
938 Complex *tmp_data = dup (data (), length ()); |
|
939 |
1253
|
940 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
941 |
|
942 for (int j = 0; j < nsamples; j++) |
1253
|
943 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], wsave); |
677
|
944 |
|
945 delete [] wsave; |
|
946 |
|
947 npts = nc; |
|
948 nsamples = nr; |
|
949 nn = 4*npts+15; |
|
950 wsave = new Complex [nn]; |
|
951 Complex *row = new Complex[npts]; |
|
952 |
1253
|
953 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
954 |
1321
|
955 for (int j = 0; j < nsamples; j++) |
677
|
956 { |
|
957 for (int i = 0; i < npts; i++) |
|
958 row[i] = tmp_data[i*nr + j]; |
|
959 |
1253
|
960 F77_FCN (cfftf, CFFTF) (npts, row, wsave); |
677
|
961 |
1321
|
962 for (int i = 0; i < npts; i++) |
677
|
963 tmp_data[i*nr + j] = row[i]; |
|
964 } |
|
965 |
|
966 delete [] wsave; |
|
967 delete [] row; |
|
968 |
|
969 return ComplexMatrix (tmp_data, nr, nc); |
|
970 } |
|
971 |
|
972 ComplexMatrix |
|
973 ComplexMatrix::ifourier2d (void) const |
|
974 { |
|
975 int nr = rows (); |
|
976 int nc = cols (); |
|
977 int npts, nsamples; |
|
978 if (nr == 1 || nc == 1) |
|
979 { |
|
980 npts = nr > nc ? nr : nc; |
|
981 nsamples = 1; |
|
982 } |
|
983 else |
|
984 { |
|
985 npts = nr; |
|
986 nsamples = nc; |
|
987 } |
|
988 |
|
989 int nn = 4*npts+15; |
|
990 Complex *wsave = new Complex [nn]; |
|
991 Complex *tmp_data = dup (data (), length ()); |
|
992 |
1253
|
993 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
994 |
|
995 for (int j = 0; j < nsamples; j++) |
1253
|
996 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], wsave); |
677
|
997 |
|
998 delete [] wsave; |
|
999 |
1321
|
1000 for (int j = 0; j < npts*nsamples; j++) |
677
|
1001 tmp_data[j] = tmp_data[j] / (double) npts; |
|
1002 |
|
1003 npts = nc; |
|
1004 nsamples = nr; |
|
1005 nn = 4*npts+15; |
|
1006 wsave = new Complex [nn]; |
|
1007 Complex *row = new Complex[npts]; |
|
1008 |
1253
|
1009 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
1010 |
1321
|
1011 for (int j = 0; j < nsamples; j++) |
677
|
1012 { |
|
1013 for (int i = 0; i < npts; i++) |
|
1014 row[i] = tmp_data[i*nr + j]; |
|
1015 |
1253
|
1016 F77_FCN (cfftb, CFFTB) (npts, row, wsave); |
677
|
1017 |
1321
|
1018 for (int i = 0; i < npts; i++) |
677
|
1019 tmp_data[i*nr + j] = row[i] / (double) npts; |
|
1020 } |
|
1021 |
|
1022 delete [] wsave; |
|
1023 delete [] row; |
|
1024 |
|
1025 return ComplexMatrix (tmp_data, nr, nc); |
|
1026 } |
|
1027 |
458
|
1028 ComplexDET |
|
1029 ComplexMatrix::determinant (void) const |
|
1030 { |
|
1031 int info; |
|
1032 double rcond; |
|
1033 return determinant (info, rcond); |
|
1034 } |
|
1035 |
|
1036 ComplexDET |
|
1037 ComplexMatrix::determinant (int& info) const |
|
1038 { |
|
1039 double rcond; |
|
1040 return determinant (info, rcond); |
|
1041 } |
|
1042 |
|
1043 ComplexDET |
532
|
1044 ComplexMatrix::determinant (int& info, double& rcond) const |
458
|
1045 { |
|
1046 ComplexDET retval; |
|
1047 |
|
1048 int nr = rows (); |
|
1049 int nc = cols (); |
|
1050 |
|
1051 if (nr == 0 || nc == 0) |
|
1052 { |
|
1053 Complex d[2]; |
|
1054 d[0] = 1.0; |
|
1055 d[1] = 0.0; |
|
1056 retval = ComplexDET (d); |
|
1057 } |
|
1058 else |
|
1059 { |
|
1060 info = 0; |
|
1061 int *ipvt = new int [nr]; |
|
1062 |
|
1063 Complex *z = new Complex [nr]; |
|
1064 Complex *tmp_data = dup (data (), length ()); |
|
1065 |
1253
|
1066 F77_FCN (zgeco, ZGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
1067 |
1195
|
1068 volatile double rcond_plus_one = rcond + 1.0; |
|
1069 if (rcond_plus_one == 1.0) |
458
|
1070 { |
|
1071 info = -1; |
|
1072 retval = ComplexDET (); |
|
1073 } |
|
1074 else |
|
1075 { |
|
1076 Complex d[2]; |
1253
|
1077 F77_FCN (zgedi, ZGEDI) (tmp_data, nr, nr, ipvt, d, z, 10); |
458
|
1078 retval = ComplexDET (d); |
|
1079 } |
|
1080 |
|
1081 delete [] tmp_data; |
|
1082 delete [] ipvt; |
|
1083 delete [] z; |
|
1084 } |
|
1085 |
|
1086 return retval; |
|
1087 } |
|
1088 |
|
1089 ComplexMatrix |
|
1090 ComplexMatrix::solve (const Matrix& b) const |
|
1091 { |
|
1092 int info; |
|
1093 double rcond; |
|
1094 return solve (b, info, rcond); |
|
1095 } |
|
1096 |
|
1097 ComplexMatrix |
|
1098 ComplexMatrix::solve (const Matrix& b, int& info) const |
|
1099 { |
|
1100 double rcond; |
|
1101 return solve (b, info, rcond); |
|
1102 } |
|
1103 |
|
1104 ComplexMatrix |
|
1105 ComplexMatrix::solve (const Matrix& b, int& info, double& rcond) const |
|
1106 { |
|
1107 ComplexMatrix tmp (b); |
|
1108 return solve (tmp, info, rcond); |
|
1109 } |
|
1110 |
|
1111 ComplexMatrix |
|
1112 ComplexMatrix::solve (const ComplexMatrix& b) const |
|
1113 { |
|
1114 int info; |
|
1115 double rcond; |
|
1116 return solve (b, info, rcond); |
|
1117 } |
|
1118 |
|
1119 ComplexMatrix |
|
1120 ComplexMatrix::solve (const ComplexMatrix& b, int& info) const |
|
1121 { |
|
1122 double rcond; |
|
1123 return solve (b, info, rcond); |
|
1124 } |
|
1125 ComplexMatrix |
532
|
1126 ComplexMatrix::solve (const ComplexMatrix& b, int& info, double& rcond) const |
458
|
1127 { |
|
1128 ComplexMatrix retval; |
|
1129 |
|
1130 int nr = rows (); |
|
1131 int nc = cols (); |
|
1132 int b_nr = b.rows (); |
|
1133 int b_nc = b.cols (); |
|
1134 if (nr == 0 || nc == 0 || nr != nc || nr != b_nr) |
|
1135 { |
|
1136 (*current_liboctave_error_handler) |
|
1137 ("matrix dimension mismatch in solution of linear equations"); |
|
1138 return ComplexMatrix (); |
|
1139 } |
|
1140 |
|
1141 info = 0; |
|
1142 int *ipvt = new int [nr]; |
|
1143 |
|
1144 Complex *z = new Complex [nr]; |
|
1145 Complex *tmp_data = dup (data (), length ()); |
|
1146 |
1253
|
1147 F77_FCN (zgeco, ZGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
1148 |
1195
|
1149 volatile double rcond_plus_one = rcond + 1.0; |
|
1150 if (rcond_plus_one == 1.0) |
458
|
1151 { |
|
1152 info = -2; |
|
1153 } |
|
1154 else |
|
1155 { |
|
1156 Complex *result = dup (b.data (), b.length ()); |
|
1157 |
|
1158 for (int j = 0; j < b_nc; j++) |
1253
|
1159 F77_FCN (zgesl, ZGESL) (tmp_data, nr, nr, ipvt, &result[nr*j], 0); |
458
|
1160 |
|
1161 retval = ComplexMatrix (result, b_nr, b_nc); |
|
1162 } |
|
1163 |
|
1164 delete [] tmp_data; |
|
1165 delete [] ipvt; |
|
1166 delete [] z; |
|
1167 |
|
1168 return retval; |
|
1169 } |
|
1170 |
|
1171 ComplexColumnVector |
|
1172 ComplexMatrix::solve (const ComplexColumnVector& b) const |
|
1173 { |
|
1174 int info; |
|
1175 double rcond; |
|
1176 return solve (b, info, rcond); |
|
1177 } |
|
1178 |
|
1179 ComplexColumnVector |
|
1180 ComplexMatrix::solve (const ComplexColumnVector& b, int& info) const |
|
1181 { |
|
1182 double rcond; |
|
1183 return solve (b, info, rcond); |
|
1184 } |
|
1185 |
|
1186 ComplexColumnVector |
|
1187 ComplexMatrix::solve (const ComplexColumnVector& b, int& info, |
532
|
1188 double& rcond) const |
458
|
1189 { |
|
1190 ComplexColumnVector retval; |
|
1191 |
|
1192 int nr = rows (); |
|
1193 int nc = cols (); |
|
1194 int b_len = b.length (); |
|
1195 if (nr == 0 || nc == 0 || nr != nc || nr != b_len) |
|
1196 { |
|
1197 (*current_liboctave_error_handler) |
|
1198 ("matrix dimension mismatch in solution of linear equations"); |
|
1199 return ComplexColumnVector (); |
|
1200 } |
|
1201 |
|
1202 info = 0; |
|
1203 int *ipvt = new int [nr]; |
|
1204 |
|
1205 Complex *z = new Complex [nr]; |
|
1206 Complex *tmp_data = dup (data (), length ()); |
|
1207 |
1253
|
1208 F77_FCN (zgeco, ZGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
1209 |
1195
|
1210 volatile double rcond_plus_one = rcond + 1.0; |
|
1211 if (rcond_plus_one == 1.0) |
458
|
1212 { |
|
1213 info = -2; |
|
1214 } |
|
1215 else |
|
1216 { |
|
1217 Complex *result = dup (b.data (), b_len); |
|
1218 |
1253
|
1219 F77_FCN (zgesl, ZGESL) (tmp_data, nr, nr, ipvt, result, 0); |
458
|
1220 |
|
1221 retval = ComplexColumnVector (result, b_len); |
|
1222 } |
|
1223 |
|
1224 delete [] tmp_data; |
|
1225 delete [] ipvt; |
|
1226 delete [] z; |
|
1227 |
|
1228 return retval; |
|
1229 } |
|
1230 |
|
1231 ComplexMatrix |
|
1232 ComplexMatrix::lssolve (const ComplexMatrix& b) const |
|
1233 { |
|
1234 int info; |
|
1235 int rank; |
|
1236 return lssolve (b, info, rank); |
|
1237 } |
|
1238 |
|
1239 ComplexMatrix |
|
1240 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info) const |
|
1241 { |
|
1242 int rank; |
|
1243 return lssolve (b, info, rank); |
|
1244 } |
|
1245 |
|
1246 ComplexMatrix |
|
1247 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info, int& rank) const |
|
1248 { |
|
1249 int nrhs = b.cols (); |
|
1250 |
|
1251 int m = rows (); |
|
1252 int n = cols (); |
|
1253 |
|
1254 if (m == 0 || n == 0 || m != b.rows ()) |
|
1255 { |
|
1256 (*current_liboctave_error_handler) |
|
1257 ("matrix dimension mismatch solution of linear equations"); |
|
1258 return Matrix (); |
|
1259 } |
|
1260 |
|
1261 Complex *tmp_data = dup (data (), length ()); |
|
1262 |
|
1263 int nrr = m > n ? m : n; |
|
1264 ComplexMatrix result (nrr, nrhs); |
|
1265 |
1321
|
1266 for (int j = 0; j < nrhs; j++) |
|
1267 for (int i = 0; i < m; i++) |
458
|
1268 result.elem (i, j) = b.elem (i, j); |
|
1269 |
|
1270 Complex *presult = result.fortran_vec (); |
|
1271 |
|
1272 int len_s = m < n ? m : n; |
|
1273 double *s = new double [len_s]; |
|
1274 double rcond = -1.0; |
|
1275 int lwork; |
|
1276 if (m < n) |
|
1277 lwork = 2*m + (nrhs > n ? nrhs : n); |
|
1278 else |
|
1279 lwork = 2*n + (nrhs > m ? nrhs : m); |
|
1280 |
|
1281 Complex *work = new Complex [lwork]; |
|
1282 |
|
1283 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1284 lrwork = lrwork > 1 ? lrwork : 1; |
|
1285 double *rwork = new double [lrwork]; |
|
1286 |
1253
|
1287 F77_FCN (zgelss, ZGELSS) (m, n, nrhs, tmp_data, m, presult, nrr, s, |
|
1288 rcond, rank, work, lwork, rwork, info); |
458
|
1289 |
|
1290 ComplexMatrix retval (n, nrhs); |
1321
|
1291 for (int j = 0; j < nrhs; j++) |
|
1292 for (int i = 0; i < n; i++) |
458
|
1293 retval.elem (i, j) = result.elem (i, j); |
|
1294 |
|
1295 delete [] tmp_data; |
|
1296 delete [] s; |
|
1297 delete [] work; |
|
1298 delete [] rwork; |
|
1299 |
|
1300 return retval; |
|
1301 } |
|
1302 |
|
1303 ComplexColumnVector |
|
1304 ComplexMatrix::lssolve (const ComplexColumnVector& b) const |
|
1305 { |
|
1306 int info; |
|
1307 int rank; |
|
1308 return lssolve (b, info, rank); |
|
1309 } |
|
1310 |
|
1311 ComplexColumnVector |
|
1312 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info) const |
|
1313 { |
|
1314 int rank; |
|
1315 return lssolve (b, info, rank); |
|
1316 } |
|
1317 |
|
1318 ComplexColumnVector |
|
1319 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info, |
|
1320 int& rank) const |
|
1321 { |
|
1322 int nrhs = 1; |
|
1323 |
|
1324 int m = rows (); |
|
1325 int n = cols (); |
|
1326 |
|
1327 if (m == 0 || n == 0 || m != b.length ()) |
|
1328 { |
|
1329 (*current_liboctave_error_handler) |
|
1330 ("matrix dimension mismatch solution of least squares problem"); |
|
1331 return ComplexColumnVector (); |
|
1332 } |
|
1333 |
|
1334 Complex *tmp_data = dup (data (), length ()); |
|
1335 |
|
1336 int nrr = m > n ? m : n; |
|
1337 ComplexColumnVector result (nrr); |
|
1338 |
1321
|
1339 for (int i = 0; i < m; i++) |
458
|
1340 result.elem (i) = b.elem (i); |
|
1341 |
|
1342 Complex *presult = result.fortran_vec (); |
|
1343 |
|
1344 int len_s = m < n ? m : n; |
|
1345 double *s = new double [len_s]; |
|
1346 double rcond = -1.0; |
|
1347 int lwork; |
|
1348 if (m < n) |
|
1349 lwork = 2*m + (nrhs > n ? nrhs : n); |
|
1350 else |
|
1351 lwork = 2*n + (nrhs > m ? nrhs : m); |
|
1352 |
|
1353 Complex *work = new Complex [lwork]; |
|
1354 |
|
1355 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1356 lrwork = lrwork > 1 ? lrwork : 1; |
|
1357 double *rwork = new double [lrwork]; |
|
1358 |
1253
|
1359 F77_FCN (zgelss, ZGELSS) (m, n, nrhs, tmp_data, m, presult, nrr, s, |
|
1360 rcond, rank, work, lwork, rwork, info); |
458
|
1361 |
|
1362 ComplexColumnVector retval (n); |
1321
|
1363 for (int i = 0; i < n; i++) |
458
|
1364 retval.elem (i) = result.elem (i); |
|
1365 |
|
1366 delete [] tmp_data; |
|
1367 delete [] s; |
|
1368 delete [] work; |
|
1369 delete [] rwork; |
|
1370 |
|
1371 return retval; |
|
1372 } |
|
1373 |
1205
|
1374 // column vector by row vector -> matrix operations |
|
1375 |
|
1376 ComplexMatrix |
|
1377 operator * (const ColumnVector& v, const ComplexRowVector& a) |
|
1378 { |
|
1379 ComplexColumnVector tmp (v); |
|
1380 return tmp * a; |
|
1381 } |
|
1382 |
|
1383 ComplexMatrix |
|
1384 operator * (const ComplexColumnVector& a, const RowVector& b) |
|
1385 { |
|
1386 ComplexRowVector tmp (b); |
|
1387 return a * tmp; |
|
1388 } |
|
1389 |
|
1390 ComplexMatrix |
|
1391 operator * (const ComplexColumnVector& v, const ComplexRowVector& a) |
|
1392 { |
|
1393 int len = v.length (); |
|
1394 int a_len = a.length (); |
|
1395 if (len != a_len) |
|
1396 { |
|
1397 (*current_liboctave_error_handler) |
|
1398 ("nonconformant vector multiplication attempted"); |
|
1399 return ComplexMatrix (); |
|
1400 } |
|
1401 |
|
1402 if (len == 0) |
|
1403 return ComplexMatrix (len, len, 0.0); |
|
1404 |
|
1405 Complex *c = new Complex [len * a_len]; |
|
1406 |
1253
|
1407 F77_FCN (zgemm, ZGEMM) ("N", "N", len, a_len, 1, 1.0, v.data (), |
|
1408 len, a.data (), 1, 0.0, c, len, 1L, 1L); |
1205
|
1409 |
|
1410 return ComplexMatrix (c, len, a_len); |
|
1411 } |
|
1412 |
|
1413 // diagonal matrix by scalar -> matrix operations |
|
1414 |
|
1415 ComplexMatrix |
|
1416 operator + (const DiagMatrix& a, const Complex& s) |
|
1417 { |
|
1418 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1419 return a + tmp; |
|
1420 } |
|
1421 |
|
1422 ComplexMatrix |
|
1423 operator - (const DiagMatrix& a, const Complex& s) |
|
1424 { |
|
1425 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1426 return a + tmp; |
|
1427 } |
|
1428 |
|
1429 ComplexMatrix |
|
1430 operator + (const ComplexDiagMatrix& a, double s) |
|
1431 { |
|
1432 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1433 return a + tmp; |
|
1434 } |
|
1435 |
|
1436 ComplexMatrix |
|
1437 operator - (const ComplexDiagMatrix& a, double s) |
|
1438 { |
|
1439 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1440 return a + tmp; |
|
1441 } |
|
1442 |
|
1443 ComplexMatrix |
|
1444 operator + (const ComplexDiagMatrix& a, const Complex& s) |
|
1445 { |
|
1446 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1447 return a + tmp; |
|
1448 } |
|
1449 |
|
1450 ComplexMatrix |
|
1451 operator - (const ComplexDiagMatrix& a, const Complex& s) |
|
1452 { |
|
1453 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1454 return a + tmp; |
|
1455 } |
|
1456 |
|
1457 // scalar by diagonal matrix -> matrix operations |
|
1458 |
|
1459 ComplexMatrix |
|
1460 operator + (const Complex& s, const DiagMatrix& a) |
|
1461 { |
|
1462 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1463 return tmp + a; |
|
1464 } |
|
1465 |
|
1466 ComplexMatrix |
|
1467 operator - (const Complex& s, const DiagMatrix& a) |
|
1468 { |
|
1469 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1470 return tmp - a; |
|
1471 } |
|
1472 |
|
1473 ComplexMatrix |
|
1474 operator + (double s, const ComplexDiagMatrix& a) |
|
1475 { |
|
1476 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1477 return tmp + a; |
|
1478 } |
|
1479 |
|
1480 ComplexMatrix |
|
1481 operator - (double s, const ComplexDiagMatrix& a) |
|
1482 { |
|
1483 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1484 return tmp - a; |
|
1485 } |
|
1486 |
|
1487 ComplexMatrix |
|
1488 operator + (const Complex& s, const ComplexDiagMatrix& a) |
|
1489 { |
|
1490 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1491 return tmp + a; |
|
1492 } |
|
1493 |
|
1494 ComplexMatrix |
|
1495 operator - (const Complex& s, const ComplexDiagMatrix& a) |
|
1496 { |
|
1497 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1498 return tmp - a; |
|
1499 } |
|
1500 |
458
|
1501 // matrix by diagonal matrix -> matrix operations |
|
1502 |
|
1503 ComplexMatrix& |
|
1504 ComplexMatrix::operator += (const DiagMatrix& a) |
|
1505 { |
|
1506 int nr = rows (); |
|
1507 int nc = cols (); |
|
1508 if (nr != a.rows () || nc != a.cols ()) |
|
1509 { |
|
1510 (*current_liboctave_error_handler) |
|
1511 ("nonconformant matrix += operation attempted"); |
889
|
1512 return *this; |
458
|
1513 } |
|
1514 |
|
1515 for (int i = 0; i < a.length (); i++) |
|
1516 elem (i, i) += a.elem (i, i); |
|
1517 |
|
1518 return *this; |
|
1519 } |
|
1520 |
|
1521 ComplexMatrix& |
|
1522 ComplexMatrix::operator -= (const DiagMatrix& a) |
|
1523 { |
|
1524 int nr = rows (); |
|
1525 int nc = cols (); |
|
1526 if (nr != a.rows () || nc != a.cols ()) |
|
1527 { |
|
1528 (*current_liboctave_error_handler) |
|
1529 ("nonconformant matrix -= operation attempted"); |
889
|
1530 return *this; |
458
|
1531 } |
|
1532 |
|
1533 for (int i = 0; i < a.length (); i++) |
|
1534 elem (i, i) -= a.elem (i, i); |
|
1535 |
|
1536 return *this; |
|
1537 } |
|
1538 |
|
1539 ComplexMatrix& |
|
1540 ComplexMatrix::operator += (const ComplexDiagMatrix& a) |
|
1541 { |
|
1542 int nr = rows (); |
|
1543 int nc = cols (); |
|
1544 if (nr != a.rows () || nc != a.cols ()) |
|
1545 { |
|
1546 (*current_liboctave_error_handler) |
|
1547 ("nonconformant matrix += operation attempted"); |
889
|
1548 return *this; |
458
|
1549 } |
|
1550 |
|
1551 for (int i = 0; i < a.length (); i++) |
|
1552 elem (i, i) += a.elem (i, i); |
|
1553 |
|
1554 return *this; |
|
1555 } |
|
1556 |
|
1557 ComplexMatrix& |
|
1558 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) |
|
1559 { |
|
1560 int nr = rows (); |
|
1561 int nc = cols (); |
|
1562 if (nr != a.rows () || nc != a.cols ()) |
|
1563 { |
|
1564 (*current_liboctave_error_handler) |
|
1565 ("nonconformant matrix -= operation attempted"); |
889
|
1566 return *this; |
458
|
1567 } |
|
1568 |
|
1569 for (int i = 0; i < a.length (); i++) |
|
1570 elem (i, i) -= a.elem (i, i); |
|
1571 |
|
1572 return *this; |
|
1573 } |
|
1574 |
1205
|
1575 ComplexMatrix |
|
1576 operator + (const Matrix& m, const ComplexDiagMatrix& a) |
|
1577 { |
|
1578 int nr = m.rows (); |
|
1579 int nc = m.cols (); |
|
1580 if (nr != a.rows () || nc != a.cols ()) |
|
1581 { |
|
1582 (*current_liboctave_error_handler) |
|
1583 ("nonconformant matrix addition attempted"); |
|
1584 return ComplexMatrix (); |
|
1585 } |
|
1586 |
|
1587 if (nr == 0 || nc == 0) |
|
1588 return ComplexMatrix (nr, nc); |
|
1589 |
|
1590 ComplexMatrix result (m); |
|
1591 for (int i = 0; i < a.length (); i++) |
|
1592 result.elem (i, i) += a.elem (i, i); |
|
1593 |
|
1594 return result; |
|
1595 } |
|
1596 |
|
1597 ComplexMatrix |
|
1598 operator - (const Matrix& m, const ComplexDiagMatrix& a) |
|
1599 { |
|
1600 int nr = m.rows (); |
|
1601 int nc = m.cols (); |
|
1602 if (nr != a.rows () || nc != a.cols ()) |
|
1603 { |
|
1604 (*current_liboctave_error_handler) |
|
1605 ("nonconformant matrix subtraction attempted"); |
|
1606 return ComplexMatrix (); |
|
1607 } |
|
1608 |
|
1609 if (nr == 0 || nc == 0) |
|
1610 return ComplexMatrix (nr, nc); |
|
1611 |
|
1612 ComplexMatrix result (m); |
|
1613 for (int i = 0; i < a.length (); i++) |
|
1614 result.elem (i, i) -= a.elem (i, i); |
|
1615 |
|
1616 return result; |
|
1617 } |
|
1618 |
|
1619 ComplexMatrix |
|
1620 operator * (const Matrix& m, const ComplexDiagMatrix& a) |
|
1621 { |
|
1622 int nr = m.rows (); |
|
1623 int nc = m.cols (); |
|
1624 int a_nr = a.rows (); |
|
1625 int a_nc = a.cols (); |
|
1626 if (nc != a_nr) |
|
1627 { |
|
1628 (*current_liboctave_error_handler) |
|
1629 ("nonconformant matrix multiplication attempted"); |
|
1630 return ComplexMatrix (); |
|
1631 } |
|
1632 |
|
1633 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1634 return ComplexMatrix (nr, a_nc, 0.0); |
|
1635 |
|
1636 Complex *c = new Complex [nr*a_nc]; |
|
1637 Complex *ctmp = 0; |
|
1638 |
|
1639 for (int j = 0; j < a.length (); j++) |
|
1640 { |
|
1641 int idx = j * nr; |
|
1642 ctmp = c + idx; |
|
1643 if (a.elem (j, j) == 1.0) |
|
1644 { |
|
1645 for (int i = 0; i < nr; i++) |
|
1646 ctmp[i] = m.elem (i, j); |
|
1647 } |
|
1648 else if (a.elem (j, j) == 0.0) |
|
1649 { |
|
1650 for (int i = 0; i < nr; i++) |
|
1651 ctmp[i] = 0.0; |
|
1652 } |
|
1653 else |
|
1654 { |
|
1655 for (int i = 0; i < nr; i++) |
|
1656 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
1657 } |
|
1658 } |
|
1659 |
|
1660 if (a_nr < a_nc) |
|
1661 { |
|
1662 for (int i = nr * nc; i < nr * a_nc; i++) |
|
1663 ctmp[i] = 0.0; |
|
1664 } |
|
1665 |
|
1666 return ComplexMatrix (c, nr, a_nc); |
|
1667 } |
|
1668 |
|
1669 // diagonal matrix by matrix -> matrix operations |
|
1670 |
|
1671 ComplexMatrix |
|
1672 operator + (const DiagMatrix& m, const ComplexMatrix& a) |
|
1673 { |
|
1674 int nr = m.rows (); |
|
1675 int nc = m.cols (); |
|
1676 if (nr != a.rows () || nc != a.cols ()) |
|
1677 { |
|
1678 (*current_liboctave_error_handler) |
|
1679 ("nonconformant matrix addition attempted"); |
|
1680 return ComplexMatrix (); |
|
1681 } |
|
1682 |
|
1683 if (nr == 0 || nc == 0) |
|
1684 return ComplexMatrix (nr, nc); |
|
1685 |
|
1686 ComplexMatrix result (a); |
|
1687 for (int i = 0; i < m.length (); i++) |
|
1688 result.elem (i, i) += m.elem (i, i); |
|
1689 |
|
1690 return result; |
|
1691 } |
|
1692 |
|
1693 ComplexMatrix |
|
1694 operator - (const DiagMatrix& m, const ComplexMatrix& a) |
|
1695 { |
|
1696 int nr = m.rows (); |
|
1697 int nc = m.cols (); |
|
1698 if (nr != a.rows () || nc != a.cols ()) |
|
1699 { |
|
1700 (*current_liboctave_error_handler) |
|
1701 ("nonconformant matrix subtraction attempted"); |
|
1702 return ComplexMatrix (); |
|
1703 } |
|
1704 |
|
1705 if (nr == 0 || nc == 0) |
|
1706 return ComplexMatrix (nr, nc); |
|
1707 |
|
1708 ComplexMatrix result (-a); |
|
1709 for (int i = 0; i < m.length (); i++) |
|
1710 result.elem (i, i) += m.elem (i, i); |
|
1711 |
|
1712 return result; |
|
1713 } |
|
1714 |
|
1715 ComplexMatrix |
|
1716 operator * (const DiagMatrix& m, const ComplexMatrix& a) |
|
1717 { |
|
1718 int nr = m.rows (); |
|
1719 int nc = m.cols (); |
|
1720 int a_nr = a.rows (); |
|
1721 int a_nc = a.cols (); |
|
1722 if (nc != a_nr) |
|
1723 { |
|
1724 (*current_liboctave_error_handler) |
|
1725 ("nonconformant matrix multiplication attempted"); |
|
1726 return ComplexMatrix (); |
|
1727 } |
|
1728 |
|
1729 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1730 return ComplexMatrix (nr, nc, 0.0); |
|
1731 |
|
1732 ComplexMatrix c (nr, a_nc); |
|
1733 |
|
1734 for (int i = 0; i < m.length (); i++) |
|
1735 { |
|
1736 if (m.elem (i, i) == 1.0) |
|
1737 { |
|
1738 for (int j = 0; j < a_nc; j++) |
|
1739 c.elem (i, j) = a.elem (i, j); |
|
1740 } |
|
1741 else if (m.elem (i, i) == 0.0) |
|
1742 { |
|
1743 for (int j = 0; j < a_nc; j++) |
|
1744 c.elem (i, j) = 0.0; |
|
1745 } |
|
1746 else |
|
1747 { |
|
1748 for (int j = 0; j < a_nc; j++) |
|
1749 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
1750 } |
|
1751 } |
|
1752 |
|
1753 if (nr > nc) |
|
1754 { |
|
1755 for (int j = 0; j < a_nc; j++) |
|
1756 for (int i = a_nr; i < nr; i++) |
|
1757 c.elem (i, j) = 0.0; |
|
1758 } |
|
1759 |
|
1760 return c; |
|
1761 } |
|
1762 |
|
1763 ComplexMatrix |
|
1764 operator + (const ComplexDiagMatrix& m, const Matrix& a) |
|
1765 { |
|
1766 int nr = m.rows (); |
|
1767 int nc = m.cols (); |
|
1768 if (nr != a.rows () || nc != a.cols ()) |
|
1769 { |
|
1770 (*current_liboctave_error_handler) |
|
1771 ("nonconformant matrix addition attempted"); |
|
1772 return ComplexMatrix (); |
|
1773 } |
|
1774 |
|
1775 if (nr == 0 || nc == 0) |
|
1776 return ComplexMatrix (nr, nc); |
|
1777 |
|
1778 ComplexMatrix result (a); |
|
1779 for (int i = 0; i < m.length (); i++) |
|
1780 result.elem (i, i) += m.elem (i, i); |
|
1781 |
|
1782 return result; |
|
1783 } |
|
1784 |
|
1785 ComplexMatrix |
|
1786 operator - (const ComplexDiagMatrix& m, const Matrix& a) |
|
1787 { |
|
1788 int nr = m.rows (); |
|
1789 int nc = m.cols (); |
|
1790 if (nr != a.rows () || nc != a.cols ()) |
|
1791 { |
|
1792 (*current_liboctave_error_handler) |
|
1793 ("nonconformant matrix subtraction attempted"); |
|
1794 return ComplexMatrix (); |
|
1795 } |
|
1796 |
|
1797 if (nr == 0 || nc == 0) |
|
1798 return ComplexMatrix (nr, nc); |
|
1799 |
|
1800 ComplexMatrix result (-a); |
|
1801 for (int i = 0; i < m.length (); i++) |
|
1802 result.elem (i, i) += m.elem (i, i); |
|
1803 |
|
1804 return result; |
|
1805 } |
|
1806 |
|
1807 ComplexMatrix |
|
1808 operator * (const ComplexDiagMatrix& m, const Matrix& a) |
|
1809 { |
|
1810 int nr = m.rows (); |
|
1811 int nc = m.cols (); |
|
1812 int a_nr = a.rows (); |
|
1813 int a_nc = a.cols (); |
|
1814 if (nc != a_nr) |
|
1815 { |
|
1816 (*current_liboctave_error_handler) |
|
1817 ("nonconformant matrix multiplication attempted"); |
|
1818 return ComplexMatrix (); |
|
1819 } |
|
1820 |
|
1821 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1822 return ComplexMatrix (nr, a_nc, 0.0); |
|
1823 |
|
1824 ComplexMatrix c (nr, a_nc); |
|
1825 |
|
1826 for (int i = 0; i < m.length (); i++) |
|
1827 { |
|
1828 if (m.elem (i, i) == 1.0) |
|
1829 { |
|
1830 for (int j = 0; j < a_nc; j++) |
|
1831 c.elem (i, j) = a.elem (i, j); |
|
1832 } |
|
1833 else if (m.elem (i, i) == 0.0) |
|
1834 { |
|
1835 for (int j = 0; j < a_nc; j++) |
|
1836 c.elem (i, j) = 0.0; |
|
1837 } |
|
1838 else |
|
1839 { |
|
1840 for (int j = 0; j < a_nc; j++) |
|
1841 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
1842 } |
|
1843 } |
|
1844 |
|
1845 if (nr > nc) |
|
1846 { |
|
1847 for (int j = 0; j < a_nc; j++) |
|
1848 for (int i = a_nr; i < nr; i++) |
|
1849 c.elem (i, j) = 0.0; |
|
1850 } |
|
1851 |
|
1852 return c; |
|
1853 } |
|
1854 |
|
1855 ComplexMatrix |
|
1856 operator + (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
1857 { |
|
1858 int nr = m.rows (); |
|
1859 int nc = m.cols (); |
|
1860 if (nr != a.rows () || nc != a.cols ()) |
|
1861 { |
|
1862 (*current_liboctave_error_handler) |
|
1863 ("nonconformant matrix addition attempted"); |
|
1864 return ComplexMatrix (); |
|
1865 } |
|
1866 |
|
1867 if (nr == 0 || nc == 0) |
|
1868 return ComplexMatrix (nr, nc); |
|
1869 |
|
1870 ComplexMatrix result (a); |
|
1871 for (int i = 0; i < m.length (); i++) |
|
1872 result.elem (i, i) += m.elem (i, i); |
|
1873 |
|
1874 return result; |
|
1875 } |
|
1876 |
|
1877 ComplexMatrix |
|
1878 operator - (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
1879 { |
|
1880 int nr = m.rows (); |
|
1881 int nc = m.cols (); |
|
1882 if (nr != a.rows () || nc != a.cols ()) |
|
1883 { |
|
1884 (*current_liboctave_error_handler) |
|
1885 ("nonconformant matrix subtraction attempted"); |
|
1886 return ComplexMatrix (); |
|
1887 } |
|
1888 |
|
1889 if (nr == 0 || nc == 0) |
|
1890 return ComplexMatrix (nr, nc); |
|
1891 |
|
1892 ComplexMatrix result (-a); |
|
1893 for (int i = 0; i < m.length (); i++) |
|
1894 result.elem (i, i) += m.elem (i, i); |
|
1895 |
|
1896 return result; |
|
1897 } |
|
1898 |
|
1899 ComplexMatrix |
|
1900 operator * (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
1901 { |
|
1902 int nr = m.rows (); |
|
1903 int nc = m.cols (); |
|
1904 int a_nr = a.rows (); |
|
1905 int a_nc = a.cols (); |
|
1906 if (nc != a_nr) |
|
1907 { |
|
1908 (*current_liboctave_error_handler) |
|
1909 ("nonconformant matrix multiplication attempted"); |
|
1910 return ComplexMatrix (); |
|
1911 } |
|
1912 |
|
1913 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1914 return ComplexMatrix (nr, a_nc, 0.0); |
|
1915 |
|
1916 ComplexMatrix c (nr, a_nc); |
|
1917 |
|
1918 for (int i = 0; i < m.length (); i++) |
|
1919 { |
|
1920 if (m.elem (i, i) == 1.0) |
|
1921 { |
|
1922 for (int j = 0; j < a_nc; j++) |
|
1923 c.elem (i, j) = a.elem (i, j); |
|
1924 } |
|
1925 else if (m.elem (i, i) == 0.0) |
|
1926 { |
|
1927 for (int j = 0; j < a_nc; j++) |
|
1928 c.elem (i, j) = 0.0; |
|
1929 } |
|
1930 else |
|
1931 { |
|
1932 for (int j = 0; j < a_nc; j++) |
|
1933 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
1934 } |
|
1935 } |
|
1936 |
|
1937 if (nr > nc) |
|
1938 { |
|
1939 for (int j = 0; j < a_nc; j++) |
|
1940 for (int i = a_nr; i < nr; i++) |
|
1941 c.elem (i, j) = 0.0; |
|
1942 } |
|
1943 |
|
1944 return c; |
|
1945 } |
|
1946 |
458
|
1947 // matrix by matrix -> matrix operations |
|
1948 |
|
1949 ComplexMatrix& |
|
1950 ComplexMatrix::operator += (const Matrix& a) |
|
1951 { |
|
1952 int nr = rows (); |
|
1953 int nc = cols (); |
|
1954 if (nr != a.rows () || nc != a.cols ()) |
|
1955 { |
|
1956 (*current_liboctave_error_handler) |
|
1957 ("nonconformant matrix += operation attempted"); |
|
1958 return *this; |
|
1959 } |
|
1960 |
|
1961 if (nr == 0 || nc == 0) |
|
1962 return *this; |
|
1963 |
|
1964 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1965 |
|
1966 add2 (d, a.data (), length ()); |
|
1967 return *this; |
|
1968 } |
|
1969 |
|
1970 ComplexMatrix& |
|
1971 ComplexMatrix::operator -= (const Matrix& a) |
|
1972 { |
|
1973 int nr = rows (); |
|
1974 int nc = cols (); |
|
1975 if (nr != a.rows () || nc != a.cols ()) |
|
1976 { |
|
1977 (*current_liboctave_error_handler) |
|
1978 ("nonconformant matrix -= operation attempted"); |
|
1979 return *this; |
|
1980 } |
|
1981 |
|
1982 if (nr == 0 || nc == 0) |
|
1983 return *this; |
|
1984 |
|
1985 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1986 |
|
1987 subtract2 (d, a.data (), length ()); |
|
1988 return *this; |
|
1989 } |
|
1990 |
|
1991 ComplexMatrix& |
|
1992 ComplexMatrix::operator += (const ComplexMatrix& a) |
|
1993 { |
|
1994 int nr = rows (); |
|
1995 int nc = cols (); |
|
1996 if (nr != a.rows () || nc != a.cols ()) |
|
1997 { |
|
1998 (*current_liboctave_error_handler) |
|
1999 ("nonconformant matrix += operation attempted"); |
|
2000 return *this; |
|
2001 } |
|
2002 |
|
2003 if (nr == 0 || nc == 0) |
|
2004 return *this; |
|
2005 |
|
2006 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2007 |
|
2008 add2 (d, a.data (), length ()); |
|
2009 return *this; |
|
2010 } |
|
2011 |
|
2012 ComplexMatrix& |
|
2013 ComplexMatrix::operator -= (const ComplexMatrix& a) |
|
2014 { |
|
2015 int nr = rows (); |
|
2016 int nc = cols (); |
|
2017 if (nr != a.rows () || nc != a.cols ()) |
|
2018 { |
|
2019 (*current_liboctave_error_handler) |
|
2020 ("nonconformant matrix -= operation attempted"); |
|
2021 return *this; |
|
2022 } |
|
2023 |
|
2024 if (nr == 0 || nc == 0) |
|
2025 return *this; |
|
2026 |
|
2027 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2028 |
|
2029 subtract2 (d, a.data (), length ()); |
|
2030 return *this; |
|
2031 } |
|
2032 |
|
2033 // unary operations |
|
2034 |
|
2035 Matrix |
|
2036 ComplexMatrix::operator ! (void) const |
|
2037 { |
|
2038 return Matrix (not (data (), length ()), rows (), cols ()); |
|
2039 } |
|
2040 |
|
2041 // matrix by scalar -> matrix operations |
|
2042 |
|
2043 ComplexMatrix |
1205
|
2044 operator + (const Matrix& a, const Complex& s) |
|
2045 { |
|
2046 return ComplexMatrix (add (a.data (), a.length (), s), |
|
2047 a.rows (), a.cols ()); |
|
2048 } |
|
2049 |
|
2050 ComplexMatrix |
|
2051 operator - (const Matrix& a, const Complex& s) |
|
2052 { |
|
2053 return ComplexMatrix (subtract (a.data (), a.length (), s), |
|
2054 a.rows (), a.cols ()); |
|
2055 } |
|
2056 |
|
2057 ComplexMatrix |
|
2058 operator * (const Matrix& a, const Complex& s) |
|
2059 { |
|
2060 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2061 a.rows (), a.cols ()); |
|
2062 } |
|
2063 |
|
2064 ComplexMatrix |
|
2065 operator / (const Matrix& a, const Complex& s) |
|
2066 { |
|
2067 return ComplexMatrix (divide (a.data (), a.length (), s), |
|
2068 a.rows (), a.cols ()); |
|
2069 } |
|
2070 |
|
2071 ComplexMatrix |
458
|
2072 operator + (const ComplexMatrix& a, double s) |
|
2073 { |
|
2074 return ComplexMatrix (add (a.data (), a.length (), s), |
|
2075 a.rows (), a.cols ()); |
|
2076 } |
|
2077 |
|
2078 ComplexMatrix |
|
2079 operator - (const ComplexMatrix& a, double s) |
|
2080 { |
|
2081 return ComplexMatrix (subtract (a.data (), a.length (), s), |
|
2082 a.rows (), a.cols ()); |
|
2083 } |
|
2084 |
|
2085 ComplexMatrix |
|
2086 operator * (const ComplexMatrix& a, double s) |
|
2087 { |
|
2088 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2089 a.rows (), a.cols ()); |
|
2090 } |
|
2091 |
|
2092 ComplexMatrix |
|
2093 operator / (const ComplexMatrix& a, double s) |
|
2094 { |
|
2095 return ComplexMatrix (divide (a.data (), a.length (), s), |
|
2096 a.rows (), a.cols ()); |
|
2097 } |
|
2098 |
|
2099 // scalar by matrix -> matrix operations |
|
2100 |
|
2101 ComplexMatrix |
|
2102 operator + (double s, const ComplexMatrix& a) |
|
2103 { |
|
2104 return ComplexMatrix (add (a.data (), a.length (), s), a.rows (), |
|
2105 a.cols ()); |
|
2106 } |
|
2107 |
|
2108 ComplexMatrix |
|
2109 operator - (double s, const ComplexMatrix& a) |
|
2110 { |
|
2111 return ComplexMatrix (subtract (s, a.data (), a.length ()), |
|
2112 a.rows (), a.cols ()); |
|
2113 } |
|
2114 |
|
2115 ComplexMatrix |
|
2116 operator * (double s, const ComplexMatrix& a) |
|
2117 { |
|
2118 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2119 a.rows (), a.cols ()); |
|
2120 } |
|
2121 |
|
2122 ComplexMatrix |
|
2123 operator / (double s, const ComplexMatrix& a) |
|
2124 { |
|
2125 return ComplexMatrix (divide (s, a.data (), a.length ()), |
|
2126 a.rows (), a.cols ()); |
|
2127 } |
|
2128 |
1205
|
2129 ComplexMatrix |
|
2130 operator + (const Complex& s, const Matrix& a) |
458
|
2131 { |
1205
|
2132 return ComplexMatrix (add (s, a.data (), a.length ()), |
|
2133 a.rows (), a.cols ()); |
458
|
2134 } |
|
2135 |
1205
|
2136 ComplexMatrix |
|
2137 operator - (const Complex& s, const Matrix& a) |
458
|
2138 { |
1205
|
2139 return ComplexMatrix (subtract (s, a.data (), a.length ()), |
|
2140 a.rows (), a.cols ()); |
|
2141 } |
|
2142 |
|
2143 ComplexMatrix |
|
2144 operator * (const Complex& s, const Matrix& a) |
|
2145 { |
|
2146 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2147 a.rows (), a.cols ()); |
|
2148 } |
|
2149 |
|
2150 ComplexMatrix |
|
2151 operator / (const Complex& s, const Matrix& a) |
|
2152 { |
|
2153 return ComplexMatrix (divide (s, a.data (), a.length ()), |
|
2154 a.rows (), a.cols ()); |
458
|
2155 } |
|
2156 |
|
2157 // matrix by diagonal matrix -> matrix operations |
|
2158 |
|
2159 ComplexMatrix |
|
2160 operator + (const ComplexMatrix& m, const DiagMatrix& a) |
|
2161 { |
|
2162 int nr = m.rows (); |
|
2163 int nc = m.cols (); |
|
2164 if (nr != a.rows () || nc != a.cols ()) |
|
2165 { |
|
2166 (*current_liboctave_error_handler) |
|
2167 ("nonconformant matrix addition attempted"); |
|
2168 return ComplexMatrix (); |
|
2169 } |
|
2170 |
|
2171 if (nr == 0 || nc == 0) |
|
2172 return ComplexMatrix (nr, nc); |
|
2173 |
|
2174 ComplexMatrix result (m); |
|
2175 for (int i = 0; i < a.length (); i++) |
|
2176 result.elem (i, i) += a.elem (i, i); |
|
2177 |
|
2178 return result; |
|
2179 } |
|
2180 |
|
2181 ComplexMatrix |
|
2182 operator - (const ComplexMatrix& m, const DiagMatrix& a) |
|
2183 { |
|
2184 int nr = m.rows (); |
|
2185 int nc = m.cols (); |
|
2186 if (nr != a.rows () || nc != a.cols ()) |
|
2187 { |
|
2188 (*current_liboctave_error_handler) |
|
2189 ("nonconformant matrix subtraction attempted"); |
|
2190 return ComplexMatrix (); |
|
2191 } |
|
2192 |
|
2193 if (nr == 0 || nc == 0) |
|
2194 return ComplexMatrix (nr, nc); |
|
2195 |
|
2196 ComplexMatrix result (m); |
|
2197 for (int i = 0; i < a.length (); i++) |
|
2198 result.elem (i, i) -= a.elem (i, i); |
|
2199 |
|
2200 return result; |
|
2201 } |
|
2202 |
|
2203 ComplexMatrix |
|
2204 operator * (const ComplexMatrix& m, const DiagMatrix& a) |
|
2205 { |
|
2206 int nr = m.rows (); |
|
2207 int nc = m.cols (); |
|
2208 int a_nc = a.cols (); |
|
2209 if (nc != a.rows ()) |
|
2210 { |
|
2211 (*current_liboctave_error_handler) |
|
2212 ("nonconformant matrix multiplication attempted"); |
|
2213 return ComplexMatrix (); |
|
2214 } |
|
2215 |
|
2216 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2217 return ComplexMatrix (nr, nc, 0.0); |
|
2218 |
|
2219 Complex *c = new Complex [nr*a_nc]; |
533
|
2220 Complex *ctmp = 0; |
458
|
2221 |
|
2222 for (int j = 0; j < a.length (); j++) |
|
2223 { |
|
2224 int idx = j * nr; |
|
2225 ctmp = c + idx; |
|
2226 if (a.elem (j, j) == 1.0) |
|
2227 { |
|
2228 for (int i = 0; i < nr; i++) |
|
2229 ctmp[i] = m.elem (i, j); |
|
2230 } |
|
2231 else if (a.elem (j, j) == 0.0) |
|
2232 { |
|
2233 for (int i = 0; i < nr; i++) |
|
2234 ctmp[i] = 0.0; |
|
2235 } |
|
2236 else |
|
2237 { |
|
2238 for (int i = 0; i < nr; i++) |
|
2239 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
2240 } |
|
2241 } |
|
2242 |
|
2243 if (a.rows () < a_nc) |
|
2244 { |
|
2245 for (int i = nr * nc; i < nr * a_nc; i++) |
|
2246 ctmp[i] = 0.0; |
|
2247 } |
|
2248 |
|
2249 return ComplexMatrix (c, nr, a_nc); |
|
2250 } |
|
2251 |
|
2252 ComplexMatrix |
|
2253 operator + (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2254 { |
|
2255 int nr = m.rows (); |
|
2256 int nc = m.cols (); |
|
2257 if (nr != a.rows () || nc != a.cols ()) |
|
2258 { |
|
2259 (*current_liboctave_error_handler) |
|
2260 ("nonconformant matrix addition attempted"); |
|
2261 return ComplexMatrix (); |
|
2262 } |
|
2263 |
|
2264 if (nr == 0 || nc == 0) |
|
2265 return ComplexMatrix (nr, nc); |
|
2266 |
|
2267 ComplexMatrix result (m); |
|
2268 for (int i = 0; i < a.length (); i++) |
|
2269 result.elem (i, i) += a.elem (i, i); |
|
2270 |
|
2271 return result; |
|
2272 } |
|
2273 |
|
2274 ComplexMatrix |
|
2275 operator - (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2276 { |
|
2277 int nr = m.rows (); |
|
2278 int nc = m.cols (); |
|
2279 if (nr != a.rows () || nc != a.cols ()) |
|
2280 { |
|
2281 (*current_liboctave_error_handler) |
|
2282 ("nonconformant matrix subtraction attempted"); |
|
2283 return ComplexMatrix (); |
|
2284 } |
|
2285 |
|
2286 if (nr == 0 || nc == 0) |
|
2287 return ComplexMatrix (nr, nc); |
|
2288 |
|
2289 ComplexMatrix result (m); |
|
2290 for (int i = 0; i < a.length (); i++) |
|
2291 result.elem (i, i) -= a.elem (i, i); |
|
2292 |
|
2293 return result; |
|
2294 } |
|
2295 |
|
2296 ComplexMatrix |
|
2297 operator * (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2298 { |
|
2299 int nr = m.rows (); |
|
2300 int nc = m.cols (); |
|
2301 int a_nc = a.cols (); |
|
2302 if (nc != a.rows ()) |
|
2303 { |
|
2304 (*current_liboctave_error_handler) |
|
2305 ("nonconformant matrix multiplication attempted"); |
|
2306 return ComplexMatrix (); |
|
2307 } |
|
2308 |
|
2309 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2310 return ComplexMatrix (nr, nc, 0.0); |
|
2311 |
|
2312 Complex *c = new Complex [nr*a_nc]; |
533
|
2313 Complex *ctmp = 0; |
458
|
2314 |
|
2315 for (int j = 0; j < a.length (); j++) |
|
2316 { |
|
2317 int idx = j * nr; |
|
2318 ctmp = c + idx; |
|
2319 if (a.elem (j, j) == 1.0) |
|
2320 { |
|
2321 for (int i = 0; i < nr; i++) |
|
2322 ctmp[i] = m.elem (i, j); |
|
2323 } |
|
2324 else if (a.elem (j, j) == 0.0) |
|
2325 { |
|
2326 for (int i = 0; i < nr; i++) |
|
2327 ctmp[i] = 0.0; |
|
2328 } |
|
2329 else |
|
2330 { |
|
2331 for (int i = 0; i < nr; i++) |
|
2332 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
2333 } |
|
2334 } |
|
2335 |
|
2336 if (a.rows () < a_nc) |
|
2337 { |
|
2338 for (int i = nr * nc; i < nr * a_nc; i++) |
|
2339 ctmp[i] = 0.0; |
|
2340 } |
|
2341 |
|
2342 return ComplexMatrix (c, nr, a_nc); |
|
2343 } |
|
2344 |
|
2345 // matrix by matrix -> matrix operations |
|
2346 |
|
2347 ComplexMatrix |
|
2348 operator + (const ComplexMatrix& m, const Matrix& a) |
|
2349 { |
|
2350 int nr = m.rows (); |
|
2351 int nc = m.cols (); |
|
2352 if (nr != a.rows () || nc != a.cols ()) |
|
2353 { |
|
2354 (*current_liboctave_error_handler) |
|
2355 ("nonconformant matrix addition attempted"); |
|
2356 return ComplexMatrix (); |
|
2357 } |
|
2358 |
|
2359 if (nr == 0 || nc == 0) |
|
2360 return ComplexMatrix (nr, nc); |
|
2361 |
|
2362 return ComplexMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
2363 } |
|
2364 |
|
2365 ComplexMatrix |
|
2366 operator - (const ComplexMatrix& m, const Matrix& a) |
|
2367 { |
|
2368 int nr = m.rows (); |
|
2369 int nc = m.cols (); |
|
2370 if (nr != a.rows () || nc != a.cols ()) |
|
2371 { |
|
2372 (*current_liboctave_error_handler) |
|
2373 ("nonconformant matrix subtraction attempted"); |
|
2374 return ComplexMatrix (); |
|
2375 } |
|
2376 |
|
2377 if (nr == 0 || nc == 0) |
|
2378 return ComplexMatrix (nr, nc); |
|
2379 |
|
2380 return ComplexMatrix (subtract (m.data (), a.data (), m.length ()), nr, nc); |
|
2381 } |
|
2382 |
|
2383 ComplexMatrix |
1205
|
2384 operator + (const Matrix& m, const ComplexMatrix& a) |
|
2385 { |
|
2386 int nr = m.rows (); |
|
2387 int nc = m.cols (); |
|
2388 if (nr != a.rows () || nc != a.cols ()) |
|
2389 { |
|
2390 (*current_liboctave_error_handler) |
|
2391 ("nonconformant matrix addition attempted"); |
|
2392 return ComplexMatrix (); |
|
2393 } |
|
2394 |
|
2395 return ComplexMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
2396 } |
|
2397 |
|
2398 ComplexMatrix |
|
2399 operator - (const Matrix& m, const ComplexMatrix& a) |
|
2400 { |
|
2401 int nr = m.rows (); |
|
2402 int nc = m.cols (); |
|
2403 if (nr != a.rows () || nc != a.cols ()) |
|
2404 { |
|
2405 (*current_liboctave_error_handler) |
|
2406 ("nonconformant matrix subtraction attempted"); |
|
2407 return ComplexMatrix (); |
|
2408 } |
|
2409 |
|
2410 if (nr == 0 || nc == 0) |
|
2411 return ComplexMatrix (nr, nc); |
|
2412 |
|
2413 return ComplexMatrix (subtract (m.data (), a.data (), m.length ()), nr, nc); |
|
2414 } |
|
2415 |
|
2416 ComplexMatrix |
458
|
2417 operator * (const ComplexMatrix& m, const Matrix& a) |
|
2418 { |
|
2419 ComplexMatrix tmp (a); |
|
2420 return m * tmp; |
|
2421 } |
|
2422 |
|
2423 ComplexMatrix |
1205
|
2424 operator * (const Matrix& m, const ComplexMatrix& a) |
|
2425 { |
|
2426 ComplexMatrix tmp (m); |
|
2427 return tmp * a; |
|
2428 } |
|
2429 |
|
2430 ComplexMatrix |
458
|
2431 operator * (const ComplexMatrix& m, const ComplexMatrix& a) |
|
2432 { |
|
2433 int nr = m.rows (); |
|
2434 int nc = m.cols (); |
|
2435 int a_nc = a.cols (); |
|
2436 if (nc != a.rows ()) |
|
2437 { |
|
2438 (*current_liboctave_error_handler) |
|
2439 ("nonconformant matrix multiplication attempted"); |
|
2440 return ComplexMatrix (); |
|
2441 } |
|
2442 |
|
2443 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2444 return ComplexMatrix (nr, nc, 0.0); |
|
2445 |
|
2446 int ld = nr; |
|
2447 int lda = a.rows (); |
|
2448 |
|
2449 Complex *c = new Complex [nr*a_nc]; |
|
2450 |
1253
|
2451 F77_FCN (zgemm, ZGEMM) ("N", "N", nr, a_nc, nc, 1.0, m.data (), ld, |
|
2452 a.data (), lda, 0.0, c, nr, 1L, 1L); |
458
|
2453 |
|
2454 return ComplexMatrix (c, nr, a_nc); |
|
2455 } |
|
2456 |
|
2457 ComplexMatrix |
|
2458 product (const ComplexMatrix& m, const Matrix& a) |
|
2459 { |
|
2460 int nr = m.rows (); |
|
2461 int nc = m.cols (); |
|
2462 if (nr != a.rows () || nc != a.cols ()) |
|
2463 { |
|
2464 (*current_liboctave_error_handler) |
|
2465 ("nonconformant matrix product attempted"); |
|
2466 return ComplexMatrix (); |
|
2467 } |
|
2468 |
|
2469 if (nr == 0 || nc == 0) |
|
2470 return ComplexMatrix (nr, nc); |
|
2471 |
|
2472 return ComplexMatrix (multiply (m.data (), a.data (), m.length ()), nr, nc); |
|
2473 } |
|
2474 |
|
2475 ComplexMatrix |
|
2476 quotient (const ComplexMatrix& m, const Matrix& a) |
|
2477 { |
|
2478 int nr = m.rows (); |
|
2479 int nc = m.cols (); |
|
2480 if (nr != a.rows () || nc != a.cols ()) |
|
2481 { |
|
2482 (*current_liboctave_error_handler) |
|
2483 ("nonconformant matrix quotient attempted"); |
|
2484 return ComplexMatrix (); |
|
2485 } |
|
2486 |
|
2487 if (nr == 0 || nc == 0) |
|
2488 return ComplexMatrix (nr, nc); |
|
2489 |
|
2490 return ComplexMatrix (divide (m.data (), a.data (), m.length ()), nr, nc); |
|
2491 } |
|
2492 |
1205
|
2493 ComplexMatrix |
|
2494 product (const Matrix& m, const ComplexMatrix& a) |
|
2495 { |
|
2496 int nr = m.rows (); |
|
2497 int nc = m.cols (); |
|
2498 if (nr != a.rows () || nc != a.cols ()) |
|
2499 { |
|
2500 (*current_liboctave_error_handler) |
|
2501 ("nonconformant matrix product attempted"); |
|
2502 return ComplexMatrix (); |
|
2503 } |
|
2504 |
|
2505 if (nr == 0 || nc == 0) |
|
2506 return ComplexMatrix (nr, nc); |
|
2507 |
|
2508 return ComplexMatrix (multiply (m.data (), a.data (), m.length ()), nr, nc); |
|
2509 } |
|
2510 |
|
2511 ComplexMatrix |
|
2512 quotient (const Matrix& m, const ComplexMatrix& a) |
|
2513 { |
|
2514 int nr = m.rows (); |
|
2515 int nc = m.cols (); |
|
2516 if (nr != a.rows () || nc != a.cols ()) |
|
2517 { |
|
2518 (*current_liboctave_error_handler) |
|
2519 ("nonconformant matrix quotient attempted"); |
|
2520 return ComplexMatrix (); |
|
2521 } |
|
2522 |
|
2523 if (nr == 0 || nc == 0) |
|
2524 return ComplexMatrix (nr, nc); |
|
2525 |
|
2526 return ComplexMatrix (divide (m.data (), a.data (), m.length ()), nr, nc); |
|
2527 } |
|
2528 |
458
|
2529 // other operations |
|
2530 |
|
2531 ComplexMatrix |
|
2532 map (c_c_Mapper f, const ComplexMatrix& a) |
|
2533 { |
|
2534 ComplexMatrix b (a); |
|
2535 b.map (f); |
|
2536 return b; |
|
2537 } |
|
2538 |
|
2539 void |
|
2540 ComplexMatrix::map (c_c_Mapper f) |
|
2541 { |
|
2542 for (int j = 0; j < cols (); j++) |
|
2543 for (int i = 0; i < rows (); i++) |
|
2544 elem (i, j) = f (elem (i, j)); |
|
2545 } |
|
2546 |
|
2547 Matrix |
|
2548 ComplexMatrix::all (void) const |
|
2549 { |
|
2550 int nr = rows (); |
|
2551 int nc = cols (); |
|
2552 Matrix retval; |
|
2553 if (nr > 0 && nc > 0) |
|
2554 { |
|
2555 if (nr == 1) |
|
2556 { |
|
2557 retval.resize (1, 1); |
|
2558 retval.elem (0, 0) = 1.0; |
|
2559 for (int j = 0; j < nc; j++) |
|
2560 { |
|
2561 if (elem (0, j) == 0.0) |
|
2562 { |
|
2563 retval.elem (0, 0) = 0.0; |
|
2564 break; |
|
2565 } |
|
2566 } |
|
2567 } |
|
2568 else if (nc == 1) |
|
2569 { |
|
2570 retval.resize (1, 1); |
|
2571 retval.elem (0, 0) = 1.0; |
|
2572 for (int i = 0; i < nr; i++) |
|
2573 { |
|
2574 if (elem (i, 0) == 0.0) |
|
2575 { |
|
2576 retval.elem (0, 0) = 0.0; |
|
2577 break; |
|
2578 } |
|
2579 } |
|
2580 } |
|
2581 else |
|
2582 { |
|
2583 retval.resize (1, nc); |
|
2584 for (int j = 0; j < nc; j++) |
|
2585 { |
|
2586 retval.elem (0, j) = 1.0; |
|
2587 for (int i = 0; i < nr; i++) |
|
2588 { |
|
2589 if (elem (i, j) == 0.0) |
|
2590 { |
|
2591 retval.elem (0, j) = 0.0; |
|
2592 break; |
|
2593 } |
|
2594 } |
|
2595 } |
|
2596 } |
|
2597 } |
|
2598 return retval; |
|
2599 } |
|
2600 |
|
2601 Matrix |
|
2602 ComplexMatrix::any (void) const |
|
2603 { |
|
2604 int nr = rows (); |
|
2605 int nc = cols (); |
|
2606 Matrix retval; |
|
2607 if (nr > 0 && nc > 0) |
|
2608 { |
|
2609 if (nr == 1) |
|
2610 { |
|
2611 retval.resize (1, 1); |
|
2612 retval.elem (0, 0) = 0.0; |
|
2613 for (int j = 0; j < nc; j++) |
|
2614 { |
|
2615 if (elem (0, j) != 0.0) |
|
2616 { |
|
2617 retval.elem (0, 0) = 1.0; |
|
2618 break; |
|
2619 } |
|
2620 } |
|
2621 } |
|
2622 else if (nc == 1) |
|
2623 { |
|
2624 retval.resize (1, 1); |
|
2625 retval.elem (0, 0) = 0.0; |
|
2626 for (int i = 0; i < nr; i++) |
|
2627 { |
|
2628 if (elem (i, 0) != 0.0) |
|
2629 { |
|
2630 retval.elem (0, 0) = 1.0; |
|
2631 break; |
|
2632 } |
|
2633 } |
|
2634 } |
|
2635 else |
|
2636 { |
|
2637 retval.resize (1, nc); |
|
2638 for (int j = 0; j < nc; j++) |
|
2639 { |
|
2640 retval.elem (0, j) = 0.0; |
|
2641 for (int i = 0; i < nr; i++) |
|
2642 { |
|
2643 if (elem (i, j) != 0.0) |
|
2644 { |
|
2645 retval.elem (0, j) = 1.0; |
|
2646 break; |
|
2647 } |
|
2648 } |
|
2649 } |
|
2650 } |
|
2651 } |
|
2652 return retval; |
|
2653 } |
|
2654 |
|
2655 ComplexMatrix |
|
2656 ComplexMatrix::cumprod (void) const |
|
2657 { |
|
2658 int nr = rows (); |
|
2659 int nc = cols (); |
|
2660 ComplexMatrix retval; |
|
2661 if (nr > 0 && nc > 0) |
|
2662 { |
|
2663 if (nr == 1) |
|
2664 { |
|
2665 retval.resize (1, nc); |
|
2666 Complex prod = elem (0, 0); |
|
2667 for (int j = 0; j < nc; j++) |
|
2668 { |
|
2669 retval.elem (0, j) = prod; |
|
2670 if (j < nc - 1) |
|
2671 prod *= elem (0, j+1); |
|
2672 } |
|
2673 } |
|
2674 else if (nc == 1) |
|
2675 { |
|
2676 retval.resize (nr, 1); |
|
2677 Complex prod = elem (0, 0); |
|
2678 for (int i = 0; i < nr; i++) |
|
2679 { |
|
2680 retval.elem (i, 0) = prod; |
|
2681 if (i < nr - 1) |
|
2682 prod *= elem (i+1, 0); |
|
2683 } |
|
2684 } |
|
2685 else |
|
2686 { |
|
2687 retval.resize (nr, nc); |
|
2688 for (int j = 0; j < nc; j++) |
|
2689 { |
|
2690 Complex prod = elem (0, j); |
|
2691 for (int i = 0; i < nr; i++) |
|
2692 { |
|
2693 retval.elem (i, j) = prod; |
|
2694 if (i < nr - 1) |
|
2695 prod *= elem (i+1, j); |
|
2696 } |
|
2697 } |
|
2698 } |
|
2699 } |
|
2700 return retval; |
|
2701 } |
|
2702 |
|
2703 ComplexMatrix |
|
2704 ComplexMatrix::cumsum (void) const |
|
2705 { |
|
2706 int nr = rows (); |
|
2707 int nc = cols (); |
|
2708 ComplexMatrix retval; |
|
2709 if (nr > 0 && nc > 0) |
|
2710 { |
|
2711 if (nr == 1) |
|
2712 { |
|
2713 retval.resize (1, nc); |
|
2714 Complex sum = elem (0, 0); |
|
2715 for (int j = 0; j < nc; j++) |
|
2716 { |
|
2717 retval.elem (0, j) = sum; |
|
2718 if (j < nc - 1) |
|
2719 sum += elem (0, j+1); |
|
2720 } |
|
2721 } |
|
2722 else if (nc == 1) |
|
2723 { |
|
2724 retval.resize (nr, 1); |
|
2725 Complex sum = elem (0, 0); |
|
2726 for (int i = 0; i < nr; i++) |
|
2727 { |
|
2728 retval.elem (i, 0) = sum; |
|
2729 if (i < nr - 1) |
|
2730 sum += elem (i+1, 0); |
|
2731 } |
|
2732 } |
|
2733 else |
|
2734 { |
|
2735 retval.resize (nr, nc); |
|
2736 for (int j = 0; j < nc; j++) |
|
2737 { |
|
2738 Complex sum = elem (0, j); |
|
2739 for (int i = 0; i < nr; i++) |
|
2740 { |
|
2741 retval.elem (i, j) = sum; |
|
2742 if (i < nr - 1) |
|
2743 sum += elem (i+1, j); |
|
2744 } |
|
2745 } |
|
2746 } |
|
2747 } |
|
2748 return retval; |
|
2749 } |
|
2750 |
|
2751 ComplexMatrix |
|
2752 ComplexMatrix::prod (void) const |
|
2753 { |
|
2754 int nr = rows (); |
|
2755 int nc = cols (); |
|
2756 ComplexMatrix retval; |
|
2757 if (nr > 0 && nc > 0) |
|
2758 { |
|
2759 if (nr == 1) |
|
2760 { |
|
2761 retval.resize (1, 1); |
|
2762 retval.elem (0, 0) = 1.0; |
|
2763 for (int j = 0; j < nc; j++) |
|
2764 retval.elem (0, 0) *= elem (0, j); |
|
2765 } |
|
2766 else if (nc == 1) |
|
2767 { |
|
2768 retval.resize (1, 1); |
|
2769 retval.elem (0, 0) = 1.0; |
|
2770 for (int i = 0; i < nr; i++) |
|
2771 retval.elem (0, 0) *= elem (i, 0); |
|
2772 } |
|
2773 else |
|
2774 { |
|
2775 retval.resize (1, nc); |
|
2776 for (int j = 0; j < nc; j++) |
|
2777 { |
|
2778 retval.elem (0, j) = 1.0; |
|
2779 for (int i = 0; i < nr; i++) |
|
2780 retval.elem (0, j) *= elem (i, j); |
|
2781 } |
|
2782 } |
|
2783 } |
|
2784 return retval; |
|
2785 } |
|
2786 |
|
2787 ComplexMatrix |
|
2788 ComplexMatrix::sum (void) const |
|
2789 { |
|
2790 int nr = rows (); |
|
2791 int nc = cols (); |
|
2792 ComplexMatrix retval; |
|
2793 if (nr > 0 && nc > 0) |
|
2794 { |
|
2795 if (nr == 1) |
|
2796 { |
|
2797 retval.resize (1, 1); |
|
2798 retval.elem (0, 0) = 0.0; |
|
2799 for (int j = 0; j < nc; j++) |
|
2800 retval.elem (0, 0) += elem (0, j); |
|
2801 } |
|
2802 else if (nc == 1) |
|
2803 { |
|
2804 retval.resize (1, 1); |
|
2805 retval.elem (0, 0) = 0.0; |
|
2806 for (int i = 0; i < nr; i++) |
|
2807 retval.elem (0, 0) += elem (i, 0); |
|
2808 } |
|
2809 else |
|
2810 { |
|
2811 retval.resize (1, nc); |
|
2812 for (int j = 0; j < nc; j++) |
|
2813 { |
|
2814 retval.elem (0, j) = 0.0; |
|
2815 for (int i = 0; i < nr; i++) |
|
2816 retval.elem (0, j) += elem (i, j); |
|
2817 } |
|
2818 } |
|
2819 } |
|
2820 return retval; |
|
2821 } |
|
2822 |
|
2823 ComplexMatrix |
|
2824 ComplexMatrix::sumsq (void) const |
|
2825 { |
|
2826 int nr = rows (); |
|
2827 int nc = cols (); |
|
2828 ComplexMatrix retval; |
|
2829 if (nr > 0 && nc > 0) |
|
2830 { |
|
2831 if (nr == 1) |
|
2832 { |
|
2833 retval.resize (1, 1); |
|
2834 retval.elem (0, 0) = 0.0; |
|
2835 for (int j = 0; j < nc; j++) |
|
2836 { |
|
2837 Complex d = elem (0, j); |
|
2838 retval.elem (0, 0) += d * d; |
|
2839 } |
|
2840 } |
|
2841 else if (nc == 1) |
|
2842 { |
|
2843 retval.resize (1, 1); |
|
2844 retval.elem (0, 0) = 0.0; |
|
2845 for (int i = 0; i < nr; i++) |
|
2846 { |
|
2847 Complex d = elem (i, 0); |
|
2848 retval.elem (0, 0) += d * d; |
|
2849 } |
|
2850 } |
|
2851 else |
|
2852 { |
|
2853 retval.resize (1, nc); |
|
2854 for (int j = 0; j < nc; j++) |
|
2855 { |
|
2856 retval.elem (0, j) = 0.0; |
|
2857 for (int i = 0; i < nr; i++) |
|
2858 { |
|
2859 Complex d = elem (i, j); |
|
2860 retval.elem (0, j) += d * d; |
|
2861 } |
|
2862 } |
|
2863 } |
|
2864 } |
|
2865 return retval; |
|
2866 } |
|
2867 |
|
2868 ComplexColumnVector |
|
2869 ComplexMatrix::diag (void) const |
|
2870 { |
|
2871 return diag (0); |
|
2872 } |
|
2873 |
|
2874 ComplexColumnVector |
|
2875 ComplexMatrix::diag (int k) const |
|
2876 { |
|
2877 int nnr = rows (); |
|
2878 int nnc = cols (); |
|
2879 if (k > 0) |
|
2880 nnc -= k; |
|
2881 else if (k < 0) |
|
2882 nnr += k; |
|
2883 |
|
2884 ComplexColumnVector d; |
|
2885 |
|
2886 if (nnr > 0 && nnc > 0) |
|
2887 { |
|
2888 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
2889 |
|
2890 d.resize (ndiag); |
|
2891 |
|
2892 if (k > 0) |
|
2893 { |
|
2894 for (int i = 0; i < ndiag; i++) |
|
2895 d.elem (i) = elem (i, i+k); |
|
2896 } |
|
2897 else if ( k < 0) |
|
2898 { |
|
2899 for (int i = 0; i < ndiag; i++) |
|
2900 d.elem (i) = elem (i-k, i); |
|
2901 } |
|
2902 else |
|
2903 { |
|
2904 for (int i = 0; i < ndiag; i++) |
|
2905 d.elem (i) = elem (i, i); |
|
2906 } |
|
2907 } |
|
2908 else |
|
2909 cerr << "diag: requested diagonal out of range\n"; |
|
2910 |
|
2911 return d; |
|
2912 } |
|
2913 |
891
|
2914 // XXX FIXME XXX -- it would be nice to share some code among all the |
|
2915 // min/max functions below. It would also be nice to combine the |
|
2916 // min/max and min_loc/max_loc functions. |
|
2917 |
458
|
2918 ComplexColumnVector |
|
2919 ComplexMatrix::row_min (void) const |
|
2920 { |
|
2921 ComplexColumnVector result; |
|
2922 |
|
2923 int nr = rows (); |
|
2924 int nc = cols (); |
|
2925 if (nr > 0 && nc > 0) |
|
2926 { |
|
2927 result.resize (nr); |
|
2928 |
|
2929 for (int i = 0; i < nr; i++) |
|
2930 { |
891
|
2931 int row_is_real_only = 1; |
|
2932 for (int j = 0; j < nc; j++) |
|
2933 if (imag (elem (i, j)) != 0.0) |
458
|
2934 { |
891
|
2935 row_is_real_only = 0; |
|
2936 break; |
458
|
2937 } |
891
|
2938 |
|
2939 if (row_is_real_only) |
|
2940 { |
|
2941 double res = real (elem (i, 0)); |
|
2942 for (int j = 1; j < nc; j++) |
|
2943 { |
|
2944 double tmp = real (elem (i, j)); |
|
2945 if (tmp < res) |
|
2946 res = tmp; |
|
2947 } |
|
2948 result.elem (i) = res; |
|
2949 } |
|
2950 else |
|
2951 { |
|
2952 Complex res = elem (i, 0); |
|
2953 double absres = abs (res); |
|
2954 for (int j = 1; j < nc; j++) |
|
2955 if (abs (elem (i, j)) < absres) |
|
2956 { |
|
2957 res = elem (i, j); |
|
2958 absres = abs (res); |
|
2959 } |
|
2960 result.elem (i) = res; |
|
2961 } |
458
|
2962 } |
|
2963 } |
|
2964 |
|
2965 return result; |
|
2966 } |
|
2967 |
|
2968 ComplexColumnVector |
|
2969 ComplexMatrix::row_min_loc (void) const |
|
2970 { |
|
2971 ComplexColumnVector result; |
|
2972 |
|
2973 int nr = rows (); |
|
2974 int nc = cols (); |
|
2975 |
|
2976 if (nr > 0 && nc > 0) |
|
2977 { |
|
2978 result.resize (nr); |
|
2979 |
|
2980 for (int i = 0; i < nr; i++) |
|
2981 { |
891
|
2982 int column_is_real_only = 1; |
|
2983 for (int j = 0; j < nc; j++) |
|
2984 if (imag (elem (i, j)) != 0.0) |
|
2985 { |
|
2986 column_is_real_only = 0; |
|
2987 break; |
|
2988 } |
|
2989 |
|
2990 if (column_is_real_only) |
|
2991 { |
|
2992 double res = 0; |
|
2993 double tmp = real (elem (i, 0)); |
|
2994 for (int j = 1; j < nc; j++) |
|
2995 if (real (elem (i, j)) < tmp) |
|
2996 res = j; |
|
2997 |
|
2998 result.elem (i) = res + 1; |
|
2999 } |
|
3000 else |
|
3001 { |
|
3002 Complex res = 0; |
|
3003 double absres = abs (elem (i, 0)); |
|
3004 for (int j = 1; j < nc; j++) |
|
3005 if (abs (elem (i, j)) < absres) |
|
3006 { |
|
3007 res = j; |
|
3008 absres = abs (elem (i, j)); |
|
3009 } |
|
3010 result.elem (i) = res + 1; |
|
3011 } |
458
|
3012 } |
|
3013 } |
|
3014 |
|
3015 return result; |
|
3016 } |
|
3017 |
|
3018 ComplexColumnVector |
|
3019 ComplexMatrix::row_max (void) const |
|
3020 { |
|
3021 ComplexColumnVector result; |
|
3022 |
|
3023 int nr = rows (); |
|
3024 int nc = cols (); |
|
3025 |
|
3026 if (nr > 0 && nc > 0) |
|
3027 { |
|
3028 result.resize (nr); |
|
3029 |
|
3030 for (int i = 0; i < nr; i++) |
|
3031 { |
891
|
3032 int row_is_real_only = 1; |
|
3033 for (int j = 0; j < nc; j++) |
|
3034 if (imag (elem (i, j)) != 0.0) |
458
|
3035 { |
891
|
3036 row_is_real_only = 0; |
|
3037 break; |
458
|
3038 } |
891
|
3039 |
|
3040 if (row_is_real_only) |
|
3041 { |
|
3042 double res = real (elem (i, 0)); |
|
3043 for (int j = 1; j < nc; j++) |
|
3044 { |
|
3045 double tmp = real (elem (i, j)); |
|
3046 if (tmp > res) |
|
3047 res = tmp; |
|
3048 } |
|
3049 result.elem (i) = res; |
|
3050 } |
|
3051 else |
|
3052 { |
|
3053 Complex res = elem (i, 0); |
|
3054 double absres = abs (res); |
|
3055 for (int j = 1; j < nc; j++) |
|
3056 if (abs (elem (i, j)) > absres) |
|
3057 { |
|
3058 res = elem (i, j); |
|
3059 absres = abs (res); |
|
3060 } |
|
3061 result.elem (i) = res; |
|
3062 } |
458
|
3063 } |
|
3064 } |
|
3065 |
|
3066 return result; |
|
3067 } |
|
3068 |
|
3069 ComplexColumnVector |
|
3070 ComplexMatrix::row_max_loc (void) const |
|
3071 { |
|
3072 ComplexColumnVector result; |
|
3073 |
|
3074 int nr = rows (); |
|
3075 int nc = cols (); |
|
3076 |
|
3077 if (nr > 0 && nc > 0) |
|
3078 { |
|
3079 result.resize (nr); |
|
3080 |
|
3081 for (int i = 0; i < nr; i++) |
|
3082 { |
891
|
3083 int column_is_real_only = 1; |
|
3084 for (int j = 0; j < nc; j++) |
|
3085 if (imag (elem (i, j)) != 0.0) |
|
3086 { |
|
3087 column_is_real_only = 0; |
|
3088 break; |
|
3089 } |
|
3090 |
|
3091 if (column_is_real_only) |
|
3092 { |
|
3093 double res = 0; |
|
3094 double tmp = real (elem (i, 0)); |
|
3095 for (int j = 1; j < nc; j++) |
|
3096 if (real (elem (i, j)) > tmp) |
|
3097 res = j; |
|
3098 |
|
3099 result.elem (i) = res + 1; |
|
3100 } |
|
3101 else |
|
3102 { |
|
3103 Complex res = 0; |
|
3104 double absres = abs (elem (i, 0)); |
|
3105 for (int j = 1; j < nc; j++) |
|
3106 if (abs (elem (i, j)) > absres) |
|
3107 { |
|
3108 res = j; |
|
3109 absres = abs (elem (i, j)); |
|
3110 } |
|
3111 result.elem (i) = res + 1; |
|
3112 } |
458
|
3113 } |
|
3114 } |
|
3115 |
|
3116 return result; |
|
3117 } |
|
3118 |
|
3119 ComplexRowVector |
|
3120 ComplexMatrix::column_min (void) const |
|
3121 { |
|
3122 ComplexRowVector result; |
|
3123 |
|
3124 int nr = rows (); |
|
3125 int nc = cols (); |
|
3126 |
|
3127 if (nr > 0 && nc > 0) |
|
3128 { |
|
3129 result.resize (nc); |
|
3130 |
|
3131 for (int j = 0; j < nc; j++) |
|
3132 { |
891
|
3133 int column_is_real_only = 1; |
|
3134 for (int i = 0; i < nr; i++) |
|
3135 if (imag (elem (i, j)) != 0.0) |
458
|
3136 { |
891
|
3137 column_is_real_only = 0; |
|
3138 break; |
458
|
3139 } |
891
|
3140 |
|
3141 if (column_is_real_only) |
|
3142 { |
|
3143 double res = real (elem (0, j)); |
|
3144 for (int i = 1; i < nr; i++) |
|
3145 { |
|
3146 double tmp = real (elem (i, j)); |
|
3147 if (tmp < res) |
|
3148 res = tmp; |
|
3149 } |
|
3150 result.elem (j) = res; |
|
3151 } |
|
3152 else |
|
3153 { |
|
3154 Complex res = elem (0, j); |
|
3155 double absres = abs (res); |
|
3156 for (int i = 1; i < nr; i++) |
|
3157 if (abs (elem (i, j)) < absres) |
|
3158 { |
|
3159 res = elem (i, j); |
|
3160 absres = abs (res); |
|
3161 } |
|
3162 result.elem (j) = res; |
|
3163 } |
458
|
3164 } |
|
3165 } |
|
3166 |
|
3167 return result; |
|
3168 } |
|
3169 |
|
3170 ComplexRowVector |
|
3171 ComplexMatrix::column_min_loc (void) const |
|
3172 { |
|
3173 ComplexRowVector result; |
|
3174 |
|
3175 int nr = rows (); |
|
3176 int nc = cols (); |
|
3177 |
|
3178 if (nr > 0 && nc > 0) |
|
3179 { |
|
3180 result.resize (nc); |
|
3181 |
|
3182 for (int j = 0; j < nc; j++) |
|
3183 { |
891
|
3184 int column_is_real_only = 1; |
|
3185 for (int i = 0; i < nr; i++) |
|
3186 if (imag (elem (i, j)) != 0.0) |
|
3187 { |
|
3188 column_is_real_only = 0; |
|
3189 break; |
|
3190 } |
|
3191 |
|
3192 if (column_is_real_only) |
|
3193 { |
|
3194 double res = 0; |
892
|
3195 double tmp = real (elem (0, j)); |
891
|
3196 for (int i = 1; i < nr; i++) |
|
3197 if (real (elem (i, j)) < tmp) |
|
3198 res = i; |
|
3199 |
|
3200 result.elem (j) = res + 1; |
|
3201 } |
|
3202 else |
|
3203 { |
|
3204 Complex res = 0; |
|
3205 double absres = abs (elem (0, j)); |
|
3206 for (int i = 1; i < nr; i++) |
|
3207 if (abs (elem (i, j)) < absres) |
|
3208 { |
|
3209 res = i; |
|
3210 absres = abs (elem (i, j)); |
|
3211 } |
|
3212 result.elem (j) = res + 1; |
|
3213 } |
458
|
3214 } |
|
3215 } |
|
3216 |
|
3217 return result; |
|
3218 } |
|
3219 |
|
3220 ComplexRowVector |
|
3221 ComplexMatrix::column_max (void) const |
|
3222 { |
|
3223 ComplexRowVector result; |
|
3224 |
|
3225 int nr = rows (); |
|
3226 int nc = cols (); |
|
3227 |
|
3228 if (nr > 0 && nc > 0) |
|
3229 { |
|
3230 result.resize (nc); |
|
3231 |
|
3232 for (int j = 0; j < nc; j++) |
|
3233 { |
891
|
3234 int column_is_real_only = 1; |
|
3235 for (int i = 0; i < nr; i++) |
|
3236 if (imag (elem (i, j)) != 0.0) |
458
|
3237 { |
891
|
3238 column_is_real_only = 0; |
|
3239 break; |
458
|
3240 } |
891
|
3241 |
|
3242 if (column_is_real_only) |
|
3243 { |
|
3244 double res = real (elem (0, j)); |
|
3245 for (int i = 1; i < nr; i++) |
|
3246 { |
|
3247 double tmp = real (elem (i, j)); |
|
3248 if (tmp > res) |
|
3249 res = tmp; |
|
3250 } |
|
3251 result.elem (j) = res; |
|
3252 } |
|
3253 else |
|
3254 { |
|
3255 Complex res = elem (0, j); |
|
3256 double absres = abs (res); |
|
3257 for (int i = 1; i < nr; i++) |
|
3258 if (abs (elem (i, j)) > absres) |
|
3259 { |
|
3260 res = elem (i, j); |
|
3261 absres = abs (res); |
|
3262 } |
|
3263 result.elem (j) = res; |
|
3264 } |
458
|
3265 } |
|
3266 } |
|
3267 |
|
3268 return result; |
|
3269 } |
|
3270 |
|
3271 ComplexRowVector |
|
3272 ComplexMatrix::column_max_loc (void) const |
|
3273 { |
|
3274 ComplexRowVector result; |
|
3275 |
|
3276 int nr = rows (); |
|
3277 int nc = cols (); |
|
3278 |
|
3279 if (nr > 0 && nc > 0) |
|
3280 { |
|
3281 result.resize (nc); |
|
3282 |
|
3283 for (int j = 0; j < nc; j++) |
|
3284 { |
891
|
3285 int column_is_real_only = 1; |
|
3286 for (int i = 0; i < nr; i++) |
|
3287 if (imag (elem (i, j)) != 0.0) |
|
3288 { |
|
3289 column_is_real_only = 0; |
|
3290 break; |
|
3291 } |
|
3292 |
|
3293 if (column_is_real_only) |
|
3294 { |
|
3295 double res = 0; |
892
|
3296 double tmp = real (elem (0, j)); |
891
|
3297 for (int i = 1; i < nr; i++) |
|
3298 if (real (elem (i, j)) > tmp) |
|
3299 res = i; |
|
3300 |
|
3301 result.elem (j) = res + 1; |
|
3302 } |
|
3303 else |
|
3304 { |
|
3305 Complex res = 0; |
|
3306 double absres = abs (elem (0, j)); |
|
3307 for (int i = 1; i < nr; i++) |
|
3308 if (abs (elem (i, j)) > absres) |
|
3309 { |
|
3310 res = i; |
|
3311 absres = abs (elem (i, j)); |
|
3312 } |
|
3313 result.elem (j) = res + 1; |
|
3314 } |
458
|
3315 } |
|
3316 } |
|
3317 |
|
3318 return result; |
|
3319 } |
|
3320 |
|
3321 // i/o |
|
3322 |
|
3323 ostream& |
|
3324 operator << (ostream& os, const ComplexMatrix& a) |
|
3325 { |
|
3326 // int field_width = os.precision () + 7; |
|
3327 for (int i = 0; i < a.rows (); i++) |
|
3328 { |
|
3329 for (int j = 0; j < a.cols (); j++) |
|
3330 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
3331 os << "\n"; |
|
3332 } |
|
3333 return os; |
|
3334 } |
|
3335 |
|
3336 istream& |
|
3337 operator >> (istream& is, ComplexMatrix& a) |
|
3338 { |
|
3339 int nr = a.rows (); |
|
3340 int nc = a.cols (); |
|
3341 |
|
3342 if (nr < 1 || nc < 1) |
|
3343 is.clear (ios::badbit); |
|
3344 else |
|
3345 { |
|
3346 Complex tmp; |
|
3347 for (int i = 0; i < nr; i++) |
|
3348 for (int j = 0; j < nc; j++) |
|
3349 { |
|
3350 is >> tmp; |
|
3351 if (is) |
|
3352 a.elem (i, j) = tmp; |
|
3353 else |
|
3354 break; |
|
3355 } |
|
3356 } |
|
3357 |
|
3358 return is; |
|
3359 } |
|
3360 |
|
3361 /* |
|
3362 ;;; Local Variables: *** |
|
3363 ;;; mode: C++ *** |
|
3364 ;;; page-delimiter: "^/\\*" *** |
|
3365 ;;; End: *** |
|
3366 */ |