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