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