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