458
|
1 // DiagMatrix manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
|
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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #if defined (__GNUG__) |
|
29 #pragma implementation |
|
30 #endif |
|
31 |
|
32 #include <iostream.h> |
|
33 |
|
34 #include <Complex.h> |
|
35 |
|
36 #include "mx-base.h" |
|
37 #include "mx-inlines.cc" |
|
38 #include "lo-error.h" |
|
39 |
|
40 /* |
|
41 * Complex Diagonal Matrix class |
|
42 */ |
|
43 |
|
44 #define KLUDGE_DIAG_MATRICES |
|
45 #define TYPE Complex |
|
46 #define KL_DMAT_TYPE ComplexDiagMatrix |
|
47 #include "mx-kludge.cc" |
|
48 #undef KLUDGE_DIAG_MATRICES |
|
49 #undef TYPE |
|
50 #undef KL_DMAT_TYPE |
|
51 |
|
52 ComplexDiagMatrix::ComplexDiagMatrix (const RowVector& a) |
|
53 : DiagArray<Complex> (a.length ()) |
|
54 { |
|
55 for (int i = 0; i < length (); i++) |
|
56 elem (i, i) = a.elem (i); |
|
57 } |
|
58 |
|
59 ComplexDiagMatrix::ComplexDiagMatrix (const ColumnVector& a) |
|
60 : DiagArray<Complex> (a.length ()) |
|
61 { |
|
62 for (int i = 0; i < length (); i++) |
|
63 elem (i, i) = a.elem (i); |
|
64 } |
|
65 |
|
66 ComplexDiagMatrix::ComplexDiagMatrix (const DiagMatrix& a) |
|
67 : DiagArray<Complex> (a.rows (), a.cols ()) |
|
68 { |
|
69 for (int i = 0; i < length (); i++) |
|
70 elem (i, i) = a.elem (i, i); |
|
71 } |
|
72 |
|
73 #if 0 |
|
74 ComplexDiagMatrix& |
|
75 ComplexDiagMatrix::resize (int r, int c) |
|
76 { |
|
77 if (r < 0 || c < 0) |
|
78 { |
|
79 (*current_liboctave_error_handler) |
|
80 ("can't resize to negative dimensions"); |
|
81 return *this; |
|
82 } |
|
83 |
|
84 int new_len = r < c ? r : c; |
533
|
85 Complex *new_data = 0; |
458
|
86 if (new_len > 0) |
|
87 { |
|
88 new_data = new Complex [new_len]; |
|
89 |
|
90 int min_len = new_len < len ? new_len : len; |
|
91 |
|
92 for (int i = 0; i < min_len; i++) |
|
93 new_data[i] = data[i]; |
|
94 } |
|
95 |
|
96 delete [] data; |
|
97 nr = r; |
|
98 nc = c; |
|
99 len = new_len; |
|
100 data = new_data; |
|
101 |
|
102 return *this; |
|
103 } |
|
104 |
|
105 ComplexDiagMatrix& |
|
106 ComplexDiagMatrix::resize (int r, int c, double val) |
|
107 { |
|
108 if (r < 0 || c < 0) |
|
109 { |
|
110 (*current_liboctave_error_handler) |
|
111 ("can't resize to negative dimensions"); |
|
112 return *this; |
|
113 } |
|
114 |
|
115 int new_len = r < c ? r : c; |
533
|
116 Complex *new_data = 0; |
458
|
117 if (new_len > 0) |
|
118 { |
|
119 new_data = new Complex [new_len]; |
|
120 |
|
121 int min_len = new_len < len ? new_len : len; |
|
122 |
|
123 for (int i = 0; i < min_len; i++) |
|
124 new_data[i] = data[i]; |
|
125 |
|
126 for (i = min_len; i < new_len; i++) |
|
127 new_data[i] = val; |
|
128 } |
|
129 |
|
130 delete [] data; |
|
131 nr = r; |
|
132 nc = c; |
|
133 len = new_len; |
|
134 data = new_data; |
|
135 |
|
136 return *this; |
|
137 } |
|
138 |
|
139 ComplexDiagMatrix& |
|
140 ComplexDiagMatrix::resize (int r, int c, const Complex& val) |
|
141 { |
|
142 if (r < 0 || c < 0) |
|
143 { |
|
144 (*current_liboctave_error_handler) |
|
145 ("can't resize to negative dimensions"); |
|
146 return *this; |
|
147 } |
|
148 |
|
149 int new_len = r < c ? r : c; |
533
|
150 Complex *new_data = 0; |
458
|
151 if (new_len > 0) |
|
152 { |
|
153 new_data = new Complex [new_len]; |
|
154 |
|
155 int min_len = new_len < len ? new_len : len; |
|
156 |
|
157 for (int i = 0; i < min_len; i++) |
|
158 new_data[i] = data[i]; |
|
159 |
|
160 for (i = min_len; i < new_len; i++) |
|
161 new_data[i] = val; |
|
162 } |
|
163 |
|
164 delete [] data; |
|
165 nr = r; |
|
166 nc = c; |
|
167 len = new_len; |
|
168 data = new_data; |
|
169 |
|
170 return *this; |
|
171 } |
|
172 #endif |
|
173 |
|
174 int |
|
175 ComplexDiagMatrix::operator == (const ComplexDiagMatrix& a) const |
|
176 { |
|
177 if (rows () != a.rows () || cols () != a.cols ()) |
|
178 return 0; |
|
179 |
|
180 return equal (data (), a.data (), length ()); |
|
181 } |
|
182 |
|
183 int |
|
184 ComplexDiagMatrix::operator != (const ComplexDiagMatrix& a) const |
|
185 { |
|
186 return !(*this == a); |
|
187 } |
|
188 |
|
189 ComplexDiagMatrix |
|
190 ComplexDiagMatrix::hermitian (void) const |
|
191 { |
|
192 return ComplexDiagMatrix (conj_dup (data (), length ()), cols (), rows ()); |
|
193 } |
|
194 |
|
195 ComplexDiagMatrix& |
|
196 ComplexDiagMatrix::fill (double val) |
|
197 { |
|
198 for (int i = 0; i < length (); i++) |
|
199 elem (i, i) = val; |
|
200 return *this; |
|
201 } |
|
202 |
|
203 ComplexDiagMatrix& |
|
204 ComplexDiagMatrix::fill (const Complex& val) |
|
205 { |
|
206 for (int i = 0; i < length (); i++) |
|
207 elem (i, i) = val; |
|
208 return *this; |
|
209 } |
|
210 |
|
211 ComplexDiagMatrix& |
|
212 ComplexDiagMatrix::fill (double val, int beg, int end) |
|
213 { |
|
214 if (beg < 0 || end >= length () || end < beg) |
|
215 { |
|
216 (*current_liboctave_error_handler) ("range error for fill"); |
|
217 return *this; |
|
218 } |
|
219 |
|
220 for (int i = beg; i < end; i++) |
|
221 elem (i, i) = val; |
|
222 |
|
223 return *this; |
|
224 } |
|
225 |
|
226 ComplexDiagMatrix& |
|
227 ComplexDiagMatrix::fill (const Complex& val, int beg, int end) |
|
228 { |
|
229 if (beg < 0 || end >= length () || end < beg) |
|
230 { |
|
231 (*current_liboctave_error_handler) ("range error for fill"); |
|
232 return *this; |
|
233 } |
|
234 |
|
235 for (int i = beg; i < end; i++) |
|
236 elem (i, i) = val; |
|
237 |
|
238 return *this; |
|
239 } |
|
240 |
|
241 ComplexDiagMatrix& |
|
242 ComplexDiagMatrix::fill (const ColumnVector& a) |
|
243 { |
|
244 int len = length (); |
|
245 if (a.length () != len) |
|
246 { |
|
247 (*current_liboctave_error_handler) ("range error for fill"); |
|
248 return *this; |
|
249 } |
|
250 |
|
251 for (int i = 0; i < len; i++) |
|
252 elem (i, i) = a.elem (i); |
|
253 |
|
254 return *this; |
|
255 } |
|
256 |
|
257 ComplexDiagMatrix& |
|
258 ComplexDiagMatrix::fill (const ComplexColumnVector& a) |
|
259 { |
|
260 int len = length (); |
|
261 if (a.length () != len) |
|
262 { |
|
263 (*current_liboctave_error_handler) ("range error for fill"); |
|
264 return *this; |
|
265 } |
|
266 |
|
267 for (int i = 0; i < len; i++) |
|
268 elem (i, i) = a.elem (i); |
|
269 |
|
270 return *this; |
|
271 } |
|
272 |
|
273 ComplexDiagMatrix& |
|
274 ComplexDiagMatrix::fill (const RowVector& a) |
|
275 { |
|
276 int len = length (); |
|
277 if (a.length () != len) |
|
278 { |
|
279 (*current_liboctave_error_handler) ("range error for fill"); |
|
280 return *this; |
|
281 } |
|
282 |
|
283 for (int i = 0; i < len; i++) |
|
284 elem (i, i) = a.elem (i); |
|
285 |
|
286 return *this; |
|
287 } |
|
288 |
|
289 ComplexDiagMatrix& |
|
290 ComplexDiagMatrix::fill (const ComplexRowVector& a) |
|
291 { |
|
292 int len = length (); |
|
293 if (a.length () != len) |
|
294 { |
|
295 (*current_liboctave_error_handler) ("range error for fill"); |
|
296 return *this; |
|
297 } |
|
298 |
|
299 for (int i = 0; i < len; i++) |
|
300 elem (i, i) = a.elem (i); |
|
301 |
|
302 return *this; |
|
303 } |
|
304 |
|
305 ComplexDiagMatrix& |
|
306 ComplexDiagMatrix::fill (const ColumnVector& a, int beg) |
|
307 { |
|
308 int a_len = a.length (); |
|
309 if (beg < 0 || beg + a_len >= length ()) |
|
310 { |
|
311 (*current_liboctave_error_handler) ("range error for fill"); |
|
312 return *this; |
|
313 } |
|
314 |
|
315 for (int i = 0; i < a_len; i++) |
|
316 elem (i+beg, i+beg) = a.elem (i); |
|
317 |
|
318 return *this; |
|
319 } |
|
320 |
|
321 ComplexDiagMatrix& |
|
322 ComplexDiagMatrix::fill (const ComplexColumnVector& a, int beg) |
|
323 { |
|
324 int a_len = a.length (); |
|
325 if (beg < 0 || beg + a_len >= length ()) |
|
326 { |
|
327 (*current_liboctave_error_handler) ("range error for fill"); |
|
328 return *this; |
|
329 } |
|
330 |
|
331 for (int i = 0; i < a_len; i++) |
|
332 elem (i+beg, i+beg) = a.elem (i); |
|
333 |
|
334 return *this; |
|
335 } |
|
336 |
|
337 ComplexDiagMatrix& |
|
338 ComplexDiagMatrix::fill (const RowVector& a, int beg) |
|
339 { |
|
340 int a_len = a.length (); |
|
341 if (beg < 0 || beg + a_len >= length ()) |
|
342 { |
|
343 (*current_liboctave_error_handler) ("range error for fill"); |
|
344 return *this; |
|
345 } |
|
346 |
|
347 for (int i = 0; i < a_len; i++) |
|
348 elem (i+beg, i+beg) = a.elem (i); |
|
349 |
|
350 return *this; |
|
351 } |
|
352 |
|
353 ComplexDiagMatrix& |
|
354 ComplexDiagMatrix::fill (const ComplexRowVector& a, int beg) |
|
355 { |
|
356 int a_len = a.length (); |
|
357 if (beg < 0 || beg + a_len >= length ()) |
|
358 { |
|
359 (*current_liboctave_error_handler) ("range error for fill"); |
|
360 return *this; |
|
361 } |
|
362 |
|
363 for (int i = 0; i < a_len; i++) |
|
364 elem (i+beg, i+beg) = a.elem (i); |
|
365 |
|
366 return *this; |
|
367 } |
|
368 |
|
369 ComplexDiagMatrix |
|
370 ComplexDiagMatrix::transpose (void) const |
|
371 { |
|
372 return ComplexDiagMatrix (dup (data (), length ()), cols (), rows ()); |
|
373 } |
|
374 |
|
375 DiagMatrix |
|
376 real (const ComplexDiagMatrix& a) |
|
377 { |
|
378 DiagMatrix retval; |
|
379 int a_len = a.length (); |
|
380 if (a_len > 0) |
|
381 retval = DiagMatrix (real_dup (a.data (), a_len), a.rows (), |
|
382 a.cols ()); |
|
383 return retval; |
|
384 } |
|
385 |
|
386 DiagMatrix |
|
387 imag (const ComplexDiagMatrix& a) |
|
388 { |
|
389 DiagMatrix retval; |
|
390 int a_len = a.length (); |
|
391 if (a_len > 0) |
|
392 retval = DiagMatrix (imag_dup (a.data (), a_len), a.rows (), |
|
393 a.cols ()); |
|
394 return retval; |
|
395 } |
|
396 |
|
397 ComplexDiagMatrix |
|
398 conj (const ComplexDiagMatrix& a) |
|
399 { |
|
400 ComplexDiagMatrix retval; |
|
401 int a_len = a.length (); |
|
402 if (a_len > 0) |
|
403 retval = ComplexDiagMatrix (conj_dup (a.data (), a_len), |
|
404 a.rows (), a.cols ()); |
|
405 return retval; |
|
406 } |
|
407 |
|
408 // resize is the destructive analog for this one |
|
409 |
|
410 ComplexMatrix |
|
411 ComplexDiagMatrix::extract (int r1, int c1, int r2, int c2) const |
|
412 { |
|
413 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
414 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
415 |
|
416 int new_r = r2 - r1 + 1; |
|
417 int new_c = c2 - c1 + 1; |
|
418 |
|
419 ComplexMatrix result (new_r, new_c); |
|
420 |
|
421 for (int j = 0; j < new_c; j++) |
|
422 for (int i = 0; i < new_r; i++) |
|
423 result.elem (i, j) = elem (r1+i, c1+j); |
|
424 |
|
425 return result; |
|
426 } |
|
427 |
|
428 // extract row or column i. |
|
429 |
|
430 ComplexRowVector |
|
431 ComplexDiagMatrix::row (int i) const |
|
432 { |
|
433 int nr = rows (); |
|
434 int nc = cols (); |
|
435 if (i < 0 || i >= nr) |
|
436 { |
|
437 (*current_liboctave_error_handler) ("invalid row selection"); |
|
438 return RowVector (); |
|
439 } |
|
440 |
|
441 ComplexRowVector retval (nc, 0.0); |
|
442 if (nr <= nc || (nr > nc && i < nc)) |
|
443 retval.elem (i) = elem (i, i); |
|
444 |
|
445 return retval; |
|
446 } |
|
447 |
|
448 ComplexRowVector |
|
449 ComplexDiagMatrix::row (char *s) const |
|
450 { |
533
|
451 if (! s) |
458
|
452 { |
|
453 (*current_liboctave_error_handler) ("invalid row selection"); |
|
454 return ComplexRowVector (); |
|
455 } |
|
456 |
|
457 char c = *s; |
|
458 if (c == 'f' || c == 'F') |
|
459 return row (0); |
|
460 else if (c == 'l' || c == 'L') |
|
461 return row (rows () - 1); |
|
462 else |
|
463 { |
|
464 (*current_liboctave_error_handler) ("invalid row selection"); |
|
465 return ComplexRowVector (); |
|
466 } |
|
467 } |
|
468 |
|
469 ComplexColumnVector |
|
470 ComplexDiagMatrix::column (int i) const |
|
471 { |
|
472 int nr = rows (); |
|
473 int nc = cols (); |
|
474 if (i < 0 || i >= nc) |
|
475 { |
|
476 (*current_liboctave_error_handler) ("invalid column selection"); |
|
477 return ColumnVector (); |
|
478 } |
|
479 |
|
480 ComplexColumnVector retval (nr, 0.0); |
|
481 if (nr >= nc || (nr < nc && i < nr)) |
|
482 retval.elem (i) = elem (i, i); |
|
483 |
|
484 return retval; |
|
485 } |
|
486 |
|
487 ComplexColumnVector |
|
488 ComplexDiagMatrix::column (char *s) const |
|
489 { |
533
|
490 if (! s) |
458
|
491 { |
|
492 (*current_liboctave_error_handler) ("invalid column selection"); |
|
493 return ColumnVector (); |
|
494 } |
|
495 |
|
496 char c = *s; |
|
497 if (c == 'f' || c == 'F') |
|
498 return column (0); |
|
499 else if (c == 'l' || c == 'L') |
|
500 return column (cols () - 1); |
|
501 else |
|
502 { |
|
503 (*current_liboctave_error_handler) ("invalid column selection"); |
|
504 return ColumnVector (); |
|
505 } |
|
506 } |
|
507 |
|
508 ComplexDiagMatrix |
|
509 ComplexDiagMatrix::inverse (void) const |
|
510 { |
|
511 int info; |
|
512 return inverse (info); |
|
513 } |
|
514 |
|
515 ComplexDiagMatrix |
|
516 ComplexDiagMatrix::inverse (int& info) const |
|
517 { |
|
518 int nr = rows (); |
|
519 int nc = cols (); |
|
520 if (nr != nc) |
|
521 { |
|
522 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
523 return DiagMatrix (); |
|
524 } |
|
525 |
|
526 ComplexDiagMatrix retval (nr, nc); |
|
527 |
|
528 info = 0; |
|
529 for (int i = 0; i < length (); i++) |
|
530 { |
|
531 if (elem (i, i) == 0.0) |
|
532 { |
|
533 info = -1; |
|
534 return *this; |
|
535 } |
|
536 else |
|
537 retval.elem (i, i) = 1.0 / elem (i, i); |
|
538 } |
|
539 |
|
540 return *this; |
|
541 } |
|
542 |
|
543 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
544 |
|
545 ComplexDiagMatrix& |
|
546 ComplexDiagMatrix::operator += (const DiagMatrix& a) |
|
547 { |
|
548 int nr = rows (); |
|
549 int nc = cols (); |
|
550 if (nr != a.rows () || nc != a.cols ()) |
|
551 { |
|
552 (*current_liboctave_error_handler) |
|
553 ("nonconformant matrix += operation attempted"); |
|
554 return *this; |
|
555 } |
|
556 |
|
557 if (nr == 0 || nc == 0) |
|
558 return *this; |
|
559 |
|
560 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
561 |
|
562 add2 (d, a.data (), length ()); |
|
563 return *this; |
|
564 } |
|
565 |
|
566 ComplexDiagMatrix& |
|
567 ComplexDiagMatrix::operator -= (const DiagMatrix& a) |
|
568 { |
|
569 int nr = rows (); |
|
570 int nc = cols (); |
|
571 if (nr != a.rows () || nc != a.cols ()) |
|
572 { |
|
573 (*current_liboctave_error_handler) |
|
574 ("nonconformant matrix -= operation attempted"); |
|
575 return *this; |
|
576 } |
|
577 |
|
578 if (nr == 0 || nc == 0) |
|
579 return *this; |
|
580 |
|
581 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
582 |
|
583 subtract2 (d, a.data (), length ()); |
|
584 return *this; |
|
585 } |
|
586 |
|
587 ComplexDiagMatrix& |
|
588 ComplexDiagMatrix::operator += (const ComplexDiagMatrix& a) |
|
589 { |
|
590 int nr = rows (); |
|
591 int nc = cols (); |
|
592 if (nr != a.rows () || nc != a.cols ()) |
|
593 { |
|
594 (*current_liboctave_error_handler) |
|
595 ("nonconformant matrix += operation attempted"); |
|
596 return *this; |
|
597 } |
|
598 |
|
599 if (nr == 0 || nc == 0) |
|
600 return *this; |
|
601 |
|
602 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
603 |
|
604 add2 (d, a.data (), length ()); |
|
605 return *this; |
|
606 } |
|
607 |
|
608 ComplexDiagMatrix& |
|
609 ComplexDiagMatrix::operator -= (const ComplexDiagMatrix& a) |
|
610 { |
|
611 int nr = rows (); |
|
612 int nc = cols (); |
|
613 if (nr != a.rows () || nc != a.cols ()) |
|
614 { |
|
615 (*current_liboctave_error_handler) |
|
616 ("nonconformant matrix -= operation attempted"); |
|
617 return *this; |
|
618 } |
|
619 |
|
620 if (nr == 0 || nc == 0) |
|
621 return *this; |
|
622 |
|
623 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
624 |
|
625 subtract2 (d, a.data (), length ()); |
|
626 return *this; |
|
627 } |
|
628 |
|
629 // diagonal matrix by scalar -> matrix operations |
|
630 |
|
631 ComplexMatrix |
|
632 operator + (const ComplexDiagMatrix& a, double s) |
|
633 { |
|
634 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
635 return a + tmp; |
|
636 } |
|
637 |
|
638 ComplexMatrix |
|
639 operator - (const ComplexDiagMatrix& a, double s) |
|
640 { |
|
641 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
642 return a + tmp; |
|
643 } |
|
644 |
|
645 ComplexMatrix |
|
646 operator + (const ComplexDiagMatrix& a, const Complex& s) |
|
647 { |
|
648 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
649 return a + tmp; |
|
650 } |
|
651 |
|
652 ComplexMatrix |
|
653 operator - (const ComplexDiagMatrix& a, const Complex& s) |
|
654 { |
|
655 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
656 return a + tmp; |
|
657 } |
|
658 |
|
659 // diagonal matrix by scalar -> diagonal matrix operations |
|
660 |
|
661 ComplexDiagMatrix |
|
662 operator * (const ComplexDiagMatrix& a, double s) |
|
663 { |
|
664 return ComplexDiagMatrix (multiply (a.data (), a.length (), s), |
|
665 a.rows (), a.cols ()); |
|
666 } |
|
667 |
|
668 ComplexDiagMatrix |
|
669 operator / (const ComplexDiagMatrix& a, double s) |
|
670 { |
|
671 return ComplexDiagMatrix (divide (a.data (), a.length (), s), |
|
672 a.rows (), a.cols ()); |
|
673 } |
|
674 |
|
675 // scalar by diagonal matrix -> matrix operations |
|
676 |
|
677 ComplexMatrix |
|
678 operator + (double s, const ComplexDiagMatrix& a) |
|
679 { |
|
680 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
681 return tmp + a; |
|
682 } |
|
683 |
|
684 ComplexMatrix |
|
685 operator - (double s, const ComplexDiagMatrix& a) |
|
686 { |
|
687 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
688 return tmp - a; |
|
689 } |
|
690 |
|
691 ComplexMatrix |
|
692 operator + (const Complex& s, const ComplexDiagMatrix& a) |
|
693 { |
|
694 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
695 return tmp + a; |
|
696 } |
|
697 |
|
698 ComplexMatrix |
|
699 operator - (const Complex& s, const ComplexDiagMatrix& a) |
|
700 { |
|
701 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
702 return tmp - a; |
|
703 } |
|
704 |
|
705 // scalar by diagonal matrix -> diagonal matrix operations |
|
706 |
|
707 ComplexDiagMatrix |
|
708 operator * (double s, const ComplexDiagMatrix& a) |
|
709 { |
|
710 return ComplexDiagMatrix (multiply (a.data (), a.length (), s), |
|
711 a.rows (), a.cols ()); |
|
712 } |
|
713 |
|
714 // diagonal matrix by column vector -> column vector operations |
|
715 |
|
716 ComplexColumnVector |
|
717 operator * (const ComplexDiagMatrix& m, const ColumnVector& a) |
|
718 { |
|
719 int nr = m.rows (); |
|
720 int nc = m.cols (); |
|
721 int a_len = a.length (); |
|
722 if (nc != a_len) |
|
723 { |
|
724 (*current_liboctave_error_handler) |
|
725 ("nonconformant matrix muliplication attempted"); |
|
726 return ComplexColumnVector (); |
|
727 } |
|
728 |
|
729 if (nc == 0 || nr == 0) |
|
730 return ComplexColumnVector (0); |
|
731 |
|
732 ComplexColumnVector result (nr); |
|
733 |
|
734 for (int i = 0; i < a_len; i++) |
|
735 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
736 |
|
737 for (i = a_len; i < nr; i++) |
|
738 result.elem (i) = 0.0; |
|
739 |
|
740 return result; |
|
741 } |
|
742 |
|
743 ComplexColumnVector |
|
744 operator * (const ComplexDiagMatrix& m, const ComplexColumnVector& a) |
|
745 { |
|
746 int nr = m.rows (); |
|
747 int nc = m.cols (); |
|
748 int a_len = a.length (); |
|
749 if (nc != a_len) |
|
750 { |
|
751 (*current_liboctave_error_handler) |
|
752 ("nonconformant matrix muliplication attempted"); |
|
753 return ComplexColumnVector (); |
|
754 } |
|
755 |
|
756 if (nc == 0 || nr == 0) |
|
757 return ComplexColumnVector (0); |
|
758 |
|
759 ComplexColumnVector result (nr); |
|
760 |
|
761 for (int i = 0; i < a_len; i++) |
|
762 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
763 |
|
764 for (i = a_len; i < nr; i++) |
|
765 result.elem (i) = 0.0; |
|
766 |
|
767 return result; |
|
768 } |
|
769 |
|
770 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
771 |
|
772 ComplexDiagMatrix |
|
773 operator * (const ComplexDiagMatrix& a, const ComplexDiagMatrix& b) |
|
774 { |
|
775 int nr_a = a.rows (); |
|
776 int nc_a = a.cols (); |
|
777 int nr_b = b.rows (); |
|
778 int nc_b = b.cols (); |
|
779 if (nc_a != nr_b) |
|
780 { |
|
781 (*current_liboctave_error_handler) |
|
782 ("nonconformant matrix multiplication attempted"); |
|
783 return ComplexDiagMatrix (); |
|
784 } |
|
785 |
|
786 if (nr_a == 0 || nc_a == 0 || nc_b == 0) |
|
787 return ComplexDiagMatrix (nr_a, nc_a, 0.0); |
|
788 |
|
789 ComplexDiagMatrix c (nr_a, nc_b); |
|
790 |
|
791 int len = nr_a < nc_b ? nr_a : nc_b; |
|
792 |
|
793 for (int i = 0; i < len; i++) |
|
794 { |
|
795 Complex a_element = a.elem (i, i); |
|
796 Complex b_element = b.elem (i, i); |
|
797 |
|
798 if (a_element == 0.0 || b_element == 0.0) |
|
799 c.elem (i, i) = 0.0; |
|
800 else if (a_element == 1.0) |
|
801 c.elem (i, i) = b_element; |
|
802 else if (b_element == 1.0) |
|
803 c.elem (i, i) = a_element; |
|
804 else |
|
805 c.elem (i, i) = a_element * b_element; |
|
806 } |
|
807 |
|
808 return c; |
|
809 } |
|
810 |
|
811 ComplexDiagMatrix |
|
812 operator + (const ComplexDiagMatrix& m, const DiagMatrix& a) |
|
813 { |
|
814 int nr = m.rows (); |
|
815 int nc = m.cols (); |
|
816 if (nr != a.rows () || nc != a.cols ()) |
|
817 { |
|
818 (*current_liboctave_error_handler) |
|
819 ("nonconformant matrix addition attempted"); |
|
820 return ComplexDiagMatrix (); |
|
821 } |
|
822 |
|
823 if (nr == 0 || nc == 0) |
|
824 return ComplexDiagMatrix (nr, nc); |
|
825 |
|
826 return ComplexDiagMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
827 } |
|
828 |
|
829 ComplexDiagMatrix |
|
830 operator - (const ComplexDiagMatrix& m, const DiagMatrix& a) |
|
831 { |
|
832 int nr = m.rows (); |
|
833 int nc = m.cols (); |
|
834 if (nr != a.rows () || nc != a.cols ()) |
|
835 { |
|
836 (*current_liboctave_error_handler) |
|
837 ("nonconformant matrix subtraction attempted"); |
|
838 return ComplexDiagMatrix (); |
|
839 } |
|
840 |
|
841 if (nr == 0 || nc == 0) |
|
842 return ComplexDiagMatrix (nr, nc); |
|
843 |
|
844 return ComplexDiagMatrix (subtract (m.data (), a.data (), m.length ()), |
|
845 nr, nc); |
|
846 } |
|
847 |
|
848 ComplexDiagMatrix |
|
849 operator * (const ComplexDiagMatrix& a, const DiagMatrix& b) |
|
850 { |
|
851 int nr_a = a.rows (); |
|
852 int nc_a = a.cols (); |
|
853 int nr_b = b.rows (); |
|
854 int nc_b = b.cols (); |
|
855 if (nc_a != nr_b) |
|
856 { |
|
857 (*current_liboctave_error_handler) |
|
858 ("nonconformant matrix multiplication attempted"); |
|
859 return ComplexDiagMatrix (); |
|
860 } |
|
861 |
|
862 if (nr_a == 0 || nc_a == 0 || nc_b == 0) |
|
863 return ComplexDiagMatrix (nr_a, nc_a, 0.0); |
|
864 |
|
865 ComplexDiagMatrix c (nr_a, nc_b); |
|
866 |
|
867 int len = nr_a < nc_b ? nr_a : nc_b; |
|
868 |
|
869 for (int i = 0; i < len; i++) |
|
870 { |
|
871 Complex a_element = a.elem (i, i); |
|
872 double b_element = b.elem (i, i); |
|
873 |
|
874 if (a_element == 0.0 || b_element == 0.0) |
|
875 c.elem (i, i) = 0.0; |
|
876 else if (a_element == 1.0) |
|
877 c.elem (i, i) = b_element; |
|
878 else if (b_element == 1.0) |
|
879 c.elem (i, i) = a_element; |
|
880 else |
|
881 c.elem (i, i) = a_element * b_element; |
|
882 } |
|
883 |
|
884 return c; |
|
885 } |
|
886 |
|
887 ComplexDiagMatrix |
|
888 product (const ComplexDiagMatrix& m, const DiagMatrix& a) |
|
889 { |
|
890 int nr = m.rows (); |
|
891 int nc = m.cols (); |
|
892 if (nr != a.rows () || nc != a.cols ()) |
|
893 { |
|
894 (*current_liboctave_error_handler) |
|
895 ("nonconformant matrix product attempted"); |
|
896 return ComplexDiagMatrix (); |
|
897 } |
|
898 |
|
899 if (nr == 0 || nc == 0) |
|
900 return ComplexDiagMatrix (nr, nc); |
|
901 |
|
902 return ComplexDiagMatrix (multiply (m.data (), a.data (), m.length ()), |
|
903 nr, nc); |
|
904 } |
|
905 |
|
906 // diagonal matrix by matrix -> matrix operations |
|
907 |
|
908 ComplexMatrix |
|
909 operator + (const ComplexDiagMatrix& m, const Matrix& a) |
|
910 { |
|
911 int nr = m.rows (); |
|
912 int nc = m.cols (); |
|
913 if (nr != a.rows () || nc != a.cols ()) |
|
914 { |
|
915 (*current_liboctave_error_handler) |
|
916 ("nonconformant matrix addition attempted"); |
|
917 return ComplexMatrix (); |
|
918 } |
|
919 |
|
920 if (nr == 0 || nc == 0) |
|
921 return ComplexMatrix (nr, nc); |
|
922 |
|
923 ComplexMatrix result (a); |
|
924 for (int i = 0; i < m.length (); i++) |
|
925 result.elem (i, i) += m.elem (i, i); |
|
926 |
|
927 return result; |
|
928 } |
|
929 |
|
930 ComplexMatrix |
|
931 operator - (const ComplexDiagMatrix& m, const Matrix& a) |
|
932 { |
|
933 int nr = m.rows (); |
|
934 int nc = m.cols (); |
|
935 if (nr != a.rows () || nc != a.cols ()) |
|
936 { |
|
937 (*current_liboctave_error_handler) |
|
938 ("nonconformant matrix subtraction attempted"); |
|
939 return ComplexMatrix (); |
|
940 } |
|
941 |
|
942 if (nr == 0 || nc == 0) |
|
943 return ComplexMatrix (nr, nc); |
|
944 |
|
945 ComplexMatrix result (-a); |
|
946 for (int i = 0; i < m.length (); i++) |
|
947 result.elem (i, i) += m.elem (i, i); |
|
948 |
|
949 return result; |
|
950 } |
|
951 |
|
952 ComplexMatrix |
|
953 operator * (const ComplexDiagMatrix& m, const Matrix& a) |
|
954 { |
|
955 int nr = m.rows (); |
|
956 int nc = m.cols (); |
|
957 int a_nr = a.rows (); |
|
958 int a_nc = a.cols (); |
|
959 if (nc != a_nr) |
|
960 { |
|
961 (*current_liboctave_error_handler) |
|
962 ("nonconformant matrix multiplication attempted"); |
|
963 return ComplexMatrix (); |
|
964 } |
|
965 |
|
966 if (nr == 0 || nc == 0 || a_nc == 0) |
|
967 return ComplexMatrix (nr, a_nc, 0.0); |
|
968 |
|
969 ComplexMatrix c (nr, a_nc); |
|
970 |
|
971 for (int i = 0; i < m.length (); i++) |
|
972 { |
|
973 if (m.elem (i, i) == 1.0) |
|
974 { |
|
975 for (int j = 0; j < a_nc; j++) |
|
976 c.elem (i, j) = a.elem (i, j); |
|
977 } |
|
978 else if (m.elem (i, i) == 0.0) |
|
979 { |
|
980 for (int j = 0; j < a_nc; j++) |
|
981 c.elem (i, j) = 0.0; |
|
982 } |
|
983 else |
|
984 { |
|
985 for (int j = 0; j < a_nc; j++) |
|
986 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
987 } |
|
988 } |
|
989 |
|
990 if (nr > nc) |
|
991 { |
|
992 for (int j = 0; j < a_nc; j++) |
|
993 for (int i = a_nr; i < nr; i++) |
|
994 c.elem (i, j) = 0.0; |
|
995 } |
|
996 |
|
997 return c; |
|
998 } |
|
999 |
|
1000 ComplexMatrix |
|
1001 operator + (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
1002 { |
|
1003 int nr = m.rows (); |
|
1004 int nc = m.cols (); |
|
1005 if (nr != a.rows () || nc != a.cols ()) |
|
1006 { |
|
1007 (*current_liboctave_error_handler) |
|
1008 ("nonconformant matrix addition attempted"); |
|
1009 return ComplexMatrix (); |
|
1010 } |
|
1011 |
|
1012 if (nr == 0 || nc == 0) |
|
1013 return ComplexMatrix (nr, nc); |
|
1014 |
|
1015 ComplexMatrix result (a); |
|
1016 for (int i = 0; i < m.length (); i++) |
|
1017 result.elem (i, i) += m.elem (i, i); |
|
1018 |
|
1019 return result; |
|
1020 } |
|
1021 |
|
1022 ComplexMatrix |
|
1023 operator - (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
1024 { |
|
1025 int nr = m.rows (); |
|
1026 int nc = m.cols (); |
|
1027 if (nr != a.rows () || nc != a.cols ()) |
|
1028 { |
|
1029 (*current_liboctave_error_handler) |
|
1030 ("nonconformant matrix subtraction attempted"); |
|
1031 return ComplexMatrix (); |
|
1032 } |
|
1033 |
|
1034 if (nr == 0 || nc == 0) |
|
1035 return ComplexMatrix (nr, nc); |
|
1036 |
|
1037 ComplexMatrix result (-a); |
|
1038 for (int i = 0; i < m.length (); i++) |
|
1039 result.elem (i, i) += m.elem (i, i); |
|
1040 |
|
1041 return result; |
|
1042 } |
|
1043 |
|
1044 ComplexMatrix |
|
1045 operator * (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
1046 { |
|
1047 int nr = m.rows (); |
|
1048 int nc = m.cols (); |
|
1049 int a_nr = a.rows (); |
|
1050 int a_nc = a.cols (); |
|
1051 if (nc != a_nr) |
|
1052 { |
|
1053 (*current_liboctave_error_handler) |
|
1054 ("nonconformant matrix multiplication attempted"); |
|
1055 return ComplexMatrix (); |
|
1056 } |
|
1057 |
|
1058 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1059 return ComplexMatrix (nr, a_nc, 0.0); |
|
1060 |
|
1061 ComplexMatrix c (nr, a_nc); |
|
1062 |
|
1063 for (int i = 0; i < m.length (); i++) |
|
1064 { |
|
1065 if (m.elem (i, i) == 1.0) |
|
1066 { |
|
1067 for (int j = 0; j < a_nc; j++) |
|
1068 c.elem (i, j) = a.elem (i, j); |
|
1069 } |
|
1070 else if (m.elem (i, i) == 0.0) |
|
1071 { |
|
1072 for (int j = 0; j < a_nc; j++) |
|
1073 c.elem (i, j) = 0.0; |
|
1074 } |
|
1075 else |
|
1076 { |
|
1077 for (int j = 0; j < a_nc; j++) |
|
1078 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
1079 } |
|
1080 } |
|
1081 |
|
1082 if (nr > nc) |
|
1083 { |
|
1084 for (int j = 0; j < a_nc; j++) |
|
1085 for (int i = a_nr; i < nr; i++) |
|
1086 c.elem (i, j) = 0.0; |
|
1087 } |
|
1088 |
|
1089 return c; |
|
1090 } |
|
1091 |
|
1092 // other operations |
|
1093 |
|
1094 ComplexColumnVector |
|
1095 ComplexDiagMatrix::diag (void) const |
|
1096 { |
|
1097 return diag (0); |
|
1098 } |
|
1099 |
|
1100 // Could be optimized... |
|
1101 |
|
1102 ComplexColumnVector |
|
1103 ComplexDiagMatrix::diag (int k) const |
|
1104 { |
|
1105 int nnr = rows (); |
|
1106 int nnc = cols (); |
|
1107 if (k > 0) |
|
1108 nnc -= k; |
|
1109 else if (k < 0) |
|
1110 nnr += k; |
|
1111 |
|
1112 ComplexColumnVector d; |
|
1113 |
|
1114 if (nnr > 0 && nnc > 0) |
|
1115 { |
|
1116 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
1117 |
|
1118 d.resize (ndiag); |
|
1119 |
|
1120 if (k > 0) |
|
1121 { |
|
1122 for (int i = 0; i < ndiag; i++) |
|
1123 d.elem (i) = elem (i, i+k); |
|
1124 } |
|
1125 else if ( k < 0) |
|
1126 { |
|
1127 for (int i = 0; i < ndiag; i++) |
|
1128 d.elem (i) = elem (i-k, i); |
|
1129 } |
|
1130 else |
|
1131 { |
|
1132 for (int i = 0; i < ndiag; i++) |
|
1133 d.elem (i) = elem (i, i); |
|
1134 } |
|
1135 } |
|
1136 else |
|
1137 cerr << "diag: requested diagonal out of range\n"; |
|
1138 |
|
1139 return d; |
|
1140 } |
|
1141 |
|
1142 // i/o |
|
1143 |
|
1144 ostream& |
|
1145 operator << (ostream& os, const ComplexDiagMatrix& a) |
|
1146 { |
|
1147 Complex ZERO (0.0); |
|
1148 // int field_width = os.precision () + 7; |
|
1149 for (int i = 0; i < a.rows (); i++) |
|
1150 { |
|
1151 for (int j = 0; j < a.cols (); j++) |
|
1152 { |
|
1153 if (i == j) |
|
1154 os << " " /* setw (field_width) */ << a.elem (i, i); |
|
1155 else |
|
1156 os << " " /* setw (field_width) */ << ZERO; |
|
1157 } |
|
1158 os << "\n"; |
|
1159 } |
|
1160 return os; |
|
1161 } |
|
1162 |
|
1163 /* |
|
1164 ;;; Local Variables: *** |
|
1165 ;;; mode: C++ *** |
|
1166 ;;; page-delimiter: "^/\\*" *** |
|
1167 ;;; End: *** |
|
1168 */ |