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