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