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