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