1993
|
1 // Matrix manipulations. |
458
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 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 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
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 |
3503
|
34 #include <iostream> |
1367
|
35 |
2317
|
36 #include "byte-swap.h" |
2828
|
37 #include "dMatrix.h" |
1819
|
38 #include "dbleAEPBAL.h" |
458
|
39 #include "dbleDET.h" |
1819
|
40 #include "dbleSCHUR.h" |
740
|
41 #include "dbleSVD.h" |
1847
|
42 #include "f77-fcn.h" |
458
|
43 #include "lo-error.h" |
2354
|
44 #include "lo-ieee.h" |
|
45 #include "lo-mappers.h" |
1968
|
46 #include "lo-utils.h" |
1367
|
47 #include "mx-base.h" |
2828
|
48 #include "mx-m-dm.h" |
3176
|
49 #include "mx-dm-m.h" |
1367
|
50 #include "mx-inlines.cc" |
1650
|
51 #include "oct-cmplx.h" |
4153
|
52 #include "quit.h" |
458
|
53 |
3827
|
54 #ifdef HAVE_FFTW |
|
55 #include "oct-fftw.h" |
|
56 #endif |
|
57 |
458
|
58 // Fortran functions we call. |
|
59 |
|
60 extern "C" |
|
61 { |
4552
|
62 F77_RET_T |
|
63 F77_FUNC (dgebal, DGEBAL) (F77_CONST_CHAR_ARG_DECL, |
|
64 const int&, double*, const int&, int&, |
|
65 int&, double*, int& |
|
66 F77_CHAR_ARG_LEN_DECL); |
|
67 |
|
68 F77_RET_T |
|
69 F77_FUNC (dgebak, DGEBAK) (F77_CONST_CHAR_ARG_DECL, |
|
70 F77_CONST_CHAR_ARG_DECL, |
|
71 const int&, const int&, const int&, double*, |
|
72 const int&, double*, const int&, int& |
|
73 F77_CHAR_ARG_LEN_DECL |
|
74 F77_CHAR_ARG_LEN_DECL); |
|
75 |
|
76 |
|
77 F77_RET_T |
|
78 F77_FUNC (dgemm, DGEMM) (F77_CONST_CHAR_ARG_DECL, |
|
79 F77_CONST_CHAR_ARG_DECL, |
|
80 const int&, const int&, const int&, |
|
81 const double&, const double*, const int&, |
|
82 const double*, const int&, const double&, |
|
83 double*, const int& |
|
84 F77_CHAR_ARG_LEN_DECL |
|
85 F77_CHAR_ARG_LEN_DECL); |
|
86 |
|
87 F77_RET_T |
|
88 F77_FUNC (dgetrf, DGETRF) (const int&, const int&, double*, const int&, |
4329
|
89 int*, int&); |
|
90 |
4552
|
91 F77_RET_T |
|
92 F77_FUNC (dgetrs, DGETRS) (F77_CONST_CHAR_ARG_DECL, const int&, const int&, |
|
93 const double*, const int&, |
|
94 const int*, double*, const int&, int& |
|
95 F77_CHAR_ARG_LEN_DECL); |
|
96 |
|
97 F77_RET_T |
|
98 F77_FUNC (dgetri, DGETRI) (const int&, double*, const int&, const int*, |
|
99 double*, const int&, int&); |
|
100 |
|
101 F77_RET_T |
|
102 F77_FUNC (dgecon, DGECON) (F77_CONST_CHAR_ARG_DECL, const int&, double*, |
|
103 const int&, const double&, double&, |
|
104 double*, int*, int& |
|
105 F77_CHAR_ARG_LEN_DECL); |
|
106 |
|
107 F77_RET_T |
|
108 F77_FUNC (dgelss, DGELSS) (const int&, const int&, const int&, |
|
109 double*, const int&, double*, |
|
110 const int&, double*, double&, int&, |
|
111 double*, const int&, int&); |
458
|
112 |
1360
|
113 // Note that the original complex fft routines were not written for |
|
114 // double complex arguments. They have been modified by adding an |
|
115 // implicit double precision (a-h,o-z) statement at the beginning of |
|
116 // each subroutine. |
458
|
117 |
4552
|
118 F77_RET_T |
|
119 F77_FUNC (cffti, CFFTI) (const int&, Complex*); |
|
120 |
|
121 F77_RET_T |
|
122 F77_FUNC (cfftf, CFFTF) (const int&, Complex*, Complex*); |
|
123 |
|
124 F77_RET_T |
|
125 F77_FUNC (cfftb, CFFTB) (const int&, Complex*, Complex*); |
|
126 |
|
127 F77_RET_T |
|
128 F77_FUNC (dlartg, DLARTG) (const double&, const double&, double&, |
|
129 double&, double&); |
|
130 |
|
131 F77_RET_T |
|
132 F77_FUNC (dtrsyl, DTRSYL) (F77_CONST_CHAR_ARG_DECL, |
|
133 F77_CONST_CHAR_ARG_DECL, |
|
134 const int&, const int&, const int&, |
|
135 const double*, const int&, const double*, |
|
136 const int&, const double*, const int&, |
|
137 double&, int& |
|
138 F77_CHAR_ARG_LEN_DECL |
|
139 F77_CHAR_ARG_LEN_DECL); |
|
140 |
|
141 F77_RET_T |
|
142 F77_FUNC (xdlange, XDLANGE) (F77_CONST_CHAR_ARG_DECL, const int&, |
|
143 const int&, const double*, |
|
144 const int&, double*, double& |
|
145 F77_CHAR_ARG_LEN_DECL); |
458
|
146 } |
|
147 |
1360
|
148 // Matrix class. |
458
|
149 |
2349
|
150 Matrix::Matrix (const RowVector& rv) |
|
151 : MArray2<double> (1, rv.length (), 0.0) |
|
152 { |
|
153 for (int i = 0; i < rv.length (); i++) |
|
154 elem (0, i) = rv.elem (i); |
|
155 } |
|
156 |
|
157 Matrix::Matrix (const ColumnVector& cv) |
|
158 : MArray2<double> (cv.length (), 1, 0.0) |
|
159 { |
|
160 for (int i = 0; i < cv.length (); i++) |
|
161 elem (i, 0) = cv.elem (i); |
|
162 } |
|
163 |
458
|
164 Matrix::Matrix (const DiagMatrix& a) |
1214
|
165 : MArray2<double> (a.rows (), a.cols (), 0.0) |
458
|
166 { |
|
167 for (int i = 0; i < a.length (); i++) |
|
168 elem (i, i) = a.elem (i, i); |
|
169 } |
|
170 |
1574
|
171 // XXX FIXME XXX -- could we use a templated mixed-type copy function |
|
172 // here? |
|
173 |
2828
|
174 Matrix::Matrix (const boolMatrix& a) |
|
175 : MArray2<double> (a.rows (), a.cols ()) |
|
176 { |
|
177 for (int i = 0; i < a.rows (); i++) |
|
178 for (int j = 0; j < a.cols (); j++) |
|
179 elem (i, j) = a.elem (i, j); |
|
180 } |
|
181 |
1574
|
182 Matrix::Matrix (const charMatrix& a) |
|
183 : MArray2<double> (a.rows (), a.cols ()) |
|
184 { |
|
185 for (int i = 0; i < a.rows (); i++) |
|
186 for (int j = 0; j < a.cols (); j++) |
|
187 elem (i, j) = a.elem (i, j); |
|
188 } |
|
189 |
2385
|
190 bool |
458
|
191 Matrix::operator == (const Matrix& a) const |
|
192 { |
|
193 if (rows () != a.rows () || cols () != a.cols ()) |
2385
|
194 return false; |
458
|
195 |
3769
|
196 return mx_inline_equal (data (), a.data (), length ()); |
458
|
197 } |
|
198 |
2385
|
199 bool |
458
|
200 Matrix::operator != (const Matrix& a) const |
|
201 { |
|
202 return !(*this == a); |
|
203 } |
|
204 |
3354
|
205 bool |
|
206 Matrix::is_symmetric (void) const |
|
207 { |
|
208 if (is_square () && rows () > 0) |
|
209 { |
|
210 for (int i = 0; i < rows (); i++) |
|
211 for (int j = i+1; j < cols (); j++) |
|
212 if (elem (i, j) != elem (j, i)) |
|
213 return false; |
|
214 |
|
215 return true; |
|
216 } |
|
217 |
|
218 return false; |
|
219 } |
|
220 |
458
|
221 Matrix& |
|
222 Matrix::insert (const Matrix& a, int r, int c) |
|
223 { |
1561
|
224 Array2<double>::insert (a, r, c); |
458
|
225 return *this; |
|
226 } |
|
227 |
|
228 Matrix& |
|
229 Matrix::insert (const RowVector& a, int r, int c) |
|
230 { |
|
231 int a_len = a.length (); |
4316
|
232 |
1698
|
233 if (r < 0 || r >= rows () || c < 0 || c + a_len > cols ()) |
458
|
234 { |
|
235 (*current_liboctave_error_handler) ("range error for insert"); |
|
236 return *this; |
|
237 } |
|
238 |
4316
|
239 if (a_len > 0) |
|
240 { |
|
241 make_unique (); |
|
242 |
|
243 for (int i = 0; i < a_len; i++) |
|
244 xelem (r, c+i) = a.elem (i); |
|
245 } |
458
|
246 |
|
247 return *this; |
|
248 } |
|
249 |
|
250 Matrix& |
|
251 Matrix::insert (const ColumnVector& a, int r, int c) |
|
252 { |
|
253 int a_len = a.length (); |
4316
|
254 |
1698
|
255 if (r < 0 || r + a_len > rows () || c < 0 || c >= cols ()) |
458
|
256 { |
|
257 (*current_liboctave_error_handler) ("range error for insert"); |
|
258 return *this; |
|
259 } |
|
260 |
4316
|
261 if (a_len > 0) |
|
262 { |
|
263 make_unique (); |
|
264 |
|
265 for (int i = 0; i < a_len; i++) |
|
266 xelem (r+i, c) = a.elem (i); |
|
267 } |
458
|
268 |
|
269 return *this; |
|
270 } |
|
271 |
|
272 Matrix& |
|
273 Matrix::insert (const DiagMatrix& a, int r, int c) |
|
274 { |
1697
|
275 int a_nr = a.rows (); |
|
276 int a_nc = a.cols (); |
|
277 |
1698
|
278 if (r < 0 || r + a_nr > rows () || c < 0 || c + a_nc > cols ()) |
458
|
279 { |
|
280 (*current_liboctave_error_handler) ("range error for insert"); |
|
281 return *this; |
|
282 } |
|
283 |
1697
|
284 fill (0.0, r, c, r + a_nr - 1, c + a_nc - 1); |
|
285 |
4316
|
286 int a_len = a.length (); |
|
287 |
|
288 if (a_len > 0) |
|
289 { |
|
290 make_unique (); |
|
291 |
|
292 for (int i = 0; i < a_len; i++) |
|
293 xelem (r+i, c+i) = a.elem (i, i); |
|
294 } |
458
|
295 |
|
296 return *this; |
|
297 } |
|
298 |
|
299 Matrix& |
|
300 Matrix::fill (double val) |
|
301 { |
|
302 int nr = rows (); |
|
303 int nc = cols (); |
4316
|
304 |
458
|
305 if (nr > 0 && nc > 0) |
4316
|
306 { |
|
307 make_unique (); |
|
308 |
|
309 for (int j = 0; j < nc; j++) |
|
310 for (int i = 0; i < nr; i++) |
|
311 xelem (i, j) = val; |
|
312 } |
458
|
313 |
|
314 return *this; |
|
315 } |
|
316 |
|
317 Matrix& |
|
318 Matrix::fill (double val, int r1, int c1, int r2, int c2) |
|
319 { |
|
320 int nr = rows (); |
|
321 int nc = cols (); |
4316
|
322 |
458
|
323 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
|
324 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) |
|
325 { |
|
326 (*current_liboctave_error_handler) ("range error for fill"); |
|
327 return *this; |
|
328 } |
|
329 |
|
330 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
331 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
332 |
4316
|
333 if (r2 >= r1 && c2 >= c1) |
|
334 { |
|
335 make_unique (); |
|
336 |
|
337 for (int j = c1; j <= c2; j++) |
|
338 for (int i = r1; i <= r2; i++) |
|
339 xelem (i, j) = val; |
|
340 } |
458
|
341 |
|
342 return *this; |
|
343 } |
|
344 |
|
345 Matrix |
|
346 Matrix::append (const Matrix& a) const |
|
347 { |
|
348 int nr = rows (); |
|
349 int nc = cols (); |
|
350 if (nr != a.rows ()) |
|
351 { |
|
352 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
353 return Matrix (); |
|
354 } |
|
355 |
|
356 int nc_insert = nc; |
|
357 Matrix retval (nr, nc + a.cols ()); |
|
358 retval.insert (*this, 0, 0); |
|
359 retval.insert (a, 0, nc_insert); |
|
360 return retval; |
|
361 } |
|
362 |
|
363 Matrix |
|
364 Matrix::append (const RowVector& a) const |
|
365 { |
|
366 int nr = rows (); |
|
367 int nc = cols (); |
|
368 if (nr != 1) |
|
369 { |
|
370 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
371 return Matrix (); |
|
372 } |
|
373 |
|
374 int nc_insert = nc; |
|
375 Matrix retval (nr, nc + a.length ()); |
|
376 retval.insert (*this, 0, 0); |
|
377 retval.insert (a, 0, nc_insert); |
|
378 return retval; |
|
379 } |
|
380 |
|
381 Matrix |
|
382 Matrix::append (const ColumnVector& a) const |
|
383 { |
|
384 int nr = rows (); |
|
385 int nc = cols (); |
|
386 if (nr != a.length ()) |
|
387 { |
|
388 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
389 return Matrix (); |
|
390 } |
|
391 |
|
392 int nc_insert = nc; |
|
393 Matrix retval (nr, nc + 1); |
|
394 retval.insert (*this, 0, 0); |
|
395 retval.insert (a, 0, nc_insert); |
|
396 return retval; |
|
397 } |
|
398 |
|
399 Matrix |
|
400 Matrix::append (const DiagMatrix& a) const |
|
401 { |
|
402 int nr = rows (); |
|
403 int nc = cols (); |
|
404 if (nr != a.rows ()) |
|
405 { |
|
406 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
407 return *this; |
|
408 } |
|
409 |
|
410 int nc_insert = nc; |
|
411 Matrix retval (nr, nc + a.cols ()); |
|
412 retval.insert (*this, 0, 0); |
|
413 retval.insert (a, 0, nc_insert); |
|
414 return retval; |
|
415 } |
|
416 |
|
417 Matrix |
|
418 Matrix::stack (const Matrix& a) const |
|
419 { |
|
420 int nr = rows (); |
|
421 int nc = cols (); |
|
422 if (nc != a.cols ()) |
|
423 { |
|
424 (*current_liboctave_error_handler) |
|
425 ("column dimension mismatch for stack"); |
|
426 return Matrix (); |
|
427 } |
|
428 |
|
429 int nr_insert = nr; |
|
430 Matrix retval (nr + a.rows (), nc); |
|
431 retval.insert (*this, 0, 0); |
|
432 retval.insert (a, nr_insert, 0); |
|
433 return retval; |
|
434 } |
|
435 |
|
436 Matrix |
|
437 Matrix::stack (const RowVector& a) const |
|
438 { |
|
439 int nr = rows (); |
|
440 int nc = cols (); |
|
441 if (nc != a.length ()) |
|
442 { |
|
443 (*current_liboctave_error_handler) |
|
444 ("column dimension mismatch for stack"); |
|
445 return Matrix (); |
|
446 } |
|
447 |
|
448 int nr_insert = nr; |
|
449 Matrix retval (nr + 1, nc); |
|
450 retval.insert (*this, 0, 0); |
|
451 retval.insert (a, nr_insert, 0); |
|
452 return retval; |
|
453 } |
|
454 |
|
455 Matrix |
|
456 Matrix::stack (const ColumnVector& a) const |
|
457 { |
|
458 int nr = rows (); |
|
459 int nc = cols (); |
|
460 if (nc != 1) |
|
461 { |
|
462 (*current_liboctave_error_handler) |
|
463 ("column dimension mismatch for stack"); |
|
464 return Matrix (); |
|
465 } |
|
466 |
|
467 int nr_insert = nr; |
|
468 Matrix retval (nr + a.length (), nc); |
|
469 retval.insert (*this, 0, 0); |
|
470 retval.insert (a, nr_insert, 0); |
|
471 return retval; |
|
472 } |
|
473 |
|
474 Matrix |
|
475 Matrix::stack (const DiagMatrix& a) const |
|
476 { |
|
477 int nr = rows (); |
|
478 int nc = cols (); |
|
479 if (nc != a.cols ()) |
|
480 { |
|
481 (*current_liboctave_error_handler) |
|
482 ("column dimension mismatch for stack"); |
|
483 return Matrix (); |
|
484 } |
|
485 |
|
486 int nr_insert = nr; |
|
487 Matrix retval (nr + a.rows (), nc); |
|
488 retval.insert (*this, 0, 0); |
|
489 retval.insert (a, nr_insert, 0); |
|
490 return retval; |
|
491 } |
|
492 |
|
493 Matrix |
1205
|
494 real (const ComplexMatrix& a) |
|
495 { |
|
496 int a_len = a.length (); |
|
497 Matrix retval; |
|
498 if (a_len > 0) |
3769
|
499 retval = Matrix (mx_inline_real_dup (a.data (), a_len), |
|
500 a.rows (), a.cols ()); |
1205
|
501 return retval; |
|
502 } |
|
503 |
|
504 Matrix |
|
505 imag (const ComplexMatrix& a) |
|
506 { |
|
507 int a_len = a.length (); |
|
508 Matrix retval; |
|
509 if (a_len > 0) |
3769
|
510 retval = Matrix (mx_inline_imag_dup (a.data (), a_len), |
|
511 a.rows (), a.cols ()); |
1205
|
512 return retval; |
|
513 } |
|
514 |
|
515 Matrix |
458
|
516 Matrix::extract (int r1, int c1, int r2, int c2) const |
|
517 { |
|
518 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
519 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
520 |
|
521 int new_r = r2 - r1 + 1; |
|
522 int new_c = c2 - c1 + 1; |
|
523 |
|
524 Matrix result (new_r, new_c); |
|
525 |
|
526 for (int j = 0; j < new_c; j++) |
|
527 for (int i = 0; i < new_r; i++) |
4316
|
528 result.xelem (i, j) = elem (r1+i, c1+j); |
|
529 |
|
530 return result; |
|
531 } |
|
532 |
|
533 Matrix |
|
534 Matrix::extract_n (int r1, int c1, int nr, int nc) const |
|
535 { |
|
536 Matrix result (nr, nc); |
|
537 |
|
538 for (int j = 0; j < nc; j++) |
|
539 for (int i = 0; i < nr; i++) |
|
540 result.xelem (i, j) = elem (r1+i, c1+j); |
458
|
541 |
|
542 return result; |
|
543 } |
|
544 |
|
545 // extract row or column i. |
|
546 |
|
547 RowVector |
|
548 Matrix::row (int i) const |
|
549 { |
|
550 int nc = cols (); |
|
551 if (i < 0 || i >= rows ()) |
|
552 { |
|
553 (*current_liboctave_error_handler) ("invalid row selection"); |
|
554 return RowVector (); |
|
555 } |
|
556 |
|
557 RowVector retval (nc); |
|
558 for (int j = 0; j < nc; j++) |
4316
|
559 retval.xelem (j) = elem (i, j); |
458
|
560 |
|
561 return retval; |
|
562 } |
|
563 |
|
564 RowVector |
|
565 Matrix::row (char *s) const |
|
566 { |
533
|
567 if (! s) |
458
|
568 { |
|
569 (*current_liboctave_error_handler) ("invalid row selection"); |
|
570 return RowVector (); |
|
571 } |
|
572 |
|
573 char c = *s; |
|
574 if (c == 'f' || c == 'F') |
|
575 return row (0); |
|
576 else if (c == 'l' || c == 'L') |
|
577 return row (rows () - 1); |
|
578 else |
|
579 { |
|
580 (*current_liboctave_error_handler) ("invalid row selection"); |
|
581 return RowVector (); |
|
582 } |
|
583 } |
|
584 |
|
585 ColumnVector |
|
586 Matrix::column (int i) const |
|
587 { |
|
588 int nr = rows (); |
|
589 if (i < 0 || i >= cols ()) |
|
590 { |
|
591 (*current_liboctave_error_handler) ("invalid column selection"); |
|
592 return ColumnVector (); |
|
593 } |
|
594 |
|
595 ColumnVector retval (nr); |
|
596 for (int j = 0; j < nr; j++) |
4316
|
597 retval.xelem (j) = elem (j, i); |
458
|
598 |
|
599 return retval; |
|
600 } |
|
601 |
|
602 ColumnVector |
|
603 Matrix::column (char *s) const |
|
604 { |
533
|
605 if (! s) |
458
|
606 { |
|
607 (*current_liboctave_error_handler) ("invalid column selection"); |
|
608 return ColumnVector (); |
|
609 } |
|
610 |
|
611 char c = *s; |
|
612 if (c == 'f' || c == 'F') |
|
613 return column (0); |
|
614 else if (c == 'l' || c == 'L') |
|
615 return column (cols () - 1); |
|
616 else |
|
617 { |
|
618 (*current_liboctave_error_handler) ("invalid column selection"); |
|
619 return ColumnVector (); |
|
620 } |
|
621 } |
|
622 |
|
623 Matrix |
|
624 Matrix::inverse (void) const |
|
625 { |
|
626 int info; |
|
627 double rcond; |
4329
|
628 return inverse (info, rcond, 0, 0); |
458
|
629 } |
|
630 |
|
631 Matrix |
|
632 Matrix::inverse (int& info) const |
|
633 { |
|
634 double rcond; |
4329
|
635 return inverse (info, rcond, 0, 0); |
458
|
636 } |
|
637 |
|
638 Matrix |
4329
|
639 Matrix::inverse (int& info, double& rcond, int force, int calc_cond) const |
458
|
640 { |
1948
|
641 Matrix retval; |
|
642 |
458
|
643 int nr = rows (); |
|
644 int nc = cols (); |
1948
|
645 |
458
|
646 if (nr != nc || nr == 0 || nc == 0) |
1948
|
647 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
458
|
648 else |
|
649 { |
1948
|
650 Array<int> ipvt (nr); |
|
651 int *pipvt = ipvt.fortran_vec (); |
|
652 |
|
653 retval = *this; |
|
654 double *tmp_data = retval.fortran_vec (); |
|
655 |
4329
|
656 Array<double> z(1); |
|
657 int lwork = -1; |
|
658 |
4330
|
659 // Query the optimum work array size. |
4329
|
660 F77_XFCN (dgetri, DGETRI, (nc, tmp_data, nr, pipvt, |
|
661 z.fortran_vec (), lwork, info)); |
|
662 |
|
663 if (f77_exception_encountered) |
|
664 { |
|
665 (*current_liboctave_error_handler) |
|
666 ("unrecoverable error in dgetri"); |
|
667 return retval; |
|
668 } |
|
669 |
|
670 lwork = static_cast<int> (z(0)); |
|
671 lwork = (lwork < 2 *nc ? 2*nc : lwork); |
|
672 z.resize (lwork); |
|
673 double *pz = z.fortran_vec (); |
|
674 |
|
675 info = 0; |
|
676 |
4330
|
677 // Calculate the norm of the matrix, for later use. |
4329
|
678 double anorm = 0; |
|
679 if (calc_cond) |
|
680 anorm = retval.abs().sum().row(0).max(); |
|
681 |
|
682 F77_XFCN (dgetrf, DGETRF, (nc, nc, tmp_data, nr, pipvt, info)); |
1948
|
683 |
|
684 if (f77_exception_encountered) |
4329
|
685 (*current_liboctave_error_handler) ("unrecoverable error in dgetrf"); |
1948
|
686 else |
|
687 { |
4330
|
688 // Throw-away extra info LAPACK gives so as to not change output. |
4509
|
689 rcond = 0.0; |
|
690 if (info != 0) |
1948
|
691 info = -1; |
4329
|
692 else if (calc_cond) |
|
693 { |
4330
|
694 // Now calculate the condition number for non-singular matrix. |
4329
|
695 char job = '1'; |
|
696 Array<int> iz (nc); |
|
697 int *piz = iz.fortran_vec (); |
4552
|
698 F77_XFCN (dgecon, DGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
699 nc, tmp_data, nr, anorm, |
|
700 rcond, pz, piz, info |
|
701 F77_CHAR_ARG_LEN (1))); |
4329
|
702 |
|
703 if (f77_exception_encountered) |
|
704 (*current_liboctave_error_handler) |
|
705 ("unrecoverable error in dgecon"); |
|
706 |
4509
|
707 if (info != 0) |
4329
|
708 info = -1; |
|
709 } |
1948
|
710 |
|
711 if (info == -1 && ! force) |
|
712 retval = *this; // Restore matrix contents. |
|
713 else |
|
714 { |
4329
|
715 F77_XFCN (dgetri, DGETRI, (nc, tmp_data, nr, pipvt, |
4552
|
716 pz, lwork, info)); |
1948
|
717 |
|
718 if (f77_exception_encountered) |
|
719 (*current_liboctave_error_handler) |
4329
|
720 ("unrecoverable error in dgetri"); |
|
721 |
4509
|
722 if (info != 0) |
4329
|
723 info = -1; |
1948
|
724 } |
|
725 } |
458
|
726 } |
|
727 |
1948
|
728 return retval; |
458
|
729 } |
|
730 |
740
|
731 Matrix |
4384
|
732 Matrix::pseudo_inverse (double tol) const |
740
|
733 { |
3480
|
734 SVD result (*this, SVD::economy); |
740
|
735 |
|
736 DiagMatrix S = result.singular_values (); |
|
737 Matrix U = result.left_singular_matrix (); |
|
738 Matrix V = result.right_singular_matrix (); |
|
739 |
|
740 ColumnVector sigma = S.diag (); |
|
741 |
|
742 int r = sigma.length () - 1; |
|
743 int nr = rows (); |
|
744 int nc = cols (); |
|
745 |
|
746 if (tol <= 0.0) |
|
747 { |
|
748 if (nr > nc) |
|
749 tol = nr * sigma.elem (0) * DBL_EPSILON; |
|
750 else |
|
751 tol = nc * sigma.elem (0) * DBL_EPSILON; |
|
752 } |
|
753 |
|
754 while (r >= 0 && sigma.elem (r) < tol) |
|
755 r--; |
|
756 |
|
757 if (r < 0) |
|
758 return Matrix (nc, nr, 0.0); |
|
759 else |
|
760 { |
|
761 Matrix Ur = U.extract (0, 0, nr-1, r); |
|
762 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); |
|
763 Matrix Vr = V.extract (0, 0, nc-1, r); |
|
764 return Vr * D * Ur.transpose (); |
|
765 } |
|
766 } |
|
767 |
3827
|
768 #ifdef HAVE_FFTW |
|
769 |
|
770 ComplexMatrix |
|
771 Matrix::fourier (void) const |
|
772 { |
|
773 size_t nr = rows (); |
|
774 size_t nc = cols (); |
|
775 |
|
776 ComplexMatrix retval (nr, nc); |
|
777 |
|
778 size_t npts, nsamples; |
|
779 |
|
780 if (nr == 1 || nc == 1) |
|
781 { |
|
782 npts = nr > nc ? nr : nc; |
|
783 nsamples = 1; |
|
784 } |
|
785 else |
|
786 { |
|
787 npts = nr; |
|
788 nsamples = nc; |
|
789 } |
|
790 |
|
791 ComplexMatrix tmp (*this); |
|
792 Complex *in (tmp.fortran_vec ()); |
|
793 Complex *out (retval.fortran_vec ()); |
|
794 |
|
795 for (size_t i = 0; i < nsamples; i++) |
|
796 { |
4153
|
797 OCTAVE_QUIT; |
|
798 |
3827
|
799 octave_fftw::fft (&in[npts * i], &out[npts * i], npts); |
|
800 } |
|
801 |
|
802 return retval; |
|
803 } |
|
804 |
|
805 ComplexMatrix |
|
806 Matrix::ifourier (void) const |
|
807 { |
|
808 size_t nr = rows (); |
|
809 size_t nc = cols (); |
|
810 |
|
811 ComplexMatrix retval (nr, nc); |
|
812 |
|
813 size_t npts, nsamples; |
|
814 |
|
815 if (nr == 1 || nc == 1) |
|
816 { |
|
817 npts = nr > nc ? nr : nc; |
|
818 nsamples = 1; |
|
819 } |
|
820 else |
|
821 { |
|
822 npts = nr; |
|
823 nsamples = nc; |
|
824 } |
|
825 |
|
826 ComplexMatrix tmp (*this); |
|
827 Complex *in (tmp.fortran_vec ()); |
|
828 Complex *out (retval.fortran_vec ()); |
|
829 |
|
830 for (size_t i = 0; i < nsamples; i++) |
|
831 { |
4153
|
832 OCTAVE_QUIT; |
|
833 |
3827
|
834 octave_fftw::ifft (&in[npts * i], &out[npts * i], npts); |
|
835 } |
|
836 |
|
837 return retval; |
|
838 } |
|
839 |
|
840 ComplexMatrix |
|
841 Matrix::fourier2d (void) const |
|
842 { |
|
843 int nr = rows (); |
|
844 int nc = cols (); |
|
845 |
|
846 ComplexMatrix retval (*this); |
|
847 // Note the order of passing the rows and columns to account for |
|
848 // column-major storage. |
|
849 octave_fftw::fft2d (retval.fortran_vec (), nc, nr); |
|
850 |
|
851 return retval; |
|
852 } |
|
853 |
|
854 ComplexMatrix |
|
855 Matrix::ifourier2d (void) const |
|
856 { |
|
857 int nr = rows (); |
|
858 int nc = cols (); |
|
859 |
|
860 ComplexMatrix retval (*this); |
|
861 // Note the order of passing the rows and columns to account for |
|
862 // column-major storage. |
|
863 octave_fftw::ifft2d (retval.fortran_vec (), nc, nr); |
|
864 |
|
865 return retval; |
|
866 } |
|
867 |
|
868 #else |
|
869 |
458
|
870 ComplexMatrix |
|
871 Matrix::fourier (void) const |
|
872 { |
1948
|
873 ComplexMatrix retval; |
|
874 |
458
|
875 int nr = rows (); |
|
876 int nc = cols (); |
1948
|
877 |
458
|
878 int npts, nsamples; |
1948
|
879 |
458
|
880 if (nr == 1 || nc == 1) |
|
881 { |
|
882 npts = nr > nc ? nr : nc; |
|
883 nsamples = 1; |
|
884 } |
|
885 else |
|
886 { |
|
887 npts = nr; |
|
888 nsamples = nc; |
|
889 } |
|
890 |
|
891 int nn = 4*npts+15; |
1948
|
892 |
|
893 Array<Complex> wsave (nn); |
|
894 Complex *pwsave = wsave.fortran_vec (); |
|
895 |
3585
|
896 retval = ComplexMatrix (*this); |
1948
|
897 Complex *tmp_data = retval.fortran_vec (); |
|
898 |
3887
|
899 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
458
|
900 |
|
901 for (int j = 0; j < nsamples; j++) |
4153
|
902 { |
|
903 OCTAVE_QUIT; |
|
904 |
|
905 F77_FUNC (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
906 } |
1948
|
907 |
|
908 return retval; |
458
|
909 } |
|
910 |
|
911 ComplexMatrix |
|
912 Matrix::ifourier (void) const |
|
913 { |
1948
|
914 ComplexMatrix retval; |
|
915 |
458
|
916 int nr = rows (); |
|
917 int nc = cols (); |
1948
|
918 |
458
|
919 int npts, nsamples; |
1948
|
920 |
458
|
921 if (nr == 1 || nc == 1) |
|
922 { |
|
923 npts = nr > nc ? nr : nc; |
|
924 nsamples = 1; |
|
925 } |
|
926 else |
|
927 { |
|
928 npts = nr; |
|
929 nsamples = nc; |
|
930 } |
|
931 |
|
932 int nn = 4*npts+15; |
1948
|
933 |
|
934 Array<Complex> wsave (nn); |
|
935 Complex *pwsave = wsave.fortran_vec (); |
|
936 |
3585
|
937 retval = ComplexMatrix (*this); |
1948
|
938 Complex *tmp_data = retval.fortran_vec (); |
|
939 |
3887
|
940 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
458
|
941 |
|
942 for (int j = 0; j < nsamples; j++) |
4153
|
943 { |
|
944 OCTAVE_QUIT; |
|
945 |
|
946 F77_FUNC (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
|
947 } |
458
|
948 |
1321
|
949 for (int j = 0; j < npts*nsamples; j++) |
3572
|
950 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
458
|
951 |
1948
|
952 return retval; |
458
|
953 } |
|
954 |
677
|
955 ComplexMatrix |
|
956 Matrix::fourier2d (void) const |
|
957 { |
1948
|
958 ComplexMatrix retval; |
|
959 |
677
|
960 int nr = rows (); |
|
961 int nc = cols (); |
1948
|
962 |
677
|
963 int npts, nsamples; |
1948
|
964 |
677
|
965 if (nr == 1 || nc == 1) |
|
966 { |
|
967 npts = nr > nc ? nr : nc; |
|
968 nsamples = 1; |
|
969 } |
|
970 else |
|
971 { |
|
972 npts = nr; |
|
973 nsamples = nc; |
|
974 } |
|
975 |
|
976 int nn = 4*npts+15; |
1948
|
977 |
|
978 Array<Complex> wsave (nn); |
|
979 Complex *pwsave = wsave.fortran_vec (); |
|
980 |
3585
|
981 retval = ComplexMatrix (*this); |
1948
|
982 Complex *tmp_data = retval.fortran_vec (); |
|
983 |
3887
|
984 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
985 |
|
986 for (int j = 0; j < nsamples; j++) |
4153
|
987 { |
|
988 OCTAVE_QUIT; |
|
989 |
|
990 F77_FUNC (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
991 } |
677
|
992 |
|
993 npts = nc; |
|
994 nsamples = nr; |
|
995 nn = 4*npts+15; |
1948
|
996 |
|
997 wsave.resize (nn); |
|
998 pwsave = wsave.fortran_vec (); |
|
999 |
|
1000 Array<Complex> row (npts); |
|
1001 Complex *prow = row.fortran_vec (); |
|
1002 |
3887
|
1003 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1004 |
1321
|
1005 for (int j = 0; j < nsamples; j++) |
677
|
1006 { |
4153
|
1007 OCTAVE_QUIT; |
|
1008 |
677
|
1009 for (int i = 0; i < npts; i++) |
1948
|
1010 prow[i] = tmp_data[i*nr + j]; |
|
1011 |
3887
|
1012 F77_FUNC (cfftf, CFFTF) (npts, prow, pwsave); |
677
|
1013 |
1321
|
1014 for (int i = 0; i < npts; i++) |
1948
|
1015 tmp_data[i*nr + j] = prow[i]; |
677
|
1016 } |
|
1017 |
1948
|
1018 return retval; |
677
|
1019 } |
|
1020 |
|
1021 ComplexMatrix |
|
1022 Matrix::ifourier2d (void) const |
|
1023 { |
1948
|
1024 ComplexMatrix retval; |
|
1025 |
677
|
1026 int nr = rows (); |
|
1027 int nc = cols (); |
1948
|
1028 |
677
|
1029 int npts, nsamples; |
1948
|
1030 |
677
|
1031 if (nr == 1 || nc == 1) |
|
1032 { |
|
1033 npts = nr > nc ? nr : nc; |
|
1034 nsamples = 1; |
|
1035 } |
|
1036 else |
|
1037 { |
|
1038 npts = nr; |
|
1039 nsamples = nc; |
|
1040 } |
|
1041 |
|
1042 int nn = 4*npts+15; |
1948
|
1043 |
|
1044 Array<Complex> wsave (nn); |
|
1045 Complex *pwsave = wsave.fortran_vec (); |
|
1046 |
3585
|
1047 retval = ComplexMatrix (*this); |
1948
|
1048 Complex *tmp_data = retval.fortran_vec (); |
|
1049 |
3887
|
1050 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1051 |
|
1052 for (int j = 0; j < nsamples; j++) |
4153
|
1053 { |
|
1054 OCTAVE_QUIT; |
|
1055 |
|
1056 F77_FUNC (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
|
1057 } |
677
|
1058 |
1321
|
1059 for (int j = 0; j < npts*nsamples; j++) |
3572
|
1060 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
677
|
1061 |
|
1062 npts = nc; |
|
1063 nsamples = nr; |
|
1064 nn = 4*npts+15; |
1948
|
1065 |
|
1066 wsave.resize (nn); |
|
1067 pwsave = wsave.fortran_vec (); |
|
1068 |
|
1069 Array<Complex> row (npts); |
|
1070 Complex *prow = row.fortran_vec (); |
|
1071 |
3887
|
1072 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1073 |
1321
|
1074 for (int j = 0; j < nsamples; j++) |
677
|
1075 { |
4153
|
1076 OCTAVE_QUIT; |
|
1077 |
677
|
1078 for (int i = 0; i < npts; i++) |
1948
|
1079 prow[i] = tmp_data[i*nr + j]; |
|
1080 |
3887
|
1081 F77_FUNC (cfftb, CFFTB) (npts, prow, pwsave); |
677
|
1082 |
1321
|
1083 for (int i = 0; i < npts; i++) |
3572
|
1084 tmp_data[i*nr + j] = prow[i] / static_cast<double> (npts); |
677
|
1085 } |
|
1086 |
1948
|
1087 return retval; |
677
|
1088 } |
|
1089 |
3827
|
1090 #endif |
|
1091 |
458
|
1092 DET |
|
1093 Matrix::determinant (void) const |
|
1094 { |
|
1095 int info; |
|
1096 double rcond; |
4329
|
1097 return determinant (info, rcond, 0); |
458
|
1098 } |
|
1099 |
|
1100 DET |
|
1101 Matrix::determinant (int& info) const |
|
1102 { |
|
1103 double rcond; |
4329
|
1104 return determinant (info, rcond, 0); |
458
|
1105 } |
|
1106 |
|
1107 DET |
4329
|
1108 Matrix::determinant (int& info, double& rcond, int calc_cond) const |
458
|
1109 { |
|
1110 DET retval; |
|
1111 |
|
1112 int nr = rows (); |
|
1113 int nc = cols (); |
|
1114 |
|
1115 if (nr == 0 || nc == 0) |
|
1116 { |
|
1117 double d[2]; |
|
1118 d[0] = 1.0; |
|
1119 d[1] = 0.0; |
|
1120 retval = DET (d); |
|
1121 } |
|
1122 else |
|
1123 { |
1948
|
1124 Array<int> ipvt (nr); |
|
1125 int *pipvt = ipvt.fortran_vec (); |
|
1126 |
|
1127 Matrix atmp = *this; |
|
1128 double *tmp_data = atmp.fortran_vec (); |
|
1129 |
4329
|
1130 info = 0; |
|
1131 |
4330
|
1132 // Calculate the norm of the matrix, for later use. |
4329
|
1133 double anorm = 0; |
|
1134 if (calc_cond) |
|
1135 anorm = atmp.abs().sum().row(0).max(); |
|
1136 |
|
1137 F77_XFCN (dgetrf, DGETRF, (nr, nr, tmp_data, nr, pipvt, info)); |
1948
|
1138 |
|
1139 if (f77_exception_encountered) |
4329
|
1140 (*current_liboctave_error_handler) ("unrecoverable error in dgetrf"); |
458
|
1141 else |
|
1142 { |
4330
|
1143 // Throw-away extra info LAPACK gives so as to not change output. |
4509
|
1144 rcond = 0.0; |
|
1145 if (info != 0) |
1948
|
1146 { |
4509
|
1147 info = -1; |
|
1148 retval = DET (); |
4329
|
1149 } |
|
1150 else |
1948
|
1151 { |
4329
|
1152 if (calc_cond) |
|
1153 { |
4330
|
1154 // Now calc the condition number for non-singular matrix. |
4329
|
1155 char job = '1'; |
|
1156 Array<double> z (4 * nc); |
|
1157 double *pz = z.fortran_vec (); |
|
1158 Array<int> iz (nc); |
|
1159 int *piz = iz.fortran_vec (); |
|
1160 |
4552
|
1161 F77_XFCN (dgecon, DGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1162 nc, tmp_data, nr, anorm, |
|
1163 rcond, pz, piz, info |
|
1164 F77_CHAR_ARG_LEN (1))); |
4329
|
1165 |
|
1166 if (f77_exception_encountered) |
|
1167 (*current_liboctave_error_handler) |
|
1168 ("unrecoverable error in dgecon"); |
|
1169 } |
|
1170 |
4509
|
1171 if (info != 0) |
4329
|
1172 { |
|
1173 info = -1; |
|
1174 retval = DET (); |
|
1175 } |
|
1176 else |
|
1177 { |
|
1178 double d[2] = { 1., 0.}; |
|
1179 for (int i=0; i<nc; i++) |
|
1180 { |
|
1181 if (ipvt(i) != (i+1)) d[0] = -d[0]; |
|
1182 d[0] *= atmp(i,i); |
|
1183 if (d[0] == 0.) break; |
|
1184 while (fabs(d[0]) < 1.) |
|
1185 { |
|
1186 d[0] = 10. * d[0]; |
4509
|
1187 d[1] = d[1] - 1.0; |
4329
|
1188 } |
|
1189 while (fabs(d[0]) >= 10.) |
|
1190 { |
|
1191 d[0] = 0.1 * d[0]; |
4509
|
1192 d[1] = d[1] + 1.0; |
4329
|
1193 } |
|
1194 } |
|
1195 retval = DET (d); |
|
1196 } |
1948
|
1197 } |
458
|
1198 } |
|
1199 } |
|
1200 |
|
1201 return retval; |
|
1202 } |
|
1203 |
|
1204 Matrix |
|
1205 Matrix::solve (const Matrix& b) const |
|
1206 { |
|
1207 int info; |
|
1208 double rcond; |
4329
|
1209 return solve (b, info, rcond, 0); |
458
|
1210 } |
|
1211 |
|
1212 Matrix |
|
1213 Matrix::solve (const Matrix& b, int& info) const |
|
1214 { |
|
1215 double rcond; |
4329
|
1216 return solve (b, info, rcond, 0); |
458
|
1217 } |
|
1218 |
|
1219 Matrix |
532
|
1220 Matrix::solve (const Matrix& b, int& info, double& rcond) const |
458
|
1221 { |
3480
|
1222 return solve (b, info, rcond, 0); |
|
1223 } |
|
1224 |
|
1225 Matrix |
|
1226 Matrix::solve (const Matrix& b, int& info, double& rcond, |
|
1227 solve_singularity_handler sing_handler) const |
|
1228 { |
458
|
1229 Matrix retval; |
|
1230 |
|
1231 int nr = rows (); |
|
1232 int nc = cols (); |
1948
|
1233 |
458
|
1234 if (nr == 0 || nc == 0 || nr != nc || nr != b.rows ()) |
1948
|
1235 (*current_liboctave_error_handler) |
|
1236 ("matrix dimension mismatch solution of linear equations"); |
458
|
1237 else |
|
1238 { |
1948
|
1239 info = 0; |
|
1240 |
|
1241 Array<int> ipvt (nr); |
|
1242 int *pipvt = ipvt.fortran_vec (); |
|
1243 |
|
1244 Matrix atmp = *this; |
|
1245 double *tmp_data = atmp.fortran_vec (); |
|
1246 |
4329
|
1247 Array<double> z (4 * nc); |
|
1248 double *pz = z.fortran_vec (); |
|
1249 Array<int> iz (nc); |
|
1250 int *piz = iz.fortran_vec (); |
|
1251 |
4330
|
1252 // Calculate the norm of the matrix, for later use. |
4329
|
1253 double anorm = atmp.abs().sum().row(0).max(); |
|
1254 |
|
1255 F77_XFCN (dgetrf, DGETRF, (nr, nr, tmp_data, nr, pipvt, info)); |
1948
|
1256 |
|
1257 if (f77_exception_encountered) |
4329
|
1258 (*current_liboctave_error_handler) ("unrecoverable error in dgetrf"); |
1948
|
1259 else |
|
1260 { |
4330
|
1261 // Throw-away extra info LAPACK gives so as to not change output. |
4509
|
1262 rcond = 0.0; |
|
1263 if (info != 0) |
1948
|
1264 { |
|
1265 info = -2; |
3480
|
1266 |
|
1267 if (sing_handler) |
|
1268 sing_handler (rcond); |
|
1269 else |
|
1270 (*current_liboctave_error_handler) |
4329
|
1271 ("matrix singular to machine precision"); |
|
1272 |
|
1273 } |
|
1274 else |
1948
|
1275 { |
4330
|
1276 // Now calculate the condition number for non-singular matrix. |
4329
|
1277 char job = '1'; |
4552
|
1278 F77_XFCN (dgecon, DGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1279 nc, tmp_data, nr, anorm, |
|
1280 rcond, pz, piz, info |
|
1281 F77_CHAR_ARG_LEN (1))); |
4329
|
1282 |
|
1283 if (f77_exception_encountered) |
|
1284 (*current_liboctave_error_handler) |
|
1285 ("unrecoverable error in dgecon"); |
|
1286 |
4509
|
1287 if (info != 0) |
4329
|
1288 info = -2; |
|
1289 |
|
1290 volatile double rcond_plus_one = rcond + 1.0; |
|
1291 |
|
1292 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
1948
|
1293 { |
4329
|
1294 info = -2; |
|
1295 |
|
1296 if (sing_handler) |
|
1297 sing_handler (rcond); |
|
1298 else |
|
1299 (*current_liboctave_error_handler) |
|
1300 ("matrix singular to machine precision, rcond = %g", |
|
1301 rcond); |
|
1302 } |
|
1303 else |
|
1304 { |
|
1305 retval = b; |
|
1306 double *result = retval.fortran_vec (); |
|
1307 |
|
1308 int b_nc = b.cols (); |
|
1309 |
4587
|
1310 job = 'N'; |
4552
|
1311 F77_XFCN (dgetrs, DGETRS, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1312 nr, b_nc, tmp_data, nr, |
|
1313 pipvt, result, b.rows(), info |
|
1314 F77_CHAR_ARG_LEN (1))); |
4329
|
1315 |
1948
|
1316 if (f77_exception_encountered) |
4329
|
1317 (*current_liboctave_error_handler) |
|
1318 ("unrecoverable error in dgetrs"); |
1948
|
1319 } |
|
1320 } |
|
1321 } |
458
|
1322 } |
|
1323 |
|
1324 return retval; |
|
1325 } |
|
1326 |
|
1327 ComplexMatrix |
|
1328 Matrix::solve (const ComplexMatrix& b) const |
|
1329 { |
|
1330 ComplexMatrix tmp (*this); |
|
1331 return tmp.solve (b); |
|
1332 } |
|
1333 |
|
1334 ComplexMatrix |
|
1335 Matrix::solve (const ComplexMatrix& b, int& info) const |
|
1336 { |
|
1337 ComplexMatrix tmp (*this); |
|
1338 return tmp.solve (b, info); |
|
1339 } |
|
1340 |
|
1341 ComplexMatrix |
|
1342 Matrix::solve (const ComplexMatrix& b, int& info, double& rcond) const |
|
1343 { |
|
1344 ComplexMatrix tmp (*this); |
|
1345 return tmp.solve (b, info, rcond); |
|
1346 } |
|
1347 |
3480
|
1348 ComplexMatrix |
|
1349 Matrix::solve (const ComplexMatrix& b, int& info, double& rcond, |
|
1350 solve_singularity_handler sing_handler) const |
|
1351 { |
|
1352 ComplexMatrix tmp (*this); |
|
1353 return tmp.solve (b, info, rcond, sing_handler); |
|
1354 } |
|
1355 |
458
|
1356 ColumnVector |
|
1357 Matrix::solve (const ColumnVector& b) const |
|
1358 { |
|
1359 int info; double rcond; |
|
1360 return solve (b, info, rcond); |
|
1361 } |
|
1362 |
|
1363 ColumnVector |
|
1364 Matrix::solve (const ColumnVector& b, int& info) const |
|
1365 { |
|
1366 double rcond; |
|
1367 return solve (b, info, rcond); |
|
1368 } |
|
1369 |
|
1370 ColumnVector |
532
|
1371 Matrix::solve (const ColumnVector& b, int& info, double& rcond) const |
458
|
1372 { |
3480
|
1373 return solve (b, info, rcond, 0); |
|
1374 } |
|
1375 |
|
1376 ColumnVector |
|
1377 Matrix::solve (const ColumnVector& b, int& info, double& rcond, |
|
1378 solve_singularity_handler sing_handler) const |
|
1379 { |
458
|
1380 ColumnVector retval; |
|
1381 |
|
1382 int nr = rows (); |
|
1383 int nc = cols (); |
1948
|
1384 |
458
|
1385 if (nr == 0 || nc == 0 || nr != nc || nr != b.length ()) |
1948
|
1386 (*current_liboctave_error_handler) |
|
1387 ("matrix dimension mismatch solution of linear equations"); |
458
|
1388 else |
|
1389 { |
1948
|
1390 info = 0; |
|
1391 |
|
1392 Array<int> ipvt (nr); |
|
1393 int *pipvt = ipvt.fortran_vec (); |
|
1394 |
|
1395 Matrix atmp = *this; |
|
1396 double *tmp_data = atmp.fortran_vec (); |
|
1397 |
4329
|
1398 Array<double> z (4 * nc); |
|
1399 double *pz = z.fortran_vec (); |
|
1400 Array<int> iz (nc); |
|
1401 int *piz = iz.fortran_vec (); |
|
1402 |
4330
|
1403 // Calculate the norm of the matrix, for later use. |
4329
|
1404 double anorm = atmp.abs().sum().row(0).max(); |
|
1405 |
|
1406 F77_XFCN (dgetrf, DGETRF, (nr, nr, tmp_data, nr, pipvt, info)); |
1948
|
1407 |
|
1408 if (f77_exception_encountered) |
4329
|
1409 (*current_liboctave_error_handler) ("unrecoverable error in dgetrf"); |
1948
|
1410 else |
|
1411 { |
4330
|
1412 // Throw-away extra info LAPACK gives so as to not change output. |
4509
|
1413 rcond = 0.0; |
|
1414 if (info > 0) |
1948
|
1415 { |
|
1416 info = -2; |
3480
|
1417 |
|
1418 if (sing_handler) |
|
1419 sing_handler (rcond); |
|
1420 else |
|
1421 (*current_liboctave_error_handler) |
4329
|
1422 ("matrix singular to machine precision"); |
|
1423 |
|
1424 } |
|
1425 else |
1948
|
1426 { |
4330
|
1427 // Now calculate the condition number for non-singular matrix. |
4329
|
1428 char job = '1'; |
4552
|
1429 F77_XFCN (dgecon, DGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1430 nc, tmp_data, nr, anorm, |
|
1431 rcond, pz, piz, info |
|
1432 F77_CHAR_ARG_LEN (1))); |
4329
|
1433 |
1948
|
1434 if (f77_exception_encountered) |
4329
|
1435 (*current_liboctave_error_handler) |
|
1436 ("unrecoverable error in dgecon"); |
|
1437 |
4509
|
1438 if (info != 0) |
4329
|
1439 info = -2; |
|
1440 |
|
1441 volatile double rcond_plus_one = rcond + 1.0; |
|
1442 |
|
1443 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
1444 { |
|
1445 info = -2; |
|
1446 |
|
1447 if (sing_handler) |
|
1448 sing_handler (rcond); |
|
1449 else |
|
1450 (*current_liboctave_error_handler) |
|
1451 ("matrix singular to machine precision, rcond = %g", |
|
1452 rcond); |
|
1453 } |
|
1454 else |
|
1455 { |
|
1456 retval = b; |
|
1457 double *result = retval.fortran_vec (); |
|
1458 |
4587
|
1459 job = 'N'; |
4552
|
1460 F77_XFCN (dgetrs, DGETRS, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1461 nr, 1, tmp_data, nr, pipvt, |
|
1462 result, b.length(), info |
|
1463 F77_CHAR_ARG_LEN (1))); |
4329
|
1464 |
|
1465 if (f77_exception_encountered) |
|
1466 (*current_liboctave_error_handler) |
|
1467 ("unrecoverable error in dgetrs"); |
|
1468 } |
1948
|
1469 } |
|
1470 } |
458
|
1471 } |
4329
|
1472 |
458
|
1473 return retval; |
|
1474 } |
|
1475 |
|
1476 ComplexColumnVector |
|
1477 Matrix::solve (const ComplexColumnVector& b) const |
|
1478 { |
|
1479 ComplexMatrix tmp (*this); |
|
1480 return tmp.solve (b); |
|
1481 } |
|
1482 |
|
1483 ComplexColumnVector |
|
1484 Matrix::solve (const ComplexColumnVector& b, int& info) const |
|
1485 { |
|
1486 ComplexMatrix tmp (*this); |
|
1487 return tmp.solve (b, info); |
|
1488 } |
|
1489 |
|
1490 ComplexColumnVector |
|
1491 Matrix::solve (const ComplexColumnVector& b, int& info, double& rcond) const |
|
1492 { |
|
1493 ComplexMatrix tmp (*this); |
|
1494 return tmp.solve (b, info, rcond); |
|
1495 } |
|
1496 |
3480
|
1497 ComplexColumnVector |
|
1498 Matrix::solve (const ComplexColumnVector& b, int& info, double& rcond, |
|
1499 solve_singularity_handler sing_handler) const |
|
1500 { |
|
1501 ComplexMatrix tmp (*this); |
|
1502 return tmp.solve (b, info, rcond, sing_handler); |
|
1503 } |
|
1504 |
458
|
1505 Matrix |
|
1506 Matrix::lssolve (const Matrix& b) const |
|
1507 { |
|
1508 int info; |
|
1509 int rank; |
|
1510 return lssolve (b, info, rank); |
|
1511 } |
|
1512 |
|
1513 Matrix |
|
1514 Matrix::lssolve (const Matrix& b, int& info) const |
|
1515 { |
|
1516 int rank; |
|
1517 return lssolve (b, info, rank); |
|
1518 } |
|
1519 |
|
1520 Matrix |
|
1521 Matrix::lssolve (const Matrix& b, int& info, int& rank) const |
|
1522 { |
1948
|
1523 Matrix retval; |
|
1524 |
458
|
1525 int nrhs = b.cols (); |
|
1526 |
|
1527 int m = rows (); |
|
1528 int n = cols (); |
|
1529 |
|
1530 if (m == 0 || n == 0 || m != b.rows ()) |
1948
|
1531 (*current_liboctave_error_handler) |
|
1532 ("matrix dimension mismatch in solution of least squares problem"); |
|
1533 else |
458
|
1534 { |
1948
|
1535 Matrix atmp = *this; |
|
1536 double *tmp_data = atmp.fortran_vec (); |
|
1537 |
|
1538 int nrr = m > n ? m : n; |
3754
|
1539 Matrix result (nrr, nrhs, 0.0); |
1948
|
1540 |
|
1541 for (int j = 0; j < nrhs; j++) |
|
1542 for (int i = 0; i < m; i++) |
|
1543 result.elem (i, j) = b.elem (i, j); |
|
1544 |
|
1545 double *presult = result.fortran_vec (); |
|
1546 |
|
1547 int len_s = m < n ? m : n; |
|
1548 Array<double> s (len_s); |
|
1549 double *ps = s.fortran_vec (); |
|
1550 |
|
1551 double rcond = -1.0; |
|
1552 |
3752
|
1553 // Ask DGELSS what the dimension of WORK should be. |
|
1554 |
|
1555 int lwork = -1; |
|
1556 |
|
1557 Array<double> work (1); |
1948
|
1558 |
|
1559 F77_XFCN (dgelss, DGELSS, (m, n, nrhs, tmp_data, m, presult, nrr, ps, |
3752
|
1560 rcond, rank, work.fortran_vec (), |
|
1561 lwork, info)); |
1948
|
1562 |
|
1563 if (f77_exception_encountered) |
|
1564 (*current_liboctave_error_handler) ("unrecoverable error in dgelss"); |
|
1565 else |
|
1566 { |
3752
|
1567 lwork = static_cast<int> (work(0)); |
|
1568 work.resize (lwork); |
|
1569 |
|
1570 F77_XFCN (dgelss, DGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
1571 nrr, ps, rcond, rank, |
|
1572 work.fortran_vec (), lwork, info)); |
|
1573 |
|
1574 if (f77_exception_encountered) |
|
1575 (*current_liboctave_error_handler) |
|
1576 ("unrecoverable error in dgelss"); |
|
1577 else |
|
1578 { |
|
1579 retval.resize (n, nrhs); |
|
1580 for (int j = 0; j < nrhs; j++) |
|
1581 for (int i = 0; i < n; i++) |
|
1582 retval.elem (i, j) = result.elem (i, j); |
|
1583 } |
1948
|
1584 } |
458
|
1585 } |
|
1586 |
|
1587 return retval; |
|
1588 } |
|
1589 |
|
1590 ComplexMatrix |
|
1591 Matrix::lssolve (const ComplexMatrix& b) const |
|
1592 { |
|
1593 ComplexMatrix tmp (*this); |
1484
|
1594 int info; |
|
1595 int rank; |
|
1596 return tmp.lssolve (b, info, rank); |
458
|
1597 } |
|
1598 |
|
1599 ComplexMatrix |
|
1600 Matrix::lssolve (const ComplexMatrix& b, int& info) const |
|
1601 { |
|
1602 ComplexMatrix tmp (*this); |
1484
|
1603 int rank; |
|
1604 return tmp.lssolve (b, info, rank); |
458
|
1605 } |
|
1606 |
|
1607 ComplexMatrix |
|
1608 Matrix::lssolve (const ComplexMatrix& b, int& info, int& rank) const |
|
1609 { |
|
1610 ComplexMatrix tmp (*this); |
1484
|
1611 return tmp.lssolve (b, info, rank); |
458
|
1612 } |
|
1613 |
|
1614 ColumnVector |
|
1615 Matrix::lssolve (const ColumnVector& b) const |
|
1616 { |
|
1617 int info; |
1484
|
1618 int rank; |
|
1619 return lssolve (b, info, rank); |
458
|
1620 } |
|
1621 |
|
1622 ColumnVector |
|
1623 Matrix::lssolve (const ColumnVector& b, int& info) const |
|
1624 { |
|
1625 int rank; |
|
1626 return lssolve (b, info, rank); |
|
1627 } |
|
1628 |
|
1629 ColumnVector |
|
1630 Matrix::lssolve (const ColumnVector& b, int& info, int& rank) const |
|
1631 { |
1948
|
1632 ColumnVector retval; |
|
1633 |
458
|
1634 int nrhs = 1; |
|
1635 |
|
1636 int m = rows (); |
|
1637 int n = cols (); |
|
1638 |
|
1639 if (m == 0 || n == 0 || m != b.length ()) |
1948
|
1640 (*current_liboctave_error_handler) |
|
1641 ("matrix dimension mismatch in solution of least squares problem"); |
|
1642 else |
458
|
1643 { |
1948
|
1644 Matrix atmp = *this; |
|
1645 double *tmp_data = atmp.fortran_vec (); |
|
1646 |
|
1647 int nrr = m > n ? m : n; |
|
1648 ColumnVector result (nrr); |
|
1649 |
|
1650 for (int i = 0; i < m; i++) |
|
1651 result.elem (i) = b.elem (i); |
|
1652 |
|
1653 double *presult = result.fortran_vec (); |
|
1654 |
|
1655 int len_s = m < n ? m : n; |
|
1656 Array<double> s (len_s); |
|
1657 double *ps = s.fortran_vec (); |
|
1658 |
|
1659 double rcond = -1.0; |
|
1660 |
3752
|
1661 // Ask DGELSS what the dimension of WORK should be. |
|
1662 |
|
1663 int lwork = -1; |
|
1664 |
|
1665 Array<double> work (1); |
|
1666 |
|
1667 F77_XFCN (dgelss, DGELSS, (m, n, nrhs, tmp_data, m, presult, nrr, ps, |
|
1668 rcond, rank, work.fortran_vec (), |
|
1669 lwork, info)); |
1948
|
1670 |
|
1671 if (f77_exception_encountered) |
|
1672 (*current_liboctave_error_handler) ("unrecoverable error in dgelss"); |
|
1673 else |
|
1674 { |
3752
|
1675 lwork = static_cast<int> (work(0)); |
|
1676 work.resize (lwork); |
|
1677 |
|
1678 F77_XFCN (dgelss, DGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
1679 nrr, ps, rcond, rank, |
|
1680 work.fortran_vec (), lwork, info)); |
|
1681 |
|
1682 if (f77_exception_encountered) |
|
1683 (*current_liboctave_error_handler) |
|
1684 ("unrecoverable error in dgelss"); |
|
1685 else |
|
1686 { |
|
1687 retval.resize (n); |
|
1688 for (int i = 0; i < n; i++) |
|
1689 retval.elem (i) = result.elem (i); |
|
1690 } |
1948
|
1691 } |
458
|
1692 } |
|
1693 |
|
1694 return retval; |
|
1695 } |
|
1696 |
|
1697 ComplexColumnVector |
|
1698 Matrix::lssolve (const ComplexColumnVector& b) const |
|
1699 { |
|
1700 ComplexMatrix tmp (*this); |
|
1701 return tmp.lssolve (b); |
|
1702 } |
|
1703 |
|
1704 ComplexColumnVector |
|
1705 Matrix::lssolve (const ComplexColumnVector& b, int& info) const |
|
1706 { |
|
1707 ComplexMatrix tmp (*this); |
|
1708 return tmp.lssolve (b, info); |
|
1709 } |
|
1710 |
|
1711 ComplexColumnVector |
|
1712 Matrix::lssolve (const ComplexColumnVector& b, int& info, int& rank) const |
|
1713 { |
|
1714 ComplexMatrix tmp (*this); |
|
1715 return tmp.lssolve (b, info, rank); |
|
1716 } |
|
1717 |
1819
|
1718 // Constants for matrix exponential calculation. |
|
1719 |
|
1720 static double padec [] = |
|
1721 { |
|
1722 5.0000000000000000e-1, |
|
1723 1.1666666666666667e-1, |
|
1724 1.6666666666666667e-2, |
|
1725 1.6025641025641026e-3, |
|
1726 1.0683760683760684e-4, |
|
1727 4.8562548562548563e-6, |
|
1728 1.3875013875013875e-7, |
|
1729 1.9270852604185938e-9, |
|
1730 }; |
|
1731 |
|
1732 Matrix |
|
1733 Matrix::expm (void) const |
|
1734 { |
|
1735 Matrix retval; |
|
1736 |
|
1737 Matrix m = *this; |
|
1738 |
|
1739 int nc = columns (); |
|
1740 |
3130
|
1741 // Preconditioning step 1: trace normalization to reduce dynamic |
|
1742 // range of poles, but avoid making stable eigenvalues unstable. |
|
1743 |
1819
|
1744 // trace shift value |
3331
|
1745 volatile double trshift = 0.0; |
1819
|
1746 |
|
1747 for (int i = 0; i < nc; i++) |
|
1748 trshift += m.elem (i, i); |
|
1749 |
|
1750 trshift /= nc; |
|
1751 |
3130
|
1752 if (trshift > 0.0) |
|
1753 { |
|
1754 for (int i = 0; i < nc; i++) |
|
1755 m.elem (i, i) -= trshift; |
|
1756 } |
1819
|
1757 |
3331
|
1758 // Preconditioning step 2: balancing; code follows development |
|
1759 // in AEPBAL |
|
1760 |
|
1761 double *p_m = m.fortran_vec (); |
|
1762 |
3468
|
1763 int info, ilo, ihi, ilos, ihis; |
|
1764 Array<double> dpermute (nc); |
|
1765 Array<double> dscale (nc); |
3466
|
1766 |
3468
|
1767 // permutation first |
|
1768 char job = 'P'; |
4552
|
1769 F77_XFCN (dgebal, DGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1770 nc, p_m, nc, ilo, ihi, |
|
1771 dpermute.fortran_vec (), info |
|
1772 F77_CHAR_ARG_LEN (1))); |
3466
|
1773 |
3468
|
1774 // then scaling |
|
1775 job = 'S'; |
4552
|
1776 F77_XFCN (dgebal, DGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1777 nc, p_m, nc, ilos, ihis, |
|
1778 dscale.fortran_vec (), info |
|
1779 F77_CHAR_ARG_LEN (1))); |
3331
|
1780 |
|
1781 if (f77_exception_encountered) |
|
1782 { |
|
1783 (*current_liboctave_error_handler) ("unrecoverable error in dgebal"); |
|
1784 return retval; |
|
1785 } |
|
1786 |
1819
|
1787 // Preconditioning step 3: scaling. |
3331
|
1788 |
1819
|
1789 ColumnVector work(nc); |
3130
|
1790 double inf_norm; |
3331
|
1791 |
4552
|
1792 F77_XFCN (xdlange, XDLANGE, (F77_CONST_CHAR_ARG2 ("I", 1), |
|
1793 nc, nc, m.fortran_vec (), nc, |
|
1794 work.fortran_vec (), inf_norm |
|
1795 F77_CHAR_ARG_LEN (1))); |
3331
|
1796 |
|
1797 if (f77_exception_encountered) |
|
1798 { |
|
1799 (*current_liboctave_error_handler) ("unrecoverable error in dlange"); |
|
1800 return retval; |
|
1801 } |
1819
|
1802 |
|
1803 int sqpow = (int) (inf_norm > 0.0 |
|
1804 ? (1.0 + log (inf_norm) / log (2.0)) |
|
1805 : 0.0); |
3331
|
1806 |
1819
|
1807 // Check whether we need to square at all. |
3331
|
1808 |
1819
|
1809 if (sqpow < 0) |
|
1810 sqpow = 0; |
3331
|
1811 |
1819
|
1812 if (sqpow > 0) |
|
1813 { |
|
1814 double scale_factor = 1.0; |
|
1815 for (int i = 0; i < sqpow; i++) |
|
1816 scale_factor *= 2.0; |
3331
|
1817 |
1819
|
1818 m = m / scale_factor; |
|
1819 } |
3331
|
1820 |
1819
|
1821 // npp, dpp: pade' approx polynomial matrices. |
3331
|
1822 |
1819
|
1823 Matrix npp (nc, nc, 0.0); |
|
1824 Matrix dpp = npp; |
3331
|
1825 |
1819
|
1826 // Now powers a^8 ... a^1. |
3331
|
1827 |
1819
|
1828 int minus_one_j = -1; |
|
1829 for (int j = 7; j >= 0; j--) |
|
1830 { |
3573
|
1831 npp = m * npp + padec[j] * m; |
|
1832 dpp = m * dpp + (minus_one_j * padec[j]) * m; |
1819
|
1833 minus_one_j *= -1; |
|
1834 } |
3331
|
1835 |
1819
|
1836 // Zero power. |
3331
|
1837 |
1819
|
1838 dpp = -dpp; |
3130
|
1839 for (int j = 0; j < nc; j++) |
1819
|
1840 { |
|
1841 npp.elem (j, j) += 1.0; |
|
1842 dpp.elem (j, j) += 1.0; |
|
1843 } |
3331
|
1844 |
1819
|
1845 // Compute pade approximation = inverse (dpp) * npp. |
|
1846 |
3331
|
1847 retval = dpp.solve (npp, info); |
|
1848 |
1819
|
1849 // Reverse preconditioning step 3: repeated squaring. |
3331
|
1850 |
1819
|
1851 while (sqpow) |
|
1852 { |
|
1853 retval = retval * retval; |
|
1854 sqpow--; |
|
1855 } |
3331
|
1856 |
1819
|
1857 // Reverse preconditioning step 2: inverse balancing. |
3466
|
1858 // apply inverse scaling to computed exponential |
3468
|
1859 for (int i = 0; i < nc; i++) |
|
1860 for (int j = 0; j < nc; j++) |
|
1861 retval(i,j) *= dscale(i) / dscale(j); |
3466
|
1862 |
4153
|
1863 OCTAVE_QUIT; |
|
1864 |
3466
|
1865 // construct balancing permutation vector |
3468
|
1866 Array<int> ipermute (nc); |
|
1867 for (int i = 0; i < nc; i++) |
|
1868 ipermute(i) = i; // identity permutation |
3466
|
1869 |
|
1870 // leading permutations in forward order |
3468
|
1871 for (int i = 0; i < (ilo-1); i++) |
|
1872 { |
|
1873 int swapidx = static_cast<int> (dpermute(i)) - 1; |
|
1874 int tmp = ipermute(i); |
|
1875 ipermute(i) = ipermute (swapidx); |
|
1876 ipermute(swapidx) = tmp; |
|
1877 } |
3466
|
1878 |
|
1879 // trailing permutations must be done in reverse order |
3468
|
1880 for (int i = nc - 1; i >= ihi; i--) |
|
1881 { |
|
1882 int swapidx = static_cast<int> (dpermute(i)) - 1; |
|
1883 int tmp = ipermute(i); |
|
1884 ipermute(i) = ipermute(swapidx); |
|
1885 ipermute(swapidx) = tmp; |
|
1886 } |
3466
|
1887 |
|
1888 // construct inverse balancing permutation vector |
3468
|
1889 Array<int> invpvec (nc); |
|
1890 for (int i = 0; i < nc; i++) |
|
1891 invpvec(ipermute(i)) = i; // Thanks to R. A. Lippert for this method |
4153
|
1892 |
|
1893 OCTAVE_QUIT; |
3466
|
1894 |
|
1895 Matrix tmpMat = retval; |
3468
|
1896 for (int i = 0; i < nc; i++) |
|
1897 for (int j = 0; j < nc; j++) |
|
1898 retval(i,j) = tmpMat(invpvec(i),invpvec(j)); |
3466
|
1899 |
1819
|
1900 // Reverse preconditioning step 1: fix trace normalization. |
3331
|
1901 |
3130
|
1902 if (trshift > 0.0) |
|
1903 retval = exp (trshift) * retval; |
|
1904 |
|
1905 return retval; |
1819
|
1906 } |
|
1907 |
458
|
1908 Matrix& |
|
1909 Matrix::operator += (const DiagMatrix& a) |
|
1910 { |
2385
|
1911 int nr = rows (); |
|
1912 int nc = cols (); |
|
1913 |
|
1914 int a_nr = a.rows (); |
|
1915 int a_nc = a.cols (); |
|
1916 |
|
1917 if (nr != a_nr || nc != a_nc) |
458
|
1918 { |
2385
|
1919 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
1920 return *this; |
|
1921 } |
|
1922 |
|
1923 for (int i = 0; i < a.length (); i++) |
|
1924 elem (i, i) += a.elem (i, i); |
|
1925 |
|
1926 return *this; |
|
1927 } |
|
1928 |
|
1929 Matrix& |
|
1930 Matrix::operator -= (const DiagMatrix& a) |
|
1931 { |
2385
|
1932 int nr = rows (); |
|
1933 int nc = cols (); |
|
1934 |
|
1935 int a_nr = a.rows (); |
|
1936 int a_nc = a.cols (); |
|
1937 |
|
1938 if (nr != a_nr || nc != a_nc) |
458
|
1939 { |
2385
|
1940 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
1941 return *this; |
|
1942 } |
|
1943 |
|
1944 for (int i = 0; i < a.length (); i++) |
|
1945 elem (i, i) -= a.elem (i, i); |
|
1946 |
|
1947 return *this; |
|
1948 } |
|
1949 |
|
1950 // unary operations |
|
1951 |
2964
|
1952 boolMatrix |
458
|
1953 Matrix::operator ! (void) const |
|
1954 { |
|
1955 int nr = rows (); |
|
1956 int nc = cols (); |
|
1957 |
2964
|
1958 boolMatrix b (nr, nc); |
458
|
1959 |
|
1960 for (int j = 0; j < nc; j++) |
|
1961 for (int i = 0; i < nr; i++) |
|
1962 b.elem (i, j) = ! elem (i, j); |
|
1963 |
|
1964 return b; |
|
1965 } |
|
1966 |
1205
|
1967 // column vector by row vector -> matrix operations |
458
|
1968 |
1205
|
1969 Matrix |
|
1970 operator * (const ColumnVector& v, const RowVector& a) |
458
|
1971 { |
1948
|
1972 Matrix retval; |
|
1973 |
1205
|
1974 int len = v.length (); |
3233
|
1975 |
|
1976 if (len != 0) |
1205
|
1977 { |
3233
|
1978 int a_len = a.length (); |
|
1979 |
|
1980 retval.resize (len, a_len); |
|
1981 double *c = retval.fortran_vec (); |
|
1982 |
4552
|
1983 F77_XFCN (dgemm, DGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
1984 F77_CONST_CHAR_ARG2 ("N", 1), |
|
1985 len, a_len, 1, 1.0, v.data (), len, |
|
1986 a.data (), 1, 0.0, c, len |
|
1987 F77_CHAR_ARG_LEN (1) |
|
1988 F77_CHAR_ARG_LEN (1))); |
3233
|
1989 |
|
1990 if (f77_exception_encountered) |
|
1991 (*current_liboctave_error_handler) |
|
1992 ("unrecoverable error in dgemm"); |
1205
|
1993 } |
458
|
1994 |
1948
|
1995 return retval; |
458
|
1996 } |
|
1997 |
|
1998 // other operations. |
|
1999 |
|
2000 Matrix |
2676
|
2001 Matrix::map (d_d_Mapper f) const |
1205
|
2002 { |
2676
|
2003 Matrix b (*this); |
|
2004 return b.apply (f); |
1205
|
2005 } |
|
2006 |
3248
|
2007 boolMatrix |
|
2008 Matrix::map (b_d_Mapper f) const |
|
2009 { |
|
2010 int nr = rows (); |
|
2011 int nc = cols (); |
|
2012 |
|
2013 boolMatrix retval (nr, nc); |
|
2014 |
|
2015 for (int j = 0; j < nc; j++) |
|
2016 for (int i = 0; i < nr; i++) |
|
2017 retval(i,j) = f (elem(i,j)); |
|
2018 |
|
2019 return retval; |
|
2020 } |
|
2021 |
2676
|
2022 Matrix& |
|
2023 Matrix::apply (d_d_Mapper f) |
458
|
2024 { |
|
2025 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2026 |
|
2027 for (int i = 0; i < length (); i++) |
|
2028 d[i] = f (d[i]); |
2676
|
2029 |
|
2030 return *this; |
458
|
2031 } |
|
2032 |
2385
|
2033 bool |
4431
|
2034 Matrix::any_element_is_negative (bool neg_zero) const |
2385
|
2035 { |
|
2036 int nr = rows (); |
|
2037 int nc = cols (); |
|
2038 |
4431
|
2039 if (neg_zero) |
|
2040 { |
|
2041 for (int j = 0; j < nc; j++) |
|
2042 for (int i = 0; i < nr; i++) |
|
2043 if (lo_ieee_signbit (elem (i, j))) |
|
2044 return true; |
|
2045 } |
|
2046 else |
|
2047 { |
|
2048 for (int j = 0; j < nc; j++) |
|
2049 for (int i = 0; i < nr; i++) |
|
2050 if (elem (i, j) < 0) |
|
2051 return true; |
|
2052 } |
2385
|
2053 |
|
2054 return false; |
|
2055 } |
|
2056 |
|
2057 |
|
2058 bool |
|
2059 Matrix::any_element_is_inf_or_nan (void) const |
|
2060 { |
|
2061 int nr = rows (); |
|
2062 int nc = cols (); |
|
2063 |
|
2064 for (int j = 0; j < nc; j++) |
|
2065 for (int i = 0; i < nr; i++) |
|
2066 { |
|
2067 double val = elem (i, j); |
|
2068 if (xisinf (val) || xisnan (val)) |
|
2069 return 1; |
|
2070 } |
|
2071 return 0; |
|
2072 } |
|
2073 |
|
2074 bool |
|
2075 Matrix::all_elements_are_int_or_inf_or_nan (void) const |
|
2076 { |
|
2077 int nr = rows (); |
|
2078 int nc = cols (); |
|
2079 |
|
2080 for (int j = 0; j < nc; j++) |
|
2081 for (int i = 0; i < nr; i++) |
|
2082 { |
|
2083 double val = elem (i, j); |
|
2084 if (xisnan (val) || D_NINT (val) == val) |
|
2085 continue; |
|
2086 else |
|
2087 return false; |
|
2088 } |
|
2089 |
|
2090 return true; |
|
2091 } |
|
2092 |
1968
|
2093 // Return nonzero if any element of M is not an integer. Also extract |
|
2094 // the largest and smallest values and return them in MAX_VAL and MIN_VAL. |
|
2095 |
2385
|
2096 bool |
1968
|
2097 Matrix::all_integers (double& max_val, double& min_val) const |
|
2098 { |
|
2099 int nr = rows (); |
|
2100 int nc = cols (); |
|
2101 |
|
2102 if (nr > 0 && nc > 0) |
|
2103 { |
|
2104 max_val = elem (0, 0); |
|
2105 min_val = elem (0, 0); |
|
2106 } |
|
2107 else |
2385
|
2108 return false; |
1968
|
2109 |
|
2110 for (int j = 0; j < nc; j++) |
|
2111 for (int i = 0; i < nr; i++) |
|
2112 { |
|
2113 double val = elem (i, j); |
|
2114 |
|
2115 if (val > max_val) |
|
2116 max_val = val; |
|
2117 |
|
2118 if (val < min_val) |
|
2119 min_val = val; |
|
2120 |
|
2121 if (D_NINT (val) != val) |
2385
|
2122 return false; |
1968
|
2123 } |
2385
|
2124 |
|
2125 return true; |
1968
|
2126 } |
|
2127 |
2385
|
2128 bool |
1968
|
2129 Matrix::too_large_for_float (void) const |
|
2130 { |
|
2131 int nr = rows (); |
2385
|
2132 int nc = cols (); |
1968
|
2133 |
|
2134 for (int j = 0; j < nc; j++) |
|
2135 for (int i = 0; i < nr; i++) |
|
2136 { |
|
2137 double val = elem (i, j); |
|
2138 |
|
2139 if (val > FLT_MAX || val < FLT_MIN) |
2385
|
2140 return true; |
1968
|
2141 } |
|
2142 |
2385
|
2143 return false; |
1968
|
2144 } |
|
2145 |
4015
|
2146 // XXX FIXME XXX Do these really belong here? Maybe they should be |
|
2147 // in a base class? |
458
|
2148 |
2832
|
2149 boolMatrix |
4015
|
2150 Matrix::all (int dim) const |
458
|
2151 { |
4015
|
2152 MX_ALL_OP (dim); |
458
|
2153 } |
|
2154 |
2832
|
2155 boolMatrix |
4015
|
2156 Matrix::any (int dim) const |
458
|
2157 { |
4015
|
2158 MX_ANY_OP (dim); |
458
|
2159 } |
|
2160 |
|
2161 Matrix |
3723
|
2162 Matrix::cumprod (int dim) const |
458
|
2163 { |
4015
|
2164 MX_CUMULATIVE_OP (Matrix, double, *=); |
458
|
2165 } |
|
2166 |
|
2167 Matrix |
3723
|
2168 Matrix::cumsum (int dim) const |
458
|
2169 { |
4015
|
2170 MX_CUMULATIVE_OP (Matrix, double, +=); |
458
|
2171 } |
|
2172 |
|
2173 Matrix |
3723
|
2174 Matrix::prod (int dim) const |
458
|
2175 { |
3864
|
2176 MX_REDUCTION_OP (Matrix, *=, 1.0, 1.0); |
458
|
2177 } |
|
2178 |
|
2179 Matrix |
3723
|
2180 Matrix::sum (int dim) const |
458
|
2181 { |
3864
|
2182 MX_REDUCTION_OP (Matrix, +=, 0.0, 0.0); |
458
|
2183 } |
|
2184 |
|
2185 Matrix |
3723
|
2186 Matrix::sumsq (int dim) const |
458
|
2187 { |
3864
|
2188 #define ROW_EXPR \ |
|
2189 double d = elem (i, j); \ |
|
2190 retval.elem (i, 0) += d * d |
|
2191 |
|
2192 #define COL_EXPR \ |
|
2193 double d = elem (i, j); \ |
|
2194 retval.elem (0, j) += d * d |
|
2195 |
|
2196 MX_BASE_REDUCTION_OP (Matrix, ROW_EXPR, COL_EXPR, 0.0, 0.0); |
|
2197 |
|
2198 #undef ROW_EXPR |
|
2199 #undef COL_EXPR |
458
|
2200 } |
|
2201 |
2385
|
2202 Matrix |
|
2203 Matrix::abs (void) const |
|
2204 { |
|
2205 int nr = rows (); |
|
2206 int nc = cols (); |
|
2207 |
|
2208 Matrix retval (nr, nc); |
|
2209 |
|
2210 for (int j = 0; j < nc; j++) |
|
2211 for (int i = 0; i < nr; i++) |
|
2212 retval (i, j) = fabs (elem (i, j)); |
|
2213 |
|
2214 return retval; |
|
2215 } |
|
2216 |
458
|
2217 ColumnVector |
|
2218 Matrix::diag (void) const |
|
2219 { |
|
2220 return diag (0); |
|
2221 } |
|
2222 |
|
2223 ColumnVector |
|
2224 Matrix::diag (int k) const |
|
2225 { |
|
2226 int nnr = rows (); |
|
2227 int nnc = cols (); |
|
2228 if (k > 0) |
|
2229 nnc -= k; |
|
2230 else if (k < 0) |
|
2231 nnr += k; |
|
2232 |
|
2233 ColumnVector d; |
|
2234 |
|
2235 if (nnr > 0 && nnc > 0) |
|
2236 { |
|
2237 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
2238 |
|
2239 d.resize (ndiag); |
|
2240 |
|
2241 if (k > 0) |
|
2242 { |
|
2243 for (int i = 0; i < ndiag; i++) |
|
2244 d.elem (i) = elem (i, i+k); |
|
2245 } |
4509
|
2246 else if (k < 0) |
458
|
2247 { |
|
2248 for (int i = 0; i < ndiag; i++) |
|
2249 d.elem (i) = elem (i-k, i); |
|
2250 } |
|
2251 else |
|
2252 { |
|
2253 for (int i = 0; i < ndiag; i++) |
|
2254 d.elem (i) = elem (i, i); |
|
2255 } |
|
2256 } |
|
2257 else |
4513
|
2258 (*current_liboctave_error_handler) |
|
2259 ("diag: requested diagonal out of range"); |
458
|
2260 |
|
2261 return d; |
|
2262 } |
|
2263 |
|
2264 ColumnVector |
|
2265 Matrix::row_min (void) const |
|
2266 { |
4587
|
2267 Array<int> dummy_idx; |
|
2268 return row_min (dummy_idx); |
458
|
2269 } |
|
2270 |
|
2271 ColumnVector |
4587
|
2272 Matrix::row_min (Array<int>& idx_arg) const |
458
|
2273 { |
|
2274 ColumnVector result; |
|
2275 |
|
2276 int nr = rows (); |
|
2277 int nc = cols (); |
|
2278 |
|
2279 if (nr > 0 && nc > 0) |
|
2280 { |
|
2281 result.resize (nr); |
4587
|
2282 idx_arg.resize (nr); |
458
|
2283 |
|
2284 for (int i = 0; i < nr; i++) |
|
2285 { |
4469
|
2286 int idx_j; |
|
2287 |
|
2288 double tmp_min = octave_NaN; |
|
2289 |
|
2290 for (idx_j = 0; idx_j < nc; idx_j++) |
2354
|
2291 { |
4469
|
2292 tmp_min = elem (i, idx_j); |
|
2293 |
|
2294 if (! octave_is_NaN_or_NA (tmp_min)) |
|
2295 break; |
|
2296 } |
|
2297 |
|
2298 for (int j = idx_j+1; j < nc; j++) |
|
2299 { |
|
2300 double tmp = elem (i, j); |
|
2301 |
|
2302 if (octave_is_NaN_or_NA (tmp)) |
|
2303 continue; |
|
2304 else if (tmp < tmp_min) |
2354
|
2305 { |
4469
|
2306 idx_j = j; |
|
2307 tmp_min = tmp; |
2354
|
2308 } |
|
2309 } |
|
2310 |
4469
|
2311 result.elem (i) = tmp_min; |
4587
|
2312 idx_arg.elem (i) = octave_is_NaN_or_NA (tmp_min) ? 0 : idx_j; |
458
|
2313 } |
|
2314 } |
|
2315 |
|
2316 return result; |
|
2317 } |
|
2318 |
|
2319 ColumnVector |
|
2320 Matrix::row_max (void) const |
|
2321 { |
4587
|
2322 Array<int> dummy_idx; |
|
2323 return row_max (dummy_idx); |
458
|
2324 } |
|
2325 |
|
2326 ColumnVector |
4587
|
2327 Matrix::row_max (Array<int>& idx_arg) const |
458
|
2328 { |
|
2329 ColumnVector result; |
|
2330 |
|
2331 int nr = rows (); |
|
2332 int nc = cols (); |
|
2333 |
|
2334 if (nr > 0 && nc > 0) |
|
2335 { |
|
2336 result.resize (nr); |
4587
|
2337 idx_arg.resize (nr); |
458
|
2338 |
|
2339 for (int i = 0; i < nr; i++) |
|
2340 { |
4469
|
2341 int idx_j; |
|
2342 |
|
2343 double tmp_max = octave_NaN; |
|
2344 |
|
2345 for (idx_j = 0; idx_j < nc; idx_j++) |
2354
|
2346 { |
4469
|
2347 tmp_max = elem (i, idx_j); |
|
2348 |
|
2349 if (! octave_is_NaN_or_NA (tmp_max)) |
|
2350 break; |
|
2351 } |
|
2352 |
|
2353 for (int j = idx_j+1; j < nc; j++) |
|
2354 { |
|
2355 double tmp = elem (i, j); |
|
2356 |
|
2357 if (octave_is_NaN_or_NA (tmp)) |
|
2358 continue; |
|
2359 else if (tmp > tmp_max) |
2354
|
2360 { |
4469
|
2361 idx_j = j; |
|
2362 tmp_max = tmp; |
2354
|
2363 } |
|
2364 } |
|
2365 |
4469
|
2366 result.elem (i) = tmp_max; |
4587
|
2367 idx_arg.elem (i) = octave_is_NaN_or_NA (tmp_max) ? 0 : idx_j; |
458
|
2368 } |
|
2369 } |
|
2370 |
|
2371 return result; |
|
2372 } |
|
2373 |
|
2374 RowVector |
|
2375 Matrix::column_min (void) const |
|
2376 { |
4587
|
2377 Array<int> dummy_idx; |
|
2378 return column_min (dummy_idx); |
458
|
2379 } |
2354
|
2380 |
458
|
2381 RowVector |
4587
|
2382 Matrix::column_min (Array<int>& idx_arg) const |
458
|
2383 { |
|
2384 RowVector result; |
|
2385 |
|
2386 int nr = rows (); |
|
2387 int nc = cols (); |
|
2388 |
|
2389 if (nr > 0 && nc > 0) |
|
2390 { |
|
2391 result.resize (nc); |
4587
|
2392 idx_arg.resize (nc); |
458
|
2393 |
|
2394 for (int j = 0; j < nc; j++) |
|
2395 { |
4469
|
2396 int idx_i; |
|
2397 |
|
2398 double tmp_min = octave_NaN; |
|
2399 |
|
2400 for (idx_i = 0; idx_i < nr; idx_i++) |
2354
|
2401 { |
4469
|
2402 tmp_min = elem (idx_i, j); |
|
2403 |
|
2404 if (! octave_is_NaN_or_NA (tmp_min)) |
|
2405 break; |
|
2406 } |
|
2407 |
|
2408 for (int i = idx_i+1; i < nr; i++) |
|
2409 { |
|
2410 double tmp = elem (i, j); |
|
2411 |
|
2412 if (octave_is_NaN_or_NA (tmp)) |
|
2413 continue; |
|
2414 else if (tmp < tmp_min) |
2354
|
2415 { |
4469
|
2416 idx_i = i; |
|
2417 tmp_min = tmp; |
2354
|
2418 } |
|
2419 } |
|
2420 |
4469
|
2421 result.elem (j) = tmp_min; |
4587
|
2422 idx_arg.elem (j) = octave_is_NaN_or_NA (tmp_min) ? 0 : idx_i; |
458
|
2423 } |
|
2424 } |
|
2425 |
|
2426 return result; |
|
2427 } |
|
2428 |
2354
|
2429 RowVector |
|
2430 Matrix::column_max (void) const |
|
2431 { |
4587
|
2432 Array<int> dummy_idx; |
|
2433 return column_max (dummy_idx); |
2354
|
2434 } |
458
|
2435 |
|
2436 RowVector |
4587
|
2437 Matrix::column_max (Array<int>& idx_arg) const |
458
|
2438 { |
|
2439 RowVector result; |
|
2440 |
|
2441 int nr = rows (); |
|
2442 int nc = cols (); |
|
2443 |
|
2444 if (nr > 0 && nc > 0) |
|
2445 { |
|
2446 result.resize (nc); |
4587
|
2447 idx_arg.resize (nc); |
458
|
2448 |
|
2449 for (int j = 0; j < nc; j++) |
|
2450 { |
4469
|
2451 int idx_i; |
|
2452 |
|
2453 double tmp_max = octave_NaN; |
|
2454 |
|
2455 for (idx_i = 0; idx_i < nr; idx_i++) |
2354
|
2456 { |
4469
|
2457 tmp_max = elem (idx_i, j); |
|
2458 |
|
2459 if (! octave_is_NaN_or_NA (tmp_max)) |
|
2460 break; |
|
2461 } |
|
2462 |
|
2463 for (int i = idx_i+1; i < nr; i++) |
|
2464 { |
|
2465 double tmp = elem (i, j); |
|
2466 |
|
2467 if (octave_is_NaN_or_NA (tmp)) |
|
2468 continue; |
|
2469 else if (tmp > tmp_max) |
2354
|
2470 { |
4469
|
2471 idx_i = i; |
|
2472 tmp_max = tmp; |
2354
|
2473 } |
|
2474 } |
|
2475 |
4469
|
2476 result.elem (j) = tmp_max; |
4587
|
2477 idx_arg.elem (j) = octave_is_NaN_or_NA (tmp_max) ? 0 : idx_i; |
458
|
2478 } |
|
2479 } |
|
2480 |
|
2481 return result; |
|
2482 } |
|
2483 |
3504
|
2484 std::ostream& |
|
2485 operator << (std::ostream& os, const Matrix& a) |
458
|
2486 { |
|
2487 for (int i = 0; i < a.rows (); i++) |
|
2488 { |
|
2489 for (int j = 0; j < a.cols (); j++) |
4130
|
2490 { |
|
2491 os << " "; |
|
2492 octave_write_double (os, a.elem (i, j)); |
|
2493 } |
458
|
2494 os << "\n"; |
|
2495 } |
|
2496 return os; |
|
2497 } |
|
2498 |
3504
|
2499 std::istream& |
|
2500 operator >> (std::istream& is, Matrix& a) |
458
|
2501 { |
|
2502 int nr = a.rows (); |
|
2503 int nc = a.cols (); |
|
2504 |
|
2505 if (nr < 1 || nc < 1) |
3504
|
2506 is.clear (std::ios::badbit); |
458
|
2507 else |
|
2508 { |
|
2509 double tmp; |
|
2510 for (int i = 0; i < nr; i++) |
|
2511 for (int j = 0; j < nc; j++) |
|
2512 { |
4130
|
2513 tmp = octave_read_double (is); |
458
|
2514 if (is) |
|
2515 a.elem (i, j) = tmp; |
|
2516 else |
2795
|
2517 goto done; |
458
|
2518 } |
|
2519 } |
|
2520 |
2795
|
2521 done: |
|
2522 |
458
|
2523 return is; |
|
2524 } |
|
2525 |
2317
|
2526 template <class T> |
3504
|
2527 void |
|
2528 read_int (std::istream& is, bool swap_bytes, T& val) |
2317
|
2529 { |
3145
|
2530 is.read (X_CAST (char *, &val), sizeof (T)); |
2317
|
2531 |
4282
|
2532 int t = sizeof (T); |
|
2533 |
|
2534 if (swap_bytes && t > 1) |
2317
|
2535 { |
4282
|
2536 switch (t) |
2317
|
2537 { |
|
2538 case 2: |
3145
|
2539 swap_2_bytes (X_CAST (char *, &val)); |
2317
|
2540 break; |
|
2541 |
|
2542 case 4: |
3145
|
2543 swap_4_bytes (X_CAST (char *, &val)); |
2317
|
2544 break; |
|
2545 |
|
2546 case 8: |
3145
|
2547 swap_8_bytes (X_CAST (char *, &val)); |
2317
|
2548 break; |
|
2549 |
|
2550 default: |
|
2551 (*current_liboctave_error_handler) |
|
2552 ("read_int: unrecognized data format!"); |
|
2553 } |
|
2554 } |
|
2555 } |
|
2556 |
3504
|
2557 template void read_int (std::istream&, bool, char&); |
|
2558 template void read_int (std::istream&, bool, signed char&); |
|
2559 template void read_int (std::istream&, bool, unsigned char&); |
|
2560 template void read_int (std::istream&, bool, short&); |
|
2561 template void read_int (std::istream&, bool, unsigned short&); |
|
2562 template void read_int (std::istream&, bool, int&); |
|
2563 template void read_int (std::istream&, bool, unsigned int&); |
|
2564 template void read_int (std::istream&, bool, long&); |
|
2565 template void read_int (std::istream&, bool, unsigned long&); |
2317
|
2566 |
|
2567 static inline bool |
3504
|
2568 do_read (std::istream& is, oct_data_conv::data_type dt, |
2317
|
2569 oct_mach_info::float_format flt_fmt, bool swap_bytes, |
|
2570 bool do_float_conversion, double& val) |
|
2571 { |
|
2572 bool retval = true; |
|
2573 |
|
2574 switch (dt) |
|
2575 { |
|
2576 case oct_data_conv::dt_char: |
|
2577 { |
|
2578 char tmp; |
|
2579 read_int (is, swap_bytes, tmp); |
|
2580 val = tmp; |
|
2581 } |
|
2582 break; |
|
2583 |
|
2584 case oct_data_conv::dt_schar: |
|
2585 { |
|
2586 signed char tmp; |
|
2587 read_int (is, swap_bytes, tmp); |
|
2588 val = tmp; |
|
2589 } |
|
2590 break; |
|
2591 |
|
2592 case oct_data_conv::dt_uchar: |
|
2593 { |
|
2594 unsigned char tmp; |
|
2595 read_int (is, swap_bytes, tmp); |
|
2596 val = tmp; |
|
2597 } |
|
2598 break; |
|
2599 |
|
2600 case oct_data_conv::dt_short: |
|
2601 { |
|
2602 short tmp; |
|
2603 read_int (is, swap_bytes, tmp); |
|
2604 val = tmp; |
|
2605 } |
|
2606 break; |
|
2607 |
|
2608 case oct_data_conv::dt_ushort: |
|
2609 { |
|
2610 unsigned short tmp; |
|
2611 read_int (is, swap_bytes, tmp); |
|
2612 val = tmp; |
|
2613 } |
|
2614 break; |
|
2615 |
|
2616 case oct_data_conv::dt_int: |
|
2617 { |
|
2618 int tmp; |
|
2619 read_int (is, swap_bytes, tmp); |
|
2620 val = tmp; |
|
2621 } |
|
2622 break; |
|
2623 |
|
2624 case oct_data_conv::dt_uint: |
|
2625 { |
|
2626 unsigned int tmp; |
|
2627 read_int (is, swap_bytes, tmp); |
|
2628 val = tmp; |
|
2629 } |
|
2630 break; |
|
2631 |
|
2632 case oct_data_conv::dt_long: |
|
2633 { |
|
2634 long tmp; |
|
2635 read_int (is, swap_bytes, tmp); |
|
2636 val = tmp; |
|
2637 } |
|
2638 break; |
|
2639 |
|
2640 case oct_data_conv::dt_ulong: |
|
2641 { |
|
2642 unsigned long tmp; |
|
2643 read_int (is, swap_bytes, tmp); |
|
2644 val = tmp; |
|
2645 } |
|
2646 break; |
|
2647 |
|
2648 case oct_data_conv::dt_float: |
|
2649 { |
|
2650 float f; |
|
2651 |
3145
|
2652 is.read (X_CAST (char *, &f), sizeof (float)); |
2317
|
2653 |
|
2654 if (do_float_conversion) |
|
2655 do_float_format_conversion (&f, 1, flt_fmt); |
|
2656 |
|
2657 val = f; |
|
2658 } |
|
2659 break; |
|
2660 |
|
2661 case oct_data_conv::dt_double: |
|
2662 { |
3145
|
2663 is.read (X_CAST (char *, &val), sizeof (double)); |
2317
|
2664 |
|
2665 if (do_float_conversion) |
|
2666 do_double_format_conversion (&val, 1, flt_fmt); |
|
2667 } |
|
2668 break; |
|
2669 |
|
2670 default: |
|
2671 retval = false; |
|
2672 (*current_liboctave_error_handler) |
|
2673 ("read: invalid type specification"); |
|
2674 break; |
|
2675 } |
|
2676 |
|
2677 return retval; |
|
2678 } |
1360
|
2679 |
458
|
2680 int |
3504
|
2681 Matrix::read (std::istream& is, int nr, int nc, |
2317
|
2682 oct_data_conv::data_type dt, int skip, |
|
2683 oct_mach_info::float_format flt_fmt) |
458
|
2684 { |
4293
|
2685 if (nr == 0 || nc == 0) |
|
2686 { |
|
2687 if (nr >= 0 && nc >= 0) |
|
2688 resize (nr, nc); |
|
2689 else |
|
2690 resize (0, 0); |
|
2691 |
|
2692 return 0; |
|
2693 } |
|
2694 |
2317
|
2695 int retval = -1; |
|
2696 |
|
2697 bool ok = true; |
|
2698 |
|
2699 int count = 0; |
|
2700 |
4587
|
2701 double *dat = 0; |
2317
|
2702 int max_size = 0; |
|
2703 |
|
2704 int final_nr = 0; |
|
2705 int final_nc = 0; |
|
2706 |
|
2707 if (nr > 0) |
458
|
2708 { |
2317
|
2709 if (nc > 0) |
|
2710 { |
|
2711 resize (nr, nc, 0.0); |
4587
|
2712 dat = fortran_vec (); |
2317
|
2713 max_size = nr * nc; |
|
2714 } |
|
2715 else |
|
2716 { |
|
2717 resize (nr, 32, 0.0); |
4587
|
2718 dat = fortran_vec (); |
2317
|
2719 max_size = nr * 32; |
|
2720 } |
|
2721 } |
471
|
2722 else |
|
2723 { |
2317
|
2724 resize (32, 1, 0.0); |
4587
|
2725 dat = fortran_vec (); |
2317
|
2726 max_size = 32; |
|
2727 } |
|
2728 |
|
2729 oct_mach_info::float_format native_flt_fmt |
|
2730 = oct_mach_info::float_format (); |
|
2731 |
|
2732 bool do_float_conversion = (flt_fmt != native_flt_fmt); |
|
2733 |
|
2734 // XXX FIXME XXX -- byte order for Cray? |
|
2735 |
|
2736 bool swap_bytes = false; |
|
2737 |
|
2738 if (oct_mach_info::words_big_endian ()) |
4574
|
2739 swap_bytes = (flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian |
|
2740 || flt_fmt == oct_mach_info::flt_fmt_vax_g |
|
2741 || flt_fmt == oct_mach_info::flt_fmt_vax_g); |
2317
|
2742 else |
4574
|
2743 swap_bytes = (flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
2317
|
2744 |
|
2745 for (;;) |
|
2746 { |
|
2747 // XXX FIXME XXX -- maybe there should be a special case for |
|
2748 // skip == 0. |
|
2749 |
|
2750 if (is) |
|
2751 { |
|
2752 if (nr > 0 && nc > 0 && count == max_size) |
|
2753 { |
|
2754 final_nr = nr; |
|
2755 final_nc = nc; |
|
2756 |
|
2757 break; |
|
2758 } |
|
2759 |
4083
|
2760 double tmp = 0.0; |
|
2761 |
|
2762 ok = do_read (is, dt, flt_fmt, swap_bytes, do_float_conversion, tmp); |
|
2763 |
|
2764 if (ok) |
2317
|
2765 { |
4083
|
2766 if (is) |
2317
|
2767 { |
4083
|
2768 if (count == max_size) |
2317
|
2769 { |
4083
|
2770 max_size *= 2; |
|
2771 |
|
2772 if (nr > 0) |
|
2773 resize (nr, max_size / nr, 0.0); |
|
2774 else |
|
2775 resize (max_size, 1, 0.0); |
|
2776 |
4587
|
2777 dat = fortran_vec (); |
2317
|
2778 } |
3180
|
2779 |
4587
|
2780 dat[count++] = tmp; |
4083
|
2781 } |
|
2782 |
|
2783 if (skip != 0) |
|
2784 is.seekg (skip, std::ios::cur); |
|
2785 |
|
2786 if (is.eof ()) |
|
2787 { |
|
2788 if (nr > 0) |
2317
|
2789 { |
4083
|
2790 if (count > nr) |
2317
|
2791 { |
4083
|
2792 final_nr = nr; |
|
2793 final_nc = (count - 1) / nr + 1; |
|
2794 } |
|
2795 else |
|
2796 { |
|
2797 final_nr = count; |
|
2798 final_nc = 1; |
2317
|
2799 } |
|
2800 } |
4083
|
2801 else |
|
2802 { |
|
2803 final_nr = count; |
|
2804 final_nc = 1; |
|
2805 } |
|
2806 |
|
2807 break; |
2317
|
2808 } |
|
2809 } |
|
2810 else |
|
2811 { |
|
2812 ok = false; |
|
2813 break; |
|
2814 } |
|
2815 } |
|
2816 else |
|
2817 { |
|
2818 ok = false; |
|
2819 break; |
|
2820 } |
|
2821 } |
|
2822 |
|
2823 if (ok) |
|
2824 { |
|
2825 resize (final_nr, final_nc, 0.0); |
|
2826 |
|
2827 retval = count; |
458
|
2828 } |
|
2829 |
2317
|
2830 return retval; |
|
2831 } |
|
2832 |
|
2833 template <class T> |
3504
|
2834 void |
|
2835 write_int (std::ostream& os, bool swap_bytes, T val) |
2317
|
2836 { |
4282
|
2837 int t = sizeof (T); |
|
2838 |
|
2839 if (swap_bytes && t > 1) |
2317
|
2840 { |
4282
|
2841 switch (t) |
2317
|
2842 { |
|
2843 case 2: |
3145
|
2844 swap_2_bytes (X_CAST (char *, &val)); |
2317
|
2845 break; |
|
2846 |
|
2847 case 4: |
3145
|
2848 swap_4_bytes (X_CAST (char *, &val)); |
2317
|
2849 break; |
|
2850 |
|
2851 case 8: |
3145
|
2852 swap_8_bytes (X_CAST (char *, &val)); |
2317
|
2853 break; |
|
2854 |
|
2855 default: |
|
2856 (*current_liboctave_error_handler) |
|
2857 ("write_int: unrecognized data format!"); |
|
2858 } |
|
2859 } |
|
2860 |
3145
|
2861 os.write (X_CAST (char *, &val), sizeof (T)); |
458
|
2862 } |
|
2863 |
3504
|
2864 template void write_int (std::ostream&, bool, char); |
|
2865 template void write_int (std::ostream&, bool, signed char); |
|
2866 template void write_int (std::ostream&, bool, unsigned char); |
|
2867 template void write_int (std::ostream&, bool, short); |
|
2868 template void write_int (std::ostream&, bool, unsigned short); |
|
2869 template void write_int (std::ostream&, bool, int); |
|
2870 template void write_int (std::ostream&, bool, unsigned int); |
|
2871 template void write_int (std::ostream&, bool, long); |
|
2872 template void write_int (std::ostream&, bool, unsigned long); |
2317
|
2873 |
|
2874 static inline bool |
3504
|
2875 do_write (std::ostream& os, double d, oct_data_conv::data_type dt, |
2317
|
2876 oct_mach_info::float_format flt_fmt, bool swap_bytes, |
|
2877 bool do_float_conversion) |
|
2878 { |
|
2879 bool retval = true; |
|
2880 |
|
2881 switch (dt) |
|
2882 { |
|
2883 case oct_data_conv::dt_char: |
3145
|
2884 write_int (os, swap_bytes, X_CAST (char, d)); |
2317
|
2885 break; |
|
2886 |
|
2887 case oct_data_conv::dt_schar: |
3145
|
2888 write_int (os, swap_bytes, X_CAST (signed char, d)); |
2317
|
2889 break; |
|
2890 |
|
2891 case oct_data_conv::dt_uchar: |
3145
|
2892 write_int (os, swap_bytes, X_CAST (unsigned char, d)); |
2317
|
2893 break; |
|
2894 |
|
2895 case oct_data_conv::dt_short: |
3145
|
2896 write_int (os, swap_bytes, X_CAST (short, d)); |
2317
|
2897 break; |
|
2898 |
|
2899 case oct_data_conv::dt_ushort: |
3145
|
2900 write_int (os, swap_bytes, X_CAST (unsigned short, d)); |
2317
|
2901 break; |
|
2902 |
|
2903 case oct_data_conv::dt_int: |
3145
|
2904 write_int (os, swap_bytes, X_CAST (int, d)); |
2317
|
2905 break; |
|
2906 |
|
2907 case oct_data_conv::dt_uint: |
3145
|
2908 write_int (os, swap_bytes, X_CAST (unsigned int, d)); |
2317
|
2909 break; |
|
2910 |
|
2911 case oct_data_conv::dt_long: |
3145
|
2912 write_int (os, swap_bytes, X_CAST (long, d)); |
2317
|
2913 break; |
|
2914 |
|
2915 case oct_data_conv::dt_ulong: |
3145
|
2916 write_int (os, swap_bytes, X_CAST (unsigned long, d)); |
2317
|
2917 break; |
|
2918 |
|
2919 case oct_data_conv::dt_float: |
|
2920 { |
2800
|
2921 float f = d; |
2317
|
2922 |
|
2923 if (do_float_conversion) |
|
2924 do_float_format_conversion (&f, 1, flt_fmt); |
|
2925 |
3145
|
2926 os.write (X_CAST (char *, &f), sizeof (float)); |
2317
|
2927 } |
|
2928 break; |
|
2929 |
|
2930 case oct_data_conv::dt_double: |
|
2931 { |
|
2932 if (do_float_conversion) |
|
2933 do_double_format_conversion (&d, 1, flt_fmt); |
|
2934 |
3145
|
2935 os.write (X_CAST (char *, &d), sizeof (double)); |
2317
|
2936 } |
|
2937 break; |
|
2938 |
|
2939 default: |
|
2940 retval = false; |
|
2941 (*current_liboctave_error_handler) |
|
2942 ("write: invalid type specification"); |
|
2943 break; |
|
2944 } |
|
2945 |
|
2946 return retval; |
|
2947 } |
1360
|
2948 |
458
|
2949 int |
3504
|
2950 Matrix::write (std::ostream& os, oct_data_conv::data_type dt, int skip, |
2317
|
2951 oct_mach_info::float_format flt_fmt) |
458
|
2952 { |
2317
|
2953 int retval = -1; |
|
2954 |
|
2955 bool ok = true; |
|
2956 |
|
2957 int count = 0; |
|
2958 |
|
2959 const double *d = data (); |
|
2960 |
|
2961 int n = length (); |
|
2962 |
|
2963 oct_mach_info::float_format native_flt_fmt |
|
2964 = oct_mach_info::float_format (); |
|
2965 |
|
2966 bool do_float_conversion = (flt_fmt != native_flt_fmt); |
|
2967 |
|
2968 // XXX FIXME XXX -- byte order for Cray? |
|
2969 |
|
2970 bool swap_bytes = false; |
|
2971 |
|
2972 if (oct_mach_info::words_big_endian ()) |
4574
|
2973 swap_bytes = (flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian |
|
2974 || flt_fmt == oct_mach_info::flt_fmt_vax_g |
|
2975 || flt_fmt == oct_mach_info::flt_fmt_vax_g); |
2317
|
2976 else |
4574
|
2977 swap_bytes = (flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
2317
|
2978 |
|
2979 for (int i = 0; i < n; i++) |
458
|
2980 { |
2317
|
2981 if (os) |
|
2982 { |
|
2983 if (skip != 0) |
3504
|
2984 os.seekp (skip, std::ios::cur); |
2317
|
2985 |
|
2986 if (os) |
|
2987 { |
|
2988 ok = do_write (os, d[i], dt, flt_fmt, swap_bytes, |
|
2989 do_float_conversion); |
|
2990 |
|
2991 if (os && ok) |
|
2992 count++; |
|
2993 else |
|
2994 break; |
|
2995 } |
|
2996 else |
|
2997 { |
|
2998 ok = false; |
|
2999 break; |
|
3000 } |
|
3001 } |
|
3002 else |
|
3003 { |
|
3004 ok = false; |
|
3005 break; |
|
3006 } |
471
|
3007 } |
458
|
3008 |
2317
|
3009 if (ok) |
|
3010 retval = count; |
|
3011 |
|
3012 return retval; |
458
|
3013 } |
|
3014 |
2317
|
3015 |
|
3016 |
1819
|
3017 Matrix |
|
3018 Givens (double x, double y) |
|
3019 { |
|
3020 double cc, s, temp_r; |
|
3021 |
3887
|
3022 F77_FUNC (dlartg, DLARTG) (x, y, cc, s, temp_r); |
1819
|
3023 |
|
3024 Matrix g (2, 2); |
|
3025 |
|
3026 g.elem (0, 0) = cc; |
|
3027 g.elem (1, 1) = cc; |
|
3028 g.elem (0, 1) = s; |
|
3029 g.elem (1, 0) = -s; |
|
3030 |
|
3031 return g; |
|
3032 } |
|
3033 |
|
3034 Matrix |
|
3035 Sylvester (const Matrix& a, const Matrix& b, const Matrix& c) |
|
3036 { |
|
3037 Matrix retval; |
|
3038 |
|
3039 // XXX FIXME XXX -- need to check that a, b, and c are all the same |
|
3040 // size. |
|
3041 |
|
3042 // Compute Schur decompositions. |
|
3043 |
|
3044 SCHUR as (a, "U"); |
|
3045 SCHUR bs (b, "U"); |
|
3046 |
|
3047 // Transform c to new coordinates. |
|
3048 |
|
3049 Matrix ua = as.unitary_matrix (); |
|
3050 Matrix sch_a = as.schur_matrix (); |
|
3051 |
|
3052 Matrix ub = bs.unitary_matrix (); |
|
3053 Matrix sch_b = bs.schur_matrix (); |
|
3054 |
|
3055 Matrix cx = ua.transpose () * c * ub; |
|
3056 |
|
3057 // Solve the sylvester equation, back-transform, and return the |
|
3058 // solution. |
|
3059 |
|
3060 int a_nr = a.rows (); |
|
3061 int b_nr = b.rows (); |
|
3062 |
|
3063 double scale; |
|
3064 int info; |
|
3065 |
1950
|
3066 double *pa = sch_a.fortran_vec (); |
|
3067 double *pb = sch_b.fortran_vec (); |
|
3068 double *px = cx.fortran_vec (); |
|
3069 |
4552
|
3070 F77_XFCN (dtrsyl, DTRSYL, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3071 F77_CONST_CHAR_ARG2 ("N", 1), |
|
3072 1, a_nr, b_nr, pa, a_nr, pb, |
|
3073 b_nr, px, a_nr, scale, info |
|
3074 F77_CHAR_ARG_LEN (1) |
|
3075 F77_CHAR_ARG_LEN (1))); |
1950
|
3076 |
|
3077 |
|
3078 if (f77_exception_encountered) |
|
3079 (*current_liboctave_error_handler) ("unrecoverable error in dtrsyl"); |
|
3080 else |
|
3081 { |
|
3082 // XXX FIXME XXX -- check info? |
1819
|
3083 |
1950
|
3084 retval = -ua*cx*ub.transpose (); |
|
3085 } |
1819
|
3086 |
|
3087 return retval; |
|
3088 } |
|
3089 |
2828
|
3090 // matrix by matrix -> matrix operations |
|
3091 |
|
3092 Matrix |
|
3093 operator * (const Matrix& m, const Matrix& a) |
|
3094 { |
|
3095 Matrix retval; |
|
3096 |
|
3097 int nr = m.rows (); |
|
3098 int nc = m.cols (); |
|
3099 |
|
3100 int a_nr = a.rows (); |
|
3101 int a_nc = a.cols (); |
|
3102 |
|
3103 if (nc != a_nr) |
|
3104 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
|
3105 else |
|
3106 { |
|
3107 if (nr == 0 || nc == 0 || a_nc == 0) |
|
3108 retval.resize (nr, a_nc, 0.0); |
|
3109 else |
|
3110 { |
|
3111 int ld = nr; |
|
3112 int lda = a_nr; |
|
3113 |
|
3114 retval.resize (nr, a_nc); |
|
3115 double *c = retval.fortran_vec (); |
|
3116 |
4552
|
3117 F77_XFCN (dgemm, DGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3118 F77_CONST_CHAR_ARG2 ("N", 1), |
|
3119 nr, a_nc, nc, 1.0, m.data (), |
|
3120 ld, a.data (), lda, 0.0, c, nr |
|
3121 F77_CHAR_ARG_LEN (1) |
|
3122 F77_CHAR_ARG_LEN (1))); |
2828
|
3123 |
|
3124 if (f77_exception_encountered) |
|
3125 (*current_liboctave_error_handler) |
|
3126 ("unrecoverable error in dgemm"); |
|
3127 } |
|
3128 } |
|
3129 |
|
3130 return retval; |
|
3131 } |
|
3132 |
4309
|
3133 // XXX FIXME XXX -- it would be nice to share code among the min/max |
|
3134 // functions below. |
|
3135 |
|
3136 #define EMPTY_RETURN_CHECK(T) \ |
|
3137 if (nr == 0 || nc == 0) \ |
|
3138 return T (nr, nc); |
|
3139 |
|
3140 Matrix |
|
3141 min (double d, const Matrix& m) |
|
3142 { |
|
3143 int nr = m.rows (); |
|
3144 int nc = m.columns (); |
|
3145 |
|
3146 EMPTY_RETURN_CHECK (Matrix); |
|
3147 |
|
3148 Matrix result (nr, nc); |
|
3149 |
|
3150 for (int j = 0; j < nc; j++) |
|
3151 for (int i = 0; i < nr; i++) |
|
3152 { |
|
3153 OCTAVE_QUIT; |
|
3154 result (i, j) = xmin (d, m (i, j)); |
|
3155 } |
|
3156 |
|
3157 return result; |
|
3158 } |
|
3159 |
|
3160 Matrix |
|
3161 min (const Matrix& m, double d) |
|
3162 { |
|
3163 int nr = m.rows (); |
|
3164 int nc = m.columns (); |
|
3165 |
|
3166 EMPTY_RETURN_CHECK (Matrix); |
|
3167 |
|
3168 Matrix result (nr, nc); |
|
3169 |
|
3170 for (int j = 0; j < nc; j++) |
|
3171 for (int i = 0; i < nr; i++) |
|
3172 { |
|
3173 OCTAVE_QUIT; |
|
3174 result (i, j) = xmin (m (i, j), d); |
|
3175 } |
|
3176 |
|
3177 return result; |
|
3178 } |
|
3179 |
|
3180 Matrix |
|
3181 min (const Matrix& a, const Matrix& b) |
|
3182 { |
|
3183 int nr = a.rows (); |
|
3184 int nc = a.columns (); |
|
3185 |
|
3186 if (nr != b.rows () || nc != b.columns ()) |
|
3187 { |
|
3188 (*current_liboctave_error_handler) |
|
3189 ("two-arg min expecting args of same size"); |
|
3190 return Matrix (); |
|
3191 } |
|
3192 |
|
3193 EMPTY_RETURN_CHECK (Matrix); |
|
3194 |
|
3195 Matrix result (nr, nc); |
|
3196 |
|
3197 for (int j = 0; j < nc; j++) |
|
3198 for (int i = 0; i < nr; i++) |
|
3199 { |
|
3200 OCTAVE_QUIT; |
|
3201 result (i, j) = xmin (a (i, j), b (i, j)); |
|
3202 } |
|
3203 |
|
3204 return result; |
|
3205 } |
|
3206 |
|
3207 Matrix |
|
3208 max (double d, const Matrix& m) |
|
3209 { |
|
3210 int nr = m.rows (); |
|
3211 int nc = m.columns (); |
|
3212 |
|
3213 EMPTY_RETURN_CHECK (Matrix); |
|
3214 |
|
3215 Matrix result (nr, nc); |
|
3216 |
|
3217 for (int j = 0; j < nc; j++) |
|
3218 for (int i = 0; i < nr; i++) |
|
3219 { |
|
3220 OCTAVE_QUIT; |
|
3221 result (i, j) = xmax (d, m (i, j)); |
|
3222 } |
|
3223 |
|
3224 return result; |
|
3225 } |
|
3226 |
|
3227 Matrix |
|
3228 max (const Matrix& m, double d) |
|
3229 { |
|
3230 int nr = m.rows (); |
|
3231 int nc = m.columns (); |
|
3232 |
|
3233 EMPTY_RETURN_CHECK (Matrix); |
|
3234 |
|
3235 Matrix result (nr, nc); |
|
3236 |
|
3237 for (int j = 0; j < nc; j++) |
|
3238 for (int i = 0; i < nr; i++) |
|
3239 { |
|
3240 OCTAVE_QUIT; |
|
3241 result (i, j) = xmax (m (i, j), d); |
|
3242 } |
|
3243 |
|
3244 return result; |
|
3245 } |
|
3246 |
|
3247 Matrix |
|
3248 max (const Matrix& a, const Matrix& b) |
|
3249 { |
|
3250 int nr = a.rows (); |
|
3251 int nc = a.columns (); |
|
3252 |
|
3253 if (nr != b.rows () || nc != b.columns ()) |
|
3254 { |
|
3255 (*current_liboctave_error_handler) |
|
3256 ("two-arg max expecting args of same size"); |
|
3257 return Matrix (); |
|
3258 } |
|
3259 |
|
3260 EMPTY_RETURN_CHECK (Matrix); |
|
3261 |
|
3262 Matrix result (nr, nc); |
|
3263 |
|
3264 for (int j = 0; j < nc; j++) |
|
3265 for (int i = 0; i < nr; i++) |
|
3266 { |
|
3267 OCTAVE_QUIT; |
|
3268 result (i, j) = xmax (a (i, j), b (i, j)); |
|
3269 } |
|
3270 |
|
3271 return result; |
|
3272 } |
|
3273 |
2870
|
3274 MS_CMP_OPS(Matrix, , double, ) |
3504
|
3275 MS_BOOL_OPS(Matrix, double, 0.0) |
2870
|
3276 |
|
3277 SM_CMP_OPS(double, , Matrix, ) |
3504
|
3278 SM_BOOL_OPS(double, Matrix, 0.0) |
2870
|
3279 |
|
3280 MM_CMP_OPS(Matrix, , Matrix, ) |
3504
|
3281 MM_BOOL_OPS(Matrix, Matrix, 0.0) |
2870
|
3282 |
458
|
3283 /* |
|
3284 ;;; Local Variables: *** |
|
3285 ;;; mode: C++ *** |
|
3286 ;;; End: *** |
|
3287 */ |