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