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