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