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