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