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