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