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