458
|
1 // DiagMatrix 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 |
|
32 #include <iostream.h> |
|
33 |
|
34 #include <Complex.h> |
|
35 |
1368
|
36 #include "lo-error.h" |
458
|
37 #include "mx-base.h" |
|
38 #include "mx-inlines.cc" |
|
39 |
1360
|
40 // Complex Diagonal Matrix class |
458
|
41 |
|
42 ComplexDiagMatrix::ComplexDiagMatrix (const RowVector& a) |
1214
|
43 : MDiagArray<Complex> (a.length ()) |
458
|
44 { |
|
45 for (int i = 0; i < length (); i++) |
|
46 elem (i, i) = a.elem (i); |
|
47 } |
|
48 |
|
49 ComplexDiagMatrix::ComplexDiagMatrix (const ColumnVector& a) |
1214
|
50 : MDiagArray<Complex> (a.length ()) |
458
|
51 { |
|
52 for (int i = 0; i < length (); i++) |
|
53 elem (i, i) = a.elem (i); |
|
54 } |
|
55 |
|
56 ComplexDiagMatrix::ComplexDiagMatrix (const DiagMatrix& a) |
1214
|
57 : MDiagArray<Complex> (a.rows (), a.cols ()) |
458
|
58 { |
|
59 for (int i = 0; i < length (); i++) |
|
60 elem (i, i) = a.elem (i, i); |
|
61 } |
|
62 |
|
63 int |
|
64 ComplexDiagMatrix::operator == (const ComplexDiagMatrix& a) const |
|
65 { |
|
66 if (rows () != a.rows () || cols () != a.cols ()) |
|
67 return 0; |
|
68 |
|
69 return equal (data (), a.data (), length ()); |
|
70 } |
|
71 |
|
72 int |
|
73 ComplexDiagMatrix::operator != (const ComplexDiagMatrix& a) const |
|
74 { |
|
75 return !(*this == a); |
|
76 } |
|
77 |
|
78 ComplexDiagMatrix& |
|
79 ComplexDiagMatrix::fill (double val) |
|
80 { |
|
81 for (int i = 0; i < length (); i++) |
|
82 elem (i, i) = val; |
|
83 return *this; |
|
84 } |
|
85 |
|
86 ComplexDiagMatrix& |
|
87 ComplexDiagMatrix::fill (const Complex& val) |
|
88 { |
|
89 for (int i = 0; i < length (); i++) |
|
90 elem (i, i) = val; |
|
91 return *this; |
|
92 } |
|
93 |
|
94 ComplexDiagMatrix& |
|
95 ComplexDiagMatrix::fill (double val, int beg, int end) |
|
96 { |
|
97 if (beg < 0 || end >= length () || end < beg) |
|
98 { |
|
99 (*current_liboctave_error_handler) ("range error for fill"); |
|
100 return *this; |
|
101 } |
|
102 |
|
103 for (int i = beg; i < end; i++) |
|
104 elem (i, i) = val; |
|
105 |
|
106 return *this; |
|
107 } |
|
108 |
|
109 ComplexDiagMatrix& |
|
110 ComplexDiagMatrix::fill (const Complex& val, int beg, int end) |
|
111 { |
|
112 if (beg < 0 || end >= length () || end < beg) |
|
113 { |
|
114 (*current_liboctave_error_handler) ("range error for fill"); |
|
115 return *this; |
|
116 } |
|
117 |
|
118 for (int i = beg; i < end; i++) |
|
119 elem (i, i) = val; |
|
120 |
|
121 return *this; |
|
122 } |
|
123 |
|
124 ComplexDiagMatrix& |
|
125 ComplexDiagMatrix::fill (const ColumnVector& a) |
|
126 { |
|
127 int len = length (); |
|
128 if (a.length () != len) |
|
129 { |
|
130 (*current_liboctave_error_handler) ("range error for fill"); |
|
131 return *this; |
|
132 } |
|
133 |
|
134 for (int i = 0; i < len; i++) |
|
135 elem (i, i) = a.elem (i); |
|
136 |
|
137 return *this; |
|
138 } |
|
139 |
|
140 ComplexDiagMatrix& |
|
141 ComplexDiagMatrix::fill (const ComplexColumnVector& a) |
|
142 { |
|
143 int len = length (); |
|
144 if (a.length () != len) |
|
145 { |
|
146 (*current_liboctave_error_handler) ("range error for fill"); |
|
147 return *this; |
|
148 } |
|
149 |
|
150 for (int i = 0; i < len; i++) |
|
151 elem (i, i) = a.elem (i); |
|
152 |
|
153 return *this; |
|
154 } |
|
155 |
|
156 ComplexDiagMatrix& |
|
157 ComplexDiagMatrix::fill (const RowVector& a) |
|
158 { |
|
159 int len = length (); |
|
160 if (a.length () != len) |
|
161 { |
|
162 (*current_liboctave_error_handler) ("range error for fill"); |
|
163 return *this; |
|
164 } |
|
165 |
|
166 for (int i = 0; i < len; i++) |
|
167 elem (i, i) = a.elem (i); |
|
168 |
|
169 return *this; |
|
170 } |
|
171 |
|
172 ComplexDiagMatrix& |
|
173 ComplexDiagMatrix::fill (const ComplexRowVector& a) |
|
174 { |
|
175 int len = length (); |
|
176 if (a.length () != len) |
|
177 { |
|
178 (*current_liboctave_error_handler) ("range error for fill"); |
|
179 return *this; |
|
180 } |
|
181 |
|
182 for (int i = 0; i < len; i++) |
|
183 elem (i, i) = a.elem (i); |
|
184 |
|
185 return *this; |
|
186 } |
|
187 |
|
188 ComplexDiagMatrix& |
|
189 ComplexDiagMatrix::fill (const ColumnVector& a, int beg) |
|
190 { |
|
191 int a_len = a.length (); |
|
192 if (beg < 0 || beg + a_len >= length ()) |
|
193 { |
|
194 (*current_liboctave_error_handler) ("range error for fill"); |
|
195 return *this; |
|
196 } |
|
197 |
|
198 for (int i = 0; i < a_len; i++) |
|
199 elem (i+beg, i+beg) = a.elem (i); |
|
200 |
|
201 return *this; |
|
202 } |
|
203 |
|
204 ComplexDiagMatrix& |
|
205 ComplexDiagMatrix::fill (const ComplexColumnVector& a, int beg) |
|
206 { |
|
207 int a_len = a.length (); |
|
208 if (beg < 0 || beg + a_len >= length ()) |
|
209 { |
|
210 (*current_liboctave_error_handler) ("range error for fill"); |
|
211 return *this; |
|
212 } |
|
213 |
|
214 for (int i = 0; i < a_len; i++) |
|
215 elem (i+beg, i+beg) = a.elem (i); |
|
216 |
|
217 return *this; |
|
218 } |
|
219 |
|
220 ComplexDiagMatrix& |
|
221 ComplexDiagMatrix::fill (const RowVector& a, int beg) |
|
222 { |
|
223 int a_len = a.length (); |
|
224 if (beg < 0 || beg + a_len >= length ()) |
|
225 { |
|
226 (*current_liboctave_error_handler) ("range error for fill"); |
|
227 return *this; |
|
228 } |
|
229 |
|
230 for (int i = 0; i < a_len; i++) |
|
231 elem (i+beg, i+beg) = a.elem (i); |
|
232 |
|
233 return *this; |
|
234 } |
|
235 |
|
236 ComplexDiagMatrix& |
|
237 ComplexDiagMatrix::fill (const ComplexRowVector& a, int beg) |
|
238 { |
|
239 int a_len = a.length (); |
|
240 if (beg < 0 || beg + a_len >= length ()) |
|
241 { |
|
242 (*current_liboctave_error_handler) ("range error for fill"); |
|
243 return *this; |
|
244 } |
|
245 |
|
246 for (int i = 0; i < a_len; i++) |
|
247 elem (i+beg, i+beg) = a.elem (i); |
|
248 |
|
249 return *this; |
|
250 } |
|
251 |
|
252 ComplexDiagMatrix |
1205
|
253 ComplexDiagMatrix::hermitian (void) const |
|
254 { |
|
255 return ComplexDiagMatrix (conj_dup (data (), length ()), cols (), rows ()); |
|
256 } |
|
257 |
|
258 ComplexDiagMatrix |
458
|
259 ComplexDiagMatrix::transpose (void) const |
|
260 { |
|
261 return ComplexDiagMatrix (dup (data (), length ()), cols (), rows ()); |
|
262 } |
|
263 |
|
264 ComplexDiagMatrix |
|
265 conj (const ComplexDiagMatrix& a) |
|
266 { |
|
267 ComplexDiagMatrix retval; |
|
268 int a_len = a.length (); |
|
269 if (a_len > 0) |
|
270 retval = ComplexDiagMatrix (conj_dup (a.data (), a_len), |
|
271 a.rows (), a.cols ()); |
|
272 return retval; |
|
273 } |
|
274 |
|
275 // resize is the destructive analog for this one |
|
276 |
|
277 ComplexMatrix |
|
278 ComplexDiagMatrix::extract (int r1, int c1, int r2, int c2) const |
|
279 { |
|
280 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
281 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
282 |
|
283 int new_r = r2 - r1 + 1; |
|
284 int new_c = c2 - c1 + 1; |
|
285 |
|
286 ComplexMatrix result (new_r, new_c); |
|
287 |
|
288 for (int j = 0; j < new_c; j++) |
|
289 for (int i = 0; i < new_r; i++) |
|
290 result.elem (i, j) = elem (r1+i, c1+j); |
|
291 |
|
292 return result; |
|
293 } |
|
294 |
|
295 // extract row or column i. |
|
296 |
|
297 ComplexRowVector |
|
298 ComplexDiagMatrix::row (int i) const |
|
299 { |
|
300 int nr = rows (); |
|
301 int nc = cols (); |
|
302 if (i < 0 || i >= nr) |
|
303 { |
|
304 (*current_liboctave_error_handler) ("invalid row selection"); |
|
305 return RowVector (); |
|
306 } |
|
307 |
|
308 ComplexRowVector retval (nc, 0.0); |
|
309 if (nr <= nc || (nr > nc && i < nc)) |
|
310 retval.elem (i) = elem (i, i); |
|
311 |
|
312 return retval; |
|
313 } |
|
314 |
|
315 ComplexRowVector |
|
316 ComplexDiagMatrix::row (char *s) const |
|
317 { |
533
|
318 if (! s) |
458
|
319 { |
|
320 (*current_liboctave_error_handler) ("invalid row selection"); |
|
321 return ComplexRowVector (); |
|
322 } |
|
323 |
|
324 char c = *s; |
|
325 if (c == 'f' || c == 'F') |
|
326 return row (0); |
|
327 else if (c == 'l' || c == 'L') |
|
328 return row (rows () - 1); |
|
329 else |
|
330 { |
|
331 (*current_liboctave_error_handler) ("invalid row selection"); |
|
332 return ComplexRowVector (); |
|
333 } |
|
334 } |
|
335 |
|
336 ComplexColumnVector |
|
337 ComplexDiagMatrix::column (int i) const |
|
338 { |
|
339 int nr = rows (); |
|
340 int nc = cols (); |
|
341 if (i < 0 || i >= nc) |
|
342 { |
|
343 (*current_liboctave_error_handler) ("invalid column selection"); |
|
344 return ColumnVector (); |
|
345 } |
|
346 |
|
347 ComplexColumnVector retval (nr, 0.0); |
|
348 if (nr >= nc || (nr < nc && i < nr)) |
|
349 retval.elem (i) = elem (i, i); |
|
350 |
|
351 return retval; |
|
352 } |
|
353 |
|
354 ComplexColumnVector |
|
355 ComplexDiagMatrix::column (char *s) const |
|
356 { |
533
|
357 if (! s) |
458
|
358 { |
|
359 (*current_liboctave_error_handler) ("invalid column selection"); |
|
360 return ColumnVector (); |
|
361 } |
|
362 |
|
363 char c = *s; |
|
364 if (c == 'f' || c == 'F') |
|
365 return column (0); |
|
366 else if (c == 'l' || c == 'L') |
|
367 return column (cols () - 1); |
|
368 else |
|
369 { |
|
370 (*current_liboctave_error_handler) ("invalid column selection"); |
|
371 return ColumnVector (); |
|
372 } |
|
373 } |
|
374 |
|
375 ComplexDiagMatrix |
|
376 ComplexDiagMatrix::inverse (void) const |
|
377 { |
|
378 int info; |
|
379 return inverse (info); |
|
380 } |
|
381 |
|
382 ComplexDiagMatrix |
|
383 ComplexDiagMatrix::inverse (int& info) const |
|
384 { |
|
385 int nr = rows (); |
|
386 int nc = cols (); |
|
387 if (nr != nc) |
|
388 { |
|
389 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
390 return DiagMatrix (); |
|
391 } |
|
392 |
|
393 ComplexDiagMatrix retval (nr, nc); |
|
394 |
|
395 info = 0; |
|
396 for (int i = 0; i < length (); i++) |
|
397 { |
|
398 if (elem (i, i) == 0.0) |
|
399 { |
|
400 info = -1; |
|
401 return *this; |
|
402 } |
|
403 else |
|
404 retval.elem (i, i) = 1.0 / elem (i, i); |
|
405 } |
|
406 |
1627
|
407 return retval; |
458
|
408 } |
|
409 |
|
410 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
411 |
|
412 ComplexDiagMatrix& |
|
413 ComplexDiagMatrix::operator += (const DiagMatrix& a) |
|
414 { |
|
415 int nr = rows (); |
|
416 int nc = cols (); |
|
417 if (nr != a.rows () || nc != a.cols ()) |
|
418 { |
|
419 (*current_liboctave_error_handler) |
|
420 ("nonconformant matrix += operation attempted"); |
|
421 return *this; |
|
422 } |
|
423 |
|
424 if (nr == 0 || nc == 0) |
|
425 return *this; |
|
426 |
|
427 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
428 |
|
429 add2 (d, a.data (), length ()); |
|
430 return *this; |
|
431 } |
|
432 |
|
433 ComplexDiagMatrix& |
|
434 ComplexDiagMatrix::operator -= (const DiagMatrix& a) |
|
435 { |
|
436 int nr = rows (); |
|
437 int nc = cols (); |
|
438 if (nr != a.rows () || nc != a.cols ()) |
|
439 { |
|
440 (*current_liboctave_error_handler) |
|
441 ("nonconformant matrix -= operation attempted"); |
|
442 return *this; |
|
443 } |
|
444 |
|
445 if (nr == 0 || nc == 0) |
|
446 return *this; |
|
447 |
|
448 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
449 |
|
450 subtract2 (d, a.data (), length ()); |
|
451 return *this; |
|
452 } |
|
453 |
|
454 ComplexDiagMatrix& |
|
455 ComplexDiagMatrix::operator += (const ComplexDiagMatrix& a) |
|
456 { |
|
457 int nr = rows (); |
|
458 int nc = cols (); |
|
459 if (nr != a.rows () || nc != a.cols ()) |
|
460 { |
|
461 (*current_liboctave_error_handler) |
|
462 ("nonconformant matrix += operation attempted"); |
|
463 return *this; |
|
464 } |
|
465 |
|
466 if (nr == 0 || nc == 0) |
|
467 return *this; |
|
468 |
|
469 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
470 |
|
471 add2 (d, a.data (), length ()); |
|
472 return *this; |
|
473 } |
|
474 |
|
475 ComplexDiagMatrix& |
|
476 ComplexDiagMatrix::operator -= (const ComplexDiagMatrix& a) |
|
477 { |
|
478 int nr = rows (); |
|
479 int nc = cols (); |
|
480 if (nr != a.rows () || nc != a.cols ()) |
|
481 { |
|
482 (*current_liboctave_error_handler) |
|
483 ("nonconformant matrix -= operation attempted"); |
|
484 return *this; |
|
485 } |
|
486 |
|
487 if (nr == 0 || nc == 0) |
|
488 return *this; |
|
489 |
|
490 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
491 |
|
492 subtract2 (d, a.data (), length ()); |
|
493 return *this; |
|
494 } |
|
495 |
|
496 // diagonal matrix by scalar -> diagonal matrix operations |
|
497 |
|
498 ComplexDiagMatrix |
|
499 operator * (const ComplexDiagMatrix& a, double s) |
|
500 { |
|
501 return ComplexDiagMatrix (multiply (a.data (), a.length (), s), |
|
502 a.rows (), a.cols ()); |
|
503 } |
|
504 |
|
505 ComplexDiagMatrix |
|
506 operator / (const ComplexDiagMatrix& a, double s) |
|
507 { |
|
508 return ComplexDiagMatrix (divide (a.data (), a.length (), s), |
|
509 a.rows (), a.cols ()); |
|
510 } |
|
511 |
1205
|
512 ComplexDiagMatrix |
|
513 operator * (const DiagMatrix& a, const Complex& s) |
458
|
514 { |
1205
|
515 return ComplexDiagMatrix (multiply (a.data (), a.length (), s), |
|
516 a.rows (), a.cols ()); |
458
|
517 } |
|
518 |
1205
|
519 ComplexDiagMatrix |
|
520 operator / (const DiagMatrix& a, const Complex& s) |
458
|
521 { |
1205
|
522 return ComplexDiagMatrix (divide (a.data (), a.length (), s), |
|
523 a.rows (), a.cols ()); |
458
|
524 } |
|
525 |
|
526 // scalar by diagonal matrix -> diagonal matrix operations |
|
527 |
|
528 ComplexDiagMatrix |
|
529 operator * (double s, const ComplexDiagMatrix& a) |
|
530 { |
|
531 return ComplexDiagMatrix (multiply (a.data (), a.length (), s), |
|
532 a.rows (), a.cols ()); |
|
533 } |
|
534 |
1205
|
535 ComplexDiagMatrix |
|
536 operator * (const Complex& s, const DiagMatrix& a) |
458
|
537 { |
1205
|
538 return ComplexDiagMatrix (multiply (a.data (), a.length (), s), |
|
539 a.rows (), a.cols ()); |
458
|
540 } |
|
541 |
|
542 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
543 |
|
544 ComplexDiagMatrix |
|
545 operator * (const ComplexDiagMatrix& a, const ComplexDiagMatrix& b) |
|
546 { |
|
547 int nr_a = a.rows (); |
|
548 int nc_a = a.cols (); |
|
549 int nr_b = b.rows (); |
|
550 int nc_b = b.cols (); |
|
551 if (nc_a != nr_b) |
|
552 { |
|
553 (*current_liboctave_error_handler) |
|
554 ("nonconformant matrix multiplication attempted"); |
|
555 return ComplexDiagMatrix (); |
|
556 } |
|
557 |
|
558 if (nr_a == 0 || nc_a == 0 || nc_b == 0) |
|
559 return ComplexDiagMatrix (nr_a, nc_a, 0.0); |
|
560 |
|
561 ComplexDiagMatrix c (nr_a, nc_b); |
|
562 |
|
563 int len = nr_a < nc_b ? nr_a : nc_b; |
|
564 |
|
565 for (int i = 0; i < len; i++) |
|
566 { |
|
567 Complex a_element = a.elem (i, i); |
|
568 Complex b_element = b.elem (i, i); |
|
569 |
|
570 if (a_element == 0.0 || b_element == 0.0) |
|
571 c.elem (i, i) = 0.0; |
|
572 else if (a_element == 1.0) |
|
573 c.elem (i, i) = b_element; |
|
574 else if (b_element == 1.0) |
|
575 c.elem (i, i) = a_element; |
|
576 else |
|
577 c.elem (i, i) = a_element * b_element; |
|
578 } |
|
579 |
|
580 return c; |
|
581 } |
|
582 |
|
583 ComplexDiagMatrix |
|
584 operator + (const ComplexDiagMatrix& m, const DiagMatrix& a) |
|
585 { |
|
586 int nr = m.rows (); |
|
587 int nc = m.cols (); |
|
588 if (nr != a.rows () || nc != a.cols ()) |
|
589 { |
|
590 (*current_liboctave_error_handler) |
|
591 ("nonconformant matrix addition attempted"); |
|
592 return ComplexDiagMatrix (); |
|
593 } |
|
594 |
|
595 if (nr == 0 || nc == 0) |
|
596 return ComplexDiagMatrix (nr, nc); |
|
597 |
|
598 return ComplexDiagMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
599 } |
|
600 |
|
601 ComplexDiagMatrix |
|
602 operator - (const ComplexDiagMatrix& m, const DiagMatrix& a) |
|
603 { |
|
604 int nr = m.rows (); |
|
605 int nc = m.cols (); |
|
606 if (nr != a.rows () || nc != a.cols ()) |
|
607 { |
|
608 (*current_liboctave_error_handler) |
|
609 ("nonconformant matrix subtraction attempted"); |
|
610 return ComplexDiagMatrix (); |
|
611 } |
|
612 |
|
613 if (nr == 0 || nc == 0) |
|
614 return ComplexDiagMatrix (nr, nc); |
|
615 |
|
616 return ComplexDiagMatrix (subtract (m.data (), a.data (), m.length ()), |
|
617 nr, nc); |
|
618 } |
|
619 |
|
620 ComplexDiagMatrix |
|
621 operator * (const ComplexDiagMatrix& a, const DiagMatrix& b) |
|
622 { |
|
623 int nr_a = a.rows (); |
|
624 int nc_a = a.cols (); |
|
625 int nr_b = b.rows (); |
|
626 int nc_b = b.cols (); |
|
627 if (nc_a != nr_b) |
|
628 { |
|
629 (*current_liboctave_error_handler) |
|
630 ("nonconformant matrix multiplication attempted"); |
|
631 return ComplexDiagMatrix (); |
|
632 } |
|
633 |
|
634 if (nr_a == 0 || nc_a == 0 || nc_b == 0) |
|
635 return ComplexDiagMatrix (nr_a, nc_a, 0.0); |
|
636 |
|
637 ComplexDiagMatrix c (nr_a, nc_b); |
|
638 |
|
639 int len = nr_a < nc_b ? nr_a : nc_b; |
|
640 |
|
641 for (int i = 0; i < len; i++) |
|
642 { |
|
643 Complex a_element = a.elem (i, i); |
|
644 double b_element = b.elem (i, i); |
|
645 |
|
646 if (a_element == 0.0 || b_element == 0.0) |
|
647 c.elem (i, i) = 0.0; |
|
648 else if (a_element == 1.0) |
|
649 c.elem (i, i) = b_element; |
|
650 else if (b_element == 1.0) |
|
651 c.elem (i, i) = a_element; |
|
652 else |
|
653 c.elem (i, i) = a_element * b_element; |
|
654 } |
|
655 |
|
656 return c; |
|
657 } |
|
658 |
|
659 ComplexDiagMatrix |
1205
|
660 operator + (const DiagMatrix& m, const ComplexDiagMatrix& a) |
|
661 { |
|
662 int nr = m.rows (); |
|
663 int nc = m.cols (); |
|
664 if (nr != a.rows () || nc != a.cols ()) |
|
665 { |
|
666 (*current_liboctave_error_handler) |
|
667 ("nonconformant matrix addition attempted"); |
|
668 return ComplexDiagMatrix (); |
|
669 } |
|
670 |
|
671 if (nc == 0 || nr == 0) |
|
672 return ComplexDiagMatrix (nr, nc); |
|
673 |
|
674 return ComplexDiagMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
675 } |
|
676 |
|
677 ComplexDiagMatrix |
|
678 operator - (const DiagMatrix& m, const ComplexDiagMatrix& a) |
|
679 { |
|
680 int nr = m.rows (); |
|
681 int nc = m.cols (); |
|
682 if (nr != a.rows () || nc != a.cols ()) |
|
683 { |
|
684 (*current_liboctave_error_handler) |
|
685 ("nonconformant matrix subtraction attempted"); |
|
686 return ComplexDiagMatrix (); |
|
687 } |
|
688 |
|
689 if (nc == 0 || nr == 0) |
|
690 return ComplexDiagMatrix (nr, nc); |
|
691 |
|
692 return ComplexDiagMatrix (subtract (m.data (), a.data (), m.length ()), |
|
693 nr, nc); |
|
694 } |
|
695 |
|
696 ComplexDiagMatrix |
|
697 operator * (const DiagMatrix& a, const ComplexDiagMatrix& b) |
|
698 { |
|
699 int nr_a = a.rows (); |
|
700 int nc_a = a.cols (); |
|
701 int nr_b = b.rows (); |
|
702 int nc_b = b.cols (); |
|
703 if (nc_a != nr_b) |
|
704 { |
|
705 (*current_liboctave_error_handler) |
|
706 ("nonconformant matrix multiplication attempted"); |
|
707 return ComplexDiagMatrix (); |
|
708 } |
|
709 |
|
710 if (nr_a == 0 || nc_a == 0 || nc_b == 0) |
|
711 return ComplexDiagMatrix (nr_a, nc_a, 0.0); |
|
712 |
|
713 ComplexDiagMatrix c (nr_a, nc_b); |
|
714 |
|
715 int len = nr_a < nc_b ? nr_a : nc_b; |
|
716 |
|
717 for (int i = 0; i < len; i++) |
|
718 { |
|
719 double a_element = a.elem (i, i); |
|
720 Complex b_element = b.elem (i, i); |
|
721 |
|
722 if (a_element == 0.0 || b_element == 0.0) |
|
723 c.elem (i, i) = 0.0; |
|
724 else if (a_element == 1.0) |
|
725 c.elem (i, i) = b_element; |
|
726 else if (b_element == 1.0) |
|
727 c.elem (i, i) = a_element; |
|
728 else |
|
729 c.elem (i, i) = a_element * b_element; |
|
730 } |
|
731 |
|
732 return c; |
|
733 } |
|
734 |
|
735 ComplexDiagMatrix |
458
|
736 product (const ComplexDiagMatrix& m, const DiagMatrix& a) |
|
737 { |
|
738 int nr = m.rows (); |
|
739 int nc = m.cols (); |
|
740 if (nr != a.rows () || nc != a.cols ()) |
|
741 { |
|
742 (*current_liboctave_error_handler) |
|
743 ("nonconformant matrix product attempted"); |
|
744 return ComplexDiagMatrix (); |
|
745 } |
|
746 |
|
747 if (nr == 0 || nc == 0) |
|
748 return ComplexDiagMatrix (nr, nc); |
|
749 |
|
750 return ComplexDiagMatrix (multiply (m.data (), a.data (), m.length ()), |
|
751 nr, nc); |
|
752 } |
|
753 |
1205
|
754 ComplexDiagMatrix |
|
755 product (const DiagMatrix& m, const ComplexDiagMatrix& a) |
458
|
756 { |
|
757 int nr = m.rows (); |
|
758 int nc = m.cols (); |
|
759 if (nr != a.rows () || nc != a.cols ()) |
|
760 { |
|
761 (*current_liboctave_error_handler) |
1205
|
762 ("nonconformant matrix product attempted"); |
|
763 return ComplexDiagMatrix (); |
458
|
764 } |
|
765 |
1205
|
766 if (nc == 0 || nr == 0) |
|
767 return ComplexDiagMatrix (nr, nc); |
458
|
768 |
1205
|
769 return ComplexDiagMatrix (multiply (m.data (), a.data (), m.length ()), |
|
770 nr, nc); |
458
|
771 } |
|
772 |
|
773 // other operations |
|
774 |
|
775 ComplexColumnVector |
|
776 ComplexDiagMatrix::diag (void) const |
|
777 { |
|
778 return diag (0); |
|
779 } |
|
780 |
|
781 // Could be optimized... |
|
782 |
|
783 ComplexColumnVector |
|
784 ComplexDiagMatrix::diag (int k) const |
|
785 { |
|
786 int nnr = rows (); |
|
787 int nnc = cols (); |
|
788 if (k > 0) |
|
789 nnc -= k; |
|
790 else if (k < 0) |
|
791 nnr += k; |
|
792 |
|
793 ComplexColumnVector d; |
|
794 |
|
795 if (nnr > 0 && nnc > 0) |
|
796 { |
|
797 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
798 |
|
799 d.resize (ndiag); |
|
800 |
|
801 if (k > 0) |
|
802 { |
|
803 for (int i = 0; i < ndiag; i++) |
|
804 d.elem (i) = elem (i, i+k); |
|
805 } |
|
806 else if ( k < 0) |
|
807 { |
|
808 for (int i = 0; i < ndiag; i++) |
|
809 d.elem (i) = elem (i-k, i); |
|
810 } |
|
811 else |
|
812 { |
|
813 for (int i = 0; i < ndiag; i++) |
|
814 d.elem (i) = elem (i, i); |
|
815 } |
|
816 } |
|
817 else |
|
818 cerr << "diag: requested diagonal out of range\n"; |
|
819 |
|
820 return d; |
|
821 } |
|
822 |
|
823 // i/o |
|
824 |
|
825 ostream& |
|
826 operator << (ostream& os, const ComplexDiagMatrix& a) |
|
827 { |
|
828 Complex ZERO (0.0); |
|
829 // int field_width = os.precision () + 7; |
|
830 for (int i = 0; i < a.rows (); i++) |
|
831 { |
|
832 for (int j = 0; j < a.cols (); j++) |
|
833 { |
|
834 if (i == j) |
|
835 os << " " /* setw (field_width) */ << a.elem (i, i); |
|
836 else |
|
837 os << " " /* setw (field_width) */ << ZERO; |
|
838 } |
|
839 os << "\n"; |
|
840 } |
|
841 return os; |
|
842 } |
|
843 |
|
844 /* |
|
845 ;;; Local Variables: *** |
|
846 ;;; mode: C++ *** |
|
847 ;;; page-delimiter: "^/\\*" *** |
|
848 ;;; End: *** |
|
849 */ |