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