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 |
1360
|
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. |
458
|
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 |
1360
|
84 // Matrix class. |
458
|
85 |
|
86 Matrix::Matrix (const DiagMatrix& a) |
1214
|
87 : MArray2<double> (a.rows (), a.cols (), 0.0) |
458
|
88 { |
|
89 for (int i = 0; i < a.length (); i++) |
|
90 elem (i, i) = a.elem (i, i); |
|
91 } |
|
92 |
|
93 int |
|
94 Matrix::operator == (const Matrix& a) const |
|
95 { |
|
96 if (rows () != a.rows () || cols () != a.cols ()) |
|
97 return 0; |
|
98 |
|
99 return equal (data (), a.data (), length ()); |
|
100 } |
|
101 |
|
102 int |
|
103 Matrix::operator != (const Matrix& a) const |
|
104 { |
|
105 return !(*this == a); |
|
106 } |
|
107 |
|
108 Matrix& |
|
109 Matrix::insert (const Matrix& a, int r, int c) |
|
110 { |
|
111 int a_rows = a.rows (); |
|
112 int a_cols = a.cols (); |
|
113 if (r < 0 || r + a_rows - 1 > rows () |
|
114 || c < 0 || c + a_cols - 1 > cols ()) |
|
115 { |
|
116 (*current_liboctave_error_handler) ("range error for insert"); |
|
117 return *this; |
|
118 } |
|
119 |
|
120 for (int j = 0; j < a_cols; j++) |
|
121 for (int i = 0; i < a_rows; i++) |
|
122 elem (r+i, c+j) = a.elem (i, j); |
|
123 |
|
124 return *this; |
|
125 } |
|
126 |
|
127 Matrix& |
|
128 Matrix::insert (const RowVector& a, int r, int c) |
|
129 { |
|
130 int a_len = a.length (); |
|
131 if (r < 0 || r >= rows () || c < 0 || c + a_len - 1 > cols ()) |
|
132 { |
|
133 (*current_liboctave_error_handler) ("range error for insert"); |
|
134 return *this; |
|
135 } |
|
136 |
|
137 for (int i = 0; i < a_len; i++) |
|
138 elem (r, c+i) = a.elem (i); |
|
139 |
|
140 return *this; |
|
141 } |
|
142 |
|
143 Matrix& |
|
144 Matrix::insert (const ColumnVector& a, int r, int c) |
|
145 { |
|
146 int a_len = a.length (); |
|
147 if (r < 0 || r + a_len - 1 > rows () || c < 0 || c >= cols ()) |
|
148 { |
|
149 (*current_liboctave_error_handler) ("range error for insert"); |
|
150 return *this; |
|
151 } |
|
152 |
|
153 for (int i = 0; i < a_len; i++) |
|
154 elem (r+i, c) = a.elem (i); |
|
155 |
|
156 return *this; |
|
157 } |
|
158 |
|
159 Matrix& |
|
160 Matrix::insert (const DiagMatrix& a, int r, int c) |
|
161 { |
|
162 if (r < 0 || r + a.rows () - 1 > rows () |
|
163 || c < 0 || c + a.cols () - 1 > cols ()) |
|
164 { |
|
165 (*current_liboctave_error_handler) ("range error for insert"); |
|
166 return *this; |
|
167 } |
|
168 |
|
169 for (int i = 0; i < a.length (); i++) |
|
170 elem (r+i, c+i) = a.elem (i, i); |
|
171 |
|
172 return *this; |
|
173 } |
|
174 |
|
175 Matrix& |
|
176 Matrix::fill (double val) |
|
177 { |
|
178 int nr = rows (); |
|
179 int nc = cols (); |
|
180 if (nr > 0 && nc > 0) |
|
181 for (int j = 0; j < nc; j++) |
|
182 for (int i = 0; i < nr; i++) |
|
183 elem (i, j) = val; |
|
184 |
|
185 return *this; |
|
186 } |
|
187 |
|
188 Matrix& |
|
189 Matrix::fill (double val, int r1, int c1, int r2, int c2) |
|
190 { |
|
191 int nr = rows (); |
|
192 int nc = cols (); |
|
193 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
|
194 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) |
|
195 { |
|
196 (*current_liboctave_error_handler) ("range error for fill"); |
|
197 return *this; |
|
198 } |
|
199 |
|
200 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
201 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
202 |
|
203 for (int j = c1; j <= c2; j++) |
|
204 for (int i = r1; i <= r2; i++) |
|
205 elem (i, j) = val; |
|
206 |
|
207 return *this; |
|
208 } |
|
209 |
|
210 Matrix |
|
211 Matrix::append (const Matrix& a) const |
|
212 { |
|
213 int nr = rows (); |
|
214 int nc = cols (); |
|
215 if (nr != a.rows ()) |
|
216 { |
|
217 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
218 return Matrix (); |
|
219 } |
|
220 |
|
221 int nc_insert = nc; |
|
222 Matrix retval (nr, nc + a.cols ()); |
|
223 retval.insert (*this, 0, 0); |
|
224 retval.insert (a, 0, nc_insert); |
|
225 return retval; |
|
226 } |
|
227 |
|
228 Matrix |
|
229 Matrix::append (const RowVector& a) const |
|
230 { |
|
231 int nr = rows (); |
|
232 int nc = cols (); |
|
233 if (nr != 1) |
|
234 { |
|
235 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
236 return Matrix (); |
|
237 } |
|
238 |
|
239 int nc_insert = nc; |
|
240 Matrix retval (nr, nc + a.length ()); |
|
241 retval.insert (*this, 0, 0); |
|
242 retval.insert (a, 0, nc_insert); |
|
243 return retval; |
|
244 } |
|
245 |
|
246 Matrix |
|
247 Matrix::append (const ColumnVector& a) const |
|
248 { |
|
249 int nr = rows (); |
|
250 int nc = cols (); |
|
251 if (nr != a.length ()) |
|
252 { |
|
253 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
254 return Matrix (); |
|
255 } |
|
256 |
|
257 int nc_insert = nc; |
|
258 Matrix retval (nr, nc + 1); |
|
259 retval.insert (*this, 0, 0); |
|
260 retval.insert (a, 0, nc_insert); |
|
261 return retval; |
|
262 } |
|
263 |
|
264 Matrix |
|
265 Matrix::append (const DiagMatrix& a) const |
|
266 { |
|
267 int nr = rows (); |
|
268 int nc = cols (); |
|
269 if (nr != a.rows ()) |
|
270 { |
|
271 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
272 return *this; |
|
273 } |
|
274 |
|
275 int nc_insert = nc; |
|
276 Matrix retval (nr, nc + a.cols ()); |
|
277 retval.insert (*this, 0, 0); |
|
278 retval.insert (a, 0, nc_insert); |
|
279 return retval; |
|
280 } |
|
281 |
|
282 Matrix |
|
283 Matrix::stack (const Matrix& a) const |
|
284 { |
|
285 int nr = rows (); |
|
286 int nc = cols (); |
|
287 if (nc != a.cols ()) |
|
288 { |
|
289 (*current_liboctave_error_handler) |
|
290 ("column dimension mismatch for stack"); |
|
291 return Matrix (); |
|
292 } |
|
293 |
|
294 int nr_insert = nr; |
|
295 Matrix retval (nr + a.rows (), nc); |
|
296 retval.insert (*this, 0, 0); |
|
297 retval.insert (a, nr_insert, 0); |
|
298 return retval; |
|
299 } |
|
300 |
|
301 Matrix |
|
302 Matrix::stack (const RowVector& a) const |
|
303 { |
|
304 int nr = rows (); |
|
305 int nc = cols (); |
|
306 if (nc != a.length ()) |
|
307 { |
|
308 (*current_liboctave_error_handler) |
|
309 ("column dimension mismatch for stack"); |
|
310 return Matrix (); |
|
311 } |
|
312 |
|
313 int nr_insert = nr; |
|
314 Matrix retval (nr + 1, nc); |
|
315 retval.insert (*this, 0, 0); |
|
316 retval.insert (a, nr_insert, 0); |
|
317 return retval; |
|
318 } |
|
319 |
|
320 Matrix |
|
321 Matrix::stack (const ColumnVector& a) const |
|
322 { |
|
323 int nr = rows (); |
|
324 int nc = cols (); |
|
325 if (nc != 1) |
|
326 { |
|
327 (*current_liboctave_error_handler) |
|
328 ("column dimension mismatch for stack"); |
|
329 return Matrix (); |
|
330 } |
|
331 |
|
332 int nr_insert = nr; |
|
333 Matrix retval (nr + a.length (), nc); |
|
334 retval.insert (*this, 0, 0); |
|
335 retval.insert (a, nr_insert, 0); |
|
336 return retval; |
|
337 } |
|
338 |
|
339 Matrix |
|
340 Matrix::stack (const DiagMatrix& a) const |
|
341 { |
|
342 int nr = rows (); |
|
343 int nc = cols (); |
|
344 if (nc != a.cols ()) |
|
345 { |
|
346 (*current_liboctave_error_handler) |
|
347 ("column dimension mismatch for stack"); |
|
348 return Matrix (); |
|
349 } |
|
350 |
|
351 int nr_insert = nr; |
|
352 Matrix retval (nr + a.rows (), nc); |
|
353 retval.insert (*this, 0, 0); |
|
354 retval.insert (a, nr_insert, 0); |
|
355 return retval; |
|
356 } |
|
357 |
|
358 Matrix |
|
359 Matrix::transpose (void) const |
|
360 { |
|
361 int nr = rows (); |
|
362 int nc = cols (); |
|
363 Matrix result (nc, nr); |
|
364 if (length () > 0) |
|
365 { |
|
366 for (int j = 0; j < nc; j++) |
|
367 for (int i = 0; i < nr; i++) |
|
368 result.elem (j, i) = elem (i, j); |
|
369 } |
|
370 return result; |
|
371 } |
|
372 |
|
373 Matrix |
1205
|
374 real (const ComplexMatrix& a) |
|
375 { |
|
376 int a_len = a.length (); |
|
377 Matrix retval; |
|
378 if (a_len > 0) |
|
379 retval = Matrix (real_dup (a.data (), a_len), a.rows (), a.cols ()); |
|
380 return retval; |
|
381 } |
|
382 |
|
383 Matrix |
|
384 imag (const ComplexMatrix& a) |
|
385 { |
|
386 int a_len = a.length (); |
|
387 Matrix retval; |
|
388 if (a_len > 0) |
|
389 retval = Matrix (imag_dup (a.data (), a_len), a.rows (), a.cols ()); |
|
390 return retval; |
|
391 } |
|
392 |
|
393 Matrix |
458
|
394 Matrix::extract (int r1, int c1, int r2, int c2) const |
|
395 { |
|
396 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
397 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
398 |
|
399 int new_r = r2 - r1 + 1; |
|
400 int new_c = c2 - c1 + 1; |
|
401 |
|
402 Matrix result (new_r, new_c); |
|
403 |
|
404 for (int j = 0; j < new_c; j++) |
|
405 for (int i = 0; i < new_r; i++) |
|
406 result.elem (i, j) = elem (r1+i, c1+j); |
|
407 |
|
408 return result; |
|
409 } |
|
410 |
|
411 // extract row or column i. |
|
412 |
|
413 RowVector |
|
414 Matrix::row (int i) const |
|
415 { |
|
416 int nc = cols (); |
|
417 if (i < 0 || i >= rows ()) |
|
418 { |
|
419 (*current_liboctave_error_handler) ("invalid row selection"); |
|
420 return RowVector (); |
|
421 } |
|
422 |
|
423 RowVector retval (nc); |
|
424 for (int j = 0; j < nc; j++) |
|
425 retval.elem (j) = elem (i, j); |
|
426 |
|
427 return retval; |
|
428 } |
|
429 |
|
430 RowVector |
|
431 Matrix::row (char *s) const |
|
432 { |
533
|
433 if (! s) |
458
|
434 { |
|
435 (*current_liboctave_error_handler) ("invalid row selection"); |
|
436 return RowVector (); |
|
437 } |
|
438 |
|
439 char c = *s; |
|
440 if (c == 'f' || c == 'F') |
|
441 return row (0); |
|
442 else if (c == 'l' || c == 'L') |
|
443 return row (rows () - 1); |
|
444 else |
|
445 { |
|
446 (*current_liboctave_error_handler) ("invalid row selection"); |
|
447 return RowVector (); |
|
448 } |
|
449 } |
|
450 |
|
451 ColumnVector |
|
452 Matrix::column (int i) const |
|
453 { |
|
454 int nr = rows (); |
|
455 if (i < 0 || i >= cols ()) |
|
456 { |
|
457 (*current_liboctave_error_handler) ("invalid column selection"); |
|
458 return ColumnVector (); |
|
459 } |
|
460 |
|
461 ColumnVector retval (nr); |
|
462 for (int j = 0; j < nr; j++) |
|
463 retval.elem (j) = elem (j, i); |
|
464 |
|
465 return retval; |
|
466 } |
|
467 |
|
468 ColumnVector |
|
469 Matrix::column (char *s) const |
|
470 { |
533
|
471 if (! s) |
458
|
472 { |
|
473 (*current_liboctave_error_handler) ("invalid column selection"); |
|
474 return ColumnVector (); |
|
475 } |
|
476 |
|
477 char c = *s; |
|
478 if (c == 'f' || c == 'F') |
|
479 return column (0); |
|
480 else if (c == 'l' || c == 'L') |
|
481 return column (cols () - 1); |
|
482 else |
|
483 { |
|
484 (*current_liboctave_error_handler) ("invalid column selection"); |
|
485 return ColumnVector (); |
|
486 } |
|
487 } |
|
488 |
|
489 Matrix |
|
490 Matrix::inverse (void) const |
|
491 { |
|
492 int info; |
|
493 double rcond; |
|
494 return inverse (info, rcond); |
|
495 } |
|
496 |
|
497 Matrix |
|
498 Matrix::inverse (int& info) const |
|
499 { |
|
500 double rcond; |
|
501 return inverse (info, rcond); |
|
502 } |
|
503 |
|
504 Matrix |
532
|
505 Matrix::inverse (int& info, double& rcond) const |
458
|
506 { |
|
507 int nr = rows (); |
|
508 int nc = cols (); |
|
509 int len = length (); |
|
510 if (nr != nc || nr == 0 || nc == 0) |
|
511 { |
|
512 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
513 return Matrix (); |
|
514 } |
|
515 |
|
516 info = 0; |
|
517 |
|
518 int *ipvt = new int [nr]; |
|
519 double *z = new double [nr]; |
|
520 double *tmp_data = dup (data (), len); |
|
521 |
1253
|
522 F77_FCN (dgeco, DGECO) (tmp_data, nr, nc, ipvt, rcond, z); |
458
|
523 |
1195
|
524 volatile double rcond_plus_one = rcond + 1.0; |
|
525 if (rcond_plus_one == 1.0) |
458
|
526 { |
|
527 info = -1; |
|
528 copy (tmp_data, data (), len); // Restore matrix contents. |
|
529 } |
|
530 else |
|
531 { |
1365
|
532 double *dummy = 0; |
458
|
533 |
1253
|
534 F77_FCN (dgedi, DGEDI) (tmp_data, nr, nc, ipvt, dummy, z, 1); |
458
|
535 } |
|
536 |
|
537 delete [] ipvt; |
|
538 delete [] z; |
|
539 |
|
540 return Matrix (tmp_data, nr, nc); |
|
541 } |
|
542 |
740
|
543 Matrix |
|
544 Matrix::pseudo_inverse (double tol) |
|
545 { |
|
546 SVD result (*this); |
|
547 |
|
548 DiagMatrix S = result.singular_values (); |
|
549 Matrix U = result.left_singular_matrix (); |
|
550 Matrix V = result.right_singular_matrix (); |
|
551 |
|
552 ColumnVector sigma = S.diag (); |
|
553 |
|
554 int r = sigma.length () - 1; |
|
555 int nr = rows (); |
|
556 int nc = cols (); |
|
557 |
|
558 if (tol <= 0.0) |
|
559 { |
|
560 if (nr > nc) |
|
561 tol = nr * sigma.elem (0) * DBL_EPSILON; |
|
562 else |
|
563 tol = nc * sigma.elem (0) * DBL_EPSILON; |
|
564 } |
|
565 |
|
566 while (r >= 0 && sigma.elem (r) < tol) |
|
567 r--; |
|
568 |
|
569 if (r < 0) |
|
570 return Matrix (nc, nr, 0.0); |
|
571 else |
|
572 { |
|
573 Matrix Ur = U.extract (0, 0, nr-1, r); |
|
574 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); |
|
575 Matrix Vr = V.extract (0, 0, nc-1, r); |
|
576 return Vr * D * Ur.transpose (); |
|
577 } |
|
578 } |
|
579 |
458
|
580 ComplexMatrix |
|
581 Matrix::fourier (void) const |
|
582 { |
|
583 int nr = rows (); |
|
584 int nc = cols (); |
|
585 int npts, nsamples; |
|
586 if (nr == 1 || nc == 1) |
|
587 { |
|
588 npts = nr > nc ? nr : nc; |
|
589 nsamples = 1; |
|
590 } |
|
591 else |
|
592 { |
|
593 npts = nr; |
|
594 nsamples = nc; |
|
595 } |
|
596 |
|
597 int nn = 4*npts+15; |
|
598 Complex *wsave = new Complex [nn]; |
|
599 Complex *tmp_data = make_complex (data (), length ()); |
|
600 |
1253
|
601 F77_FCN (cffti, CFFTI) (npts, wsave); |
458
|
602 |
|
603 for (int j = 0; j < nsamples; j++) |
1253
|
604 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], wsave); |
458
|
605 |
|
606 delete [] wsave; |
|
607 |
|
608 return ComplexMatrix (tmp_data, nr, nc); |
|
609 } |
|
610 |
|
611 ComplexMatrix |
|
612 Matrix::ifourier (void) const |
|
613 { |
|
614 int nr = rows (); |
|
615 int nc = cols (); |
|
616 int npts, nsamples; |
|
617 if (nr == 1 || nc == 1) |
|
618 { |
|
619 npts = nr > nc ? nr : nc; |
|
620 nsamples = 1; |
|
621 } |
|
622 else |
|
623 { |
|
624 npts = nr; |
|
625 nsamples = nc; |
|
626 } |
|
627 |
|
628 int nn = 4*npts+15; |
|
629 Complex *wsave = new Complex [nn]; |
|
630 Complex *tmp_data = make_complex (data (), length ()); |
|
631 |
1253
|
632 F77_FCN (cffti, CFFTI) (npts, wsave); |
458
|
633 |
|
634 for (int j = 0; j < nsamples; j++) |
1253
|
635 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], wsave); |
458
|
636 |
1321
|
637 for (int j = 0; j < npts*nsamples; j++) |
458
|
638 tmp_data[j] = tmp_data[j] / (double) npts; |
|
639 |
|
640 delete [] wsave; |
|
641 |
|
642 return ComplexMatrix (tmp_data, nr, nc); |
|
643 } |
|
644 |
677
|
645 ComplexMatrix |
|
646 Matrix::fourier2d (void) const |
|
647 { |
|
648 int nr = rows (); |
|
649 int nc = cols (); |
|
650 int npts, nsamples; |
|
651 if (nr == 1 || nc == 1) |
|
652 { |
|
653 npts = nr > nc ? nr : nc; |
|
654 nsamples = 1; |
|
655 } |
|
656 else |
|
657 { |
|
658 npts = nr; |
|
659 nsamples = nc; |
|
660 } |
|
661 |
|
662 int nn = 4*npts+15; |
|
663 Complex *wsave = new Complex [nn]; |
|
664 Complex *tmp_data = make_complex (data (), length ()); |
|
665 |
1253
|
666 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
667 |
|
668 for (int j = 0; j < nsamples; j++) |
1253
|
669 F77_FCN (cfftf, CFFTF) (npts, &tmp_data[npts*j], wsave); |
677
|
670 |
|
671 delete [] wsave; |
|
672 |
|
673 npts = nc; |
|
674 nsamples = nr; |
|
675 nn = 4*npts+15; |
|
676 wsave = new Complex [nn]; |
|
677 Complex *row = new Complex[npts]; |
|
678 |
1253
|
679 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
680 |
1321
|
681 for (int j = 0; j < nsamples; j++) |
677
|
682 { |
|
683 for (int i = 0; i < npts; i++) |
|
684 row[i] = tmp_data[i*nr + j]; |
|
685 |
1253
|
686 F77_FCN (cfftf, CFFTF) (npts, row, wsave); |
677
|
687 |
1321
|
688 for (int i = 0; i < npts; i++) |
677
|
689 tmp_data[i*nr + j] = row[i]; |
|
690 } |
|
691 |
|
692 delete [] wsave; |
|
693 delete [] row; |
|
694 |
|
695 return ComplexMatrix (tmp_data, nr, nc); |
|
696 } |
|
697 |
|
698 ComplexMatrix |
|
699 Matrix::ifourier2d (void) const |
|
700 { |
|
701 int nr = rows (); |
|
702 int nc = cols (); |
|
703 int npts, nsamples; |
|
704 if (nr == 1 || nc == 1) |
|
705 { |
|
706 npts = nr > nc ? nr : nc; |
|
707 nsamples = 1; |
|
708 } |
|
709 else |
|
710 { |
|
711 npts = nr; |
|
712 nsamples = nc; |
|
713 } |
|
714 |
|
715 int nn = 4*npts+15; |
|
716 Complex *wsave = new Complex [nn]; |
|
717 Complex *tmp_data = make_complex (data (), length ()); |
|
718 |
1253
|
719 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
720 |
|
721 for (int j = 0; j < nsamples; j++) |
1253
|
722 F77_FCN (cfftb, CFFTB) (npts, &tmp_data[npts*j], wsave); |
677
|
723 |
|
724 delete [] wsave; |
|
725 |
1321
|
726 for (int j = 0; j < npts*nsamples; j++) |
677
|
727 tmp_data[j] = tmp_data[j] / (double) npts; |
|
728 |
|
729 npts = nc; |
|
730 nsamples = nr; |
|
731 nn = 4*npts+15; |
|
732 wsave = new Complex [nn]; |
|
733 Complex *row = new Complex[npts]; |
|
734 |
1253
|
735 F77_FCN (cffti, CFFTI) (npts, wsave); |
677
|
736 |
1321
|
737 for (int j = 0; j < nsamples; j++) |
677
|
738 { |
|
739 for (int i = 0; i < npts; i++) |
|
740 row[i] = tmp_data[i*nr + j]; |
|
741 |
1253
|
742 F77_FCN (cfftb, CFFTB) (npts, row, wsave); |
677
|
743 |
1321
|
744 for (int i = 0; i < npts; i++) |
677
|
745 tmp_data[i*nr + j] = row[i] / (double) npts; |
|
746 } |
|
747 |
|
748 delete [] wsave; |
|
749 delete [] row; |
|
750 |
|
751 return ComplexMatrix (tmp_data, nr, nc); |
|
752 } |
|
753 |
458
|
754 DET |
|
755 Matrix::determinant (void) const |
|
756 { |
|
757 int info; |
|
758 double rcond; |
|
759 return determinant (info, rcond); |
|
760 } |
|
761 |
|
762 DET |
|
763 Matrix::determinant (int& info) const |
|
764 { |
|
765 double rcond; |
|
766 return determinant (info, rcond); |
|
767 } |
|
768 |
|
769 DET |
532
|
770 Matrix::determinant (int& info, double& rcond) const |
458
|
771 { |
|
772 DET retval; |
|
773 |
|
774 int nr = rows (); |
|
775 int nc = cols (); |
|
776 |
|
777 if (nr == 0 || nc == 0) |
|
778 { |
|
779 double d[2]; |
|
780 d[0] = 1.0; |
|
781 d[1] = 0.0; |
|
782 retval = DET (d); |
|
783 } |
|
784 else |
|
785 { |
|
786 info = 0; |
|
787 int *ipvt = new int [nr]; |
|
788 |
|
789 double *z = new double [nr]; |
|
790 double *tmp_data = dup (data (), length ()); |
|
791 |
1253
|
792 F77_FCN (dgeco, DGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
793 |
1195
|
794 volatile double rcond_plus_one = rcond + 1.0; |
|
795 if (rcond_plus_one == 1.0) |
458
|
796 { |
|
797 info = -1; |
|
798 retval = DET (); |
|
799 } |
|
800 else |
|
801 { |
|
802 double d[2]; |
1253
|
803 F77_FCN (dgedi, DGEDI) (tmp_data, nr, nr, ipvt, d, z, 10); |
458
|
804 retval = DET (d); |
|
805 } |
|
806 |
|
807 delete [] tmp_data; |
|
808 delete [] ipvt; |
|
809 delete [] z; |
|
810 } |
|
811 |
|
812 return retval; |
|
813 } |
|
814 |
|
815 Matrix |
|
816 Matrix::solve (const Matrix& b) const |
|
817 { |
|
818 int info; |
|
819 double rcond; |
|
820 return solve (b, info, rcond); |
|
821 } |
|
822 |
|
823 Matrix |
|
824 Matrix::solve (const Matrix& b, int& info) const |
|
825 { |
|
826 double rcond; |
|
827 return solve (b, info, rcond); |
|
828 } |
|
829 |
|
830 Matrix |
532
|
831 Matrix::solve (const Matrix& b, int& info, double& rcond) const |
458
|
832 { |
|
833 Matrix retval; |
|
834 |
|
835 int nr = rows (); |
|
836 int nc = cols (); |
|
837 if (nr == 0 || nc == 0 || nr != nc || nr != b.rows ()) |
|
838 { |
|
839 (*current_liboctave_error_handler) |
|
840 ("matrix dimension mismatch solution of linear equations"); |
|
841 return Matrix (); |
|
842 } |
|
843 |
|
844 info = 0; |
|
845 int *ipvt = new int [nr]; |
|
846 |
|
847 double *z = new double [nr]; |
|
848 double *tmp_data = dup (data (), length ()); |
|
849 |
1253
|
850 F77_FCN (dgeco, DGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
851 |
1195
|
852 volatile double rcond_plus_one = rcond + 1.0; |
|
853 if (rcond_plus_one == 1.0) |
458
|
854 { |
|
855 info = -2; |
|
856 } |
|
857 else |
|
858 { |
|
859 double *result = dup (b.data (), b.length ()); |
|
860 |
|
861 int b_nc = b.cols (); |
|
862 for (int j = 0; j < b_nc; j++) |
1253
|
863 F77_FCN (dgesl, DGESL) (tmp_data, nr, nr, ipvt, &result[nr*j], 0); |
458
|
864 |
|
865 retval = Matrix (result, b.rows (), b_nc); |
|
866 } |
|
867 |
|
868 delete [] tmp_data; |
|
869 delete [] ipvt; |
|
870 delete [] z; |
|
871 |
|
872 return retval; |
|
873 } |
|
874 |
|
875 ComplexMatrix |
|
876 Matrix::solve (const ComplexMatrix& b) const |
|
877 { |
|
878 ComplexMatrix tmp (*this); |
|
879 return tmp.solve (b); |
|
880 } |
|
881 |
|
882 ComplexMatrix |
|
883 Matrix::solve (const ComplexMatrix& b, int& info) const |
|
884 { |
|
885 ComplexMatrix tmp (*this); |
|
886 return tmp.solve (b, info); |
|
887 } |
|
888 |
|
889 ComplexMatrix |
|
890 Matrix::solve (const ComplexMatrix& b, int& info, double& rcond) const |
|
891 { |
|
892 ComplexMatrix tmp (*this); |
|
893 return tmp.solve (b, info, rcond); |
|
894 } |
|
895 |
|
896 ColumnVector |
|
897 Matrix::solve (const ColumnVector& b) const |
|
898 { |
|
899 int info; double rcond; |
|
900 return solve (b, info, rcond); |
|
901 } |
|
902 |
|
903 ColumnVector |
|
904 Matrix::solve (const ColumnVector& b, int& info) const |
|
905 { |
|
906 double rcond; |
|
907 return solve (b, info, rcond); |
|
908 } |
|
909 |
|
910 ColumnVector |
532
|
911 Matrix::solve (const ColumnVector& b, int& info, double& rcond) const |
458
|
912 { |
|
913 ColumnVector retval; |
|
914 |
|
915 int nr = rows (); |
|
916 int nc = cols (); |
|
917 if (nr == 0 || nc == 0 || nr != nc || nr != b.length ()) |
|
918 { |
|
919 (*current_liboctave_error_handler) |
|
920 ("matrix dimension mismatch solution of linear equations"); |
|
921 return ColumnVector (); |
|
922 } |
|
923 |
|
924 info = 0; |
|
925 int *ipvt = new int [nr]; |
|
926 |
|
927 double *z = new double [nr]; |
|
928 double *tmp_data = dup (data (), length ()); |
|
929 |
1253
|
930 F77_FCN (dgeco, DGECO) (tmp_data, nr, nr, ipvt, rcond, z); |
458
|
931 |
1195
|
932 volatile double rcond_plus_one = rcond + 1.0; |
|
933 if (rcond_plus_one == 1.0) |
458
|
934 { |
|
935 info = -2; |
|
936 } |
|
937 else |
|
938 { |
|
939 int b_len = b.length (); |
|
940 |
|
941 double *result = dup (b.data (), b_len); |
|
942 |
1253
|
943 F77_FCN (dgesl, DGESL) (tmp_data, nr, nr, ipvt, result, 0); |
458
|
944 |
|
945 retval = ColumnVector (result, b_len); |
|
946 } |
|
947 |
|
948 delete [] tmp_data; |
|
949 delete [] ipvt; |
|
950 delete [] z; |
|
951 |
|
952 return retval; |
|
953 } |
|
954 |
|
955 ComplexColumnVector |
|
956 Matrix::solve (const ComplexColumnVector& b) const |
|
957 { |
|
958 ComplexMatrix tmp (*this); |
|
959 return tmp.solve (b); |
|
960 } |
|
961 |
|
962 ComplexColumnVector |
|
963 Matrix::solve (const ComplexColumnVector& b, int& info) const |
|
964 { |
|
965 ComplexMatrix tmp (*this); |
|
966 return tmp.solve (b, info); |
|
967 } |
|
968 |
|
969 ComplexColumnVector |
|
970 Matrix::solve (const ComplexColumnVector& b, int& info, double& rcond) const |
|
971 { |
|
972 ComplexMatrix tmp (*this); |
|
973 return tmp.solve (b, info, rcond); |
|
974 } |
|
975 |
|
976 Matrix |
|
977 Matrix::lssolve (const Matrix& b) const |
|
978 { |
|
979 int info; |
|
980 int rank; |
|
981 return lssolve (b, info, rank); |
|
982 } |
|
983 |
|
984 Matrix |
|
985 Matrix::lssolve (const Matrix& b, int& info) const |
|
986 { |
|
987 int rank; |
|
988 return lssolve (b, info, rank); |
|
989 } |
|
990 |
|
991 Matrix |
|
992 Matrix::lssolve (const Matrix& b, int& info, int& rank) const |
|
993 { |
|
994 int nrhs = b.cols (); |
|
995 |
|
996 int m = rows (); |
|
997 int n = cols (); |
|
998 |
|
999 if (m == 0 || n == 0 || m != b.rows ()) |
|
1000 { |
|
1001 (*current_liboctave_error_handler) |
|
1002 ("matrix dimension mismatch in solution of least squares problem"); |
|
1003 return Matrix (); |
|
1004 } |
|
1005 |
|
1006 double *tmp_data = dup (data (), length ()); |
|
1007 |
|
1008 int nrr = m > n ? m : n; |
|
1009 Matrix result (nrr, nrhs); |
|
1010 |
1321
|
1011 for (int j = 0; j < nrhs; j++) |
|
1012 for (int i = 0; i < m; i++) |
458
|
1013 result.elem (i, j) = b.elem (i, j); |
|
1014 |
|
1015 double *presult = result.fortran_vec (); |
|
1016 |
|
1017 int len_s = m < n ? m : n; |
|
1018 double *s = new double [len_s]; |
|
1019 double rcond = -1.0; |
|
1020 int lwork; |
|
1021 if (m < n) |
|
1022 lwork = 3*m + (2*m > nrhs ? (2*m > n ? 2*m : n) : (nrhs > n ? nrhs : n)); |
|
1023 else |
|
1024 lwork = 3*n + (2*n > nrhs ? (2*n > m ? 2*n : m) : (nrhs > m ? nrhs : m)); |
|
1025 |
|
1026 double *work = new double [lwork]; |
|
1027 |
1253
|
1028 F77_FCN (dgelss, DGELSS) (m, n, nrhs, tmp_data, m, presult, nrr, s, |
|
1029 rcond, rank, work, lwork, info); |
458
|
1030 |
|
1031 Matrix retval (n, nrhs); |
1321
|
1032 for (int j = 0; j < nrhs; j++) |
|
1033 for (int i = 0; i < n; i++) |
458
|
1034 retval.elem (i, j) = result.elem (i, j); |
|
1035 |
|
1036 delete [] tmp_data; |
|
1037 delete [] s; |
|
1038 delete [] work; |
|
1039 |
|
1040 return retval; |
|
1041 } |
|
1042 |
|
1043 ComplexMatrix |
|
1044 Matrix::lssolve (const ComplexMatrix& b) const |
|
1045 { |
|
1046 ComplexMatrix tmp (*this); |
|
1047 return tmp.lssolve (b); |
|
1048 } |
|
1049 |
|
1050 ComplexMatrix |
|
1051 Matrix::lssolve (const ComplexMatrix& b, int& info) const |
|
1052 { |
|
1053 ComplexMatrix tmp (*this); |
|
1054 return tmp.lssolve (b); |
|
1055 } |
|
1056 |
|
1057 ComplexMatrix |
|
1058 Matrix::lssolve (const ComplexMatrix& b, int& info, int& rank) const |
|
1059 { |
|
1060 ComplexMatrix tmp (*this); |
|
1061 return tmp.lssolve (b); |
|
1062 } |
|
1063 |
|
1064 ColumnVector |
|
1065 Matrix::lssolve (const ColumnVector& b) const |
|
1066 { |
|
1067 int info; |
|
1068 int rank; return lssolve (b, info, rank); |
|
1069 } |
|
1070 |
|
1071 ColumnVector |
|
1072 Matrix::lssolve (const ColumnVector& b, int& info) const |
|
1073 { |
|
1074 int rank; |
|
1075 return lssolve (b, info, rank); |
|
1076 } |
|
1077 |
|
1078 ColumnVector |
|
1079 Matrix::lssolve (const ColumnVector& b, int& info, int& rank) const |
|
1080 { |
|
1081 int nrhs = 1; |
|
1082 |
|
1083 int m = rows (); |
|
1084 int n = cols (); |
|
1085 |
|
1086 if (m == 0 || n == 0 || m != b.length ()) |
|
1087 { |
|
1088 (*current_liboctave_error_handler) |
|
1089 ("matrix dimension mismatch in solution of least squares problem"); |
|
1090 return ColumnVector (); |
|
1091 } |
|
1092 |
|
1093 double *tmp_data = dup (data (), length ()); |
|
1094 |
|
1095 int nrr = m > n ? m : n; |
|
1096 ColumnVector result (nrr); |
|
1097 |
1321
|
1098 for (int i = 0; i < m; i++) |
458
|
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); |
1321
|
1118 for (int i = 0; i < n; i++) |
458
|
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; |
1360
|
2155 |
458
|
2156 for (int i = 0; i < a.rows (); i++) |
|
2157 { |
|
2158 for (int j = 0; j < a.cols (); j++) |
|
2159 os << " " /* setw (field_width) */ << a.elem (i, j); |
|
2160 os << "\n"; |
|
2161 } |
|
2162 return os; |
|
2163 } |
|
2164 |
|
2165 istream& |
|
2166 operator >> (istream& is, Matrix& a) |
|
2167 { |
|
2168 int nr = a.rows (); |
|
2169 int nc = a.cols (); |
|
2170 |
|
2171 if (nr < 1 || nc < 1) |
|
2172 is.clear (ios::badbit); |
|
2173 else |
|
2174 { |
|
2175 double tmp; |
|
2176 for (int i = 0; i < nr; i++) |
|
2177 for (int j = 0; j < nc; j++) |
|
2178 { |
|
2179 is >> tmp; |
|
2180 if (is) |
|
2181 a.elem (i, j) = tmp; |
|
2182 else |
|
2183 break; |
|
2184 } |
|
2185 } |
|
2186 |
|
2187 return is; |
|
2188 } |
|
2189 |
1365
|
2190 // Read an array of data from a file in binary format. |
1360
|
2191 |
458
|
2192 int |
1365
|
2193 Matrix::read (FILE *fptr, const char *type) |
458
|
2194 { |
1360
|
2195 // Allocate buffer pointers. |
458
|
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 |
1360
|
2213 // Convert data to double. |
458
|
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 |
1360
|
2267 // Write the data array to a file in binary format. |
|
2268 |
458
|
2269 int |
1365
|
2270 Matrix::write (FILE *fptr, const char *type) |
458
|
2271 { |
1360
|
2272 // Allocate buffer pointers. |
458
|
2273 |
|
2274 union |
|
2275 { |
|
2276 void *vd; |
|
2277 char *ch; |
|
2278 u_char *uc; |
|
2279 short *sh; |
|
2280 u_short *us; |
|
2281 int *in; |
|
2282 u_int *ui; |
|
2283 long *ln; |
|
2284 u_long *ul; |
|
2285 float *fl; |
|
2286 double *db; |
|
2287 } |
|
2288 buf; |
|
2289 |
471
|
2290 int nitems = length (); |
458
|
2291 |
471
|
2292 double *d = fortran_vec (); |
458
|
2293 |
1360
|
2294 // Convert from double to correct size. |
458
|
2295 |
471
|
2296 if (! type) |
458
|
2297 { |
471
|
2298 (*current_liboctave_error_handler) |
|
2299 ("fwrite: invalid NULL type parameter"); |
|
2300 return 0; |
|
2301 } |
458
|
2302 |
471
|
2303 size_t size; |
|
2304 int count; |
458
|
2305 |
471
|
2306 #define DO_FWRITE(TYPE,ELEM) \ |
|
2307 do \ |
|
2308 { \ |
|
2309 size = sizeof (TYPE); \ |
|
2310 buf.ELEM = new TYPE [nitems]; \ |
|
2311 for (int k = 0; k < nitems; k++) \ |
|
2312 buf.ELEM[k] = (TYPE) d[k]; \ |
|
2313 count = fwrite (buf.ELEM, size, nitems, fptr); \ |
|
2314 delete [] buf.ELEM; \ |
|
2315 } \ |
|
2316 while (0) |
458
|
2317 |
471
|
2318 if (strcasecmp (type, "double") == 0) |
|
2319 DO_FWRITE (double, db); |
|
2320 else if (strcasecmp (type, "char") == 0) |
|
2321 DO_FWRITE (char, ch); |
|
2322 else if (strcasecmp (type, "uchar") == 0) |
|
2323 DO_FWRITE (u_char, uc); |
|
2324 else if (strcasecmp (type, "short") == 0) |
|
2325 DO_FWRITE (short, sh); |
|
2326 else if (strcasecmp (type, "ushort") == 0) |
|
2327 DO_FWRITE (u_short, us); |
|
2328 else if (strcasecmp (type, "int") == 0) |
|
2329 DO_FWRITE (int, in); |
|
2330 else if (strcasecmp (type, "uint") == 0) |
|
2331 DO_FWRITE (u_int, ui); |
|
2332 else if (strcasecmp (type, "long") == 0) |
|
2333 DO_FWRITE (long, ln); |
|
2334 else if (strcasecmp (type, "ulong") == 0) |
|
2335 DO_FWRITE (u_long, ul); |
|
2336 else if (strcasecmp (type, "float") == 0) |
|
2337 DO_FWRITE (float, fl); |
|
2338 else |
|
2339 { |
|
2340 (*current_liboctave_error_handler) |
|
2341 ("fwrite: unrecognized type parameter %s", type); |
458
|
2342 return 0; |
471
|
2343 } |
458
|
2344 |
|
2345 return count; |
|
2346 } |
|
2347 |
|
2348 /* |
|
2349 ;;; Local Variables: *** |
|
2350 ;;; mode: C++ *** |
|
2351 ;;; page-delimiter: "^/\\*" *** |
|
2352 ;;; End: *** |
|
2353 */ |