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