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