1993
|
1 // Matrix manipulations. |
458
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 John W. Eaton |
458
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
458
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
458
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
458
|
30 #endif |
|
31 |
1367
|
32 #include <cfloat> |
|
33 |
458
|
34 #include <iostream.h> |
1367
|
35 |
2317
|
36 #include "byte-swap.h" |
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" |
1367
|
49 #include "mx-inlines.cc" |
1650
|
50 #include "oct-cmplx.h" |
458
|
51 |
|
52 // Fortran functions we call. |
|
53 |
|
54 extern "C" |
|
55 { |
1253
|
56 int F77_FCN (dgemm, DGEMM) (const char*, const char*, const int&, |
|
57 const int&, const int&, const double&, |
|
58 const double*, const int&, |
|
59 const double*, const int&, |
|
60 const double&, double*, const int&, |
|
61 long, long); |
458
|
62 |
1253
|
63 int F77_FCN (dgeco, DGECO) (double*, const int&, const int&, int*, |
|
64 double&, double*); |
458
|
65 |
1253
|
66 int F77_FCN (dgesl, DGESL) (const double*, const int&, const int&, |
|
67 const int*, double*, const int&); |
458
|
68 |
1253
|
69 int F77_FCN (dgedi, DGEDI) (double*, const int&, const int&, |
|
70 const int*, double*, double*, |
|
71 const int&); |
458
|
72 |
1253
|
73 int F77_FCN (dgelss, DGELSS) (const int&, const int&, const int&, |
|
74 double*, const int&, double*, |
|
75 const int&, double*, double&, int&, |
|
76 double*, const int&, int&); |
458
|
77 |
1360
|
78 // Note that the original complex fft routines were not written for |
|
79 // double complex arguments. They have been modified by adding an |
|
80 // implicit double precision (a-h,o-z) statement at the beginning of |
|
81 // each subroutine. |
458
|
82 |
1253
|
83 int F77_FCN (cffti, CFFTI) (const int&, Complex*); |
458
|
84 |
1253
|
85 int F77_FCN (cfftf, CFFTF) (const int&, Complex*, Complex*); |
458
|
86 |
1253
|
87 int F77_FCN (cfftb, CFFTB) (const int&, Complex*, Complex*); |
1819
|
88 |
|
89 int F77_FCN (dlartg, DLARTG) (const double&, const double&, double&, |
|
90 double&, double&); |
|
91 |
|
92 int F77_FCN (dtrsyl, DTRSYL) (const char*, const char*, const int&, |
|
93 const int&, const int&, const double*, |
|
94 const int&, const double*, const int&, |
|
95 const double*, const int&, double&, |
|
96 int&, long, long); |
|
97 |
|
98 double F77_FCN (dlange, DLANGE) (const char*, const int&, |
|
99 const int&, const double*, |
|
100 const int&, double*); |
1959
|
101 |
|
102 int F77_FCN (qzhes, QZHES) (const int&, const int&, double*, |
|
103 double*, const long&, double*); |
|
104 |
|
105 int F77_FCN (qzit, QZIT) (const int&, const int&, double*, double*, |
|
106 const double&, const long&, double*, |
|
107 int&); |
|
108 |
|
109 int F77_FCN (qzval, QZVAL) (const int&, const int&, double*, |
|
110 double*, double*, double*, double*, |
|
111 const long&, double*); |
458
|
112 } |
|
113 |
1360
|
114 // Matrix class. |
458
|
115 |
2349
|
116 Matrix::Matrix (const RowVector& rv) |
|
117 : MArray2<double> (1, rv.length (), 0.0) |
|
118 { |
|
119 for (int i = 0; i < rv.length (); i++) |
|
120 elem (0, i) = rv.elem (i); |
|
121 } |
|
122 |
|
123 Matrix::Matrix (const ColumnVector& cv) |
|
124 : MArray2<double> (cv.length (), 1, 0.0) |
|
125 { |
|
126 for (int i = 0; i < cv.length (); i++) |
|
127 elem (i, 0) = cv.elem (i); |
|
128 } |
|
129 |
458
|
130 Matrix::Matrix (const DiagMatrix& a) |
1214
|
131 : MArray2<double> (a.rows (), a.cols (), 0.0) |
458
|
132 { |
|
133 for (int i = 0; i < a.length (); i++) |
|
134 elem (i, i) = a.elem (i, i); |
|
135 } |
|
136 |
1574
|
137 // XXX FIXME XXX -- could we use a templated mixed-type copy function |
|
138 // here? |
|
139 |
2828
|
140 Matrix::Matrix (const boolMatrix& a) |
|
141 : MArray2<double> (a.rows (), a.cols ()) |
|
142 { |
|
143 for (int i = 0; i < a.rows (); i++) |
|
144 for (int j = 0; j < a.cols (); j++) |
|
145 elem (i, j) = a.elem (i, j); |
|
146 } |
|
147 |
1574
|
148 Matrix::Matrix (const charMatrix& a) |
|
149 : MArray2<double> (a.rows (), a.cols ()) |
|
150 { |
|
151 for (int i = 0; i < a.rows (); i++) |
|
152 for (int j = 0; j < a.cols (); j++) |
|
153 elem (i, j) = a.elem (i, j); |
|
154 } |
|
155 |
2385
|
156 bool |
458
|
157 Matrix::operator == (const Matrix& a) const |
|
158 { |
|
159 if (rows () != a.rows () || cols () != a.cols ()) |
2385
|
160 return false; |
458
|
161 |
|
162 return equal (data (), a.data (), length ()); |
|
163 } |
|
164 |
2385
|
165 bool |
458
|
166 Matrix::operator != (const Matrix& a) const |
|
167 { |
|
168 return !(*this == a); |
|
169 } |
|
170 |
|
171 Matrix& |
|
172 Matrix::insert (const Matrix& a, int r, int c) |
|
173 { |
1561
|
174 Array2<double>::insert (a, r, c); |
458
|
175 return *this; |
|
176 } |
|
177 |
|
178 Matrix& |
|
179 Matrix::insert (const RowVector& a, int r, int c) |
|
180 { |
|
181 int a_len = a.length (); |
1698
|
182 if (r < 0 || r >= rows () || c < 0 || c + a_len > cols ()) |
458
|
183 { |
|
184 (*current_liboctave_error_handler) ("range error for insert"); |
|
185 return *this; |
|
186 } |
|
187 |
|
188 for (int i = 0; i < a_len; i++) |
|
189 elem (r, c+i) = a.elem (i); |
|
190 |
|
191 return *this; |
|
192 } |
|
193 |
|
194 Matrix& |
|
195 Matrix::insert (const ColumnVector& a, int r, int c) |
|
196 { |
|
197 int a_len = a.length (); |
1698
|
198 if (r < 0 || r + a_len > rows () || c < 0 || c >= cols ()) |
458
|
199 { |
|
200 (*current_liboctave_error_handler) ("range error for insert"); |
|
201 return *this; |
|
202 } |
|
203 |
|
204 for (int i = 0; i < a_len; i++) |
|
205 elem (r+i, c) = a.elem (i); |
|
206 |
|
207 return *this; |
|
208 } |
|
209 |
|
210 Matrix& |
|
211 Matrix::insert (const DiagMatrix& a, int r, int c) |
|
212 { |
1697
|
213 int a_nr = a.rows (); |
|
214 int a_nc = a.cols (); |
|
215 |
1698
|
216 if (r < 0 || r + a_nr > rows () || c < 0 || c + a_nc > cols ()) |
458
|
217 { |
|
218 (*current_liboctave_error_handler) ("range error for insert"); |
|
219 return *this; |
|
220 } |
|
221 |
1697
|
222 fill (0.0, r, c, r + a_nr - 1, c + a_nc - 1); |
|
223 |
458
|
224 for (int i = 0; i < a.length (); i++) |
|
225 elem (r+i, c+i) = a.elem (i, i); |
|
226 |
|
227 return *this; |
|
228 } |
|
229 |
|
230 Matrix& |
|
231 Matrix::fill (double val) |
|
232 { |
|
233 int nr = rows (); |
|
234 int nc = cols (); |
|
235 if (nr > 0 && nc > 0) |
|
236 for (int j = 0; j < nc; j++) |
|
237 for (int i = 0; i < nr; i++) |
|
238 elem (i, j) = val; |
|
239 |
|
240 return *this; |
|
241 } |
|
242 |
|
243 Matrix& |
|
244 Matrix::fill (double val, int r1, int c1, int r2, int c2) |
|
245 { |
|
246 int nr = rows (); |
|
247 int nc = cols (); |
|
248 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
|
249 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) |
|
250 { |
|
251 (*current_liboctave_error_handler) ("range error for fill"); |
|
252 return *this; |
|
253 } |
|
254 |
|
255 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
256 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
257 |
|
258 for (int j = c1; j <= c2; j++) |
|
259 for (int i = r1; i <= r2; i++) |
|
260 elem (i, j) = val; |
|
261 |
|
262 return *this; |
|
263 } |
|
264 |
|
265 Matrix |
|
266 Matrix::append (const Matrix& a) const |
|
267 { |
|
268 int nr = rows (); |
|
269 int nc = cols (); |
|
270 if (nr != a.rows ()) |
|
271 { |
|
272 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
273 return Matrix (); |
|
274 } |
|
275 |
|
276 int nc_insert = nc; |
|
277 Matrix retval (nr, nc + a.cols ()); |
|
278 retval.insert (*this, 0, 0); |
|
279 retval.insert (a, 0, nc_insert); |
|
280 return retval; |
|
281 } |
|
282 |
|
283 Matrix |
|
284 Matrix::append (const RowVector& a) const |
|
285 { |
|
286 int nr = rows (); |
|
287 int nc = cols (); |
|
288 if (nr != 1) |
|
289 { |
|
290 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
291 return Matrix (); |
|
292 } |
|
293 |
|
294 int nc_insert = nc; |
|
295 Matrix retval (nr, nc + a.length ()); |
|
296 retval.insert (*this, 0, 0); |
|
297 retval.insert (a, 0, nc_insert); |
|
298 return retval; |
|
299 } |
|
300 |
|
301 Matrix |
|
302 Matrix::append (const ColumnVector& a) const |
|
303 { |
|
304 int nr = rows (); |
|
305 int nc = cols (); |
|
306 if (nr != a.length ()) |
|
307 { |
|
308 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
309 return Matrix (); |
|
310 } |
|
311 |
|
312 int nc_insert = nc; |
|
313 Matrix retval (nr, nc + 1); |
|
314 retval.insert (*this, 0, 0); |
|
315 retval.insert (a, 0, nc_insert); |
|
316 return retval; |
|
317 } |
|
318 |
|
319 Matrix |
|
320 Matrix::append (const DiagMatrix& a) const |
|
321 { |
|
322 int nr = rows (); |
|
323 int nc = cols (); |
|
324 if (nr != a.rows ()) |
|
325 { |
|
326 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
327 return *this; |
|
328 } |
|
329 |
|
330 int nc_insert = nc; |
|
331 Matrix retval (nr, nc + a.cols ()); |
|
332 retval.insert (*this, 0, 0); |
|
333 retval.insert (a, 0, nc_insert); |
|
334 return retval; |
|
335 } |
|
336 |
|
337 Matrix |
|
338 Matrix::stack (const Matrix& a) const |
|
339 { |
|
340 int nr = rows (); |
|
341 int nc = cols (); |
|
342 if (nc != a.cols ()) |
|
343 { |
|
344 (*current_liboctave_error_handler) |
|
345 ("column dimension mismatch for stack"); |
|
346 return Matrix (); |
|
347 } |
|
348 |
|
349 int nr_insert = nr; |
|
350 Matrix retval (nr + a.rows (), nc); |
|
351 retval.insert (*this, 0, 0); |
|
352 retval.insert (a, nr_insert, 0); |
|
353 return retval; |
|
354 } |
|
355 |
|
356 Matrix |
|
357 Matrix::stack (const RowVector& a) const |
|
358 { |
|
359 int nr = rows (); |
|
360 int nc = cols (); |
|
361 if (nc != a.length ()) |
|
362 { |
|
363 (*current_liboctave_error_handler) |
|
364 ("column dimension mismatch for stack"); |
|
365 return Matrix (); |
|
366 } |
|
367 |
|
368 int nr_insert = nr; |
|
369 Matrix retval (nr + 1, nc); |
|
370 retval.insert (*this, 0, 0); |
|
371 retval.insert (a, nr_insert, 0); |
|
372 return retval; |
|
373 } |
|
374 |
|
375 Matrix |
|
376 Matrix::stack (const ColumnVector& a) const |
|
377 { |
|
378 int nr = rows (); |
|
379 int nc = cols (); |
|
380 if (nc != 1) |
|
381 { |
|
382 (*current_liboctave_error_handler) |
|
383 ("column dimension mismatch for stack"); |
|
384 return Matrix (); |
|
385 } |
|
386 |
|
387 int nr_insert = nr; |
|
388 Matrix retval (nr + a.length (), nc); |
|
389 retval.insert (*this, 0, 0); |
|
390 retval.insert (a, nr_insert, 0); |
|
391 return retval; |
|
392 } |
|
393 |
|
394 Matrix |
|
395 Matrix::stack (const DiagMatrix& a) const |
|
396 { |
|
397 int nr = rows (); |
|
398 int nc = cols (); |
|
399 if (nc != a.cols ()) |
|
400 { |
|
401 (*current_liboctave_error_handler) |
|
402 ("column dimension mismatch for stack"); |
|
403 return Matrix (); |
|
404 } |
|
405 |
|
406 int nr_insert = nr; |
|
407 Matrix retval (nr + a.rows (), nc); |
|
408 retval.insert (*this, 0, 0); |
|
409 retval.insert (a, nr_insert, 0); |
|
410 return retval; |
|
411 } |
|
412 |
|
413 Matrix |
|
414 Matrix::transpose (void) const |
|
415 { |
|
416 int nr = rows (); |
|
417 int nc = cols (); |
|
418 Matrix result (nc, nr); |
|
419 if (length () > 0) |
|
420 { |
|
421 for (int j = 0; j < nc; j++) |
|
422 for (int i = 0; i < nr; i++) |
|
423 result.elem (j, i) = elem (i, j); |
|
424 } |
|
425 return result; |
|
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 |
|
1169 Array<double> work (lwork); |
|
1170 double *pwork = work.fortran_vec (); |
|
1171 |
|
1172 F77_XFCN (dgelss, DGELSS, (m, n, nrhs, tmp_data, m, presult, nrr, ps, |
|
1173 rcond, rank, pwork, lwork, info)); |
|
1174 |
|
1175 if (f77_exception_encountered) |
|
1176 (*current_liboctave_error_handler) ("unrecoverable error in dgelss"); |
|
1177 else |
|
1178 { |
|
1179 retval.resize (n, nrhs); |
|
1180 for (int j = 0; j < nrhs; j++) |
|
1181 for (int i = 0; i < n; i++) |
|
1182 retval.elem (i, j) = result.elem (i, j); |
|
1183 } |
458
|
1184 } |
|
1185 |
|
1186 return retval; |
|
1187 } |
|
1188 |
|
1189 ComplexMatrix |
|
1190 Matrix::lssolve (const ComplexMatrix& b) const |
|
1191 { |
|
1192 ComplexMatrix tmp (*this); |
1484
|
1193 int info; |
|
1194 int rank; |
|
1195 return tmp.lssolve (b, info, rank); |
458
|
1196 } |
|
1197 |
|
1198 ComplexMatrix |
|
1199 Matrix::lssolve (const ComplexMatrix& b, int& info) const |
|
1200 { |
|
1201 ComplexMatrix tmp (*this); |
1484
|
1202 int rank; |
|
1203 return tmp.lssolve (b, info, rank); |
458
|
1204 } |
|
1205 |
|
1206 ComplexMatrix |
|
1207 Matrix::lssolve (const ComplexMatrix& b, int& info, int& rank) const |
|
1208 { |
|
1209 ComplexMatrix tmp (*this); |
1484
|
1210 return tmp.lssolve (b, info, rank); |
458
|
1211 } |
|
1212 |
|
1213 ColumnVector |
|
1214 Matrix::lssolve (const ColumnVector& b) const |
|
1215 { |
|
1216 int info; |
1484
|
1217 int rank; |
|
1218 return lssolve (b, info, rank); |
458
|
1219 } |
|
1220 |
|
1221 ColumnVector |
|
1222 Matrix::lssolve (const ColumnVector& b, int& info) const |
|
1223 { |
|
1224 int rank; |
|
1225 return lssolve (b, info, rank); |
|
1226 } |
|
1227 |
|
1228 ColumnVector |
|
1229 Matrix::lssolve (const ColumnVector& b, int& info, int& rank) const |
|
1230 { |
1948
|
1231 ColumnVector retval; |
|
1232 |
458
|
1233 int nrhs = 1; |
|
1234 |
|
1235 int m = rows (); |
|
1236 int n = cols (); |
|
1237 |
|
1238 if (m == 0 || n == 0 || m != b.length ()) |
1948
|
1239 (*current_liboctave_error_handler) |
|
1240 ("matrix dimension mismatch in solution of least squares problem"); |
|
1241 else |
458
|
1242 { |
1948
|
1243 Matrix atmp = *this; |
|
1244 double *tmp_data = atmp.fortran_vec (); |
|
1245 |
|
1246 int nrr = m > n ? m : n; |
|
1247 ColumnVector result (nrr); |
|
1248 |
|
1249 for (int i = 0; i < m; i++) |
|
1250 result.elem (i) = b.elem (i); |
|
1251 |
|
1252 double *presult = result.fortran_vec (); |
|
1253 |
|
1254 int len_s = m < n ? m : n; |
|
1255 Array<double> s (len_s); |
|
1256 double *ps = s.fortran_vec (); |
|
1257 |
|
1258 double rcond = -1.0; |
|
1259 |
|
1260 int lwork; |
|
1261 if (m < n) |
|
1262 lwork = 3*m + (2*m > nrhs |
|
1263 ? (2*m > n ? 2*m : n) |
|
1264 : (nrhs > n ? nrhs : n)); |
|
1265 else |
|
1266 lwork = 3*n + (2*n > nrhs |
|
1267 ? (2*n > m ? 2*n : m) |
|
1268 : (nrhs > m ? nrhs : m)); |
|
1269 |
|
1270 Array<double> work (lwork); |
|
1271 double *pwork = work.fortran_vec (); |
|
1272 |
|
1273 F77_XFCN (dgelss, DGELSS, (m, n, nrhs, tmp_data, m, presult, nrr, |
|
1274 ps, rcond, rank, pwork, lwork, info)); |
|
1275 |
|
1276 if (f77_exception_encountered) |
|
1277 (*current_liboctave_error_handler) ("unrecoverable error in dgelss"); |
|
1278 else |
|
1279 { |
|
1280 retval.resize (n); |
|
1281 for (int i = 0; i < n; i++) |
|
1282 retval.elem (i) = result.elem (i); |
|
1283 } |
458
|
1284 } |
|
1285 |
|
1286 return retval; |
|
1287 } |
|
1288 |
|
1289 ComplexColumnVector |
|
1290 Matrix::lssolve (const ComplexColumnVector& b) const |
|
1291 { |
|
1292 ComplexMatrix tmp (*this); |
|
1293 return tmp.lssolve (b); |
|
1294 } |
|
1295 |
|
1296 ComplexColumnVector |
|
1297 Matrix::lssolve (const ComplexColumnVector& b, int& info) const |
|
1298 { |
|
1299 ComplexMatrix tmp (*this); |
|
1300 return tmp.lssolve (b, info); |
|
1301 } |
|
1302 |
|
1303 ComplexColumnVector |
|
1304 Matrix::lssolve (const ComplexColumnVector& b, int& info, int& rank) const |
|
1305 { |
|
1306 ComplexMatrix tmp (*this); |
|
1307 return tmp.lssolve (b, info, rank); |
|
1308 } |
|
1309 |
1819
|
1310 // Constants for matrix exponential calculation. |
|
1311 |
|
1312 static double padec [] = |
|
1313 { |
|
1314 5.0000000000000000e-1, |
|
1315 1.1666666666666667e-1, |
|
1316 1.6666666666666667e-2, |
|
1317 1.6025641025641026e-3, |
|
1318 1.0683760683760684e-4, |
|
1319 4.8562548562548563e-6, |
|
1320 1.3875013875013875e-7, |
|
1321 1.9270852604185938e-9, |
|
1322 }; |
|
1323 |
|
1324 Matrix |
|
1325 Matrix::expm (void) const |
|
1326 { |
|
1327 Matrix retval; |
|
1328 |
|
1329 Matrix m = *this; |
|
1330 |
|
1331 int nc = columns (); |
|
1332 |
|
1333 // trace shift value |
|
1334 double trshift = 0; |
|
1335 |
|
1336 // Preconditioning step 1: trace normalization. |
|
1337 |
|
1338 for (int i = 0; i < nc; i++) |
|
1339 trshift += m.elem (i, i); |
|
1340 |
|
1341 trshift /= nc; |
|
1342 |
|
1343 for (int i = 0; i < nc; i++) |
|
1344 m.elem (i, i) -= trshift; |
|
1345 |
|
1346 // Preconditioning step 2: balancing. |
|
1347 |
|
1348 AEPBALANCE mbal (m, "B"); |
|
1349 m = mbal.balanced_matrix (); |
|
1350 Matrix d = mbal.balancing_matrix (); |
|
1351 |
|
1352 // Preconditioning step 3: scaling. |
|
1353 |
|
1354 ColumnVector work(nc); |
|
1355 double inf_norm |
|
1356 = F77_FCN (dlange, DLANGE) ("I", nc, nc, m.fortran_vec (),nc, |
|
1357 work.fortran_vec ()); |
|
1358 |
|
1359 int sqpow = (int) (inf_norm > 0.0 |
|
1360 ? (1.0 + log (inf_norm) / log (2.0)) |
|
1361 : 0.0); |
|
1362 |
|
1363 // Check whether we need to square at all. |
|
1364 |
|
1365 if (sqpow < 0) |
|
1366 sqpow = 0; |
|
1367 |
|
1368 if (sqpow > 0) |
|
1369 { |
|
1370 double scale_factor = 1.0; |
|
1371 for (int i = 0; i < sqpow; i++) |
|
1372 scale_factor *= 2.0; |
|
1373 |
|
1374 m = m / scale_factor; |
|
1375 } |
|
1376 |
|
1377 // npp, dpp: pade' approx polynomial matrices. |
|
1378 |
|
1379 Matrix npp (nc, nc, 0.0); |
|
1380 Matrix dpp = npp; |
|
1381 |
|
1382 // Now powers a^8 ... a^1. |
|
1383 |
|
1384 int minus_one_j = -1; |
|
1385 for (int j = 7; j >= 0; j--) |
|
1386 { |
|
1387 npp = m * npp + m * padec[j]; |
|
1388 dpp = m * dpp + m * (minus_one_j * padec[j]); |
|
1389 minus_one_j *= -1; |
|
1390 } |
|
1391 |
|
1392 // Zero power. |
|
1393 |
|
1394 dpp = -dpp; |
|
1395 for(int j = 0; j < nc; j++) |
|
1396 { |
|
1397 npp.elem (j, j) += 1.0; |
|
1398 dpp.elem (j, j) += 1.0; |
|
1399 } |
|
1400 |
|
1401 // Compute pade approximation = inverse (dpp) * npp. |
|
1402 |
|
1403 retval = dpp.solve (npp); |
|
1404 |
|
1405 // Reverse preconditioning step 3: repeated squaring. |
|
1406 |
|
1407 while (sqpow) |
|
1408 { |
|
1409 retval = retval * retval; |
|
1410 sqpow--; |
|
1411 } |
|
1412 |
|
1413 // Reverse preconditioning step 2: inverse balancing. |
|
1414 |
|
1415 retval = retval.transpose(); |
|
1416 d = d.transpose (); |
|
1417 retval = retval * d; |
|
1418 retval = d.solve (retval); |
|
1419 retval = retval.transpose (); |
|
1420 |
|
1421 // Reverse preconditioning step 1: fix trace normalization. |
|
1422 |
|
1423 return retval * exp (trshift); |
|
1424 } |
|
1425 |
458
|
1426 Matrix& |
|
1427 Matrix::operator += (const Matrix& a) |
|
1428 { |
|
1429 int nr = rows (); |
|
1430 int nc = cols (); |
2385
|
1431 |
|
1432 int a_nr = a.rows (); |
|
1433 int a_nc = a.cols (); |
|
1434 |
|
1435 if (nr != a_nr || nc != a_nc) |
458
|
1436 { |
2385
|
1437 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
1438 return *this; |
|
1439 } |
|
1440 |
|
1441 if (nr == 0 || nc == 0) |
|
1442 return *this; |
|
1443 |
|
1444 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1445 |
|
1446 add2 (d, a.data (), length ()); |
|
1447 |
|
1448 return *this; |
|
1449 } |
|
1450 |
|
1451 Matrix& |
|
1452 Matrix::operator -= (const Matrix& a) |
|
1453 { |
|
1454 int nr = rows (); |
|
1455 int nc = cols (); |
2385
|
1456 |
|
1457 int a_nr = a.rows (); |
|
1458 int a_nc = a.cols (); |
|
1459 |
|
1460 if (nr != a_nr || nc != a_nc) |
458
|
1461 { |
2385
|
1462 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
1463 return *this; |
|
1464 } |
|
1465 |
|
1466 if (nr == 0 || nc == 0) |
|
1467 return *this; |
|
1468 |
|
1469 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1470 |
|
1471 subtract2 (d, a.data (), length ()); |
|
1472 |
|
1473 return *this; |
|
1474 } |
|
1475 |
|
1476 Matrix& |
|
1477 Matrix::operator += (const DiagMatrix& a) |
|
1478 { |
2385
|
1479 int nr = rows (); |
|
1480 int nc = cols (); |
|
1481 |
|
1482 int a_nr = a.rows (); |
|
1483 int a_nc = a.cols (); |
|
1484 |
|
1485 if (nr != a_nr || nc != a_nc) |
458
|
1486 { |
2385
|
1487 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
1488 return *this; |
|
1489 } |
|
1490 |
|
1491 for (int i = 0; i < a.length (); i++) |
|
1492 elem (i, i) += a.elem (i, i); |
|
1493 |
|
1494 return *this; |
|
1495 } |
|
1496 |
|
1497 Matrix& |
|
1498 Matrix::operator -= (const DiagMatrix& a) |
|
1499 { |
2385
|
1500 int nr = rows (); |
|
1501 int nc = cols (); |
|
1502 |
|
1503 int a_nr = a.rows (); |
|
1504 int a_nc = a.cols (); |
|
1505 |
|
1506 if (nr != a_nr || nc != a_nc) |
458
|
1507 { |
2385
|
1508 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
1509 return *this; |
|
1510 } |
|
1511 |
|
1512 for (int i = 0; i < a.length (); i++) |
|
1513 elem (i, i) -= a.elem (i, i); |
|
1514 |
|
1515 return *this; |
|
1516 } |
|
1517 |
|
1518 // unary operations |
|
1519 |
|
1520 Matrix |
|
1521 Matrix::operator ! (void) const |
|
1522 { |
|
1523 int nr = rows (); |
|
1524 int nc = cols (); |
|
1525 |
|
1526 Matrix b (nr, nc); |
|
1527 |
|
1528 for (int j = 0; j < nc; j++) |
|
1529 for (int i = 0; i < nr; i++) |
|
1530 b.elem (i, j) = ! elem (i, j); |
|
1531 |
|
1532 return b; |
|
1533 } |
|
1534 |
1205
|
1535 // column vector by row vector -> matrix operations |
458
|
1536 |
1205
|
1537 Matrix |
|
1538 operator * (const ColumnVector& v, const RowVector& a) |
458
|
1539 { |
1948
|
1540 Matrix retval; |
|
1541 |
1205
|
1542 int len = v.length (); |
|
1543 int a_len = a.length (); |
1948
|
1544 |
1205
|
1545 if (len != a_len) |
2385
|
1546 gripe_nonconformant ("operator *", len, 1, 1, a_len); |
1948
|
1547 else |
1205
|
1548 { |
1948
|
1549 if (len != 0) |
|
1550 { |
|
1551 retval.resize (len, a_len); |
|
1552 double *c = retval.fortran_vec (); |
|
1553 |
|
1554 F77_XFCN (dgemm, DGEMM, ("N", "N", len, a_len, 1, 1.0, |
|
1555 v.data (), len, a.data (), 1, 0.0, |
|
1556 c, len, 1L, 1L)); |
|
1557 |
|
1558 if (f77_exception_encountered) |
|
1559 (*current_liboctave_error_handler) |
|
1560 ("unrecoverable error in dgemm"); |
|
1561 } |
1205
|
1562 } |
458
|
1563 |
1948
|
1564 return retval; |
458
|
1565 } |
|
1566 |
|
1567 // other operations. |
|
1568 |
|
1569 Matrix |
2676
|
1570 Matrix::map (d_d_Mapper f) const |
1205
|
1571 { |
2676
|
1572 Matrix b (*this); |
|
1573 return b.apply (f); |
1205
|
1574 } |
|
1575 |
2676
|
1576 Matrix& |
|
1577 Matrix::apply (d_d_Mapper f) |
458
|
1578 { |
|
1579 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1580 |
|
1581 for (int i = 0; i < length (); i++) |
|
1582 d[i] = f (d[i]); |
2676
|
1583 |
|
1584 return *this; |
458
|
1585 } |
|
1586 |
2385
|
1587 bool |
|
1588 Matrix::any_element_is_negative (void) const |
|
1589 { |
|
1590 int nr = rows (); |
|
1591 int nc = cols (); |
|
1592 |
|
1593 for (int j = 0; j < nc; j++) |
|
1594 for (int i = 0; i < nr; i++) |
|
1595 if (elem (i, j) < 0.0) |
|
1596 return true; |
|
1597 |
|
1598 return false; |
|
1599 } |
|
1600 |
|
1601 |
|
1602 bool |
|
1603 Matrix::any_element_is_inf_or_nan (void) const |
|
1604 { |
|
1605 int nr = rows (); |
|
1606 int nc = cols (); |
|
1607 |
|
1608 for (int j = 0; j < nc; j++) |
|
1609 for (int i = 0; i < nr; i++) |
|
1610 { |
|
1611 double val = elem (i, j); |
|
1612 if (xisinf (val) || xisnan (val)) |
|
1613 return 1; |
|
1614 } |
|
1615 return 0; |
|
1616 } |
|
1617 |
|
1618 bool |
|
1619 Matrix::all_elements_are_int_or_inf_or_nan (void) const |
|
1620 { |
|
1621 int nr = rows (); |
|
1622 int nc = cols (); |
|
1623 |
|
1624 for (int j = 0; j < nc; j++) |
|
1625 for (int i = 0; i < nr; i++) |
|
1626 { |
|
1627 double val = elem (i, j); |
|
1628 if (xisnan (val) || D_NINT (val) == val) |
|
1629 continue; |
|
1630 else |
|
1631 return false; |
|
1632 } |
|
1633 |
|
1634 return true; |
|
1635 } |
|
1636 |
1968
|
1637 // Return nonzero if any element of M is not an integer. Also extract |
|
1638 // the largest and smallest values and return them in MAX_VAL and MIN_VAL. |
|
1639 |
2385
|
1640 bool |
1968
|
1641 Matrix::all_integers (double& max_val, double& min_val) const |
|
1642 { |
|
1643 int nr = rows (); |
|
1644 int nc = cols (); |
|
1645 |
|
1646 if (nr > 0 && nc > 0) |
|
1647 { |
|
1648 max_val = elem (0, 0); |
|
1649 min_val = elem (0, 0); |
|
1650 } |
|
1651 else |
2385
|
1652 return false; |
1968
|
1653 |
|
1654 for (int j = 0; j < nc; j++) |
|
1655 for (int i = 0; i < nr; i++) |
|
1656 { |
|
1657 double val = elem (i, j); |
|
1658 |
|
1659 if (val > max_val) |
|
1660 max_val = val; |
|
1661 |
|
1662 if (val < min_val) |
|
1663 min_val = val; |
|
1664 |
|
1665 if (D_NINT (val) != val) |
2385
|
1666 return false; |
1968
|
1667 } |
2385
|
1668 |
|
1669 return true; |
1968
|
1670 } |
|
1671 |
2385
|
1672 bool |
1968
|
1673 Matrix::too_large_for_float (void) const |
|
1674 { |
|
1675 int nr = rows (); |
2385
|
1676 int nc = cols (); |
1968
|
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 |
|
1683 if (val > FLT_MAX || val < FLT_MIN) |
2385
|
1684 return true; |
1968
|
1685 } |
|
1686 |
2385
|
1687 return false; |
1968
|
1688 } |
|
1689 |
458
|
1690 // XXX FIXME XXX Do these really belong here? They should maybe be |
|
1691 // cleaned up a bit, no? What about corresponding functions for the |
|
1692 // Vectors? |
|
1693 |
|
1694 Matrix |
|
1695 Matrix::all (void) const |
|
1696 { |
|
1697 int nr = rows (); |
|
1698 int nc = cols (); |
|
1699 Matrix retval; |
|
1700 if (nr > 0 && nc > 0) |
|
1701 { |
|
1702 if (nr == 1) |
|
1703 { |
|
1704 retval.resize (1, 1); |
|
1705 retval.elem (0, 0) = 1.0; |
|
1706 for (int j = 0; j < nc; j++) |
|
1707 { |
|
1708 if (elem (0, j) == 0.0) |
|
1709 { |
|
1710 retval.elem (0, 0) = 0.0; |
|
1711 break; |
|
1712 } |
|
1713 } |
|
1714 } |
|
1715 else if (nc == 1) |
|
1716 { |
|
1717 retval.resize (1, 1); |
|
1718 retval.elem (0, 0) = 1.0; |
|
1719 for (int i = 0; i < nr; i++) |
|
1720 { |
|
1721 if (elem (i, 0) == 0.0) |
|
1722 { |
|
1723 retval.elem (0, 0) = 0.0; |
|
1724 break; |
|
1725 } |
|
1726 } |
|
1727 } |
|
1728 else |
|
1729 { |
|
1730 retval.resize (1, nc); |
|
1731 for (int j = 0; j < nc; j++) |
|
1732 { |
|
1733 retval.elem (0, j) = 1.0; |
|
1734 for (int i = 0; i < nr; i++) |
|
1735 { |
|
1736 if (elem (i, j) == 0.0) |
|
1737 { |
|
1738 retval.elem (0, j) = 0.0; |
|
1739 break; |
|
1740 } |
|
1741 } |
|
1742 } |
|
1743 } |
|
1744 } |
|
1745 return retval; |
|
1746 } |
|
1747 |
|
1748 Matrix |
|
1749 Matrix::any (void) const |
|
1750 { |
|
1751 int nr = rows (); |
|
1752 int nc = cols (); |
|
1753 Matrix retval; |
|
1754 if (nr > 0 && nc > 0) |
|
1755 { |
|
1756 if (nr == 1) |
|
1757 { |
|
1758 retval.resize (1, 1); |
|
1759 retval.elem (0, 0) = 0.0; |
|
1760 for (int j = 0; j < nc; j++) |
|
1761 { |
|
1762 if (elem (0, j) != 0.0) |
|
1763 { |
|
1764 retval.elem (0, 0) = 1.0; |
|
1765 break; |
|
1766 } |
|
1767 } |
|
1768 } |
|
1769 else if (nc == 1) |
|
1770 { |
|
1771 retval.resize (1, 1); |
|
1772 retval.elem (0, 0) = 0.0; |
|
1773 for (int i = 0; i < nr; i++) |
|
1774 { |
|
1775 if (elem (i, 0) != 0.0) |
|
1776 { |
|
1777 retval.elem (0, 0) = 1.0; |
|
1778 break; |
|
1779 } |
|
1780 } |
|
1781 } |
|
1782 else |
|
1783 { |
|
1784 retval.resize (1, nc); |
|
1785 for (int j = 0; j < nc; j++) |
|
1786 { |
|
1787 retval.elem (0, j) = 0.0; |
|
1788 for (int i = 0; i < nr; i++) |
|
1789 { |
|
1790 if (elem (i, j) != 0.0) |
|
1791 { |
|
1792 retval.elem (0, j) = 1.0; |
|
1793 break; |
|
1794 } |
|
1795 } |
|
1796 } |
|
1797 } |
|
1798 } |
|
1799 return retval; |
|
1800 } |
|
1801 |
|
1802 Matrix |
|
1803 Matrix::cumprod (void) const |
|
1804 { |
|
1805 Matrix retval; |
|
1806 |
|
1807 int nr = rows (); |
|
1808 int nc = cols (); |
|
1809 |
|
1810 if (nr == 1) |
|
1811 { |
|
1812 retval.resize (1, nc); |
|
1813 if (nc > 0) |
|
1814 { |
|
1815 double prod = elem (0, 0); |
|
1816 for (int j = 0; j < nc; j++) |
|
1817 { |
|
1818 retval.elem (0, j) = prod; |
|
1819 if (j < nc - 1) |
|
1820 prod *= elem (0, j+1); |
|
1821 } |
|
1822 } |
|
1823 } |
|
1824 else if (nc == 1) |
|
1825 { |
|
1826 retval.resize (nr, 1); |
|
1827 if (nr > 0) |
|
1828 { |
|
1829 double prod = elem (0, 0); |
|
1830 for (int i = 0; i < nr; i++) |
|
1831 { |
|
1832 retval.elem (i, 0) = prod; |
|
1833 if (i < nr - 1) |
|
1834 prod *= elem (i+1, 0); |
|
1835 } |
|
1836 } |
|
1837 } |
|
1838 else |
|
1839 { |
|
1840 retval.resize (nr, nc); |
|
1841 if (nr > 0 && nc > 0) |
|
1842 { |
|
1843 for (int j = 0; j < nc; j++) |
|
1844 { |
|
1845 double prod = elem (0, j); |
|
1846 for (int i = 0; i < nr; i++) |
|
1847 { |
|
1848 retval.elem (i, j) = prod; |
|
1849 if (i < nr - 1) |
|
1850 prod *= elem (i+1, j); |
|
1851 } |
|
1852 } |
|
1853 } |
|
1854 } |
|
1855 return retval; |
|
1856 } |
|
1857 |
|
1858 Matrix |
|
1859 Matrix::cumsum (void) const |
|
1860 { |
|
1861 Matrix retval; |
|
1862 |
|
1863 int nr = rows (); |
|
1864 int nc = cols (); |
|
1865 |
|
1866 if (nr == 1) |
|
1867 { |
|
1868 retval.resize (1, nc); |
|
1869 if (nc > 0) |
|
1870 { |
|
1871 double sum = elem (0, 0); |
|
1872 for (int j = 0; j < nc; j++) |
|
1873 { |
|
1874 retval.elem (0, j) = sum; |
|
1875 if (j < nc - 1) |
|
1876 sum += elem (0, j+1); |
|
1877 } |
|
1878 } |
|
1879 } |
|
1880 else if (nc == 1) |
|
1881 { |
|
1882 retval.resize (nr, 1); |
|
1883 if (nr > 0) |
|
1884 { |
|
1885 double sum = elem (0, 0); |
|
1886 for (int i = 0; i < nr; i++) |
|
1887 { |
|
1888 retval.elem (i, 0) = sum; |
|
1889 if (i < nr - 1) |
|
1890 sum += elem (i+1, 0); |
|
1891 } |
|
1892 } |
|
1893 } |
|
1894 else |
|
1895 { |
|
1896 retval.resize (nr, nc); |
|
1897 if (nr > 0 && nc > 0) |
|
1898 { |
|
1899 for (int j = 0; j < nc; j++) |
|
1900 { |
|
1901 double sum = elem (0, j); |
|
1902 for (int i = 0; i < nr; i++) |
|
1903 { |
|
1904 retval.elem (i, j) = sum; |
|
1905 if (i < nr - 1) |
|
1906 sum += elem (i+1, j); |
|
1907 } |
|
1908 } |
|
1909 } |
|
1910 } |
|
1911 return retval; |
|
1912 } |
|
1913 |
|
1914 Matrix |
|
1915 Matrix::prod (void) const |
|
1916 { |
|
1917 Matrix retval; |
|
1918 |
|
1919 int nr = rows (); |
|
1920 int nc = cols (); |
|
1921 |
|
1922 if (nr == 1) |
|
1923 { |
|
1924 retval.resize (1, 1); |
|
1925 retval.elem (0, 0) = 1.0; |
|
1926 for (int j = 0; j < nc; j++) |
|
1927 retval.elem (0, 0) *= elem (0, j); |
|
1928 } |
|
1929 else if (nc == 1) |
|
1930 { |
|
1931 retval.resize (1, 1); |
|
1932 retval.elem (0, 0) = 1.0; |
|
1933 for (int i = 0; i < nr; i++) |
|
1934 retval.elem (0, 0) *= elem (i, 0); |
|
1935 } |
|
1936 else |
|
1937 { |
|
1938 if (nc == 0) |
|
1939 { |
|
1940 retval.resize (1, 1); |
|
1941 retval.elem (0, 0) = 1.0; |
|
1942 } |
|
1943 else |
|
1944 retval.resize (1, nc); |
|
1945 |
|
1946 for (int j = 0; j < nc; j++) |
|
1947 { |
|
1948 retval.elem (0, j) = 1.0; |
|
1949 for (int i = 0; i < nr; i++) |
|
1950 retval.elem (0, j) *= elem (i, j); |
|
1951 } |
|
1952 } |
|
1953 return retval; |
|
1954 } |
|
1955 |
|
1956 Matrix |
|
1957 Matrix::sum (void) const |
|
1958 { |
|
1959 Matrix retval; |
|
1960 |
|
1961 int nr = rows (); |
|
1962 int nc = cols (); |
|
1963 |
|
1964 if (nr == 1) |
|
1965 { |
|
1966 retval.resize (1, 1); |
|
1967 retval.elem (0, 0) = 0.0; |
|
1968 for (int j = 0; j < nc; j++) |
|
1969 retval.elem (0, 0) += elem (0, j); |
|
1970 } |
|
1971 else if (nc == 1) |
|
1972 { |
|
1973 retval.resize (1, 1); |
|
1974 retval.elem (0, 0) = 0.0; |
|
1975 for (int i = 0; i < nr; i++) |
|
1976 retval.elem (0, 0) += elem (i, 0); |
|
1977 } |
|
1978 else |
|
1979 { |
|
1980 if (nc == 0) |
|
1981 { |
|
1982 retval.resize (1, 1); |
|
1983 retval.elem (0, 0) = 0.0; |
|
1984 } |
|
1985 else |
|
1986 retval.resize (1, nc); |
|
1987 |
|
1988 for (int j = 0; j < nc; j++) |
|
1989 { |
|
1990 retval.elem (0, j) = 0.0; |
|
1991 for (int i = 0; i < nr; i++) |
|
1992 retval.elem (0, j) += elem (i, j); |
|
1993 } |
|
1994 } |
|
1995 return retval; |
|
1996 } |
|
1997 |
|
1998 Matrix |
|
1999 Matrix::sumsq (void) const |
|
2000 { |
|
2001 Matrix retval; |
|
2002 |
|
2003 int nr = rows (); |
|
2004 int nc = cols (); |
|
2005 |
|
2006 if (nr == 1) |
|
2007 { |
|
2008 retval.resize (1, 1); |
|
2009 retval.elem (0, 0) = 0.0; |
|
2010 for (int j = 0; j < nc; j++) |
|
2011 { |
|
2012 double d = elem (0, j); |
|
2013 retval.elem (0, 0) += d * d; |
|
2014 } |
|
2015 } |
|
2016 else if (nc == 1) |
|
2017 { |
|
2018 retval.resize (1, 1); |
|
2019 retval.elem (0, 0) = 0.0; |
|
2020 for (int i = 0; i < nr; i++) |
|
2021 { |
|
2022 double d = elem (i, 0); |
|
2023 retval.elem (0, 0) += d * d; |
|
2024 } |
|
2025 } |
|
2026 else |
|
2027 { |
|
2028 retval.resize (1, nc); |
|
2029 for (int j = 0; j < nc; j++) |
|
2030 { |
|
2031 retval.elem (0, j) = 0.0; |
|
2032 for (int i = 0; i < nr; i++) |
|
2033 { |
|
2034 double d = elem (i, j); |
|
2035 retval.elem (0, j) += d * d; |
|
2036 } |
|
2037 } |
|
2038 } |
|
2039 return retval; |
|
2040 } |
|
2041 |
2385
|
2042 Matrix |
|
2043 Matrix::abs (void) const |
|
2044 { |
|
2045 int nr = rows (); |
|
2046 int nc = cols (); |
|
2047 |
|
2048 Matrix retval (nr, nc); |
|
2049 |
|
2050 for (int j = 0; j < nc; j++) |
|
2051 for (int i = 0; i < nr; i++) |
|
2052 retval (i, j) = fabs (elem (i, j)); |
|
2053 |
|
2054 return retval; |
|
2055 } |
|
2056 |
458
|
2057 ColumnVector |
|
2058 Matrix::diag (void) const |
|
2059 { |
|
2060 return diag (0); |
|
2061 } |
|
2062 |
|
2063 ColumnVector |
|
2064 Matrix::diag (int k) const |
|
2065 { |
|
2066 int nnr = rows (); |
|
2067 int nnc = cols (); |
|
2068 if (k > 0) |
|
2069 nnc -= k; |
|
2070 else if (k < 0) |
|
2071 nnr += k; |
|
2072 |
|
2073 ColumnVector d; |
|
2074 |
|
2075 if (nnr > 0 && nnc > 0) |
|
2076 { |
|
2077 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
2078 |
|
2079 d.resize (ndiag); |
|
2080 |
|
2081 if (k > 0) |
|
2082 { |
|
2083 for (int i = 0; i < ndiag; i++) |
|
2084 d.elem (i) = elem (i, i+k); |
|
2085 } |
|
2086 else if ( k < 0) |
|
2087 { |
|
2088 for (int i = 0; i < ndiag; i++) |
|
2089 d.elem (i) = elem (i-k, i); |
|
2090 } |
|
2091 else |
|
2092 { |
|
2093 for (int i = 0; i < ndiag; i++) |
|
2094 d.elem (i) = elem (i, i); |
|
2095 } |
|
2096 } |
|
2097 else |
|
2098 cerr << "diag: requested diagonal out of range\n"; |
|
2099 |
|
2100 return d; |
|
2101 } |
|
2102 |
|
2103 ColumnVector |
|
2104 Matrix::row_min (void) const |
|
2105 { |
2354
|
2106 Array<int> index; |
|
2107 return row_min (index); |
458
|
2108 } |
|
2109 |
|
2110 ColumnVector |
2354
|
2111 Matrix::row_min (Array<int>& index) const |
458
|
2112 { |
|
2113 ColumnVector result; |
|
2114 |
|
2115 int nr = rows (); |
|
2116 int nc = cols (); |
|
2117 |
|
2118 if (nr > 0 && nc > 0) |
|
2119 { |
|
2120 result.resize (nr); |
2354
|
2121 index.resize (nr); |
458
|
2122 |
|
2123 for (int i = 0; i < nr; i++) |
|
2124 { |
2354
|
2125 int idx = 0; |
|
2126 |
|
2127 double tmp_min = elem (i, idx); |
|
2128 |
|
2129 if (xisnan (tmp_min)) |
|
2130 idx = -1; |
|
2131 else |
|
2132 { |
|
2133 for (int j = 1; j < nc; j++) |
|
2134 { |
|
2135 double tmp = elem (i, j); |
|
2136 |
|
2137 if (xisnan (tmp)) |
|
2138 { |
|
2139 idx = -1; |
|
2140 break; |
|
2141 } |
|
2142 else if (tmp < tmp_min) |
|
2143 { |
|
2144 idx = j; |
|
2145 tmp_min = tmp; |
|
2146 } |
|
2147 } |
|
2148 } |
|
2149 |
|
2150 result.elem (i) = (idx < 0) ? octave_NaN : tmp_min; |
|
2151 index.elem (i) = idx; |
458
|
2152 } |
|
2153 } |
|
2154 |
|
2155 return result; |
|
2156 } |
|
2157 |
|
2158 ColumnVector |
|
2159 Matrix::row_max (void) const |
|
2160 { |
2354
|
2161 Array<int> index; |
|
2162 return row_max (index); |
458
|
2163 } |
|
2164 |
|
2165 ColumnVector |
2354
|
2166 Matrix::row_max (Array<int>& index) const |
458
|
2167 { |
|
2168 ColumnVector result; |
|
2169 |
|
2170 int nr = rows (); |
|
2171 int nc = cols (); |
|
2172 |
|
2173 if (nr > 0 && nc > 0) |
|
2174 { |
|
2175 result.resize (nr); |
2354
|
2176 index.resize (nr); |
458
|
2177 |
|
2178 for (int i = 0; i < nr; i++) |
|
2179 { |
2354
|
2180 int idx = 0; |
|
2181 |
|
2182 double tmp_max = elem (i, idx); |
|
2183 |
|
2184 if (xisnan (tmp_max)) |
|
2185 idx = -1; |
|
2186 else |
|
2187 { |
|
2188 for (int j = 1; j < nc; j++) |
|
2189 { |
|
2190 double tmp = elem (i, j); |
|
2191 |
|
2192 if (xisnan (tmp)) |
|
2193 { |
|
2194 idx = -1; |
|
2195 break; |
|
2196 } |
|
2197 else if (tmp > tmp_max) |
|
2198 { |
|
2199 idx = j; |
|
2200 tmp_max = tmp; |
|
2201 } |
|
2202 } |
|
2203 } |
|
2204 |
|
2205 result.elem (i) = (idx < 0) ? octave_NaN : tmp_max; |
|
2206 index.elem (i) = idx; |
458
|
2207 } |
|
2208 } |
|
2209 |
|
2210 return result; |
|
2211 } |
|
2212 |
|
2213 RowVector |
|
2214 Matrix::column_min (void) const |
|
2215 { |
2354
|
2216 Array<int> index; |
|
2217 return column_min (index); |
458
|
2218 } |
2354
|
2219 |
458
|
2220 RowVector |
2354
|
2221 Matrix::column_min (Array<int>& index) const |
458
|
2222 { |
|
2223 RowVector result; |
|
2224 |
|
2225 int nr = rows (); |
|
2226 int nc = cols (); |
|
2227 |
|
2228 if (nr > 0 && nc > 0) |
|
2229 { |
|
2230 result.resize (nc); |
2354
|
2231 index.resize (nc); |
458
|
2232 |
|
2233 for (int j = 0; j < nc; j++) |
|
2234 { |
2354
|
2235 int idx = 0; |
|
2236 |
|
2237 double tmp_min = elem (idx, j); |
|
2238 |
|
2239 if (xisnan (tmp_min)) |
|
2240 idx = -1; |
|
2241 else |
|
2242 { |
|
2243 for (int i = 1; i < nr; i++) |
|
2244 { |
|
2245 double tmp = elem (i, j); |
|
2246 |
|
2247 if (xisnan (tmp)) |
|
2248 { |
|
2249 idx = -1; |
|
2250 break; |
|
2251 } |
|
2252 else if (tmp < tmp_min) |
|
2253 { |
|
2254 idx = i; |
|
2255 tmp_min = tmp; |
|
2256 } |
|
2257 } |
|
2258 } |
|
2259 |
|
2260 result.elem (j) = (idx < 0) ? octave_NaN : tmp_min; |
|
2261 index.elem (j) = idx; |
458
|
2262 } |
|
2263 } |
|
2264 |
|
2265 return result; |
|
2266 } |
|
2267 |
2354
|
2268 RowVector |
|
2269 Matrix::column_max (void) const |
|
2270 { |
|
2271 Array<int> index; |
|
2272 return column_max (index); |
|
2273 } |
458
|
2274 |
|
2275 RowVector |
2354
|
2276 Matrix::column_max (Array<int>& index) const |
458
|
2277 { |
|
2278 RowVector result; |
|
2279 |
|
2280 int nr = rows (); |
|
2281 int nc = cols (); |
|
2282 |
|
2283 if (nr > 0 && nc > 0) |
|
2284 { |
|
2285 result.resize (nc); |
2354
|
2286 index.resize (nc); |
458
|
2287 |
|
2288 for (int j = 0; j < nc; j++) |
|
2289 { |
2354
|
2290 int idx = 0; |
|
2291 |
|
2292 double tmp_max = elem (idx, j); |
|
2293 |
|
2294 if (xisnan (tmp_max)) |
|
2295 idx = -1; |
|
2296 else |
|
2297 { |
|
2298 for (int i = 1; i < nr; i++) |
|
2299 { |
|
2300 double tmp = elem (i, j); |
|
2301 |
|
2302 if (xisnan (tmp)) |
|
2303 { |
|
2304 idx = -1; |
|
2305 break; |
|
2306 } |
|
2307 else if (tmp > tmp_max) |
|
2308 { |
|
2309 idx = i; |
|
2310 tmp_max = tmp; |
|
2311 } |
|
2312 } |
|
2313 } |
|
2314 |
|
2315 result.elem (j) = (idx < 0) ? octave_NaN : tmp_max; |
|
2316 index.elem (j) = idx; |
458
|
2317 } |
|
2318 } |
|
2319 |
|
2320 return result; |
|
2321 } |
|
2322 |
|
2323 ostream& |
|
2324 operator << (ostream& os, const Matrix& a) |
|
2325 { |
|
2326 // int field_width = os.precision () + 7; |
1360
|
2327 |
458
|
2328 for (int i = 0; i < a.rows (); i++) |
|
2329 { |
|
2330 for (int j = 0; j < a.cols (); j++) |
|
2331 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
2332 os << "\n"; |
|
2333 } |
|
2334 return os; |
|
2335 } |
|
2336 |
|
2337 istream& |
|
2338 operator >> (istream& is, Matrix& a) |
|
2339 { |
|
2340 int nr = a.rows (); |
|
2341 int nc = a.cols (); |
|
2342 |
|
2343 if (nr < 1 || nc < 1) |
|
2344 is.clear (ios::badbit); |
|
2345 else |
|
2346 { |
|
2347 double tmp; |
|
2348 for (int i = 0; i < nr; i++) |
|
2349 for (int j = 0; j < nc; j++) |
|
2350 { |
|
2351 is >> tmp; |
|
2352 if (is) |
|
2353 a.elem (i, j) = tmp; |
|
2354 else |
2795
|
2355 goto done; |
458
|
2356 } |
|
2357 } |
|
2358 |
2795
|
2359 done: |
|
2360 |
458
|
2361 return is; |
|
2362 } |
|
2363 |
2317
|
2364 template <class T> |
|
2365 static void |
|
2366 read_int (istream& is, bool swap_bytes, T& val) |
|
2367 { |
2800
|
2368 is.read (static_cast<char *> (&val), sizeof (T)); |
2317
|
2369 |
|
2370 if (swap_bytes) |
|
2371 { |
|
2372 switch (sizeof (T)) |
|
2373 { |
|
2374 case 1: |
|
2375 break; |
|
2376 |
|
2377 case 2: |
2800
|
2378 swap_2_bytes (static_cast<char *> (&val)); |
2317
|
2379 break; |
|
2380 |
|
2381 case 4: |
2800
|
2382 swap_4_bytes (static_cast<char *> (&val)); |
2317
|
2383 break; |
|
2384 |
|
2385 case 8: |
2800
|
2386 swap_8_bytes (static_cast<char *> (&val)); |
2317
|
2387 break; |
|
2388 |
|
2389 default: |
|
2390 (*current_liboctave_error_handler) |
|
2391 ("read_int: unrecognized data format!"); |
|
2392 } |
|
2393 } |
|
2394 } |
|
2395 |
|
2396 template void read_int (istream&, bool, char&); |
|
2397 template void read_int (istream&, bool, signed char&); |
|
2398 template void read_int (istream&, bool, unsigned char&); |
|
2399 template void read_int (istream&, bool, short&); |
|
2400 template void read_int (istream&, bool, unsigned short&); |
|
2401 template void read_int (istream&, bool, int&); |
|
2402 template void read_int (istream&, bool, unsigned int&); |
|
2403 template void read_int (istream&, bool, long&); |
|
2404 template void read_int (istream&, bool, unsigned long&); |
|
2405 |
|
2406 static inline bool |
|
2407 do_read (istream& is, oct_data_conv::data_type dt, |
|
2408 oct_mach_info::float_format flt_fmt, bool swap_bytes, |
|
2409 bool do_float_conversion, double& val) |
|
2410 { |
|
2411 bool retval = true; |
|
2412 |
|
2413 switch (dt) |
|
2414 { |
|
2415 case oct_data_conv::dt_char: |
|
2416 { |
|
2417 char tmp; |
|
2418 read_int (is, swap_bytes, tmp); |
|
2419 val = tmp; |
|
2420 } |
|
2421 break; |
|
2422 |
|
2423 case oct_data_conv::dt_schar: |
|
2424 { |
|
2425 signed char tmp; |
|
2426 read_int (is, swap_bytes, tmp); |
|
2427 val = tmp; |
|
2428 } |
|
2429 break; |
|
2430 |
|
2431 case oct_data_conv::dt_uchar: |
|
2432 { |
|
2433 unsigned char tmp; |
|
2434 read_int (is, swap_bytes, tmp); |
|
2435 val = tmp; |
|
2436 } |
|
2437 break; |
|
2438 |
|
2439 case oct_data_conv::dt_short: |
|
2440 { |
|
2441 short tmp; |
|
2442 read_int (is, swap_bytes, tmp); |
|
2443 val = tmp; |
|
2444 } |
|
2445 break; |
|
2446 |
|
2447 case oct_data_conv::dt_ushort: |
|
2448 { |
|
2449 unsigned short tmp; |
|
2450 read_int (is, swap_bytes, tmp); |
|
2451 val = tmp; |
|
2452 } |
|
2453 break; |
|
2454 |
|
2455 case oct_data_conv::dt_int: |
|
2456 { |
|
2457 int tmp; |
|
2458 read_int (is, swap_bytes, tmp); |
|
2459 val = tmp; |
|
2460 } |
|
2461 break; |
|
2462 |
|
2463 case oct_data_conv::dt_uint: |
|
2464 { |
|
2465 unsigned int tmp; |
|
2466 read_int (is, swap_bytes, tmp); |
|
2467 val = tmp; |
|
2468 } |
|
2469 break; |
|
2470 |
|
2471 case oct_data_conv::dt_long: |
|
2472 { |
|
2473 long tmp; |
|
2474 read_int (is, swap_bytes, tmp); |
|
2475 val = tmp; |
|
2476 } |
|
2477 break; |
|
2478 |
|
2479 case oct_data_conv::dt_ulong: |
|
2480 { |
|
2481 unsigned long tmp; |
|
2482 read_int (is, swap_bytes, tmp); |
|
2483 val = tmp; |
|
2484 } |
|
2485 break; |
|
2486 |
|
2487 case oct_data_conv::dt_float: |
|
2488 { |
|
2489 float f; |
|
2490 |
2800
|
2491 is.read (static_cast<char *> (&f), sizeof (float)); |
2317
|
2492 |
|
2493 if (do_float_conversion) |
|
2494 do_float_format_conversion (&f, 1, flt_fmt); |
|
2495 |
|
2496 val = f; |
|
2497 } |
|
2498 break; |
|
2499 |
|
2500 case oct_data_conv::dt_double: |
|
2501 { |
2800
|
2502 is.read (static_cast<char *> (&val), sizeof (double)); |
2317
|
2503 |
|
2504 if (do_float_conversion) |
|
2505 do_double_format_conversion (&val, 1, flt_fmt); |
|
2506 } |
|
2507 break; |
|
2508 |
|
2509 default: |
|
2510 retval = false; |
|
2511 (*current_liboctave_error_handler) |
|
2512 ("read: invalid type specification"); |
|
2513 break; |
|
2514 } |
|
2515 |
|
2516 return retval; |
|
2517 } |
1360
|
2518 |
458
|
2519 int |
2317
|
2520 Matrix::read (istream& is, int nr, int nc, |
|
2521 oct_data_conv::data_type dt, int skip, |
|
2522 oct_mach_info::float_format flt_fmt) |
458
|
2523 { |
2317
|
2524 int retval = -1; |
|
2525 |
|
2526 bool ok = true; |
|
2527 |
|
2528 int count = 0; |
|
2529 |
|
2530 double *data = 0; |
|
2531 int max_size = 0; |
|
2532 |
|
2533 int final_nr = 0; |
|
2534 int final_nc = 0; |
|
2535 |
|
2536 if (nr > 0) |
458
|
2537 { |
2317
|
2538 if (nc > 0) |
|
2539 { |
|
2540 resize (nr, nc, 0.0); |
|
2541 data = fortran_vec (); |
|
2542 max_size = nr * nc; |
|
2543 } |
|
2544 else |
|
2545 { |
|
2546 resize (nr, 32, 0.0); |
|
2547 data = fortran_vec (); |
|
2548 max_size = nr * 32; |
|
2549 } |
|
2550 } |
471
|
2551 else |
|
2552 { |
2317
|
2553 resize (32, 1, 0.0); |
|
2554 data = fortran_vec (); |
|
2555 max_size = 32; |
|
2556 } |
|
2557 |
|
2558 oct_mach_info::float_format native_flt_fmt |
|
2559 = oct_mach_info::float_format (); |
|
2560 |
|
2561 bool do_float_conversion = (flt_fmt != native_flt_fmt); |
|
2562 |
|
2563 // XXX FIXME XXX -- byte order for Cray? |
|
2564 |
|
2565 bool swap_bytes = false; |
|
2566 |
|
2567 if (oct_mach_info::words_big_endian ()) |
|
2568 swap_bytes = (flt_fmt == oct_mach_info::ieee_little_endian |
|
2569 || flt_fmt == oct_mach_info::vax_g |
|
2570 || flt_fmt == oct_mach_info::vax_g); |
|
2571 else |
|
2572 swap_bytes = (flt_fmt == oct_mach_info::ieee_big_endian); |
|
2573 |
|
2574 for (;;) |
|
2575 { |
|
2576 // XXX FIXME XXX -- maybe there should be a special case for |
|
2577 // skip == 0. |
|
2578 |
|
2579 if (is) |
|
2580 { |
|
2581 if (nr > 0 && nc > 0 && count == max_size) |
|
2582 { |
|
2583 final_nr = nr; |
|
2584 final_nc = nc; |
|
2585 |
|
2586 break; |
|
2587 } |
|
2588 |
|
2589 if (skip != 0) |
|
2590 is.seekg (skip, ios::cur); |
|
2591 |
|
2592 if (is) |
|
2593 { |
|
2594 double tmp = 0.0; |
|
2595 |
|
2596 ok = do_read (is, dt, flt_fmt, swap_bytes, |
|
2597 do_float_conversion, tmp); |
|
2598 |
|
2599 if (ok) |
|
2600 { |
|
2601 if (is) |
|
2602 { |
|
2603 if (count == max_size) |
|
2604 { |
|
2605 max_size *= 2; |
|
2606 |
|
2607 if (nr > 0) |
2601
|
2608 resize (nr, max_size / nr, 0.0); |
2317
|
2609 else |
|
2610 resize (max_size, 1, 0.0); |
|
2611 |
|
2612 data = fortran_vec (); |
|
2613 } |
|
2614 |
|
2615 data[count++] = tmp; |
|
2616 } |
|
2617 else |
|
2618 { |
|
2619 if (is.eof ()) |
|
2620 { |
|
2621 if (nr > 0) |
|
2622 { |
|
2623 if (count > nr) |
|
2624 { |
|
2625 final_nr = nr; |
|
2626 final_nc = (count - 1) / nr + 1; |
|
2627 } |
|
2628 else |
|
2629 { |
|
2630 final_nr = count; |
|
2631 final_nc = 1; |
|
2632 } |
|
2633 } |
|
2634 else |
|
2635 { |
|
2636 final_nr = count; |
|
2637 final_nc = 1; |
|
2638 } |
|
2639 } |
|
2640 |
|
2641 break; |
|
2642 } |
|
2643 } |
|
2644 else |
|
2645 break; |
|
2646 } |
|
2647 else |
|
2648 { |
|
2649 ok = false; |
|
2650 break; |
|
2651 } |
|
2652 } |
|
2653 else |
|
2654 { |
|
2655 ok = false; |
|
2656 break; |
|
2657 } |
|
2658 } |
|
2659 |
|
2660 if (ok) |
|
2661 { |
|
2662 resize (final_nr, final_nc, 0.0); |
|
2663 |
|
2664 retval = count; |
458
|
2665 } |
|
2666 |
2317
|
2667 return retval; |
|
2668 } |
|
2669 |
|
2670 template <class T> |
|
2671 static void |
|
2672 write_int (ostream& os, bool swap_bytes, T val) |
|
2673 { |
|
2674 if (swap_bytes) |
|
2675 { |
|
2676 switch (sizeof (T)) |
|
2677 { |
|
2678 case 1: |
|
2679 break; |
|
2680 |
|
2681 case 2: |
2800
|
2682 swap_2_bytes (static_cast<char *> (&val)); |
2317
|
2683 break; |
|
2684 |
|
2685 case 4: |
2800
|
2686 swap_4_bytes (static_cast<char *> (&val)); |
2317
|
2687 break; |
|
2688 |
|
2689 case 8: |
2800
|
2690 swap_8_bytes (static_cast<char *> (&val)); |
2317
|
2691 break; |
|
2692 |
|
2693 default: |
|
2694 (*current_liboctave_error_handler) |
|
2695 ("write_int: unrecognized data format!"); |
|
2696 } |
|
2697 } |
|
2698 |
2800
|
2699 os.write (static_cast<char *> (&val), sizeof (T)); |
458
|
2700 } |
|
2701 |
2317
|
2702 template void write_int (ostream&, bool, char); |
|
2703 template void write_int (ostream&, bool, signed char); |
|
2704 template void write_int (ostream&, bool, unsigned char); |
|
2705 template void write_int (ostream&, bool, short); |
|
2706 template void write_int (ostream&, bool, unsigned short); |
|
2707 template void write_int (ostream&, bool, int); |
|
2708 template void write_int (ostream&, bool, unsigned int); |
|
2709 template void write_int (ostream&, bool, long); |
|
2710 template void write_int (ostream&, bool, unsigned long); |
|
2711 |
|
2712 static inline bool |
|
2713 do_write (ostream& os, double d, oct_data_conv::data_type dt, |
|
2714 oct_mach_info::float_format flt_fmt, bool swap_bytes, |
|
2715 bool do_float_conversion) |
|
2716 { |
|
2717 bool retval = true; |
|
2718 |
|
2719 switch (dt) |
|
2720 { |
|
2721 case oct_data_conv::dt_char: |
2800
|
2722 write_int (os, swap_bytes, static_cast<char> (d)); |
2317
|
2723 break; |
|
2724 |
|
2725 case oct_data_conv::dt_schar: |
2800
|
2726 write_int (os, swap_bytes, static_cast<signed char> (d)); |
2317
|
2727 break; |
|
2728 |
|
2729 case oct_data_conv::dt_uchar: |
2800
|
2730 write_int (os, swap_bytes, static_cast<unsigned char> (d)); |
2317
|
2731 break; |
|
2732 |
|
2733 case oct_data_conv::dt_short: |
2800
|
2734 write_int (os, swap_bytes, static_cast<short> (d)); |
2317
|
2735 break; |
|
2736 |
|
2737 case oct_data_conv::dt_ushort: |
2800
|
2738 write_int (os, swap_bytes, static_cast<unsigned short> (d)); |
2317
|
2739 break; |
|
2740 |
|
2741 case oct_data_conv::dt_int: |
2800
|
2742 write_int (os, swap_bytes, static_cast<int> (d)); |
2317
|
2743 break; |
|
2744 |
|
2745 case oct_data_conv::dt_uint: |
2800
|
2746 write_int (os, swap_bytes, static_cast<unsigned int> (d)); |
2317
|
2747 break; |
|
2748 |
|
2749 case oct_data_conv::dt_long: |
2800
|
2750 write_int (os, swap_bytes, static_cast<long> (d)); |
2317
|
2751 break; |
|
2752 |
|
2753 case oct_data_conv::dt_ulong: |
2800
|
2754 write_int (os, swap_bytes, static_cast<unsigned long> (d)); |
2317
|
2755 break; |
|
2756 |
|
2757 case oct_data_conv::dt_float: |
|
2758 { |
2800
|
2759 float f = d; |
2317
|
2760 |
|
2761 if (do_float_conversion) |
|
2762 do_float_format_conversion (&f, 1, flt_fmt); |
|
2763 |
2800
|
2764 os.write (static_cast<char *> (&f), sizeof (float)); |
2317
|
2765 } |
|
2766 break; |
|
2767 |
|
2768 case oct_data_conv::dt_double: |
|
2769 { |
|
2770 if (do_float_conversion) |
|
2771 do_double_format_conversion (&d, 1, flt_fmt); |
|
2772 |
2800
|
2773 os.write (static_cast<char *> (&d), sizeof (double)); |
2317
|
2774 } |
|
2775 break; |
|
2776 |
|
2777 default: |
|
2778 retval = false; |
|
2779 (*current_liboctave_error_handler) |
|
2780 ("write: invalid type specification"); |
|
2781 break; |
|
2782 } |
|
2783 |
|
2784 return retval; |
|
2785 } |
1360
|
2786 |
458
|
2787 int |
2317
|
2788 Matrix::write (ostream& os, oct_data_conv::data_type dt, int skip, |
|
2789 oct_mach_info::float_format flt_fmt) |
458
|
2790 { |
2317
|
2791 int retval = -1; |
|
2792 |
|
2793 bool ok = true; |
|
2794 |
|
2795 int count = 0; |
|
2796 |
|
2797 const double *d = data (); |
|
2798 |
|
2799 int n = length (); |
|
2800 |
|
2801 oct_mach_info::float_format native_flt_fmt |
|
2802 = oct_mach_info::float_format (); |
|
2803 |
|
2804 bool do_float_conversion = (flt_fmt != native_flt_fmt); |
|
2805 |
|
2806 // XXX FIXME XXX -- byte order for Cray? |
|
2807 |
|
2808 bool swap_bytes = false; |
|
2809 |
|
2810 if (oct_mach_info::words_big_endian ()) |
|
2811 swap_bytes = (flt_fmt == oct_mach_info::ieee_little_endian |
|
2812 || flt_fmt == oct_mach_info::vax_g |
|
2813 || flt_fmt == oct_mach_info::vax_g); |
|
2814 else |
|
2815 swap_bytes = (flt_fmt == oct_mach_info::ieee_big_endian); |
|
2816 |
|
2817 for (int i = 0; i < n; i++) |
458
|
2818 { |
2317
|
2819 if (os) |
|
2820 { |
|
2821 if (skip != 0) |
|
2822 os.seekp (skip, ios::cur); |
|
2823 |
|
2824 if (os) |
|
2825 { |
|
2826 ok = do_write (os, d[i], dt, flt_fmt, swap_bytes, |
|
2827 do_float_conversion); |
|
2828 |
|
2829 if (os && ok) |
|
2830 count++; |
|
2831 else |
|
2832 break; |
|
2833 } |
|
2834 else |
|
2835 { |
|
2836 ok = false; |
|
2837 break; |
|
2838 } |
|
2839 } |
|
2840 else |
|
2841 { |
|
2842 ok = false; |
|
2843 break; |
|
2844 } |
471
|
2845 } |
458
|
2846 |
2317
|
2847 if (ok) |
|
2848 retval = count; |
|
2849 |
|
2850 return retval; |
458
|
2851 } |
|
2852 |
2317
|
2853 |
|
2854 |
1819
|
2855 Matrix |
|
2856 Givens (double x, double y) |
|
2857 { |
|
2858 double cc, s, temp_r; |
|
2859 |
|
2860 F77_FCN (dlartg, DLARTG) (x, y, cc, s, temp_r); |
|
2861 |
|
2862 Matrix g (2, 2); |
|
2863 |
|
2864 g.elem (0, 0) = cc; |
|
2865 g.elem (1, 1) = cc; |
|
2866 g.elem (0, 1) = s; |
|
2867 g.elem (1, 0) = -s; |
|
2868 |
|
2869 return g; |
|
2870 } |
|
2871 |
|
2872 Matrix |
|
2873 Sylvester (const Matrix& a, const Matrix& b, const Matrix& c) |
|
2874 { |
|
2875 Matrix retval; |
|
2876 |
|
2877 // XXX FIXME XXX -- need to check that a, b, and c are all the same |
|
2878 // size. |
|
2879 |
|
2880 // Compute Schur decompositions. |
|
2881 |
|
2882 SCHUR as (a, "U"); |
|
2883 SCHUR bs (b, "U"); |
|
2884 |
|
2885 // Transform c to new coordinates. |
|
2886 |
|
2887 Matrix ua = as.unitary_matrix (); |
|
2888 Matrix sch_a = as.schur_matrix (); |
|
2889 |
|
2890 Matrix ub = bs.unitary_matrix (); |
|
2891 Matrix sch_b = bs.schur_matrix (); |
|
2892 |
|
2893 Matrix cx = ua.transpose () * c * ub; |
|
2894 |
|
2895 // Solve the sylvester equation, back-transform, and return the |
|
2896 // solution. |
|
2897 |
|
2898 int a_nr = a.rows (); |
|
2899 int b_nr = b.rows (); |
|
2900 |
|
2901 double scale; |
|
2902 int info; |
|
2903 |
1950
|
2904 double *pa = sch_a.fortran_vec (); |
|
2905 double *pb = sch_b.fortran_vec (); |
|
2906 double *px = cx.fortran_vec (); |
|
2907 |
|
2908 F77_XFCN (dtrsyl, DTRSYL, ("N", "N", 1, a_nr, b_nr, pa, a_nr, pb, |
|
2909 b_nr, px, a_nr, scale, info, 1L, 1L)); |
|
2910 |
|
2911 |
|
2912 if (f77_exception_encountered) |
|
2913 (*current_liboctave_error_handler) ("unrecoverable error in dtrsyl"); |
|
2914 else |
|
2915 { |
|
2916 // XXX FIXME XXX -- check info? |
1819
|
2917 |
1950
|
2918 retval = -ua*cx*ub.transpose (); |
|
2919 } |
1819
|
2920 |
|
2921 return retval; |
|
2922 } |
|
2923 |
1959
|
2924 ComplexColumnVector |
|
2925 Qzval (const Matrix& a, const Matrix& b) |
|
2926 { |
|
2927 ComplexColumnVector retval; |
|
2928 |
|
2929 int a_nr = a.rows(); |
|
2930 int a_nc = a.cols(); |
|
2931 |
|
2932 int b_nr = b.rows(); |
|
2933 int b_nc = b.cols(); |
|
2934 |
|
2935 if (a_nr == a_nc) |
|
2936 { |
|
2937 if (a_nr == b_nr && a_nc == b_nc) |
|
2938 { |
|
2939 if (a_nr != 0) |
|
2940 { |
|
2941 Matrix jnk (a_nr, a_nr, 0.0); |
|
2942 double *pjnk = jnk.fortran_vec (); |
|
2943 |
|
2944 ColumnVector alfr (a_nr); |
|
2945 double *palfr = alfr.fortran_vec (); |
|
2946 |
|
2947 ColumnVector alfi (a_nr); |
2561
|
2948 double *palfi = alfi.fortran_vec (); |
1959
|
2949 |
|
2950 ColumnVector beta (a_nr); |
2561
|
2951 double *pbeta = beta.fortran_vec (); |
1959
|
2952 |
|
2953 Matrix atmp = a; |
|
2954 double *pa = atmp.fortran_vec (); |
|
2955 |
|
2956 Matrix btmp = b; |
|
2957 double *pb = btmp.fortran_vec (); |
|
2958 |
|
2959 long matz = 0; |
|
2960 int info; |
|
2961 |
|
2962 // XXX FIXME ??? XXX |
|
2963 double eps = DBL_EPSILON; |
|
2964 |
|
2965 F77_FCN (qzhes, QZHES) (a_nr, a_nr, pa, pb, matz, pjnk); |
|
2966 |
|
2967 F77_FCN (qzit, QZIT) (a_nr, a_nr, pa, pb, eps, matz, pjnk, info); |
|
2968 |
|
2969 if (! info) |
|
2970 { |
|
2971 F77_FCN (qzval, QZVAL) (a_nr, a_nr, pa, pb, palfr, |
|
2972 palfi, pbeta, matz, pjnk); |
|
2973 |
|
2974 // Count and extract finite generalized eigenvalues. |
|
2975 |
|
2976 int cnt = 0; |
|
2977 |
|
2978 for (int i = 0; i < a_nr; i++) |
2561
|
2979 if (beta(i) != 0) |
1959
|
2980 cnt++; |
|
2981 |
2561
|
2982 ComplexColumnVector cx (cnt); |
|
2983 |
|
2984 cnt = 0; |
1959
|
2985 |
|
2986 for (int i = 0; i < a_nr; i++) |
|
2987 { |
2561
|
2988 if (beta(i) != 0) |
1959
|
2989 { |
|
2990 // Finite generalized eigenvalue. |
|
2991 |
2561
|
2992 cx(cnt++) = Complex (alfr(i), alfi(i)) / beta(i); |
1959
|
2993 } |
|
2994 } |
|
2995 |
|
2996 retval = cx; |
|
2997 } |
|
2998 else |
|
2999 (*current_liboctave_error_handler) |
|
3000 ("qzval: trouble in qzit, info = %d", info); |
|
3001 } |
|
3002 } |
|
3003 else |
2385
|
3004 gripe_nonconformant ("qzval", a_nr, a_nc, b_nr, b_nc); |
1959
|
3005 } |
|
3006 else |
|
3007 (*current_liboctave_error_handler) ("qzval: square matrices required"); |
|
3008 |
|
3009 return retval; |
|
3010 } |
|
3011 |
2828
|
3012 // matrix by matrix -> matrix operations |
|
3013 |
|
3014 Matrix |
|
3015 operator * (const Matrix& m, const Matrix& a) |
|
3016 { |
|
3017 Matrix retval; |
|
3018 |
|
3019 int nr = m.rows (); |
|
3020 int nc = m.cols (); |
|
3021 |
|
3022 int a_nr = a.rows (); |
|
3023 int a_nc = a.cols (); |
|
3024 |
|
3025 if (nc != a_nr) |
|
3026 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
|
3027 else |
|
3028 { |
|
3029 if (nr == 0 || nc == 0 || a_nc == 0) |
|
3030 retval.resize (nr, a_nc, 0.0); |
|
3031 else |
|
3032 { |
|
3033 int ld = nr; |
|
3034 int lda = a_nr; |
|
3035 |
|
3036 retval.resize (nr, a_nc); |
|
3037 double *c = retval.fortran_vec (); |
|
3038 |
|
3039 F77_XFCN (dgemm, DGEMM, ("N", "N", nr, a_nc, nc, 1.0, |
|
3040 m.data (), ld, a.data (), lda, 0.0, |
|
3041 c, nr, 1L, 1L)); |
|
3042 |
|
3043 if (f77_exception_encountered) |
|
3044 (*current_liboctave_error_handler) |
|
3045 ("unrecoverable error in dgemm"); |
|
3046 } |
|
3047 } |
|
3048 |
|
3049 return retval; |
|
3050 } |
|
3051 |
458
|
3052 /* |
|
3053 ;;; Local Variables: *** |
|
3054 ;;; mode: C++ *** |
|
3055 ;;; End: *** |
|
3056 */ |