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 |
|
1844 map (d_d_Mapper f, const Matrix& a) |
|
1845 { |
|
1846 Matrix b (a); |
|
1847 b.map (f); |
|
1848 return b; |
|
1849 } |
|
1850 |
1205
|
1851 Matrix |
|
1852 map (d_c_Mapper f, const ComplexMatrix& a) |
|
1853 { |
|
1854 int a_nc = a.cols (); |
|
1855 int a_nr = a.rows (); |
|
1856 Matrix b (a_nr, a_nc); |
|
1857 for (int j = 0; j < a_nc; j++) |
|
1858 for (int i = 0; i < a_nr; i++) |
|
1859 b.elem (i, j) = f (a.elem (i, j)); |
|
1860 return b; |
|
1861 } |
|
1862 |
458
|
1863 void |
|
1864 Matrix::map (d_d_Mapper f) |
|
1865 { |
|
1866 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1867 |
|
1868 for (int i = 0; i < length (); i++) |
|
1869 d[i] = f (d[i]); |
|
1870 } |
|
1871 |
2385
|
1872 bool |
|
1873 Matrix::any_element_is_negative (void) const |
|
1874 { |
|
1875 int nr = rows (); |
|
1876 int nc = cols (); |
|
1877 |
|
1878 for (int j = 0; j < nc; j++) |
|
1879 for (int i = 0; i < nr; i++) |
|
1880 if (elem (i, j) < 0.0) |
|
1881 return true; |
|
1882 |
|
1883 return false; |
|
1884 } |
|
1885 |
|
1886 |
|
1887 bool |
|
1888 Matrix::any_element_is_inf_or_nan (void) const |
|
1889 { |
|
1890 int nr = rows (); |
|
1891 int nc = cols (); |
|
1892 |
|
1893 for (int j = 0; j < nc; j++) |
|
1894 for (int i = 0; i < nr; i++) |
|
1895 { |
|
1896 double val = elem (i, j); |
|
1897 if (xisinf (val) || xisnan (val)) |
|
1898 return 1; |
|
1899 } |
|
1900 return 0; |
|
1901 } |
|
1902 |
|
1903 bool |
|
1904 Matrix::all_elements_are_int_or_inf_or_nan (void) const |
|
1905 { |
|
1906 int nr = rows (); |
|
1907 int nc = cols (); |
|
1908 |
|
1909 for (int j = 0; j < nc; j++) |
|
1910 for (int i = 0; i < nr; i++) |
|
1911 { |
|
1912 double val = elem (i, j); |
|
1913 if (xisnan (val) || D_NINT (val) == val) |
|
1914 continue; |
|
1915 else |
|
1916 return false; |
|
1917 } |
|
1918 |
|
1919 return true; |
|
1920 } |
|
1921 |
1968
|
1922 // Return nonzero if any element of M is not an integer. Also extract |
|
1923 // the largest and smallest values and return them in MAX_VAL and MIN_VAL. |
|
1924 |
2385
|
1925 bool |
1968
|
1926 Matrix::all_integers (double& max_val, double& min_val) const |
|
1927 { |
|
1928 int nr = rows (); |
|
1929 int nc = cols (); |
|
1930 |
|
1931 if (nr > 0 && nc > 0) |
|
1932 { |
|
1933 max_val = elem (0, 0); |
|
1934 min_val = elem (0, 0); |
|
1935 } |
|
1936 else |
2385
|
1937 return false; |
1968
|
1938 |
|
1939 for (int j = 0; j < nc; j++) |
|
1940 for (int i = 0; i < nr; i++) |
|
1941 { |
|
1942 double val = elem (i, j); |
|
1943 |
|
1944 if (val > max_val) |
|
1945 max_val = val; |
|
1946 |
|
1947 if (val < min_val) |
|
1948 min_val = val; |
|
1949 |
|
1950 if (D_NINT (val) != val) |
2385
|
1951 return false; |
1968
|
1952 } |
2385
|
1953 |
|
1954 return true; |
1968
|
1955 } |
|
1956 |
2385
|
1957 bool |
1968
|
1958 Matrix::too_large_for_float (void) const |
|
1959 { |
|
1960 int nr = rows (); |
2385
|
1961 int nc = cols (); |
1968
|
1962 |
|
1963 for (int j = 0; j < nc; j++) |
|
1964 for (int i = 0; i < nr; i++) |
|
1965 { |
|
1966 double val = elem (i, j); |
|
1967 |
|
1968 if (val > FLT_MAX || val < FLT_MIN) |
2385
|
1969 return true; |
1968
|
1970 } |
|
1971 |
2385
|
1972 return false; |
1968
|
1973 } |
|
1974 |
458
|
1975 // XXX FIXME XXX Do these really belong here? They should maybe be |
|
1976 // cleaned up a bit, no? What about corresponding functions for the |
|
1977 // Vectors? |
|
1978 |
|
1979 Matrix |
|
1980 Matrix::all (void) const |
|
1981 { |
|
1982 int nr = rows (); |
|
1983 int nc = cols (); |
|
1984 Matrix retval; |
|
1985 if (nr > 0 && nc > 0) |
|
1986 { |
|
1987 if (nr == 1) |
|
1988 { |
|
1989 retval.resize (1, 1); |
|
1990 retval.elem (0, 0) = 1.0; |
|
1991 for (int j = 0; j < nc; j++) |
|
1992 { |
|
1993 if (elem (0, j) == 0.0) |
|
1994 { |
|
1995 retval.elem (0, 0) = 0.0; |
|
1996 break; |
|
1997 } |
|
1998 } |
|
1999 } |
|
2000 else if (nc == 1) |
|
2001 { |
|
2002 retval.resize (1, 1); |
|
2003 retval.elem (0, 0) = 1.0; |
|
2004 for (int i = 0; i < nr; i++) |
|
2005 { |
|
2006 if (elem (i, 0) == 0.0) |
|
2007 { |
|
2008 retval.elem (0, 0) = 0.0; |
|
2009 break; |
|
2010 } |
|
2011 } |
|
2012 } |
|
2013 else |
|
2014 { |
|
2015 retval.resize (1, nc); |
|
2016 for (int j = 0; j < nc; j++) |
|
2017 { |
|
2018 retval.elem (0, j) = 1.0; |
|
2019 for (int i = 0; i < nr; i++) |
|
2020 { |
|
2021 if (elem (i, j) == 0.0) |
|
2022 { |
|
2023 retval.elem (0, j) = 0.0; |
|
2024 break; |
|
2025 } |
|
2026 } |
|
2027 } |
|
2028 } |
|
2029 } |
|
2030 return retval; |
|
2031 } |
|
2032 |
|
2033 Matrix |
|
2034 Matrix::any (void) const |
|
2035 { |
|
2036 int nr = rows (); |
|
2037 int nc = cols (); |
|
2038 Matrix retval; |
|
2039 if (nr > 0 && nc > 0) |
|
2040 { |
|
2041 if (nr == 1) |
|
2042 { |
|
2043 retval.resize (1, 1); |
|
2044 retval.elem (0, 0) = 0.0; |
|
2045 for (int j = 0; j < nc; j++) |
|
2046 { |
|
2047 if (elem (0, j) != 0.0) |
|
2048 { |
|
2049 retval.elem (0, 0) = 1.0; |
|
2050 break; |
|
2051 } |
|
2052 } |
|
2053 } |
|
2054 else if (nc == 1) |
|
2055 { |
|
2056 retval.resize (1, 1); |
|
2057 retval.elem (0, 0) = 0.0; |
|
2058 for (int i = 0; i < nr; i++) |
|
2059 { |
|
2060 if (elem (i, 0) != 0.0) |
|
2061 { |
|
2062 retval.elem (0, 0) = 1.0; |
|
2063 break; |
|
2064 } |
|
2065 } |
|
2066 } |
|
2067 else |
|
2068 { |
|
2069 retval.resize (1, nc); |
|
2070 for (int j = 0; j < nc; j++) |
|
2071 { |
|
2072 retval.elem (0, j) = 0.0; |
|
2073 for (int i = 0; i < nr; i++) |
|
2074 { |
|
2075 if (elem (i, j) != 0.0) |
|
2076 { |
|
2077 retval.elem (0, j) = 1.0; |
|
2078 break; |
|
2079 } |
|
2080 } |
|
2081 } |
|
2082 } |
|
2083 } |
|
2084 return retval; |
|
2085 } |
|
2086 |
|
2087 Matrix |
|
2088 Matrix::cumprod (void) const |
|
2089 { |
|
2090 Matrix retval; |
|
2091 |
|
2092 int nr = rows (); |
|
2093 int nc = cols (); |
|
2094 |
|
2095 if (nr == 1) |
|
2096 { |
|
2097 retval.resize (1, nc); |
|
2098 if (nc > 0) |
|
2099 { |
|
2100 double prod = elem (0, 0); |
|
2101 for (int j = 0; j < nc; j++) |
|
2102 { |
|
2103 retval.elem (0, j) = prod; |
|
2104 if (j < nc - 1) |
|
2105 prod *= elem (0, j+1); |
|
2106 } |
|
2107 } |
|
2108 } |
|
2109 else if (nc == 1) |
|
2110 { |
|
2111 retval.resize (nr, 1); |
|
2112 if (nr > 0) |
|
2113 { |
|
2114 double prod = elem (0, 0); |
|
2115 for (int i = 0; i < nr; i++) |
|
2116 { |
|
2117 retval.elem (i, 0) = prod; |
|
2118 if (i < nr - 1) |
|
2119 prod *= elem (i+1, 0); |
|
2120 } |
|
2121 } |
|
2122 } |
|
2123 else |
|
2124 { |
|
2125 retval.resize (nr, nc); |
|
2126 if (nr > 0 && nc > 0) |
|
2127 { |
|
2128 for (int j = 0; j < nc; j++) |
|
2129 { |
|
2130 double prod = elem (0, j); |
|
2131 for (int i = 0; i < nr; i++) |
|
2132 { |
|
2133 retval.elem (i, j) = prod; |
|
2134 if (i < nr - 1) |
|
2135 prod *= elem (i+1, j); |
|
2136 } |
|
2137 } |
|
2138 } |
|
2139 } |
|
2140 return retval; |
|
2141 } |
|
2142 |
|
2143 Matrix |
|
2144 Matrix::cumsum (void) const |
|
2145 { |
|
2146 Matrix retval; |
|
2147 |
|
2148 int nr = rows (); |
|
2149 int nc = cols (); |
|
2150 |
|
2151 if (nr == 1) |
|
2152 { |
|
2153 retval.resize (1, nc); |
|
2154 if (nc > 0) |
|
2155 { |
|
2156 double sum = elem (0, 0); |
|
2157 for (int j = 0; j < nc; j++) |
|
2158 { |
|
2159 retval.elem (0, j) = sum; |
|
2160 if (j < nc - 1) |
|
2161 sum += elem (0, j+1); |
|
2162 } |
|
2163 } |
|
2164 } |
|
2165 else if (nc == 1) |
|
2166 { |
|
2167 retval.resize (nr, 1); |
|
2168 if (nr > 0) |
|
2169 { |
|
2170 double sum = elem (0, 0); |
|
2171 for (int i = 0; i < nr; i++) |
|
2172 { |
|
2173 retval.elem (i, 0) = sum; |
|
2174 if (i < nr - 1) |
|
2175 sum += elem (i+1, 0); |
|
2176 } |
|
2177 } |
|
2178 } |
|
2179 else |
|
2180 { |
|
2181 retval.resize (nr, nc); |
|
2182 if (nr > 0 && nc > 0) |
|
2183 { |
|
2184 for (int j = 0; j < nc; j++) |
|
2185 { |
|
2186 double sum = elem (0, j); |
|
2187 for (int i = 0; i < nr; i++) |
|
2188 { |
|
2189 retval.elem (i, j) = sum; |
|
2190 if (i < nr - 1) |
|
2191 sum += elem (i+1, j); |
|
2192 } |
|
2193 } |
|
2194 } |
|
2195 } |
|
2196 return retval; |
|
2197 } |
|
2198 |
|
2199 Matrix |
|
2200 Matrix::prod (void) const |
|
2201 { |
|
2202 Matrix retval; |
|
2203 |
|
2204 int nr = rows (); |
|
2205 int nc = cols (); |
|
2206 |
|
2207 if (nr == 1) |
|
2208 { |
|
2209 retval.resize (1, 1); |
|
2210 retval.elem (0, 0) = 1.0; |
|
2211 for (int j = 0; j < nc; j++) |
|
2212 retval.elem (0, 0) *= elem (0, j); |
|
2213 } |
|
2214 else if (nc == 1) |
|
2215 { |
|
2216 retval.resize (1, 1); |
|
2217 retval.elem (0, 0) = 1.0; |
|
2218 for (int i = 0; i < nr; i++) |
|
2219 retval.elem (0, 0) *= elem (i, 0); |
|
2220 } |
|
2221 else |
|
2222 { |
|
2223 if (nc == 0) |
|
2224 { |
|
2225 retval.resize (1, 1); |
|
2226 retval.elem (0, 0) = 1.0; |
|
2227 } |
|
2228 else |
|
2229 retval.resize (1, nc); |
|
2230 |
|
2231 for (int j = 0; j < nc; j++) |
|
2232 { |
|
2233 retval.elem (0, j) = 1.0; |
|
2234 for (int i = 0; i < nr; i++) |
|
2235 retval.elem (0, j) *= elem (i, j); |
|
2236 } |
|
2237 } |
|
2238 return retval; |
|
2239 } |
|
2240 |
|
2241 Matrix |
|
2242 Matrix::sum (void) const |
|
2243 { |
|
2244 Matrix retval; |
|
2245 |
|
2246 int nr = rows (); |
|
2247 int nc = cols (); |
|
2248 |
|
2249 if (nr == 1) |
|
2250 { |
|
2251 retval.resize (1, 1); |
|
2252 retval.elem (0, 0) = 0.0; |
|
2253 for (int j = 0; j < nc; j++) |
|
2254 retval.elem (0, 0) += elem (0, j); |
|
2255 } |
|
2256 else if (nc == 1) |
|
2257 { |
|
2258 retval.resize (1, 1); |
|
2259 retval.elem (0, 0) = 0.0; |
|
2260 for (int i = 0; i < nr; i++) |
|
2261 retval.elem (0, 0) += elem (i, 0); |
|
2262 } |
|
2263 else |
|
2264 { |
|
2265 if (nc == 0) |
|
2266 { |
|
2267 retval.resize (1, 1); |
|
2268 retval.elem (0, 0) = 0.0; |
|
2269 } |
|
2270 else |
|
2271 retval.resize (1, nc); |
|
2272 |
|
2273 for (int j = 0; j < nc; j++) |
|
2274 { |
|
2275 retval.elem (0, j) = 0.0; |
|
2276 for (int i = 0; i < nr; i++) |
|
2277 retval.elem (0, j) += elem (i, j); |
|
2278 } |
|
2279 } |
|
2280 return retval; |
|
2281 } |
|
2282 |
|
2283 Matrix |
|
2284 Matrix::sumsq (void) const |
|
2285 { |
|
2286 Matrix retval; |
|
2287 |
|
2288 int nr = rows (); |
|
2289 int nc = cols (); |
|
2290 |
|
2291 if (nr == 1) |
|
2292 { |
|
2293 retval.resize (1, 1); |
|
2294 retval.elem (0, 0) = 0.0; |
|
2295 for (int j = 0; j < nc; j++) |
|
2296 { |
|
2297 double d = elem (0, j); |
|
2298 retval.elem (0, 0) += d * d; |
|
2299 } |
|
2300 } |
|
2301 else if (nc == 1) |
|
2302 { |
|
2303 retval.resize (1, 1); |
|
2304 retval.elem (0, 0) = 0.0; |
|
2305 for (int i = 0; i < nr; i++) |
|
2306 { |
|
2307 double d = elem (i, 0); |
|
2308 retval.elem (0, 0) += d * d; |
|
2309 } |
|
2310 } |
|
2311 else |
|
2312 { |
|
2313 retval.resize (1, nc); |
|
2314 for (int j = 0; j < nc; j++) |
|
2315 { |
|
2316 retval.elem (0, j) = 0.0; |
|
2317 for (int i = 0; i < nr; i++) |
|
2318 { |
|
2319 double d = elem (i, j); |
|
2320 retval.elem (0, j) += d * d; |
|
2321 } |
|
2322 } |
|
2323 } |
|
2324 return retval; |
|
2325 } |
|
2326 |
2385
|
2327 Matrix |
|
2328 Matrix::abs (void) const |
|
2329 { |
|
2330 int nr = rows (); |
|
2331 int nc = cols (); |
|
2332 |
|
2333 Matrix retval (nr, nc); |
|
2334 |
|
2335 for (int j = 0; j < nc; j++) |
|
2336 for (int i = 0; i < nr; i++) |
|
2337 retval (i, j) = fabs (elem (i, j)); |
|
2338 |
|
2339 return retval; |
|
2340 } |
|
2341 |
458
|
2342 ColumnVector |
|
2343 Matrix::diag (void) const |
|
2344 { |
|
2345 return diag (0); |
|
2346 } |
|
2347 |
|
2348 ColumnVector |
|
2349 Matrix::diag (int k) const |
|
2350 { |
|
2351 int nnr = rows (); |
|
2352 int nnc = cols (); |
|
2353 if (k > 0) |
|
2354 nnc -= k; |
|
2355 else if (k < 0) |
|
2356 nnr += k; |
|
2357 |
|
2358 ColumnVector d; |
|
2359 |
|
2360 if (nnr > 0 && nnc > 0) |
|
2361 { |
|
2362 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
2363 |
|
2364 d.resize (ndiag); |
|
2365 |
|
2366 if (k > 0) |
|
2367 { |
|
2368 for (int i = 0; i < ndiag; i++) |
|
2369 d.elem (i) = elem (i, i+k); |
|
2370 } |
|
2371 else if ( k < 0) |
|
2372 { |
|
2373 for (int i = 0; i < ndiag; i++) |
|
2374 d.elem (i) = elem (i-k, i); |
|
2375 } |
|
2376 else |
|
2377 { |
|
2378 for (int i = 0; i < ndiag; i++) |
|
2379 d.elem (i) = elem (i, i); |
|
2380 } |
|
2381 } |
|
2382 else |
|
2383 cerr << "diag: requested diagonal out of range\n"; |
|
2384 |
|
2385 return d; |
|
2386 } |
|
2387 |
|
2388 ColumnVector |
|
2389 Matrix::row_min (void) const |
|
2390 { |
2354
|
2391 Array<int> index; |
|
2392 return row_min (index); |
458
|
2393 } |
|
2394 |
|
2395 ColumnVector |
2354
|
2396 Matrix::row_min (Array<int>& index) const |
458
|
2397 { |
|
2398 ColumnVector result; |
|
2399 |
|
2400 int nr = rows (); |
|
2401 int nc = cols (); |
|
2402 |
|
2403 if (nr > 0 && nc > 0) |
|
2404 { |
|
2405 result.resize (nr); |
2354
|
2406 index.resize (nr); |
458
|
2407 |
|
2408 for (int i = 0; i < nr; i++) |
|
2409 { |
2354
|
2410 int idx = 0; |
|
2411 |
|
2412 double tmp_min = elem (i, idx); |
|
2413 |
|
2414 if (xisnan (tmp_min)) |
|
2415 idx = -1; |
|
2416 else |
|
2417 { |
|
2418 for (int j = 1; j < nc; j++) |
|
2419 { |
|
2420 double tmp = elem (i, j); |
|
2421 |
|
2422 if (xisnan (tmp)) |
|
2423 { |
|
2424 idx = -1; |
|
2425 break; |
|
2426 } |
|
2427 else if (tmp < tmp_min) |
|
2428 { |
|
2429 idx = j; |
|
2430 tmp_min = tmp; |
|
2431 } |
|
2432 } |
|
2433 } |
|
2434 |
|
2435 result.elem (i) = (idx < 0) ? octave_NaN : tmp_min; |
|
2436 index.elem (i) = idx; |
458
|
2437 } |
|
2438 } |
|
2439 |
|
2440 return result; |
|
2441 } |
|
2442 |
|
2443 ColumnVector |
|
2444 Matrix::row_max (void) const |
|
2445 { |
2354
|
2446 Array<int> index; |
|
2447 return row_max (index); |
458
|
2448 } |
|
2449 |
|
2450 ColumnVector |
2354
|
2451 Matrix::row_max (Array<int>& index) const |
458
|
2452 { |
|
2453 ColumnVector result; |
|
2454 |
|
2455 int nr = rows (); |
|
2456 int nc = cols (); |
|
2457 |
|
2458 if (nr > 0 && nc > 0) |
|
2459 { |
|
2460 result.resize (nr); |
2354
|
2461 index.resize (nr); |
458
|
2462 |
|
2463 for (int i = 0; i < nr; i++) |
|
2464 { |
2354
|
2465 int idx = 0; |
|
2466 |
|
2467 double tmp_max = elem (i, idx); |
|
2468 |
|
2469 if (xisnan (tmp_max)) |
|
2470 idx = -1; |
|
2471 else |
|
2472 { |
|
2473 for (int j = 1; j < nc; j++) |
|
2474 { |
|
2475 double tmp = elem (i, j); |
|
2476 |
|
2477 if (xisnan (tmp)) |
|
2478 { |
|
2479 idx = -1; |
|
2480 break; |
|
2481 } |
|
2482 else if (tmp > tmp_max) |
|
2483 { |
|
2484 idx = j; |
|
2485 tmp_max = tmp; |
|
2486 } |
|
2487 } |
|
2488 } |
|
2489 |
|
2490 result.elem (i) = (idx < 0) ? octave_NaN : tmp_max; |
|
2491 index.elem (i) = idx; |
458
|
2492 } |
|
2493 } |
|
2494 |
|
2495 return result; |
|
2496 } |
|
2497 |
|
2498 RowVector |
|
2499 Matrix::column_min (void) const |
|
2500 { |
2354
|
2501 Array<int> index; |
|
2502 return column_min (index); |
458
|
2503 } |
2354
|
2504 |
458
|
2505 RowVector |
2354
|
2506 Matrix::column_min (Array<int>& index) const |
458
|
2507 { |
|
2508 RowVector result; |
|
2509 |
|
2510 int nr = rows (); |
|
2511 int nc = cols (); |
|
2512 |
|
2513 if (nr > 0 && nc > 0) |
|
2514 { |
|
2515 result.resize (nc); |
2354
|
2516 index.resize (nc); |
458
|
2517 |
|
2518 for (int j = 0; j < nc; j++) |
|
2519 { |
2354
|
2520 int idx = 0; |
|
2521 |
|
2522 double tmp_min = elem (idx, j); |
|
2523 |
|
2524 if (xisnan (tmp_min)) |
|
2525 idx = -1; |
|
2526 else |
|
2527 { |
|
2528 for (int i = 1; i < nr; i++) |
|
2529 { |
|
2530 double tmp = elem (i, j); |
|
2531 |
|
2532 if (xisnan (tmp)) |
|
2533 { |
|
2534 idx = -1; |
|
2535 break; |
|
2536 } |
|
2537 else if (tmp < tmp_min) |
|
2538 { |
|
2539 idx = i; |
|
2540 tmp_min = tmp; |
|
2541 } |
|
2542 } |
|
2543 } |
|
2544 |
|
2545 result.elem (j) = (idx < 0) ? octave_NaN : tmp_min; |
|
2546 index.elem (j) = idx; |
458
|
2547 } |
|
2548 } |
|
2549 |
|
2550 return result; |
|
2551 } |
|
2552 |
2354
|
2553 RowVector |
|
2554 Matrix::column_max (void) const |
|
2555 { |
|
2556 Array<int> index; |
|
2557 return column_max (index); |
|
2558 } |
458
|
2559 |
|
2560 RowVector |
2354
|
2561 Matrix::column_max (Array<int>& index) const |
458
|
2562 { |
|
2563 RowVector result; |
|
2564 |
|
2565 int nr = rows (); |
|
2566 int nc = cols (); |
|
2567 |
|
2568 if (nr > 0 && nc > 0) |
|
2569 { |
|
2570 result.resize (nc); |
2354
|
2571 index.resize (nc); |
458
|
2572 |
|
2573 for (int j = 0; j < nc; j++) |
|
2574 { |
2354
|
2575 int idx = 0; |
|
2576 |
|
2577 double tmp_max = elem (idx, j); |
|
2578 |
|
2579 if (xisnan (tmp_max)) |
|
2580 idx = -1; |
|
2581 else |
|
2582 { |
|
2583 for (int i = 1; i < nr; i++) |
|
2584 { |
|
2585 double tmp = elem (i, j); |
|
2586 |
|
2587 if (xisnan (tmp)) |
|
2588 { |
|
2589 idx = -1; |
|
2590 break; |
|
2591 } |
|
2592 else if (tmp > tmp_max) |
|
2593 { |
|
2594 idx = i; |
|
2595 tmp_max = tmp; |
|
2596 } |
|
2597 } |
|
2598 } |
|
2599 |
|
2600 result.elem (j) = (idx < 0) ? octave_NaN : tmp_max; |
|
2601 index.elem (j) = idx; |
458
|
2602 } |
|
2603 } |
|
2604 |
|
2605 return result; |
|
2606 } |
|
2607 |
|
2608 ostream& |
|
2609 operator << (ostream& os, const Matrix& a) |
|
2610 { |
|
2611 // int field_width = os.precision () + 7; |
1360
|
2612 |
458
|
2613 for (int i = 0; i < a.rows (); i++) |
|
2614 { |
|
2615 for (int j = 0; j < a.cols (); j++) |
|
2616 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
2617 os << "\n"; |
|
2618 } |
|
2619 return os; |
|
2620 } |
|
2621 |
|
2622 istream& |
|
2623 operator >> (istream& is, Matrix& a) |
|
2624 { |
|
2625 int nr = a.rows (); |
|
2626 int nc = a.cols (); |
|
2627 |
|
2628 if (nr < 1 || nc < 1) |
|
2629 is.clear (ios::badbit); |
|
2630 else |
|
2631 { |
|
2632 double tmp; |
|
2633 for (int i = 0; i < nr; i++) |
|
2634 for (int j = 0; j < nc; j++) |
|
2635 { |
|
2636 is >> tmp; |
|
2637 if (is) |
|
2638 a.elem (i, j) = tmp; |
|
2639 else |
|
2640 break; |
|
2641 } |
|
2642 } |
|
2643 |
|
2644 return is; |
|
2645 } |
|
2646 |
2317
|
2647 template <class T> |
|
2648 static void |
|
2649 read_int (istream& is, bool swap_bytes, T& val) |
|
2650 { |
|
2651 is.read ((char *) &val, sizeof (T)); |
|
2652 |
|
2653 if (swap_bytes) |
|
2654 { |
|
2655 switch (sizeof (T)) |
|
2656 { |
|
2657 case 1: |
|
2658 break; |
|
2659 |
|
2660 case 2: |
|
2661 swap_2_bytes ((char *) &val); |
|
2662 break; |
|
2663 |
|
2664 case 4: |
|
2665 swap_4_bytes ((char *) &val); |
|
2666 break; |
|
2667 |
|
2668 case 8: |
|
2669 swap_8_bytes ((char *) &val); |
|
2670 break; |
|
2671 |
|
2672 default: |
|
2673 (*current_liboctave_error_handler) |
|
2674 ("read_int: unrecognized data format!"); |
|
2675 } |
|
2676 } |
|
2677 } |
|
2678 |
|
2679 template void read_int (istream&, bool, char&); |
|
2680 template void read_int (istream&, bool, signed char&); |
|
2681 template void read_int (istream&, bool, unsigned char&); |
|
2682 template void read_int (istream&, bool, short&); |
|
2683 template void read_int (istream&, bool, unsigned short&); |
|
2684 template void read_int (istream&, bool, int&); |
|
2685 template void read_int (istream&, bool, unsigned int&); |
|
2686 template void read_int (istream&, bool, long&); |
|
2687 template void read_int (istream&, bool, unsigned long&); |
|
2688 |
|
2689 static inline bool |
|
2690 do_read (istream& is, oct_data_conv::data_type dt, |
|
2691 oct_mach_info::float_format flt_fmt, bool swap_bytes, |
|
2692 bool do_float_conversion, double& val) |
|
2693 { |
|
2694 bool retval = true; |
|
2695 |
|
2696 switch (dt) |
|
2697 { |
|
2698 case oct_data_conv::dt_char: |
|
2699 { |
|
2700 char tmp; |
|
2701 read_int (is, swap_bytes, tmp); |
|
2702 val = tmp; |
|
2703 } |
|
2704 break; |
|
2705 |
|
2706 case oct_data_conv::dt_schar: |
|
2707 { |
|
2708 signed char tmp; |
|
2709 read_int (is, swap_bytes, tmp); |
|
2710 val = tmp; |
|
2711 } |
|
2712 break; |
|
2713 |
|
2714 case oct_data_conv::dt_uchar: |
|
2715 { |
|
2716 unsigned char tmp; |
|
2717 read_int (is, swap_bytes, tmp); |
|
2718 val = tmp; |
|
2719 } |
|
2720 break; |
|
2721 |
|
2722 case oct_data_conv::dt_short: |
|
2723 { |
|
2724 short tmp; |
|
2725 read_int (is, swap_bytes, tmp); |
|
2726 val = tmp; |
|
2727 } |
|
2728 break; |
|
2729 |
|
2730 case oct_data_conv::dt_ushort: |
|
2731 { |
|
2732 unsigned short tmp; |
|
2733 read_int (is, swap_bytes, tmp); |
|
2734 val = tmp; |
|
2735 } |
|
2736 break; |
|
2737 |
|
2738 case oct_data_conv::dt_int: |
|
2739 { |
|
2740 int tmp; |
|
2741 read_int (is, swap_bytes, tmp); |
|
2742 val = tmp; |
|
2743 } |
|
2744 break; |
|
2745 |
|
2746 case oct_data_conv::dt_uint: |
|
2747 { |
|
2748 unsigned int tmp; |
|
2749 read_int (is, swap_bytes, tmp); |
|
2750 val = tmp; |
|
2751 } |
|
2752 break; |
|
2753 |
|
2754 case oct_data_conv::dt_long: |
|
2755 { |
|
2756 long tmp; |
|
2757 read_int (is, swap_bytes, tmp); |
|
2758 val = tmp; |
|
2759 } |
|
2760 break; |
|
2761 |
|
2762 case oct_data_conv::dt_ulong: |
|
2763 { |
|
2764 unsigned long tmp; |
|
2765 read_int (is, swap_bytes, tmp); |
|
2766 val = tmp; |
|
2767 } |
|
2768 break; |
|
2769 |
|
2770 case oct_data_conv::dt_float: |
|
2771 { |
|
2772 float f; |
|
2773 |
|
2774 is.read ((char *) &f, sizeof (float)); |
|
2775 |
|
2776 if (do_float_conversion) |
|
2777 do_float_format_conversion (&f, 1, flt_fmt); |
|
2778 |
|
2779 val = f; |
|
2780 } |
|
2781 break; |
|
2782 |
|
2783 case oct_data_conv::dt_double: |
|
2784 { |
|
2785 is.read ((char *) &val, sizeof (double)); |
|
2786 |
|
2787 if (do_float_conversion) |
|
2788 do_double_format_conversion (&val, 1, flt_fmt); |
|
2789 } |
|
2790 break; |
|
2791 |
|
2792 default: |
|
2793 retval = false; |
|
2794 (*current_liboctave_error_handler) |
|
2795 ("read: invalid type specification"); |
|
2796 break; |
|
2797 } |
|
2798 |
|
2799 return retval; |
|
2800 } |
1360
|
2801 |
458
|
2802 int |
2317
|
2803 Matrix::read (istream& is, int nr, int nc, |
|
2804 oct_data_conv::data_type dt, int skip, |
|
2805 oct_mach_info::float_format flt_fmt) |
458
|
2806 { |
2317
|
2807 int retval = -1; |
|
2808 |
|
2809 bool ok = true; |
|
2810 |
|
2811 int count = 0; |
|
2812 |
|
2813 double *data = 0; |
|
2814 int max_size = 0; |
|
2815 |
|
2816 int final_nr = 0; |
|
2817 int final_nc = 0; |
|
2818 |
|
2819 if (nr > 0) |
458
|
2820 { |
2317
|
2821 if (nc > 0) |
|
2822 { |
|
2823 resize (nr, nc, 0.0); |
|
2824 data = fortran_vec (); |
|
2825 max_size = nr * nc; |
|
2826 } |
|
2827 else |
|
2828 { |
|
2829 resize (nr, 32, 0.0); |
|
2830 data = fortran_vec (); |
|
2831 max_size = nr * 32; |
|
2832 } |
|
2833 } |
471
|
2834 else |
|
2835 { |
2317
|
2836 resize (32, 1, 0.0); |
|
2837 data = fortran_vec (); |
|
2838 max_size = 32; |
|
2839 } |
|
2840 |
|
2841 oct_mach_info::float_format native_flt_fmt |
|
2842 = oct_mach_info::float_format (); |
|
2843 |
|
2844 bool do_float_conversion = (flt_fmt != native_flt_fmt); |
|
2845 |
|
2846 // XXX FIXME XXX -- byte order for Cray? |
|
2847 |
|
2848 bool swap_bytes = false; |
|
2849 |
|
2850 if (oct_mach_info::words_big_endian ()) |
|
2851 swap_bytes = (flt_fmt == oct_mach_info::ieee_little_endian |
|
2852 || flt_fmt == oct_mach_info::vax_g |
|
2853 || flt_fmt == oct_mach_info::vax_g); |
|
2854 else |
|
2855 swap_bytes = (flt_fmt == oct_mach_info::ieee_big_endian); |
|
2856 |
|
2857 for (;;) |
|
2858 { |
|
2859 // XXX FIXME XXX -- maybe there should be a special case for |
|
2860 // skip == 0. |
|
2861 |
|
2862 if (is) |
|
2863 { |
|
2864 if (nr > 0 && nc > 0 && count == max_size) |
|
2865 { |
|
2866 final_nr = nr; |
|
2867 final_nc = nc; |
|
2868 |
|
2869 break; |
|
2870 } |
|
2871 |
|
2872 if (skip != 0) |
|
2873 is.seekg (skip, ios::cur); |
|
2874 |
|
2875 if (is) |
|
2876 { |
|
2877 double tmp = 0.0; |
|
2878 |
|
2879 ok = do_read (is, dt, flt_fmt, swap_bytes, |
|
2880 do_float_conversion, tmp); |
|
2881 |
|
2882 if (ok) |
|
2883 { |
|
2884 if (is) |
|
2885 { |
|
2886 if (count == max_size) |
|
2887 { |
|
2888 max_size *= 2; |
|
2889 |
|
2890 if (nr > 0) |
|
2891 resize (nr, max_size / 2, 0.0); |
|
2892 else |
|
2893 resize (max_size, 1, 0.0); |
|
2894 |
|
2895 data = fortran_vec (); |
|
2896 } |
|
2897 |
|
2898 data[count++] = tmp; |
|
2899 } |
|
2900 else |
|
2901 { |
|
2902 if (is.eof ()) |
|
2903 { |
|
2904 if (nr > 0) |
|
2905 { |
|
2906 if (count > nr) |
|
2907 { |
|
2908 final_nr = nr; |
|
2909 final_nc = (count - 1) / nr + 1; |
|
2910 } |
|
2911 else |
|
2912 { |
|
2913 final_nr = count; |
|
2914 final_nc = 1; |
|
2915 } |
|
2916 } |
|
2917 else |
|
2918 { |
|
2919 final_nr = count; |
|
2920 final_nc = 1; |
|
2921 } |
|
2922 } |
|
2923 |
|
2924 break; |
|
2925 } |
|
2926 } |
|
2927 else |
|
2928 break; |
|
2929 } |
|
2930 else |
|
2931 { |
|
2932 ok = false; |
|
2933 break; |
|
2934 } |
|
2935 } |
|
2936 else |
|
2937 { |
|
2938 ok = false; |
|
2939 break; |
|
2940 } |
|
2941 } |
|
2942 |
|
2943 if (ok) |
|
2944 { |
|
2945 resize (final_nr, final_nc, 0.0); |
|
2946 |
|
2947 retval = count; |
458
|
2948 } |
|
2949 |
2317
|
2950 return retval; |
|
2951 } |
|
2952 |
|
2953 template <class T> |
|
2954 static void |
|
2955 write_int (ostream& os, bool swap_bytes, T val) |
|
2956 { |
|
2957 if (swap_bytes) |
|
2958 { |
|
2959 switch (sizeof (T)) |
|
2960 { |
|
2961 case 1: |
|
2962 break; |
|
2963 |
|
2964 case 2: |
|
2965 swap_2_bytes ((char *) &val); |
|
2966 break; |
|
2967 |
|
2968 case 4: |
|
2969 swap_4_bytes ((char *) &val); |
|
2970 break; |
|
2971 |
|
2972 case 8: |
|
2973 swap_8_bytes ((char *) &val); |
|
2974 break; |
|
2975 |
|
2976 default: |
|
2977 (*current_liboctave_error_handler) |
|
2978 ("write_int: unrecognized data format!"); |
|
2979 } |
|
2980 } |
|
2981 |
|
2982 os.write ((char *) &val, sizeof (T)); |
458
|
2983 } |
|
2984 |
2317
|
2985 template void write_int (ostream&, bool, char); |
|
2986 template void write_int (ostream&, bool, signed char); |
|
2987 template void write_int (ostream&, bool, unsigned char); |
|
2988 template void write_int (ostream&, bool, short); |
|
2989 template void write_int (ostream&, bool, unsigned short); |
|
2990 template void write_int (ostream&, bool, int); |
|
2991 template void write_int (ostream&, bool, unsigned int); |
|
2992 template void write_int (ostream&, bool, long); |
|
2993 template void write_int (ostream&, bool, unsigned long); |
|
2994 |
|
2995 static inline bool |
|
2996 do_write (ostream& os, double d, oct_data_conv::data_type dt, |
|
2997 oct_mach_info::float_format flt_fmt, bool swap_bytes, |
|
2998 bool do_float_conversion) |
|
2999 { |
|
3000 bool retval = true; |
|
3001 |
|
3002 switch (dt) |
|
3003 { |
|
3004 case oct_data_conv::dt_char: |
|
3005 write_int (os, swap_bytes, (char) d); |
|
3006 break; |
|
3007 |
|
3008 case oct_data_conv::dt_schar: |
|
3009 write_int (os, swap_bytes, (signed char) d); |
|
3010 break; |
|
3011 |
|
3012 case oct_data_conv::dt_uchar: |
|
3013 write_int (os, swap_bytes, (unsigned char) d); |
|
3014 break; |
|
3015 |
|
3016 case oct_data_conv::dt_short: |
|
3017 write_int (os, swap_bytes, (short) d); |
|
3018 break; |
|
3019 |
|
3020 case oct_data_conv::dt_ushort: |
|
3021 write_int (os, swap_bytes, (unsigned short) d); |
|
3022 break; |
|
3023 |
|
3024 case oct_data_conv::dt_int: |
|
3025 write_int (os, swap_bytes, (int) d); |
|
3026 break; |
|
3027 |
|
3028 case oct_data_conv::dt_uint: |
|
3029 write_int (os, swap_bytes, (unsigned int) d); |
|
3030 break; |
|
3031 |
|
3032 case oct_data_conv::dt_long: |
|
3033 write_int (os, swap_bytes, (long) d); |
|
3034 break; |
|
3035 |
|
3036 case oct_data_conv::dt_ulong: |
|
3037 write_int (os, swap_bytes, (unsigned long) d); |
|
3038 break; |
|
3039 |
|
3040 case oct_data_conv::dt_float: |
|
3041 { |
|
3042 float f = (float) d; |
|
3043 |
|
3044 if (do_float_conversion) |
|
3045 do_float_format_conversion (&f, 1, flt_fmt); |
|
3046 |
|
3047 os.write ((char *) &f, sizeof (float)); |
|
3048 } |
|
3049 break; |
|
3050 |
|
3051 case oct_data_conv::dt_double: |
|
3052 { |
|
3053 if (do_float_conversion) |
|
3054 do_double_format_conversion (&d, 1, flt_fmt); |
|
3055 |
|
3056 os.write ((char *) &d, sizeof (double)); |
|
3057 } |
|
3058 break; |
|
3059 |
|
3060 default: |
|
3061 retval = false; |
|
3062 (*current_liboctave_error_handler) |
|
3063 ("write: invalid type specification"); |
|
3064 break; |
|
3065 } |
|
3066 |
|
3067 return retval; |
|
3068 } |
1360
|
3069 |
458
|
3070 int |
2317
|
3071 Matrix::write (ostream& os, oct_data_conv::data_type dt, int skip, |
|
3072 oct_mach_info::float_format flt_fmt) |
458
|
3073 { |
2317
|
3074 int retval = -1; |
|
3075 |
|
3076 bool ok = true; |
|
3077 |
|
3078 int count = 0; |
|
3079 |
|
3080 const double *d = data (); |
|
3081 |
|
3082 int n = length (); |
|
3083 |
|
3084 oct_mach_info::float_format native_flt_fmt |
|
3085 = oct_mach_info::float_format (); |
|
3086 |
|
3087 bool do_float_conversion = (flt_fmt != native_flt_fmt); |
|
3088 |
|
3089 // XXX FIXME XXX -- byte order for Cray? |
|
3090 |
|
3091 bool swap_bytes = false; |
|
3092 |
|
3093 if (oct_mach_info::words_big_endian ()) |
|
3094 swap_bytes = (flt_fmt == oct_mach_info::ieee_little_endian |
|
3095 || flt_fmt == oct_mach_info::vax_g |
|
3096 || flt_fmt == oct_mach_info::vax_g); |
|
3097 else |
|
3098 swap_bytes = (flt_fmt == oct_mach_info::ieee_big_endian); |
|
3099 |
|
3100 for (int i = 0; i < n; i++) |
458
|
3101 { |
2317
|
3102 if (os) |
|
3103 { |
|
3104 if (skip != 0) |
|
3105 os.seekp (skip, ios::cur); |
|
3106 |
|
3107 if (os) |
|
3108 { |
|
3109 ok = do_write (os, d[i], dt, flt_fmt, swap_bytes, |
|
3110 do_float_conversion); |
|
3111 |
|
3112 if (os && ok) |
|
3113 count++; |
|
3114 else |
|
3115 break; |
|
3116 } |
|
3117 else |
|
3118 { |
|
3119 ok = false; |
|
3120 break; |
|
3121 } |
|
3122 } |
|
3123 else |
|
3124 { |
|
3125 ok = false; |
|
3126 break; |
|
3127 } |
471
|
3128 } |
458
|
3129 |
2317
|
3130 if (ok) |
|
3131 retval = count; |
|
3132 |
|
3133 return retval; |
458
|
3134 } |
|
3135 |
2317
|
3136 |
|
3137 |
1819
|
3138 Matrix |
|
3139 Givens (double x, double y) |
|
3140 { |
|
3141 double cc, s, temp_r; |
|
3142 |
|
3143 F77_FCN (dlartg, DLARTG) (x, y, cc, s, temp_r); |
|
3144 |
|
3145 Matrix g (2, 2); |
|
3146 |
|
3147 g.elem (0, 0) = cc; |
|
3148 g.elem (1, 1) = cc; |
|
3149 g.elem (0, 1) = s; |
|
3150 g.elem (1, 0) = -s; |
|
3151 |
|
3152 return g; |
|
3153 } |
|
3154 |
|
3155 Matrix |
|
3156 Sylvester (const Matrix& a, const Matrix& b, const Matrix& c) |
|
3157 { |
|
3158 Matrix retval; |
|
3159 |
|
3160 // XXX FIXME XXX -- need to check that a, b, and c are all the same |
|
3161 // size. |
|
3162 |
|
3163 // Compute Schur decompositions. |
|
3164 |
|
3165 SCHUR as (a, "U"); |
|
3166 SCHUR bs (b, "U"); |
|
3167 |
|
3168 // Transform c to new coordinates. |
|
3169 |
|
3170 Matrix ua = as.unitary_matrix (); |
|
3171 Matrix sch_a = as.schur_matrix (); |
|
3172 |
|
3173 Matrix ub = bs.unitary_matrix (); |
|
3174 Matrix sch_b = bs.schur_matrix (); |
|
3175 |
|
3176 Matrix cx = ua.transpose () * c * ub; |
|
3177 |
|
3178 // Solve the sylvester equation, back-transform, and return the |
|
3179 // solution. |
|
3180 |
|
3181 int a_nr = a.rows (); |
|
3182 int b_nr = b.rows (); |
|
3183 |
|
3184 double scale; |
|
3185 int info; |
|
3186 |
1950
|
3187 double *pa = sch_a.fortran_vec (); |
|
3188 double *pb = sch_b.fortran_vec (); |
|
3189 double *px = cx.fortran_vec (); |
|
3190 |
|
3191 F77_XFCN (dtrsyl, DTRSYL, ("N", "N", 1, a_nr, b_nr, pa, a_nr, pb, |
|
3192 b_nr, px, a_nr, scale, info, 1L, 1L)); |
|
3193 |
|
3194 |
|
3195 if (f77_exception_encountered) |
|
3196 (*current_liboctave_error_handler) ("unrecoverable error in dtrsyl"); |
|
3197 else |
|
3198 { |
|
3199 // XXX FIXME XXX -- check info? |
1819
|
3200 |
1950
|
3201 retval = -ua*cx*ub.transpose (); |
|
3202 } |
1819
|
3203 |
|
3204 return retval; |
|
3205 } |
|
3206 |
1959
|
3207 ComplexColumnVector |
|
3208 Qzval (const Matrix& a, const Matrix& b) |
|
3209 { |
|
3210 ComplexColumnVector retval; |
|
3211 |
|
3212 int a_nr = a.rows(); |
|
3213 int a_nc = a.cols(); |
|
3214 |
|
3215 int b_nr = b.rows(); |
|
3216 int b_nc = b.cols(); |
|
3217 |
|
3218 if (a_nr == a_nc) |
|
3219 { |
|
3220 if (a_nr == b_nr && a_nc == b_nc) |
|
3221 { |
|
3222 if (a_nr != 0) |
|
3223 { |
|
3224 Matrix jnk (a_nr, a_nr, 0.0); |
|
3225 double *pjnk = jnk.fortran_vec (); |
|
3226 |
|
3227 ColumnVector alfr (a_nr); |
|
3228 double *palfr = alfr.fortran_vec (); |
|
3229 |
|
3230 ColumnVector alfi (a_nr); |
|
3231 double *palfi = alfr.fortran_vec (); |
|
3232 |
|
3233 ColumnVector beta (a_nr); |
|
3234 double *pbeta = alfr.fortran_vec (); |
|
3235 |
|
3236 Matrix atmp = a; |
|
3237 double *pa = atmp.fortran_vec (); |
|
3238 |
|
3239 Matrix btmp = b; |
|
3240 double *pb = btmp.fortran_vec (); |
|
3241 |
|
3242 long matz = 0; |
|
3243 int info; |
|
3244 |
|
3245 // XXX FIXME ??? XXX |
|
3246 double eps = DBL_EPSILON; |
|
3247 |
|
3248 F77_FCN (qzhes, QZHES) (a_nr, a_nr, pa, pb, matz, pjnk); |
|
3249 |
|
3250 F77_FCN (qzit, QZIT) (a_nr, a_nr, pa, pb, eps, matz, pjnk, info); |
|
3251 |
|
3252 if (! info) |
|
3253 { |
|
3254 F77_FCN (qzval, QZVAL) (a_nr, a_nr, pa, pb, palfr, |
|
3255 palfi, pbeta, matz, pjnk); |
|
3256 |
|
3257 // Count and extract finite generalized eigenvalues. |
|
3258 |
|
3259 int cnt = 0; |
|
3260 |
|
3261 for (int i = 0; i < a_nr; i++) |
|
3262 if (beta.elem (i) != 0) |
|
3263 cnt++; |
|
3264 |
|
3265 ComplexColumnVector cx (cnt, 0.0); |
|
3266 |
|
3267 Complex Im (0, 1); |
|
3268 |
|
3269 for (int i = 0; i < a_nr; i++) |
|
3270 { |
|
3271 if (beta.elem (i) != 0) |
|
3272 { |
|
3273 // Finite generalized eigenvalue. |
|
3274 |
|
3275 cnt--; |
|
3276 cx.elem (cnt) = (alfr.elem (i) + Im * alfi.elem (i)) |
|
3277 / beta.elem (i); |
|
3278 } |
|
3279 } |
|
3280 |
|
3281 retval = cx; |
|
3282 } |
|
3283 else |
|
3284 (*current_liboctave_error_handler) |
|
3285 ("qzval: trouble in qzit, info = %d", info); |
|
3286 } |
|
3287 } |
|
3288 else |
2385
|
3289 gripe_nonconformant ("qzval", a_nr, a_nc, b_nr, b_nc); |
1959
|
3290 } |
|
3291 else |
|
3292 (*current_liboctave_error_handler) ("qzval: square matrices required"); |
|
3293 |
|
3294 return retval; |
|
3295 } |
|
3296 |
458
|
3297 /* |
|
3298 ;;; Local Variables: *** |
|
3299 ;;; mode: C++ *** |
|
3300 ;;; End: *** |
|
3301 */ |