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