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