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 { |
6486
|
1189 ComplexCHOL chol (*this, info, calc_cond); |
6207
|
1190 if (info == 0) |
6486
|
1191 { |
|
1192 if (calc_cond) |
|
1193 rcond = chol.rcond(); |
|
1194 else |
|
1195 rcond = 1.0; |
|
1196 ret = chol.inverse (); |
|
1197 } |
6207
|
1198 else |
|
1199 mattype.mark_as_unsymmetric (); |
|
1200 } |
|
1201 |
|
1202 if (!mattype.is_hermitian ()) |
|
1203 ret = finverse(mattype, info, rcond, force, calc_cond); |
|
1204 } |
|
1205 |
|
1206 return ret; |
|
1207 } |
|
1208 |
|
1209 ComplexMatrix |
4384
|
1210 ComplexMatrix::pseudo_inverse (double tol) const |
740
|
1211 { |
1549
|
1212 ComplexMatrix retval; |
|
1213 |
3480
|
1214 ComplexSVD result (*this, SVD::economy); |
740
|
1215 |
|
1216 DiagMatrix S = result.singular_values (); |
|
1217 ComplexMatrix U = result.left_singular_matrix (); |
|
1218 ComplexMatrix V = result.right_singular_matrix (); |
|
1219 |
|
1220 ColumnVector sigma = S.diag (); |
|
1221 |
5275
|
1222 octave_idx_type r = sigma.length () - 1; |
|
1223 octave_idx_type nr = rows (); |
|
1224 octave_idx_type nc = cols (); |
740
|
1225 |
|
1226 if (tol <= 0.0) |
|
1227 { |
|
1228 if (nr > nc) |
|
1229 tol = nr * sigma.elem (0) * DBL_EPSILON; |
|
1230 else |
|
1231 tol = nc * sigma.elem (0) * DBL_EPSILON; |
|
1232 } |
|
1233 |
|
1234 while (r >= 0 && sigma.elem (r) < tol) |
|
1235 r--; |
|
1236 |
|
1237 if (r < 0) |
1549
|
1238 retval = ComplexMatrix (nc, nr, 0.0); |
740
|
1239 else |
|
1240 { |
|
1241 ComplexMatrix Ur = U.extract (0, 0, nr-1, r); |
|
1242 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); |
|
1243 ComplexMatrix Vr = V.extract (0, 0, nc-1, r); |
1549
|
1244 retval = Vr * D * Ur.hermitian (); |
740
|
1245 } |
1549
|
1246 |
|
1247 return retval; |
740
|
1248 } |
|
1249 |
4773
|
1250 #if defined (HAVE_FFTW3) |
3827
|
1251 |
|
1252 ComplexMatrix |
|
1253 ComplexMatrix::fourier (void) const |
|
1254 { |
|
1255 size_t nr = rows (); |
|
1256 size_t nc = cols (); |
|
1257 |
|
1258 ComplexMatrix retval (nr, nc); |
|
1259 |
|
1260 size_t npts, nsamples; |
|
1261 |
|
1262 if (nr == 1 || nc == 1) |
|
1263 { |
|
1264 npts = nr > nc ? nr : nc; |
|
1265 nsamples = 1; |
|
1266 } |
|
1267 else |
|
1268 { |
|
1269 npts = nr; |
|
1270 nsamples = nc; |
|
1271 } |
|
1272 |
|
1273 const Complex *in (data ()); |
|
1274 Complex *out (retval.fortran_vec ()); |
|
1275 |
4773
|
1276 octave_fftw::fft (in, out, npts, nsamples); |
3827
|
1277 |
|
1278 return retval; |
|
1279 } |
|
1280 |
|
1281 ComplexMatrix |
|
1282 ComplexMatrix::ifourier (void) const |
|
1283 { |
|
1284 size_t nr = rows (); |
|
1285 size_t nc = cols (); |
|
1286 |
|
1287 ComplexMatrix retval (nr, nc); |
|
1288 |
|
1289 size_t npts, nsamples; |
|
1290 |
|
1291 if (nr == 1 || nc == 1) |
|
1292 { |
|
1293 npts = nr > nc ? nr : nc; |
|
1294 nsamples = 1; |
|
1295 } |
|
1296 else |
|
1297 { |
|
1298 npts = nr; |
|
1299 nsamples = nc; |
|
1300 } |
|
1301 |
|
1302 const Complex *in (data ()); |
|
1303 Complex *out (retval.fortran_vec ()); |
|
1304 |
4773
|
1305 octave_fftw::ifft (in, out, npts, nsamples); |
3827
|
1306 |
|
1307 return retval; |
|
1308 } |
|
1309 |
|
1310 ComplexMatrix |
|
1311 ComplexMatrix::fourier2d (void) const |
|
1312 { |
4773
|
1313 dim_vector dv(rows (), cols ()); |
|
1314 |
|
1315 ComplexMatrix retval (rows (), cols ()); |
|
1316 const Complex *in (data ()); |
|
1317 Complex *out (retval.fortran_vec ()); |
|
1318 |
|
1319 octave_fftw::fftNd (in, out, 2, dv); |
3827
|
1320 |
|
1321 return retval; |
|
1322 } |
|
1323 |
|
1324 ComplexMatrix |
|
1325 ComplexMatrix::ifourier2d (void) const |
|
1326 { |
4773
|
1327 dim_vector dv(rows (), cols ()); |
|
1328 |
|
1329 ComplexMatrix retval (rows (), cols ()); |
|
1330 const Complex *in (data ()); |
|
1331 Complex *out (retval.fortran_vec ()); |
|
1332 |
|
1333 octave_fftw::ifftNd (in, out, 2, dv); |
3827
|
1334 |
|
1335 return retval; |
|
1336 } |
|
1337 |
|
1338 #else |
|
1339 |
740
|
1340 ComplexMatrix |
458
|
1341 ComplexMatrix::fourier (void) const |
|
1342 { |
1948
|
1343 ComplexMatrix retval; |
|
1344 |
5275
|
1345 octave_idx_type nr = rows (); |
|
1346 octave_idx_type nc = cols (); |
|
1347 |
|
1348 octave_idx_type npts, nsamples; |
1948
|
1349 |
458
|
1350 if (nr == 1 || nc == 1) |
|
1351 { |
|
1352 npts = nr > nc ? nr : nc; |
|
1353 nsamples = 1; |
|
1354 } |
|
1355 else |
|
1356 { |
|
1357 npts = nr; |
|
1358 nsamples = nc; |
|
1359 } |
|
1360 |
5275
|
1361 octave_idx_type nn = 4*npts+15; |
1948
|
1362 |
|
1363 Array<Complex> wsave (nn); |
|
1364 Complex *pwsave = wsave.fortran_vec (); |
|
1365 |
|
1366 retval = *this; |
|
1367 Complex *tmp_data = retval.fortran_vec (); |
|
1368 |
3887
|
1369 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
458
|
1370 |
5275
|
1371 for (octave_idx_type j = 0; j < nsamples; j++) |
4153
|
1372 { |
|
1373 OCTAVE_QUIT; |
|
1374 |
|
1375 F77_FUNC (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
1376 } |
1948
|
1377 |
|
1378 return retval; |
458
|
1379 } |
|
1380 |
|
1381 ComplexMatrix |
|
1382 ComplexMatrix::ifourier (void) const |
|
1383 { |
1948
|
1384 ComplexMatrix retval; |
|
1385 |
5275
|
1386 octave_idx_type nr = rows (); |
|
1387 octave_idx_type nc = cols (); |
|
1388 |
|
1389 octave_idx_type npts, nsamples; |
1948
|
1390 |
458
|
1391 if (nr == 1 || nc == 1) |
|
1392 { |
|
1393 npts = nr > nc ? nr : nc; |
|
1394 nsamples = 1; |
|
1395 } |
|
1396 else |
|
1397 { |
|
1398 npts = nr; |
|
1399 nsamples = nc; |
|
1400 } |
|
1401 |
5275
|
1402 octave_idx_type nn = 4*npts+15; |
1948
|
1403 |
|
1404 Array<Complex> wsave (nn); |
|
1405 Complex *pwsave = wsave.fortran_vec (); |
|
1406 |
|
1407 retval = *this; |
|
1408 Complex *tmp_data = retval.fortran_vec (); |
|
1409 |
3887
|
1410 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
458
|
1411 |
5275
|
1412 for (octave_idx_type j = 0; j < nsamples; j++) |
4153
|
1413 { |
|
1414 OCTAVE_QUIT; |
|
1415 |
|
1416 F77_FUNC (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
|
1417 } |
458
|
1418 |
5275
|
1419 for (octave_idx_type j = 0; j < npts*nsamples; j++) |
3572
|
1420 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
458
|
1421 |
1948
|
1422 return retval; |
458
|
1423 } |
|
1424 |
677
|
1425 ComplexMatrix |
|
1426 ComplexMatrix::fourier2d (void) const |
|
1427 { |
1948
|
1428 ComplexMatrix retval; |
|
1429 |
5275
|
1430 octave_idx_type nr = rows (); |
|
1431 octave_idx_type nc = cols (); |
|
1432 |
|
1433 octave_idx_type npts, nsamples; |
1948
|
1434 |
677
|
1435 if (nr == 1 || nc == 1) |
|
1436 { |
|
1437 npts = nr > nc ? nr : nc; |
|
1438 nsamples = 1; |
|
1439 } |
|
1440 else |
|
1441 { |
|
1442 npts = nr; |
|
1443 nsamples = nc; |
|
1444 } |
|
1445 |
5275
|
1446 octave_idx_type nn = 4*npts+15; |
1948
|
1447 |
|
1448 Array<Complex> wsave (nn); |
|
1449 Complex *pwsave = wsave.fortran_vec (); |
|
1450 |
|
1451 retval = *this; |
|
1452 Complex *tmp_data = retval.fortran_vec (); |
|
1453 |
3887
|
1454 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1455 |
5275
|
1456 for (octave_idx_type j = 0; j < nsamples; j++) |
4153
|
1457 { |
|
1458 OCTAVE_QUIT; |
|
1459 |
|
1460 F77_FUNC (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
1461 } |
677
|
1462 |
|
1463 npts = nc; |
|
1464 nsamples = nr; |
|
1465 nn = 4*npts+15; |
1948
|
1466 |
|
1467 wsave.resize (nn); |
|
1468 pwsave = wsave.fortran_vec (); |
|
1469 |
4773
|
1470 Array<Complex> tmp (npts); |
|
1471 Complex *prow = tmp.fortran_vec (); |
1948
|
1472 |
3887
|
1473 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1474 |
5275
|
1475 for (octave_idx_type j = 0; j < nsamples; j++) |
677
|
1476 { |
4153
|
1477 OCTAVE_QUIT; |
|
1478 |
5275
|
1479 for (octave_idx_type i = 0; i < npts; i++) |
1948
|
1480 prow[i] = tmp_data[i*nr + j]; |
|
1481 |
3887
|
1482 F77_FUNC (cfftf, CFFTF) (npts, prow, pwsave); |
677
|
1483 |
5275
|
1484 for (octave_idx_type i = 0; i < npts; i++) |
1948
|
1485 tmp_data[i*nr + j] = prow[i]; |
677
|
1486 } |
|
1487 |
1948
|
1488 return retval; |
677
|
1489 } |
|
1490 |
|
1491 ComplexMatrix |
|
1492 ComplexMatrix::ifourier2d (void) const |
|
1493 { |
1948
|
1494 ComplexMatrix retval; |
|
1495 |
5275
|
1496 octave_idx_type nr = rows (); |
|
1497 octave_idx_type nc = cols (); |
|
1498 |
|
1499 octave_idx_type npts, nsamples; |
1948
|
1500 |
677
|
1501 if (nr == 1 || nc == 1) |
|
1502 { |
|
1503 npts = nr > nc ? nr : nc; |
|
1504 nsamples = 1; |
|
1505 } |
|
1506 else |
|
1507 { |
|
1508 npts = nr; |
|
1509 nsamples = nc; |
|
1510 } |
|
1511 |
5275
|
1512 octave_idx_type nn = 4*npts+15; |
1948
|
1513 |
|
1514 Array<Complex> wsave (nn); |
|
1515 Complex *pwsave = wsave.fortran_vec (); |
|
1516 |
|
1517 retval = *this; |
|
1518 Complex *tmp_data = retval.fortran_vec (); |
|
1519 |
3887
|
1520 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1521 |
5275
|
1522 for (octave_idx_type j = 0; j < nsamples; j++) |
4153
|
1523 { |
|
1524 OCTAVE_QUIT; |
|
1525 |
|
1526 F77_FUNC (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
|
1527 } |
677
|
1528 |
5275
|
1529 for (octave_idx_type j = 0; j < npts*nsamples; j++) |
3572
|
1530 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
677
|
1531 |
|
1532 npts = nc; |
|
1533 nsamples = nr; |
|
1534 nn = 4*npts+15; |
1948
|
1535 |
|
1536 wsave.resize (nn); |
|
1537 pwsave = wsave.fortran_vec (); |
|
1538 |
4773
|
1539 Array<Complex> tmp (npts); |
|
1540 Complex *prow = tmp.fortran_vec (); |
1948
|
1541 |
3887
|
1542 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1543 |
5275
|
1544 for (octave_idx_type j = 0; j < nsamples; j++) |
677
|
1545 { |
4153
|
1546 OCTAVE_QUIT; |
|
1547 |
5275
|
1548 for (octave_idx_type i = 0; i < npts; i++) |
1948
|
1549 prow[i] = tmp_data[i*nr + j]; |
|
1550 |
3887
|
1551 F77_FUNC (cfftb, CFFTB) (npts, prow, pwsave); |
677
|
1552 |
5275
|
1553 for (octave_idx_type i = 0; i < npts; i++) |
3572
|
1554 tmp_data[i*nr + j] = prow[i] / static_cast<double> (npts); |
677
|
1555 } |
|
1556 |
1948
|
1557 return retval; |
677
|
1558 } |
|
1559 |
3827
|
1560 #endif |
|
1561 |
458
|
1562 ComplexDET |
|
1563 ComplexMatrix::determinant (void) const |
|
1564 { |
5275
|
1565 octave_idx_type info; |
458
|
1566 double rcond; |
4329
|
1567 return determinant (info, rcond, 0); |
458
|
1568 } |
|
1569 |
|
1570 ComplexDET |
5275
|
1571 ComplexMatrix::determinant (octave_idx_type& info) const |
458
|
1572 { |
|
1573 double rcond; |
4329
|
1574 return determinant (info, rcond, 0); |
458
|
1575 } |
|
1576 |
|
1577 ComplexDET |
5275
|
1578 ComplexMatrix::determinant (octave_idx_type& info, double& rcond, int calc_cond) const |
458
|
1579 { |
|
1580 ComplexDET retval; |
|
1581 |
5275
|
1582 octave_idx_type nr = rows (); |
|
1583 octave_idx_type nc = cols (); |
458
|
1584 |
|
1585 if (nr == 0 || nc == 0) |
|
1586 { |
5634
|
1587 retval = ComplexDET (1.0, 0); |
458
|
1588 } |
|
1589 else |
|
1590 { |
5275
|
1591 Array<octave_idx_type> ipvt (nr); |
|
1592 octave_idx_type *pipvt = ipvt.fortran_vec (); |
1948
|
1593 |
|
1594 ComplexMatrix atmp = *this; |
|
1595 Complex *tmp_data = atmp.fortran_vec (); |
|
1596 |
4329
|
1597 info = 0; |
|
1598 |
4330
|
1599 // Calculate the norm of the matrix, for later use. |
4329
|
1600 double anorm = 0; |
|
1601 if (calc_cond) |
5275
|
1602 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); |
4329
|
1603 |
|
1604 F77_XFCN (zgetrf, ZGETRF, (nr, nc, tmp_data, nr, pipvt, info)); |
1948
|
1605 |
|
1606 if (f77_exception_encountered) |
4329
|
1607 (*current_liboctave_error_handler) ("unrecoverable error in zgetrf"); |
458
|
1608 else |
|
1609 { |
4330
|
1610 // Throw-away extra info LAPACK gives so as to not change output. |
4509
|
1611 rcond = 0.0; |
|
1612 if (info != 0) |
1948
|
1613 { |
|
1614 info = -1; |
|
1615 retval = ComplexDET (); |
4329
|
1616 } |
|
1617 else |
1948
|
1618 { |
4329
|
1619 if (calc_cond) |
|
1620 { |
4330
|
1621 // Now calc the condition number for non-singular matrix. |
4329
|
1622 char job = '1'; |
|
1623 Array<Complex> z (2*nr); |
|
1624 Complex *pz = z.fortran_vec (); |
|
1625 Array<double> rz (2*nr); |
|
1626 double *prz = rz.fortran_vec (); |
|
1627 |
4552
|
1628 F77_XFCN (zgecon, ZGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1629 nc, tmp_data, nr, anorm, |
|
1630 rcond, pz, prz, info |
|
1631 F77_CHAR_ARG_LEN (1))); |
4329
|
1632 |
|
1633 if (f77_exception_encountered) |
|
1634 (*current_liboctave_error_handler) |
|
1635 ("unrecoverable error in zgecon"); |
|
1636 } |
|
1637 |
4509
|
1638 if (info != 0) |
4329
|
1639 { |
|
1640 info = -1; |
|
1641 retval = ComplexDET (); |
|
1642 } |
|
1643 else |
|
1644 { |
5634
|
1645 Complex c = 1.0; |
|
1646 int e = 0; |
|
1647 |
|
1648 for (octave_idx_type i = 0; i < nc; i++) |
4329
|
1649 { |
5634
|
1650 if (ipvt(i) != (i+1)) |
|
1651 c = -c; |
|
1652 |
|
1653 c *= atmp(i,i); |
|
1654 |
|
1655 if (c == 0.0) |
|
1656 break; |
|
1657 |
|
1658 while (std::abs(c) < 0.5) |
4329
|
1659 { |
5634
|
1660 c *= 2.0; |
|
1661 e--; |
4329
|
1662 } |
5634
|
1663 |
|
1664 while (std::abs(c) >= 2.0) |
4329
|
1665 { |
5634
|
1666 c /= 2.0; |
|
1667 e++; |
4329
|
1668 } |
|
1669 } |
5634
|
1670 |
|
1671 retval = ComplexDET (c, e); |
4329
|
1672 } |
1948
|
1673 } |
458
|
1674 } |
|
1675 } |
4329
|
1676 |
458
|
1677 return retval; |
|
1678 } |
|
1679 |
|
1680 ComplexMatrix |
5785
|
1681 ComplexMatrix::utsolve (MatrixType &mattype, const ComplexMatrix& b, |
|
1682 octave_idx_type& info, double& rcond, |
|
1683 solve_singularity_handler sing_handler, |
|
1684 bool calc_cond) const |
|
1685 { |
|
1686 ComplexMatrix retval; |
|
1687 |
|
1688 octave_idx_type nr = rows (); |
|
1689 octave_idx_type nc = cols (); |
|
1690 |
|
1691 if (nr == 0 || nc == 0 || nr != b.rows ()) |
|
1692 (*current_liboctave_error_handler) |
|
1693 ("matrix dimension mismatch solution of linear equations"); |
|
1694 else |
|
1695 { |
|
1696 volatile int typ = mattype.type (); |
|
1697 |
|
1698 if (typ == MatrixType::Permuted_Upper || |
|
1699 typ == MatrixType::Upper) |
|
1700 { |
|
1701 octave_idx_type b_nc = b.cols (); |
|
1702 rcond = 1.; |
|
1703 info = 0; |
|
1704 |
|
1705 if (typ == MatrixType::Permuted_Upper) |
|
1706 { |
|
1707 (*current_liboctave_error_handler) |
6390
|
1708 ("permuted triangular matrix not implemented"); |
5785
|
1709 } |
|
1710 else |
|
1711 { |
|
1712 const Complex *tmp_data = fortran_vec (); |
|
1713 |
|
1714 if (calc_cond) |
|
1715 { |
|
1716 char norm = '1'; |
|
1717 char uplo = 'U'; |
|
1718 char dia = 'N'; |
|
1719 |
|
1720 Array<Complex> z (2 * nc); |
|
1721 Complex *pz = z.fortran_vec (); |
|
1722 Array<double> rz (nc); |
|
1723 double *prz = rz.fortran_vec (); |
|
1724 |
|
1725 F77_XFCN (ztrcon, ZTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), |
|
1726 F77_CONST_CHAR_ARG2 (&uplo, 1), |
|
1727 F77_CONST_CHAR_ARG2 (&dia, 1), |
|
1728 nr, tmp_data, nr, rcond, |
|
1729 pz, prz, info |
|
1730 F77_CHAR_ARG_LEN (1) |
|
1731 F77_CHAR_ARG_LEN (1) |
|
1732 F77_CHAR_ARG_LEN (1))); |
|
1733 |
|
1734 if (f77_exception_encountered) |
|
1735 (*current_liboctave_error_handler) |
|
1736 ("unrecoverable error in ztrcon"); |
|
1737 |
|
1738 if (info != 0) |
|
1739 info = -2; |
|
1740 |
|
1741 volatile double rcond_plus_one = rcond + 1.0; |
|
1742 |
|
1743 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
1744 { |
|
1745 info = -2; |
|
1746 |
|
1747 if (sing_handler) |
|
1748 sing_handler (rcond); |
|
1749 else |
|
1750 (*current_liboctave_error_handler) |
|
1751 ("matrix singular to machine precision, rcond = %g", |
|
1752 rcond); |
|
1753 } |
|
1754 } |
|
1755 |
|
1756 if (info == 0) |
|
1757 { |
|
1758 retval = b; |
|
1759 Complex *result = retval.fortran_vec (); |
|
1760 |
|
1761 char uplo = 'U'; |
|
1762 char trans = 'N'; |
|
1763 char dia = 'N'; |
|
1764 |
|
1765 F77_XFCN (ztrtrs, ZTRTRS, (F77_CONST_CHAR_ARG2 (&uplo, 1), |
|
1766 F77_CONST_CHAR_ARG2 (&trans, 1), |
|
1767 F77_CONST_CHAR_ARG2 (&dia, 1), |
|
1768 nr, b_nc, tmp_data, nr, |
|
1769 result, nr, info |
|
1770 F77_CHAR_ARG_LEN (1) |
|
1771 F77_CHAR_ARG_LEN (1) |
|
1772 F77_CHAR_ARG_LEN (1))); |
|
1773 |
|
1774 if (f77_exception_encountered) |
|
1775 (*current_liboctave_error_handler) |
|
1776 ("unrecoverable error in dtrtrs"); |
|
1777 } |
|
1778 } |
|
1779 } |
|
1780 else |
|
1781 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
1782 } |
|
1783 |
|
1784 return retval; |
|
1785 } |
|
1786 |
|
1787 ComplexMatrix |
|
1788 ComplexMatrix::ltsolve (MatrixType &mattype, const ComplexMatrix& b, |
|
1789 octave_idx_type& info, double& rcond, |
|
1790 solve_singularity_handler sing_handler, |
|
1791 bool calc_cond) const |
|
1792 { |
|
1793 ComplexMatrix retval; |
|
1794 |
|
1795 octave_idx_type nr = rows (); |
|
1796 octave_idx_type nc = cols (); |
|
1797 |
|
1798 if (nr == 0 || nc == 0 || nr != b.rows ()) |
|
1799 (*current_liboctave_error_handler) |
|
1800 ("matrix dimension mismatch solution of linear equations"); |
|
1801 else |
|
1802 { |
|
1803 volatile int typ = mattype.type (); |
|
1804 |
|
1805 if (typ == MatrixType::Permuted_Lower || |
|
1806 typ == MatrixType::Lower) |
|
1807 { |
|
1808 octave_idx_type b_nc = b.cols (); |
|
1809 rcond = 1.; |
|
1810 info = 0; |
|
1811 |
|
1812 if (typ == MatrixType::Permuted_Lower) |
|
1813 { |
|
1814 (*current_liboctave_error_handler) |
6390
|
1815 ("permuted triangular matrix not implemented"); |
5785
|
1816 } |
|
1817 else |
|
1818 { |
|
1819 const Complex *tmp_data = fortran_vec (); |
|
1820 |
|
1821 if (calc_cond) |
|
1822 { |
|
1823 char norm = '1'; |
|
1824 char uplo = 'L'; |
|
1825 char dia = 'N'; |
|
1826 |
|
1827 Array<Complex> z (2 * nc); |
|
1828 Complex *pz = z.fortran_vec (); |
|
1829 Array<double> rz (nc); |
|
1830 double *prz = rz.fortran_vec (); |
|
1831 |
|
1832 F77_XFCN (ztrcon, ZTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), |
|
1833 F77_CONST_CHAR_ARG2 (&uplo, 1), |
|
1834 F77_CONST_CHAR_ARG2 (&dia, 1), |
|
1835 nr, tmp_data, nr, rcond, |
|
1836 pz, prz, info |
|
1837 F77_CHAR_ARG_LEN (1) |
|
1838 F77_CHAR_ARG_LEN (1) |
|
1839 F77_CHAR_ARG_LEN (1))); |
|
1840 |
|
1841 if (f77_exception_encountered) |
|
1842 (*current_liboctave_error_handler) |
|
1843 ("unrecoverable error in ztrcon"); |
|
1844 |
|
1845 if (info != 0) |
|
1846 info = -2; |
|
1847 |
|
1848 volatile double rcond_plus_one = rcond + 1.0; |
|
1849 |
|
1850 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
1851 { |
|
1852 info = -2; |
|
1853 |
|
1854 if (sing_handler) |
|
1855 sing_handler (rcond); |
|
1856 else |
|
1857 (*current_liboctave_error_handler) |
|
1858 ("matrix singular to machine precision, rcond = %g", |
|
1859 rcond); |
|
1860 } |
|
1861 } |
|
1862 |
|
1863 if (info == 0) |
|
1864 { |
|
1865 retval = b; |
|
1866 Complex *result = retval.fortran_vec (); |
|
1867 |
|
1868 char uplo = 'L'; |
|
1869 char trans = 'N'; |
|
1870 char dia = 'N'; |
|
1871 |
|
1872 F77_XFCN (ztrtrs, ZTRTRS, (F77_CONST_CHAR_ARG2 (&uplo, 1), |
|
1873 F77_CONST_CHAR_ARG2 (&trans, 1), |
|
1874 F77_CONST_CHAR_ARG2 (&dia, 1), |
|
1875 nr, b_nc, tmp_data, nr, |
|
1876 result, nr, info |
|
1877 F77_CHAR_ARG_LEN (1) |
|
1878 F77_CHAR_ARG_LEN (1) |
|
1879 F77_CHAR_ARG_LEN (1))); |
|
1880 |
|
1881 if (f77_exception_encountered) |
|
1882 (*current_liboctave_error_handler) |
|
1883 ("unrecoverable error in dtrtrs"); |
|
1884 } |
|
1885 } |
|
1886 } |
|
1887 else |
|
1888 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
1889 } |
|
1890 |
|
1891 return retval; |
|
1892 } |
|
1893 |
|
1894 ComplexMatrix |
|
1895 ComplexMatrix::fsolve (MatrixType &mattype, const ComplexMatrix& b, |
|
1896 octave_idx_type& info, double& rcond, |
|
1897 solve_singularity_handler sing_handler, |
|
1898 bool calc_cond) const |
|
1899 { |
|
1900 ComplexMatrix retval; |
|
1901 |
|
1902 octave_idx_type nr = rows (); |
|
1903 octave_idx_type nc = cols (); |
|
1904 |
|
1905 if (nr == 0 || nc == 0 || nr != nc || nr != b.rows ()) |
|
1906 (*current_liboctave_error_handler) |
|
1907 ("matrix dimension mismatch in solution of linear equations"); |
|
1908 else |
|
1909 { |
|
1910 volatile int typ = mattype.type (); |
|
1911 |
|
1912 // Calculate the norm of the matrix, for later use. |
|
1913 double anorm = -1.; |
|
1914 |
|
1915 if (typ == MatrixType::Hermitian) |
|
1916 { |
|
1917 info = 0; |
|
1918 char job = 'L'; |
|
1919 ComplexMatrix atmp = *this; |
|
1920 Complex *tmp_data = atmp.fortran_vec (); |
|
1921 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); |
|
1922 |
|
1923 F77_XFCN (zpotrf, ZPOTRF, (F77_CONST_CHAR_ARG2 (&job, 1), nr, |
|
1924 tmp_data, nr, info |
|
1925 F77_CHAR_ARG_LEN (1))); |
|
1926 |
|
1927 if (f77_exception_encountered) |
|
1928 (*current_liboctave_error_handler) |
|
1929 ("unrecoverable error in zpotrf"); |
|
1930 else |
|
1931 { |
|
1932 // Throw-away extra info LAPACK gives so as to not change output. |
|
1933 rcond = 0.0; |
|
1934 if (info != 0) |
|
1935 { |
|
1936 info = -2; |
|
1937 |
|
1938 mattype.mark_as_unsymmetric (); |
|
1939 typ = MatrixType::Full; |
|
1940 } |
|
1941 else |
|
1942 { |
|
1943 if (calc_cond) |
|
1944 { |
|
1945 Array<Complex> z (2 * nc); |
|
1946 Complex *pz = z.fortran_vec (); |
|
1947 Array<double> rz (nc); |
|
1948 double *prz = rz.fortran_vec (); |
|
1949 |
|
1950 F77_XFCN (zpocon, ZPOCON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1951 nr, tmp_data, nr, anorm, |
|
1952 rcond, pz, prz, info |
|
1953 F77_CHAR_ARG_LEN (1))); |
|
1954 |
|
1955 if (f77_exception_encountered) |
|
1956 (*current_liboctave_error_handler) |
|
1957 ("unrecoverable error in zpocon"); |
|
1958 |
|
1959 if (info != 0) |
|
1960 info = -2; |
|
1961 |
|
1962 volatile double rcond_plus_one = rcond + 1.0; |
|
1963 |
|
1964 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
1965 { |
|
1966 info = -2; |
|
1967 |
|
1968 if (sing_handler) |
|
1969 sing_handler (rcond); |
|
1970 else |
|
1971 (*current_liboctave_error_handler) |
|
1972 ("matrix singular to machine precision, rcond = %g", |
|
1973 rcond); |
|
1974 } |
|
1975 } |
|
1976 |
|
1977 if (info == 0) |
|
1978 { |
|
1979 retval = b; |
|
1980 Complex *result = retval.fortran_vec (); |
|
1981 |
|
1982 octave_idx_type b_nc = b.cols (); |
|
1983 |
|
1984 F77_XFCN (zpotrs, ZPOTRS, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1985 nr, b_nc, tmp_data, nr, |
|
1986 result, b.rows(), info |
|
1987 F77_CHAR_ARG_LEN (1))); |
|
1988 |
|
1989 if (f77_exception_encountered) |
|
1990 (*current_liboctave_error_handler) |
|
1991 ("unrecoverable error in zpotrs"); |
|
1992 } |
|
1993 else |
|
1994 { |
|
1995 mattype.mark_as_unsymmetric (); |
|
1996 typ = MatrixType::Full; |
|
1997 } |
|
1998 } |
|
1999 } |
|
2000 } |
|
2001 |
|
2002 if (typ == MatrixType::Full) |
|
2003 { |
|
2004 info = 0; |
|
2005 |
|
2006 Array<octave_idx_type> ipvt (nr); |
|
2007 octave_idx_type *pipvt = ipvt.fortran_vec (); |
|
2008 |
|
2009 ComplexMatrix atmp = *this; |
|
2010 Complex *tmp_data = atmp.fortran_vec (); |
|
2011 |
|
2012 Array<Complex> z (2 * nc); |
|
2013 Complex *pz = z.fortran_vec (); |
|
2014 Array<double> rz (2 * nc); |
|
2015 double *prz = rz.fortran_vec (); |
|
2016 |
|
2017 // Calculate the norm of the matrix, for later use. |
|
2018 if (anorm < 0.) |
|
2019 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); |
|
2020 |
|
2021 F77_XFCN (zgetrf, ZGETRF, (nr, nr, tmp_data, nr, pipvt, info)); |
|
2022 |
|
2023 if (f77_exception_encountered) |
|
2024 (*current_liboctave_error_handler) |
|
2025 ("unrecoverable error in zgetrf"); |
|
2026 else |
|
2027 { |
|
2028 // Throw-away extra info LAPACK gives so as to not change output. |
|
2029 rcond = 0.0; |
|
2030 if (info != 0) |
|
2031 { |
|
2032 info = -2; |
|
2033 |
|
2034 if (sing_handler) |
|
2035 sing_handler (rcond); |
|
2036 else |
|
2037 (*current_liboctave_error_handler) |
|
2038 ("matrix singular to machine precision"); |
|
2039 |
|
2040 mattype.mark_as_rectangular (); |
|
2041 } |
|
2042 else |
|
2043 { |
|
2044 if (calc_cond) |
|
2045 { |
|
2046 // Now calculate the condition number for |
|
2047 // non-singular matrix. |
|
2048 char job = '1'; |
|
2049 F77_XFCN (zgecon, ZGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
2050 nc, tmp_data, nr, anorm, |
|
2051 rcond, pz, prz, info |
|
2052 F77_CHAR_ARG_LEN (1))); |
|
2053 |
|
2054 if (f77_exception_encountered) |
|
2055 (*current_liboctave_error_handler) |
|
2056 ("unrecoverable error in zgecon"); |
|
2057 |
|
2058 if (info != 0) |
|
2059 info = -2; |
|
2060 |
|
2061 volatile double rcond_plus_one = rcond + 1.0; |
|
2062 |
|
2063 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
2064 { |
|
2065 info = -2; |
|
2066 |
|
2067 if (sing_handler) |
|
2068 sing_handler (rcond); |
|
2069 else |
|
2070 (*current_liboctave_error_handler) |
|
2071 ("matrix singular to machine precision, rcond = %g", |
|
2072 rcond); |
|
2073 } |
|
2074 } |
|
2075 |
|
2076 if (info == 0) |
|
2077 { |
|
2078 retval = b; |
|
2079 Complex *result = retval.fortran_vec (); |
|
2080 |
|
2081 octave_idx_type b_nc = b.cols (); |
|
2082 |
|
2083 char job = 'N'; |
|
2084 F77_XFCN (zgetrs, ZGETRS, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
2085 nr, b_nc, tmp_data, nr, |
|
2086 pipvt, result, b.rows(), info |
|
2087 F77_CHAR_ARG_LEN (1))); |
|
2088 |
|
2089 if (f77_exception_encountered) |
|
2090 (*current_liboctave_error_handler) |
|
2091 ("unrecoverable error in zgetrs"); |
|
2092 } |
|
2093 else |
|
2094 mattype.mark_as_rectangular (); |
|
2095 } |
|
2096 } |
|
2097 } |
|
2098 } |
|
2099 |
|
2100 return retval; |
|
2101 } |
|
2102 |
|
2103 ComplexMatrix |
|
2104 ComplexMatrix::solve (MatrixType &typ, const Matrix& b) const |
|
2105 { |
|
2106 octave_idx_type info; |
|
2107 double rcond; |
|
2108 return solve (typ, b, info, rcond, 0); |
|
2109 } |
|
2110 |
|
2111 ComplexMatrix |
|
2112 ComplexMatrix::solve (MatrixType &typ, const Matrix& b, |
|
2113 octave_idx_type& info) const |
|
2114 { |
|
2115 double rcond; |
|
2116 return solve (typ, b, info, rcond, 0); |
|
2117 } |
|
2118 |
|
2119 ComplexMatrix |
|
2120 ComplexMatrix::solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
2121 double& rcond) const |
|
2122 { |
|
2123 return solve (typ, b, info, rcond, 0); |
|
2124 } |
|
2125 |
|
2126 ComplexMatrix |
|
2127 ComplexMatrix::solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
2128 double& rcond, solve_singularity_handler sing_handler, |
|
2129 bool singular_fallback) const |
|
2130 { |
|
2131 ComplexMatrix tmp (b); |
|
2132 return solve (typ, tmp, info, rcond, sing_handler, singular_fallback); |
|
2133 } |
|
2134 |
|
2135 ComplexMatrix |
|
2136 ComplexMatrix::solve (MatrixType &typ, const ComplexMatrix& b) const |
|
2137 { |
|
2138 octave_idx_type info; |
|
2139 double rcond; |
|
2140 return solve (typ, b, info, rcond, 0); |
|
2141 } |
|
2142 |
|
2143 ComplexMatrix |
|
2144 ComplexMatrix::solve (MatrixType &typ, const ComplexMatrix& b, |
|
2145 octave_idx_type& info) const |
|
2146 { |
|
2147 double rcond; |
|
2148 return solve (typ, b, info, rcond, 0); |
|
2149 } |
|
2150 |
|
2151 ComplexMatrix |
|
2152 ComplexMatrix::solve (MatrixType &typ, const ComplexMatrix& b, |
|
2153 octave_idx_type& info, double& rcond) const |
|
2154 { |
|
2155 return solve (typ, b, info, rcond, 0); |
|
2156 } |
|
2157 |
|
2158 ComplexMatrix |
|
2159 ComplexMatrix::solve (MatrixType &mattype, const ComplexMatrix& b, |
|
2160 octave_idx_type& info, double& rcond, |
|
2161 solve_singularity_handler sing_handler, |
|
2162 bool singular_fallback) const |
|
2163 { |
|
2164 ComplexMatrix retval; |
|
2165 int typ = mattype.type (); |
|
2166 |
|
2167 if (typ == MatrixType::Unknown) |
|
2168 typ = mattype.type (*this); |
|
2169 |
|
2170 // Only calculate the condition number for LU/Cholesky |
|
2171 if (typ == MatrixType::Upper || typ == MatrixType::Permuted_Upper) |
|
2172 retval = utsolve (mattype, b, info, rcond, sing_handler, false); |
|
2173 else if (typ == MatrixType::Lower || typ == MatrixType::Permuted_Lower) |
|
2174 retval = ltsolve (mattype, b, info, rcond, sing_handler, false); |
|
2175 else if (typ == MatrixType::Full || typ == MatrixType::Hermitian) |
|
2176 retval = fsolve (mattype, b, info, rcond, sing_handler, true); |
|
2177 else if (typ != MatrixType::Rectangular) |
|
2178 { |
|
2179 (*current_liboctave_error_handler) ("unknown matrix type"); |
|
2180 return ComplexMatrix (); |
|
2181 } |
|
2182 |
|
2183 // Rectangular or one of the above solvers flags a singular matrix |
|
2184 if (singular_fallback && mattype.type () == MatrixType::Rectangular) |
|
2185 { |
|
2186 octave_idx_type rank; |
|
2187 retval = lssolve (b, info, rank); |
|
2188 } |
|
2189 |
|
2190 return retval; |
|
2191 } |
|
2192 |
|
2193 ComplexColumnVector |
|
2194 ComplexMatrix::solve (MatrixType &typ, const ColumnVector& b) const |
|
2195 { |
|
2196 octave_idx_type info; |
|
2197 double rcond; |
|
2198 return solve (typ, ComplexColumnVector (b), info, rcond, 0); |
|
2199 } |
|
2200 |
|
2201 ComplexColumnVector |
|
2202 ComplexMatrix::solve (MatrixType &typ, const ColumnVector& b, |
|
2203 octave_idx_type& info) const |
|
2204 { |
|
2205 double rcond; |
|
2206 return solve (typ, ComplexColumnVector (b), info, rcond, 0); |
|
2207 } |
|
2208 |
|
2209 ComplexColumnVector |
|
2210 ComplexMatrix::solve (MatrixType &typ, const ColumnVector& b, |
|
2211 octave_idx_type& info, double& rcond) const |
|
2212 { |
|
2213 return solve (typ, ComplexColumnVector (b), info, rcond, 0); |
|
2214 } |
|
2215 |
|
2216 ComplexColumnVector |
|
2217 ComplexMatrix::solve (MatrixType &typ, const ColumnVector& b, |
|
2218 octave_idx_type& info, double& rcond, |
|
2219 solve_singularity_handler sing_handler) const |
|
2220 { |
|
2221 return solve (typ, ComplexColumnVector (b), info, rcond, sing_handler); |
|
2222 } |
|
2223 |
|
2224 ComplexColumnVector |
|
2225 ComplexMatrix::solve (MatrixType &typ, const ComplexColumnVector& b) const |
|
2226 { |
|
2227 octave_idx_type info; |
|
2228 double rcond; |
|
2229 return solve (typ, b, info, rcond, 0); |
|
2230 } |
|
2231 |
|
2232 ComplexColumnVector |
|
2233 ComplexMatrix::solve (MatrixType &typ, const ComplexColumnVector& b, |
|
2234 octave_idx_type& info) const |
|
2235 { |
|
2236 double rcond; |
|
2237 return solve (typ, b, info, rcond, 0); |
|
2238 } |
|
2239 |
|
2240 ComplexColumnVector |
|
2241 ComplexMatrix::solve (MatrixType &typ, const ComplexColumnVector& b, |
|
2242 octave_idx_type& info, double& rcond) const |
|
2243 { |
|
2244 return solve (typ, b, info, rcond, 0); |
|
2245 } |
|
2246 |
|
2247 ComplexColumnVector |
|
2248 ComplexMatrix::solve (MatrixType &typ, const ComplexColumnVector& b, |
|
2249 octave_idx_type& info, double& rcond, |
|
2250 solve_singularity_handler sing_handler) const |
|
2251 { |
|
2252 |
|
2253 ComplexMatrix tmp (b); |
|
2254 return solve (typ, tmp, info, rcond, sing_handler).column(static_cast<octave_idx_type> (0)); |
|
2255 } |
|
2256 |
|
2257 ComplexMatrix |
458
|
2258 ComplexMatrix::solve (const Matrix& b) const |
|
2259 { |
5275
|
2260 octave_idx_type info; |
458
|
2261 double rcond; |
3480
|
2262 return solve (b, info, rcond, 0); |
458
|
2263 } |
|
2264 |
|
2265 ComplexMatrix |
5275
|
2266 ComplexMatrix::solve (const Matrix& b, octave_idx_type& info) const |
458
|
2267 { |
|
2268 double rcond; |
3480
|
2269 return solve (b, info, rcond, 0); |
458
|
2270 } |
|
2271 |
|
2272 ComplexMatrix |
5275
|
2273 ComplexMatrix::solve (const Matrix& b, octave_idx_type& info, double& rcond) const |
458
|
2274 { |
3480
|
2275 return solve (b, info, rcond, 0); |
|
2276 } |
|
2277 |
|
2278 ComplexMatrix |
5275
|
2279 ComplexMatrix::solve (const Matrix& b, octave_idx_type& info, double& rcond, |
3480
|
2280 solve_singularity_handler sing_handler) const |
|
2281 { |
458
|
2282 ComplexMatrix tmp (b); |
3480
|
2283 return solve (tmp, info, rcond, sing_handler); |
458
|
2284 } |
|
2285 |
|
2286 ComplexMatrix |
|
2287 ComplexMatrix::solve (const ComplexMatrix& b) const |
|
2288 { |
5275
|
2289 octave_idx_type info; |
458
|
2290 double rcond; |
3480
|
2291 return solve (b, info, rcond, 0); |
458
|
2292 } |
|
2293 |
|
2294 ComplexMatrix |
5275
|
2295 ComplexMatrix::solve (const ComplexMatrix& b, octave_idx_type& info) const |
458
|
2296 { |
|
2297 double rcond; |
3480
|
2298 return solve (b, info, rcond, 0); |
458
|
2299 } |
3480
|
2300 |
458
|
2301 ComplexMatrix |
5275
|
2302 ComplexMatrix::solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond) const |
458
|
2303 { |
3480
|
2304 return solve (b, info, rcond, 0); |
|
2305 } |
|
2306 |
|
2307 ComplexMatrix |
5275
|
2308 ComplexMatrix::solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond, |
3480
|
2309 solve_singularity_handler sing_handler) const |
|
2310 { |
5785
|
2311 MatrixType mattype (*this); |
6060
|
2312 return solve (mattype, b, info, rcond, sing_handler); |
458
|
2313 } |
|
2314 |
|
2315 ComplexColumnVector |
3585
|
2316 ComplexMatrix::solve (const ColumnVector& b) const |
|
2317 { |
5275
|
2318 octave_idx_type info; |
3585
|
2319 double rcond; |
|
2320 return solve (ComplexColumnVector (b), info, rcond, 0); |
|
2321 } |
|
2322 |
|
2323 ComplexColumnVector |
5275
|
2324 ComplexMatrix::solve (const ColumnVector& b, octave_idx_type& info) const |
3585
|
2325 { |
|
2326 double rcond; |
|
2327 return solve (ComplexColumnVector (b), info, rcond, 0); |
|
2328 } |
|
2329 |
|
2330 ComplexColumnVector |
5785
|
2331 ComplexMatrix::solve (const ColumnVector& b, octave_idx_type& info, |
|
2332 double& rcond) const |
3585
|
2333 { |
|
2334 return solve (ComplexColumnVector (b), info, rcond, 0); |
|
2335 } |
|
2336 |
|
2337 ComplexColumnVector |
5785
|
2338 ComplexMatrix::solve (const ColumnVector& b, octave_idx_type& info, |
|
2339 double& rcond, |
3585
|
2340 solve_singularity_handler sing_handler) const |
|
2341 { |
|
2342 return solve (ComplexColumnVector (b), info, rcond, sing_handler); |
|
2343 } |
|
2344 |
|
2345 ComplexColumnVector |
458
|
2346 ComplexMatrix::solve (const ComplexColumnVector& b) const |
|
2347 { |
5275
|
2348 octave_idx_type info; |
458
|
2349 double rcond; |
3480
|
2350 return solve (b, info, rcond, 0); |
458
|
2351 } |
|
2352 |
|
2353 ComplexColumnVector |
5275
|
2354 ComplexMatrix::solve (const ComplexColumnVector& b, octave_idx_type& info) const |
458
|
2355 { |
|
2356 double rcond; |
3480
|
2357 return solve (b, info, rcond, 0); |
458
|
2358 } |
|
2359 |
|
2360 ComplexColumnVector |
5275
|
2361 ComplexMatrix::solve (const ComplexColumnVector& b, octave_idx_type& info, |
532
|
2362 double& rcond) const |
458
|
2363 { |
3480
|
2364 return solve (b, info, rcond, 0); |
|
2365 } |
|
2366 |
|
2367 ComplexColumnVector |
5275
|
2368 ComplexMatrix::solve (const ComplexColumnVector& b, octave_idx_type& info, |
3480
|
2369 double& rcond, |
|
2370 solve_singularity_handler sing_handler) const |
|
2371 { |
5785
|
2372 MatrixType mattype (*this); |
|
2373 return solve (mattype, b, info, rcond, sing_handler); |
458
|
2374 } |
|
2375 |
|
2376 ComplexMatrix |
3585
|
2377 ComplexMatrix::lssolve (const Matrix& b) const |
|
2378 { |
5275
|
2379 octave_idx_type info; |
|
2380 octave_idx_type rank; |
3585
|
2381 return lssolve (ComplexMatrix (b), info, rank); |
|
2382 } |
|
2383 |
|
2384 ComplexMatrix |
5275
|
2385 ComplexMatrix::lssolve (const Matrix& b, octave_idx_type& info) const |
3585
|
2386 { |
5275
|
2387 octave_idx_type rank; |
3585
|
2388 return lssolve (ComplexMatrix (b), info, rank); |
|
2389 } |
|
2390 |
|
2391 ComplexMatrix |
5275
|
2392 ComplexMatrix::lssolve (const Matrix& b, octave_idx_type& info, octave_idx_type& rank) const |
3585
|
2393 { |
|
2394 return lssolve (ComplexMatrix (b), info, rank); |
|
2395 } |
|
2396 |
|
2397 ComplexMatrix |
458
|
2398 ComplexMatrix::lssolve (const ComplexMatrix& b) const |
|
2399 { |
5275
|
2400 octave_idx_type info; |
|
2401 octave_idx_type rank; |
458
|
2402 return lssolve (b, info, rank); |
|
2403 } |
|
2404 |
|
2405 ComplexMatrix |
5275
|
2406 ComplexMatrix::lssolve (const ComplexMatrix& b, octave_idx_type& info) const |
458
|
2407 { |
5275
|
2408 octave_idx_type rank; |
458
|
2409 return lssolve (b, info, rank); |
|
2410 } |
|
2411 |
|
2412 ComplexMatrix |
5275
|
2413 ComplexMatrix::lssolve (const ComplexMatrix& b, octave_idx_type& info, octave_idx_type& rank) const |
458
|
2414 { |
1948
|
2415 ComplexMatrix retval; |
|
2416 |
5275
|
2417 octave_idx_type nrhs = b.cols (); |
|
2418 |
|
2419 octave_idx_type m = rows (); |
|
2420 octave_idx_type n = cols (); |
458
|
2421 |
|
2422 if (m == 0 || n == 0 || m != b.rows ()) |
1948
|
2423 (*current_liboctave_error_handler) |
|
2424 ("matrix dimension mismatch solution of linear equations"); |
|
2425 else |
458
|
2426 { |
1948
|
2427 ComplexMatrix atmp = *this; |
|
2428 Complex *tmp_data = atmp.fortran_vec (); |
|
2429 |
5275
|
2430 octave_idx_type nrr = m > n ? m : n; |
1948
|
2431 ComplexMatrix result (nrr, nrhs); |
|
2432 |
5275
|
2433 for (octave_idx_type j = 0; j < nrhs; j++) |
|
2434 for (octave_idx_type i = 0; i < m; i++) |
1948
|
2435 result.elem (i, j) = b.elem (i, j); |
|
2436 |
|
2437 Complex *presult = result.fortran_vec (); |
|
2438 |
5275
|
2439 octave_idx_type len_s = m < n ? m : n; |
1948
|
2440 Array<double> s (len_s); |
|
2441 double *ps = s.fortran_vec (); |
2563
|
2442 |
1948
|
2443 double rcond = -1.0; |
2563
|
2444 |
5275
|
2445 octave_idx_type lrwork = (5 * (m < n ? m : n)) - 4; |
1948
|
2446 lrwork = lrwork > 1 ? lrwork : 1; |
|
2447 Array<double> rwork (lrwork); |
|
2448 double *prwork = rwork.fortran_vec (); |
|
2449 |
3752
|
2450 // Ask ZGELSS what the dimension of WORK should be. |
|
2451 |
5275
|
2452 octave_idx_type lwork = -1; |
3752
|
2453 |
|
2454 Array<Complex> work (1); |
|
2455 |
1948
|
2456 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
3752
|
2457 nrr, ps, rcond, rank, |
|
2458 work.fortran_vec (), lwork, prwork, |
|
2459 info)); |
1948
|
2460 |
|
2461 if (f77_exception_encountered) |
|
2462 (*current_liboctave_error_handler) ("unrecoverable error in zgelss"); |
|
2463 else |
|
2464 { |
5315
|
2465 lwork = static_cast<octave_idx_type> (std::real (work(0))); |
3752
|
2466 work.resize (lwork); |
|
2467 |
|
2468 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
2469 nrr, ps, rcond, rank, |
|
2470 work.fortran_vec (), lwork, |
|
2471 prwork, info)); |
|
2472 |
|
2473 if (f77_exception_encountered) |
|
2474 (*current_liboctave_error_handler) |
|
2475 ("unrecoverable error in zgelss"); |
|
2476 else |
|
2477 { |
|
2478 retval.resize (n, nrhs); |
5275
|
2479 for (octave_idx_type j = 0; j < nrhs; j++) |
|
2480 for (octave_idx_type i = 0; i < n; i++) |
3752
|
2481 retval.elem (i, j) = result.elem (i, j); |
|
2482 } |
1948
|
2483 } |
458
|
2484 } |
|
2485 |
|
2486 return retval; |
|
2487 } |
|
2488 |
|
2489 ComplexColumnVector |
3585
|
2490 ComplexMatrix::lssolve (const ColumnVector& b) const |
|
2491 { |
5275
|
2492 octave_idx_type info; |
|
2493 octave_idx_type rank; |
3585
|
2494 return lssolve (ComplexColumnVector (b), info, rank); |
|
2495 } |
|
2496 |
|
2497 ComplexColumnVector |
5275
|
2498 ComplexMatrix::lssolve (const ColumnVector& b, octave_idx_type& info) const |
3585
|
2499 { |
5275
|
2500 octave_idx_type rank; |
3585
|
2501 return lssolve (ComplexColumnVector (b), info, rank); |
|
2502 } |
|
2503 |
|
2504 ComplexColumnVector |
5275
|
2505 ComplexMatrix::lssolve (const ColumnVector& b, octave_idx_type& info, octave_idx_type& rank) const |
3585
|
2506 { |
|
2507 return lssolve (ComplexColumnVector (b), info, rank); |
|
2508 } |
|
2509 |
|
2510 ComplexColumnVector |
458
|
2511 ComplexMatrix::lssolve (const ComplexColumnVector& b) const |
|
2512 { |
5275
|
2513 octave_idx_type info; |
|
2514 octave_idx_type rank; |
458
|
2515 return lssolve (b, info, rank); |
|
2516 } |
|
2517 |
|
2518 ComplexColumnVector |
5275
|
2519 ComplexMatrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info) const |
458
|
2520 { |
5275
|
2521 octave_idx_type rank; |
458
|
2522 return lssolve (b, info, rank); |
|
2523 } |
|
2524 |
|
2525 ComplexColumnVector |
5275
|
2526 ComplexMatrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info, |
|
2527 octave_idx_type& rank) const |
458
|
2528 { |
1948
|
2529 ComplexColumnVector retval; |
|
2530 |
5275
|
2531 octave_idx_type nrhs = 1; |
|
2532 |
|
2533 octave_idx_type m = rows (); |
|
2534 octave_idx_type n = cols (); |
458
|
2535 |
|
2536 if (m == 0 || n == 0 || m != b.length ()) |
1948
|
2537 (*current_liboctave_error_handler) |
|
2538 ("matrix dimension mismatch solution of least squares problem"); |
|
2539 else |
458
|
2540 { |
1948
|
2541 ComplexMatrix atmp = *this; |
|
2542 Complex *tmp_data = atmp.fortran_vec (); |
|
2543 |
5275
|
2544 octave_idx_type nrr = m > n ? m : n; |
1948
|
2545 ComplexColumnVector result (nrr); |
|
2546 |
5275
|
2547 for (octave_idx_type i = 0; i < m; i++) |
1948
|
2548 result.elem (i) = b.elem (i); |
|
2549 |
|
2550 Complex *presult = result.fortran_vec (); |
|
2551 |
5275
|
2552 octave_idx_type len_s = m < n ? m : n; |
1948
|
2553 Array<double> s (len_s); |
|
2554 double *ps = s.fortran_vec (); |
|
2555 |
|
2556 double rcond = -1.0; |
|
2557 |
5275
|
2558 octave_idx_type lrwork = (5 * (m < n ? m : n)) - 4; |
1948
|
2559 lrwork = lrwork > 1 ? lrwork : 1; |
|
2560 Array<double> rwork (lrwork); |
|
2561 double *prwork = rwork.fortran_vec (); |
|
2562 |
3752
|
2563 // Ask ZGELSS what the dimension of WORK should be. |
|
2564 |
5275
|
2565 octave_idx_type lwork = -1; |
3752
|
2566 |
|
2567 Array<Complex> work (1); |
|
2568 |
1948
|
2569 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
3752
|
2570 nrr, ps, rcond, rank, |
|
2571 work.fortran_vec (), lwork, prwork, |
|
2572 info)); |
1948
|
2573 |
|
2574 if (f77_exception_encountered) |
|
2575 (*current_liboctave_error_handler) ("unrecoverable error in zgelss"); |
|
2576 else |
|
2577 { |
5315
|
2578 lwork = static_cast<int> (std::real (work(0))); |
3752
|
2579 work.resize (lwork); |
|
2580 |
|
2581 F77_XFCN (zgelss, ZGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
2582 nrr, ps, rcond, rank, |
|
2583 work.fortran_vec (), lwork, |
|
2584 prwork, info)); |
|
2585 |
|
2586 if (f77_exception_encountered) |
|
2587 (*current_liboctave_error_handler) |
|
2588 ("unrecoverable error in zgelss"); |
|
2589 else |
|
2590 { |
|
2591 retval.resize (n); |
5275
|
2592 for (octave_idx_type i = 0; i < n; i++) |
3752
|
2593 retval.elem (i) = result.elem (i); |
|
2594 } |
1948
|
2595 } |
458
|
2596 } |
|
2597 |
|
2598 return retval; |
|
2599 } |
|
2600 |
1819
|
2601 // Constants for matrix exponential calculation. |
|
2602 |
|
2603 static double padec [] = |
|
2604 { |
|
2605 5.0000000000000000e-1, |
|
2606 1.1666666666666667e-1, |
|
2607 1.6666666666666667e-2, |
|
2608 1.6025641025641026e-3, |
|
2609 1.0683760683760684e-4, |
|
2610 4.8562548562548563e-6, |
|
2611 1.3875013875013875e-7, |
|
2612 1.9270852604185938e-9, |
|
2613 }; |
|
2614 |
|
2615 ComplexMatrix |
|
2616 ComplexMatrix::expm (void) const |
|
2617 { |
|
2618 ComplexMatrix retval; |
|
2619 |
|
2620 ComplexMatrix m = *this; |
|
2621 |
5275
|
2622 octave_idx_type nc = columns (); |
1819
|
2623 |
3130
|
2624 // Preconditioning step 1: trace normalization to reduce dynamic |
|
2625 // range of poles, but avoid making stable eigenvalues unstable. |
|
2626 |
1819
|
2627 // trace shift value |
|
2628 Complex trshift = 0.0; |
|
2629 |
5275
|
2630 for (octave_idx_type i = 0; i < nc; i++) |
1819
|
2631 trshift += m.elem (i, i); |
|
2632 |
|
2633 trshift /= nc; |
|
2634 |
3130
|
2635 if (trshift.real () < 0.0) |
|
2636 trshift = trshift.imag (); |
|
2637 |
5275
|
2638 for (octave_idx_type i = 0; i < nc; i++) |
1819
|
2639 m.elem (i, i) -= trshift; |
|
2640 |
|
2641 // Preconditioning step 2: eigenvalue balancing. |
3331
|
2642 // code follows development in AEPBAL |
|
2643 |
|
2644 Complex *mp = m.fortran_vec (); |
3467
|
2645 |
5275
|
2646 octave_idx_type info, ilo, ihi,ilos,ihis; |
3468
|
2647 Array<double> dpermute (nc); |
|
2648 Array<double> dscale (nc); |
|
2649 |
5775
|
2650 // FIXME -- should pass job as a parameter in expm |
3468
|
2651 |
|
2652 // Permute first |
|
2653 char job = 'P'; |
4552
|
2654 F77_XFCN (zgebal, ZGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
2655 nc, mp, nc, ilo, ihi, |
|
2656 dpermute.fortran_vec (), info |
|
2657 F77_CHAR_ARG_LEN (1))); |
3331
|
2658 |
|
2659 if (f77_exception_encountered) |
|
2660 { |
|
2661 (*current_liboctave_error_handler) ("unrecoverable error in zgebal"); |
|
2662 return retval; |
|
2663 } |
|
2664 |
3468
|
2665 // then scale |
|
2666 job = 'S'; |
4552
|
2667 F77_XFCN (zgebal, ZGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
2668 nc, mp, nc, ilos, ihis, |
|
2669 dscale.fortran_vec (), info |
|
2670 F77_CHAR_ARG_LEN (1))); |
3331
|
2671 |
|
2672 if (f77_exception_encountered) |
|
2673 { |
3467
|
2674 (*current_liboctave_error_handler) ("unrecoverable error in zgebal"); |
3331
|
2675 return retval; |
|
2676 } |
1819
|
2677 |
|
2678 // Preconditioning step 3: scaling. |
|
2679 |
|
2680 ColumnVector work (nc); |
3130
|
2681 double inf_norm; |
|
2682 |
4552
|
2683 F77_XFCN (xzlange, XZLANGE, (F77_CONST_CHAR_ARG2 ("I", 1), |
|
2684 nc, nc, m.fortran_vec (), nc, |
|
2685 work.fortran_vec (), inf_norm |
|
2686 F77_CHAR_ARG_LEN (1))); |
3331
|
2687 |
|
2688 if (f77_exception_encountered) |
|
2689 { |
|
2690 (*current_liboctave_error_handler) ("unrecoverable error in zlange"); |
|
2691 return retval; |
|
2692 } |
1819
|
2693 |
2800
|
2694 int sqpow = (inf_norm > 0.0 |
|
2695 ? static_cast<int> (1.0 + log (inf_norm) / log (2.0)) : 0); |
1819
|
2696 |
|
2697 // Check whether we need to square at all. |
|
2698 |
|
2699 if (sqpow < 0) |
|
2700 sqpow = 0; |
|
2701 |
|
2702 if (sqpow > 0) |
|
2703 { |
|
2704 double scale_factor = 1.0; |
5275
|
2705 for (octave_idx_type i = 0; i < sqpow; i++) |
1819
|
2706 scale_factor *= 2.0; |
|
2707 |
|
2708 m = m / scale_factor; |
|
2709 } |
|
2710 |
|
2711 // npp, dpp: pade' approx polynomial matrices. |
|
2712 |
|
2713 ComplexMatrix npp (nc, nc, 0.0); |
|
2714 ComplexMatrix dpp = npp; |
|
2715 |
|
2716 // Now powers a^8 ... a^1. |
|
2717 |
|
2718 int minus_one_j = -1; |
5275
|
2719 for (octave_idx_type j = 7; j >= 0; j--) |
1819
|
2720 { |
|
2721 npp = m * npp + m * padec[j]; |
|
2722 dpp = m * dpp + m * (minus_one_j * padec[j]); |
|
2723 minus_one_j *= -1; |
|
2724 } |
|
2725 |
|
2726 // Zero power. |
|
2727 |
|
2728 dpp = -dpp; |
5275
|
2729 for (octave_idx_type j = 0; j < nc; j++) |
1819
|
2730 { |
|
2731 npp.elem (j, j) += 1.0; |
|
2732 dpp.elem (j, j) += 1.0; |
|
2733 } |
|
2734 |
|
2735 // Compute pade approximation = inverse (dpp) * npp. |
|
2736 |
|
2737 retval = dpp.solve (npp); |
|
2738 |
|
2739 // Reverse preconditioning step 3: repeated squaring. |
|
2740 |
|
2741 while (sqpow) |
|
2742 { |
|
2743 retval = retval * retval; |
|
2744 sqpow--; |
|
2745 } |
|
2746 |
|
2747 // Reverse preconditioning step 2: inverse balancing. |
3467
|
2748 // Done in two steps: inverse scaling, then inverse permutation |
|
2749 |
|
2750 // inverse scaling (diagonal transformation) |
5275
|
2751 for (octave_idx_type i = 0; i < nc; i++) |
|
2752 for (octave_idx_type j = 0; j < nc; j++) |
3468
|
2753 retval(i,j) *= dscale(i) / dscale(j); |
3467
|
2754 |
4153
|
2755 OCTAVE_QUIT; |
|
2756 |
3467
|
2757 // construct balancing permutation vector |
4593
|
2758 Array<int> iperm (nc); |
5275
|
2759 for (octave_idx_type i = 0; i < nc; i++) |
4593
|
2760 iperm(i) = i; // initialize to identity permutation |
3467
|
2761 |
|
2762 // leading permutations in forward order |
5275
|
2763 for (octave_idx_type i = 0; i < (ilo-1); i++) |
3468
|
2764 { |
5275
|
2765 octave_idx_type swapidx = static_cast<int> (dpermute(i)) - 1; |
|
2766 octave_idx_type tmp = iperm(i); |
4593
|
2767 iperm(i) = iperm(swapidx); |
|
2768 iperm(swapidx) = tmp; |
3468
|
2769 } |
3467
|
2770 |
|
2771 // trailing permutations must be done in reverse order |
5275
|
2772 for (octave_idx_type i = nc - 1; i >= ihi; i--) |
3468
|
2773 { |
5275
|
2774 octave_idx_type swapidx = static_cast<int> (dpermute(i)) - 1; |
|
2775 octave_idx_type tmp = iperm(i); |
4593
|
2776 iperm(i) = iperm(swapidx); |
|
2777 iperm(swapidx) = tmp; |
3468
|
2778 } |
3467
|
2779 |
|
2780 // construct inverse balancing permutation vector |
3468
|
2781 Array<int> invpvec (nc); |
5275
|
2782 for (octave_idx_type i = 0; i < nc; i++) |
4593
|
2783 invpvec(iperm(i)) = i; // Thanks to R. A. Lippert for this method |
3467
|
2784 |
4153
|
2785 OCTAVE_QUIT; |
|
2786 |
3467
|
2787 ComplexMatrix tmpMat = retval; |
5275
|
2788 for (octave_idx_type i = 0; i < nc; i++) |
|
2789 for (octave_idx_type j = 0; j < nc; j++) |
3468
|
2790 retval(i,j) = tmpMat(invpvec(i),invpvec(j)); |
1819
|
2791 |
|
2792 // Reverse preconditioning step 1: fix trace normalization. |
|
2793 |
3130
|
2794 return exp (trshift) * retval; |
1819
|
2795 } |
|
2796 |
1205
|
2797 // column vector by row vector -> matrix operations |
|
2798 |
|
2799 ComplexMatrix |
|
2800 operator * (const ColumnVector& v, const ComplexRowVector& a) |
|
2801 { |
|
2802 ComplexColumnVector tmp (v); |
|
2803 return tmp * a; |
|
2804 } |
|
2805 |
|
2806 ComplexMatrix |
|
2807 operator * (const ComplexColumnVector& a, const RowVector& b) |
|
2808 { |
|
2809 ComplexRowVector tmp (b); |
|
2810 return a * tmp; |
|
2811 } |
|
2812 |
|
2813 ComplexMatrix |
|
2814 operator * (const ComplexColumnVector& v, const ComplexRowVector& a) |
|
2815 { |
1948
|
2816 ComplexMatrix retval; |
|
2817 |
5275
|
2818 octave_idx_type len = v.length (); |
3233
|
2819 |
|
2820 if (len != 0) |
1205
|
2821 { |
5275
|
2822 octave_idx_type a_len = a.length (); |
3233
|
2823 |
|
2824 retval.resize (len, a_len); |
|
2825 Complex *c = retval.fortran_vec (); |
|
2826 |
4552
|
2827 F77_XFCN (zgemm, ZGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
2828 F77_CONST_CHAR_ARG2 ("N", 1), |
|
2829 len, a_len, 1, 1.0, v.data (), len, |
|
2830 a.data (), 1, 0.0, c, len |
|
2831 F77_CHAR_ARG_LEN (1) |
|
2832 F77_CHAR_ARG_LEN (1))); |
3233
|
2833 |
|
2834 if (f77_exception_encountered) |
|
2835 (*current_liboctave_error_handler) |
|
2836 ("unrecoverable error in zgemm"); |
1205
|
2837 } |
|
2838 |
1948
|
2839 return retval; |
1205
|
2840 } |
|
2841 |
458
|
2842 // matrix by diagonal matrix -> matrix operations |
|
2843 |
|
2844 ComplexMatrix& |
|
2845 ComplexMatrix::operator += (const DiagMatrix& a) |
|
2846 { |
5275
|
2847 octave_idx_type nr = rows (); |
|
2848 octave_idx_type nc = cols (); |
|
2849 |
|
2850 octave_idx_type a_nr = rows (); |
|
2851 octave_idx_type a_nc = cols (); |
2384
|
2852 |
|
2853 if (nr != a_nr || nc != a_nc) |
458
|
2854 { |
2384
|
2855 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889
|
2856 return *this; |
458
|
2857 } |
|
2858 |
5275
|
2859 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
2860 elem (i, i) += a.elem (i, i); |
|
2861 |
|
2862 return *this; |
|
2863 } |
|
2864 |
|
2865 ComplexMatrix& |
|
2866 ComplexMatrix::operator -= (const DiagMatrix& a) |
|
2867 { |
5275
|
2868 octave_idx_type nr = rows (); |
|
2869 octave_idx_type nc = cols (); |
|
2870 |
|
2871 octave_idx_type a_nr = rows (); |
|
2872 octave_idx_type a_nc = cols (); |
2384
|
2873 |
|
2874 if (nr != a_nr || nc != a_nc) |
458
|
2875 { |
2384
|
2876 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889
|
2877 return *this; |
458
|
2878 } |
|
2879 |
5275
|
2880 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
2881 elem (i, i) -= a.elem (i, i); |
|
2882 |
|
2883 return *this; |
|
2884 } |
|
2885 |
|
2886 ComplexMatrix& |
|
2887 ComplexMatrix::operator += (const ComplexDiagMatrix& a) |
|
2888 { |
5275
|
2889 octave_idx_type nr = rows (); |
|
2890 octave_idx_type nc = cols (); |
|
2891 |
|
2892 octave_idx_type a_nr = rows (); |
|
2893 octave_idx_type a_nc = cols (); |
2384
|
2894 |
|
2895 if (nr != a_nr || nc != a_nc) |
458
|
2896 { |
2384
|
2897 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889
|
2898 return *this; |
458
|
2899 } |
|
2900 |
5275
|
2901 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
2902 elem (i, i) += a.elem (i, i); |
|
2903 |
|
2904 return *this; |
|
2905 } |
|
2906 |
|
2907 ComplexMatrix& |
|
2908 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) |
|
2909 { |
5275
|
2910 octave_idx_type nr = rows (); |
|
2911 octave_idx_type nc = cols (); |
|
2912 |
|
2913 octave_idx_type a_nr = rows (); |
|
2914 octave_idx_type a_nc = cols (); |
2384
|
2915 |
|
2916 if (nr != a_nr || nc != a_nc) |
458
|
2917 { |
2384
|
2918 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889
|
2919 return *this; |
458
|
2920 } |
|
2921 |
5275
|
2922 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
2923 elem (i, i) -= a.elem (i, i); |
|
2924 |
|
2925 return *this; |
|
2926 } |
|
2927 |
|
2928 // matrix by matrix -> matrix operations |
|
2929 |
|
2930 ComplexMatrix& |
|
2931 ComplexMatrix::operator += (const Matrix& a) |
|
2932 { |
5275
|
2933 octave_idx_type nr = rows (); |
|
2934 octave_idx_type nc = cols (); |
|
2935 |
|
2936 octave_idx_type a_nr = a.rows (); |
|
2937 octave_idx_type a_nc = a.cols (); |
2384
|
2938 |
|
2939 if (nr != a_nr || nc != a_nc) |
458
|
2940 { |
2384
|
2941 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
2942 return *this; |
|
2943 } |
|
2944 |
|
2945 if (nr == 0 || nc == 0) |
|
2946 return *this; |
|
2947 |
|
2948 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2949 |
3769
|
2950 mx_inline_add2 (d, a.data (), length ()); |
458
|
2951 return *this; |
|
2952 } |
|
2953 |
|
2954 ComplexMatrix& |
|
2955 ComplexMatrix::operator -= (const Matrix& a) |
|
2956 { |
5275
|
2957 octave_idx_type nr = rows (); |
|
2958 octave_idx_type nc = cols (); |
|
2959 |
|
2960 octave_idx_type a_nr = a.rows (); |
|
2961 octave_idx_type a_nc = a.cols (); |
2384
|
2962 |
|
2963 if (nr != a_nr || nc != a_nc) |
458
|
2964 { |
2384
|
2965 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
2966 return *this; |
|
2967 } |
|
2968 |
|
2969 if (nr == 0 || nc == 0) |
|
2970 return *this; |
|
2971 |
|
2972 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2973 |
3769
|
2974 mx_inline_subtract2 (d, a.data (), length ()); |
458
|
2975 return *this; |
|
2976 } |
|
2977 |
|
2978 // unary operations |
|
2979 |
2964
|
2980 boolMatrix |
458
|
2981 ComplexMatrix::operator ! (void) const |
|
2982 { |
5275
|
2983 octave_idx_type nr = rows (); |
|
2984 octave_idx_type nc = cols (); |
2964
|
2985 |
|
2986 boolMatrix b (nr, nc); |
|
2987 |
5275
|
2988 for (octave_idx_type j = 0; j < nc; j++) |
|
2989 for (octave_idx_type i = 0; i < nr; i++) |
5139
|
2990 b.elem (i, j) = elem (i, j) == 0.0; |
2964
|
2991 |
|
2992 return b; |
458
|
2993 } |
|
2994 |
|
2995 // other operations |
|
2996 |
|
2997 ComplexMatrix |
2676
|
2998 ComplexMatrix::map (c_c_Mapper f) const |
458
|
2999 { |
2676
|
3000 ComplexMatrix b (*this); |
|
3001 return b.apply (f); |
458
|
3002 } |
|
3003 |
2676
|
3004 Matrix |
|
3005 ComplexMatrix::map (d_c_Mapper f) const |
458
|
3006 { |
5275
|
3007 octave_idx_type nr = rows (); |
|
3008 octave_idx_type nc = cols (); |
3248
|
3009 |
|
3010 Matrix retval (nr, nc); |
|
3011 |
5275
|
3012 for (octave_idx_type j = 0; j < nc; j++) |
|
3013 for (octave_idx_type i = 0; i < nr; i++) |
3248
|
3014 retval(i,j) = f (elem(i,j)); |
|
3015 |
|
3016 return retval; |
|
3017 } |
|
3018 |
|
3019 boolMatrix |
|
3020 ComplexMatrix::map (b_c_Mapper f) const |
|
3021 { |
5275
|
3022 octave_idx_type nr = rows (); |
|
3023 octave_idx_type nc = cols (); |
3248
|
3024 |
|
3025 boolMatrix retval (nr, nc); |
|
3026 |
5275
|
3027 for (octave_idx_type j = 0; j < nc; j++) |
|
3028 for (octave_idx_type i = 0; i < nr; i++) |
3248
|
3029 retval(i,j) = f (elem(i,j)); |
2676
|
3030 |
|
3031 return retval; |
|
3032 } |
|
3033 |
|
3034 ComplexMatrix& |
|
3035 ComplexMatrix::apply (c_c_Mapper f) |
|
3036 { |
|
3037 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
3038 |
5275
|
3039 for (octave_idx_type i = 0; i < length (); i++) |
2676
|
3040 d[i] = f (d[i]); |
|
3041 |
|
3042 return *this; |
458
|
3043 } |
|
3044 |
2384
|
3045 bool |
|
3046 ComplexMatrix::any_element_is_inf_or_nan (void) const |
|
3047 { |
5275
|
3048 octave_idx_type nr = rows (); |
|
3049 octave_idx_type nc = cols (); |
|
3050 |
|
3051 for (octave_idx_type j = 0; j < nc; j++) |
|
3052 for (octave_idx_type i = 0; i < nr; i++) |
2384
|
3053 { |
|
3054 Complex val = elem (i, j); |
|
3055 if (xisinf (val) || xisnan (val)) |
|
3056 return true; |
|
3057 } |
|
3058 |
|
3059 return false; |
|
3060 } |
|
3061 |
2408
|
3062 // Return true if no elements have imaginary components. |
|
3063 |
|
3064 bool |
|
3065 ComplexMatrix::all_elements_are_real (void) const |
|
3066 { |
5275
|
3067 octave_idx_type nr = rows (); |
|
3068 octave_idx_type nc = cols (); |
|
3069 |
|
3070 for (octave_idx_type j = 0; j < nc; j++) |
4349
|
3071 { |
5275
|
3072 for (octave_idx_type i = 0; i < nr; i++) |
4349
|
3073 { |
5315
|
3074 double ip = std::imag (elem (i, j)); |
4349
|
3075 |
|
3076 if (ip != 0.0 || lo_ieee_signbit (ip)) |
|
3077 return false; |
|
3078 } |
|
3079 } |
2408
|
3080 |
|
3081 return true; |
|
3082 } |
|
3083 |
1968
|
3084 // Return nonzero if any element of CM has a non-integer real or |
|
3085 // imaginary part. Also extract the largest and smallest (real or |
|
3086 // imaginary) values and return them in MAX_VAL and MIN_VAL. |
|
3087 |
2384
|
3088 bool |
1968
|
3089 ComplexMatrix::all_integers (double& max_val, double& min_val) const |
|
3090 { |
5275
|
3091 octave_idx_type nr = rows (); |
|
3092 octave_idx_type nc = cols (); |
1968
|
3093 |
|
3094 if (nr > 0 && nc > 0) |
|
3095 { |
|
3096 Complex val = elem (0, 0); |
|
3097 |
5315
|
3098 double r_val = std::real (val); |
|
3099 double i_val = std::imag (val); |
1968
|
3100 |
|
3101 max_val = r_val; |
|
3102 min_val = r_val; |
|
3103 |
|
3104 if (i_val > max_val) |
|
3105 max_val = i_val; |
|
3106 |
|
3107 if (i_val < max_val) |
|
3108 min_val = i_val; |
|
3109 } |
|
3110 else |
2384
|
3111 return false; |
1968
|
3112 |
5275
|
3113 for (octave_idx_type j = 0; j < nc; j++) |
|
3114 for (octave_idx_type i = 0; i < nr; i++) |
1968
|
3115 { |
|
3116 Complex val = elem (i, j); |
|
3117 |
5315
|
3118 double r_val = std::real (val); |
|
3119 double i_val = std::imag (val); |
1968
|
3120 |
|
3121 if (r_val > max_val) |
|
3122 max_val = r_val; |
|
3123 |
|
3124 if (i_val > max_val) |
|
3125 max_val = i_val; |
|
3126 |
|
3127 if (r_val < min_val) |
|
3128 min_val = r_val; |
|
3129 |
|
3130 if (i_val < min_val) |
|
3131 min_val = i_val; |
|
3132 |
|
3133 if (D_NINT (r_val) != r_val || D_NINT (i_val) != i_val) |
2384
|
3134 return false; |
1968
|
3135 } |
2384
|
3136 |
|
3137 return true; |
1968
|
3138 } |
|
3139 |
2384
|
3140 bool |
1968
|
3141 ComplexMatrix::too_large_for_float (void) const |
|
3142 { |
5275
|
3143 octave_idx_type nr = rows (); |
|
3144 octave_idx_type nc = cols (); |
|
3145 |
|
3146 for (octave_idx_type j = 0; j < nc; j++) |
|
3147 for (octave_idx_type i = 0; i < nr; i++) |
1968
|
3148 { |
|
3149 Complex val = elem (i, j); |
|
3150 |
5315
|
3151 double r_val = std::real (val); |
|
3152 double i_val = std::imag (val); |
1968
|
3153 |
5389
|
3154 if ((! (xisnan (r_val) || xisinf (r_val)) |
5387
|
3155 && fabs (r_val) > FLT_MAX) |
5389
|
3156 || (! (xisnan (i_val) || xisinf (i_val)) |
5387
|
3157 && fabs (i_val) > FLT_MAX)) |
2384
|
3158 return true; |
1968
|
3159 } |
|
3160 |
2384
|
3161 return false; |
1968
|
3162 } |
|
3163 |
5775
|
3164 // FIXME Do these really belong here? Maybe they should be |
4015
|
3165 // in a base class? |
|
3166 |
2832
|
3167 boolMatrix |
4015
|
3168 ComplexMatrix::all (int dim) const |
458
|
3169 { |
4015
|
3170 MX_ALL_OP (dim); |
458
|
3171 } |
|
3172 |
2832
|
3173 boolMatrix |
4015
|
3174 ComplexMatrix::any (int dim) const |
458
|
3175 { |
4015
|
3176 MX_ANY_OP (dim); |
458
|
3177 } |
|
3178 |
|
3179 ComplexMatrix |
3723
|
3180 ComplexMatrix::cumprod (int dim) const |
458
|
3181 { |
4015
|
3182 MX_CUMULATIVE_OP (ComplexMatrix, Complex, *=); |
458
|
3183 } |
|
3184 |
|
3185 ComplexMatrix |
3723
|
3186 ComplexMatrix::cumsum (int dim) const |
458
|
3187 { |
4015
|
3188 MX_CUMULATIVE_OP (ComplexMatrix, Complex, +=); |
458
|
3189 } |
|
3190 |
|
3191 ComplexMatrix |
3723
|
3192 ComplexMatrix::prod (int dim) const |
458
|
3193 { |
3864
|
3194 MX_REDUCTION_OP (ComplexMatrix, *=, 1.0, 1.0); |
458
|
3195 } |
|
3196 |
|
3197 ComplexMatrix |
3723
|
3198 ComplexMatrix::sum (int dim) const |
458
|
3199 { |
3864
|
3200 MX_REDUCTION_OP (ComplexMatrix, +=, 0.0, 0.0); |
458
|
3201 } |
|
3202 |
|
3203 ComplexMatrix |
3723
|
3204 ComplexMatrix::sumsq (int dim) const |
458
|
3205 { |
3864
|
3206 #define ROW_EXPR \ |
|
3207 Complex d = elem (i, j); \ |
|
3208 retval.elem (i, 0) += d * conj (d) |
|
3209 |
|
3210 #define COL_EXPR \ |
|
3211 Complex d = elem (i, j); \ |
|
3212 retval.elem (0, j) += d * conj (d) |
|
3213 |
|
3214 MX_BASE_REDUCTION_OP (ComplexMatrix, ROW_EXPR, COL_EXPR, 0.0, 0.0); |
|
3215 |
|
3216 #undef ROW_EXPR |
|
3217 #undef COL_EXPR |
458
|
3218 } |
|
3219 |
4329
|
3220 Matrix ComplexMatrix::abs (void) const |
|
3221 { |
5275
|
3222 octave_idx_type nr = rows (); |
|
3223 octave_idx_type nc = cols (); |
4329
|
3224 |
|
3225 Matrix retval (nr, nc); |
|
3226 |
5275
|
3227 for (octave_idx_type j = 0; j < nc; j++) |
|
3228 for (octave_idx_type i = 0; i < nr; i++) |
5315
|
3229 retval (i, j) = std::abs (elem (i, j)); |
4329
|
3230 |
|
3231 return retval; |
|
3232 } |
|
3233 |
458
|
3234 ComplexColumnVector |
|
3235 ComplexMatrix::diag (void) const |
|
3236 { |
|
3237 return diag (0); |
|
3238 } |
|
3239 |
|
3240 ComplexColumnVector |
5275
|
3241 ComplexMatrix::diag (octave_idx_type k) const |
458
|
3242 { |
5275
|
3243 octave_idx_type nnr = rows (); |
|
3244 octave_idx_type nnc = cols (); |
458
|
3245 if (k > 0) |
|
3246 nnc -= k; |
|
3247 else if (k < 0) |
|
3248 nnr += k; |
|
3249 |
|
3250 ComplexColumnVector d; |
|
3251 |
|
3252 if (nnr > 0 && nnc > 0) |
|
3253 { |
5275
|
3254 octave_idx_type ndiag = (nnr < nnc) ? nnr : nnc; |
458
|
3255 |
|
3256 d.resize (ndiag); |
|
3257 |
|
3258 if (k > 0) |
|
3259 { |
5275
|
3260 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
3261 d.elem (i) = elem (i, i+k); |
|
3262 } |
4509
|
3263 else if (k < 0) |
458
|
3264 { |
5275
|
3265 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
3266 d.elem (i) = elem (i-k, i); |
|
3267 } |
|
3268 else |
|
3269 { |
5275
|
3270 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
3271 d.elem (i) = elem (i, i); |
|
3272 } |
|
3273 } |
|
3274 else |
4513
|
3275 (*current_liboctave_error_handler) |
|
3276 ("diag: requested diagonal out of range"); |
458
|
3277 |
|
3278 return d; |
|
3279 } |
|
3280 |
2354
|
3281 bool |
5275
|
3282 ComplexMatrix::row_is_real_only (octave_idx_type i) const |
2354
|
3283 { |
|
3284 bool retval = true; |
|
3285 |
5275
|
3286 octave_idx_type nc = columns (); |
|
3287 |
|
3288 for (octave_idx_type j = 0; j < nc; j++) |
2354
|
3289 { |
5315
|
3290 if (std::imag (elem (i, j)) != 0.0) |
2354
|
3291 { |
|
3292 retval = false; |
|
3293 break; |
|
3294 } |
|
3295 } |
|
3296 |
|
3297 return retval; |
|
3298 } |
|
3299 |
|
3300 bool |
5275
|
3301 ComplexMatrix::column_is_real_only (octave_idx_type j) const |
2354
|
3302 { |
|
3303 bool retval = true; |
|
3304 |
5275
|
3305 octave_idx_type nr = rows (); |
|
3306 |
|
3307 for (octave_idx_type i = 0; i < nr; i++) |
2354
|
3308 { |
5315
|
3309 if (std::imag (elem (i, j)) != 0.0) |
2354
|
3310 { |
|
3311 retval = false; |
|
3312 break; |
|
3313 } |
|
3314 } |
|
3315 |
|
3316 return retval; |
|
3317 } |
891
|
3318 |
458
|
3319 ComplexColumnVector |
|
3320 ComplexMatrix::row_min (void) const |
|
3321 { |
5275
|
3322 Array<octave_idx_type> dummy_idx; |
4587
|
3323 return row_min (dummy_idx); |
458
|
3324 } |
|
3325 |
|
3326 ComplexColumnVector |
5275
|
3327 ComplexMatrix::row_min (Array<octave_idx_type>& idx_arg) const |
458
|
3328 { |
|
3329 ComplexColumnVector result; |
|
3330 |
5275
|
3331 octave_idx_type nr = rows (); |
|
3332 octave_idx_type nc = cols (); |
458
|
3333 |
|
3334 if (nr > 0 && nc > 0) |
|
3335 { |
|
3336 result.resize (nr); |
4587
|
3337 idx_arg.resize (nr); |
458
|
3338 |
5275
|
3339 for (octave_idx_type i = 0; i < nr; i++) |
458
|
3340 { |
2354
|
3341 bool real_only = row_is_real_only (i); |
|
3342 |
5275
|
3343 octave_idx_type idx_j; |
4469
|
3344 |
|
3345 Complex tmp_min; |
|
3346 |
|
3347 double abs_min = octave_NaN; |
|
3348 |
|
3349 for (idx_j = 0; idx_j < nc; idx_j++) |
|
3350 { |
|
3351 tmp_min = elem (i, idx_j); |
|
3352 |
5389
|
3353 if (! xisnan (tmp_min)) |
4469
|
3354 { |
5315
|
3355 abs_min = real_only ? std::real (tmp_min) : std::abs (tmp_min); |
4469
|
3356 break; |
|
3357 } |
|
3358 } |
|
3359 |
5275
|
3360 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469
|
3361 { |
|
3362 Complex tmp = elem (i, j); |
|
3363 |
5389
|
3364 if (xisnan (tmp)) |
4469
|
3365 continue; |
|
3366 |
5315
|
3367 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469
|
3368 |
|
3369 if (abs_tmp < abs_min) |
|
3370 { |
|
3371 idx_j = j; |
|
3372 tmp_min = tmp; |
|
3373 abs_min = abs_tmp; |
|
3374 } |
|
3375 } |
|
3376 |
5389
|
3377 if (xisnan (tmp_min)) |
4469
|
3378 { |
|
3379 result.elem (i) = Complex_NaN_result; |
4587
|
3380 idx_arg.elem (i) = 0; |
4469
|
3381 } |
891
|
3382 else |
|
3383 { |
4469
|
3384 result.elem (i) = tmp_min; |
4587
|
3385 idx_arg.elem (i) = idx_j; |
891
|
3386 } |
458
|
3387 } |
|
3388 } |
|
3389 |
|
3390 return result; |
|
3391 } |
|
3392 |
|
3393 ComplexColumnVector |
|
3394 ComplexMatrix::row_max (void) const |
|
3395 { |
5275
|
3396 Array<octave_idx_type> dummy_idx; |
4587
|
3397 return row_max (dummy_idx); |
458
|
3398 } |
|
3399 |
|
3400 ComplexColumnVector |
5275
|
3401 ComplexMatrix::row_max (Array<octave_idx_type>& idx_arg) const |
458
|
3402 { |
|
3403 ComplexColumnVector result; |
|
3404 |
5275
|
3405 octave_idx_type nr = rows (); |
|
3406 octave_idx_type nc = cols (); |
458
|
3407 |
|
3408 if (nr > 0 && nc > 0) |
|
3409 { |
|
3410 result.resize (nr); |
4587
|
3411 idx_arg.resize (nr); |
458
|
3412 |
5275
|
3413 for (octave_idx_type i = 0; i < nr; i++) |
458
|
3414 { |
2354
|
3415 bool real_only = row_is_real_only (i); |
|
3416 |
5275
|
3417 octave_idx_type idx_j; |
4469
|
3418 |
|
3419 Complex tmp_max; |
|
3420 |
|
3421 double abs_max = octave_NaN; |
|
3422 |
|
3423 for (idx_j = 0; idx_j < nc; idx_j++) |
|
3424 { |
|
3425 tmp_max = elem (i, idx_j); |
|
3426 |
5389
|
3427 if (! xisnan (tmp_max)) |
4469
|
3428 { |
5315
|
3429 abs_max = real_only ? std::real (tmp_max) : std::abs (tmp_max); |
4469
|
3430 break; |
|
3431 } |
|
3432 } |
|
3433 |
5275
|
3434 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469
|
3435 { |
|
3436 Complex tmp = elem (i, j); |
|
3437 |
5389
|
3438 if (xisnan (tmp)) |
4469
|
3439 continue; |
|
3440 |
5315
|
3441 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469
|
3442 |
|
3443 if (abs_tmp > abs_max) |
|
3444 { |
|
3445 idx_j = j; |
|
3446 tmp_max = tmp; |
|
3447 abs_max = abs_tmp; |
|
3448 } |
|
3449 } |
|
3450 |
5389
|
3451 if (xisnan (tmp_max)) |
4469
|
3452 { |
|
3453 result.elem (i) = Complex_NaN_result; |
4587
|
3454 idx_arg.elem (i) = 0; |
4469
|
3455 } |
891
|
3456 else |
|
3457 { |
4469
|
3458 result.elem (i) = tmp_max; |
4587
|
3459 idx_arg.elem (i) = idx_j; |
891
|
3460 } |
458
|
3461 } |
|
3462 } |
|
3463 |
|
3464 return result; |
|
3465 } |
|
3466 |
|
3467 ComplexRowVector |
|
3468 ComplexMatrix::column_min (void) const |
|
3469 { |
5275
|
3470 Array<octave_idx_type> dummy_idx; |
4587
|
3471 return column_min (dummy_idx); |
458
|
3472 } |
|
3473 |
|
3474 ComplexRowVector |
5275
|
3475 ComplexMatrix::column_min (Array<octave_idx_type>& idx_arg) const |
458
|
3476 { |
|
3477 ComplexRowVector result; |
|
3478 |
5275
|
3479 octave_idx_type nr = rows (); |
|
3480 octave_idx_type nc = cols (); |
458
|
3481 |
|
3482 if (nr > 0 && nc > 0) |
|
3483 { |
|
3484 result.resize (nc); |
4587
|
3485 idx_arg.resize (nc); |
458
|
3486 |
5275
|
3487 for (octave_idx_type j = 0; j < nc; j++) |
458
|
3488 { |
2354
|
3489 bool real_only = column_is_real_only (j); |
|
3490 |
5275
|
3491 octave_idx_type idx_i; |
4469
|
3492 |
|
3493 Complex tmp_min; |
|
3494 |
|
3495 double abs_min = octave_NaN; |
|
3496 |
|
3497 for (idx_i = 0; idx_i < nr; idx_i++) |
|
3498 { |
|
3499 tmp_min = elem (idx_i, j); |
|
3500 |
5389
|
3501 if (! xisnan (tmp_min)) |
4469
|
3502 { |
5315
|
3503 abs_min = real_only ? std::real (tmp_min) : std::abs (tmp_min); |
4469
|
3504 break; |
|
3505 } |
|
3506 } |
|
3507 |
5275
|
3508 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469
|
3509 { |
|
3510 Complex tmp = elem (i, j); |
|
3511 |
5389
|
3512 if (xisnan (tmp)) |
4469
|
3513 continue; |
|
3514 |
5315
|
3515 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469
|
3516 |
|
3517 if (abs_tmp < abs_min) |
|
3518 { |
|
3519 idx_i = i; |
|
3520 tmp_min = tmp; |
|
3521 abs_min = abs_tmp; |
|
3522 } |
|
3523 } |
|
3524 |
5389
|
3525 if (xisnan (tmp_min)) |
4469
|
3526 { |
|
3527 result.elem (j) = Complex_NaN_result; |
4587
|
3528 idx_arg.elem (j) = 0; |
4469
|
3529 } |
891
|
3530 else |
|
3531 { |
4469
|
3532 result.elem (j) = tmp_min; |
4587
|
3533 idx_arg.elem (j) = idx_i; |
891
|
3534 } |
458
|
3535 } |
|
3536 } |
|
3537 |
|
3538 return result; |
|
3539 } |
|
3540 |
|
3541 ComplexRowVector |
|
3542 ComplexMatrix::column_max (void) const |
|
3543 { |
5275
|
3544 Array<octave_idx_type> dummy_idx; |
4587
|
3545 return column_max (dummy_idx); |
458
|
3546 } |
|
3547 |
|
3548 ComplexRowVector |
5275
|
3549 ComplexMatrix::column_max (Array<octave_idx_type>& idx_arg) const |
458
|
3550 { |
|
3551 ComplexRowVector result; |
|
3552 |
5275
|
3553 octave_idx_type nr = rows (); |
|
3554 octave_idx_type nc = cols (); |
458
|
3555 |
|
3556 if (nr > 0 && nc > 0) |
|
3557 { |
|
3558 result.resize (nc); |
4587
|
3559 idx_arg.resize (nc); |
458
|
3560 |
5275
|
3561 for (octave_idx_type j = 0; j < nc; j++) |
458
|
3562 { |
2354
|
3563 bool real_only = column_is_real_only (j); |
|
3564 |
5275
|
3565 octave_idx_type idx_i; |
4469
|
3566 |
|
3567 Complex tmp_max; |
|
3568 |
|
3569 double abs_max = octave_NaN; |
|
3570 |
|
3571 for (idx_i = 0; idx_i < nr; idx_i++) |
|
3572 { |
|
3573 tmp_max = elem (idx_i, j); |
|
3574 |
5389
|
3575 if (! xisnan (tmp_max)) |
4469
|
3576 { |
5315
|
3577 abs_max = real_only ? std::real (tmp_max) : std::abs (tmp_max); |
4469
|
3578 break; |
|
3579 } |
|
3580 } |
|
3581 |
5275
|
3582 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469
|
3583 { |
|
3584 Complex tmp = elem (i, j); |
|
3585 |
5389
|
3586 if (xisnan (tmp)) |
4469
|
3587 continue; |
|
3588 |
5315
|
3589 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469
|
3590 |
|
3591 if (abs_tmp > abs_max) |
|
3592 { |
|
3593 idx_i = i; |
|
3594 tmp_max = tmp; |
|
3595 abs_max = abs_tmp; |
|
3596 } |
|
3597 } |
|
3598 |
5389
|
3599 if (xisnan (tmp_max)) |
4469
|
3600 { |
|
3601 result.elem (j) = Complex_NaN_result; |
4587
|
3602 idx_arg.elem (j) = 0; |
4469
|
3603 } |
891
|
3604 else |
|
3605 { |
4469
|
3606 result.elem (j) = tmp_max; |
4587
|
3607 idx_arg.elem (j) = idx_i; |
891
|
3608 } |
458
|
3609 } |
|
3610 } |
|
3611 |
|
3612 return result; |
|
3613 } |
|
3614 |
|
3615 // i/o |
|
3616 |
3504
|
3617 std::ostream& |
|
3618 operator << (std::ostream& os, const ComplexMatrix& a) |
458
|
3619 { |
5275
|
3620 for (octave_idx_type i = 0; i < a.rows (); i++) |
458
|
3621 { |
5275
|
3622 for (octave_idx_type j = 0; j < a.cols (); j++) |
4130
|
3623 { |
|
3624 os << " "; |
|
3625 octave_write_complex (os, a.elem (i, j)); |
|
3626 } |
458
|
3627 os << "\n"; |
|
3628 } |
|
3629 return os; |
|
3630 } |
|
3631 |
3504
|
3632 std::istream& |
|
3633 operator >> (std::istream& is, ComplexMatrix& a) |
458
|
3634 { |
5275
|
3635 octave_idx_type nr = a.rows (); |
|
3636 octave_idx_type nc = a.cols (); |
458
|
3637 |
|
3638 if (nr < 1 || nc < 1) |
3504
|
3639 is.clear (std::ios::badbit); |
458
|
3640 else |
|
3641 { |
|
3642 Complex tmp; |
5275
|
3643 for (octave_idx_type i = 0; i < nr; i++) |
|
3644 for (octave_idx_type j = 0; j < nc; j++) |
458
|
3645 { |
4130
|
3646 tmp = octave_read_complex (is); |
458
|
3647 if (is) |
|
3648 a.elem (i, j) = tmp; |
|
3649 else |
2993
|
3650 goto done; |
458
|
3651 } |
|
3652 } |
|
3653 |
2993
|
3654 done: |
|
3655 |
458
|
3656 return is; |
|
3657 } |
|
3658 |
1819
|
3659 ComplexMatrix |
|
3660 Givens (const Complex& x, const Complex& y) |
|
3661 { |
|
3662 double cc; |
|
3663 Complex cs, temp_r; |
|
3664 |
3887
|
3665 F77_FUNC (zlartg, ZLARTG) (x, y, cc, cs, temp_r); |
1819
|
3666 |
|
3667 ComplexMatrix g (2, 2); |
|
3668 |
|
3669 g.elem (0, 0) = cc; |
|
3670 g.elem (1, 1) = cc; |
|
3671 g.elem (0, 1) = cs; |
|
3672 g.elem (1, 0) = -conj (cs); |
|
3673 |
|
3674 return g; |
|
3675 } |
|
3676 |
|
3677 ComplexMatrix |
|
3678 Sylvester (const ComplexMatrix& a, const ComplexMatrix& b, |
|
3679 const ComplexMatrix& c) |
|
3680 { |
|
3681 ComplexMatrix retval; |
|
3682 |
5775
|
3683 // FIXME -- need to check that a, b, and c are all the same |
1819
|
3684 // size. |
|
3685 |
|
3686 // Compute Schur decompositions |
|
3687 |
|
3688 ComplexSCHUR as (a, "U"); |
|
3689 ComplexSCHUR bs (b, "U"); |
|
3690 |
|
3691 // Transform c to new coordinates. |
|
3692 |
|
3693 ComplexMatrix ua = as.unitary_matrix (); |
|
3694 ComplexMatrix sch_a = as.schur_matrix (); |
|
3695 |
|
3696 ComplexMatrix ub = bs.unitary_matrix (); |
|
3697 ComplexMatrix sch_b = bs.schur_matrix (); |
|
3698 |
|
3699 ComplexMatrix cx = ua.hermitian () * c * ub; |
|
3700 |
|
3701 // Solve the sylvester equation, back-transform, and return the |
|
3702 // solution. |
|
3703 |
5275
|
3704 octave_idx_type a_nr = a.rows (); |
|
3705 octave_idx_type b_nr = b.rows (); |
1819
|
3706 |
|
3707 double scale; |
5275
|
3708 octave_idx_type info; |
1950
|
3709 |
|
3710 Complex *pa = sch_a.fortran_vec (); |
|
3711 Complex *pb = sch_b.fortran_vec (); |
|
3712 Complex *px = cx.fortran_vec (); |
1819
|
3713 |
4552
|
3714 F77_XFCN (ztrsyl, ZTRSYL, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3715 F77_CONST_CHAR_ARG2 ("N", 1), |
|
3716 1, a_nr, b_nr, pa, a_nr, pb, |
|
3717 b_nr, px, a_nr, scale, info |
|
3718 F77_CHAR_ARG_LEN (1) |
|
3719 F77_CHAR_ARG_LEN (1))); |
1950
|
3720 |
|
3721 if (f77_exception_encountered) |
|
3722 (*current_liboctave_error_handler) ("unrecoverable error in ztrsyl"); |
|
3723 else |
|
3724 { |
5775
|
3725 // FIXME -- check info? |
1950
|
3726 |
|
3727 retval = -ua * cx * ub.hermitian (); |
|
3728 } |
1819
|
3729 |
|
3730 return retval; |
|
3731 } |
|
3732 |
2828
|
3733 ComplexMatrix |
|
3734 operator * (const ComplexMatrix& m, const Matrix& a) |
|
3735 { |
|
3736 ComplexMatrix tmp (a); |
|
3737 return m * tmp; |
|
3738 } |
|
3739 |
|
3740 ComplexMatrix |
|
3741 operator * (const Matrix& m, const ComplexMatrix& a) |
|
3742 { |
|
3743 ComplexMatrix tmp (m); |
|
3744 return tmp * a; |
|
3745 } |
|
3746 |
6162
|
3747 /* Simple Dot Product, Matrix-Vector and Matrix-Matrix Unit tests |
|
3748 %!assert([1+i 2+i 3+i] * [ 4+i ; 5+i ; 6+i], 29+21i, 1e-14) |
|
3749 %!assert([1+i 2+i ; 3+i 4+i ] * [5+i ; 6+i], [15 + 14i ; 37 + 18i], 1e-14) |
|
3750 %!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) |
|
3751 */ |
|
3752 |
|
3753 /* Test some simple identities |
|
3754 %!shared M, cv, rv |
|
3755 %! M = randn(10,10)+i*rand(10,10); |
|
3756 %! cv = randn(10,1)+i*rand(10,1); |
|
3757 %! rv = randn(1,10)+i*rand(1,10); |
|
3758 %!assert([M*cv,M*cv],M*[cv,cv],1e-14) |
|
3759 %!assert([rv*M;rv*M],[rv;rv]*M,1e-14) |
|
3760 %!assert(2*rv*cv,[rv,rv]*[cv;cv],1e-14) |
|
3761 */ |
|
3762 |
2828
|
3763 ComplexMatrix |
|
3764 operator * (const ComplexMatrix& m, const ComplexMatrix& a) |
|
3765 { |
|
3766 ComplexMatrix retval; |
|
3767 |
5275
|
3768 octave_idx_type nr = m.rows (); |
|
3769 octave_idx_type nc = m.cols (); |
|
3770 |
|
3771 octave_idx_type a_nr = a.rows (); |
|
3772 octave_idx_type a_nc = a.cols (); |
2828
|
3773 |
|
3774 if (nc != a_nr) |
|
3775 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
|
3776 else |
|
3777 { |
|
3778 if (nr == 0 || nc == 0 || a_nc == 0) |
3760
|
3779 retval.resize (nr, a_nc, 0.0); |
2828
|
3780 else |
|
3781 { |
5275
|
3782 octave_idx_type ld = nr; |
|
3783 octave_idx_type lda = a.rows (); |
2828
|
3784 |
|
3785 retval.resize (nr, a_nc); |
|
3786 Complex *c = retval.fortran_vec (); |
|
3787 |
5983
|
3788 if (a_nc == 1) |
|
3789 { |
|
3790 if (nr == 1) |
|
3791 F77_FUNC (xzdotu, XZDOTU) (nc, m.data (), 1, a.data (), 1, *c); |
|
3792 else |
6390
|
3793 { |
|
3794 F77_XFCN (zgemv, ZGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3795 nr, nc, 1.0, m.data (), ld, |
|
3796 a.data (), 1, 0.0, c, 1 |
|
3797 F77_CHAR_ARG_LEN (1))); |
|
3798 |
|
3799 if (f77_exception_encountered) |
|
3800 (*current_liboctave_error_handler) |
|
3801 ("unrecoverable error in zgemv"); |
|
3802 } |
5983
|
3803 } |
|
3804 else |
6390
|
3805 { |
|
3806 F77_XFCN (zgemm, ZGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3807 F77_CONST_CHAR_ARG2 ("N", 1), |
|
3808 nr, a_nc, nc, 1.0, m.data (), |
|
3809 ld, a.data (), lda, 0.0, c, nr |
|
3810 F77_CHAR_ARG_LEN (1) |
|
3811 F77_CHAR_ARG_LEN (1))); |
|
3812 |
|
3813 if (f77_exception_encountered) |
|
3814 (*current_liboctave_error_handler) |
|
3815 ("unrecoverable error in zgemm"); |
|
3816 } |
2828
|
3817 } |
|
3818 } |
|
3819 |
|
3820 return retval; |
|
3821 } |
|
3822 |
5775
|
3823 // FIXME -- it would be nice to share code among the min/max |
4309
|
3824 // functions below. |
|
3825 |
|
3826 #define EMPTY_RETURN_CHECK(T) \ |
|
3827 if (nr == 0 || nc == 0) \ |
|
3828 return T (nr, nc); |
|
3829 |
|
3830 ComplexMatrix |
|
3831 min (const Complex& c, const ComplexMatrix& m) |
|
3832 { |
5275
|
3833 octave_idx_type nr = m.rows (); |
|
3834 octave_idx_type nc = m.columns (); |
4309
|
3835 |
|
3836 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3837 |
|
3838 ComplexMatrix result (nr, nc); |
|
3839 |
5275
|
3840 for (octave_idx_type j = 0; j < nc; j++) |
|
3841 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3842 { |
|
3843 OCTAVE_QUIT; |
|
3844 result (i, j) = xmin (c, m (i, j)); |
|
3845 } |
|
3846 |
|
3847 return result; |
|
3848 } |
|
3849 |
|
3850 ComplexMatrix |
|
3851 min (const ComplexMatrix& m, const Complex& c) |
|
3852 { |
5275
|
3853 octave_idx_type nr = m.rows (); |
|
3854 octave_idx_type nc = m.columns (); |
4309
|
3855 |
|
3856 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3857 |
|
3858 ComplexMatrix result (nr, nc); |
|
3859 |
5275
|
3860 for (octave_idx_type j = 0; j < nc; j++) |
|
3861 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3862 { |
|
3863 OCTAVE_QUIT; |
|
3864 result (i, j) = xmin (m (i, j), c); |
|
3865 } |
|
3866 |
|
3867 return result; |
|
3868 } |
|
3869 |
|
3870 ComplexMatrix |
|
3871 min (const ComplexMatrix& a, const ComplexMatrix& b) |
|
3872 { |
5275
|
3873 octave_idx_type nr = a.rows (); |
|
3874 octave_idx_type nc = a.columns (); |
4309
|
3875 |
|
3876 if (nr != b.rows () || nc != b.columns ()) |
|
3877 { |
|
3878 (*current_liboctave_error_handler) |
|
3879 ("two-arg min expecting args of same size"); |
|
3880 return ComplexMatrix (); |
|
3881 } |
|
3882 |
|
3883 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3884 |
|
3885 ComplexMatrix result (nr, nc); |
|
3886 |
5275
|
3887 for (octave_idx_type j = 0; j < nc; j++) |
4309
|
3888 { |
|
3889 int columns_are_real_only = 1; |
5275
|
3890 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3891 { |
|
3892 OCTAVE_QUIT; |
5315
|
3893 if (std::imag (a (i, j)) != 0.0 || std::imag (b (i, j)) != 0.0) |
4309
|
3894 { |
|
3895 columns_are_real_only = 0; |
|
3896 break; |
|
3897 } |
|
3898 } |
|
3899 |
|
3900 if (columns_are_real_only) |
|
3901 { |
5275
|
3902 for (octave_idx_type i = 0; i < nr; i++) |
5315
|
3903 result (i, j) = xmin (std::real (a (i, j)), std::real (b (i, j))); |
4309
|
3904 } |
|
3905 else |
|
3906 { |
5275
|
3907 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3908 { |
|
3909 OCTAVE_QUIT; |
|
3910 result (i, j) = xmin (a (i, j), b (i, j)); |
|
3911 } |
|
3912 } |
|
3913 } |
|
3914 |
|
3915 return result; |
|
3916 } |
|
3917 |
|
3918 ComplexMatrix |
|
3919 max (const Complex& c, const ComplexMatrix& m) |
|
3920 { |
5275
|
3921 octave_idx_type nr = m.rows (); |
|
3922 octave_idx_type nc = m.columns (); |
4309
|
3923 |
|
3924 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3925 |
|
3926 ComplexMatrix result (nr, nc); |
|
3927 |
5275
|
3928 for (octave_idx_type j = 0; j < nc; j++) |
|
3929 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3930 { |
|
3931 OCTAVE_QUIT; |
|
3932 result (i, j) = xmax (c, m (i, j)); |
|
3933 } |
|
3934 |
|
3935 return result; |
|
3936 } |
|
3937 |
|
3938 ComplexMatrix |
|
3939 max (const ComplexMatrix& m, const Complex& c) |
|
3940 { |
5275
|
3941 octave_idx_type nr = m.rows (); |
|
3942 octave_idx_type nc = m.columns (); |
4309
|
3943 |
|
3944 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3945 |
|
3946 ComplexMatrix result (nr, nc); |
|
3947 |
5275
|
3948 for (octave_idx_type j = 0; j < nc; j++) |
|
3949 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3950 { |
|
3951 OCTAVE_QUIT; |
|
3952 result (i, j) = xmax (m (i, j), c); |
|
3953 } |
|
3954 |
|
3955 return result; |
|
3956 } |
|
3957 |
|
3958 ComplexMatrix |
|
3959 max (const ComplexMatrix& a, const ComplexMatrix& b) |
|
3960 { |
5275
|
3961 octave_idx_type nr = a.rows (); |
|
3962 octave_idx_type nc = a.columns (); |
4309
|
3963 |
|
3964 if (nr != b.rows () || nc != b.columns ()) |
|
3965 { |
|
3966 (*current_liboctave_error_handler) |
|
3967 ("two-arg max expecting args of same size"); |
|
3968 return ComplexMatrix (); |
|
3969 } |
|
3970 |
|
3971 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3972 |
|
3973 ComplexMatrix result (nr, nc); |
|
3974 |
5275
|
3975 for (octave_idx_type j = 0; j < nc; j++) |
4309
|
3976 { |
|
3977 int columns_are_real_only = 1; |
5275
|
3978 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3979 { |
|
3980 OCTAVE_QUIT; |
5315
|
3981 if (std::imag (a (i, j)) != 0.0 || std::imag (b (i, j)) != 0.0) |
4309
|
3982 { |
|
3983 columns_are_real_only = 0; |
|
3984 break; |
|
3985 } |
|
3986 } |
|
3987 |
|
3988 if (columns_are_real_only) |
|
3989 { |
5275
|
3990 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3991 { |
|
3992 OCTAVE_QUIT; |
5315
|
3993 result (i, j) = xmax (std::real (a (i, j)), std::real (b (i, j))); |
4309
|
3994 } |
|
3995 } |
|
3996 else |
|
3997 { |
5275
|
3998 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3999 { |
|
4000 OCTAVE_QUIT; |
|
4001 result (i, j) = xmax (a (i, j), b (i, j)); |
|
4002 } |
|
4003 } |
|
4004 } |
|
4005 |
|
4006 return result; |
|
4007 } |
|
4008 |
5315
|
4009 MS_CMP_OPS(ComplexMatrix, std::real, Complex, std::real) |
3504
|
4010 MS_BOOL_OPS(ComplexMatrix, Complex, 0.0) |
2870
|
4011 |
5315
|
4012 SM_CMP_OPS(Complex, std::real, ComplexMatrix, std::real) |
3504
|
4013 SM_BOOL_OPS(Complex, ComplexMatrix, 0.0) |
2870
|
4014 |
5315
|
4015 MM_CMP_OPS(ComplexMatrix, std::real, ComplexMatrix, std::real) |
3504
|
4016 MM_BOOL_OPS(ComplexMatrix, ComplexMatrix, 0.0) |
2870
|
4017 |
458
|
4018 /* |
|
4019 ;;; Local Variables: *** |
|
4020 ;;; mode: C++ *** |
|
4021 ;;; End: *** |
|
4022 */ |