458
|
1 // DiagMatrix manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994 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 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #if defined (__GNUG__) |
|
29 #pragma implementation |
|
30 #endif |
|
31 |
|
32 #include <iostream.h> |
|
33 |
|
34 #include <Complex.h> |
|
35 |
|
36 #include "mx-base.h" |
|
37 #include "mx-inlines.cc" |
|
38 #include "lo-error.h" |
|
39 |
|
40 /* |
|
41 * Diagonal Matrix class. |
|
42 */ |
|
43 |
|
44 #define KLUDGE_DIAG_MATRICES |
|
45 #define TYPE double |
|
46 #define KL_DMAT_TYPE DiagMatrix |
|
47 #include "mx-kludge.cc" |
|
48 #undef KLUDGE_DIAG_MATRICES |
|
49 #undef TYPE |
|
50 #undef KL_DMAT_TYPE |
|
51 |
|
52 #if 0 |
|
53 DiagMatrix& |
|
54 DiagMatrix::resize (int r, int c) |
|
55 { |
|
56 if (r < 0 || c < 0) |
|
57 { |
|
58 (*current_liboctave_error_handler) |
|
59 ("can't resize to negative dimensions"); |
|
60 return *this; |
|
61 } |
|
62 |
|
63 int new_len = r < c ? r : c; |
|
64 double *new_data = (double *) NULL; |
|
65 if (new_len > 0) |
|
66 { |
|
67 new_data = new double [new_len]; |
|
68 |
|
69 int min_len = new_len < len ? new_len : len; |
|
70 |
|
71 for (int i = 0; i < min_len; i++) |
|
72 new_data[i] = data[i]; |
|
73 } |
|
74 |
|
75 delete [] data; |
|
76 nr = r; |
|
77 nc = c; |
|
78 len = new_len; |
|
79 data = new_data; |
|
80 |
|
81 return *this; |
|
82 } |
|
83 |
|
84 DiagMatrix& |
|
85 DiagMatrix::resize (int r, int c, double val) |
|
86 { |
|
87 if (r < 0 || c < 0) |
|
88 { |
|
89 (*current_liboctave_error_handler) |
|
90 ("can't resize to negative dimensions"); |
|
91 return *this; |
|
92 } |
|
93 |
|
94 int new_len = r < c ? r : c; |
|
95 double *new_data = (double *) NULL; |
|
96 if (new_len > 0) |
|
97 { |
|
98 new_data = new double [new_len]; |
|
99 |
|
100 int min_len = new_len < len ? new_len : len; |
|
101 |
|
102 for (int i = 0; i < min_len; i++) |
|
103 new_data[i] = data[i]; |
|
104 |
|
105 for (i = min_len; i < new_len; i++) |
|
106 new_data[i] = val; |
|
107 } |
|
108 |
|
109 delete [] data; |
|
110 nr = r; |
|
111 nc = c; |
|
112 len = new_len; |
|
113 data = new_data; |
|
114 |
|
115 return *this; |
|
116 } |
|
117 #endif |
|
118 |
|
119 int |
|
120 DiagMatrix::operator == (const DiagMatrix& a) const |
|
121 { |
|
122 if (rows () != a.rows () || cols () != a.cols ()) |
|
123 return 0; |
|
124 |
|
125 return equal (data (), a.data (), length ()); |
|
126 } |
|
127 |
|
128 int |
|
129 DiagMatrix::operator != (const DiagMatrix& a) const |
|
130 { |
|
131 return !(*this == a); |
|
132 } |
|
133 |
|
134 DiagMatrix& |
|
135 DiagMatrix::fill (double val) |
|
136 { |
|
137 for (int i = 0; i < length (); i++) |
|
138 elem (i, i) = val; |
|
139 return *this; |
|
140 } |
|
141 |
|
142 DiagMatrix& |
|
143 DiagMatrix::fill (double val, int beg, int end) |
|
144 { |
|
145 if (beg < 0 || end >= length () || end < beg) |
|
146 { |
|
147 (*current_liboctave_error_handler) ("range error for fill"); |
|
148 return *this; |
|
149 } |
|
150 |
|
151 for (int i = beg; i < end; i++) |
|
152 elem (i, i) = val; |
|
153 |
|
154 return *this; |
|
155 } |
|
156 |
|
157 DiagMatrix& |
|
158 DiagMatrix::fill (const ColumnVector& 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 DiagMatrix& |
|
174 DiagMatrix::fill (const RowVector& a) |
|
175 { |
|
176 int len = length (); |
|
177 if (a.length () != len) |
|
178 { |
|
179 (*current_liboctave_error_handler) ("range error for fill"); |
|
180 return *this; |
|
181 } |
|
182 |
|
183 for (int i = 0; i < len; i++) |
|
184 elem (i, i) = a.elem (i); |
|
185 |
|
186 return *this; |
|
187 } |
|
188 |
|
189 DiagMatrix& |
|
190 DiagMatrix::fill (const ColumnVector& 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 DiagMatrix& |
|
206 DiagMatrix::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 DiagMatrix |
|
222 DiagMatrix::transpose (void) const |
|
223 { |
|
224 return DiagMatrix (dup (data (), length ()), cols (), rows ()); |
|
225 } |
|
226 |
|
227 Matrix |
|
228 DiagMatrix::extract (int r1, int c1, int r2, int c2) const |
|
229 { |
|
230 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
231 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
232 |
|
233 int new_r = r2 - r1 + 1; |
|
234 int new_c = c2 - c1 + 1; |
|
235 |
|
236 Matrix result (new_r, new_c); |
|
237 |
|
238 for (int j = 0; j < new_c; j++) |
|
239 for (int i = 0; i < new_r; i++) |
|
240 result.elem (i, j) = elem (r1+i, c1+j); |
|
241 |
|
242 return result; |
|
243 } |
|
244 |
|
245 // extract row or column i. |
|
246 |
|
247 RowVector |
|
248 DiagMatrix::row (int i) const |
|
249 { |
|
250 int nr = rows (); |
|
251 int nc = cols (); |
|
252 if (i < 0 || i >= nr) |
|
253 { |
|
254 (*current_liboctave_error_handler) ("invalid row selection"); |
|
255 return RowVector (); |
|
256 } |
|
257 |
|
258 RowVector retval (nc, 0.0); |
|
259 if (nr <= nc || (nr > nc && i < nc)) |
|
260 retval.elem (i) = elem (i, i); |
|
261 |
|
262 return retval; |
|
263 } |
|
264 |
|
265 RowVector |
|
266 DiagMatrix::row (char *s) const |
|
267 { |
|
268 if (s == (char *) NULL) |
|
269 { |
|
270 (*current_liboctave_error_handler) ("invalid row selection"); |
|
271 return RowVector (); |
|
272 } |
|
273 |
|
274 char c = *s; |
|
275 if (c == 'f' || c == 'F') |
|
276 return row (0); |
|
277 else if (c == 'l' || c == 'L') |
|
278 return row (rows () - 1); |
|
279 else |
|
280 { |
|
281 (*current_liboctave_error_handler) ("invalid row selection"); |
|
282 return RowVector (); |
|
283 } |
|
284 } |
|
285 |
|
286 ColumnVector |
|
287 DiagMatrix::column (int i) const |
|
288 { |
|
289 int nr = rows (); |
|
290 int nc = cols (); |
|
291 if (i < 0 || i >= nc) |
|
292 { |
|
293 (*current_liboctave_error_handler) ("invalid column selection"); |
|
294 return ColumnVector (); |
|
295 } |
|
296 |
|
297 ColumnVector retval (nr, 0.0); |
|
298 if (nr >= nc || (nr < nc && i < nr)) |
|
299 retval.elem (i) = elem (i, i); |
|
300 |
|
301 return retval; |
|
302 } |
|
303 |
|
304 ColumnVector |
|
305 DiagMatrix::column (char *s) const |
|
306 { |
|
307 if (s == (char *) NULL) |
|
308 { |
|
309 (*current_liboctave_error_handler) ("invalid column selection"); |
|
310 return ColumnVector (); |
|
311 } |
|
312 |
|
313 char c = *s; |
|
314 if (c == 'f' || c == 'F') |
|
315 return column (0); |
|
316 else if (c == 'l' || c == 'L') |
|
317 return column (cols () - 1); |
|
318 else |
|
319 { |
|
320 (*current_liboctave_error_handler) ("invalid column selection"); |
|
321 return ColumnVector (); |
|
322 } |
|
323 } |
|
324 |
|
325 DiagMatrix |
|
326 DiagMatrix::inverse (void) const |
|
327 { |
|
328 int info; |
|
329 return inverse (info); |
|
330 } |
|
331 |
|
332 DiagMatrix |
|
333 DiagMatrix::inverse (int &info) const |
|
334 { |
|
335 int nr = rows (); |
|
336 int nc = cols (); |
|
337 int len = length (); |
|
338 if (nr != nc) |
|
339 { |
|
340 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
341 return DiagMatrix (); |
|
342 } |
|
343 |
|
344 info = 0; |
|
345 double *tmp_data = dup (data (), len); |
|
346 for (int i = 0; i < len; i++) |
|
347 { |
|
348 if (elem (i, i) == 0.0) |
|
349 { |
|
350 info = -1; |
|
351 copy (tmp_data, data (), len); // Restore contents. |
|
352 break; |
|
353 } |
|
354 else |
|
355 { |
|
356 tmp_data[i] = 1.0 / elem (i, i); |
|
357 } |
|
358 } |
|
359 |
|
360 return DiagMatrix (tmp_data, nr, nc); |
|
361 } |
|
362 |
|
363 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
364 |
|
365 DiagMatrix& |
|
366 DiagMatrix::operator += (const DiagMatrix& a) |
|
367 { |
|
368 int nr = rows (); |
|
369 int nc = cols (); |
|
370 if (nr != a.rows () || nc != a.cols ()) |
|
371 { |
|
372 (*current_liboctave_error_handler) |
|
373 ("nonconformant matrix += operation attempted"); |
|
374 return *this; |
|
375 } |
|
376 |
|
377 if (nc == 0 || nr == 0) |
|
378 return *this; |
|
379 |
|
380 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
381 |
|
382 add2 (d, a.data (), length ()); |
|
383 return *this; |
|
384 } |
|
385 |
|
386 DiagMatrix& |
|
387 DiagMatrix::operator -= (const DiagMatrix& a) |
|
388 { |
|
389 int nr = rows (); |
|
390 int nc = cols (); |
|
391 if (nr != a.rows () || nc != a.cols ()) |
|
392 { |
|
393 (*current_liboctave_error_handler) |
|
394 ("nonconformant matrix -= operation attempted"); |
|
395 return *this; |
|
396 } |
|
397 |
|
398 if (nr == 0 || nc == 0) |
|
399 return *this; |
|
400 |
|
401 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
402 |
|
403 subtract2 (d, a.data (), length ()); |
|
404 return *this; |
|
405 } |
|
406 |
|
407 // diagonal matrix by scalar -> matrix operations |
|
408 |
|
409 Matrix |
|
410 operator + (const DiagMatrix& a, double s) |
|
411 { |
|
412 Matrix tmp (a.rows (), a.cols (), s); |
|
413 return a + tmp; |
|
414 } |
|
415 |
|
416 Matrix |
|
417 operator - (const DiagMatrix& a, double s) |
|
418 { |
|
419 Matrix tmp (a.rows (), a.cols (), -s); |
|
420 return a + tmp; |
|
421 } |
|
422 |
|
423 ComplexMatrix |
|
424 operator + (const DiagMatrix& a, const Complex& s) |
|
425 { |
|
426 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
427 return a + tmp; |
|
428 } |
|
429 |
|
430 ComplexMatrix |
|
431 operator - (const DiagMatrix& a, const Complex& s) |
|
432 { |
|
433 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
434 return a + tmp; |
|
435 } |
|
436 |
|
437 // diagonal matrix by scalar -> diagonal matrix operations |
|
438 |
|
439 ComplexDiagMatrix |
|
440 operator * (const DiagMatrix& a, const Complex& s) |
|
441 { |
|
442 return ComplexDiagMatrix (multiply (a.data (), a.length (), s), |
|
443 a.rows (), a.cols ()); |
|
444 } |
|
445 |
|
446 ComplexDiagMatrix |
|
447 operator / (const DiagMatrix& a, const Complex& s) |
|
448 { |
|
449 return ComplexDiagMatrix (divide (a.data (), a.length (), s), |
|
450 a.rows (), a.cols ()); |
|
451 } |
|
452 |
|
453 // scalar by diagonal matrix -> matrix operations |
|
454 |
|
455 Matrix |
|
456 operator + (double s, const DiagMatrix& a) |
|
457 { |
|
458 Matrix tmp (a.rows (), a.cols (), s); |
|
459 return tmp + a; |
|
460 } |
|
461 |
|
462 Matrix |
|
463 operator - (double s, const DiagMatrix& a) |
|
464 { |
|
465 Matrix tmp (a.rows (), a.cols (), s); |
|
466 return tmp - a; |
|
467 } |
|
468 |
|
469 ComplexMatrix |
|
470 operator + (const Complex& s, const DiagMatrix& a) |
|
471 { |
|
472 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
473 return tmp + a; |
|
474 } |
|
475 |
|
476 ComplexMatrix |
|
477 operator - (const Complex& s, const DiagMatrix& a) |
|
478 { |
|
479 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
480 return tmp - a; |
|
481 } |
|
482 |
|
483 // scalar by diagonal matrix -> diagonal matrix operations |
|
484 |
|
485 ComplexDiagMatrix |
|
486 operator * (const Complex& s, const DiagMatrix& a) |
|
487 { |
|
488 return ComplexDiagMatrix (multiply (a.data (), a.length (), s), |
|
489 a.rows (), a.cols ()); |
|
490 } |
|
491 |
|
492 // diagonal matrix by column vector -> column vector operations |
|
493 |
|
494 ColumnVector |
|
495 operator * (const DiagMatrix& m, const ColumnVector& a) |
|
496 { |
|
497 int nr = m.rows (); |
|
498 int nc = m.cols (); |
|
499 int a_len = a.length (); |
|
500 if (nc != a_len) |
|
501 { |
|
502 (*current_liboctave_error_handler) |
|
503 ("nonconformant matrix multiplication attempted"); |
|
504 return ColumnVector (); |
|
505 } |
|
506 |
|
507 if (nc == 0 || nr == 0) |
|
508 return ColumnVector (0); |
|
509 |
|
510 ColumnVector result (nr); |
|
511 |
|
512 for (int i = 0; i < a_len; i++) |
|
513 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
514 |
|
515 for (i = a_len; i < nr; i++) |
|
516 result.elem (i) = 0.0; |
|
517 |
|
518 return result; |
|
519 } |
|
520 |
|
521 ComplexColumnVector |
|
522 operator * (const DiagMatrix& m, const ComplexColumnVector& a) |
|
523 { |
|
524 int nr = m.rows (); |
|
525 int nc = m.cols (); |
|
526 int a_len = a.length (); |
|
527 if (nc != a_len) |
|
528 { |
|
529 (*current_liboctave_error_handler) |
|
530 ("nonconformant matrix multiplication attempted"); |
|
531 return ColumnVector (); |
|
532 } |
|
533 |
|
534 if (nc == 0 || nr == 0) |
|
535 return ComplexColumnVector (0); |
|
536 |
|
537 ComplexColumnVector result (nr); |
|
538 |
|
539 for (int i = 0; i < a_len; i++) |
|
540 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
541 |
|
542 for (i = a_len; i < nr; i++) |
|
543 result.elem (i) = 0.0; |
|
544 |
|
545 return result; |
|
546 } |
|
547 |
|
548 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
549 |
|
550 DiagMatrix |
|
551 operator * (const DiagMatrix& a, const DiagMatrix& b) |
|
552 { |
|
553 int nr_a = a.rows (); |
|
554 int nc_a = a.cols (); |
|
555 int nr_b = b.rows (); |
|
556 int nc_b = b.cols (); |
|
557 if (nc_a != nr_b) |
|
558 { |
|
559 (*current_liboctave_error_handler) |
|
560 ("nonconformant matrix multiplication attempted"); |
|
561 return DiagMatrix (); |
|
562 } |
|
563 |
|
564 if (nr_a == 0 || nc_a == 0 || nc_b == 0) |
|
565 return DiagMatrix (nr_a, nc_a, 0.0); |
|
566 |
|
567 DiagMatrix c (nr_a, nc_b); |
|
568 |
|
569 int len = nr_a < nc_b ? nr_a : nc_b; |
|
570 |
|
571 for (int i = 0; i < len; i++) |
|
572 { |
|
573 double a_element = a.elem (i, i); |
|
574 double b_element = b.elem (i, i); |
|
575 |
|
576 if (a_element == 0.0 || b_element == 0.0) |
|
577 c.elem (i, i) = 0.0; |
|
578 else if (a_element == 1.0) |
|
579 c.elem (i, i) = b_element; |
|
580 else if (b_element == 1.0) |
|
581 c.elem (i, i) = a_element; |
|
582 else |
|
583 c.elem (i, i) = a_element * b_element; |
|
584 } |
|
585 |
|
586 return c; |
|
587 } |
|
588 |
|
589 ComplexDiagMatrix |
|
590 operator + (const DiagMatrix& m, const ComplexDiagMatrix& a) |
|
591 { |
|
592 int nr = m.rows (); |
|
593 int nc = m.cols (); |
|
594 if (nr != a.rows () || nc != a.cols ()) |
|
595 { |
|
596 (*current_liboctave_error_handler) |
|
597 ("nonconformant matrix addition attempted"); |
|
598 return ComplexDiagMatrix (); |
|
599 } |
|
600 |
|
601 if (nc == 0 || nr == 0) |
|
602 return ComplexDiagMatrix (nr, nc); |
|
603 |
|
604 return ComplexDiagMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
605 } |
|
606 |
|
607 ComplexDiagMatrix |
|
608 operator - (const DiagMatrix& m, const ComplexDiagMatrix& a) |
|
609 { |
|
610 int nr = m.rows (); |
|
611 int nc = m.cols (); |
|
612 if (nr != a.rows () || nc != a.cols ()) |
|
613 { |
|
614 (*current_liboctave_error_handler) |
|
615 ("nonconformant matrix subtraction attempted"); |
|
616 return ComplexDiagMatrix (); |
|
617 } |
|
618 |
|
619 if (nc == 0 || nr == 0) |
|
620 return ComplexDiagMatrix (nr, nc); |
|
621 |
|
622 return ComplexDiagMatrix (subtract (m.data (), a.data (), m.length ()), |
|
623 nr, nc); |
|
624 } |
|
625 |
|
626 ComplexDiagMatrix |
|
627 operator * (const DiagMatrix& a, const ComplexDiagMatrix& b) |
|
628 { |
|
629 int nr_a = a.rows (); |
|
630 int nc_a = a.cols (); |
|
631 int nr_b = b.rows (); |
|
632 int nc_b = b.cols (); |
|
633 if (nc_a != nr_b) |
|
634 { |
|
635 (*current_liboctave_error_handler) |
|
636 ("nonconformant matrix multiplication attempted"); |
|
637 return ComplexDiagMatrix (); |
|
638 } |
|
639 |
|
640 if (nr_a == 0 || nc_a == 0 || nc_b == 0) |
|
641 return ComplexDiagMatrix (nr_a, nc_a, 0.0); |
|
642 |
|
643 ComplexDiagMatrix c (nr_a, nc_b); |
|
644 |
|
645 int len = nr_a < nc_b ? nr_a : nc_b; |
|
646 |
|
647 for (int i = 0; i < len; i++) |
|
648 { |
|
649 double a_element = a.elem (i, i); |
|
650 Complex b_element = b.elem (i, i); |
|
651 |
|
652 if (a_element == 0.0 || b_element == 0.0) |
|
653 c.elem (i, i) = 0.0; |
|
654 else if (a_element == 1.0) |
|
655 c.elem (i, i) = b_element; |
|
656 else if (b_element == 1.0) |
|
657 c.elem (i, i) = a_element; |
|
658 else |
|
659 c.elem (i, i) = a_element * b_element; |
|
660 } |
|
661 |
|
662 return c; |
|
663 } |
|
664 |
|
665 ComplexDiagMatrix |
|
666 product (const DiagMatrix& m, const ComplexDiagMatrix& a) |
|
667 { |
|
668 int nr = m.rows (); |
|
669 int nc = m.cols (); |
|
670 if (nr != a.rows () || nc != a.cols ()) |
|
671 { |
|
672 (*current_liboctave_error_handler) |
|
673 ("nonconformant matrix product attempted"); |
|
674 return ComplexDiagMatrix (); |
|
675 } |
|
676 |
|
677 if (nc == 0 || nr == 0) |
|
678 return ComplexDiagMatrix (nr, nc); |
|
679 |
|
680 return ComplexDiagMatrix (multiply (m.data (), a.data (), m.length ()), |
|
681 nr, nc); |
|
682 } |
|
683 |
|
684 // diagonal matrix by matrix -> matrix operations |
|
685 |
|
686 Matrix |
|
687 operator + (const DiagMatrix& m, const Matrix& a) |
|
688 { |
|
689 int nr = m.rows (); |
|
690 int nc = m.cols (); |
|
691 if (nr != a.rows () || nc != a.cols ()) |
|
692 { |
|
693 (*current_liboctave_error_handler) |
|
694 ("nonconformant matrix addition attempted"); |
|
695 return Matrix (); |
|
696 } |
|
697 |
|
698 if (nr == 0 || nc == 0) |
|
699 return Matrix (nr, nc); |
|
700 |
|
701 Matrix result (a); |
|
702 for (int i = 0; i < m.length (); i++) |
|
703 result.elem (i, i) += m.elem (i, i); |
|
704 |
|
705 return result; |
|
706 } |
|
707 |
|
708 Matrix |
|
709 operator - (const DiagMatrix& m, const Matrix& a) |
|
710 { |
|
711 int nr = m.rows (); |
|
712 int nc = m.cols (); |
|
713 if (nr != a.rows () || nc != a.cols ()) |
|
714 { |
|
715 (*current_liboctave_error_handler) |
|
716 ("nonconformant matrix subtraction attempted"); |
|
717 return Matrix (); |
|
718 } |
|
719 |
|
720 if (nr == 0 || nc == 0) |
|
721 return Matrix (nr, nc); |
|
722 |
|
723 Matrix result (-a); |
|
724 for (int i = 0; i < m.length (); i++) |
|
725 result.elem (i, i) += m.elem (i, i); |
|
726 |
|
727 return result; |
|
728 } |
|
729 |
|
730 Matrix |
|
731 operator * (const DiagMatrix& m, const Matrix& a) |
|
732 { |
|
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) |
|
738 { |
|
739 (*current_liboctave_error_handler) |
|
740 ("nonconformant matrix multiplication attempted"); |
|
741 return Matrix (); |
|
742 } |
|
743 |
|
744 if (nr == 0 || nc == 0 || a_nc == 0) |
|
745 return Matrix (nr, a_nc, 0.0); |
|
746 |
|
747 Matrix c (nr, a_nc); |
|
748 |
|
749 for (int i = 0; i < m.length (); i++) |
|
750 { |
|
751 if (m.elem (i, i) == 1.0) |
|
752 { |
|
753 for (int j = 0; j < a_nc; j++) |
|
754 c.elem (i, j) = a.elem (i, j); |
|
755 } |
|
756 else if (m.elem (i, i) == 0.0) |
|
757 { |
|
758 for (int j = 0; j < a_nc; j++) |
|
759 c.elem (i, j) = 0.0; |
|
760 } |
|
761 else |
|
762 { |
|
763 for (int j = 0; j < a_nc; j++) |
|
764 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
765 } |
|
766 } |
|
767 |
|
768 if (nr > nc) |
|
769 { |
|
770 for (int j = 0; j < a_nc; j++) |
|
771 for (int i = a_nr; i < nr; i++) |
|
772 c.elem (i, j) = 0.0; |
|
773 } |
|
774 |
|
775 return c; |
|
776 } |
|
777 |
|
778 ComplexMatrix |
|
779 operator + (const DiagMatrix& m, const ComplexMatrix& a) |
|
780 { |
|
781 int nr = m.rows (); |
|
782 int nc = m.cols (); |
|
783 if (nr != a.rows () || nc != a.cols ()) |
|
784 { |
|
785 (*current_liboctave_error_handler) |
|
786 ("nonconformant matrix addition attempted"); |
|
787 return ComplexMatrix (); |
|
788 } |
|
789 |
|
790 if (nr == 0 || nc == 0) |
|
791 return ComplexMatrix (nr, nc); |
|
792 |
|
793 ComplexMatrix result (a); |
|
794 for (int i = 0; i < m.length (); i++) |
|
795 result.elem (i, i) += m.elem (i, i); |
|
796 |
|
797 return result; |
|
798 } |
|
799 |
|
800 ComplexMatrix |
|
801 operator - (const DiagMatrix& m, const ComplexMatrix& a) |
|
802 { |
|
803 int nr = m.rows (); |
|
804 int nc = m.cols (); |
|
805 if (nr != a.rows () || nc != a.cols ()) |
|
806 { |
|
807 (*current_liboctave_error_handler) |
|
808 ("nonconformant matrix subtraction attempted"); |
|
809 return ComplexMatrix (); |
|
810 } |
|
811 |
|
812 if (nr == 0 || nc == 0) |
|
813 return ComplexMatrix (nr, nc); |
|
814 |
|
815 ComplexMatrix result (-a); |
|
816 for (int i = 0; i < m.length (); i++) |
|
817 result.elem (i, i) += m.elem (i, i); |
|
818 |
|
819 return result; |
|
820 } |
|
821 |
|
822 ComplexMatrix |
|
823 operator * (const DiagMatrix& m, const ComplexMatrix& a) |
|
824 { |
|
825 int nr = m.rows (); |
|
826 int nc = m.cols (); |
|
827 int a_nr = a.rows (); |
|
828 int a_nc = a.cols (); |
|
829 if (nc != a_nr) |
|
830 { |
|
831 (*current_liboctave_error_handler) |
|
832 ("nonconformant matrix multiplication attempted"); |
|
833 return ComplexMatrix (); |
|
834 } |
|
835 |
|
836 if (nr == 0 || nc == 0 || a_nc == 0) |
|
837 return ComplexMatrix (nr, nc, 0.0); |
|
838 |
|
839 ComplexMatrix c (nr, a_nc); |
|
840 |
|
841 for (int i = 0; i < m.length (); i++) |
|
842 { |
|
843 if (m.elem (i, i) == 1.0) |
|
844 { |
|
845 for (int j = 0; j < a_nc; j++) |
|
846 c.elem (i, j) = a.elem (i, j); |
|
847 } |
|
848 else if (m.elem (i, i) == 0.0) |
|
849 { |
|
850 for (int j = 0; j < a_nc; j++) |
|
851 c.elem (i, j) = 0.0; |
|
852 } |
|
853 else |
|
854 { |
|
855 for (int j = 0; j < a_nc; j++) |
|
856 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
857 } |
|
858 } |
|
859 |
|
860 if (nr > nc) |
|
861 { |
|
862 for (int j = 0; j < a_nc; j++) |
|
863 for (int i = a_nr; i < nr; i++) |
|
864 c.elem (i, j) = 0.0; |
|
865 } |
|
866 |
|
867 return c; |
|
868 } |
|
869 |
|
870 // other operations |
|
871 |
|
872 ColumnVector |
|
873 DiagMatrix::diag (void) const |
|
874 { |
|
875 return diag (0); |
|
876 } |
|
877 |
|
878 // Could be optimized... |
|
879 |
|
880 ColumnVector |
|
881 DiagMatrix::diag (int k) const |
|
882 { |
|
883 int nnr = rows (); |
|
884 int nnc = cols (); |
|
885 if (k > 0) |
|
886 nnc -= k; |
|
887 else if (k < 0) |
|
888 nnr += k; |
|
889 |
|
890 ColumnVector d; |
|
891 |
|
892 if (nnr > 0 && nnc > 0) |
|
893 { |
|
894 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
895 |
|
896 d.resize (ndiag); |
|
897 |
|
898 if (k > 0) |
|
899 { |
|
900 for (int i = 0; i < ndiag; i++) |
|
901 d.elem (i) = elem (i, i+k); |
|
902 } |
|
903 else if ( k < 0) |
|
904 { |
|
905 for (int i = 0; i < ndiag; i++) |
|
906 d.elem (i) = elem (i-k, i); |
|
907 } |
|
908 else |
|
909 { |
|
910 for (int i = 0; i < ndiag; i++) |
|
911 d.elem (i) = elem (i, i); |
|
912 } |
|
913 } |
|
914 else |
|
915 cerr << "diag: requested diagonal out of range\n"; |
|
916 |
|
917 return d; |
|
918 } |
|
919 |
|
920 ostream& |
|
921 operator << (ostream& os, const DiagMatrix& a) |
|
922 { |
|
923 // int field_width = os.precision () + 7; |
|
924 for (int i = 0; i < a.rows (); i++) |
|
925 { |
|
926 for (int j = 0; j < a.cols (); j++) |
|
927 { |
|
928 if (i == j) |
|
929 os << " " /* setw (field_width) */ << a.elem (i, i); |
|
930 else |
|
931 os << " " /* setw (field_width) */ << 0.0; |
|
932 } |
|
933 os << "\n"; |
|
934 } |
|
935 return os; |
|
936 } |
|
937 |
|
938 /* |
|
939 ;;; Local Variables: *** |
|
940 ;;; mode: C++ *** |
|
941 ;;; page-delimiter: "^/\\*" *** |
|
942 ;;; End: *** |
|
943 */ |