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