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