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