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 |
|
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 { |
3573
|
1465 npp = m * npp + padec[j] * m; |
|
1466 dpp = m * dpp + (minus_one_j * padec[j]) * m; |
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 DiagMatrix& a) |
|
1540 { |
2385
|
1541 int nr = rows (); |
|
1542 int nc = cols (); |
|
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 for (int i = 0; i < a.length (); i++) |
|
1554 elem (i, i) += a.elem (i, i); |
|
1555 |
|
1556 return *this; |
|
1557 } |
|
1558 |
|
1559 Matrix& |
|
1560 Matrix::operator -= (const DiagMatrix& a) |
|
1561 { |
2385
|
1562 int nr = rows (); |
|
1563 int nc = cols (); |
|
1564 |
|
1565 int a_nr = a.rows (); |
|
1566 int a_nc = a.cols (); |
|
1567 |
|
1568 if (nr != a_nr || nc != a_nc) |
458
|
1569 { |
2385
|
1570 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
1571 return *this; |
|
1572 } |
|
1573 |
|
1574 for (int i = 0; i < a.length (); i++) |
|
1575 elem (i, i) -= a.elem (i, i); |
|
1576 |
|
1577 return *this; |
|
1578 } |
|
1579 |
|
1580 // unary operations |
|
1581 |
2964
|
1582 boolMatrix |
458
|
1583 Matrix::operator ! (void) const |
|
1584 { |
|
1585 int nr = rows (); |
|
1586 int nc = cols (); |
|
1587 |
2964
|
1588 boolMatrix b (nr, nc); |
458
|
1589 |
|
1590 for (int j = 0; j < nc; j++) |
|
1591 for (int i = 0; i < nr; i++) |
|
1592 b.elem (i, j) = ! elem (i, j); |
|
1593 |
|
1594 return b; |
|
1595 } |
|
1596 |
1205
|
1597 // column vector by row vector -> matrix operations |
458
|
1598 |
1205
|
1599 Matrix |
|
1600 operator * (const ColumnVector& v, const RowVector& a) |
458
|
1601 { |
1948
|
1602 Matrix retval; |
|
1603 |
1205
|
1604 int len = v.length (); |
3233
|
1605 |
|
1606 if (len != 0) |
1205
|
1607 { |
3233
|
1608 int a_len = a.length (); |
|
1609 |
|
1610 retval.resize (len, a_len); |
|
1611 double *c = retval.fortran_vec (); |
|
1612 |
|
1613 F77_XFCN (dgemm, DGEMM, ("N", "N", len, a_len, 1, 1.0, |
|
1614 v.data (), len, a.data (), 1, 0.0, |
|
1615 c, len, 1L, 1L)); |
|
1616 |
|
1617 if (f77_exception_encountered) |
|
1618 (*current_liboctave_error_handler) |
|
1619 ("unrecoverable error in dgemm"); |
1205
|
1620 } |
458
|
1621 |
1948
|
1622 return retval; |
458
|
1623 } |
|
1624 |
|
1625 // other operations. |
|
1626 |
|
1627 Matrix |
2676
|
1628 Matrix::map (d_d_Mapper f) const |
1205
|
1629 { |
2676
|
1630 Matrix b (*this); |
|
1631 return b.apply (f); |
1205
|
1632 } |
|
1633 |
3248
|
1634 boolMatrix |
|
1635 Matrix::map (b_d_Mapper f) const |
|
1636 { |
|
1637 int nr = rows (); |
|
1638 int nc = cols (); |
|
1639 |
|
1640 boolMatrix retval (nr, nc); |
|
1641 |
|
1642 for (int j = 0; j < nc; j++) |
|
1643 for (int i = 0; i < nr; i++) |
|
1644 retval(i,j) = f (elem(i,j)); |
|
1645 |
|
1646 return retval; |
|
1647 } |
|
1648 |
2676
|
1649 Matrix& |
|
1650 Matrix::apply (d_d_Mapper f) |
458
|
1651 { |
|
1652 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1653 |
|
1654 for (int i = 0; i < length (); i++) |
|
1655 d[i] = f (d[i]); |
2676
|
1656 |
|
1657 return *this; |
458
|
1658 } |
|
1659 |
2385
|
1660 bool |
|
1661 Matrix::any_element_is_negative (void) const |
|
1662 { |
|
1663 int nr = rows (); |
|
1664 int nc = cols (); |
|
1665 |
|
1666 for (int j = 0; j < nc; j++) |
|
1667 for (int i = 0; i < nr; i++) |
|
1668 if (elem (i, j) < 0.0) |
|
1669 return true; |
|
1670 |
|
1671 return false; |
|
1672 } |
|
1673 |
|
1674 |
|
1675 bool |
|
1676 Matrix::any_element_is_inf_or_nan (void) const |
|
1677 { |
|
1678 int nr = rows (); |
|
1679 int nc = cols (); |
|
1680 |
|
1681 for (int j = 0; j < nc; j++) |
|
1682 for (int i = 0; i < nr; i++) |
|
1683 { |
|
1684 double val = elem (i, j); |
|
1685 if (xisinf (val) || xisnan (val)) |
|
1686 return 1; |
|
1687 } |
|
1688 return 0; |
|
1689 } |
|
1690 |
|
1691 bool |
|
1692 Matrix::all_elements_are_int_or_inf_or_nan (void) const |
|
1693 { |
|
1694 int nr = rows (); |
|
1695 int nc = cols (); |
|
1696 |
|
1697 for (int j = 0; j < nc; j++) |
|
1698 for (int i = 0; i < nr; i++) |
|
1699 { |
|
1700 double val = elem (i, j); |
|
1701 if (xisnan (val) || D_NINT (val) == val) |
|
1702 continue; |
|
1703 else |
|
1704 return false; |
|
1705 } |
|
1706 |
|
1707 return true; |
|
1708 } |
|
1709 |
1968
|
1710 // Return nonzero if any element of M is not an integer. Also extract |
|
1711 // the largest and smallest values and return them in MAX_VAL and MIN_VAL. |
|
1712 |
2385
|
1713 bool |
1968
|
1714 Matrix::all_integers (double& max_val, double& min_val) const |
|
1715 { |
|
1716 int nr = rows (); |
|
1717 int nc = cols (); |
|
1718 |
|
1719 if (nr > 0 && nc > 0) |
|
1720 { |
|
1721 max_val = elem (0, 0); |
|
1722 min_val = elem (0, 0); |
|
1723 } |
|
1724 else |
2385
|
1725 return false; |
1968
|
1726 |
|
1727 for (int j = 0; j < nc; j++) |
|
1728 for (int i = 0; i < nr; i++) |
|
1729 { |
|
1730 double val = elem (i, j); |
|
1731 |
|
1732 if (val > max_val) |
|
1733 max_val = val; |
|
1734 |
|
1735 if (val < min_val) |
|
1736 min_val = val; |
|
1737 |
|
1738 if (D_NINT (val) != val) |
2385
|
1739 return false; |
1968
|
1740 } |
2385
|
1741 |
|
1742 return true; |
1968
|
1743 } |
|
1744 |
2385
|
1745 bool |
1968
|
1746 Matrix::too_large_for_float (void) const |
|
1747 { |
|
1748 int nr = rows (); |
2385
|
1749 int nc = cols (); |
1968
|
1750 |
|
1751 for (int j = 0; j < nc; j++) |
|
1752 for (int i = 0; i < nr; i++) |
|
1753 { |
|
1754 double val = elem (i, j); |
|
1755 |
|
1756 if (val > FLT_MAX || val < FLT_MIN) |
2385
|
1757 return true; |
1968
|
1758 } |
|
1759 |
2385
|
1760 return false; |
1968
|
1761 } |
|
1762 |
458
|
1763 // XXX FIXME XXX Do these really belong here? They should maybe be |
|
1764 // cleaned up a bit, no? What about corresponding functions for the |
|
1765 // Vectors? |
|
1766 |
2832
|
1767 boolMatrix |
458
|
1768 Matrix::all (void) const |
|
1769 { |
|
1770 int nr = rows (); |
|
1771 int nc = cols (); |
2832
|
1772 boolMatrix retval; |
458
|
1773 if (nr > 0 && nc > 0) |
|
1774 { |
|
1775 if (nr == 1) |
|
1776 { |
|
1777 retval.resize (1, 1); |
2832
|
1778 retval.elem (0, 0) = true; |
458
|
1779 for (int j = 0; j < nc; j++) |
|
1780 { |
|
1781 if (elem (0, j) == 0.0) |
|
1782 { |
2832
|
1783 retval.elem (0, 0) = false; |
458
|
1784 break; |
|
1785 } |
|
1786 } |
|
1787 } |
|
1788 else if (nc == 1) |
|
1789 { |
|
1790 retval.resize (1, 1); |
2832
|
1791 retval.elem (0, 0) = true; |
458
|
1792 for (int i = 0; i < nr; i++) |
|
1793 { |
|
1794 if (elem (i, 0) == 0.0) |
|
1795 { |
2832
|
1796 retval.elem (0, 0) = false; |
458
|
1797 break; |
|
1798 } |
|
1799 } |
|
1800 } |
|
1801 else |
|
1802 { |
|
1803 retval.resize (1, nc); |
|
1804 for (int j = 0; j < nc; j++) |
|
1805 { |
2832
|
1806 retval.elem (0, j) = true; |
458
|
1807 for (int i = 0; i < nr; i++) |
|
1808 { |
|
1809 if (elem (i, j) == 0.0) |
|
1810 { |
2832
|
1811 retval.elem (0, j) = false; |
458
|
1812 break; |
|
1813 } |
|
1814 } |
|
1815 } |
|
1816 } |
|
1817 } |
|
1818 return retval; |
|
1819 } |
|
1820 |
2832
|
1821 boolMatrix |
458
|
1822 Matrix::any (void) const |
|
1823 { |
|
1824 int nr = rows (); |
|
1825 int nc = cols (); |
2832
|
1826 boolMatrix retval; |
458
|
1827 if (nr > 0 && nc > 0) |
|
1828 { |
|
1829 if (nr == 1) |
|
1830 { |
|
1831 retval.resize (1, 1); |
2832
|
1832 retval.elem (0, 0) = false; |
458
|
1833 for (int j = 0; j < nc; j++) |
|
1834 { |
|
1835 if (elem (0, j) != 0.0) |
|
1836 { |
2832
|
1837 retval.elem (0, 0) = true; |
458
|
1838 break; |
|
1839 } |
|
1840 } |
|
1841 } |
|
1842 else if (nc == 1) |
|
1843 { |
|
1844 retval.resize (1, 1); |
2832
|
1845 retval.elem (0, 0) = false; |
458
|
1846 for (int i = 0; i < nr; i++) |
|
1847 { |
|
1848 if (elem (i, 0) != 0.0) |
|
1849 { |
2832
|
1850 retval.elem (0, 0) = true; |
458
|
1851 break; |
|
1852 } |
|
1853 } |
|
1854 } |
|
1855 else |
|
1856 { |
|
1857 retval.resize (1, nc); |
|
1858 for (int j = 0; j < nc; j++) |
|
1859 { |
2832
|
1860 retval.elem (0, j) = false; |
458
|
1861 for (int i = 0; i < nr; i++) |
|
1862 { |
|
1863 if (elem (i, j) != 0.0) |
|
1864 { |
2832
|
1865 retval.elem (0, j) = true; |
458
|
1866 break; |
|
1867 } |
|
1868 } |
|
1869 } |
|
1870 } |
|
1871 } |
|
1872 return retval; |
|
1873 } |
|
1874 |
|
1875 Matrix |
|
1876 Matrix::cumprod (void) const |
|
1877 { |
|
1878 Matrix retval; |
|
1879 |
|
1880 int nr = rows (); |
|
1881 int nc = cols (); |
|
1882 |
|
1883 if (nr == 1) |
|
1884 { |
|
1885 retval.resize (1, nc); |
|
1886 if (nc > 0) |
|
1887 { |
|
1888 double prod = elem (0, 0); |
|
1889 for (int j = 0; j < nc; j++) |
|
1890 { |
|
1891 retval.elem (0, j) = prod; |
|
1892 if (j < nc - 1) |
|
1893 prod *= elem (0, j+1); |
|
1894 } |
|
1895 } |
|
1896 } |
|
1897 else if (nc == 1) |
|
1898 { |
|
1899 retval.resize (nr, 1); |
|
1900 if (nr > 0) |
|
1901 { |
|
1902 double prod = elem (0, 0); |
|
1903 for (int i = 0; i < nr; i++) |
|
1904 { |
|
1905 retval.elem (i, 0) = prod; |
|
1906 if (i < nr - 1) |
|
1907 prod *= elem (i+1, 0); |
|
1908 } |
|
1909 } |
|
1910 } |
|
1911 else |
|
1912 { |
|
1913 retval.resize (nr, nc); |
|
1914 if (nr > 0 && nc > 0) |
|
1915 { |
|
1916 for (int j = 0; j < nc; j++) |
|
1917 { |
|
1918 double prod = elem (0, j); |
|
1919 for (int i = 0; i < nr; i++) |
|
1920 { |
|
1921 retval.elem (i, j) = prod; |
|
1922 if (i < nr - 1) |
|
1923 prod *= elem (i+1, j); |
|
1924 } |
|
1925 } |
|
1926 } |
|
1927 } |
|
1928 return retval; |
|
1929 } |
|
1930 |
|
1931 Matrix |
|
1932 Matrix::cumsum (void) const |
|
1933 { |
|
1934 Matrix retval; |
|
1935 |
|
1936 int nr = rows (); |
|
1937 int nc = cols (); |
|
1938 |
|
1939 if (nr == 1) |
|
1940 { |
|
1941 retval.resize (1, nc); |
|
1942 if (nc > 0) |
|
1943 { |
|
1944 double sum = elem (0, 0); |
|
1945 for (int j = 0; j < nc; j++) |
|
1946 { |
|
1947 retval.elem (0, j) = sum; |
|
1948 if (j < nc - 1) |
|
1949 sum += elem (0, j+1); |
|
1950 } |
|
1951 } |
|
1952 } |
|
1953 else if (nc == 1) |
|
1954 { |
|
1955 retval.resize (nr, 1); |
|
1956 if (nr > 0) |
|
1957 { |
|
1958 double sum = elem (0, 0); |
|
1959 for (int i = 0; i < nr; i++) |
|
1960 { |
|
1961 retval.elem (i, 0) = sum; |
|
1962 if (i < nr - 1) |
|
1963 sum += elem (i+1, 0); |
|
1964 } |
|
1965 } |
|
1966 } |
|
1967 else |
|
1968 { |
|
1969 retval.resize (nr, nc); |
|
1970 if (nr > 0 && nc > 0) |
|
1971 { |
|
1972 for (int j = 0; j < nc; j++) |
|
1973 { |
|
1974 double sum = elem (0, j); |
|
1975 for (int i = 0; i < nr; i++) |
|
1976 { |
|
1977 retval.elem (i, j) = sum; |
|
1978 if (i < nr - 1) |
|
1979 sum += elem (i+1, j); |
|
1980 } |
|
1981 } |
|
1982 } |
|
1983 } |
|
1984 return retval; |
|
1985 } |
|
1986 |
|
1987 Matrix |
|
1988 Matrix::prod (void) const |
|
1989 { |
|
1990 Matrix retval; |
|
1991 |
|
1992 int nr = rows (); |
|
1993 int nc = cols (); |
|
1994 |
|
1995 if (nr == 1) |
|
1996 { |
|
1997 retval.resize (1, 1); |
|
1998 retval.elem (0, 0) = 1.0; |
|
1999 for (int j = 0; j < nc; j++) |
|
2000 retval.elem (0, 0) *= elem (0, j); |
|
2001 } |
|
2002 else if (nc == 1) |
|
2003 { |
|
2004 retval.resize (1, 1); |
|
2005 retval.elem (0, 0) = 1.0; |
|
2006 for (int i = 0; i < nr; i++) |
|
2007 retval.elem (0, 0) *= elem (i, 0); |
|
2008 } |
|
2009 else |
|
2010 { |
|
2011 if (nc == 0) |
|
2012 { |
|
2013 retval.resize (1, 1); |
|
2014 retval.elem (0, 0) = 1.0; |
|
2015 } |
|
2016 else |
|
2017 retval.resize (1, nc); |
|
2018 |
|
2019 for (int j = 0; j < nc; j++) |
|
2020 { |
|
2021 retval.elem (0, j) = 1.0; |
|
2022 for (int i = 0; i < nr; i++) |
|
2023 retval.elem (0, j) *= elem (i, j); |
|
2024 } |
|
2025 } |
|
2026 return retval; |
|
2027 } |
|
2028 |
|
2029 Matrix |
|
2030 Matrix::sum (void) const |
|
2031 { |
|
2032 Matrix retval; |
|
2033 |
|
2034 int nr = rows (); |
|
2035 int nc = cols (); |
|
2036 |
|
2037 if (nr == 1) |
|
2038 { |
|
2039 retval.resize (1, 1); |
|
2040 retval.elem (0, 0) = 0.0; |
|
2041 for (int j = 0; j < nc; j++) |
|
2042 retval.elem (0, 0) += elem (0, j); |
|
2043 } |
|
2044 else if (nc == 1) |
|
2045 { |
|
2046 retval.resize (1, 1); |
|
2047 retval.elem (0, 0) = 0.0; |
|
2048 for (int i = 0; i < nr; i++) |
|
2049 retval.elem (0, 0) += elem (i, 0); |
|
2050 } |
|
2051 else |
|
2052 { |
|
2053 if (nc == 0) |
|
2054 { |
|
2055 retval.resize (1, 1); |
|
2056 retval.elem (0, 0) = 0.0; |
|
2057 } |
|
2058 else |
|
2059 retval.resize (1, nc); |
|
2060 |
|
2061 for (int j = 0; j < nc; j++) |
|
2062 { |
|
2063 retval.elem (0, j) = 0.0; |
|
2064 for (int i = 0; i < nr; i++) |
|
2065 retval.elem (0, j) += elem (i, j); |
|
2066 } |
|
2067 } |
|
2068 return retval; |
|
2069 } |
|
2070 |
|
2071 Matrix |
|
2072 Matrix::sumsq (void) const |
|
2073 { |
|
2074 Matrix retval; |
|
2075 |
|
2076 int nr = rows (); |
|
2077 int nc = cols (); |
|
2078 |
|
2079 if (nr == 1) |
|
2080 { |
|
2081 retval.resize (1, 1); |
|
2082 retval.elem (0, 0) = 0.0; |
|
2083 for (int j = 0; j < nc; j++) |
|
2084 { |
|
2085 double d = elem (0, j); |
|
2086 retval.elem (0, 0) += d * d; |
|
2087 } |
|
2088 } |
|
2089 else if (nc == 1) |
|
2090 { |
|
2091 retval.resize (1, 1); |
|
2092 retval.elem (0, 0) = 0.0; |
|
2093 for (int i = 0; i < nr; i++) |
|
2094 { |
|
2095 double d = elem (i, 0); |
|
2096 retval.elem (0, 0) += d * d; |
|
2097 } |
|
2098 } |
|
2099 else |
|
2100 { |
|
2101 retval.resize (1, nc); |
|
2102 for (int j = 0; j < nc; j++) |
|
2103 { |
|
2104 retval.elem (0, j) = 0.0; |
|
2105 for (int i = 0; i < nr; i++) |
|
2106 { |
|
2107 double d = elem (i, j); |
|
2108 retval.elem (0, j) += d * d; |
|
2109 } |
|
2110 } |
|
2111 } |
|
2112 return retval; |
|
2113 } |
|
2114 |
2385
|
2115 Matrix |
|
2116 Matrix::abs (void) const |
|
2117 { |
|
2118 int nr = rows (); |
|
2119 int nc = cols (); |
|
2120 |
|
2121 Matrix retval (nr, nc); |
|
2122 |
|
2123 for (int j = 0; j < nc; j++) |
|
2124 for (int i = 0; i < nr; i++) |
|
2125 retval (i, j) = fabs (elem (i, j)); |
|
2126 |
|
2127 return retval; |
|
2128 } |
|
2129 |
458
|
2130 ColumnVector |
|
2131 Matrix::diag (void) const |
|
2132 { |
|
2133 return diag (0); |
|
2134 } |
|
2135 |
|
2136 ColumnVector |
|
2137 Matrix::diag (int k) const |
|
2138 { |
|
2139 int nnr = rows (); |
|
2140 int nnc = cols (); |
|
2141 if (k > 0) |
|
2142 nnc -= k; |
|
2143 else if (k < 0) |
|
2144 nnr += k; |
|
2145 |
|
2146 ColumnVector d; |
|
2147 |
|
2148 if (nnr > 0 && nnc > 0) |
|
2149 { |
|
2150 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
2151 |
|
2152 d.resize (ndiag); |
|
2153 |
|
2154 if (k > 0) |
|
2155 { |
|
2156 for (int i = 0; i < ndiag; i++) |
|
2157 d.elem (i) = elem (i, i+k); |
|
2158 } |
|
2159 else if ( k < 0) |
|
2160 { |
|
2161 for (int i = 0; i < ndiag; i++) |
|
2162 d.elem (i) = elem (i-k, i); |
|
2163 } |
|
2164 else |
|
2165 { |
|
2166 for (int i = 0; i < ndiag; i++) |
|
2167 d.elem (i) = elem (i, i); |
|
2168 } |
|
2169 } |
|
2170 else |
3504
|
2171 std::cerr << "diag: requested diagonal out of range\n"; |
458
|
2172 |
|
2173 return d; |
|
2174 } |
|
2175 |
|
2176 ColumnVector |
|
2177 Matrix::row_min (void) const |
|
2178 { |
2354
|
2179 Array<int> index; |
|
2180 return row_min (index); |
458
|
2181 } |
|
2182 |
|
2183 ColumnVector |
2354
|
2184 Matrix::row_min (Array<int>& index) const |
458
|
2185 { |
|
2186 ColumnVector result; |
|
2187 |
|
2188 int nr = rows (); |
|
2189 int nc = cols (); |
|
2190 |
|
2191 if (nr > 0 && nc > 0) |
|
2192 { |
|
2193 result.resize (nr); |
2354
|
2194 index.resize (nr); |
458
|
2195 |
|
2196 for (int i = 0; i < nr; i++) |
|
2197 { |
3504
|
2198 int idx_j = 0; |
|
2199 |
|
2200 double tmp_min = elem (i, idx_j); |
2354
|
2201 |
|
2202 if (xisnan (tmp_min)) |
3504
|
2203 idx_j = -1; |
2354
|
2204 else |
|
2205 { |
|
2206 for (int j = 1; j < nc; j++) |
|
2207 { |
|
2208 double tmp = elem (i, j); |
|
2209 |
|
2210 if (xisnan (tmp)) |
|
2211 { |
3504
|
2212 idx_j = -1; |
2354
|
2213 break; |
|
2214 } |
|
2215 else if (tmp < tmp_min) |
|
2216 { |
3504
|
2217 idx_j = j; |
2354
|
2218 tmp_min = tmp; |
|
2219 } |
|
2220 } |
|
2221 } |
|
2222 |
3504
|
2223 result.elem (i) = (idx_j < 0) ? octave_NaN : tmp_min; |
|
2224 index.elem (i) = idx_j; |
458
|
2225 } |
|
2226 } |
|
2227 |
|
2228 return result; |
|
2229 } |
|
2230 |
|
2231 ColumnVector |
|
2232 Matrix::row_max (void) const |
|
2233 { |
2354
|
2234 Array<int> index; |
|
2235 return row_max (index); |
458
|
2236 } |
|
2237 |
|
2238 ColumnVector |
2354
|
2239 Matrix::row_max (Array<int>& index) const |
458
|
2240 { |
|
2241 ColumnVector result; |
|
2242 |
|
2243 int nr = rows (); |
|
2244 int nc = cols (); |
|
2245 |
|
2246 if (nr > 0 && nc > 0) |
|
2247 { |
|
2248 result.resize (nr); |
2354
|
2249 index.resize (nr); |
458
|
2250 |
|
2251 for (int i = 0; i < nr; i++) |
|
2252 { |
3504
|
2253 int idx_j = 0; |
|
2254 |
|
2255 double tmp_max = elem (i, idx_j); |
2354
|
2256 |
|
2257 if (xisnan (tmp_max)) |
3504
|
2258 idx_j = -1; |
2354
|
2259 else |
|
2260 { |
|
2261 for (int j = 1; j < nc; j++) |
|
2262 { |
|
2263 double tmp = elem (i, j); |
|
2264 |
|
2265 if (xisnan (tmp)) |
|
2266 { |
3504
|
2267 idx_j = -1; |
2354
|
2268 break; |
|
2269 } |
|
2270 else if (tmp > tmp_max) |
|
2271 { |
3504
|
2272 idx_j = j; |
2354
|
2273 tmp_max = tmp; |
|
2274 } |
|
2275 } |
|
2276 } |
|
2277 |
3504
|
2278 result.elem (i) = (idx_j < 0) ? octave_NaN : tmp_max; |
|
2279 index.elem (i) = idx_j; |
458
|
2280 } |
|
2281 } |
|
2282 |
|
2283 return result; |
|
2284 } |
|
2285 |
|
2286 RowVector |
|
2287 Matrix::column_min (void) const |
|
2288 { |
2354
|
2289 Array<int> index; |
|
2290 return column_min (index); |
458
|
2291 } |
2354
|
2292 |
458
|
2293 RowVector |
2354
|
2294 Matrix::column_min (Array<int>& index) const |
458
|
2295 { |
|
2296 RowVector result; |
|
2297 |
|
2298 int nr = rows (); |
|
2299 int nc = cols (); |
|
2300 |
|
2301 if (nr > 0 && nc > 0) |
|
2302 { |
|
2303 result.resize (nc); |
2354
|
2304 index.resize (nc); |
458
|
2305 |
|
2306 for (int j = 0; j < nc; j++) |
|
2307 { |
3504
|
2308 int idx_i = 0; |
|
2309 |
|
2310 double tmp_min = elem (idx_i, j); |
2354
|
2311 |
|
2312 if (xisnan (tmp_min)) |
3504
|
2313 idx_i = -1; |
2354
|
2314 else |
|
2315 { |
|
2316 for (int i = 1; i < nr; i++) |
|
2317 { |
|
2318 double tmp = elem (i, j); |
|
2319 |
|
2320 if (xisnan (tmp)) |
|
2321 { |
3504
|
2322 idx_i = -1; |
2354
|
2323 break; |
|
2324 } |
|
2325 else if (tmp < tmp_min) |
|
2326 { |
3504
|
2327 idx_i = i; |
2354
|
2328 tmp_min = tmp; |
|
2329 } |
|
2330 } |
|
2331 } |
|
2332 |
3504
|
2333 result.elem (j) = (idx_i < 0) ? octave_NaN : tmp_min; |
|
2334 index.elem (j) = idx_i; |
458
|
2335 } |
|
2336 } |
|
2337 |
|
2338 return result; |
|
2339 } |
|
2340 |
2354
|
2341 RowVector |
|
2342 Matrix::column_max (void) const |
|
2343 { |
|
2344 Array<int> index; |
|
2345 return column_max (index); |
|
2346 } |
458
|
2347 |
|
2348 RowVector |
2354
|
2349 Matrix::column_max (Array<int>& index) const |
458
|
2350 { |
|
2351 RowVector result; |
|
2352 |
|
2353 int nr = rows (); |
|
2354 int nc = cols (); |
|
2355 |
|
2356 if (nr > 0 && nc > 0) |
|
2357 { |
|
2358 result.resize (nc); |
2354
|
2359 index.resize (nc); |
458
|
2360 |
|
2361 for (int j = 0; j < nc; j++) |
|
2362 { |
3504
|
2363 int idx_i = 0; |
|
2364 |
|
2365 double tmp_max = elem (idx_i, j); |
2354
|
2366 |
|
2367 if (xisnan (tmp_max)) |
3504
|
2368 idx_i = -1; |
2354
|
2369 else |
|
2370 { |
|
2371 for (int i = 1; i < nr; i++) |
|
2372 { |
|
2373 double tmp = elem (i, j); |
|
2374 |
|
2375 if (xisnan (tmp)) |
|
2376 { |
3504
|
2377 idx_i = -1; |
2354
|
2378 break; |
|
2379 } |
|
2380 else if (tmp > tmp_max) |
|
2381 { |
3504
|
2382 idx_i = i; |
2354
|
2383 tmp_max = tmp; |
|
2384 } |
|
2385 } |
|
2386 } |
|
2387 |
3504
|
2388 result.elem (j) = (idx_i < 0) ? octave_NaN : tmp_max; |
|
2389 index.elem (j) = idx_i; |
458
|
2390 } |
|
2391 } |
|
2392 |
|
2393 return result; |
|
2394 } |
|
2395 |
3504
|
2396 std::ostream& |
|
2397 operator << (std::ostream& os, const Matrix& a) |
458
|
2398 { |
|
2399 // int field_width = os.precision () + 7; |
1360
|
2400 |
458
|
2401 for (int i = 0; i < a.rows (); i++) |
|
2402 { |
|
2403 for (int j = 0; j < a.cols (); j++) |
|
2404 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
2405 os << "\n"; |
|
2406 } |
|
2407 return os; |
|
2408 } |
|
2409 |
3504
|
2410 std::istream& |
|
2411 operator >> (std::istream& is, Matrix& a) |
458
|
2412 { |
|
2413 int nr = a.rows (); |
|
2414 int nc = a.cols (); |
|
2415 |
|
2416 if (nr < 1 || nc < 1) |
3504
|
2417 is.clear (std::ios::badbit); |
458
|
2418 else |
|
2419 { |
|
2420 double tmp; |
|
2421 for (int i = 0; i < nr; i++) |
|
2422 for (int j = 0; j < nc; j++) |
|
2423 { |
|
2424 is >> tmp; |
|
2425 if (is) |
|
2426 a.elem (i, j) = tmp; |
|
2427 else |
2795
|
2428 goto done; |
458
|
2429 } |
|
2430 } |
|
2431 |
2795
|
2432 done: |
|
2433 |
458
|
2434 return is; |
|
2435 } |
|
2436 |
2317
|
2437 template <class T> |
3504
|
2438 void |
|
2439 read_int (std::istream& is, bool swap_bytes, T& val) |
2317
|
2440 { |
3145
|
2441 is.read (X_CAST (char *, &val), sizeof (T)); |
2317
|
2442 |
|
2443 if (swap_bytes) |
|
2444 { |
|
2445 switch (sizeof (T)) |
|
2446 { |
|
2447 case 1: |
|
2448 break; |
|
2449 |
|
2450 case 2: |
3145
|
2451 swap_2_bytes (X_CAST (char *, &val)); |
2317
|
2452 break; |
|
2453 |
|
2454 case 4: |
3145
|
2455 swap_4_bytes (X_CAST (char *, &val)); |
2317
|
2456 break; |
|
2457 |
|
2458 case 8: |
3145
|
2459 swap_8_bytes (X_CAST (char *, &val)); |
2317
|
2460 break; |
|
2461 |
|
2462 default: |
|
2463 (*current_liboctave_error_handler) |
|
2464 ("read_int: unrecognized data format!"); |
|
2465 } |
|
2466 } |
|
2467 } |
|
2468 |
3504
|
2469 template void read_int (std::istream&, bool, char&); |
|
2470 template void read_int (std::istream&, bool, signed char&); |
|
2471 template void read_int (std::istream&, bool, unsigned char&); |
|
2472 template void read_int (std::istream&, bool, short&); |
|
2473 template void read_int (std::istream&, bool, unsigned short&); |
|
2474 template void read_int (std::istream&, bool, int&); |
|
2475 template void read_int (std::istream&, bool, unsigned int&); |
|
2476 template void read_int (std::istream&, bool, long&); |
|
2477 template void read_int (std::istream&, bool, unsigned long&); |
2317
|
2478 |
|
2479 static inline bool |
3504
|
2480 do_read (std::istream& is, oct_data_conv::data_type dt, |
2317
|
2481 oct_mach_info::float_format flt_fmt, bool swap_bytes, |
|
2482 bool do_float_conversion, double& val) |
|
2483 { |
|
2484 bool retval = true; |
|
2485 |
|
2486 switch (dt) |
|
2487 { |
|
2488 case oct_data_conv::dt_char: |
|
2489 { |
|
2490 char tmp; |
|
2491 read_int (is, swap_bytes, tmp); |
|
2492 val = tmp; |
|
2493 } |
|
2494 break; |
|
2495 |
|
2496 case oct_data_conv::dt_schar: |
|
2497 { |
|
2498 signed char tmp; |
|
2499 read_int (is, swap_bytes, tmp); |
|
2500 val = tmp; |
|
2501 } |
|
2502 break; |
|
2503 |
|
2504 case oct_data_conv::dt_uchar: |
|
2505 { |
|
2506 unsigned char tmp; |
|
2507 read_int (is, swap_bytes, tmp); |
|
2508 val = tmp; |
|
2509 } |
|
2510 break; |
|
2511 |
|
2512 case oct_data_conv::dt_short: |
|
2513 { |
|
2514 short tmp; |
|
2515 read_int (is, swap_bytes, tmp); |
|
2516 val = tmp; |
|
2517 } |
|
2518 break; |
|
2519 |
|
2520 case oct_data_conv::dt_ushort: |
|
2521 { |
|
2522 unsigned short tmp; |
|
2523 read_int (is, swap_bytes, tmp); |
|
2524 val = tmp; |
|
2525 } |
|
2526 break; |
|
2527 |
|
2528 case oct_data_conv::dt_int: |
|
2529 { |
|
2530 int tmp; |
|
2531 read_int (is, swap_bytes, tmp); |
|
2532 val = tmp; |
|
2533 } |
|
2534 break; |
|
2535 |
|
2536 case oct_data_conv::dt_uint: |
|
2537 { |
|
2538 unsigned int tmp; |
|
2539 read_int (is, swap_bytes, tmp); |
|
2540 val = tmp; |
|
2541 } |
|
2542 break; |
|
2543 |
|
2544 case oct_data_conv::dt_long: |
|
2545 { |
|
2546 long tmp; |
|
2547 read_int (is, swap_bytes, tmp); |
|
2548 val = tmp; |
|
2549 } |
|
2550 break; |
|
2551 |
|
2552 case oct_data_conv::dt_ulong: |
|
2553 { |
|
2554 unsigned long tmp; |
|
2555 read_int (is, swap_bytes, tmp); |
|
2556 val = tmp; |
|
2557 } |
|
2558 break; |
|
2559 |
|
2560 case oct_data_conv::dt_float: |
|
2561 { |
|
2562 float f; |
|
2563 |
3145
|
2564 is.read (X_CAST (char *, &f), sizeof (float)); |
2317
|
2565 |
|
2566 if (do_float_conversion) |
|
2567 do_float_format_conversion (&f, 1, flt_fmt); |
|
2568 |
|
2569 val = f; |
|
2570 } |
|
2571 break; |
|
2572 |
|
2573 case oct_data_conv::dt_double: |
|
2574 { |
3145
|
2575 is.read (X_CAST (char *, &val), sizeof (double)); |
2317
|
2576 |
|
2577 if (do_float_conversion) |
|
2578 do_double_format_conversion (&val, 1, flt_fmt); |
|
2579 } |
|
2580 break; |
|
2581 |
|
2582 default: |
|
2583 retval = false; |
|
2584 (*current_liboctave_error_handler) |
|
2585 ("read: invalid type specification"); |
|
2586 break; |
|
2587 } |
|
2588 |
|
2589 return retval; |
|
2590 } |
1360
|
2591 |
458
|
2592 int |
3504
|
2593 Matrix::read (std::istream& is, int nr, int nc, |
2317
|
2594 oct_data_conv::data_type dt, int skip, |
|
2595 oct_mach_info::float_format flt_fmt) |
458
|
2596 { |
2317
|
2597 int retval = -1; |
|
2598 |
|
2599 bool ok = true; |
|
2600 |
|
2601 int count = 0; |
|
2602 |
|
2603 double *data = 0; |
|
2604 int max_size = 0; |
|
2605 |
|
2606 int final_nr = 0; |
|
2607 int final_nc = 0; |
|
2608 |
|
2609 if (nr > 0) |
458
|
2610 { |
2317
|
2611 if (nc > 0) |
|
2612 { |
|
2613 resize (nr, nc, 0.0); |
|
2614 data = fortran_vec (); |
|
2615 max_size = nr * nc; |
|
2616 } |
|
2617 else |
|
2618 { |
|
2619 resize (nr, 32, 0.0); |
|
2620 data = fortran_vec (); |
|
2621 max_size = nr * 32; |
|
2622 } |
|
2623 } |
471
|
2624 else |
|
2625 { |
2317
|
2626 resize (32, 1, 0.0); |
|
2627 data = fortran_vec (); |
|
2628 max_size = 32; |
|
2629 } |
|
2630 |
|
2631 oct_mach_info::float_format native_flt_fmt |
|
2632 = oct_mach_info::float_format (); |
|
2633 |
|
2634 bool do_float_conversion = (flt_fmt != native_flt_fmt); |
|
2635 |
|
2636 // XXX FIXME XXX -- byte order for Cray? |
|
2637 |
|
2638 bool swap_bytes = false; |
|
2639 |
|
2640 if (oct_mach_info::words_big_endian ()) |
|
2641 swap_bytes = (flt_fmt == oct_mach_info::ieee_little_endian |
|
2642 || flt_fmt == oct_mach_info::vax_g |
|
2643 || flt_fmt == oct_mach_info::vax_g); |
|
2644 else |
|
2645 swap_bytes = (flt_fmt == oct_mach_info::ieee_big_endian); |
|
2646 |
|
2647 for (;;) |
|
2648 { |
|
2649 // XXX FIXME XXX -- maybe there should be a special case for |
|
2650 // skip == 0. |
|
2651 |
|
2652 if (is) |
|
2653 { |
|
2654 if (nr > 0 && nc > 0 && count == max_size) |
|
2655 { |
|
2656 final_nr = nr; |
|
2657 final_nc = nc; |
|
2658 |
|
2659 break; |
|
2660 } |
|
2661 |
|
2662 if (is) |
|
2663 { |
|
2664 double tmp = 0.0; |
|
2665 |
|
2666 ok = do_read (is, dt, flt_fmt, swap_bytes, |
|
2667 do_float_conversion, tmp); |
|
2668 |
|
2669 if (ok) |
|
2670 { |
|
2671 if (is) |
|
2672 { |
|
2673 if (count == max_size) |
|
2674 { |
|
2675 max_size *= 2; |
|
2676 |
|
2677 if (nr > 0) |
2601
|
2678 resize (nr, max_size / nr, 0.0); |
2317
|
2679 else |
|
2680 resize (max_size, 1, 0.0); |
|
2681 |
|
2682 data = fortran_vec (); |
|
2683 } |
|
2684 |
|
2685 data[count++] = tmp; |
|
2686 } |
3180
|
2687 |
|
2688 if (ok && skip != 0) |
3504
|
2689 is.seekg (skip, std::ios::cur); |
3180
|
2690 |
3219
|
2691 if (! ok || is.eof ()) |
2317
|
2692 { |
|
2693 if (is.eof ()) |
|
2694 { |
|
2695 if (nr > 0) |
|
2696 { |
|
2697 if (count > nr) |
|
2698 { |
|
2699 final_nr = nr; |
|
2700 final_nc = (count - 1) / nr + 1; |
|
2701 } |
|
2702 else |
|
2703 { |
|
2704 final_nr = count; |
|
2705 final_nc = 1; |
|
2706 } |
|
2707 } |
|
2708 else |
|
2709 { |
|
2710 final_nr = count; |
|
2711 final_nc = 1; |
|
2712 } |
|
2713 } |
|
2714 |
|
2715 break; |
|
2716 } |
|
2717 } |
|
2718 else |
|
2719 break; |
|
2720 } |
|
2721 else |
|
2722 { |
|
2723 ok = false; |
|
2724 break; |
|
2725 } |
|
2726 } |
|
2727 else |
|
2728 { |
|
2729 ok = false; |
|
2730 break; |
|
2731 } |
|
2732 } |
|
2733 |
|
2734 if (ok) |
|
2735 { |
|
2736 resize (final_nr, final_nc, 0.0); |
|
2737 |
|
2738 retval = count; |
458
|
2739 } |
|
2740 |
2317
|
2741 return retval; |
|
2742 } |
|
2743 |
|
2744 template <class T> |
3504
|
2745 void |
|
2746 write_int (std::ostream& os, bool swap_bytes, T val) |
2317
|
2747 { |
|
2748 if (swap_bytes) |
|
2749 { |
|
2750 switch (sizeof (T)) |
|
2751 { |
|
2752 case 1: |
|
2753 break; |
|
2754 |
|
2755 case 2: |
3145
|
2756 swap_2_bytes (X_CAST (char *, &val)); |
2317
|
2757 break; |
|
2758 |
|
2759 case 4: |
3145
|
2760 swap_4_bytes (X_CAST (char *, &val)); |
2317
|
2761 break; |
|
2762 |
|
2763 case 8: |
3145
|
2764 swap_8_bytes (X_CAST (char *, &val)); |
2317
|
2765 break; |
|
2766 |
|
2767 default: |
|
2768 (*current_liboctave_error_handler) |
|
2769 ("write_int: unrecognized data format!"); |
|
2770 } |
|
2771 } |
|
2772 |
3145
|
2773 os.write (X_CAST (char *, &val), sizeof (T)); |
458
|
2774 } |
|
2775 |
3504
|
2776 template void write_int (std::ostream&, bool, char); |
|
2777 template void write_int (std::ostream&, bool, signed char); |
|
2778 template void write_int (std::ostream&, bool, unsigned char); |
|
2779 template void write_int (std::ostream&, bool, short); |
|
2780 template void write_int (std::ostream&, bool, unsigned short); |
|
2781 template void write_int (std::ostream&, bool, int); |
|
2782 template void write_int (std::ostream&, bool, unsigned int); |
|
2783 template void write_int (std::ostream&, bool, long); |
|
2784 template void write_int (std::ostream&, bool, unsigned long); |
2317
|
2785 |
|
2786 static inline bool |
3504
|
2787 do_write (std::ostream& os, double d, oct_data_conv::data_type dt, |
2317
|
2788 oct_mach_info::float_format flt_fmt, bool swap_bytes, |
|
2789 bool do_float_conversion) |
|
2790 { |
|
2791 bool retval = true; |
|
2792 |
|
2793 switch (dt) |
|
2794 { |
|
2795 case oct_data_conv::dt_char: |
3145
|
2796 write_int (os, swap_bytes, X_CAST (char, d)); |
2317
|
2797 break; |
|
2798 |
|
2799 case oct_data_conv::dt_schar: |
3145
|
2800 write_int (os, swap_bytes, X_CAST (signed char, d)); |
2317
|
2801 break; |
|
2802 |
|
2803 case oct_data_conv::dt_uchar: |
3145
|
2804 write_int (os, swap_bytes, X_CAST (unsigned char, d)); |
2317
|
2805 break; |
|
2806 |
|
2807 case oct_data_conv::dt_short: |
3145
|
2808 write_int (os, swap_bytes, X_CAST (short, d)); |
2317
|
2809 break; |
|
2810 |
|
2811 case oct_data_conv::dt_ushort: |
3145
|
2812 write_int (os, swap_bytes, X_CAST (unsigned short, d)); |
2317
|
2813 break; |
|
2814 |
|
2815 case oct_data_conv::dt_int: |
3145
|
2816 write_int (os, swap_bytes, X_CAST (int, d)); |
2317
|
2817 break; |
|
2818 |
|
2819 case oct_data_conv::dt_uint: |
3145
|
2820 write_int (os, swap_bytes, X_CAST (unsigned int, d)); |
2317
|
2821 break; |
|
2822 |
|
2823 case oct_data_conv::dt_long: |
3145
|
2824 write_int (os, swap_bytes, X_CAST (long, d)); |
2317
|
2825 break; |
|
2826 |
|
2827 case oct_data_conv::dt_ulong: |
3145
|
2828 write_int (os, swap_bytes, X_CAST (unsigned long, d)); |
2317
|
2829 break; |
|
2830 |
|
2831 case oct_data_conv::dt_float: |
|
2832 { |
2800
|
2833 float f = d; |
2317
|
2834 |
|
2835 if (do_float_conversion) |
|
2836 do_float_format_conversion (&f, 1, flt_fmt); |
|
2837 |
3145
|
2838 os.write (X_CAST (char *, &f), sizeof (float)); |
2317
|
2839 } |
|
2840 break; |
|
2841 |
|
2842 case oct_data_conv::dt_double: |
|
2843 { |
|
2844 if (do_float_conversion) |
|
2845 do_double_format_conversion (&d, 1, flt_fmt); |
|
2846 |
3145
|
2847 os.write (X_CAST (char *, &d), sizeof (double)); |
2317
|
2848 } |
|
2849 break; |
|
2850 |
|
2851 default: |
|
2852 retval = false; |
|
2853 (*current_liboctave_error_handler) |
|
2854 ("write: invalid type specification"); |
|
2855 break; |
|
2856 } |
|
2857 |
|
2858 return retval; |
|
2859 } |
1360
|
2860 |
458
|
2861 int |
3504
|
2862 Matrix::write (std::ostream& os, oct_data_conv::data_type dt, int skip, |
2317
|
2863 oct_mach_info::float_format flt_fmt) |
458
|
2864 { |
2317
|
2865 int retval = -1; |
|
2866 |
|
2867 bool ok = true; |
|
2868 |
|
2869 int count = 0; |
|
2870 |
|
2871 const double *d = data (); |
|
2872 |
|
2873 int n = length (); |
|
2874 |
|
2875 oct_mach_info::float_format native_flt_fmt |
|
2876 = oct_mach_info::float_format (); |
|
2877 |
|
2878 bool do_float_conversion = (flt_fmt != native_flt_fmt); |
|
2879 |
|
2880 // XXX FIXME XXX -- byte order for Cray? |
|
2881 |
|
2882 bool swap_bytes = false; |
|
2883 |
|
2884 if (oct_mach_info::words_big_endian ()) |
|
2885 swap_bytes = (flt_fmt == oct_mach_info::ieee_little_endian |
|
2886 || flt_fmt == oct_mach_info::vax_g |
|
2887 || flt_fmt == oct_mach_info::vax_g); |
|
2888 else |
|
2889 swap_bytes = (flt_fmt == oct_mach_info::ieee_big_endian); |
|
2890 |
|
2891 for (int i = 0; i < n; i++) |
458
|
2892 { |
2317
|
2893 if (os) |
|
2894 { |
|
2895 if (skip != 0) |
3504
|
2896 os.seekp (skip, std::ios::cur); |
2317
|
2897 |
|
2898 if (os) |
|
2899 { |
|
2900 ok = do_write (os, d[i], dt, flt_fmt, swap_bytes, |
|
2901 do_float_conversion); |
|
2902 |
|
2903 if (os && ok) |
|
2904 count++; |
|
2905 else |
|
2906 break; |
|
2907 } |
|
2908 else |
|
2909 { |
|
2910 ok = false; |
|
2911 break; |
|
2912 } |
|
2913 } |
|
2914 else |
|
2915 { |
|
2916 ok = false; |
|
2917 break; |
|
2918 } |
471
|
2919 } |
458
|
2920 |
2317
|
2921 if (ok) |
|
2922 retval = count; |
|
2923 |
|
2924 return retval; |
458
|
2925 } |
|
2926 |
2317
|
2927 |
|
2928 |
1819
|
2929 Matrix |
|
2930 Givens (double x, double y) |
|
2931 { |
|
2932 double cc, s, temp_r; |
|
2933 |
|
2934 F77_FCN (dlartg, DLARTG) (x, y, cc, s, temp_r); |
|
2935 |
|
2936 Matrix g (2, 2); |
|
2937 |
|
2938 g.elem (0, 0) = cc; |
|
2939 g.elem (1, 1) = cc; |
|
2940 g.elem (0, 1) = s; |
|
2941 g.elem (1, 0) = -s; |
|
2942 |
|
2943 return g; |
|
2944 } |
|
2945 |
|
2946 Matrix |
|
2947 Sylvester (const Matrix& a, const Matrix& b, const Matrix& c) |
|
2948 { |
|
2949 Matrix retval; |
|
2950 |
|
2951 // XXX FIXME XXX -- need to check that a, b, and c are all the same |
|
2952 // size. |
|
2953 |
|
2954 // Compute Schur decompositions. |
|
2955 |
|
2956 SCHUR as (a, "U"); |
|
2957 SCHUR bs (b, "U"); |
|
2958 |
|
2959 // Transform c to new coordinates. |
|
2960 |
|
2961 Matrix ua = as.unitary_matrix (); |
|
2962 Matrix sch_a = as.schur_matrix (); |
|
2963 |
|
2964 Matrix ub = bs.unitary_matrix (); |
|
2965 Matrix sch_b = bs.schur_matrix (); |
|
2966 |
|
2967 Matrix cx = ua.transpose () * c * ub; |
|
2968 |
|
2969 // Solve the sylvester equation, back-transform, and return the |
|
2970 // solution. |
|
2971 |
|
2972 int a_nr = a.rows (); |
|
2973 int b_nr = b.rows (); |
|
2974 |
|
2975 double scale; |
|
2976 int info; |
|
2977 |
1950
|
2978 double *pa = sch_a.fortran_vec (); |
|
2979 double *pb = sch_b.fortran_vec (); |
|
2980 double *px = cx.fortran_vec (); |
|
2981 |
|
2982 F77_XFCN (dtrsyl, DTRSYL, ("N", "N", 1, a_nr, b_nr, pa, a_nr, pb, |
|
2983 b_nr, px, a_nr, scale, info, 1L, 1L)); |
|
2984 |
|
2985 |
|
2986 if (f77_exception_encountered) |
|
2987 (*current_liboctave_error_handler) ("unrecoverable error in dtrsyl"); |
|
2988 else |
|
2989 { |
|
2990 // XXX FIXME XXX -- check info? |
1819
|
2991 |
1950
|
2992 retval = -ua*cx*ub.transpose (); |
|
2993 } |
1819
|
2994 |
|
2995 return retval; |
|
2996 } |
|
2997 |
2828
|
2998 // matrix by matrix -> matrix operations |
|
2999 |
|
3000 Matrix |
|
3001 operator * (const Matrix& m, const Matrix& a) |
|
3002 { |
|
3003 Matrix retval; |
|
3004 |
|
3005 int nr = m.rows (); |
|
3006 int nc = m.cols (); |
|
3007 |
|
3008 int a_nr = a.rows (); |
|
3009 int a_nc = a.cols (); |
|
3010 |
|
3011 if (nc != a_nr) |
|
3012 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
|
3013 else |
|
3014 { |
|
3015 if (nr == 0 || nc == 0 || a_nc == 0) |
|
3016 retval.resize (nr, a_nc, 0.0); |
|
3017 else |
|
3018 { |
|
3019 int ld = nr; |
|
3020 int lda = a_nr; |
|
3021 |
|
3022 retval.resize (nr, a_nc); |
|
3023 double *c = retval.fortran_vec (); |
|
3024 |
|
3025 F77_XFCN (dgemm, DGEMM, ("N", "N", nr, a_nc, nc, 1.0, |
|
3026 m.data (), ld, a.data (), lda, 0.0, |
|
3027 c, nr, 1L, 1L)); |
|
3028 |
|
3029 if (f77_exception_encountered) |
|
3030 (*current_liboctave_error_handler) |
|
3031 ("unrecoverable error in dgemm"); |
|
3032 } |
|
3033 } |
|
3034 |
|
3035 return retval; |
|
3036 } |
|
3037 |
2870
|
3038 MS_CMP_OPS(Matrix, , double, ) |
3504
|
3039 MS_BOOL_OPS(Matrix, double, 0.0) |
2870
|
3040 |
|
3041 SM_CMP_OPS(double, , Matrix, ) |
3504
|
3042 SM_BOOL_OPS(double, Matrix, 0.0) |
2870
|
3043 |
|
3044 MM_CMP_OPS(Matrix, , Matrix, ) |
3504
|
3045 MM_BOOL_OPS(Matrix, Matrix, 0.0) |
2870
|
3046 |
458
|
3047 /* |
|
3048 ;;; Local Variables: *** |
|
3049 ;;; mode: C++ *** |
|
3050 ;;; End: *** |
|
3051 */ |