3
|
1 // DiagMatrix manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
378
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
3
|
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 |
378
|
536 DiagMatrix |
|
537 operator * (const DiagMatrix& a, const DiagMatrix& b) |
|
538 { |
|
539 int nr_a = a.rows (); |
|
540 int nc_a = a.cols (); |
|
541 int nr_b = b.rows (); |
|
542 int nc_b = b.cols (); |
|
543 if (nc_a != nr_b) |
|
544 { |
|
545 (*current_liboctave_error_handler) |
|
546 ("nonconformant matrix multiplication attempted"); |
|
547 return DiagMatrix (); |
|
548 } |
|
549 |
|
550 if (nr_a == 0 || nc_a == 0 || nc_b == 0) |
|
551 return DiagMatrix (nr_a, nc_a, 0.0); |
|
552 |
|
553 DiagMatrix c (nr_a, nc_b); |
|
554 |
|
555 int len = nr_a < nc_b ? nr_a : nc_b; |
|
556 |
|
557 for (int i = 0; i < len; i++) |
|
558 { |
|
559 double a_element = a.elem (i, i); |
|
560 double b_element = b.elem (i, i); |
|
561 |
|
562 if (a_element == 0.0 || b_element == 0.0) |
|
563 c.elem (i, i) = 0.0; |
|
564 else if (a_element == 1.0) |
|
565 c.elem (i, i) = b_element; |
|
566 else if (b_element == 1.0) |
|
567 c.elem (i, i) = a_element; |
|
568 else |
|
569 c.elem (i, i) = a_element * b_element; |
|
570 } |
|
571 |
|
572 return c; |
|
573 } |
|
574 |
238
|
575 ComplexDiagMatrix |
|
576 operator + (const DiagMatrix& m, const ComplexDiagMatrix& a) |
3
|
577 { |
238
|
578 int nr = m.rows (); |
|
579 int nc = m.cols (); |
|
580 if (nr != a.rows () || nc != a.cols ()) |
227
|
581 { |
|
582 (*current_liboctave_error_handler) |
|
583 ("nonconformant matrix addition attempted"); |
|
584 return ComplexDiagMatrix (); |
|
585 } |
3
|
586 |
|
587 if (nc == 0 || nr == 0) |
|
588 return ComplexDiagMatrix (nr, nc); |
|
589 |
238
|
590 return ComplexDiagMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
3
|
591 } |
|
592 |
|
593 ComplexDiagMatrix |
238
|
594 operator - (const DiagMatrix& m, const ComplexDiagMatrix& a) |
3
|
595 { |
238
|
596 int nr = m.rows (); |
|
597 int nc = m.cols (); |
|
598 if (nr != a.rows () || nc != a.cols ()) |
227
|
599 { |
|
600 (*current_liboctave_error_handler) |
|
601 ("nonconformant matrix subtraction attempted"); |
|
602 return ComplexDiagMatrix (); |
|
603 } |
3
|
604 |
|
605 if (nc == 0 || nr == 0) |
|
606 return ComplexDiagMatrix (nr, nc); |
|
607 |
238
|
608 return ComplexDiagMatrix (subtract (m.data (), a.data (), m.length ()), |
|
609 nr, nc); |
3
|
610 } |
|
611 |
|
612 ComplexDiagMatrix |
378
|
613 operator * (const DiagMatrix& a, const ComplexDiagMatrix& b) |
|
614 { |
|
615 int nr_a = a.rows (); |
|
616 int nc_a = a.cols (); |
|
617 int nr_b = b.rows (); |
|
618 int nc_b = b.cols (); |
|
619 if (nc_a != nr_b) |
|
620 { |
|
621 (*current_liboctave_error_handler) |
|
622 ("nonconformant matrix multiplication attempted"); |
|
623 return ComplexDiagMatrix (); |
|
624 } |
|
625 |
|
626 if (nr_a == 0 || nc_a == 0 || nc_b == 0) |
|
627 return ComplexDiagMatrix (nr_a, nc_a, 0.0); |
|
628 |
|
629 ComplexDiagMatrix c (nr_a, nc_b); |
|
630 |
|
631 int len = nr_a < nc_b ? nr_a : nc_b; |
|
632 |
|
633 for (int i = 0; i < len; i++) |
|
634 { |
|
635 double a_element = a.elem (i, i); |
|
636 Complex b_element = b.elem (i, i); |
|
637 |
|
638 if (a_element == 0.0 || b_element == 0.0) |
|
639 c.elem (i, i) = 0.0; |
|
640 else if (a_element == 1.0) |
|
641 c.elem (i, i) = b_element; |
|
642 else if (b_element == 1.0) |
|
643 c.elem (i, i) = a_element; |
|
644 else |
|
645 c.elem (i, i) = a_element * b_element; |
|
646 } |
|
647 |
|
648 return c; |
|
649 } |
|
650 |
|
651 ComplexDiagMatrix |
238
|
652 product (const DiagMatrix& m, const ComplexDiagMatrix& a) |
3
|
653 { |
238
|
654 int nr = m.rows (); |
|
655 int nc = m.cols (); |
|
656 if (nr != a.rows () || nc != a.cols ()) |
227
|
657 { |
|
658 (*current_liboctave_error_handler) |
|
659 ("nonconformant matrix product attempted"); |
|
660 return ComplexDiagMatrix (); |
|
661 } |
3
|
662 |
|
663 if (nc == 0 || nr == 0) |
|
664 return ComplexDiagMatrix (nr, nc); |
|
665 |
238
|
666 return ComplexDiagMatrix (multiply (m.data (), a.data (), m.length ()), |
|
667 nr, nc); |
3
|
668 } |
|
669 |
|
670 // diagonal matrix by matrix -> matrix operations |
|
671 |
|
672 Matrix |
238
|
673 operator + (const DiagMatrix& m, const Matrix& a) |
3
|
674 { |
238
|
675 int nr = m.rows (); |
|
676 int nc = m.cols (); |
|
677 if (nr != a.rows () || nc != a.cols ()) |
227
|
678 { |
|
679 (*current_liboctave_error_handler) |
|
680 ("nonconformant matrix addition attempted"); |
|
681 return Matrix (); |
|
682 } |
3
|
683 |
|
684 if (nr == 0 || nc == 0) |
|
685 return Matrix (nr, nc); |
|
686 |
|
687 Matrix result (a); |
238
|
688 for (int i = 0; i < m.length (); i++) |
|
689 result.elem (i, i) += m.elem (i, i); |
3
|
690 |
|
691 return result; |
|
692 } |
|
693 |
|
694 Matrix |
238
|
695 operator - (const DiagMatrix& m, const Matrix& a) |
3
|
696 { |
238
|
697 int nr = m.rows (); |
|
698 int nc = m.cols (); |
|
699 if (nr != a.rows () || nc != a.cols ()) |
227
|
700 { |
|
701 (*current_liboctave_error_handler) |
|
702 ("nonconformant matrix subtraction attempted"); |
|
703 return Matrix (); |
|
704 } |
3
|
705 |
|
706 if (nr == 0 || nc == 0) |
|
707 return Matrix (nr, nc); |
|
708 |
|
709 Matrix result (-a); |
238
|
710 for (int i = 0; i < m.length (); i++) |
|
711 result.elem (i, i) += m.elem (i, i); |
3
|
712 |
|
713 return result; |
|
714 } |
|
715 |
|
716 Matrix |
238
|
717 operator * (const DiagMatrix& m, const Matrix& a) |
3
|
718 { |
238
|
719 int nr = m.rows (); |
|
720 int nc = m.cols (); |
|
721 int a_nr = a.rows (); |
|
722 int a_nc = a.cols (); |
|
723 if (nc != a_nr) |
227
|
724 { |
|
725 (*current_liboctave_error_handler) |
|
726 ("nonconformant matrix multiplication attempted"); |
|
727 return Matrix (); |
|
728 } |
3
|
729 |
238
|
730 if (nr == 0 || nc == 0 || a_nc == 0) |
|
731 return Matrix (nr, a_nc, 0.0); |
3
|
732 |
238
|
733 Matrix c (nr, a_nc); |
3
|
734 |
238
|
735 for (int i = 0; i < m.length (); i++) |
3
|
736 { |
238
|
737 if (m.elem (i, i) == 1.0) |
3
|
738 { |
238
|
739 for (int j = 0; j < a_nc; j++) |
3
|
740 c.elem (i, j) = a.elem (i, j); |
|
741 } |
238
|
742 else if (m.elem (i, i) == 0.0) |
3
|
743 { |
238
|
744 for (int j = 0; j < a_nc; j++) |
3
|
745 c.elem (i, j) = 0.0; |
|
746 } |
|
747 else |
|
748 { |
238
|
749 for (int j = 0; j < a_nc; j++) |
|
750 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
3
|
751 } |
|
752 } |
|
753 |
|
754 if (nr > nc) |
|
755 { |
238
|
756 for (int j = 0; j < a_nc; j++) |
|
757 for (int i = a_nr; i < nr; i++) |
3
|
758 c.elem (i, j) = 0.0; |
|
759 } |
|
760 |
|
761 return c; |
|
762 } |
|
763 |
|
764 ComplexMatrix |
238
|
765 operator + (const DiagMatrix& m, const ComplexMatrix& a) |
3
|
766 { |
238
|
767 int nr = m.rows (); |
|
768 int nc = m.cols (); |
|
769 if (nr != a.rows () || nc != a.cols ()) |
227
|
770 { |
|
771 (*current_liboctave_error_handler) |
|
772 ("nonconformant matrix addition attempted"); |
|
773 return ComplexMatrix (); |
|
774 } |
3
|
775 |
|
776 if (nr == 0 || nc == 0) |
|
777 return ComplexMatrix (nr, nc); |
|
778 |
|
779 ComplexMatrix result (a); |
238
|
780 for (int i = 0; i < m.length (); i++) |
|
781 result.elem (i, i) += m.elem (i, i); |
3
|
782 |
|
783 return result; |
|
784 } |
|
785 |
|
786 ComplexMatrix |
238
|
787 operator - (const DiagMatrix& m, const ComplexMatrix& a) |
3
|
788 { |
238
|
789 int nr = m.rows (); |
|
790 int nc = m.cols (); |
|
791 if (nr != a.rows () || nc != a.cols ()) |
227
|
792 { |
|
793 (*current_liboctave_error_handler) |
|
794 ("nonconformant matrix subtraction attempted"); |
|
795 return ComplexMatrix (); |
|
796 } |
3
|
797 |
|
798 if (nr == 0 || nc == 0) |
|
799 return ComplexMatrix (nr, nc); |
|
800 |
|
801 ComplexMatrix result (-a); |
238
|
802 for (int i = 0; i < m.length (); i++) |
|
803 result.elem (i, i) += m.elem (i, i); |
3
|
804 |
|
805 return result; |
|
806 } |
|
807 |
|
808 ComplexMatrix |
238
|
809 operator * (const DiagMatrix& m, const ComplexMatrix& a) |
3
|
810 { |
238
|
811 int nr = m.rows (); |
|
812 int nc = m.cols (); |
|
813 int a_nr = a.rows (); |
|
814 int a_nc = a.cols (); |
|
815 if (nc != a_nr) |
227
|
816 { |
|
817 (*current_liboctave_error_handler) |
|
818 ("nonconformant matrix multiplication attempted"); |
|
819 return ComplexMatrix (); |
|
820 } |
3
|
821 |
238
|
822 if (nr == 0 || nc == 0 || a_nc == 0) |
3
|
823 return ComplexMatrix (nr, nc, 0.0); |
|
824 |
238
|
825 ComplexMatrix c (nr, a_nc); |
3
|
826 |
238
|
827 for (int i = 0; i < m.length (); i++) |
3
|
828 { |
238
|
829 if (m.elem (i, i) == 1.0) |
3
|
830 { |
238
|
831 for (int j = 0; j < a_nc; j++) |
3
|
832 c.elem (i, j) = a.elem (i, j); |
|
833 } |
238
|
834 else if (m.elem (i, i) == 0.0) |
3
|
835 { |
238
|
836 for (int j = 0; j < a_nc; j++) |
3
|
837 c.elem (i, j) = 0.0; |
|
838 } |
|
839 else |
|
840 { |
238
|
841 for (int j = 0; j < a_nc; j++) |
|
842 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
3
|
843 } |
|
844 } |
|
845 |
|
846 if (nr > nc) |
|
847 { |
238
|
848 for (int j = 0; j < a_nc; j++) |
|
849 for (int i = a_nr; i < nr; i++) |
3
|
850 c.elem (i, j) = 0.0; |
|
851 } |
|
852 |
|
853 return c; |
|
854 } |
|
855 |
238
|
856 // other operations |
3
|
857 |
|
858 ColumnVector |
|
859 DiagMatrix::diag (void) const |
|
860 { |
|
861 return diag (0); |
|
862 } |
|
863 |
|
864 // Could be optimized... |
|
865 |
|
866 ColumnVector |
|
867 DiagMatrix::diag (int k) const |
|
868 { |
238
|
869 int nnr = rows (); |
|
870 int nnc = cols (); |
3
|
871 if (k > 0) |
|
872 nnc -= k; |
|
873 else if (k < 0) |
|
874 nnr += k; |
|
875 |
|
876 ColumnVector d; |
|
877 |
|
878 if (nnr > 0 && nnc > 0) |
|
879 { |
|
880 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
881 |
|
882 d.resize (ndiag); |
|
883 |
|
884 if (k > 0) |
|
885 { |
|
886 for (int i = 0; i < ndiag; i++) |
|
887 d.elem (i) = elem (i, i+k); |
|
888 } |
|
889 else if ( k < 0) |
|
890 { |
|
891 for (int i = 0; i < ndiag; i++) |
|
892 d.elem (i) = elem (i-k, i); |
|
893 } |
|
894 else |
|
895 { |
|
896 for (int i = 0; i < ndiag; i++) |
|
897 d.elem (i) = elem (i, i); |
|
898 } |
|
899 } |
|
900 else |
|
901 cerr << "diag: requested diagonal out of range\n"; |
|
902 |
|
903 return d; |
|
904 } |
|
905 |
|
906 ostream& |
|
907 operator << (ostream& os, const DiagMatrix& a) |
|
908 { |
|
909 // int field_width = os.precision () + 7; |
238
|
910 for (int i = 0; i < a.rows (); i++) |
3
|
911 { |
238
|
912 for (int j = 0; j < a.cols (); j++) |
3
|
913 { |
|
914 if (i == j) |
238
|
915 os << " " /* setw (field_width) */ << a.elem (i, i); |
3
|
916 else |
238
|
917 os << " " /* setw (field_width) */ << 0.0; |
3
|
918 } |
|
919 os << "\n"; |
|
920 } |
|
921 return os; |
|
922 } |
|
923 |
|
924 /* |
|
925 * Complex Diagonal Matrix class |
|
926 */ |
|
927 |
238
|
928 ComplexDiagMatrix::ComplexDiagMatrix (const RowVector& a) |
|
929 : DiagArray<Complex> (a.length ()) |
3
|
930 { |
238
|
931 for (int i = 0; i < length (); i++) |
|
932 elem (i, i) = a.elem (i); |
3
|
933 } |
|
934 |
|
935 ComplexDiagMatrix::ComplexDiagMatrix (const ColumnVector& a) |
238
|
936 : DiagArray<Complex> (a.length ()) |
3
|
937 { |
238
|
938 for (int i = 0; i < length (); i++) |
|
939 elem (i, i) = a.elem (i); |
3
|
940 } |
|
941 |
|
942 ComplexDiagMatrix::ComplexDiagMatrix (const DiagMatrix& a) |
238
|
943 : DiagArray<Complex> (a.rows (), a.cols ()) |
3
|
944 { |
238
|
945 for (int i = 0; i < length (); i++) |
|
946 elem (i, i) = a.elem (i, i); |
3
|
947 } |
|
948 |
238
|
949 #if 0 |
3
|
950 ComplexDiagMatrix& |
|
951 ComplexDiagMatrix::resize (int r, int c) |
|
952 { |
|
953 if (r < 0 || c < 0) |
227
|
954 { |
|
955 (*current_liboctave_error_handler) |
|
956 ("can't resize to negative dimensions"); |
|
957 return *this; |
|
958 } |
3
|
959 |
|
960 int new_len = r < c ? r : c; |
|
961 Complex *new_data = (Complex *) NULL; |
|
962 if (new_len > 0) |
|
963 { |
|
964 new_data = new Complex [new_len]; |
|
965 |
|
966 int min_len = new_len < len ? new_len : len; |
|
967 |
|
968 for (int i = 0; i < min_len; i++) |
|
969 new_data[i] = data[i]; |
|
970 } |
|
971 |
|
972 delete [] data; |
|
973 nr = r; |
|
974 nc = c; |
|
975 len = new_len; |
|
976 data = new_data; |
|
977 |
|
978 return *this; |
|
979 } |
|
980 |
|
981 ComplexDiagMatrix& |
|
982 ComplexDiagMatrix::resize (int r, int c, double val) |
|
983 { |
|
984 if (r < 0 || c < 0) |
227
|
985 { |
|
986 (*current_liboctave_error_handler) |
|
987 ("can't resize to negative dimensions"); |
|
988 return *this; |
|
989 } |
3
|
990 |
|
991 int new_len = r < c ? r : c; |
|
992 Complex *new_data = (Complex *) NULL; |
|
993 if (new_len > 0) |
|
994 { |
|
995 new_data = new Complex [new_len]; |
|
996 |
|
997 int min_len = new_len < len ? new_len : len; |
|
998 |
|
999 for (int i = 0; i < min_len; i++) |
|
1000 new_data[i] = data[i]; |
|
1001 |
|
1002 for (i = min_len; i < new_len; i++) |
|
1003 new_data[i] = val; |
|
1004 } |
|
1005 |
|
1006 delete [] data; |
|
1007 nr = r; |
|
1008 nc = c; |
|
1009 len = new_len; |
|
1010 data = new_data; |
|
1011 |
|
1012 return *this; |
|
1013 } |
|
1014 |
|
1015 ComplexDiagMatrix& |
161
|
1016 ComplexDiagMatrix::resize (int r, int c, const Complex& val) |
3
|
1017 { |
|
1018 if (r < 0 || c < 0) |
227
|
1019 { |
|
1020 (*current_liboctave_error_handler) |
|
1021 ("can't resize to negative dimensions"); |
|
1022 return *this; |
|
1023 } |
3
|
1024 |
|
1025 int new_len = r < c ? r : c; |
|
1026 Complex *new_data = (Complex *) NULL; |
|
1027 if (new_len > 0) |
|
1028 { |
|
1029 new_data = new Complex [new_len]; |
|
1030 |
|
1031 int min_len = new_len < len ? new_len : len; |
|
1032 |
|
1033 for (int i = 0; i < min_len; i++) |
|
1034 new_data[i] = data[i]; |
|
1035 |
|
1036 for (i = min_len; i < new_len; i++) |
|
1037 new_data[i] = val; |
|
1038 } |
|
1039 |
|
1040 delete [] data; |
|
1041 nr = r; |
|
1042 nc = c; |
|
1043 len = new_len; |
|
1044 data = new_data; |
|
1045 |
|
1046 return *this; |
|
1047 } |
238
|
1048 #endif |
3
|
1049 |
|
1050 int |
|
1051 ComplexDiagMatrix::operator == (const ComplexDiagMatrix& a) const |
|
1052 { |
238
|
1053 if (rows () != a.rows () || cols () != a.cols ()) |
3
|
1054 return 0; |
|
1055 |
238
|
1056 return equal (data (), a.data (), length ()); |
3
|
1057 } |
|
1058 |
|
1059 int |
|
1060 ComplexDiagMatrix::operator != (const ComplexDiagMatrix& a) const |
|
1061 { |
238
|
1062 return !(*this == a); |
3
|
1063 } |
|
1064 |
|
1065 ComplexDiagMatrix |
|
1066 ComplexDiagMatrix::hermitian (void) const |
|
1067 { |
238
|
1068 return ComplexDiagMatrix (conj_dup (data (), length ()), cols (), rows ()); |
3
|
1069 } |
|
1070 |
|
1071 ComplexDiagMatrix& |
|
1072 ComplexDiagMatrix::fill (double val) |
|
1073 { |
238
|
1074 for (int i = 0; i < length (); i++) |
|
1075 elem (i, i) = val; |
3
|
1076 return *this; |
|
1077 } |
|
1078 |
|
1079 ComplexDiagMatrix& |
161
|
1080 ComplexDiagMatrix::fill (const Complex& val) |
3
|
1081 { |
238
|
1082 for (int i = 0; i < length (); i++) |
|
1083 elem (i, i) = val; |
3
|
1084 return *this; |
|
1085 } |
|
1086 |
|
1087 ComplexDiagMatrix& |
|
1088 ComplexDiagMatrix::fill (double val, int beg, int end) |
|
1089 { |
238
|
1090 if (beg < 0 || end >= length () || end < beg) |
227
|
1091 { |
|
1092 (*current_liboctave_error_handler) ("range error for fill"); |
|
1093 return *this; |
|
1094 } |
3
|
1095 |
238
|
1096 for (int i = beg; i < end; i++) |
|
1097 elem (i, i) = val; |
|
1098 |
3
|
1099 return *this; |
|
1100 } |
|
1101 |
|
1102 ComplexDiagMatrix& |
161
|
1103 ComplexDiagMatrix::fill (const Complex& val, int beg, int end) |
3
|
1104 { |
238
|
1105 if (beg < 0 || end >= length () || end < beg) |
227
|
1106 { |
|
1107 (*current_liboctave_error_handler) ("range error for fill"); |
|
1108 return *this; |
|
1109 } |
3
|
1110 |
238
|
1111 for (int i = beg; i < end; i++) |
|
1112 elem (i, i) = val; |
|
1113 |
3
|
1114 return *this; |
|
1115 } |
|
1116 |
|
1117 ComplexDiagMatrix& |
|
1118 ComplexDiagMatrix::fill (const ColumnVector& a) |
|
1119 { |
238
|
1120 int len = length (); |
|
1121 if (a.length () != len) |
227
|
1122 { |
|
1123 (*current_liboctave_error_handler) ("range error for fill"); |
|
1124 return *this; |
|
1125 } |
3
|
1126 |
238
|
1127 for (int i = 0; i < len; i++) |
|
1128 elem (i, i) = a.elem (i); |
|
1129 |
3
|
1130 return *this; |
|
1131 } |
|
1132 |
|
1133 ComplexDiagMatrix& |
|
1134 ComplexDiagMatrix::fill (const ComplexColumnVector& a) |
|
1135 { |
238
|
1136 int len = length (); |
|
1137 if (a.length () != len) |
227
|
1138 { |
|
1139 (*current_liboctave_error_handler) ("range error for fill"); |
|
1140 return *this; |
|
1141 } |
3
|
1142 |
238
|
1143 for (int i = 0; i < len; i++) |
|
1144 elem (i, i) = a.elem (i); |
|
1145 |
3
|
1146 return *this; |
|
1147 } |
|
1148 |
|
1149 ComplexDiagMatrix& |
|
1150 ComplexDiagMatrix::fill (const RowVector& a) |
|
1151 { |
238
|
1152 int len = length (); |
|
1153 if (a.length () != len) |
227
|
1154 { |
|
1155 (*current_liboctave_error_handler) ("range error for fill"); |
|
1156 return *this; |
|
1157 } |
3
|
1158 |
238
|
1159 for (int i = 0; i < len; i++) |
|
1160 elem (i, i) = a.elem (i); |
|
1161 |
3
|
1162 return *this; |
|
1163 } |
|
1164 |
|
1165 ComplexDiagMatrix& |
|
1166 ComplexDiagMatrix::fill (const ComplexRowVector& a) |
|
1167 { |
238
|
1168 int len = length (); |
|
1169 if (a.length () != len) |
227
|
1170 { |
|
1171 (*current_liboctave_error_handler) ("range error for fill"); |
|
1172 return *this; |
|
1173 } |
3
|
1174 |
238
|
1175 for (int i = 0; i < len; i++) |
|
1176 elem (i, i) = a.elem (i); |
|
1177 |
3
|
1178 return *this; |
|
1179 } |
|
1180 |
|
1181 ComplexDiagMatrix& |
|
1182 ComplexDiagMatrix::fill (const ColumnVector& a, int beg) |
|
1183 { |
238
|
1184 int a_len = a.length (); |
|
1185 if (beg < 0 || beg + a_len >= length ()) |
227
|
1186 { |
|
1187 (*current_liboctave_error_handler) ("range error for fill"); |
|
1188 return *this; |
|
1189 } |
3
|
1190 |
238
|
1191 for (int i = 0; i < a_len; i++) |
|
1192 elem (i+beg, i+beg) = a.elem (i); |
|
1193 |
3
|
1194 return *this; |
|
1195 } |
|
1196 |
|
1197 ComplexDiagMatrix& |
|
1198 ComplexDiagMatrix::fill (const ComplexColumnVector& a, int beg) |
|
1199 { |
238
|
1200 int a_len = a.length (); |
|
1201 if (beg < 0 || beg + a_len >= length ()) |
227
|
1202 { |
|
1203 (*current_liboctave_error_handler) ("range error for fill"); |
|
1204 return *this; |
|
1205 } |
3
|
1206 |
238
|
1207 for (int i = 0; i < a_len; i++) |
|
1208 elem (i+beg, i+beg) = a.elem (i); |
|
1209 |
3
|
1210 return *this; |
|
1211 } |
|
1212 |
|
1213 ComplexDiagMatrix& |
|
1214 ComplexDiagMatrix::fill (const RowVector& a, int beg) |
|
1215 { |
238
|
1216 int a_len = a.length (); |
|
1217 if (beg < 0 || beg + a_len >= length ()) |
227
|
1218 { |
|
1219 (*current_liboctave_error_handler) ("range error for fill"); |
|
1220 return *this; |
|
1221 } |
3
|
1222 |
238
|
1223 for (int i = 0; i < a_len; i++) |
|
1224 elem (i+beg, i+beg) = a.elem (i); |
|
1225 |
3
|
1226 return *this; |
|
1227 } |
|
1228 |
|
1229 ComplexDiagMatrix& |
|
1230 ComplexDiagMatrix::fill (const ComplexRowVector& a, int beg) |
|
1231 { |
238
|
1232 int a_len = a.length (); |
|
1233 if (beg < 0 || beg + a_len >= length ()) |
227
|
1234 { |
|
1235 (*current_liboctave_error_handler) ("range error for fill"); |
|
1236 return *this; |
|
1237 } |
3
|
1238 |
238
|
1239 for (int i = 0; i < a_len; i++) |
|
1240 elem (i+beg, i+beg) = a.elem (i); |
|
1241 |
3
|
1242 return *this; |
|
1243 } |
|
1244 |
|
1245 ComplexDiagMatrix |
|
1246 ComplexDiagMatrix::transpose (void) const |
|
1247 { |
238
|
1248 return ComplexDiagMatrix (dup (data (), length ()), cols (), rows ()); |
3
|
1249 } |
|
1250 |
|
1251 DiagMatrix |
|
1252 real (const ComplexDiagMatrix& a) |
|
1253 { |
|
1254 DiagMatrix retval; |
238
|
1255 int a_len = a.length (); |
|
1256 if (a_len > 0) |
|
1257 retval = DiagMatrix (real_dup (a.data (), a_len), a.rows (), |
|
1258 a.cols ()); |
3
|
1259 return retval; |
|
1260 } |
|
1261 |
|
1262 DiagMatrix |
|
1263 imag (const ComplexDiagMatrix& a) |
|
1264 { |
|
1265 DiagMatrix retval; |
238
|
1266 int a_len = a.length (); |
|
1267 if (a_len > 0) |
|
1268 retval = DiagMatrix (imag_dup (a.data (), a_len), a.rows (), |
|
1269 a.cols ()); |
3
|
1270 return retval; |
|
1271 } |
|
1272 |
|
1273 ComplexDiagMatrix |
|
1274 conj (const ComplexDiagMatrix& a) |
|
1275 { |
|
1276 ComplexDiagMatrix retval; |
238
|
1277 int a_len = a.length (); |
|
1278 if (a_len > 0) |
|
1279 retval = ComplexDiagMatrix (conj_dup (a.data (), a_len), |
|
1280 a.rows (), a.cols ()); |
3
|
1281 return retval; |
|
1282 } |
|
1283 |
|
1284 // resize is the destructive analog for this one |
|
1285 |
|
1286 ComplexMatrix |
|
1287 ComplexDiagMatrix::extract (int r1, int c1, int r2, int c2) const |
|
1288 { |
|
1289 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
1290 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
1291 |
|
1292 int new_r = r2 - r1 + 1; |
|
1293 int new_c = c2 - c1 + 1; |
|
1294 |
|
1295 ComplexMatrix result (new_r, new_c); |
|
1296 |
|
1297 for (int j = 0; j < new_c; j++) |
|
1298 for (int i = 0; i < new_r; i++) |
238
|
1299 result.elem (i, j) = elem (r1+i, c1+j); |
3
|
1300 |
|
1301 return result; |
|
1302 } |
|
1303 |
|
1304 // extract row or column i. |
|
1305 |
|
1306 ComplexRowVector |
|
1307 ComplexDiagMatrix::row (int i) const |
|
1308 { |
238
|
1309 int nr = rows (); |
|
1310 int nc = cols (); |
3
|
1311 if (i < 0 || i >= nr) |
227
|
1312 { |
|
1313 (*current_liboctave_error_handler) ("invalid row selection"); |
|
1314 return RowVector (); |
|
1315 } |
3
|
1316 |
|
1317 ComplexRowVector retval (nc, 0.0); |
192
|
1318 if (nr <= nc || (nr > nc && i < nc)) |
238
|
1319 retval.elem (i) = elem (i, i); |
3
|
1320 |
|
1321 return retval; |
|
1322 } |
|
1323 |
|
1324 ComplexRowVector |
|
1325 ComplexDiagMatrix::row (char *s) const |
|
1326 { |
|
1327 if (s == (char *) NULL) |
227
|
1328 { |
|
1329 (*current_liboctave_error_handler) ("invalid row selection"); |
|
1330 return ComplexRowVector (); |
|
1331 } |
3
|
1332 |
|
1333 char c = *s; |
|
1334 if (c == 'f' || c == 'F') |
|
1335 return row (0); |
|
1336 else if (c == 'l' || c == 'L') |
238
|
1337 return row (rows () - 1); |
3
|
1338 else |
227
|
1339 { |
|
1340 (*current_liboctave_error_handler) ("invalid row selection"); |
|
1341 return ComplexRowVector (); |
|
1342 } |
3
|
1343 } |
|
1344 |
|
1345 ComplexColumnVector |
|
1346 ComplexDiagMatrix::column (int i) const |
|
1347 { |
238
|
1348 int nr = rows (); |
|
1349 int nc = cols (); |
3
|
1350 if (i < 0 || i >= nc) |
227
|
1351 { |
|
1352 (*current_liboctave_error_handler) ("invalid column selection"); |
|
1353 return ColumnVector (); |
|
1354 } |
3
|
1355 |
|
1356 ComplexColumnVector retval (nr, 0.0); |
192
|
1357 if (nr >= nc || (nr < nc && i < nr)) |
238
|
1358 retval.elem (i) = elem (i, i); |
3
|
1359 |
|
1360 return retval; |
|
1361 } |
|
1362 |
|
1363 ComplexColumnVector |
|
1364 ComplexDiagMatrix::column (char *s) const |
|
1365 { |
|
1366 if (s == (char *) NULL) |
227
|
1367 { |
|
1368 (*current_liboctave_error_handler) ("invalid column selection"); |
|
1369 return ColumnVector (); |
|
1370 } |
3
|
1371 |
|
1372 char c = *s; |
|
1373 if (c == 'f' || c == 'F') |
|
1374 return column (0); |
|
1375 else if (c == 'l' || c == 'L') |
238
|
1376 return column (cols () - 1); |
3
|
1377 else |
227
|
1378 { |
|
1379 (*current_liboctave_error_handler) ("invalid column selection"); |
|
1380 return ColumnVector (); |
|
1381 } |
3
|
1382 } |
|
1383 |
|
1384 ComplexDiagMatrix |
|
1385 ComplexDiagMatrix::inverse (void) const |
|
1386 { |
|
1387 int info; |
|
1388 return inverse (info); |
|
1389 } |
|
1390 |
238
|
1391 ComplexDiagMatrix |
|
1392 ComplexDiagMatrix::inverse (int& info) const |
|
1393 { |
|
1394 int nr = rows (); |
|
1395 int nc = cols (); |
|
1396 if (nr != nc) |
|
1397 { |
|
1398 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
1399 return DiagMatrix (); |
|
1400 } |
|
1401 |
|
1402 ComplexDiagMatrix retval (nr, nc); |
|
1403 |
|
1404 info = 0; |
|
1405 for (int i = 0; i < length (); i++) |
|
1406 { |
|
1407 if (elem (i, i) == 0.0) |
|
1408 { |
|
1409 info = -1; |
|
1410 return *this; |
|
1411 } |
|
1412 else |
|
1413 retval.elem (i, i) = 1.0 / elem (i, i); |
|
1414 } |
|
1415 |
|
1416 return *this; |
|
1417 } |
|
1418 |
|
1419 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
1420 |
|
1421 ComplexDiagMatrix& |
|
1422 ComplexDiagMatrix::operator += (const DiagMatrix& a) |
|
1423 { |
|
1424 int nr = rows (); |
|
1425 int nc = cols (); |
|
1426 if (nr != a.rows () || nc != a.cols ()) |
|
1427 { |
|
1428 (*current_liboctave_error_handler) |
|
1429 ("nonconformant matrix += operation attempted"); |
|
1430 return *this; |
|
1431 } |
|
1432 |
|
1433 if (nr == 0 || nc == 0) |
|
1434 return *this; |
|
1435 |
|
1436 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1437 |
|
1438 add2 (d, a.data (), length ()); |
|
1439 return *this; |
|
1440 } |
|
1441 |
|
1442 ComplexDiagMatrix& |
|
1443 ComplexDiagMatrix::operator -= (const DiagMatrix& a) |
|
1444 { |
|
1445 int nr = rows (); |
|
1446 int nc = cols (); |
|
1447 if (nr != a.rows () || nc != a.cols ()) |
|
1448 { |
|
1449 (*current_liboctave_error_handler) |
|
1450 ("nonconformant matrix -= operation attempted"); |
|
1451 return *this; |
|
1452 } |
|
1453 |
|
1454 if (nr == 0 || nc == 0) |
|
1455 return *this; |
|
1456 |
|
1457 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1458 |
|
1459 subtract2 (d, a.data (), length ()); |
|
1460 return *this; |
|
1461 } |
|
1462 |
|
1463 ComplexDiagMatrix& |
|
1464 ComplexDiagMatrix::operator += (const ComplexDiagMatrix& a) |
|
1465 { |
|
1466 int nr = rows (); |
|
1467 int nc = cols (); |
|
1468 if (nr != a.rows () || nc != a.cols ()) |
|
1469 { |
|
1470 (*current_liboctave_error_handler) |
|
1471 ("nonconformant matrix += operation attempted"); |
|
1472 return *this; |
|
1473 } |
|
1474 |
|
1475 if (nr == 0 || nc == 0) |
|
1476 return *this; |
|
1477 |
|
1478 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1479 |
|
1480 add2 (d, a.data (), length ()); |
|
1481 return *this; |
|
1482 } |
|
1483 |
|
1484 ComplexDiagMatrix& |
|
1485 ComplexDiagMatrix::operator -= (const ComplexDiagMatrix& a) |
|
1486 { |
|
1487 int nr = rows (); |
|
1488 int nc = cols (); |
|
1489 if (nr != a.rows () || nc != a.cols ()) |
|
1490 { |
|
1491 (*current_liboctave_error_handler) |
|
1492 ("nonconformant matrix -= operation attempted"); |
|
1493 return *this; |
|
1494 } |
|
1495 |
|
1496 if (nr == 0 || nc == 0) |
|
1497 return *this; |
|
1498 |
|
1499 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1500 |
|
1501 subtract2 (d, a.data (), length ()); |
|
1502 return *this; |
|
1503 } |
|
1504 |
3
|
1505 // diagonal matrix by scalar -> matrix operations |
|
1506 |
|
1507 ComplexMatrix |
238
|
1508 operator + (const ComplexDiagMatrix& a, double s) |
3
|
1509 { |
238
|
1510 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1511 return a + tmp; |
3
|
1512 } |
|
1513 |
|
1514 ComplexMatrix |
238
|
1515 operator - (const ComplexDiagMatrix& a, double s) |
3
|
1516 { |
238
|
1517 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1518 return a + tmp; |
3
|
1519 } |
|
1520 |
|
1521 ComplexMatrix |
238
|
1522 operator + (const ComplexDiagMatrix& a, const Complex& s) |
3
|
1523 { |
238
|
1524 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1525 return a + tmp; |
3
|
1526 } |
|
1527 |
|
1528 ComplexMatrix |
238
|
1529 operator - (const ComplexDiagMatrix& a, const Complex& s) |
3
|
1530 { |
238
|
1531 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1532 return a + tmp; |
3
|
1533 } |
|
1534 |
|
1535 // diagonal matrix by scalar -> diagonal matrix operations |
|
1536 |
|
1537 ComplexDiagMatrix |
238
|
1538 operator * (const ComplexDiagMatrix& a, double s) |
3
|
1539 { |
238
|
1540 return ComplexDiagMatrix (multiply (a.data (), a.length (), s), |
|
1541 a.rows (), a.cols ()); |
3
|
1542 } |
|
1543 |
|
1544 ComplexDiagMatrix |
238
|
1545 operator / (const ComplexDiagMatrix& a, double s) |
3
|
1546 { |
238
|
1547 return ComplexDiagMatrix (divide (a.data (), a.length (), s), |
|
1548 a.rows (), a.cols ()); |
3
|
1549 } |
|
1550 |
|
1551 // scalar by diagonal matrix -> matrix operations |
|
1552 |
|
1553 ComplexMatrix |
|
1554 operator + (double s, const ComplexDiagMatrix& a) |
|
1555 { |
238
|
1556 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1557 return tmp + a; |
3
|
1558 } |
|
1559 |
|
1560 ComplexMatrix |
|
1561 operator - (double s, const ComplexDiagMatrix& a) |
|
1562 { |
238
|
1563 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1564 return tmp - a; |
3
|
1565 } |
|
1566 |
|
1567 ComplexMatrix |
161
|
1568 operator + (const Complex& s, const ComplexDiagMatrix& a) |
3
|
1569 { |
238
|
1570 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1571 return tmp + a; |
3
|
1572 } |
|
1573 |
|
1574 ComplexMatrix |
161
|
1575 operator - (const Complex& s, const ComplexDiagMatrix& a) |
3
|
1576 { |
238
|
1577 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1578 return tmp - a; |
3
|
1579 } |
|
1580 |
|
1581 // scalar by diagonal matrix -> diagonal matrix operations |
|
1582 |
|
1583 ComplexDiagMatrix |
|
1584 operator * (double s, const ComplexDiagMatrix& a) |
|
1585 { |
238
|
1586 return ComplexDiagMatrix (multiply (a.data (), a.length (), s), |
|
1587 a.rows (), a.cols ()); |
3
|
1588 } |
|
1589 |
|
1590 // diagonal matrix by column vector -> column vector operations |
|
1591 |
|
1592 ComplexColumnVector |
238
|
1593 operator * (const ComplexDiagMatrix& m, const ColumnVector& a) |
3
|
1594 { |
238
|
1595 int nr = m.rows (); |
|
1596 int nc = m.cols (); |
|
1597 int a_len = a.length (); |
|
1598 if (nc != a_len) |
227
|
1599 { |
|
1600 (*current_liboctave_error_handler) |
|
1601 ("nonconformant matrix muliplication attempted"); |
|
1602 return ComplexColumnVector (); |
|
1603 } |
3
|
1604 |
|
1605 if (nc == 0 || nr == 0) |
|
1606 return ComplexColumnVector (0); |
|
1607 |
|
1608 ComplexColumnVector result (nr); |
|
1609 |
238
|
1610 for (int i = 0; i < a_len; i++) |
|
1611 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
1612 |
|
1613 for (i = a_len; i < nr; i++) |
|
1614 result.elem (i) = 0.0; |
|
1615 |
|
1616 return result; |
|
1617 } |
3
|
1618 |
238
|
1619 ComplexColumnVector |
|
1620 operator * (const ComplexDiagMatrix& m, const ComplexColumnVector& a) |
|
1621 { |
|
1622 int nr = m.rows (); |
|
1623 int nc = m.cols (); |
|
1624 int a_len = a.length (); |
|
1625 if (nc != a_len) |
|
1626 { |
|
1627 (*current_liboctave_error_handler) |
|
1628 ("nonconformant matrix muliplication attempted"); |
|
1629 return ComplexColumnVector (); |
|
1630 } |
|
1631 |
|
1632 if (nc == 0 || nr == 0) |
|
1633 return ComplexColumnVector (0); |
|
1634 |
|
1635 ComplexColumnVector result (nr); |
|
1636 |
|
1637 for (int i = 0; i < a_len; i++) |
|
1638 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
1639 |
|
1640 for (i = a_len; i < nr; i++) |
|
1641 result.elem (i) = 0.0; |
3
|
1642 |
|
1643 return result; |
|
1644 } |
|
1645 |
|
1646 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
1647 |
|
1648 ComplexDiagMatrix |
378
|
1649 operator * (const ComplexDiagMatrix& a, const ComplexDiagMatrix& b) |
|
1650 { |
|
1651 int nr_a = a.rows (); |
|
1652 int nc_a = a.cols (); |
|
1653 int nr_b = b.rows (); |
|
1654 int nc_b = b.cols (); |
|
1655 if (nc_a != nr_b) |
|
1656 { |
|
1657 (*current_liboctave_error_handler) |
|
1658 ("nonconformant matrix multiplication attempted"); |
|
1659 return ComplexDiagMatrix (); |
|
1660 } |
|
1661 |
|
1662 if (nr_a == 0 || nc_a == 0 || nc_b == 0) |
|
1663 return ComplexDiagMatrix (nr_a, nc_a, 0.0); |
|
1664 |
|
1665 ComplexDiagMatrix c (nr_a, nc_b); |
|
1666 |
|
1667 int len = nr_a < nc_b ? nr_a : nc_b; |
|
1668 |
|
1669 for (int i = 0; i < len; i++) |
|
1670 { |
|
1671 Complex a_element = a.elem (i, i); |
|
1672 Complex b_element = b.elem (i, i); |
|
1673 |
|
1674 if (a_element == 0.0 || b_element == 0.0) |
|
1675 c.elem (i, i) = 0.0; |
|
1676 else if (a_element == 1.0) |
|
1677 c.elem (i, i) = b_element; |
|
1678 else if (b_element == 1.0) |
|
1679 c.elem (i, i) = a_element; |
|
1680 else |
|
1681 c.elem (i, i) = a_element * b_element; |
|
1682 } |
|
1683 |
|
1684 return c; |
|
1685 } |
|
1686 |
|
1687 ComplexDiagMatrix |
238
|
1688 operator + (const ComplexDiagMatrix& m, const DiagMatrix& a) |
3
|
1689 { |
238
|
1690 int nr = m.rows (); |
|
1691 int nc = m.cols (); |
|
1692 if (nr != a.rows () || nc != a.cols ()) |
227
|
1693 { |
|
1694 (*current_liboctave_error_handler) |
|
1695 ("nonconformant matrix addition attempted"); |
|
1696 return ComplexDiagMatrix (); |
|
1697 } |
3
|
1698 |
|
1699 if (nr == 0 || nc == 0) |
|
1700 return ComplexDiagMatrix (nr, nc); |
|
1701 |
238
|
1702 return ComplexDiagMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
3
|
1703 } |
|
1704 |
|
1705 ComplexDiagMatrix |
238
|
1706 operator - (const ComplexDiagMatrix& m, const DiagMatrix& a) |
3
|
1707 { |
238
|
1708 int nr = m.rows (); |
|
1709 int nc = m.cols (); |
|
1710 if (nr != a.rows () || nc != a.cols ()) |
227
|
1711 { |
|
1712 (*current_liboctave_error_handler) |
|
1713 ("nonconformant matrix subtraction attempted"); |
|
1714 return ComplexDiagMatrix (); |
|
1715 } |
3
|
1716 |
|
1717 if (nr == 0 || nc == 0) |
|
1718 return ComplexDiagMatrix (nr, nc); |
|
1719 |
238
|
1720 return ComplexDiagMatrix (subtract (m.data (), a.data (), m.length ()), |
|
1721 nr, nc); |
3
|
1722 } |
|
1723 |
|
1724 ComplexDiagMatrix |
378
|
1725 operator * (const ComplexDiagMatrix& a, const DiagMatrix& b) |
|
1726 { |
|
1727 int nr_a = a.rows (); |
|
1728 int nc_a = a.cols (); |
|
1729 int nr_b = b.rows (); |
|
1730 int nc_b = b.cols (); |
|
1731 if (nc_a != nr_b) |
|
1732 { |
|
1733 (*current_liboctave_error_handler) |
|
1734 ("nonconformant matrix multiplication attempted"); |
|
1735 return ComplexDiagMatrix (); |
|
1736 } |
|
1737 |
|
1738 if (nr_a == 0 || nc_a == 0 || nc_b == 0) |
|
1739 return ComplexDiagMatrix (nr_a, nc_a, 0.0); |
|
1740 |
|
1741 ComplexDiagMatrix c (nr_a, nc_b); |
|
1742 |
|
1743 int len = nr_a < nc_b ? nr_a : nc_b; |
|
1744 |
|
1745 for (int i = 0; i < len; i++) |
|
1746 { |
|
1747 Complex a_element = a.elem (i, i); |
|
1748 double b_element = b.elem (i, i); |
|
1749 |
|
1750 if (a_element == 0.0 || b_element == 0.0) |
|
1751 c.elem (i, i) = 0.0; |
|
1752 else if (a_element == 1.0) |
|
1753 c.elem (i, i) = b_element; |
|
1754 else if (b_element == 1.0) |
|
1755 c.elem (i, i) = a_element; |
|
1756 else |
|
1757 c.elem (i, i) = a_element * b_element; |
|
1758 } |
|
1759 |
|
1760 return c; |
|
1761 } |
|
1762 |
|
1763 ComplexDiagMatrix |
238
|
1764 product (const ComplexDiagMatrix& m, const DiagMatrix& a) |
3
|
1765 { |
238
|
1766 int nr = m.rows (); |
|
1767 int nc = m.cols (); |
|
1768 if (nr != a.rows () || nc != a.cols ()) |
227
|
1769 { |
|
1770 (*current_liboctave_error_handler) |
|
1771 ("nonconformant matrix product attempted"); |
|
1772 return ComplexDiagMatrix (); |
|
1773 } |
3
|
1774 |
|
1775 if (nr == 0 || nc == 0) |
|
1776 return ComplexDiagMatrix (nr, nc); |
|
1777 |
238
|
1778 return ComplexDiagMatrix (multiply (m.data (), a.data (), m.length ()), |
|
1779 nr, nc); |
3
|
1780 } |
|
1781 |
|
1782 // diagonal matrix by matrix -> matrix operations |
|
1783 |
|
1784 ComplexMatrix |
238
|
1785 operator + (const ComplexDiagMatrix& m, const Matrix& a) |
3
|
1786 { |
238
|
1787 int nr = m.rows (); |
|
1788 int nc = m.cols (); |
|
1789 if (nr != a.rows () || nc != a.cols ()) |
227
|
1790 { |
|
1791 (*current_liboctave_error_handler) |
|
1792 ("nonconformant matrix addition attempted"); |
|
1793 return ComplexMatrix (); |
|
1794 } |
3
|
1795 |
|
1796 if (nr == 0 || nc == 0) |
|
1797 return ComplexMatrix (nr, nc); |
|
1798 |
|
1799 ComplexMatrix result (a); |
238
|
1800 for (int i = 0; i < m.length (); i++) |
|
1801 result.elem (i, i) += m.elem (i, i); |
3
|
1802 |
|
1803 return result; |
|
1804 } |
|
1805 |
|
1806 ComplexMatrix |
238
|
1807 operator - (const ComplexDiagMatrix& m, const Matrix& a) |
3
|
1808 { |
238
|
1809 int nr = m.rows (); |
|
1810 int nc = m.cols (); |
|
1811 if (nr != a.rows () || nc != a.cols ()) |
227
|
1812 { |
|
1813 (*current_liboctave_error_handler) |
|
1814 ("nonconformant matrix subtraction attempted"); |
|
1815 return ComplexMatrix (); |
|
1816 } |
3
|
1817 |
|
1818 if (nr == 0 || nc == 0) |
|
1819 return ComplexMatrix (nr, nc); |
|
1820 |
|
1821 ComplexMatrix result (-a); |
238
|
1822 for (int i = 0; i < m.length (); i++) |
|
1823 result.elem (i, i) += m.elem (i, i); |
3
|
1824 |
|
1825 return result; |
|
1826 } |
|
1827 |
|
1828 ComplexMatrix |
238
|
1829 operator * (const ComplexDiagMatrix& m, const Matrix& a) |
3
|
1830 { |
238
|
1831 int nr = m.rows (); |
|
1832 int nc = m.cols (); |
|
1833 int a_nr = a.rows (); |
|
1834 int a_nc = a.cols (); |
|
1835 if (nc != a_nr) |
227
|
1836 { |
|
1837 (*current_liboctave_error_handler) |
|
1838 ("nonconformant matrix multiplication attempted"); |
|
1839 return ComplexMatrix (); |
|
1840 } |
3
|
1841 |
238
|
1842 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1843 return ComplexMatrix (nr, a_nc, 0.0); |
3
|
1844 |
238
|
1845 ComplexMatrix c (nr, a_nc); |
3
|
1846 |
238
|
1847 for (int i = 0; i < m.length (); i++) |
3
|
1848 { |
238
|
1849 if (m.elem (i, i) == 1.0) |
3
|
1850 { |
238
|
1851 for (int j = 0; j < a_nc; j++) |
3
|
1852 c.elem (i, j) = a.elem (i, j); |
|
1853 } |
238
|
1854 else if (m.elem (i, i) == 0.0) |
3
|
1855 { |
238
|
1856 for (int j = 0; j < a_nc; j++) |
3
|
1857 c.elem (i, j) = 0.0; |
|
1858 } |
|
1859 else |
|
1860 { |
238
|
1861 for (int j = 0; j < a_nc; j++) |
|
1862 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
3
|
1863 } |
|
1864 } |
|
1865 |
|
1866 if (nr > nc) |
|
1867 { |
238
|
1868 for (int j = 0; j < a_nc; j++) |
|
1869 for (int i = a_nr; i < nr; i++) |
3
|
1870 c.elem (i, j) = 0.0; |
|
1871 } |
|
1872 |
|
1873 return c; |
|
1874 } |
|
1875 |
|
1876 ComplexMatrix |
238
|
1877 operator + (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
3
|
1878 { |
238
|
1879 int nr = m.rows (); |
|
1880 int nc = m.cols (); |
|
1881 if (nr != a.rows () || nc != a.cols ()) |
227
|
1882 { |
|
1883 (*current_liboctave_error_handler) |
|
1884 ("nonconformant matrix addition attempted"); |
|
1885 return ComplexMatrix (); |
|
1886 } |
3
|
1887 |
|
1888 if (nr == 0 || nc == 0) |
|
1889 return ComplexMatrix (nr, nc); |
|
1890 |
|
1891 ComplexMatrix result (a); |
238
|
1892 for (int i = 0; i < m.length (); i++) |
|
1893 result.elem (i, i) += m.elem (i, i); |
3
|
1894 |
|
1895 return result; |
|
1896 } |
|
1897 |
|
1898 ComplexMatrix |
238
|
1899 operator - (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
3
|
1900 { |
238
|
1901 int nr = m.rows (); |
|
1902 int nc = m.cols (); |
|
1903 if (nr != a.rows () || nc != a.cols ()) |
227
|
1904 { |
|
1905 (*current_liboctave_error_handler) |
|
1906 ("nonconformant matrix subtraction attempted"); |
|
1907 return ComplexMatrix (); |
|
1908 } |
3
|
1909 |
|
1910 if (nr == 0 || nc == 0) |
|
1911 return ComplexMatrix (nr, nc); |
|
1912 |
|
1913 ComplexMatrix result (-a); |
238
|
1914 for (int i = 0; i < m.length (); i++) |
|
1915 result.elem (i, i) += m.elem (i, i); |
3
|
1916 |
|
1917 return result; |
|
1918 } |
|
1919 |
|
1920 ComplexMatrix |
238
|
1921 operator * (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
3
|
1922 { |
238
|
1923 int nr = m.rows (); |
|
1924 int nc = m.cols (); |
|
1925 int a_nr = a.rows (); |
|
1926 int a_nc = a.cols (); |
|
1927 if (nc != a_nr) |
227
|
1928 { |
|
1929 (*current_liboctave_error_handler) |
|
1930 ("nonconformant matrix multiplication attempted"); |
|
1931 return ComplexMatrix (); |
|
1932 } |
3
|
1933 |
238
|
1934 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1935 return ComplexMatrix (nr, a_nc, 0.0); |
3
|
1936 |
238
|
1937 ComplexMatrix c (nr, a_nc); |
3
|
1938 |
238
|
1939 for (int i = 0; i < m.length (); i++) |
3
|
1940 { |
238
|
1941 if (m.elem (i, i) == 1.0) |
3
|
1942 { |
238
|
1943 for (int j = 0; j < a_nc; j++) |
3
|
1944 c.elem (i, j) = a.elem (i, j); |
|
1945 } |
238
|
1946 else if (m.elem (i, i) == 0.0) |
3
|
1947 { |
238
|
1948 for (int j = 0; j < a_nc; j++) |
3
|
1949 c.elem (i, j) = 0.0; |
|
1950 } |
|
1951 else |
|
1952 { |
238
|
1953 for (int j = 0; j < a_nc; j++) |
|
1954 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
3
|
1955 } |
|
1956 } |
|
1957 |
|
1958 if (nr > nc) |
|
1959 { |
238
|
1960 for (int j = 0; j < a_nc; j++) |
|
1961 for (int i = a_nr; i < nr; i++) |
3
|
1962 c.elem (i, j) = 0.0; |
|
1963 } |
|
1964 |
|
1965 return c; |
|
1966 } |
|
1967 |
238
|
1968 // other operations |
3
|
1969 |
|
1970 ComplexColumnVector |
|
1971 ComplexDiagMatrix::diag (void) const |
|
1972 { |
|
1973 return diag (0); |
|
1974 } |
|
1975 |
|
1976 // Could be optimized... |
|
1977 |
|
1978 ComplexColumnVector |
|
1979 ComplexDiagMatrix::diag (int k) const |
|
1980 { |
238
|
1981 int nnr = rows (); |
|
1982 int nnc = cols (); |
3
|
1983 if (k > 0) |
|
1984 nnc -= k; |
|
1985 else if (k < 0) |
|
1986 nnr += k; |
|
1987 |
|
1988 ComplexColumnVector d; |
|
1989 |
|
1990 if (nnr > 0 && nnc > 0) |
|
1991 { |
|
1992 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
1993 |
|
1994 d.resize (ndiag); |
|
1995 |
|
1996 if (k > 0) |
|
1997 { |
|
1998 for (int i = 0; i < ndiag; i++) |
|
1999 d.elem (i) = elem (i, i+k); |
|
2000 } |
|
2001 else if ( k < 0) |
|
2002 { |
|
2003 for (int i = 0; i < ndiag; i++) |
|
2004 d.elem (i) = elem (i-k, i); |
|
2005 } |
|
2006 else |
|
2007 { |
|
2008 for (int i = 0; i < ndiag; i++) |
|
2009 d.elem (i) = elem (i, i); |
|
2010 } |
|
2011 } |
|
2012 else |
|
2013 cerr << "diag: requested diagonal out of range\n"; |
|
2014 |
|
2015 return d; |
|
2016 } |
|
2017 |
|
2018 // i/o |
|
2019 |
|
2020 ostream& |
|
2021 operator << (ostream& os, const ComplexDiagMatrix& a) |
|
2022 { |
|
2023 Complex ZERO (0.0); |
|
2024 // int field_width = os.precision () + 7; |
238
|
2025 for (int i = 0; i < a.rows (); i++) |
3
|
2026 { |
238
|
2027 for (int j = 0; j < a.cols (); j++) |
3
|
2028 { |
|
2029 if (i == j) |
238
|
2030 os << " " /* setw (field_width) */ << a.elem (i, i); |
3
|
2031 else |
192
|
2032 os << " " /* setw (field_width) */ << ZERO; |
3
|
2033 } |
|
2034 os << "\n"; |
|
2035 } |
|
2036 return os; |
|
2037 } |
|
2038 |
|
2039 /* |
|
2040 ;;; Local Variables: *** |
|
2041 ;;; mode: C++ *** |
|
2042 ;;; page-delimiter: "^/\\*" *** |
|
2043 ;;; End: *** |
|
2044 */ |