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