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