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