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