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