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