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 { |
4330
|
695 // Now calculate the condition number for non-singular matrix. |
4329
|
696 char job = '1'; |
|
697 Array<int> iz (nc); |
|
698 int *piz = iz.fortran_vec (); |
4552
|
699 F77_XFCN (dgecon, DGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
700 nc, tmp_data, nr, anorm, |
|
701 rcond, pz, piz, info |
|
702 F77_CHAR_ARG_LEN (1))); |
4329
|
703 |
|
704 if (f77_exception_encountered) |
|
705 (*current_liboctave_error_handler) |
|
706 ("unrecoverable error in dgecon"); |
|
707 |
4509
|
708 if (info != 0) |
4329
|
709 info = -1; |
|
710 } |
1948
|
711 |
|
712 if (info == -1 && ! force) |
|
713 retval = *this; // Restore matrix contents. |
|
714 else |
|
715 { |
4329
|
716 F77_XFCN (dgetri, DGETRI, (nc, tmp_data, nr, pipvt, |
4552
|
717 pz, lwork, info)); |
1948
|
718 |
|
719 if (f77_exception_encountered) |
|
720 (*current_liboctave_error_handler) |
4329
|
721 ("unrecoverable error in dgetri"); |
|
722 |
4509
|
723 if (info != 0) |
4329
|
724 info = -1; |
1948
|
725 } |
|
726 } |
458
|
727 } |
|
728 |
1948
|
729 return retval; |
458
|
730 } |
|
731 |
740
|
732 Matrix |
4384
|
733 Matrix::pseudo_inverse (double tol) const |
740
|
734 { |
3480
|
735 SVD result (*this, SVD::economy); |
740
|
736 |
|
737 DiagMatrix S = result.singular_values (); |
|
738 Matrix U = result.left_singular_matrix (); |
|
739 Matrix V = result.right_singular_matrix (); |
|
740 |
|
741 ColumnVector sigma = S.diag (); |
|
742 |
|
743 int r = sigma.length () - 1; |
|
744 int nr = rows (); |
|
745 int nc = cols (); |
|
746 |
|
747 if (tol <= 0.0) |
|
748 { |
|
749 if (nr > nc) |
|
750 tol = nr * sigma.elem (0) * DBL_EPSILON; |
|
751 else |
|
752 tol = nc * sigma.elem (0) * DBL_EPSILON; |
|
753 } |
|
754 |
|
755 while (r >= 0 && sigma.elem (r) < tol) |
|
756 r--; |
|
757 |
|
758 if (r < 0) |
|
759 return Matrix (nc, nr, 0.0); |
|
760 else |
|
761 { |
|
762 Matrix Ur = U.extract (0, 0, nr-1, r); |
|
763 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); |
|
764 Matrix Vr = V.extract (0, 0, nc-1, r); |
|
765 return Vr * D * Ur.transpose (); |
|
766 } |
|
767 } |
|
768 |
4773
|
769 #if defined (HAVE_FFTW3) |
3827
|
770 |
|
771 ComplexMatrix |
|
772 Matrix::fourier (void) const |
|
773 { |
|
774 size_t nr = rows (); |
|
775 size_t nc = cols (); |
|
776 |
|
777 ComplexMatrix retval (nr, nc); |
|
778 |
|
779 size_t npts, nsamples; |
|
780 |
|
781 if (nr == 1 || nc == 1) |
|
782 { |
|
783 npts = nr > nc ? nr : nc; |
|
784 nsamples = 1; |
|
785 } |
|
786 else |
|
787 { |
|
788 npts = nr; |
|
789 nsamples = nc; |
|
790 } |
|
791 |
4773
|
792 const double *in (fortran_vec ()); |
3827
|
793 Complex *out (retval.fortran_vec ()); |
|
794 |
4773
|
795 octave_fftw::fft (in, out, npts, nsamples); |
3827
|
796 |
|
797 return retval; |
|
798 } |
|
799 |
|
800 ComplexMatrix |
|
801 Matrix::ifourier (void) const |
|
802 { |
|
803 size_t nr = rows (); |
|
804 size_t nc = cols (); |
|
805 |
|
806 ComplexMatrix retval (nr, nc); |
|
807 |
|
808 size_t npts, nsamples; |
|
809 |
|
810 if (nr == 1 || nc == 1) |
|
811 { |
|
812 npts = nr > nc ? nr : nc; |
|
813 nsamples = 1; |
|
814 } |
|
815 else |
|
816 { |
|
817 npts = nr; |
|
818 nsamples = nc; |
|
819 } |
|
820 |
|
821 ComplexMatrix tmp (*this); |
|
822 Complex *in (tmp.fortran_vec ()); |
|
823 Complex *out (retval.fortran_vec ()); |
|
824 |
4773
|
825 octave_fftw::ifft (in, out, npts, nsamples); |
3827
|
826 |
|
827 return retval; |
|
828 } |
|
829 |
|
830 ComplexMatrix |
|
831 Matrix::fourier2d (void) const |
|
832 { |
4773
|
833 dim_vector dv(rows (), cols ()); |
|
834 |
|
835 const double *in = fortran_vec (); |
|
836 ComplexMatrix retval (rows (), cols ()); |
|
837 octave_fftw::fftNd (in, retval.fortran_vec (), 2, dv); |
3827
|
838 |
|
839 return retval; |
|
840 } |
|
841 |
|
842 ComplexMatrix |
|
843 Matrix::ifourier2d (void) const |
|
844 { |
4773
|
845 dim_vector dv(rows (), cols ()); |
3827
|
846 |
|
847 ComplexMatrix retval (*this); |
4773
|
848 Complex *out (retval.fortran_vec ()); |
|
849 |
|
850 octave_fftw::ifftNd (out, out, 2, dv); |
3827
|
851 |
|
852 return retval; |
|
853 } |
|
854 |
|
855 #else |
|
856 |
458
|
857 ComplexMatrix |
|
858 Matrix::fourier (void) const |
|
859 { |
1948
|
860 ComplexMatrix retval; |
|
861 |
458
|
862 int nr = rows (); |
|
863 int nc = cols (); |
1948
|
864 |
458
|
865 int npts, nsamples; |
1948
|
866 |
458
|
867 if (nr == 1 || nc == 1) |
|
868 { |
|
869 npts = nr > nc ? nr : nc; |
|
870 nsamples = 1; |
|
871 } |
|
872 else |
|
873 { |
|
874 npts = nr; |
|
875 nsamples = nc; |
|
876 } |
|
877 |
|
878 int nn = 4*npts+15; |
1948
|
879 |
|
880 Array<Complex> wsave (nn); |
|
881 Complex *pwsave = wsave.fortran_vec (); |
|
882 |
3585
|
883 retval = ComplexMatrix (*this); |
1948
|
884 Complex *tmp_data = retval.fortran_vec (); |
|
885 |
3887
|
886 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
458
|
887 |
|
888 for (int j = 0; j < nsamples; j++) |
4153
|
889 { |
|
890 OCTAVE_QUIT; |
|
891 |
|
892 F77_FUNC (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
893 } |
1948
|
894 |
|
895 return retval; |
458
|
896 } |
|
897 |
|
898 ComplexMatrix |
|
899 Matrix::ifourier (void) const |
|
900 { |
1948
|
901 ComplexMatrix retval; |
|
902 |
458
|
903 int nr = rows (); |
|
904 int nc = cols (); |
1948
|
905 |
458
|
906 int npts, nsamples; |
1948
|
907 |
458
|
908 if (nr == 1 || nc == 1) |
|
909 { |
|
910 npts = nr > nc ? nr : nc; |
|
911 nsamples = 1; |
|
912 } |
|
913 else |
|
914 { |
|
915 npts = nr; |
|
916 nsamples = nc; |
|
917 } |
|
918 |
|
919 int nn = 4*npts+15; |
1948
|
920 |
|
921 Array<Complex> wsave (nn); |
|
922 Complex *pwsave = wsave.fortran_vec (); |
|
923 |
3585
|
924 retval = ComplexMatrix (*this); |
1948
|
925 Complex *tmp_data = retval.fortran_vec (); |
|
926 |
3887
|
927 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
458
|
928 |
|
929 for (int j = 0; j < nsamples; j++) |
4153
|
930 { |
|
931 OCTAVE_QUIT; |
|
932 |
|
933 F77_FUNC (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
|
934 } |
458
|
935 |
1321
|
936 for (int j = 0; j < npts*nsamples; j++) |
3572
|
937 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
458
|
938 |
1948
|
939 return retval; |
458
|
940 } |
|
941 |
677
|
942 ComplexMatrix |
|
943 Matrix::fourier2d (void) const |
|
944 { |
1948
|
945 ComplexMatrix retval; |
|
946 |
677
|
947 int nr = rows (); |
|
948 int nc = cols (); |
1948
|
949 |
677
|
950 int npts, nsamples; |
1948
|
951 |
677
|
952 if (nr == 1 || nc == 1) |
|
953 { |
|
954 npts = nr > nc ? nr : nc; |
|
955 nsamples = 1; |
|
956 } |
|
957 else |
|
958 { |
|
959 npts = nr; |
|
960 nsamples = nc; |
|
961 } |
|
962 |
|
963 int nn = 4*npts+15; |
1948
|
964 |
|
965 Array<Complex> wsave (nn); |
|
966 Complex *pwsave = wsave.fortran_vec (); |
|
967 |
3585
|
968 retval = ComplexMatrix (*this); |
1948
|
969 Complex *tmp_data = retval.fortran_vec (); |
|
970 |
3887
|
971 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
972 |
|
973 for (int j = 0; j < nsamples; j++) |
4153
|
974 { |
|
975 OCTAVE_QUIT; |
|
976 |
|
977 F77_FUNC (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
978 } |
677
|
979 |
|
980 npts = nc; |
|
981 nsamples = nr; |
|
982 nn = 4*npts+15; |
1948
|
983 |
|
984 wsave.resize (nn); |
|
985 pwsave = wsave.fortran_vec (); |
|
986 |
4773
|
987 Array<Complex> tmp (npts); |
|
988 Complex *prow = tmp.fortran_vec (); |
1948
|
989 |
3887
|
990 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
991 |
1321
|
992 for (int j = 0; j < nsamples; j++) |
677
|
993 { |
4153
|
994 OCTAVE_QUIT; |
|
995 |
677
|
996 for (int i = 0; i < npts; i++) |
1948
|
997 prow[i] = tmp_data[i*nr + j]; |
|
998 |
3887
|
999 F77_FUNC (cfftf, CFFTF) (npts, prow, pwsave); |
677
|
1000 |
1321
|
1001 for (int i = 0; i < npts; i++) |
1948
|
1002 tmp_data[i*nr + j] = prow[i]; |
677
|
1003 } |
|
1004 |
1948
|
1005 return retval; |
677
|
1006 } |
|
1007 |
|
1008 ComplexMatrix |
|
1009 Matrix::ifourier2d (void) const |
|
1010 { |
1948
|
1011 ComplexMatrix retval; |
|
1012 |
677
|
1013 int nr = rows (); |
|
1014 int nc = cols (); |
1948
|
1015 |
677
|
1016 int npts, nsamples; |
1948
|
1017 |
677
|
1018 if (nr == 1 || nc == 1) |
|
1019 { |
|
1020 npts = nr > nc ? nr : nc; |
|
1021 nsamples = 1; |
|
1022 } |
|
1023 else |
|
1024 { |
|
1025 npts = nr; |
|
1026 nsamples = nc; |
|
1027 } |
|
1028 |
|
1029 int nn = 4*npts+15; |
1948
|
1030 |
|
1031 Array<Complex> wsave (nn); |
|
1032 Complex *pwsave = wsave.fortran_vec (); |
|
1033 |
3585
|
1034 retval = ComplexMatrix (*this); |
1948
|
1035 Complex *tmp_data = retval.fortran_vec (); |
|
1036 |
3887
|
1037 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1038 |
|
1039 for (int j = 0; j < nsamples; j++) |
4153
|
1040 { |
|
1041 OCTAVE_QUIT; |
|
1042 |
|
1043 F77_FUNC (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
|
1044 } |
677
|
1045 |
1321
|
1046 for (int j = 0; j < npts*nsamples; j++) |
3572
|
1047 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
677
|
1048 |
|
1049 npts = nc; |
|
1050 nsamples = nr; |
|
1051 nn = 4*npts+15; |
1948
|
1052 |
|
1053 wsave.resize (nn); |
|
1054 pwsave = wsave.fortran_vec (); |
|
1055 |
4773
|
1056 Array<Complex> tmp (npts); |
|
1057 Complex *prow = tmp.fortran_vec (); |
1948
|
1058 |
3887
|
1059 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1060 |
1321
|
1061 for (int j = 0; j < nsamples; j++) |
677
|
1062 { |
4153
|
1063 OCTAVE_QUIT; |
|
1064 |
677
|
1065 for (int i = 0; i < npts; i++) |
1948
|
1066 prow[i] = tmp_data[i*nr + j]; |
|
1067 |
3887
|
1068 F77_FUNC (cfftb, CFFTB) (npts, prow, pwsave); |
677
|
1069 |
1321
|
1070 for (int i = 0; i < npts; i++) |
3572
|
1071 tmp_data[i*nr + j] = prow[i] / static_cast<double> (npts); |
677
|
1072 } |
|
1073 |
1948
|
1074 return retval; |
677
|
1075 } |
|
1076 |
3827
|
1077 #endif |
|
1078 |
458
|
1079 DET |
|
1080 Matrix::determinant (void) const |
|
1081 { |
|
1082 int info; |
|
1083 double rcond; |
4329
|
1084 return determinant (info, rcond, 0); |
458
|
1085 } |
|
1086 |
|
1087 DET |
|
1088 Matrix::determinant (int& info) const |
|
1089 { |
|
1090 double rcond; |
4329
|
1091 return determinant (info, rcond, 0); |
458
|
1092 } |
|
1093 |
|
1094 DET |
4329
|
1095 Matrix::determinant (int& info, double& rcond, int calc_cond) const |
458
|
1096 { |
|
1097 DET retval; |
|
1098 |
|
1099 int nr = rows (); |
|
1100 int nc = cols (); |
|
1101 |
|
1102 if (nr == 0 || nc == 0) |
|
1103 { |
|
1104 double d[2]; |
|
1105 d[0] = 1.0; |
|
1106 d[1] = 0.0; |
|
1107 retval = DET (d); |
|
1108 } |
|
1109 else |
|
1110 { |
1948
|
1111 Array<int> ipvt (nr); |
|
1112 int *pipvt = ipvt.fortran_vec (); |
|
1113 |
|
1114 Matrix atmp = *this; |
|
1115 double *tmp_data = atmp.fortran_vec (); |
|
1116 |
4329
|
1117 info = 0; |
|
1118 |
4330
|
1119 // Calculate the norm of the matrix, for later use. |
4329
|
1120 double anorm = 0; |
|
1121 if (calc_cond) |
|
1122 anorm = atmp.abs().sum().row(0).max(); |
|
1123 |
|
1124 F77_XFCN (dgetrf, DGETRF, (nr, nr, tmp_data, nr, pipvt, info)); |
1948
|
1125 |
|
1126 if (f77_exception_encountered) |
4329
|
1127 (*current_liboctave_error_handler) ("unrecoverable error in dgetrf"); |
458
|
1128 else |
|
1129 { |
4330
|
1130 // Throw-away extra info LAPACK gives so as to not change output. |
4509
|
1131 rcond = 0.0; |
|
1132 if (info != 0) |
1948
|
1133 { |
4509
|
1134 info = -1; |
|
1135 retval = DET (); |
4329
|
1136 } |
|
1137 else |
1948
|
1138 { |
4329
|
1139 if (calc_cond) |
|
1140 { |
4330
|
1141 // Now calc the condition number for non-singular matrix. |
4329
|
1142 char job = '1'; |
|
1143 Array<double> z (4 * nc); |
|
1144 double *pz = z.fortran_vec (); |
|
1145 Array<int> iz (nc); |
|
1146 int *piz = iz.fortran_vec (); |
|
1147 |
4552
|
1148 F77_XFCN (dgecon, DGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1149 nc, tmp_data, nr, anorm, |
|
1150 rcond, pz, piz, info |
|
1151 F77_CHAR_ARG_LEN (1))); |
4329
|
1152 |
|
1153 if (f77_exception_encountered) |
|
1154 (*current_liboctave_error_handler) |
|
1155 ("unrecoverable error in dgecon"); |
|
1156 } |
|
1157 |
4509
|
1158 if (info != 0) |
4329
|
1159 { |
|
1160 info = -1; |
|
1161 retval = DET (); |
|
1162 } |
|
1163 else |
|
1164 { |
|
1165 double d[2] = { 1., 0.}; |
|
1166 for (int i=0; i<nc; i++) |
|
1167 { |
|
1168 if (ipvt(i) != (i+1)) d[0] = -d[0]; |
|
1169 d[0] *= atmp(i,i); |
|
1170 if (d[0] == 0.) break; |
|
1171 while (fabs(d[0]) < 1.) |
|
1172 { |
|
1173 d[0] = 10. * d[0]; |
4509
|
1174 d[1] = d[1] - 1.0; |
4329
|
1175 } |
|
1176 while (fabs(d[0]) >= 10.) |
|
1177 { |
|
1178 d[0] = 0.1 * d[0]; |
4509
|
1179 d[1] = d[1] + 1.0; |
4329
|
1180 } |
|
1181 } |
|
1182 retval = DET (d); |
|
1183 } |
1948
|
1184 } |
458
|
1185 } |
|
1186 } |
|
1187 |
|
1188 return retval; |
|
1189 } |
|
1190 |
|
1191 Matrix |
|
1192 Matrix::solve (const Matrix& b) const |
|
1193 { |
|
1194 int info; |
|
1195 double rcond; |
4329
|
1196 return solve (b, info, rcond, 0); |
458
|
1197 } |
|
1198 |
|
1199 Matrix |
|
1200 Matrix::solve (const Matrix& b, int& info) const |
|
1201 { |
|
1202 double rcond; |
4329
|
1203 return solve (b, info, rcond, 0); |
458
|
1204 } |
|
1205 |
|
1206 Matrix |
532
|
1207 Matrix::solve (const Matrix& b, int& info, double& rcond) const |
458
|
1208 { |
3480
|
1209 return solve (b, info, rcond, 0); |
|
1210 } |
|
1211 |
|
1212 Matrix |
|
1213 Matrix::solve (const Matrix& b, int& info, double& rcond, |
|
1214 solve_singularity_handler sing_handler) const |
|
1215 { |
458
|
1216 Matrix retval; |
|
1217 |
|
1218 int nr = rows (); |
|
1219 int nc = cols (); |
1948
|
1220 |
458
|
1221 if (nr == 0 || nc == 0 || nr != nc || nr != b.rows ()) |
1948
|
1222 (*current_liboctave_error_handler) |
|
1223 ("matrix dimension mismatch solution of linear equations"); |
458
|
1224 else |
|
1225 { |
1948
|
1226 info = 0; |
|
1227 |
|
1228 Array<int> ipvt (nr); |
|
1229 int *pipvt = ipvt.fortran_vec (); |
|
1230 |
|
1231 Matrix atmp = *this; |
|
1232 double *tmp_data = atmp.fortran_vec (); |
|
1233 |
4329
|
1234 Array<double> z (4 * nc); |
|
1235 double *pz = z.fortran_vec (); |
|
1236 Array<int> iz (nc); |
|
1237 int *piz = iz.fortran_vec (); |
|
1238 |
4330
|
1239 // Calculate the norm of the matrix, for later use. |
4329
|
1240 double anorm = atmp.abs().sum().row(0).max(); |
|
1241 |
|
1242 F77_XFCN (dgetrf, DGETRF, (nr, nr, tmp_data, nr, pipvt, info)); |
1948
|
1243 |
|
1244 if (f77_exception_encountered) |
4329
|
1245 (*current_liboctave_error_handler) ("unrecoverable error in dgetrf"); |
1948
|
1246 else |
|
1247 { |
4330
|
1248 // Throw-away extra info LAPACK gives so as to not change output. |
4509
|
1249 rcond = 0.0; |
|
1250 if (info != 0) |
1948
|
1251 { |
|
1252 info = -2; |
3480
|
1253 |
|
1254 if (sing_handler) |
|
1255 sing_handler (rcond); |
|
1256 else |
|
1257 (*current_liboctave_error_handler) |
4329
|
1258 ("matrix singular to machine precision"); |
|
1259 |
|
1260 } |
|
1261 else |
1948
|
1262 { |
4330
|
1263 // Now calculate the condition number for non-singular matrix. |
4329
|
1264 char job = '1'; |
4552
|
1265 F77_XFCN (dgecon, DGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1266 nc, tmp_data, nr, anorm, |
|
1267 rcond, pz, piz, info |
|
1268 F77_CHAR_ARG_LEN (1))); |
4329
|
1269 |
|
1270 if (f77_exception_encountered) |
|
1271 (*current_liboctave_error_handler) |
|
1272 ("unrecoverable error in dgecon"); |
|
1273 |
4509
|
1274 if (info != 0) |
4329
|
1275 info = -2; |
|
1276 |
|
1277 volatile double rcond_plus_one = rcond + 1.0; |
|
1278 |
|
1279 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
1948
|
1280 { |
4329
|
1281 info = -2; |
|
1282 |
|
1283 if (sing_handler) |
|
1284 sing_handler (rcond); |
|
1285 else |
|
1286 (*current_liboctave_error_handler) |
|
1287 ("matrix singular to machine precision, rcond = %g", |
|
1288 rcond); |
|
1289 } |
|
1290 else |
|
1291 { |
|
1292 retval = b; |
|
1293 double *result = retval.fortran_vec (); |
|
1294 |
|
1295 int b_nc = b.cols (); |
|
1296 |
4587
|
1297 job = 'N'; |
4552
|
1298 F77_XFCN (dgetrs, DGETRS, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1299 nr, b_nc, tmp_data, nr, |
|
1300 pipvt, result, b.rows(), info |
|
1301 F77_CHAR_ARG_LEN (1))); |
4329
|
1302 |
1948
|
1303 if (f77_exception_encountered) |
4329
|
1304 (*current_liboctave_error_handler) |
|
1305 ("unrecoverable error in dgetrs"); |
1948
|
1306 } |
|
1307 } |
|
1308 } |
458
|
1309 } |
|
1310 |
|
1311 return retval; |
|
1312 } |
|
1313 |
|
1314 ComplexMatrix |
|
1315 Matrix::solve (const ComplexMatrix& b) const |
|
1316 { |
|
1317 ComplexMatrix tmp (*this); |
|
1318 return tmp.solve (b); |
|
1319 } |
|
1320 |
|
1321 ComplexMatrix |
|
1322 Matrix::solve (const ComplexMatrix& b, int& info) const |
|
1323 { |
|
1324 ComplexMatrix tmp (*this); |
|
1325 return tmp.solve (b, info); |
|
1326 } |
|
1327 |
|
1328 ComplexMatrix |
|
1329 Matrix::solve (const ComplexMatrix& b, int& info, double& rcond) const |
|
1330 { |
|
1331 ComplexMatrix tmp (*this); |
|
1332 return tmp.solve (b, info, rcond); |
|
1333 } |
|
1334 |
3480
|
1335 ComplexMatrix |
|
1336 Matrix::solve (const ComplexMatrix& b, int& info, double& rcond, |
|
1337 solve_singularity_handler sing_handler) const |
|
1338 { |
|
1339 ComplexMatrix tmp (*this); |
|
1340 return tmp.solve (b, info, rcond, sing_handler); |
|
1341 } |
|
1342 |
458
|
1343 ColumnVector |
|
1344 Matrix::solve (const ColumnVector& b) const |
|
1345 { |
|
1346 int info; double rcond; |
|
1347 return solve (b, info, rcond); |
|
1348 } |
|
1349 |
|
1350 ColumnVector |
|
1351 Matrix::solve (const ColumnVector& b, int& info) const |
|
1352 { |
|
1353 double rcond; |
|
1354 return solve (b, info, rcond); |
|
1355 } |
|
1356 |
|
1357 ColumnVector |
532
|
1358 Matrix::solve (const ColumnVector& b, int& info, double& rcond) const |
458
|
1359 { |
3480
|
1360 return solve (b, info, rcond, 0); |
|
1361 } |
|
1362 |
|
1363 ColumnVector |
|
1364 Matrix::solve (const ColumnVector& b, int& info, double& rcond, |
|
1365 solve_singularity_handler sing_handler) const |
|
1366 { |
458
|
1367 ColumnVector retval; |
|
1368 |
|
1369 int nr = rows (); |
|
1370 int nc = cols (); |
1948
|
1371 |
458
|
1372 if (nr == 0 || nc == 0 || nr != nc || nr != b.length ()) |
1948
|
1373 (*current_liboctave_error_handler) |
|
1374 ("matrix dimension mismatch solution of linear equations"); |
458
|
1375 else |
|
1376 { |
1948
|
1377 info = 0; |
|
1378 |
|
1379 Array<int> ipvt (nr); |
|
1380 int *pipvt = ipvt.fortran_vec (); |
|
1381 |
|
1382 Matrix atmp = *this; |
|
1383 double *tmp_data = atmp.fortran_vec (); |
|
1384 |
4329
|
1385 Array<double> z (4 * nc); |
|
1386 double *pz = z.fortran_vec (); |
|
1387 Array<int> iz (nc); |
|
1388 int *piz = iz.fortran_vec (); |
|
1389 |
4330
|
1390 // Calculate the norm of the matrix, for later use. |
4329
|
1391 double anorm = atmp.abs().sum().row(0).max(); |
|
1392 |
|
1393 F77_XFCN (dgetrf, DGETRF, (nr, nr, tmp_data, nr, pipvt, info)); |
1948
|
1394 |
|
1395 if (f77_exception_encountered) |
4329
|
1396 (*current_liboctave_error_handler) ("unrecoverable error in dgetrf"); |
1948
|
1397 else |
|
1398 { |
4330
|
1399 // Throw-away extra info LAPACK gives so as to not change output. |
4509
|
1400 rcond = 0.0; |
|
1401 if (info > 0) |
1948
|
1402 { |
|
1403 info = -2; |
3480
|
1404 |
|
1405 if (sing_handler) |
|
1406 sing_handler (rcond); |
|
1407 else |
|
1408 (*current_liboctave_error_handler) |
4329
|
1409 ("matrix singular to machine precision"); |
|
1410 |
|
1411 } |
|
1412 else |
1948
|
1413 { |
4330
|
1414 // Now calculate the condition number for non-singular matrix. |
4329
|
1415 char job = '1'; |
4552
|
1416 F77_XFCN (dgecon, DGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1417 nc, tmp_data, nr, anorm, |
|
1418 rcond, pz, piz, info |
|
1419 F77_CHAR_ARG_LEN (1))); |
4329
|
1420 |
1948
|
1421 if (f77_exception_encountered) |
4329
|
1422 (*current_liboctave_error_handler) |
|
1423 ("unrecoverable error in dgecon"); |
|
1424 |
4509
|
1425 if (info != 0) |
4329
|
1426 info = -2; |
|
1427 |
|
1428 volatile double rcond_plus_one = rcond + 1.0; |
|
1429 |
|
1430 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
1431 { |
|
1432 info = -2; |
|
1433 |
|
1434 if (sing_handler) |
|
1435 sing_handler (rcond); |
|
1436 else |
|
1437 (*current_liboctave_error_handler) |
|
1438 ("matrix singular to machine precision, rcond = %g", |
|
1439 rcond); |
|
1440 } |
|
1441 else |
|
1442 { |
|
1443 retval = b; |
|
1444 double *result = retval.fortran_vec (); |
|
1445 |
4587
|
1446 job = 'N'; |
4552
|
1447 F77_XFCN (dgetrs, DGETRS, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1448 nr, 1, tmp_data, nr, pipvt, |
|
1449 result, b.length(), info |
|
1450 F77_CHAR_ARG_LEN (1))); |
4329
|
1451 |
|
1452 if (f77_exception_encountered) |
|
1453 (*current_liboctave_error_handler) |
|
1454 ("unrecoverable error in dgetrs"); |
|
1455 } |
1948
|
1456 } |
|
1457 } |
458
|
1458 } |
4329
|
1459 |
458
|
1460 return retval; |
|
1461 } |
|
1462 |
|
1463 ComplexColumnVector |
|
1464 Matrix::solve (const ComplexColumnVector& b) const |
|
1465 { |
|
1466 ComplexMatrix tmp (*this); |
|
1467 return tmp.solve (b); |
|
1468 } |
|
1469 |
|
1470 ComplexColumnVector |
|
1471 Matrix::solve (const ComplexColumnVector& b, int& info) const |
|
1472 { |
|
1473 ComplexMatrix tmp (*this); |
|
1474 return tmp.solve (b, info); |
|
1475 } |
|
1476 |
|
1477 ComplexColumnVector |
|
1478 Matrix::solve (const ComplexColumnVector& b, int& info, double& rcond) const |
|
1479 { |
|
1480 ComplexMatrix tmp (*this); |
|
1481 return tmp.solve (b, info, rcond); |
|
1482 } |
|
1483 |
3480
|
1484 ComplexColumnVector |
|
1485 Matrix::solve (const ComplexColumnVector& b, int& info, double& rcond, |
|
1486 solve_singularity_handler sing_handler) const |
|
1487 { |
|
1488 ComplexMatrix tmp (*this); |
|
1489 return tmp.solve (b, info, rcond, sing_handler); |
|
1490 } |
|
1491 |
458
|
1492 Matrix |
|
1493 Matrix::lssolve (const Matrix& b) const |
|
1494 { |
|
1495 int info; |
|
1496 int rank; |
|
1497 return lssolve (b, info, rank); |
|
1498 } |
|
1499 |
|
1500 Matrix |
|
1501 Matrix::lssolve (const Matrix& b, int& info) const |
|
1502 { |
|
1503 int rank; |
|
1504 return lssolve (b, info, rank); |
|
1505 } |
|
1506 |
|
1507 Matrix |
|
1508 Matrix::lssolve (const Matrix& b, int& info, int& rank) const |
|
1509 { |
1948
|
1510 Matrix retval; |
|
1511 |
458
|
1512 int nrhs = b.cols (); |
|
1513 |
|
1514 int m = rows (); |
|
1515 int n = cols (); |
|
1516 |
|
1517 if (m == 0 || n == 0 || m != b.rows ()) |
1948
|
1518 (*current_liboctave_error_handler) |
|
1519 ("matrix dimension mismatch in solution of least squares problem"); |
|
1520 else |
458
|
1521 { |
1948
|
1522 Matrix atmp = *this; |
|
1523 double *tmp_data = atmp.fortran_vec (); |
|
1524 |
|
1525 int nrr = m > n ? m : n; |
3754
|
1526 Matrix result (nrr, nrhs, 0.0); |
1948
|
1527 |
|
1528 for (int j = 0; j < nrhs; j++) |
|
1529 for (int i = 0; i < m; i++) |
|
1530 result.elem (i, j) = b.elem (i, j); |
|
1531 |
|
1532 double *presult = result.fortran_vec (); |
|
1533 |
|
1534 int len_s = m < n ? m : n; |
|
1535 Array<double> s (len_s); |
|
1536 double *ps = s.fortran_vec (); |
|
1537 |
|
1538 double rcond = -1.0; |
|
1539 |
3752
|
1540 // Ask DGELSS what the dimension of WORK should be. |
|
1541 |
|
1542 int lwork = -1; |
|
1543 |
|
1544 Array<double> work (1); |
1948
|
1545 |
|
1546 F77_XFCN (dgelss, DGELSS, (m, n, nrhs, tmp_data, m, presult, nrr, ps, |
3752
|
1547 rcond, rank, work.fortran_vec (), |
|
1548 lwork, info)); |
1948
|
1549 |
|
1550 if (f77_exception_encountered) |
|
1551 (*current_liboctave_error_handler) ("unrecoverable error in dgelss"); |
|
1552 else |
|
1553 { |
3752
|
1554 lwork = static_cast<int> (work(0)); |
|
1555 work.resize (lwork); |
|
1556 |
|
1557 F77_XFCN (dgelss, DGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
1558 nrr, ps, rcond, rank, |
|
1559 work.fortran_vec (), lwork, info)); |
|
1560 |
|
1561 if (f77_exception_encountered) |
|
1562 (*current_liboctave_error_handler) |
|
1563 ("unrecoverable error in dgelss"); |
|
1564 else |
|
1565 { |
|
1566 retval.resize (n, nrhs); |
|
1567 for (int j = 0; j < nrhs; j++) |
|
1568 for (int i = 0; i < n; i++) |
|
1569 retval.elem (i, j) = result.elem (i, j); |
|
1570 } |
1948
|
1571 } |
458
|
1572 } |
|
1573 |
|
1574 return retval; |
|
1575 } |
|
1576 |
|
1577 ComplexMatrix |
|
1578 Matrix::lssolve (const ComplexMatrix& b) const |
|
1579 { |
|
1580 ComplexMatrix tmp (*this); |
1484
|
1581 int info; |
|
1582 int rank; |
|
1583 return tmp.lssolve (b, info, rank); |
458
|
1584 } |
|
1585 |
|
1586 ComplexMatrix |
|
1587 Matrix::lssolve (const ComplexMatrix& b, int& info) const |
|
1588 { |
|
1589 ComplexMatrix tmp (*this); |
1484
|
1590 int rank; |
|
1591 return tmp.lssolve (b, info, rank); |
458
|
1592 } |
|
1593 |
|
1594 ComplexMatrix |
|
1595 Matrix::lssolve (const ComplexMatrix& b, int& info, int& rank) const |
|
1596 { |
|
1597 ComplexMatrix tmp (*this); |
1484
|
1598 return tmp.lssolve (b, info, rank); |
458
|
1599 } |
|
1600 |
|
1601 ColumnVector |
|
1602 Matrix::lssolve (const ColumnVector& b) const |
|
1603 { |
|
1604 int info; |
1484
|
1605 int rank; |
|
1606 return lssolve (b, info, rank); |
458
|
1607 } |
|
1608 |
|
1609 ColumnVector |
|
1610 Matrix::lssolve (const ColumnVector& b, int& info) const |
|
1611 { |
|
1612 int rank; |
|
1613 return lssolve (b, info, rank); |
|
1614 } |
|
1615 |
|
1616 ColumnVector |
|
1617 Matrix::lssolve (const ColumnVector& b, int& info, int& rank) const |
|
1618 { |
1948
|
1619 ColumnVector retval; |
|
1620 |
458
|
1621 int nrhs = 1; |
|
1622 |
|
1623 int m = rows (); |
|
1624 int n = cols (); |
|
1625 |
|
1626 if (m == 0 || n == 0 || m != b.length ()) |
1948
|
1627 (*current_liboctave_error_handler) |
|
1628 ("matrix dimension mismatch in solution of least squares problem"); |
|
1629 else |
458
|
1630 { |
1948
|
1631 Matrix atmp = *this; |
|
1632 double *tmp_data = atmp.fortran_vec (); |
|
1633 |
|
1634 int nrr = m > n ? m : n; |
|
1635 ColumnVector result (nrr); |
|
1636 |
|
1637 for (int i = 0; i < m; i++) |
|
1638 result.elem (i) = b.elem (i); |
|
1639 |
|
1640 double *presult = result.fortran_vec (); |
|
1641 |
|
1642 int len_s = m < n ? m : n; |
|
1643 Array<double> s (len_s); |
|
1644 double *ps = s.fortran_vec (); |
|
1645 |
|
1646 double rcond = -1.0; |
|
1647 |
3752
|
1648 // Ask DGELSS what the dimension of WORK should be. |
|
1649 |
|
1650 int lwork = -1; |
|
1651 |
|
1652 Array<double> work (1); |
|
1653 |
|
1654 F77_XFCN (dgelss, DGELSS, (m, n, nrhs, tmp_data, m, presult, nrr, ps, |
|
1655 rcond, rank, work.fortran_vec (), |
|
1656 lwork, info)); |
1948
|
1657 |
|
1658 if (f77_exception_encountered) |
|
1659 (*current_liboctave_error_handler) ("unrecoverable error in dgelss"); |
|
1660 else |
|
1661 { |
3752
|
1662 lwork = static_cast<int> (work(0)); |
|
1663 work.resize (lwork); |
|
1664 |
|
1665 F77_XFCN (dgelss, DGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
1666 nrr, ps, rcond, rank, |
|
1667 work.fortran_vec (), lwork, info)); |
|
1668 |
|
1669 if (f77_exception_encountered) |
|
1670 (*current_liboctave_error_handler) |
|
1671 ("unrecoverable error in dgelss"); |
|
1672 else |
|
1673 { |
|
1674 retval.resize (n); |
|
1675 for (int i = 0; i < n; i++) |
|
1676 retval.elem (i) = result.elem (i); |
|
1677 } |
1948
|
1678 } |
458
|
1679 } |
|
1680 |
|
1681 return retval; |
|
1682 } |
|
1683 |
|
1684 ComplexColumnVector |
|
1685 Matrix::lssolve (const ComplexColumnVector& b) const |
|
1686 { |
|
1687 ComplexMatrix tmp (*this); |
|
1688 return tmp.lssolve (b); |
|
1689 } |
|
1690 |
|
1691 ComplexColumnVector |
|
1692 Matrix::lssolve (const ComplexColumnVector& b, int& info) const |
|
1693 { |
|
1694 ComplexMatrix tmp (*this); |
|
1695 return tmp.lssolve (b, info); |
|
1696 } |
|
1697 |
|
1698 ComplexColumnVector |
|
1699 Matrix::lssolve (const ComplexColumnVector& b, int& info, int& rank) const |
|
1700 { |
|
1701 ComplexMatrix tmp (*this); |
|
1702 return tmp.lssolve (b, info, rank); |
|
1703 } |
|
1704 |
1819
|
1705 // Constants for matrix exponential calculation. |
|
1706 |
|
1707 static double padec [] = |
|
1708 { |
|
1709 5.0000000000000000e-1, |
|
1710 1.1666666666666667e-1, |
|
1711 1.6666666666666667e-2, |
|
1712 1.6025641025641026e-3, |
|
1713 1.0683760683760684e-4, |
|
1714 4.8562548562548563e-6, |
|
1715 1.3875013875013875e-7, |
|
1716 1.9270852604185938e-9, |
|
1717 }; |
|
1718 |
|
1719 Matrix |
|
1720 Matrix::expm (void) const |
|
1721 { |
|
1722 Matrix retval; |
|
1723 |
|
1724 Matrix m = *this; |
|
1725 |
|
1726 int nc = columns (); |
|
1727 |
3130
|
1728 // Preconditioning step 1: trace normalization to reduce dynamic |
|
1729 // range of poles, but avoid making stable eigenvalues unstable. |
|
1730 |
1819
|
1731 // trace shift value |
3331
|
1732 volatile double trshift = 0.0; |
1819
|
1733 |
|
1734 for (int i = 0; i < nc; i++) |
|
1735 trshift += m.elem (i, i); |
|
1736 |
|
1737 trshift /= nc; |
|
1738 |
3130
|
1739 if (trshift > 0.0) |
|
1740 { |
|
1741 for (int i = 0; i < nc; i++) |
|
1742 m.elem (i, i) -= trshift; |
|
1743 } |
1819
|
1744 |
3331
|
1745 // Preconditioning step 2: balancing; code follows development |
|
1746 // in AEPBAL |
|
1747 |
|
1748 double *p_m = m.fortran_vec (); |
|
1749 |
3468
|
1750 int info, ilo, ihi, ilos, ihis; |
|
1751 Array<double> dpermute (nc); |
|
1752 Array<double> dscale (nc); |
3466
|
1753 |
3468
|
1754 // permutation first |
|
1755 char job = 'P'; |
4552
|
1756 F77_XFCN (dgebal, DGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1757 nc, p_m, nc, ilo, ihi, |
|
1758 dpermute.fortran_vec (), info |
|
1759 F77_CHAR_ARG_LEN (1))); |
3466
|
1760 |
3468
|
1761 // then scaling |
|
1762 job = 'S'; |
4552
|
1763 F77_XFCN (dgebal, DGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1764 nc, p_m, nc, ilos, ihis, |
|
1765 dscale.fortran_vec (), info |
|
1766 F77_CHAR_ARG_LEN (1))); |
3331
|
1767 |
|
1768 if (f77_exception_encountered) |
|
1769 { |
|
1770 (*current_liboctave_error_handler) ("unrecoverable error in dgebal"); |
|
1771 return retval; |
|
1772 } |
|
1773 |
1819
|
1774 // Preconditioning step 3: scaling. |
3331
|
1775 |
1819
|
1776 ColumnVector work(nc); |
3130
|
1777 double inf_norm; |
3331
|
1778 |
4552
|
1779 F77_XFCN (xdlange, XDLANGE, (F77_CONST_CHAR_ARG2 ("I", 1), |
|
1780 nc, nc, m.fortran_vec (), nc, |
|
1781 work.fortran_vec (), inf_norm |
|
1782 F77_CHAR_ARG_LEN (1))); |
3331
|
1783 |
|
1784 if (f77_exception_encountered) |
|
1785 { |
|
1786 (*current_liboctave_error_handler) ("unrecoverable error in dlange"); |
|
1787 return retval; |
|
1788 } |
1819
|
1789 |
|
1790 int sqpow = (int) (inf_norm > 0.0 |
|
1791 ? (1.0 + log (inf_norm) / log (2.0)) |
|
1792 : 0.0); |
3331
|
1793 |
1819
|
1794 // Check whether we need to square at all. |
3331
|
1795 |
1819
|
1796 if (sqpow < 0) |
|
1797 sqpow = 0; |
3331
|
1798 |
1819
|
1799 if (sqpow > 0) |
|
1800 { |
|
1801 double scale_factor = 1.0; |
|
1802 for (int i = 0; i < sqpow; i++) |
|
1803 scale_factor *= 2.0; |
3331
|
1804 |
1819
|
1805 m = m / scale_factor; |
|
1806 } |
3331
|
1807 |
1819
|
1808 // npp, dpp: pade' approx polynomial matrices. |
3331
|
1809 |
1819
|
1810 Matrix npp (nc, nc, 0.0); |
|
1811 Matrix dpp = npp; |
3331
|
1812 |
1819
|
1813 // Now powers a^8 ... a^1. |
3331
|
1814 |
1819
|
1815 int minus_one_j = -1; |
|
1816 for (int j = 7; j >= 0; j--) |
|
1817 { |
3573
|
1818 npp = m * npp + padec[j] * m; |
|
1819 dpp = m * dpp + (minus_one_j * padec[j]) * m; |
1819
|
1820 minus_one_j *= -1; |
|
1821 } |
3331
|
1822 |
1819
|
1823 // Zero power. |
3331
|
1824 |
1819
|
1825 dpp = -dpp; |
3130
|
1826 for (int j = 0; j < nc; j++) |
1819
|
1827 { |
|
1828 npp.elem (j, j) += 1.0; |
|
1829 dpp.elem (j, j) += 1.0; |
|
1830 } |
3331
|
1831 |
1819
|
1832 // Compute pade approximation = inverse (dpp) * npp. |
|
1833 |
3331
|
1834 retval = dpp.solve (npp, info); |
|
1835 |
1819
|
1836 // Reverse preconditioning step 3: repeated squaring. |
3331
|
1837 |
1819
|
1838 while (sqpow) |
|
1839 { |
|
1840 retval = retval * retval; |
|
1841 sqpow--; |
|
1842 } |
3331
|
1843 |
1819
|
1844 // Reverse preconditioning step 2: inverse balancing. |
3466
|
1845 // apply inverse scaling to computed exponential |
3468
|
1846 for (int i = 0; i < nc; i++) |
|
1847 for (int j = 0; j < nc; j++) |
|
1848 retval(i,j) *= dscale(i) / dscale(j); |
3466
|
1849 |
4153
|
1850 OCTAVE_QUIT; |
|
1851 |
3466
|
1852 // construct balancing permutation vector |
4593
|
1853 Array<int> iperm (nc); |
3468
|
1854 for (int i = 0; i < nc; i++) |
4593
|
1855 iperm(i) = i; // identity permutation |
3466
|
1856 |
|
1857 // leading permutations in forward order |
3468
|
1858 for (int i = 0; i < (ilo-1); i++) |
|
1859 { |
|
1860 int swapidx = static_cast<int> (dpermute(i)) - 1; |
4593
|
1861 int tmp = iperm(i); |
|
1862 iperm(i) = iperm (swapidx); |
|
1863 iperm(swapidx) = tmp; |
3468
|
1864 } |
3466
|
1865 |
|
1866 // trailing permutations must be done in reverse order |
3468
|
1867 for (int i = nc - 1; i >= ihi; i--) |
|
1868 { |
|
1869 int swapidx = static_cast<int> (dpermute(i)) - 1; |
4593
|
1870 int tmp = iperm(i); |
|
1871 iperm(i) = iperm(swapidx); |
|
1872 iperm(swapidx) = tmp; |
3468
|
1873 } |
3466
|
1874 |
|
1875 // construct inverse balancing permutation vector |
3468
|
1876 Array<int> invpvec (nc); |
|
1877 for (int i = 0; i < nc; i++) |
4593
|
1878 invpvec(iperm(i)) = i; // Thanks to R. A. Lippert for this method |
4153
|
1879 |
|
1880 OCTAVE_QUIT; |
3466
|
1881 |
|
1882 Matrix tmpMat = retval; |
3468
|
1883 for (int i = 0; i < nc; i++) |
|
1884 for (int j = 0; j < nc; j++) |
|
1885 retval(i,j) = tmpMat(invpvec(i),invpvec(j)); |
3466
|
1886 |
1819
|
1887 // Reverse preconditioning step 1: fix trace normalization. |
3331
|
1888 |
3130
|
1889 if (trshift > 0.0) |
|
1890 retval = exp (trshift) * retval; |
|
1891 |
|
1892 return retval; |
1819
|
1893 } |
|
1894 |
458
|
1895 Matrix& |
|
1896 Matrix::operator += (const DiagMatrix& a) |
|
1897 { |
2385
|
1898 int nr = rows (); |
|
1899 int nc = cols (); |
|
1900 |
|
1901 int a_nr = a.rows (); |
|
1902 int a_nc = a.cols (); |
|
1903 |
|
1904 if (nr != a_nr || nc != a_nc) |
458
|
1905 { |
2385
|
1906 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
1907 return *this; |
|
1908 } |
|
1909 |
|
1910 for (int i = 0; i < a.length (); i++) |
|
1911 elem (i, i) += a.elem (i, i); |
|
1912 |
|
1913 return *this; |
|
1914 } |
|
1915 |
|
1916 Matrix& |
|
1917 Matrix::operator -= (const DiagMatrix& a) |
|
1918 { |
2385
|
1919 int nr = rows (); |
|
1920 int nc = cols (); |
|
1921 |
|
1922 int a_nr = a.rows (); |
|
1923 int a_nc = a.cols (); |
|
1924 |
|
1925 if (nr != a_nr || nc != a_nc) |
458
|
1926 { |
2385
|
1927 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
1928 return *this; |
|
1929 } |
|
1930 |
|
1931 for (int i = 0; i < a.length (); i++) |
|
1932 elem (i, i) -= a.elem (i, i); |
|
1933 |
|
1934 return *this; |
|
1935 } |
|
1936 |
|
1937 // unary operations |
|
1938 |
2964
|
1939 boolMatrix |
458
|
1940 Matrix::operator ! (void) const |
|
1941 { |
|
1942 int nr = rows (); |
|
1943 int nc = cols (); |
|
1944 |
2964
|
1945 boolMatrix b (nr, nc); |
458
|
1946 |
|
1947 for (int j = 0; j < nc; j++) |
|
1948 for (int i = 0; i < nr; i++) |
|
1949 b.elem (i, j) = ! elem (i, j); |
|
1950 |
|
1951 return b; |
|
1952 } |
|
1953 |
1205
|
1954 // column vector by row vector -> matrix operations |
458
|
1955 |
1205
|
1956 Matrix |
|
1957 operator * (const ColumnVector& v, const RowVector& a) |
458
|
1958 { |
1948
|
1959 Matrix retval; |
|
1960 |
1205
|
1961 int len = v.length (); |
3233
|
1962 |
|
1963 if (len != 0) |
1205
|
1964 { |
3233
|
1965 int a_len = a.length (); |
|
1966 |
|
1967 retval.resize (len, a_len); |
|
1968 double *c = retval.fortran_vec (); |
|
1969 |
4552
|
1970 F77_XFCN (dgemm, DGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
1971 F77_CONST_CHAR_ARG2 ("N", 1), |
|
1972 len, a_len, 1, 1.0, v.data (), len, |
|
1973 a.data (), 1, 0.0, c, len |
|
1974 F77_CHAR_ARG_LEN (1) |
|
1975 F77_CHAR_ARG_LEN (1))); |
3233
|
1976 |
|
1977 if (f77_exception_encountered) |
|
1978 (*current_liboctave_error_handler) |
|
1979 ("unrecoverable error in dgemm"); |
1205
|
1980 } |
458
|
1981 |
1948
|
1982 return retval; |
458
|
1983 } |
|
1984 |
|
1985 // other operations. |
|
1986 |
|
1987 Matrix |
2676
|
1988 Matrix::map (d_d_Mapper f) const |
1205
|
1989 { |
2676
|
1990 Matrix b (*this); |
|
1991 return b.apply (f); |
1205
|
1992 } |
|
1993 |
3248
|
1994 boolMatrix |
|
1995 Matrix::map (b_d_Mapper f) const |
|
1996 { |
|
1997 int nr = rows (); |
|
1998 int nc = cols (); |
|
1999 |
|
2000 boolMatrix retval (nr, nc); |
|
2001 |
|
2002 for (int j = 0; j < nc; j++) |
|
2003 for (int i = 0; i < nr; i++) |
|
2004 retval(i,j) = f (elem(i,j)); |
|
2005 |
|
2006 return retval; |
|
2007 } |
|
2008 |
2676
|
2009 Matrix& |
|
2010 Matrix::apply (d_d_Mapper f) |
458
|
2011 { |
|
2012 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2013 |
|
2014 for (int i = 0; i < length (); i++) |
|
2015 d[i] = f (d[i]); |
2676
|
2016 |
|
2017 return *this; |
458
|
2018 } |
|
2019 |
2385
|
2020 bool |
4431
|
2021 Matrix::any_element_is_negative (bool neg_zero) const |
2385
|
2022 { |
4634
|
2023 int nel = nelem (); |
2385
|
2024 |
4431
|
2025 if (neg_zero) |
|
2026 { |
4634
|
2027 for (int i = 0; i < nel; i++) |
|
2028 if (lo_ieee_signbit (elem (i))) |
|
2029 return true; |
4431
|
2030 } |
|
2031 else |
|
2032 { |
4634
|
2033 for (int i = 0; i < nel; i++) |
|
2034 if (elem (i) < 0) |
|
2035 return true; |
4431
|
2036 } |
2385
|
2037 |
|
2038 return false; |
|
2039 } |
|
2040 |
|
2041 |
|
2042 bool |
|
2043 Matrix::any_element_is_inf_or_nan (void) const |
|
2044 { |
4634
|
2045 int nel = nelem (); |
|
2046 |
|
2047 for (int i = 0; i < nel; i++) |
|
2048 { |
|
2049 double val = elem (i); |
|
2050 if (xisinf (val) || xisnan (val)) |
|
2051 return true; |
|
2052 } |
|
2053 |
|
2054 return false; |
2385
|
2055 } |
|
2056 |
|
2057 bool |
|
2058 Matrix::all_elements_are_int_or_inf_or_nan (void) const |
|
2059 { |
4634
|
2060 int nel = nelem (); |
|
2061 |
|
2062 for (int i = 0; i < nel; i++) |
|
2063 { |
|
2064 double val = elem (i); |
|
2065 if (xisnan (val) || D_NINT (val) == val) |
|
2066 continue; |
|
2067 else |
|
2068 return false; |
|
2069 } |
2385
|
2070 |
|
2071 return true; |
|
2072 } |
|
2073 |
1968
|
2074 // Return nonzero if any element of M is not an integer. Also extract |
|
2075 // the largest and smallest values and return them in MAX_VAL and MIN_VAL. |
|
2076 |
2385
|
2077 bool |
1968
|
2078 Matrix::all_integers (double& max_val, double& min_val) const |
|
2079 { |
4634
|
2080 int nel = nelem (); |
|
2081 |
|
2082 if (nel > 0) |
1968
|
2083 { |
4634
|
2084 max_val = elem (0); |
|
2085 min_val = elem (0); |
1968
|
2086 } |
|
2087 else |
2385
|
2088 return false; |
1968
|
2089 |
4634
|
2090 for (int i = 0; i < nel; i++) |
|
2091 { |
|
2092 double val = elem (i); |
|
2093 |
|
2094 if (val > max_val) |
|
2095 max_val = val; |
|
2096 |
|
2097 if (val < min_val) |
|
2098 min_val = val; |
|
2099 |
|
2100 if (D_NINT (val) != val) |
|
2101 return false; |
|
2102 } |
2385
|
2103 |
|
2104 return true; |
1968
|
2105 } |
|
2106 |
2385
|
2107 bool |
1968
|
2108 Matrix::too_large_for_float (void) const |
|
2109 { |
4634
|
2110 int nel = nelem (); |
|
2111 |
|
2112 for (int i = 0; i < nel; i++) |
|
2113 { |
|
2114 double val = elem (i); |
|
2115 |
|
2116 if (val > FLT_MAX || val < FLT_MIN) |
|
2117 return true; |
|
2118 } |
1968
|
2119 |
2385
|
2120 return false; |
1968
|
2121 } |
|
2122 |
4015
|
2123 // XXX FIXME XXX Do these really belong here? Maybe they should be |
|
2124 // in a base class? |
458
|
2125 |
2832
|
2126 boolMatrix |
4015
|
2127 Matrix::all (int dim) const |
458
|
2128 { |
4015
|
2129 MX_ALL_OP (dim); |
458
|
2130 } |
|
2131 |
2832
|
2132 boolMatrix |
4015
|
2133 Matrix::any (int dim) const |
458
|
2134 { |
4015
|
2135 MX_ANY_OP (dim); |
458
|
2136 } |
|
2137 |
|
2138 Matrix |
3723
|
2139 Matrix::cumprod (int dim) const |
458
|
2140 { |
4015
|
2141 MX_CUMULATIVE_OP (Matrix, double, *=); |
458
|
2142 } |
|
2143 |
|
2144 Matrix |
3723
|
2145 Matrix::cumsum (int dim) const |
458
|
2146 { |
4015
|
2147 MX_CUMULATIVE_OP (Matrix, double, +=); |
458
|
2148 } |
|
2149 |
|
2150 Matrix |
3723
|
2151 Matrix::prod (int dim) const |
458
|
2152 { |
3864
|
2153 MX_REDUCTION_OP (Matrix, *=, 1.0, 1.0); |
458
|
2154 } |
|
2155 |
|
2156 Matrix |
3723
|
2157 Matrix::sum (int dim) const |
458
|
2158 { |
3864
|
2159 MX_REDUCTION_OP (Matrix, +=, 0.0, 0.0); |
458
|
2160 } |
|
2161 |
|
2162 Matrix |
3723
|
2163 Matrix::sumsq (int dim) const |
458
|
2164 { |
3864
|
2165 #define ROW_EXPR \ |
|
2166 double d = elem (i, j); \ |
|
2167 retval.elem (i, 0) += d * d |
|
2168 |
|
2169 #define COL_EXPR \ |
|
2170 double d = elem (i, j); \ |
|
2171 retval.elem (0, j) += d * d |
|
2172 |
|
2173 MX_BASE_REDUCTION_OP (Matrix, ROW_EXPR, COL_EXPR, 0.0, 0.0); |
|
2174 |
|
2175 #undef ROW_EXPR |
|
2176 #undef COL_EXPR |
458
|
2177 } |
|
2178 |
2385
|
2179 Matrix |
|
2180 Matrix::abs (void) const |
|
2181 { |
|
2182 int nr = rows (); |
|
2183 int nc = cols (); |
|
2184 |
|
2185 Matrix retval (nr, nc); |
|
2186 |
|
2187 for (int j = 0; j < nc; j++) |
|
2188 for (int i = 0; i < nr; i++) |
|
2189 retval (i, j) = fabs (elem (i, j)); |
|
2190 |
|
2191 return retval; |
|
2192 } |
|
2193 |
458
|
2194 ColumnVector |
|
2195 Matrix::diag (void) const |
|
2196 { |
|
2197 return diag (0); |
|
2198 } |
|
2199 |
|
2200 ColumnVector |
|
2201 Matrix::diag (int k) const |
|
2202 { |
|
2203 int nnr = rows (); |
|
2204 int nnc = cols (); |
|
2205 if (k > 0) |
|
2206 nnc -= k; |
|
2207 else if (k < 0) |
|
2208 nnr += k; |
|
2209 |
|
2210 ColumnVector d; |
|
2211 |
|
2212 if (nnr > 0 && nnc > 0) |
|
2213 { |
|
2214 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
2215 |
|
2216 d.resize (ndiag); |
|
2217 |
|
2218 if (k > 0) |
|
2219 { |
|
2220 for (int i = 0; i < ndiag; i++) |
|
2221 d.elem (i) = elem (i, i+k); |
|
2222 } |
4509
|
2223 else if (k < 0) |
458
|
2224 { |
|
2225 for (int i = 0; i < ndiag; i++) |
|
2226 d.elem (i) = elem (i-k, i); |
|
2227 } |
|
2228 else |
|
2229 { |
|
2230 for (int i = 0; i < ndiag; i++) |
|
2231 d.elem (i) = elem (i, i); |
|
2232 } |
|
2233 } |
|
2234 else |
4513
|
2235 (*current_liboctave_error_handler) |
|
2236 ("diag: requested diagonal out of range"); |
458
|
2237 |
|
2238 return d; |
|
2239 } |
|
2240 |
|
2241 ColumnVector |
|
2242 Matrix::row_min (void) const |
|
2243 { |
4587
|
2244 Array<int> dummy_idx; |
|
2245 return row_min (dummy_idx); |
458
|
2246 } |
|
2247 |
|
2248 ColumnVector |
4587
|
2249 Matrix::row_min (Array<int>& idx_arg) const |
458
|
2250 { |
|
2251 ColumnVector result; |
|
2252 |
|
2253 int nr = rows (); |
|
2254 int nc = cols (); |
|
2255 |
|
2256 if (nr > 0 && nc > 0) |
|
2257 { |
|
2258 result.resize (nr); |
4587
|
2259 idx_arg.resize (nr); |
458
|
2260 |
|
2261 for (int i = 0; i < nr; i++) |
|
2262 { |
4469
|
2263 int idx_j; |
|
2264 |
|
2265 double tmp_min = octave_NaN; |
|
2266 |
|
2267 for (idx_j = 0; idx_j < nc; idx_j++) |
2354
|
2268 { |
4469
|
2269 tmp_min = elem (i, idx_j); |
|
2270 |
|
2271 if (! octave_is_NaN_or_NA (tmp_min)) |
|
2272 break; |
|
2273 } |
|
2274 |
|
2275 for (int j = idx_j+1; j < nc; j++) |
|
2276 { |
|
2277 double tmp = elem (i, j); |
|
2278 |
|
2279 if (octave_is_NaN_or_NA (tmp)) |
|
2280 continue; |
|
2281 else if (tmp < tmp_min) |
2354
|
2282 { |
4469
|
2283 idx_j = j; |
|
2284 tmp_min = tmp; |
2354
|
2285 } |
|
2286 } |
|
2287 |
4469
|
2288 result.elem (i) = tmp_min; |
4587
|
2289 idx_arg.elem (i) = octave_is_NaN_or_NA (tmp_min) ? 0 : idx_j; |
458
|
2290 } |
|
2291 } |
|
2292 |
|
2293 return result; |
|
2294 } |
|
2295 |
|
2296 ColumnVector |
|
2297 Matrix::row_max (void) const |
|
2298 { |
4587
|
2299 Array<int> dummy_idx; |
|
2300 return row_max (dummy_idx); |
458
|
2301 } |
|
2302 |
|
2303 ColumnVector |
4587
|
2304 Matrix::row_max (Array<int>& idx_arg) const |
458
|
2305 { |
|
2306 ColumnVector result; |
|
2307 |
|
2308 int nr = rows (); |
|
2309 int nc = cols (); |
|
2310 |
|
2311 if (nr > 0 && nc > 0) |
|
2312 { |
|
2313 result.resize (nr); |
4587
|
2314 idx_arg.resize (nr); |
458
|
2315 |
|
2316 for (int i = 0; i < nr; i++) |
|
2317 { |
4469
|
2318 int idx_j; |
|
2319 |
|
2320 double tmp_max = octave_NaN; |
|
2321 |
|
2322 for (idx_j = 0; idx_j < nc; idx_j++) |
2354
|
2323 { |
4469
|
2324 tmp_max = elem (i, idx_j); |
|
2325 |
|
2326 if (! octave_is_NaN_or_NA (tmp_max)) |
|
2327 break; |
|
2328 } |
|
2329 |
|
2330 for (int j = idx_j+1; j < nc; j++) |
|
2331 { |
|
2332 double tmp = elem (i, j); |
|
2333 |
|
2334 if (octave_is_NaN_or_NA (tmp)) |
|
2335 continue; |
|
2336 else if (tmp > tmp_max) |
2354
|
2337 { |
4469
|
2338 idx_j = j; |
|
2339 tmp_max = tmp; |
2354
|
2340 } |
|
2341 } |
|
2342 |
4469
|
2343 result.elem (i) = tmp_max; |
4587
|
2344 idx_arg.elem (i) = octave_is_NaN_or_NA (tmp_max) ? 0 : idx_j; |
458
|
2345 } |
|
2346 } |
|
2347 |
|
2348 return result; |
|
2349 } |
|
2350 |
|
2351 RowVector |
|
2352 Matrix::column_min (void) const |
|
2353 { |
4587
|
2354 Array<int> dummy_idx; |
|
2355 return column_min (dummy_idx); |
458
|
2356 } |
2354
|
2357 |
458
|
2358 RowVector |
4587
|
2359 Matrix::column_min (Array<int>& idx_arg) const |
458
|
2360 { |
|
2361 RowVector result; |
|
2362 |
|
2363 int nr = rows (); |
|
2364 int nc = cols (); |
|
2365 |
|
2366 if (nr > 0 && nc > 0) |
|
2367 { |
|
2368 result.resize (nc); |
4587
|
2369 idx_arg.resize (nc); |
458
|
2370 |
|
2371 for (int j = 0; j < nc; j++) |
|
2372 { |
4469
|
2373 int idx_i; |
|
2374 |
|
2375 double tmp_min = octave_NaN; |
|
2376 |
|
2377 for (idx_i = 0; idx_i < nr; idx_i++) |
2354
|
2378 { |
4469
|
2379 tmp_min = elem (idx_i, j); |
|
2380 |
|
2381 if (! octave_is_NaN_or_NA (tmp_min)) |
|
2382 break; |
|
2383 } |
|
2384 |
|
2385 for (int i = idx_i+1; i < nr; i++) |
|
2386 { |
|
2387 double tmp = elem (i, j); |
|
2388 |
|
2389 if (octave_is_NaN_or_NA (tmp)) |
|
2390 continue; |
|
2391 else if (tmp < tmp_min) |
2354
|
2392 { |
4469
|
2393 idx_i = i; |
|
2394 tmp_min = tmp; |
2354
|
2395 } |
|
2396 } |
|
2397 |
4469
|
2398 result.elem (j) = tmp_min; |
4587
|
2399 idx_arg.elem (j) = octave_is_NaN_or_NA (tmp_min) ? 0 : idx_i; |
458
|
2400 } |
|
2401 } |
|
2402 |
|
2403 return result; |
|
2404 } |
|
2405 |
2354
|
2406 RowVector |
|
2407 Matrix::column_max (void) const |
|
2408 { |
4587
|
2409 Array<int> dummy_idx; |
|
2410 return column_max (dummy_idx); |
2354
|
2411 } |
458
|
2412 |
|
2413 RowVector |
4587
|
2414 Matrix::column_max (Array<int>& idx_arg) const |
458
|
2415 { |
|
2416 RowVector result; |
|
2417 |
|
2418 int nr = rows (); |
|
2419 int nc = cols (); |
|
2420 |
|
2421 if (nr > 0 && nc > 0) |
|
2422 { |
|
2423 result.resize (nc); |
4587
|
2424 idx_arg.resize (nc); |
458
|
2425 |
|
2426 for (int j = 0; j < nc; j++) |
|
2427 { |
4469
|
2428 int idx_i; |
|
2429 |
|
2430 double tmp_max = octave_NaN; |
|
2431 |
|
2432 for (idx_i = 0; idx_i < nr; idx_i++) |
2354
|
2433 { |
4469
|
2434 tmp_max = elem (idx_i, j); |
|
2435 |
|
2436 if (! octave_is_NaN_or_NA (tmp_max)) |
|
2437 break; |
|
2438 } |
|
2439 |
|
2440 for (int i = idx_i+1; i < nr; i++) |
|
2441 { |
|
2442 double tmp = elem (i, j); |
|
2443 |
|
2444 if (octave_is_NaN_or_NA (tmp)) |
|
2445 continue; |
|
2446 else if (tmp > tmp_max) |
2354
|
2447 { |
4469
|
2448 idx_i = i; |
|
2449 tmp_max = tmp; |
2354
|
2450 } |
|
2451 } |
|
2452 |
4469
|
2453 result.elem (j) = tmp_max; |
4587
|
2454 idx_arg.elem (j) = octave_is_NaN_or_NA (tmp_max) ? 0 : idx_i; |
458
|
2455 } |
|
2456 } |
|
2457 |
|
2458 return result; |
|
2459 } |
|
2460 |
3504
|
2461 std::ostream& |
|
2462 operator << (std::ostream& os, const Matrix& a) |
458
|
2463 { |
|
2464 for (int i = 0; i < a.rows (); i++) |
|
2465 { |
|
2466 for (int j = 0; j < a.cols (); j++) |
4130
|
2467 { |
|
2468 os << " "; |
|
2469 octave_write_double (os, a.elem (i, j)); |
|
2470 } |
458
|
2471 os << "\n"; |
|
2472 } |
|
2473 return os; |
|
2474 } |
|
2475 |
3504
|
2476 std::istream& |
|
2477 operator >> (std::istream& is, Matrix& a) |
458
|
2478 { |
|
2479 int nr = a.rows (); |
|
2480 int nc = a.cols (); |
|
2481 |
|
2482 if (nr < 1 || nc < 1) |
3504
|
2483 is.clear (std::ios::badbit); |
458
|
2484 else |
|
2485 { |
|
2486 double tmp; |
|
2487 for (int i = 0; i < nr; i++) |
|
2488 for (int j = 0; j < nc; j++) |
|
2489 { |
4130
|
2490 tmp = octave_read_double (is); |
458
|
2491 if (is) |
|
2492 a.elem (i, j) = tmp; |
|
2493 else |
2795
|
2494 goto done; |
458
|
2495 } |
|
2496 } |
|
2497 |
2795
|
2498 done: |
|
2499 |
458
|
2500 return is; |
|
2501 } |
|
2502 |
1819
|
2503 Matrix |
|
2504 Givens (double x, double y) |
|
2505 { |
|
2506 double cc, s, temp_r; |
|
2507 |
3887
|
2508 F77_FUNC (dlartg, DLARTG) (x, y, cc, s, temp_r); |
1819
|
2509 |
|
2510 Matrix g (2, 2); |
|
2511 |
|
2512 g.elem (0, 0) = cc; |
|
2513 g.elem (1, 1) = cc; |
|
2514 g.elem (0, 1) = s; |
|
2515 g.elem (1, 0) = -s; |
|
2516 |
|
2517 return g; |
|
2518 } |
|
2519 |
|
2520 Matrix |
|
2521 Sylvester (const Matrix& a, const Matrix& b, const Matrix& c) |
|
2522 { |
|
2523 Matrix retval; |
|
2524 |
|
2525 // XXX FIXME XXX -- need to check that a, b, and c are all the same |
|
2526 // size. |
|
2527 |
|
2528 // Compute Schur decompositions. |
|
2529 |
|
2530 SCHUR as (a, "U"); |
|
2531 SCHUR bs (b, "U"); |
|
2532 |
|
2533 // Transform c to new coordinates. |
|
2534 |
|
2535 Matrix ua = as.unitary_matrix (); |
|
2536 Matrix sch_a = as.schur_matrix (); |
|
2537 |
|
2538 Matrix ub = bs.unitary_matrix (); |
|
2539 Matrix sch_b = bs.schur_matrix (); |
|
2540 |
|
2541 Matrix cx = ua.transpose () * c * ub; |
|
2542 |
|
2543 // Solve the sylvester equation, back-transform, and return the |
|
2544 // solution. |
|
2545 |
|
2546 int a_nr = a.rows (); |
|
2547 int b_nr = b.rows (); |
|
2548 |
|
2549 double scale; |
|
2550 int info; |
|
2551 |
1950
|
2552 double *pa = sch_a.fortran_vec (); |
|
2553 double *pb = sch_b.fortran_vec (); |
|
2554 double *px = cx.fortran_vec (); |
|
2555 |
4552
|
2556 F77_XFCN (dtrsyl, DTRSYL, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
2557 F77_CONST_CHAR_ARG2 ("N", 1), |
|
2558 1, a_nr, b_nr, pa, a_nr, pb, |
|
2559 b_nr, px, a_nr, scale, info |
|
2560 F77_CHAR_ARG_LEN (1) |
|
2561 F77_CHAR_ARG_LEN (1))); |
1950
|
2562 |
|
2563 |
|
2564 if (f77_exception_encountered) |
|
2565 (*current_liboctave_error_handler) ("unrecoverable error in dtrsyl"); |
|
2566 else |
|
2567 { |
|
2568 // XXX FIXME XXX -- check info? |
1819
|
2569 |
1950
|
2570 retval = -ua*cx*ub.transpose (); |
|
2571 } |
1819
|
2572 |
|
2573 return retval; |
|
2574 } |
|
2575 |
2828
|
2576 // matrix by matrix -> matrix operations |
|
2577 |
|
2578 Matrix |
|
2579 operator * (const Matrix& m, const Matrix& a) |
|
2580 { |
|
2581 Matrix retval; |
|
2582 |
|
2583 int nr = m.rows (); |
|
2584 int nc = m.cols (); |
|
2585 |
|
2586 int a_nr = a.rows (); |
|
2587 int a_nc = a.cols (); |
|
2588 |
|
2589 if (nc != a_nr) |
|
2590 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
|
2591 else |
|
2592 { |
|
2593 if (nr == 0 || nc == 0 || a_nc == 0) |
|
2594 retval.resize (nr, a_nc, 0.0); |
|
2595 else |
|
2596 { |
|
2597 int ld = nr; |
|
2598 int lda = a_nr; |
|
2599 |
|
2600 retval.resize (nr, a_nc); |
|
2601 double *c = retval.fortran_vec (); |
|
2602 |
4552
|
2603 F77_XFCN (dgemm, DGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
2604 F77_CONST_CHAR_ARG2 ("N", 1), |
|
2605 nr, a_nc, nc, 1.0, m.data (), |
|
2606 ld, a.data (), lda, 0.0, c, nr |
|
2607 F77_CHAR_ARG_LEN (1) |
|
2608 F77_CHAR_ARG_LEN (1))); |
2828
|
2609 |
|
2610 if (f77_exception_encountered) |
|
2611 (*current_liboctave_error_handler) |
|
2612 ("unrecoverable error in dgemm"); |
|
2613 } |
|
2614 } |
|
2615 |
|
2616 return retval; |
|
2617 } |
|
2618 |
4309
|
2619 // XXX FIXME XXX -- it would be nice to share code among the min/max |
|
2620 // functions below. |
|
2621 |
|
2622 #define EMPTY_RETURN_CHECK(T) \ |
|
2623 if (nr == 0 || nc == 0) \ |
|
2624 return T (nr, nc); |
|
2625 |
|
2626 Matrix |
|
2627 min (double d, const Matrix& m) |
|
2628 { |
|
2629 int nr = m.rows (); |
|
2630 int nc = m.columns (); |
|
2631 |
|
2632 EMPTY_RETURN_CHECK (Matrix); |
|
2633 |
|
2634 Matrix result (nr, nc); |
|
2635 |
|
2636 for (int j = 0; j < nc; j++) |
|
2637 for (int i = 0; i < nr; i++) |
|
2638 { |
|
2639 OCTAVE_QUIT; |
|
2640 result (i, j) = xmin (d, m (i, j)); |
|
2641 } |
|
2642 |
|
2643 return result; |
|
2644 } |
|
2645 |
|
2646 Matrix |
|
2647 min (const Matrix& m, double d) |
|
2648 { |
|
2649 int nr = m.rows (); |
|
2650 int nc = m.columns (); |
|
2651 |
|
2652 EMPTY_RETURN_CHECK (Matrix); |
|
2653 |
|
2654 Matrix result (nr, nc); |
|
2655 |
|
2656 for (int j = 0; j < nc; j++) |
|
2657 for (int i = 0; i < nr; i++) |
|
2658 { |
|
2659 OCTAVE_QUIT; |
|
2660 result (i, j) = xmin (m (i, j), d); |
|
2661 } |
|
2662 |
|
2663 return result; |
|
2664 } |
|
2665 |
|
2666 Matrix |
|
2667 min (const Matrix& a, const Matrix& b) |
|
2668 { |
|
2669 int nr = a.rows (); |
|
2670 int nc = a.columns (); |
|
2671 |
|
2672 if (nr != b.rows () || nc != b.columns ()) |
|
2673 { |
|
2674 (*current_liboctave_error_handler) |
|
2675 ("two-arg min expecting args of same size"); |
|
2676 return Matrix (); |
|
2677 } |
|
2678 |
|
2679 EMPTY_RETURN_CHECK (Matrix); |
|
2680 |
|
2681 Matrix result (nr, nc); |
|
2682 |
|
2683 for (int j = 0; j < nc; j++) |
|
2684 for (int i = 0; i < nr; i++) |
|
2685 { |
|
2686 OCTAVE_QUIT; |
|
2687 result (i, j) = xmin (a (i, j), b (i, j)); |
|
2688 } |
|
2689 |
|
2690 return result; |
|
2691 } |
|
2692 |
|
2693 Matrix |
|
2694 max (double d, const Matrix& m) |
|
2695 { |
|
2696 int nr = m.rows (); |
|
2697 int nc = m.columns (); |
|
2698 |
|
2699 EMPTY_RETURN_CHECK (Matrix); |
|
2700 |
|
2701 Matrix result (nr, nc); |
|
2702 |
|
2703 for (int j = 0; j < nc; j++) |
|
2704 for (int i = 0; i < nr; i++) |
|
2705 { |
|
2706 OCTAVE_QUIT; |
|
2707 result (i, j) = xmax (d, m (i, j)); |
|
2708 } |
|
2709 |
|
2710 return result; |
|
2711 } |
|
2712 |
|
2713 Matrix |
|
2714 max (const Matrix& m, double d) |
|
2715 { |
|
2716 int nr = m.rows (); |
|
2717 int nc = m.columns (); |
|
2718 |
|
2719 EMPTY_RETURN_CHECK (Matrix); |
|
2720 |
|
2721 Matrix result (nr, nc); |
|
2722 |
|
2723 for (int j = 0; j < nc; j++) |
|
2724 for (int i = 0; i < nr; i++) |
|
2725 { |
|
2726 OCTAVE_QUIT; |
|
2727 result (i, j) = xmax (m (i, j), d); |
|
2728 } |
|
2729 |
|
2730 return result; |
|
2731 } |
|
2732 |
|
2733 Matrix |
|
2734 max (const Matrix& a, const Matrix& b) |
|
2735 { |
|
2736 int nr = a.rows (); |
|
2737 int nc = a.columns (); |
|
2738 |
|
2739 if (nr != b.rows () || nc != b.columns ()) |
|
2740 { |
|
2741 (*current_liboctave_error_handler) |
|
2742 ("two-arg max expecting args of same size"); |
|
2743 return Matrix (); |
|
2744 } |
|
2745 |
|
2746 EMPTY_RETURN_CHECK (Matrix); |
|
2747 |
|
2748 Matrix result (nr, nc); |
|
2749 |
|
2750 for (int j = 0; j < nc; j++) |
|
2751 for (int i = 0; i < nr; i++) |
|
2752 { |
|
2753 OCTAVE_QUIT; |
|
2754 result (i, j) = xmax (a (i, j), b (i, j)); |
|
2755 } |
|
2756 |
|
2757 return result; |
|
2758 } |
|
2759 |
2870
|
2760 MS_CMP_OPS(Matrix, , double, ) |
3504
|
2761 MS_BOOL_OPS(Matrix, double, 0.0) |
2870
|
2762 |
|
2763 SM_CMP_OPS(double, , Matrix, ) |
3504
|
2764 SM_BOOL_OPS(double, Matrix, 0.0) |
2870
|
2765 |
|
2766 MM_CMP_OPS(Matrix, , Matrix, ) |
3504
|
2767 MM_BOOL_OPS(Matrix, Matrix, 0.0) |
2870
|
2768 |
458
|
2769 /* |
|
2770 ;;; Local Variables: *** |
|
2771 ;;; mode: C++ *** |
|
2772 ;;; End: *** |
|
2773 */ |