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