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