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