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