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