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