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