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