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