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