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