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