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