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