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