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 |
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 |
3769
|
54 return mx_inline_equal (data (), a.data (), length ()); |
458
|
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 { |
3769
|
240 return ComplexDiagMatrix (mx_inline_conj_dup (data (), length ()), |
|
241 cols (), rows ()); |
1205
|
242 } |
|
243 |
|
244 ComplexDiagMatrix |
458
|
245 ComplexDiagMatrix::transpose (void) const |
|
246 { |
3769
|
247 return ComplexDiagMatrix (mx_inline_dup (data (), length ()), |
|
248 cols (), rows ()); |
458
|
249 } |
|
250 |
|
251 ComplexDiagMatrix |
|
252 conj (const ComplexDiagMatrix& a) |
|
253 { |
|
254 ComplexDiagMatrix retval; |
|
255 int a_len = a.length (); |
|
256 if (a_len > 0) |
3769
|
257 retval = ComplexDiagMatrix (mx_inline_conj_dup (a.data (), a_len), |
458
|
258 a.rows (), a.cols ()); |
|
259 return retval; |
|
260 } |
|
261 |
|
262 // resize is the destructive analog for this one |
|
263 |
|
264 ComplexMatrix |
|
265 ComplexDiagMatrix::extract (int r1, int c1, int r2, int c2) const |
|
266 { |
|
267 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
268 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
269 |
|
270 int new_r = r2 - r1 + 1; |
|
271 int new_c = c2 - c1 + 1; |
|
272 |
|
273 ComplexMatrix result (new_r, new_c); |
|
274 |
|
275 for (int j = 0; j < new_c; j++) |
|
276 for (int i = 0; i < new_r; i++) |
|
277 result.elem (i, j) = elem (r1+i, c1+j); |
|
278 |
|
279 return result; |
|
280 } |
|
281 |
|
282 // extract row or column i. |
|
283 |
|
284 ComplexRowVector |
|
285 ComplexDiagMatrix::row (int i) const |
|
286 { |
3504
|
287 int r = rows (); |
|
288 int c = cols (); |
|
289 if (i < 0 || i >= r) |
458
|
290 { |
|
291 (*current_liboctave_error_handler) ("invalid row selection"); |
3585
|
292 return ComplexRowVector (); |
458
|
293 } |
|
294 |
3504
|
295 ComplexRowVector retval (c, 0.0); |
|
296 if (r <= c || (r > c && i < c)) |
458
|
297 retval.elem (i) = elem (i, i); |
|
298 |
|
299 return retval; |
|
300 } |
|
301 |
|
302 ComplexRowVector |
|
303 ComplexDiagMatrix::row (char *s) const |
|
304 { |
533
|
305 if (! s) |
458
|
306 { |
|
307 (*current_liboctave_error_handler) ("invalid row selection"); |
|
308 return ComplexRowVector (); |
|
309 } |
|
310 |
|
311 char c = *s; |
|
312 if (c == 'f' || c == 'F') |
|
313 return row (0); |
|
314 else if (c == 'l' || c == 'L') |
|
315 return row (rows () - 1); |
|
316 else |
|
317 { |
|
318 (*current_liboctave_error_handler) ("invalid row selection"); |
|
319 return ComplexRowVector (); |
|
320 } |
|
321 } |
|
322 |
|
323 ComplexColumnVector |
|
324 ComplexDiagMatrix::column (int i) const |
|
325 { |
3504
|
326 int r = rows (); |
|
327 int c = cols (); |
|
328 if (i < 0 || i >= c) |
458
|
329 { |
|
330 (*current_liboctave_error_handler) ("invalid column selection"); |
3585
|
331 return ComplexColumnVector (); |
458
|
332 } |
|
333 |
3504
|
334 ComplexColumnVector retval (r, 0.0); |
|
335 if (r >= c || (r < c && i < r)) |
458
|
336 retval.elem (i) = elem (i, i); |
|
337 |
|
338 return retval; |
|
339 } |
|
340 |
|
341 ComplexColumnVector |
|
342 ComplexDiagMatrix::column (char *s) const |
|
343 { |
533
|
344 if (! s) |
458
|
345 { |
|
346 (*current_liboctave_error_handler) ("invalid column selection"); |
3585
|
347 return ComplexColumnVector (); |
458
|
348 } |
|
349 |
|
350 char c = *s; |
|
351 if (c == 'f' || c == 'F') |
|
352 return column (0); |
|
353 else if (c == 'l' || c == 'L') |
|
354 return column (cols () - 1); |
|
355 else |
|
356 { |
|
357 (*current_liboctave_error_handler) ("invalid column selection"); |
3585
|
358 return ComplexColumnVector (); |
458
|
359 } |
|
360 } |
|
361 |
|
362 ComplexDiagMatrix |
|
363 ComplexDiagMatrix::inverse (void) const |
|
364 { |
|
365 int info; |
|
366 return inverse (info); |
|
367 } |
|
368 |
|
369 ComplexDiagMatrix |
|
370 ComplexDiagMatrix::inverse (int& info) const |
|
371 { |
3504
|
372 int r = rows (); |
|
373 int c = cols (); |
|
374 if (r != c) |
458
|
375 { |
|
376 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
3585
|
377 return ComplexDiagMatrix (); |
458
|
378 } |
|
379 |
3504
|
380 ComplexDiagMatrix retval (r, c); |
458
|
381 |
|
382 info = 0; |
|
383 for (int i = 0; i < length (); i++) |
|
384 { |
|
385 if (elem (i, i) == 0.0) |
|
386 { |
|
387 info = -1; |
|
388 return *this; |
|
389 } |
|
390 else |
|
391 retval.elem (i, i) = 1.0 / elem (i, i); |
|
392 } |
|
393 |
1627
|
394 return retval; |
458
|
395 } |
|
396 |
|
397 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
398 |
|
399 ComplexDiagMatrix& |
|
400 ComplexDiagMatrix::operator += (const DiagMatrix& a) |
|
401 { |
3504
|
402 int r = rows (); |
|
403 int c = cols (); |
2386
|
404 |
|
405 int a_nr = a.rows (); |
|
406 int a_nc = a.cols (); |
|
407 |
3504
|
408 if (r != a_nr || c != a_nc) |
458
|
409 { |
3504
|
410 gripe_nonconformant ("operator +=", r, c, a_nr, a_nc); |
458
|
411 return *this; |
|
412 } |
|
413 |
3504
|
414 if (r == 0 || c == 0) |
458
|
415 return *this; |
|
416 |
|
417 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
418 |
3769
|
419 mx_inline_add2 (d, a.data (), length ()); |
458
|
420 return *this; |
|
421 } |
|
422 |
|
423 ComplexDiagMatrix |
|
424 operator * (const ComplexDiagMatrix& a, const DiagMatrix& b) |
|
425 { |
3504
|
426 int a_nr = a.rows (); |
|
427 int a_nc = a.cols (); |
2386
|
428 |
3504
|
429 int b_nr = b.rows (); |
|
430 int b_nc = b.cols (); |
2386
|
431 |
3504
|
432 if (a_nc != b_nr) |
458
|
433 { |
3504
|
434 gripe_nonconformant ("operator *", a_nr, a_nc, b_nr, b_nc); |
458
|
435 return ComplexDiagMatrix (); |
|
436 } |
|
437 |
3504
|
438 if (a_nr == 0 || a_nc == 0 || b_nc == 0) |
|
439 return ComplexDiagMatrix (a_nr, a_nc, 0.0); |
458
|
440 |
3504
|
441 ComplexDiagMatrix c (a_nr, b_nc); |
458
|
442 |
3504
|
443 int len = a_nr < b_nc ? a_nr : b_nc; |
458
|
444 |
|
445 for (int i = 0; i < len; i++) |
|
446 { |
|
447 Complex a_element = a.elem (i, i); |
|
448 double b_element = b.elem (i, i); |
|
449 |
|
450 if (a_element == 0.0 || b_element == 0.0) |
|
451 c.elem (i, i) = 0.0; |
|
452 else if (a_element == 1.0) |
|
453 c.elem (i, i) = b_element; |
|
454 else if (b_element == 1.0) |
|
455 c.elem (i, i) = a_element; |
|
456 else |
|
457 c.elem (i, i) = a_element * b_element; |
|
458 } |
|
459 |
|
460 return c; |
|
461 } |
|
462 |
|
463 ComplexDiagMatrix |
1205
|
464 operator * (const DiagMatrix& a, const ComplexDiagMatrix& b) |
|
465 { |
3504
|
466 int a_nr = a.rows (); |
|
467 int a_nc = a.cols (); |
2386
|
468 |
3504
|
469 int b_nr = b.rows (); |
|
470 int b_nc = b.cols (); |
2386
|
471 |
3504
|
472 if (a_nc != b_nr) |
1205
|
473 { |
3504
|
474 gripe_nonconformant ("operator *", a_nr, a_nc, b_nr, b_nc); |
1205
|
475 return ComplexDiagMatrix (); |
|
476 } |
|
477 |
3504
|
478 if (a_nr == 0 || a_nc == 0 || b_nc == 0) |
|
479 return ComplexDiagMatrix (a_nr, a_nc, 0.0); |
1205
|
480 |
3504
|
481 ComplexDiagMatrix c (a_nr, b_nc); |
1205
|
482 |
3504
|
483 int len = a_nr < b_nc ? a_nr : b_nc; |
1205
|
484 |
|
485 for (int i = 0; i < len; i++) |
|
486 { |
|
487 double a_element = a.elem (i, i); |
|
488 Complex b_element = b.elem (i, i); |
|
489 |
|
490 if (a_element == 0.0 || b_element == 0.0) |
|
491 c.elem (i, i) = 0.0; |
|
492 else if (a_element == 1.0) |
|
493 c.elem (i, i) = b_element; |
|
494 else if (b_element == 1.0) |
|
495 c.elem (i, i) = a_element; |
|
496 else |
|
497 c.elem (i, i) = a_element * b_element; |
|
498 } |
|
499 |
|
500 return c; |
|
501 } |
|
502 |
458
|
503 // other operations |
|
504 |
|
505 ComplexColumnVector |
|
506 ComplexDiagMatrix::diag (void) const |
|
507 { |
|
508 return diag (0); |
|
509 } |
|
510 |
|
511 // Could be optimized... |
|
512 |
|
513 ComplexColumnVector |
|
514 ComplexDiagMatrix::diag (int k) const |
|
515 { |
|
516 int nnr = rows (); |
|
517 int nnc = cols (); |
|
518 if (k > 0) |
|
519 nnc -= k; |
|
520 else if (k < 0) |
|
521 nnr += k; |
|
522 |
|
523 ComplexColumnVector d; |
|
524 |
|
525 if (nnr > 0 && nnc > 0) |
|
526 { |
|
527 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
528 |
|
529 d.resize (ndiag); |
|
530 |
|
531 if (k > 0) |
|
532 { |
|
533 for (int i = 0; i < ndiag; i++) |
|
534 d.elem (i) = elem (i, i+k); |
|
535 } |
|
536 else if ( k < 0) |
|
537 { |
|
538 for (int i = 0; i < ndiag; i++) |
|
539 d.elem (i) = elem (i-k, i); |
|
540 } |
|
541 else |
|
542 { |
|
543 for (int i = 0; i < ndiag; i++) |
|
544 d.elem (i) = elem (i, i); |
|
545 } |
|
546 } |
|
547 else |
3504
|
548 std::cerr << "diag: requested diagonal out of range\n"; |
458
|
549 |
|
550 return d; |
|
551 } |
|
552 |
|
553 // i/o |
|
554 |
3504
|
555 std::ostream& |
|
556 operator << (std::ostream& os, const ComplexDiagMatrix& a) |
458
|
557 { |
|
558 Complex ZERO (0.0); |
|
559 // int field_width = os.precision () + 7; |
|
560 for (int i = 0; i < a.rows (); i++) |
|
561 { |
|
562 for (int j = 0; j < a.cols (); j++) |
|
563 { |
|
564 if (i == j) |
|
565 os << " " /* setw (field_width) */ << a.elem (i, i); |
|
566 else |
|
567 os << " " /* setw (field_width) */ << ZERO; |
|
568 } |
|
569 os << "\n"; |
|
570 } |
|
571 return os; |
|
572 } |
|
573 |
|
574 /* |
|
575 ;;; Local Variables: *** |
|
576 ;;; mode: C++ *** |
|
577 ;;; End: *** |
|
578 */ |