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