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