458
|
1 // Matrix manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 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" |
1367
|
44 #include "f77-uscore.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 { |
|
527 int nr = rows (); |
|
528 int nc = cols (); |
|
529 int len = length (); |
|
530 if (nr != nc || nr == 0 || nc == 0) |
|
531 { |
|
532 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
533 return Matrix (); |
|
534 } |
|
535 |
|
536 info = 0; |
|
537 |
|
538 int *ipvt = new int [nr]; |
|
539 double *z = new double [nr]; |
|
540 double *tmp_data = dup (data (), len); |
|
541 |
1253
|
542 F77_FCN (dgeco, DGECO) (tmp_data, nr, nc, ipvt, rcond, z); |
458
|
543 |
1195
|
544 volatile double rcond_plus_one = rcond + 1.0; |
1657
|
545 |
|
546 if (rcond_plus_one == 1.0) |
|
547 info = -1; |
|
548 |
|
549 if (info == -1 && ! force) |
458
|
550 { |
|
551 copy (tmp_data, data (), len); // Restore matrix contents. |
|
552 } |
|
553 else |
|
554 { |
1365
|
555 double *dummy = 0; |
458
|
556 |
1253
|
557 F77_FCN (dgedi, DGEDI) (tmp_data, nr, nc, ipvt, dummy, z, 1); |
458
|
558 } |
|
559 |
|
560 delete [] ipvt; |
|
561 delete [] z; |
|
562 |
|
563 return Matrix (tmp_data, nr, nc); |
|
564 } |
|
565 |
740
|
566 Matrix |
|
567 Matrix::pseudo_inverse (double tol) |
|
568 { |
|
569 SVD result (*this); |
|
570 |
|
571 DiagMatrix S = result.singular_values (); |
|
572 Matrix U = result.left_singular_matrix (); |
|
573 Matrix V = result.right_singular_matrix (); |
|
574 |
|
575 ColumnVector sigma = S.diag (); |
|
576 |
|
577 int r = sigma.length () - 1; |
|
578 int nr = rows (); |
|
579 int nc = cols (); |
|
580 |
|
581 if (tol <= 0.0) |
|
582 { |
|
583 if (nr > nc) |
|
584 tol = nr * sigma.elem (0) * DBL_EPSILON; |
|
585 else |
|
586 tol = nc * sigma.elem (0) * DBL_EPSILON; |
|
587 } |
|
588 |
|
589 while (r >= 0 && sigma.elem (r) < tol) |
|
590 r--; |
|
591 |
|
592 if (r < 0) |
|
593 return Matrix (nc, nr, 0.0); |
|
594 else |
|
595 { |
|
596 Matrix Ur = U.extract (0, 0, nr-1, r); |
|
597 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); |
|
598 Matrix Vr = V.extract (0, 0, nc-1, r); |
|
599 return Vr * D * Ur.transpose (); |
|
600 } |
|
601 } |
|
602 |
458
|
603 ComplexMatrix |
|
604 Matrix::fourier (void) const |
|
605 { |
|
606 int nr = rows (); |
|
607 int nc = cols (); |
|
608 int npts, nsamples; |
|
609 if (nr == 1 || nc == 1) |
|
610 { |
|
611 npts = nr > nc ? nr : nc; |
|
612 nsamples = 1; |
|
613 } |
|
614 else |
|
615 { |
|
616 npts = nr; |
|
617 nsamples = nc; |
|
618 } |
|
619 |
|
620 int nn = 4*npts+15; |
|
621 Complex *wsave = new Complex [nn]; |
|
622 Complex *tmp_data = make_complex (data (), length ()); |
|
623 |
1253
|
624 F77_FCN (cffti, CFFTI) (npts, wsave); |
458
|
625 |
|
626 for (int j = 0; j < nsamples; j++) |
1253
|
627 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], wsave); |
458
|
628 |
|
629 delete [] wsave; |
|
630 |
|
631 return ComplexMatrix (tmp_data, nr, nc); |
|
632 } |
|
633 |
|
634 ComplexMatrix |
|
635 Matrix::ifourier (void) const |
|
636 { |
|
637 int nr = rows (); |
|
638 int nc = cols (); |
|
639 int npts, nsamples; |
|
640 if (nr == 1 || nc == 1) |
|
641 { |
|
642 npts = nr > nc ? nr : nc; |
|
643 nsamples = 1; |
|
644 } |
|
645 else |
|
646 { |
|
647 npts = nr; |
|
648 nsamples = nc; |
|
649 } |
|
650 |
|
651 int nn = 4*npts+15; |
|
652 Complex *wsave = new Complex [nn]; |
|
653 Complex *tmp_data = make_complex (data (), length ()); |
|
654 |
1253
|
655 F77_FCN (cffti, CFFTI) (npts, wsave); |
458
|
656 |
|
657 for (int j = 0; j < nsamples; j++) |
1253
|
658 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], wsave); |
458
|
659 |
1321
|
660 for (int j = 0; j < npts*nsamples; j++) |
458
|
661 tmp_data[j] = tmp_data[j] / (double) npts; |
|
662 |
|
663 delete [] wsave; |
|
664 |
|
665 return ComplexMatrix (tmp_data, nr, nc); |
|
666 } |
|
667 |
677
|
668 ComplexMatrix |
|
669 Matrix::fourier2d (void) const |
|
670 { |
|
671 int nr = rows (); |
|
672 int nc = cols (); |
|
673 int npts, nsamples; |
|
674 if (nr == 1 || nc == 1) |
|
675 { |
|
676 npts = nr > nc ? nr : nc; |
|
677 nsamples = 1; |
|
678 } |
|
679 else |
|
680 { |
|
681 npts = nr; |
|
682 nsamples = nc; |
|
683 } |
|
684 |
|
685 int nn = 4*npts+15; |
|
686 Complex *wsave = new Complex [nn]; |
|
687 Complex *tmp_data = make_complex (data (), length ()); |
|
688 |
1253
|
689 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
690 |
|
691 for (int j = 0; j < nsamples; j++) |
1253
|
692 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], wsave); |
677
|
693 |
|
694 delete [] wsave; |
|
695 |
|
696 npts = nc; |
|
697 nsamples = nr; |
|
698 nn = 4*npts+15; |
|
699 wsave = new Complex [nn]; |
|
700 Complex *row = new Complex[npts]; |
|
701 |
1253
|
702 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
703 |
1321
|
704 for (int j = 0; j < nsamples; j++) |
677
|
705 { |
|
706 for (int i = 0; i < npts; i++) |
|
707 row[i] = tmp_data[i*nr + j]; |
|
708 |
1253
|
709 F77_FCN (cfftf, CFFTF) (npts, row, wsave); |
677
|
710 |
1321
|
711 for (int i = 0; i < npts; i++) |
677
|
712 tmp_data[i*nr + j] = row[i]; |
|
713 } |
|
714 |
|
715 delete [] wsave; |
|
716 delete [] row; |
|
717 |
|
718 return ComplexMatrix (tmp_data, nr, nc); |
|
719 } |
|
720 |
|
721 ComplexMatrix |
|
722 Matrix::ifourier2d (void) const |
|
723 { |
|
724 int nr = rows (); |
|
725 int nc = cols (); |
|
726 int npts, nsamples; |
|
727 if (nr == 1 || nc == 1) |
|
728 { |
|
729 npts = nr > nc ? nr : nc; |
|
730 nsamples = 1; |
|
731 } |
|
732 else |
|
733 { |
|
734 npts = nr; |
|
735 nsamples = nc; |
|
736 } |
|
737 |
|
738 int nn = 4*npts+15; |
|
739 Complex *wsave = new Complex [nn]; |
|
740 Complex *tmp_data = make_complex (data (), length ()); |
|
741 |
1253
|
742 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
743 |
|
744 for (int j = 0; j < nsamples; j++) |
1253
|
745 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], wsave); |
677
|
746 |
|
747 delete [] wsave; |
|
748 |
1321
|
749 for (int j = 0; j < npts*nsamples; j++) |
677
|
750 tmp_data[j] = tmp_data[j] / (double) npts; |
|
751 |
|
752 npts = nc; |
|
753 nsamples = nr; |
|
754 nn = 4*npts+15; |
|
755 wsave = new Complex [nn]; |
|
756 Complex *row = new Complex[npts]; |
|
757 |
1253
|
758 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
759 |
1321
|
760 for (int j = 0; j < nsamples; j++) |
677
|
761 { |
|
762 for (int i = 0; i < npts; i++) |
|
763 row[i] = tmp_data[i*nr + j]; |
|
764 |
1253
|
765 F77_FCN (cfftb, CFFTB) (npts, row, wsave); |
677
|
766 |
1321
|
767 for (int i = 0; i < npts; i++) |
677
|
768 tmp_data[i*nr + j] = row[i] / (double) npts; |
|
769 } |
|
770 |
|
771 delete [] wsave; |
|
772 delete [] row; |
|
773 |
|
774 return ComplexMatrix (tmp_data, nr, nc); |
|
775 } |
|
776 |
458
|
777 DET |
|
778 Matrix::determinant (void) const |
|
779 { |
|
780 int info; |
|
781 double rcond; |
|
782 return determinant (info, rcond); |
|
783 } |
|
784 |
|
785 DET |
|
786 Matrix::determinant (int& info) const |
|
787 { |
|
788 double rcond; |
|
789 return determinant (info, rcond); |
|
790 } |
|
791 |
|
792 DET |
532
|
793 Matrix::determinant (int& info, double& rcond) const |
458
|
794 { |
|
795 DET retval; |
|
796 |
|
797 int nr = rows (); |
|
798 int nc = cols (); |
|
799 |
|
800 if (nr == 0 || nc == 0) |
|
801 { |
|
802 double d[2]; |
|
803 d[0] = 1.0; |
|
804 d[1] = 0.0; |
|
805 retval = DET (d); |
|
806 } |
|
807 else |
|
808 { |
|
809 info = 0; |
|
810 int *ipvt = new int [nr]; |
|
811 |
|
812 double *z = new double [nr]; |
|
813 double *tmp_data = dup (data (), length ()); |
|
814 |
1253
|
815 F77_FCN (dgeco, DGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
816 |
1195
|
817 volatile double rcond_plus_one = rcond + 1.0; |
|
818 if (rcond_plus_one == 1.0) |
458
|
819 { |
|
820 info = -1; |
|
821 retval = DET (); |
|
822 } |
|
823 else |
|
824 { |
|
825 double d[2]; |
1253
|
826 F77_FCN (dgedi, DGEDI) (tmp_data, nr, nr, ipvt, d, z, 10); |
458
|
827 retval = DET (d); |
|
828 } |
|
829 |
|
830 delete [] tmp_data; |
|
831 delete [] ipvt; |
|
832 delete [] z; |
|
833 } |
|
834 |
|
835 return retval; |
|
836 } |
|
837 |
|
838 Matrix |
|
839 Matrix::solve (const Matrix& b) const |
|
840 { |
|
841 int info; |
|
842 double rcond; |
|
843 return solve (b, info, rcond); |
|
844 } |
|
845 |
|
846 Matrix |
|
847 Matrix::solve (const Matrix& b, int& info) const |
|
848 { |
|
849 double rcond; |
|
850 return solve (b, info, rcond); |
|
851 } |
|
852 |
|
853 Matrix |
532
|
854 Matrix::solve (const Matrix& b, int& info, double& rcond) const |
458
|
855 { |
|
856 Matrix retval; |
|
857 |
|
858 int nr = rows (); |
|
859 int nc = cols (); |
|
860 if (nr == 0 || nc == 0 || nr != nc || nr != b.rows ()) |
|
861 { |
|
862 (*current_liboctave_error_handler) |
|
863 ("matrix dimension mismatch solution of linear equations"); |
|
864 return Matrix (); |
|
865 } |
|
866 |
|
867 info = 0; |
|
868 int *ipvt = new int [nr]; |
|
869 |
|
870 double *z = new double [nr]; |
|
871 double *tmp_data = dup (data (), length ()); |
|
872 |
1253
|
873 F77_FCN (dgeco, DGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
874 |
1195
|
875 volatile double rcond_plus_one = rcond + 1.0; |
|
876 if (rcond_plus_one == 1.0) |
458
|
877 { |
|
878 info = -2; |
|
879 } |
|
880 else |
|
881 { |
|
882 double *result = dup (b.data (), b.length ()); |
|
883 |
|
884 int b_nc = b.cols (); |
|
885 for (int j = 0; j < b_nc; j++) |
1253
|
886 F77_FCN (dgesl, DGESL) (tmp_data, nr, nr, ipvt, &result[nr*j], 0); |
458
|
887 |
|
888 retval = Matrix (result, b.rows (), b_nc); |
|
889 } |
|
890 |
|
891 delete [] tmp_data; |
|
892 delete [] ipvt; |
|
893 delete [] z; |
|
894 |
|
895 return retval; |
|
896 } |
|
897 |
|
898 ComplexMatrix |
|
899 Matrix::solve (const ComplexMatrix& b) const |
|
900 { |
|
901 ComplexMatrix tmp (*this); |
|
902 return tmp.solve (b); |
|
903 } |
|
904 |
|
905 ComplexMatrix |
|
906 Matrix::solve (const ComplexMatrix& b, int& info) const |
|
907 { |
|
908 ComplexMatrix tmp (*this); |
|
909 return tmp.solve (b, info); |
|
910 } |
|
911 |
|
912 ComplexMatrix |
|
913 Matrix::solve (const ComplexMatrix& b, int& info, double& rcond) const |
|
914 { |
|
915 ComplexMatrix tmp (*this); |
|
916 return tmp.solve (b, info, rcond); |
|
917 } |
|
918 |
|
919 ColumnVector |
|
920 Matrix::solve (const ColumnVector& b) const |
|
921 { |
|
922 int info; double rcond; |
|
923 return solve (b, info, rcond); |
|
924 } |
|
925 |
|
926 ColumnVector |
|
927 Matrix::solve (const ColumnVector& b, int& info) const |
|
928 { |
|
929 double rcond; |
|
930 return solve (b, info, rcond); |
|
931 } |
|
932 |
|
933 ColumnVector |
532
|
934 Matrix::solve (const ColumnVector& b, int& info, double& rcond) const |
458
|
935 { |
|
936 ColumnVector retval; |
|
937 |
|
938 int nr = rows (); |
|
939 int nc = cols (); |
|
940 if (nr == 0 || nc == 0 || nr != nc || nr != b.length ()) |
|
941 { |
|
942 (*current_liboctave_error_handler) |
|
943 ("matrix dimension mismatch solution of linear equations"); |
|
944 return ColumnVector (); |
|
945 } |
|
946 |
|
947 info = 0; |
|
948 int *ipvt = new int [nr]; |
|
949 |
|
950 double *z = new double [nr]; |
|
951 double *tmp_data = dup (data (), length ()); |
|
952 |
1253
|
953 F77_FCN (dgeco, DGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
954 |
1195
|
955 volatile double rcond_plus_one = rcond + 1.0; |
|
956 if (rcond_plus_one == 1.0) |
458
|
957 { |
|
958 info = -2; |
|
959 } |
|
960 else |
|
961 { |
|
962 int b_len = b.length (); |
|
963 |
|
964 double *result = dup (b.data (), b_len); |
|
965 |
1253
|
966 F77_FCN (dgesl, DGESL) (tmp_data, nr, nr, ipvt, result, 0); |
458
|
967 |
|
968 retval = ColumnVector (result, b_len); |
|
969 } |
|
970 |
|
971 delete [] tmp_data; |
|
972 delete [] ipvt; |
|
973 delete [] z; |
|
974 |
|
975 return retval; |
|
976 } |
|
977 |
|
978 ComplexColumnVector |
|
979 Matrix::solve (const ComplexColumnVector& b) const |
|
980 { |
|
981 ComplexMatrix tmp (*this); |
|
982 return tmp.solve (b); |
|
983 } |
|
984 |
|
985 ComplexColumnVector |
|
986 Matrix::solve (const ComplexColumnVector& b, int& info) const |
|
987 { |
|
988 ComplexMatrix tmp (*this); |
|
989 return tmp.solve (b, info); |
|
990 } |
|
991 |
|
992 ComplexColumnVector |
|
993 Matrix::solve (const ComplexColumnVector& b, int& info, double& rcond) const |
|
994 { |
|
995 ComplexMatrix tmp (*this); |
|
996 return tmp.solve (b, info, rcond); |
|
997 } |
|
998 |
|
999 Matrix |
|
1000 Matrix::lssolve (const Matrix& b) const |
|
1001 { |
|
1002 int info; |
|
1003 int rank; |
|
1004 return lssolve (b, info, rank); |
|
1005 } |
|
1006 |
|
1007 Matrix |
|
1008 Matrix::lssolve (const Matrix& b, int& info) const |
|
1009 { |
|
1010 int rank; |
|
1011 return lssolve (b, info, rank); |
|
1012 } |
|
1013 |
|
1014 Matrix |
|
1015 Matrix::lssolve (const Matrix& b, int& info, int& rank) const |
|
1016 { |
|
1017 int nrhs = b.cols (); |
|
1018 |
|
1019 int m = rows (); |
|
1020 int n = cols (); |
|
1021 |
|
1022 if (m == 0 || n == 0 || m != b.rows ()) |
|
1023 { |
|
1024 (*current_liboctave_error_handler) |
|
1025 ("matrix dimension mismatch in solution of least squares problem"); |
|
1026 return Matrix (); |
|
1027 } |
|
1028 |
|
1029 double *tmp_data = dup (data (), length ()); |
|
1030 |
|
1031 int nrr = m > n ? m : n; |
|
1032 Matrix result (nrr, nrhs); |
|
1033 |
1321
|
1034 for (int j = 0; j < nrhs; j++) |
|
1035 for (int i = 0; i < m; i++) |
458
|
1036 result.elem (i, j) = b.elem (i, j); |
|
1037 |
|
1038 double *presult = result.fortran_vec (); |
|
1039 |
|
1040 int len_s = m < n ? m : n; |
|
1041 double *s = new double [len_s]; |
|
1042 double rcond = -1.0; |
|
1043 int lwork; |
|
1044 if (m < n) |
|
1045 lwork = 3*m + (2*m > nrhs ? (2*m > n ? 2*m : n) : (nrhs > n ? nrhs : n)); |
|
1046 else |
|
1047 lwork = 3*n + (2*n > nrhs ? (2*n > m ? 2*n : m) : (nrhs > m ? nrhs : m)); |
|
1048 |
|
1049 double *work = new double [lwork]; |
|
1050 |
1253
|
1051 F77_FCN (dgelss, DGELSS) (m, n, nrhs, tmp_data, m, presult, nrr, s, |
|
1052 rcond, rank, work, lwork, info); |
458
|
1053 |
|
1054 Matrix retval (n, nrhs); |
1321
|
1055 for (int j = 0; j < nrhs; j++) |
|
1056 for (int i = 0; i < n; i++) |
458
|
1057 retval.elem (i, j) = result.elem (i, j); |
|
1058 |
|
1059 delete [] tmp_data; |
|
1060 delete [] s; |
|
1061 delete [] work; |
|
1062 |
|
1063 return retval; |
|
1064 } |
|
1065 |
|
1066 ComplexMatrix |
|
1067 Matrix::lssolve (const ComplexMatrix& b) const |
|
1068 { |
|
1069 ComplexMatrix tmp (*this); |
1484
|
1070 int info; |
|
1071 int rank; |
|
1072 return tmp.lssolve (b, info, rank); |
458
|
1073 } |
|
1074 |
|
1075 ComplexMatrix |
|
1076 Matrix::lssolve (const ComplexMatrix& b, int& info) const |
|
1077 { |
|
1078 ComplexMatrix tmp (*this); |
1484
|
1079 int rank; |
|
1080 return tmp.lssolve (b, info, rank); |
458
|
1081 } |
|
1082 |
|
1083 ComplexMatrix |
|
1084 Matrix::lssolve (const ComplexMatrix& b, int& info, int& rank) const |
|
1085 { |
|
1086 ComplexMatrix tmp (*this); |
1484
|
1087 return tmp.lssolve (b, info, rank); |
458
|
1088 } |
|
1089 |
|
1090 ColumnVector |
|
1091 Matrix::lssolve (const ColumnVector& b) const |
|
1092 { |
|
1093 int info; |
1484
|
1094 int rank; |
|
1095 return lssolve (b, info, rank); |
458
|
1096 } |
|
1097 |
|
1098 ColumnVector |
|
1099 Matrix::lssolve (const ColumnVector& b, int& info) const |
|
1100 { |
|
1101 int rank; |
|
1102 return lssolve (b, info, rank); |
|
1103 } |
|
1104 |
|
1105 ColumnVector |
|
1106 Matrix::lssolve (const ColumnVector& b, int& info, int& rank) const |
|
1107 { |
|
1108 int nrhs = 1; |
|
1109 |
|
1110 int m = rows (); |
|
1111 int n = cols (); |
|
1112 |
|
1113 if (m == 0 || n == 0 || m != b.length ()) |
|
1114 { |
|
1115 (*current_liboctave_error_handler) |
|
1116 ("matrix dimension mismatch in solution of least squares problem"); |
|
1117 return ColumnVector (); |
|
1118 } |
|
1119 |
|
1120 double *tmp_data = dup (data (), length ()); |
|
1121 |
|
1122 int nrr = m > n ? m : n; |
|
1123 ColumnVector result (nrr); |
|
1124 |
1321
|
1125 for (int i = 0; i < m; i++) |
458
|
1126 result.elem (i) = b.elem (i); |
|
1127 |
|
1128 double *presult = result.fortran_vec (); |
|
1129 |
|
1130 int len_s = m < n ? m : n; |
|
1131 double *s = new double [len_s]; |
|
1132 double rcond = -1.0; |
|
1133 int lwork; |
|
1134 if (m < n) |
|
1135 lwork = 3*m + (2*m > nrhs ? (2*m > n ? 2*m : n) : (nrhs > n ? nrhs : n)); |
|
1136 else |
|
1137 lwork = 3*n + (2*n > nrhs ? (2*n > m ? 2*n : m) : (nrhs > m ? nrhs : m)); |
|
1138 |
|
1139 double *work = new double [lwork]; |
|
1140 |
1253
|
1141 F77_FCN (dgelss, DGELSS) (m, n, nrhs, tmp_data, m, presult, nrr, s, |
|
1142 rcond, rank, work, lwork, info); |
458
|
1143 |
|
1144 ColumnVector retval (n); |
1321
|
1145 for (int i = 0; i < n; i++) |
458
|
1146 retval.elem (i) = result.elem (i); |
|
1147 |
|
1148 delete [] tmp_data; |
|
1149 delete [] s; |
|
1150 delete [] work; |
|
1151 |
|
1152 return retval; |
|
1153 } |
|
1154 |
|
1155 ComplexColumnVector |
|
1156 Matrix::lssolve (const ComplexColumnVector& b) const |
|
1157 { |
|
1158 ComplexMatrix tmp (*this); |
|
1159 return tmp.lssolve (b); |
|
1160 } |
|
1161 |
|
1162 ComplexColumnVector |
|
1163 Matrix::lssolve (const ComplexColumnVector& b, int& info) const |
|
1164 { |
|
1165 ComplexMatrix tmp (*this); |
|
1166 return tmp.lssolve (b, info); |
|
1167 } |
|
1168 |
|
1169 ComplexColumnVector |
|
1170 Matrix::lssolve (const ComplexColumnVector& b, int& info, int& rank) const |
|
1171 { |
|
1172 ComplexMatrix tmp (*this); |
|
1173 return tmp.lssolve (b, info, rank); |
|
1174 } |
|
1175 |
1819
|
1176 // Constants for matrix exponential calculation. |
|
1177 |
|
1178 static double padec [] = |
|
1179 { |
|
1180 5.0000000000000000e-1, |
|
1181 1.1666666666666667e-1, |
|
1182 1.6666666666666667e-2, |
|
1183 1.6025641025641026e-3, |
|
1184 1.0683760683760684e-4, |
|
1185 4.8562548562548563e-6, |
|
1186 1.3875013875013875e-7, |
|
1187 1.9270852604185938e-9, |
|
1188 }; |
|
1189 |
|
1190 Matrix |
|
1191 Matrix::expm (void) const |
|
1192 { |
|
1193 Matrix retval; |
|
1194 |
|
1195 Matrix m = *this; |
|
1196 |
|
1197 int nc = columns (); |
|
1198 |
|
1199 // trace shift value |
|
1200 double trshift = 0; |
|
1201 |
|
1202 // Preconditioning step 1: trace normalization. |
|
1203 |
|
1204 for (int i = 0; i < nc; i++) |
|
1205 trshift += m.elem (i, i); |
|
1206 |
|
1207 trshift /= nc; |
|
1208 |
|
1209 for (int i = 0; i < nc; i++) |
|
1210 m.elem (i, i) -= trshift; |
|
1211 |
|
1212 // Preconditioning step 2: balancing. |
|
1213 |
|
1214 AEPBALANCE mbal (m, "B"); |
|
1215 m = mbal.balanced_matrix (); |
|
1216 Matrix d = mbal.balancing_matrix (); |
|
1217 |
|
1218 // Preconditioning step 3: scaling. |
|
1219 |
|
1220 ColumnVector work(nc); |
|
1221 double inf_norm |
|
1222 = F77_FCN (dlange, DLANGE) ("I", nc, nc, m.fortran_vec (),nc, |
|
1223 work.fortran_vec ()); |
|
1224 |
|
1225 int sqpow = (int) (inf_norm > 0.0 |
|
1226 ? (1.0 + log (inf_norm) / log (2.0)) |
|
1227 : 0.0); |
|
1228 |
|
1229 // Check whether we need to square at all. |
|
1230 |
|
1231 if (sqpow < 0) |
|
1232 sqpow = 0; |
|
1233 |
|
1234 if (sqpow > 0) |
|
1235 { |
|
1236 double scale_factor = 1.0; |
|
1237 for (int i = 0; i < sqpow; i++) |
|
1238 scale_factor *= 2.0; |
|
1239 |
|
1240 m = m / scale_factor; |
|
1241 } |
|
1242 |
|
1243 // npp, dpp: pade' approx polynomial matrices. |
|
1244 |
|
1245 Matrix npp (nc, nc, 0.0); |
|
1246 Matrix dpp = npp; |
|
1247 |
|
1248 // Now powers a^8 ... a^1. |
|
1249 |
|
1250 int minus_one_j = -1; |
|
1251 for (int j = 7; j >= 0; j--) |
|
1252 { |
|
1253 npp = m * npp + m * padec[j]; |
|
1254 dpp = m * dpp + m * (minus_one_j * padec[j]); |
|
1255 minus_one_j *= -1; |
|
1256 } |
|
1257 |
|
1258 // Zero power. |
|
1259 |
|
1260 dpp = -dpp; |
|
1261 for(int j = 0; j < nc; j++) |
|
1262 { |
|
1263 npp.elem (j, j) += 1.0; |
|
1264 dpp.elem (j, j) += 1.0; |
|
1265 } |
|
1266 |
|
1267 // Compute pade approximation = inverse (dpp) * npp. |
|
1268 |
|
1269 retval = dpp.solve (npp); |
|
1270 |
|
1271 // Reverse preconditioning step 3: repeated squaring. |
|
1272 |
|
1273 while (sqpow) |
|
1274 { |
|
1275 retval = retval * retval; |
|
1276 sqpow--; |
|
1277 } |
|
1278 |
|
1279 // Reverse preconditioning step 2: inverse balancing. |
|
1280 |
|
1281 retval = retval.transpose(); |
|
1282 d = d.transpose (); |
|
1283 retval = retval * d; |
|
1284 retval = d.solve (retval); |
|
1285 retval = retval.transpose (); |
|
1286 |
|
1287 // Reverse preconditioning step 1: fix trace normalization. |
|
1288 |
|
1289 return retval * exp (trshift); |
|
1290 } |
|
1291 |
458
|
1292 Matrix& |
|
1293 Matrix::operator += (const Matrix& a) |
|
1294 { |
|
1295 int nr = rows (); |
|
1296 int nc = cols (); |
|
1297 if (nr != a.rows () || nc != a.cols ()) |
|
1298 { |
|
1299 (*current_liboctave_error_handler) |
|
1300 ("nonconformant matrix += operation attempted"); |
|
1301 return *this; |
|
1302 } |
|
1303 |
|
1304 if (nr == 0 || nc == 0) |
|
1305 return *this; |
|
1306 |
|
1307 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1308 |
|
1309 add2 (d, a.data (), length ()); |
|
1310 |
|
1311 return *this; |
|
1312 } |
|
1313 |
|
1314 Matrix& |
|
1315 Matrix::operator -= (const Matrix& a) |
|
1316 { |
|
1317 int nr = rows (); |
|
1318 int nc = cols (); |
|
1319 if (nr != a.rows () || nc != a.cols ()) |
|
1320 { |
|
1321 (*current_liboctave_error_handler) |
|
1322 ("nonconformant matrix -= operation attempted"); |
|
1323 return *this; |
|
1324 } |
|
1325 |
|
1326 if (nr == 0 || nc == 0) |
|
1327 return *this; |
|
1328 |
|
1329 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1330 |
|
1331 subtract2 (d, a.data (), length ()); |
|
1332 |
|
1333 return *this; |
|
1334 } |
|
1335 |
|
1336 Matrix& |
|
1337 Matrix::operator += (const DiagMatrix& a) |
|
1338 { |
|
1339 if (rows () != a.rows () || cols () != a.cols ()) |
|
1340 { |
|
1341 (*current_liboctave_error_handler) |
|
1342 ("nonconformant matrix += operation attempted"); |
|
1343 return *this; |
|
1344 } |
|
1345 |
|
1346 for (int i = 0; i < a.length (); i++) |
|
1347 elem (i, i) += a.elem (i, i); |
|
1348 |
|
1349 return *this; |
|
1350 } |
|
1351 |
|
1352 Matrix& |
|
1353 Matrix::operator -= (const DiagMatrix& a) |
|
1354 { |
|
1355 if (rows () != a.rows () || cols () != a.cols ()) |
|
1356 { |
|
1357 (*current_liboctave_error_handler) |
|
1358 ("nonconformant matrix += operation attempted"); |
|
1359 return *this; |
|
1360 } |
|
1361 |
|
1362 for (int i = 0; i < a.length (); i++) |
|
1363 elem (i, i) -= a.elem (i, i); |
|
1364 |
|
1365 return *this; |
|
1366 } |
|
1367 |
|
1368 // unary operations |
|
1369 |
|
1370 Matrix |
|
1371 Matrix::operator ! (void) const |
|
1372 { |
|
1373 int nr = rows (); |
|
1374 int nc = cols (); |
|
1375 |
|
1376 Matrix b (nr, nc); |
|
1377 |
|
1378 for (int j = 0; j < nc; j++) |
|
1379 for (int i = 0; i < nr; i++) |
|
1380 b.elem (i, j) = ! elem (i, j); |
|
1381 |
|
1382 return b; |
|
1383 } |
|
1384 |
1205
|
1385 // column vector by row vector -> matrix operations |
458
|
1386 |
1205
|
1387 Matrix |
|
1388 operator * (const ColumnVector& v, const RowVector& a) |
458
|
1389 { |
1205
|
1390 int len = v.length (); |
|
1391 int a_len = a.length (); |
|
1392 if (len != a_len) |
|
1393 { |
|
1394 (*current_liboctave_error_handler) |
|
1395 ("nonconformant vector multiplication attempted"); |
|
1396 return Matrix (); |
|
1397 } |
458
|
1398 |
1205
|
1399 if (len == 0) |
|
1400 return Matrix (len, len, 0.0); |
458
|
1401 |
1205
|
1402 double *c = new double [len * a_len]; |
|
1403 |
1253
|
1404 F77_FCN (dgemm, DGEMM) ("N", "N", len, a_len, 1, 1.0, v.data (), |
|
1405 len, a.data (), 1, 0.0, c, len, 1L, 1L); |
1205
|
1406 |
|
1407 return Matrix (c, len, a_len); |
458
|
1408 } |
|
1409 |
1205
|
1410 // diagonal matrix by scalar -> matrix operations |
|
1411 |
|
1412 Matrix |
|
1413 operator + (const DiagMatrix& a, double s) |
458
|
1414 { |
1205
|
1415 Matrix tmp (a.rows (), a.cols (), s); |
|
1416 return a + tmp; |
458
|
1417 } |
|
1418 |
1205
|
1419 Matrix |
|
1420 operator - (const DiagMatrix& a, double s) |
458
|
1421 { |
1205
|
1422 Matrix tmp (a.rows (), a.cols (), -s); |
|
1423 return a + tmp; |
458
|
1424 } |
|
1425 |
1205
|
1426 // scalar by diagonal matrix -> matrix operations |
|
1427 |
|
1428 Matrix |
|
1429 operator + (double s, const DiagMatrix& a) |
458
|
1430 { |
1205
|
1431 Matrix tmp (a.rows (), a.cols (), s); |
|
1432 return tmp + a; |
|
1433 } |
|
1434 |
|
1435 Matrix |
|
1436 operator - (double s, const DiagMatrix& a) |
|
1437 { |
|
1438 Matrix tmp (a.rows (), a.cols (), s); |
|
1439 return tmp - a; |
458
|
1440 } |
|
1441 |
|
1442 // matrix by diagonal matrix -> matrix operations |
|
1443 |
|
1444 Matrix |
|
1445 operator + (const Matrix& m, const DiagMatrix& a) |
|
1446 { |
|
1447 int nr = m.rows (); |
|
1448 int nc = m.cols (); |
|
1449 if (nr != a.rows () || nc != a.cols ()) |
|
1450 { |
|
1451 (*current_liboctave_error_handler) |
|
1452 ("nonconformant matrix addition attempted"); |
|
1453 return Matrix (); |
|
1454 } |
|
1455 |
|
1456 if (nr == 0 || nc == 0) |
|
1457 return Matrix (nr, nc); |
|
1458 |
|
1459 Matrix result (m); |
|
1460 int a_len = a.length (); |
|
1461 for (int i = 0; i < a_len; i++) |
|
1462 result.elem (i, i) += a.elem (i, i); |
|
1463 |
|
1464 return result; |
|
1465 } |
|
1466 |
|
1467 Matrix |
|
1468 operator - (const Matrix& m, const DiagMatrix& a) |
|
1469 { |
|
1470 int nr = m.rows (); |
|
1471 int nc = m.cols (); |
|
1472 if (nr != a.rows () || nc != a.cols ()) |
|
1473 { |
|
1474 (*current_liboctave_error_handler) |
|
1475 ("nonconformant matrix subtraction attempted"); |
|
1476 return Matrix (); |
|
1477 } |
|
1478 |
|
1479 if (nr == 0 || nc == 0) |
|
1480 return Matrix (nr, nc); |
|
1481 |
|
1482 Matrix result (m); |
|
1483 int a_len = a.length (); |
|
1484 for (int i = 0; i < a_len; i++) |
|
1485 result.elem (i, i) -= a.elem (i, i); |
|
1486 |
|
1487 return result; |
|
1488 } |
|
1489 |
|
1490 Matrix |
|
1491 operator * (const Matrix& m, const DiagMatrix& a) |
|
1492 { |
|
1493 int nr = m.rows (); |
|
1494 int nc = m.cols (); |
|
1495 int a_nr = a.rows (); |
|
1496 int a_nc = a.cols (); |
|
1497 if (nc != a_nr) |
|
1498 { |
|
1499 (*current_liboctave_error_handler) |
|
1500 ("nonconformant matrix multiplication attempted"); |
|
1501 return Matrix (); |
|
1502 } |
|
1503 |
|
1504 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1505 return Matrix (nr, a_nc, 0.0); |
|
1506 |
|
1507 double *c = new double [nr*a_nc]; |
533
|
1508 double *ctmp = 0; |
458
|
1509 |
|
1510 int a_len = a.length (); |
|
1511 for (int j = 0; j < a_len; j++) |
|
1512 { |
|
1513 int idx = j * nr; |
|
1514 ctmp = c + idx; |
|
1515 if (a.elem (j, j) == 1.0) |
|
1516 { |
|
1517 for (int i = 0; i < nr; i++) |
|
1518 ctmp[i] = m.elem (i, j); |
|
1519 } |
|
1520 else if (a.elem (j, j) == 0.0) |
|
1521 { |
|
1522 for (int i = 0; i < nr; i++) |
|
1523 ctmp[i] = 0.0; |
|
1524 } |
|
1525 else |
|
1526 { |
|
1527 for (int i = 0; i < nr; i++) |
|
1528 ctmp[i] = a.elem (j, j) * m.elem (i, j); |
|
1529 } |
|
1530 } |
|
1531 |
|
1532 if (a_nr < a_nc) |
|
1533 { |
|
1534 for (int i = nr * nc; i < nr * a_nc; i++) |
|
1535 ctmp[i] = 0.0; |
|
1536 } |
|
1537 |
|
1538 return Matrix (c, nr, a_nc); |
|
1539 } |
|
1540 |
1205
|
1541 // diagonal matrix by matrix -> matrix operations |
|
1542 |
|
1543 Matrix |
|
1544 operator + (const DiagMatrix& m, const Matrix& a) |
458
|
1545 { |
|
1546 int nr = m.rows (); |
|
1547 int nc = m.cols (); |
|
1548 if (nr != a.rows () || nc != a.cols ()) |
|
1549 { |
|
1550 (*current_liboctave_error_handler) |
|
1551 ("nonconformant matrix addition attempted"); |
1205
|
1552 return Matrix (); |
458
|
1553 } |
|
1554 |
|
1555 if (nr == 0 || nc == 0) |
1205
|
1556 return Matrix (nr, nc); |
458
|
1557 |
1205
|
1558 Matrix result (a); |
|
1559 for (int i = 0; i < m.length (); i++) |
|
1560 result.elem (i, i) += m.elem (i, i); |
458
|
1561 |
|
1562 return result; |
|
1563 } |
|
1564 |
1205
|
1565 Matrix |
|
1566 operator - (const DiagMatrix& m, const Matrix& a) |
458
|
1567 { |
|
1568 int nr = m.rows (); |
|
1569 int nc = m.cols (); |
|
1570 if (nr != a.rows () || nc != a.cols ()) |
|
1571 { |
|
1572 (*current_liboctave_error_handler) |
|
1573 ("nonconformant matrix subtraction attempted"); |
1205
|
1574 return Matrix (); |
458
|
1575 } |
|
1576 |
|
1577 if (nr == 0 || nc == 0) |
1205
|
1578 return Matrix (nr, nc); |
458
|
1579 |
1205
|
1580 Matrix result (-a); |
|
1581 for (int i = 0; i < m.length (); i++) |
|
1582 result.elem (i, i) += m.elem (i, i); |
458
|
1583 |
|
1584 return result; |
|
1585 } |
|
1586 |
1205
|
1587 Matrix |
|
1588 operator * (const DiagMatrix& m, const Matrix& a) |
458
|
1589 { |
|
1590 int nr = m.rows (); |
|
1591 int nc = m.cols (); |
|
1592 int a_nr = a.rows (); |
|
1593 int a_nc = a.cols (); |
|
1594 if (nc != a_nr) |
|
1595 { |
|
1596 (*current_liboctave_error_handler) |
|
1597 ("nonconformant matrix multiplication attempted"); |
1205
|
1598 return Matrix (); |
458
|
1599 } |
|
1600 |
|
1601 if (nr == 0 || nc == 0 || a_nc == 0) |
1205
|
1602 return Matrix (nr, a_nc, 0.0); |
458
|
1603 |
1205
|
1604 Matrix c (nr, a_nc); |
458
|
1605 |
1205
|
1606 for (int i = 0; i < m.length (); i++) |
458
|
1607 { |
1205
|
1608 if (m.elem (i, i) == 1.0) |
458
|
1609 { |
1205
|
1610 for (int j = 0; j < a_nc; j++) |
|
1611 c.elem (i, j) = a.elem (i, j); |
458
|
1612 } |
1205
|
1613 else if (m.elem (i, i) == 0.0) |
458
|
1614 { |
1205
|
1615 for (int j = 0; j < a_nc; j++) |
|
1616 c.elem (i, j) = 0.0; |
458
|
1617 } |
|
1618 else |
|
1619 { |
1205
|
1620 for (int j = 0; j < a_nc; j++) |
|
1621 c.elem (i, j) = m.elem (i, i) * a.elem (i, j); |
458
|
1622 } |
|
1623 } |
|
1624 |
1205
|
1625 if (nr > nc) |
458
|
1626 { |
1205
|
1627 for (int j = 0; j < a_nc; j++) |
|
1628 for (int i = a_nr; i < nr; i++) |
|
1629 c.elem (i, j) = 0.0; |
458
|
1630 } |
|
1631 |
1205
|
1632 return c; |
458
|
1633 } |
|
1634 |
|
1635 // matrix by matrix -> matrix operations |
|
1636 |
|
1637 Matrix |
|
1638 operator * (const Matrix& m, const Matrix& a) |
|
1639 { |
|
1640 int nr = m.rows (); |
|
1641 int nc = m.cols (); |
|
1642 int a_nr = a.rows (); |
|
1643 int a_nc = a.cols (); |
|
1644 if (nc != a_nr) |
|
1645 { |
|
1646 (*current_liboctave_error_handler) |
|
1647 ("nonconformant matrix multiplication attempted"); |
|
1648 return Matrix (); |
|
1649 } |
|
1650 |
|
1651 if (nr == 0 || nc == 0 || a_nc == 0) |
|
1652 return Matrix (nr, a_nc, 0.0); |
|
1653 |
|
1654 int ld = nr; |
|
1655 int lda = a_nr; |
|
1656 |
|
1657 double *c = new double [nr*a_nc]; |
|
1658 |
1253
|
1659 F77_FCN (dgemm, DGEMM) ("N", "N", nr, a_nc, nc, 1.0, m.data (), |
|
1660 ld, a.data (), lda, 0.0, c, nr, 1L, 1L); |
458
|
1661 |
|
1662 return Matrix (c, nr, a_nc); |
|
1663 } |
|
1664 |
|
1665 // other operations. |
|
1666 |
|
1667 Matrix |
|
1668 map (d_d_Mapper f, const Matrix& a) |
|
1669 { |
|
1670 Matrix b (a); |
|
1671 b.map (f); |
|
1672 return b; |
|
1673 } |
|
1674 |
1205
|
1675 Matrix |
|
1676 map (d_c_Mapper f, const ComplexMatrix& a) |
|
1677 { |
|
1678 int a_nc = a.cols (); |
|
1679 int a_nr = a.rows (); |
|
1680 Matrix b (a_nr, a_nc); |
|
1681 for (int j = 0; j < a_nc; j++) |
|
1682 for (int i = 0; i < a_nr; i++) |
|
1683 b.elem (i, j) = f (a.elem (i, j)); |
|
1684 return b; |
|
1685 } |
|
1686 |
458
|
1687 void |
|
1688 Matrix::map (d_d_Mapper f) |
|
1689 { |
|
1690 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
1691 |
|
1692 for (int i = 0; i < length (); i++) |
|
1693 d[i] = f (d[i]); |
|
1694 } |
|
1695 |
|
1696 // XXX FIXME XXX Do these really belong here? They should maybe be |
|
1697 // cleaned up a bit, no? What about corresponding functions for the |
|
1698 // Vectors? |
|
1699 |
|
1700 Matrix |
|
1701 Matrix::all (void) const |
|
1702 { |
|
1703 int nr = rows (); |
|
1704 int nc = cols (); |
|
1705 Matrix retval; |
|
1706 if (nr > 0 && nc > 0) |
|
1707 { |
|
1708 if (nr == 1) |
|
1709 { |
|
1710 retval.resize (1, 1); |
|
1711 retval.elem (0, 0) = 1.0; |
|
1712 for (int j = 0; j < nc; j++) |
|
1713 { |
|
1714 if (elem (0, j) == 0.0) |
|
1715 { |
|
1716 retval.elem (0, 0) = 0.0; |
|
1717 break; |
|
1718 } |
|
1719 } |
|
1720 } |
|
1721 else if (nc == 1) |
|
1722 { |
|
1723 retval.resize (1, 1); |
|
1724 retval.elem (0, 0) = 1.0; |
|
1725 for (int i = 0; i < nr; i++) |
|
1726 { |
|
1727 if (elem (i, 0) == 0.0) |
|
1728 { |
|
1729 retval.elem (0, 0) = 0.0; |
|
1730 break; |
|
1731 } |
|
1732 } |
|
1733 } |
|
1734 else |
|
1735 { |
|
1736 retval.resize (1, nc); |
|
1737 for (int j = 0; j < nc; j++) |
|
1738 { |
|
1739 retval.elem (0, j) = 1.0; |
|
1740 for (int i = 0; i < nr; i++) |
|
1741 { |
|
1742 if (elem (i, j) == 0.0) |
|
1743 { |
|
1744 retval.elem (0, j) = 0.0; |
|
1745 break; |
|
1746 } |
|
1747 } |
|
1748 } |
|
1749 } |
|
1750 } |
|
1751 return retval; |
|
1752 } |
|
1753 |
|
1754 Matrix |
|
1755 Matrix::any (void) const |
|
1756 { |
|
1757 int nr = rows (); |
|
1758 int nc = cols (); |
|
1759 Matrix retval; |
|
1760 if (nr > 0 && nc > 0) |
|
1761 { |
|
1762 if (nr == 1) |
|
1763 { |
|
1764 retval.resize (1, 1); |
|
1765 retval.elem (0, 0) = 0.0; |
|
1766 for (int j = 0; j < nc; j++) |
|
1767 { |
|
1768 if (elem (0, j) != 0.0) |
|
1769 { |
|
1770 retval.elem (0, 0) = 1.0; |
|
1771 break; |
|
1772 } |
|
1773 } |
|
1774 } |
|
1775 else if (nc == 1) |
|
1776 { |
|
1777 retval.resize (1, 1); |
|
1778 retval.elem (0, 0) = 0.0; |
|
1779 for (int i = 0; i < nr; i++) |
|
1780 { |
|
1781 if (elem (i, 0) != 0.0) |
|
1782 { |
|
1783 retval.elem (0, 0) = 1.0; |
|
1784 break; |
|
1785 } |
|
1786 } |
|
1787 } |
|
1788 else |
|
1789 { |
|
1790 retval.resize (1, nc); |
|
1791 for (int j = 0; j < nc; j++) |
|
1792 { |
|
1793 retval.elem (0, j) = 0.0; |
|
1794 for (int i = 0; i < nr; i++) |
|
1795 { |
|
1796 if (elem (i, j) != 0.0) |
|
1797 { |
|
1798 retval.elem (0, j) = 1.0; |
|
1799 break; |
|
1800 } |
|
1801 } |
|
1802 } |
|
1803 } |
|
1804 } |
|
1805 return retval; |
|
1806 } |
|
1807 |
|
1808 Matrix |
|
1809 Matrix::cumprod (void) const |
|
1810 { |
|
1811 Matrix retval; |
|
1812 |
|
1813 int nr = rows (); |
|
1814 int nc = cols (); |
|
1815 |
|
1816 if (nr == 1) |
|
1817 { |
|
1818 retval.resize (1, nc); |
|
1819 if (nc > 0) |
|
1820 { |
|
1821 double prod = elem (0, 0); |
|
1822 for (int j = 0; j < nc; j++) |
|
1823 { |
|
1824 retval.elem (0, j) = prod; |
|
1825 if (j < nc - 1) |
|
1826 prod *= elem (0, j+1); |
|
1827 } |
|
1828 } |
|
1829 } |
|
1830 else if (nc == 1) |
|
1831 { |
|
1832 retval.resize (nr, 1); |
|
1833 if (nr > 0) |
|
1834 { |
|
1835 double prod = elem (0, 0); |
|
1836 for (int i = 0; i < nr; i++) |
|
1837 { |
|
1838 retval.elem (i, 0) = prod; |
|
1839 if (i < nr - 1) |
|
1840 prod *= elem (i+1, 0); |
|
1841 } |
|
1842 } |
|
1843 } |
|
1844 else |
|
1845 { |
|
1846 retval.resize (nr, nc); |
|
1847 if (nr > 0 && nc > 0) |
|
1848 { |
|
1849 for (int j = 0; j < nc; j++) |
|
1850 { |
|
1851 double prod = elem (0, j); |
|
1852 for (int i = 0; i < nr; i++) |
|
1853 { |
|
1854 retval.elem (i, j) = prod; |
|
1855 if (i < nr - 1) |
|
1856 prod *= elem (i+1, j); |
|
1857 } |
|
1858 } |
|
1859 } |
|
1860 } |
|
1861 return retval; |
|
1862 } |
|
1863 |
|
1864 Matrix |
|
1865 Matrix::cumsum (void) const |
|
1866 { |
|
1867 Matrix retval; |
|
1868 |
|
1869 int nr = rows (); |
|
1870 int nc = cols (); |
|
1871 |
|
1872 if (nr == 1) |
|
1873 { |
|
1874 retval.resize (1, nc); |
|
1875 if (nc > 0) |
|
1876 { |
|
1877 double sum = elem (0, 0); |
|
1878 for (int j = 0; j < nc; j++) |
|
1879 { |
|
1880 retval.elem (0, j) = sum; |
|
1881 if (j < nc - 1) |
|
1882 sum += elem (0, j+1); |
|
1883 } |
|
1884 } |
|
1885 } |
|
1886 else if (nc == 1) |
|
1887 { |
|
1888 retval.resize (nr, 1); |
|
1889 if (nr > 0) |
|
1890 { |
|
1891 double sum = elem (0, 0); |
|
1892 for (int i = 0; i < nr; i++) |
|
1893 { |
|
1894 retval.elem (i, 0) = sum; |
|
1895 if (i < nr - 1) |
|
1896 sum += elem (i+1, 0); |
|
1897 } |
|
1898 } |
|
1899 } |
|
1900 else |
|
1901 { |
|
1902 retval.resize (nr, nc); |
|
1903 if (nr > 0 && nc > 0) |
|
1904 { |
|
1905 for (int j = 0; j < nc; j++) |
|
1906 { |
|
1907 double sum = elem (0, j); |
|
1908 for (int i = 0; i < nr; i++) |
|
1909 { |
|
1910 retval.elem (i, j) = sum; |
|
1911 if (i < nr - 1) |
|
1912 sum += elem (i+1, j); |
|
1913 } |
|
1914 } |
|
1915 } |
|
1916 } |
|
1917 return retval; |
|
1918 } |
|
1919 |
|
1920 Matrix |
|
1921 Matrix::prod (void) const |
|
1922 { |
|
1923 Matrix retval; |
|
1924 |
|
1925 int nr = rows (); |
|
1926 int nc = cols (); |
|
1927 |
|
1928 if (nr == 1) |
|
1929 { |
|
1930 retval.resize (1, 1); |
|
1931 retval.elem (0, 0) = 1.0; |
|
1932 for (int j = 0; j < nc; j++) |
|
1933 retval.elem (0, 0) *= elem (0, j); |
|
1934 } |
|
1935 else if (nc == 1) |
|
1936 { |
|
1937 retval.resize (1, 1); |
|
1938 retval.elem (0, 0) = 1.0; |
|
1939 for (int i = 0; i < nr; i++) |
|
1940 retval.elem (0, 0) *= elem (i, 0); |
|
1941 } |
|
1942 else |
|
1943 { |
|
1944 if (nc == 0) |
|
1945 { |
|
1946 retval.resize (1, 1); |
|
1947 retval.elem (0, 0) = 1.0; |
|
1948 } |
|
1949 else |
|
1950 retval.resize (1, nc); |
|
1951 |
|
1952 for (int j = 0; j < nc; j++) |
|
1953 { |
|
1954 retval.elem (0, j) = 1.0; |
|
1955 for (int i = 0; i < nr; i++) |
|
1956 retval.elem (0, j) *= elem (i, j); |
|
1957 } |
|
1958 } |
|
1959 return retval; |
|
1960 } |
|
1961 |
|
1962 Matrix |
|
1963 Matrix::sum (void) const |
|
1964 { |
|
1965 Matrix retval; |
|
1966 |
|
1967 int nr = rows (); |
|
1968 int nc = cols (); |
|
1969 |
|
1970 if (nr == 1) |
|
1971 { |
|
1972 retval.resize (1, 1); |
|
1973 retval.elem (0, 0) = 0.0; |
|
1974 for (int j = 0; j < nc; j++) |
|
1975 retval.elem (0, 0) += elem (0, j); |
|
1976 } |
|
1977 else if (nc == 1) |
|
1978 { |
|
1979 retval.resize (1, 1); |
|
1980 retval.elem (0, 0) = 0.0; |
|
1981 for (int i = 0; i < nr; i++) |
|
1982 retval.elem (0, 0) += elem (i, 0); |
|
1983 } |
|
1984 else |
|
1985 { |
|
1986 if (nc == 0) |
|
1987 { |
|
1988 retval.resize (1, 1); |
|
1989 retval.elem (0, 0) = 0.0; |
|
1990 } |
|
1991 else |
|
1992 retval.resize (1, nc); |
|
1993 |
|
1994 for (int j = 0; j < nc; j++) |
|
1995 { |
|
1996 retval.elem (0, j) = 0.0; |
|
1997 for (int i = 0; i < nr; i++) |
|
1998 retval.elem (0, j) += elem (i, j); |
|
1999 } |
|
2000 } |
|
2001 return retval; |
|
2002 } |
|
2003 |
|
2004 Matrix |
|
2005 Matrix::sumsq (void) const |
|
2006 { |
|
2007 Matrix retval; |
|
2008 |
|
2009 int nr = rows (); |
|
2010 int nc = cols (); |
|
2011 |
|
2012 if (nr == 1) |
|
2013 { |
|
2014 retval.resize (1, 1); |
|
2015 retval.elem (0, 0) = 0.0; |
|
2016 for (int j = 0; j < nc; j++) |
|
2017 { |
|
2018 double d = elem (0, j); |
|
2019 retval.elem (0, 0) += d * d; |
|
2020 } |
|
2021 } |
|
2022 else if (nc == 1) |
|
2023 { |
|
2024 retval.resize (1, 1); |
|
2025 retval.elem (0, 0) = 0.0; |
|
2026 for (int i = 0; i < nr; i++) |
|
2027 { |
|
2028 double d = elem (i, 0); |
|
2029 retval.elem (0, 0) += d * d; |
|
2030 } |
|
2031 } |
|
2032 else |
|
2033 { |
|
2034 retval.resize (1, nc); |
|
2035 for (int j = 0; j < nc; j++) |
|
2036 { |
|
2037 retval.elem (0, j) = 0.0; |
|
2038 for (int i = 0; i < nr; i++) |
|
2039 { |
|
2040 double d = elem (i, j); |
|
2041 retval.elem (0, j) += d * d; |
|
2042 } |
|
2043 } |
|
2044 } |
|
2045 return retval; |
|
2046 } |
|
2047 |
|
2048 ColumnVector |
|
2049 Matrix::diag (void) const |
|
2050 { |
|
2051 return diag (0); |
|
2052 } |
|
2053 |
|
2054 ColumnVector |
|
2055 Matrix::diag (int k) const |
|
2056 { |
|
2057 int nnr = rows (); |
|
2058 int nnc = cols (); |
|
2059 if (k > 0) |
|
2060 nnc -= k; |
|
2061 else if (k < 0) |
|
2062 nnr += k; |
|
2063 |
|
2064 ColumnVector d; |
|
2065 |
|
2066 if (nnr > 0 && nnc > 0) |
|
2067 { |
|
2068 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
2069 |
|
2070 d.resize (ndiag); |
|
2071 |
|
2072 if (k > 0) |
|
2073 { |
|
2074 for (int i = 0; i < ndiag; i++) |
|
2075 d.elem (i) = elem (i, i+k); |
|
2076 } |
|
2077 else if ( k < 0) |
|
2078 { |
|
2079 for (int i = 0; i < ndiag; i++) |
|
2080 d.elem (i) = elem (i-k, i); |
|
2081 } |
|
2082 else |
|
2083 { |
|
2084 for (int i = 0; i < ndiag; i++) |
|
2085 d.elem (i) = elem (i, i); |
|
2086 } |
|
2087 } |
|
2088 else |
|
2089 cerr << "diag: requested diagonal out of range\n"; |
|
2090 |
|
2091 return d; |
|
2092 } |
|
2093 |
|
2094 ColumnVector |
|
2095 Matrix::row_min (void) const |
|
2096 { |
|
2097 ColumnVector result; |
|
2098 |
|
2099 int nr = rows (); |
|
2100 int nc = cols (); |
|
2101 |
|
2102 if (nr > 0 && nc > 0) |
|
2103 { |
|
2104 result.resize (nr); |
|
2105 |
|
2106 for (int i = 0; i < nr; i++) |
|
2107 { |
|
2108 double res = elem (i, 0); |
|
2109 for (int j = 1; j < nc; j++) |
|
2110 if (elem (i, j) < res) |
|
2111 res = elem (i, j); |
|
2112 result.elem (i) = res; |
|
2113 } |
|
2114 } |
|
2115 |
|
2116 return result; |
|
2117 } |
|
2118 |
|
2119 ColumnVector |
|
2120 Matrix::row_min_loc (void) const |
|
2121 { |
|
2122 ColumnVector result; |
|
2123 |
|
2124 int nr = rows (); |
|
2125 int nc = cols (); |
|
2126 |
|
2127 if (nr > 0 && nc > 0) |
|
2128 { |
|
2129 result.resize (nr); |
|
2130 |
|
2131 for (int i = 0; i < nr; i++) |
|
2132 { |
|
2133 int res = 0; |
|
2134 for (int j = 0; j < nc; j++) |
|
2135 if (elem (i, j) < elem (i, res)) |
|
2136 res = j; |
|
2137 result.elem (i) = (double) (res + 1); |
|
2138 } |
|
2139 } |
|
2140 |
|
2141 return result; |
|
2142 } |
|
2143 |
|
2144 ColumnVector |
|
2145 Matrix::row_max (void) const |
|
2146 { |
|
2147 ColumnVector result; |
|
2148 |
|
2149 int nr = rows (); |
|
2150 int nc = cols (); |
|
2151 |
|
2152 if (nr > 0 && nc > 0) |
|
2153 { |
|
2154 result.resize (nr); |
|
2155 |
|
2156 for (int i = 0; i < nr; i++) |
|
2157 { |
|
2158 double res = elem (i, 0); |
|
2159 for (int j = 1; j < nc; j++) |
|
2160 if (elem (i, j) > res) |
|
2161 res = elem (i, j); |
|
2162 result.elem (i) = res; |
|
2163 } |
|
2164 } |
|
2165 |
|
2166 return result; |
|
2167 } |
|
2168 |
|
2169 ColumnVector |
|
2170 Matrix::row_max_loc (void) const |
|
2171 { |
|
2172 ColumnVector result; |
|
2173 |
|
2174 int nr = rows (); |
|
2175 int nc = cols (); |
|
2176 |
|
2177 if (nr > 0 && nc > 0) |
|
2178 { |
|
2179 result.resize (nr); |
|
2180 |
|
2181 for (int i = 0; i < nr; i++) |
|
2182 { |
|
2183 int res = 0; |
|
2184 for (int j = 0; j < nc; j++) |
|
2185 if (elem (i, j) > elem (i, res)) |
|
2186 res = j; |
|
2187 result.elem (i) = (double) (res + 1); |
|
2188 } |
|
2189 } |
|
2190 |
|
2191 return result; |
|
2192 } |
|
2193 |
|
2194 RowVector |
|
2195 Matrix::column_min (void) const |
|
2196 { |
|
2197 RowVector result; |
|
2198 |
|
2199 int nr = rows (); |
|
2200 int nc = cols (); |
|
2201 |
|
2202 if (nr > 0 && nc > 0) |
|
2203 { |
|
2204 result.resize (nc); |
|
2205 |
|
2206 for (int j = 0; j < nc; j++) |
|
2207 { |
|
2208 double res = elem (0, j); |
|
2209 for (int i = 1; i < nr; i++) |
|
2210 if (elem (i, j) < res) |
|
2211 res = elem (i, j); |
|
2212 result.elem (j) = res; |
|
2213 } |
|
2214 } |
|
2215 |
|
2216 return result; |
|
2217 } |
|
2218 RowVector |
|
2219 Matrix::column_min_loc (void) const |
|
2220 { |
|
2221 RowVector result; |
|
2222 |
|
2223 int nr = rows (); |
|
2224 int nc = cols (); |
|
2225 |
|
2226 if (nr > 0 && nc > 0) |
|
2227 { |
|
2228 result.resize (nc); |
|
2229 |
|
2230 for (int j = 0; j < nc; j++) |
|
2231 { |
|
2232 int res = 0; |
|
2233 for (int i = 0; i < nr; i++) |
|
2234 if (elem (i, j) < elem (res, j)) |
|
2235 res = i; |
|
2236 result.elem (j) = (double) (res + 1); |
|
2237 } |
|
2238 } |
|
2239 |
|
2240 return result; |
|
2241 } |
|
2242 |
|
2243 |
|
2244 RowVector |
|
2245 Matrix::column_max (void) const |
|
2246 { |
|
2247 RowVector result; |
|
2248 |
|
2249 int nr = rows (); |
|
2250 int nc = cols (); |
|
2251 |
|
2252 if (nr > 0 && nc > 0) |
|
2253 { |
|
2254 result.resize (nc); |
|
2255 |
|
2256 for (int j = 0; j < nc; j++) |
|
2257 { |
|
2258 double res = elem (0, j); |
|
2259 for (int i = 1; i < nr; i++) |
|
2260 if (elem (i, j) > res) |
|
2261 res = elem (i, j); |
|
2262 result.elem (j) = res; |
|
2263 } |
|
2264 } |
|
2265 |
|
2266 return result; |
|
2267 } |
|
2268 |
|
2269 RowVector |
|
2270 Matrix::column_max_loc (void) const |
|
2271 { |
|
2272 RowVector result; |
|
2273 |
|
2274 int nr = rows (); |
|
2275 int nc = cols (); |
|
2276 |
|
2277 if (nr > 0 && nc > 0) |
|
2278 { |
|
2279 result.resize (nc); |
|
2280 |
|
2281 for (int j = 0; j < nc; j++) |
|
2282 { |
|
2283 int res = 0; |
|
2284 for (int i = 0; i < nr; i++) |
|
2285 if (elem (i, j) > elem (res, j)) |
|
2286 res = i; |
|
2287 result.elem (j) = (double) (res + 1); |
|
2288 } |
|
2289 } |
|
2290 |
|
2291 return result; |
|
2292 } |
|
2293 |
|
2294 ostream& |
|
2295 operator << (ostream& os, const Matrix& a) |
|
2296 { |
|
2297 // int field_width = os.precision () + 7; |
1360
|
2298 |
458
|
2299 for (int i = 0; i < a.rows (); i++) |
|
2300 { |
|
2301 for (int j = 0; j < a.cols (); j++) |
|
2302 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
2303 os << "\n"; |
|
2304 } |
|
2305 return os; |
|
2306 } |
|
2307 |
|
2308 istream& |
|
2309 operator >> (istream& is, Matrix& a) |
|
2310 { |
|
2311 int nr = a.rows (); |
|
2312 int nc = a.cols (); |
|
2313 |
|
2314 if (nr < 1 || nc < 1) |
|
2315 is.clear (ios::badbit); |
|
2316 else |
|
2317 { |
|
2318 double tmp; |
|
2319 for (int i = 0; i < nr; i++) |
|
2320 for (int j = 0; j < nc; j++) |
|
2321 { |
|
2322 is >> tmp; |
|
2323 if (is) |
|
2324 a.elem (i, j) = tmp; |
|
2325 else |
|
2326 break; |
|
2327 } |
|
2328 } |
|
2329 |
|
2330 return is; |
|
2331 } |
|
2332 |
1365
|
2333 // Read an array of data from a file in binary format. |
1360
|
2334 |
458
|
2335 int |
1365
|
2336 Matrix::read (FILE *fptr, const char *type) |
458
|
2337 { |
1360
|
2338 // Allocate buffer pointers. |
458
|
2339 |
|
2340 union |
|
2341 { |
|
2342 void *vd; |
|
2343 char *ch; |
|
2344 u_char *uc; |
|
2345 short *sh; |
|
2346 u_short *us; |
|
2347 int *in; |
|
2348 u_int *ui; |
|
2349 long *ln; |
|
2350 u_long *ul; |
|
2351 float *fl; |
|
2352 double *db; |
|
2353 } |
|
2354 buf; |
|
2355 |
1360
|
2356 // Convert data to double. |
458
|
2357 |
471
|
2358 if (! type) |
458
|
2359 { |
471
|
2360 (*current_liboctave_error_handler) |
|
2361 ("fread: invalid NULL type parameter"); |
|
2362 return 0; |
|
2363 } |
458
|
2364 |
471
|
2365 int count; |
|
2366 int nitems = length (); |
458
|
2367 |
471
|
2368 double *d = fortran_vec (); // Ensures only one reference to my privates! |
458
|
2369 |
471
|
2370 #define DO_FREAD(TYPE,ELEM) \ |
|
2371 do \ |
|
2372 { \ |
|
2373 size_t size = sizeof (TYPE); \ |
|
2374 buf.ch = new char [size * nitems]; \ |
|
2375 count = fread (buf.ch, size, nitems, fptr); \ |
|
2376 for (int k = 0; k < count; k++) \ |
|
2377 d[k] = buf.ELEM[k]; \ |
|
2378 delete [] buf.ch; \ |
|
2379 } \ |
|
2380 while (0) |
458
|
2381 |
471
|
2382 if (strcasecmp (type, "double") == 0) |
|
2383 DO_FREAD (double, db); |
|
2384 else if (strcasecmp (type, "char") == 0) |
|
2385 DO_FREAD (char, ch); |
|
2386 else if (strcasecmp (type, "uchar") == 0) |
|
2387 DO_FREAD (u_char, uc); |
|
2388 else if (strcasecmp (type, "short") == 0) |
|
2389 DO_FREAD (short, sh); |
|
2390 else if (strcasecmp (type, "ushort") == 0) |
|
2391 DO_FREAD (u_short, us); |
|
2392 else if (strcasecmp (type, "int") == 0) |
|
2393 DO_FREAD (int, in); |
|
2394 else if (strcasecmp (type, "uint") == 0) |
|
2395 DO_FREAD (u_int, ui); |
|
2396 else if (strcasecmp (type, "long") == 0) |
|
2397 DO_FREAD (long, ul); |
|
2398 else if (strcasecmp (type, "float") == 0) |
|
2399 DO_FREAD (float, fl); |
|
2400 else |
|
2401 { |
|
2402 (*current_liboctave_error_handler) |
|
2403 ("fread: invalid NULL type parameter"); |
458
|
2404 return 0; |
|
2405 } |
|
2406 |
|
2407 return count; |
|
2408 } |
|
2409 |
1360
|
2410 // Write the data array to a file in binary format. |
|
2411 |
458
|
2412 int |
1365
|
2413 Matrix::write (FILE *fptr, const char *type) |
458
|
2414 { |
1360
|
2415 // Allocate buffer pointers. |
458
|
2416 |
|
2417 union |
|
2418 { |
|
2419 void *vd; |
|
2420 char *ch; |
|
2421 u_char *uc; |
|
2422 short *sh; |
|
2423 u_short *us; |
|
2424 int *in; |
|
2425 u_int *ui; |
|
2426 long *ln; |
|
2427 u_long *ul; |
|
2428 float *fl; |
|
2429 double *db; |
|
2430 } |
|
2431 buf; |
|
2432 |
471
|
2433 int nitems = length (); |
458
|
2434 |
471
|
2435 double *d = fortran_vec (); |
458
|
2436 |
1360
|
2437 // Convert from double to correct size. |
458
|
2438 |
471
|
2439 if (! type) |
458
|
2440 { |
471
|
2441 (*current_liboctave_error_handler) |
|
2442 ("fwrite: invalid NULL type parameter"); |
|
2443 return 0; |
|
2444 } |
458
|
2445 |
471
|
2446 size_t size; |
|
2447 int count; |
458
|
2448 |
471
|
2449 #define DO_FWRITE(TYPE,ELEM) \ |
|
2450 do \ |
|
2451 { \ |
|
2452 size = sizeof (TYPE); \ |
|
2453 buf.ELEM = new TYPE [nitems]; \ |
|
2454 for (int k = 0; k < nitems; k++) \ |
|
2455 buf.ELEM[k] = (TYPE) d[k]; \ |
|
2456 count = fwrite (buf.ELEM, size, nitems, fptr); \ |
|
2457 delete [] buf.ELEM; \ |
|
2458 } \ |
|
2459 while (0) |
458
|
2460 |
471
|
2461 if (strcasecmp (type, "double") == 0) |
|
2462 DO_FWRITE (double, db); |
|
2463 else if (strcasecmp (type, "char") == 0) |
|
2464 DO_FWRITE (char, ch); |
|
2465 else if (strcasecmp (type, "uchar") == 0) |
|
2466 DO_FWRITE (u_char, uc); |
|
2467 else if (strcasecmp (type, "short") == 0) |
|
2468 DO_FWRITE (short, sh); |
|
2469 else if (strcasecmp (type, "ushort") == 0) |
|
2470 DO_FWRITE (u_short, us); |
|
2471 else if (strcasecmp (type, "int") == 0) |
|
2472 DO_FWRITE (int, in); |
|
2473 else if (strcasecmp (type, "uint") == 0) |
|
2474 DO_FWRITE (u_int, ui); |
|
2475 else if (strcasecmp (type, "long") == 0) |
|
2476 DO_FWRITE (long, ln); |
|
2477 else if (strcasecmp (type, "ulong") == 0) |
|
2478 DO_FWRITE (u_long, ul); |
|
2479 else if (strcasecmp (type, "float") == 0) |
|
2480 DO_FWRITE (float, fl); |
|
2481 else |
|
2482 { |
|
2483 (*current_liboctave_error_handler) |
|
2484 ("fwrite: unrecognized type parameter %s", type); |
458
|
2485 return 0; |
471
|
2486 } |
458
|
2487 |
|
2488 return count; |
|
2489 } |
|
2490 |
1819
|
2491 Matrix |
|
2492 Givens (double x, double y) |
|
2493 { |
|
2494 double cc, s, temp_r; |
|
2495 |
|
2496 F77_FCN (dlartg, DLARTG) (x, y, cc, s, temp_r); |
|
2497 |
|
2498 Matrix g (2, 2); |
|
2499 |
|
2500 g.elem (0, 0) = cc; |
|
2501 g.elem (1, 1) = cc; |
|
2502 g.elem (0, 1) = s; |
|
2503 g.elem (1, 0) = -s; |
|
2504 |
|
2505 return g; |
|
2506 } |
|
2507 |
|
2508 Matrix |
|
2509 Sylvester (const Matrix& a, const Matrix& b, const Matrix& c) |
|
2510 { |
|
2511 Matrix retval; |
|
2512 |
|
2513 // XXX FIXME XXX -- need to check that a, b, and c are all the same |
|
2514 // size. |
|
2515 |
|
2516 // Compute Schur decompositions. |
|
2517 |
|
2518 SCHUR as (a, "U"); |
|
2519 SCHUR bs (b, "U"); |
|
2520 |
|
2521 // Transform c to new coordinates. |
|
2522 |
|
2523 Matrix ua = as.unitary_matrix (); |
|
2524 Matrix sch_a = as.schur_matrix (); |
|
2525 |
|
2526 Matrix ub = bs.unitary_matrix (); |
|
2527 Matrix sch_b = bs.schur_matrix (); |
|
2528 |
|
2529 Matrix cx = ua.transpose () * c * ub; |
|
2530 |
|
2531 // Solve the sylvester equation, back-transform, and return the |
|
2532 // solution. |
|
2533 |
|
2534 int a_nr = a.rows (); |
|
2535 int b_nr = b.rows (); |
|
2536 |
|
2537 double scale; |
|
2538 int info; |
|
2539 |
|
2540 F77_FCN (dtrsyl, DTRSYL) ("N", "N", 1, a_nr, b_nr, |
|
2541 sch_a.fortran_vec (), a_nr, |
|
2542 sch_b.fortran_vec (), b_nr, |
|
2543 cx.fortran_vec (), a_nr, scale, |
|
2544 info, 1L, 1L); |
|
2545 |
|
2546 |
|
2547 // XXX FIXME XXX -- check info? |
|
2548 |
|
2549 retval = -ua*cx*ub.transpose (); |
|
2550 |
|
2551 return retval; |
|
2552 } |
|
2553 |
458
|
2554 /* |
|
2555 ;;; Local Variables: *** |
|
2556 ;;; mode: C++ *** |
|
2557 ;;; page-delimiter: "^/\\*" *** |
|
2558 ;;; End: *** |
|
2559 */ |