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