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