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