1993
|
1 // Matrix manipulations. |
458
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
458
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
21 02110-1301, USA. |
458
|
22 |
|
23 */ |
|
24 |
|
25 #ifdef HAVE_CONFIG_H |
1192
|
26 #include <config.h> |
458
|
27 #endif |
|
28 |
1367
|
29 #include <cfloat> |
|
30 |
3503
|
31 #include <iostream> |
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 |
|
1205 if (rcond == 0.) |
|
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 |
6699
|
2630 if (numel () == 1) |
|
2631 return ComplexMatrix (1, 1, exp (m(0))); |
|
2632 |
5275
|
2633 octave_idx_type nc = columns (); |
1819
|
2634 |
3130
|
2635 // Preconditioning step 1: trace normalization to reduce dynamic |
|
2636 // range of poles, but avoid making stable eigenvalues unstable. |
|
2637 |
1819
|
2638 // trace shift value |
|
2639 Complex trshift = 0.0; |
|
2640 |
5275
|
2641 for (octave_idx_type i = 0; i < nc; i++) |
1819
|
2642 trshift += m.elem (i, i); |
|
2643 |
|
2644 trshift /= nc; |
|
2645 |
3130
|
2646 if (trshift.real () < 0.0) |
|
2647 trshift = trshift.imag (); |
|
2648 |
5275
|
2649 for (octave_idx_type i = 0; i < nc; i++) |
1819
|
2650 m.elem (i, i) -= trshift; |
|
2651 |
|
2652 // Preconditioning step 2: eigenvalue balancing. |
3331
|
2653 // code follows development in AEPBAL |
|
2654 |
|
2655 Complex *mp = m.fortran_vec (); |
3467
|
2656 |
5275
|
2657 octave_idx_type info, ilo, ihi,ilos,ihis; |
3468
|
2658 Array<double> dpermute (nc); |
|
2659 Array<double> dscale (nc); |
|
2660 |
5775
|
2661 // FIXME -- should pass job as a parameter in expm |
3468
|
2662 |
|
2663 // Permute first |
|
2664 char job = 'P'; |
4552
|
2665 F77_XFCN (zgebal, ZGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
2666 nc, mp, nc, ilo, ihi, |
|
2667 dpermute.fortran_vec (), info |
|
2668 F77_CHAR_ARG_LEN (1))); |
3331
|
2669 |
|
2670 if (f77_exception_encountered) |
|
2671 { |
|
2672 (*current_liboctave_error_handler) ("unrecoverable error in zgebal"); |
|
2673 return retval; |
|
2674 } |
|
2675 |
3468
|
2676 // then scale |
|
2677 job = 'S'; |
4552
|
2678 F77_XFCN (zgebal, ZGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
2679 nc, mp, nc, ilos, ihis, |
|
2680 dscale.fortran_vec (), info |
|
2681 F77_CHAR_ARG_LEN (1))); |
3331
|
2682 |
|
2683 if (f77_exception_encountered) |
|
2684 { |
3467
|
2685 (*current_liboctave_error_handler) ("unrecoverable error in zgebal"); |
3331
|
2686 return retval; |
|
2687 } |
1819
|
2688 |
|
2689 // Preconditioning step 3: scaling. |
|
2690 |
|
2691 ColumnVector work (nc); |
3130
|
2692 double inf_norm; |
|
2693 |
4552
|
2694 F77_XFCN (xzlange, XZLANGE, (F77_CONST_CHAR_ARG2 ("I", 1), |
|
2695 nc, nc, m.fortran_vec (), nc, |
|
2696 work.fortran_vec (), inf_norm |
|
2697 F77_CHAR_ARG_LEN (1))); |
3331
|
2698 |
|
2699 if (f77_exception_encountered) |
|
2700 { |
|
2701 (*current_liboctave_error_handler) ("unrecoverable error in zlange"); |
|
2702 return retval; |
|
2703 } |
1819
|
2704 |
2800
|
2705 int sqpow = (inf_norm > 0.0 |
|
2706 ? static_cast<int> (1.0 + log (inf_norm) / log (2.0)) : 0); |
1819
|
2707 |
|
2708 // Check whether we need to square at all. |
|
2709 |
|
2710 if (sqpow < 0) |
|
2711 sqpow = 0; |
|
2712 |
|
2713 if (sqpow > 0) |
|
2714 { |
|
2715 double scale_factor = 1.0; |
5275
|
2716 for (octave_idx_type i = 0; i < sqpow; i++) |
1819
|
2717 scale_factor *= 2.0; |
|
2718 |
|
2719 m = m / scale_factor; |
|
2720 } |
|
2721 |
|
2722 // npp, dpp: pade' approx polynomial matrices. |
|
2723 |
|
2724 ComplexMatrix npp (nc, nc, 0.0); |
|
2725 ComplexMatrix dpp = npp; |
|
2726 |
|
2727 // Now powers a^8 ... a^1. |
|
2728 |
|
2729 int minus_one_j = -1; |
5275
|
2730 for (octave_idx_type j = 7; j >= 0; j--) |
1819
|
2731 { |
|
2732 npp = m * npp + m * padec[j]; |
|
2733 dpp = m * dpp + m * (minus_one_j * padec[j]); |
|
2734 minus_one_j *= -1; |
|
2735 } |
|
2736 |
|
2737 // Zero power. |
|
2738 |
|
2739 dpp = -dpp; |
5275
|
2740 for (octave_idx_type j = 0; j < nc; j++) |
1819
|
2741 { |
|
2742 npp.elem (j, j) += 1.0; |
|
2743 dpp.elem (j, j) += 1.0; |
|
2744 } |
|
2745 |
|
2746 // Compute pade approximation = inverse (dpp) * npp. |
|
2747 |
|
2748 retval = dpp.solve (npp); |
|
2749 |
|
2750 // Reverse preconditioning step 3: repeated squaring. |
|
2751 |
|
2752 while (sqpow) |
|
2753 { |
|
2754 retval = retval * retval; |
|
2755 sqpow--; |
|
2756 } |
|
2757 |
|
2758 // Reverse preconditioning step 2: inverse balancing. |
3467
|
2759 // Done in two steps: inverse scaling, then inverse permutation |
|
2760 |
|
2761 // inverse scaling (diagonal transformation) |
5275
|
2762 for (octave_idx_type i = 0; i < nc; i++) |
|
2763 for (octave_idx_type j = 0; j < nc; j++) |
3468
|
2764 retval(i,j) *= dscale(i) / dscale(j); |
3467
|
2765 |
4153
|
2766 OCTAVE_QUIT; |
|
2767 |
3467
|
2768 // construct balancing permutation vector |
6867
|
2769 Array<octave_idx_type> iperm (nc); |
5275
|
2770 for (octave_idx_type i = 0; i < nc; i++) |
4593
|
2771 iperm(i) = i; // initialize to identity permutation |
3467
|
2772 |
|
2773 // leading permutations in forward order |
5275
|
2774 for (octave_idx_type i = 0; i < (ilo-1); i++) |
3468
|
2775 { |
6867
|
2776 octave_idx_type swapidx = static_cast<octave_idx_type> (dpermute(i)) - 1; |
5275
|
2777 octave_idx_type tmp = iperm(i); |
4593
|
2778 iperm(i) = iperm(swapidx); |
|
2779 iperm(swapidx) = tmp; |
3468
|
2780 } |
3467
|
2781 |
|
2782 // trailing permutations must be done in reverse order |
5275
|
2783 for (octave_idx_type i = nc - 1; i >= ihi; 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 // construct inverse balancing permutation vector |
6867
|
2792 Array<octave_idx_type> invpvec (nc); |
5275
|
2793 for (octave_idx_type i = 0; i < nc; i++) |
4593
|
2794 invpvec(iperm(i)) = i; // Thanks to R. A. Lippert for this method |
3467
|
2795 |
4153
|
2796 OCTAVE_QUIT; |
|
2797 |
3467
|
2798 ComplexMatrix tmpMat = retval; |
5275
|
2799 for (octave_idx_type i = 0; i < nc; i++) |
|
2800 for (octave_idx_type j = 0; j < nc; j++) |
3468
|
2801 retval(i,j) = tmpMat(invpvec(i),invpvec(j)); |
1819
|
2802 |
|
2803 // Reverse preconditioning step 1: fix trace normalization. |
|
2804 |
3130
|
2805 return exp (trshift) * retval; |
1819
|
2806 } |
|
2807 |
1205
|
2808 // column vector by row vector -> matrix operations |
|
2809 |
|
2810 ComplexMatrix |
|
2811 operator * (const ColumnVector& v, const ComplexRowVector& a) |
|
2812 { |
|
2813 ComplexColumnVector tmp (v); |
|
2814 return tmp * a; |
|
2815 } |
|
2816 |
|
2817 ComplexMatrix |
|
2818 operator * (const ComplexColumnVector& a, const RowVector& b) |
|
2819 { |
|
2820 ComplexRowVector tmp (b); |
|
2821 return a * tmp; |
|
2822 } |
|
2823 |
|
2824 ComplexMatrix |
|
2825 operator * (const ComplexColumnVector& v, const ComplexRowVector& a) |
|
2826 { |
1948
|
2827 ComplexMatrix retval; |
|
2828 |
5275
|
2829 octave_idx_type len = v.length (); |
3233
|
2830 |
|
2831 if (len != 0) |
1205
|
2832 { |
5275
|
2833 octave_idx_type a_len = a.length (); |
3233
|
2834 |
|
2835 retval.resize (len, a_len); |
|
2836 Complex *c = retval.fortran_vec (); |
|
2837 |
4552
|
2838 F77_XFCN (zgemm, ZGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
2839 F77_CONST_CHAR_ARG2 ("N", 1), |
|
2840 len, a_len, 1, 1.0, v.data (), len, |
|
2841 a.data (), 1, 0.0, c, len |
|
2842 F77_CHAR_ARG_LEN (1) |
|
2843 F77_CHAR_ARG_LEN (1))); |
3233
|
2844 |
|
2845 if (f77_exception_encountered) |
|
2846 (*current_liboctave_error_handler) |
|
2847 ("unrecoverable error in zgemm"); |
1205
|
2848 } |
|
2849 |
1948
|
2850 return retval; |
1205
|
2851 } |
|
2852 |
458
|
2853 // matrix by diagonal matrix -> matrix operations |
|
2854 |
|
2855 ComplexMatrix& |
|
2856 ComplexMatrix::operator += (const DiagMatrix& a) |
|
2857 { |
5275
|
2858 octave_idx_type nr = rows (); |
|
2859 octave_idx_type nc = cols (); |
|
2860 |
|
2861 octave_idx_type a_nr = rows (); |
|
2862 octave_idx_type a_nc = cols (); |
2384
|
2863 |
|
2864 if (nr != a_nr || nc != a_nc) |
458
|
2865 { |
2384
|
2866 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889
|
2867 return *this; |
458
|
2868 } |
|
2869 |
5275
|
2870 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
2871 elem (i, i) += a.elem (i, i); |
|
2872 |
|
2873 return *this; |
|
2874 } |
|
2875 |
|
2876 ComplexMatrix& |
|
2877 ComplexMatrix::operator -= (const DiagMatrix& a) |
|
2878 { |
5275
|
2879 octave_idx_type nr = rows (); |
|
2880 octave_idx_type nc = cols (); |
|
2881 |
|
2882 octave_idx_type a_nr = rows (); |
|
2883 octave_idx_type a_nc = cols (); |
2384
|
2884 |
|
2885 if (nr != a_nr || nc != a_nc) |
458
|
2886 { |
2384
|
2887 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889
|
2888 return *this; |
458
|
2889 } |
|
2890 |
5275
|
2891 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
2892 elem (i, i) -= a.elem (i, i); |
|
2893 |
|
2894 return *this; |
|
2895 } |
|
2896 |
|
2897 ComplexMatrix& |
|
2898 ComplexMatrix::operator += (const ComplexDiagMatrix& a) |
|
2899 { |
5275
|
2900 octave_idx_type nr = rows (); |
|
2901 octave_idx_type nc = cols (); |
|
2902 |
|
2903 octave_idx_type a_nr = rows (); |
|
2904 octave_idx_type a_nc = cols (); |
2384
|
2905 |
|
2906 if (nr != a_nr || nc != a_nc) |
458
|
2907 { |
2384
|
2908 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889
|
2909 return *this; |
458
|
2910 } |
|
2911 |
5275
|
2912 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
2913 elem (i, i) += a.elem (i, i); |
|
2914 |
|
2915 return *this; |
|
2916 } |
|
2917 |
|
2918 ComplexMatrix& |
|
2919 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) |
|
2920 { |
5275
|
2921 octave_idx_type nr = rows (); |
|
2922 octave_idx_type nc = cols (); |
|
2923 |
|
2924 octave_idx_type a_nr = rows (); |
|
2925 octave_idx_type a_nc = cols (); |
2384
|
2926 |
|
2927 if (nr != a_nr || nc != a_nc) |
458
|
2928 { |
2384
|
2929 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889
|
2930 return *this; |
458
|
2931 } |
|
2932 |
5275
|
2933 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
2934 elem (i, i) -= a.elem (i, i); |
|
2935 |
|
2936 return *this; |
|
2937 } |
|
2938 |
|
2939 // matrix by matrix -> matrix operations |
|
2940 |
|
2941 ComplexMatrix& |
|
2942 ComplexMatrix::operator += (const Matrix& a) |
|
2943 { |
5275
|
2944 octave_idx_type nr = rows (); |
|
2945 octave_idx_type nc = cols (); |
|
2946 |
|
2947 octave_idx_type a_nr = a.rows (); |
|
2948 octave_idx_type a_nc = a.cols (); |
2384
|
2949 |
|
2950 if (nr != a_nr || nc != a_nc) |
458
|
2951 { |
2384
|
2952 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
2953 return *this; |
|
2954 } |
|
2955 |
|
2956 if (nr == 0 || nc == 0) |
|
2957 return *this; |
|
2958 |
|
2959 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2960 |
3769
|
2961 mx_inline_add2 (d, a.data (), length ()); |
458
|
2962 return *this; |
|
2963 } |
|
2964 |
|
2965 ComplexMatrix& |
|
2966 ComplexMatrix::operator -= (const Matrix& a) |
|
2967 { |
5275
|
2968 octave_idx_type nr = rows (); |
|
2969 octave_idx_type nc = cols (); |
|
2970 |
|
2971 octave_idx_type a_nr = a.rows (); |
|
2972 octave_idx_type a_nc = a.cols (); |
2384
|
2973 |
|
2974 if (nr != a_nr || nc != a_nc) |
458
|
2975 { |
2384
|
2976 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
2977 return *this; |
|
2978 } |
|
2979 |
|
2980 if (nr == 0 || nc == 0) |
|
2981 return *this; |
|
2982 |
|
2983 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2984 |
3769
|
2985 mx_inline_subtract2 (d, a.data (), length ()); |
458
|
2986 return *this; |
|
2987 } |
|
2988 |
|
2989 // unary operations |
|
2990 |
2964
|
2991 boolMatrix |
458
|
2992 ComplexMatrix::operator ! (void) const |
|
2993 { |
5275
|
2994 octave_idx_type nr = rows (); |
|
2995 octave_idx_type nc = cols (); |
2964
|
2996 |
|
2997 boolMatrix b (nr, nc); |
|
2998 |
5275
|
2999 for (octave_idx_type j = 0; j < nc; j++) |
|
3000 for (octave_idx_type i = 0; i < nr; i++) |
5139
|
3001 b.elem (i, j) = elem (i, j) == 0.0; |
2964
|
3002 |
|
3003 return b; |
458
|
3004 } |
|
3005 |
|
3006 // other operations |
|
3007 |
|
3008 ComplexMatrix |
2676
|
3009 ComplexMatrix::map (c_c_Mapper f) const |
458
|
3010 { |
2676
|
3011 ComplexMatrix b (*this); |
|
3012 return b.apply (f); |
458
|
3013 } |
|
3014 |
2676
|
3015 Matrix |
|
3016 ComplexMatrix::map (d_c_Mapper f) const |
458
|
3017 { |
5275
|
3018 octave_idx_type nr = rows (); |
|
3019 octave_idx_type nc = cols (); |
3248
|
3020 |
|
3021 Matrix retval (nr, nc); |
|
3022 |
5275
|
3023 for (octave_idx_type j = 0; j < nc; j++) |
|
3024 for (octave_idx_type i = 0; i < nr; i++) |
3248
|
3025 retval(i,j) = f (elem(i,j)); |
|
3026 |
|
3027 return retval; |
|
3028 } |
|
3029 |
|
3030 boolMatrix |
|
3031 ComplexMatrix::map (b_c_Mapper f) const |
|
3032 { |
5275
|
3033 octave_idx_type nr = rows (); |
|
3034 octave_idx_type nc = cols (); |
3248
|
3035 |
|
3036 boolMatrix retval (nr, nc); |
|
3037 |
5275
|
3038 for (octave_idx_type j = 0; j < nc; j++) |
|
3039 for (octave_idx_type i = 0; i < nr; i++) |
3248
|
3040 retval(i,j) = f (elem(i,j)); |
2676
|
3041 |
|
3042 return retval; |
|
3043 } |
|
3044 |
|
3045 ComplexMatrix& |
|
3046 ComplexMatrix::apply (c_c_Mapper f) |
|
3047 { |
|
3048 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
3049 |
5275
|
3050 for (octave_idx_type i = 0; i < length (); i++) |
2676
|
3051 d[i] = f (d[i]); |
|
3052 |
|
3053 return *this; |
458
|
3054 } |
|
3055 |
2384
|
3056 bool |
|
3057 ComplexMatrix::any_element_is_inf_or_nan (void) const |
|
3058 { |
5275
|
3059 octave_idx_type nr = rows (); |
|
3060 octave_idx_type nc = cols (); |
|
3061 |
|
3062 for (octave_idx_type j = 0; j < nc; j++) |
|
3063 for (octave_idx_type i = 0; i < nr; i++) |
2384
|
3064 { |
|
3065 Complex val = elem (i, j); |
|
3066 if (xisinf (val) || xisnan (val)) |
|
3067 return true; |
|
3068 } |
|
3069 |
|
3070 return false; |
|
3071 } |
|
3072 |
2408
|
3073 // Return true if no elements have imaginary components. |
|
3074 |
|
3075 bool |
|
3076 ComplexMatrix::all_elements_are_real (void) const |
|
3077 { |
5275
|
3078 octave_idx_type nr = rows (); |
|
3079 octave_idx_type nc = cols (); |
|
3080 |
|
3081 for (octave_idx_type j = 0; j < nc; j++) |
4349
|
3082 { |
5275
|
3083 for (octave_idx_type i = 0; i < nr; i++) |
4349
|
3084 { |
5315
|
3085 double ip = std::imag (elem (i, j)); |
4349
|
3086 |
|
3087 if (ip != 0.0 || lo_ieee_signbit (ip)) |
|
3088 return false; |
|
3089 } |
|
3090 } |
2408
|
3091 |
|
3092 return true; |
|
3093 } |
|
3094 |
1968
|
3095 // Return nonzero if any element of CM has a non-integer real or |
|
3096 // imaginary part. Also extract the largest and smallest (real or |
|
3097 // imaginary) values and return them in MAX_VAL and MIN_VAL. |
|
3098 |
2384
|
3099 bool |
1968
|
3100 ComplexMatrix::all_integers (double& max_val, double& min_val) const |
|
3101 { |
5275
|
3102 octave_idx_type nr = rows (); |
|
3103 octave_idx_type nc = cols (); |
1968
|
3104 |
|
3105 if (nr > 0 && nc > 0) |
|
3106 { |
|
3107 Complex val = elem (0, 0); |
|
3108 |
5315
|
3109 double r_val = std::real (val); |
|
3110 double i_val = std::imag (val); |
1968
|
3111 |
|
3112 max_val = r_val; |
|
3113 min_val = r_val; |
|
3114 |
|
3115 if (i_val > max_val) |
|
3116 max_val = i_val; |
|
3117 |
|
3118 if (i_val < max_val) |
|
3119 min_val = i_val; |
|
3120 } |
|
3121 else |
2384
|
3122 return false; |
1968
|
3123 |
5275
|
3124 for (octave_idx_type j = 0; j < nc; j++) |
|
3125 for (octave_idx_type i = 0; i < nr; i++) |
1968
|
3126 { |
|
3127 Complex val = elem (i, j); |
|
3128 |
5315
|
3129 double r_val = std::real (val); |
|
3130 double i_val = std::imag (val); |
1968
|
3131 |
|
3132 if (r_val > max_val) |
|
3133 max_val = r_val; |
|
3134 |
|
3135 if (i_val > max_val) |
|
3136 max_val = i_val; |
|
3137 |
|
3138 if (r_val < min_val) |
|
3139 min_val = r_val; |
|
3140 |
|
3141 if (i_val < min_val) |
|
3142 min_val = i_val; |
|
3143 |
|
3144 if (D_NINT (r_val) != r_val || D_NINT (i_val) != i_val) |
2384
|
3145 return false; |
1968
|
3146 } |
2384
|
3147 |
|
3148 return true; |
1968
|
3149 } |
|
3150 |
2384
|
3151 bool |
1968
|
3152 ComplexMatrix::too_large_for_float (void) const |
|
3153 { |
5275
|
3154 octave_idx_type nr = rows (); |
|
3155 octave_idx_type nc = cols (); |
|
3156 |
|
3157 for (octave_idx_type j = 0; j < nc; j++) |
|
3158 for (octave_idx_type i = 0; i < nr; i++) |
1968
|
3159 { |
|
3160 Complex val = elem (i, j); |
|
3161 |
5315
|
3162 double r_val = std::real (val); |
|
3163 double i_val = std::imag (val); |
1968
|
3164 |
5389
|
3165 if ((! (xisnan (r_val) || xisinf (r_val)) |
5387
|
3166 && fabs (r_val) > FLT_MAX) |
5389
|
3167 || (! (xisnan (i_val) || xisinf (i_val)) |
5387
|
3168 && fabs (i_val) > FLT_MAX)) |
2384
|
3169 return true; |
1968
|
3170 } |
|
3171 |
2384
|
3172 return false; |
1968
|
3173 } |
|
3174 |
5775
|
3175 // FIXME Do these really belong here? Maybe they should be |
4015
|
3176 // in a base class? |
|
3177 |
2832
|
3178 boolMatrix |
4015
|
3179 ComplexMatrix::all (int dim) const |
458
|
3180 { |
4015
|
3181 MX_ALL_OP (dim); |
458
|
3182 } |
|
3183 |
2832
|
3184 boolMatrix |
4015
|
3185 ComplexMatrix::any (int dim) const |
458
|
3186 { |
4015
|
3187 MX_ANY_OP (dim); |
458
|
3188 } |
|
3189 |
|
3190 ComplexMatrix |
3723
|
3191 ComplexMatrix::cumprod (int dim) const |
458
|
3192 { |
4015
|
3193 MX_CUMULATIVE_OP (ComplexMatrix, Complex, *=); |
458
|
3194 } |
|
3195 |
|
3196 ComplexMatrix |
3723
|
3197 ComplexMatrix::cumsum (int dim) const |
458
|
3198 { |
4015
|
3199 MX_CUMULATIVE_OP (ComplexMatrix, Complex, +=); |
458
|
3200 } |
|
3201 |
|
3202 ComplexMatrix |
3723
|
3203 ComplexMatrix::prod (int dim) const |
458
|
3204 { |
3864
|
3205 MX_REDUCTION_OP (ComplexMatrix, *=, 1.0, 1.0); |
458
|
3206 } |
|
3207 |
|
3208 ComplexMatrix |
3723
|
3209 ComplexMatrix::sum (int dim) const |
458
|
3210 { |
3864
|
3211 MX_REDUCTION_OP (ComplexMatrix, +=, 0.0, 0.0); |
458
|
3212 } |
|
3213 |
|
3214 ComplexMatrix |
3723
|
3215 ComplexMatrix::sumsq (int dim) const |
458
|
3216 { |
3864
|
3217 #define ROW_EXPR \ |
|
3218 Complex d = elem (i, j); \ |
|
3219 retval.elem (i, 0) += d * conj (d) |
|
3220 |
|
3221 #define COL_EXPR \ |
|
3222 Complex d = elem (i, j); \ |
|
3223 retval.elem (0, j) += d * conj (d) |
|
3224 |
|
3225 MX_BASE_REDUCTION_OP (ComplexMatrix, ROW_EXPR, COL_EXPR, 0.0, 0.0); |
|
3226 |
|
3227 #undef ROW_EXPR |
|
3228 #undef COL_EXPR |
458
|
3229 } |
|
3230 |
4329
|
3231 Matrix ComplexMatrix::abs (void) const |
|
3232 { |
5275
|
3233 octave_idx_type nr = rows (); |
|
3234 octave_idx_type nc = cols (); |
4329
|
3235 |
|
3236 Matrix retval (nr, nc); |
|
3237 |
5275
|
3238 for (octave_idx_type j = 0; j < nc; j++) |
|
3239 for (octave_idx_type i = 0; i < nr; i++) |
5315
|
3240 retval (i, j) = std::abs (elem (i, j)); |
4329
|
3241 |
|
3242 return retval; |
|
3243 } |
|
3244 |
458
|
3245 ComplexColumnVector |
|
3246 ComplexMatrix::diag (void) const |
|
3247 { |
|
3248 return diag (0); |
|
3249 } |
|
3250 |
|
3251 ComplexColumnVector |
5275
|
3252 ComplexMatrix::diag (octave_idx_type k) const |
458
|
3253 { |
5275
|
3254 octave_idx_type nnr = rows (); |
|
3255 octave_idx_type nnc = cols (); |
458
|
3256 if (k > 0) |
|
3257 nnc -= k; |
|
3258 else if (k < 0) |
|
3259 nnr += k; |
|
3260 |
|
3261 ComplexColumnVector d; |
|
3262 |
|
3263 if (nnr > 0 && nnc > 0) |
|
3264 { |
5275
|
3265 octave_idx_type ndiag = (nnr < nnc) ? nnr : nnc; |
458
|
3266 |
|
3267 d.resize (ndiag); |
|
3268 |
|
3269 if (k > 0) |
|
3270 { |
5275
|
3271 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
3272 d.elem (i) = elem (i, i+k); |
|
3273 } |
4509
|
3274 else if (k < 0) |
458
|
3275 { |
5275
|
3276 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
3277 d.elem (i) = elem (i-k, i); |
|
3278 } |
|
3279 else |
|
3280 { |
5275
|
3281 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
3282 d.elem (i) = elem (i, i); |
|
3283 } |
|
3284 } |
|
3285 else |
4513
|
3286 (*current_liboctave_error_handler) |
|
3287 ("diag: requested diagonal out of range"); |
458
|
3288 |
|
3289 return d; |
|
3290 } |
|
3291 |
2354
|
3292 bool |
5275
|
3293 ComplexMatrix::row_is_real_only (octave_idx_type i) const |
2354
|
3294 { |
|
3295 bool retval = true; |
|
3296 |
5275
|
3297 octave_idx_type nc = columns (); |
|
3298 |
|
3299 for (octave_idx_type j = 0; j < nc; j++) |
2354
|
3300 { |
5315
|
3301 if (std::imag (elem (i, j)) != 0.0) |
2354
|
3302 { |
|
3303 retval = false; |
|
3304 break; |
|
3305 } |
|
3306 } |
|
3307 |
|
3308 return retval; |
|
3309 } |
|
3310 |
|
3311 bool |
5275
|
3312 ComplexMatrix::column_is_real_only (octave_idx_type j) const |
2354
|
3313 { |
|
3314 bool retval = true; |
|
3315 |
5275
|
3316 octave_idx_type nr = rows (); |
|
3317 |
|
3318 for (octave_idx_type i = 0; i < nr; i++) |
2354
|
3319 { |
5315
|
3320 if (std::imag (elem (i, j)) != 0.0) |
2354
|
3321 { |
|
3322 retval = false; |
|
3323 break; |
|
3324 } |
|
3325 } |
|
3326 |
|
3327 return retval; |
|
3328 } |
891
|
3329 |
458
|
3330 ComplexColumnVector |
|
3331 ComplexMatrix::row_min (void) const |
|
3332 { |
5275
|
3333 Array<octave_idx_type> dummy_idx; |
4587
|
3334 return row_min (dummy_idx); |
458
|
3335 } |
|
3336 |
|
3337 ComplexColumnVector |
5275
|
3338 ComplexMatrix::row_min (Array<octave_idx_type>& idx_arg) const |
458
|
3339 { |
|
3340 ComplexColumnVector result; |
|
3341 |
5275
|
3342 octave_idx_type nr = rows (); |
|
3343 octave_idx_type nc = cols (); |
458
|
3344 |
|
3345 if (nr > 0 && nc > 0) |
|
3346 { |
|
3347 result.resize (nr); |
4587
|
3348 idx_arg.resize (nr); |
458
|
3349 |
5275
|
3350 for (octave_idx_type i = 0; i < nr; i++) |
458
|
3351 { |
2354
|
3352 bool real_only = row_is_real_only (i); |
|
3353 |
5275
|
3354 octave_idx_type idx_j; |
4469
|
3355 |
|
3356 Complex tmp_min; |
|
3357 |
|
3358 double abs_min = octave_NaN; |
|
3359 |
|
3360 for (idx_j = 0; idx_j < nc; idx_j++) |
|
3361 { |
|
3362 tmp_min = elem (i, idx_j); |
|
3363 |
5389
|
3364 if (! xisnan (tmp_min)) |
4469
|
3365 { |
5315
|
3366 abs_min = real_only ? std::real (tmp_min) : std::abs (tmp_min); |
4469
|
3367 break; |
|
3368 } |
|
3369 } |
|
3370 |
5275
|
3371 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469
|
3372 { |
|
3373 Complex tmp = elem (i, j); |
|
3374 |
5389
|
3375 if (xisnan (tmp)) |
4469
|
3376 continue; |
|
3377 |
5315
|
3378 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469
|
3379 |
|
3380 if (abs_tmp < abs_min) |
|
3381 { |
|
3382 idx_j = j; |
|
3383 tmp_min = tmp; |
|
3384 abs_min = abs_tmp; |
|
3385 } |
|
3386 } |
|
3387 |
5389
|
3388 if (xisnan (tmp_min)) |
4469
|
3389 { |
|
3390 result.elem (i) = Complex_NaN_result; |
4587
|
3391 idx_arg.elem (i) = 0; |
4469
|
3392 } |
891
|
3393 else |
|
3394 { |
4469
|
3395 result.elem (i) = tmp_min; |
4587
|
3396 idx_arg.elem (i) = idx_j; |
891
|
3397 } |
458
|
3398 } |
|
3399 } |
|
3400 |
|
3401 return result; |
|
3402 } |
|
3403 |
|
3404 ComplexColumnVector |
|
3405 ComplexMatrix::row_max (void) const |
|
3406 { |
5275
|
3407 Array<octave_idx_type> dummy_idx; |
4587
|
3408 return row_max (dummy_idx); |
458
|
3409 } |
|
3410 |
|
3411 ComplexColumnVector |
5275
|
3412 ComplexMatrix::row_max (Array<octave_idx_type>& idx_arg) const |
458
|
3413 { |
|
3414 ComplexColumnVector result; |
|
3415 |
5275
|
3416 octave_idx_type nr = rows (); |
|
3417 octave_idx_type nc = cols (); |
458
|
3418 |
|
3419 if (nr > 0 && nc > 0) |
|
3420 { |
|
3421 result.resize (nr); |
4587
|
3422 idx_arg.resize (nr); |
458
|
3423 |
5275
|
3424 for (octave_idx_type i = 0; i < nr; i++) |
458
|
3425 { |
2354
|
3426 bool real_only = row_is_real_only (i); |
|
3427 |
5275
|
3428 octave_idx_type idx_j; |
4469
|
3429 |
|
3430 Complex tmp_max; |
|
3431 |
|
3432 double abs_max = octave_NaN; |
|
3433 |
|
3434 for (idx_j = 0; idx_j < nc; idx_j++) |
|
3435 { |
|
3436 tmp_max = elem (i, idx_j); |
|
3437 |
5389
|
3438 if (! xisnan (tmp_max)) |
4469
|
3439 { |
5315
|
3440 abs_max = real_only ? std::real (tmp_max) : std::abs (tmp_max); |
4469
|
3441 break; |
|
3442 } |
|
3443 } |
|
3444 |
5275
|
3445 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469
|
3446 { |
|
3447 Complex tmp = elem (i, j); |
|
3448 |
5389
|
3449 if (xisnan (tmp)) |
4469
|
3450 continue; |
|
3451 |
5315
|
3452 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469
|
3453 |
|
3454 if (abs_tmp > abs_max) |
|
3455 { |
|
3456 idx_j = j; |
|
3457 tmp_max = tmp; |
|
3458 abs_max = abs_tmp; |
|
3459 } |
|
3460 } |
|
3461 |
5389
|
3462 if (xisnan (tmp_max)) |
4469
|
3463 { |
|
3464 result.elem (i) = Complex_NaN_result; |
4587
|
3465 idx_arg.elem (i) = 0; |
4469
|
3466 } |
891
|
3467 else |
|
3468 { |
4469
|
3469 result.elem (i) = tmp_max; |
4587
|
3470 idx_arg.elem (i) = idx_j; |
891
|
3471 } |
458
|
3472 } |
|
3473 } |
|
3474 |
|
3475 return result; |
|
3476 } |
|
3477 |
|
3478 ComplexRowVector |
|
3479 ComplexMatrix::column_min (void) const |
|
3480 { |
5275
|
3481 Array<octave_idx_type> dummy_idx; |
4587
|
3482 return column_min (dummy_idx); |
458
|
3483 } |
|
3484 |
|
3485 ComplexRowVector |
5275
|
3486 ComplexMatrix::column_min (Array<octave_idx_type>& idx_arg) const |
458
|
3487 { |
|
3488 ComplexRowVector result; |
|
3489 |
5275
|
3490 octave_idx_type nr = rows (); |
|
3491 octave_idx_type nc = cols (); |
458
|
3492 |
|
3493 if (nr > 0 && nc > 0) |
|
3494 { |
|
3495 result.resize (nc); |
4587
|
3496 idx_arg.resize (nc); |
458
|
3497 |
5275
|
3498 for (octave_idx_type j = 0; j < nc; j++) |
458
|
3499 { |
2354
|
3500 bool real_only = column_is_real_only (j); |
|
3501 |
5275
|
3502 octave_idx_type idx_i; |
4469
|
3503 |
|
3504 Complex tmp_min; |
|
3505 |
|
3506 double abs_min = octave_NaN; |
|
3507 |
|
3508 for (idx_i = 0; idx_i < nr; idx_i++) |
|
3509 { |
|
3510 tmp_min = elem (idx_i, j); |
|
3511 |
5389
|
3512 if (! xisnan (tmp_min)) |
4469
|
3513 { |
5315
|
3514 abs_min = real_only ? std::real (tmp_min) : std::abs (tmp_min); |
4469
|
3515 break; |
|
3516 } |
|
3517 } |
|
3518 |
5275
|
3519 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469
|
3520 { |
|
3521 Complex tmp = elem (i, j); |
|
3522 |
5389
|
3523 if (xisnan (tmp)) |
4469
|
3524 continue; |
|
3525 |
5315
|
3526 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469
|
3527 |
|
3528 if (abs_tmp < abs_min) |
|
3529 { |
|
3530 idx_i = i; |
|
3531 tmp_min = tmp; |
|
3532 abs_min = abs_tmp; |
|
3533 } |
|
3534 } |
|
3535 |
5389
|
3536 if (xisnan (tmp_min)) |
4469
|
3537 { |
|
3538 result.elem (j) = Complex_NaN_result; |
4587
|
3539 idx_arg.elem (j) = 0; |
4469
|
3540 } |
891
|
3541 else |
|
3542 { |
4469
|
3543 result.elem (j) = tmp_min; |
4587
|
3544 idx_arg.elem (j) = idx_i; |
891
|
3545 } |
458
|
3546 } |
|
3547 } |
|
3548 |
|
3549 return result; |
|
3550 } |
|
3551 |
|
3552 ComplexRowVector |
|
3553 ComplexMatrix::column_max (void) const |
|
3554 { |
5275
|
3555 Array<octave_idx_type> dummy_idx; |
4587
|
3556 return column_max (dummy_idx); |
458
|
3557 } |
|
3558 |
|
3559 ComplexRowVector |
5275
|
3560 ComplexMatrix::column_max (Array<octave_idx_type>& idx_arg) const |
458
|
3561 { |
|
3562 ComplexRowVector result; |
|
3563 |
5275
|
3564 octave_idx_type nr = rows (); |
|
3565 octave_idx_type nc = cols (); |
458
|
3566 |
|
3567 if (nr > 0 && nc > 0) |
|
3568 { |
|
3569 result.resize (nc); |
4587
|
3570 idx_arg.resize (nc); |
458
|
3571 |
5275
|
3572 for (octave_idx_type j = 0; j < nc; j++) |
458
|
3573 { |
2354
|
3574 bool real_only = column_is_real_only (j); |
|
3575 |
5275
|
3576 octave_idx_type idx_i; |
4469
|
3577 |
|
3578 Complex tmp_max; |
|
3579 |
|
3580 double abs_max = octave_NaN; |
|
3581 |
|
3582 for (idx_i = 0; idx_i < nr; idx_i++) |
|
3583 { |
|
3584 tmp_max = elem (idx_i, j); |
|
3585 |
5389
|
3586 if (! xisnan (tmp_max)) |
4469
|
3587 { |
5315
|
3588 abs_max = real_only ? std::real (tmp_max) : std::abs (tmp_max); |
4469
|
3589 break; |
|
3590 } |
|
3591 } |
|
3592 |
5275
|
3593 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469
|
3594 { |
|
3595 Complex tmp = elem (i, j); |
|
3596 |
5389
|
3597 if (xisnan (tmp)) |
4469
|
3598 continue; |
|
3599 |
5315
|
3600 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469
|
3601 |
|
3602 if (abs_tmp > abs_max) |
|
3603 { |
|
3604 idx_i = i; |
|
3605 tmp_max = tmp; |
|
3606 abs_max = abs_tmp; |
|
3607 } |
|
3608 } |
|
3609 |
5389
|
3610 if (xisnan (tmp_max)) |
4469
|
3611 { |
|
3612 result.elem (j) = Complex_NaN_result; |
4587
|
3613 idx_arg.elem (j) = 0; |
4469
|
3614 } |
891
|
3615 else |
|
3616 { |
4469
|
3617 result.elem (j) = tmp_max; |
4587
|
3618 idx_arg.elem (j) = idx_i; |
891
|
3619 } |
458
|
3620 } |
|
3621 } |
|
3622 |
|
3623 return result; |
|
3624 } |
|
3625 |
|
3626 // i/o |
|
3627 |
3504
|
3628 std::ostream& |
|
3629 operator << (std::ostream& os, const ComplexMatrix& a) |
458
|
3630 { |
5275
|
3631 for (octave_idx_type i = 0; i < a.rows (); i++) |
458
|
3632 { |
5275
|
3633 for (octave_idx_type j = 0; j < a.cols (); j++) |
4130
|
3634 { |
|
3635 os << " "; |
|
3636 octave_write_complex (os, a.elem (i, j)); |
|
3637 } |
458
|
3638 os << "\n"; |
|
3639 } |
|
3640 return os; |
|
3641 } |
|
3642 |
3504
|
3643 std::istream& |
|
3644 operator >> (std::istream& is, ComplexMatrix& a) |
458
|
3645 { |
5275
|
3646 octave_idx_type nr = a.rows (); |
|
3647 octave_idx_type nc = a.cols (); |
458
|
3648 |
|
3649 if (nr < 1 || nc < 1) |
3504
|
3650 is.clear (std::ios::badbit); |
458
|
3651 else |
|
3652 { |
|
3653 Complex tmp; |
5275
|
3654 for (octave_idx_type i = 0; i < nr; i++) |
|
3655 for (octave_idx_type j = 0; j < nc; j++) |
458
|
3656 { |
4130
|
3657 tmp = octave_read_complex (is); |
458
|
3658 if (is) |
|
3659 a.elem (i, j) = tmp; |
|
3660 else |
2993
|
3661 goto done; |
458
|
3662 } |
|
3663 } |
|
3664 |
2993
|
3665 done: |
|
3666 |
458
|
3667 return is; |
|
3668 } |
|
3669 |
1819
|
3670 ComplexMatrix |
|
3671 Givens (const Complex& x, const Complex& y) |
|
3672 { |
|
3673 double cc; |
|
3674 Complex cs, temp_r; |
|
3675 |
3887
|
3676 F77_FUNC (zlartg, ZLARTG) (x, y, cc, cs, temp_r); |
1819
|
3677 |
|
3678 ComplexMatrix g (2, 2); |
|
3679 |
|
3680 g.elem (0, 0) = cc; |
|
3681 g.elem (1, 1) = cc; |
|
3682 g.elem (0, 1) = cs; |
|
3683 g.elem (1, 0) = -conj (cs); |
|
3684 |
|
3685 return g; |
|
3686 } |
|
3687 |
|
3688 ComplexMatrix |
|
3689 Sylvester (const ComplexMatrix& a, const ComplexMatrix& b, |
|
3690 const ComplexMatrix& c) |
|
3691 { |
|
3692 ComplexMatrix retval; |
|
3693 |
5775
|
3694 // FIXME -- need to check that a, b, and c are all the same |
1819
|
3695 // size. |
|
3696 |
|
3697 // Compute Schur decompositions |
|
3698 |
|
3699 ComplexSCHUR as (a, "U"); |
|
3700 ComplexSCHUR bs (b, "U"); |
|
3701 |
|
3702 // Transform c to new coordinates. |
|
3703 |
|
3704 ComplexMatrix ua = as.unitary_matrix (); |
|
3705 ComplexMatrix sch_a = as.schur_matrix (); |
|
3706 |
|
3707 ComplexMatrix ub = bs.unitary_matrix (); |
|
3708 ComplexMatrix sch_b = bs.schur_matrix (); |
|
3709 |
|
3710 ComplexMatrix cx = ua.hermitian () * c * ub; |
|
3711 |
|
3712 // Solve the sylvester equation, back-transform, and return the |
|
3713 // solution. |
|
3714 |
5275
|
3715 octave_idx_type a_nr = a.rows (); |
|
3716 octave_idx_type b_nr = b.rows (); |
1819
|
3717 |
|
3718 double scale; |
5275
|
3719 octave_idx_type info; |
1950
|
3720 |
|
3721 Complex *pa = sch_a.fortran_vec (); |
|
3722 Complex *pb = sch_b.fortran_vec (); |
|
3723 Complex *px = cx.fortran_vec (); |
1819
|
3724 |
4552
|
3725 F77_XFCN (ztrsyl, ZTRSYL, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3726 F77_CONST_CHAR_ARG2 ("N", 1), |
|
3727 1, a_nr, b_nr, pa, a_nr, pb, |
|
3728 b_nr, px, a_nr, scale, info |
|
3729 F77_CHAR_ARG_LEN (1) |
|
3730 F77_CHAR_ARG_LEN (1))); |
1950
|
3731 |
|
3732 if (f77_exception_encountered) |
|
3733 (*current_liboctave_error_handler) ("unrecoverable error in ztrsyl"); |
|
3734 else |
|
3735 { |
5775
|
3736 // FIXME -- check info? |
1950
|
3737 |
|
3738 retval = -ua * cx * ub.hermitian (); |
|
3739 } |
1819
|
3740 |
|
3741 return retval; |
|
3742 } |
|
3743 |
2828
|
3744 ComplexMatrix |
|
3745 operator * (const ComplexMatrix& m, const Matrix& a) |
|
3746 { |
|
3747 ComplexMatrix tmp (a); |
|
3748 return m * tmp; |
|
3749 } |
|
3750 |
|
3751 ComplexMatrix |
|
3752 operator * (const Matrix& m, const ComplexMatrix& a) |
|
3753 { |
|
3754 ComplexMatrix tmp (m); |
|
3755 return tmp * a; |
|
3756 } |
|
3757 |
6162
|
3758 /* Simple Dot Product, Matrix-Vector and Matrix-Matrix Unit tests |
|
3759 %!assert([1+i 2+i 3+i] * [ 4+i ; 5+i ; 6+i], 29+21i, 1e-14) |
|
3760 %!assert([1+i 2+i ; 3+i 4+i ] * [5+i ; 6+i], [15 + 14i ; 37 + 18i], 1e-14) |
|
3761 %!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) |
|
3762 */ |
|
3763 |
|
3764 /* Test some simple identities |
|
3765 %!shared M, cv, rv |
|
3766 %! M = randn(10,10)+i*rand(10,10); |
|
3767 %! cv = randn(10,1)+i*rand(10,1); |
|
3768 %! rv = randn(1,10)+i*rand(1,10); |
|
3769 %!assert([M*cv,M*cv],M*[cv,cv],1e-14) |
|
3770 %!assert([rv*M;rv*M],[rv;rv]*M,1e-14) |
|
3771 %!assert(2*rv*cv,[rv,rv]*[cv;cv],1e-14) |
|
3772 */ |
|
3773 |
2828
|
3774 ComplexMatrix |
|
3775 operator * (const ComplexMatrix& m, const ComplexMatrix& a) |
|
3776 { |
|
3777 ComplexMatrix retval; |
|
3778 |
5275
|
3779 octave_idx_type nr = m.rows (); |
|
3780 octave_idx_type nc = m.cols (); |
|
3781 |
|
3782 octave_idx_type a_nr = a.rows (); |
|
3783 octave_idx_type a_nc = a.cols (); |
2828
|
3784 |
|
3785 if (nc != a_nr) |
|
3786 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
|
3787 else |
|
3788 { |
|
3789 if (nr == 0 || nc == 0 || a_nc == 0) |
3760
|
3790 retval.resize (nr, a_nc, 0.0); |
2828
|
3791 else |
|
3792 { |
5275
|
3793 octave_idx_type ld = nr; |
|
3794 octave_idx_type lda = a.rows (); |
2828
|
3795 |
|
3796 retval.resize (nr, a_nc); |
|
3797 Complex *c = retval.fortran_vec (); |
|
3798 |
5983
|
3799 if (a_nc == 1) |
|
3800 { |
|
3801 if (nr == 1) |
|
3802 F77_FUNC (xzdotu, XZDOTU) (nc, m.data (), 1, a.data (), 1, *c); |
|
3803 else |
6390
|
3804 { |
|
3805 F77_XFCN (zgemv, ZGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3806 nr, nc, 1.0, m.data (), ld, |
|
3807 a.data (), 1, 0.0, c, 1 |
|
3808 F77_CHAR_ARG_LEN (1))); |
|
3809 |
|
3810 if (f77_exception_encountered) |
|
3811 (*current_liboctave_error_handler) |
|
3812 ("unrecoverable error in zgemv"); |
|
3813 } |
5983
|
3814 } |
|
3815 else |
6390
|
3816 { |
|
3817 F77_XFCN (zgemm, ZGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3818 F77_CONST_CHAR_ARG2 ("N", 1), |
|
3819 nr, a_nc, nc, 1.0, m.data (), |
|
3820 ld, a.data (), lda, 0.0, c, nr |
|
3821 F77_CHAR_ARG_LEN (1) |
|
3822 F77_CHAR_ARG_LEN (1))); |
|
3823 |
|
3824 if (f77_exception_encountered) |
|
3825 (*current_liboctave_error_handler) |
|
3826 ("unrecoverable error in zgemm"); |
|
3827 } |
2828
|
3828 } |
|
3829 } |
|
3830 |
|
3831 return retval; |
|
3832 } |
|
3833 |
5775
|
3834 // FIXME -- it would be nice to share code among the min/max |
4309
|
3835 // functions below. |
|
3836 |
|
3837 #define EMPTY_RETURN_CHECK(T) \ |
|
3838 if (nr == 0 || nc == 0) \ |
|
3839 return T (nr, nc); |
|
3840 |
|
3841 ComplexMatrix |
|
3842 min (const Complex& c, const ComplexMatrix& m) |
|
3843 { |
5275
|
3844 octave_idx_type nr = m.rows (); |
|
3845 octave_idx_type nc = m.columns (); |
4309
|
3846 |
|
3847 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3848 |
|
3849 ComplexMatrix result (nr, nc); |
|
3850 |
5275
|
3851 for (octave_idx_type j = 0; j < nc; j++) |
|
3852 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3853 { |
|
3854 OCTAVE_QUIT; |
|
3855 result (i, j) = xmin (c, m (i, j)); |
|
3856 } |
|
3857 |
|
3858 return result; |
|
3859 } |
|
3860 |
|
3861 ComplexMatrix |
|
3862 min (const ComplexMatrix& m, const Complex& c) |
|
3863 { |
5275
|
3864 octave_idx_type nr = m.rows (); |
|
3865 octave_idx_type nc = m.columns (); |
4309
|
3866 |
|
3867 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3868 |
|
3869 ComplexMatrix result (nr, nc); |
|
3870 |
5275
|
3871 for (octave_idx_type j = 0; j < nc; j++) |
|
3872 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3873 { |
|
3874 OCTAVE_QUIT; |
|
3875 result (i, j) = xmin (m (i, j), c); |
|
3876 } |
|
3877 |
|
3878 return result; |
|
3879 } |
|
3880 |
|
3881 ComplexMatrix |
|
3882 min (const ComplexMatrix& a, const ComplexMatrix& b) |
|
3883 { |
5275
|
3884 octave_idx_type nr = a.rows (); |
|
3885 octave_idx_type nc = a.columns (); |
4309
|
3886 |
|
3887 if (nr != b.rows () || nc != b.columns ()) |
|
3888 { |
|
3889 (*current_liboctave_error_handler) |
|
3890 ("two-arg min expecting args of same size"); |
|
3891 return ComplexMatrix (); |
|
3892 } |
|
3893 |
|
3894 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3895 |
|
3896 ComplexMatrix result (nr, nc); |
|
3897 |
5275
|
3898 for (octave_idx_type j = 0; j < nc; j++) |
4309
|
3899 { |
|
3900 int columns_are_real_only = 1; |
5275
|
3901 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3902 { |
|
3903 OCTAVE_QUIT; |
5315
|
3904 if (std::imag (a (i, j)) != 0.0 || std::imag (b (i, j)) != 0.0) |
4309
|
3905 { |
|
3906 columns_are_real_only = 0; |
|
3907 break; |
|
3908 } |
|
3909 } |
|
3910 |
|
3911 if (columns_are_real_only) |
|
3912 { |
5275
|
3913 for (octave_idx_type i = 0; i < nr; i++) |
5315
|
3914 result (i, j) = xmin (std::real (a (i, j)), std::real (b (i, j))); |
4309
|
3915 } |
|
3916 else |
|
3917 { |
5275
|
3918 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3919 { |
|
3920 OCTAVE_QUIT; |
|
3921 result (i, j) = xmin (a (i, j), b (i, j)); |
|
3922 } |
|
3923 } |
|
3924 } |
|
3925 |
|
3926 return result; |
|
3927 } |
|
3928 |
|
3929 ComplexMatrix |
|
3930 max (const Complex& c, const ComplexMatrix& m) |
|
3931 { |
5275
|
3932 octave_idx_type nr = m.rows (); |
|
3933 octave_idx_type nc = m.columns (); |
4309
|
3934 |
|
3935 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3936 |
|
3937 ComplexMatrix result (nr, nc); |
|
3938 |
5275
|
3939 for (octave_idx_type j = 0; j < nc; j++) |
|
3940 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3941 { |
|
3942 OCTAVE_QUIT; |
|
3943 result (i, j) = xmax (c, m (i, j)); |
|
3944 } |
|
3945 |
|
3946 return result; |
|
3947 } |
|
3948 |
|
3949 ComplexMatrix |
|
3950 max (const ComplexMatrix& m, const Complex& c) |
|
3951 { |
5275
|
3952 octave_idx_type nr = m.rows (); |
|
3953 octave_idx_type nc = m.columns (); |
4309
|
3954 |
|
3955 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3956 |
|
3957 ComplexMatrix result (nr, nc); |
|
3958 |
5275
|
3959 for (octave_idx_type j = 0; j < nc; j++) |
|
3960 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3961 { |
|
3962 OCTAVE_QUIT; |
|
3963 result (i, j) = xmax (m (i, j), c); |
|
3964 } |
|
3965 |
|
3966 return result; |
|
3967 } |
|
3968 |
|
3969 ComplexMatrix |
|
3970 max (const ComplexMatrix& a, const ComplexMatrix& b) |
|
3971 { |
5275
|
3972 octave_idx_type nr = a.rows (); |
|
3973 octave_idx_type nc = a.columns (); |
4309
|
3974 |
|
3975 if (nr != b.rows () || nc != b.columns ()) |
|
3976 { |
|
3977 (*current_liboctave_error_handler) |
|
3978 ("two-arg max expecting args of same size"); |
|
3979 return ComplexMatrix (); |
|
3980 } |
|
3981 |
|
3982 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3983 |
|
3984 ComplexMatrix result (nr, nc); |
|
3985 |
5275
|
3986 for (octave_idx_type j = 0; j < nc; j++) |
4309
|
3987 { |
|
3988 int columns_are_real_only = 1; |
5275
|
3989 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3990 { |
|
3991 OCTAVE_QUIT; |
5315
|
3992 if (std::imag (a (i, j)) != 0.0 || std::imag (b (i, j)) != 0.0) |
4309
|
3993 { |
|
3994 columns_are_real_only = 0; |
|
3995 break; |
|
3996 } |
|
3997 } |
|
3998 |
|
3999 if (columns_are_real_only) |
|
4000 { |
5275
|
4001 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
4002 { |
|
4003 OCTAVE_QUIT; |
5315
|
4004 result (i, j) = xmax (std::real (a (i, j)), std::real (b (i, j))); |
4309
|
4005 } |
|
4006 } |
|
4007 else |
|
4008 { |
5275
|
4009 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
4010 { |
|
4011 OCTAVE_QUIT; |
|
4012 result (i, j) = xmax (a (i, j), b (i, j)); |
|
4013 } |
|
4014 } |
|
4015 } |
|
4016 |
|
4017 return result; |
|
4018 } |
|
4019 |
5315
|
4020 MS_CMP_OPS(ComplexMatrix, std::real, Complex, std::real) |
3504
|
4021 MS_BOOL_OPS(ComplexMatrix, Complex, 0.0) |
2870
|
4022 |
5315
|
4023 SM_CMP_OPS(Complex, std::real, ComplexMatrix, std::real) |
3504
|
4024 SM_BOOL_OPS(Complex, ComplexMatrix, 0.0) |
2870
|
4025 |
5315
|
4026 MM_CMP_OPS(ComplexMatrix, std::real, ComplexMatrix, std::real) |
3504
|
4027 MM_BOOL_OPS(ComplexMatrix, ComplexMatrix, 0.0) |
2870
|
4028 |
458
|
4029 /* |
|
4030 ;;; Local Variables: *** |
|
4031 ;;; mode: C++ *** |
|
4032 ;;; End: *** |
|
4033 */ |