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