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