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