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