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