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