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