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