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 |
|
1359 Array<double> scale(nc); |
|
1360 double *pscale = scale.fortran_vec (); |
|
1361 |
|
1362 int info, ilo, ihi; |
|
1363 |
|
1364 // both scale and permute |
|
1365 char job = 'B'; |
|
1366 |
|
1367 F77_XFCN (dgebal, DGEBAL, (&job, nc, p_m, nc, ilo, ihi, pscale, info, |
|
1368 1L, 1L)); |
|
1369 |
|
1370 if (f77_exception_encountered) |
|
1371 { |
|
1372 (*current_liboctave_error_handler) ("unrecoverable error in dgebal"); |
|
1373 return retval; |
|
1374 } |
|
1375 |
|
1376 // construct balancing matrices D, Dinv |
|
1377 |
|
1378 Matrix dmat = Matrix (nc, nc, 0.0); |
|
1379 Matrix dinv = Matrix (nc, nc, 0.0); |
|
1380 |
|
1381 for (int i = 0; i < nc; i++) |
|
1382 dmat(i,i) = dinv(i,i) = 1.0; |
|
1383 |
|
1384 // dgebak, dside=R => dmat := D*dmat |
|
1385 char dside = 'R'; |
|
1386 F77_XFCN (dgebak, DGEBAK, (&job, &dside, nc, ilo, ihi, pscale, nc, |
|
1387 dmat.fortran_vec(), nc, info, 1L, 1L)); |
|
1388 |
|
1389 if (f77_exception_encountered) |
|
1390 { |
|
1391 (*current_liboctave_error_handler) ("unrecoverable error in dgebak"); |
|
1392 return retval; |
|
1393 } |
|
1394 |
|
1395 // dgebak, dside=L => dinv := dinv*D^{-1} |
|
1396 dside = 'L'; |
|
1397 F77_XFCN (dgebak, DGEBAK, (&job, &dside, nc, ilo, ihi, pscale, nc, |
|
1398 dinv.fortran_vec(), nc, info, 1L, 1L)); |
|
1399 |
|
1400 if (f77_exception_encountered) |
|
1401 { |
|
1402 (*current_liboctave_error_handler) ("unrecoverable error in dgebak"); |
|
1403 return retval; |
|
1404 } |
1819
|
1405 |
|
1406 // Preconditioning step 3: scaling. |
3331
|
1407 |
1819
|
1408 ColumnVector work(nc); |
3130
|
1409 double inf_norm; |
3331
|
1410 |
|
1411 F77_XFCN (xdlange, XDLANGE, ("I", nc, nc, m.fortran_vec (), nc, |
|
1412 work.fortran_vec (), inf_norm)); |
|
1413 |
|
1414 if (f77_exception_encountered) |
|
1415 { |
|
1416 (*current_liboctave_error_handler) ("unrecoverable error in dlange"); |
|
1417 return retval; |
|
1418 } |
1819
|
1419 |
|
1420 int sqpow = (int) (inf_norm > 0.0 |
|
1421 ? (1.0 + log (inf_norm) / log (2.0)) |
|
1422 : 0.0); |
3331
|
1423 |
1819
|
1424 // Check whether we need to square at all. |
3331
|
1425 |
1819
|
1426 if (sqpow < 0) |
|
1427 sqpow = 0; |
3331
|
1428 |
1819
|
1429 if (sqpow > 0) |
|
1430 { |
|
1431 double scale_factor = 1.0; |
|
1432 for (int i = 0; i < sqpow; i++) |
|
1433 scale_factor *= 2.0; |
3331
|
1434 |
1819
|
1435 m = m / scale_factor; |
|
1436 } |
3331
|
1437 |
1819
|
1438 // npp, dpp: pade' approx polynomial matrices. |
3331
|
1439 |
1819
|
1440 Matrix npp (nc, nc, 0.0); |
|
1441 Matrix dpp = npp; |
3331
|
1442 |
1819
|
1443 // Now powers a^8 ... a^1. |
3331
|
1444 |
1819
|
1445 int minus_one_j = -1; |
|
1446 for (int j = 7; j >= 0; j--) |
|
1447 { |
|
1448 npp = m * npp + m * padec[j]; |
|
1449 dpp = m * dpp + m * (minus_one_j * padec[j]); |
|
1450 minus_one_j *= -1; |
|
1451 } |
3331
|
1452 |
1819
|
1453 // Zero power. |
3331
|
1454 |
1819
|
1455 dpp = -dpp; |
3130
|
1456 for (int j = 0; j < nc; j++) |
1819
|
1457 { |
|
1458 npp.elem (j, j) += 1.0; |
|
1459 dpp.elem (j, j) += 1.0; |
|
1460 } |
3331
|
1461 |
1819
|
1462 // Compute pade approximation = inverse (dpp) * npp. |
|
1463 |
3331
|
1464 retval = dpp.solve (npp, info); |
|
1465 |
1819
|
1466 // Reverse preconditioning step 3: repeated squaring. |
3331
|
1467 |
1819
|
1468 while (sqpow) |
|
1469 { |
|
1470 retval = retval * retval; |
|
1471 sqpow--; |
|
1472 } |
3331
|
1473 |
1819
|
1474 // Reverse preconditioning step 2: inverse balancing. |
3331
|
1475 retval = dmat*retval*dinv; |
|
1476 |
1819
|
1477 // Reverse preconditioning step 1: fix trace normalization. |
3331
|
1478 |
3130
|
1479 if (trshift > 0.0) |
|
1480 retval = exp (trshift) * retval; |
|
1481 |
|
1482 return retval; |
1819
|
1483 } |
|
1484 |
458
|
1485 Matrix& |
|
1486 Matrix::operator += (const Matrix& a) |
|
1487 { |
|
1488 int nr = rows (); |
|
1489 int nc = cols (); |
2385
|
1490 |
|
1491 int a_nr = a.rows (); |
|
1492 int a_nc = a.cols (); |
|
1493 |
|
1494 if (nr != a_nr || nc != a_nc) |
458
|
1495 { |
2385
|
1496 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
1497 return *this; |
|
1498 } |
|
1499 |
|
1500 if (nr == 0 || nc == 0) |
|
1501 return *this; |
|
1502 |
|
1503 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1504 |
|
1505 add2 (d, a.data (), length ()); |
|
1506 |
|
1507 return *this; |
|
1508 } |
|
1509 |
|
1510 Matrix& |
|
1511 Matrix::operator -= (const Matrix& a) |
|
1512 { |
|
1513 int nr = rows (); |
|
1514 int nc = cols (); |
2385
|
1515 |
|
1516 int a_nr = a.rows (); |
|
1517 int a_nc = a.cols (); |
|
1518 |
|
1519 if (nr != a_nr || nc != a_nc) |
458
|
1520 { |
2385
|
1521 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
1522 return *this; |
|
1523 } |
|
1524 |
|
1525 if (nr == 0 || nc == 0) |
|
1526 return *this; |
|
1527 |
|
1528 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1529 |
|
1530 subtract2 (d, a.data (), length ()); |
|
1531 |
|
1532 return *this; |
|
1533 } |
|
1534 |
|
1535 Matrix& |
|
1536 Matrix::operator += (const DiagMatrix& a) |
|
1537 { |
2385
|
1538 int nr = rows (); |
|
1539 int nc = cols (); |
|
1540 |
|
1541 int a_nr = a.rows (); |
|
1542 int a_nc = a.cols (); |
|
1543 |
|
1544 if (nr != a_nr || nc != a_nc) |
458
|
1545 { |
2385
|
1546 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
1547 return *this; |
|
1548 } |
|
1549 |
|
1550 for (int i = 0; i < a.length (); i++) |
|
1551 elem (i, i) += a.elem (i, i); |
|
1552 |
|
1553 return *this; |
|
1554 } |
|
1555 |
|
1556 Matrix& |
|
1557 Matrix::operator -= (const DiagMatrix& a) |
|
1558 { |
2385
|
1559 int nr = rows (); |
|
1560 int nc = cols (); |
|
1561 |
|
1562 int a_nr = a.rows (); |
|
1563 int a_nc = a.cols (); |
|
1564 |
|
1565 if (nr != a_nr || nc != a_nc) |
458
|
1566 { |
2385
|
1567 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
1568 return *this; |
|
1569 } |
|
1570 |
|
1571 for (int i = 0; i < a.length (); i++) |
|
1572 elem (i, i) -= a.elem (i, i); |
|
1573 |
|
1574 return *this; |
|
1575 } |
|
1576 |
|
1577 // unary operations |
|
1578 |
2964
|
1579 boolMatrix |
458
|
1580 Matrix::operator ! (void) const |
|
1581 { |
|
1582 int nr = rows (); |
|
1583 int nc = cols (); |
|
1584 |
2964
|
1585 boolMatrix b (nr, nc); |
458
|
1586 |
|
1587 for (int j = 0; j < nc; j++) |
|
1588 for (int i = 0; i < nr; i++) |
|
1589 b.elem (i, j) = ! elem (i, j); |
|
1590 |
|
1591 return b; |
|
1592 } |
|
1593 |
1205
|
1594 // column vector by row vector -> matrix operations |
458
|
1595 |
1205
|
1596 Matrix |
|
1597 operator * (const ColumnVector& v, const RowVector& a) |
458
|
1598 { |
1948
|
1599 Matrix retval; |
|
1600 |
1205
|
1601 int len = v.length (); |
3233
|
1602 |
|
1603 if (len != 0) |
1205
|
1604 { |
3233
|
1605 int a_len = a.length (); |
|
1606 |
|
1607 retval.resize (len, a_len); |
|
1608 double *c = retval.fortran_vec (); |
|
1609 |
|
1610 F77_XFCN (dgemm, DGEMM, ("N", "N", len, a_len, 1, 1.0, |
|
1611 v.data (), len, a.data (), 1, 0.0, |
|
1612 c, len, 1L, 1L)); |
|
1613 |
|
1614 if (f77_exception_encountered) |
|
1615 (*current_liboctave_error_handler) |
|
1616 ("unrecoverable error in dgemm"); |
1205
|
1617 } |
458
|
1618 |
1948
|
1619 return retval; |
458
|
1620 } |
|
1621 |
|
1622 // other operations. |
|
1623 |
|
1624 Matrix |
2676
|
1625 Matrix::map (d_d_Mapper f) const |
1205
|
1626 { |
2676
|
1627 Matrix b (*this); |
|
1628 return b.apply (f); |
1205
|
1629 } |
|
1630 |
3248
|
1631 boolMatrix |
|
1632 Matrix::map (b_d_Mapper f) const |
|
1633 { |
|
1634 int nr = rows (); |
|
1635 int nc = cols (); |
|
1636 |
|
1637 boolMatrix retval (nr, nc); |
|
1638 |
|
1639 for (int j = 0; j < nc; j++) |
|
1640 for (int i = 0; i < nr; i++) |
|
1641 retval(i,j) = f (elem(i,j)); |
|
1642 |
|
1643 return retval; |
|
1644 } |
|
1645 |
2676
|
1646 Matrix& |
|
1647 Matrix::apply (d_d_Mapper f) |
458
|
1648 { |
|
1649 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1650 |
|
1651 for (int i = 0; i < length (); i++) |
|
1652 d[i] = f (d[i]); |
2676
|
1653 |
|
1654 return *this; |
458
|
1655 } |
|
1656 |
2385
|
1657 bool |
|
1658 Matrix::any_element_is_negative (void) const |
|
1659 { |
|
1660 int nr = rows (); |
|
1661 int nc = cols (); |
|
1662 |
|
1663 for (int j = 0; j < nc; j++) |
|
1664 for (int i = 0; i < nr; i++) |
|
1665 if (elem (i, j) < 0.0) |
|
1666 return true; |
|
1667 |
|
1668 return false; |
|
1669 } |
|
1670 |
|
1671 |
|
1672 bool |
|
1673 Matrix::any_element_is_inf_or_nan (void) const |
|
1674 { |
|
1675 int nr = rows (); |
|
1676 int nc = cols (); |
|
1677 |
|
1678 for (int j = 0; j < nc; j++) |
|
1679 for (int i = 0; i < nr; i++) |
|
1680 { |
|
1681 double val = elem (i, j); |
|
1682 if (xisinf (val) || xisnan (val)) |
|
1683 return 1; |
|
1684 } |
|
1685 return 0; |
|
1686 } |
|
1687 |
|
1688 bool |
|
1689 Matrix::all_elements_are_int_or_inf_or_nan (void) const |
|
1690 { |
|
1691 int nr = rows (); |
|
1692 int nc = cols (); |
|
1693 |
|
1694 for (int j = 0; j < nc; j++) |
|
1695 for (int i = 0; i < nr; i++) |
|
1696 { |
|
1697 double val = elem (i, j); |
|
1698 if (xisnan (val) || D_NINT (val) == val) |
|
1699 continue; |
|
1700 else |
|
1701 return false; |
|
1702 } |
|
1703 |
|
1704 return true; |
|
1705 } |
|
1706 |
1968
|
1707 // Return nonzero if any element of M is not an integer. Also extract |
|
1708 // the largest and smallest values and return them in MAX_VAL and MIN_VAL. |
|
1709 |
2385
|
1710 bool |
1968
|
1711 Matrix::all_integers (double& max_val, double& min_val) const |
|
1712 { |
|
1713 int nr = rows (); |
|
1714 int nc = cols (); |
|
1715 |
|
1716 if (nr > 0 && nc > 0) |
|
1717 { |
|
1718 max_val = elem (0, 0); |
|
1719 min_val = elem (0, 0); |
|
1720 } |
|
1721 else |
2385
|
1722 return false; |
1968
|
1723 |
|
1724 for (int j = 0; j < nc; j++) |
|
1725 for (int i = 0; i < nr; i++) |
|
1726 { |
|
1727 double val = elem (i, j); |
|
1728 |
|
1729 if (val > max_val) |
|
1730 max_val = val; |
|
1731 |
|
1732 if (val < min_val) |
|
1733 min_val = val; |
|
1734 |
|
1735 if (D_NINT (val) != val) |
2385
|
1736 return false; |
1968
|
1737 } |
2385
|
1738 |
|
1739 return true; |
1968
|
1740 } |
|
1741 |
2385
|
1742 bool |
1968
|
1743 Matrix::too_large_for_float (void) const |
|
1744 { |
|
1745 int nr = rows (); |
2385
|
1746 int nc = cols (); |
1968
|
1747 |
|
1748 for (int j = 0; j < nc; j++) |
|
1749 for (int i = 0; i < nr; i++) |
|
1750 { |
|
1751 double val = elem (i, j); |
|
1752 |
|
1753 if (val > FLT_MAX || val < FLT_MIN) |
2385
|
1754 return true; |
1968
|
1755 } |
|
1756 |
2385
|
1757 return false; |
1968
|
1758 } |
|
1759 |
458
|
1760 // XXX FIXME XXX Do these really belong here? They should maybe be |
|
1761 // cleaned up a bit, no? What about corresponding functions for the |
|
1762 // Vectors? |
|
1763 |
2832
|
1764 boolMatrix |
458
|
1765 Matrix::all (void) const |
|
1766 { |
|
1767 int nr = rows (); |
|
1768 int nc = cols (); |
2832
|
1769 boolMatrix retval; |
458
|
1770 if (nr > 0 && nc > 0) |
|
1771 { |
|
1772 if (nr == 1) |
|
1773 { |
|
1774 retval.resize (1, 1); |
2832
|
1775 retval.elem (0, 0) = true; |
458
|
1776 for (int j = 0; j < nc; j++) |
|
1777 { |
|
1778 if (elem (0, j) == 0.0) |
|
1779 { |
2832
|
1780 retval.elem (0, 0) = false; |
458
|
1781 break; |
|
1782 } |
|
1783 } |
|
1784 } |
|
1785 else if (nc == 1) |
|
1786 { |
|
1787 retval.resize (1, 1); |
2832
|
1788 retval.elem (0, 0) = true; |
458
|
1789 for (int i = 0; i < nr; i++) |
|
1790 { |
|
1791 if (elem (i, 0) == 0.0) |
|
1792 { |
2832
|
1793 retval.elem (0, 0) = false; |
458
|
1794 break; |
|
1795 } |
|
1796 } |
|
1797 } |
|
1798 else |
|
1799 { |
|
1800 retval.resize (1, nc); |
|
1801 for (int j = 0; j < nc; j++) |
|
1802 { |
2832
|
1803 retval.elem (0, j) = true; |
458
|
1804 for (int i = 0; i < nr; i++) |
|
1805 { |
|
1806 if (elem (i, j) == 0.0) |
|
1807 { |
2832
|
1808 retval.elem (0, j) = false; |
458
|
1809 break; |
|
1810 } |
|
1811 } |
|
1812 } |
|
1813 } |
|
1814 } |
|
1815 return retval; |
|
1816 } |
|
1817 |
2832
|
1818 boolMatrix |
458
|
1819 Matrix::any (void) const |
|
1820 { |
|
1821 int nr = rows (); |
|
1822 int nc = cols (); |
2832
|
1823 boolMatrix retval; |
458
|
1824 if (nr > 0 && nc > 0) |
|
1825 { |
|
1826 if (nr == 1) |
|
1827 { |
|
1828 retval.resize (1, 1); |
2832
|
1829 retval.elem (0, 0) = false; |
458
|
1830 for (int j = 0; j < nc; j++) |
|
1831 { |
|
1832 if (elem (0, j) != 0.0) |
|
1833 { |
2832
|
1834 retval.elem (0, 0) = true; |
458
|
1835 break; |
|
1836 } |
|
1837 } |
|
1838 } |
|
1839 else if (nc == 1) |
|
1840 { |
|
1841 retval.resize (1, 1); |
2832
|
1842 retval.elem (0, 0) = false; |
458
|
1843 for (int i = 0; i < nr; i++) |
|
1844 { |
|
1845 if (elem (i, 0) != 0.0) |
|
1846 { |
2832
|
1847 retval.elem (0, 0) = true; |
458
|
1848 break; |
|
1849 } |
|
1850 } |
|
1851 } |
|
1852 else |
|
1853 { |
|
1854 retval.resize (1, nc); |
|
1855 for (int j = 0; j < nc; j++) |
|
1856 { |
2832
|
1857 retval.elem (0, j) = false; |
458
|
1858 for (int i = 0; i < nr; i++) |
|
1859 { |
|
1860 if (elem (i, j) != 0.0) |
|
1861 { |
2832
|
1862 retval.elem (0, j) = true; |
458
|
1863 break; |
|
1864 } |
|
1865 } |
|
1866 } |
|
1867 } |
|
1868 } |
|
1869 return retval; |
|
1870 } |
|
1871 |
|
1872 Matrix |
|
1873 Matrix::cumprod (void) const |
|
1874 { |
|
1875 Matrix retval; |
|
1876 |
|
1877 int nr = rows (); |
|
1878 int nc = cols (); |
|
1879 |
|
1880 if (nr == 1) |
|
1881 { |
|
1882 retval.resize (1, nc); |
|
1883 if (nc > 0) |
|
1884 { |
|
1885 double prod = elem (0, 0); |
|
1886 for (int j = 0; j < nc; j++) |
|
1887 { |
|
1888 retval.elem (0, j) = prod; |
|
1889 if (j < nc - 1) |
|
1890 prod *= elem (0, j+1); |
|
1891 } |
|
1892 } |
|
1893 } |
|
1894 else if (nc == 1) |
|
1895 { |
|
1896 retval.resize (nr, 1); |
|
1897 if (nr > 0) |
|
1898 { |
|
1899 double prod = elem (0, 0); |
|
1900 for (int i = 0; i < nr; i++) |
|
1901 { |
|
1902 retval.elem (i, 0) = prod; |
|
1903 if (i < nr - 1) |
|
1904 prod *= elem (i+1, 0); |
|
1905 } |
|
1906 } |
|
1907 } |
|
1908 else |
|
1909 { |
|
1910 retval.resize (nr, nc); |
|
1911 if (nr > 0 && nc > 0) |
|
1912 { |
|
1913 for (int j = 0; j < nc; j++) |
|
1914 { |
|
1915 double prod = elem (0, j); |
|
1916 for (int i = 0; i < nr; i++) |
|
1917 { |
|
1918 retval.elem (i, j) = prod; |
|
1919 if (i < nr - 1) |
|
1920 prod *= elem (i+1, j); |
|
1921 } |
|
1922 } |
|
1923 } |
|
1924 } |
|
1925 return retval; |
|
1926 } |
|
1927 |
|
1928 Matrix |
|
1929 Matrix::cumsum (void) const |
|
1930 { |
|
1931 Matrix retval; |
|
1932 |
|
1933 int nr = rows (); |
|
1934 int nc = cols (); |
|
1935 |
|
1936 if (nr == 1) |
|
1937 { |
|
1938 retval.resize (1, nc); |
|
1939 if (nc > 0) |
|
1940 { |
|
1941 double sum = elem (0, 0); |
|
1942 for (int j = 0; j < nc; j++) |
|
1943 { |
|
1944 retval.elem (0, j) = sum; |
|
1945 if (j < nc - 1) |
|
1946 sum += elem (0, j+1); |
|
1947 } |
|
1948 } |
|
1949 } |
|
1950 else if (nc == 1) |
|
1951 { |
|
1952 retval.resize (nr, 1); |
|
1953 if (nr > 0) |
|
1954 { |
|
1955 double sum = elem (0, 0); |
|
1956 for (int i = 0; i < nr; i++) |
|
1957 { |
|
1958 retval.elem (i, 0) = sum; |
|
1959 if (i < nr - 1) |
|
1960 sum += elem (i+1, 0); |
|
1961 } |
|
1962 } |
|
1963 } |
|
1964 else |
|
1965 { |
|
1966 retval.resize (nr, nc); |
|
1967 if (nr > 0 && nc > 0) |
|
1968 { |
|
1969 for (int j = 0; j < nc; j++) |
|
1970 { |
|
1971 double sum = elem (0, j); |
|
1972 for (int i = 0; i < nr; i++) |
|
1973 { |
|
1974 retval.elem (i, j) = sum; |
|
1975 if (i < nr - 1) |
|
1976 sum += elem (i+1, j); |
|
1977 } |
|
1978 } |
|
1979 } |
|
1980 } |
|
1981 return retval; |
|
1982 } |
|
1983 |
|
1984 Matrix |
|
1985 Matrix::prod (void) const |
|
1986 { |
|
1987 Matrix retval; |
|
1988 |
|
1989 int nr = rows (); |
|
1990 int nc = cols (); |
|
1991 |
|
1992 if (nr == 1) |
|
1993 { |
|
1994 retval.resize (1, 1); |
|
1995 retval.elem (0, 0) = 1.0; |
|
1996 for (int j = 0; j < nc; j++) |
|
1997 retval.elem (0, 0) *= elem (0, j); |
|
1998 } |
|
1999 else if (nc == 1) |
|
2000 { |
|
2001 retval.resize (1, 1); |
|
2002 retval.elem (0, 0) = 1.0; |
|
2003 for (int i = 0; i < nr; i++) |
|
2004 retval.elem (0, 0) *= elem (i, 0); |
|
2005 } |
|
2006 else |
|
2007 { |
|
2008 if (nc == 0) |
|
2009 { |
|
2010 retval.resize (1, 1); |
|
2011 retval.elem (0, 0) = 1.0; |
|
2012 } |
|
2013 else |
|
2014 retval.resize (1, nc); |
|
2015 |
|
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 retval.elem (0, j) *= elem (i, j); |
|
2021 } |
|
2022 } |
|
2023 return retval; |
|
2024 } |
|
2025 |
|
2026 Matrix |
|
2027 Matrix::sum (void) const |
|
2028 { |
|
2029 Matrix retval; |
|
2030 |
|
2031 int nr = rows (); |
|
2032 int nc = cols (); |
|
2033 |
|
2034 if (nr == 1) |
|
2035 { |
|
2036 retval.resize (1, 1); |
|
2037 retval.elem (0, 0) = 0.0; |
|
2038 for (int j = 0; j < nc; j++) |
|
2039 retval.elem (0, 0) += elem (0, j); |
|
2040 } |
|
2041 else if (nc == 1) |
|
2042 { |
|
2043 retval.resize (1, 1); |
|
2044 retval.elem (0, 0) = 0.0; |
|
2045 for (int i = 0; i < nr; i++) |
|
2046 retval.elem (0, 0) += elem (i, 0); |
|
2047 } |
|
2048 else |
|
2049 { |
|
2050 if (nc == 0) |
|
2051 { |
|
2052 retval.resize (1, 1); |
|
2053 retval.elem (0, 0) = 0.0; |
|
2054 } |
|
2055 else |
|
2056 retval.resize (1, nc); |
|
2057 |
|
2058 for (int j = 0; j < nc; j++) |
|
2059 { |
|
2060 retval.elem (0, j) = 0.0; |
|
2061 for (int i = 0; i < nr; i++) |
|
2062 retval.elem (0, j) += elem (i, j); |
|
2063 } |
|
2064 } |
|
2065 return retval; |
|
2066 } |
|
2067 |
|
2068 Matrix |
|
2069 Matrix::sumsq (void) const |
|
2070 { |
|
2071 Matrix retval; |
|
2072 |
|
2073 int nr = rows (); |
|
2074 int nc = cols (); |
|
2075 |
|
2076 if (nr == 1) |
|
2077 { |
|
2078 retval.resize (1, 1); |
|
2079 retval.elem (0, 0) = 0.0; |
|
2080 for (int j = 0; j < nc; j++) |
|
2081 { |
|
2082 double d = elem (0, j); |
|
2083 retval.elem (0, 0) += d * d; |
|
2084 } |
|
2085 } |
|
2086 else if (nc == 1) |
|
2087 { |
|
2088 retval.resize (1, 1); |
|
2089 retval.elem (0, 0) = 0.0; |
|
2090 for (int i = 0; i < nr; i++) |
|
2091 { |
|
2092 double d = elem (i, 0); |
|
2093 retval.elem (0, 0) += d * d; |
|
2094 } |
|
2095 } |
|
2096 else |
|
2097 { |
|
2098 retval.resize (1, nc); |
|
2099 for (int j = 0; j < nc; j++) |
|
2100 { |
|
2101 retval.elem (0, j) = 0.0; |
|
2102 for (int i = 0; i < nr; i++) |
|
2103 { |
|
2104 double d = elem (i, j); |
|
2105 retval.elem (0, j) += d * d; |
|
2106 } |
|
2107 } |
|
2108 } |
|
2109 return retval; |
|
2110 } |
|
2111 |
2385
|
2112 Matrix |
|
2113 Matrix::abs (void) const |
|
2114 { |
|
2115 int nr = rows (); |
|
2116 int nc = cols (); |
|
2117 |
|
2118 Matrix retval (nr, nc); |
|
2119 |
|
2120 for (int j = 0; j < nc; j++) |
|
2121 for (int i = 0; i < nr; i++) |
|
2122 retval (i, j) = fabs (elem (i, j)); |
|
2123 |
|
2124 return retval; |
|
2125 } |
|
2126 |
458
|
2127 ColumnVector |
|
2128 Matrix::diag (void) const |
|
2129 { |
|
2130 return diag (0); |
|
2131 } |
|
2132 |
|
2133 ColumnVector |
|
2134 Matrix::diag (int k) const |
|
2135 { |
|
2136 int nnr = rows (); |
|
2137 int nnc = cols (); |
|
2138 if (k > 0) |
|
2139 nnc -= k; |
|
2140 else if (k < 0) |
|
2141 nnr += k; |
|
2142 |
|
2143 ColumnVector d; |
|
2144 |
|
2145 if (nnr > 0 && nnc > 0) |
|
2146 { |
|
2147 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
2148 |
|
2149 d.resize (ndiag); |
|
2150 |
|
2151 if (k > 0) |
|
2152 { |
|
2153 for (int i = 0; i < ndiag; i++) |
|
2154 d.elem (i) = elem (i, i+k); |
|
2155 } |
|
2156 else if ( k < 0) |
|
2157 { |
|
2158 for (int i = 0; i < ndiag; i++) |
|
2159 d.elem (i) = elem (i-k, i); |
|
2160 } |
|
2161 else |
|
2162 { |
|
2163 for (int i = 0; i < ndiag; i++) |
|
2164 d.elem (i) = elem (i, i); |
|
2165 } |
|
2166 } |
|
2167 else |
|
2168 cerr << "diag: requested diagonal out of range\n"; |
|
2169 |
|
2170 return d; |
|
2171 } |
|
2172 |
|
2173 ColumnVector |
|
2174 Matrix::row_min (void) const |
|
2175 { |
2354
|
2176 Array<int> index; |
|
2177 return row_min (index); |
458
|
2178 } |
|
2179 |
|
2180 ColumnVector |
2354
|
2181 Matrix::row_min (Array<int>& index) const |
458
|
2182 { |
|
2183 ColumnVector result; |
|
2184 |
|
2185 int nr = rows (); |
|
2186 int nc = cols (); |
|
2187 |
|
2188 if (nr > 0 && nc > 0) |
|
2189 { |
|
2190 result.resize (nr); |
2354
|
2191 index.resize (nr); |
458
|
2192 |
|
2193 for (int i = 0; i < nr; i++) |
|
2194 { |
2354
|
2195 int idx = 0; |
|
2196 |
|
2197 double tmp_min = elem (i, idx); |
|
2198 |
|
2199 if (xisnan (tmp_min)) |
|
2200 idx = -1; |
|
2201 else |
|
2202 { |
|
2203 for (int j = 1; j < nc; j++) |
|
2204 { |
|
2205 double tmp = elem (i, j); |
|
2206 |
|
2207 if (xisnan (tmp)) |
|
2208 { |
|
2209 idx = -1; |
|
2210 break; |
|
2211 } |
|
2212 else if (tmp < tmp_min) |
|
2213 { |
|
2214 idx = j; |
|
2215 tmp_min = tmp; |
|
2216 } |
|
2217 } |
|
2218 } |
|
2219 |
|
2220 result.elem (i) = (idx < 0) ? octave_NaN : tmp_min; |
|
2221 index.elem (i) = idx; |
458
|
2222 } |
|
2223 } |
|
2224 |
|
2225 return result; |
|
2226 } |
|
2227 |
|
2228 ColumnVector |
|
2229 Matrix::row_max (void) const |
|
2230 { |
2354
|
2231 Array<int> index; |
|
2232 return row_max (index); |
458
|
2233 } |
|
2234 |
|
2235 ColumnVector |
2354
|
2236 Matrix::row_max (Array<int>& index) const |
458
|
2237 { |
|
2238 ColumnVector result; |
|
2239 |
|
2240 int nr = rows (); |
|
2241 int nc = cols (); |
|
2242 |
|
2243 if (nr > 0 && nc > 0) |
|
2244 { |
|
2245 result.resize (nr); |
2354
|
2246 index.resize (nr); |
458
|
2247 |
|
2248 for (int i = 0; i < nr; i++) |
|
2249 { |
2354
|
2250 int idx = 0; |
|
2251 |
|
2252 double tmp_max = elem (i, idx); |
|
2253 |
|
2254 if (xisnan (tmp_max)) |
|
2255 idx = -1; |
|
2256 else |
|
2257 { |
|
2258 for (int j = 1; j < nc; j++) |
|
2259 { |
|
2260 double tmp = elem (i, j); |
|
2261 |
|
2262 if (xisnan (tmp)) |
|
2263 { |
|
2264 idx = -1; |
|
2265 break; |
|
2266 } |
|
2267 else if (tmp > tmp_max) |
|
2268 { |
|
2269 idx = j; |
|
2270 tmp_max = tmp; |
|
2271 } |
|
2272 } |
|
2273 } |
|
2274 |
|
2275 result.elem (i) = (idx < 0) ? octave_NaN : tmp_max; |
|
2276 index.elem (i) = idx; |
458
|
2277 } |
|
2278 } |
|
2279 |
|
2280 return result; |
|
2281 } |
|
2282 |
|
2283 RowVector |
|
2284 Matrix::column_min (void) const |
|
2285 { |
2354
|
2286 Array<int> index; |
|
2287 return column_min (index); |
458
|
2288 } |
2354
|
2289 |
458
|
2290 RowVector |
2354
|
2291 Matrix::column_min (Array<int>& index) const |
458
|
2292 { |
|
2293 RowVector result; |
|
2294 |
|
2295 int nr = rows (); |
|
2296 int nc = cols (); |
|
2297 |
|
2298 if (nr > 0 && nc > 0) |
|
2299 { |
|
2300 result.resize (nc); |
2354
|
2301 index.resize (nc); |
458
|
2302 |
|
2303 for (int j = 0; j < nc; j++) |
|
2304 { |
2354
|
2305 int idx = 0; |
|
2306 |
|
2307 double tmp_min = elem (idx, j); |
|
2308 |
|
2309 if (xisnan (tmp_min)) |
|
2310 idx = -1; |
|
2311 else |
|
2312 { |
|
2313 for (int i = 1; i < nr; i++) |
|
2314 { |
|
2315 double tmp = elem (i, j); |
|
2316 |
|
2317 if (xisnan (tmp)) |
|
2318 { |
|
2319 idx = -1; |
|
2320 break; |
|
2321 } |
|
2322 else if (tmp < tmp_min) |
|
2323 { |
|
2324 idx = i; |
|
2325 tmp_min = tmp; |
|
2326 } |
|
2327 } |
|
2328 } |
|
2329 |
|
2330 result.elem (j) = (idx < 0) ? octave_NaN : tmp_min; |
|
2331 index.elem (j) = idx; |
458
|
2332 } |
|
2333 } |
|
2334 |
|
2335 return result; |
|
2336 } |
|
2337 |
2354
|
2338 RowVector |
|
2339 Matrix::column_max (void) const |
|
2340 { |
|
2341 Array<int> index; |
|
2342 return column_max (index); |
|
2343 } |
458
|
2344 |
|
2345 RowVector |
2354
|
2346 Matrix::column_max (Array<int>& index) const |
458
|
2347 { |
|
2348 RowVector result; |
|
2349 |
|
2350 int nr = rows (); |
|
2351 int nc = cols (); |
|
2352 |
|
2353 if (nr > 0 && nc > 0) |
|
2354 { |
|
2355 result.resize (nc); |
2354
|
2356 index.resize (nc); |
458
|
2357 |
|
2358 for (int j = 0; j < nc; j++) |
|
2359 { |
2354
|
2360 int idx = 0; |
|
2361 |
|
2362 double tmp_max = elem (idx, j); |
|
2363 |
|
2364 if (xisnan (tmp_max)) |
|
2365 idx = -1; |
|
2366 else |
|
2367 { |
|
2368 for (int i = 1; i < nr; i++) |
|
2369 { |
|
2370 double tmp = elem (i, j); |
|
2371 |
|
2372 if (xisnan (tmp)) |
|
2373 { |
|
2374 idx = -1; |
|
2375 break; |
|
2376 } |
|
2377 else if (tmp > tmp_max) |
|
2378 { |
|
2379 idx = i; |
|
2380 tmp_max = tmp; |
|
2381 } |
|
2382 } |
|
2383 } |
|
2384 |
|
2385 result.elem (j) = (idx < 0) ? octave_NaN : tmp_max; |
|
2386 index.elem (j) = idx; |
458
|
2387 } |
|
2388 } |
|
2389 |
|
2390 return result; |
|
2391 } |
|
2392 |
|
2393 ostream& |
|
2394 operator << (ostream& os, const Matrix& a) |
|
2395 { |
|
2396 // int field_width = os.precision () + 7; |
1360
|
2397 |
458
|
2398 for (int i = 0; i < a.rows (); i++) |
|
2399 { |
|
2400 for (int j = 0; j < a.cols (); j++) |
|
2401 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
2402 os << "\n"; |
|
2403 } |
|
2404 return os; |
|
2405 } |
|
2406 |
|
2407 istream& |
|
2408 operator >> (istream& is, Matrix& a) |
|
2409 { |
|
2410 int nr = a.rows (); |
|
2411 int nc = a.cols (); |
|
2412 |
|
2413 if (nr < 1 || nc < 1) |
|
2414 is.clear (ios::badbit); |
|
2415 else |
|
2416 { |
|
2417 double tmp; |
|
2418 for (int i = 0; i < nr; i++) |
|
2419 for (int j = 0; j < nc; j++) |
|
2420 { |
|
2421 is >> tmp; |
|
2422 if (is) |
|
2423 a.elem (i, j) = tmp; |
|
2424 else |
2795
|
2425 goto done; |
458
|
2426 } |
|
2427 } |
|
2428 |
2795
|
2429 done: |
|
2430 |
458
|
2431 return is; |
|
2432 } |
|
2433 |
2317
|
2434 template <class T> |
|
2435 static void |
|
2436 read_int (istream& is, bool swap_bytes, T& val) |
|
2437 { |
3145
|
2438 is.read (X_CAST (char *, &val), sizeof (T)); |
2317
|
2439 |
|
2440 if (swap_bytes) |
|
2441 { |
|
2442 switch (sizeof (T)) |
|
2443 { |
|
2444 case 1: |
|
2445 break; |
|
2446 |
|
2447 case 2: |
3145
|
2448 swap_2_bytes (X_CAST (char *, &val)); |
2317
|
2449 break; |
|
2450 |
|
2451 case 4: |
3145
|
2452 swap_4_bytes (X_CAST (char *, &val)); |
2317
|
2453 break; |
|
2454 |
|
2455 case 8: |
3145
|
2456 swap_8_bytes (X_CAST (char *, &val)); |
2317
|
2457 break; |
|
2458 |
|
2459 default: |
|
2460 (*current_liboctave_error_handler) |
|
2461 ("read_int: unrecognized data format!"); |
|
2462 } |
|
2463 } |
|
2464 } |
|
2465 |
|
2466 template void read_int (istream&, bool, char&); |
|
2467 template void read_int (istream&, bool, signed char&); |
|
2468 template void read_int (istream&, bool, unsigned char&); |
|
2469 template void read_int (istream&, bool, short&); |
|
2470 template void read_int (istream&, bool, unsigned short&); |
|
2471 template void read_int (istream&, bool, int&); |
|
2472 template void read_int (istream&, bool, unsigned int&); |
|
2473 template void read_int (istream&, bool, long&); |
|
2474 template void read_int (istream&, bool, unsigned long&); |
|
2475 |
|
2476 static inline bool |
|
2477 do_read (istream& is, oct_data_conv::data_type dt, |
|
2478 oct_mach_info::float_format flt_fmt, bool swap_bytes, |
|
2479 bool do_float_conversion, double& val) |
|
2480 { |
|
2481 bool retval = true; |
|
2482 |
|
2483 switch (dt) |
|
2484 { |
|
2485 case oct_data_conv::dt_char: |
|
2486 { |
|
2487 char tmp; |
|
2488 read_int (is, swap_bytes, tmp); |
|
2489 val = tmp; |
|
2490 } |
|
2491 break; |
|
2492 |
|
2493 case oct_data_conv::dt_schar: |
|
2494 { |
|
2495 signed char tmp; |
|
2496 read_int (is, swap_bytes, tmp); |
|
2497 val = tmp; |
|
2498 } |
|
2499 break; |
|
2500 |
|
2501 case oct_data_conv::dt_uchar: |
|
2502 { |
|
2503 unsigned char tmp; |
|
2504 read_int (is, swap_bytes, tmp); |
|
2505 val = tmp; |
|
2506 } |
|
2507 break; |
|
2508 |
|
2509 case oct_data_conv::dt_short: |
|
2510 { |
|
2511 short tmp; |
|
2512 read_int (is, swap_bytes, tmp); |
|
2513 val = tmp; |
|
2514 } |
|
2515 break; |
|
2516 |
|
2517 case oct_data_conv::dt_ushort: |
|
2518 { |
|
2519 unsigned short tmp; |
|
2520 read_int (is, swap_bytes, tmp); |
|
2521 val = tmp; |
|
2522 } |
|
2523 break; |
|
2524 |
|
2525 case oct_data_conv::dt_int: |
|
2526 { |
|
2527 int tmp; |
|
2528 read_int (is, swap_bytes, tmp); |
|
2529 val = tmp; |
|
2530 } |
|
2531 break; |
|
2532 |
|
2533 case oct_data_conv::dt_uint: |
|
2534 { |
|
2535 unsigned int tmp; |
|
2536 read_int (is, swap_bytes, tmp); |
|
2537 val = tmp; |
|
2538 } |
|
2539 break; |
|
2540 |
|
2541 case oct_data_conv::dt_long: |
|
2542 { |
|
2543 long tmp; |
|
2544 read_int (is, swap_bytes, tmp); |
|
2545 val = tmp; |
|
2546 } |
|
2547 break; |
|
2548 |
|
2549 case oct_data_conv::dt_ulong: |
|
2550 { |
|
2551 unsigned long tmp; |
|
2552 read_int (is, swap_bytes, tmp); |
|
2553 val = tmp; |
|
2554 } |
|
2555 break; |
|
2556 |
|
2557 case oct_data_conv::dt_float: |
|
2558 { |
|
2559 float f; |
|
2560 |
3145
|
2561 is.read (X_CAST (char *, &f), sizeof (float)); |
2317
|
2562 |
|
2563 if (do_float_conversion) |
|
2564 do_float_format_conversion (&f, 1, flt_fmt); |
|
2565 |
|
2566 val = f; |
|
2567 } |
|
2568 break; |
|
2569 |
|
2570 case oct_data_conv::dt_double: |
|
2571 { |
3145
|
2572 is.read (X_CAST (char *, &val), sizeof (double)); |
2317
|
2573 |
|
2574 if (do_float_conversion) |
|
2575 do_double_format_conversion (&val, 1, flt_fmt); |
|
2576 } |
|
2577 break; |
|
2578 |
|
2579 default: |
|
2580 retval = false; |
|
2581 (*current_liboctave_error_handler) |
|
2582 ("read: invalid type specification"); |
|
2583 break; |
|
2584 } |
|
2585 |
|
2586 return retval; |
|
2587 } |
1360
|
2588 |
458
|
2589 int |
2317
|
2590 Matrix::read (istream& is, int nr, int nc, |
|
2591 oct_data_conv::data_type dt, int skip, |
|
2592 oct_mach_info::float_format flt_fmt) |
458
|
2593 { |
2317
|
2594 int retval = -1; |
|
2595 |
|
2596 bool ok = true; |
|
2597 |
|
2598 int count = 0; |
|
2599 |
|
2600 double *data = 0; |
|
2601 int max_size = 0; |
|
2602 |
|
2603 int final_nr = 0; |
|
2604 int final_nc = 0; |
|
2605 |
|
2606 if (nr > 0) |
458
|
2607 { |
2317
|
2608 if (nc > 0) |
|
2609 { |
|
2610 resize (nr, nc, 0.0); |
|
2611 data = fortran_vec (); |
|
2612 max_size = nr * nc; |
|
2613 } |
|
2614 else |
|
2615 { |
|
2616 resize (nr, 32, 0.0); |
|
2617 data = fortran_vec (); |
|
2618 max_size = nr * 32; |
|
2619 } |
|
2620 } |
471
|
2621 else |
|
2622 { |
2317
|
2623 resize (32, 1, 0.0); |
|
2624 data = fortran_vec (); |
|
2625 max_size = 32; |
|
2626 } |
|
2627 |
|
2628 oct_mach_info::float_format native_flt_fmt |
|
2629 = oct_mach_info::float_format (); |
|
2630 |
|
2631 bool do_float_conversion = (flt_fmt != native_flt_fmt); |
|
2632 |
|
2633 // XXX FIXME XXX -- byte order for Cray? |
|
2634 |
|
2635 bool swap_bytes = false; |
|
2636 |
|
2637 if (oct_mach_info::words_big_endian ()) |
|
2638 swap_bytes = (flt_fmt == oct_mach_info::ieee_little_endian |
|
2639 || flt_fmt == oct_mach_info::vax_g |
|
2640 || flt_fmt == oct_mach_info::vax_g); |
|
2641 else |
|
2642 swap_bytes = (flt_fmt == oct_mach_info::ieee_big_endian); |
|
2643 |
|
2644 for (;;) |
|
2645 { |
|
2646 // XXX FIXME XXX -- maybe there should be a special case for |
|
2647 // skip == 0. |
|
2648 |
|
2649 if (is) |
|
2650 { |
|
2651 if (nr > 0 && nc > 0 && count == max_size) |
|
2652 { |
|
2653 final_nr = nr; |
|
2654 final_nc = nc; |
|
2655 |
|
2656 break; |
|
2657 } |
|
2658 |
|
2659 if (is) |
|
2660 { |
|
2661 double tmp = 0.0; |
|
2662 |
|
2663 ok = do_read (is, dt, flt_fmt, swap_bytes, |
|
2664 do_float_conversion, tmp); |
|
2665 |
|
2666 if (ok) |
|
2667 { |
|
2668 if (is) |
|
2669 { |
|
2670 if (count == max_size) |
|
2671 { |
|
2672 max_size *= 2; |
|
2673 |
|
2674 if (nr > 0) |
2601
|
2675 resize (nr, max_size / nr, 0.0); |
2317
|
2676 else |
|
2677 resize (max_size, 1, 0.0); |
|
2678 |
|
2679 data = fortran_vec (); |
|
2680 } |
|
2681 |
|
2682 data[count++] = tmp; |
|
2683 } |
3180
|
2684 |
|
2685 if (ok && skip != 0) |
|
2686 is.seekg (skip, ios::cur); |
|
2687 |
3219
|
2688 if (! ok || is.eof ()) |
2317
|
2689 { |
|
2690 if (is.eof ()) |
|
2691 { |
|
2692 if (nr > 0) |
|
2693 { |
|
2694 if (count > nr) |
|
2695 { |
|
2696 final_nr = nr; |
|
2697 final_nc = (count - 1) / nr + 1; |
|
2698 } |
|
2699 else |
|
2700 { |
|
2701 final_nr = count; |
|
2702 final_nc = 1; |
|
2703 } |
|
2704 } |
|
2705 else |
|
2706 { |
|
2707 final_nr = count; |
|
2708 final_nc = 1; |
|
2709 } |
|
2710 } |
|
2711 |
|
2712 break; |
|
2713 } |
|
2714 } |
|
2715 else |
|
2716 break; |
|
2717 } |
|
2718 else |
|
2719 { |
|
2720 ok = false; |
|
2721 break; |
|
2722 } |
|
2723 } |
|
2724 else |
|
2725 { |
|
2726 ok = false; |
|
2727 break; |
|
2728 } |
|
2729 } |
|
2730 |
|
2731 if (ok) |
|
2732 { |
|
2733 resize (final_nr, final_nc, 0.0); |
|
2734 |
|
2735 retval = count; |
458
|
2736 } |
|
2737 |
2317
|
2738 return retval; |
|
2739 } |
|
2740 |
|
2741 template <class T> |
|
2742 static void |
|
2743 write_int (ostream& os, bool swap_bytes, T val) |
|
2744 { |
|
2745 if (swap_bytes) |
|
2746 { |
|
2747 switch (sizeof (T)) |
|
2748 { |
|
2749 case 1: |
|
2750 break; |
|
2751 |
|
2752 case 2: |
3145
|
2753 swap_2_bytes (X_CAST (char *, &val)); |
2317
|
2754 break; |
|
2755 |
|
2756 case 4: |
3145
|
2757 swap_4_bytes (X_CAST (char *, &val)); |
2317
|
2758 break; |
|
2759 |
|
2760 case 8: |
3145
|
2761 swap_8_bytes (X_CAST (char *, &val)); |
2317
|
2762 break; |
|
2763 |
|
2764 default: |
|
2765 (*current_liboctave_error_handler) |
|
2766 ("write_int: unrecognized data format!"); |
|
2767 } |
|
2768 } |
|
2769 |
3145
|
2770 os.write (X_CAST (char *, &val), sizeof (T)); |
458
|
2771 } |
|
2772 |
2317
|
2773 template void write_int (ostream&, bool, char); |
|
2774 template void write_int (ostream&, bool, signed char); |
|
2775 template void write_int (ostream&, bool, unsigned char); |
|
2776 template void write_int (ostream&, bool, short); |
|
2777 template void write_int (ostream&, bool, unsigned short); |
|
2778 template void write_int (ostream&, bool, int); |
|
2779 template void write_int (ostream&, bool, unsigned int); |
|
2780 template void write_int (ostream&, bool, long); |
|
2781 template void write_int (ostream&, bool, unsigned long); |
|
2782 |
|
2783 static inline bool |
|
2784 do_write (ostream& os, double d, oct_data_conv::data_type dt, |
|
2785 oct_mach_info::float_format flt_fmt, bool swap_bytes, |
|
2786 bool do_float_conversion) |
|
2787 { |
|
2788 bool retval = true; |
|
2789 |
|
2790 switch (dt) |
|
2791 { |
|
2792 case oct_data_conv::dt_char: |
3145
|
2793 write_int (os, swap_bytes, X_CAST (char, d)); |
2317
|
2794 break; |
|
2795 |
|
2796 case oct_data_conv::dt_schar: |
3145
|
2797 write_int (os, swap_bytes, X_CAST (signed char, d)); |
2317
|
2798 break; |
|
2799 |
|
2800 case oct_data_conv::dt_uchar: |
3145
|
2801 write_int (os, swap_bytes, X_CAST (unsigned char, d)); |
2317
|
2802 break; |
|
2803 |
|
2804 case oct_data_conv::dt_short: |
3145
|
2805 write_int (os, swap_bytes, X_CAST (short, d)); |
2317
|
2806 break; |
|
2807 |
|
2808 case oct_data_conv::dt_ushort: |
3145
|
2809 write_int (os, swap_bytes, X_CAST (unsigned short, d)); |
2317
|
2810 break; |
|
2811 |
|
2812 case oct_data_conv::dt_int: |
3145
|
2813 write_int (os, swap_bytes, X_CAST (int, d)); |
2317
|
2814 break; |
|
2815 |
|
2816 case oct_data_conv::dt_uint: |
3145
|
2817 write_int (os, swap_bytes, X_CAST (unsigned int, d)); |
2317
|
2818 break; |
|
2819 |
|
2820 case oct_data_conv::dt_long: |
3145
|
2821 write_int (os, swap_bytes, X_CAST (long, d)); |
2317
|
2822 break; |
|
2823 |
|
2824 case oct_data_conv::dt_ulong: |
3145
|
2825 write_int (os, swap_bytes, X_CAST (unsigned long, d)); |
2317
|
2826 break; |
|
2827 |
|
2828 case oct_data_conv::dt_float: |
|
2829 { |
2800
|
2830 float f = d; |
2317
|
2831 |
|
2832 if (do_float_conversion) |
|
2833 do_float_format_conversion (&f, 1, flt_fmt); |
|
2834 |
3145
|
2835 os.write (X_CAST (char *, &f), sizeof (float)); |
2317
|
2836 } |
|
2837 break; |
|
2838 |
|
2839 case oct_data_conv::dt_double: |
|
2840 { |
|
2841 if (do_float_conversion) |
|
2842 do_double_format_conversion (&d, 1, flt_fmt); |
|
2843 |
3145
|
2844 os.write (X_CAST (char *, &d), sizeof (double)); |
2317
|
2845 } |
|
2846 break; |
|
2847 |
|
2848 default: |
|
2849 retval = false; |
|
2850 (*current_liboctave_error_handler) |
|
2851 ("write: invalid type specification"); |
|
2852 break; |
|
2853 } |
|
2854 |
|
2855 return retval; |
|
2856 } |
1360
|
2857 |
458
|
2858 int |
2317
|
2859 Matrix::write (ostream& os, oct_data_conv::data_type dt, int skip, |
|
2860 oct_mach_info::float_format flt_fmt) |
458
|
2861 { |
2317
|
2862 int retval = -1; |
|
2863 |
|
2864 bool ok = true; |
|
2865 |
|
2866 int count = 0; |
|
2867 |
|
2868 const double *d = data (); |
|
2869 |
|
2870 int n = length (); |
|
2871 |
|
2872 oct_mach_info::float_format native_flt_fmt |
|
2873 = oct_mach_info::float_format (); |
|
2874 |
|
2875 bool do_float_conversion = (flt_fmt != native_flt_fmt); |
|
2876 |
|
2877 // XXX FIXME XXX -- byte order for Cray? |
|
2878 |
|
2879 bool swap_bytes = false; |
|
2880 |
|
2881 if (oct_mach_info::words_big_endian ()) |
|
2882 swap_bytes = (flt_fmt == oct_mach_info::ieee_little_endian |
|
2883 || flt_fmt == oct_mach_info::vax_g |
|
2884 || flt_fmt == oct_mach_info::vax_g); |
|
2885 else |
|
2886 swap_bytes = (flt_fmt == oct_mach_info::ieee_big_endian); |
|
2887 |
|
2888 for (int i = 0; i < n; i++) |
458
|
2889 { |
2317
|
2890 if (os) |
|
2891 { |
|
2892 if (skip != 0) |
|
2893 os.seekp (skip, ios::cur); |
|
2894 |
|
2895 if (os) |
|
2896 { |
|
2897 ok = do_write (os, d[i], dt, flt_fmt, swap_bytes, |
|
2898 do_float_conversion); |
|
2899 |
|
2900 if (os && ok) |
|
2901 count++; |
|
2902 else |
|
2903 break; |
|
2904 } |
|
2905 else |
|
2906 { |
|
2907 ok = false; |
|
2908 break; |
|
2909 } |
|
2910 } |
|
2911 else |
|
2912 { |
|
2913 ok = false; |
|
2914 break; |
|
2915 } |
471
|
2916 } |
458
|
2917 |
2317
|
2918 if (ok) |
|
2919 retval = count; |
|
2920 |
|
2921 return retval; |
458
|
2922 } |
|
2923 |
2317
|
2924 |
|
2925 |
1819
|
2926 Matrix |
|
2927 Givens (double x, double y) |
|
2928 { |
|
2929 double cc, s, temp_r; |
|
2930 |
|
2931 F77_FCN (dlartg, DLARTG) (x, y, cc, s, temp_r); |
|
2932 |
|
2933 Matrix g (2, 2); |
|
2934 |
|
2935 g.elem (0, 0) = cc; |
|
2936 g.elem (1, 1) = cc; |
|
2937 g.elem (0, 1) = s; |
|
2938 g.elem (1, 0) = -s; |
|
2939 |
|
2940 return g; |
|
2941 } |
|
2942 |
|
2943 Matrix |
|
2944 Sylvester (const Matrix& a, const Matrix& b, const Matrix& c) |
|
2945 { |
|
2946 Matrix retval; |
|
2947 |
|
2948 // XXX FIXME XXX -- need to check that a, b, and c are all the same |
|
2949 // size. |
|
2950 |
|
2951 // Compute Schur decompositions. |
|
2952 |
|
2953 SCHUR as (a, "U"); |
|
2954 SCHUR bs (b, "U"); |
|
2955 |
|
2956 // Transform c to new coordinates. |
|
2957 |
|
2958 Matrix ua = as.unitary_matrix (); |
|
2959 Matrix sch_a = as.schur_matrix (); |
|
2960 |
|
2961 Matrix ub = bs.unitary_matrix (); |
|
2962 Matrix sch_b = bs.schur_matrix (); |
|
2963 |
|
2964 Matrix cx = ua.transpose () * c * ub; |
|
2965 |
|
2966 // Solve the sylvester equation, back-transform, and return the |
|
2967 // solution. |
|
2968 |
|
2969 int a_nr = a.rows (); |
|
2970 int b_nr = b.rows (); |
|
2971 |
|
2972 double scale; |
|
2973 int info; |
|
2974 |
1950
|
2975 double *pa = sch_a.fortran_vec (); |
|
2976 double *pb = sch_b.fortran_vec (); |
|
2977 double *px = cx.fortran_vec (); |
|
2978 |
|
2979 F77_XFCN (dtrsyl, DTRSYL, ("N", "N", 1, a_nr, b_nr, pa, a_nr, pb, |
|
2980 b_nr, px, a_nr, scale, info, 1L, 1L)); |
|
2981 |
|
2982 |
|
2983 if (f77_exception_encountered) |
|
2984 (*current_liboctave_error_handler) ("unrecoverable error in dtrsyl"); |
|
2985 else |
|
2986 { |
|
2987 // XXX FIXME XXX -- check info? |
1819
|
2988 |
1950
|
2989 retval = -ua*cx*ub.transpose (); |
|
2990 } |
1819
|
2991 |
|
2992 return retval; |
|
2993 } |
|
2994 |
2828
|
2995 // matrix by matrix -> matrix operations |
|
2996 |
|
2997 Matrix |
|
2998 operator * (const Matrix& m, const Matrix& a) |
|
2999 { |
|
3000 Matrix retval; |
|
3001 |
|
3002 int nr = m.rows (); |
|
3003 int nc = m.cols (); |
|
3004 |
|
3005 int a_nr = a.rows (); |
|
3006 int a_nc = a.cols (); |
|
3007 |
|
3008 if (nc != a_nr) |
|
3009 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
|
3010 else |
|
3011 { |
|
3012 if (nr == 0 || nc == 0 || a_nc == 0) |
|
3013 retval.resize (nr, a_nc, 0.0); |
|
3014 else |
|
3015 { |
|
3016 int ld = nr; |
|
3017 int lda = a_nr; |
|
3018 |
|
3019 retval.resize (nr, a_nc); |
|
3020 double *c = retval.fortran_vec (); |
|
3021 |
|
3022 F77_XFCN (dgemm, DGEMM, ("N", "N", nr, a_nc, nc, 1.0, |
|
3023 m.data (), ld, a.data (), lda, 0.0, |
|
3024 c, nr, 1L, 1L)); |
|
3025 |
|
3026 if (f77_exception_encountered) |
|
3027 (*current_liboctave_error_handler) |
|
3028 ("unrecoverable error in dgemm"); |
|
3029 } |
|
3030 } |
|
3031 |
|
3032 return retval; |
|
3033 } |
|
3034 |
2870
|
3035 MS_CMP_OPS(Matrix, , double, ) |
|
3036 MS_BOOL_OPS(Matrix, double) |
|
3037 |
|
3038 SM_CMP_OPS(double, , Matrix, ) |
|
3039 SM_BOOL_OPS(double, Matrix) |
|
3040 |
|
3041 MM_CMP_OPS(Matrix, , Matrix, ) |
|
3042 MM_BOOL_OPS(Matrix, Matrix) |
|
3043 |
458
|
3044 /* |
|
3045 ;;; Local Variables: *** |
|
3046 ;;; mode: C++ *** |
|
3047 ;;; End: *** |
|
3048 */ |