3
|
1 // DiagMatrix manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
238
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #include <iostream.h> |
3
|
29 |
|
30 #include "Matrix.h" |
|
31 #include "mx-inlines.cc" |
227
|
32 #include "lo-error.h" |
3
|
33 |
|
34 /* |
|
35 * Diagonal Matrix class. |
|
36 */ |
|
37 |
238
|
38 #if 0 |
3
|
39 DiagMatrix& |
|
40 DiagMatrix::resize (int r, int c) |
|
41 { |
|
42 if (r < 0 || c < 0) |
227
|
43 { |
|
44 (*current_liboctave_error_handler) |
|
45 ("can't resize to negative dimensions"); |
|
46 return *this; |
|
47 } |
3
|
48 |
|
49 int new_len = r < c ? r : c; |
|
50 double *new_data = (double *) NULL; |
|
51 if (new_len > 0) |
|
52 { |
|
53 new_data = new double [new_len]; |
|
54 |
|
55 int min_len = new_len < len ? new_len : len; |
|
56 |
|
57 for (int i = 0; i < min_len; i++) |
|
58 new_data[i] = data[i]; |
|
59 } |
|
60 |
|
61 delete [] data; |
|
62 nr = r; |
|
63 nc = c; |
|
64 len = new_len; |
|
65 data = new_data; |
|
66 |
|
67 return *this; |
|
68 } |
|
69 |
|
70 DiagMatrix& |
|
71 DiagMatrix::resize (int r, int c, double val) |
|
72 { |
|
73 if (r < 0 || c < 0) |
227
|
74 { |
|
75 (*current_liboctave_error_handler) |
|
76 ("can't resize to negative dimensions"); |
|
77 return *this; |
|
78 } |
3
|
79 |
|
80 int new_len = r < c ? r : c; |
|
81 double *new_data = (double *) NULL; |
|
82 if (new_len > 0) |
|
83 { |
|
84 new_data = new double [new_len]; |
|
85 |
|
86 int min_len = new_len < len ? new_len : len; |
|
87 |
|
88 for (int i = 0; i < min_len; i++) |
|
89 new_data[i] = data[i]; |
|
90 |
|
91 for (i = min_len; i < new_len; i++) |
|
92 new_data[i] = val; |
|
93 } |
|
94 |
|
95 delete [] data; |
|
96 nr = r; |
|
97 nc = c; |
|
98 len = new_len; |
|
99 data = new_data; |
|
100 |
|
101 return *this; |
|
102 } |
238
|
103 #endif |
3
|
104 |
|
105 int |
|
106 DiagMatrix::operator == (const DiagMatrix& a) const |
|
107 { |
238
|
108 if (rows () != a.rows () || cols () != a.cols ()) |
3
|
109 return 0; |
|
110 |
238
|
111 return equal (data (), a.data (), length ()); |
3
|
112 } |
|
113 |
|
114 int |
|
115 DiagMatrix::operator != (const DiagMatrix& a) const |
|
116 { |
238
|
117 return !(*this == a); |
3
|
118 } |
|
119 |
|
120 DiagMatrix& |
|
121 DiagMatrix::fill (double val) |
|
122 { |
238
|
123 for (int i = 0; i < length (); i++) |
|
124 elem (i, i) = val; |
3
|
125 return *this; |
|
126 } |
|
127 |
|
128 DiagMatrix& |
|
129 DiagMatrix::fill (double val, int beg, int end) |
|
130 { |
238
|
131 if (beg < 0 || end >= length () || end < beg) |
227
|
132 { |
|
133 (*current_liboctave_error_handler) ("range error for fill"); |
|
134 return *this; |
|
135 } |
3
|
136 |
238
|
137 for (int i = beg; i < end; i++) |
|
138 elem (i, i) = val; |
|
139 |
3
|
140 return *this; |
|
141 } |
|
142 |
|
143 DiagMatrix& |
|
144 DiagMatrix::fill (const ColumnVector& a) |
|
145 { |
238
|
146 int len = length (); |
|
147 if (a.length () != len) |
227
|
148 { |
|
149 (*current_liboctave_error_handler) ("range error for fill"); |
|
150 return *this; |
|
151 } |
3
|
152 |
238
|
153 for (int i = 0; i < len; i++) |
|
154 elem (i, i) = a.elem (i); |
|
155 |
3
|
156 return *this; |
|
157 } |
|
158 |
|
159 DiagMatrix& |
|
160 DiagMatrix::fill (const RowVector& a) |
|
161 { |
238
|
162 int len = length (); |
|
163 if (a.length () != len) |
227
|
164 { |
|
165 (*current_liboctave_error_handler) ("range error for fill"); |
|
166 return *this; |
|
167 } |
3
|
168 |
238
|
169 for (int i = 0; i < len; i++) |
|
170 elem (i, i) = a.elem (i); |
|
171 |
3
|
172 return *this; |
|
173 } |
|
174 |
|
175 DiagMatrix& |
|
176 DiagMatrix::fill (const ColumnVector& a, int beg) |
|
177 { |
238
|
178 int a_len = a.length (); |
|
179 if (beg < 0 || beg + a_len >= length ()) |
227
|
180 { |
|
181 (*current_liboctave_error_handler) ("range error for fill"); |
|
182 return *this; |
|
183 } |
3
|
184 |
238
|
185 for (int i = 0; i < a_len; i++) |
|
186 elem (i+beg, i+beg) = a.elem (i); |
|
187 |
3
|
188 return *this; |
|
189 } |
|
190 |
|
191 DiagMatrix& |
|
192 DiagMatrix::fill (const RowVector& a, int beg) |
|
193 { |
238
|
194 int a_len = a.length (); |
|
195 if (beg < 0 || beg + a_len >= length ()) |
227
|
196 { |
|
197 (*current_liboctave_error_handler) ("range error for fill"); |
|
198 return *this; |
|
199 } |
3
|
200 |
238
|
201 for (int i = 0; i < a_len; i++) |
|
202 elem (i+beg, i+beg) = a.elem (i); |
|
203 |
3
|
204 return *this; |
|
205 } |
|
206 |
|
207 DiagMatrix |
|
208 DiagMatrix::transpose (void) const |
|
209 { |
238
|
210 return DiagMatrix (dup (data (), length ()), cols (), rows ()); |
3
|
211 } |
|
212 |
|
213 Matrix |
|
214 DiagMatrix::extract (int r1, int c1, int r2, int c2) const |
|
215 { |
|
216 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
217 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
218 |
|
219 int new_r = r2 - r1 + 1; |
|
220 int new_c = c2 - c1 + 1; |
|
221 |
|
222 Matrix result (new_r, new_c); |
|
223 |
|
224 for (int j = 0; j < new_c; j++) |
|
225 for (int i = 0; i < new_r; i++) |
238
|
226 result.elem (i, j) = elem (r1+i, c1+j); |
3
|
227 |
|
228 return result; |
|
229 } |
|
230 |
|
231 // extract row or column i. |
|
232 |
|
233 RowVector |
|
234 DiagMatrix::row (int i) const |
|
235 { |
238
|
236 int nr = rows (); |
|
237 int nc = cols (); |
3
|
238 if (i < 0 || i >= nr) |
227
|
239 { |
|
240 (*current_liboctave_error_handler) ("invalid row selection"); |
|
241 return RowVector (); |
|
242 } |
3
|
243 |
|
244 RowVector retval (nc, 0.0); |
192
|
245 if (nr <= nc || (nr > nc && i < nc)) |
238
|
246 retval.elem (i) = elem (i, i); |
3
|
247 |
|
248 return retval; |
|
249 } |
|
250 |
|
251 RowVector |
|
252 DiagMatrix::row (char *s) const |
|
253 { |
|
254 if (s == (char *) NULL) |
227
|
255 { |
|
256 (*current_liboctave_error_handler) ("invalid row selection"); |
|
257 return RowVector (); |
|
258 } |
3
|
259 |
|
260 char c = *s; |
|
261 if (c == 'f' || c == 'F') |
|
262 return row (0); |
|
263 else if (c == 'l' || c == 'L') |
238
|
264 return row (rows () - 1); |
3
|
265 else |
227
|
266 { |
|
267 (*current_liboctave_error_handler) ("invalid row selection"); |
|
268 return RowVector (); |
|
269 } |
3
|
270 } |
|
271 |
|
272 ColumnVector |
|
273 DiagMatrix::column (int i) const |
|
274 { |
238
|
275 int nr = rows (); |
|
276 int nc = cols (); |
3
|
277 if (i < 0 || i >= nc) |
227
|
278 { |
|
279 (*current_liboctave_error_handler) ("invalid column selection"); |
|
280 return ColumnVector (); |
|
281 } |
3
|
282 |
|
283 ColumnVector retval (nr, 0.0); |
192
|
284 if (nr >= nc || (nr < nc && i < nr)) |
238
|
285 retval.elem (i) = elem (i, i); |
3
|
286 |
|
287 return retval; |
|
288 } |
|
289 |
|
290 ColumnVector |
|
291 DiagMatrix::column (char *s) const |
|
292 { |
|
293 if (s == (char *) NULL) |
227
|
294 { |
|
295 (*current_liboctave_error_handler) ("invalid column selection"); |
|
296 return ColumnVector (); |
|
297 } |
3
|
298 |
|
299 char c = *s; |
|
300 if (c == 'f' || c == 'F') |
|
301 return column (0); |
|
302 else if (c == 'l' || c == 'L') |
238
|
303 return column (cols () - 1); |
3
|
304 else |
227
|
305 { |
|
306 (*current_liboctave_error_handler) ("invalid column selection"); |
|
307 return ColumnVector (); |
|
308 } |
3
|
309 } |
|
310 |
|
311 DiagMatrix |
|
312 DiagMatrix::inverse (void) const |
|
313 { |
|
314 int info; |
|
315 return inverse (info); |
|
316 } |
|
317 |
238
|
318 DiagMatrix |
|
319 DiagMatrix::inverse (int &info) const |
|
320 { |
|
321 int nr = rows (); |
|
322 int nc = cols (); |
|
323 int len = length (); |
|
324 if (nr != nc) |
|
325 { |
|
326 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
327 return DiagMatrix (); |
|
328 } |
|
329 |
|
330 info = 0; |
|
331 double *tmp_data = dup (data (), len); |
|
332 for (int i = 0; i < len; i++) |
|
333 { |
|
334 if (elem (i, i) == 0.0) |
|
335 { |
|
336 info = -1; |
|
337 copy (tmp_data, data (), len); // Restore contents. |
|
338 break; |
|
339 } |
|
340 else |
|
341 { |
|
342 tmp_data[i] = 1.0 / elem (i, i); |
|
343 } |
|
344 } |
|
345 |
|
346 return DiagMatrix (tmp_data, nr, nc); |
|
347 } |
|
348 |
|
349 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
350 |
|
351 DiagMatrix& |
|
352 DiagMatrix::operator += (const DiagMatrix& a) |
|
353 { |
|
354 int nr = rows (); |
|
355 int nc = cols (); |
|
356 if (nr != a.rows () || nc != a.cols ()) |
|
357 { |
|
358 (*current_liboctave_error_handler) |
|
359 ("nonconformant matrix += operation attempted"); |
|
360 return *this; |
|
361 } |
|
362 |
|
363 if (nc == 0 || nr == 0) |
|
364 return *this; |
|
365 |
|
366 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
367 |
|
368 add2 (d, a.data (), length ()); |
|
369 return *this; |
|
370 } |
|
371 |
|
372 DiagMatrix& |
|
373 DiagMatrix::operator -= (const DiagMatrix& a) |
|
374 { |
|
375 int nr = rows (); |
|
376 int nc = cols (); |
|
377 if (nr != a.rows () || nc != a.cols ()) |
|
378 { |
|
379 (*current_liboctave_error_handler) |
|
380 ("nonconformant matrix -= operation attempted"); |
|
381 return *this; |
|
382 } |
|
383 |
|
384 if (nr == 0 || nc == 0) |
|
385 return *this; |
|
386 |
|
387 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
388 |
|
389 subtract2 (d, a.data (), length ()); |
|
390 return *this; |
|
391 } |
|
392 |
3
|
393 // diagonal matrix by scalar -> matrix operations |
|
394 |
|
395 Matrix |
238
|
396 operator + (const DiagMatrix& a, double s) |
3
|
397 { |
238
|
398 Matrix tmp (a.rows (), a.cols (), s); |
|
399 return a + tmp; |
3
|
400 } |
|
401 |
|
402 Matrix |
238
|
403 operator - (const DiagMatrix& a, double s) |
3
|
404 { |
238
|
405 Matrix tmp (a.rows (), a.cols (), -s); |
|
406 return a + tmp; |
3
|
407 } |
|
408 |
|
409 ComplexMatrix |
238
|
410 operator + (const DiagMatrix& a, const Complex& s) |
3
|
411 { |
238
|
412 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
413 return a + tmp; |
3
|
414 } |
|
415 |
|
416 ComplexMatrix |
238
|
417 operator - (const DiagMatrix& a, const Complex& s) |
3
|
418 { |
238
|
419 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
420 return a + tmp; |
3
|
421 } |
|
422 |
|
423 // diagonal matrix by scalar -> diagonal matrix operations |
|
424 |
238
|
425 ComplexDiagMatrix |
|
426 operator * (const DiagMatrix& a, const Complex& s) |
3
|
427 { |
238
|
428 return ComplexDiagMatrix (multiply (a.data (), a.length (), s), |
|
429 a.rows (), a.cols ()); |
3
|
430 } |
|
431 |
|
432 ComplexDiagMatrix |
238
|
433 operator / (const DiagMatrix& a, const Complex& s) |
3
|
434 { |
238
|
435 return ComplexDiagMatrix (divide (a.data (), a.length (), s), |
|
436 a.rows (), a.cols ()); |
3
|
437 } |
|
438 |
|
439 // scalar by diagonal matrix -> matrix operations |
|
440 |
|
441 Matrix |
|
442 operator + (double s, const DiagMatrix& a) |
|
443 { |
238
|
444 Matrix tmp (a.rows (), a.cols (), s); |
|
445 return tmp + a; |
3
|
446 } |
|
447 |
|
448 Matrix |
|
449 operator - (double s, const DiagMatrix& a) |
|
450 { |
238
|
451 Matrix tmp (a.rows (), a.cols (), s); |
|
452 return tmp - a; |
|
453 } |
|
454 |
|
455 ComplexMatrix |
|
456 operator + (const Complex& s, const DiagMatrix& a) |
|
457 { |
|
458 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
459 return tmp + a; |
|
460 } |
|
461 |
|
462 ComplexMatrix |
|
463 operator - (const Complex& s, const DiagMatrix& a) |
|
464 { |
|
465 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
466 return tmp - a; |
3
|
467 } |
|
468 |
|
469 // scalar by diagonal matrix -> diagonal matrix operations |
|
470 |
238
|
471 ComplexDiagMatrix |
|
472 operator * (const Complex& s, const DiagMatrix& a) |
3
|
473 { |
238
|
474 return ComplexDiagMatrix (multiply (a.data (), a.length (), s), |
|
475 a.rows (), a.cols ()); |
3
|
476 } |
|
477 |
|
478 // diagonal matrix by column vector -> column vector operations |
|
479 |
|
480 ColumnVector |
238
|
481 operator * (const DiagMatrix& m, const ColumnVector& a) |
3
|
482 { |
238
|
483 int nr = m.rows (); |
|
484 int nc = m.cols (); |
|
485 int a_len = a.length (); |
|
486 if (nc != a_len) |
227
|
487 { |
|
488 (*current_liboctave_error_handler) |
|
489 ("nonconformant matrix multiplication attempted"); |
|
490 return ColumnVector (); |
|
491 } |
3
|
492 |
|
493 if (nc == 0 || nr == 0) |
|
494 return ColumnVector (0); |
|
495 |
|
496 ColumnVector result (nr); |
|
497 |
238
|
498 for (int i = 0; i < a_len; i++) |
|
499 result.elem (i) = a.elem (i) * m.elem (i, i); |
3
|
500 |
238
|
501 for (i = a_len; i < nr; i++) |
|
502 result.elem (i) = 0.0; |
3
|
503 |
|
504 return result; |
|
505 } |
|
506 |
|
507 ComplexColumnVector |
238
|
508 operator * (const DiagMatrix& m, const ComplexColumnVector& a) |
3
|
509 { |
238
|
510 int nr = m.rows (); |
|
511 int nc = m.cols (); |
|
512 int a_len = a.length (); |
|
513 if (nc != a_len) |
227
|
514 { |
|
515 (*current_liboctave_error_handler) |
|
516 ("nonconformant matrix multiplication attempted"); |
|
517 return ColumnVector (); |
|
518 } |
3
|
519 |
|
520 if (nc == 0 || nr == 0) |
|
521 return ComplexColumnVector (0); |
|
522 |
|
523 ComplexColumnVector result (nr); |
|
524 |
238
|
525 for (int i = 0; i < a_len; i++) |
|
526 result.elem (i) = a.elem (i) * m.elem (i, i); |
3
|
527 |
238
|
528 for (i = a_len; i < nr; i++) |
|
529 result.elem (i) = 0.0; |
3
|
530 |
|
531 return result; |
|
532 } |
|
533 |
|
534 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
535 |
238
|
536 ComplexDiagMatrix |
|
537 operator + (const DiagMatrix& m, const ComplexDiagMatrix& a) |
3
|
538 { |
238
|
539 int nr = m.rows (); |
|
540 int nc = m.cols (); |
|
541 if (nr != a.rows () || nc != a.cols ()) |
227
|
542 { |
|
543 (*current_liboctave_error_handler) |
|
544 ("nonconformant matrix addition attempted"); |
|
545 return ComplexDiagMatrix (); |
|
546 } |
3
|
547 |
|
548 if (nc == 0 || nr == 0) |
|
549 return ComplexDiagMatrix (nr, nc); |
|
550 |
238
|
551 return ComplexDiagMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
3
|
552 } |
|
553 |
|
554 ComplexDiagMatrix |
238
|
555 operator - (const DiagMatrix& m, const ComplexDiagMatrix& a) |
3
|
556 { |
238
|
557 int nr = m.rows (); |
|
558 int nc = m.cols (); |
|
559 if (nr != a.rows () || nc != a.cols ()) |
227
|
560 { |
|
561 (*current_liboctave_error_handler) |
|
562 ("nonconformant matrix subtraction attempted"); |
|
563 return ComplexDiagMatrix (); |
|
564 } |
3
|
565 |
|
566 if (nc == 0 || nr == 0) |
|
567 return ComplexDiagMatrix (nr, nc); |
|
568 |
238
|
569 return ComplexDiagMatrix (subtract (m.data (), a.data (), m.length ()), |
|
570 nr, nc); |
3
|
571 } |
|
572 |
|
573 ComplexDiagMatrix |
238
|
574 product (const DiagMatrix& m, const ComplexDiagMatrix& a) |
3
|
575 { |
238
|
576 int nr = m.rows (); |
|
577 int nc = m.cols (); |
|
578 if (nr != a.rows () || nc != a.cols ()) |
227
|
579 { |
|
580 (*current_liboctave_error_handler) |
|
581 ("nonconformant matrix product attempted"); |
|
582 return ComplexDiagMatrix (); |
|
583 } |
3
|
584 |
|
585 if (nc == 0 || nr == 0) |
|
586 return ComplexDiagMatrix (nr, nc); |
|
587 |
238
|
588 return ComplexDiagMatrix (multiply (m.data (), a.data (), m.length ()), |
|
589 nr, nc); |
3
|
590 } |
|
591 |
|
592 // diagonal matrix by matrix -> matrix operations |
|
593 |
|
594 Matrix |
238
|
595 operator + (const DiagMatrix& m, const Matrix& a) |
3
|
596 { |
238
|
597 int nr = m.rows (); |
|
598 int nc = m.cols (); |
|
599 if (nr != a.rows () || nc != a.cols ()) |
227
|
600 { |
|
601 (*current_liboctave_error_handler) |
|
602 ("nonconformant matrix addition attempted"); |
|
603 return Matrix (); |
|
604 } |
3
|
605 |
|
606 if (nr == 0 || nc == 0) |
|
607 return Matrix (nr, nc); |
|
608 |
|
609 Matrix result (a); |
238
|
610 for (int i = 0; i < m.length (); i++) |
|
611 result.elem (i, i) += m.elem (i, i); |
3
|
612 |
|
613 return result; |
|
614 } |
|
615 |
|
616 Matrix |
238
|
617 operator - (const DiagMatrix& m, const Matrix& a) |
3
|
618 { |
238
|
619 int nr = m.rows (); |
|
620 int nc = m.cols (); |
|
621 if (nr != a.rows () || nc != a.cols ()) |
227
|
622 { |
|
623 (*current_liboctave_error_handler) |
|
624 ("nonconformant matrix subtraction attempted"); |
|
625 return Matrix (); |
|
626 } |
3
|
627 |
|
628 if (nr == 0 || nc == 0) |
|
629 return Matrix (nr, nc); |
|
630 |
|
631 Matrix result (-a); |
238
|
632 for (int i = 0; i < m.length (); i++) |
|
633 result.elem (i, i) += m.elem (i, i); |
3
|
634 |
|
635 return result; |
|
636 } |
|
637 |
|
638 Matrix |
238
|
639 operator * (const DiagMatrix& m, const Matrix& a) |
3
|
640 { |
238
|
641 int nr = m.rows (); |
|
642 int nc = m.cols (); |
|
643 int a_nr = a.rows (); |
|
644 int a_nc = a.cols (); |
|
645 if (nc != a_nr) |
227
|
646 { |
|
647 (*current_liboctave_error_handler) |
|
648 ("nonconformant matrix multiplication attempted"); |
|
649 return Matrix (); |
|
650 } |
3
|
651 |
238
|
652 if (nr == 0 || nc == 0 || a_nc == 0) |
|
653 return Matrix (nr, a_nc, 0.0); |
3
|
654 |
238
|
655 Matrix c (nr, a_nc); |
3
|
656 |
238
|
657 for (int i = 0; i < m.length (); i++) |
3
|
658 { |
238
|
659 if (m.elem (i, i) == 1.0) |
3
|
660 { |
238
|
661 for (int j = 0; j < a_nc; j++) |
3
|
662 c.elem (i, j) = a.elem (i, j); |
|
663 } |
238
|
664 else if (m.elem (i, i) == 0.0) |
3
|
665 { |
238
|
666 for (int j = 0; j < a_nc; j++) |
3
|
667 c.elem (i, j) = 0.0; |
|
668 } |
|
669 else |
|
670 { |
238
|
671 for (int j = 0; j < a_nc; j++) |
|
672 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
3
|
673 } |
|
674 } |
|
675 |
|
676 if (nr > nc) |
|
677 { |
238
|
678 for (int j = 0; j < a_nc; j++) |
|
679 for (int i = a_nr; i < nr; i++) |
3
|
680 c.elem (i, j) = 0.0; |
|
681 } |
|
682 |
|
683 return c; |
|
684 } |
|
685 |
|
686 ComplexMatrix |
238
|
687 operator + (const DiagMatrix& m, const ComplexMatrix& a) |
3
|
688 { |
238
|
689 int nr = m.rows (); |
|
690 int nc = m.cols (); |
|
691 if (nr != a.rows () || nc != a.cols ()) |
227
|
692 { |
|
693 (*current_liboctave_error_handler) |
|
694 ("nonconformant matrix addition attempted"); |
|
695 return ComplexMatrix (); |
|
696 } |
3
|
697 |
|
698 if (nr == 0 || nc == 0) |
|
699 return ComplexMatrix (nr, nc); |
|
700 |
|
701 ComplexMatrix result (a); |
238
|
702 for (int i = 0; i < m.length (); i++) |
|
703 result.elem (i, i) += m.elem (i, i); |
3
|
704 |
|
705 return result; |
|
706 } |
|
707 |
|
708 ComplexMatrix |
238
|
709 operator - (const DiagMatrix& m, const ComplexMatrix& a) |
3
|
710 { |
238
|
711 int nr = m.rows (); |
|
712 int nc = m.cols (); |
|
713 if (nr != a.rows () || nc != a.cols ()) |
227
|
714 { |
|
715 (*current_liboctave_error_handler) |
|
716 ("nonconformant matrix subtraction attempted"); |
|
717 return ComplexMatrix (); |
|
718 } |
3
|
719 |
|
720 if (nr == 0 || nc == 0) |
|
721 return ComplexMatrix (nr, nc); |
|
722 |
|
723 ComplexMatrix result (-a); |
238
|
724 for (int i = 0; i < m.length (); i++) |
|
725 result.elem (i, i) += m.elem (i, i); |
3
|
726 |
|
727 return result; |
|
728 } |
|
729 |
|
730 ComplexMatrix |
238
|
731 operator * (const DiagMatrix& m, const ComplexMatrix& a) |
3
|
732 { |
238
|
733 int nr = m.rows (); |
|
734 int nc = m.cols (); |
|
735 int a_nr = a.rows (); |
|
736 int a_nc = a.cols (); |
|
737 if (nc != a_nr) |
227
|
738 { |
|
739 (*current_liboctave_error_handler) |
|
740 ("nonconformant matrix multiplication attempted"); |
|
741 return ComplexMatrix (); |
|
742 } |
3
|
743 |
238
|
744 if (nr == 0 || nc == 0 || a_nc == 0) |
3
|
745 return ComplexMatrix (nr, nc, 0.0); |
|
746 |
238
|
747 ComplexMatrix c (nr, a_nc); |
3
|
748 |
238
|
749 for (int i = 0; i < m.length (); i++) |
3
|
750 { |
238
|
751 if (m.elem (i, i) == 1.0) |
3
|
752 { |
238
|
753 for (int j = 0; j < a_nc; j++) |
3
|
754 c.elem (i, j) = a.elem (i, j); |
|
755 } |
238
|
756 else if (m.elem (i, i) == 0.0) |
3
|
757 { |
238
|
758 for (int j = 0; j < a_nc; j++) |
3
|
759 c.elem (i, j) = 0.0; |
|
760 } |
|
761 else |
|
762 { |
238
|
763 for (int j = 0; j < a_nc; j++) |
|
764 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
3
|
765 } |
|
766 } |
|
767 |
|
768 if (nr > nc) |
|
769 { |
238
|
770 for (int j = 0; j < a_nc; j++) |
|
771 for (int i = a_nr; i < nr; i++) |
3
|
772 c.elem (i, j) = 0.0; |
|
773 } |
|
774 |
|
775 return c; |
|
776 } |
|
777 |
238
|
778 // other operations |
3
|
779 |
|
780 ColumnVector |
|
781 DiagMatrix::diag (void) const |
|
782 { |
|
783 return diag (0); |
|
784 } |
|
785 |
|
786 // Could be optimized... |
|
787 |
|
788 ColumnVector |
|
789 DiagMatrix::diag (int k) const |
|
790 { |
238
|
791 int nnr = rows (); |
|
792 int nnc = cols (); |
3
|
793 if (k > 0) |
|
794 nnc -= k; |
|
795 else if (k < 0) |
|
796 nnr += k; |
|
797 |
|
798 ColumnVector d; |
|
799 |
|
800 if (nnr > 0 && nnc > 0) |
|
801 { |
|
802 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
803 |
|
804 d.resize (ndiag); |
|
805 |
|
806 if (k > 0) |
|
807 { |
|
808 for (int i = 0; i < ndiag; i++) |
|
809 d.elem (i) = elem (i, i+k); |
|
810 } |
|
811 else if ( k < 0) |
|
812 { |
|
813 for (int i = 0; i < ndiag; i++) |
|
814 d.elem (i) = elem (i-k, i); |
|
815 } |
|
816 else |
|
817 { |
|
818 for (int i = 0; i < ndiag; i++) |
|
819 d.elem (i) = elem (i, i); |
|
820 } |
|
821 } |
|
822 else |
|
823 cerr << "diag: requested diagonal out of range\n"; |
|
824 |
|
825 return d; |
|
826 } |
|
827 |
|
828 ostream& |
|
829 operator << (ostream& os, const DiagMatrix& a) |
|
830 { |
|
831 // int field_width = os.precision () + 7; |
238
|
832 for (int i = 0; i < a.rows (); i++) |
3
|
833 { |
238
|
834 for (int j = 0; j < a.cols (); j++) |
3
|
835 { |
|
836 if (i == j) |
238
|
837 os << " " /* setw (field_width) */ << a.elem (i, i); |
3
|
838 else |
238
|
839 os << " " /* setw (field_width) */ << 0.0; |
3
|
840 } |
|
841 os << "\n"; |
|
842 } |
|
843 return os; |
|
844 } |
|
845 |
|
846 /* |
|
847 * Complex Diagonal Matrix class |
|
848 */ |
|
849 |
238
|
850 ComplexDiagMatrix::ComplexDiagMatrix (const RowVector& a) |
|
851 : DiagArray<Complex> (a.length ()) |
3
|
852 { |
238
|
853 for (int i = 0; i < length (); i++) |
|
854 elem (i, i) = a.elem (i); |
3
|
855 } |
|
856 |
|
857 ComplexDiagMatrix::ComplexDiagMatrix (const ColumnVector& a) |
238
|
858 : DiagArray<Complex> (a.length ()) |
3
|
859 { |
238
|
860 for (int i = 0; i < length (); i++) |
|
861 elem (i, i) = a.elem (i); |
3
|
862 } |
|
863 |
|
864 ComplexDiagMatrix::ComplexDiagMatrix (const DiagMatrix& a) |
238
|
865 : DiagArray<Complex> (a.rows (), a.cols ()) |
3
|
866 { |
238
|
867 for (int i = 0; i < length (); i++) |
|
868 elem (i, i) = a.elem (i, i); |
3
|
869 } |
|
870 |
238
|
871 #if 0 |
3
|
872 ComplexDiagMatrix& |
|
873 ComplexDiagMatrix::resize (int r, int c) |
|
874 { |
|
875 if (r < 0 || c < 0) |
227
|
876 { |
|
877 (*current_liboctave_error_handler) |
|
878 ("can't resize to negative dimensions"); |
|
879 return *this; |
|
880 } |
3
|
881 |
|
882 int new_len = r < c ? r : c; |
|
883 Complex *new_data = (Complex *) NULL; |
|
884 if (new_len > 0) |
|
885 { |
|
886 new_data = new Complex [new_len]; |
|
887 |
|
888 int min_len = new_len < len ? new_len : len; |
|
889 |
|
890 for (int i = 0; i < min_len; i++) |
|
891 new_data[i] = data[i]; |
|
892 } |
|
893 |
|
894 delete [] data; |
|
895 nr = r; |
|
896 nc = c; |
|
897 len = new_len; |
|
898 data = new_data; |
|
899 |
|
900 return *this; |
|
901 } |
|
902 |
|
903 ComplexDiagMatrix& |
|
904 ComplexDiagMatrix::resize (int r, int c, double val) |
|
905 { |
|
906 if (r < 0 || c < 0) |
227
|
907 { |
|
908 (*current_liboctave_error_handler) |
|
909 ("can't resize to negative dimensions"); |
|
910 return *this; |
|
911 } |
3
|
912 |
|
913 int new_len = r < c ? r : c; |
|
914 Complex *new_data = (Complex *) NULL; |
|
915 if (new_len > 0) |
|
916 { |
|
917 new_data = new Complex [new_len]; |
|
918 |
|
919 int min_len = new_len < len ? new_len : len; |
|
920 |
|
921 for (int i = 0; i < min_len; i++) |
|
922 new_data[i] = data[i]; |
|
923 |
|
924 for (i = min_len; i < new_len; i++) |
|
925 new_data[i] = val; |
|
926 } |
|
927 |
|
928 delete [] data; |
|
929 nr = r; |
|
930 nc = c; |
|
931 len = new_len; |
|
932 data = new_data; |
|
933 |
|
934 return *this; |
|
935 } |
|
936 |
|
937 ComplexDiagMatrix& |
161
|
938 ComplexDiagMatrix::resize (int r, int c, const Complex& val) |
3
|
939 { |
|
940 if (r < 0 || c < 0) |
227
|
941 { |
|
942 (*current_liboctave_error_handler) |
|
943 ("can't resize to negative dimensions"); |
|
944 return *this; |
|
945 } |
3
|
946 |
|
947 int new_len = r < c ? r : c; |
|
948 Complex *new_data = (Complex *) NULL; |
|
949 if (new_len > 0) |
|
950 { |
|
951 new_data = new Complex [new_len]; |
|
952 |
|
953 int min_len = new_len < len ? new_len : len; |
|
954 |
|
955 for (int i = 0; i < min_len; i++) |
|
956 new_data[i] = data[i]; |
|
957 |
|
958 for (i = min_len; i < new_len; i++) |
|
959 new_data[i] = val; |
|
960 } |
|
961 |
|
962 delete [] data; |
|
963 nr = r; |
|
964 nc = c; |
|
965 len = new_len; |
|
966 data = new_data; |
|
967 |
|
968 return *this; |
|
969 } |
238
|
970 #endif |
3
|
971 |
|
972 int |
|
973 ComplexDiagMatrix::operator == (const ComplexDiagMatrix& a) const |
|
974 { |
238
|
975 if (rows () != a.rows () || cols () != a.cols ()) |
3
|
976 return 0; |
|
977 |
238
|
978 return equal (data (), a.data (), length ()); |
3
|
979 } |
|
980 |
|
981 int |
|
982 ComplexDiagMatrix::operator != (const ComplexDiagMatrix& a) const |
|
983 { |
238
|
984 return !(*this == a); |
3
|
985 } |
|
986 |
|
987 ComplexDiagMatrix |
|
988 ComplexDiagMatrix::hermitian (void) const |
|
989 { |
238
|
990 return ComplexDiagMatrix (conj_dup (data (), length ()), cols (), rows ()); |
3
|
991 } |
|
992 |
|
993 ComplexDiagMatrix& |
|
994 ComplexDiagMatrix::fill (double val) |
|
995 { |
238
|
996 for (int i = 0; i < length (); i++) |
|
997 elem (i, i) = val; |
3
|
998 return *this; |
|
999 } |
|
1000 |
|
1001 ComplexDiagMatrix& |
161
|
1002 ComplexDiagMatrix::fill (const Complex& val) |
3
|
1003 { |
238
|
1004 for (int i = 0; i < length (); i++) |
|
1005 elem (i, i) = val; |
3
|
1006 return *this; |
|
1007 } |
|
1008 |
|
1009 ComplexDiagMatrix& |
|
1010 ComplexDiagMatrix::fill (double val, int beg, int end) |
|
1011 { |
238
|
1012 if (beg < 0 || end >= length () || end < beg) |
227
|
1013 { |
|
1014 (*current_liboctave_error_handler) ("range error for fill"); |
|
1015 return *this; |
|
1016 } |
3
|
1017 |
238
|
1018 for (int i = beg; i < end; i++) |
|
1019 elem (i, i) = val; |
|
1020 |
3
|
1021 return *this; |
|
1022 } |
|
1023 |
|
1024 ComplexDiagMatrix& |
161
|
1025 ComplexDiagMatrix::fill (const Complex& val, int beg, int end) |
3
|
1026 { |
238
|
1027 if (beg < 0 || end >= length () || end < beg) |
227
|
1028 { |
|
1029 (*current_liboctave_error_handler) ("range error for fill"); |
|
1030 return *this; |
|
1031 } |
3
|
1032 |
238
|
1033 for (int i = beg; i < end; i++) |
|
1034 elem (i, i) = val; |
|
1035 |
3
|
1036 return *this; |
|
1037 } |
|
1038 |
|
1039 ComplexDiagMatrix& |
|
1040 ComplexDiagMatrix::fill (const ColumnVector& a) |
|
1041 { |
238
|
1042 int len = length (); |
|
1043 if (a.length () != len) |
227
|
1044 { |
|
1045 (*current_liboctave_error_handler) ("range error for fill"); |
|
1046 return *this; |
|
1047 } |
3
|
1048 |
238
|
1049 for (int i = 0; i < len; i++) |
|
1050 elem (i, i) = a.elem (i); |
|
1051 |
3
|
1052 return *this; |
|
1053 } |
|
1054 |
|
1055 ComplexDiagMatrix& |
|
1056 ComplexDiagMatrix::fill (const ComplexColumnVector& a) |
|
1057 { |
238
|
1058 int len = length (); |
|
1059 if (a.length () != len) |
227
|
1060 { |
|
1061 (*current_liboctave_error_handler) ("range error for fill"); |
|
1062 return *this; |
|
1063 } |
3
|
1064 |
238
|
1065 for (int i = 0; i < len; i++) |
|
1066 elem (i, i) = a.elem (i); |
|
1067 |
3
|
1068 return *this; |
|
1069 } |
|
1070 |
|
1071 ComplexDiagMatrix& |
|
1072 ComplexDiagMatrix::fill (const RowVector& a) |
|
1073 { |
238
|
1074 int len = length (); |
|
1075 if (a.length () != len) |
227
|
1076 { |
|
1077 (*current_liboctave_error_handler) ("range error for fill"); |
|
1078 return *this; |
|
1079 } |
3
|
1080 |
238
|
1081 for (int i = 0; i < len; i++) |
|
1082 elem (i, i) = a.elem (i); |
|
1083 |
3
|
1084 return *this; |
|
1085 } |
|
1086 |
|
1087 ComplexDiagMatrix& |
|
1088 ComplexDiagMatrix::fill (const ComplexRowVector& a) |
|
1089 { |
238
|
1090 int len = length (); |
|
1091 if (a.length () != len) |
227
|
1092 { |
|
1093 (*current_liboctave_error_handler) ("range error for fill"); |
|
1094 return *this; |
|
1095 } |
3
|
1096 |
238
|
1097 for (int i = 0; i < len; i++) |
|
1098 elem (i, i) = a.elem (i); |
|
1099 |
3
|
1100 return *this; |
|
1101 } |
|
1102 |
|
1103 ComplexDiagMatrix& |
|
1104 ComplexDiagMatrix::fill (const ColumnVector& a, int beg) |
|
1105 { |
238
|
1106 int a_len = a.length (); |
|
1107 if (beg < 0 || beg + a_len >= length ()) |
227
|
1108 { |
|
1109 (*current_liboctave_error_handler) ("range error for fill"); |
|
1110 return *this; |
|
1111 } |
3
|
1112 |
238
|
1113 for (int i = 0; i < a_len; i++) |
|
1114 elem (i+beg, i+beg) = a.elem (i); |
|
1115 |
3
|
1116 return *this; |
|
1117 } |
|
1118 |
|
1119 ComplexDiagMatrix& |
|
1120 ComplexDiagMatrix::fill (const ComplexColumnVector& a, int beg) |
|
1121 { |
238
|
1122 int a_len = a.length (); |
|
1123 if (beg < 0 || beg + a_len >= length ()) |
227
|
1124 { |
|
1125 (*current_liboctave_error_handler) ("range error for fill"); |
|
1126 return *this; |
|
1127 } |
3
|
1128 |
238
|
1129 for (int i = 0; i < a_len; i++) |
|
1130 elem (i+beg, i+beg) = a.elem (i); |
|
1131 |
3
|
1132 return *this; |
|
1133 } |
|
1134 |
|
1135 ComplexDiagMatrix& |
|
1136 ComplexDiagMatrix::fill (const RowVector& a, int beg) |
|
1137 { |
238
|
1138 int a_len = a.length (); |
|
1139 if (beg < 0 || beg + a_len >= length ()) |
227
|
1140 { |
|
1141 (*current_liboctave_error_handler) ("range error for fill"); |
|
1142 return *this; |
|
1143 } |
3
|
1144 |
238
|
1145 for (int i = 0; i < a_len; i++) |
|
1146 elem (i+beg, i+beg) = a.elem (i); |
|
1147 |
3
|
1148 return *this; |
|
1149 } |
|
1150 |
|
1151 ComplexDiagMatrix& |
|
1152 ComplexDiagMatrix::fill (const ComplexRowVector& a, int beg) |
|
1153 { |
238
|
1154 int a_len = a.length (); |
|
1155 if (beg < 0 || beg + a_len >= length ()) |
227
|
1156 { |
|
1157 (*current_liboctave_error_handler) ("range error for fill"); |
|
1158 return *this; |
|
1159 } |
3
|
1160 |
238
|
1161 for (int i = 0; i < a_len; i++) |
|
1162 elem (i+beg, i+beg) = a.elem (i); |
|
1163 |
3
|
1164 return *this; |
|
1165 } |
|
1166 |
|
1167 ComplexDiagMatrix |
|
1168 ComplexDiagMatrix::transpose (void) const |
|
1169 { |
238
|
1170 return ComplexDiagMatrix (dup (data (), length ()), cols (), rows ()); |
3
|
1171 } |
|
1172 |
|
1173 DiagMatrix |
|
1174 real (const ComplexDiagMatrix& a) |
|
1175 { |
|
1176 DiagMatrix retval; |
238
|
1177 int a_len = a.length (); |
|
1178 if (a_len > 0) |
|
1179 retval = DiagMatrix (real_dup (a.data (), a_len), a.rows (), |
|
1180 a.cols ()); |
3
|
1181 return retval; |
|
1182 } |
|
1183 |
|
1184 DiagMatrix |
|
1185 imag (const ComplexDiagMatrix& a) |
|
1186 { |
|
1187 DiagMatrix retval; |
238
|
1188 int a_len = a.length (); |
|
1189 if (a_len > 0) |
|
1190 retval = DiagMatrix (imag_dup (a.data (), a_len), a.rows (), |
|
1191 a.cols ()); |
3
|
1192 return retval; |
|
1193 } |
|
1194 |
|
1195 ComplexDiagMatrix |
|
1196 conj (const ComplexDiagMatrix& a) |
|
1197 { |
|
1198 ComplexDiagMatrix retval; |
238
|
1199 int a_len = a.length (); |
|
1200 if (a_len > 0) |
|
1201 retval = ComplexDiagMatrix (conj_dup (a.data (), a_len), |
|
1202 a.rows (), a.cols ()); |
3
|
1203 return retval; |
|
1204 } |
|
1205 |
|
1206 // resize is the destructive analog for this one |
|
1207 |
|
1208 ComplexMatrix |
|
1209 ComplexDiagMatrix::extract (int r1, int c1, int r2, int c2) const |
|
1210 { |
|
1211 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
1212 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
1213 |
|
1214 int new_r = r2 - r1 + 1; |
|
1215 int new_c = c2 - c1 + 1; |
|
1216 |
|
1217 ComplexMatrix result (new_r, new_c); |
|
1218 |
|
1219 for (int j = 0; j < new_c; j++) |
|
1220 for (int i = 0; i < new_r; i++) |
238
|
1221 result.elem (i, j) = elem (r1+i, c1+j); |
3
|
1222 |
|
1223 return result; |
|
1224 } |
|
1225 |
|
1226 // extract row or column i. |
|
1227 |
|
1228 ComplexRowVector |
|
1229 ComplexDiagMatrix::row (int i) const |
|
1230 { |
238
|
1231 int nr = rows (); |
|
1232 int nc = cols (); |
3
|
1233 if (i < 0 || i >= nr) |
227
|
1234 { |
|
1235 (*current_liboctave_error_handler) ("invalid row selection"); |
|
1236 return RowVector (); |
|
1237 } |
3
|
1238 |
|
1239 ComplexRowVector retval (nc, 0.0); |
192
|
1240 if (nr <= nc || (nr > nc && i < nc)) |
238
|
1241 retval.elem (i) = elem (i, i); |
3
|
1242 |
|
1243 return retval; |
|
1244 } |
|
1245 |
|
1246 ComplexRowVector |
|
1247 ComplexDiagMatrix::row (char *s) const |
|
1248 { |
|
1249 if (s == (char *) NULL) |
227
|
1250 { |
|
1251 (*current_liboctave_error_handler) ("invalid row selection"); |
|
1252 return ComplexRowVector (); |
|
1253 } |
3
|
1254 |
|
1255 char c = *s; |
|
1256 if (c == 'f' || c == 'F') |
|
1257 return row (0); |
|
1258 else if (c == 'l' || c == 'L') |
238
|
1259 return row (rows () - 1); |
3
|
1260 else |
227
|
1261 { |
|
1262 (*current_liboctave_error_handler) ("invalid row selection"); |
|
1263 return ComplexRowVector (); |
|
1264 } |
3
|
1265 } |
|
1266 |
|
1267 ComplexColumnVector |
|
1268 ComplexDiagMatrix::column (int i) const |
|
1269 { |
238
|
1270 int nr = rows (); |
|
1271 int nc = cols (); |
3
|
1272 if (i < 0 || i >= nc) |
227
|
1273 { |
|
1274 (*current_liboctave_error_handler) ("invalid column selection"); |
|
1275 return ColumnVector (); |
|
1276 } |
3
|
1277 |
|
1278 ComplexColumnVector retval (nr, 0.0); |
192
|
1279 if (nr >= nc || (nr < nc && i < nr)) |
238
|
1280 retval.elem (i) = elem (i, i); |
3
|
1281 |
|
1282 return retval; |
|
1283 } |
|
1284 |
|
1285 ComplexColumnVector |
|
1286 ComplexDiagMatrix::column (char *s) const |
|
1287 { |
|
1288 if (s == (char *) NULL) |
227
|
1289 { |
|
1290 (*current_liboctave_error_handler) ("invalid column selection"); |
|
1291 return ColumnVector (); |
|
1292 } |
3
|
1293 |
|
1294 char c = *s; |
|
1295 if (c == 'f' || c == 'F') |
|
1296 return column (0); |
|
1297 else if (c == 'l' || c == 'L') |
238
|
1298 return column (cols () - 1); |
3
|
1299 else |
227
|
1300 { |
|
1301 (*current_liboctave_error_handler) ("invalid column selection"); |
|
1302 return ColumnVector (); |
|
1303 } |
3
|
1304 } |
|
1305 |
|
1306 ComplexDiagMatrix |
|
1307 ComplexDiagMatrix::inverse (void) const |
|
1308 { |
|
1309 int info; |
|
1310 return inverse (info); |
|
1311 } |
|
1312 |
238
|
1313 ComplexDiagMatrix |
|
1314 ComplexDiagMatrix::inverse (int& info) const |
|
1315 { |
|
1316 int nr = rows (); |
|
1317 int nc = cols (); |
|
1318 if (nr != nc) |
|
1319 { |
|
1320 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
1321 return DiagMatrix (); |
|
1322 } |
|
1323 |
|
1324 ComplexDiagMatrix retval (nr, nc); |
|
1325 |
|
1326 info = 0; |
|
1327 for (int i = 0; i < length (); i++) |
|
1328 { |
|
1329 if (elem (i, i) == 0.0) |
|
1330 { |
|
1331 info = -1; |
|
1332 return *this; |
|
1333 } |
|
1334 else |
|
1335 retval.elem (i, i) = 1.0 / elem (i, i); |
|
1336 } |
|
1337 |
|
1338 return *this; |
|
1339 } |
|
1340 |
|
1341 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
1342 |
|
1343 ComplexDiagMatrix& |
|
1344 ComplexDiagMatrix::operator += (const DiagMatrix& a) |
|
1345 { |
|
1346 int nr = rows (); |
|
1347 int nc = cols (); |
|
1348 if (nr != a.rows () || nc != a.cols ()) |
|
1349 { |
|
1350 (*current_liboctave_error_handler) |
|
1351 ("nonconformant matrix += operation attempted"); |
|
1352 return *this; |
|
1353 } |
|
1354 |
|
1355 if (nr == 0 || nc == 0) |
|
1356 return *this; |
|
1357 |
|
1358 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1359 |
|
1360 add2 (d, a.data (), length ()); |
|
1361 return *this; |
|
1362 } |
|
1363 |
|
1364 ComplexDiagMatrix& |
|
1365 ComplexDiagMatrix::operator -= (const DiagMatrix& a) |
|
1366 { |
|
1367 int nr = rows (); |
|
1368 int nc = cols (); |
|
1369 if (nr != a.rows () || nc != a.cols ()) |
|
1370 { |
|
1371 (*current_liboctave_error_handler) |
|
1372 ("nonconformant matrix -= operation attempted"); |
|
1373 return *this; |
|
1374 } |
|
1375 |
|
1376 if (nr == 0 || nc == 0) |
|
1377 return *this; |
|
1378 |
|
1379 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1380 |
|
1381 subtract2 (d, a.data (), length ()); |
|
1382 return *this; |
|
1383 } |
|
1384 |
|
1385 ComplexDiagMatrix& |
|
1386 ComplexDiagMatrix::operator += (const ComplexDiagMatrix& a) |
|
1387 { |
|
1388 int nr = rows (); |
|
1389 int nc = cols (); |
|
1390 if (nr != a.rows () || nc != a.cols ()) |
|
1391 { |
|
1392 (*current_liboctave_error_handler) |
|
1393 ("nonconformant matrix += operation attempted"); |
|
1394 return *this; |
|
1395 } |
|
1396 |
|
1397 if (nr == 0 || nc == 0) |
|
1398 return *this; |
|
1399 |
|
1400 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1401 |
|
1402 add2 (d, a.data (), length ()); |
|
1403 return *this; |
|
1404 } |
|
1405 |
|
1406 ComplexDiagMatrix& |
|
1407 ComplexDiagMatrix::operator -= (const ComplexDiagMatrix& a) |
|
1408 { |
|
1409 int nr = rows (); |
|
1410 int nc = cols (); |
|
1411 if (nr != a.rows () || nc != a.cols ()) |
|
1412 { |
|
1413 (*current_liboctave_error_handler) |
|
1414 ("nonconformant matrix -= operation attempted"); |
|
1415 return *this; |
|
1416 } |
|
1417 |
|
1418 if (nr == 0 || nc == 0) |
|
1419 return *this; |
|
1420 |
|
1421 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1422 |
|
1423 subtract2 (d, a.data (), length ()); |
|
1424 return *this; |
|
1425 } |
|
1426 |
3
|
1427 // diagonal matrix by scalar -> matrix operations |
|
1428 |
|
1429 ComplexMatrix |
238
|
1430 operator + (const ComplexDiagMatrix& a, double s) |
3
|
1431 { |
238
|
1432 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1433 return a + tmp; |
3
|
1434 } |
|
1435 |
|
1436 ComplexMatrix |
238
|
1437 operator - (const ComplexDiagMatrix& a, double s) |
3
|
1438 { |
238
|
1439 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1440 return a + tmp; |
3
|
1441 } |
|
1442 |
|
1443 ComplexMatrix |
238
|
1444 operator + (const ComplexDiagMatrix& a, const Complex& s) |
3
|
1445 { |
238
|
1446 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1447 return a + tmp; |
3
|
1448 } |
|
1449 |
|
1450 ComplexMatrix |
238
|
1451 operator - (const ComplexDiagMatrix& a, const Complex& s) |
3
|
1452 { |
238
|
1453 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1454 return a + tmp; |
3
|
1455 } |
|
1456 |
|
1457 // diagonal matrix by scalar -> diagonal matrix operations |
|
1458 |
|
1459 ComplexDiagMatrix |
238
|
1460 operator * (const ComplexDiagMatrix& a, double s) |
3
|
1461 { |
238
|
1462 return ComplexDiagMatrix (multiply (a.data (), a.length (), s), |
|
1463 a.rows (), a.cols ()); |
3
|
1464 } |
|
1465 |
|
1466 ComplexDiagMatrix |
238
|
1467 operator / (const ComplexDiagMatrix& a, double s) |
3
|
1468 { |
238
|
1469 return ComplexDiagMatrix (divide (a.data (), a.length (), s), |
|
1470 a.rows (), a.cols ()); |
3
|
1471 } |
|
1472 |
|
1473 // scalar by diagonal matrix -> matrix operations |
|
1474 |
|
1475 ComplexMatrix |
|
1476 operator + (double s, const ComplexDiagMatrix& a) |
|
1477 { |
238
|
1478 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1479 return tmp + a; |
3
|
1480 } |
|
1481 |
|
1482 ComplexMatrix |
|
1483 operator - (double s, const ComplexDiagMatrix& a) |
|
1484 { |
238
|
1485 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1486 return tmp - a; |
3
|
1487 } |
|
1488 |
|
1489 ComplexMatrix |
161
|
1490 operator + (const Complex& s, const ComplexDiagMatrix& a) |
3
|
1491 { |
238
|
1492 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1493 return tmp + a; |
3
|
1494 } |
|
1495 |
|
1496 ComplexMatrix |
161
|
1497 operator - (const Complex& s, const ComplexDiagMatrix& a) |
3
|
1498 { |
238
|
1499 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1500 return tmp - a; |
3
|
1501 } |
|
1502 |
|
1503 // scalar by diagonal matrix -> diagonal matrix operations |
|
1504 |
|
1505 ComplexDiagMatrix |
|
1506 operator * (double s, const ComplexDiagMatrix& a) |
|
1507 { |
238
|
1508 return ComplexDiagMatrix (multiply (a.data (), a.length (), s), |
|
1509 a.rows (), a.cols ()); |
3
|
1510 } |
|
1511 |
|
1512 // diagonal matrix by column vector -> column vector operations |
|
1513 |
|
1514 ComplexColumnVector |
238
|
1515 operator * (const ComplexDiagMatrix& m, const ColumnVector& a) |
3
|
1516 { |
238
|
1517 int nr = m.rows (); |
|
1518 int nc = m.cols (); |
|
1519 int a_len = a.length (); |
|
1520 if (nc != a_len) |
227
|
1521 { |
|
1522 (*current_liboctave_error_handler) |
|
1523 ("nonconformant matrix muliplication attempted"); |
|
1524 return ComplexColumnVector (); |
|
1525 } |
3
|
1526 |
|
1527 if (nc == 0 || nr == 0) |
|
1528 return ComplexColumnVector (0); |
|
1529 |
|
1530 ComplexColumnVector result (nr); |
|
1531 |
238
|
1532 for (int i = 0; i < a_len; i++) |
|
1533 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
1534 |
|
1535 for (i = a_len; i < nr; i++) |
|
1536 result.elem (i) = 0.0; |
|
1537 |
|
1538 return result; |
|
1539 } |
3
|
1540 |
238
|
1541 ComplexColumnVector |
|
1542 operator * (const ComplexDiagMatrix& m, const ComplexColumnVector& a) |
|
1543 { |
|
1544 int nr = m.rows (); |
|
1545 int nc = m.cols (); |
|
1546 int a_len = a.length (); |
|
1547 if (nc != a_len) |
|
1548 { |
|
1549 (*current_liboctave_error_handler) |
|
1550 ("nonconformant matrix muliplication attempted"); |
|
1551 return ComplexColumnVector (); |
|
1552 } |
|
1553 |
|
1554 if (nc == 0 || nr == 0) |
|
1555 return ComplexColumnVector (0); |
|
1556 |
|
1557 ComplexColumnVector result (nr); |
|
1558 |
|
1559 for (int i = 0; i < a_len; i++) |
|
1560 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
1561 |
|
1562 for (i = a_len; i < nr; i++) |
|
1563 result.elem (i) = 0.0; |
3
|
1564 |
|
1565 return result; |
|
1566 } |
|
1567 |
|
1568 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
1569 |
|
1570 ComplexDiagMatrix |
238
|
1571 operator + (const ComplexDiagMatrix& m, const DiagMatrix& a) |
3
|
1572 { |
238
|
1573 int nr = m.rows (); |
|
1574 int nc = m.cols (); |
|
1575 if (nr != a.rows () || nc != a.cols ()) |
227
|
1576 { |
|
1577 (*current_liboctave_error_handler) |
|
1578 ("nonconformant matrix addition attempted"); |
|
1579 return ComplexDiagMatrix (); |
|
1580 } |
3
|
1581 |
|
1582 if (nr == 0 || nc == 0) |
|
1583 return ComplexDiagMatrix (nr, nc); |
|
1584 |
238
|
1585 return ComplexDiagMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
3
|
1586 } |
|
1587 |
|
1588 ComplexDiagMatrix |
238
|
1589 operator - (const ComplexDiagMatrix& m, const DiagMatrix& a) |
3
|
1590 { |
238
|
1591 int nr = m.rows (); |
|
1592 int nc = m.cols (); |
|
1593 if (nr != a.rows () || nc != a.cols ()) |
227
|
1594 { |
|
1595 (*current_liboctave_error_handler) |
|
1596 ("nonconformant matrix subtraction attempted"); |
|
1597 return ComplexDiagMatrix (); |
|
1598 } |
3
|
1599 |
|
1600 if (nr == 0 || nc == 0) |
|
1601 return ComplexDiagMatrix (nr, nc); |
|
1602 |
238
|
1603 return ComplexDiagMatrix (subtract (m.data (), a.data (), m.length ()), |
|
1604 nr, nc); |
3
|
1605 } |
|
1606 |
|
1607 ComplexDiagMatrix |
238
|
1608 product (const ComplexDiagMatrix& m, const DiagMatrix& a) |
3
|
1609 { |
238
|
1610 int nr = m.rows (); |
|
1611 int nc = m.cols (); |
|
1612 if (nr != a.rows () || nc != a.cols ()) |
227
|
1613 { |
|
1614 (*current_liboctave_error_handler) |
|
1615 ("nonconformant matrix product attempted"); |
|
1616 return ComplexDiagMatrix (); |
|
1617 } |
3
|
1618 |
|
1619 if (nr == 0 || nc == 0) |
|
1620 return ComplexDiagMatrix (nr, nc); |
|
1621 |
238
|
1622 return ComplexDiagMatrix (multiply (m.data (), a.data (), m.length ()), |
|
1623 nr, nc); |
3
|
1624 } |
|
1625 |
|
1626 // diagonal matrix by matrix -> matrix operations |
|
1627 |
|
1628 ComplexMatrix |
238
|
1629 operator + (const ComplexDiagMatrix& m, const Matrix& a) |
3
|
1630 { |
238
|
1631 int nr = m.rows (); |
|
1632 int nc = m.cols (); |
|
1633 if (nr != a.rows () || nc != a.cols ()) |
227
|
1634 { |
|
1635 (*current_liboctave_error_handler) |
|
1636 ("nonconformant matrix addition attempted"); |
|
1637 return ComplexMatrix (); |
|
1638 } |
3
|
1639 |
|
1640 if (nr == 0 || nc == 0) |
|
1641 return ComplexMatrix (nr, nc); |
|
1642 |
|
1643 ComplexMatrix result (a); |
238
|
1644 for (int i = 0; i < m.length (); i++) |
|
1645 result.elem (i, i) += m.elem (i, i); |
3
|
1646 |
|
1647 return result; |
|
1648 } |
|
1649 |
|
1650 ComplexMatrix |
238
|
1651 operator - (const ComplexDiagMatrix& m, const Matrix& a) |
3
|
1652 { |
238
|
1653 int nr = m.rows (); |
|
1654 int nc = m.cols (); |
|
1655 if (nr != a.rows () || nc != a.cols ()) |
227
|
1656 { |
|
1657 (*current_liboctave_error_handler) |
|
1658 ("nonconformant matrix subtraction attempted"); |
|
1659 return ComplexMatrix (); |
|
1660 } |
3
|
1661 |
|
1662 if (nr == 0 || nc == 0) |
|
1663 return ComplexMatrix (nr, nc); |
|
1664 |
|
1665 ComplexMatrix result (-a); |
238
|
1666 for (int i = 0; i < m.length (); i++) |
|
1667 result.elem (i, i) += m.elem (i, i); |
3
|
1668 |
|
1669 return result; |
|
1670 } |
|
1671 |
|
1672 ComplexMatrix |
238
|
1673 operator * (const ComplexDiagMatrix& m, const Matrix& a) |
3
|
1674 { |
238
|
1675 int nr = m.rows (); |
|
1676 int nc = m.cols (); |
|
1677 int a_nr = a.rows (); |
|
1678 int a_nc = a.cols (); |
|
1679 if (nc != a_nr) |
227
|
1680 { |
|
1681 (*current_liboctave_error_handler) |
|
1682 ("nonconformant matrix multiplication attempted"); |
|
1683 return ComplexMatrix (); |
|
1684 } |
3
|
1685 |
238
|
1686 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1687 return ComplexMatrix (nr, a_nc, 0.0); |
3
|
1688 |
238
|
1689 ComplexMatrix c (nr, a_nc); |
3
|
1690 |
238
|
1691 for (int i = 0; i < m.length (); i++) |
3
|
1692 { |
238
|
1693 if (m.elem (i, i) == 1.0) |
3
|
1694 { |
238
|
1695 for (int j = 0; j < a_nc; j++) |
3
|
1696 c.elem (i, j) = a.elem (i, j); |
|
1697 } |
238
|
1698 else if (m.elem (i, i) == 0.0) |
3
|
1699 { |
238
|
1700 for (int j = 0; j < a_nc; j++) |
3
|
1701 c.elem (i, j) = 0.0; |
|
1702 } |
|
1703 else |
|
1704 { |
238
|
1705 for (int j = 0; j < a_nc; j++) |
|
1706 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
3
|
1707 } |
|
1708 } |
|
1709 |
|
1710 if (nr > nc) |
|
1711 { |
238
|
1712 for (int j = 0; j < a_nc; j++) |
|
1713 for (int i = a_nr; i < nr; i++) |
3
|
1714 c.elem (i, j) = 0.0; |
|
1715 } |
|
1716 |
|
1717 return c; |
|
1718 } |
|
1719 |
|
1720 ComplexMatrix |
238
|
1721 operator + (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
3
|
1722 { |
238
|
1723 int nr = m.rows (); |
|
1724 int nc = m.cols (); |
|
1725 if (nr != a.rows () || nc != a.cols ()) |
227
|
1726 { |
|
1727 (*current_liboctave_error_handler) |
|
1728 ("nonconformant matrix addition attempted"); |
|
1729 return ComplexMatrix (); |
|
1730 } |
3
|
1731 |
|
1732 if (nr == 0 || nc == 0) |
|
1733 return ComplexMatrix (nr, nc); |
|
1734 |
|
1735 ComplexMatrix result (a); |
238
|
1736 for (int i = 0; i < m.length (); i++) |
|
1737 result.elem (i, i) += m.elem (i, i); |
3
|
1738 |
|
1739 return result; |
|
1740 } |
|
1741 |
|
1742 ComplexMatrix |
238
|
1743 operator - (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
3
|
1744 { |
238
|
1745 int nr = m.rows (); |
|
1746 int nc = m.cols (); |
|
1747 if (nr != a.rows () || nc != a.cols ()) |
227
|
1748 { |
|
1749 (*current_liboctave_error_handler) |
|
1750 ("nonconformant matrix subtraction attempted"); |
|
1751 return ComplexMatrix (); |
|
1752 } |
3
|
1753 |
|
1754 if (nr == 0 || nc == 0) |
|
1755 return ComplexMatrix (nr, nc); |
|
1756 |
|
1757 ComplexMatrix result (-a); |
238
|
1758 for (int i = 0; i < m.length (); i++) |
|
1759 result.elem (i, i) += m.elem (i, i); |
3
|
1760 |
|
1761 return result; |
|
1762 } |
|
1763 |
|
1764 ComplexMatrix |
238
|
1765 operator * (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
3
|
1766 { |
238
|
1767 int nr = m.rows (); |
|
1768 int nc = m.cols (); |
|
1769 int a_nr = a.rows (); |
|
1770 int a_nc = a.cols (); |
|
1771 if (nc != a_nr) |
227
|
1772 { |
|
1773 (*current_liboctave_error_handler) |
|
1774 ("nonconformant matrix multiplication attempted"); |
|
1775 return ComplexMatrix (); |
|
1776 } |
3
|
1777 |
238
|
1778 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1779 return ComplexMatrix (nr, a_nc, 0.0); |
3
|
1780 |
238
|
1781 ComplexMatrix c (nr, a_nc); |
3
|
1782 |
238
|
1783 for (int i = 0; i < m.length (); i++) |
3
|
1784 { |
238
|
1785 if (m.elem (i, i) == 1.0) |
3
|
1786 { |
238
|
1787 for (int j = 0; j < a_nc; j++) |
3
|
1788 c.elem (i, j) = a.elem (i, j); |
|
1789 } |
238
|
1790 else if (m.elem (i, i) == 0.0) |
3
|
1791 { |
238
|
1792 for (int j = 0; j < a_nc; j++) |
3
|
1793 c.elem (i, j) = 0.0; |
|
1794 } |
|
1795 else |
|
1796 { |
238
|
1797 for (int j = 0; j < a_nc; j++) |
|
1798 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
3
|
1799 } |
|
1800 } |
|
1801 |
|
1802 if (nr > nc) |
|
1803 { |
238
|
1804 for (int j = 0; j < a_nc; j++) |
|
1805 for (int i = a_nr; i < nr; i++) |
3
|
1806 c.elem (i, j) = 0.0; |
|
1807 } |
|
1808 |
|
1809 return c; |
|
1810 } |
|
1811 |
238
|
1812 // other operations |
3
|
1813 |
|
1814 ComplexColumnVector |
|
1815 ComplexDiagMatrix::diag (void) const |
|
1816 { |
|
1817 return diag (0); |
|
1818 } |
|
1819 |
|
1820 // Could be optimized... |
|
1821 |
|
1822 ComplexColumnVector |
|
1823 ComplexDiagMatrix::diag (int k) const |
|
1824 { |
238
|
1825 int nnr = rows (); |
|
1826 int nnc = cols (); |
3
|
1827 if (k > 0) |
|
1828 nnc -= k; |
|
1829 else if (k < 0) |
|
1830 nnr += k; |
|
1831 |
|
1832 ComplexColumnVector d; |
|
1833 |
|
1834 if (nnr > 0 && nnc > 0) |
|
1835 { |
|
1836 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
1837 |
|
1838 d.resize (ndiag); |
|
1839 |
|
1840 if (k > 0) |
|
1841 { |
|
1842 for (int i = 0; i < ndiag; i++) |
|
1843 d.elem (i) = elem (i, i+k); |
|
1844 } |
|
1845 else if ( k < 0) |
|
1846 { |
|
1847 for (int i = 0; i < ndiag; i++) |
|
1848 d.elem (i) = elem (i-k, i); |
|
1849 } |
|
1850 else |
|
1851 { |
|
1852 for (int i = 0; i < ndiag; i++) |
|
1853 d.elem (i) = elem (i, i); |
|
1854 } |
|
1855 } |
|
1856 else |
|
1857 cerr << "diag: requested diagonal out of range\n"; |
|
1858 |
|
1859 return d; |
|
1860 } |
|
1861 |
|
1862 // i/o |
|
1863 |
|
1864 ostream& |
|
1865 operator << (ostream& os, const ComplexDiagMatrix& a) |
|
1866 { |
|
1867 Complex ZERO (0.0); |
|
1868 // int field_width = os.precision () + 7; |
238
|
1869 for (int i = 0; i < a.rows (); i++) |
3
|
1870 { |
238
|
1871 for (int j = 0; j < a.cols (); j++) |
3
|
1872 { |
|
1873 if (i == j) |
238
|
1874 os << " " /* setw (field_width) */ << a.elem (i, i); |
3
|
1875 else |
192
|
1876 os << " " /* setw (field_width) */ << ZERO; |
3
|
1877 } |
|
1878 os << "\n"; |
|
1879 } |
|
1880 return os; |
|
1881 } |
|
1882 |
|
1883 /* |
|
1884 ;;; Local Variables: *** |
|
1885 ;;; mode: C++ *** |
|
1886 ;;; page-delimiter: "^/\\*" *** |
|
1887 ;;; End: *** |
|
1888 */ |