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 |
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 |
3503
|
32 #include <iostream> |
458
|
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 |
2386
|
48 bool |
458
|
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 |
2386
|
57 bool |
458
|
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 { |
3504
|
285 int r = rows (); |
|
286 int c = cols (); |
|
287 if (i < 0 || i >= r) |
458
|
288 { |
|
289 (*current_liboctave_error_handler) ("invalid row selection"); |
3585
|
290 return ComplexRowVector (); |
458
|
291 } |
|
292 |
3504
|
293 ComplexRowVector retval (c, 0.0); |
|
294 if (r <= c || (r > c && i < c)) |
458
|
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 { |
3504
|
324 int r = rows (); |
|
325 int c = cols (); |
|
326 if (i < 0 || i >= c) |
458
|
327 { |
|
328 (*current_liboctave_error_handler) ("invalid column selection"); |
3585
|
329 return ComplexColumnVector (); |
458
|
330 } |
|
331 |
3504
|
332 ComplexColumnVector retval (r, 0.0); |
|
333 if (r >= c || (r < c && i < r)) |
458
|
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"); |
3585
|
345 return ComplexColumnVector (); |
458
|
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"); |
3585
|
356 return ComplexColumnVector (); |
458
|
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 { |
3504
|
370 int r = rows (); |
|
371 int c = cols (); |
|
372 if (r != c) |
458
|
373 { |
|
374 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
3585
|
375 return ComplexDiagMatrix (); |
458
|
376 } |
|
377 |
3504
|
378 ComplexDiagMatrix retval (r, c); |
458
|
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 { |
3504
|
400 int r = rows (); |
|
401 int c = cols (); |
2386
|
402 |
|
403 int a_nr = a.rows (); |
|
404 int a_nc = a.cols (); |
|
405 |
3504
|
406 if (r != a_nr || c != a_nc) |
458
|
407 { |
3504
|
408 gripe_nonconformant ("operator +=", r, c, a_nr, a_nc); |
458
|
409 return *this; |
|
410 } |
|
411 |
3504
|
412 if (r == 0 || c == 0) |
458
|
413 return *this; |
|
414 |
|
415 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
416 |
|
417 add2 (d, a.data (), length ()); |
|
418 return *this; |
|
419 } |
|
420 |
|
421 ComplexDiagMatrix |
|
422 operator * (const ComplexDiagMatrix& a, const DiagMatrix& b) |
|
423 { |
3504
|
424 int a_nr = a.rows (); |
|
425 int a_nc = a.cols (); |
2386
|
426 |
3504
|
427 int b_nr = b.rows (); |
|
428 int b_nc = b.cols (); |
2386
|
429 |
3504
|
430 if (a_nc != b_nr) |
458
|
431 { |
3504
|
432 gripe_nonconformant ("operator *", a_nr, a_nc, b_nr, b_nc); |
458
|
433 return ComplexDiagMatrix (); |
|
434 } |
|
435 |
3504
|
436 if (a_nr == 0 || a_nc == 0 || b_nc == 0) |
|
437 return ComplexDiagMatrix (a_nr, a_nc, 0.0); |
458
|
438 |
3504
|
439 ComplexDiagMatrix c (a_nr, b_nc); |
458
|
440 |
3504
|
441 int len = a_nr < b_nc ? a_nr : b_nc; |
458
|
442 |
|
443 for (int i = 0; i < len; i++) |
|
444 { |
|
445 Complex a_element = a.elem (i, i); |
|
446 double b_element = b.elem (i, i); |
|
447 |
|
448 if (a_element == 0.0 || b_element == 0.0) |
|
449 c.elem (i, i) = 0.0; |
|
450 else if (a_element == 1.0) |
|
451 c.elem (i, i) = b_element; |
|
452 else if (b_element == 1.0) |
|
453 c.elem (i, i) = a_element; |
|
454 else |
|
455 c.elem (i, i) = a_element * b_element; |
|
456 } |
|
457 |
|
458 return c; |
|
459 } |
|
460 |
|
461 ComplexDiagMatrix |
1205
|
462 operator * (const DiagMatrix& a, const ComplexDiagMatrix& b) |
|
463 { |
3504
|
464 int a_nr = a.rows (); |
|
465 int a_nc = a.cols (); |
2386
|
466 |
3504
|
467 int b_nr = b.rows (); |
|
468 int b_nc = b.cols (); |
2386
|
469 |
3504
|
470 if (a_nc != b_nr) |
1205
|
471 { |
3504
|
472 gripe_nonconformant ("operator *", a_nr, a_nc, b_nr, b_nc); |
1205
|
473 return ComplexDiagMatrix (); |
|
474 } |
|
475 |
3504
|
476 if (a_nr == 0 || a_nc == 0 || b_nc == 0) |
|
477 return ComplexDiagMatrix (a_nr, a_nc, 0.0); |
1205
|
478 |
3504
|
479 ComplexDiagMatrix c (a_nr, b_nc); |
1205
|
480 |
3504
|
481 int len = a_nr < b_nc ? a_nr : b_nc; |
1205
|
482 |
|
483 for (int i = 0; i < len; i++) |
|
484 { |
|
485 double a_element = a.elem (i, i); |
|
486 Complex b_element = b.elem (i, i); |
|
487 |
|
488 if (a_element == 0.0 || b_element == 0.0) |
|
489 c.elem (i, i) = 0.0; |
|
490 else if (a_element == 1.0) |
|
491 c.elem (i, i) = b_element; |
|
492 else if (b_element == 1.0) |
|
493 c.elem (i, i) = a_element; |
|
494 else |
|
495 c.elem (i, i) = a_element * b_element; |
|
496 } |
|
497 |
|
498 return c; |
|
499 } |
|
500 |
458
|
501 // other operations |
|
502 |
|
503 ComplexColumnVector |
|
504 ComplexDiagMatrix::diag (void) const |
|
505 { |
|
506 return diag (0); |
|
507 } |
|
508 |
|
509 // Could be optimized... |
|
510 |
|
511 ComplexColumnVector |
|
512 ComplexDiagMatrix::diag (int k) const |
|
513 { |
|
514 int nnr = rows (); |
|
515 int nnc = cols (); |
|
516 if (k > 0) |
|
517 nnc -= k; |
|
518 else if (k < 0) |
|
519 nnr += k; |
|
520 |
|
521 ComplexColumnVector d; |
|
522 |
|
523 if (nnr > 0 && nnc > 0) |
|
524 { |
|
525 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
526 |
|
527 d.resize (ndiag); |
|
528 |
|
529 if (k > 0) |
|
530 { |
|
531 for (int i = 0; i < ndiag; i++) |
|
532 d.elem (i) = elem (i, i+k); |
|
533 } |
|
534 else if ( k < 0) |
|
535 { |
|
536 for (int i = 0; i < ndiag; i++) |
|
537 d.elem (i) = elem (i-k, i); |
|
538 } |
|
539 else |
|
540 { |
|
541 for (int i = 0; i < ndiag; i++) |
|
542 d.elem (i) = elem (i, i); |
|
543 } |
|
544 } |
|
545 else |
3504
|
546 std::cerr << "diag: requested diagonal out of range\n"; |
458
|
547 |
|
548 return d; |
|
549 } |
|
550 |
|
551 // i/o |
|
552 |
3504
|
553 std::ostream& |
|
554 operator << (std::ostream& os, const ComplexDiagMatrix& a) |
458
|
555 { |
|
556 Complex ZERO (0.0); |
|
557 // int field_width = os.precision () + 7; |
|
558 for (int i = 0; i < a.rows (); i++) |
|
559 { |
|
560 for (int j = 0; j < a.cols (); j++) |
|
561 { |
|
562 if (i == j) |
|
563 os << " " /* setw (field_width) */ << a.elem (i, i); |
|
564 else |
|
565 os << " " /* setw (field_width) */ << ZERO; |
|
566 } |
|
567 os << "\n"; |
|
568 } |
|
569 return os; |
|
570 } |
|
571 |
|
572 /* |
|
573 ;;; Local Variables: *** |
|
574 ;;; mode: C++ *** |
|
575 ;;; End: *** |
|
576 */ |