Mercurial > hg > octave-nkf
annotate liboctave/CMatrix.cc @ 8999:dc07bc4157b8
allow empty matrices in stream input operators
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 20 Mar 2009 11:39:25 +0100 |
parents | d91fa4b20bbb |
children | 8145f2255276 |
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 | |
8920 | 6 Copyright (C) 2008, 2009 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++) | |
8956
d91fa4b20bbb
ensure nonnegative char -> real conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
315 elem (i, j) = static_cast<unsigned char> (a.elem (i, j)); |
1574 | 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, |
8806 | 1556 octave_idx_type& info, double& rcon, |
1557 int calc_cond) const | |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1558 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1559 ComplexDET retval (1.0); |
458 | 1560 |
5275 | 1561 octave_idx_type nr = rows (); |
1562 octave_idx_type nc = cols (); | |
458 | 1563 |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1564 if (nr != nc) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1565 (*current_liboctave_error_handler) ("matrix must be square"); |
458 | 1566 else |
1567 { | |
8806 | 1568 volatile int typ = mattype.type (); |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1569 |
8337
e02242c54c49
reuse matrix type detected in det
Jaroslav Hajek <highegg@gmail.com>
parents:
8336
diff
changeset
|
1570 if (typ == MatrixType::Unknown) |
e02242c54c49
reuse matrix type detected in det
Jaroslav Hajek <highegg@gmail.com>
parents:
8336
diff
changeset
|
1571 typ = mattype.type (*this); |
e02242c54c49
reuse matrix type detected in det
Jaroslav Hajek <highegg@gmail.com>
parents:
8336
diff
changeset
|
1572 |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1573 if (typ == MatrixType::Lower || typ == MatrixType::Upper) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1574 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1575 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
|
1576 retval *= elem (i,i); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1577 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1578 else if (typ == MatrixType::Hermitian) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1579 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1580 ComplexMatrix atmp = *this; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1581 Complex *tmp_data = atmp.fortran_vec (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1582 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1583 info = 0; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1584 double anorm = 0; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1585 if (calc_cond) anorm = xnorm (*this, 1); |
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 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1588 char job = 'L'; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1589 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
|
1590 tmp_data, nr, info |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1591 F77_CHAR_ARG_LEN (1))); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1592 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1593 if (info != 0) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1594 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1595 rcon = 0.0; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1596 mattype.mark_as_unsymmetric (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1597 typ = MatrixType::Full; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1598 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1599 else |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1600 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1601 Array<Complex> z (2 * nc); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1602 Complex *pz = z.fortran_vec (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1603 Array<double> rz (nc); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1604 double *prz = rz.fortran_vec (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1605 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1606 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
|
1607 nr, tmp_data, nr, anorm, |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1608 rcon, pz, prz, info |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1609 F77_CHAR_ARG_LEN (1))); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1610 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1611 if (info != 0) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1612 rcon = 0.0; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1613 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1614 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
|
1615 retval *= atmp (i,i); |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1616 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1617 retval = retval.square (); |
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 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1620 else if (typ != MatrixType::Full) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1621 (*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
|
1622 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1623 if (typ == MatrixType::Full) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1624 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1625 Array<octave_idx_type> ipvt (nr); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1626 octave_idx_type *pipvt = ipvt.fortran_vec (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1627 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1628 ComplexMatrix atmp = *this; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1629 Complex *tmp_data = atmp.fortran_vec (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1630 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1631 info = 0; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1632 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1633 // 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
|
1634 double anorm = 0; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1635 if (calc_cond) anorm = xnorm (*this, 1); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1636 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1637 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
|
1638 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1639 // 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
|
1640 rcon = 0.0; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1641 if (info != 0) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1642 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1643 info = -1; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1644 retval = ComplexDET (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1645 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1646 else |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1647 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1648 if (calc_cond) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1649 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1650 // 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
|
1651 char job = '1'; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1652 Array<Complex> z (2 * nc); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1653 Complex *pz = z.fortran_vec (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1654 Array<double> rz (2 * nc); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1655 double *prz = rz.fortran_vec (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1656 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1657 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
|
1658 nc, tmp_data, nr, anorm, |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1659 rcon, pz, prz, info |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1660 F77_CHAR_ARG_LEN (1))); |
8335 | 1661 } |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1662 |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1663 if (info != 0) |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1664 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1665 info = -1; |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1666 retval = ComplexDET (); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1667 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1668 else |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1669 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1670 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
|
1671 { |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1672 Complex c = atmp(i,i); |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1673 retval *= (ipvt(i) != (i+1)) ? -c : c; |
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 } |
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1677 } |
458 | 1678 } |
8336
9813c07ca946
make det take advantage of matrix type
Jaroslav Hajek <highegg@gmail.com>
parents:
8335
diff
changeset
|
1679 |
458 | 1680 return retval; |
1681 } | |
1682 | |
7788 | 1683 double |
1684 ComplexMatrix::rcond (void) const | |
1685 { | |
1686 MatrixType mattype (*this); | |
1687 return rcond (mattype); | |
1688 } | |
1689 | |
1690 double | |
1691 ComplexMatrix::rcond (MatrixType &mattype) const | |
1692 { | |
1693 double rcon; | |
1694 octave_idx_type nr = rows (); | |
1695 octave_idx_type nc = cols (); | |
1696 | |
1697 if (nr != nc) | |
1698 (*current_liboctave_error_handler) ("matrix must be square"); | |
1699 else if (nr == 0 || nc == 0) | |
1700 rcon = octave_Inf; | |
1701 else | |
1702 { | |
1703 int typ = mattype.type (); | |
1704 | |
1705 if (typ == MatrixType::Unknown) | |
1706 typ = mattype.type (*this); | |
1707 | |
1708 // Only calculate the condition number for LU/Cholesky | |
1709 if (typ == MatrixType::Upper) | |
1710 { | |
1711 const Complex *tmp_data = fortran_vec (); | |
1712 octave_idx_type info = 0; | |
1713 char norm = '1'; | |
1714 char uplo = 'U'; | |
1715 char dia = 'N'; | |
1716 | |
1717 Array<Complex> z (2 * nc); | |
1718 Complex *pz = z.fortran_vec (); | |
1719 Array<double> rz (nc); | |
1720 double *prz = rz.fortran_vec (); | |
1721 | |
1722 F77_XFCN (ztrcon, ZTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), | |
1723 F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1724 F77_CONST_CHAR_ARG2 (&dia, 1), | |
1725 nr, tmp_data, nr, rcon, | |
1726 pz, prz, info | |
1727 F77_CHAR_ARG_LEN (1) | |
1728 F77_CHAR_ARG_LEN (1) | |
1729 F77_CHAR_ARG_LEN (1))); | |
1730 | |
1731 if (info != 0) | |
1732 rcon = 0; | |
1733 } | |
1734 else if (typ == MatrixType::Permuted_Upper) | |
1735 (*current_liboctave_error_handler) | |
1736 ("permuted triangular matrix not implemented"); | |
1737 else if (typ == MatrixType::Lower) | |
1738 { | |
1739 const Complex *tmp_data = fortran_vec (); | |
1740 octave_idx_type info = 0; | |
1741 char norm = '1'; | |
1742 char uplo = 'L'; | |
1743 char dia = 'N'; | |
1744 | |
1745 Array<Complex> z (2 * nc); | |
1746 Complex *pz = z.fortran_vec (); | |
1747 Array<double> rz (nc); | |
1748 double *prz = rz.fortran_vec (); | |
1749 | |
1750 F77_XFCN (ztrcon, ZTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), | |
1751 F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1752 F77_CONST_CHAR_ARG2 (&dia, 1), | |
1753 nr, tmp_data, nr, rcon, | |
1754 pz, prz, info | |
1755 F77_CHAR_ARG_LEN (1) | |
1756 F77_CHAR_ARG_LEN (1) | |
1757 F77_CHAR_ARG_LEN (1))); | |
1758 | |
1759 if (info != 0) | |
1760 rcon = 0.0; | |
1761 } | |
1762 else if (typ == MatrixType::Permuted_Lower) | |
1763 (*current_liboctave_error_handler) | |
1764 ("permuted triangular matrix not implemented"); | |
1765 else if (typ == MatrixType::Full || typ == MatrixType::Hermitian) | |
1766 { | |
1767 double anorm = -1.0; | |
1768 ComplexMatrix atmp = *this; | |
1769 Complex *tmp_data = atmp.fortran_vec (); | |
1770 | |
1771 if (typ == MatrixType::Hermitian) | |
1772 { | |
1773 octave_idx_type info = 0; | |
1774 char job = 'L'; | |
1775 anorm = atmp.abs().sum(). | |
1776 row(static_cast<octave_idx_type>(0)).max(); | |
1777 | |
1778 F77_XFCN (zpotrf, ZPOTRF, (F77_CONST_CHAR_ARG2 (&job, 1), nr, | |
1779 tmp_data, nr, info | |
1780 F77_CHAR_ARG_LEN (1))); | |
1781 | |
1782 if (info != 0) | |
1783 { | |
1784 rcon = 0.0; | |
1785 | |
1786 mattype.mark_as_unsymmetric (); | |
1787 typ = MatrixType::Full; | |
1788 } | |
1789 else | |
1790 { | |
1791 Array<Complex> z (2 * nc); | |
1792 Complex *pz = z.fortran_vec (); | |
1793 Array<double> rz (nc); | |
1794 double *prz = rz.fortran_vec (); | |
1795 | |
1796 F77_XFCN (zpocon, ZPOCON, (F77_CONST_CHAR_ARG2 (&job, 1), | |
1797 nr, tmp_data, nr, anorm, | |
1798 rcon, pz, prz, info | |
1799 F77_CHAR_ARG_LEN (1))); | |
1800 | |
1801 if (info != 0) | |
1802 rcon = 0.0; | |
1803 } | |
1804 } | |
1805 | |
1806 | |
1807 if (typ == MatrixType::Full) | |
1808 { | |
1809 octave_idx_type info = 0; | |
1810 | |
1811 Array<octave_idx_type> ipvt (nr); | |
1812 octave_idx_type *pipvt = ipvt.fortran_vec (); | |
1813 | |
1814 if(anorm < 0.) | |
1815 anorm = atmp.abs().sum(). | |
1816 row(static_cast<octave_idx_type>(0)).max(); | |
1817 | |
1818 Array<Complex> z (2 * nc); | |
1819 Complex *pz = z.fortran_vec (); | |
1820 Array<double> rz (2 * nc); | |
1821 double *prz = rz.fortran_vec (); | |
1822 | |
1823 F77_XFCN (zgetrf, ZGETRF, (nr, nr, tmp_data, nr, pipvt, info)); | |
1824 | |
1825 if (info != 0) | |
1826 { | |
1827 rcon = 0.0; | |
1828 mattype.mark_as_rectangular (); | |
1829 } | |
1830 else | |
1831 { | |
1832 char job = '1'; | |
1833 F77_XFCN (zgecon, ZGECON, (F77_CONST_CHAR_ARG2 (&job, 1), | |
1834 nc, tmp_data, nr, anorm, | |
1835 rcon, pz, prz, info | |
1836 F77_CHAR_ARG_LEN (1))); | |
1837 | |
1838 if (info != 0) | |
1839 rcon = 0.0; | |
1840 } | |
1841 } | |
1842 } | |
1843 else | |
1844 rcon = 0.0; | |
1845 } | |
1846 | |
1847 return rcon; | |
1848 } | |
1849 | |
458 | 1850 ComplexMatrix |
5785 | 1851 ComplexMatrix::utsolve (MatrixType &mattype, const ComplexMatrix& b, |
7788 | 1852 octave_idx_type& info, double& rcon, |
5785 | 1853 solve_singularity_handler sing_handler, |
1854 bool calc_cond) const | |
1855 { | |
1856 ComplexMatrix retval; | |
1857 | |
1858 octave_idx_type nr = rows (); | |
1859 octave_idx_type nc = cols (); | |
1860 | |
6924 | 1861 if (nr != b.rows ()) |
5785 | 1862 (*current_liboctave_error_handler) |
1863 ("matrix dimension mismatch solution of linear equations"); | |
6924 | 1864 else if (nr == 0 || nc == 0 || b.cols () == 0) |
1865 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); | |
5785 | 1866 else |
1867 { | |
1868 volatile int typ = mattype.type (); | |
1869 | |
1870 if (typ == MatrixType::Permuted_Upper || | |
1871 typ == MatrixType::Upper) | |
1872 { | |
1873 octave_idx_type b_nc = b.cols (); | |
7788 | 1874 rcon = 1.; |
5785 | 1875 info = 0; |
1876 | |
1877 if (typ == MatrixType::Permuted_Upper) | |
1878 { | |
1879 (*current_liboctave_error_handler) | |
6390 | 1880 ("permuted triangular matrix not implemented"); |
5785 | 1881 } |
1882 else | |
1883 { | |
1884 const Complex *tmp_data = fortran_vec (); | |
1885 | |
1886 if (calc_cond) | |
1887 { | |
1888 char norm = '1'; | |
1889 char uplo = 'U'; | |
1890 char dia = 'N'; | |
1891 | |
1892 Array<Complex> z (2 * nc); | |
1893 Complex *pz = z.fortran_vec (); | |
1894 Array<double> rz (nc); | |
1895 double *prz = rz.fortran_vec (); | |
1896 | |
1897 F77_XFCN (ztrcon, ZTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), | |
1898 F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1899 F77_CONST_CHAR_ARG2 (&dia, 1), | |
7788 | 1900 nr, tmp_data, nr, rcon, |
5785 | 1901 pz, prz, info |
1902 F77_CHAR_ARG_LEN (1) | |
1903 F77_CHAR_ARG_LEN (1) | |
1904 F77_CHAR_ARG_LEN (1))); | |
1905 | |
1906 if (info != 0) | |
1907 info = -2; | |
1908 | |
7788 | 1909 volatile double rcond_plus_one = rcon + 1.0; |
1910 | |
1911 if (rcond_plus_one == 1.0 || xisnan (rcon)) | |
5785 | 1912 { |
1913 info = -2; | |
1914 | |
1915 if (sing_handler) | |
7788 | 1916 sing_handler (rcon); |
5785 | 1917 else |
1918 (*current_liboctave_error_handler) | |
1919 ("matrix singular to machine precision, rcond = %g", | |
7788 | 1920 rcon); |
5785 | 1921 } |
1922 } | |
1923 | |
1924 if (info == 0) | |
1925 { | |
1926 retval = b; | |
1927 Complex *result = retval.fortran_vec (); | |
1928 | |
1929 char uplo = 'U'; | |
1930 char trans = 'N'; | |
1931 char dia = 'N'; | |
1932 | |
1933 F77_XFCN (ztrtrs, ZTRTRS, (F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1934 F77_CONST_CHAR_ARG2 (&trans, 1), | |
1935 F77_CONST_CHAR_ARG2 (&dia, 1), | |
1936 nr, b_nc, tmp_data, nr, | |
1937 result, nr, info | |
1938 F77_CHAR_ARG_LEN (1) | |
1939 F77_CHAR_ARG_LEN (1) | |
1940 F77_CHAR_ARG_LEN (1))); | |
1941 } | |
1942 } | |
1943 } | |
1944 else | |
1945 (*current_liboctave_error_handler) ("incorrect matrix type"); | |
1946 } | |
1947 | |
1948 return retval; | |
1949 } | |
1950 | |
1951 ComplexMatrix | |
1952 ComplexMatrix::ltsolve (MatrixType &mattype, const ComplexMatrix& b, | |
7788 | 1953 octave_idx_type& info, double& rcon, |
5785 | 1954 solve_singularity_handler sing_handler, |
1955 bool calc_cond) const | |
1956 { | |
1957 ComplexMatrix retval; | |
1958 | |
1959 octave_idx_type nr = rows (); | |
1960 octave_idx_type nc = cols (); | |
1961 | |
6924 | 1962 if (nr != b.rows ()) |
5785 | 1963 (*current_liboctave_error_handler) |
1964 ("matrix dimension mismatch solution of linear equations"); | |
6924 | 1965 else if (nr == 0 || nc == 0 || b.cols () == 0) |
1966 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); | |
5785 | 1967 else |
1968 { | |
1969 volatile int typ = mattype.type (); | |
1970 | |
1971 if (typ == MatrixType::Permuted_Lower || | |
1972 typ == MatrixType::Lower) | |
1973 { | |
1974 octave_idx_type b_nc = b.cols (); | |
7788 | 1975 rcon = 1.; |
5785 | 1976 info = 0; |
1977 | |
1978 if (typ == MatrixType::Permuted_Lower) | |
1979 { | |
1980 (*current_liboctave_error_handler) | |
6390 | 1981 ("permuted triangular matrix not implemented"); |
5785 | 1982 } |
1983 else | |
1984 { | |
1985 const Complex *tmp_data = fortran_vec (); | |
1986 | |
1987 if (calc_cond) | |
1988 { | |
1989 char norm = '1'; | |
1990 char uplo = 'L'; | |
1991 char dia = 'N'; | |
1992 | |
1993 Array<Complex> z (2 * nc); | |
1994 Complex *pz = z.fortran_vec (); | |
1995 Array<double> rz (nc); | |
1996 double *prz = rz.fortran_vec (); | |
1997 | |
1998 F77_XFCN (ztrcon, ZTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), | |
1999 F77_CONST_CHAR_ARG2 (&uplo, 1), | |
2000 F77_CONST_CHAR_ARG2 (&dia, 1), | |
7788 | 2001 nr, tmp_data, nr, rcon, |
5785 | 2002 pz, prz, info |
2003 F77_CHAR_ARG_LEN (1) | |
2004 F77_CHAR_ARG_LEN (1) | |
2005 F77_CHAR_ARG_LEN (1))); | |
2006 | |
2007 if (info != 0) | |
2008 info = -2; | |
2009 | |
7788 | 2010 volatile double rcond_plus_one = rcon + 1.0; |
2011 | |
2012 if (rcond_plus_one == 1.0 || xisnan (rcon)) | |
5785 | 2013 { |
2014 info = -2; | |
2015 | |
2016 if (sing_handler) | |
7788 | 2017 sing_handler (rcon); |
5785 | 2018 else |
2019 (*current_liboctave_error_handler) | |
2020 ("matrix singular to machine precision, rcond = %g", | |
7788 | 2021 rcon); |
5785 | 2022 } |
2023 } | |
2024 | |
2025 if (info == 0) | |
2026 { | |
2027 retval = b; | |
2028 Complex *result = retval.fortran_vec (); | |
2029 | |
2030 char uplo = 'L'; | |
2031 char trans = 'N'; | |
2032 char dia = 'N'; | |
2033 | |
2034 F77_XFCN (ztrtrs, ZTRTRS, (F77_CONST_CHAR_ARG2 (&uplo, 1), | |
2035 F77_CONST_CHAR_ARG2 (&trans, 1), | |
2036 F77_CONST_CHAR_ARG2 (&dia, 1), | |
2037 nr, b_nc, tmp_data, nr, | |
2038 result, nr, info | |
2039 F77_CHAR_ARG_LEN (1) | |
2040 F77_CHAR_ARG_LEN (1) | |
2041 F77_CHAR_ARG_LEN (1))); | |
2042 } | |
2043 } | |
2044 } | |
2045 else | |
2046 (*current_liboctave_error_handler) ("incorrect matrix type"); | |
2047 } | |
2048 | |
2049 return retval; | |
2050 } | |
2051 | |
2052 ComplexMatrix | |
2053 ComplexMatrix::fsolve (MatrixType &mattype, const ComplexMatrix& b, | |
7788 | 2054 octave_idx_type& info, double& rcon, |
5785 | 2055 solve_singularity_handler sing_handler, |
2056 bool calc_cond) const | |
2057 { | |
2058 ComplexMatrix retval; | |
2059 | |
2060 octave_idx_type nr = rows (); | |
2061 octave_idx_type nc = cols (); | |
2062 | |
6924 | 2063 |
2064 if (nr != nc || nr != b.rows ()) | |
5785 | 2065 (*current_liboctave_error_handler) |
6924 | 2066 ("matrix dimension mismatch solution of linear equations"); |
2067 else if (nr == 0 || b.cols () == 0) | |
2068 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); | |
5785 | 2069 else |
2070 { | |
2071 volatile int typ = mattype.type (); | |
2072 | |
2073 // Calculate the norm of the matrix, for later use. | |
2074 double anorm = -1.; | |
2075 | |
2076 if (typ == MatrixType::Hermitian) | |
2077 { | |
2078 info = 0; | |
2079 char job = 'L'; | |
2080 ComplexMatrix atmp = *this; | |
2081 Complex *tmp_data = atmp.fortran_vec (); | |
2082 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); | |
2083 | |
2084 F77_XFCN (zpotrf, ZPOTRF, (F77_CONST_CHAR_ARG2 (&job, 1), nr, | |
2085 tmp_data, nr, info | |
2086 F77_CHAR_ARG_LEN (1))); | |
2087 | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2088 // Throw-away extra info LAPACK gives so as to not change output. |
7788 | 2089 rcon = 0.0; |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2090 if (info != 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2091 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2092 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2093 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2094 mattype.mark_as_unsymmetric (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2095 typ = MatrixType::Full; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2096 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2097 else |
5785 | 2098 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2099 if (calc_cond) |
5785 | 2100 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2101 Array<Complex> z (2 * nc); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2102 Complex *pz = z.fortran_vec (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2103 Array<double> rz (nc); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2104 double *prz = rz.fortran_vec (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2105 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2106 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
|
2107 nr, tmp_data, nr, anorm, |
7788 | 2108 rcon, pz, prz, info |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2109 F77_CHAR_ARG_LEN (1))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2110 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2111 if (info != 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2112 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2113 |
7788 | 2114 volatile double rcond_plus_one = rcon + 1.0; |
2115 | |
2116 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
|
2117 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2118 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2119 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2120 if (sing_handler) |
7788 | 2121 sing_handler (rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2122 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2123 (*current_liboctave_error_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2124 ("matrix singular to machine precision, rcond = %g", |
7788 | 2125 rcon); |
7482
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 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2129 if (info == 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2130 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2131 retval = b; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2132 Complex *result = retval.fortran_vec (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2133 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2134 octave_idx_type b_nc = b.cols (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2135 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2136 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
|
2137 nr, b_nc, tmp_data, nr, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2138 result, b.rows(), info |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2139 F77_CHAR_ARG_LEN (1))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2140 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2141 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2142 { |
5785 | 2143 mattype.mark_as_unsymmetric (); |
2144 typ = MatrixType::Full; | |
2145 } | |
2146 } | |
2147 } | |
2148 | |
2149 if (typ == MatrixType::Full) | |
2150 { | |
2151 info = 0; | |
2152 | |
2153 Array<octave_idx_type> ipvt (nr); | |
2154 octave_idx_type *pipvt = ipvt.fortran_vec (); | |
2155 | |
2156 ComplexMatrix atmp = *this; | |
2157 Complex *tmp_data = atmp.fortran_vec (); | |
2158 | |
2159 Array<Complex> z (2 * nc); | |
2160 Complex *pz = z.fortran_vec (); | |
2161 Array<double> rz (2 * nc); | |
2162 double *prz = rz.fortran_vec (); | |
2163 | |
2164 // Calculate the norm of the matrix, for later use. | |
2165 if (anorm < 0.) | |
2166 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); | |
2167 | |
2168 F77_XFCN (zgetrf, ZGETRF, (nr, nr, tmp_data, nr, pipvt, info)); | |
2169 | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2170 // Throw-away extra info LAPACK gives so as to not change output. |
7788 | 2171 rcon = 0.0; |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2172 if (info != 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2173 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2174 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2175 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2176 if (sing_handler) |
7788 | 2177 sing_handler (rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2178 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2179 (*current_liboctave_error_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2180 ("matrix singular to machine precision"); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2181 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2182 mattype.mark_as_rectangular (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2183 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2184 else |
5785 | 2185 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2186 if (calc_cond) |
5785 | 2187 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2188 // Now calculate the condition number for |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2189 // non-singular matrix. |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2190 char job = '1'; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2191 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
|
2192 nc, tmp_data, nr, anorm, |
7788 | 2193 rcon, pz, prz, info |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2194 F77_CHAR_ARG_LEN (1))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2195 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2196 if (info != 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2197 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2198 |
7788 | 2199 volatile double rcond_plus_one = rcon + 1.0; |
2200 | |
2201 if (rcond_plus_one == 1.0 || xisnan (rcon)) | |
5785 | 2202 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2203 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2204 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2205 if (sing_handler) |
7788 | 2206 sing_handler (rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2207 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2208 (*current_liboctave_error_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2209 ("matrix singular to machine precision, rcond = %g", |
7788 | 2210 rcon); |
5785 | 2211 } |
2212 } | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2213 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2214 if (info == 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2215 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2216 retval = b; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2217 Complex *result = retval.fortran_vec (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2218 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2219 octave_idx_type b_nc = b.cols (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2220 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2221 char job = 'N'; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2222 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
|
2223 nr, b_nc, tmp_data, nr, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2224 pipvt, result, b.rows(), info |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2225 F77_CHAR_ARG_LEN (1))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2226 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2227 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2228 mattype.mark_as_rectangular (); |
5785 | 2229 } |
2230 } | |
2231 } | |
2232 | |
2233 return retval; | |
2234 } | |
2235 | |
2236 ComplexMatrix | |
2237 ComplexMatrix::solve (MatrixType &typ, const Matrix& b) const | |
2238 { | |
2239 octave_idx_type info; | |
7788 | 2240 double rcon; |
2241 return solve (typ, b, info, rcon, 0); | |
5785 | 2242 } |
2243 | |
2244 ComplexMatrix | |
2245 ComplexMatrix::solve (MatrixType &typ, const Matrix& b, | |
2246 octave_idx_type& info) const | |
2247 { | |
7788 | 2248 double rcon; |
2249 return solve (typ, b, info, rcon, 0); | |
5785 | 2250 } |
2251 | |
2252 ComplexMatrix | |
2253 ComplexMatrix::solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, | |
7788 | 2254 double& rcon) const |
5785 | 2255 { |
7788 | 2256 return solve (typ, b, info, rcon, 0); |
5785 | 2257 } |
2258 | |
2259 ComplexMatrix | |
2260 ComplexMatrix::solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, | |
7788 | 2261 double& rcon, solve_singularity_handler sing_handler, |
5785 | 2262 bool singular_fallback) const |
2263 { | |
2264 ComplexMatrix tmp (b); | |
7788 | 2265 return solve (typ, tmp, info, rcon, sing_handler, singular_fallback); |
5785 | 2266 } |
2267 | |
2268 ComplexMatrix | |
2269 ComplexMatrix::solve (MatrixType &typ, const ComplexMatrix& b) const | |
2270 { | |
2271 octave_idx_type info; | |
7788 | 2272 double rcon; |
2273 return solve (typ, b, info, rcon, 0); | |
5785 | 2274 } |
2275 | |
2276 ComplexMatrix | |
2277 ComplexMatrix::solve (MatrixType &typ, const ComplexMatrix& b, | |
2278 octave_idx_type& info) const | |
2279 { | |
7788 | 2280 double rcon; |
2281 return solve (typ, b, info, rcon, 0); | |
5785 | 2282 } |
2283 | |
2284 ComplexMatrix | |
2285 ComplexMatrix::solve (MatrixType &typ, const ComplexMatrix& b, | |
7788 | 2286 octave_idx_type& info, double& rcon) const |
5785 | 2287 { |
7788 | 2288 return solve (typ, b, info, rcon, 0); |
5785 | 2289 } |
2290 | |
2291 ComplexMatrix | |
2292 ComplexMatrix::solve (MatrixType &mattype, const ComplexMatrix& b, | |
7788 | 2293 octave_idx_type& info, double& rcon, |
5785 | 2294 solve_singularity_handler sing_handler, |
2295 bool singular_fallback) const | |
2296 { | |
2297 ComplexMatrix retval; | |
2298 int typ = mattype.type (); | |
2299 | |
2300 if (typ == MatrixType::Unknown) | |
2301 typ = mattype.type (*this); | |
2302 | |
2303 // Only calculate the condition number for LU/Cholesky | |
2304 if (typ == MatrixType::Upper || typ == MatrixType::Permuted_Upper) | |
7788 | 2305 retval = utsolve (mattype, b, info, rcon, sing_handler, false); |
5785 | 2306 else if (typ == MatrixType::Lower || typ == MatrixType::Permuted_Lower) |
7788 | 2307 retval = ltsolve (mattype, b, info, rcon, sing_handler, false); |
5785 | 2308 else if (typ == MatrixType::Full || typ == MatrixType::Hermitian) |
7788 | 2309 retval = fsolve (mattype, b, info, rcon, sing_handler, true); |
5785 | 2310 else if (typ != MatrixType::Rectangular) |
2311 { | |
2312 (*current_liboctave_error_handler) ("unknown matrix type"); | |
2313 return ComplexMatrix (); | |
2314 } | |
2315 | |
2316 // Rectangular or one of the above solvers flags a singular matrix | |
2317 if (singular_fallback && mattype.type () == MatrixType::Rectangular) | |
2318 { | |
2319 octave_idx_type rank; | |
7788 | 2320 retval = lssolve (b, info, rank, rcon); |
5785 | 2321 } |
2322 | |
2323 return retval; | |
2324 } | |
2325 | |
2326 ComplexColumnVector | |
2327 ComplexMatrix::solve (MatrixType &typ, const ColumnVector& b) const | |
2328 { | |
2329 octave_idx_type info; | |
7788 | 2330 double rcon; |
2331 return solve (typ, ComplexColumnVector (b), info, rcon, 0); | |
5785 | 2332 } |
2333 | |
2334 ComplexColumnVector | |
2335 ComplexMatrix::solve (MatrixType &typ, const ColumnVector& b, | |
2336 octave_idx_type& info) const | |
2337 { | |
7788 | 2338 double rcon; |
2339 return solve (typ, ComplexColumnVector (b), info, rcon, 0); | |
5785 | 2340 } |
2341 | |
2342 ComplexColumnVector | |
2343 ComplexMatrix::solve (MatrixType &typ, const ColumnVector& b, | |
7788 | 2344 octave_idx_type& info, double& rcon) const |
5785 | 2345 { |
7788 | 2346 return solve (typ, ComplexColumnVector (b), info, rcon, 0); |
5785 | 2347 } |
2348 | |
2349 ComplexColumnVector | |
2350 ComplexMatrix::solve (MatrixType &typ, const ColumnVector& b, | |
7788 | 2351 octave_idx_type& info, double& rcon, |
5785 | 2352 solve_singularity_handler sing_handler) const |
2353 { | |
7788 | 2354 return solve (typ, ComplexColumnVector (b), info, rcon, sing_handler); |
5785 | 2355 } |
2356 | |
2357 ComplexColumnVector | |
2358 ComplexMatrix::solve (MatrixType &typ, const ComplexColumnVector& b) const | |
2359 { | |
2360 octave_idx_type info; | |
7788 | 2361 double rcon; |
2362 return solve (typ, b, info, rcon, 0); | |
5785 | 2363 } |
2364 | |
2365 ComplexColumnVector | |
2366 ComplexMatrix::solve (MatrixType &typ, const ComplexColumnVector& b, | |
2367 octave_idx_type& info) const | |
2368 { | |
7788 | 2369 double rcon; |
2370 return solve (typ, b, info, rcon, 0); | |
5785 | 2371 } |
2372 | |
2373 ComplexColumnVector | |
2374 ComplexMatrix::solve (MatrixType &typ, const ComplexColumnVector& b, | |
7788 | 2375 octave_idx_type& info, double& rcon) const |
5785 | 2376 { |
7788 | 2377 return solve (typ, b, info, rcon, 0); |
5785 | 2378 } |
2379 | |
2380 ComplexColumnVector | |
2381 ComplexMatrix::solve (MatrixType &typ, const ComplexColumnVector& b, | |
7788 | 2382 octave_idx_type& info, double& rcon, |
5785 | 2383 solve_singularity_handler sing_handler) const |
2384 { | |
2385 | |
2386 ComplexMatrix tmp (b); | |
7788 | 2387 return solve (typ, tmp, info, rcon, sing_handler).column(static_cast<octave_idx_type> (0)); |
5785 | 2388 } |
2389 | |
2390 ComplexMatrix | |
458 | 2391 ComplexMatrix::solve (const Matrix& b) const |
2392 { | |
5275 | 2393 octave_idx_type info; |
7788 | 2394 double rcon; |
2395 return solve (b, info, rcon, 0); | |
458 | 2396 } |
2397 | |
2398 ComplexMatrix | |
5275 | 2399 ComplexMatrix::solve (const Matrix& b, octave_idx_type& info) const |
458 | 2400 { |
7788 | 2401 double rcon; |
2402 return solve (b, info, rcon, 0); | |
458 | 2403 } |
2404 | |
2405 ComplexMatrix | |
7788 | 2406 ComplexMatrix::solve (const Matrix& b, octave_idx_type& info, double& rcon) const |
458 | 2407 { |
7788 | 2408 return solve (b, info, rcon, 0); |
3480 | 2409 } |
2410 | |
2411 ComplexMatrix | |
7788 | 2412 ComplexMatrix::solve (const Matrix& b, octave_idx_type& info, double& rcon, |
3480 | 2413 solve_singularity_handler sing_handler) const |
2414 { | |
458 | 2415 ComplexMatrix tmp (b); |
7788 | 2416 return solve (tmp, info, rcon, sing_handler); |
458 | 2417 } |
2418 | |
2419 ComplexMatrix | |
2420 ComplexMatrix::solve (const ComplexMatrix& b) const | |
2421 { | |
5275 | 2422 octave_idx_type info; |
7788 | 2423 double rcon; |
2424 return solve (b, info, rcon, 0); | |
458 | 2425 } |
2426 | |
2427 ComplexMatrix | |
5275 | 2428 ComplexMatrix::solve (const ComplexMatrix& b, octave_idx_type& info) const |
458 | 2429 { |
7788 | 2430 double rcon; |
2431 return solve (b, info, rcon, 0); | |
458 | 2432 } |
3480 | 2433 |
458 | 2434 ComplexMatrix |
7788 | 2435 ComplexMatrix::solve (const ComplexMatrix& b, octave_idx_type& info, double& rcon) const |
458 | 2436 { |
7788 | 2437 return solve (b, info, rcon, 0); |
3480 | 2438 } |
2439 | |
2440 ComplexMatrix | |
7788 | 2441 ComplexMatrix::solve (const ComplexMatrix& b, octave_idx_type& info, double& rcon, |
3480 | 2442 solve_singularity_handler sing_handler) const |
2443 { | |
5785 | 2444 MatrixType mattype (*this); |
7788 | 2445 return solve (mattype, b, info, rcon, sing_handler); |
458 | 2446 } |
2447 | |
2448 ComplexColumnVector | |
3585 | 2449 ComplexMatrix::solve (const ColumnVector& b) const |
2450 { | |
5275 | 2451 octave_idx_type info; |
7788 | 2452 double rcon; |
2453 return solve (ComplexColumnVector (b), info, rcon, 0); | |
3585 | 2454 } |
2455 | |
2456 ComplexColumnVector | |
5275 | 2457 ComplexMatrix::solve (const ColumnVector& b, octave_idx_type& info) const |
3585 | 2458 { |
7788 | 2459 double rcon; |
2460 return solve (ComplexColumnVector (b), info, rcon, 0); | |
3585 | 2461 } |
2462 | |
2463 ComplexColumnVector | |
5785 | 2464 ComplexMatrix::solve (const ColumnVector& b, octave_idx_type& info, |
7788 | 2465 double& rcon) const |
3585 | 2466 { |
7788 | 2467 return solve (ComplexColumnVector (b), info, rcon, 0); |
3585 | 2468 } |
2469 | |
2470 ComplexColumnVector | |
5785 | 2471 ComplexMatrix::solve (const ColumnVector& b, octave_idx_type& info, |
7788 | 2472 double& rcon, |
3585 | 2473 solve_singularity_handler sing_handler) const |
2474 { | |
7788 | 2475 return solve (ComplexColumnVector (b), info, rcon, sing_handler); |
3585 | 2476 } |
2477 | |
2478 ComplexColumnVector | |
458 | 2479 ComplexMatrix::solve (const ComplexColumnVector& b) const |
2480 { | |
5275 | 2481 octave_idx_type info; |
7788 | 2482 double rcon; |
2483 return solve (b, info, rcon, 0); | |
458 | 2484 } |
2485 | |
2486 ComplexColumnVector | |
5275 | 2487 ComplexMatrix::solve (const ComplexColumnVector& b, octave_idx_type& info) const |
458 | 2488 { |
7788 | 2489 double rcon; |
2490 return solve (b, info, rcon, 0); | |
458 | 2491 } |
2492 | |
2493 ComplexColumnVector | |
5275 | 2494 ComplexMatrix::solve (const ComplexColumnVector& b, octave_idx_type& info, |
7788 | 2495 double& rcon) const |
458 | 2496 { |
7788 | 2497 return solve (b, info, rcon, 0); |
3480 | 2498 } |
2499 | |
2500 ComplexColumnVector | |
5275 | 2501 ComplexMatrix::solve (const ComplexColumnVector& b, octave_idx_type& info, |
7788 | 2502 double& rcon, |
3480 | 2503 solve_singularity_handler sing_handler) const |
2504 { | |
5785 | 2505 MatrixType mattype (*this); |
7788 | 2506 return solve (mattype, b, info, rcon, sing_handler); |
458 | 2507 } |
2508 | |
2509 ComplexMatrix | |
3585 | 2510 ComplexMatrix::lssolve (const Matrix& b) const |
2511 { | |
5275 | 2512 octave_idx_type info; |
2513 octave_idx_type rank; | |
7788 | 2514 double rcon; |
2515 return lssolve (ComplexMatrix (b), info, rank, rcon); | |
3585 | 2516 } |
2517 | |
2518 ComplexMatrix | |
5275 | 2519 ComplexMatrix::lssolve (const Matrix& b, octave_idx_type& info) const |
3585 | 2520 { |
5275 | 2521 octave_idx_type rank; |
7788 | 2522 double rcon; |
2523 return lssolve (ComplexMatrix (b), info, rank, rcon); | |
3585 | 2524 } |
2525 | |
2526 ComplexMatrix | |
7076 | 2527 ComplexMatrix::lssolve (const Matrix& b, octave_idx_type& info, |
2528 octave_idx_type& rank) const | |
3585 | 2529 { |
7788 | 2530 double rcon; |
2531 return lssolve (ComplexMatrix (b), info, rank, rcon); | |
7076 | 2532 } |
2533 | |
2534 ComplexMatrix | |
2535 ComplexMatrix::lssolve (const Matrix& b, octave_idx_type& info, | |
7788 | 2536 octave_idx_type& rank, double& rcon) const |
7076 | 2537 { |
7788 | 2538 return lssolve (ComplexMatrix (b), info, rank, rcon); |
3585 | 2539 } |
2540 | |
2541 ComplexMatrix | |
458 | 2542 ComplexMatrix::lssolve (const ComplexMatrix& b) const |
2543 { | |
5275 | 2544 octave_idx_type info; |
2545 octave_idx_type rank; | |
7788 | 2546 double rcon; |
2547 return lssolve (b, info, rank, rcon); | |
458 | 2548 } |
2549 | |
2550 ComplexMatrix | |
5275 | 2551 ComplexMatrix::lssolve (const ComplexMatrix& b, octave_idx_type& info) const |
458 | 2552 { |
5275 | 2553 octave_idx_type rank; |
7788 | 2554 double rcon; |
2555 return lssolve (b, info, rank, rcon); | |
458 | 2556 } |
2557 | |
2558 ComplexMatrix | |
7076 | 2559 ComplexMatrix::lssolve (const ComplexMatrix& b, octave_idx_type& info, |
2560 octave_idx_type& rank) const | |
2561 { | |
7788 | 2562 double rcon; |
2563 return lssolve (b, info, rank, rcon); | |
7076 | 2564 } |
2565 | |
2566 ComplexMatrix | |
2567 ComplexMatrix::lssolve (const ComplexMatrix& b, octave_idx_type& info, | |
7788 | 2568 octave_idx_type& rank, double& rcon) const |
458 | 2569 { |
1948 | 2570 ComplexMatrix retval; |
2571 | |
5275 | 2572 octave_idx_type nrhs = b.cols (); |
2573 | |
2574 octave_idx_type m = rows (); | |
2575 octave_idx_type n = cols (); | |
458 | 2576 |
6924 | 2577 if (m != b.rows ()) |
1948 | 2578 (*current_liboctave_error_handler) |
2579 ("matrix dimension mismatch solution of linear equations"); | |
6924 | 2580 else if (m== 0 || n == 0 || b.cols () == 0) |
2581 retval = ComplexMatrix (n, b.cols (), Complex (0.0, 0.0)); | |
1948 | 2582 else |
458 | 2583 { |
7072 | 2584 volatile octave_idx_type minmn = (m < n ? m : n); |
2585 octave_idx_type maxmn = m > n ? m : n; | |
7788 | 2586 rcon = -1.0; |
7072 | 2587 |
2588 if (m != n) | |
2589 { | |
2590 retval = ComplexMatrix (maxmn, nrhs); | |
2591 | |
2592 for (octave_idx_type j = 0; j < nrhs; j++) | |
2593 for (octave_idx_type i = 0; i < m; i++) | |
2594 retval.elem (i, j) = b.elem (i, j); | |
2595 } | |
2596 else | |
2597 retval = b; | |
2598 | |
1948 | 2599 ComplexMatrix atmp = *this; |
2600 Complex *tmp_data = atmp.fortran_vec (); | |
2601 | |
7072 | 2602 Complex *pretval = retval.fortran_vec (); |
2603 Array<double> s (minmn); | |
7071 | 2604 double *ps = s.fortran_vec (); |
2563 | 2605 |
7072 | 2606 // Ask ZGELSD what the dimension of WORK should be. |
5275 | 2607 octave_idx_type lwork = -1; |
3752 | 2608 |
2609 Array<Complex> work (1); | |
7079 | 2610 |
7477 | 2611 octave_idx_type smlsiz; |
2612 F77_FUNC (xilaenv, XILAENV) (9, F77_CONST_CHAR_ARG2 ("ZGELSD", 6), | |
2613 F77_CONST_CHAR_ARG2 (" ", 1), | |
7478 | 2614 0, 0, 0, 0, smlsiz |
7477 | 2615 F77_CHAR_ARG_LEN (6) |
7478 | 2616 F77_CHAR_ARG_LEN (1)); |
7079 | 2617 |
7486
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2618 octave_idx_type mnthr; |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2619 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
|
2620 F77_CONST_CHAR_ARG2 (" ", 1), |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2621 m, n, nrhs, -1, mnthr |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2622 F77_CHAR_ARG_LEN (6) |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2623 F77_CHAR_ARG_LEN (1)); |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2624 |
7079 | 2625 // We compute the size of rwork and iwork because ZGELSD in |
2626 // older versions of LAPACK does not return them on a query | |
2627 // call. | |
7124 | 2628 double dminmn = static_cast<double> (minmn); |
2629 double dsmlsizp1 = static_cast<double> (smlsiz+1); | |
7079 | 2630 #if defined (HAVE_LOG2) |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2631 double tmp = log2 (dminmn / dsmlsizp1); |
7079 | 2632 #else |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2633 double tmp = log (dminmn / dsmlsizp1) / log (2.0); |
7079 | 2634 #endif |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2635 octave_idx_type nlvl = static_cast<octave_idx_type> (tmp) + 1; |
7079 | 2636 if (nlvl < 0) |
2637 nlvl = 0; | |
2638 | |
2639 octave_idx_type lrwork = minmn*(10 + 2*smlsiz + 8*nlvl) | |
2640 + 3*smlsiz*nrhs + (smlsiz+1)*(smlsiz+1); | |
2641 if (lrwork < 1) | |
2642 lrwork = 1; | |
2643 Array<double> rwork (lrwork); | |
2644 double *prwork = rwork.fortran_vec (); | |
2645 | |
2646 octave_idx_type liwork = 3 * minmn * nlvl + 11 * minmn; | |
2647 if (liwork < 1) | |
2648 liwork = 1; | |
2649 Array<octave_idx_type> iwork (liwork); | |
2650 octave_idx_type* piwork = iwork.fortran_vec (); | |
7072 | 2651 |
2652 F77_XFCN (zgelsd, ZGELSD, (m, n, nrhs, tmp_data, m, pretval, maxmn, | |
7788 | 2653 ps, rcon, rank, work.fortran_vec (), |
7079 | 2654 lwork, prwork, piwork, info)); |
1948 | 2655 |
7476 | 2656 // 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
|
2657 // 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
|
2658 // should provide sufficient workspace for ZGELSD to operate |
7476 | 2659 // efficiently. |
7488
6470f946a425
another small xGELSD workspace fix
John W. Eaton <jwe@octave.org>
parents:
7486
diff
changeset
|
2660 if (n >= mnthr) |
7476 | 2661 { |
2662 octave_idx_type addend = m; | |
2663 | |
2664 if (2*m-4 > addend) | |
2665 addend = 2*m-4; | |
2666 | |
2667 if (nrhs > addend) | |
2668 addend = nrhs; | |
2669 | |
2670 if (n-3*m > addend) | |
2671 addend = n-3*m; | |
2672 | |
2673 const octave_idx_type lworkaround = 4*m + m*m + addend; | |
2674 | |
2675 if (std::real (work(0)) < lworkaround) | |
2676 work(0) = lworkaround; | |
2677 } | |
7532
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2678 else if (m >= n) |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2679 { |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2680 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
|
2681 |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2682 if (std::real (work(0)) < lworkaround) |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2683 work(0) = lworkaround; |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2684 } |
7476 | 2685 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2686 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
|
2687 work.resize (lwork); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2688 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2689 F77_XFCN (zgelsd, ZGELSD, (m, n, nrhs, tmp_data, m, pretval, |
7788 | 2690 maxmn, ps, rcon, rank, |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2691 work.fortran_vec (), lwork, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2692 prwork, piwork, info)); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2693 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2694 if (rank < minmn) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2695 (*current_liboctave_warning_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2696 ("zgelsd: rank deficient %dx%d matrix, rank = %d, tol = %e", |
7788 | 2697 m, n, rank, rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2698 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2699 if (s.elem (0) == 0.0) |
7788 | 2700 rcon = 0.0; |
1948 | 2701 else |
7788 | 2702 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
|
2703 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2704 retval.resize (n, nrhs); |
458 | 2705 } |
2706 | |
2707 return retval; | |
2708 } | |
2709 | |
2710 ComplexColumnVector | |
3585 | 2711 ComplexMatrix::lssolve (const ColumnVector& b) const |
2712 { | |
5275 | 2713 octave_idx_type info; |
2714 octave_idx_type rank; | |
7788 | 2715 double rcon; |
2716 return lssolve (ComplexColumnVector (b), info, rank, rcon); | |
3585 | 2717 } |
2718 | |
2719 ComplexColumnVector | |
5275 | 2720 ComplexMatrix::lssolve (const ColumnVector& b, octave_idx_type& info) const |
3585 | 2721 { |
5275 | 2722 octave_idx_type rank; |
7788 | 2723 double rcon; |
2724 return lssolve (ComplexColumnVector (b), info, rank, rcon); | |
3585 | 2725 } |
2726 | |
2727 ComplexColumnVector | |
7076 | 2728 ComplexMatrix::lssolve (const ColumnVector& b, octave_idx_type& info, |
2729 octave_idx_type& rank) const | |
3585 | 2730 { |
7788 | 2731 double rcon; |
2732 return lssolve (ComplexColumnVector (b), info, rank, rcon); | |
7076 | 2733 } |
2734 | |
2735 ComplexColumnVector | |
2736 ComplexMatrix::lssolve (const ColumnVector& b, octave_idx_type& info, | |
7788 | 2737 octave_idx_type& rank, double& rcon) const |
7076 | 2738 { |
7788 | 2739 return lssolve (ComplexColumnVector (b), info, rank, rcon); |
3585 | 2740 } |
2741 | |
2742 ComplexColumnVector | |
458 | 2743 ComplexMatrix::lssolve (const ComplexColumnVector& b) const |
2744 { | |
5275 | 2745 octave_idx_type info; |
2746 octave_idx_type rank; | |
7788 | 2747 double rcon; |
2748 return lssolve (b, info, rank, rcon); | |
458 | 2749 } |
2750 | |
2751 ComplexColumnVector | |
5275 | 2752 ComplexMatrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info) const |
458 | 2753 { |
5275 | 2754 octave_idx_type rank; |
7788 | 2755 double rcon; |
2756 return lssolve (b, info, rank, rcon); | |
458 | 2757 } |
2758 | |
2759 ComplexColumnVector | |
5275 | 2760 ComplexMatrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info, |
2761 octave_idx_type& rank) const | |
458 | 2762 { |
7788 | 2763 double rcon; |
2764 return lssolve (b, info, rank, rcon); | |
7076 | 2765 |
2766 } | |
2767 | |
2768 ComplexColumnVector | |
2769 ComplexMatrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info, | |
7788 | 2770 octave_idx_type& rank, double& rcon) const |
7076 | 2771 { |
1948 | 2772 ComplexColumnVector retval; |
2773 | |
5275 | 2774 octave_idx_type nrhs = 1; |
2775 | |
2776 octave_idx_type m = rows (); | |
2777 octave_idx_type n = cols (); | |
458 | 2778 |
6924 | 2779 if (m != b.length ()) |
1948 | 2780 (*current_liboctave_error_handler) |
6924 | 2781 ("matrix dimension mismatch solution of linear equations"); |
2782 else if (m == 0 || n == 0 || b.cols () == 0) | |
2783 retval = ComplexColumnVector (n, Complex (0.0, 0.0)); | |
1948 | 2784 else |
458 | 2785 { |
7072 | 2786 volatile octave_idx_type minmn = (m < n ? m : n); |
2787 octave_idx_type maxmn = m > n ? m : n; | |
7788 | 2788 rcon = -1.0; |
7072 | 2789 |
2790 if (m != n) | |
2791 { | |
2792 retval = ComplexColumnVector (maxmn); | |
2793 | |
2794 for (octave_idx_type i = 0; i < m; i++) | |
2795 retval.elem (i) = b.elem (i); | |
2796 } | |
2797 else | |
2798 retval = b; | |
2799 | |
1948 | 2800 ComplexMatrix atmp = *this; |
2801 Complex *tmp_data = atmp.fortran_vec (); | |
2802 | |
7072 | 2803 Complex *pretval = retval.fortran_vec (); |
2804 Array<double> s (minmn); | |
7071 | 2805 double *ps = s.fortran_vec (); |
1948 | 2806 |
7072 | 2807 // Ask ZGELSD what the dimension of WORK should be. |
5275 | 2808 octave_idx_type lwork = -1; |
3752 | 2809 |
2810 Array<Complex> work (1); | |
7079 | 2811 |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2812 octave_idx_type smlsiz; |
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2813 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
|
2814 F77_CONST_CHAR_ARG2 (" ", 1), |
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2815 0, 0, 0, 0, smlsiz |
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2816 F77_CHAR_ARG_LEN (6) |
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2817 F77_CHAR_ARG_LEN (1)); |
7079 | 2818 |
2819 // We compute the size of rwork and iwork because ZGELSD in | |
2820 // older versions of LAPACK does not return them on a query | |
2821 // call. | |
7124 | 2822 double dminmn = static_cast<double> (minmn); |
2823 double dsmlsizp1 = static_cast<double> (smlsiz+1); | |
7079 | 2824 #if defined (HAVE_LOG2) |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2825 double tmp = log2 (dminmn / dsmlsizp1); |
7079 | 2826 #else |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2827 double tmp = log (dminmn / dsmlsizp1) / log (2.0); |
7079 | 2828 #endif |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2829 octave_idx_type nlvl = static_cast<octave_idx_type> (tmp) + 1; |
7079 | 2830 if (nlvl < 0) |
2831 nlvl = 0; | |
2832 | |
2833 octave_idx_type lrwork = minmn*(10 + 2*smlsiz + 8*nlvl) | |
2834 + 3*smlsiz*nrhs + (smlsiz+1)*(smlsiz+1); | |
2835 if (lrwork < 1) | |
2836 lrwork = 1; | |
2837 Array<double> rwork (lrwork); | |
2838 double *prwork = rwork.fortran_vec (); | |
2839 | |
2840 octave_idx_type liwork = 3 * minmn * nlvl + 11 * minmn; | |
2841 if (liwork < 1) | |
2842 liwork = 1; | |
2843 Array<octave_idx_type> iwork (liwork); | |
2844 octave_idx_type* piwork = iwork.fortran_vec (); | |
7072 | 2845 |
2846 F77_XFCN (zgelsd, ZGELSD, (m, n, nrhs, tmp_data, m, pretval, maxmn, | |
7788 | 2847 ps, rcon, rank, work.fortran_vec (), |
7079 | 2848 lwork, prwork, piwork, info)); |
1948 | 2849 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2850 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
|
2851 work.resize (lwork); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2852 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
|
2853 iwork.resize (iwork(0)); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2854 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2855 F77_XFCN (zgelsd, ZGELSD, (m, n, nrhs, tmp_data, m, pretval, |
7788 | 2856 maxmn, ps, rcon, rank, |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2857 work.fortran_vec (), lwork, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2858 prwork, piwork, info)); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2859 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2860 if (rank < minmn) |
1948 | 2861 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2862 if (rank < minmn) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2863 (*current_liboctave_warning_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2864 ("zgelsd: rank deficient %dx%d matrix, rank = %d, tol = %e", |
7788 | 2865 m, n, rank, rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2866 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2867 if (s.elem (0) == 0.0) |
7788 | 2868 rcon = 0.0; |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2869 else |
7788 | 2870 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
|
2871 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2872 retval.resize (n, nrhs); |
1948 | 2873 } |
458 | 2874 } |
2875 | |
2876 return retval; | |
2877 } | |
2878 | |
1205 | 2879 // column vector by row vector -> matrix operations |
2880 | |
2881 ComplexMatrix | |
2882 operator * (const ColumnVector& v, const ComplexRowVector& a) | |
2883 { | |
2884 ComplexColumnVector tmp (v); | |
2885 return tmp * a; | |
2886 } | |
2887 | |
2888 ComplexMatrix | |
2889 operator * (const ComplexColumnVector& a, const RowVector& b) | |
2890 { | |
2891 ComplexRowVector tmp (b); | |
2892 return a * tmp; | |
2893 } | |
2894 | |
2895 ComplexMatrix | |
2896 operator * (const ComplexColumnVector& v, const ComplexRowVector& a) | |
2897 { | |
1948 | 2898 ComplexMatrix retval; |
2899 | |
5275 | 2900 octave_idx_type len = v.length (); |
3233 | 2901 |
2902 if (len != 0) | |
1205 | 2903 { |
5275 | 2904 octave_idx_type a_len = a.length (); |
3233 | 2905 |
2906 retval.resize (len, a_len); | |
2907 Complex *c = retval.fortran_vec (); | |
2908 | |
4552 | 2909 F77_XFCN (zgemm, ZGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
2910 F77_CONST_CHAR_ARG2 ("N", 1), | |
2911 len, a_len, 1, 1.0, v.data (), len, | |
2912 a.data (), 1, 0.0, c, len | |
2913 F77_CHAR_ARG_LEN (1) | |
2914 F77_CHAR_ARG_LEN (1))); | |
1205 | 2915 } |
2916 | |
1948 | 2917 return retval; |
1205 | 2918 } |
2919 | |
458 | 2920 // matrix by diagonal matrix -> matrix operations |
2921 | |
2922 ComplexMatrix& | |
2923 ComplexMatrix::operator += (const DiagMatrix& a) | |
2924 { | |
5275 | 2925 octave_idx_type nr = rows (); |
2926 octave_idx_type nc = cols (); | |
2927 | |
2928 octave_idx_type a_nr = rows (); | |
2929 octave_idx_type a_nc = cols (); | |
2384 | 2930 |
2931 if (nr != a_nr || nc != a_nc) | |
458 | 2932 { |
2384 | 2933 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889 | 2934 return *this; |
458 | 2935 } |
2936 | |
5275 | 2937 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 2938 elem (i, i) += a.elem (i, i); |
2939 | |
2940 return *this; | |
2941 } | |
2942 | |
2943 ComplexMatrix& | |
2944 ComplexMatrix::operator -= (const DiagMatrix& a) | |
2945 { | |
5275 | 2946 octave_idx_type nr = rows (); |
2947 octave_idx_type nc = cols (); | |
2948 | |
2949 octave_idx_type a_nr = rows (); | |
2950 octave_idx_type a_nc = cols (); | |
2384 | 2951 |
2952 if (nr != a_nr || nc != a_nc) | |
458 | 2953 { |
2384 | 2954 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889 | 2955 return *this; |
458 | 2956 } |
2957 | |
5275 | 2958 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 2959 elem (i, i) -= a.elem (i, i); |
2960 | |
2961 return *this; | |
2962 } | |
2963 | |
2964 ComplexMatrix& | |
2965 ComplexMatrix::operator += (const ComplexDiagMatrix& a) | |
2966 { | |
5275 | 2967 octave_idx_type nr = rows (); |
2968 octave_idx_type nc = cols (); | |
2969 | |
2970 octave_idx_type a_nr = rows (); | |
2971 octave_idx_type a_nc = cols (); | |
2384 | 2972 |
2973 if (nr != a_nr || nc != a_nc) | |
458 | 2974 { |
2384 | 2975 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
889 | 2976 return *this; |
458 | 2977 } |
2978 | |
5275 | 2979 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 2980 elem (i, i) += a.elem (i, i); |
2981 | |
2982 return *this; | |
2983 } | |
2984 | |
2985 ComplexMatrix& | |
2986 ComplexMatrix::operator -= (const ComplexDiagMatrix& a) | |
2987 { | |
5275 | 2988 octave_idx_type nr = rows (); |
2989 octave_idx_type nc = cols (); | |
2990 | |
2991 octave_idx_type a_nr = rows (); | |
2992 octave_idx_type a_nc = cols (); | |
2384 | 2993 |
2994 if (nr != a_nr || nc != a_nc) | |
458 | 2995 { |
2384 | 2996 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
889 | 2997 return *this; |
458 | 2998 } |
2999 | |
5275 | 3000 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 3001 elem (i, i) -= a.elem (i, i); |
3002 | |
3003 return *this; | |
3004 } | |
3005 | |
3006 // matrix by matrix -> matrix operations | |
3007 | |
3008 ComplexMatrix& | |
3009 ComplexMatrix::operator += (const Matrix& a) | |
3010 { | |
5275 | 3011 octave_idx_type nr = rows (); |
3012 octave_idx_type nc = cols (); | |
3013 | |
3014 octave_idx_type a_nr = a.rows (); | |
3015 octave_idx_type a_nc = a.cols (); | |
2384 | 3016 |
3017 if (nr != a_nr || nc != a_nc) | |
458 | 3018 { |
2384 | 3019 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458 | 3020 return *this; |
3021 } | |
3022 | |
3023 if (nr == 0 || nc == 0) | |
3024 return *this; | |
3025 | |
3026 Complex *d = fortran_vec (); // Ensures only one reference to my privates! | |
3027 | |
3769 | 3028 mx_inline_add2 (d, a.data (), length ()); |
458 | 3029 return *this; |
3030 } | |
3031 | |
3032 ComplexMatrix& | |
3033 ComplexMatrix::operator -= (const Matrix& a) | |
3034 { | |
5275 | 3035 octave_idx_type nr = rows (); |
3036 octave_idx_type nc = cols (); | |
3037 | |
3038 octave_idx_type a_nr = a.rows (); | |
3039 octave_idx_type a_nc = a.cols (); | |
2384 | 3040 |
3041 if (nr != a_nr || nc != a_nc) | |
458 | 3042 { |
2384 | 3043 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458 | 3044 return *this; |
3045 } | |
3046 | |
3047 if (nr == 0 || nc == 0) | |
3048 return *this; | |
3049 | |
3050 Complex *d = fortran_vec (); // Ensures only one reference to my privates! | |
3051 | |
3769 | 3052 mx_inline_subtract2 (d, a.data (), length ()); |
458 | 3053 return *this; |
3054 } | |
3055 | |
3056 // unary operations | |
3057 | |
2964 | 3058 boolMatrix |
458 | 3059 ComplexMatrix::operator ! (void) const |
3060 { | |
5275 | 3061 octave_idx_type nr = rows (); |
3062 octave_idx_type nc = cols (); | |
2964 | 3063 |
3064 boolMatrix b (nr, nc); | |
3065 | |
5275 | 3066 for (octave_idx_type j = 0; j < nc; j++) |
3067 for (octave_idx_type i = 0; i < nr; i++) | |
5139 | 3068 b.elem (i, j) = elem (i, j) == 0.0; |
2964 | 3069 |
3070 return b; | |
458 | 3071 } |
3072 | |
3073 // other operations | |
3074 | |
2676 | 3075 Matrix |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3076 ComplexMatrix::map (dmapper fcn) const |
458 | 3077 { |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3078 return MArray2<Complex>::map<double> (func_ptr (fcn)); |
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 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3081 ComplexMatrix |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3082 ComplexMatrix::map (cmapper fcn) const |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3083 { |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3084 return MArray2<Complex>::map<Complex> (func_ptr (fcn)); |
3248 | 3085 } |
3086 | |
3087 boolMatrix | |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3088 ComplexMatrix::map (bmapper fcn) const |
3248 | 3089 { |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
3090 return MArray2<Complex>::map<bool> (func_ptr (fcn)); |
458 | 3091 } |
3092 | |
2384 | 3093 bool |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3094 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
|
3095 { |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3096 octave_idx_type nr = rows (); |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3097 octave_idx_type nc = cols (); |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3098 |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3099 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
|
3100 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
|
3101 { |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3102 Complex val = elem (i, j); |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3103 if (xisnan (val)) |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3104 return true; |
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 |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3107 return false; |
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 |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7803
diff
changeset
|
3110 bool |
2384 | 3111 ComplexMatrix::any_element_is_inf_or_nan (void) const |
3112 { | |
5275 | 3113 octave_idx_type nr = rows (); |
3114 octave_idx_type nc = cols (); | |
3115 | |
3116 for (octave_idx_type j = 0; j < nc; j++) | |
3117 for (octave_idx_type i = 0; i < nr; i++) | |
2384 | 3118 { |
3119 Complex val = elem (i, j); | |
3120 if (xisinf (val) || xisnan (val)) | |
3121 return true; | |
3122 } | |
3123 | |
3124 return false; | |
3125 } | |
3126 | |
2408 | 3127 // Return true if no elements have imaginary components. |
3128 | |
3129 bool | |
3130 ComplexMatrix::all_elements_are_real (void) const | |
3131 { | |
5275 | 3132 octave_idx_type nr = rows (); |
3133 octave_idx_type nc = cols (); | |
3134 | |
3135 for (octave_idx_type j = 0; j < nc; j++) | |
4349 | 3136 { |
5275 | 3137 for (octave_idx_type i = 0; i < nr; i++) |
4349 | 3138 { |
5315 | 3139 double ip = std::imag (elem (i, j)); |
4349 | 3140 |
3141 if (ip != 0.0 || lo_ieee_signbit (ip)) | |
3142 return false; | |
3143 } | |
3144 } | |
2408 | 3145 |
3146 return true; | |
3147 } | |
3148 | |
1968 | 3149 // Return nonzero if any element of CM has a non-integer real or |
3150 // imaginary part. Also extract the largest and smallest (real or | |
3151 // imaginary) values and return them in MAX_VAL and MIN_VAL. | |
3152 | |
2384 | 3153 bool |
1968 | 3154 ComplexMatrix::all_integers (double& max_val, double& min_val) const |
3155 { | |
5275 | 3156 octave_idx_type nr = rows (); |
3157 octave_idx_type nc = cols (); | |
1968 | 3158 |
3159 if (nr > 0 && nc > 0) | |
3160 { | |
3161 Complex val = elem (0, 0); | |
3162 | |
5315 | 3163 double r_val = std::real (val); |
3164 double i_val = std::imag (val); | |
1968 | 3165 |
3166 max_val = r_val; | |
3167 min_val = r_val; | |
3168 | |
3169 if (i_val > max_val) | |
3170 max_val = i_val; | |
3171 | |
3172 if (i_val < max_val) | |
3173 min_val = i_val; | |
3174 } | |
3175 else | |
2384 | 3176 return false; |
1968 | 3177 |
5275 | 3178 for (octave_idx_type j = 0; j < nc; j++) |
3179 for (octave_idx_type i = 0; i < nr; i++) | |
1968 | 3180 { |
3181 Complex val = elem (i, j); | |
3182 | |
5315 | 3183 double r_val = std::real (val); |
3184 double i_val = std::imag (val); | |
1968 | 3185 |
3186 if (r_val > max_val) | |
3187 max_val = r_val; | |
3188 | |
3189 if (i_val > max_val) | |
3190 max_val = i_val; | |
3191 | |
3192 if (r_val < min_val) | |
3193 min_val = r_val; | |
3194 | |
3195 if (i_val < min_val) | |
3196 min_val = i_val; | |
3197 | |
3198 if (D_NINT (r_val) != r_val || D_NINT (i_val) != i_val) | |
2384 | 3199 return false; |
1968 | 3200 } |
2384 | 3201 |
3202 return true; | |
1968 | 3203 } |
3204 | |
2384 | 3205 bool |
1968 | 3206 ComplexMatrix::too_large_for_float (void) const |
3207 { | |
5275 | 3208 octave_idx_type nr = rows (); |
3209 octave_idx_type nc = cols (); | |
3210 | |
3211 for (octave_idx_type j = 0; j < nc; j++) | |
3212 for (octave_idx_type i = 0; i < nr; i++) | |
1968 | 3213 { |
3214 Complex val = elem (i, j); | |
3215 | |
5315 | 3216 double r_val = std::real (val); |
3217 double i_val = std::imag (val); | |
1968 | 3218 |
5389 | 3219 if ((! (xisnan (r_val) || xisinf (r_val)) |
5387 | 3220 && fabs (r_val) > FLT_MAX) |
5389 | 3221 || (! (xisnan (i_val) || xisinf (i_val)) |
5387 | 3222 && fabs (i_val) > FLT_MAX)) |
2384 | 3223 return true; |
1968 | 3224 } |
3225 | |
2384 | 3226 return false; |
1968 | 3227 } |
3228 | |
5775 | 3229 // FIXME Do these really belong here? Maybe they should be |
4015 | 3230 // in a base class? |
3231 | |
2832 | 3232 boolMatrix |
4015 | 3233 ComplexMatrix::all (int dim) const |
458 | 3234 { |
8743
1bd918cfb6e2
reimplement any & all using the new reduction code
Jaroslav Hajek <highegg@gmail.com>
parents:
8736
diff
changeset
|
3235 return do_mx_red_op<boolMatrix> (*this, dim, mx_inline_all); |
458 | 3236 } |
3237 | |
2832 | 3238 boolMatrix |
4015 | 3239 ComplexMatrix::any (int dim) const |
458 | 3240 { |
8743
1bd918cfb6e2
reimplement any & all using the new reduction code
Jaroslav Hajek <highegg@gmail.com>
parents:
8736
diff
changeset
|
3241 return do_mx_red_op<boolMatrix> (*this, dim, mx_inline_any); |
458 | 3242 } |
3243 | |
3244 ComplexMatrix | |
3723 | 3245 ComplexMatrix::cumprod (int dim) const |
458 | 3246 { |
8736
53b4fdeacc2e
improve reduction functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8650
diff
changeset
|
3247 return do_mx_cum_op<ComplexMatrix> (*this, dim, mx_inline_cumprod); |
458 | 3248 } |
3249 | |
3250 ComplexMatrix | |
3723 | 3251 ComplexMatrix::cumsum (int dim) const |
458 | 3252 { |
8736
53b4fdeacc2e
improve reduction functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8650
diff
changeset
|
3253 return do_mx_cum_op<ComplexMatrix> (*this, dim, mx_inline_cumsum); |
458 | 3254 } |
3255 | |
3256 ComplexMatrix | |
3723 | 3257 ComplexMatrix::prod (int dim) const |
458 | 3258 { |
8736
53b4fdeacc2e
improve reduction functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8650
diff
changeset
|
3259 return do_mx_red_op<ComplexMatrix> (*this, dim, mx_inline_prod); |
458 | 3260 } |
3261 | |
3262 ComplexMatrix | |
3723 | 3263 ComplexMatrix::sum (int dim) const |
458 | 3264 { |
8736
53b4fdeacc2e
improve reduction functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8650
diff
changeset
|
3265 return do_mx_red_op<ComplexMatrix> (*this, dim, mx_inline_sum); |
458 | 3266 } |
3267 | |
3268 ComplexMatrix | |
3723 | 3269 ComplexMatrix::sumsq (int dim) const |
458 | 3270 { |
8736
53b4fdeacc2e
improve reduction functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8650
diff
changeset
|
3271 return do_mx_red_op<Matrix> (*this, dim, mx_inline_sumsq); |
458 | 3272 } |
3273 | |
4329 | 3274 Matrix ComplexMatrix::abs (void) const |
3275 { | |
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
|
3276 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
|
3277 rows (), cols ()); |
4329 | 3278 } |
3279 | |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7544
diff
changeset
|
3280 ComplexMatrix |
5275 | 3281 ComplexMatrix::diag (octave_idx_type k) const |
458 | 3282 { |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7544
diff
changeset
|
3283 return MArray2<Complex>::diag (k); |
458 | 3284 } |
3285 | |
2354 | 3286 bool |
5275 | 3287 ComplexMatrix::row_is_real_only (octave_idx_type i) const |
2354 | 3288 { |
3289 bool retval = true; | |
3290 | |
5275 | 3291 octave_idx_type nc = columns (); |
3292 | |
3293 for (octave_idx_type j = 0; j < nc; j++) | |
2354 | 3294 { |
5315 | 3295 if (std::imag (elem (i, j)) != 0.0) |
2354 | 3296 { |
3297 retval = false; | |
3298 break; | |
3299 } | |
3300 } | |
3301 | |
3302 return retval; | |
3303 } | |
3304 | |
3305 bool | |
5275 | 3306 ComplexMatrix::column_is_real_only (octave_idx_type j) const |
2354 | 3307 { |
3308 bool retval = true; | |
3309 | |
5275 | 3310 octave_idx_type nr = rows (); |
3311 | |
3312 for (octave_idx_type i = 0; i < nr; i++) | |
2354 | 3313 { |
5315 | 3314 if (std::imag (elem (i, j)) != 0.0) |
2354 | 3315 { |
3316 retval = false; | |
3317 break; | |
3318 } | |
3319 } | |
3320 | |
3321 return retval; | |
3322 } | |
891 | 3323 |
458 | 3324 ComplexColumnVector |
3325 ComplexMatrix::row_min (void) const | |
3326 { | |
5275 | 3327 Array<octave_idx_type> dummy_idx; |
4587 | 3328 return row_min (dummy_idx); |
458 | 3329 } |
3330 | |
3331 ComplexColumnVector | |
5275 | 3332 ComplexMatrix::row_min (Array<octave_idx_type>& idx_arg) const |
458 | 3333 { |
3334 ComplexColumnVector result; | |
3335 | |
5275 | 3336 octave_idx_type nr = rows (); |
3337 octave_idx_type nc = cols (); | |
458 | 3338 |
3339 if (nr > 0 && nc > 0) | |
3340 { | |
3341 result.resize (nr); | |
4587 | 3342 idx_arg.resize (nr); |
458 | 3343 |
5275 | 3344 for (octave_idx_type i = 0; i < nr; i++) |
458 | 3345 { |
2354 | 3346 bool real_only = row_is_real_only (i); |
3347 | |
5275 | 3348 octave_idx_type idx_j; |
4469 | 3349 |
3350 Complex tmp_min; | |
3351 | |
3352 double abs_min = octave_NaN; | |
3353 | |
3354 for (idx_j = 0; idx_j < nc; idx_j++) | |
3355 { | |
3356 tmp_min = elem (i, idx_j); | |
3357 | |
5389 | 3358 if (! xisnan (tmp_min)) |
4469 | 3359 { |
5315 | 3360 abs_min = real_only ? std::real (tmp_min) : std::abs (tmp_min); |
4469 | 3361 break; |
3362 } | |
3363 } | |
3364 | |
5275 | 3365 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469 | 3366 { |
3367 Complex tmp = elem (i, j); | |
3368 | |
5389 | 3369 if (xisnan (tmp)) |
4469 | 3370 continue; |
3371 | |
5315 | 3372 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469 | 3373 |
3374 if (abs_tmp < abs_min) | |
3375 { | |
3376 idx_j = j; | |
3377 tmp_min = tmp; | |
3378 abs_min = abs_tmp; | |
3379 } | |
3380 } | |
3381 | |
5389 | 3382 if (xisnan (tmp_min)) |
4469 | 3383 { |
3384 result.elem (i) = Complex_NaN_result; | |
4587 | 3385 idx_arg.elem (i) = 0; |
4469 | 3386 } |
891 | 3387 else |
3388 { | |
4469 | 3389 result.elem (i) = tmp_min; |
4587 | 3390 idx_arg.elem (i) = idx_j; |
891 | 3391 } |
458 | 3392 } |
3393 } | |
3394 | |
3395 return result; | |
3396 } | |
3397 | |
3398 ComplexColumnVector | |
3399 ComplexMatrix::row_max (void) const | |
3400 { | |
5275 | 3401 Array<octave_idx_type> dummy_idx; |
4587 | 3402 return row_max (dummy_idx); |
458 | 3403 } |
3404 | |
3405 ComplexColumnVector | |
5275 | 3406 ComplexMatrix::row_max (Array<octave_idx_type>& idx_arg) const |
458 | 3407 { |
3408 ComplexColumnVector result; | |
3409 | |
5275 | 3410 octave_idx_type nr = rows (); |
3411 octave_idx_type nc = cols (); | |
458 | 3412 |
3413 if (nr > 0 && nc > 0) | |
3414 { | |
3415 result.resize (nr); | |
4587 | 3416 idx_arg.resize (nr); |
458 | 3417 |
5275 | 3418 for (octave_idx_type i = 0; i < nr; i++) |
458 | 3419 { |
2354 | 3420 bool real_only = row_is_real_only (i); |
3421 | |
5275 | 3422 octave_idx_type idx_j; |
4469 | 3423 |
3424 Complex tmp_max; | |
3425 | |
3426 double abs_max = octave_NaN; | |
3427 | |
3428 for (idx_j = 0; idx_j < nc; idx_j++) | |
3429 { | |
3430 tmp_max = elem (i, idx_j); | |
3431 | |
5389 | 3432 if (! xisnan (tmp_max)) |
4469 | 3433 { |
5315 | 3434 abs_max = real_only ? std::real (tmp_max) : std::abs (tmp_max); |
4469 | 3435 break; |
3436 } | |
3437 } | |
3438 | |
5275 | 3439 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469 | 3440 { |
3441 Complex tmp = elem (i, j); | |
3442 | |
5389 | 3443 if (xisnan (tmp)) |
4469 | 3444 continue; |
3445 | |
5315 | 3446 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469 | 3447 |
3448 if (abs_tmp > abs_max) | |
3449 { | |
3450 idx_j = j; | |
3451 tmp_max = tmp; | |
3452 abs_max = abs_tmp; | |
3453 } | |
3454 } | |
3455 | |
5389 | 3456 if (xisnan (tmp_max)) |
4469 | 3457 { |
3458 result.elem (i) = Complex_NaN_result; | |
4587 | 3459 idx_arg.elem (i) = 0; |
4469 | 3460 } |
891 | 3461 else |
3462 { | |
4469 | 3463 result.elem (i) = tmp_max; |
4587 | 3464 idx_arg.elem (i) = idx_j; |
891 | 3465 } |
458 | 3466 } |
3467 } | |
3468 | |
3469 return result; | |
3470 } | |
3471 | |
3472 ComplexRowVector | |
3473 ComplexMatrix::column_min (void) const | |
3474 { | |
5275 | 3475 Array<octave_idx_type> dummy_idx; |
4587 | 3476 return column_min (dummy_idx); |
458 | 3477 } |
3478 | |
3479 ComplexRowVector | |
5275 | 3480 ComplexMatrix::column_min (Array<octave_idx_type>& idx_arg) const |
458 | 3481 { |
3482 ComplexRowVector result; | |
3483 | |
5275 | 3484 octave_idx_type nr = rows (); |
3485 octave_idx_type nc = cols (); | |
458 | 3486 |
3487 if (nr > 0 && nc > 0) | |
3488 { | |
3489 result.resize (nc); | |
4587 | 3490 idx_arg.resize (nc); |
458 | 3491 |
5275 | 3492 for (octave_idx_type j = 0; j < nc; j++) |
458 | 3493 { |
2354 | 3494 bool real_only = column_is_real_only (j); |
3495 | |
5275 | 3496 octave_idx_type idx_i; |
4469 | 3497 |
3498 Complex tmp_min; | |
3499 | |
3500 double abs_min = octave_NaN; | |
3501 | |
3502 for (idx_i = 0; idx_i < nr; idx_i++) | |
3503 { | |
3504 tmp_min = elem (idx_i, j); | |
3505 | |
5389 | 3506 if (! xisnan (tmp_min)) |
4469 | 3507 { |
5315 | 3508 abs_min = real_only ? std::real (tmp_min) : std::abs (tmp_min); |
4469 | 3509 break; |
3510 } | |
3511 } | |
3512 | |
5275 | 3513 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469 | 3514 { |
3515 Complex tmp = elem (i, j); | |
3516 | |
5389 | 3517 if (xisnan (tmp)) |
4469 | 3518 continue; |
3519 | |
5315 | 3520 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469 | 3521 |
3522 if (abs_tmp < abs_min) | |
3523 { | |
3524 idx_i = i; | |
3525 tmp_min = tmp; | |
3526 abs_min = abs_tmp; | |
3527 } | |
3528 } | |
3529 | |
5389 | 3530 if (xisnan (tmp_min)) |
4469 | 3531 { |
3532 result.elem (j) = Complex_NaN_result; | |
4587 | 3533 idx_arg.elem (j) = 0; |
4469 | 3534 } |
891 | 3535 else |
3536 { | |
4469 | 3537 result.elem (j) = tmp_min; |
4587 | 3538 idx_arg.elem (j) = idx_i; |
891 | 3539 } |
458 | 3540 } |
3541 } | |
3542 | |
3543 return result; | |
3544 } | |
3545 | |
3546 ComplexRowVector | |
3547 ComplexMatrix::column_max (void) const | |
3548 { | |
5275 | 3549 Array<octave_idx_type> dummy_idx; |
4587 | 3550 return column_max (dummy_idx); |
458 | 3551 } |
3552 | |
3553 ComplexRowVector | |
5275 | 3554 ComplexMatrix::column_max (Array<octave_idx_type>& idx_arg) const |
458 | 3555 { |
3556 ComplexRowVector result; | |
3557 | |
5275 | 3558 octave_idx_type nr = rows (); |
3559 octave_idx_type nc = cols (); | |
458 | 3560 |
3561 if (nr > 0 && nc > 0) | |
3562 { | |
3563 result.resize (nc); | |
4587 | 3564 idx_arg.resize (nc); |
458 | 3565 |
5275 | 3566 for (octave_idx_type j = 0; j < nc; j++) |
458 | 3567 { |
2354 | 3568 bool real_only = column_is_real_only (j); |
3569 | |
5275 | 3570 octave_idx_type idx_i; |
4469 | 3571 |
3572 Complex tmp_max; | |
3573 | |
3574 double abs_max = octave_NaN; | |
3575 | |
3576 for (idx_i = 0; idx_i < nr; idx_i++) | |
3577 { | |
3578 tmp_max = elem (idx_i, j); | |
3579 | |
5389 | 3580 if (! xisnan (tmp_max)) |
4469 | 3581 { |
5315 | 3582 abs_max = real_only ? std::real (tmp_max) : std::abs (tmp_max); |
4469 | 3583 break; |
3584 } | |
3585 } | |
3586 | |
5275 | 3587 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469 | 3588 { |
3589 Complex tmp = elem (i, j); | |
3590 | |
5389 | 3591 if (xisnan (tmp)) |
4469 | 3592 continue; |
3593 | |
5315 | 3594 double abs_tmp = real_only ? std::real (tmp) : std::abs (tmp); |
4469 | 3595 |
3596 if (abs_tmp > abs_max) | |
3597 { | |
3598 idx_i = i; | |
3599 tmp_max = tmp; | |
3600 abs_max = abs_tmp; | |
3601 } | |
3602 } | |
3603 | |
5389 | 3604 if (xisnan (tmp_max)) |
4469 | 3605 { |
3606 result.elem (j) = Complex_NaN_result; | |
4587 | 3607 idx_arg.elem (j) = 0; |
4469 | 3608 } |
891 | 3609 else |
3610 { | |
4469 | 3611 result.elem (j) = tmp_max; |
4587 | 3612 idx_arg.elem (j) = idx_i; |
891 | 3613 } |
458 | 3614 } |
3615 } | |
3616 | |
3617 return result; | |
3618 } | |
3619 | |
3620 // i/o | |
3621 | |
3504 | 3622 std::ostream& |
3623 operator << (std::ostream& os, const ComplexMatrix& a) | |
458 | 3624 { |
5275 | 3625 for (octave_idx_type i = 0; i < a.rows (); i++) |
458 | 3626 { |
5275 | 3627 for (octave_idx_type j = 0; j < a.cols (); j++) |
4130 | 3628 { |
3629 os << " "; | |
3630 octave_write_complex (os, a.elem (i, j)); | |
3631 } | |
458 | 3632 os << "\n"; |
3633 } | |
3634 return os; | |
3635 } | |
3636 | |
3504 | 3637 std::istream& |
3638 operator >> (std::istream& is, ComplexMatrix& a) | |
458 | 3639 { |
5275 | 3640 octave_idx_type nr = a.rows (); |
3641 octave_idx_type nc = a.cols (); | |
458 | 3642 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8956
diff
changeset
|
3643 if (nr > 0 && nc > 0) |
458 | 3644 { |
3645 Complex tmp; | |
5275 | 3646 for (octave_idx_type i = 0; i < nr; i++) |
3647 for (octave_idx_type j = 0; j < nc; j++) | |
458 | 3648 { |
4130 | 3649 tmp = octave_read_complex (is); |
458 | 3650 if (is) |
3651 a.elem (i, j) = tmp; | |
3652 else | |
2993 | 3653 goto done; |
458 | 3654 } |
3655 } | |
3656 | |
2993 | 3657 done: |
3658 | |
458 | 3659 return is; |
3660 } | |
3661 | |
1819 | 3662 ComplexMatrix |
3663 Givens (const Complex& x, const Complex& y) | |
3664 { | |
3665 double cc; | |
3666 Complex cs, temp_r; | |
3667 | |
3887 | 3668 F77_FUNC (zlartg, ZLARTG) (x, y, cc, cs, temp_r); |
1819 | 3669 |
3670 ComplexMatrix g (2, 2); | |
3671 | |
3672 g.elem (0, 0) = cc; | |
3673 g.elem (1, 1) = cc; | |
3674 g.elem (0, 1) = cs; | |
3675 g.elem (1, 0) = -conj (cs); | |
3676 | |
3677 return g; | |
3678 } | |
3679 | |
3680 ComplexMatrix | |
3681 Sylvester (const ComplexMatrix& a, const ComplexMatrix& b, | |
3682 const ComplexMatrix& c) | |
3683 { | |
3684 ComplexMatrix retval; | |
3685 | |
5775 | 3686 // FIXME -- need to check that a, b, and c are all the same |
1819 | 3687 // size. |
3688 | |
3689 // Compute Schur decompositions | |
3690 | |
3691 ComplexSCHUR as (a, "U"); | |
3692 ComplexSCHUR bs (b, "U"); | |
3693 | |
3694 // Transform c to new coordinates. | |
3695 | |
3696 ComplexMatrix ua = as.unitary_matrix (); | |
3697 ComplexMatrix sch_a = as.schur_matrix (); | |
3698 | |
3699 ComplexMatrix ub = bs.unitary_matrix (); | |
3700 ComplexMatrix sch_b = bs.schur_matrix (); | |
3701 | |
3702 ComplexMatrix cx = ua.hermitian () * c * ub; | |
3703 | |
3704 // Solve the sylvester equation, back-transform, and return the | |
3705 // solution. | |
3706 | |
5275 | 3707 octave_idx_type a_nr = a.rows (); |
3708 octave_idx_type b_nr = b.rows (); | |
1819 | 3709 |
3710 double scale; | |
5275 | 3711 octave_idx_type info; |
1950 | 3712 |
3713 Complex *pa = sch_a.fortran_vec (); | |
3714 Complex *pb = sch_b.fortran_vec (); | |
3715 Complex *px = cx.fortran_vec (); | |
1819 | 3716 |
4552 | 3717 F77_XFCN (ztrsyl, ZTRSYL, (F77_CONST_CHAR_ARG2 ("N", 1), |
3718 F77_CONST_CHAR_ARG2 ("N", 1), | |
3719 1, a_nr, b_nr, pa, a_nr, pb, | |
3720 b_nr, px, a_nr, scale, info | |
3721 F77_CHAR_ARG_LEN (1) | |
3722 F77_CHAR_ARG_LEN (1))); | |
1950 | 3723 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
3724 // FIXME -- check info? |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
3725 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
3726 retval = -ua * cx * ub.hermitian (); |
1819 | 3727 |
3728 return retval; | |
3729 } | |
3730 | |
2828 | 3731 ComplexMatrix |
3732 operator * (const ComplexMatrix& m, const Matrix& a) | |
3733 { | |
3734 ComplexMatrix tmp (a); | |
3735 return m * tmp; | |
3736 } | |
3737 | |
3738 ComplexMatrix | |
3739 operator * (const Matrix& m, const ComplexMatrix& a) | |
3740 { | |
3741 ComplexMatrix tmp (m); | |
3742 return tmp * a; | |
3743 } | |
3744 | |
6162 | 3745 /* Simple Dot Product, Matrix-Vector and Matrix-Matrix Unit tests |
3746 %!assert([1+i 2+i 3+i] * [ 4+i ; 5+i ; 6+i], 29+21i, 1e-14) | |
3747 %!assert([1+i 2+i ; 3+i 4+i ] * [5+i ; 6+i], [15 + 14i ; 37 + 18i], 1e-14) | |
3748 %!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) | |
3749 */ | |
3750 | |
3751 /* Test some simple identities | |
3752 %!shared M, cv, rv | |
3753 %! M = randn(10,10)+i*rand(10,10); | |
3754 %! cv = randn(10,1)+i*rand(10,1); | |
3755 %! rv = randn(1,10)+i*rand(1,10); | |
3756 %!assert([M*cv,M*cv],M*[cv,cv],1e-14) | |
3757 %!assert([rv*M;rv*M],[rv;rv]*M,1e-14) | |
3758 %!assert(2*rv*cv,[rv,rv]*[cv;cv],1e-14) | |
3759 */ | |
3760 | |
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
|
3761 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
|
3762 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
|
3763 { |
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 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
|
3765 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
|
3766 } |
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 // 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
|
3769 |
2828 | 3770 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
|
3771 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
|
3772 bool transb, bool conjb, const ComplexMatrix& b) |
2828 | 3773 { |
3774 ComplexMatrix retval; | |
3775 | |
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
|
3776 // 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
|
3777 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
|
3778 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
|
3779 |
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 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
|
3781 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
|
3782 |
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 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
|
3784 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
|
3785 |
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 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
|
3787 gripe_nonconformant ("operator *", a_nr, a_nc, b_nr, b_nc); |
2828 | 3788 else |
3789 { | |
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
|
3790 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
|
3791 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
|
3792 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
|
3793 { |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3794 octave_idx_type lda = a.rows (); |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3795 |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3796 retval.resize (a_nr, b_nc); |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3797 Complex *c = retval.fortran_vec (); |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3798 |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3799 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
|
3800 if (conja || conjb) |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3801 { |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3802 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
|
3803 F77_CONST_CHAR_ARG2 (ctransa, 1), |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3804 a_nr, a_nc, 1.0, |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3805 a.data (), lda, 0.0, c, a_nr |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3806 F77_CHAR_ARG_LEN (1) |
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 for (int j = 0; j < a_nr; j++) |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3809 for (int i = 0; i < j; i++) |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3810 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
|
3811 } |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3812 else |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3813 { |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3814 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
|
3815 F77_CONST_CHAR_ARG2 (ctransa, 1), |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3816 a_nr, a_nc, 1.0, |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3817 a.data (), lda, 0.0, c, a_nr |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3818 F77_CHAR_ARG_LEN (1) |
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 for (int j = 0; j < a_nr; j++) |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3821 for (int i = 0; i < j; i++) |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3822 retval.xelem (j,i) = retval.xelem (i,j); |
776791438957
map symmetric cases to xHERK, xSYRK
Jaroslav Hajek <highegg@gmail.com>
parents:
7800
diff
changeset
|
3823 |
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 } |
2828 | 3827 else |
3828 { | |
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
|
3829 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
|
3830 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
|
3831 |
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 retval.resize (a_nr, b_nc); |
2828 | 3833 Complex *c = retval.fortran_vec (); |
3834 | |
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
|
3835 if (b_nc == 1 && a_nr == 1) |
5983 | 3836 { |
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
|
3837 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
|
3838 { |
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 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
|
3840 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
|
3841 } |
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 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
|
3843 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
|
3844 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
|
3845 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
|
3846 } |
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 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
|
3848 { |
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 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
|
3850 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
|
3851 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
|
3852 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
|
3853 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
|
3854 } |
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 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
|
3856 { |
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 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
|
3858 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
|
3859 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
|
3860 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
|
3861 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
|
3862 } |
5983 | 3863 else |
6390 | 3864 { |
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
|
3865 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
|
3866 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
|
3867 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
|
3868 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
|
3869 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
|
3870 lda, b.data (), ldb, 0.0, c, a_nr |
6390 | 3871 F77_CHAR_ARG_LEN (1) |
3872 F77_CHAR_ARG_LEN (1))); | |
3873 } | |
2828 | 3874 } |
3875 } | |
3876 | |
3877 return retval; | |
3878 } | |
3879 | |
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
|
3880 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
|
3881 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
|
3882 { |
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 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
|
3884 } |
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 |
5775 | 3886 // FIXME -- it would be nice to share code among the min/max |
4309 | 3887 // functions below. |
3888 | |
3889 #define EMPTY_RETURN_CHECK(T) \ | |
3890 if (nr == 0 || nc == 0) \ | |
3891 return T (nr, nc); | |
3892 | |
3893 ComplexMatrix | |
3894 min (const Complex& c, const ComplexMatrix& m) | |
3895 { | |
5275 | 3896 octave_idx_type nr = m.rows (); |
3897 octave_idx_type nc = m.columns (); | |
4309 | 3898 |
3899 EMPTY_RETURN_CHECK (ComplexMatrix); | |
3900 | |
3901 ComplexMatrix result (nr, nc); | |
3902 | |
5275 | 3903 for (octave_idx_type j = 0; j < nc; j++) |
3904 for (octave_idx_type i = 0; i < nr; i++) | |
4309 | 3905 { |
3906 OCTAVE_QUIT; | |
3907 result (i, j) = xmin (c, m (i, j)); | |
3908 } | |
3909 | |
3910 return result; | |
3911 } | |
3912 | |
3913 ComplexMatrix | |
3914 min (const ComplexMatrix& m, const Complex& c) | |
3915 { | |
5275 | 3916 octave_idx_type nr = m.rows (); |
3917 octave_idx_type nc = m.columns (); | |
4309 | 3918 |
3919 EMPTY_RETURN_CHECK (ComplexMatrix); | |
3920 | |
3921 ComplexMatrix result (nr, nc); | |
3922 | |
5275 | 3923 for (octave_idx_type j = 0; j < nc; j++) |
3924 for (octave_idx_type i = 0; i < nr; i++) | |
4309 | 3925 { |
3926 OCTAVE_QUIT; | |
3927 result (i, j) = xmin (m (i, j), c); | |
3928 } | |
3929 | |
3930 return result; | |
3931 } | |
3932 | |
3933 ComplexMatrix | |
3934 min (const ComplexMatrix& a, const ComplexMatrix& b) | |
3935 { | |
5275 | 3936 octave_idx_type nr = a.rows (); |
3937 octave_idx_type nc = a.columns (); | |
4309 | 3938 |
3939 if (nr != b.rows () || nc != b.columns ()) | |
3940 { | |
3941 (*current_liboctave_error_handler) | |
3942 ("two-arg min expecting args of same size"); | |
3943 return ComplexMatrix (); | |
3944 } | |
3945 | |
3946 EMPTY_RETURN_CHECK (ComplexMatrix); | |
3947 | |
3948 ComplexMatrix result (nr, nc); | |
3949 | |
5275 | 3950 for (octave_idx_type j = 0; j < nc; j++) |
4309 | 3951 { |
3952 int columns_are_real_only = 1; | |
5275 | 3953 for (octave_idx_type i = 0; i < nr; i++) |
4309 | 3954 { |
3955 OCTAVE_QUIT; | |
5315 | 3956 if (std::imag (a (i, j)) != 0.0 || std::imag (b (i, j)) != 0.0) |
4309 | 3957 { |
3958 columns_are_real_only = 0; | |
3959 break; | |
3960 } | |
3961 } | |
3962 | |
3963 if (columns_are_real_only) | |
3964 { | |
5275 | 3965 for (octave_idx_type i = 0; i < nr; i++) |
5315 | 3966 result (i, j) = xmin (std::real (a (i, j)), std::real (b (i, j))); |
4309 | 3967 } |
3968 else | |
3969 { | |
5275 | 3970 for (octave_idx_type i = 0; i < nr; i++) |
4309 | 3971 { |
3972 OCTAVE_QUIT; | |
3973 result (i, j) = xmin (a (i, j), b (i, j)); | |
3974 } | |
3975 } | |
3976 } | |
3977 | |
3978 return result; | |
3979 } | |
3980 | |
3981 ComplexMatrix | |
3982 max (const Complex& c, const ComplexMatrix& m) | |
3983 { | |
5275 | 3984 octave_idx_type nr = m.rows (); |
3985 octave_idx_type nc = m.columns (); | |
4309 | 3986 |
3987 EMPTY_RETURN_CHECK (ComplexMatrix); | |
3988 | |
3989 ComplexMatrix result (nr, nc); | |
3990 | |
5275 | 3991 for (octave_idx_type j = 0; j < nc; j++) |
3992 for (octave_idx_type i = 0; i < nr; i++) | |
4309 | 3993 { |
3994 OCTAVE_QUIT; | |
3995 result (i, j) = xmax (c, m (i, j)); | |
3996 } | |
3997 | |
3998 return result; | |
3999 } | |
4000 | |
4001 ComplexMatrix | |
4002 max (const ComplexMatrix& m, const Complex& c) | |
4003 { | |
5275 | 4004 octave_idx_type nr = m.rows (); |
4005 octave_idx_type nc = m.columns (); | |
4309 | 4006 |
4007 EMPTY_RETURN_CHECK (ComplexMatrix); | |
4008 | |
4009 ComplexMatrix result (nr, nc); | |
4010 | |
5275 | 4011 for (octave_idx_type j = 0; j < nc; j++) |
4012 for (octave_idx_type i = 0; i < nr; i++) | |
4309 | 4013 { |
4014 OCTAVE_QUIT; | |
4015 result (i, j) = xmax (m (i, j), c); | |
4016 } | |
4017 | |
4018 return result; | |
4019 } | |
4020 | |
4021 ComplexMatrix | |
4022 max (const ComplexMatrix& a, const ComplexMatrix& b) | |
4023 { | |
5275 | 4024 octave_idx_type nr = a.rows (); |
4025 octave_idx_type nc = a.columns (); | |
4309 | 4026 |
4027 if (nr != b.rows () || nc != b.columns ()) | |
4028 { | |
4029 (*current_liboctave_error_handler) | |
4030 ("two-arg max expecting args of same size"); | |
4031 return ComplexMatrix (); | |
4032 } | |
4033 | |
4034 EMPTY_RETURN_CHECK (ComplexMatrix); | |
4035 | |
4036 ComplexMatrix result (nr, nc); | |
4037 | |
5275 | 4038 for (octave_idx_type j = 0; j < nc; j++) |
4309 | 4039 { |
4040 int columns_are_real_only = 1; | |
5275 | 4041 for (octave_idx_type i = 0; i < nr; i++) |
4309 | 4042 { |
4043 OCTAVE_QUIT; | |
5315 | 4044 if (std::imag (a (i, j)) != 0.0 || std::imag (b (i, j)) != 0.0) |
4309 | 4045 { |
4046 columns_are_real_only = 0; | |
4047 break; | |
4048 } | |
4049 } | |
4050 | |
4051 if (columns_are_real_only) | |
4052 { | |
5275 | 4053 for (octave_idx_type i = 0; i < nr; i++) |
4309 | 4054 { |
4055 OCTAVE_QUIT; | |
5315 | 4056 result (i, j) = xmax (std::real (a (i, j)), std::real (b (i, j))); |
4309 | 4057 } |
4058 } | |
4059 else | |
4060 { | |
5275 | 4061 for (octave_idx_type i = 0; i < nr; i++) |
4309 | 4062 { |
4063 OCTAVE_QUIT; | |
4064 result (i, j) = xmax (a (i, j), b (i, j)); | |
4065 } | |
4066 } | |
4067 } | |
4068 | |
4069 return result; | |
4070 } | |
4071 | |
5315 | 4072 MS_CMP_OPS(ComplexMatrix, std::real, Complex, std::real) |
3504 | 4073 MS_BOOL_OPS(ComplexMatrix, Complex, 0.0) |
2870 | 4074 |
5315 | 4075 SM_CMP_OPS(Complex, std::real, ComplexMatrix, std::real) |
3504 | 4076 SM_BOOL_OPS(Complex, ComplexMatrix, 0.0) |
2870 | 4077 |
5315 | 4078 MM_CMP_OPS(ComplexMatrix, std::real, ComplexMatrix, std::real) |
3504 | 4079 MM_BOOL_OPS(ComplexMatrix, ComplexMatrix, 0.0) |
2870 | 4080 |
458 | 4081 /* |
4082 ;;; Local Variables: *** | |
4083 ;;; mode: C++ *** | |
4084 ;;; End: *** | |
4085 */ |