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