Mercurial > octave-libgccjit
annotate liboctave/dMatrix.cc @ 7788:45f5faba05a2
Add the rcond function
author | David Bateman <dbateman@free.fr> |
---|---|
date | Wed, 14 May 2008 18:09:56 +0200 |
parents | 36594d5bbe13 |
children | 82be108cc558 |
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; |
7788 | 654 double rcon; |
6207 | 655 MatrixType mattype (*this); |
7788 | 656 return inverse (mattype, info, rcon, 0, 0); |
6207 | 657 } |
658 | |
659 Matrix | |
6479 | 660 Matrix::inverse (octave_idx_type& info) const |
661 { | |
7788 | 662 double rcon; |
6479 | 663 MatrixType mattype (*this); |
7788 | 664 return inverse (mattype, info, rcon, 0, 0); |
6479 | 665 } |
666 | |
667 Matrix | |
7788 | 668 Matrix::inverse (octave_idx_type& info, double& rcon, int force, |
6479 | 669 int calc_cond) const |
670 { | |
671 MatrixType mattype (*this); | |
7788 | 672 return inverse (mattype, info, rcon, force, calc_cond); |
6479 | 673 } |
674 | |
675 Matrix | |
6207 | 676 Matrix::inverse (MatrixType& mattype) const |
677 { | |
678 octave_idx_type info; | |
7788 | 679 double rcon; |
680 return inverse (mattype, info, rcon, 0, 0); | |
6207 | 681 } |
682 | |
683 Matrix | |
684 Matrix::inverse (MatrixType &mattype, octave_idx_type& info) const | |
685 { | |
7788 | 686 double rcon; |
687 return inverse (mattype, info, rcon, 0, 0); | |
458 | 688 } |
689 | |
690 Matrix | |
7788 | 691 Matrix::tinverse (MatrixType &mattype, octave_idx_type& info, double& rcon, |
6207 | 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. |
7788 | 716 rcon = 0.0; |
7482
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), |
7788 | 730 nr, tmp_data, nr, rcon, |
7482
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 |
7788 | 749 Matrix::finverse (MatrixType &mattype, octave_idx_type& info, double& rcon, |
6207 | 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. |
7788 | 789 rcon = 0.0; |
7482
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, |
7788 | 802 rcon, pz, piz, dgecon_info |
7482
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 |
7788 | 830 Matrix::inverse (MatrixType &mattype, octave_idx_type& info, double& rcon, |
6207 | 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) | |
7788 | 840 ret = tinverse (mattype, info, rcon, 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) | |
7788 | 849 rcon = chol.rcond (); |
6486 | 850 else |
7788 | 851 rcon = 1.0; |
6486 | 852 ret = chol.inverse (); |
853 } | |
6207 | 854 else |
855 mattype.mark_as_unsymmetric (); | |
856 } | |
857 | |
858 if (!mattype.is_hermitian ()) | |
7788 | 859 ret = finverse(mattype, info, rcon, force, calc_cond); |
860 | |
861 if ((mattype.is_hermitian () || calc_cond) && rcon == 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; |
7788 | 1219 double rcon; |
1220 return determinant (info, rcon, 0); | |
458 | 1221 } |
1222 | |
1223 DET | |
5275 | 1224 Matrix::determinant (octave_idx_type& info) const |
458 | 1225 { |
7788 | 1226 double rcon; |
1227 return determinant (info, rcon, 0); | |
458 | 1228 } |
1229 | |
1230 DET | |
7788 | 1231 Matrix::determinant (octave_idx_type& info, double& rcon, 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. |
7788 | 1260 rcon = 0.0; |
7482
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, |
7788 | 1279 rcon, pz, piz, info |
7482
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 | |
7788 | 1324 double |
1325 Matrix::rcond (void) const | |
1326 { | |
1327 MatrixType mattype (*this); | |
1328 return rcond (mattype); | |
1329 } | |
1330 | |
1331 double | |
1332 Matrix::rcond (MatrixType &mattype) const | |
1333 { | |
1334 double rcon; | |
1335 octave_idx_type nr = rows (); | |
1336 octave_idx_type nc = cols (); | |
1337 | |
1338 if (nr != nc) | |
1339 (*current_liboctave_error_handler) ("matrix must be square"); | |
1340 else if (nr == 0 || nc == 0) | |
1341 rcon = octave_Inf; | |
1342 else | |
1343 { | |
1344 int typ = mattype.type (); | |
1345 | |
1346 if (typ == MatrixType::Unknown) | |
1347 typ = mattype.type (*this); | |
1348 | |
1349 // Only calculate the condition number for LU/Cholesky | |
1350 if (typ == MatrixType::Upper) | |
1351 { | |
1352 const double *tmp_data = fortran_vec (); | |
1353 octave_idx_type info = 0; | |
1354 char norm = '1'; | |
1355 char uplo = 'U'; | |
1356 char dia = 'N'; | |
1357 | |
1358 Array<double> z (3 * nc); | |
1359 double *pz = z.fortran_vec (); | |
1360 Array<octave_idx_type> iz (nc); | |
1361 octave_idx_type *piz = iz.fortran_vec (); | |
1362 | |
1363 F77_XFCN (dtrcon, DTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), | |
1364 F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1365 F77_CONST_CHAR_ARG2 (&dia, 1), | |
1366 nr, tmp_data, nr, rcon, | |
1367 pz, piz, info | |
1368 F77_CHAR_ARG_LEN (1) | |
1369 F77_CHAR_ARG_LEN (1) | |
1370 F77_CHAR_ARG_LEN (1))); | |
1371 | |
1372 if (info != 0) | |
1373 rcon = 0.0; | |
1374 } | |
1375 else if (typ == MatrixType::Permuted_Upper) | |
1376 (*current_liboctave_error_handler) | |
1377 ("permuted triangular matrix not implemented"); | |
1378 else if (typ == MatrixType::Lower) | |
1379 { | |
1380 const double *tmp_data = fortran_vec (); | |
1381 octave_idx_type info = 0; | |
1382 char norm = '1'; | |
1383 char uplo = 'L'; | |
1384 char dia = 'N'; | |
1385 | |
1386 Array<double> z (3 * nc); | |
1387 double *pz = z.fortran_vec (); | |
1388 Array<octave_idx_type> iz (nc); | |
1389 octave_idx_type *piz = iz.fortran_vec (); | |
1390 | |
1391 F77_XFCN (dtrcon, DTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), | |
1392 F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1393 F77_CONST_CHAR_ARG2 (&dia, 1), | |
1394 nr, tmp_data, nr, rcon, | |
1395 pz, piz, info | |
1396 F77_CHAR_ARG_LEN (1) | |
1397 F77_CHAR_ARG_LEN (1) | |
1398 F77_CHAR_ARG_LEN (1))); | |
1399 | |
1400 if (info != 0) | |
1401 rcon = 0.0; | |
1402 } | |
1403 else if (typ == MatrixType::Permuted_Lower) | |
1404 (*current_liboctave_error_handler) | |
1405 ("permuted triangular matrix not implemented"); | |
1406 else if (typ == MatrixType::Full || typ == MatrixType::Hermitian) | |
1407 { | |
1408 double anorm = -1.0; | |
1409 Matrix atmp = *this; | |
1410 double *tmp_data = atmp.fortran_vec (); | |
1411 | |
1412 if (typ == MatrixType::Hermitian) | |
1413 { | |
1414 octave_idx_type info = 0; | |
1415 char job = 'L'; | |
1416 anorm = atmp.abs().sum(). | |
1417 row(static_cast<octave_idx_type>(0)).max(); | |
1418 | |
1419 F77_XFCN (dpotrf, DPOTRF, (F77_CONST_CHAR_ARG2 (&job, 1), nr, | |
1420 tmp_data, nr, info | |
1421 F77_CHAR_ARG_LEN (1))); | |
1422 | |
1423 if (info != 0) | |
1424 { | |
1425 rcon = 0.0; | |
1426 mattype.mark_as_unsymmetric (); | |
1427 typ = MatrixType::Full; | |
1428 } | |
1429 else | |
1430 { | |
1431 Array<double> z (3 * nc); | |
1432 double *pz = z.fortran_vec (); | |
1433 Array<octave_idx_type> iz (nc); | |
1434 octave_idx_type *piz = iz.fortran_vec (); | |
1435 | |
1436 F77_XFCN (dpocon, DPOCON, (F77_CONST_CHAR_ARG2 (&job, 1), | |
1437 nr, tmp_data, nr, anorm, | |
1438 rcon, pz, piz, info | |
1439 F77_CHAR_ARG_LEN (1))); | |
1440 | |
1441 if (info != 0) | |
1442 rcon = 0.0; | |
1443 } | |
1444 } | |
1445 | |
1446 if (typ == MatrixType::Full) | |
1447 { | |
1448 octave_idx_type info = 0; | |
1449 | |
1450 Array<octave_idx_type> ipvt (nr); | |
1451 octave_idx_type *pipvt = ipvt.fortran_vec (); | |
1452 | |
1453 if(anorm < 0.) | |
1454 anorm = atmp.abs().sum(). | |
1455 row(static_cast<octave_idx_type>(0)).max(); | |
1456 | |
1457 Array<double> z (4 * nc); | |
1458 double *pz = z.fortran_vec (); | |
1459 Array<octave_idx_type> iz (nc); | |
1460 octave_idx_type *piz = iz.fortran_vec (); | |
1461 | |
1462 F77_XFCN (dgetrf, DGETRF, (nr, nr, tmp_data, nr, pipvt, info)); | |
1463 | |
1464 if (info != 0) | |
1465 { | |
1466 rcon = 0.0; | |
1467 mattype.mark_as_rectangular (); | |
1468 } | |
1469 else | |
1470 { | |
1471 char job = '1'; | |
1472 F77_XFCN (dgecon, DGECON, (F77_CONST_CHAR_ARG2 (&job, 1), | |
1473 nc, tmp_data, nr, anorm, | |
1474 rcon, pz, piz, info | |
1475 F77_CHAR_ARG_LEN (1))); | |
1476 | |
1477 if (info != 0) | |
1478 rcon = 0.0; | |
1479 } | |
1480 } | |
1481 } | |
1482 else | |
1483 rcon = 0.0; | |
1484 } | |
1485 | |
1486 return rcon; | |
1487 } | |
1488 | |
458 | 1489 Matrix |
5785 | 1490 Matrix::utsolve (MatrixType &mattype, const Matrix& b, octave_idx_type& info, |
7788 | 1491 double& rcon, solve_singularity_handler sing_handler, |
5785 | 1492 bool calc_cond) const |
1493 { | |
1494 Matrix retval; | |
1495 | |
1496 octave_idx_type nr = rows (); | |
1497 octave_idx_type nc = cols (); | |
1498 | |
6924 | 1499 if (nr != b.rows ()) |
5785 | 1500 (*current_liboctave_error_handler) |
1501 ("matrix dimension mismatch solution of linear equations"); | |
6924 | 1502 else if (nr == 0 || nc == 0 || b.cols () == 0) |
1503 retval = Matrix (nc, b.cols (), 0.0); | |
5785 | 1504 else |
1505 { | |
1506 volatile int typ = mattype.type (); | |
1507 | |
1508 if (typ == MatrixType::Permuted_Upper || | |
1509 typ == MatrixType::Upper) | |
1510 { | |
1511 octave_idx_type b_nc = b.cols (); | |
7788 | 1512 rcon = 1.; |
5785 | 1513 info = 0; |
1514 | |
1515 if (typ == MatrixType::Permuted_Upper) | |
1516 { | |
1517 (*current_liboctave_error_handler) | |
6390 | 1518 ("permuted triangular matrix not implemented"); |
5785 | 1519 } |
1520 else | |
1521 { | |
1522 const double *tmp_data = fortran_vec (); | |
1523 | |
1524 if (calc_cond) | |
1525 { | |
1526 char norm = '1'; | |
1527 char uplo = 'U'; | |
1528 char dia = 'N'; | |
1529 | |
1530 Array<double> z (3 * nc); | |
1531 double *pz = z.fortran_vec (); | |
1532 Array<octave_idx_type> iz (nc); | |
1533 octave_idx_type *piz = iz.fortran_vec (); | |
1534 | |
1535 F77_XFCN (dtrcon, DTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), | |
1536 F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1537 F77_CONST_CHAR_ARG2 (&dia, 1), | |
7788 | 1538 nr, tmp_data, nr, rcon, |
5785 | 1539 pz, piz, info |
1540 F77_CHAR_ARG_LEN (1) | |
1541 F77_CHAR_ARG_LEN (1) | |
1542 F77_CHAR_ARG_LEN (1))); | |
1543 | |
1544 if (info != 0) | |
1545 info = -2; | |
1546 | |
7788 | 1547 volatile double rcond_plus_one = rcon + 1.0; |
1548 | |
1549 if (rcond_plus_one == 1.0 || xisnan (rcon)) | |
5785 | 1550 { |
1551 info = -2; | |
1552 | |
1553 if (sing_handler) | |
7788 | 1554 sing_handler (rcon); |
5785 | 1555 else |
1556 (*current_liboctave_error_handler) | |
1557 ("matrix singular to machine precision, rcond = %g", | |
7788 | 1558 rcon); |
5785 | 1559 } |
1560 } | |
1561 | |
1562 if (info == 0) | |
1563 { | |
1564 retval = b; | |
1565 double *result = retval.fortran_vec (); | |
1566 | |
1567 char uplo = 'U'; | |
1568 char trans = 'N'; | |
1569 char dia = 'N'; | |
1570 | |
1571 F77_XFCN (dtrtrs, DTRTRS, (F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1572 F77_CONST_CHAR_ARG2 (&trans, 1), | |
1573 F77_CONST_CHAR_ARG2 (&dia, 1), | |
1574 nr, b_nc, tmp_data, nr, | |
1575 result, nr, info | |
1576 F77_CHAR_ARG_LEN (1) | |
1577 F77_CHAR_ARG_LEN (1) | |
1578 F77_CHAR_ARG_LEN (1))); | |
1579 } | |
1580 } | |
1581 } | |
1582 else | |
1583 (*current_liboctave_error_handler) ("incorrect matrix type"); | |
1584 } | |
1585 | |
1586 return retval; | |
1587 } | |
1588 | |
1589 Matrix | |
1590 Matrix::ltsolve (MatrixType &mattype, const Matrix& b, octave_idx_type& info, | |
7788 | 1591 double& rcon, solve_singularity_handler sing_handler, |
5785 | 1592 bool calc_cond) const |
1593 { | |
1594 Matrix retval; | |
1595 | |
1596 octave_idx_type nr = rows (); | |
1597 octave_idx_type nc = cols (); | |
1598 | |
6924 | 1599 if (nr != b.rows ()) |
5785 | 1600 (*current_liboctave_error_handler) |
1601 ("matrix dimension mismatch solution of linear equations"); | |
6924 | 1602 else if (nr == 0 || nc == 0 || b.cols () == 0) |
1603 retval = Matrix (nc, b.cols (), 0.0); | |
5785 | 1604 else |
1605 { | |
1606 volatile int typ = mattype.type (); | |
1607 | |
1608 if (typ == MatrixType::Permuted_Lower || | |
1609 typ == MatrixType::Lower) | |
1610 { | |
1611 octave_idx_type b_nc = b.cols (); | |
7788 | 1612 rcon = 1.; |
5785 | 1613 info = 0; |
1614 | |
1615 if (typ == MatrixType::Permuted_Lower) | |
1616 { | |
1617 (*current_liboctave_error_handler) | |
6390 | 1618 ("permuted triangular matrix not implemented"); |
5785 | 1619 } |
1620 else | |
1621 { | |
1622 const double *tmp_data = fortran_vec (); | |
1623 | |
1624 if (calc_cond) | |
1625 { | |
1626 char norm = '1'; | |
1627 char uplo = 'L'; | |
1628 char dia = 'N'; | |
1629 | |
1630 Array<double> z (3 * nc); | |
1631 double *pz = z.fortran_vec (); | |
1632 Array<octave_idx_type> iz (nc); | |
1633 octave_idx_type *piz = iz.fortran_vec (); | |
1634 | |
1635 F77_XFCN (dtrcon, DTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), | |
1636 F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1637 F77_CONST_CHAR_ARG2 (&dia, 1), | |
7788 | 1638 nr, tmp_data, nr, rcon, |
5785 | 1639 pz, piz, info |
1640 F77_CHAR_ARG_LEN (1) | |
1641 F77_CHAR_ARG_LEN (1) | |
1642 F77_CHAR_ARG_LEN (1))); | |
1643 | |
1644 if (info != 0) | |
1645 info = -2; | |
1646 | |
7788 | 1647 volatile double rcond_plus_one = rcon + 1.0; |
1648 | |
1649 if (rcond_plus_one == 1.0 || xisnan (rcon)) | |
5785 | 1650 { |
1651 info = -2; | |
1652 | |
1653 if (sing_handler) | |
7788 | 1654 sing_handler (rcon); |
5785 | 1655 else |
1656 (*current_liboctave_error_handler) | |
1657 ("matrix singular to machine precision, rcond = %g", | |
7788 | 1658 rcon); |
5785 | 1659 } |
1660 } | |
1661 | |
1662 if (info == 0) | |
1663 { | |
1664 retval = b; | |
1665 double *result = retval.fortran_vec (); | |
1666 | |
1667 char uplo = 'L'; | |
1668 char trans = 'N'; | |
1669 char dia = 'N'; | |
1670 | |
1671 F77_XFCN (dtrtrs, DTRTRS, (F77_CONST_CHAR_ARG2 (&uplo, 1), | |
1672 F77_CONST_CHAR_ARG2 (&trans, 1), | |
1673 F77_CONST_CHAR_ARG2 (&dia, 1), | |
1674 nr, b_nc, tmp_data, nr, | |
1675 result, nr, info | |
1676 F77_CHAR_ARG_LEN (1) | |
1677 F77_CHAR_ARG_LEN (1) | |
1678 F77_CHAR_ARG_LEN (1))); | |
1679 } | |
1680 } | |
1681 } | |
1682 else | |
1683 (*current_liboctave_error_handler) ("incorrect matrix type"); | |
1684 } | |
1685 | |
1686 return retval; | |
1687 } | |
1688 | |
1689 Matrix | |
1690 Matrix::fsolve (MatrixType &mattype, const Matrix& b, octave_idx_type& info, | |
7788 | 1691 double& rcon, solve_singularity_handler sing_handler, |
5785 | 1692 bool calc_cond) const |
1693 { | |
1694 Matrix retval; | |
1695 | |
1696 octave_idx_type nr = rows (); | |
1697 octave_idx_type nc = cols (); | |
1698 | |
6924 | 1699 if (nr != nc || nr != b.rows ()) |
5785 | 1700 (*current_liboctave_error_handler) |
1701 ("matrix dimension mismatch solution of linear equations"); | |
6924 | 1702 else if (nr == 0 || b.cols () == 0) |
1703 retval = Matrix (nc, b.cols (), 0.0); | |
5785 | 1704 else |
1705 { | |
1706 volatile int typ = mattype.type (); | |
1707 | |
1708 // Calculate the norm of the matrix, for later use. | |
1709 double anorm = -1.; | |
1710 | |
1711 if (typ == MatrixType::Hermitian) | |
1712 { | |
1713 info = 0; | |
1714 char job = 'L'; | |
1715 Matrix atmp = *this; | |
1716 double *tmp_data = atmp.fortran_vec (); | |
1717 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); | |
1718 | |
1719 F77_XFCN (dpotrf, DPOTRF, (F77_CONST_CHAR_ARG2 (&job, 1), nr, | |
1720 tmp_data, nr, info | |
1721 F77_CHAR_ARG_LEN (1))); | |
1722 | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1723 // Throw-away extra info LAPACK gives so as to not change output. |
7788 | 1724 rcon = 0.0; |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1725 if (info != 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1726 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1727 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1728 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1729 mattype.mark_as_unsymmetric (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1730 typ = MatrixType::Full; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1731 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1732 else |
5785 | 1733 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1734 if (calc_cond) |
5785 | 1735 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1736 Array<double> z (3 * nc); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1737 double *pz = z.fortran_vec (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1738 Array<octave_idx_type> iz (nc); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1739 octave_idx_type *piz = iz.fortran_vec (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1740 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1741 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
|
1742 nr, tmp_data, nr, anorm, |
7788 | 1743 rcon, pz, piz, info |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1744 F77_CHAR_ARG_LEN (1))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1745 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1746 if (info != 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1747 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1748 |
7788 | 1749 volatile double rcond_plus_one = rcon + 1.0; |
1750 | |
1751 if (rcond_plus_one == 1.0 || xisnan (rcon)) | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1752 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1753 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1754 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1755 if (sing_handler) |
7788 | 1756 sing_handler (rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1757 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1758 (*current_liboctave_error_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1759 ("matrix singular to machine precision, rcond = %g", |
7788 | 1760 rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1761 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1762 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1763 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1764 if (info == 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1765 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1766 retval = b; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1767 double *result = retval.fortran_vec (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1768 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1769 octave_idx_type b_nc = b.cols (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1770 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1771 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
|
1772 nr, b_nc, tmp_data, nr, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1773 result, b.rows(), info |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1774 F77_CHAR_ARG_LEN (1))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1775 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1776 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1777 { |
5785 | 1778 mattype.mark_as_unsymmetric (); |
1779 typ = MatrixType::Full; | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1780 } |
5785 | 1781 } |
1782 } | |
1783 | |
1784 if (typ == MatrixType::Full) | |
1785 { | |
1786 info = 0; | |
1787 | |
1788 Array<octave_idx_type> ipvt (nr); | |
1789 octave_idx_type *pipvt = ipvt.fortran_vec (); | |
1790 | |
1791 Matrix atmp = *this; | |
1792 double *tmp_data = atmp.fortran_vec (); | |
1793 if(anorm < 0.) | |
1794 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); | |
1795 | |
1796 Array<double> z (4 * nc); | |
1797 double *pz = z.fortran_vec (); | |
1798 Array<octave_idx_type> iz (nc); | |
1799 octave_idx_type *piz = iz.fortran_vec (); | |
1800 | |
1801 F77_XFCN (dgetrf, DGETRF, (nr, nr, tmp_data, nr, pipvt, info)); | |
1802 | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1803 // Throw-away extra info LAPACK gives so as to not change output. |
7788 | 1804 rcon = 0.0; |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1805 if (info != 0) |
5785 | 1806 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1807 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1808 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1809 if (sing_handler) |
7788 | 1810 sing_handler (rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1811 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1812 (*current_liboctave_error_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1813 ("matrix singular to machine precision"); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1814 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1815 mattype.mark_as_rectangular (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1816 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1817 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1818 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1819 if (calc_cond) |
5785 | 1820 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1821 // Now calculate the condition number for |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1822 // non-singular matrix. |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1823 char job = '1'; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1824 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
|
1825 nc, tmp_data, nr, anorm, |
7788 | 1826 rcon, pz, piz, info |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1827 F77_CHAR_ARG_LEN (1))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1828 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1829 if (info != 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1830 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1831 |
7788 | 1832 volatile double rcond_plus_one = rcon + 1.0; |
1833 | |
1834 if (rcond_plus_one == 1.0 || xisnan (rcon)) | |
5785 | 1835 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1836 info = -2; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1837 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1838 if (sing_handler) |
7788 | 1839 sing_handler (rcon); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1840 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1841 (*current_liboctave_error_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1842 ("matrix singular to machine precision, rcond = %g", |
7788 | 1843 rcon); |
5785 | 1844 } |
1845 } | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1846 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1847 if (info == 0) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1848 { |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1849 retval = b; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1850 double *result = retval.fortran_vec (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1851 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1852 octave_idx_type b_nc = b.cols (); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1853 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1854 char job = 'N'; |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1855 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
|
1856 nr, b_nc, tmp_data, nr, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1857 pipvt, result, b.rows(), info |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1858 F77_CHAR_ARG_LEN (1))); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1859 } |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1860 else |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
1861 mattype.mark_as_rectangular (); |
5785 | 1862 } |
1863 } | |
1864 else if (typ != MatrixType::Hermitian) | |
1865 (*current_liboctave_error_handler) ("incorrect matrix type"); | |
1866 } | |
1867 | |
1868 return retval; | |
1869 } | |
1870 | |
1871 Matrix | |
1872 Matrix::solve (MatrixType &typ, const Matrix& b) const | |
1873 { | |
1874 octave_idx_type info; | |
7788 | 1875 double rcon; |
1876 return solve (typ, b, info, rcon, 0); | |
5785 | 1877 } |
1878 | |
1879 Matrix | |
1880 Matrix::solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, | |
7788 | 1881 double& rcon) const |
5785 | 1882 { |
7788 | 1883 return solve (typ, b, info, rcon, 0); |
5785 | 1884 } |
1885 | |
1886 Matrix | |
1887 Matrix::solve (MatrixType &mattype, const Matrix& b, octave_idx_type& info, | |
7788 | 1888 double& rcon, solve_singularity_handler sing_handler, |
5785 | 1889 bool singular_fallback) const |
1890 { | |
1891 Matrix retval; | |
1892 int typ = mattype.type (); | |
1893 | |
1894 if (typ == MatrixType::Unknown) | |
1895 typ = mattype.type (*this); | |
1896 | |
1897 // Only calculate the condition number for LU/Cholesky | |
1898 if (typ == MatrixType::Upper || typ == MatrixType::Permuted_Upper) | |
7788 | 1899 retval = utsolve (mattype, b, info, rcon, sing_handler, false); |
5785 | 1900 else if (typ == MatrixType::Lower || typ == MatrixType::Permuted_Lower) |
7788 | 1901 retval = ltsolve (mattype, b, info, rcon, sing_handler, false); |
5785 | 1902 else if (typ == MatrixType::Full || typ == MatrixType::Hermitian) |
7788 | 1903 retval = fsolve (mattype, b, info, rcon, sing_handler, true); |
5785 | 1904 else if (typ != MatrixType::Rectangular) |
1905 { | |
1906 (*current_liboctave_error_handler) ("unknown matrix type"); | |
1907 return Matrix (); | |
1908 } | |
1909 | |
1910 // Rectangular or one of the above solvers flags a singular matrix | |
1911 if (singular_fallback && mattype.type () == MatrixType::Rectangular) | |
1912 { | |
1913 octave_idx_type rank; | |
7788 | 1914 retval = lssolve (b, info, rank, rcon); |
5785 | 1915 } |
1916 | |
1917 return retval; | |
1918 } | |
1919 | |
1920 ComplexMatrix | |
1921 Matrix::solve (MatrixType &typ, const ComplexMatrix& b) const | |
1922 { | |
1923 ComplexMatrix tmp (*this); | |
1924 return tmp.solve (typ, b); | |
1925 } | |
1926 | |
1927 ComplexMatrix | |
1928 Matrix::solve (MatrixType &typ, const ComplexMatrix& b, | |
1929 octave_idx_type& info) const | |
1930 { | |
1931 ComplexMatrix tmp (*this); | |
1932 return tmp.solve (typ, b, info); | |
1933 } | |
1934 | |
1935 ComplexMatrix | |
1936 Matrix::solve (MatrixType &typ, const ComplexMatrix& b, octave_idx_type& info, | |
7788 | 1937 double& rcon) const |
5785 | 1938 { |
1939 ComplexMatrix tmp (*this); | |
7788 | 1940 return tmp.solve (typ, b, info, rcon); |
5785 | 1941 } |
1942 | |
1943 ComplexMatrix | |
1944 Matrix::solve (MatrixType &typ, const ComplexMatrix& b, octave_idx_type& info, | |
7788 | 1945 double& rcon, solve_singularity_handler sing_handler, |
5785 | 1946 bool singular_fallback) const |
1947 { | |
1948 ComplexMatrix tmp (*this); | |
7788 | 1949 return tmp.solve (typ, b, info, rcon, sing_handler, singular_fallback); |
5785 | 1950 } |
1951 | |
1952 ColumnVector | |
1953 Matrix::solve (MatrixType &typ, const ColumnVector& b) const | |
1954 { | |
7788 | 1955 octave_idx_type info; double rcon; |
1956 return solve (typ, b, info, rcon); | |
5785 | 1957 } |
1958 | |
1959 ColumnVector | |
1960 Matrix::solve (MatrixType &typ, const ColumnVector& b, | |
1961 octave_idx_type& info) const | |
1962 { | |
7788 | 1963 double rcon; |
1964 return solve (typ, b, info, rcon); | |
5785 | 1965 } |
1966 | |
1967 ColumnVector | |
1968 Matrix::solve (MatrixType &typ, const ColumnVector& b, octave_idx_type& info, | |
7788 | 1969 double& rcon) const |
5785 | 1970 { |
7788 | 1971 return solve (typ, b, info, rcon, 0); |
5785 | 1972 } |
1973 | |
1974 ColumnVector | |
1975 Matrix::solve (MatrixType &typ, const ColumnVector& b, octave_idx_type& info, | |
7788 | 1976 double& rcon, solve_singularity_handler sing_handler) const |
5785 | 1977 { |
1978 Matrix tmp (b); | |
7788 | 1979 return solve (typ, tmp, info, rcon, sing_handler).column(static_cast<octave_idx_type> (0)); |
5785 | 1980 } |
1981 | |
1982 ComplexColumnVector | |
1983 Matrix::solve (MatrixType &typ, const ComplexColumnVector& b) const | |
1984 { | |
1985 ComplexMatrix tmp (*this); | |
1986 return tmp.solve (typ, b); | |
1987 } | |
1988 | |
1989 ComplexColumnVector | |
1990 Matrix::solve (MatrixType &typ, const ComplexColumnVector& b, | |
1991 octave_idx_type& info) const | |
1992 { | |
1993 ComplexMatrix tmp (*this); | |
1994 return tmp.solve (typ, b, info); | |
1995 } | |
1996 | |
1997 ComplexColumnVector | |
1998 Matrix::solve (MatrixType &typ, const ComplexColumnVector& b, | |
7788 | 1999 octave_idx_type& info, double& rcon) const |
5785 | 2000 { |
2001 ComplexMatrix tmp (*this); | |
7788 | 2002 return tmp.solve (typ, b, info, rcon); |
5785 | 2003 } |
2004 | |
2005 ComplexColumnVector | |
2006 Matrix::solve (MatrixType &typ, const ComplexColumnVector& b, | |
7788 | 2007 octave_idx_type& info, double& rcon, |
5785 | 2008 solve_singularity_handler sing_handler) const |
2009 { | |
2010 ComplexMatrix tmp (*this); | |
7788 | 2011 return tmp.solve(typ, b, info, rcon, sing_handler); |
5785 | 2012 } |
2013 | |
2014 Matrix | |
458 | 2015 Matrix::solve (const Matrix& b) const |
2016 { | |
5275 | 2017 octave_idx_type info; |
7788 | 2018 double rcon; |
2019 return solve (b, info, rcon, 0); | |
458 | 2020 } |
2021 | |
2022 Matrix | |
5275 | 2023 Matrix::solve (const Matrix& b, octave_idx_type& info) const |
458 | 2024 { |
7788 | 2025 double rcon; |
2026 return solve (b, info, rcon, 0); | |
458 | 2027 } |
2028 | |
2029 Matrix | |
7788 | 2030 Matrix::solve (const Matrix& b, octave_idx_type& info, double& rcon) const |
458 | 2031 { |
7788 | 2032 return solve (b, info, rcon, 0); |
3480 | 2033 } |
2034 | |
2035 Matrix | |
5785 | 2036 Matrix::solve (const Matrix& b, octave_idx_type& info, |
7788 | 2037 double& rcon, solve_singularity_handler sing_handler) const |
3480 | 2038 { |
5785 | 2039 MatrixType mattype (*this); |
7788 | 2040 return solve (mattype, b, info, rcon, sing_handler); |
458 | 2041 } |
2042 | |
2043 ComplexMatrix | |
2044 Matrix::solve (const ComplexMatrix& b) const | |
2045 { | |
2046 ComplexMatrix tmp (*this); | |
2047 return tmp.solve (b); | |
2048 } | |
2049 | |
2050 ComplexMatrix | |
5275 | 2051 Matrix::solve (const ComplexMatrix& b, octave_idx_type& info) const |
458 | 2052 { |
2053 ComplexMatrix tmp (*this); | |
2054 return tmp.solve (b, info); | |
2055 } | |
2056 | |
2057 ComplexMatrix | |
7788 | 2058 Matrix::solve (const ComplexMatrix& b, octave_idx_type& info, double& rcon) const |
458 | 2059 { |
2060 ComplexMatrix tmp (*this); | |
7788 | 2061 return tmp.solve (b, info, rcon); |
458 | 2062 } |
2063 | |
3480 | 2064 ComplexMatrix |
7788 | 2065 Matrix::solve (const ComplexMatrix& b, octave_idx_type& info, double& rcon, |
3480 | 2066 solve_singularity_handler sing_handler) const |
2067 { | |
2068 ComplexMatrix tmp (*this); | |
7788 | 2069 return tmp.solve (b, info, rcon, sing_handler); |
3480 | 2070 } |
2071 | |
458 | 2072 ColumnVector |
2073 Matrix::solve (const ColumnVector& b) const | |
2074 { | |
7788 | 2075 octave_idx_type info; double rcon; |
2076 return solve (b, info, rcon); | |
458 | 2077 } |
2078 | |
2079 ColumnVector | |
5275 | 2080 Matrix::solve (const ColumnVector& b, octave_idx_type& info) const |
458 | 2081 { |
7788 | 2082 double rcon; |
2083 return solve (b, info, rcon); | |
458 | 2084 } |
2085 | |
2086 ColumnVector | |
7788 | 2087 Matrix::solve (const ColumnVector& b, octave_idx_type& info, double& rcon) const |
458 | 2088 { |
7788 | 2089 return solve (b, info, rcon, 0); |
3480 | 2090 } |
2091 | |
2092 ColumnVector | |
7788 | 2093 Matrix::solve (const ColumnVector& b, octave_idx_type& info, double& rcon, |
3480 | 2094 solve_singularity_handler sing_handler) const |
2095 { | |
5785 | 2096 MatrixType mattype (*this); |
7788 | 2097 return solve (mattype, b, info, rcon, sing_handler); |
458 | 2098 } |
2099 | |
2100 ComplexColumnVector | |
2101 Matrix::solve (const ComplexColumnVector& b) const | |
2102 { | |
2103 ComplexMatrix tmp (*this); | |
2104 return tmp.solve (b); | |
2105 } | |
2106 | |
2107 ComplexColumnVector | |
5275 | 2108 Matrix::solve (const ComplexColumnVector& b, octave_idx_type& info) const |
458 | 2109 { |
2110 ComplexMatrix tmp (*this); | |
2111 return tmp.solve (b, info); | |
2112 } | |
2113 | |
2114 ComplexColumnVector | |
7788 | 2115 Matrix::solve (const ComplexColumnVector& b, octave_idx_type& info, double& rcon) const |
458 | 2116 { |
2117 ComplexMatrix tmp (*this); | |
7788 | 2118 return tmp.solve (b, info, rcon); |
458 | 2119 } |
2120 | |
3480 | 2121 ComplexColumnVector |
7788 | 2122 Matrix::solve (const ComplexColumnVector& b, octave_idx_type& info, double& rcon, |
3480 | 2123 solve_singularity_handler sing_handler) const |
2124 { | |
2125 ComplexMatrix tmp (*this); | |
7788 | 2126 return tmp.solve (b, info, rcon, sing_handler); |
3480 | 2127 } |
2128 | |
458 | 2129 Matrix |
2130 Matrix::lssolve (const Matrix& b) const | |
2131 { | |
5275 | 2132 octave_idx_type info; |
2133 octave_idx_type rank; | |
7788 | 2134 double rcon; |
2135 return lssolve (b, info, rank, rcon); | |
458 | 2136 } |
2137 | |
2138 Matrix | |
5275 | 2139 Matrix::lssolve (const Matrix& b, octave_idx_type& info) const |
458 | 2140 { |
5275 | 2141 octave_idx_type rank; |
7788 | 2142 double rcon; |
2143 return lssolve (b, info, rank, rcon); | |
458 | 2144 } |
2145 | |
2146 Matrix | |
7072 | 2147 Matrix::lssolve (const Matrix& b, octave_idx_type& info, |
2148 octave_idx_type& rank) const | |
458 | 2149 { |
7788 | 2150 double rcon; |
2151 return lssolve (b, info, rank, rcon); | |
7076 | 2152 } |
2153 | |
2154 Matrix | |
2155 Matrix::lssolve (const Matrix& b, octave_idx_type& info, | |
7788 | 2156 octave_idx_type& rank, double &rcon) const |
7076 | 2157 { |
1948 | 2158 Matrix retval; |
2159 | |
5275 | 2160 octave_idx_type nrhs = b.cols (); |
2161 | |
2162 octave_idx_type m = rows (); | |
2163 octave_idx_type n = cols (); | |
458 | 2164 |
6924 | 2165 if (m != b.rows ()) |
1948 | 2166 (*current_liboctave_error_handler) |
6924 | 2167 ("matrix dimension mismatch solution of linear equations"); |
2168 else if (m == 0 || n == 0 || b.cols () == 0) | |
2169 retval = Matrix (n, b.cols (), 0.0); | |
1948 | 2170 else |
458 | 2171 { |
7072 | 2172 volatile octave_idx_type minmn = (m < n ? m : n); |
2173 octave_idx_type maxmn = m > n ? m : n; | |
7788 | 2174 rcon = -1.0; |
7072 | 2175 if (m != n) |
2176 { | |
2177 retval = Matrix (maxmn, nrhs, 0.0); | |
2178 | |
2179 for (octave_idx_type j = 0; j < nrhs; j++) | |
2180 for (octave_idx_type i = 0; i < m; i++) | |
2181 retval.elem (i, j) = b.elem (i, j); | |
2182 } | |
2183 else | |
2184 retval = b; | |
2185 | |
1948 | 2186 Matrix atmp = *this; |
2187 double *tmp_data = atmp.fortran_vec (); | |
2188 | |
7072 | 2189 double *pretval = retval.fortran_vec (); |
2190 Array<double> s (minmn); | |
7071 | 2191 double *ps = s.fortran_vec (); |
1948 | 2192 |
7072 | 2193 // Ask DGELSD what the dimension of WORK should be. |
5275 | 2194 octave_idx_type lwork = -1; |
3752 | 2195 |
2196 Array<double> work (1); | |
1948 | 2197 |
7477 | 2198 octave_idx_type smlsiz; |
2199 F77_FUNC (xilaenv, XILAENV) (9, F77_CONST_CHAR_ARG2 ("DGELSD", 6), | |
2200 F77_CONST_CHAR_ARG2 (" ", 1), | |
7478 | 2201 0, 0, 0, 0, smlsiz |
7477 | 2202 F77_CHAR_ARG_LEN (6) |
7478 | 2203 F77_CHAR_ARG_LEN (1)); |
7079 | 2204 |
7486
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2205 octave_idx_type mnthr; |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2206 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
|
2207 F77_CONST_CHAR_ARG2 (" ", 1), |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2208 m, n, nrhs, -1, mnthr |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2209 F77_CHAR_ARG_LEN (6) |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2210 F77_CHAR_ARG_LEN (1)); |
6a6d2abe51ff
more xGELSD workspace fixes
John W. Eaton <jwe@octave.org>
parents:
7482
diff
changeset
|
2211 |
7079 | 2212 // We compute the size of iwork because DGELSD in older versions |
2213 // of LAPACK does not return it on a query call. | |
7124 | 2214 double dminmn = static_cast<double> (minmn); |
2215 double dsmlsizp1 = static_cast<double> (smlsiz+1); | |
7079 | 2216 #if defined (HAVE_LOG2) |
7367 | 2217 double tmp = log2 (dminmn / dsmlsizp1); |
7079 | 2218 #else |
7367 | 2219 double tmp = log (dminmn / dsmlsizp1) / log (2.0); |
7079 | 2220 #endif |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2221 octave_idx_type nlvl = static_cast<octave_idx_type> (tmp) + 1; |
7079 | 2222 if (nlvl < 0) |
2223 nlvl = 0; | |
2224 | |
2225 octave_idx_type liwork = 3 * minmn * nlvl + 11 * minmn; | |
2226 if (liwork < 1) | |
2227 liwork = 1; | |
7072 | 2228 Array<octave_idx_type> iwork (liwork); |
2229 octave_idx_type* piwork = iwork.fortran_vec (); | |
2230 | |
2231 F77_XFCN (dgelsd, DGELSD, (m, n, nrhs, tmp_data, m, pretval, maxmn, | |
7788 | 2232 ps, rcon, rank, work.fortran_vec (), |
7072 | 2233 lwork, piwork, info)); |
1948 | 2234 |
7476 | 2235 // 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
|
2236 // through 3.1.1 when n >= mnthr. The obtuse formula below |
7476 | 2237 // should provide sufficient workspace for DGELSD to operate |
2238 // efficiently. | |
7488
6470f946a425
another small xGELSD workspace fix
John W. Eaton <jwe@octave.org>
parents:
7486
diff
changeset
|
2239 if (n >= mnthr) |
7476 | 2240 { |
2241 const octave_idx_type wlalsd | |
2242 = 9*m + 2*m*smlsiz + 8*m*nlvl + m*nrhs + (smlsiz+1)*(smlsiz+1); | |
2243 | |
2244 octave_idx_type addend = m; | |
2245 | |
2246 if (2*m-4 > addend) | |
2247 addend = 2*m-4; | |
2248 | |
2249 if (nrhs > addend) | |
2250 addend = nrhs; | |
2251 | |
2252 if (n-3*m > addend) | |
2253 addend = n-3*m; | |
2254 | |
2255 if (wlalsd > addend) | |
2256 addend = wlalsd; | |
2257 | |
2258 const octave_idx_type lworkaround = 4*m + m*m + addend; | |
2259 | |
2260 if (work(0) < lworkaround) | |
2261 work(0) = lworkaround; | |
2262 } | |
7532
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2263 else if (m >= n) |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2264 { |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2265 octave_idx_type lworkaround |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2266 = 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
|
2267 |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2268 if (work(0) < lworkaround) |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2269 work(0) = lworkaround; |
493bb0de3199
avoid another xGELSD workspace query bug
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
2270 } |
7476 | 2271 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2272 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
|
2273 work.resize (lwork); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2274 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2275 F77_XFCN (dgelsd, DGELSD, (m, n, nrhs, tmp_data, m, pretval, |
7788 | 2276 maxmn, ps, rcon, rank, |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2277 work.fortran_vec (), lwork, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2278 piwork, info)); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2279 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2280 if (rank < minmn) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2281 (*current_liboctave_warning_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2282 ("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
|
2283 if (s.elem (0) == 0.0) |
7788 | 2284 rcon = 0.0; |
1948 | 2285 else |
7788 | 2286 rcon = s.elem (minmn - 1) / s.elem (0); |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2287 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2288 retval.resize (n, nrhs); |
458 | 2289 } |
2290 | |
2291 return retval; | |
2292 } | |
2293 | |
2294 ComplexMatrix | |
2295 Matrix::lssolve (const ComplexMatrix& b) const | |
2296 { | |
2297 ComplexMatrix tmp (*this); | |
5275 | 2298 octave_idx_type info; |
2299 octave_idx_type rank; | |
7788 | 2300 double rcon; |
2301 return tmp.lssolve (b, info, rank, rcon); | |
458 | 2302 } |
2303 | |
2304 ComplexMatrix | |
5275 | 2305 Matrix::lssolve (const ComplexMatrix& b, octave_idx_type& info) const |
458 | 2306 { |
2307 ComplexMatrix tmp (*this); | |
5275 | 2308 octave_idx_type rank; |
7788 | 2309 double rcon; |
2310 return tmp.lssolve (b, info, rank, rcon); | |
458 | 2311 } |
2312 | |
2313 ComplexMatrix | |
7076 | 2314 Matrix::lssolve (const ComplexMatrix& b, octave_idx_type& info, |
2315 octave_idx_type& rank) const | |
458 | 2316 { |
2317 ComplexMatrix tmp (*this); | |
7788 | 2318 double rcon; |
2319 return tmp.lssolve (b, info, rank, rcon); | |
7076 | 2320 } |
2321 | |
2322 ComplexMatrix | |
2323 Matrix::lssolve (const ComplexMatrix& b, octave_idx_type& info, | |
7788 | 2324 octave_idx_type& rank, double& rcon) const |
7076 | 2325 { |
2326 ComplexMatrix tmp (*this); | |
7788 | 2327 return tmp.lssolve (b, info, rank, rcon); |
458 | 2328 } |
2329 | |
2330 ColumnVector | |
2331 Matrix::lssolve (const ColumnVector& b) const | |
2332 { | |
5275 | 2333 octave_idx_type info; |
2334 octave_idx_type rank; | |
7788 | 2335 double rcon; |
2336 return lssolve (b, info, rank, rcon); | |
458 | 2337 } |
2338 | |
2339 ColumnVector | |
5275 | 2340 Matrix::lssolve (const ColumnVector& b, octave_idx_type& info) const |
458 | 2341 { |
5275 | 2342 octave_idx_type rank; |
7788 | 2343 double rcon; |
2344 return lssolve (b, info, rank, rcon); | |
458 | 2345 } |
2346 | |
2347 ColumnVector | |
7072 | 2348 Matrix::lssolve (const ColumnVector& b, octave_idx_type& info, |
2349 octave_idx_type& rank) const | |
458 | 2350 { |
7788 | 2351 double rcon; |
2352 return lssolve (b, info, rank, rcon); | |
7076 | 2353 } |
2354 | |
2355 ColumnVector | |
2356 Matrix::lssolve (const ColumnVector& b, octave_idx_type& info, | |
7788 | 2357 octave_idx_type& rank, double &rcon) const |
7076 | 2358 { |
1948 | 2359 ColumnVector retval; |
2360 | |
5275 | 2361 octave_idx_type nrhs = 1; |
2362 | |
2363 octave_idx_type m = rows (); | |
2364 octave_idx_type n = cols (); | |
458 | 2365 |
6924 | 2366 if (m != b.length ()) |
1948 | 2367 (*current_liboctave_error_handler) |
6924 | 2368 ("matrix dimension mismatch solution of linear equations"); |
2369 else if (m == 0 || n == 0) | |
2370 retval = ColumnVector (n, 0.0); | |
1948 | 2371 else |
458 | 2372 { |
7072 | 2373 volatile octave_idx_type minmn = (m < n ? m : n); |
2374 octave_idx_type maxmn = m > n ? m : n; | |
7788 | 2375 rcon = -1.0; |
7072 | 2376 |
2377 if (m != n) | |
2378 { | |
2379 retval = ColumnVector (maxmn, 0.0); | |
2380 | |
2381 for (octave_idx_type i = 0; i < m; i++) | |
2382 retval.elem (i) = b.elem (i); | |
2383 } | |
2384 else | |
2385 retval = b; | |
2386 | |
1948 | 2387 Matrix atmp = *this; |
2388 double *tmp_data = atmp.fortran_vec (); | |
2389 | |
7072 | 2390 double *pretval = retval.fortran_vec (); |
2391 Array<double> s (minmn); | |
7071 | 2392 double *ps = s.fortran_vec (); |
1948 | 2393 |
7072 | 2394 // Ask DGELSD what the dimension of WORK should be. |
5275 | 2395 octave_idx_type lwork = -1; |
3752 | 2396 |
2397 Array<double> work (1); | |
2398 | |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2399 octave_idx_type smlsiz; |
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2400 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
|
2401 F77_CONST_CHAR_ARG2 (" ", 1), |
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2402 0, 0, 0, 0, smlsiz |
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2403 F77_CHAR_ARG_LEN (6) |
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2404 F77_CHAR_ARG_LEN (1)); |
7079 | 2405 |
2406 // We compute the size of iwork because DGELSD in older versions | |
2407 // of LAPACK does not return it on a query call. | |
7124 | 2408 double dminmn = static_cast<double> (minmn); |
2409 double dsmlsizp1 = static_cast<double> (smlsiz+1); | |
7079 | 2410 #if defined (HAVE_LOG2) |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2411 double tmp = log2 (dminmn / dsmlsizp1); |
7079 | 2412 #else |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2413 double tmp = log (dminmn / dsmlsizp1) / log (2.0); |
7079 | 2414 #endif |
7544
f9983d2761df
more xGELSD workspace fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
7532
diff
changeset
|
2415 octave_idx_type nlvl = static_cast<octave_idx_type> (tmp) + 1; |
7079 | 2416 if (nlvl < 0) |
2417 nlvl = 0; | |
2418 | |
2419 octave_idx_type liwork = 3 * minmn * nlvl + 11 * minmn; | |
2420 if (liwork < 1) | |
2421 liwork = 1; | |
7072 | 2422 Array<octave_idx_type> iwork (liwork); |
2423 octave_idx_type* piwork = iwork.fortran_vec (); | |
2424 | |
2425 F77_XFCN (dgelsd, DGELSD, (m, n, nrhs, tmp_data, m, pretval, maxmn, | |
7788 | 2426 ps, rcon, rank, work.fortran_vec (), |
7072 | 2427 lwork, piwork, info)); |
1948 | 2428 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2429 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
|
2430 work.resize (lwork); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2431 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2432 F77_XFCN (dgelsd, DGELSD, (m, n, nrhs, tmp_data, m, pretval, |
7788 | 2433 maxmn, ps, rcon, rank, |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2434 work.fortran_vec (), lwork, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2435 piwork, info)); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2436 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2437 if (rank < minmn) |
1948 | 2438 { |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2439 if (rank < minmn) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2440 (*current_liboctave_warning_handler) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2441 ("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
|
2442 if (s.elem (0) == 0.0) |
7788 | 2443 rcon = 0.0; |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2444 else |
7788 | 2445 rcon = s.elem (minmn - 1) / s.elem (0); |
1948 | 2446 } |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2447 |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
2448 retval.resize (n, nrhs); |
458 | 2449 } |
2450 | |
2451 return retval; | |
2452 } | |
2453 | |
2454 ComplexColumnVector | |
2455 Matrix::lssolve (const ComplexColumnVector& b) const | |
2456 { | |
2457 ComplexMatrix tmp (*this); | |
7076 | 2458 octave_idx_type info; |
2459 octave_idx_type rank; | |
7788 | 2460 double rcon; |
2461 return tmp.lssolve (b, info, rank, rcon); | |
458 | 2462 } |
2463 | |
2464 ComplexColumnVector | |
5275 | 2465 Matrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info) const |
458 | 2466 { |
2467 ComplexMatrix tmp (*this); | |
7076 | 2468 octave_idx_type rank; |
7788 | 2469 double rcon; |
2470 return tmp.lssolve (b, info, rank, rcon); | |
458 | 2471 } |
2472 | |
2473 ComplexColumnVector | |
7076 | 2474 Matrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info, |
2475 octave_idx_type& rank) const | |
458 | 2476 { |
2477 ComplexMatrix tmp (*this); | |
7788 | 2478 double rcon; |
2479 return tmp.lssolve (b, info, rank, rcon); | |
7076 | 2480 } |
2481 | |
2482 ComplexColumnVector | |
2483 Matrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info, | |
7788 | 2484 octave_idx_type& rank, double &rcon) const |
7076 | 2485 { |
2486 ComplexMatrix tmp (*this); | |
7788 | 2487 return tmp.lssolve (b, info, rank, rcon); |
458 | 2488 } |
2489 | |
1819 | 2490 // Constants for matrix exponential calculation. |
2491 | |
2492 static double padec [] = | |
2493 { | |
2494 5.0000000000000000e-1, | |
2495 1.1666666666666667e-1, | |
2496 1.6666666666666667e-2, | |
2497 1.6025641025641026e-3, | |
2498 1.0683760683760684e-4, | |
2499 4.8562548562548563e-6, | |
2500 1.3875013875013875e-7, | |
2501 1.9270852604185938e-9, | |
2502 }; | |
2503 | |
7400 | 2504 static void |
7788 | 2505 solve_singularity_warning (double rcon) |
7400 | 2506 { |
2507 (*current_liboctave_warning_handler) | |
2508 ("singular matrix encountered in expm calculation, rcond = %g", | |
7788 | 2509 rcon); |
7400 | 2510 } |
2511 | |
1819 | 2512 Matrix |
2513 Matrix::expm (void) const | |
2514 { | |
2515 Matrix retval; | |
2516 | |
2517 Matrix m = *this; | |
2518 | |
6699 | 2519 if (numel () == 1) |
2520 return Matrix (1, 1, exp (m(0))); | |
2521 | |
5275 | 2522 octave_idx_type nc = columns (); |
1819 | 2523 |
3130 | 2524 // Preconditioning step 1: trace normalization to reduce dynamic |
2525 // range of poles, but avoid making stable eigenvalues unstable. | |
2526 | |
1819 | 2527 // trace shift value |
3331 | 2528 volatile double trshift = 0.0; |
1819 | 2529 |
5275 | 2530 for (octave_idx_type i = 0; i < nc; i++) |
1819 | 2531 trshift += m.elem (i, i); |
2532 | |
2533 trshift /= nc; | |
2534 | |
3130 | 2535 if (trshift > 0.0) |
2536 { | |
5275 | 2537 for (octave_idx_type i = 0; i < nc; i++) |
3130 | 2538 m.elem (i, i) -= trshift; |
2539 } | |
1819 | 2540 |
3331 | 2541 // Preconditioning step 2: balancing; code follows development |
2542 // in AEPBAL | |
2543 | |
2544 double *p_m = m.fortran_vec (); | |
2545 | |
5275 | 2546 octave_idx_type info, ilo, ihi, ilos, ihis; |
3468 | 2547 Array<double> dpermute (nc); |
2548 Array<double> dscale (nc); | |
3466 | 2549 |
3468 | 2550 // permutation first |
2551 char job = 'P'; | |
4552 | 2552 F77_XFCN (dgebal, DGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
2553 nc, p_m, nc, ilo, ihi, | |
2554 dpermute.fortran_vec (), info | |
2555 F77_CHAR_ARG_LEN (1))); | |
3466 | 2556 |
3468 | 2557 // then scaling |
2558 job = 'S'; | |
4552 | 2559 F77_XFCN (dgebal, DGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
2560 nc, p_m, nc, ilos, ihis, | |
2561 dscale.fortran_vec (), info | |
2562 F77_CHAR_ARG_LEN (1))); | |
3331 | 2563 |
1819 | 2564 // Preconditioning step 3: scaling. |
3331 | 2565 |
1819 | 2566 ColumnVector work(nc); |
3130 | 2567 double inf_norm; |
3331 | 2568 |
4552 | 2569 F77_XFCN (xdlange, XDLANGE, (F77_CONST_CHAR_ARG2 ("I", 1), |
2570 nc, nc, m.fortran_vec (), nc, | |
2571 work.fortran_vec (), inf_norm | |
2572 F77_CHAR_ARG_LEN (1))); | |
3331 | 2573 |
5275 | 2574 octave_idx_type sqpow = static_cast<octave_idx_type> (inf_norm > 0.0 |
1819 | 2575 ? (1.0 + log (inf_norm) / log (2.0)) |
2576 : 0.0); | |
3331 | 2577 |
1819 | 2578 // Check whether we need to square at all. |
3331 | 2579 |
1819 | 2580 if (sqpow < 0) |
2581 sqpow = 0; | |
3331 | 2582 |
1819 | 2583 if (sqpow > 0) |
2584 { | |
7400 | 2585 if (sqpow > 1023) |
2586 sqpow = 1023; | |
2587 | |
1819 | 2588 double scale_factor = 1.0; |
5275 | 2589 for (octave_idx_type i = 0; i < sqpow; i++) |
1819 | 2590 scale_factor *= 2.0; |
7400 | 2591 |
1819 | 2592 m = m / scale_factor; |
2593 } | |
3331 | 2594 |
1819 | 2595 // npp, dpp: pade' approx polynomial matrices. |
3331 | 2596 |
1819 | 2597 Matrix npp (nc, nc, 0.0); |
7265 | 2598 double *pnpp = npp.fortran_vec (); |
1819 | 2599 Matrix dpp = npp; |
7265 | 2600 double *pdpp = dpp.fortran_vec (); |
3331 | 2601 |
1819 | 2602 // Now powers a^8 ... a^1. |
3331 | 2603 |
5275 | 2604 octave_idx_type minus_one_j = -1; |
2605 for (octave_idx_type j = 7; j >= 0; j--) | |
1819 | 2606 { |
7265 | 2607 for (octave_idx_type i = 0; i < nc; i++) |
2608 { | |
2609 octave_idx_type k = i * nc + i; | |
2610 pnpp[k] += padec[j]; | |
2611 pdpp[k] += minus_one_j * padec[j]; | |
2612 } | |
2613 | |
2614 npp = m * npp; | |
2615 pnpp = npp.fortran_vec (); | |
2616 | |
2617 dpp = m * dpp; | |
2618 pdpp = dpp.fortran_vec (); | |
2619 | |
1819 | 2620 minus_one_j *= -1; |
2621 } | |
3331 | 2622 |
1819 | 2623 // Zero power. |
3331 | 2624 |
1819 | 2625 dpp = -dpp; |
5275 | 2626 for (octave_idx_type j = 0; j < nc; j++) |
1819 | 2627 { |
2628 npp.elem (j, j) += 1.0; | |
2629 dpp.elem (j, j) += 1.0; | |
2630 } | |
3331 | 2631 |
1819 | 2632 // Compute pade approximation = inverse (dpp) * npp. |
2633 | |
7788 | 2634 double rcon; |
2635 retval = dpp.solve (npp, info, rcon, solve_singularity_warning); | |
7400 | 2636 |
2637 if (info < 0) | |
2638 return retval; | |
2639 | |
1819 | 2640 // Reverse preconditioning step 3: repeated squaring. |
3331 | 2641 |
1819 | 2642 while (sqpow) |
2643 { | |
2644 retval = retval * retval; | |
2645 sqpow--; | |
2646 } | |
3331 | 2647 |
1819 | 2648 // Reverse preconditioning step 2: inverse balancing. |
3466 | 2649 // apply inverse scaling to computed exponential |
5275 | 2650 for (octave_idx_type i = 0; i < nc; i++) |
2651 for (octave_idx_type j = 0; j < nc; j++) | |
3468 | 2652 retval(i,j) *= dscale(i) / dscale(j); |
3466 | 2653 |
4153 | 2654 OCTAVE_QUIT; |
2655 | |
3466 | 2656 // construct balancing permutation vector |
5275 | 2657 Array<octave_idx_type> iperm (nc); |
2658 for (octave_idx_type i = 0; i < nc; i++) | |
4593 | 2659 iperm(i) = i; // identity permutation |
3466 | 2660 |
2661 // leading permutations in forward order | |
5275 | 2662 for (octave_idx_type i = 0; i < (ilo-1); i++) |
3468 | 2663 { |
5275 | 2664 octave_idx_type swapidx = static_cast<octave_idx_type> (dpermute(i)) - 1; |
2665 octave_idx_type tmp = iperm(i); | |
4593 | 2666 iperm(i) = iperm (swapidx); |
2667 iperm(swapidx) = tmp; | |
3468 | 2668 } |
3466 | 2669 |
7416 | 2670 // construct inverse balancing permutation vector |
2671 Array<octave_idx_type> invpvec (nc); | |
2672 for (octave_idx_type i = 0; i < nc; i++) | |
2673 invpvec(iperm(i)) = i; // Thanks to R. A. Lippert for this method | |
2674 | |
2675 OCTAVE_QUIT; | |
2676 | |
2677 Matrix tmpMat = retval; | |
2678 for (octave_idx_type i = 0; i < nc; i++) | |
2679 for (octave_idx_type j = 0; j < nc; j++) | |
2680 retval(i,j) = tmpMat(invpvec(i),invpvec(j)); | |
2681 | |
2682 OCTAVE_QUIT; | |
2683 | |
2684 for (octave_idx_type i = 0; i < nc; i++) | |
2685 iperm(i) = i; // identity permutation | |
2686 | |
3466 | 2687 // trailing permutations must be done in reverse order |
5275 | 2688 for (octave_idx_type i = nc - 1; i >= ihi; i--) |
3468 | 2689 { |
5275 | 2690 octave_idx_type swapidx = static_cast<octave_idx_type> (dpermute(i)) - 1; |
2691 octave_idx_type tmp = iperm(i); | |
4593 | 2692 iperm(i) = iperm(swapidx); |
2693 iperm(swapidx) = tmp; | |
3468 | 2694 } |
3466 | 2695 |
2696 // construct inverse balancing permutation vector | |
5275 | 2697 for (octave_idx_type i = 0; i < nc; i++) |
4593 | 2698 invpvec(iperm(i)) = i; // Thanks to R. A. Lippert for this method |
4153 | 2699 |
2700 OCTAVE_QUIT; | |
3466 | 2701 |
7416 | 2702 tmpMat = retval; |
5275 | 2703 for (octave_idx_type i = 0; i < nc; i++) |
2704 for (octave_idx_type j = 0; j < nc; j++) | |
3468 | 2705 retval(i,j) = tmpMat(invpvec(i),invpvec(j)); |
3466 | 2706 |
1819 | 2707 // Reverse preconditioning step 1: fix trace normalization. |
3331 | 2708 |
3130 | 2709 if (trshift > 0.0) |
2710 retval = exp (trshift) * retval; | |
2711 | |
2712 return retval; | |
1819 | 2713 } |
2714 | |
458 | 2715 Matrix& |
2716 Matrix::operator += (const DiagMatrix& a) | |
2717 { | |
5275 | 2718 octave_idx_type nr = rows (); |
2719 octave_idx_type nc = cols (); | |
2720 | |
2721 octave_idx_type a_nr = a.rows (); | |
2722 octave_idx_type a_nc = a.cols (); | |
2385 | 2723 |
2724 if (nr != a_nr || nc != a_nc) | |
458 | 2725 { |
2385 | 2726 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458 | 2727 return *this; |
2728 } | |
2729 | |
5275 | 2730 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 2731 elem (i, i) += a.elem (i, i); |
2732 | |
2733 return *this; | |
2734 } | |
2735 | |
2736 Matrix& | |
2737 Matrix::operator -= (const DiagMatrix& a) | |
2738 { | |
5275 | 2739 octave_idx_type nr = rows (); |
2740 octave_idx_type nc = cols (); | |
2741 | |
2742 octave_idx_type a_nr = a.rows (); | |
2743 octave_idx_type a_nc = a.cols (); | |
2385 | 2744 |
2745 if (nr != a_nr || nc != a_nc) | |
458 | 2746 { |
2385 | 2747 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458 | 2748 return *this; |
2749 } | |
2750 | |
5275 | 2751 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 2752 elem (i, i) -= a.elem (i, i); |
2753 | |
2754 return *this; | |
2755 } | |
2756 | |
2757 // unary operations | |
2758 | |
2964 | 2759 boolMatrix |
458 | 2760 Matrix::operator ! (void) const |
2761 { | |
5275 | 2762 octave_idx_type nr = rows (); |
2763 octave_idx_type nc = cols (); | |
458 | 2764 |
2964 | 2765 boolMatrix b (nr, nc); |
458 | 2766 |
5275 | 2767 for (octave_idx_type j = 0; j < nc; j++) |
2768 for (octave_idx_type i = 0; i < nr; i++) | |
458 | 2769 b.elem (i, j) = ! elem (i, j); |
2770 | |
2771 return b; | |
2772 } | |
2773 | |
1205 | 2774 // column vector by row vector -> matrix operations |
458 | 2775 |
1205 | 2776 Matrix |
2777 operator * (const ColumnVector& v, const RowVector& a) | |
458 | 2778 { |
1948 | 2779 Matrix retval; |
2780 | |
5275 | 2781 octave_idx_type len = v.length (); |
3233 | 2782 |
2783 if (len != 0) | |
1205 | 2784 { |
5275 | 2785 octave_idx_type a_len = a.length (); |
3233 | 2786 |
2787 retval.resize (len, a_len); | |
2788 double *c = retval.fortran_vec (); | |
2789 | |
4552 | 2790 F77_XFCN (dgemm, DGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
2791 F77_CONST_CHAR_ARG2 ("N", 1), | |
2792 len, a_len, 1, 1.0, v.data (), len, | |
2793 a.data (), 1, 0.0, c, len | |
2794 F77_CHAR_ARG_LEN (1) | |
2795 F77_CHAR_ARG_LEN (1))); | |
1205 | 2796 } |
458 | 2797 |
1948 | 2798 return retval; |
458 | 2799 } |
2800 | |
2801 // other operations. | |
2802 | |
2803 Matrix | |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
2804 Matrix::map (dmapper fcn) const |
1205 | 2805 { |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
2806 return MArray2<double>::map<double> (func_ptr (fcn)); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
2807 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
2808 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
2809 ComplexMatrix |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
2810 Matrix::map (cmapper fcn) const |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
2811 { |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
2812 return MArray2<double>::map<Complex> (func_ptr (fcn)); |
1205 | 2813 } |
2814 | |
3248 | 2815 boolMatrix |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
2816 Matrix::map (bmapper fcn) const |
3248 | 2817 { |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7488
diff
changeset
|
2818 return MArray2<double>::map<bool> (func_ptr (fcn)); |
458 | 2819 } |
2820 | |
2385 | 2821 bool |
4431 | 2822 Matrix::any_element_is_negative (bool neg_zero) const |
2385 | 2823 { |
5275 | 2824 octave_idx_type nel = nelem (); |
2385 | 2825 |
4431 | 2826 if (neg_zero) |
2827 { | |
5275 | 2828 for (octave_idx_type i = 0; i < nel; i++) |
4634 | 2829 if (lo_ieee_signbit (elem (i))) |
2830 return true; | |
4431 | 2831 } |
2832 else | |
2833 { | |
5275 | 2834 for (octave_idx_type i = 0; i < nel; i++) |
4634 | 2835 if (elem (i) < 0) |
2836 return true; | |
4431 | 2837 } |
2385 | 2838 |
2839 return false; | |
2840 } | |
2841 | |
2842 | |
2843 bool | |
2844 Matrix::any_element_is_inf_or_nan (void) const | |
2845 { | |
5275 | 2846 octave_idx_type nel = nelem (); |
2847 | |
2848 for (octave_idx_type i = 0; i < nel; i++) | |
4634 | 2849 { |
2850 double val = elem (i); | |
2851 if (xisinf (val) || xisnan (val)) | |
2852 return true; | |
2853 } | |
2854 | |
2855 return false; | |
2385 | 2856 } |
2857 | |
2858 bool | |
5943 | 2859 Matrix::any_element_not_one_or_zero (void) const |
2860 { | |
2861 octave_idx_type nel = nelem (); | |
2862 | |
2863 for (octave_idx_type i = 0; i < nel; i++) | |
2864 { | |
2865 double val = elem (i); | |
2866 if (val != 0 && val != 1) | |
2867 return true; | |
2868 } | |
2869 | |
2870 return false; | |
2871 } | |
2872 | |
2873 bool | |
2385 | 2874 Matrix::all_elements_are_int_or_inf_or_nan (void) const |
2875 { | |
5275 | 2876 octave_idx_type nel = nelem (); |
2877 | |
2878 for (octave_idx_type i = 0; i < nel; i++) | |
4634 | 2879 { |
2880 double val = elem (i); | |
2881 if (xisnan (val) || D_NINT (val) == val) | |
2882 continue; | |
2883 else | |
2884 return false; | |
2885 } | |
2385 | 2886 |
2887 return true; | |
2888 } | |
2889 | |
1968 | 2890 // Return nonzero if any element of M is not an integer. Also extract |
2891 // the largest and smallest values and return them in MAX_VAL and MIN_VAL. | |
2892 | |
2385 | 2893 bool |
1968 | 2894 Matrix::all_integers (double& max_val, double& min_val) const |
2895 { | |
5275 | 2896 octave_idx_type nel = nelem (); |
4634 | 2897 |
2898 if (nel > 0) | |
1968 | 2899 { |
4634 | 2900 max_val = elem (0); |
2901 min_val = elem (0); | |
1968 | 2902 } |
2903 else | |
2385 | 2904 return false; |
1968 | 2905 |
5275 | 2906 for (octave_idx_type i = 0; i < nel; i++) |
4634 | 2907 { |
2908 double val = elem (i); | |
2909 | |
2910 if (val > max_val) | |
2911 max_val = val; | |
2912 | |
2913 if (val < min_val) | |
2914 min_val = val; | |
2915 | |
2916 if (D_NINT (val) != val) | |
2917 return false; | |
2918 } | |
2385 | 2919 |
2920 return true; | |
1968 | 2921 } |
2922 | |
2385 | 2923 bool |
1968 | 2924 Matrix::too_large_for_float (void) const |
2925 { | |
5275 | 2926 octave_idx_type nel = nelem (); |
2927 | |
2928 for (octave_idx_type i = 0; i < nel; i++) | |
4634 | 2929 { |
2930 double val = elem (i); | |
2931 | |
5389 | 2932 if (! (xisnan (val) || xisinf (val)) |
5387 | 2933 && fabs (val) > FLT_MAX) |
4634 | 2934 return true; |
2935 } | |
1968 | 2936 |
2385 | 2937 return false; |
1968 | 2938 } |
2939 | |
5775 | 2940 // FIXME Do these really belong here? Maybe they should be |
4015 | 2941 // in a base class? |
458 | 2942 |
2832 | 2943 boolMatrix |
4015 | 2944 Matrix::all (int dim) const |
458 | 2945 { |
4015 | 2946 MX_ALL_OP (dim); |
458 | 2947 } |
2948 | |
2832 | 2949 boolMatrix |
4015 | 2950 Matrix::any (int dim) const |
458 | 2951 { |
4015 | 2952 MX_ANY_OP (dim); |
458 | 2953 } |
2954 | |
2955 Matrix | |
3723 | 2956 Matrix::cumprod (int dim) const |
458 | 2957 { |
4015 | 2958 MX_CUMULATIVE_OP (Matrix, double, *=); |
458 | 2959 } |
2960 | |
2961 Matrix | |
3723 | 2962 Matrix::cumsum (int dim) const |
458 | 2963 { |
4015 | 2964 MX_CUMULATIVE_OP (Matrix, double, +=); |
458 | 2965 } |
2966 | |
2967 Matrix | |
3723 | 2968 Matrix::prod (int dim) const |
458 | 2969 { |
3864 | 2970 MX_REDUCTION_OP (Matrix, *=, 1.0, 1.0); |
458 | 2971 } |
2972 | |
2973 Matrix | |
3723 | 2974 Matrix::sum (int dim) const |
458 | 2975 { |
3864 | 2976 MX_REDUCTION_OP (Matrix, +=, 0.0, 0.0); |
458 | 2977 } |
2978 | |
2979 Matrix | |
3723 | 2980 Matrix::sumsq (int dim) const |
458 | 2981 { |
3864 | 2982 #define ROW_EXPR \ |
2983 double d = elem (i, j); \ | |
2984 retval.elem (i, 0) += d * d | |
2985 | |
2986 #define COL_EXPR \ | |
2987 double d = elem (i, j); \ | |
2988 retval.elem (0, j) += d * d | |
2989 | |
2990 MX_BASE_REDUCTION_OP (Matrix, ROW_EXPR, COL_EXPR, 0.0, 0.0); | |
2991 | |
2992 #undef ROW_EXPR | |
2993 #undef COL_EXPR | |
458 | 2994 } |
2995 | |
2385 | 2996 Matrix |
2997 Matrix::abs (void) const | |
2998 { | |
5275 | 2999 octave_idx_type nr = rows (); |
3000 octave_idx_type nc = cols (); | |
2385 | 3001 |
3002 Matrix retval (nr, nc); | |
3003 | |
5275 | 3004 for (octave_idx_type j = 0; j < nc; j++) |
3005 for (octave_idx_type i = 0; i < nr; i++) | |
2385 | 3006 retval (i, j) = fabs (elem (i, j)); |
3007 | |
3008 return retval; | |
3009 } | |
3010 | |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7544
diff
changeset
|
3011 Matrix |
5275 | 3012 Matrix::diag (octave_idx_type k) const |
458 | 3013 { |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7544
diff
changeset
|
3014 return MArray2<double>::diag (k); |
458 | 3015 } |
3016 | |
3017 ColumnVector | |
3018 Matrix::row_min (void) const | |
3019 { | |
5275 | 3020 Array<octave_idx_type> dummy_idx; |
4587 | 3021 return row_min (dummy_idx); |
458 | 3022 } |
3023 | |
3024 ColumnVector | |
5275 | 3025 Matrix::row_min (Array<octave_idx_type>& idx_arg) const |
458 | 3026 { |
3027 ColumnVector result; | |
3028 | |
5275 | 3029 octave_idx_type nr = rows (); |
3030 octave_idx_type nc = cols (); | |
458 | 3031 |
3032 if (nr > 0 && nc > 0) | |
3033 { | |
3034 result.resize (nr); | |
4587 | 3035 idx_arg.resize (nr); |
458 | 3036 |
5275 | 3037 for (octave_idx_type i = 0; i < nr; i++) |
458 | 3038 { |
5275 | 3039 octave_idx_type idx_j; |
4469 | 3040 |
3041 double tmp_min = octave_NaN; | |
3042 | |
3043 for (idx_j = 0; idx_j < nc; idx_j++) | |
2354 | 3044 { |
4469 | 3045 tmp_min = elem (i, idx_j); |
3046 | |
5389 | 3047 if (! xisnan (tmp_min)) |
4469 | 3048 break; |
3049 } | |
3050 | |
5275 | 3051 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469 | 3052 { |
3053 double tmp = elem (i, j); | |
3054 | |
5389 | 3055 if (xisnan (tmp)) |
4469 | 3056 continue; |
3057 else if (tmp < tmp_min) | |
2354 | 3058 { |
4469 | 3059 idx_j = j; |
3060 tmp_min = tmp; | |
2354 | 3061 } |
3062 } | |
3063 | |
4469 | 3064 result.elem (i) = tmp_min; |
5389 | 3065 idx_arg.elem (i) = xisnan (tmp_min) ? 0 : idx_j; |
458 | 3066 } |
3067 } | |
3068 | |
3069 return result; | |
3070 } | |
3071 | |
3072 ColumnVector | |
3073 Matrix::row_max (void) const | |
3074 { | |
5275 | 3075 Array<octave_idx_type> dummy_idx; |
4587 | 3076 return row_max (dummy_idx); |
458 | 3077 } |
3078 | |
3079 ColumnVector | |
5275 | 3080 Matrix::row_max (Array<octave_idx_type>& idx_arg) const |
458 | 3081 { |
3082 ColumnVector result; | |
3083 | |
5275 | 3084 octave_idx_type nr = rows (); |
3085 octave_idx_type nc = cols (); | |
458 | 3086 |
3087 if (nr > 0 && nc > 0) | |
3088 { | |
3089 result.resize (nr); | |
4587 | 3090 idx_arg.resize (nr); |
458 | 3091 |
5275 | 3092 for (octave_idx_type i = 0; i < nr; i++) |
458 | 3093 { |
5275 | 3094 octave_idx_type idx_j; |
4469 | 3095 |
3096 double tmp_max = octave_NaN; | |
3097 | |
3098 for (idx_j = 0; idx_j < nc; idx_j++) | |
2354 | 3099 { |
4469 | 3100 tmp_max = elem (i, idx_j); |
3101 | |
5389 | 3102 if (! xisnan (tmp_max)) |
4469 | 3103 break; |
3104 } | |
3105 | |
5275 | 3106 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469 | 3107 { |
3108 double tmp = elem (i, j); | |
3109 | |
5389 | 3110 if (xisnan (tmp)) |
4469 | 3111 continue; |
3112 else if (tmp > tmp_max) | |
2354 | 3113 { |
4469 | 3114 idx_j = j; |
3115 tmp_max = tmp; | |
2354 | 3116 } |
3117 } | |
3118 | |
4469 | 3119 result.elem (i) = tmp_max; |
5389 | 3120 idx_arg.elem (i) = xisnan (tmp_max) ? 0 : idx_j; |
458 | 3121 } |
3122 } | |
3123 | |
3124 return result; | |
3125 } | |
3126 | |
3127 RowVector | |
3128 Matrix::column_min (void) const | |
3129 { | |
5275 | 3130 Array<octave_idx_type> dummy_idx; |
4587 | 3131 return column_min (dummy_idx); |
458 | 3132 } |
2354 | 3133 |
458 | 3134 RowVector |
5275 | 3135 Matrix::column_min (Array<octave_idx_type>& idx_arg) const |
458 | 3136 { |
3137 RowVector result; | |
3138 | |
5275 | 3139 octave_idx_type nr = rows (); |
3140 octave_idx_type nc = cols (); | |
458 | 3141 |
3142 if (nr > 0 && nc > 0) | |
3143 { | |
3144 result.resize (nc); | |
4587 | 3145 idx_arg.resize (nc); |
458 | 3146 |
5275 | 3147 for (octave_idx_type j = 0; j < nc; j++) |
458 | 3148 { |
5275 | 3149 octave_idx_type idx_i; |
4469 | 3150 |
3151 double tmp_min = octave_NaN; | |
3152 | |
3153 for (idx_i = 0; idx_i < nr; idx_i++) | |
2354 | 3154 { |
4469 | 3155 tmp_min = elem (idx_i, j); |
3156 | |
5389 | 3157 if (! xisnan (tmp_min)) |
4469 | 3158 break; |
3159 } | |
3160 | |
5275 | 3161 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469 | 3162 { |
3163 double tmp = elem (i, j); | |
3164 | |
5389 | 3165 if (xisnan (tmp)) |
4469 | 3166 continue; |
3167 else if (tmp < tmp_min) | |
2354 | 3168 { |
4469 | 3169 idx_i = i; |
3170 tmp_min = tmp; | |
2354 | 3171 } |
3172 } | |
3173 | |
4469 | 3174 result.elem (j) = tmp_min; |
5389 | 3175 idx_arg.elem (j) = xisnan (tmp_min) ? 0 : idx_i; |
458 | 3176 } |
3177 } | |
3178 | |
3179 return result; | |
3180 } | |
3181 | |
2354 | 3182 RowVector |
3183 Matrix::column_max (void) const | |
3184 { | |
5275 | 3185 Array<octave_idx_type> dummy_idx; |
4587 | 3186 return column_max (dummy_idx); |
2354 | 3187 } |
458 | 3188 |
3189 RowVector | |
5275 | 3190 Matrix::column_max (Array<octave_idx_type>& idx_arg) const |
458 | 3191 { |
3192 RowVector result; | |
3193 | |
5275 | 3194 octave_idx_type nr = rows (); |
3195 octave_idx_type nc = cols (); | |
458 | 3196 |
3197 if (nr > 0 && nc > 0) | |
3198 { | |
3199 result.resize (nc); | |
4587 | 3200 idx_arg.resize (nc); |
458 | 3201 |
5275 | 3202 for (octave_idx_type j = 0; j < nc; j++) |
458 | 3203 { |
5275 | 3204 octave_idx_type idx_i; |
4469 | 3205 |
3206 double tmp_max = octave_NaN; | |
3207 | |
3208 for (idx_i = 0; idx_i < nr; idx_i++) | |
2354 | 3209 { |
4469 | 3210 tmp_max = elem (idx_i, j); |
3211 | |
5389 | 3212 if (! xisnan (tmp_max)) |
4469 | 3213 break; |
3214 } | |
3215 | |
5275 | 3216 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469 | 3217 { |
3218 double tmp = elem (i, j); | |
3219 | |
5389 | 3220 if (xisnan (tmp)) |
4469 | 3221 continue; |
3222 else if (tmp > tmp_max) | |
2354 | 3223 { |
4469 | 3224 idx_i = i; |
3225 tmp_max = tmp; | |
2354 | 3226 } |
3227 } | |
3228 | |
4469 | 3229 result.elem (j) = tmp_max; |
5389 | 3230 idx_arg.elem (j) = xisnan (tmp_max) ? 0 : idx_i; |
458 | 3231 } |
3232 } | |
3233 | |
3234 return result; | |
3235 } | |
3236 | |
3504 | 3237 std::ostream& |
3238 operator << (std::ostream& os, const Matrix& a) | |
458 | 3239 { |
5275 | 3240 for (octave_idx_type i = 0; i < a.rows (); i++) |
458 | 3241 { |
5275 | 3242 for (octave_idx_type j = 0; j < a.cols (); j++) |
4130 | 3243 { |
3244 os << " "; | |
3245 octave_write_double (os, a.elem (i, j)); | |
3246 } | |
458 | 3247 os << "\n"; |
3248 } | |
3249 return os; | |
3250 } | |
3251 | |
3504 | 3252 std::istream& |
3253 operator >> (std::istream& is, Matrix& a) | |
458 | 3254 { |
5275 | 3255 octave_idx_type nr = a.rows (); |
3256 octave_idx_type nc = a.cols (); | |
458 | 3257 |
3258 if (nr < 1 || nc < 1) | |
3504 | 3259 is.clear (std::ios::badbit); |
458 | 3260 else |
3261 { | |
3262 double tmp; | |
5275 | 3263 for (octave_idx_type i = 0; i < nr; i++) |
3264 for (octave_idx_type j = 0; j < nc; j++) | |
458 | 3265 { |
4130 | 3266 tmp = octave_read_double (is); |
458 | 3267 if (is) |
3268 a.elem (i, j) = tmp; | |
3269 else | |
2795 | 3270 goto done; |
458 | 3271 } |
3272 } | |
3273 | |
2795 | 3274 done: |
3275 | |
458 | 3276 return is; |
3277 } | |
3278 | |
1819 | 3279 Matrix |
3280 Givens (double x, double y) | |
3281 { | |
3282 double cc, s, temp_r; | |
3283 | |
3887 | 3284 F77_FUNC (dlartg, DLARTG) (x, y, cc, s, temp_r); |
1819 | 3285 |
3286 Matrix g (2, 2); | |
3287 | |
3288 g.elem (0, 0) = cc; | |
3289 g.elem (1, 1) = cc; | |
3290 g.elem (0, 1) = s; | |
3291 g.elem (1, 0) = -s; | |
3292 | |
3293 return g; | |
3294 } | |
3295 | |
3296 Matrix | |
3297 Sylvester (const Matrix& a, const Matrix& b, const Matrix& c) | |
3298 { | |
3299 Matrix retval; | |
3300 | |
5775 | 3301 // FIXME -- need to check that a, b, and c are all the same |
1819 | 3302 // size. |
3303 | |
3304 // Compute Schur decompositions. | |
3305 | |
3306 SCHUR as (a, "U"); | |
3307 SCHUR bs (b, "U"); | |
3308 | |
3309 // Transform c to new coordinates. | |
3310 | |
3311 Matrix ua = as.unitary_matrix (); | |
3312 Matrix sch_a = as.schur_matrix (); | |
3313 | |
3314 Matrix ub = bs.unitary_matrix (); | |
3315 Matrix sch_b = bs.schur_matrix (); | |
3316 | |
3317 Matrix cx = ua.transpose () * c * ub; | |
3318 | |
3319 // Solve the sylvester equation, back-transform, and return the | |
3320 // solution. | |
3321 | |
5275 | 3322 octave_idx_type a_nr = a.rows (); |
3323 octave_idx_type b_nr = b.rows (); | |
1819 | 3324 |
3325 double scale; | |
5275 | 3326 octave_idx_type info; |
1819 | 3327 |
1950 | 3328 double *pa = sch_a.fortran_vec (); |
3329 double *pb = sch_b.fortran_vec (); | |
3330 double *px = cx.fortran_vec (); | |
3331 | |
4552 | 3332 F77_XFCN (dtrsyl, DTRSYL, (F77_CONST_CHAR_ARG2 ("N", 1), |
3333 F77_CONST_CHAR_ARG2 ("N", 1), | |
3334 1, a_nr, b_nr, pa, a_nr, pb, | |
3335 b_nr, px, a_nr, scale, info | |
3336 F77_CHAR_ARG_LEN (1) | |
3337 F77_CHAR_ARG_LEN (1))); | |
1950 | 3338 |
3339 | |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
3340 // FIXME -- check info? |
1819 | 3341 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7478
diff
changeset
|
3342 retval = -ua*cx*ub.transpose (); |
1819 | 3343 |
3344 return retval; | |
3345 } | |
3346 | |
2828 | 3347 // matrix by matrix -> matrix operations |
3348 | |
6162 | 3349 /* Simple Dot Product, Matrix-Vector and Matrix-Matrix Unit tests |
3350 %!assert([1 2 3] * [ 4 ; 5 ; 6], 32, 1e-14) | |
3351 %!assert([1 2 ; 3 4 ] * [5 ; 6], [17 ; 39 ], 1e-14) | |
3352 %!assert([1 2 ; 3 4 ] * [5 6 ; 7 8], [19 22; 43 50], 1e-14) | |
3353 */ | |
3354 | |
3355 /* Test some simple identities | |
3356 %!shared M, cv, rv | |
3357 %! M = randn(10,10); | |
3358 %! cv = randn(10,1); | |
3359 %! rv = randn(1,10); | |
3360 %!assert([M*cv,M*cv],M*[cv,cv],1e-14) | |
3361 %!assert([rv*M;rv*M],[rv;rv]*M,1e-14) | |
3362 %!assert(2*rv*cv,[rv,rv]*[cv;cv],1e-14) | |
3363 */ | |
3364 | |
3365 | |
2828 | 3366 Matrix |
3367 operator * (const Matrix& m, const Matrix& a) | |
3368 { | |
3369 Matrix retval; | |
3370 | |
5275 | 3371 octave_idx_type nr = m.rows (); |
3372 octave_idx_type nc = m.cols (); | |
3373 | |
3374 octave_idx_type a_nr = a.rows (); | |
3375 octave_idx_type a_nc = a.cols (); | |
2828 | 3376 |
3377 if (nc != a_nr) | |
3378 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); | |
3379 else | |
3380 { | |
3381 if (nr == 0 || nc == 0 || a_nc == 0) | |
3382 retval.resize (nr, a_nc, 0.0); | |
3383 else | |
3384 { | |
5275 | 3385 octave_idx_type ld = nr; |
3386 octave_idx_type lda = a_nr; | |
2828 | 3387 |
3388 retval.resize (nr, a_nc); | |
3389 double *c = retval.fortran_vec (); | |
3390 | |
5983 | 3391 if (a_nc == 1) |
3392 { | |
3393 if (nr == 1) | |
3394 F77_FUNC (xddot, XDDOT) (nc, m.data (), 1, a.data (), 1, *c); | |
3395 else | |
6390 | 3396 { |
3397 F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), | |
3398 nr, nc, 1.0, m.data (), ld, | |
3399 a.data (), 1, 0.0, c, 1 | |
3400 F77_CHAR_ARG_LEN (1))); | |
3401 } | |
5983 | 3402 } |
3403 else | |
6390 | 3404 { |
3405 F77_XFCN (dgemm, DGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), | |
3406 F77_CONST_CHAR_ARG2 ("N", 1), | |
3407 nr, a_nc, nc, 1.0, m.data (), | |
3408 ld, a.data (), lda, 0.0, c, nr | |
3409 F77_CHAR_ARG_LEN (1) | |
3410 F77_CHAR_ARG_LEN (1))); | |
3411 } | |
2828 | 3412 } |
3413 } | |
3414 | |
3415 return retval; | |
3416 } | |
3417 | |
5775 | 3418 // FIXME -- it would be nice to share code among the min/max |
4309 | 3419 // functions below. |
3420 | |
3421 #define EMPTY_RETURN_CHECK(T) \ | |
3422 if (nr == 0 || nc == 0) \ | |
3423 return T (nr, nc); | |
3424 | |
3425 Matrix | |
3426 min (double d, const Matrix& m) | |
3427 { | |
5275 | 3428 octave_idx_type nr = m.rows (); |
3429 octave_idx_type nc = m.columns (); | |
4309 | 3430 |
3431 EMPTY_RETURN_CHECK (Matrix); | |
3432 | |
3433 Matrix result (nr, nc); | |
3434 | |
5275 | 3435 for (octave_idx_type j = 0; j < nc; j++) |
3436 for (octave_idx_type i = 0; i < nr; i++) | |
4309 | 3437 { |
3438 OCTAVE_QUIT; | |
3439 result (i, j) = xmin (d, m (i, j)); | |
3440 } | |
3441 | |
3442 return result; | |
3443 } | |
3444 | |
3445 Matrix | |
3446 min (const Matrix& m, double d) | |
3447 { | |
5275 | 3448 octave_idx_type nr = m.rows (); |
3449 octave_idx_type nc = m.columns (); | |
4309 | 3450 |
3451 EMPTY_RETURN_CHECK (Matrix); | |
3452 | |
3453 Matrix result (nr, nc); | |
3454 | |
5275 | 3455 for (octave_idx_type j = 0; j < nc; j++) |
3456 for (octave_idx_type i = 0; i < nr; i++) | |
4309 | 3457 { |
3458 OCTAVE_QUIT; | |
3459 result (i, j) = xmin (m (i, j), d); | |
3460 } | |
3461 | |
3462 return result; | |
3463 } | |
3464 | |
3465 Matrix | |
3466 min (const Matrix& a, const Matrix& b) | |
3467 { | |
5275 | 3468 octave_idx_type nr = a.rows (); |
3469 octave_idx_type nc = a.columns (); | |
4309 | 3470 |
3471 if (nr != b.rows () || nc != b.columns ()) | |
3472 { | |
3473 (*current_liboctave_error_handler) | |
3474 ("two-arg min expecting args of same size"); | |
3475 return Matrix (); | |
3476 } | |
3477 | |
3478 EMPTY_RETURN_CHECK (Matrix); | |
3479 | |
3480 Matrix result (nr, nc); | |
3481 | |
5275 | 3482 for (octave_idx_type j = 0; j < nc; j++) |
3483 for (octave_idx_type i = 0; i < nr; i++) | |
4309 | 3484 { |
3485 OCTAVE_QUIT; | |
3486 result (i, j) = xmin (a (i, j), b (i, j)); | |
3487 } | |
3488 | |
3489 return result; | |
3490 } | |
3491 | |
3492 Matrix | |
3493 max (double d, const Matrix& m) | |
3494 { | |
5275 | 3495 octave_idx_type nr = m.rows (); |
3496 octave_idx_type nc = m.columns (); | |
4309 | 3497 |
3498 EMPTY_RETURN_CHECK (Matrix); | |
3499 | |
3500 Matrix result (nr, nc); | |
3501 | |
5275 | 3502 for (octave_idx_type j = 0; j < nc; j++) |
3503 for (octave_idx_type i = 0; i < nr; i++) | |
4309 | 3504 { |
3505 OCTAVE_QUIT; | |
3506 result (i, j) = xmax (d, m (i, j)); | |
3507 } | |
3508 | |
3509 return result; | |
3510 } | |
3511 | |
3512 Matrix | |
3513 max (const Matrix& m, double d) | |
3514 { | |
5275 | 3515 octave_idx_type nr = m.rows (); |
3516 octave_idx_type nc = m.columns (); | |
4309 | 3517 |
3518 EMPTY_RETURN_CHECK (Matrix); | |
3519 | |
3520 Matrix result (nr, nc); | |
3521 | |
5275 | 3522 for (octave_idx_type j = 0; j < nc; j++) |
3523 for (octave_idx_type i = 0; i < nr; i++) | |
4309 | 3524 { |
3525 OCTAVE_QUIT; | |
3526 result (i, j) = xmax (m (i, j), d); | |
3527 } | |
3528 | |
3529 return result; | |
3530 } | |
3531 | |
3532 Matrix | |
3533 max (const Matrix& a, const Matrix& b) | |
3534 { | |
5275 | 3535 octave_idx_type nr = a.rows (); |
3536 octave_idx_type nc = a.columns (); | |
4309 | 3537 |
3538 if (nr != b.rows () || nc != b.columns ()) | |
3539 { | |
3540 (*current_liboctave_error_handler) | |
3541 ("two-arg max expecting args of same size"); | |
3542 return Matrix (); | |
3543 } | |
3544 | |
3545 EMPTY_RETURN_CHECK (Matrix); | |
3546 | |
3547 Matrix result (nr, nc); | |
3548 | |
5275 | 3549 for (octave_idx_type j = 0; j < nc; j++) |
3550 for (octave_idx_type i = 0; i < nr; i++) | |
4309 | 3551 { |
3552 OCTAVE_QUIT; | |
3553 result (i, j) = xmax (a (i, j), b (i, j)); | |
3554 } | |
3555 | |
3556 return result; | |
3557 } | |
3558 | |
2870 | 3559 MS_CMP_OPS(Matrix, , double, ) |
3504 | 3560 MS_BOOL_OPS(Matrix, double, 0.0) |
2870 | 3561 |
3562 SM_CMP_OPS(double, , Matrix, ) | |
3504 | 3563 SM_BOOL_OPS(double, Matrix, 0.0) |
2870 | 3564 |
3565 MM_CMP_OPS(Matrix, , Matrix, ) | |
3504 | 3566 MM_BOOL_OPS(Matrix, Matrix, 0.0) |
2870 | 3567 |
458 | 3568 /* |
3569 ;;; Local Variables: *** | |
3570 ;;; mode: C++ *** | |
3571 ;;; End: *** | |
3572 */ |