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