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