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