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); |
7079
|
2494 |
|
2495 // FIXME: Can SMLSIZ be other than 25? |
|
2496 octave_idx_type smlsiz = 25; |
|
2497 |
|
2498 // We compute the size of rwork and iwork because ZGELSD in |
|
2499 // older versions of LAPACK does not return them on a query |
|
2500 // call. |
7124
|
2501 double dminmn = static_cast<double> (minmn); |
|
2502 double dsmlsizp1 = static_cast<double> (smlsiz+1); |
7079
|
2503 #if defined (HAVE_LOG2) |
7124
|
2504 double tmp = log2 (dminmn) / dsmlsizp1 + 1; |
7079
|
2505 #else |
7124
|
2506 double tmp = log (dminmn) / dsmlsizp1 / log (2.0) + 1; |
7079
|
2507 #endif |
|
2508 octave_idx_type nlvl = static_cast<int> (tmp); |
|
2509 if (nlvl < 0) |
|
2510 nlvl = 0; |
|
2511 |
|
2512 octave_idx_type lrwork = minmn*(10 + 2*smlsiz + 8*nlvl) |
|
2513 + 3*smlsiz*nrhs + (smlsiz+1)*(smlsiz+1); |
|
2514 if (lrwork < 1) |
|
2515 lrwork = 1; |
|
2516 Array<double> rwork (lrwork); |
|
2517 double *prwork = rwork.fortran_vec (); |
|
2518 |
|
2519 octave_idx_type liwork = 3 * minmn * nlvl + 11 * minmn; |
|
2520 if (liwork < 1) |
|
2521 liwork = 1; |
|
2522 Array<octave_idx_type> iwork (liwork); |
|
2523 octave_idx_type* piwork = iwork.fortran_vec (); |
7072
|
2524 |
|
2525 F77_XFCN (zgelsd, ZGELSD, (m, n, nrhs, tmp_data, m, pretval, maxmn, |
|
2526 ps, rcond, rank, work.fortran_vec (), |
7079
|
2527 lwork, prwork, piwork, info)); |
1948
|
2528 |
|
2529 if (f77_exception_encountered) |
7072
|
2530 (*current_liboctave_error_handler) |
|
2531 ("unrecoverable error in zgelsd"); |
1948
|
2532 else |
|
2533 { |
5315
|
2534 lwork = static_cast<octave_idx_type> (std::real (work(0))); |
3752
|
2535 work.resize (lwork); |
7072
|
2536 |
|
2537 F77_XFCN (zgelsd, ZGELSD, (m, n, nrhs, tmp_data, m, pretval, |
|
2538 maxmn, ps, rcond, rank, |
|
2539 work.fortran_vec (), lwork, |
7079
|
2540 prwork, piwork, info)); |
3752
|
2541 |
|
2542 if (f77_exception_encountered) |
7072
|
2543 (*current_liboctave_error_handler) |
|
2544 ("unrecoverable error in zgelsd"); |
7076
|
2545 else |
|
2546 { |
|
2547 if (rank < minmn) |
|
2548 (*current_liboctave_warning_handler) |
|
2549 ("zgelsd: rank deficient %dx%d matrix, rank = %d, tol = %e", |
|
2550 m, n, rank, rcond); |
|
2551 |
|
2552 if (s.elem (0) == 0.0) |
|
2553 rcond = 0.0; |
|
2554 else |
|
2555 rcond = s.elem (minmn - 1) / s.elem (0); |
7079
|
2556 |
|
2557 retval.resize (n, nrhs); |
7076
|
2558 } |
1948
|
2559 } |
458
|
2560 } |
|
2561 |
|
2562 return retval; |
|
2563 } |
|
2564 |
|
2565 ComplexColumnVector |
3585
|
2566 ComplexMatrix::lssolve (const ColumnVector& b) const |
|
2567 { |
5275
|
2568 octave_idx_type info; |
|
2569 octave_idx_type rank; |
7076
|
2570 double rcond; |
|
2571 return lssolve (ComplexColumnVector (b), info, rank, rcond); |
3585
|
2572 } |
|
2573 |
|
2574 ComplexColumnVector |
5275
|
2575 ComplexMatrix::lssolve (const ColumnVector& b, octave_idx_type& info) const |
3585
|
2576 { |
5275
|
2577 octave_idx_type rank; |
7076
|
2578 double rcond; |
|
2579 return lssolve (ComplexColumnVector (b), info, rank, rcond); |
3585
|
2580 } |
|
2581 |
|
2582 ComplexColumnVector |
7076
|
2583 ComplexMatrix::lssolve (const ColumnVector& b, octave_idx_type& info, |
|
2584 octave_idx_type& rank) const |
3585
|
2585 { |
7076
|
2586 double rcond; |
|
2587 return lssolve (ComplexColumnVector (b), info, rank, rcond); |
|
2588 } |
|
2589 |
|
2590 ComplexColumnVector |
|
2591 ComplexMatrix::lssolve (const ColumnVector& b, octave_idx_type& info, |
|
2592 octave_idx_type& rank, double& rcond) const |
|
2593 { |
|
2594 return lssolve (ComplexColumnVector (b), info, rank, rcond); |
3585
|
2595 } |
|
2596 |
|
2597 ComplexColumnVector |
458
|
2598 ComplexMatrix::lssolve (const ComplexColumnVector& b) const |
|
2599 { |
5275
|
2600 octave_idx_type info; |
|
2601 octave_idx_type rank; |
7076
|
2602 double rcond; |
|
2603 return lssolve (b, info, rank, rcond); |
458
|
2604 } |
|
2605 |
|
2606 ComplexColumnVector |
5275
|
2607 ComplexMatrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info) const |
458
|
2608 { |
5275
|
2609 octave_idx_type rank; |
7076
|
2610 double rcond; |
|
2611 return lssolve (b, info, rank, rcond); |
458
|
2612 } |
|
2613 |
|
2614 ComplexColumnVector |
5275
|
2615 ComplexMatrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info, |
|
2616 octave_idx_type& rank) const |
458
|
2617 { |
7076
|
2618 double rcond; |
|
2619 return lssolve (b, info, rank, rcond); |
|
2620 |
|
2621 } |
|
2622 |
|
2623 ComplexColumnVector |
|
2624 ComplexMatrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info, |
|
2625 octave_idx_type& rank, double& rcond) const |
|
2626 { |
1948
|
2627 ComplexColumnVector retval; |
|
2628 |
5275
|
2629 octave_idx_type nrhs = 1; |
|
2630 |
|
2631 octave_idx_type m = rows (); |
|
2632 octave_idx_type n = cols (); |
458
|
2633 |
6924
|
2634 if (m != b.length ()) |
1948
|
2635 (*current_liboctave_error_handler) |
6924
|
2636 ("matrix dimension mismatch solution of linear equations"); |
|
2637 else if (m == 0 || n == 0 || b.cols () == 0) |
|
2638 retval = ComplexColumnVector (n, Complex (0.0, 0.0)); |
1948
|
2639 else |
458
|
2640 { |
7072
|
2641 volatile octave_idx_type minmn = (m < n ? m : n); |
|
2642 octave_idx_type maxmn = m > n ? m : n; |
7076
|
2643 rcond = -1.0; |
7072
|
2644 |
|
2645 if (m != n) |
|
2646 { |
|
2647 retval = ComplexColumnVector (maxmn); |
|
2648 |
|
2649 for (octave_idx_type i = 0; i < m; i++) |
|
2650 retval.elem (i) = b.elem (i); |
|
2651 } |
|
2652 else |
|
2653 retval = b; |
|
2654 |
1948
|
2655 ComplexMatrix atmp = *this; |
|
2656 Complex *tmp_data = atmp.fortran_vec (); |
|
2657 |
7072
|
2658 Complex *pretval = retval.fortran_vec (); |
|
2659 Array<double> s (minmn); |
7071
|
2660 double *ps = s.fortran_vec (); |
1948
|
2661 |
7072
|
2662 // Ask ZGELSD what the dimension of WORK should be. |
5275
|
2663 octave_idx_type lwork = -1; |
3752
|
2664 |
|
2665 Array<Complex> work (1); |
7079
|
2666 |
|
2667 // FIXME: Can SMLSIZ be other than 25? |
|
2668 octave_idx_type smlsiz = 25; |
|
2669 |
|
2670 // We compute the size of rwork and iwork because ZGELSD in |
|
2671 // older versions of LAPACK does not return them on a query |
|
2672 // call. |
7124
|
2673 double dminmn = static_cast<double> (minmn); |
|
2674 double dsmlsizp1 = static_cast<double> (smlsiz+1); |
7079
|
2675 #if defined (HAVE_LOG2) |
7124
|
2676 double tmp = log2 (dminmn) / dsmlsizp1 + 1; |
7079
|
2677 #else |
7124
|
2678 double tmp = log (dminmn) / dsmlsizp1 / log (2.0) + 1; |
7079
|
2679 #endif |
|
2680 octave_idx_type nlvl = static_cast<int> (tmp); |
|
2681 if (nlvl < 0) |
|
2682 nlvl = 0; |
|
2683 |
|
2684 octave_idx_type lrwork = minmn*(10 + 2*smlsiz + 8*nlvl) |
|
2685 + 3*smlsiz*nrhs + (smlsiz+1)*(smlsiz+1); |
|
2686 if (lrwork < 1) |
|
2687 lrwork = 1; |
|
2688 Array<double> rwork (lrwork); |
|
2689 double *prwork = rwork.fortran_vec (); |
|
2690 |
|
2691 octave_idx_type liwork = 3 * minmn * nlvl + 11 * minmn; |
|
2692 if (liwork < 1) |
|
2693 liwork = 1; |
|
2694 Array<octave_idx_type> iwork (liwork); |
|
2695 octave_idx_type* piwork = iwork.fortran_vec (); |
7072
|
2696 |
|
2697 F77_XFCN (zgelsd, ZGELSD, (m, n, nrhs, tmp_data, m, pretval, maxmn, |
|
2698 ps, rcond, rank, work.fortran_vec (), |
7079
|
2699 lwork, prwork, piwork, info)); |
1948
|
2700 |
|
2701 if (f77_exception_encountered) |
7072
|
2702 (*current_liboctave_error_handler) |
|
2703 ("unrecoverable error in zgelsd"); |
1948
|
2704 else |
|
2705 { |
7072
|
2706 lwork = static_cast<octave_idx_type> (std::real (work(0))); |
3752
|
2707 work.resize (lwork); |
7072
|
2708 rwork.resize (static_cast<octave_idx_type> (rwork(0))); |
|
2709 iwork.resize (iwork(0)); |
|
2710 |
|
2711 F77_XFCN (zgelsd, ZGELSD, (m, n, nrhs, tmp_data, m, pretval, |
|
2712 maxmn, ps, rcond, rank, |
|
2713 work.fortran_vec (), lwork, |
7079
|
2714 prwork, piwork, info)); |
3752
|
2715 |
|
2716 if (f77_exception_encountered) |
7072
|
2717 (*current_liboctave_error_handler) |
|
2718 ("unrecoverable error in zgelsd"); |
|
2719 else if (rank < minmn) |
7076
|
2720 { |
|
2721 if (rank < minmn) |
|
2722 (*current_liboctave_warning_handler) |
|
2723 ("zgelsd: rank deficient %dx%d matrix, rank = %d, tol = %e", |
|
2724 m, n, rank, rcond); |
|
2725 |
|
2726 if (s.elem (0) == 0.0) |
|
2727 rcond = 0.0; |
|
2728 else |
|
2729 rcond = s.elem (minmn - 1) / s.elem (0); |
7079
|
2730 |
|
2731 retval.resize (n, nrhs); |
7076
|
2732 } |
1948
|
2733 } |
458
|
2734 } |
|
2735 |
|
2736 return retval; |
|
2737 } |
|
2738 |
1819
|
2739 // Constants for matrix exponential calculation. |
|
2740 |
|
2741 static double padec [] = |
|
2742 { |
|
2743 5.0000000000000000e-1, |
|
2744 1.1666666666666667e-1, |
|
2745 1.6666666666666667e-2, |
|
2746 1.6025641025641026e-3, |
|
2747 1.0683760683760684e-4, |
|
2748 4.8562548562548563e-6, |
|
2749 1.3875013875013875e-7, |
|
2750 1.9270852604185938e-9, |
|
2751 }; |
|
2752 |
|
2753 ComplexMatrix |
|
2754 ComplexMatrix::expm (void) const |
|
2755 { |
|
2756 ComplexMatrix retval; |
|
2757 |
|
2758 ComplexMatrix m = *this; |
|
2759 |
5275
|
2760 octave_idx_type nc = columns (); |
1819
|
2761 |
3130
|
2762 // Preconditioning step 1: trace normalization to reduce dynamic |
|
2763 // range of poles, but avoid making stable eigenvalues unstable. |
|
2764 |
1819
|
2765 // trace shift value |
|
2766 Complex trshift = 0.0; |
|
2767 |
5275
|
2768 for (octave_idx_type i = 0; i < nc; i++) |
1819
|
2769 trshift += m.elem (i, i); |
|
2770 |
|
2771 trshift /= nc; |
|
2772 |
3130
|
2773 if (trshift.real () < 0.0) |
6958
|
2774 { |
|
2775 trshift = trshift.imag (); |
|
2776 if (trshift.real () > 709.0) |
|
2777 trshift = 709.0; |
|
2778 } |
3130
|
2779 |
5275
|
2780 for (octave_idx_type i = 0; i < nc; i++) |
1819
|
2781 m.elem (i, i) -= trshift; |
|
2782 |
|
2783 // Preconditioning step 2: eigenvalue balancing. |
3331
|
2784 // code follows development in AEPBAL |
|
2785 |
|
2786 Complex *mp = m.fortran_vec (); |
3467
|
2787 |
5275
|
2788 octave_idx_type info, ilo, ihi,ilos,ihis; |
3468
|
2789 Array<double> dpermute (nc); |
|
2790 Array<double> dscale (nc); |
|
2791 |
5775
|
2792 // FIXME -- should pass job as a parameter in expm |
3468
|
2793 |
|
2794 // Permute first |
|
2795 char job = 'P'; |
4552
|
2796 F77_XFCN (zgebal, ZGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
2797 nc, mp, nc, ilo, ihi, |
|
2798 dpermute.fortran_vec (), info |
|
2799 F77_CHAR_ARG_LEN (1))); |
3331
|
2800 |
|
2801 if (f77_exception_encountered) |
|
2802 { |
|
2803 (*current_liboctave_error_handler) ("unrecoverable error in zgebal"); |
|
2804 return retval; |
|
2805 } |
|
2806 |
3468
|
2807 // then scale |
|
2808 job = 'S'; |
4552
|
2809 F77_XFCN (zgebal, ZGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
2810 nc, mp, nc, ilos, ihis, |
|
2811 dscale.fortran_vec (), info |
|
2812 F77_CHAR_ARG_LEN (1))); |
3331
|
2813 |
|
2814 if (f77_exception_encountered) |
|
2815 { |
3467
|
2816 (*current_liboctave_error_handler) ("unrecoverable error in zgebal"); |
3331
|
2817 return retval; |
|
2818 } |
1819
|
2819 |
|
2820 // Preconditioning step 3: scaling. |
|
2821 |
|
2822 ColumnVector work (nc); |
3130
|
2823 double inf_norm; |
|
2824 |
4552
|
2825 F77_XFCN (xzlange, XZLANGE, (F77_CONST_CHAR_ARG2 ("I", 1), |
|
2826 nc, nc, m.fortran_vec (), nc, |
|
2827 work.fortran_vec (), inf_norm |
|
2828 F77_CHAR_ARG_LEN (1))); |
3331
|
2829 |
|
2830 if (f77_exception_encountered) |
|
2831 { |
|
2832 (*current_liboctave_error_handler) ("unrecoverable error in zlange"); |
|
2833 return retval; |
|
2834 } |
1819
|
2835 |
2800
|
2836 int sqpow = (inf_norm > 0.0 |
|
2837 ? static_cast<int> (1.0 + log (inf_norm) / log (2.0)) : 0); |
1819
|
2838 |
|
2839 // Check whether we need to square at all. |
|
2840 |
|
2841 if (sqpow < 0) |
|
2842 sqpow = 0; |
|
2843 |
|
2844 if (sqpow > 0) |
|
2845 { |
|
2846 double scale_factor = 1.0; |
5275
|
2847 for (octave_idx_type i = 0; i < sqpow; i++) |
1819
|
2848 scale_factor *= 2.0; |
|
2849 |
|
2850 m = m / scale_factor; |
|
2851 } |
|
2852 |
|
2853 // npp, dpp: pade' approx polynomial matrices. |
|
2854 |
|
2855 ComplexMatrix npp (nc, nc, 0.0); |
6958
|
2856 Complex *pnpp = npp.fortran_vec (); |
1819
|
2857 ComplexMatrix dpp = npp; |
6958
|
2858 Complex *pdpp = dpp.fortran_vec (); |
1819
|
2859 |
|
2860 // Now powers a^8 ... a^1. |
|
2861 |
|
2862 int minus_one_j = -1; |
5275
|
2863 for (octave_idx_type j = 7; j >= 0; j--) |
1819
|
2864 { |
6958
|
2865 for (octave_idx_type i = 0; i < nc; i++) |
|
2866 { |
|
2867 octave_idx_type k = i * nc + i; |
7265
|
2868 pnpp[k] += padec[j]; |
|
2869 pdpp[k] += minus_one_j * padec[j]; |
6958
|
2870 } |
7265
|
2871 |
6958
|
2872 npp = m * npp; |
7265
|
2873 pnpp = npp.fortran_vec (); |
|
2874 |
6958
|
2875 dpp = m * dpp; |
7265
|
2876 pdpp = dpp.fortran_vec (); |
|
2877 |
1819
|
2878 minus_one_j *= -1; |
|
2879 } |
|
2880 |
|
2881 // Zero power. |
|
2882 |
|
2883 dpp = -dpp; |
5275
|
2884 for (octave_idx_type j = 0; j < nc; j++) |
1819
|
2885 { |
|
2886 npp.elem (j, j) += 1.0; |
|
2887 dpp.elem (j, j) += 1.0; |
|
2888 } |
|
2889 |
|
2890 // Compute pade approximation = inverse (dpp) * npp. |
|
2891 |
|
2892 retval = dpp.solve (npp); |
|
2893 |
|
2894 // Reverse preconditioning step 3: repeated squaring. |
|
2895 |
|
2896 while (sqpow) |
|
2897 { |
|
2898 retval = retval * retval; |
|
2899 sqpow--; |
|
2900 } |
|
2901 |
|
2902 // Reverse preconditioning step 2: inverse balancing. |
3467
|
2903 // Done in two steps: inverse scaling, then inverse permutation |
|
2904 |
|
2905 // inverse scaling (diagonal transformation) |
5275
|
2906 for (octave_idx_type i = 0; i < nc; i++) |
|
2907 for (octave_idx_type j = 0; j < nc; j++) |
3468
|
2908 retval(i,j) *= dscale(i) / dscale(j); |
3467
|
2909 |
4153
|
2910 OCTAVE_QUIT; |
|
2911 |
3467
|
2912 // construct balancing permutation vector |
6867
|
2913 Array<octave_idx_type> iperm (nc); |
5275
|
2914 for (octave_idx_type i = 0; i < nc; i++) |
4593
|
2915 iperm(i) = i; // initialize to identity permutation |
3467
|
2916 |
|
2917 // leading permutations in forward order |
5275
|
2918 for (octave_idx_type i = 0; i < (ilo-1); i++) |
3468
|
2919 { |
6867
|
2920 octave_idx_type swapidx = static_cast<octave_idx_type> (dpermute(i)) - 1; |
5275
|
2921 octave_idx_type tmp = iperm(i); |
4593
|
2922 iperm(i) = iperm(swapidx); |
|
2923 iperm(swapidx) = tmp; |
3468
|
2924 } |
3467
|
2925 |
|
2926 // trailing permutations must be done in reverse order |
5275
|
2927 for (octave_idx_type i = nc - 1; i >= ihi; i--) |
3468
|
2928 { |
6867
|
2929 octave_idx_type swapidx = static_cast<octave_idx_type> (dpermute(i)) - 1; |
5275
|
2930 octave_idx_type tmp = iperm(i); |
4593
|
2931 iperm(i) = iperm(swapidx); |
|
2932 iperm(swapidx) = tmp; |
3468
|
2933 } |
3467
|
2934 |
|
2935 // construct inverse balancing permutation vector |
6867
|
2936 Array<octave_idx_type> invpvec (nc); |
5275
|
2937 for (octave_idx_type i = 0; i < nc; i++) |
4593
|
2938 invpvec(iperm(i)) = i; // Thanks to R. A. Lippert for this method |
3467
|
2939 |
4153
|
2940 OCTAVE_QUIT; |
|
2941 |
3467
|
2942 ComplexMatrix tmpMat = retval; |
5275
|
2943 for (octave_idx_type i = 0; i < nc; i++) |
|
2944 for (octave_idx_type j = 0; j < nc; j++) |
3468
|
2945 retval(i,j) = tmpMat(invpvec(i),invpvec(j)); |
1819
|
2946 |
|
2947 // Reverse preconditioning step 1: fix trace normalization. |
|
2948 |
3130
|
2949 return exp (trshift) * retval; |
1819
|
2950 } |
|
2951 |
1205
|
2952 // column vector by row vector -> matrix operations |
|
2953 |
|
2954 ComplexMatrix |
|
2955 operator * (const ColumnVector& v, const ComplexRowVector& a) |
|
2956 { |
|
2957 ComplexColumnVector tmp (v); |
|
2958 return tmp * a; |
|
2959 } |
|
2960 |
|
2961 ComplexMatrix |
|
2962 operator * (const ComplexColumnVector& a, const RowVector& b) |
|
2963 { |
|
2964 ComplexRowVector tmp (b); |
|
2965 return a * tmp; |
|
2966 } |
|
2967 |
|
2968 ComplexMatrix |
|
2969 operator * (const ComplexColumnVector& v, const ComplexRowVector& a) |
|
2970 { |
1948
|
2971 ComplexMatrix retval; |
|
2972 |
5275
|
2973 octave_idx_type len = v.length (); |
3233
|
2974 |
|
2975 if (len != 0) |
1205
|
2976 { |
5275
|
2977 octave_idx_type a_len = a.length (); |
3233
|
2978 |
|
2979 retval.resize (len, a_len); |
|
2980 Complex *c = retval.fortran_vec (); |
|
2981 |
4552
|
2982 F77_XFCN (zgemm, ZGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
2983 F77_CONST_CHAR_ARG2 ("N", 1), |
|
2984 len, a_len, 1, 1.0, v.data (), len, |
|
2985 a.data (), 1, 0.0, c, len |
|
2986 F77_CHAR_ARG_LEN (1) |
|
2987 F77_CHAR_ARG_LEN (1))); |
3233
|
2988 |
|
2989 if (f77_exception_encountered) |
|
2990 (*current_liboctave_error_handler) |
|
2991 ("unrecoverable error in zgemm"); |
1205
|
2992 } |
|
2993 |
1948
|
2994 return retval; |
1205
|
2995 } |
|
2996 |
458
|
2997 // matrix by diagonal matrix -> matrix operations |
|
2998 |
|
2999 ComplexMatrix& |
|
3000 ComplexMatrix::operator += (const DiagMatrix& a) |
|
3001 { |
5275
|
3002 octave_idx_type nr = rows (); |
|
3003 octave_idx_type nc = cols (); |
|
3004 |
|
3005 octave_idx_type a_nr = rows (); |
|
3006 octave_idx_type a_nc = cols (); |
2384
|
3007 |
|
3008 if (nr != a_nr || nc != a_nc) |
458
|
3009 { |
2384
|
3010 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889
|
3011 return *this; |
458
|
3012 } |
|
3013 |
5275
|
3014 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
3015 elem (i, i) += a.elem (i, i); |
|
3016 |
|
3017 return *this; |
|
3018 } |
|
3019 |
|
3020 ComplexMatrix& |
|
3021 ComplexMatrix::operator -= (const DiagMatrix& a) |
|
3022 { |
5275
|
3023 octave_idx_type nr = rows (); |
|
3024 octave_idx_type nc = cols (); |
|
3025 |
|
3026 octave_idx_type a_nr = rows (); |
|
3027 octave_idx_type a_nc = cols (); |
2384
|
3028 |
|
3029 if (nr != a_nr || nc != a_nc) |
458
|
3030 { |
2384
|
3031 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889
|
3032 return *this; |
458
|
3033 } |
|
3034 |
5275
|
3035 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
3036 elem (i, i) -= a.elem (i, i); |
|
3037 |
|
3038 return *this; |
|
3039 } |
|
3040 |
|
3041 ComplexMatrix& |
|
3042 ComplexMatrix::operator += (const ComplexDiagMatrix& a) |
|
3043 { |
5275
|
3044 octave_idx_type nr = rows (); |
|
3045 octave_idx_type nc = cols (); |
|
3046 |
|
3047 octave_idx_type a_nr = rows (); |
|
3048 octave_idx_type a_nc = cols (); |
2384
|
3049 |
|
3050 if (nr != a_nr || nc != a_nc) |
458
|
3051 { |
2384
|
3052 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889
|
3053 return *this; |
458
|
3054 } |
|
3055 |
5275
|
3056 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
3057 elem (i, i) += a.elem (i, i); |
|
3058 |
|
3059 return *this; |
|
3060 } |
|
3061 |
|
3062 ComplexMatrix& |
|
3063 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) |
|
3064 { |
5275
|
3065 octave_idx_type nr = rows (); |
|
3066 octave_idx_type nc = cols (); |
|
3067 |
|
3068 octave_idx_type a_nr = rows (); |
|
3069 octave_idx_type a_nc = cols (); |
2384
|
3070 |
|
3071 if (nr != a_nr || nc != a_nc) |
458
|
3072 { |
2384
|
3073 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889
|
3074 return *this; |
458
|
3075 } |
|
3076 |
5275
|
3077 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
3078 elem (i, i) -= a.elem (i, i); |
|
3079 |
|
3080 return *this; |
|
3081 } |
|
3082 |
|
3083 // matrix by matrix -> matrix operations |
|
3084 |
|
3085 ComplexMatrix& |
|
3086 ComplexMatrix::operator += (const Matrix& a) |
|
3087 { |
5275
|
3088 octave_idx_type nr = rows (); |
|
3089 octave_idx_type nc = cols (); |
|
3090 |
|
3091 octave_idx_type a_nr = a.rows (); |
|
3092 octave_idx_type a_nc = a.cols (); |
2384
|
3093 |
|
3094 if (nr != a_nr || nc != a_nc) |
458
|
3095 { |
2384
|
3096 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
3097 return *this; |
|
3098 } |
|
3099 |
|
3100 if (nr == 0 || nc == 0) |
|
3101 return *this; |
|
3102 |
|
3103 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
3104 |
3769
|
3105 mx_inline_add2 (d, a.data (), length ()); |
458
|
3106 return *this; |
|
3107 } |
|
3108 |
|
3109 ComplexMatrix& |
|
3110 ComplexMatrix::operator -= (const Matrix& a) |
|
3111 { |
5275
|
3112 octave_idx_type nr = rows (); |
|
3113 octave_idx_type nc = cols (); |
|
3114 |
|
3115 octave_idx_type a_nr = a.rows (); |
|
3116 octave_idx_type a_nc = a.cols (); |
2384
|
3117 |
|
3118 if (nr != a_nr || nc != a_nc) |
458
|
3119 { |
2384
|
3120 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
3121 return *this; |
|
3122 } |
|
3123 |
|
3124 if (nr == 0 || nc == 0) |
|
3125 return *this; |
|
3126 |
|
3127 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
3128 |
3769
|
3129 mx_inline_subtract2 (d, a.data (), length ()); |
458
|
3130 return *this; |
|
3131 } |
|
3132 |
|
3133 // unary operations |
|
3134 |
2964
|
3135 boolMatrix |
458
|
3136 ComplexMatrix::operator ! (void) const |
|
3137 { |
5275
|
3138 octave_idx_type nr = rows (); |
|
3139 octave_idx_type nc = cols (); |
2964
|
3140 |
|
3141 boolMatrix b (nr, nc); |
|
3142 |
5275
|
3143 for (octave_idx_type j = 0; j < nc; j++) |
|
3144 for (octave_idx_type i = 0; i < nr; i++) |
5139
|
3145 b.elem (i, j) = elem (i, j) == 0.0; |
2964
|
3146 |
|
3147 return b; |
458
|
3148 } |
|
3149 |
|
3150 // other operations |
|
3151 |
|
3152 ComplexMatrix |
2676
|
3153 ComplexMatrix::map (c_c_Mapper f) const |
458
|
3154 { |
2676
|
3155 ComplexMatrix b (*this); |
|
3156 return b.apply (f); |
458
|
3157 } |
|
3158 |
2676
|
3159 Matrix |
|
3160 ComplexMatrix::map (d_c_Mapper f) const |
458
|
3161 { |
5275
|
3162 octave_idx_type nr = rows (); |
|
3163 octave_idx_type nc = cols (); |
3248
|
3164 |
|
3165 Matrix retval (nr, nc); |
|
3166 |
5275
|
3167 for (octave_idx_type j = 0; j < nc; j++) |
|
3168 for (octave_idx_type i = 0; i < nr; i++) |
3248
|
3169 retval(i,j) = f (elem(i,j)); |
|
3170 |
|
3171 return retval; |
|
3172 } |
|
3173 |
|
3174 boolMatrix |
|
3175 ComplexMatrix::map (b_c_Mapper f) const |
|
3176 { |
5275
|
3177 octave_idx_type nr = rows (); |
|
3178 octave_idx_type nc = cols (); |
3248
|
3179 |
|
3180 boolMatrix retval (nr, nc); |
|
3181 |
5275
|
3182 for (octave_idx_type j = 0; j < nc; j++) |
|
3183 for (octave_idx_type i = 0; i < nr; i++) |
3248
|
3184 retval(i,j) = f (elem(i,j)); |
2676
|
3185 |
|
3186 return retval; |
|
3187 } |
|
3188 |
|
3189 ComplexMatrix& |
|
3190 ComplexMatrix::apply (c_c_Mapper f) |
|
3191 { |
|
3192 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
3193 |
5275
|
3194 for (octave_idx_type i = 0; i < length (); i++) |
2676
|
3195 d[i] = f (d[i]); |
|
3196 |
|
3197 return *this; |
458
|
3198 } |
|
3199 |
2384
|
3200 bool |
|
3201 ComplexMatrix::any_element_is_inf_or_nan (void) const |
|
3202 { |
5275
|
3203 octave_idx_type nr = rows (); |
|
3204 octave_idx_type nc = cols (); |
|
3205 |
|
3206 for (octave_idx_type j = 0; j < nc; j++) |
|
3207 for (octave_idx_type i = 0; i < nr; i++) |
2384
|
3208 { |
|
3209 Complex val = elem (i, j); |
|
3210 if (xisinf (val) || xisnan (val)) |
|
3211 return true; |
|
3212 } |
|
3213 |
|
3214 return false; |
|
3215 } |
|
3216 |
2408
|
3217 // Return true if no elements have imaginary components. |
|
3218 |
|
3219 bool |
|
3220 ComplexMatrix::all_elements_are_real (void) const |
|
3221 { |
5275
|
3222 octave_idx_type nr = rows (); |
|
3223 octave_idx_type nc = cols (); |
|
3224 |
|
3225 for (octave_idx_type j = 0; j < nc; j++) |
4349
|
3226 { |
5275
|
3227 for (octave_idx_type i = 0; i < nr; i++) |
4349
|
3228 { |
5315
|
3229 double ip = std::imag (elem (i, j)); |
4349
|
3230 |
|
3231 if (ip != 0.0 || lo_ieee_signbit (ip)) |
|
3232 return false; |
|
3233 } |
|
3234 } |
2408
|
3235 |
|
3236 return true; |
|
3237 } |
|
3238 |
1968
|
3239 // Return nonzero if any element of CM has a non-integer real or |
|
3240 // imaginary part. Also extract the largest and smallest (real or |
|
3241 // imaginary) values and return them in MAX_VAL and MIN_VAL. |
|
3242 |
2384
|
3243 bool |
1968
|
3244 ComplexMatrix::all_integers (double& max_val, double& min_val) const |
|
3245 { |
5275
|
3246 octave_idx_type nr = rows (); |
|
3247 octave_idx_type nc = cols (); |
1968
|
3248 |
|
3249 if (nr > 0 && nc > 0) |
|
3250 { |
|
3251 Complex val = elem (0, 0); |
|
3252 |
5315
|
3253 double r_val = std::real (val); |
|
3254 double i_val = std::imag (val); |
1968
|
3255 |
|
3256 max_val = r_val; |
|
3257 min_val = r_val; |
|
3258 |
|
3259 if (i_val > max_val) |
|
3260 max_val = i_val; |
|
3261 |
|
3262 if (i_val < max_val) |
|
3263 min_val = i_val; |
|
3264 } |
|
3265 else |
2384
|
3266 return false; |
1968
|
3267 |
5275
|
3268 for (octave_idx_type j = 0; j < nc; j++) |
|
3269 for (octave_idx_type i = 0; i < nr; i++) |
1968
|
3270 { |
|
3271 Complex val = elem (i, j); |
|
3272 |
5315
|
3273 double r_val = std::real (val); |
|
3274 double i_val = std::imag (val); |
1968
|
3275 |
|
3276 if (r_val > max_val) |
|
3277 max_val = r_val; |
|
3278 |
|
3279 if (i_val > max_val) |
|
3280 max_val = i_val; |
|
3281 |
|
3282 if (r_val < min_val) |
|
3283 min_val = r_val; |
|
3284 |
|
3285 if (i_val < min_val) |
|
3286 min_val = i_val; |
|
3287 |
|
3288 if (D_NINT (r_val) != r_val || D_NINT (i_val) != i_val) |
2384
|
3289 return false; |
1968
|
3290 } |
2384
|
3291 |
|
3292 return true; |
1968
|
3293 } |
|
3294 |
2384
|
3295 bool |
1968
|
3296 ComplexMatrix::too_large_for_float (void) const |
|
3297 { |
5275
|
3298 octave_idx_type nr = rows (); |
|
3299 octave_idx_type nc = cols (); |
|
3300 |
|
3301 for (octave_idx_type j = 0; j < nc; j++) |
|
3302 for (octave_idx_type i = 0; i < nr; i++) |
1968
|
3303 { |
|
3304 Complex val = elem (i, j); |
|
3305 |
5315
|
3306 double r_val = std::real (val); |
|
3307 double i_val = std::imag (val); |
1968
|
3308 |
5389
|
3309 if ((! (xisnan (r_val) || xisinf (r_val)) |
5387
|
3310 && fabs (r_val) > FLT_MAX) |
5389
|
3311 || (! (xisnan (i_val) || xisinf (i_val)) |
5387
|
3312 && fabs (i_val) > FLT_MAX)) |
2384
|
3313 return true; |
1968
|
3314 } |
|
3315 |
2384
|
3316 return false; |
1968
|
3317 } |
|
3318 |
5775
|
3319 // FIXME Do these really belong here? Maybe they should be |
4015
|
3320 // in a base class? |
|
3321 |
2832
|
3322 boolMatrix |
4015
|
3323 ComplexMatrix::all (int dim) const |
458
|
3324 { |
4015
|
3325 MX_ALL_OP (dim); |
458
|
3326 } |
|
3327 |
2832
|
3328 boolMatrix |
4015
|
3329 ComplexMatrix::any (int dim) const |
458
|
3330 { |
4015
|
3331 MX_ANY_OP (dim); |
458
|
3332 } |
|
3333 |
|
3334 ComplexMatrix |
3723
|
3335 ComplexMatrix::cumprod (int dim) const |
458
|
3336 { |
4015
|
3337 MX_CUMULATIVE_OP (ComplexMatrix, Complex, *=); |
458
|
3338 } |
|
3339 |
|
3340 ComplexMatrix |
3723
|
3341 ComplexMatrix::cumsum (int dim) const |
458
|
3342 { |
4015
|
3343 MX_CUMULATIVE_OP (ComplexMatrix, Complex, +=); |
458
|
3344 } |
|
3345 |
|
3346 ComplexMatrix |
3723
|
3347 ComplexMatrix::prod (int dim) const |
458
|
3348 { |
3864
|
3349 MX_REDUCTION_OP (ComplexMatrix, *=, 1.0, 1.0); |
458
|
3350 } |
|
3351 |
|
3352 ComplexMatrix |
3723
|
3353 ComplexMatrix::sum (int dim) const |
458
|
3354 { |
3864
|
3355 MX_REDUCTION_OP (ComplexMatrix, +=, 0.0, 0.0); |
458
|
3356 } |
|
3357 |
|
3358 ComplexMatrix |
3723
|
3359 ComplexMatrix::sumsq (int dim) const |
458
|
3360 { |
3864
|
3361 #define ROW_EXPR \ |
|
3362 Complex d = elem (i, j); \ |
|
3363 retval.elem (i, 0) += d * conj (d) |
|
3364 |
|
3365 #define COL_EXPR \ |
|
3366 Complex d = elem (i, j); \ |
|
3367 retval.elem (0, j) += d * conj (d) |
|
3368 |
|
3369 MX_BASE_REDUCTION_OP (ComplexMatrix, ROW_EXPR, COL_EXPR, 0.0, 0.0); |
|
3370 |
|
3371 #undef ROW_EXPR |
|
3372 #undef COL_EXPR |
458
|
3373 } |
|
3374 |
4329
|
3375 Matrix ComplexMatrix::abs (void) const |
|
3376 { |
5275
|
3377 octave_idx_type nr = rows (); |
|
3378 octave_idx_type nc = cols (); |
4329
|
3379 |
|
3380 Matrix retval (nr, nc); |
|
3381 |
5275
|
3382 for (octave_idx_type j = 0; j < nc; j++) |
|
3383 for (octave_idx_type i = 0; i < nr; i++) |
5315
|
3384 retval (i, j) = std::abs (elem (i, j)); |
4329
|
3385 |
|
3386 return retval; |
|
3387 } |
|
3388 |
458
|
3389 ComplexColumnVector |
|
3390 ComplexMatrix::diag (void) const |
|
3391 { |
|
3392 return diag (0); |
|
3393 } |
|
3394 |
|
3395 ComplexColumnVector |
5275
|
3396 ComplexMatrix::diag (octave_idx_type k) const |
458
|
3397 { |
5275
|
3398 octave_idx_type nnr = rows (); |
|
3399 octave_idx_type nnc = cols (); |
458
|
3400 if (k > 0) |
|
3401 nnc -= k; |
|
3402 else if (k < 0) |
|
3403 nnr += k; |
|
3404 |
|
3405 ComplexColumnVector d; |
|
3406 |
|
3407 if (nnr > 0 && nnc > 0) |
|
3408 { |
5275
|
3409 octave_idx_type ndiag = (nnr < nnc) ? nnr : nnc; |
458
|
3410 |
|
3411 d.resize (ndiag); |
|
3412 |
|
3413 if (k > 0) |
|
3414 { |
5275
|
3415 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
3416 d.elem (i) = elem (i, i+k); |
|
3417 } |
4509
|
3418 else if (k < 0) |
458
|
3419 { |
5275
|
3420 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
3421 d.elem (i) = elem (i-k, i); |
|
3422 } |
|
3423 else |
|
3424 { |
5275
|
3425 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
3426 d.elem (i) = elem (i, i); |
|
3427 } |
|
3428 } |
|
3429 else |
4513
|
3430 (*current_liboctave_error_handler) |
|
3431 ("diag: requested diagonal out of range"); |
458
|
3432 |
|
3433 return d; |
|
3434 } |
|
3435 |
2354
|
3436 bool |
5275
|
3437 ComplexMatrix::row_is_real_only (octave_idx_type i) const |
2354
|
3438 { |
|
3439 bool retval = true; |
|
3440 |
5275
|
3441 octave_idx_type nc = columns (); |
|
3442 |
|
3443 for (octave_idx_type j = 0; j < nc; j++) |
2354
|
3444 { |
5315
|
3445 if (std::imag (elem (i, j)) != 0.0) |
2354
|
3446 { |
|
3447 retval = false; |
|
3448 break; |
|
3449 } |
|
3450 } |
|
3451 |
|
3452 return retval; |
|
3453 } |
|
3454 |
|
3455 bool |
5275
|
3456 ComplexMatrix::column_is_real_only (octave_idx_type j) const |
2354
|
3457 { |
|
3458 bool retval = true; |
|
3459 |
5275
|
3460 octave_idx_type nr = rows (); |
|
3461 |
|
3462 for (octave_idx_type i = 0; i < nr; i++) |
2354
|
3463 { |
5315
|
3464 if (std::imag (elem (i, j)) != 0.0) |
2354
|
3465 { |
|
3466 retval = false; |
|
3467 break; |
|
3468 } |
|
3469 } |
|
3470 |
|
3471 return retval; |
|
3472 } |
891
|
3473 |
458
|
3474 ComplexColumnVector |
|
3475 ComplexMatrix::row_min (void) const |
|
3476 { |
5275
|
3477 Array<octave_idx_type> dummy_idx; |
4587
|
3478 return row_min (dummy_idx); |
458
|
3479 } |
|
3480 |
|
3481 ComplexColumnVector |
5275
|
3482 ComplexMatrix::row_min (Array<octave_idx_type>& idx_arg) const |
458
|
3483 { |
|
3484 ComplexColumnVector result; |
|
3485 |
5275
|
3486 octave_idx_type nr = rows (); |
|
3487 octave_idx_type nc = cols (); |
458
|
3488 |
|
3489 if (nr > 0 && nc > 0) |
|
3490 { |
|
3491 result.resize (nr); |
4587
|
3492 idx_arg.resize (nr); |
458
|
3493 |
5275
|
3494 for (octave_idx_type i = 0; i < nr; i++) |
458
|
3495 { |
2354
|
3496 bool real_only = row_is_real_only (i); |
|
3497 |
5275
|
3498 octave_idx_type idx_j; |
4469
|
3499 |
|
3500 Complex tmp_min; |
|
3501 |
|
3502 double abs_min = octave_NaN; |
|
3503 |
|
3504 for (idx_j = 0; idx_j < nc; idx_j++) |
|
3505 { |
|
3506 tmp_min = elem (i, idx_j); |
|
3507 |
5389
|
3508 if (! xisnan (tmp_min)) |
4469
|
3509 { |
5315
|
3510 abs_min = real_only ? std::real (tmp_min) : std::abs (tmp_min); |
4469
|
3511 break; |
|
3512 } |
|
3513 } |
|
3514 |
5275
|
3515 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469
|
3516 { |
|
3517 Complex tmp = elem (i, j); |
|
3518 |
5389
|
3519 if (xisnan (tmp)) |
4469
|
3520 continue; |
|
3521 |
5315
|
3522 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469
|
3523 |
|
3524 if (abs_tmp < abs_min) |
|
3525 { |
|
3526 idx_j = j; |
|
3527 tmp_min = tmp; |
|
3528 abs_min = abs_tmp; |
|
3529 } |
|
3530 } |
|
3531 |
5389
|
3532 if (xisnan (tmp_min)) |
4469
|
3533 { |
|
3534 result.elem (i) = Complex_NaN_result; |
4587
|
3535 idx_arg.elem (i) = 0; |
4469
|
3536 } |
891
|
3537 else |
|
3538 { |
4469
|
3539 result.elem (i) = tmp_min; |
4587
|
3540 idx_arg.elem (i) = idx_j; |
891
|
3541 } |
458
|
3542 } |
|
3543 } |
|
3544 |
|
3545 return result; |
|
3546 } |
|
3547 |
|
3548 ComplexColumnVector |
|
3549 ComplexMatrix::row_max (void) const |
|
3550 { |
5275
|
3551 Array<octave_idx_type> dummy_idx; |
4587
|
3552 return row_max (dummy_idx); |
458
|
3553 } |
|
3554 |
|
3555 ComplexColumnVector |
5275
|
3556 ComplexMatrix::row_max (Array<octave_idx_type>& idx_arg) const |
458
|
3557 { |
|
3558 ComplexColumnVector result; |
|
3559 |
5275
|
3560 octave_idx_type nr = rows (); |
|
3561 octave_idx_type nc = cols (); |
458
|
3562 |
|
3563 if (nr > 0 && nc > 0) |
|
3564 { |
|
3565 result.resize (nr); |
4587
|
3566 idx_arg.resize (nr); |
458
|
3567 |
5275
|
3568 for (octave_idx_type i = 0; i < nr; i++) |
458
|
3569 { |
2354
|
3570 bool real_only = row_is_real_only (i); |
|
3571 |
5275
|
3572 octave_idx_type idx_j; |
4469
|
3573 |
|
3574 Complex tmp_max; |
|
3575 |
|
3576 double abs_max = octave_NaN; |
|
3577 |
|
3578 for (idx_j = 0; idx_j < nc; idx_j++) |
|
3579 { |
|
3580 tmp_max = elem (i, idx_j); |
|
3581 |
5389
|
3582 if (! xisnan (tmp_max)) |
4469
|
3583 { |
5315
|
3584 abs_max = real_only ? std::real (tmp_max) : std::abs (tmp_max); |
4469
|
3585 break; |
|
3586 } |
|
3587 } |
|
3588 |
5275
|
3589 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469
|
3590 { |
|
3591 Complex tmp = elem (i, j); |
|
3592 |
5389
|
3593 if (xisnan (tmp)) |
4469
|
3594 continue; |
|
3595 |
5315
|
3596 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469
|
3597 |
|
3598 if (abs_tmp > abs_max) |
|
3599 { |
|
3600 idx_j = j; |
|
3601 tmp_max = tmp; |
|
3602 abs_max = abs_tmp; |
|
3603 } |
|
3604 } |
|
3605 |
5389
|
3606 if (xisnan (tmp_max)) |
4469
|
3607 { |
|
3608 result.elem (i) = Complex_NaN_result; |
4587
|
3609 idx_arg.elem (i) = 0; |
4469
|
3610 } |
891
|
3611 else |
|
3612 { |
4469
|
3613 result.elem (i) = tmp_max; |
4587
|
3614 idx_arg.elem (i) = idx_j; |
891
|
3615 } |
458
|
3616 } |
|
3617 } |
|
3618 |
|
3619 return result; |
|
3620 } |
|
3621 |
|
3622 ComplexRowVector |
|
3623 ComplexMatrix::column_min (void) const |
|
3624 { |
5275
|
3625 Array<octave_idx_type> dummy_idx; |
4587
|
3626 return column_min (dummy_idx); |
458
|
3627 } |
|
3628 |
|
3629 ComplexRowVector |
5275
|
3630 ComplexMatrix::column_min (Array<octave_idx_type>& idx_arg) const |
458
|
3631 { |
|
3632 ComplexRowVector result; |
|
3633 |
5275
|
3634 octave_idx_type nr = rows (); |
|
3635 octave_idx_type nc = cols (); |
458
|
3636 |
|
3637 if (nr > 0 && nc > 0) |
|
3638 { |
|
3639 result.resize (nc); |
4587
|
3640 idx_arg.resize (nc); |
458
|
3641 |
5275
|
3642 for (octave_idx_type j = 0; j < nc; j++) |
458
|
3643 { |
2354
|
3644 bool real_only = column_is_real_only (j); |
|
3645 |
5275
|
3646 octave_idx_type idx_i; |
4469
|
3647 |
|
3648 Complex tmp_min; |
|
3649 |
|
3650 double abs_min = octave_NaN; |
|
3651 |
|
3652 for (idx_i = 0; idx_i < nr; idx_i++) |
|
3653 { |
|
3654 tmp_min = elem (idx_i, j); |
|
3655 |
5389
|
3656 if (! xisnan (tmp_min)) |
4469
|
3657 { |
5315
|
3658 abs_min = real_only ? std::real (tmp_min) : std::abs (tmp_min); |
4469
|
3659 break; |
|
3660 } |
|
3661 } |
|
3662 |
5275
|
3663 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469
|
3664 { |
|
3665 Complex tmp = elem (i, j); |
|
3666 |
5389
|
3667 if (xisnan (tmp)) |
4469
|
3668 continue; |
|
3669 |
5315
|
3670 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469
|
3671 |
|
3672 if (abs_tmp < abs_min) |
|
3673 { |
|
3674 idx_i = i; |
|
3675 tmp_min = tmp; |
|
3676 abs_min = abs_tmp; |
|
3677 } |
|
3678 } |
|
3679 |
5389
|
3680 if (xisnan (tmp_min)) |
4469
|
3681 { |
|
3682 result.elem (j) = Complex_NaN_result; |
4587
|
3683 idx_arg.elem (j) = 0; |
4469
|
3684 } |
891
|
3685 else |
|
3686 { |
4469
|
3687 result.elem (j) = tmp_min; |
4587
|
3688 idx_arg.elem (j) = idx_i; |
891
|
3689 } |
458
|
3690 } |
|
3691 } |
|
3692 |
|
3693 return result; |
|
3694 } |
|
3695 |
|
3696 ComplexRowVector |
|
3697 ComplexMatrix::column_max (void) const |
|
3698 { |
5275
|
3699 Array<octave_idx_type> dummy_idx; |
4587
|
3700 return column_max (dummy_idx); |
458
|
3701 } |
|
3702 |
|
3703 ComplexRowVector |
5275
|
3704 ComplexMatrix::column_max (Array<octave_idx_type>& idx_arg) const |
458
|
3705 { |
|
3706 ComplexRowVector result; |
|
3707 |
5275
|
3708 octave_idx_type nr = rows (); |
|
3709 octave_idx_type nc = cols (); |
458
|
3710 |
|
3711 if (nr > 0 && nc > 0) |
|
3712 { |
|
3713 result.resize (nc); |
4587
|
3714 idx_arg.resize (nc); |
458
|
3715 |
5275
|
3716 for (octave_idx_type j = 0; j < nc; j++) |
458
|
3717 { |
2354
|
3718 bool real_only = column_is_real_only (j); |
|
3719 |
5275
|
3720 octave_idx_type idx_i; |
4469
|
3721 |
|
3722 Complex tmp_max; |
|
3723 |
|
3724 double abs_max = octave_NaN; |
|
3725 |
|
3726 for (idx_i = 0; idx_i < nr; idx_i++) |
|
3727 { |
|
3728 tmp_max = elem (idx_i, j); |
|
3729 |
5389
|
3730 if (! xisnan (tmp_max)) |
4469
|
3731 { |
5315
|
3732 abs_max = real_only ? std::real (tmp_max) : std::abs (tmp_max); |
4469
|
3733 break; |
|
3734 } |
|
3735 } |
|
3736 |
5275
|
3737 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469
|
3738 { |
|
3739 Complex tmp = elem (i, j); |
|
3740 |
5389
|
3741 if (xisnan (tmp)) |
4469
|
3742 continue; |
|
3743 |
5315
|
3744 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469
|
3745 |
|
3746 if (abs_tmp > abs_max) |
|
3747 { |
|
3748 idx_i = i; |
|
3749 tmp_max = tmp; |
|
3750 abs_max = abs_tmp; |
|
3751 } |
|
3752 } |
|
3753 |
5389
|
3754 if (xisnan (tmp_max)) |
4469
|
3755 { |
|
3756 result.elem (j) = Complex_NaN_result; |
4587
|
3757 idx_arg.elem (j) = 0; |
4469
|
3758 } |
891
|
3759 else |
|
3760 { |
4469
|
3761 result.elem (j) = tmp_max; |
4587
|
3762 idx_arg.elem (j) = idx_i; |
891
|
3763 } |
458
|
3764 } |
|
3765 } |
|
3766 |
|
3767 return result; |
|
3768 } |
|
3769 |
|
3770 // i/o |
|
3771 |
3504
|
3772 std::ostream& |
|
3773 operator << (std::ostream& os, const ComplexMatrix& a) |
458
|
3774 { |
5275
|
3775 for (octave_idx_type i = 0; i < a.rows (); i++) |
458
|
3776 { |
5275
|
3777 for (octave_idx_type j = 0; j < a.cols (); j++) |
4130
|
3778 { |
|
3779 os << " "; |
|
3780 octave_write_complex (os, a.elem (i, j)); |
|
3781 } |
458
|
3782 os << "\n"; |
|
3783 } |
|
3784 return os; |
|
3785 } |
|
3786 |
3504
|
3787 std::istream& |
|
3788 operator >> (std::istream& is, ComplexMatrix& a) |
458
|
3789 { |
5275
|
3790 octave_idx_type nr = a.rows (); |
|
3791 octave_idx_type nc = a.cols (); |
458
|
3792 |
|
3793 if (nr < 1 || nc < 1) |
3504
|
3794 is.clear (std::ios::badbit); |
458
|
3795 else |
|
3796 { |
|
3797 Complex tmp; |
5275
|
3798 for (octave_idx_type i = 0; i < nr; i++) |
|
3799 for (octave_idx_type j = 0; j < nc; j++) |
458
|
3800 { |
4130
|
3801 tmp = octave_read_complex (is); |
458
|
3802 if (is) |
|
3803 a.elem (i, j) = tmp; |
|
3804 else |
2993
|
3805 goto done; |
458
|
3806 } |
|
3807 } |
|
3808 |
2993
|
3809 done: |
|
3810 |
458
|
3811 return is; |
|
3812 } |
|
3813 |
1819
|
3814 ComplexMatrix |
|
3815 Givens (const Complex& x, const Complex& y) |
|
3816 { |
|
3817 double cc; |
|
3818 Complex cs, temp_r; |
|
3819 |
3887
|
3820 F77_FUNC (zlartg, ZLARTG) (x, y, cc, cs, temp_r); |
1819
|
3821 |
|
3822 ComplexMatrix g (2, 2); |
|
3823 |
|
3824 g.elem (0, 0) = cc; |
|
3825 g.elem (1, 1) = cc; |
|
3826 g.elem (0, 1) = cs; |
|
3827 g.elem (1, 0) = -conj (cs); |
|
3828 |
|
3829 return g; |
|
3830 } |
|
3831 |
|
3832 ComplexMatrix |
|
3833 Sylvester (const ComplexMatrix& a, const ComplexMatrix& b, |
|
3834 const ComplexMatrix& c) |
|
3835 { |
|
3836 ComplexMatrix retval; |
|
3837 |
5775
|
3838 // FIXME -- need to check that a, b, and c are all the same |
1819
|
3839 // size. |
|
3840 |
|
3841 // Compute Schur decompositions |
|
3842 |
|
3843 ComplexSCHUR as (a, "U"); |
|
3844 ComplexSCHUR bs (b, "U"); |
|
3845 |
|
3846 // Transform c to new coordinates. |
|
3847 |
|
3848 ComplexMatrix ua = as.unitary_matrix (); |
|
3849 ComplexMatrix sch_a = as.schur_matrix (); |
|
3850 |
|
3851 ComplexMatrix ub = bs.unitary_matrix (); |
|
3852 ComplexMatrix sch_b = bs.schur_matrix (); |
|
3853 |
|
3854 ComplexMatrix cx = ua.hermitian () * c * ub; |
|
3855 |
|
3856 // Solve the sylvester equation, back-transform, and return the |
|
3857 // solution. |
|
3858 |
5275
|
3859 octave_idx_type a_nr = a.rows (); |
|
3860 octave_idx_type b_nr = b.rows (); |
1819
|
3861 |
|
3862 double scale; |
5275
|
3863 octave_idx_type info; |
1950
|
3864 |
|
3865 Complex *pa = sch_a.fortran_vec (); |
|
3866 Complex *pb = sch_b.fortran_vec (); |
|
3867 Complex *px = cx.fortran_vec (); |
1819
|
3868 |
4552
|
3869 F77_XFCN (ztrsyl, ZTRSYL, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3870 F77_CONST_CHAR_ARG2 ("N", 1), |
|
3871 1, a_nr, b_nr, pa, a_nr, pb, |
|
3872 b_nr, px, a_nr, scale, info |
|
3873 F77_CHAR_ARG_LEN (1) |
|
3874 F77_CHAR_ARG_LEN (1))); |
1950
|
3875 |
|
3876 if (f77_exception_encountered) |
|
3877 (*current_liboctave_error_handler) ("unrecoverable error in ztrsyl"); |
|
3878 else |
|
3879 { |
5775
|
3880 // FIXME -- check info? |
1950
|
3881 |
|
3882 retval = -ua * cx * ub.hermitian (); |
|
3883 } |
1819
|
3884 |
|
3885 return retval; |
|
3886 } |
|
3887 |
2828
|
3888 ComplexMatrix |
|
3889 operator * (const ComplexMatrix& m, const Matrix& a) |
|
3890 { |
|
3891 ComplexMatrix tmp (a); |
|
3892 return m * tmp; |
|
3893 } |
|
3894 |
|
3895 ComplexMatrix |
|
3896 operator * (const Matrix& m, const ComplexMatrix& a) |
|
3897 { |
|
3898 ComplexMatrix tmp (m); |
|
3899 return tmp * a; |
|
3900 } |
|
3901 |
6162
|
3902 /* Simple Dot Product, Matrix-Vector and Matrix-Matrix Unit tests |
|
3903 %!assert([1+i 2+i 3+i] * [ 4+i ; 5+i ; 6+i], 29+21i, 1e-14) |
|
3904 %!assert([1+i 2+i ; 3+i 4+i ] * [5+i ; 6+i], [15 + 14i ; 37 + 18i], 1e-14) |
|
3905 %!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) |
|
3906 */ |
|
3907 |
|
3908 /* Test some simple identities |
|
3909 %!shared M, cv, rv |
|
3910 %! M = randn(10,10)+i*rand(10,10); |
|
3911 %! cv = randn(10,1)+i*rand(10,1); |
|
3912 %! rv = randn(1,10)+i*rand(1,10); |
|
3913 %!assert([M*cv,M*cv],M*[cv,cv],1e-14) |
|
3914 %!assert([rv*M;rv*M],[rv;rv]*M,1e-14) |
|
3915 %!assert(2*rv*cv,[rv,rv]*[cv;cv],1e-14) |
|
3916 */ |
|
3917 |
2828
|
3918 ComplexMatrix |
|
3919 operator * (const ComplexMatrix& m, const ComplexMatrix& a) |
|
3920 { |
|
3921 ComplexMatrix retval; |
|
3922 |
5275
|
3923 octave_idx_type nr = m.rows (); |
|
3924 octave_idx_type nc = m.cols (); |
|
3925 |
|
3926 octave_idx_type a_nr = a.rows (); |
|
3927 octave_idx_type a_nc = a.cols (); |
2828
|
3928 |
|
3929 if (nc != a_nr) |
|
3930 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
|
3931 else |
|
3932 { |
|
3933 if (nr == 0 || nc == 0 || a_nc == 0) |
3760
|
3934 retval.resize (nr, a_nc, 0.0); |
2828
|
3935 else |
|
3936 { |
5275
|
3937 octave_idx_type ld = nr; |
|
3938 octave_idx_type lda = a.rows (); |
2828
|
3939 |
|
3940 retval.resize (nr, a_nc); |
|
3941 Complex *c = retval.fortran_vec (); |
|
3942 |
5983
|
3943 if (a_nc == 1) |
|
3944 { |
|
3945 if (nr == 1) |
|
3946 F77_FUNC (xzdotu, XZDOTU) (nc, m.data (), 1, a.data (), 1, *c); |
|
3947 else |
6390
|
3948 { |
|
3949 F77_XFCN (zgemv, ZGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3950 nr, nc, 1.0, m.data (), ld, |
|
3951 a.data (), 1, 0.0, c, 1 |
|
3952 F77_CHAR_ARG_LEN (1))); |
|
3953 |
|
3954 if (f77_exception_encountered) |
|
3955 (*current_liboctave_error_handler) |
|
3956 ("unrecoverable error in zgemv"); |
|
3957 } |
5983
|
3958 } |
|
3959 else |
6390
|
3960 { |
|
3961 F77_XFCN (zgemm, ZGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3962 F77_CONST_CHAR_ARG2 ("N", 1), |
|
3963 nr, a_nc, nc, 1.0, m.data (), |
|
3964 ld, a.data (), lda, 0.0, c, nr |
|
3965 F77_CHAR_ARG_LEN (1) |
|
3966 F77_CHAR_ARG_LEN (1))); |
|
3967 |
|
3968 if (f77_exception_encountered) |
|
3969 (*current_liboctave_error_handler) |
|
3970 ("unrecoverable error in zgemm"); |
|
3971 } |
2828
|
3972 } |
|
3973 } |
|
3974 |
|
3975 return retval; |
|
3976 } |
|
3977 |
5775
|
3978 // FIXME -- it would be nice to share code among the min/max |
4309
|
3979 // functions below. |
|
3980 |
|
3981 #define EMPTY_RETURN_CHECK(T) \ |
|
3982 if (nr == 0 || nc == 0) \ |
|
3983 return T (nr, nc); |
|
3984 |
|
3985 ComplexMatrix |
|
3986 min (const Complex& c, const ComplexMatrix& m) |
|
3987 { |
5275
|
3988 octave_idx_type nr = m.rows (); |
|
3989 octave_idx_type nc = m.columns (); |
4309
|
3990 |
|
3991 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
3992 |
|
3993 ComplexMatrix result (nr, nc); |
|
3994 |
5275
|
3995 for (octave_idx_type j = 0; j < nc; j++) |
|
3996 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3997 { |
|
3998 OCTAVE_QUIT; |
|
3999 result (i, j) = xmin (c, m (i, j)); |
|
4000 } |
|
4001 |
|
4002 return result; |
|
4003 } |
|
4004 |
|
4005 ComplexMatrix |
|
4006 min (const ComplexMatrix& m, const Complex& c) |
|
4007 { |
5275
|
4008 octave_idx_type nr = m.rows (); |
|
4009 octave_idx_type nc = m.columns (); |
4309
|
4010 |
|
4011 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
4012 |
|
4013 ComplexMatrix result (nr, nc); |
|
4014 |
5275
|
4015 for (octave_idx_type j = 0; j < nc; j++) |
|
4016 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
4017 { |
|
4018 OCTAVE_QUIT; |
|
4019 result (i, j) = xmin (m (i, j), c); |
|
4020 } |
|
4021 |
|
4022 return result; |
|
4023 } |
|
4024 |
|
4025 ComplexMatrix |
|
4026 min (const ComplexMatrix& a, const ComplexMatrix& b) |
|
4027 { |
5275
|
4028 octave_idx_type nr = a.rows (); |
|
4029 octave_idx_type nc = a.columns (); |
4309
|
4030 |
|
4031 if (nr != b.rows () || nc != b.columns ()) |
|
4032 { |
|
4033 (*current_liboctave_error_handler) |
|
4034 ("two-arg min expecting args of same size"); |
|
4035 return ComplexMatrix (); |
|
4036 } |
|
4037 |
|
4038 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
4039 |
|
4040 ComplexMatrix result (nr, nc); |
|
4041 |
5275
|
4042 for (octave_idx_type j = 0; j < nc; j++) |
4309
|
4043 { |
|
4044 int columns_are_real_only = 1; |
5275
|
4045 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
4046 { |
|
4047 OCTAVE_QUIT; |
5315
|
4048 if (std::imag (a (i, j)) != 0.0 || std::imag (b (i, j)) != 0.0) |
4309
|
4049 { |
|
4050 columns_are_real_only = 0; |
|
4051 break; |
|
4052 } |
|
4053 } |
|
4054 |
|
4055 if (columns_are_real_only) |
|
4056 { |
5275
|
4057 for (octave_idx_type i = 0; i < nr; i++) |
5315
|
4058 result (i, j) = xmin (std::real (a (i, j)), std::real (b (i, j))); |
4309
|
4059 } |
|
4060 else |
|
4061 { |
5275
|
4062 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
4063 { |
|
4064 OCTAVE_QUIT; |
|
4065 result (i, j) = xmin (a (i, j), b (i, j)); |
|
4066 } |
|
4067 } |
|
4068 } |
|
4069 |
|
4070 return result; |
|
4071 } |
|
4072 |
|
4073 ComplexMatrix |
|
4074 max (const Complex& c, const ComplexMatrix& m) |
|
4075 { |
5275
|
4076 octave_idx_type nr = m.rows (); |
|
4077 octave_idx_type nc = m.columns (); |
4309
|
4078 |
|
4079 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
4080 |
|
4081 ComplexMatrix result (nr, nc); |
|
4082 |
5275
|
4083 for (octave_idx_type j = 0; j < nc; j++) |
|
4084 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
4085 { |
|
4086 OCTAVE_QUIT; |
|
4087 result (i, j) = xmax (c, m (i, j)); |
|
4088 } |
|
4089 |
|
4090 return result; |
|
4091 } |
|
4092 |
|
4093 ComplexMatrix |
|
4094 max (const ComplexMatrix& m, const Complex& c) |
|
4095 { |
5275
|
4096 octave_idx_type nr = m.rows (); |
|
4097 octave_idx_type nc = m.columns (); |
4309
|
4098 |
|
4099 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
4100 |
|
4101 ComplexMatrix result (nr, nc); |
|
4102 |
5275
|
4103 for (octave_idx_type j = 0; j < nc; j++) |
|
4104 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
4105 { |
|
4106 OCTAVE_QUIT; |
|
4107 result (i, j) = xmax (m (i, j), c); |
|
4108 } |
|
4109 |
|
4110 return result; |
|
4111 } |
|
4112 |
|
4113 ComplexMatrix |
|
4114 max (const ComplexMatrix& a, const ComplexMatrix& b) |
|
4115 { |
5275
|
4116 octave_idx_type nr = a.rows (); |
|
4117 octave_idx_type nc = a.columns (); |
4309
|
4118 |
|
4119 if (nr != b.rows () || nc != b.columns ()) |
|
4120 { |
|
4121 (*current_liboctave_error_handler) |
|
4122 ("two-arg max expecting args of same size"); |
|
4123 return ComplexMatrix (); |
|
4124 } |
|
4125 |
|
4126 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
4127 |
|
4128 ComplexMatrix result (nr, nc); |
|
4129 |
5275
|
4130 for (octave_idx_type j = 0; j < nc; j++) |
4309
|
4131 { |
|
4132 int columns_are_real_only = 1; |
5275
|
4133 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
4134 { |
|
4135 OCTAVE_QUIT; |
5315
|
4136 if (std::imag (a (i, j)) != 0.0 || std::imag (b (i, j)) != 0.0) |
4309
|
4137 { |
|
4138 columns_are_real_only = 0; |
|
4139 break; |
|
4140 } |
|
4141 } |
|
4142 |
|
4143 if (columns_are_real_only) |
|
4144 { |
5275
|
4145 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
4146 { |
|
4147 OCTAVE_QUIT; |
5315
|
4148 result (i, j) = xmax (std::real (a (i, j)), std::real (b (i, j))); |
4309
|
4149 } |
|
4150 } |
|
4151 else |
|
4152 { |
5275
|
4153 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
4154 { |
|
4155 OCTAVE_QUIT; |
|
4156 result (i, j) = xmax (a (i, j), b (i, j)); |
|
4157 } |
|
4158 } |
|
4159 } |
|
4160 |
|
4161 return result; |
|
4162 } |
|
4163 |
5315
|
4164 MS_CMP_OPS(ComplexMatrix, std::real, Complex, std::real) |
3504
|
4165 MS_BOOL_OPS(ComplexMatrix, Complex, 0.0) |
2870
|
4166 |
5315
|
4167 SM_CMP_OPS(Complex, std::real, ComplexMatrix, std::real) |
3504
|
4168 SM_BOOL_OPS(Complex, ComplexMatrix, 0.0) |
2870
|
4169 |
5315
|
4170 MM_CMP_OPS(ComplexMatrix, std::real, ComplexMatrix, std::real) |
3504
|
4171 MM_BOOL_OPS(ComplexMatrix, ComplexMatrix, 0.0) |
2870
|
4172 |
458
|
4173 /* |
|
4174 ;;; Local Variables: *** |
|
4175 ;;; mode: C++ *** |
|
4176 ;;; End: *** |
|
4177 */ |