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