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