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