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