1993
|
1 // DiagMatrix manipulations. |
458
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 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 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
25 #pragma implementation |
|
26 #endif |
|
27 |
458
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
458
|
30 #endif |
|
31 |
3503
|
32 #include <iostream> |
458
|
33 |
4669
|
34 #include "Array-util.h" |
1368
|
35 #include "lo-error.h" |
458
|
36 #include "mx-base.h" |
|
37 #include "mx-inlines.cc" |
1650
|
38 #include "oct-cmplx.h" |
458
|
39 |
1360
|
40 // Complex Diagonal Matrix class |
458
|
41 |
|
42 ComplexDiagMatrix::ComplexDiagMatrix (const DiagMatrix& a) |
1989
|
43 : MDiagArray2<Complex> (a.rows (), a.cols ()) |
458
|
44 { |
|
45 for (int i = 0; i < length (); i++) |
|
46 elem (i, i) = a.elem (i, i); |
|
47 } |
|
48 |
2386
|
49 bool |
458
|
50 ComplexDiagMatrix::operator == (const ComplexDiagMatrix& a) const |
|
51 { |
|
52 if (rows () != a.rows () || cols () != a.cols ()) |
|
53 return 0; |
|
54 |
3769
|
55 return mx_inline_equal (data (), a.data (), length ()); |
458
|
56 } |
|
57 |
2386
|
58 bool |
458
|
59 ComplexDiagMatrix::operator != (const ComplexDiagMatrix& a) const |
|
60 { |
|
61 return !(*this == a); |
|
62 } |
|
63 |
|
64 ComplexDiagMatrix& |
|
65 ComplexDiagMatrix::fill (double val) |
|
66 { |
|
67 for (int i = 0; i < length (); i++) |
|
68 elem (i, i) = val; |
|
69 return *this; |
|
70 } |
|
71 |
|
72 ComplexDiagMatrix& |
|
73 ComplexDiagMatrix::fill (const Complex& val) |
|
74 { |
|
75 for (int i = 0; i < length (); i++) |
|
76 elem (i, i) = val; |
|
77 return *this; |
|
78 } |
|
79 |
|
80 ComplexDiagMatrix& |
|
81 ComplexDiagMatrix::fill (double val, int beg, int end) |
|
82 { |
|
83 if (beg < 0 || end >= length () || end < beg) |
|
84 { |
|
85 (*current_liboctave_error_handler) ("range error for fill"); |
|
86 return *this; |
|
87 } |
|
88 |
1696
|
89 for (int i = beg; i <= end; i++) |
458
|
90 elem (i, i) = val; |
|
91 |
|
92 return *this; |
|
93 } |
|
94 |
|
95 ComplexDiagMatrix& |
|
96 ComplexDiagMatrix::fill (const Complex& val, int beg, int end) |
|
97 { |
|
98 if (beg < 0 || end >= length () || end < beg) |
|
99 { |
|
100 (*current_liboctave_error_handler) ("range error for fill"); |
|
101 return *this; |
|
102 } |
|
103 |
1696
|
104 for (int i = beg; i <= end; i++) |
458
|
105 elem (i, i) = val; |
|
106 |
|
107 return *this; |
|
108 } |
|
109 |
|
110 ComplexDiagMatrix& |
|
111 ComplexDiagMatrix::fill (const ColumnVector& a) |
|
112 { |
|
113 int len = length (); |
|
114 if (a.length () != len) |
|
115 { |
|
116 (*current_liboctave_error_handler) ("range error for fill"); |
|
117 return *this; |
|
118 } |
|
119 |
|
120 for (int i = 0; i < len; i++) |
|
121 elem (i, i) = a.elem (i); |
|
122 |
|
123 return *this; |
|
124 } |
|
125 |
|
126 ComplexDiagMatrix& |
|
127 ComplexDiagMatrix::fill (const ComplexColumnVector& a) |
|
128 { |
|
129 int len = length (); |
|
130 if (a.length () != len) |
|
131 { |
|
132 (*current_liboctave_error_handler) ("range error for fill"); |
|
133 return *this; |
|
134 } |
|
135 |
|
136 for (int i = 0; i < len; i++) |
|
137 elem (i, i) = a.elem (i); |
|
138 |
|
139 return *this; |
|
140 } |
|
141 |
|
142 ComplexDiagMatrix& |
|
143 ComplexDiagMatrix::fill (const RowVector& a) |
|
144 { |
|
145 int len = length (); |
|
146 if (a.length () != len) |
|
147 { |
|
148 (*current_liboctave_error_handler) ("range error for fill"); |
|
149 return *this; |
|
150 } |
|
151 |
|
152 for (int i = 0; i < len; i++) |
|
153 elem (i, i) = a.elem (i); |
|
154 |
|
155 return *this; |
|
156 } |
|
157 |
|
158 ComplexDiagMatrix& |
|
159 ComplexDiagMatrix::fill (const ComplexRowVector& a) |
|
160 { |
|
161 int len = length (); |
|
162 if (a.length () != len) |
|
163 { |
|
164 (*current_liboctave_error_handler) ("range error for fill"); |
|
165 return *this; |
|
166 } |
|
167 |
|
168 for (int i = 0; i < len; i++) |
|
169 elem (i, i) = a.elem (i); |
|
170 |
|
171 return *this; |
|
172 } |
|
173 |
|
174 ComplexDiagMatrix& |
|
175 ComplexDiagMatrix::fill (const ColumnVector& a, int beg) |
|
176 { |
|
177 int a_len = a.length (); |
|
178 if (beg < 0 || beg + a_len >= length ()) |
|
179 { |
|
180 (*current_liboctave_error_handler) ("range error for fill"); |
|
181 return *this; |
|
182 } |
|
183 |
|
184 for (int i = 0; i < a_len; i++) |
|
185 elem (i+beg, i+beg) = a.elem (i); |
|
186 |
|
187 return *this; |
|
188 } |
|
189 |
|
190 ComplexDiagMatrix& |
|
191 ComplexDiagMatrix::fill (const ComplexColumnVector& a, int beg) |
|
192 { |
|
193 int a_len = a.length (); |
|
194 if (beg < 0 || beg + a_len >= length ()) |
|
195 { |
|
196 (*current_liboctave_error_handler) ("range error for fill"); |
|
197 return *this; |
|
198 } |
|
199 |
|
200 for (int i = 0; i < a_len; i++) |
|
201 elem (i+beg, i+beg) = a.elem (i); |
|
202 |
|
203 return *this; |
|
204 } |
|
205 |
|
206 ComplexDiagMatrix& |
|
207 ComplexDiagMatrix::fill (const RowVector& a, int beg) |
|
208 { |
|
209 int a_len = a.length (); |
|
210 if (beg < 0 || beg + a_len >= length ()) |
|
211 { |
|
212 (*current_liboctave_error_handler) ("range error for fill"); |
|
213 return *this; |
|
214 } |
|
215 |
|
216 for (int i = 0; i < a_len; i++) |
|
217 elem (i+beg, i+beg) = a.elem (i); |
|
218 |
|
219 return *this; |
|
220 } |
|
221 |
|
222 ComplexDiagMatrix& |
|
223 ComplexDiagMatrix::fill (const ComplexRowVector& a, int beg) |
|
224 { |
|
225 int a_len = a.length (); |
|
226 if (beg < 0 || beg + a_len >= length ()) |
|
227 { |
|
228 (*current_liboctave_error_handler) ("range error for fill"); |
|
229 return *this; |
|
230 } |
|
231 |
|
232 for (int i = 0; i < a_len; i++) |
|
233 elem (i+beg, i+beg) = a.elem (i); |
|
234 |
|
235 return *this; |
|
236 } |
|
237 |
|
238 ComplexDiagMatrix |
1205
|
239 ComplexDiagMatrix::hermitian (void) const |
|
240 { |
3769
|
241 return ComplexDiagMatrix (mx_inline_conj_dup (data (), length ()), |
|
242 cols (), rows ()); |
1205
|
243 } |
|
244 |
|
245 ComplexDiagMatrix |
458
|
246 ComplexDiagMatrix::transpose (void) const |
|
247 { |
3769
|
248 return ComplexDiagMatrix (mx_inline_dup (data (), length ()), |
|
249 cols (), rows ()); |
458
|
250 } |
|
251 |
|
252 ComplexDiagMatrix |
|
253 conj (const ComplexDiagMatrix& a) |
|
254 { |
|
255 ComplexDiagMatrix retval; |
|
256 int a_len = a.length (); |
|
257 if (a_len > 0) |
3769
|
258 retval = ComplexDiagMatrix (mx_inline_conj_dup (a.data (), a_len), |
458
|
259 a.rows (), a.cols ()); |
|
260 return retval; |
|
261 } |
|
262 |
|
263 // resize is the destructive analog for this one |
|
264 |
|
265 ComplexMatrix |
|
266 ComplexDiagMatrix::extract (int r1, int c1, int r2, int c2) const |
|
267 { |
|
268 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
269 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
270 |
|
271 int new_r = r2 - r1 + 1; |
|
272 int new_c = c2 - c1 + 1; |
|
273 |
|
274 ComplexMatrix result (new_r, new_c); |
|
275 |
|
276 for (int j = 0; j < new_c; j++) |
|
277 for (int i = 0; i < new_r; i++) |
|
278 result.elem (i, j) = elem (r1+i, c1+j); |
|
279 |
|
280 return result; |
|
281 } |
|
282 |
|
283 // extract row or column i. |
|
284 |
|
285 ComplexRowVector |
|
286 ComplexDiagMatrix::row (int i) const |
|
287 { |
3504
|
288 int r = rows (); |
|
289 int c = cols (); |
|
290 if (i < 0 || i >= r) |
458
|
291 { |
|
292 (*current_liboctave_error_handler) ("invalid row selection"); |
3585
|
293 return ComplexRowVector (); |
458
|
294 } |
|
295 |
3504
|
296 ComplexRowVector retval (c, 0.0); |
|
297 if (r <= c || (r > c && i < c)) |
458
|
298 retval.elem (i) = elem (i, i); |
|
299 |
|
300 return retval; |
|
301 } |
|
302 |
|
303 ComplexRowVector |
|
304 ComplexDiagMatrix::row (char *s) const |
|
305 { |
533
|
306 if (! s) |
458
|
307 { |
|
308 (*current_liboctave_error_handler) ("invalid row selection"); |
|
309 return ComplexRowVector (); |
|
310 } |
|
311 |
|
312 char c = *s; |
|
313 if (c == 'f' || c == 'F') |
|
314 return row (0); |
|
315 else if (c == 'l' || c == 'L') |
|
316 return row (rows () - 1); |
|
317 else |
|
318 { |
|
319 (*current_liboctave_error_handler) ("invalid row selection"); |
|
320 return ComplexRowVector (); |
|
321 } |
|
322 } |
|
323 |
|
324 ComplexColumnVector |
|
325 ComplexDiagMatrix::column (int i) const |
|
326 { |
3504
|
327 int r = rows (); |
|
328 int c = cols (); |
|
329 if (i < 0 || i >= c) |
458
|
330 { |
|
331 (*current_liboctave_error_handler) ("invalid column selection"); |
3585
|
332 return ComplexColumnVector (); |
458
|
333 } |
|
334 |
3504
|
335 ComplexColumnVector retval (r, 0.0); |
|
336 if (r >= c || (r < c && i < r)) |
458
|
337 retval.elem (i) = elem (i, i); |
|
338 |
|
339 return retval; |
|
340 } |
|
341 |
|
342 ComplexColumnVector |
|
343 ComplexDiagMatrix::column (char *s) const |
|
344 { |
533
|
345 if (! s) |
458
|
346 { |
|
347 (*current_liboctave_error_handler) ("invalid column selection"); |
3585
|
348 return ComplexColumnVector (); |
458
|
349 } |
|
350 |
|
351 char c = *s; |
|
352 if (c == 'f' || c == 'F') |
|
353 return column (0); |
|
354 else if (c == 'l' || c == 'L') |
|
355 return column (cols () - 1); |
|
356 else |
|
357 { |
|
358 (*current_liboctave_error_handler) ("invalid column selection"); |
3585
|
359 return ComplexColumnVector (); |
458
|
360 } |
|
361 } |
|
362 |
|
363 ComplexDiagMatrix |
|
364 ComplexDiagMatrix::inverse (void) const |
|
365 { |
|
366 int info; |
|
367 return inverse (info); |
|
368 } |
|
369 |
|
370 ComplexDiagMatrix |
|
371 ComplexDiagMatrix::inverse (int& info) const |
|
372 { |
3504
|
373 int r = rows (); |
|
374 int c = cols (); |
|
375 if (r != c) |
458
|
376 { |
|
377 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
3585
|
378 return ComplexDiagMatrix (); |
458
|
379 } |
|
380 |
3504
|
381 ComplexDiagMatrix retval (r, c); |
458
|
382 |
|
383 info = 0; |
|
384 for (int i = 0; i < length (); i++) |
|
385 { |
|
386 if (elem (i, i) == 0.0) |
|
387 { |
|
388 info = -1; |
|
389 return *this; |
|
390 } |
|
391 else |
|
392 retval.elem (i, i) = 1.0 / elem (i, i); |
|
393 } |
|
394 |
1627
|
395 return retval; |
458
|
396 } |
|
397 |
|
398 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
399 |
|
400 ComplexDiagMatrix& |
|
401 ComplexDiagMatrix::operator += (const DiagMatrix& a) |
|
402 { |
3504
|
403 int r = rows (); |
|
404 int c = cols (); |
2386
|
405 |
|
406 int a_nr = a.rows (); |
|
407 int a_nc = a.cols (); |
|
408 |
3504
|
409 if (r != a_nr || c != a_nc) |
458
|
410 { |
3504
|
411 gripe_nonconformant ("operator +=", r, c, a_nr, a_nc); |
458
|
412 return *this; |
|
413 } |
|
414 |
3504
|
415 if (r == 0 || c == 0) |
458
|
416 return *this; |
|
417 |
|
418 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
419 |
3769
|
420 mx_inline_add2 (d, a.data (), length ()); |
458
|
421 return *this; |
|
422 } |
|
423 |
|
424 ComplexDiagMatrix |
|
425 operator * (const ComplexDiagMatrix& a, const DiagMatrix& b) |
|
426 { |
3504
|
427 int a_nr = a.rows (); |
|
428 int a_nc = a.cols (); |
2386
|
429 |
3504
|
430 int b_nr = b.rows (); |
|
431 int b_nc = b.cols (); |
2386
|
432 |
3504
|
433 if (a_nc != b_nr) |
458
|
434 { |
3504
|
435 gripe_nonconformant ("operator *", a_nr, a_nc, b_nr, b_nc); |
458
|
436 return ComplexDiagMatrix (); |
|
437 } |
|
438 |
3504
|
439 if (a_nr == 0 || a_nc == 0 || b_nc == 0) |
|
440 return ComplexDiagMatrix (a_nr, a_nc, 0.0); |
458
|
441 |
3504
|
442 ComplexDiagMatrix c (a_nr, b_nc); |
458
|
443 |
3504
|
444 int len = a_nr < b_nc ? a_nr : b_nc; |
458
|
445 |
|
446 for (int i = 0; i < len; i++) |
|
447 { |
|
448 Complex a_element = a.elem (i, i); |
|
449 double b_element = b.elem (i, i); |
|
450 |
|
451 if (a_element == 0.0 || b_element == 0.0) |
|
452 c.elem (i, i) = 0.0; |
|
453 else if (a_element == 1.0) |
|
454 c.elem (i, i) = b_element; |
|
455 else if (b_element == 1.0) |
|
456 c.elem (i, i) = a_element; |
|
457 else |
|
458 c.elem (i, i) = a_element * b_element; |
|
459 } |
|
460 |
|
461 return c; |
|
462 } |
|
463 |
|
464 ComplexDiagMatrix |
1205
|
465 operator * (const DiagMatrix& a, const ComplexDiagMatrix& b) |
|
466 { |
3504
|
467 int a_nr = a.rows (); |
|
468 int a_nc = a.cols (); |
2386
|
469 |
3504
|
470 int b_nr = b.rows (); |
|
471 int b_nc = b.cols (); |
2386
|
472 |
3504
|
473 if (a_nc != b_nr) |
1205
|
474 { |
3504
|
475 gripe_nonconformant ("operator *", a_nr, a_nc, b_nr, b_nc); |
1205
|
476 return ComplexDiagMatrix (); |
|
477 } |
|
478 |
3504
|
479 if (a_nr == 0 || a_nc == 0 || b_nc == 0) |
|
480 return ComplexDiagMatrix (a_nr, a_nc, 0.0); |
1205
|
481 |
3504
|
482 ComplexDiagMatrix c (a_nr, b_nc); |
1205
|
483 |
3504
|
484 int len = a_nr < b_nc ? a_nr : b_nc; |
1205
|
485 |
|
486 for (int i = 0; i < len; i++) |
|
487 { |
|
488 double a_element = a.elem (i, i); |
|
489 Complex b_element = b.elem (i, i); |
|
490 |
|
491 if (a_element == 0.0 || b_element == 0.0) |
|
492 c.elem (i, i) = 0.0; |
|
493 else if (a_element == 1.0) |
|
494 c.elem (i, i) = b_element; |
|
495 else if (b_element == 1.0) |
|
496 c.elem (i, i) = a_element; |
|
497 else |
|
498 c.elem (i, i) = a_element * b_element; |
|
499 } |
|
500 |
|
501 return c; |
|
502 } |
|
503 |
458
|
504 // other operations |
|
505 |
|
506 ComplexColumnVector |
|
507 ComplexDiagMatrix::diag (void) const |
|
508 { |
|
509 return diag (0); |
|
510 } |
|
511 |
|
512 // Could be optimized... |
|
513 |
|
514 ComplexColumnVector |
|
515 ComplexDiagMatrix::diag (int k) const |
|
516 { |
|
517 int nnr = rows (); |
|
518 int nnc = cols (); |
|
519 if (k > 0) |
|
520 nnc -= k; |
|
521 else if (k < 0) |
|
522 nnr += k; |
|
523 |
|
524 ComplexColumnVector d; |
|
525 |
|
526 if (nnr > 0 && nnc > 0) |
|
527 { |
|
528 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
529 |
|
530 d.resize (ndiag); |
|
531 |
|
532 if (k > 0) |
|
533 { |
|
534 for (int i = 0; i < ndiag; i++) |
|
535 d.elem (i) = elem (i, i+k); |
|
536 } |
|
537 else if ( k < 0) |
|
538 { |
|
539 for (int i = 0; i < ndiag; i++) |
|
540 d.elem (i) = elem (i-k, i); |
|
541 } |
|
542 else |
|
543 { |
|
544 for (int i = 0; i < ndiag; i++) |
|
545 d.elem (i) = elem (i, i); |
|
546 } |
|
547 } |
|
548 else |
4513
|
549 (*current_liboctave_error_handler) |
|
550 ("diag: requested diagonal out of range"); |
458
|
551 |
|
552 return d; |
|
553 } |
|
554 |
|
555 // i/o |
|
556 |
3504
|
557 std::ostream& |
|
558 operator << (std::ostream& os, const ComplexDiagMatrix& a) |
458
|
559 { |
|
560 Complex ZERO (0.0); |
|
561 // int field_width = os.precision () + 7; |
|
562 for (int i = 0; i < a.rows (); i++) |
|
563 { |
|
564 for (int j = 0; j < a.cols (); j++) |
|
565 { |
|
566 if (i == j) |
|
567 os << " " /* setw (field_width) */ << a.elem (i, i); |
|
568 else |
|
569 os << " " /* setw (field_width) */ << ZERO; |
|
570 } |
|
571 os << "\n"; |
|
572 } |
|
573 return os; |
|
574 } |
|
575 |
|
576 /* |
|
577 ;;; Local Variables: *** |
|
578 ;;; mode: C++ *** |
|
579 ;;; End: *** |
|
580 */ |