458
|
1 // Matrix manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 John W. Eaton |
458
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
458
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
458
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
458
|
30 #endif |
|
31 |
1367
|
32 #include <cfloat> |
|
33 |
458
|
34 #include <iostream.h> |
1367
|
35 |
|
36 #include <sys/types.h> // XXX FIXME XXX |
458
|
37 |
1819
|
38 #include "CmplxAEPBAL.h" |
458
|
39 #include "CmplxDET.h" |
1819
|
40 #include "CmplxSCHUR.h" |
740
|
41 #include "CmplxSVD.h" |
1847
|
42 #include "f77-fcn.h" |
458
|
43 #include "lo-error.h" |
1968
|
44 #include "lo-utils.h" |
1367
|
45 #include "mx-base.h" |
|
46 #include "mx-inlines.cc" |
1650
|
47 #include "oct-cmplx.h" |
458
|
48 |
|
49 // Fortran functions we call. |
|
50 |
|
51 extern "C" |
|
52 { |
1253
|
53 int F77_FCN (zgemm, ZGEMM) (const char*, const char*, const int&, |
|
54 const int&, const int&, const Complex&, |
|
55 const Complex*, const int&, |
|
56 const Complex*, const int&, |
|
57 const Complex&, Complex*, const int&, |
|
58 long, long); |
|
59 |
|
60 int F77_FCN (zgeco, ZGECO) (Complex*, const int&, const int&, int*, |
|
61 double&, Complex*); |
|
62 |
|
63 int F77_FCN (zgedi, ZGEDI) (Complex*, const int&, const int&, int*, |
|
64 Complex*, Complex*, const int&); |
|
65 |
|
66 int F77_FCN (zgesl, ZGESL) (Complex*, const int&, const int&, int*, |
|
67 Complex*, const int&); |
|
68 |
|
69 int F77_FCN (zgelss, ZGELSS) (const int&, const int&, const int&, |
|
70 Complex*, const int&, Complex*, |
|
71 const int&, double*, double&, int&, |
|
72 Complex*, const int&, double*, int&); |
458
|
73 |
1360
|
74 // Note that the original complex fft routines were not written for |
|
75 // double complex arguments. They have been modified by adding an |
|
76 // implicit double precision (a-h,o-z) statement at the beginning of |
|
77 // each subroutine. |
458
|
78 |
1253
|
79 int F77_FCN (cffti, CFFTI) (const int&, Complex*); |
|
80 |
|
81 int F77_FCN (cfftf, CFFTF) (const int&, Complex*, Complex*); |
|
82 |
|
83 int F77_FCN (cfftb, CFFTB) (const int&, Complex*, Complex*); |
1819
|
84 |
|
85 int F77_FCN (zlartg, ZLARTG) (const Complex&, const Complex&, |
|
86 double&, Complex&, Complex&); |
|
87 |
|
88 int F77_FCN (ztrsyl, ZTRSYL) (const char*, const char*, const int&, |
|
89 const int&, const int&, |
|
90 const Complex*, const int&, |
|
91 const Complex*, const int&, |
|
92 const Complex*, const int&, double&, |
|
93 int&, long, long); |
|
94 |
|
95 double F77_FCN (zlange, ZLANGE) (const char*, const int&, |
|
96 const int&, const Complex*, |
|
97 const int&, double*); |
458
|
98 } |
|
99 |
1360
|
100 // Complex Matrix class |
458
|
101 |
|
102 ComplexMatrix::ComplexMatrix (const Matrix& a) |
1214
|
103 : MArray2<Complex> (a.rows (), a.cols ()) |
458
|
104 { |
|
105 for (int j = 0; j < cols (); j++) |
|
106 for (int i = 0; i < rows (); i++) |
|
107 elem (i, j) = a.elem (i, j); |
|
108 } |
|
109 |
|
110 ComplexMatrix::ComplexMatrix (const DiagMatrix& a) |
1214
|
111 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
458
|
112 { |
|
113 for (int i = 0; i < a.length (); i++) |
|
114 elem (i, i) = a.elem (i, i); |
|
115 } |
|
116 |
|
117 ComplexMatrix::ComplexMatrix (const ComplexDiagMatrix& a) |
1214
|
118 : MArray2<Complex> (a.rows (), a.cols (), 0.0) |
458
|
119 { |
|
120 for (int i = 0; i < a.length (); i++) |
|
121 elem (i, i) = a.elem (i, i); |
|
122 } |
|
123 |
1574
|
124 // XXX FIXME XXX -- could we use a templated mixed-type copy function |
|
125 // here? |
|
126 |
|
127 ComplexMatrix::ComplexMatrix (const charMatrix& a) |
|
128 { |
|
129 for (int i = 0; i < a.cols (); i++) |
|
130 for (int j = 0; j < a.rows (); j++) |
|
131 elem (i, j) = a.elem (i, j); |
|
132 } |
|
133 |
458
|
134 int |
|
135 ComplexMatrix::operator == (const ComplexMatrix& a) const |
|
136 { |
|
137 if (rows () != a.rows () || cols () != a.cols ()) |
|
138 return 0; |
|
139 |
|
140 return equal (data (), a.data (), length ()); |
|
141 } |
|
142 |
|
143 int |
|
144 ComplexMatrix::operator != (const ComplexMatrix& a) const |
|
145 { |
|
146 return !(*this == a); |
|
147 } |
|
148 |
|
149 // destructive insert/delete/reorder operations |
|
150 |
|
151 ComplexMatrix& |
|
152 ComplexMatrix::insert (const Matrix& a, int r, int c) |
|
153 { |
|
154 int a_nr = a.rows (); |
|
155 int a_nc = a.cols (); |
1699
|
156 |
|
157 if (r < 0 || r + a_nr > rows () || c < 0 || c + a_nc > cols ()) |
458
|
158 { |
|
159 (*current_liboctave_error_handler) ("range error for insert"); |
|
160 return *this; |
|
161 } |
|
162 |
|
163 for (int j = 0; j < a_nc; j++) |
|
164 for (int i = 0; i < a_nr; i++) |
|
165 elem (r+i, c+j) = a.elem (i, j); |
|
166 |
|
167 return *this; |
|
168 } |
|
169 |
|
170 ComplexMatrix& |
|
171 ComplexMatrix::insert (const RowVector& a, int r, int c) |
|
172 { |
|
173 int a_len = a.length (); |
1699
|
174 if (r < 0 || r >= rows () || c < 0 || c + a_len > cols ()) |
458
|
175 { |
|
176 (*current_liboctave_error_handler) ("range error for insert"); |
|
177 return *this; |
|
178 } |
|
179 |
|
180 for (int i = 0; i < a_len; i++) |
|
181 elem (r, c+i) = a.elem (i); |
|
182 |
|
183 return *this; |
|
184 } |
|
185 |
|
186 ComplexMatrix& |
|
187 ComplexMatrix::insert (const ColumnVector& a, int r, int c) |
|
188 { |
|
189 int a_len = a.length (); |
1699
|
190 if (r < 0 || r + a_len > rows () || c < 0 || c >= cols ()) |
458
|
191 { |
|
192 (*current_liboctave_error_handler) ("range error for insert"); |
|
193 return *this; |
|
194 } |
|
195 |
|
196 for (int i = 0; i < a_len; i++) |
|
197 elem (r+i, c) = a.elem (i); |
|
198 |
|
199 return *this; |
|
200 } |
|
201 |
|
202 ComplexMatrix& |
|
203 ComplexMatrix::insert (const DiagMatrix& a, int r, int c) |
|
204 { |
1699
|
205 int a_nr = a.rows (); |
|
206 int a_nc = a.cols (); |
|
207 |
|
208 if (r < 0 || r + a_nr > rows () || c < 0 || c + a_nc > cols ()) |
458
|
209 { |
|
210 (*current_liboctave_error_handler) ("range error for insert"); |
|
211 return *this; |
|
212 } |
|
213 |
1699
|
214 fill (0.0, r, c, r + a_nr - 1, c + a_nc - 1); |
|
215 |
458
|
216 for (int i = 0; i < a.length (); i++) |
|
217 elem (r+i, c+i) = a.elem (i, i); |
|
218 |
|
219 return *this; |
|
220 } |
|
221 |
|
222 ComplexMatrix& |
|
223 ComplexMatrix::insert (const ComplexMatrix& a, int r, int c) |
|
224 { |
1561
|
225 Array2<Complex>::insert (a, r, c); |
458
|
226 return *this; |
|
227 } |
|
228 |
|
229 ComplexMatrix& |
|
230 ComplexMatrix::insert (const ComplexRowVector& a, int r, int c) |
|
231 { |
|
232 int a_len = a.length (); |
1699
|
233 if (r < 0 || r >= rows () || c < 0 || c + a_len > cols ()) |
458
|
234 { |
|
235 (*current_liboctave_error_handler) ("range error for insert"); |
|
236 return *this; |
|
237 } |
|
238 |
|
239 for (int i = 0; i < a_len; i++) |
|
240 elem (r, c+i) = a.elem (i); |
|
241 |
|
242 return *this; |
|
243 } |
|
244 |
|
245 ComplexMatrix& |
|
246 ComplexMatrix::insert (const ComplexColumnVector& a, int r, int c) |
|
247 { |
|
248 int a_len = a.length (); |
1699
|
249 if (r < 0 || r + a_len > rows () || c < 0 || c >= cols ()) |
458
|
250 { |
|
251 (*current_liboctave_error_handler) ("range error for insert"); |
|
252 return *this; |
|
253 } |
|
254 |
|
255 for (int i = 0; i < a_len; i++) |
|
256 elem (r+i, c) = a.elem (i); |
|
257 |
|
258 return *this; |
|
259 } |
|
260 |
|
261 ComplexMatrix& |
|
262 ComplexMatrix::insert (const ComplexDiagMatrix& a, int r, int c) |
|
263 { |
1699
|
264 int a_nr = a.rows (); |
|
265 int a_nc = a.cols (); |
|
266 |
|
267 if (r < 0 || r + a_nr > rows () || c < 0 || c + a_nc > cols ()) |
458
|
268 { |
|
269 (*current_liboctave_error_handler) ("range error for insert"); |
|
270 return *this; |
|
271 } |
|
272 |
1699
|
273 fill (0.0, r, c, r + a_nr - 1, c + a_nc - 1); |
|
274 |
458
|
275 for (int i = 0; i < a.length (); i++) |
|
276 elem (r+i, c+i) = a.elem (i, i); |
|
277 |
|
278 return *this; |
|
279 } |
|
280 |
|
281 ComplexMatrix& |
|
282 ComplexMatrix::fill (double val) |
|
283 { |
|
284 int nr = rows (); |
|
285 int nc = cols (); |
|
286 if (nr > 0 && nc > 0) |
|
287 for (int j = 0; j < nc; j++) |
|
288 for (int i = 0; i < nr; i++) |
|
289 elem (i, j) = val; |
|
290 |
|
291 return *this; |
|
292 } |
|
293 |
|
294 ComplexMatrix& |
|
295 ComplexMatrix::fill (const Complex& val) |
|
296 { |
|
297 int nr = rows (); |
|
298 int nc = cols (); |
|
299 if (nr > 0 && nc > 0) |
|
300 for (int j = 0; j < nc; j++) |
|
301 for (int i = 0; i < nr; i++) |
|
302 elem (i, j) = val; |
|
303 |
|
304 return *this; |
|
305 } |
|
306 |
|
307 ComplexMatrix& |
|
308 ComplexMatrix::fill (double val, int r1, int c1, int r2, int c2) |
|
309 { |
|
310 int nr = rows (); |
|
311 int nc = cols (); |
|
312 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
|
313 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) |
|
314 { |
|
315 (*current_liboctave_error_handler) ("range error for fill"); |
|
316 return *this; |
|
317 } |
|
318 |
|
319 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
320 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
321 |
|
322 for (int j = c1; j <= c2; j++) |
|
323 for (int i = r1; i <= r2; i++) |
|
324 elem (i, j) = val; |
|
325 |
|
326 return *this; |
|
327 } |
|
328 |
|
329 ComplexMatrix& |
|
330 ComplexMatrix::fill (const Complex& val, int r1, int c1, int r2, int c2) |
|
331 { |
|
332 int nr = rows (); |
|
333 int nc = cols (); |
|
334 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
|
335 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) |
|
336 { |
|
337 (*current_liboctave_error_handler) ("range error for fill"); |
|
338 return *this; |
|
339 } |
|
340 |
|
341 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
342 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
343 |
|
344 for (int j = c1; j <= c2; j++) |
|
345 for (int i = r1; i <= r2; i++) |
|
346 elem (i, j) = val; |
|
347 |
|
348 return *this; |
|
349 } |
|
350 |
|
351 ComplexMatrix |
|
352 ComplexMatrix::append (const Matrix& a) const |
|
353 { |
|
354 int nr = rows (); |
|
355 int nc = cols (); |
|
356 if (nr != a.rows ()) |
|
357 { |
|
358 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
359 return *this; |
|
360 } |
|
361 |
|
362 int nc_insert = nc; |
|
363 ComplexMatrix retval (nr, nc + a.cols ()); |
|
364 retval.insert (*this, 0, 0); |
|
365 retval.insert (a, 0, nc_insert); |
|
366 return retval; |
|
367 } |
|
368 |
|
369 ComplexMatrix |
|
370 ComplexMatrix::append (const RowVector& a) const |
|
371 { |
|
372 int nr = rows (); |
|
373 int nc = cols (); |
|
374 if (nr != 1) |
|
375 { |
|
376 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
377 return *this; |
|
378 } |
|
379 |
|
380 int nc_insert = nc; |
|
381 ComplexMatrix retval (nr, nc + a.length ()); |
|
382 retval.insert (*this, 0, 0); |
|
383 retval.insert (a, 0, nc_insert); |
|
384 return retval; |
|
385 } |
|
386 |
|
387 ComplexMatrix |
|
388 ComplexMatrix::append (const ColumnVector& a) const |
|
389 { |
|
390 int nr = rows (); |
|
391 int nc = cols (); |
|
392 if (nr != a.length ()) |
|
393 { |
|
394 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
395 return *this; |
|
396 } |
|
397 |
|
398 int nc_insert = nc; |
|
399 ComplexMatrix retval (nr, nc + 1); |
|
400 retval.insert (*this, 0, 0); |
|
401 retval.insert (a, 0, nc_insert); |
|
402 return retval; |
|
403 } |
|
404 |
|
405 ComplexMatrix |
|
406 ComplexMatrix::append (const DiagMatrix& a) const |
|
407 { |
|
408 int nr = rows (); |
|
409 int nc = cols (); |
|
410 if (nr != a.rows ()) |
|
411 { |
|
412 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
413 return *this; |
|
414 } |
|
415 |
|
416 int nc_insert = nc; |
|
417 ComplexMatrix retval (nr, nc + a.cols ()); |
|
418 retval.insert (*this, 0, 0); |
|
419 retval.insert (a, 0, nc_insert); |
|
420 return retval; |
|
421 } |
|
422 |
|
423 ComplexMatrix |
|
424 ComplexMatrix::append (const ComplexMatrix& a) const |
|
425 { |
|
426 int nr = rows (); |
|
427 int nc = cols (); |
|
428 if (nr != a.rows ()) |
|
429 { |
|
430 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
431 return *this; |
|
432 } |
|
433 |
|
434 int nc_insert = nc; |
|
435 ComplexMatrix retval (nr, nc + a.cols ()); |
|
436 retval.insert (*this, 0, 0); |
|
437 retval.insert (a, 0, nc_insert); |
|
438 return retval; |
|
439 } |
|
440 |
|
441 ComplexMatrix |
|
442 ComplexMatrix::append (const ComplexRowVector& a) const |
|
443 { |
|
444 int nr = rows (); |
|
445 int nc = cols (); |
|
446 if (nr != 1) |
|
447 { |
|
448 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
449 return *this; |
|
450 } |
|
451 |
|
452 int nc_insert = nc; |
|
453 ComplexMatrix retval (nr, nc + a.length ()); |
|
454 retval.insert (*this, 0, 0); |
|
455 retval.insert (a, 0, nc_insert); |
|
456 return retval; |
|
457 } |
|
458 |
|
459 ComplexMatrix |
|
460 ComplexMatrix::append (const ComplexColumnVector& a) const |
|
461 { |
|
462 int nr = rows (); |
|
463 int nc = cols (); |
|
464 if (nr != a.length ()) |
|
465 { |
|
466 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
467 return *this; |
|
468 } |
|
469 |
|
470 int nc_insert = nc; |
|
471 ComplexMatrix retval (nr, nc + 1); |
|
472 retval.insert (*this, 0, 0); |
|
473 retval.insert (a, 0, nc_insert); |
|
474 return retval; |
|
475 } |
|
476 |
|
477 ComplexMatrix |
|
478 ComplexMatrix::append (const ComplexDiagMatrix& a) const |
|
479 { |
|
480 int nr = rows (); |
|
481 int nc = cols (); |
|
482 if (nr != a.rows ()) |
|
483 { |
|
484 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
485 return *this; |
|
486 } |
|
487 |
|
488 int nc_insert = nc; |
|
489 ComplexMatrix retval (nr, nc + a.cols ()); |
|
490 retval.insert (*this, 0, 0); |
|
491 retval.insert (a, 0, nc_insert); |
|
492 return retval; |
|
493 } |
|
494 |
|
495 ComplexMatrix |
|
496 ComplexMatrix::stack (const Matrix& a) const |
|
497 { |
|
498 int nr = rows (); |
|
499 int nc = cols (); |
|
500 if (nc != a.cols ()) |
|
501 { |
|
502 (*current_liboctave_error_handler) |
|
503 ("column dimension mismatch for stack"); |
|
504 return *this; |
|
505 } |
|
506 |
|
507 int nr_insert = nr; |
|
508 ComplexMatrix retval (nr + a.rows (), nc); |
|
509 retval.insert (*this, 0, 0); |
|
510 retval.insert (a, nr_insert, 0); |
|
511 return retval; |
|
512 } |
|
513 |
|
514 ComplexMatrix |
|
515 ComplexMatrix::stack (const RowVector& a) const |
|
516 { |
|
517 int nr = rows (); |
|
518 int nc = cols (); |
|
519 if (nc != a.length ()) |
|
520 { |
|
521 (*current_liboctave_error_handler) |
|
522 ("column dimension mismatch for stack"); |
|
523 return *this; |
|
524 } |
|
525 |
|
526 int nr_insert = nr; |
|
527 ComplexMatrix retval (nr + 1, nc); |
|
528 retval.insert (*this, 0, 0); |
|
529 retval.insert (a, nr_insert, 0); |
|
530 return retval; |
|
531 } |
|
532 |
|
533 ComplexMatrix |
|
534 ComplexMatrix::stack (const ColumnVector& a) const |
|
535 { |
|
536 int nr = rows (); |
|
537 int nc = cols (); |
|
538 if (nc != 1) |
|
539 { |
|
540 (*current_liboctave_error_handler) |
|
541 ("column dimension mismatch for stack"); |
|
542 return *this; |
|
543 } |
|
544 |
|
545 int nr_insert = nr; |
|
546 ComplexMatrix retval (nr + a.length (), nc); |
|
547 retval.insert (*this, 0, 0); |
|
548 retval.insert (a, nr_insert, 0); |
|
549 return retval; |
|
550 } |
|
551 |
|
552 ComplexMatrix |
|
553 ComplexMatrix::stack (const DiagMatrix& a) const |
|
554 { |
|
555 int nr = rows (); |
|
556 int nc = cols (); |
|
557 if (nc != a.cols ()) |
|
558 { |
|
559 (*current_liboctave_error_handler) |
|
560 ("column dimension mismatch for stack"); |
|
561 return *this; |
|
562 } |
|
563 |
|
564 int nr_insert = nr; |
|
565 ComplexMatrix retval (nr + a.rows (), nc); |
|
566 retval.insert (*this, 0, 0); |
|
567 retval.insert (a, nr_insert, 0); |
|
568 return retval; |
|
569 } |
|
570 |
|
571 ComplexMatrix |
|
572 ComplexMatrix::stack (const ComplexMatrix& a) const |
|
573 { |
|
574 int nr = rows (); |
|
575 int nc = cols (); |
|
576 if (nc != a.cols ()) |
|
577 { |
|
578 (*current_liboctave_error_handler) |
|
579 ("column dimension mismatch for stack"); |
|
580 return *this; |
|
581 } |
|
582 |
|
583 int nr_insert = nr; |
|
584 ComplexMatrix retval (nr + a.rows (), nc); |
|
585 retval.insert (*this, 0, 0); |
|
586 retval.insert (a, nr_insert, 0); |
|
587 return retval; |
|
588 } |
|
589 |
|
590 ComplexMatrix |
|
591 ComplexMatrix::stack (const ComplexRowVector& a) const |
|
592 { |
|
593 int nr = rows (); |
|
594 int nc = cols (); |
|
595 if (nc != a.length ()) |
|
596 { |
|
597 (*current_liboctave_error_handler) |
|
598 ("column dimension mismatch for stack"); |
|
599 return *this; |
|
600 } |
|
601 |
|
602 int nr_insert = nr; |
|
603 ComplexMatrix retval (nr + 1, nc); |
|
604 retval.insert (*this, 0, 0); |
|
605 retval.insert (a, nr_insert, 0); |
|
606 return retval; |
|
607 } |
|
608 |
|
609 ComplexMatrix |
|
610 ComplexMatrix::stack (const ComplexColumnVector& a) const |
|
611 { |
|
612 int nr = rows (); |
|
613 int nc = cols (); |
|
614 if (nc != 1) |
|
615 { |
|
616 (*current_liboctave_error_handler) |
|
617 ("column dimension mismatch for stack"); |
|
618 return *this; |
|
619 } |
|
620 |
|
621 int nr_insert = nr; |
|
622 ComplexMatrix retval (nr + a.length (), nc); |
|
623 retval.insert (*this, 0, 0); |
|
624 retval.insert (a, nr_insert, 0); |
|
625 return retval; |
|
626 } |
|
627 |
|
628 ComplexMatrix |
|
629 ComplexMatrix::stack (const ComplexDiagMatrix& a) const |
|
630 { |
|
631 int nr = rows (); |
|
632 int nc = cols (); |
|
633 if (nc != a.cols ()) |
|
634 { |
|
635 (*current_liboctave_error_handler) |
|
636 ("column dimension mismatch for stack"); |
|
637 return *this; |
|
638 } |
|
639 |
|
640 int nr_insert = nr; |
|
641 ComplexMatrix retval (nr + a.rows (), nc); |
|
642 retval.insert (*this, 0, 0); |
|
643 retval.insert (a, nr_insert, 0); |
|
644 return retval; |
|
645 } |
|
646 |
|
647 ComplexMatrix |
|
648 ComplexMatrix::hermitian (void) const |
|
649 { |
|
650 int nr = rows (); |
|
651 int nc = cols (); |
|
652 ComplexMatrix result; |
|
653 if (length () > 0) |
|
654 { |
|
655 result.resize (nc, nr); |
|
656 for (int j = 0; j < nc; j++) |
|
657 for (int i = 0; i < nr; i++) |
|
658 result.elem (j, i) = conj (elem (i, j)); |
|
659 } |
|
660 return result; |
|
661 } |
|
662 |
|
663 ComplexMatrix |
|
664 ComplexMatrix::transpose (void) const |
|
665 { |
|
666 int nr = rows (); |
|
667 int nc = cols (); |
|
668 ComplexMatrix result (nc, nr); |
|
669 if (length () > 0) |
|
670 { |
|
671 for (int j = 0; j < nc; j++) |
|
672 for (int i = 0; i < nr; i++) |
|
673 result.elem (j, i) = elem (i, j); |
|
674 } |
|
675 return result; |
|
676 } |
|
677 |
|
678 ComplexMatrix |
|
679 conj (const ComplexMatrix& a) |
|
680 { |
|
681 int a_len = a.length (); |
|
682 ComplexMatrix retval; |
|
683 if (a_len > 0) |
|
684 retval = ComplexMatrix (conj_dup (a.data (), a_len), a.rows (), |
|
685 a.cols ()); |
|
686 return retval; |
|
687 } |
|
688 |
|
689 // resize is the destructive equivalent for this one |
|
690 |
|
691 ComplexMatrix |
|
692 ComplexMatrix::extract (int r1, int c1, int r2, int c2) const |
|
693 { |
|
694 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
695 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
696 |
|
697 int new_r = r2 - r1 + 1; |
|
698 int new_c = c2 - c1 + 1; |
|
699 |
|
700 ComplexMatrix result (new_r, new_c); |
|
701 |
|
702 for (int j = 0; j < new_c; j++) |
|
703 for (int i = 0; i < new_r; i++) |
|
704 result.elem (i, j) = elem (r1+i, c1+j); |
|
705 |
|
706 return result; |
|
707 } |
|
708 |
|
709 // extract row or column i. |
|
710 |
|
711 ComplexRowVector |
|
712 ComplexMatrix::row (int i) const |
|
713 { |
|
714 int nc = cols (); |
|
715 if (i < 0 || i >= rows ()) |
|
716 { |
|
717 (*current_liboctave_error_handler) ("invalid row selection"); |
|
718 return ComplexRowVector (); |
|
719 } |
|
720 |
|
721 ComplexRowVector retval (nc); |
|
722 for (int j = 0; j < cols (); j++) |
|
723 retval.elem (j) = elem (i, j); |
|
724 |
|
725 return retval; |
|
726 } |
|
727 |
|
728 ComplexRowVector |
|
729 ComplexMatrix::row (char *s) const |
|
730 { |
533
|
731 if (! s) |
458
|
732 { |
|
733 (*current_liboctave_error_handler) ("invalid row selection"); |
|
734 return ComplexRowVector (); |
|
735 } |
|
736 |
|
737 char c = *s; |
|
738 if (c == 'f' || c == 'F') |
|
739 return row (0); |
|
740 else if (c == 'l' || c == 'L') |
|
741 return row (rows () - 1); |
|
742 else |
|
743 { |
|
744 (*current_liboctave_error_handler) ("invalid row selection"); |
|
745 return ComplexRowVector (); |
|
746 } |
|
747 } |
|
748 |
|
749 ComplexColumnVector |
|
750 ComplexMatrix::column (int i) const |
|
751 { |
|
752 int nr = rows (); |
|
753 if (i < 0 || i >= cols ()) |
|
754 { |
|
755 (*current_liboctave_error_handler) ("invalid column selection"); |
|
756 return ComplexColumnVector (); |
|
757 } |
|
758 |
|
759 ComplexColumnVector retval (nr); |
|
760 for (int j = 0; j < nr; j++) |
|
761 retval.elem (j) = elem (j, i); |
|
762 |
|
763 return retval; |
|
764 } |
|
765 |
|
766 ComplexColumnVector |
|
767 ComplexMatrix::column (char *s) const |
|
768 { |
533
|
769 if (! s) |
458
|
770 { |
|
771 (*current_liboctave_error_handler) ("invalid column selection"); |
|
772 return ComplexColumnVector (); |
|
773 } |
|
774 |
|
775 char c = *s; |
|
776 if (c == 'f' || c == 'F') |
|
777 return column (0); |
|
778 else if (c == 'l' || c == 'L') |
|
779 return column (cols () - 1); |
|
780 else |
|
781 { |
|
782 (*current_liboctave_error_handler) ("invalid column selection"); |
|
783 return ComplexColumnVector (); |
|
784 } |
|
785 } |
|
786 |
|
787 ComplexMatrix |
|
788 ComplexMatrix::inverse (void) const |
|
789 { |
|
790 int info; |
479
|
791 double rcond; |
|
792 return inverse (info, rcond); |
458
|
793 } |
|
794 |
|
795 ComplexMatrix |
|
796 ComplexMatrix::inverse (int& info) const |
|
797 { |
|
798 double rcond; |
|
799 return inverse (info, rcond); |
|
800 } |
|
801 |
|
802 ComplexMatrix |
1656
|
803 ComplexMatrix::inverse (int& info, double& rcond, int force) const |
458
|
804 { |
1948
|
805 ComplexMatrix retval; |
|
806 |
458
|
807 int nr = rows (); |
|
808 int nc = cols (); |
1948
|
809 |
458
|
810 if (nr != nc) |
1948
|
811 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
458
|
812 else |
|
813 { |
1948
|
814 info = 0; |
|
815 |
|
816 Array<int> ipvt (nr); |
|
817 int *pipvt = ipvt.fortran_vec (); |
|
818 |
|
819 Array<Complex> z (nr); |
|
820 Complex *pz = z.fortran_vec (); |
|
821 |
|
822 retval = *this; |
|
823 Complex *tmp_data = retval.fortran_vec (); |
|
824 |
|
825 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nc, pipvt, rcond, pz)); |
|
826 |
|
827 if (f77_exception_encountered) |
|
828 (*current_liboctave_error_handler) ("unrecoverable error in zgeco"); |
|
829 else |
|
830 { |
|
831 volatile double rcond_plus_one = rcond + 1.0; |
|
832 |
|
833 if (rcond_plus_one == 1.0) |
|
834 info = -1; |
|
835 |
|
836 if (info == -1 && ! force) |
|
837 retval = *this; // Restore contents. |
|
838 else |
|
839 { |
|
840 Complex *dummy = 0; |
|
841 |
|
842 F77_XFCN (zgedi, ZGEDI, (tmp_data, nr, nc, pipvt, dummy, |
|
843 pz, 1)); |
|
844 |
|
845 if (f77_exception_encountered) |
|
846 (*current_liboctave_error_handler) |
|
847 ("unrecoverable error in zgedi"); |
|
848 } |
|
849 } |
458
|
850 } |
|
851 |
1948
|
852 return retval; |
458
|
853 } |
|
854 |
|
855 ComplexMatrix |
740
|
856 ComplexMatrix::pseudo_inverse (double tol) |
|
857 { |
1549
|
858 ComplexMatrix retval; |
|
859 |
740
|
860 ComplexSVD result (*this); |
|
861 |
|
862 DiagMatrix S = result.singular_values (); |
|
863 ComplexMatrix U = result.left_singular_matrix (); |
|
864 ComplexMatrix V = result.right_singular_matrix (); |
|
865 |
|
866 ColumnVector sigma = S.diag (); |
|
867 |
|
868 int r = sigma.length () - 1; |
|
869 int nr = rows (); |
|
870 int nc = cols (); |
|
871 |
|
872 if (tol <= 0.0) |
|
873 { |
|
874 if (nr > nc) |
|
875 tol = nr * sigma.elem (0) * DBL_EPSILON; |
|
876 else |
|
877 tol = nc * sigma.elem (0) * DBL_EPSILON; |
|
878 } |
|
879 |
|
880 while (r >= 0 && sigma.elem (r) < tol) |
|
881 r--; |
|
882 |
|
883 if (r < 0) |
1549
|
884 retval = ComplexMatrix (nc, nr, 0.0); |
740
|
885 else |
|
886 { |
|
887 ComplexMatrix Ur = U.extract (0, 0, nr-1, r); |
|
888 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); |
|
889 ComplexMatrix Vr = V.extract (0, 0, nc-1, r); |
1549
|
890 retval = Vr * D * Ur.hermitian (); |
740
|
891 } |
1549
|
892 |
|
893 return retval; |
740
|
894 } |
|
895 |
|
896 ComplexMatrix |
458
|
897 ComplexMatrix::fourier (void) const |
|
898 { |
1948
|
899 ComplexMatrix retval; |
|
900 |
458
|
901 int nr = rows (); |
|
902 int nc = cols (); |
1948
|
903 |
458
|
904 int npts, nsamples; |
1948
|
905 |
458
|
906 if (nr == 1 || nc == 1) |
|
907 { |
|
908 npts = nr > nc ? nr : nc; |
|
909 nsamples = 1; |
|
910 } |
|
911 else |
|
912 { |
|
913 npts = nr; |
|
914 nsamples = nc; |
|
915 } |
|
916 |
|
917 int nn = 4*npts+15; |
1948
|
918 |
|
919 Array<Complex> wsave (nn); |
|
920 Complex *pwsave = wsave.fortran_vec (); |
|
921 |
|
922 retval = *this; |
|
923 Complex *tmp_data = retval.fortran_vec (); |
|
924 |
|
925 F77_FCN (cffti, CFFTI) (npts, pwsave); |
458
|
926 |
|
927 for (int j = 0; j < nsamples; j++) |
1948
|
928 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
929 |
|
930 return retval; |
458
|
931 } |
|
932 |
|
933 ComplexMatrix |
|
934 ComplexMatrix::ifourier (void) const |
|
935 { |
1948
|
936 ComplexMatrix retval; |
|
937 |
458
|
938 int nr = rows (); |
|
939 int nc = cols (); |
1948
|
940 |
458
|
941 int npts, nsamples; |
1948
|
942 |
458
|
943 if (nr == 1 || nc == 1) |
|
944 { |
|
945 npts = nr > nc ? nr : nc; |
|
946 nsamples = 1; |
|
947 } |
|
948 else |
|
949 { |
|
950 npts = nr; |
|
951 nsamples = nc; |
|
952 } |
|
953 |
|
954 int nn = 4*npts+15; |
1948
|
955 |
|
956 Array<Complex> wsave (nn); |
|
957 Complex *pwsave = wsave.fortran_vec (); |
|
958 |
|
959 retval = *this; |
|
960 Complex *tmp_data = retval.fortran_vec (); |
|
961 |
|
962 F77_FCN (cffti, CFFTI) (npts, pwsave); |
458
|
963 |
|
964 for (int j = 0; j < nsamples; j++) |
1948
|
965 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
458
|
966 |
1321
|
967 for (int j = 0; j < npts*nsamples; j++) |
458
|
968 tmp_data[j] = tmp_data[j] / (double) npts; |
|
969 |
1948
|
970 return retval; |
458
|
971 } |
|
972 |
677
|
973 ComplexMatrix |
|
974 ComplexMatrix::fourier2d (void) const |
|
975 { |
1948
|
976 ComplexMatrix retval; |
|
977 |
677
|
978 int nr = rows (); |
|
979 int nc = cols (); |
1948
|
980 |
677
|
981 int npts, nsamples; |
1948
|
982 |
677
|
983 if (nr == 1 || nc == 1) |
|
984 { |
|
985 npts = nr > nc ? nr : nc; |
|
986 nsamples = 1; |
|
987 } |
|
988 else |
|
989 { |
|
990 npts = nr; |
|
991 nsamples = nc; |
|
992 } |
|
993 |
|
994 int nn = 4*npts+15; |
1948
|
995 |
|
996 Array<Complex> wsave (nn); |
|
997 Complex *pwsave = wsave.fortran_vec (); |
|
998 |
|
999 retval = *this; |
|
1000 Complex *tmp_data = retval.fortran_vec (); |
|
1001 |
|
1002 F77_FCN (cffti, CFFTI) (npts, pwsave); |
677
|
1003 |
|
1004 for (int j = 0; j < nsamples; j++) |
1948
|
1005 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
677
|
1006 |
|
1007 npts = nc; |
|
1008 nsamples = nr; |
|
1009 nn = 4*npts+15; |
1948
|
1010 |
|
1011 wsave.resize (nn); |
|
1012 pwsave = wsave.fortran_vec (); |
|
1013 |
|
1014 Array<Complex> row (npts); |
|
1015 Complex *prow = row.fortran_vec (); |
|
1016 |
|
1017 F77_FCN (cffti, CFFTI) (npts, pwsave); |
677
|
1018 |
1321
|
1019 for (int j = 0; j < nsamples; j++) |
677
|
1020 { |
|
1021 for (int i = 0; i < npts; i++) |
1948
|
1022 prow[i] = tmp_data[i*nr + j]; |
|
1023 |
|
1024 F77_FCN (cfftf, CFFTF) (npts, prow, pwsave); |
677
|
1025 |
1321
|
1026 for (int i = 0; i < npts; i++) |
1948
|
1027 tmp_data[i*nr + j] = prow[i]; |
677
|
1028 } |
|
1029 |
1948
|
1030 return retval; |
677
|
1031 } |
|
1032 |
|
1033 ComplexMatrix |
|
1034 ComplexMatrix::ifourier2d (void) const |
|
1035 { |
1948
|
1036 ComplexMatrix retval; |
|
1037 |
677
|
1038 int nr = rows (); |
|
1039 int nc = cols (); |
1948
|
1040 |
677
|
1041 int npts, nsamples; |
1948
|
1042 |
677
|
1043 if (nr == 1 || nc == 1) |
|
1044 { |
|
1045 npts = nr > nc ? nr : nc; |
|
1046 nsamples = 1; |
|
1047 } |
|
1048 else |
|
1049 { |
|
1050 npts = nr; |
|
1051 nsamples = nc; |
|
1052 } |
|
1053 |
|
1054 int nn = 4*npts+15; |
1948
|
1055 |
|
1056 Array<Complex> wsave (nn); |
|
1057 Complex *pwsave = wsave.fortran_vec (); |
|
1058 |
|
1059 retval = *this; |
|
1060 Complex *tmp_data = retval.fortran_vec (); |
|
1061 |
|
1062 F77_FCN (cffti, CFFTI) (npts, pwsave); |
677
|
1063 |
|
1064 for (int j = 0; j < nsamples; j++) |
1948
|
1065 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
677
|
1066 |
1321
|
1067 for (int j = 0; j < npts*nsamples; j++) |
677
|
1068 tmp_data[j] = tmp_data[j] / (double) npts; |
|
1069 |
|
1070 npts = nc; |
|
1071 nsamples = nr; |
|
1072 nn = 4*npts+15; |
1948
|
1073 |
|
1074 wsave.resize (nn); |
|
1075 pwsave = wsave.fortran_vec (); |
|
1076 |
|
1077 Array<Complex> row (npts); |
|
1078 Complex *prow = row.fortran_vec (); |
|
1079 |
|
1080 F77_FCN (cffti, CFFTI) (npts, pwsave); |
677
|
1081 |
1321
|
1082 for (int j = 0; j < nsamples; j++) |
677
|
1083 { |
|
1084 for (int i = 0; i < npts; i++) |
1948
|
1085 prow[i] = tmp_data[i*nr + j]; |
|
1086 |
|
1087 F77_FCN (cfftb, CFFTB) (npts, prow, pwsave); |
677
|
1088 |
1321
|
1089 for (int i = 0; i < npts; i++) |
1948
|
1090 tmp_data[i*nr + j] = prow[i] / (double) npts; |
677
|
1091 } |
|
1092 |
1948
|
1093 return retval; |
677
|
1094 } |
|
1095 |
458
|
1096 ComplexDET |
|
1097 ComplexMatrix::determinant (void) const |
|
1098 { |
|
1099 int info; |
|
1100 double rcond; |
|
1101 return determinant (info, rcond); |
|
1102 } |
|
1103 |
|
1104 ComplexDET |
|
1105 ComplexMatrix::determinant (int& info) const |
|
1106 { |
|
1107 double rcond; |
|
1108 return determinant (info, rcond); |
|
1109 } |
|
1110 |
|
1111 ComplexDET |
532
|
1112 ComplexMatrix::determinant (int& info, double& rcond) const |
458
|
1113 { |
|
1114 ComplexDET retval; |
|
1115 |
|
1116 int nr = rows (); |
|
1117 int nc = cols (); |
|
1118 |
|
1119 if (nr == 0 || nc == 0) |
|
1120 { |
|
1121 Complex d[2]; |
|
1122 d[0] = 1.0; |
|
1123 d[1] = 0.0; |
|
1124 retval = ComplexDET (d); |
|
1125 } |
|
1126 else |
|
1127 { |
|
1128 info = 0; |
1948
|
1129 |
|
1130 Array<int> ipvt (nr); |
|
1131 int *pipvt = ipvt.fortran_vec (); |
|
1132 |
|
1133 Array<Complex> z (nr); |
|
1134 Complex *pz = z.fortran_vec (); |
|
1135 |
|
1136 ComplexMatrix atmp = *this; |
|
1137 Complex *tmp_data = atmp.fortran_vec (); |
|
1138 |
|
1139 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nr, pipvt, rcond, pz)); |
|
1140 |
|
1141 if (f77_exception_encountered) |
|
1142 (*current_liboctave_error_handler) ("unrecoverable error in zgeco"); |
458
|
1143 else |
|
1144 { |
1948
|
1145 volatile double rcond_plus_one = rcond + 1.0; |
|
1146 |
|
1147 if (rcond_plus_one == 1.0) |
|
1148 { |
|
1149 info = -1; |
|
1150 retval = ComplexDET (); |
|
1151 } |
|
1152 else |
|
1153 { |
|
1154 Complex d[2]; |
|
1155 |
|
1156 F77_XFCN (zgedi, ZGEDI, (tmp_data, nr, nr, pipvt, d, pz, 10)); |
|
1157 |
|
1158 if (f77_exception_encountered) |
|
1159 (*current_liboctave_error_handler) |
|
1160 ("unrecoverable error in dgedi"); |
|
1161 else |
|
1162 retval = ComplexDET (d); |
|
1163 } |
458
|
1164 } |
|
1165 } |
|
1166 |
|
1167 return retval; |
|
1168 } |
|
1169 |
|
1170 ComplexMatrix |
|
1171 ComplexMatrix::solve (const Matrix& b) const |
|
1172 { |
|
1173 int info; |
|
1174 double rcond; |
|
1175 return solve (b, info, rcond); |
|
1176 } |
|
1177 |
|
1178 ComplexMatrix |
|
1179 ComplexMatrix::solve (const Matrix& b, int& info) const |
|
1180 { |
|
1181 double rcond; |
|
1182 return solve (b, info, rcond); |
|
1183 } |
|
1184 |
|
1185 ComplexMatrix |
|
1186 ComplexMatrix::solve (const Matrix& b, int& info, double& rcond) const |
|
1187 { |
|
1188 ComplexMatrix tmp (b); |
|
1189 return solve (tmp, info, rcond); |
|
1190 } |
|
1191 |
|
1192 ComplexMatrix |
|
1193 ComplexMatrix::solve (const ComplexMatrix& b) const |
|
1194 { |
|
1195 int info; |
|
1196 double rcond; |
|
1197 return solve (b, info, rcond); |
|
1198 } |
|
1199 |
|
1200 ComplexMatrix |
|
1201 ComplexMatrix::solve (const ComplexMatrix& b, int& info) const |
|
1202 { |
|
1203 double rcond; |
|
1204 return solve (b, info, rcond); |
|
1205 } |
|
1206 ComplexMatrix |
532
|
1207 ComplexMatrix::solve (const ComplexMatrix& b, int& info, double& rcond) const |
458
|
1208 { |
|
1209 ComplexMatrix retval; |
|
1210 |
|
1211 int nr = rows (); |
|
1212 int nc = cols (); |
1948
|
1213 |
|
1214 if (nr == 0 || nc == 0 || nr != nc || nr != b.rows ()) |
|
1215 (*current_liboctave_error_handler) |
|
1216 ("matrix dimension mismatch in solution of linear equations"); |
458
|
1217 else |
|
1218 { |
1948
|
1219 info = 0; |
|
1220 |
|
1221 Array<int> ipvt (nr); |
|
1222 int *pipvt = ipvt.fortran_vec (); |
|
1223 |
|
1224 Array<Complex> z (nr); |
|
1225 Complex *pz = z.fortran_vec (); |
|
1226 |
|
1227 ComplexMatrix atmp = *this; |
|
1228 Complex *tmp_data = atmp.fortran_vec (); |
|
1229 |
|
1230 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nr, pipvt, rcond, pz)); |
|
1231 |
|
1232 if (f77_exception_encountered) |
|
1233 (*current_liboctave_error_handler) ("unrecoverable error in zgeco"); |
|
1234 else |
|
1235 { |
|
1236 volatile double rcond_plus_one = rcond + 1.0; |
|
1237 |
|
1238 if (rcond_plus_one == 1.0) |
|
1239 { |
|
1240 info = -2; |
|
1241 } |
|
1242 else |
|
1243 { |
|
1244 retval = b; |
|
1245 Complex *result = retval.fortran_vec (); |
|
1246 |
|
1247 int b_nc = b.cols (); |
|
1248 |
|
1249 for (volatile int j = 0; j < b_nc; j++) |
|
1250 { |
|
1251 F77_XFCN (zgesl, ZGESL, (tmp_data, nr, nr, pipvt, |
|
1252 &result[nr*j], 0)); |
|
1253 |
|
1254 if (f77_exception_encountered) |
|
1255 { |
|
1256 (*current_liboctave_error_handler) |
|
1257 ("unrecoverable error in dgesl"); |
|
1258 |
|
1259 break; |
|
1260 } |
|
1261 } |
|
1262 } |
|
1263 } |
458
|
1264 } |
|
1265 |
|
1266 return retval; |
|
1267 } |
|
1268 |
|
1269 ComplexColumnVector |
|
1270 ComplexMatrix::solve (const ComplexColumnVector& b) const |
|
1271 { |
|
1272 int info; |
|
1273 double rcond; |
|
1274 return solve (b, info, rcond); |
|
1275 } |
|
1276 |
|
1277 ComplexColumnVector |
|
1278 ComplexMatrix::solve (const ComplexColumnVector& b, int& info) const |
|
1279 { |
|
1280 double rcond; |
|
1281 return solve (b, info, rcond); |
|
1282 } |
|
1283 |
|
1284 ComplexColumnVector |
|
1285 ComplexMatrix::solve (const ComplexColumnVector& b, int& info, |
532
|
1286 double& rcond) const |
458
|
1287 { |
|
1288 ComplexColumnVector retval; |
|
1289 |
|
1290 int nr = rows (); |
|
1291 int nc = cols (); |
1948
|
1292 |
|
1293 if (nr == 0 || nc == 0 || nr != nc || nr != b.length ()) |
|
1294 (*current_liboctave_error_handler) |
|
1295 ("matrix dimension mismatch in solution of linear equations"); |
458
|
1296 else |
|
1297 { |
1948
|
1298 info = 0; |
|
1299 |
|
1300 Array<int> ipvt (nr); |
|
1301 int *pipvt = ipvt.fortran_vec (); |
|
1302 |
|
1303 Array<Complex> z (nr); |
|
1304 Complex *pz = z.fortran_vec (); |
|
1305 |
|
1306 ComplexMatrix atmp = *this; |
|
1307 Complex *tmp_data = atmp.fortran_vec (); |
|
1308 |
|
1309 F77_XFCN (zgeco, ZGECO, (tmp_data, nr, nr, pipvt, rcond, pz)); |
|
1310 |
|
1311 if (f77_exception_encountered) |
|
1312 (*current_liboctave_error_handler) |
|
1313 ("unrecoverable error in dgeco"); |
|
1314 else |
|
1315 { |
|
1316 volatile double rcond_plus_one = rcond + 1.0; |
|
1317 |
|
1318 if (rcond_plus_one == 1.0) |
|
1319 { |
|
1320 info = -2; |
|
1321 } |
|
1322 else |
|
1323 { |
|
1324 retval = b; |
|
1325 Complex *result = retval.fortran_vec (); |
|
1326 |
|
1327 F77_XFCN (zgesl, ZGESL, (tmp_data, nr, nr, pipvt, result, 0)); |
|
1328 |
|
1329 if (f77_exception_encountered) |
|
1330 (*current_liboctave_error_handler) |
|
1331 ("unrecoverable error in dgesl"); |
|
1332 } |
|
1333 } |
458
|
1334 } |
|
1335 |
|
1336 return retval; |
|
1337 } |
|
1338 |
|
1339 ComplexMatrix |
|
1340 ComplexMatrix::lssolve (const ComplexMatrix& b) const |
|
1341 { |
|
1342 int info; |
|
1343 int rank; |
|
1344 return lssolve (b, info, rank); |
|
1345 } |
|
1346 |
|
1347 ComplexMatrix |
|
1348 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info) const |
|
1349 { |
|
1350 int rank; |
|
1351 return lssolve (b, info, rank); |
|
1352 } |
|
1353 |
|
1354 ComplexMatrix |
|
1355 ComplexMatrix::lssolve (const ComplexMatrix& b, int& info, int& rank) const |
|
1356 { |
1948
|
1357 ComplexMatrix retval; |
|
1358 |
458
|
1359 int nrhs = b.cols (); |
|
1360 |
|
1361 int m = rows (); |
|
1362 int n = cols (); |
|
1363 |
|
1364 if (m == 0 || n == 0 || m != b.rows ()) |
1948
|
1365 (*current_liboctave_error_handler) |
|
1366 ("matrix dimension mismatch solution of linear equations"); |
|
1367 else |
458
|
1368 { |
1948
|
1369 ComplexMatrix atmp = *this; |
|
1370 Complex *tmp_data = atmp.fortran_vec (); |
|
1371 |
|
1372 int nrr = m > n ? m : n; |
|
1373 ComplexMatrix result (nrr, nrhs); |
|
1374 |
|
1375 for (int j = 0; j < nrhs; j++) |
|
1376 for (int i = 0; i < m; i++) |
|
1377 result.elem (i, j) = b.elem (i, j); |
|
1378 |
|
1379 Complex *presult = result.fortran_vec (); |
|
1380 |
|
1381 int len_s = m < n ? m : n; |
|
1382 Array<double> s (len_s); |
|
1383 double *ps = s.fortran_vec (); |
|
1384 double rcond = -1.0; |
|
1385 int lwork; |
|
1386 if (m < n) |
|
1387 lwork = 2*m + (nrhs > n ? nrhs : n); |
|
1388 else |
|
1389 lwork = 2*n + (nrhs > m ? nrhs : m); |
|
1390 |
|
1391 Array<Complex> work (lwork); |
|
1392 Complex *pwork = work.fortran_vec (); |
|
1393 |
|
1394 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1395 lrwork = lrwork > 1 ? lrwork : 1; |
|
1396 Array<double> rwork (lrwork); |
|
1397 double *prwork = rwork.fortran_vec (); |
|
1398 |
|
1399 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
1400 nrr, ps, rcond, rank, pwork, lwork, |
|
1401 prwork, info)); |
|
1402 |
|
1403 if (f77_exception_encountered) |
|
1404 (*current_liboctave_error_handler) ("unrecoverable error in zgelss"); |
|
1405 else |
|
1406 { |
|
1407 ComplexMatrix retval (n, nrhs); |
|
1408 for (int j = 0; j < nrhs; j++) |
|
1409 for (int i = 0; i < n; i++) |
|
1410 retval.elem (i, j) = result.elem (i, j); |
|
1411 } |
458
|
1412 } |
|
1413 |
|
1414 return retval; |
|
1415 } |
|
1416 |
|
1417 ComplexColumnVector |
|
1418 ComplexMatrix::lssolve (const ComplexColumnVector& b) const |
|
1419 { |
|
1420 int info; |
|
1421 int rank; |
|
1422 return lssolve (b, info, rank); |
|
1423 } |
|
1424 |
|
1425 ComplexColumnVector |
|
1426 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info) const |
|
1427 { |
|
1428 int rank; |
|
1429 return lssolve (b, info, rank); |
|
1430 } |
|
1431 |
|
1432 ComplexColumnVector |
|
1433 ComplexMatrix::lssolve (const ComplexColumnVector& b, int& info, |
|
1434 int& rank) const |
|
1435 { |
1948
|
1436 ComplexColumnVector retval; |
|
1437 |
458
|
1438 int nrhs = 1; |
|
1439 |
|
1440 int m = rows (); |
|
1441 int n = cols (); |
|
1442 |
|
1443 if (m == 0 || n == 0 || m != b.length ()) |
1948
|
1444 (*current_liboctave_error_handler) |
|
1445 ("matrix dimension mismatch solution of least squares problem"); |
|
1446 else |
458
|
1447 { |
1948
|
1448 ComplexMatrix atmp = *this; |
|
1449 Complex *tmp_data = atmp.fortran_vec (); |
|
1450 |
|
1451 int nrr = m > n ? m : n; |
|
1452 ComplexColumnVector result (nrr); |
|
1453 |
|
1454 for (int i = 0; i < m; i++) |
|
1455 result.elem (i) = b.elem (i); |
|
1456 |
|
1457 Complex *presult = result.fortran_vec (); |
|
1458 |
|
1459 int len_s = m < n ? m : n; |
|
1460 Array<double> s (len_s); |
|
1461 double *ps = s.fortran_vec (); |
|
1462 |
|
1463 double rcond = -1.0; |
|
1464 |
|
1465 int lwork; |
|
1466 if (m < n) |
|
1467 lwork = 2*m + (nrhs > n ? nrhs : n); |
|
1468 else |
|
1469 lwork = 2*n + (nrhs > m ? nrhs : m); |
|
1470 |
|
1471 Array<Complex> work (lwork); |
|
1472 Complex *pwork = work.fortran_vec (); |
|
1473 |
|
1474 int lrwork = (5 * (m < n ? m : n)) - 4; |
|
1475 lrwork = lrwork > 1 ? lrwork : 1; |
|
1476 Array<double> rwork (lrwork); |
|
1477 double *prwork = rwork.fortran_vec (); |
|
1478 |
|
1479 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
1480 nrr, ps, rcond, rank, pwork, lwork, |
|
1481 prwork, info)); |
|
1482 |
|
1483 if (f77_exception_encountered) |
|
1484 (*current_liboctave_error_handler) ("unrecoverable error in zgelss"); |
|
1485 else |
|
1486 { |
|
1487 ComplexColumnVector retval (n); |
|
1488 for (int i = 0; i < n; i++) |
|
1489 retval.elem (i) = result.elem (i); |
|
1490 } |
458
|
1491 } |
|
1492 |
|
1493 return retval; |
|
1494 } |
|
1495 |
1819
|
1496 // Constants for matrix exponential calculation. |
|
1497 |
|
1498 static double padec [] = |
|
1499 { |
|
1500 5.0000000000000000e-1, |
|
1501 1.1666666666666667e-1, |
|
1502 1.6666666666666667e-2, |
|
1503 1.6025641025641026e-3, |
|
1504 1.0683760683760684e-4, |
|
1505 4.8562548562548563e-6, |
|
1506 1.3875013875013875e-7, |
|
1507 1.9270852604185938e-9, |
|
1508 }; |
|
1509 |
|
1510 ComplexMatrix |
|
1511 ComplexMatrix::expm (void) const |
|
1512 { |
|
1513 ComplexMatrix retval; |
|
1514 |
|
1515 ComplexMatrix m = *this; |
|
1516 |
|
1517 int nc = columns (); |
|
1518 |
|
1519 // trace shift value |
|
1520 Complex trshift = 0.0; |
|
1521 |
|
1522 // Preconditioning step 1: trace normalization. |
|
1523 |
|
1524 for (int i = 0; i < nc; i++) |
|
1525 trshift += m.elem (i, i); |
|
1526 |
|
1527 trshift /= nc; |
|
1528 |
|
1529 for (int i = 0; i < nc; i++) |
|
1530 m.elem (i, i) -= trshift; |
|
1531 |
|
1532 // Preconditioning step 2: eigenvalue balancing. |
|
1533 |
|
1534 ComplexAEPBALANCE mbal (m, "B"); |
|
1535 m = mbal.balanced_matrix (); |
|
1536 ComplexMatrix d = mbal.balancing_matrix (); |
|
1537 |
|
1538 // Preconditioning step 3: scaling. |
|
1539 |
|
1540 ColumnVector work (nc); |
|
1541 double inf_norm |
|
1542 = F77_FCN (zlange, ZLANGE) ("I", nc, nc, m.fortran_vec (), nc, |
|
1543 work.fortran_vec ()); |
|
1544 |
|
1545 int sqpow = (int) (inf_norm > 0.0 |
|
1546 ? (1.0 + log (inf_norm) / log (2.0)) |
|
1547 : 0.0); |
|
1548 |
|
1549 // Check whether we need to square at all. |
|
1550 |
|
1551 if (sqpow < 0) |
|
1552 sqpow = 0; |
|
1553 |
|
1554 if (sqpow > 0) |
|
1555 { |
|
1556 double scale_factor = 1.0; |
|
1557 for (int i = 0; i < sqpow; i++) |
|
1558 scale_factor *= 2.0; |
|
1559 |
|
1560 m = m / scale_factor; |
|
1561 } |
|
1562 |
|
1563 // npp, dpp: pade' approx polynomial matrices. |
|
1564 |
|
1565 ComplexMatrix npp (nc, nc, 0.0); |
|
1566 ComplexMatrix dpp = npp; |
|
1567 |
|
1568 // Now powers a^8 ... a^1. |
|
1569 |
|
1570 int minus_one_j = -1; |
|
1571 for (int j = 7; j >= 0; j--) |
|
1572 { |
|
1573 npp = m * npp + m * padec[j]; |
|
1574 dpp = m * dpp + m * (minus_one_j * padec[j]); |
|
1575 minus_one_j *= -1; |
|
1576 } |
|
1577 |
|
1578 // Zero power. |
|
1579 |
|
1580 dpp = -dpp; |
|
1581 for (int j = 0; j < nc; j++) |
|
1582 { |
|
1583 npp.elem (j, j) += 1.0; |
|
1584 dpp.elem (j, j) += 1.0; |
|
1585 } |
|
1586 |
|
1587 // Compute pade approximation = inverse (dpp) * npp. |
|
1588 |
|
1589 retval = dpp.solve (npp); |
|
1590 |
|
1591 // Reverse preconditioning step 3: repeated squaring. |
|
1592 |
|
1593 while (sqpow) |
|
1594 { |
|
1595 retval = retval * retval; |
|
1596 sqpow--; |
|
1597 } |
|
1598 |
|
1599 // Reverse preconditioning step 2: inverse balancing. |
|
1600 // XXX FIXME XXX -- should probably do this with Lapack calls |
|
1601 // instead of a complete matrix inversion. |
|
1602 |
|
1603 retval = retval.transpose (); |
|
1604 d = d.transpose (); |
|
1605 retval = retval * d; |
|
1606 retval = d.solve (retval); |
|
1607 retval = retval.transpose (); |
|
1608 |
|
1609 // Reverse preconditioning step 1: fix trace normalization. |
|
1610 |
|
1611 return retval * exp (trshift); |
|
1612 } |
|
1613 |
1205
|
1614 // column vector by row vector -> matrix operations |
|
1615 |
|
1616 ComplexMatrix |
|
1617 operator * (const ColumnVector& v, const ComplexRowVector& a) |
|
1618 { |
|
1619 ComplexColumnVector tmp (v); |
|
1620 return tmp * a; |
|
1621 } |
|
1622 |
|
1623 ComplexMatrix |
|
1624 operator * (const ComplexColumnVector& a, const RowVector& b) |
|
1625 { |
|
1626 ComplexRowVector tmp (b); |
|
1627 return a * tmp; |
|
1628 } |
|
1629 |
|
1630 ComplexMatrix |
|
1631 operator * (const ComplexColumnVector& v, const ComplexRowVector& a) |
|
1632 { |
1948
|
1633 ComplexMatrix retval; |
|
1634 |
1205
|
1635 int len = v.length (); |
|
1636 int a_len = a.length (); |
1948
|
1637 |
1205
|
1638 if (len != a_len) |
1948
|
1639 (*current_liboctave_error_handler) |
|
1640 ("nonconformant vector multiplication attempted"); |
|
1641 else |
1205
|
1642 { |
1948
|
1643 if (len != 0) |
|
1644 { |
|
1645 retval.resize (len, a_len); |
|
1646 Complex *c = retval.fortran_vec (); |
|
1647 |
|
1648 F77_XFCN (zgemm, ZGEMM, ("N", "N", len, a_len, 1, 1.0, |
|
1649 v.data (), len, a.data (), 1, 0.0, |
|
1650 c, len, 1L, 1L)); |
|
1651 |
|
1652 if (f77_exception_encountered) |
|
1653 (*current_liboctave_error_handler) |
|
1654 ("unrecoverable error in zgemm"); |
|
1655 } |
1205
|
1656 } |
|
1657 |
1948
|
1658 return retval; |
1205
|
1659 } |
|
1660 |
|
1661 // diagonal matrix by scalar -> matrix operations |
|
1662 |
|
1663 ComplexMatrix |
|
1664 operator + (const DiagMatrix& a, const Complex& s) |
|
1665 { |
|
1666 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1667 return a + tmp; |
|
1668 } |
|
1669 |
|
1670 ComplexMatrix |
|
1671 operator - (const DiagMatrix& a, const Complex& s) |
|
1672 { |
|
1673 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1674 return a + tmp; |
|
1675 } |
|
1676 |
|
1677 ComplexMatrix |
|
1678 operator + (const ComplexDiagMatrix& a, double s) |
|
1679 { |
|
1680 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1681 return a + tmp; |
|
1682 } |
|
1683 |
|
1684 ComplexMatrix |
|
1685 operator - (const ComplexDiagMatrix& a, double s) |
|
1686 { |
|
1687 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1688 return a + tmp; |
|
1689 } |
|
1690 |
|
1691 ComplexMatrix |
|
1692 operator + (const ComplexDiagMatrix& a, const Complex& s) |
|
1693 { |
|
1694 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1695 return a + tmp; |
|
1696 } |
|
1697 |
|
1698 ComplexMatrix |
|
1699 operator - (const ComplexDiagMatrix& a, const Complex& s) |
|
1700 { |
|
1701 ComplexMatrix tmp (a.rows (), a.cols (), -s); |
|
1702 return a + tmp; |
|
1703 } |
|
1704 |
|
1705 // scalar by diagonal matrix -> matrix operations |
|
1706 |
|
1707 ComplexMatrix |
|
1708 operator + (const Complex& s, const DiagMatrix& a) |
|
1709 { |
|
1710 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1711 return tmp + a; |
|
1712 } |
|
1713 |
|
1714 ComplexMatrix |
|
1715 operator - (const Complex& s, const DiagMatrix& a) |
|
1716 { |
|
1717 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1718 return tmp - a; |
|
1719 } |
|
1720 |
|
1721 ComplexMatrix |
|
1722 operator + (double s, const ComplexDiagMatrix& a) |
|
1723 { |
|
1724 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1725 return tmp + a; |
|
1726 } |
|
1727 |
|
1728 ComplexMatrix |
|
1729 operator - (double s, const ComplexDiagMatrix& a) |
|
1730 { |
|
1731 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1732 return tmp - a; |
|
1733 } |
|
1734 |
|
1735 ComplexMatrix |
|
1736 operator + (const Complex& s, const ComplexDiagMatrix& a) |
|
1737 { |
|
1738 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1739 return tmp + a; |
|
1740 } |
|
1741 |
|
1742 ComplexMatrix |
|
1743 operator - (const Complex& s, const ComplexDiagMatrix& a) |
|
1744 { |
|
1745 ComplexMatrix tmp (a.rows (), a.cols (), s); |
|
1746 return tmp - a; |
|
1747 } |
|
1748 |
458
|
1749 // matrix by diagonal matrix -> matrix operations |
|
1750 |
|
1751 ComplexMatrix& |
|
1752 ComplexMatrix::operator += (const DiagMatrix& a) |
|
1753 { |
|
1754 int nr = rows (); |
|
1755 int nc = cols (); |
|
1756 if (nr != a.rows () || nc != a.cols ()) |
|
1757 { |
|
1758 (*current_liboctave_error_handler) |
|
1759 ("nonconformant matrix += operation attempted"); |
889
|
1760 return *this; |
458
|
1761 } |
|
1762 |
|
1763 for (int i = 0; i < a.length (); i++) |
|
1764 elem (i, i) += a.elem (i, i); |
|
1765 |
|
1766 return *this; |
|
1767 } |
|
1768 |
|
1769 ComplexMatrix& |
|
1770 ComplexMatrix::operator -= (const DiagMatrix& a) |
|
1771 { |
|
1772 int nr = rows (); |
|
1773 int nc = cols (); |
|
1774 if (nr != a.rows () || nc != a.cols ()) |
|
1775 { |
|
1776 (*current_liboctave_error_handler) |
|
1777 ("nonconformant matrix -= operation attempted"); |
889
|
1778 return *this; |
458
|
1779 } |
|
1780 |
|
1781 for (int i = 0; i < a.length (); i++) |
|
1782 elem (i, i) -= a.elem (i, i); |
|
1783 |
|
1784 return *this; |
|
1785 } |
|
1786 |
|
1787 ComplexMatrix& |
|
1788 ComplexMatrix::operator += (const ComplexDiagMatrix& a) |
|
1789 { |
|
1790 int nr = rows (); |
|
1791 int nc = cols (); |
|
1792 if (nr != a.rows () || nc != a.cols ()) |
|
1793 { |
|
1794 (*current_liboctave_error_handler) |
|
1795 ("nonconformant matrix += operation attempted"); |
889
|
1796 return *this; |
458
|
1797 } |
|
1798 |
|
1799 for (int i = 0; i < a.length (); i++) |
|
1800 elem (i, i) += a.elem (i, i); |
|
1801 |
|
1802 return *this; |
|
1803 } |
|
1804 |
|
1805 ComplexMatrix& |
|
1806 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) |
|
1807 { |
|
1808 int nr = rows (); |
|
1809 int nc = cols (); |
|
1810 if (nr != a.rows () || nc != a.cols ()) |
|
1811 { |
|
1812 (*current_liboctave_error_handler) |
|
1813 ("nonconformant matrix -= operation attempted"); |
889
|
1814 return *this; |
458
|
1815 } |
|
1816 |
|
1817 for (int i = 0; i < a.length (); i++) |
|
1818 elem (i, i) -= a.elem (i, i); |
|
1819 |
|
1820 return *this; |
|
1821 } |
|
1822 |
1205
|
1823 ComplexMatrix |
|
1824 operator + (const Matrix& m, const ComplexDiagMatrix& a) |
|
1825 { |
|
1826 int nr = m.rows (); |
|
1827 int nc = m.cols (); |
|
1828 if (nr != a.rows () || nc != a.cols ()) |
|
1829 { |
|
1830 (*current_liboctave_error_handler) |
|
1831 ("nonconformant matrix addition attempted"); |
|
1832 return ComplexMatrix (); |
|
1833 } |
|
1834 |
|
1835 if (nr == 0 || nc == 0) |
|
1836 return ComplexMatrix (nr, nc); |
|
1837 |
|
1838 ComplexMatrix result (m); |
|
1839 for (int i = 0; i < a.length (); i++) |
|
1840 result.elem (i, i) += a.elem (i, i); |
|
1841 |
|
1842 return result; |
|
1843 } |
|
1844 |
|
1845 ComplexMatrix |
|
1846 operator - (const Matrix& m, const ComplexDiagMatrix& a) |
|
1847 { |
|
1848 int nr = m.rows (); |
|
1849 int nc = m.cols (); |
|
1850 if (nr != a.rows () || nc != a.cols ()) |
|
1851 { |
|
1852 (*current_liboctave_error_handler) |
|
1853 ("nonconformant matrix subtraction attempted"); |
|
1854 return ComplexMatrix (); |
|
1855 } |
|
1856 |
|
1857 if (nr == 0 || nc == 0) |
|
1858 return ComplexMatrix (nr, nc); |
|
1859 |
|
1860 ComplexMatrix result (m); |
|
1861 for (int i = 0; i < a.length (); i++) |
|
1862 result.elem (i, i) -= a.elem (i, i); |
|
1863 |
|
1864 return result; |
|
1865 } |
|
1866 |
|
1867 ComplexMatrix |
|
1868 operator * (const Matrix& m, const ComplexDiagMatrix& a) |
|
1869 { |
1948
|
1870 ComplexMatrix retval; |
|
1871 |
1205
|
1872 int nr = m.rows (); |
|
1873 int nc = m.cols (); |
1948
|
1874 |
1205
|
1875 int a_nr = a.rows (); |
|
1876 int a_nc = a.cols (); |
1948
|
1877 |
1205
|
1878 if (nc != a_nr) |
1948
|
1879 (*current_liboctave_error_handler) |
|
1880 ("nonconformant matrix multiplication attempted"); |
|
1881 else |
1205
|
1882 { |
1948
|
1883 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1884 retval.resize (nr, a_nc, 0.0); |
1205
|
1885 else |
|
1886 { |
1948
|
1887 retval.resize (nr, a_nc); |
|
1888 Complex *c = retval.fortran_vec (); |
|
1889 |
|
1890 Complex *ctmp = 0; |
|
1891 |
|
1892 for (int j = 0; j < a.length (); j++) |
|
1893 { |
|
1894 int idx = j * nr; |
|
1895 ctmp = c + idx; |
|
1896 if (a.elem (j, j) == 1.0) |
|
1897 { |
|
1898 for (int i = 0; i < nr; i++) |
|
1899 ctmp[i] = m.elem (i, j); |
|
1900 } |
|
1901 else if (a.elem (j, j) == 0.0) |
|
1902 { |
|
1903 for (int i = 0; i < nr; i++) |
|
1904 ctmp[i] = 0.0; |
|
1905 } |
|
1906 else |
|
1907 { |
|
1908 for (int i = 0; i < nr; i++) |
|
1909 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
1910 } |
|
1911 } |
|
1912 |
|
1913 if (a_nr < a_nc) |
|
1914 { |
|
1915 for (int i = nr * nc; i < nr * a_nc; i++) |
|
1916 ctmp[i] = 0.0; |
|
1917 } |
1205
|
1918 } |
|
1919 } |
|
1920 |
1948
|
1921 return retval; |
1205
|
1922 } |
|
1923 |
|
1924 // diagonal matrix by matrix -> matrix operations |
|
1925 |
|
1926 ComplexMatrix |
|
1927 operator + (const DiagMatrix& m, const ComplexMatrix& a) |
|
1928 { |
|
1929 int nr = m.rows (); |
|
1930 int nc = m.cols (); |
|
1931 if (nr != a.rows () || nc != a.cols ()) |
|
1932 { |
|
1933 (*current_liboctave_error_handler) |
|
1934 ("nonconformant matrix addition attempted"); |
|
1935 return ComplexMatrix (); |
|
1936 } |
|
1937 |
|
1938 if (nr == 0 || nc == 0) |
|
1939 return ComplexMatrix (nr, nc); |
|
1940 |
|
1941 ComplexMatrix result (a); |
|
1942 for (int i = 0; i < m.length (); i++) |
|
1943 result.elem (i, i) += m.elem (i, i); |
|
1944 |
|
1945 return result; |
|
1946 } |
|
1947 |
|
1948 ComplexMatrix |
|
1949 operator - (const DiagMatrix& m, const ComplexMatrix& a) |
|
1950 { |
|
1951 int nr = m.rows (); |
|
1952 int nc = m.cols (); |
|
1953 if (nr != a.rows () || nc != a.cols ()) |
|
1954 { |
|
1955 (*current_liboctave_error_handler) |
|
1956 ("nonconformant matrix subtraction attempted"); |
|
1957 return ComplexMatrix (); |
|
1958 } |
|
1959 |
|
1960 if (nr == 0 || nc == 0) |
|
1961 return ComplexMatrix (nr, nc); |
|
1962 |
|
1963 ComplexMatrix result (-a); |
|
1964 for (int i = 0; i < m.length (); i++) |
|
1965 result.elem (i, i) += m.elem (i, i); |
|
1966 |
|
1967 return result; |
|
1968 } |
|
1969 |
|
1970 ComplexMatrix |
|
1971 operator * (const DiagMatrix& m, const ComplexMatrix& a) |
|
1972 { |
|
1973 int nr = m.rows (); |
|
1974 int nc = m.cols (); |
|
1975 int a_nr = a.rows (); |
|
1976 int a_nc = a.cols (); |
|
1977 if (nc != a_nr) |
|
1978 { |
|
1979 (*current_liboctave_error_handler) |
|
1980 ("nonconformant matrix multiplication attempted"); |
|
1981 return ComplexMatrix (); |
|
1982 } |
|
1983 |
|
1984 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1985 return ComplexMatrix (nr, nc, 0.0); |
|
1986 |
|
1987 ComplexMatrix c (nr, a_nc); |
|
1988 |
|
1989 for (int i = 0; i < m.length (); i++) |
|
1990 { |
|
1991 if (m.elem (i, i) == 1.0) |
|
1992 { |
|
1993 for (int j = 0; j < a_nc; j++) |
|
1994 c.elem (i, j) = a.elem (i, j); |
|
1995 } |
|
1996 else if (m.elem (i, i) == 0.0) |
|
1997 { |
|
1998 for (int j = 0; j < a_nc; j++) |
|
1999 c.elem (i, j) = 0.0; |
|
2000 } |
|
2001 else |
|
2002 { |
|
2003 for (int j = 0; j < a_nc; j++) |
|
2004 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
2005 } |
|
2006 } |
|
2007 |
|
2008 if (nr > nc) |
|
2009 { |
|
2010 for (int j = 0; j < a_nc; j++) |
|
2011 for (int i = a_nr; i < nr; i++) |
|
2012 c.elem (i, j) = 0.0; |
|
2013 } |
|
2014 |
|
2015 return c; |
|
2016 } |
|
2017 |
|
2018 ComplexMatrix |
|
2019 operator + (const ComplexDiagMatrix& m, const Matrix& a) |
|
2020 { |
|
2021 int nr = m.rows (); |
|
2022 int nc = m.cols (); |
|
2023 if (nr != a.rows () || nc != a.cols ()) |
|
2024 { |
|
2025 (*current_liboctave_error_handler) |
|
2026 ("nonconformant matrix addition attempted"); |
|
2027 return ComplexMatrix (); |
|
2028 } |
|
2029 |
|
2030 if (nr == 0 || nc == 0) |
|
2031 return ComplexMatrix (nr, nc); |
|
2032 |
|
2033 ComplexMatrix result (a); |
|
2034 for (int i = 0; i < m.length (); i++) |
|
2035 result.elem (i, i) += m.elem (i, i); |
|
2036 |
|
2037 return result; |
|
2038 } |
|
2039 |
|
2040 ComplexMatrix |
|
2041 operator - (const ComplexDiagMatrix& m, const Matrix& a) |
|
2042 { |
|
2043 int nr = m.rows (); |
|
2044 int nc = m.cols (); |
|
2045 if (nr != a.rows () || nc != a.cols ()) |
|
2046 { |
|
2047 (*current_liboctave_error_handler) |
|
2048 ("nonconformant matrix subtraction attempted"); |
|
2049 return ComplexMatrix (); |
|
2050 } |
|
2051 |
|
2052 if (nr == 0 || nc == 0) |
|
2053 return ComplexMatrix (nr, nc); |
|
2054 |
|
2055 ComplexMatrix result (-a); |
|
2056 for (int i = 0; i < m.length (); i++) |
|
2057 result.elem (i, i) += m.elem (i, i); |
|
2058 |
|
2059 return result; |
|
2060 } |
|
2061 |
|
2062 ComplexMatrix |
|
2063 operator * (const ComplexDiagMatrix& m, const Matrix& a) |
|
2064 { |
|
2065 int nr = m.rows (); |
|
2066 int nc = m.cols (); |
|
2067 int a_nr = a.rows (); |
|
2068 int a_nc = a.cols (); |
|
2069 if (nc != a_nr) |
|
2070 { |
|
2071 (*current_liboctave_error_handler) |
|
2072 ("nonconformant matrix multiplication attempted"); |
|
2073 return ComplexMatrix (); |
|
2074 } |
|
2075 |
|
2076 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2077 return ComplexMatrix (nr, a_nc, 0.0); |
|
2078 |
|
2079 ComplexMatrix c (nr, a_nc); |
|
2080 |
|
2081 for (int i = 0; i < m.length (); i++) |
|
2082 { |
|
2083 if (m.elem (i, i) == 1.0) |
|
2084 { |
|
2085 for (int j = 0; j < a_nc; j++) |
|
2086 c.elem (i, j) = a.elem (i, j); |
|
2087 } |
|
2088 else if (m.elem (i, i) == 0.0) |
|
2089 { |
|
2090 for (int j = 0; j < a_nc; j++) |
|
2091 c.elem (i, j) = 0.0; |
|
2092 } |
|
2093 else |
|
2094 { |
|
2095 for (int j = 0; j < a_nc; j++) |
|
2096 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
2097 } |
|
2098 } |
|
2099 |
|
2100 if (nr > nc) |
|
2101 { |
|
2102 for (int j = 0; j < a_nc; j++) |
|
2103 for (int i = a_nr; i < nr; i++) |
|
2104 c.elem (i, j) = 0.0; |
|
2105 } |
|
2106 |
|
2107 return c; |
|
2108 } |
|
2109 |
|
2110 ComplexMatrix |
|
2111 operator + (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
2112 { |
|
2113 int nr = m.rows (); |
|
2114 int nc = m.cols (); |
|
2115 if (nr != a.rows () || nc != a.cols ()) |
|
2116 { |
|
2117 (*current_liboctave_error_handler) |
|
2118 ("nonconformant matrix addition attempted"); |
|
2119 return ComplexMatrix (); |
|
2120 } |
|
2121 |
|
2122 if (nr == 0 || nc == 0) |
|
2123 return ComplexMatrix (nr, nc); |
|
2124 |
|
2125 ComplexMatrix result (a); |
|
2126 for (int i = 0; i < m.length (); i++) |
|
2127 result.elem (i, i) += m.elem (i, i); |
|
2128 |
|
2129 return result; |
|
2130 } |
|
2131 |
|
2132 ComplexMatrix |
|
2133 operator - (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
2134 { |
|
2135 int nr = m.rows (); |
|
2136 int nc = m.cols (); |
|
2137 if (nr != a.rows () || nc != a.cols ()) |
|
2138 { |
|
2139 (*current_liboctave_error_handler) |
|
2140 ("nonconformant matrix subtraction attempted"); |
|
2141 return ComplexMatrix (); |
|
2142 } |
|
2143 |
|
2144 if (nr == 0 || nc == 0) |
|
2145 return ComplexMatrix (nr, nc); |
|
2146 |
|
2147 ComplexMatrix result (-a); |
|
2148 for (int i = 0; i < m.length (); i++) |
|
2149 result.elem (i, i) += m.elem (i, i); |
|
2150 |
|
2151 return result; |
|
2152 } |
|
2153 |
|
2154 ComplexMatrix |
|
2155 operator * (const ComplexDiagMatrix& m, const ComplexMatrix& a) |
|
2156 { |
|
2157 int nr = m.rows (); |
|
2158 int nc = m.cols (); |
|
2159 int a_nr = a.rows (); |
|
2160 int a_nc = a.cols (); |
|
2161 if (nc != a_nr) |
|
2162 { |
|
2163 (*current_liboctave_error_handler) |
|
2164 ("nonconformant matrix multiplication attempted"); |
|
2165 return ComplexMatrix (); |
|
2166 } |
|
2167 |
|
2168 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2169 return ComplexMatrix (nr, a_nc, 0.0); |
|
2170 |
|
2171 ComplexMatrix c (nr, a_nc); |
|
2172 |
|
2173 for (int i = 0; i < m.length (); i++) |
|
2174 { |
|
2175 if (m.elem (i, i) == 1.0) |
|
2176 { |
|
2177 for (int j = 0; j < a_nc; j++) |
|
2178 c.elem (i, j) = a.elem (i, j); |
|
2179 } |
|
2180 else if (m.elem (i, i) == 0.0) |
|
2181 { |
|
2182 for (int j = 0; j < a_nc; j++) |
|
2183 c.elem (i, j) = 0.0; |
|
2184 } |
|
2185 else |
|
2186 { |
|
2187 for (int j = 0; j < a_nc; j++) |
|
2188 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
|
2189 } |
|
2190 } |
|
2191 |
|
2192 if (nr > nc) |
|
2193 { |
|
2194 for (int j = 0; j < a_nc; j++) |
|
2195 for (int i = a_nr; i < nr; i++) |
|
2196 c.elem (i, j) = 0.0; |
|
2197 } |
|
2198 |
|
2199 return c; |
|
2200 } |
|
2201 |
458
|
2202 // matrix by matrix -> matrix operations |
|
2203 |
|
2204 ComplexMatrix& |
|
2205 ComplexMatrix::operator += (const Matrix& a) |
|
2206 { |
|
2207 int nr = rows (); |
|
2208 int nc = cols (); |
|
2209 if (nr != a.rows () || nc != a.cols ()) |
|
2210 { |
|
2211 (*current_liboctave_error_handler) |
|
2212 ("nonconformant matrix += operation attempted"); |
|
2213 return *this; |
|
2214 } |
|
2215 |
|
2216 if (nr == 0 || nc == 0) |
|
2217 return *this; |
|
2218 |
|
2219 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2220 |
|
2221 add2 (d, a.data (), length ()); |
|
2222 return *this; |
|
2223 } |
|
2224 |
|
2225 ComplexMatrix& |
|
2226 ComplexMatrix::operator -= (const Matrix& a) |
|
2227 { |
|
2228 int nr = rows (); |
|
2229 int nc = cols (); |
|
2230 if (nr != a.rows () || nc != a.cols ()) |
|
2231 { |
|
2232 (*current_liboctave_error_handler) |
|
2233 ("nonconformant matrix -= operation attempted"); |
|
2234 return *this; |
|
2235 } |
|
2236 |
|
2237 if (nr == 0 || nc == 0) |
|
2238 return *this; |
|
2239 |
|
2240 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2241 |
|
2242 subtract2 (d, a.data (), length ()); |
|
2243 return *this; |
|
2244 } |
|
2245 |
|
2246 ComplexMatrix& |
|
2247 ComplexMatrix::operator += (const ComplexMatrix& a) |
|
2248 { |
|
2249 int nr = rows (); |
|
2250 int nc = cols (); |
|
2251 if (nr != a.rows () || nc != a.cols ()) |
|
2252 { |
|
2253 (*current_liboctave_error_handler) |
|
2254 ("nonconformant matrix += operation attempted"); |
|
2255 return *this; |
|
2256 } |
|
2257 |
|
2258 if (nr == 0 || nc == 0) |
|
2259 return *this; |
|
2260 |
|
2261 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2262 |
|
2263 add2 (d, a.data (), length ()); |
|
2264 return *this; |
|
2265 } |
|
2266 |
|
2267 ComplexMatrix& |
|
2268 ComplexMatrix::operator -= (const ComplexMatrix& a) |
|
2269 { |
|
2270 int nr = rows (); |
|
2271 int nc = cols (); |
|
2272 if (nr != a.rows () || nc != a.cols ()) |
|
2273 { |
|
2274 (*current_liboctave_error_handler) |
|
2275 ("nonconformant matrix -= operation attempted"); |
|
2276 return *this; |
|
2277 } |
|
2278 |
|
2279 if (nr == 0 || nc == 0) |
|
2280 return *this; |
|
2281 |
|
2282 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2283 |
|
2284 subtract2 (d, a.data (), length ()); |
|
2285 return *this; |
|
2286 } |
|
2287 |
|
2288 // unary operations |
|
2289 |
|
2290 Matrix |
|
2291 ComplexMatrix::operator ! (void) const |
|
2292 { |
|
2293 return Matrix (not (data (), length ()), rows (), cols ()); |
|
2294 } |
|
2295 |
|
2296 // matrix by scalar -> matrix operations |
|
2297 |
|
2298 ComplexMatrix |
1205
|
2299 operator + (const Matrix& a, const Complex& s) |
|
2300 { |
|
2301 return ComplexMatrix (add (a.data (), a.length (), s), |
|
2302 a.rows (), a.cols ()); |
|
2303 } |
|
2304 |
|
2305 ComplexMatrix |
|
2306 operator - (const Matrix& a, const Complex& s) |
|
2307 { |
|
2308 return ComplexMatrix (subtract (a.data (), a.length (), s), |
|
2309 a.rows (), a.cols ()); |
|
2310 } |
|
2311 |
|
2312 ComplexMatrix |
|
2313 operator * (const Matrix& a, const Complex& s) |
|
2314 { |
|
2315 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2316 a.rows (), a.cols ()); |
|
2317 } |
|
2318 |
|
2319 ComplexMatrix |
|
2320 operator / (const Matrix& a, const Complex& s) |
|
2321 { |
|
2322 return ComplexMatrix (divide (a.data (), a.length (), s), |
|
2323 a.rows (), a.cols ()); |
|
2324 } |
|
2325 |
|
2326 ComplexMatrix |
458
|
2327 operator + (const ComplexMatrix& a, double s) |
|
2328 { |
|
2329 return ComplexMatrix (add (a.data (), a.length (), s), |
|
2330 a.rows (), a.cols ()); |
|
2331 } |
|
2332 |
|
2333 ComplexMatrix |
|
2334 operator - (const ComplexMatrix& a, double s) |
|
2335 { |
|
2336 return ComplexMatrix (subtract (a.data (), a.length (), s), |
|
2337 a.rows (), a.cols ()); |
|
2338 } |
|
2339 |
|
2340 ComplexMatrix |
|
2341 operator * (const ComplexMatrix& a, double s) |
|
2342 { |
|
2343 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2344 a.rows (), a.cols ()); |
|
2345 } |
|
2346 |
|
2347 ComplexMatrix |
|
2348 operator / (const ComplexMatrix& a, double s) |
|
2349 { |
|
2350 return ComplexMatrix (divide (a.data (), a.length (), s), |
|
2351 a.rows (), a.cols ()); |
|
2352 } |
|
2353 |
|
2354 // scalar by matrix -> matrix operations |
|
2355 |
|
2356 ComplexMatrix |
|
2357 operator + (double s, const ComplexMatrix& a) |
|
2358 { |
|
2359 return ComplexMatrix (add (a.data (), a.length (), s), a.rows (), |
|
2360 a.cols ()); |
|
2361 } |
|
2362 |
|
2363 ComplexMatrix |
|
2364 operator - (double s, const ComplexMatrix& a) |
|
2365 { |
|
2366 return ComplexMatrix (subtract (s, a.data (), a.length ()), |
|
2367 a.rows (), a.cols ()); |
|
2368 } |
|
2369 |
|
2370 ComplexMatrix |
|
2371 operator * (double s, const ComplexMatrix& a) |
|
2372 { |
|
2373 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2374 a.rows (), a.cols ()); |
|
2375 } |
|
2376 |
|
2377 ComplexMatrix |
|
2378 operator / (double s, const ComplexMatrix& a) |
|
2379 { |
|
2380 return ComplexMatrix (divide (s, a.data (), a.length ()), |
|
2381 a.rows (), a.cols ()); |
|
2382 } |
|
2383 |
1205
|
2384 ComplexMatrix |
|
2385 operator + (const Complex& s, const Matrix& a) |
458
|
2386 { |
1205
|
2387 return ComplexMatrix (add (s, a.data (), a.length ()), |
|
2388 a.rows (), a.cols ()); |
458
|
2389 } |
|
2390 |
1205
|
2391 ComplexMatrix |
|
2392 operator - (const Complex& s, const Matrix& a) |
458
|
2393 { |
1205
|
2394 return ComplexMatrix (subtract (s, a.data (), a.length ()), |
|
2395 a.rows (), a.cols ()); |
|
2396 } |
|
2397 |
|
2398 ComplexMatrix |
|
2399 operator * (const Complex& s, const Matrix& a) |
|
2400 { |
|
2401 return ComplexMatrix (multiply (a.data (), a.length (), s), |
|
2402 a.rows (), a.cols ()); |
|
2403 } |
|
2404 |
|
2405 ComplexMatrix |
|
2406 operator / (const Complex& s, const Matrix& a) |
|
2407 { |
|
2408 return ComplexMatrix (divide (s, a.data (), a.length ()), |
|
2409 a.rows (), a.cols ()); |
458
|
2410 } |
|
2411 |
|
2412 // matrix by diagonal matrix -> matrix operations |
|
2413 |
|
2414 ComplexMatrix |
|
2415 operator + (const ComplexMatrix& m, const DiagMatrix& a) |
|
2416 { |
|
2417 int nr = m.rows (); |
|
2418 int nc = m.cols (); |
|
2419 if (nr != a.rows () || nc != a.cols ()) |
|
2420 { |
|
2421 (*current_liboctave_error_handler) |
|
2422 ("nonconformant matrix addition attempted"); |
|
2423 return ComplexMatrix (); |
|
2424 } |
|
2425 |
|
2426 if (nr == 0 || nc == 0) |
|
2427 return ComplexMatrix (nr, nc); |
|
2428 |
|
2429 ComplexMatrix result (m); |
|
2430 for (int i = 0; i < a.length (); i++) |
|
2431 result.elem (i, i) += a.elem (i, i); |
|
2432 |
|
2433 return result; |
|
2434 } |
|
2435 |
|
2436 ComplexMatrix |
|
2437 operator - (const ComplexMatrix& m, const DiagMatrix& a) |
|
2438 { |
|
2439 int nr = m.rows (); |
|
2440 int nc = m.cols (); |
|
2441 if (nr != a.rows () || nc != a.cols ()) |
|
2442 { |
|
2443 (*current_liboctave_error_handler) |
|
2444 ("nonconformant matrix subtraction attempted"); |
|
2445 return ComplexMatrix (); |
|
2446 } |
|
2447 |
|
2448 if (nr == 0 || nc == 0) |
|
2449 return ComplexMatrix (nr, nc); |
|
2450 |
|
2451 ComplexMatrix result (m); |
|
2452 for (int i = 0; i < a.length (); i++) |
|
2453 result.elem (i, i) -= a.elem (i, i); |
|
2454 |
|
2455 return result; |
|
2456 } |
|
2457 |
|
2458 ComplexMatrix |
|
2459 operator * (const ComplexMatrix& m, const DiagMatrix& a) |
|
2460 { |
1948
|
2461 ComplexMatrix retval; |
|
2462 |
458
|
2463 int nr = m.rows (); |
|
2464 int nc = m.cols (); |
1948
|
2465 |
458
|
2466 int a_nc = a.cols (); |
1948
|
2467 |
458
|
2468 if (nc != a.rows ()) |
1948
|
2469 (*current_liboctave_error_handler) |
|
2470 ("nonconformant matrix multiplication attempted"); |
|
2471 else |
458
|
2472 { |
1948
|
2473 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2474 retval.resize (nr, nc, 0.0); |
458
|
2475 else |
|
2476 { |
1948
|
2477 retval.resize (nr, a_nc); |
|
2478 Complex *c = retval.fortran_vec (); |
|
2479 Complex *ctmp = 0; |
|
2480 |
|
2481 for (int j = 0; j < a.length (); j++) |
|
2482 { |
|
2483 int idx = j * nr; |
|
2484 ctmp = c + idx; |
|
2485 if (a.elem (j, j) == 1.0) |
|
2486 { |
|
2487 for (int i = 0; i < nr; i++) |
|
2488 ctmp[i] = m.elem (i, j); |
|
2489 } |
|
2490 else if (a.elem (j, j) == 0.0) |
|
2491 { |
|
2492 for (int i = 0; i < nr; i++) |
|
2493 ctmp[i] = 0.0; |
|
2494 } |
|
2495 else |
|
2496 { |
|
2497 for (int i = 0; i < nr; i++) |
|
2498 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
2499 } |
|
2500 } |
|
2501 |
|
2502 if (a.rows () < a_nc) |
|
2503 { |
|
2504 for (int i = nr * nc; i < nr * a_nc; i++) |
|
2505 ctmp[i] = 0.0; |
|
2506 } |
458
|
2507 } |
|
2508 } |
|
2509 |
1948
|
2510 return retval; |
458
|
2511 } |
|
2512 |
|
2513 ComplexMatrix |
|
2514 operator + (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2515 { |
|
2516 int nr = m.rows (); |
|
2517 int nc = m.cols (); |
|
2518 if (nr != a.rows () || nc != a.cols ()) |
|
2519 { |
|
2520 (*current_liboctave_error_handler) |
|
2521 ("nonconformant matrix addition attempted"); |
|
2522 return ComplexMatrix (); |
|
2523 } |
|
2524 |
|
2525 if (nr == 0 || nc == 0) |
|
2526 return ComplexMatrix (nr, nc); |
|
2527 |
|
2528 ComplexMatrix result (m); |
|
2529 for (int i = 0; i < a.length (); i++) |
|
2530 result.elem (i, i) += a.elem (i, i); |
|
2531 |
|
2532 return result; |
|
2533 } |
|
2534 |
|
2535 ComplexMatrix |
|
2536 operator - (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2537 { |
|
2538 int nr = m.rows (); |
|
2539 int nc = m.cols (); |
|
2540 if (nr != a.rows () || nc != a.cols ()) |
|
2541 { |
|
2542 (*current_liboctave_error_handler) |
|
2543 ("nonconformant matrix subtraction attempted"); |
|
2544 return ComplexMatrix (); |
|
2545 } |
|
2546 |
|
2547 if (nr == 0 || nc == 0) |
|
2548 return ComplexMatrix (nr, nc); |
|
2549 |
|
2550 ComplexMatrix result (m); |
|
2551 for (int i = 0; i < a.length (); i++) |
|
2552 result.elem (i, i) -= a.elem (i, i); |
|
2553 |
|
2554 return result; |
|
2555 } |
|
2556 |
|
2557 ComplexMatrix |
|
2558 operator * (const ComplexMatrix& m, const ComplexDiagMatrix& a) |
|
2559 { |
1948
|
2560 ComplexMatrix retval; |
|
2561 |
458
|
2562 int nr = m.rows (); |
|
2563 int nc = m.cols (); |
1948
|
2564 |
458
|
2565 int a_nc = a.cols (); |
1948
|
2566 |
458
|
2567 if (nc != a.rows ()) |
1948
|
2568 (*current_liboctave_error_handler) |
|
2569 ("nonconformant matrix multiplication attempted"); |
|
2570 else |
458
|
2571 { |
1948
|
2572 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2573 retval.resize (nr, nc, 0.0); |
458
|
2574 else |
|
2575 { |
1948
|
2576 retval.resize (nr, nc); |
|
2577 Complex *c = retval.fortran_vec (); |
|
2578 Complex *ctmp = 0; |
|
2579 |
|
2580 for (int j = 0; j < a.length (); j++) |
|
2581 { |
|
2582 int idx = j * nr; |
|
2583 ctmp = c + idx; |
|
2584 if (a.elem (j, j) == 1.0) |
|
2585 { |
|
2586 for (int i = 0; i < nr; i++) |
|
2587 ctmp[i] = m.elem (i, j); |
|
2588 } |
|
2589 else if (a.elem (j, j) == 0.0) |
|
2590 { |
|
2591 for (int i = 0; i < nr; i++) |
|
2592 ctmp[i] = 0.0; |
|
2593 } |
|
2594 else |
|
2595 { |
|
2596 for (int i = 0; i < nr; i++) |
|
2597 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
2598 } |
|
2599 } |
|
2600 |
|
2601 if (a.rows () < a_nc) |
|
2602 { |
|
2603 for (int i = nr * nc; i < nr * a_nc; i++) |
|
2604 ctmp[i] = 0.0; |
|
2605 } |
458
|
2606 } |
|
2607 } |
|
2608 |
1948
|
2609 return retval; |
458
|
2610 } |
|
2611 |
|
2612 // matrix by matrix -> matrix operations |
|
2613 |
|
2614 ComplexMatrix |
|
2615 operator + (const ComplexMatrix& m, const Matrix& a) |
|
2616 { |
|
2617 int nr = m.rows (); |
|
2618 int nc = m.cols (); |
|
2619 if (nr != a.rows () || nc != a.cols ()) |
|
2620 { |
|
2621 (*current_liboctave_error_handler) |
|
2622 ("nonconformant matrix addition attempted"); |
|
2623 return ComplexMatrix (); |
|
2624 } |
|
2625 |
|
2626 if (nr == 0 || nc == 0) |
|
2627 return ComplexMatrix (nr, nc); |
|
2628 |
|
2629 return ComplexMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
2630 } |
|
2631 |
|
2632 ComplexMatrix |
|
2633 operator - (const ComplexMatrix& m, const Matrix& a) |
|
2634 { |
|
2635 int nr = m.rows (); |
|
2636 int nc = m.cols (); |
|
2637 if (nr != a.rows () || nc != a.cols ()) |
|
2638 { |
|
2639 (*current_liboctave_error_handler) |
|
2640 ("nonconformant matrix subtraction attempted"); |
|
2641 return ComplexMatrix (); |
|
2642 } |
|
2643 |
|
2644 if (nr == 0 || nc == 0) |
|
2645 return ComplexMatrix (nr, nc); |
|
2646 |
|
2647 return ComplexMatrix (subtract (m.data (), a.data (), m.length ()), nr, nc); |
|
2648 } |
|
2649 |
|
2650 ComplexMatrix |
1205
|
2651 operator + (const Matrix& m, const ComplexMatrix& a) |
|
2652 { |
|
2653 int nr = m.rows (); |
|
2654 int nc = m.cols (); |
|
2655 if (nr != a.rows () || nc != a.cols ()) |
|
2656 { |
|
2657 (*current_liboctave_error_handler) |
|
2658 ("nonconformant matrix addition attempted"); |
|
2659 return ComplexMatrix (); |
|
2660 } |
|
2661 |
|
2662 return ComplexMatrix (add (m.data (), a.data (), m.length ()), nr, nc); |
|
2663 } |
|
2664 |
|
2665 ComplexMatrix |
|
2666 operator - (const Matrix& m, const ComplexMatrix& a) |
|
2667 { |
|
2668 int nr = m.rows (); |
|
2669 int nc = m.cols (); |
|
2670 if (nr != a.rows () || nc != a.cols ()) |
|
2671 { |
|
2672 (*current_liboctave_error_handler) |
|
2673 ("nonconformant matrix subtraction attempted"); |
|
2674 return ComplexMatrix (); |
|
2675 } |
|
2676 |
|
2677 if (nr == 0 || nc == 0) |
|
2678 return ComplexMatrix (nr, nc); |
|
2679 |
|
2680 return ComplexMatrix (subtract (m.data (), a.data (), m.length ()), nr, nc); |
|
2681 } |
|
2682 |
|
2683 ComplexMatrix |
458
|
2684 operator * (const ComplexMatrix& m, const Matrix& a) |
|
2685 { |
|
2686 ComplexMatrix tmp (a); |
|
2687 return m * tmp; |
|
2688 } |
|
2689 |
|
2690 ComplexMatrix |
1205
|
2691 operator * (const Matrix& m, const ComplexMatrix& a) |
|
2692 { |
|
2693 ComplexMatrix tmp (m); |
|
2694 return tmp * a; |
|
2695 } |
|
2696 |
|
2697 ComplexMatrix |
458
|
2698 operator * (const ComplexMatrix& m, const ComplexMatrix& a) |
|
2699 { |
1948
|
2700 ComplexMatrix retval; |
|
2701 |
458
|
2702 int nr = m.rows (); |
|
2703 int nc = m.cols (); |
1948
|
2704 |
458
|
2705 int a_nc = a.cols (); |
1948
|
2706 |
458
|
2707 if (nc != a.rows ()) |
1948
|
2708 (*current_liboctave_error_handler) |
|
2709 ("nonconformant matrix multiplication attempted"); |
|
2710 else |
458
|
2711 { |
1948
|
2712 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2713 retval.resize (nr, nc, 0.0); |
|
2714 else |
|
2715 { |
|
2716 int ld = nr; |
|
2717 int lda = a.rows (); |
|
2718 |
|
2719 retval.resize (nr, a_nc); |
|
2720 Complex *c = retval.fortran_vec (); |
|
2721 |
|
2722 F77_XFCN (zgemm, ZGEMM, ("N", "N", nr, a_nc, nc, 1.0, |
|
2723 m.data (), ld, a.data (), lda, 0.0, |
|
2724 c, nr, 1L, 1L)); |
|
2725 |
|
2726 if (f77_exception_encountered) |
|
2727 (*current_liboctave_error_handler) |
|
2728 ("unrecoverable error in zgemm"); |
|
2729 } |
458
|
2730 } |
|
2731 |
1948
|
2732 return retval; |
458
|
2733 } |
|
2734 |
|
2735 ComplexMatrix |
|
2736 product (const ComplexMatrix& m, const Matrix& a) |
|
2737 { |
|
2738 int nr = m.rows (); |
|
2739 int nc = m.cols (); |
|
2740 if (nr != a.rows () || nc != a.cols ()) |
|
2741 { |
|
2742 (*current_liboctave_error_handler) |
|
2743 ("nonconformant matrix product attempted"); |
|
2744 return ComplexMatrix (); |
|
2745 } |
|
2746 |
|
2747 if (nr == 0 || nc == 0) |
|
2748 return ComplexMatrix (nr, nc); |
|
2749 |
|
2750 return ComplexMatrix (multiply (m.data (), a.data (), m.length ()), nr, nc); |
|
2751 } |
|
2752 |
|
2753 ComplexMatrix |
|
2754 quotient (const ComplexMatrix& m, const Matrix& a) |
|
2755 { |
|
2756 int nr = m.rows (); |
|
2757 int nc = m.cols (); |
|
2758 if (nr != a.rows () || nc != a.cols ()) |
|
2759 { |
|
2760 (*current_liboctave_error_handler) |
|
2761 ("nonconformant matrix quotient attempted"); |
|
2762 return ComplexMatrix (); |
|
2763 } |
|
2764 |
|
2765 if (nr == 0 || nc == 0) |
|
2766 return ComplexMatrix (nr, nc); |
|
2767 |
|
2768 return ComplexMatrix (divide (m.data (), a.data (), m.length ()), nr, nc); |
|
2769 } |
|
2770 |
1205
|
2771 ComplexMatrix |
|
2772 product (const Matrix& m, const ComplexMatrix& a) |
|
2773 { |
|
2774 int nr = m.rows (); |
|
2775 int nc = m.cols (); |
|
2776 if (nr != a.rows () || nc != a.cols ()) |
|
2777 { |
|
2778 (*current_liboctave_error_handler) |
|
2779 ("nonconformant matrix product attempted"); |
|
2780 return ComplexMatrix (); |
|
2781 } |
|
2782 |
|
2783 if (nr == 0 || nc == 0) |
|
2784 return ComplexMatrix (nr, nc); |
|
2785 |
|
2786 return ComplexMatrix (multiply (m.data (), a.data (), m.length ()), nr, nc); |
|
2787 } |
|
2788 |
|
2789 ComplexMatrix |
|
2790 quotient (const Matrix& m, const ComplexMatrix& a) |
|
2791 { |
|
2792 int nr = m.rows (); |
|
2793 int nc = m.cols (); |
|
2794 if (nr != a.rows () || nc != a.cols ()) |
|
2795 { |
|
2796 (*current_liboctave_error_handler) |
|
2797 ("nonconformant matrix quotient attempted"); |
|
2798 return ComplexMatrix (); |
|
2799 } |
|
2800 |
|
2801 if (nr == 0 || nc == 0) |
|
2802 return ComplexMatrix (nr, nc); |
|
2803 |
|
2804 return ComplexMatrix (divide (m.data (), a.data (), m.length ()), nr, nc); |
|
2805 } |
|
2806 |
458
|
2807 // other operations |
|
2808 |
|
2809 ComplexMatrix |
|
2810 map (c_c_Mapper f, const ComplexMatrix& a) |
|
2811 { |
|
2812 ComplexMatrix b (a); |
|
2813 b.map (f); |
|
2814 return b; |
|
2815 } |
|
2816 |
|
2817 void |
|
2818 ComplexMatrix::map (c_c_Mapper f) |
|
2819 { |
|
2820 for (int j = 0; j < cols (); j++) |
|
2821 for (int i = 0; i < rows (); i++) |
|
2822 elem (i, j) = f (elem (i, j)); |
|
2823 } |
|
2824 |
1968
|
2825 // Return nonzero if any element of CM has a non-integer real or |
|
2826 // imaginary part. Also extract the largest and smallest (real or |
|
2827 // imaginary) values and return them in MAX_VAL and MIN_VAL. |
|
2828 |
|
2829 int |
|
2830 ComplexMatrix::all_integers (double& max_val, double& min_val) const |
|
2831 { |
|
2832 int nr = rows (); |
|
2833 int nc = columns (); |
|
2834 |
|
2835 if (nr > 0 && nc > 0) |
|
2836 { |
|
2837 Complex val = elem (0, 0); |
|
2838 |
|
2839 double r_val = real (val); |
|
2840 double i_val = imag (val); |
|
2841 |
|
2842 max_val = r_val; |
|
2843 min_val = r_val; |
|
2844 |
|
2845 if (i_val > max_val) |
|
2846 max_val = i_val; |
|
2847 |
|
2848 if (i_val < max_val) |
|
2849 min_val = i_val; |
|
2850 } |
|
2851 else |
|
2852 return 0; |
|
2853 |
|
2854 for (int j = 0; j < nc; j++) |
|
2855 for (int i = 0; i < nr; i++) |
|
2856 { |
|
2857 Complex val = elem (i, j); |
|
2858 |
|
2859 double r_val = real (val); |
|
2860 double i_val = imag (val); |
|
2861 |
|
2862 if (r_val > max_val) |
|
2863 max_val = r_val; |
|
2864 |
|
2865 if (i_val > max_val) |
|
2866 max_val = i_val; |
|
2867 |
|
2868 if (r_val < min_val) |
|
2869 min_val = r_val; |
|
2870 |
|
2871 if (i_val < min_val) |
|
2872 min_val = i_val; |
|
2873 |
|
2874 if (D_NINT (r_val) != r_val || D_NINT (i_val) != i_val) |
|
2875 return 0; |
|
2876 } |
|
2877 return 1; |
|
2878 } |
|
2879 |
|
2880 int |
|
2881 ComplexMatrix::too_large_for_float (void) const |
|
2882 { |
|
2883 int nr = rows (); |
|
2884 int nc = columns (); |
|
2885 |
|
2886 for (int j = 0; j < nc; j++) |
|
2887 for (int i = 0; i < nr; i++) |
|
2888 { |
|
2889 Complex val = elem (i, j); |
|
2890 |
|
2891 double r_val = real (val); |
|
2892 double i_val = imag (val); |
|
2893 |
|
2894 if (r_val > FLT_MAX |
|
2895 || i_val > FLT_MAX |
|
2896 || r_val < FLT_MIN |
|
2897 || i_val < FLT_MIN) |
|
2898 return 1; |
|
2899 } |
|
2900 |
|
2901 return 0; |
|
2902 } |
|
2903 |
458
|
2904 Matrix |
|
2905 ComplexMatrix::all (void) const |
|
2906 { |
|
2907 int nr = rows (); |
|
2908 int nc = cols (); |
|
2909 Matrix retval; |
|
2910 if (nr > 0 && nc > 0) |
|
2911 { |
|
2912 if (nr == 1) |
|
2913 { |
|
2914 retval.resize (1, 1); |
|
2915 retval.elem (0, 0) = 1.0; |
|
2916 for (int j = 0; j < nc; j++) |
|
2917 { |
|
2918 if (elem (0, j) == 0.0) |
|
2919 { |
|
2920 retval.elem (0, 0) = 0.0; |
|
2921 break; |
|
2922 } |
|
2923 } |
|
2924 } |
|
2925 else if (nc == 1) |
|
2926 { |
|
2927 retval.resize (1, 1); |
|
2928 retval.elem (0, 0) = 1.0; |
|
2929 for (int i = 0; i < nr; i++) |
|
2930 { |
|
2931 if (elem (i, 0) == 0.0) |
|
2932 { |
|
2933 retval.elem (0, 0) = 0.0; |
|
2934 break; |
|
2935 } |
|
2936 } |
|
2937 } |
|
2938 else |
|
2939 { |
|
2940 retval.resize (1, nc); |
|
2941 for (int j = 0; j < nc; j++) |
|
2942 { |
|
2943 retval.elem (0, j) = 1.0; |
|
2944 for (int i = 0; i < nr; i++) |
|
2945 { |
|
2946 if (elem (i, j) == 0.0) |
|
2947 { |
|
2948 retval.elem (0, j) = 0.0; |
|
2949 break; |
|
2950 } |
|
2951 } |
|
2952 } |
|
2953 } |
|
2954 } |
|
2955 return retval; |
|
2956 } |
|
2957 |
|
2958 Matrix |
|
2959 ComplexMatrix::any (void) const |
|
2960 { |
|
2961 int nr = rows (); |
|
2962 int nc = cols (); |
|
2963 Matrix retval; |
|
2964 if (nr > 0 && nc > 0) |
|
2965 { |
|
2966 if (nr == 1) |
|
2967 { |
|
2968 retval.resize (1, 1); |
|
2969 retval.elem (0, 0) = 0.0; |
|
2970 for (int j = 0; j < nc; j++) |
|
2971 { |
|
2972 if (elem (0, j) != 0.0) |
|
2973 { |
|
2974 retval.elem (0, 0) = 1.0; |
|
2975 break; |
|
2976 } |
|
2977 } |
|
2978 } |
|
2979 else if (nc == 1) |
|
2980 { |
|
2981 retval.resize (1, 1); |
|
2982 retval.elem (0, 0) = 0.0; |
|
2983 for (int i = 0; i < nr; i++) |
|
2984 { |
|
2985 if (elem (i, 0) != 0.0) |
|
2986 { |
|
2987 retval.elem (0, 0) = 1.0; |
|
2988 break; |
|
2989 } |
|
2990 } |
|
2991 } |
|
2992 else |
|
2993 { |
|
2994 retval.resize (1, nc); |
|
2995 for (int j = 0; j < nc; j++) |
|
2996 { |
|
2997 retval.elem (0, j) = 0.0; |
|
2998 for (int i = 0; i < nr; i++) |
|
2999 { |
|
3000 if (elem (i, j) != 0.0) |
|
3001 { |
|
3002 retval.elem (0, j) = 1.0; |
|
3003 break; |
|
3004 } |
|
3005 } |
|
3006 } |
|
3007 } |
|
3008 } |
|
3009 return retval; |
|
3010 } |
|
3011 |
|
3012 ComplexMatrix |
|
3013 ComplexMatrix::cumprod (void) const |
|
3014 { |
|
3015 int nr = rows (); |
|
3016 int nc = cols (); |
|
3017 ComplexMatrix retval; |
|
3018 if (nr > 0 && nc > 0) |
|
3019 { |
|
3020 if (nr == 1) |
|
3021 { |
|
3022 retval.resize (1, nc); |
|
3023 Complex prod = elem (0, 0); |
|
3024 for (int j = 0; j < nc; j++) |
|
3025 { |
|
3026 retval.elem (0, j) = prod; |
|
3027 if (j < nc - 1) |
|
3028 prod *= elem (0, j+1); |
|
3029 } |
|
3030 } |
|
3031 else if (nc == 1) |
|
3032 { |
|
3033 retval.resize (nr, 1); |
|
3034 Complex prod = elem (0, 0); |
|
3035 for (int i = 0; i < nr; i++) |
|
3036 { |
|
3037 retval.elem (i, 0) = prod; |
|
3038 if (i < nr - 1) |
|
3039 prod *= elem (i+1, 0); |
|
3040 } |
|
3041 } |
|
3042 else |
|
3043 { |
|
3044 retval.resize (nr, nc); |
|
3045 for (int j = 0; j < nc; j++) |
|
3046 { |
|
3047 Complex prod = elem (0, j); |
|
3048 for (int i = 0; i < nr; i++) |
|
3049 { |
|
3050 retval.elem (i, j) = prod; |
|
3051 if (i < nr - 1) |
|
3052 prod *= elem (i+1, j); |
|
3053 } |
|
3054 } |
|
3055 } |
|
3056 } |
|
3057 return retval; |
|
3058 } |
|
3059 |
|
3060 ComplexMatrix |
|
3061 ComplexMatrix::cumsum (void) const |
|
3062 { |
|
3063 int nr = rows (); |
|
3064 int nc = cols (); |
|
3065 ComplexMatrix retval; |
|
3066 if (nr > 0 && nc > 0) |
|
3067 { |
|
3068 if (nr == 1) |
|
3069 { |
|
3070 retval.resize (1, nc); |
|
3071 Complex sum = elem (0, 0); |
|
3072 for (int j = 0; j < nc; j++) |
|
3073 { |
|
3074 retval.elem (0, j) = sum; |
|
3075 if (j < nc - 1) |
|
3076 sum += elem (0, j+1); |
|
3077 } |
|
3078 } |
|
3079 else if (nc == 1) |
|
3080 { |
|
3081 retval.resize (nr, 1); |
|
3082 Complex sum = elem (0, 0); |
|
3083 for (int i = 0; i < nr; i++) |
|
3084 { |
|
3085 retval.elem (i, 0) = sum; |
|
3086 if (i < nr - 1) |
|
3087 sum += elem (i+1, 0); |
|
3088 } |
|
3089 } |
|
3090 else |
|
3091 { |
|
3092 retval.resize (nr, nc); |
|
3093 for (int j = 0; j < nc; j++) |
|
3094 { |
|
3095 Complex sum = elem (0, j); |
|
3096 for (int i = 0; i < nr; i++) |
|
3097 { |
|
3098 retval.elem (i, j) = sum; |
|
3099 if (i < nr - 1) |
|
3100 sum += elem (i+1, j); |
|
3101 } |
|
3102 } |
|
3103 } |
|
3104 } |
|
3105 return retval; |
|
3106 } |
|
3107 |
|
3108 ComplexMatrix |
|
3109 ComplexMatrix::prod (void) const |
|
3110 { |
|
3111 int nr = rows (); |
|
3112 int nc = cols (); |
|
3113 ComplexMatrix retval; |
|
3114 if (nr > 0 && nc > 0) |
|
3115 { |
|
3116 if (nr == 1) |
|
3117 { |
|
3118 retval.resize (1, 1); |
|
3119 retval.elem (0, 0) = 1.0; |
|
3120 for (int j = 0; j < nc; j++) |
|
3121 retval.elem (0, 0) *= elem (0, j); |
|
3122 } |
|
3123 else if (nc == 1) |
|
3124 { |
|
3125 retval.resize (1, 1); |
|
3126 retval.elem (0, 0) = 1.0; |
|
3127 for (int i = 0; i < nr; i++) |
|
3128 retval.elem (0, 0) *= elem (i, 0); |
|
3129 } |
|
3130 else |
|
3131 { |
|
3132 retval.resize (1, nc); |
|
3133 for (int j = 0; j < nc; j++) |
|
3134 { |
|
3135 retval.elem (0, j) = 1.0; |
|
3136 for (int i = 0; i < nr; i++) |
|
3137 retval.elem (0, j) *= elem (i, j); |
|
3138 } |
|
3139 } |
|
3140 } |
|
3141 return retval; |
|
3142 } |
|
3143 |
|
3144 ComplexMatrix |
|
3145 ComplexMatrix::sum (void) const |
|
3146 { |
|
3147 int nr = rows (); |
|
3148 int nc = cols (); |
|
3149 ComplexMatrix retval; |
|
3150 if (nr > 0 && nc > 0) |
|
3151 { |
|
3152 if (nr == 1) |
|
3153 { |
|
3154 retval.resize (1, 1); |
|
3155 retval.elem (0, 0) = 0.0; |
|
3156 for (int j = 0; j < nc; j++) |
|
3157 retval.elem (0, 0) += elem (0, j); |
|
3158 } |
|
3159 else if (nc == 1) |
|
3160 { |
|
3161 retval.resize (1, 1); |
|
3162 retval.elem (0, 0) = 0.0; |
|
3163 for (int i = 0; i < nr; i++) |
|
3164 retval.elem (0, 0) += elem (i, 0); |
|
3165 } |
|
3166 else |
|
3167 { |
|
3168 retval.resize (1, nc); |
|
3169 for (int j = 0; j < nc; j++) |
|
3170 { |
|
3171 retval.elem (0, j) = 0.0; |
|
3172 for (int i = 0; i < nr; i++) |
|
3173 retval.elem (0, j) += elem (i, j); |
|
3174 } |
|
3175 } |
|
3176 } |
|
3177 return retval; |
|
3178 } |
|
3179 |
|
3180 ComplexMatrix |
|
3181 ComplexMatrix::sumsq (void) const |
|
3182 { |
|
3183 int nr = rows (); |
|
3184 int nc = cols (); |
|
3185 ComplexMatrix retval; |
|
3186 if (nr > 0 && nc > 0) |
|
3187 { |
|
3188 if (nr == 1) |
|
3189 { |
|
3190 retval.resize (1, 1); |
|
3191 retval.elem (0, 0) = 0.0; |
|
3192 for (int j = 0; j < nc; j++) |
|
3193 { |
|
3194 Complex d = elem (0, j); |
|
3195 retval.elem (0, 0) += d * d; |
|
3196 } |
|
3197 } |
|
3198 else if (nc == 1) |
|
3199 { |
|
3200 retval.resize (1, 1); |
|
3201 retval.elem (0, 0) = 0.0; |
|
3202 for (int i = 0; i < nr; i++) |
|
3203 { |
|
3204 Complex d = elem (i, 0); |
|
3205 retval.elem (0, 0) += d * d; |
|
3206 } |
|
3207 } |
|
3208 else |
|
3209 { |
|
3210 retval.resize (1, nc); |
|
3211 for (int j = 0; j < nc; j++) |
|
3212 { |
|
3213 retval.elem (0, j) = 0.0; |
|
3214 for (int i = 0; i < nr; i++) |
|
3215 { |
|
3216 Complex d = elem (i, j); |
|
3217 retval.elem (0, j) += d * d; |
|
3218 } |
|
3219 } |
|
3220 } |
|
3221 } |
|
3222 return retval; |
|
3223 } |
|
3224 |
|
3225 ComplexColumnVector |
|
3226 ComplexMatrix::diag (void) const |
|
3227 { |
|
3228 return diag (0); |
|
3229 } |
|
3230 |
|
3231 ComplexColumnVector |
|
3232 ComplexMatrix::diag (int k) const |
|
3233 { |
|
3234 int nnr = rows (); |
|
3235 int nnc = cols (); |
|
3236 if (k > 0) |
|
3237 nnc -= k; |
|
3238 else if (k < 0) |
|
3239 nnr += k; |
|
3240 |
|
3241 ComplexColumnVector d; |
|
3242 |
|
3243 if (nnr > 0 && nnc > 0) |
|
3244 { |
|
3245 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
3246 |
|
3247 d.resize (ndiag); |
|
3248 |
|
3249 if (k > 0) |
|
3250 { |
|
3251 for (int i = 0; i < ndiag; i++) |
|
3252 d.elem (i) = elem (i, i+k); |
|
3253 } |
|
3254 else if ( k < 0) |
|
3255 { |
|
3256 for (int i = 0; i < ndiag; i++) |
|
3257 d.elem (i) = elem (i-k, i); |
|
3258 } |
|
3259 else |
|
3260 { |
|
3261 for (int i = 0; i < ndiag; i++) |
|
3262 d.elem (i) = elem (i, i); |
|
3263 } |
|
3264 } |
|
3265 else |
|
3266 cerr << "diag: requested diagonal out of range\n"; |
|
3267 |
|
3268 return d; |
|
3269 } |
|
3270 |
891
|
3271 // XXX FIXME XXX -- it would be nice to share some code among all the |
|
3272 // min/max functions below. It would also be nice to combine the |
|
3273 // min/max and min_loc/max_loc functions. |
|
3274 |
458
|
3275 ComplexColumnVector |
|
3276 ComplexMatrix::row_min (void) const |
|
3277 { |
|
3278 ComplexColumnVector result; |
|
3279 |
|
3280 int nr = rows (); |
|
3281 int nc = cols (); |
|
3282 if (nr > 0 && nc > 0) |
|
3283 { |
|
3284 result.resize (nr); |
|
3285 |
|
3286 for (int i = 0; i < nr; i++) |
|
3287 { |
891
|
3288 int row_is_real_only = 1; |
|
3289 for (int j = 0; j < nc; j++) |
|
3290 if (imag (elem (i, j)) != 0.0) |
458
|
3291 { |
891
|
3292 row_is_real_only = 0; |
|
3293 break; |
458
|
3294 } |
891
|
3295 |
|
3296 if (row_is_real_only) |
|
3297 { |
|
3298 double res = real (elem (i, 0)); |
|
3299 for (int j = 1; j < nc; j++) |
|
3300 { |
|
3301 double tmp = real (elem (i, j)); |
|
3302 if (tmp < res) |
|
3303 res = tmp; |
|
3304 } |
|
3305 result.elem (i) = res; |
|
3306 } |
|
3307 else |
|
3308 { |
|
3309 Complex res = elem (i, 0); |
|
3310 double absres = abs (res); |
|
3311 for (int j = 1; j < nc; j++) |
|
3312 if (abs (elem (i, j)) < absres) |
|
3313 { |
|
3314 res = elem (i, j); |
|
3315 absres = abs (res); |
|
3316 } |
|
3317 result.elem (i) = res; |
|
3318 } |
458
|
3319 } |
|
3320 } |
|
3321 |
|
3322 return result; |
|
3323 } |
|
3324 |
|
3325 ComplexColumnVector |
|
3326 ComplexMatrix::row_min_loc (void) const |
|
3327 { |
|
3328 ComplexColumnVector result; |
|
3329 |
|
3330 int nr = rows (); |
|
3331 int nc = cols (); |
|
3332 |
|
3333 if (nr > 0 && nc > 0) |
|
3334 { |
|
3335 result.resize (nr); |
|
3336 |
|
3337 for (int i = 0; i < nr; i++) |
|
3338 { |
891
|
3339 int column_is_real_only = 1; |
|
3340 for (int j = 0; j < nc; j++) |
|
3341 if (imag (elem (i, j)) != 0.0) |
|
3342 { |
|
3343 column_is_real_only = 0; |
|
3344 break; |
|
3345 } |
|
3346 |
|
3347 if (column_is_real_only) |
|
3348 { |
|
3349 double res = 0; |
|
3350 double tmp = real (elem (i, 0)); |
|
3351 for (int j = 1; j < nc; j++) |
|
3352 if (real (elem (i, j)) < tmp) |
|
3353 res = j; |
|
3354 |
|
3355 result.elem (i) = res + 1; |
|
3356 } |
|
3357 else |
|
3358 { |
|
3359 Complex res = 0; |
|
3360 double absres = abs (elem (i, 0)); |
|
3361 for (int j = 1; j < nc; j++) |
|
3362 if (abs (elem (i, j)) < absres) |
|
3363 { |
|
3364 res = j; |
|
3365 absres = abs (elem (i, j)); |
|
3366 } |
|
3367 result.elem (i) = res + 1; |
|
3368 } |
458
|
3369 } |
|
3370 } |
|
3371 |
|
3372 return result; |
|
3373 } |
|
3374 |
|
3375 ComplexColumnVector |
|
3376 ComplexMatrix::row_max (void) const |
|
3377 { |
|
3378 ComplexColumnVector result; |
|
3379 |
|
3380 int nr = rows (); |
|
3381 int nc = cols (); |
|
3382 |
|
3383 if (nr > 0 && nc > 0) |
|
3384 { |
|
3385 result.resize (nr); |
|
3386 |
|
3387 for (int i = 0; i < nr; i++) |
|
3388 { |
891
|
3389 int row_is_real_only = 1; |
|
3390 for (int j = 0; j < nc; j++) |
|
3391 if (imag (elem (i, j)) != 0.0) |
458
|
3392 { |
891
|
3393 row_is_real_only = 0; |
|
3394 break; |
458
|
3395 } |
891
|
3396 |
|
3397 if (row_is_real_only) |
|
3398 { |
|
3399 double res = real (elem (i, 0)); |
|
3400 for (int j = 1; j < nc; j++) |
|
3401 { |
|
3402 double tmp = real (elem (i, j)); |
|
3403 if (tmp > res) |
|
3404 res = tmp; |
|
3405 } |
|
3406 result.elem (i) = res; |
|
3407 } |
|
3408 else |
|
3409 { |
|
3410 Complex res = elem (i, 0); |
|
3411 double absres = abs (res); |
|
3412 for (int j = 1; j < nc; j++) |
|
3413 if (abs (elem (i, j)) > absres) |
|
3414 { |
|
3415 res = elem (i, j); |
|
3416 absres = abs (res); |
|
3417 } |
|
3418 result.elem (i) = res; |
|
3419 } |
458
|
3420 } |
|
3421 } |
|
3422 |
|
3423 return result; |
|
3424 } |
|
3425 |
|
3426 ComplexColumnVector |
|
3427 ComplexMatrix::row_max_loc (void) const |
|
3428 { |
|
3429 ComplexColumnVector result; |
|
3430 |
|
3431 int nr = rows (); |
|
3432 int nc = cols (); |
|
3433 |
|
3434 if (nr > 0 && nc > 0) |
|
3435 { |
|
3436 result.resize (nr); |
|
3437 |
|
3438 for (int i = 0; i < nr; i++) |
|
3439 { |
891
|
3440 int column_is_real_only = 1; |
|
3441 for (int j = 0; j < nc; j++) |
|
3442 if (imag (elem (i, j)) != 0.0) |
|
3443 { |
|
3444 column_is_real_only = 0; |
|
3445 break; |
|
3446 } |
|
3447 |
|
3448 if (column_is_real_only) |
|
3449 { |
|
3450 double res = 0; |
|
3451 double tmp = real (elem (i, 0)); |
|
3452 for (int j = 1; j < nc; j++) |
|
3453 if (real (elem (i, j)) > tmp) |
|
3454 res = j; |
|
3455 |
|
3456 result.elem (i) = res + 1; |
|
3457 } |
|
3458 else |
|
3459 { |
|
3460 Complex res = 0; |
|
3461 double absres = abs (elem (i, 0)); |
|
3462 for (int j = 1; j < nc; j++) |
|
3463 if (abs (elem (i, j)) > absres) |
|
3464 { |
|
3465 res = j; |
|
3466 absres = abs (elem (i, j)); |
|
3467 } |
|
3468 result.elem (i) = res + 1; |
|
3469 } |
458
|
3470 } |
|
3471 } |
|
3472 |
|
3473 return result; |
|
3474 } |
|
3475 |
|
3476 ComplexRowVector |
|
3477 ComplexMatrix::column_min (void) const |
|
3478 { |
|
3479 ComplexRowVector result; |
|
3480 |
|
3481 int nr = rows (); |
|
3482 int nc = cols (); |
|
3483 |
|
3484 if (nr > 0 && nc > 0) |
|
3485 { |
|
3486 result.resize (nc); |
|
3487 |
|
3488 for (int j = 0; j < nc; j++) |
|
3489 { |
891
|
3490 int column_is_real_only = 1; |
|
3491 for (int i = 0; i < nr; i++) |
|
3492 if (imag (elem (i, j)) != 0.0) |
458
|
3493 { |
891
|
3494 column_is_real_only = 0; |
|
3495 break; |
458
|
3496 } |
891
|
3497 |
|
3498 if (column_is_real_only) |
|
3499 { |
|
3500 double res = real (elem (0, j)); |
|
3501 for (int i = 1; i < nr; i++) |
|
3502 { |
|
3503 double tmp = real (elem (i, j)); |
|
3504 if (tmp < res) |
|
3505 res = tmp; |
|
3506 } |
|
3507 result.elem (j) = res; |
|
3508 } |
|
3509 else |
|
3510 { |
|
3511 Complex res = elem (0, j); |
|
3512 double absres = abs (res); |
|
3513 for (int i = 1; i < nr; i++) |
|
3514 if (abs (elem (i, j)) < absres) |
|
3515 { |
|
3516 res = elem (i, j); |
|
3517 absres = abs (res); |
|
3518 } |
|
3519 result.elem (j) = res; |
|
3520 } |
458
|
3521 } |
|
3522 } |
|
3523 |
|
3524 return result; |
|
3525 } |
|
3526 |
|
3527 ComplexRowVector |
|
3528 ComplexMatrix::column_min_loc (void) const |
|
3529 { |
|
3530 ComplexRowVector result; |
|
3531 |
|
3532 int nr = rows (); |
|
3533 int nc = cols (); |
|
3534 |
|
3535 if (nr > 0 && nc > 0) |
|
3536 { |
|
3537 result.resize (nc); |
|
3538 |
|
3539 for (int j = 0; j < nc; j++) |
|
3540 { |
891
|
3541 int column_is_real_only = 1; |
|
3542 for (int i = 0; i < nr; i++) |
|
3543 if (imag (elem (i, j)) != 0.0) |
|
3544 { |
|
3545 column_is_real_only = 0; |
|
3546 break; |
|
3547 } |
|
3548 |
|
3549 if (column_is_real_only) |
|
3550 { |
|
3551 double res = 0; |
892
|
3552 double tmp = real (elem (0, j)); |
891
|
3553 for (int i = 1; i < nr; i++) |
|
3554 if (real (elem (i, j)) < tmp) |
|
3555 res = i; |
|
3556 |
|
3557 result.elem (j) = res + 1; |
|
3558 } |
|
3559 else |
|
3560 { |
|
3561 Complex res = 0; |
|
3562 double absres = abs (elem (0, j)); |
|
3563 for (int i = 1; i < nr; i++) |
|
3564 if (abs (elem (i, j)) < absres) |
|
3565 { |
|
3566 res = i; |
|
3567 absres = abs (elem (i, j)); |
|
3568 } |
|
3569 result.elem (j) = res + 1; |
|
3570 } |
458
|
3571 } |
|
3572 } |
|
3573 |
|
3574 return result; |
|
3575 } |
|
3576 |
|
3577 ComplexRowVector |
|
3578 ComplexMatrix::column_max (void) const |
|
3579 { |
|
3580 ComplexRowVector result; |
|
3581 |
|
3582 int nr = rows (); |
|
3583 int nc = cols (); |
|
3584 |
|
3585 if (nr > 0 && nc > 0) |
|
3586 { |
|
3587 result.resize (nc); |
|
3588 |
|
3589 for (int j = 0; j < nc; j++) |
|
3590 { |
891
|
3591 int column_is_real_only = 1; |
|
3592 for (int i = 0; i < nr; i++) |
|
3593 if (imag (elem (i, j)) != 0.0) |
458
|
3594 { |
891
|
3595 column_is_real_only = 0; |
|
3596 break; |
458
|
3597 } |
891
|
3598 |
|
3599 if (column_is_real_only) |
|
3600 { |
|
3601 double res = real (elem (0, j)); |
|
3602 for (int i = 1; i < nr; i++) |
|
3603 { |
|
3604 double tmp = real (elem (i, j)); |
|
3605 if (tmp > res) |
|
3606 res = tmp; |
|
3607 } |
|
3608 result.elem (j) = res; |
|
3609 } |
|
3610 else |
|
3611 { |
|
3612 Complex res = elem (0, j); |
|
3613 double absres = abs (res); |
|
3614 for (int i = 1; i < nr; i++) |
|
3615 if (abs (elem (i, j)) > absres) |
|
3616 { |
|
3617 res = elem (i, j); |
|
3618 absres = abs (res); |
|
3619 } |
|
3620 result.elem (j) = res; |
|
3621 } |
458
|
3622 } |
|
3623 } |
|
3624 |
|
3625 return result; |
|
3626 } |
|
3627 |
|
3628 ComplexRowVector |
|
3629 ComplexMatrix::column_max_loc (void) const |
|
3630 { |
|
3631 ComplexRowVector result; |
|
3632 |
|
3633 int nr = rows (); |
|
3634 int nc = cols (); |
|
3635 |
|
3636 if (nr > 0 && nc > 0) |
|
3637 { |
|
3638 result.resize (nc); |
|
3639 |
|
3640 for (int j = 0; j < nc; j++) |
|
3641 { |
891
|
3642 int column_is_real_only = 1; |
|
3643 for (int i = 0; i < nr; i++) |
|
3644 if (imag (elem (i, j)) != 0.0) |
|
3645 { |
|
3646 column_is_real_only = 0; |
|
3647 break; |
|
3648 } |
|
3649 |
|
3650 if (column_is_real_only) |
|
3651 { |
|
3652 double res = 0; |
892
|
3653 double tmp = real (elem (0, j)); |
891
|
3654 for (int i = 1; i < nr; i++) |
|
3655 if (real (elem (i, j)) > tmp) |
|
3656 res = i; |
|
3657 |
|
3658 result.elem (j) = res + 1; |
|
3659 } |
|
3660 else |
|
3661 { |
|
3662 Complex res = 0; |
|
3663 double absres = abs (elem (0, j)); |
|
3664 for (int i = 1; i < nr; i++) |
|
3665 if (abs (elem (i, j)) > absres) |
|
3666 { |
|
3667 res = i; |
|
3668 absres = abs (elem (i, j)); |
|
3669 } |
|
3670 result.elem (j) = res + 1; |
|
3671 } |
458
|
3672 } |
|
3673 } |
|
3674 |
|
3675 return result; |
|
3676 } |
|
3677 |
|
3678 // i/o |
|
3679 |
|
3680 ostream& |
|
3681 operator << (ostream& os, const ComplexMatrix& a) |
|
3682 { |
|
3683 // int field_width = os.precision () + 7; |
|
3684 for (int i = 0; i < a.rows (); i++) |
|
3685 { |
|
3686 for (int j = 0; j < a.cols (); j++) |
|
3687 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
3688 os << "\n"; |
|
3689 } |
|
3690 return os; |
|
3691 } |
|
3692 |
|
3693 istream& |
|
3694 operator >> (istream& is, ComplexMatrix& a) |
|
3695 { |
|
3696 int nr = a.rows (); |
|
3697 int nc = a.cols (); |
|
3698 |
|
3699 if (nr < 1 || nc < 1) |
|
3700 is.clear (ios::badbit); |
|
3701 else |
|
3702 { |
|
3703 Complex tmp; |
|
3704 for (int i = 0; i < nr; i++) |
|
3705 for (int j = 0; j < nc; j++) |
|
3706 { |
|
3707 is >> tmp; |
|
3708 if (is) |
|
3709 a.elem (i, j) = tmp; |
|
3710 else |
|
3711 break; |
|
3712 } |
|
3713 } |
|
3714 |
|
3715 return is; |
|
3716 } |
|
3717 |
1819
|
3718 ComplexMatrix |
|
3719 Givens (const Complex& x, const Complex& y) |
|
3720 { |
|
3721 double cc; |
|
3722 Complex cs, temp_r; |
|
3723 |
|
3724 F77_FCN (zlartg, ZLARTG) (x, y, cc, cs, temp_r); |
|
3725 |
|
3726 ComplexMatrix g (2, 2); |
|
3727 |
|
3728 g.elem (0, 0) = cc; |
|
3729 g.elem (1, 1) = cc; |
|
3730 g.elem (0, 1) = cs; |
|
3731 g.elem (1, 0) = -conj (cs); |
|
3732 |
|
3733 return g; |
|
3734 } |
|
3735 |
|
3736 ComplexMatrix |
|
3737 Sylvester (const ComplexMatrix& a, const ComplexMatrix& b, |
|
3738 const ComplexMatrix& c) |
|
3739 { |
|
3740 ComplexMatrix retval; |
|
3741 |
|
3742 // XXX FIXME XXX -- need to check that a, b, and c are all the same |
|
3743 // size. |
|
3744 |
|
3745 // Compute Schur decompositions |
|
3746 |
|
3747 ComplexSCHUR as (a, "U"); |
|
3748 ComplexSCHUR bs (b, "U"); |
|
3749 |
|
3750 // Transform c to new coordinates. |
|
3751 |
|
3752 ComplexMatrix ua = as.unitary_matrix (); |
|
3753 ComplexMatrix sch_a = as.schur_matrix (); |
|
3754 |
|
3755 ComplexMatrix ub = bs.unitary_matrix (); |
|
3756 ComplexMatrix sch_b = bs.schur_matrix (); |
|
3757 |
|
3758 ComplexMatrix cx = ua.hermitian () * c * ub; |
|
3759 |
|
3760 // Solve the sylvester equation, back-transform, and return the |
|
3761 // solution. |
|
3762 |
|
3763 int a_nr = a.rows (); |
|
3764 int b_nr = b.rows (); |
|
3765 |
|
3766 double scale; |
|
3767 int info; |
1950
|
3768 |
|
3769 Complex *pa = sch_a.fortran_vec (); |
|
3770 Complex *pb = sch_b.fortran_vec (); |
|
3771 Complex *px = cx.fortran_vec (); |
1819
|
3772 |
1950
|
3773 F77_XFCN (ztrsyl, ZTRSYL, ("N", "N", 1, a_nr, b_nr, pa, a_nr, pb, |
|
3774 b_nr, px, a_nr, scale, |
|
3775 info, 1L, 1L)); |
|
3776 |
|
3777 if (f77_exception_encountered) |
|
3778 (*current_liboctave_error_handler) ("unrecoverable error in ztrsyl"); |
|
3779 else |
|
3780 { |
|
3781 // XXX FIXME XXX -- check info? |
|
3782 |
|
3783 retval = -ua * cx * ub.hermitian (); |
|
3784 } |
1819
|
3785 |
|
3786 return retval; |
|
3787 } |
|
3788 |
458
|
3789 /* |
|
3790 ;;; Local Variables: *** |
|
3791 ;;; mode: C++ *** |
|
3792 ;;; page-delimiter: "^/\\*" *** |
|
3793 ;;; End: *** |
|
3794 */ |