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