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