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