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