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