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