3
|
1 // Extra Matrix manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
296
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
3
|
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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
238
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
3
|
26 #endif |
|
27 |
238
|
28 #include <iostream.h> |
326
|
29 #include <float.h> |
238
|
30 |
3
|
31 #include "Matrix.h" |
|
32 #include "mx-inlines.cc" |
227
|
33 #include "lo-error.h" |
233
|
34 #include "f77-uscore.h" |
|
35 |
|
36 // Fortran functions we call. |
|
37 |
|
38 extern "C" |
|
39 { |
|
40 int F77_FCN (dgesv) (const int*, const int*, double*, const int*, |
|
41 int*, double*, const int*, int*); |
|
42 |
|
43 int F77_FCN (dgeqrf) (const int*, const int*, double*, const int*, |
|
44 double*, double*, const int*, int*); |
|
45 |
|
46 int F77_FCN (dorgqr) (const int*, const int*, const int*, double*, |
|
47 const int*, double*, double*, const int*, int*); |
|
48 |
|
49 int F77_FCN (dgeev) (const char*, const char*, const int*, double*, |
|
50 const int*, double*, double*, double*, |
|
51 const int*, double*, const int*, double*, |
|
52 const int*, int*, long, long); |
|
53 |
274
|
54 int F77_FCN (dgeesx) (const char*, const char*, |
|
55 int (*)(double*, double*), const char*, |
233
|
56 const int*, double*, const int*, int*, double*, |
|
57 double*, double*, const int*, double*, double*, |
|
58 double*, const int*, int*, const int*, int*, |
|
59 int*, long, long); |
|
60 |
|
61 int F77_FCN (dgebal) (const char*, const int*, double*, |
|
62 const int*, int*, int*, double*, |
|
63 int*, long, long); |
|
64 |
|
65 int F77_FCN (dgebak) (const char*, const char*, const int*, const int*, |
|
66 const int*, double*, const int*, double*, const int*, |
|
67 int*, long, long); |
|
68 |
|
69 int F77_FCN (dgehrd) (const int*, const int*, const int*, |
|
70 double*, const int*, double*, double*, |
|
71 const int*, int*, long, long); |
|
72 |
|
73 int F77_FCN (dorghr) (const int*, const int*, const int*, |
|
74 double*, const int*, double*, double*, |
|
75 const int*, int*, long, long); |
|
76 |
|
77 int F77_FCN (dgesvd) (const char*, const char*, const int*, |
|
78 const int*, double*, const int*, double*, |
|
79 double*, const int*, double*, const int*, |
|
80 double*, const int*, int*, long, long); |
|
81 |
|
82 int F77_FCN (dpotrf) (const char*, const int*, double*, const int*, |
|
83 int*, long); |
|
84 |
|
85 // |
|
86 // fortran functions for generalized eigenvalue problems |
|
87 // |
|
88 int F77_FCN (reduce) (const int*, const int*, double*, |
|
89 const int*, double*, |
|
90 int*, int*, double*, double*); |
|
91 |
|
92 int F77_FCN (scaleg) (const int*, const int*, double*, |
|
93 const int*, double*, |
|
94 const int*, const int*, double*, double*, double*); |
|
95 |
|
96 int F77_FCN (gradeq) (const int*, const int*, double*, |
|
97 const int*, double*, |
|
98 int*, int*, double*, double*); |
|
99 |
|
100 /* |
|
101 * f2c translates complex*16 as |
|
102 * |
|
103 * typedef struct { doublereal re, im; } doublecomplex; |
|
104 * |
|
105 * and Complex.h from libg++ uses |
|
106 * |
|
107 * protected: |
|
108 * double re; |
|
109 * double im; |
|
110 * |
|
111 * as the only data members, so this should work (fingers crossed that |
|
112 * things don't change). |
|
113 */ |
|
114 |
|
115 int F77_FCN (zgesv) (const int*, const int*, Complex*, const int*, |
|
116 int*, Complex*, const int*, int*); |
|
117 |
|
118 int F77_FCN (zgeqrf) (const int*, const int*, Complex*, const int*, |
|
119 Complex*, Complex*, const int*, int*); |
|
120 |
274
|
121 int F77_FCN (zgeesx) (const char*, const char*, int (*)(Complex*), |
|
122 const char*, const int*, Complex*, const int*, |
|
123 int*, Complex*, Complex*, const int*, double*, |
|
124 double*, Complex*, const int*, double*, int*, |
|
125 int*, long, long); |
233
|
126 |
|
127 int F77_FCN (zgebal) (const char*, const int*, Complex*, const int*, |
|
128 int*, int*, double*, int*, long, long); |
|
129 |
|
130 int F77_FCN (zgebak) (const char*, const char*, const int*, const int*, |
|
131 const int*, double*, const int*, Complex*, |
|
132 const int*, int*, long, long); |
|
133 |
|
134 int F77_FCN (zgehrd) (const int*, const int*, const int*, Complex*, |
|
135 const int*, Complex*, Complex*, const int*, |
|
136 int*, long, long); |
|
137 |
|
138 int F77_FCN (zunghr) (const int*, const int*, const int*, Complex*, |
|
139 const int*, Complex*, Complex*, const int*, |
|
140 int*, long, long); |
|
141 |
|
142 int F77_FCN (zungqr) (const int*, const int*, const int*, Complex*, |
|
143 const int*, Complex*, Complex*, const int*, int*); |
|
144 |
|
145 int F77_FCN (zgeev) (const char*, const char*, const int*, Complex*, |
|
146 const int*, Complex*, Complex*, const int*, |
|
147 Complex*, const int*, Complex*, const int*, |
|
148 double*, int*, long, long); |
|
149 |
|
150 int F77_FCN (zgesvd) (const char*, const char*, const int*, |
|
151 const int*, Complex*, const int*, double*, |
|
152 Complex*, const int*, Complex*, const int*, |
|
153 Complex*, const int*, double*, int*, long, long); |
|
154 |
|
155 int F77_FCN (zpotrf) (const char*, const int*, Complex*, const int*, |
|
156 int*, long); |
|
157 } |
3
|
158 |
|
159 /* |
22
|
160 * AEPBALANCE operations |
|
161 */ |
|
162 |
|
163 int |
|
164 AEPBALANCE::init (const Matrix& a, const char *balance_job) |
|
165 { |
238
|
166 int a_nc = a.cols (); |
|
167 if (a.rows () != a_nc) |
227
|
168 { |
|
169 (*current_liboctave_error_handler) ("AEPBALANCE requires square matrix"); |
|
170 return -1; |
|
171 } |
22
|
172 |
238
|
173 int n = a_nc; |
22
|
174 |
|
175 // Parameters for balance call. |
|
176 |
|
177 int info; |
|
178 int ilo; |
|
179 int ihi; |
|
180 double *scale = new double [n]; |
|
181 |
|
182 // Copy matrix into local structure. |
|
183 |
|
184 balanced_mat = a; |
|
185 |
|
186 F77_FCN (dgebal) (balance_job, &n, balanced_mat.fortran_vec (), |
|
187 &n, &ilo, &ihi, scale, &info, 1L, 1L); |
|
188 |
|
189 // Initialize balancing matrix to identity. |
|
190 |
|
191 balancing_mat = Matrix (n, n, 0.0); |
|
192 for (int i = 0; i < n; i++) |
|
193 balancing_mat.elem (i ,i) = 1.0; |
|
194 |
|
195 F77_FCN (dgebak) (balance_job, "R", &n, &ilo, &ihi, scale, &n, |
|
196 balancing_mat.fortran_vec (), &n, &info, 1L, 1L); |
|
197 |
|
198 delete [] scale; |
|
199 |
|
200 return info; |
|
201 } |
|
202 |
|
203 int |
|
204 ComplexAEPBALANCE::init (const ComplexMatrix& a, const char *balance_job) |
|
205 { |
|
206 |
238
|
207 int n = a.cols (); |
22
|
208 |
|
209 // Parameters for balance call. |
|
210 |
|
211 int info; |
|
212 int ilo; |
|
213 int ihi; |
|
214 double *scale = new double [n]; |
|
215 |
|
216 // Copy matrix into local structure. |
|
217 |
|
218 balanced_mat = a; |
|
219 |
|
220 F77_FCN (zgebal) (balance_job, &n, balanced_mat.fortran_vec (), |
|
221 &n, &ilo, &ihi, scale, &info, 1L, 1L); |
|
222 |
|
223 // Initialize balancing matrix to identity. |
|
224 |
|
225 balancing_mat = Matrix (n, n, 0.0); |
|
226 for (int i = 0; i < n; i++) |
|
227 balancing_mat (i, i) = 1.0; |
|
228 |
|
229 F77_FCN (zgebak) (balance_job, "R", &n, &ilo, &ihi, scale, &n, |
238
|
230 balancing_mat.fortran_vec (), &n, &info, 1L, 1L); |
22
|
231 |
|
232 delete [] scale; |
|
233 |
|
234 return info; |
|
235 } |
|
236 |
|
237 /* |
|
238 * GEPBALANCE operations |
|
239 */ |
|
240 |
|
241 int |
|
242 GEPBALANCE::init (const Matrix& a, const Matrix& b, const char *balance_job) |
|
243 { |
238
|
244 int a_nr = a.rows (); |
|
245 int a_nc = a.cols (); |
|
246 int b_nr = b.rows (); |
|
247 if (a_nr != a_nc || a_nr != b_nr || b_nr != b.cols ()) |
227
|
248 { |
|
249 (*current_liboctave_error_handler) |
|
250 ("GEPBALANCE requires square matrices of the same size"); |
|
251 return -1; |
|
252 } |
22
|
253 |
238
|
254 int n = a_nc; |
22
|
255 |
|
256 // Parameters for balance call. |
|
257 |
|
258 int info; |
|
259 int ilo; |
|
260 int ihi; |
|
261 double *cscale = new double [n]; |
|
262 double *cperm = new double [n]; |
|
263 Matrix wk (n, 6, 0.0); |
|
264 |
|
265 // Back out the permutations: |
|
266 // |
|
267 // cscale contains the exponents of the column scaling factors in its |
|
268 // ilo through ihi locations and the reducing column permutations in |
|
269 // its first ilo-1 and its ihi+1 through n locations. |
|
270 // |
|
271 // cperm contains the column permutations applied in grading the a and b |
|
272 // submatrices in its ilo through ihi locations. |
|
273 // |
|
274 // wk contains the exponents of the row scaling factors in its ilo |
|
275 // through ihi locations, the reducing row permutations in its first |
|
276 // ilo-1 and its ihi+1 through n locations, and the row permutations |
|
277 // applied in grading the a and b submatrices in its n+ilo through |
|
278 // n+ihi locations. |
|
279 |
|
280 // Copy matrices into local structure. |
|
281 |
|
282 balanced_a_mat = a; |
|
283 balanced_b_mat = b; |
|
284 |
|
285 // Initialize balancing matrices to identity. |
|
286 |
238
|
287 left_balancing_mat = Matrix (n, n, 0.0); |
22
|
288 for (int i = 0; i < n; i++) |
|
289 left_balancing_mat (i, i) = 1.0; |
|
290 |
|
291 right_balancing_mat = left_balancing_mat; |
|
292 |
|
293 // Check for permutation option. |
|
294 |
|
295 if (*balance_job == 'P' || *balance_job == 'B') |
|
296 { |
233
|
297 F77_FCN (reduce) (&n, &n, balanced_a_mat.fortran_vec (), |
|
298 &n, balanced_b_mat.fortran_vec (), &ilo, &ihi, |
|
299 cscale, wk.fortran_vec ()); |
22
|
300 } |
|
301 else |
|
302 { |
|
303 |
|
304 // Set up for scaling later. |
|
305 |
|
306 ilo = 1; |
|
307 ihi = n; |
|
308 } |
|
309 |
|
310 // Check for scaling option. |
|
311 |
|
312 if ((*balance_job == 'S' || *balance_job == 'B') && ilo != ihi) |
|
313 { |
233
|
314 F77_FCN (scaleg) (&n, &n, balanced_a_mat.fortran_vec (), |
|
315 &n, balanced_b_mat.fortran_vec (), &ilo, &ihi, |
|
316 cscale, cperm, wk.fortran_vec ()); |
22
|
317 } |
|
318 else |
|
319 { |
|
320 |
|
321 // Set scaling data to 0's. |
|
322 |
|
323 for (int tmp = ilo-1; tmp < ihi; tmp++) |
|
324 { |
|
325 cscale[tmp] = 0.0; |
238
|
326 wk.elem (tmp, 0) = 0.0; |
22
|
327 } |
|
328 } |
|
329 |
|
330 // Scaleg returns exponents, not values, so... |
|
331 |
|
332 for (int tmp = ilo-1; tmp < ihi; tmp++) |
|
333 { |
238
|
334 cscale[tmp] = pow (2.0, cscale[tmp]); |
|
335 wk.elem (tmp, 0) = pow (2.0, -wk.elem (tmp, 0)); |
22
|
336 } |
|
337 |
|
338 // Column permutations/scaling. |
|
339 |
|
340 F77_FCN (dgebak) (balance_job, "R", &n, &ilo, &ihi, cscale, &n, |
|
341 right_balancing_mat.fortran_vec (), &n, &info, 1L, |
|
342 1L); |
|
343 |
|
344 // Row permutations/scaling. |
|
345 |
|
346 F77_FCN (dgebak) (balance_job, "L", &n, &ilo, &ihi, &wk.elem (0, 0), &n, |
|
347 left_balancing_mat.fortran_vec (), &n, &info, 1L, 1L); |
|
348 |
|
349 // XXX FIXME XXX --- these four lines need to be added and debugged. |
|
350 // GEPBALANCE::init will work without them, though, so here they are. |
|
351 |
|
352 #if 0 |
|
353 if ((*balance_job == 'P' || *balance_job == 'B') && ilo != ihi) |
|
354 { |
|
355 F77_FCN (gradeq) (&n, &n, balanced_a_mat.fortran_vec (), |
|
356 &n, balanced_b_mat.fortran_vec (), &ilo, &ihi, |
|
357 cperm, &wk.elem (0, 1)); |
|
358 } |
|
359 #endif |
|
360 |
|
361 // Transpose for aa = cc*a*dd convention... |
|
362 left_balancing_mat = left_balancing_mat.transpose (); |
|
363 |
|
364 delete [] cscale; |
|
365 delete [] cperm; |
|
366 |
|
367 return info; |
|
368 } |
|
369 |
|
370 /* |
182
|
371 * CHOL stuff |
|
372 */ |
|
373 |
|
374 int |
|
375 CHOL::init (const Matrix& a) |
|
376 { |
238
|
377 int a_nr = a.rows (); |
|
378 int a_nc = a.cols (); |
|
379 if (a_nr != a_nc) |
227
|
380 { |
|
381 (*current_liboctave_error_handler) ("CHOL requires square matrix"); |
|
382 return -1; |
|
383 } |
182
|
384 |
|
385 char uplo = 'U'; |
|
386 |
238
|
387 int n = a_nc; |
182
|
388 int info; |
|
389 |
238
|
390 double *h = dup (a.data (), a.length ()); |
182
|
391 |
|
392 F77_FCN (dpotrf) (&uplo, &n, h, &n, &info, 1L); |
|
393 |
|
394 chol_mat = Matrix (h, n, n); |
|
395 |
|
396 // If someone thinks of a more graceful way of doing this (or faster for |
|
397 // that matter :-)), please let me know! |
|
398 |
|
399 if (n > 1) |
238
|
400 for (int j = 0; j < a_nc; j++) |
|
401 for (int i = j+1; i < a_nr; i++) |
182
|
402 chol_mat.elem (i, j) = 0.0; |
|
403 |
|
404 return info; |
|
405 } |
|
406 |
|
407 int |
|
408 ComplexCHOL::init (const ComplexMatrix& a) |
|
409 { |
238
|
410 int a_nr = a.rows (); |
|
411 int a_nc = a.cols (); |
|
412 if (a_nr != a_nc) |
227
|
413 { |
|
414 (*current_liboctave_error_handler) |
|
415 ("ComplexCHOL requires square matrix"); |
|
416 return -1; |
|
417 } |
182
|
418 |
|
419 char uplo = 'U'; |
|
420 |
238
|
421 int n = a_nc; |
182
|
422 int info; |
|
423 |
238
|
424 Complex *h = dup (a.data (), a.length ()); |
182
|
425 |
|
426 F77_FCN (zpotrf) (&uplo, &n, h, &n, &info, 1L); |
|
427 |
|
428 chol_mat = ComplexMatrix (h, n, n); |
|
429 |
|
430 // If someone thinks of a more graceful way of doing this (or faster for |
|
431 // that matter :-)), please let me know! |
|
432 |
|
433 if (n > 1) |
238
|
434 for (int j = 0; j < a_nc; j++) |
|
435 for (int i = j+1; i < a_nr; i++) |
182
|
436 chol_mat.elem (i, j) = 0.0; |
|
437 |
|
438 return info; |
|
439 } |
|
440 |
|
441 /* |
3
|
442 * HESS stuff |
|
443 */ |
|
444 |
|
445 int |
|
446 HESS::init (const Matrix& a) |
|
447 { |
238
|
448 int a_nr = a.rows (); |
|
449 int a_nc = a.cols (); |
|
450 if (a_nr != a_nc) |
227
|
451 { |
|
452 (*current_liboctave_error_handler) ("HESS requires square matrix"); |
|
453 return -1; |
|
454 } |
3
|
455 |
22
|
456 char jobbal = 'N'; |
3
|
457 char side = 'R'; |
|
458 |
238
|
459 int n = a_nc; |
3
|
460 int lwork = 32 * n; |
|
461 int info; |
|
462 int ilo; |
|
463 int ihi; |
|
464 |
238
|
465 double *h = dup (a.data (), a.length ()); |
3
|
466 |
|
467 double *tau = new double [n+1]; |
|
468 double *scale = new double [n]; |
|
469 double *z = new double [n*n]; |
|
470 double *work = new double [lwork]; |
|
471 |
|
472 F77_FCN (dgebal) (&jobbal, &n, h, &n, &ilo, &ihi, scale, &info, |
|
473 1L, 1L); |
|
474 |
|
475 F77_FCN (dgehrd) (&n, &ilo, &ihi, h, &n, tau, work, &lwork, &info, |
|
476 1L, 1L); |
|
477 |
238
|
478 copy (z, h, n*n); |
3
|
479 |
|
480 F77_FCN (dorghr) (&n, &ilo, &ihi, z, &n, tau, work, &lwork, &info, |
|
481 1L, 1L); |
|
482 |
|
483 F77_FCN (dgebak) (&jobbal, &side, &n, &ilo, &ihi, scale, &n, z, &n, |
|
484 &info, 1L, 1L); |
|
485 |
|
486 // We need to clear out all of the area below the sub-diagonal which was used |
|
487 // to store the unitary matrix. |
|
488 |
238
|
489 hess_mat = Matrix (h, n, n); |
|
490 unitary_hess_mat = Matrix (z, n, n); |
3
|
491 |
|
492 // If someone thinks of a more graceful way of doing this (or faster for |
|
493 // that matter :-)), please let me know! |
|
494 |
|
495 if (n > 2) |
238
|
496 for (int j = 0; j < a_nc; j++) |
|
497 for (int i = j+2; i < a_nr; i++) |
|
498 hess_mat.elem (i, j) = 0; |
3
|
499 |
|
500 delete [] tau; |
|
501 delete [] work; |
|
502 delete [] scale; |
|
503 |
|
504 return info; |
|
505 } |
|
506 |
|
507 int |
|
508 ComplexHESS::init (const ComplexMatrix& a) |
|
509 { |
238
|
510 int a_nr = a.rows (); |
|
511 int a_nc = a.cols (); |
|
512 if (a_nr != a_nc) |
227
|
513 { |
|
514 (*current_liboctave_error_handler) |
|
515 ("ComplexHESS requires square matrix"); |
|
516 return -1; |
|
517 } |
3
|
518 |
22
|
519 char job = 'N'; |
3
|
520 char side = 'R'; |
|
521 |
238
|
522 int n = a_nc; |
3
|
523 int lwork = 32 * n; |
|
524 int info; |
|
525 int ilo; |
|
526 int ihi; |
|
527 |
238
|
528 Complex *h = dup (a.data (), a.length ()); |
3
|
529 |
|
530 double *scale = new double [n]; |
|
531 Complex *tau = new Complex [n-1]; |
|
532 Complex *work = new Complex [lwork]; |
|
533 Complex *z = new Complex [n*n]; |
|
534 |
|
535 F77_FCN (zgebal) (&job, &n, h, &n, &ilo, &ihi, scale, &info, 1L, 1L); |
|
536 |
|
537 F77_FCN (zgehrd) (&n, &ilo, &ihi, h, &n, tau, work, &lwork, &info, 1L, |
233
|
538 1L); |
3
|
539 |
238
|
540 copy (z, h, n*n); |
3
|
541 |
|
542 F77_FCN (zunghr) (&n, &ilo, &ihi, z, &n, tau, work, &lwork, &info, 1L, |
233
|
543 1L); |
3
|
544 |
|
545 F77_FCN (zgebak) (&job, &side, &n, &ilo, &ihi, scale, &n, z, &n, &info, |
233
|
546 1L, 1L); |
3
|
547 |
|
548 hess_mat = ComplexMatrix (h,n,n); |
|
549 unitary_hess_mat = ComplexMatrix (z,n,n); |
|
550 |
|
551 // If someone thinks of a more graceful way of doing this (or faster for |
|
552 // that matter :-)), please let me know! |
|
553 |
|
554 if (n > 2) |
238
|
555 for (int j = 0; j < a_nc; j++) |
|
556 for (int i = j+2; i < a_nr; i++) |
|
557 hess_mat.elem (i, j) = 0; |
3
|
558 |
|
559 delete [] work; |
|
560 delete [] tau; |
|
561 delete [] scale; |
|
562 |
|
563 return info; |
|
564 } |
|
565 |
|
566 /* |
|
567 * SCHUR stuff |
|
568 */ |
|
569 |
|
570 static int |
|
571 select_ana (double *a, double *b) |
|
572 { |
|
573 return (*a < 0.0); |
|
574 } |
|
575 |
|
576 static int |
|
577 select_dig (double *a, double *b) |
|
578 { |
|
579 return (hypot (*a, *b) < 1.0); |
|
580 } |
|
581 |
|
582 int |
|
583 SCHUR::init (const Matrix& a, const char *ord) |
|
584 { |
238
|
585 int a_nr = a.rows (); |
|
586 int a_nc = a.cols (); |
|
587 if (a_nr != a_nc) |
227
|
588 { |
|
589 (*current_liboctave_error_handler) ("SCHUR requires square matrix"); |
|
590 return -1; |
|
591 } |
3
|
592 |
|
593 char jobvs = 'V'; |
|
594 char sort; |
|
595 |
|
596 if (*ord == 'A' || *ord == 'D' || *ord == 'a' || *ord == 'd') |
|
597 sort = 'S'; |
|
598 else |
|
599 sort = 'N'; |
|
600 |
|
601 char sense = 'N'; |
|
602 |
238
|
603 int n = a_nc; |
3
|
604 int lwork = 8 * n; |
|
605 int liwork = 1; |
|
606 int info; |
|
607 int sdim; |
|
608 double rconde; |
|
609 double rcondv; |
|
610 |
238
|
611 double *s = dup (a.data (), a.length ()); |
3
|
612 |
|
613 double *wr = new double [n]; |
|
614 double *wi = new double [n]; |
|
615 double *q = new double [n*n]; |
|
616 double *work = new double [lwork]; |
|
617 |
|
618 // These are not referenced for the non-ordered Schur routine. |
|
619 |
|
620 int *iwork = (int *) NULL; |
|
621 int *bwork = (int *) NULL; |
|
622 if (*ord == 'A' || *ord == 'D' || *ord == 'a' || *ord == 'd') |
|
623 { |
|
624 iwork = new int [liwork]; |
|
625 bwork = new int [n]; |
|
626 } |
|
627 |
|
628 if (*ord == 'A' || *ord == 'a') |
|
629 { |
|
630 F77_FCN (dgeesx) (&jobvs, &sort, select_ana, &sense, &n, s, &n, |
|
631 &sdim, wr, wi, q, &n, &rconde, &rcondv, work, |
|
632 &lwork, iwork, &liwork, bwork, &info, 1L, 1L); |
|
633 } |
|
634 else if (*ord == 'D' || *ord == 'd') |
|
635 { |
|
636 F77_FCN (dgeesx) (&jobvs, &sort, select_dig, &sense, &n, s, &n, |
|
637 &sdim, wr, wi, q, &n, &rconde, &rcondv, work, |
|
638 &lwork, iwork, &liwork, bwork, &info, 1L, 1L); |
|
639 |
|
640 } |
|
641 else |
|
642 { |
274
|
643 F77_FCN (dgeesx) (&jobvs, &sort, (void *) 0, &sense, &n, s, |
3
|
644 &n, &sdim, wr, wi, q, &n, &rconde, &rcondv, |
|
645 work, &lwork, iwork, &liwork, bwork, &info, |
|
646 1L, 1L); |
|
647 } |
|
648 |
|
649 schur_mat = Matrix (s, n, n); |
|
650 unitary_mat = Matrix (q, n, n); |
|
651 |
|
652 delete [] wr; |
|
653 delete [] wi; |
|
654 delete [] work; |
|
655 delete [] iwork; |
|
656 delete [] bwork; |
|
657 |
|
658 return info; |
|
659 } |
|
660 |
|
661 static int |
|
662 complex_select_ana (Complex *a) |
|
663 { |
238
|
664 return a->real () < 0.0; |
3
|
665 } |
|
666 |
|
667 static int |
|
668 complex_select_dig (Complex *a) |
|
669 { |
|
670 return (abs (*a) < 1.0); |
|
671 } |
|
672 |
|
673 int |
|
674 ComplexSCHUR::init (const ComplexMatrix& a, const char *ord) |
|
675 { |
238
|
676 int a_nr = a.rows (); |
|
677 int a_nc = a.cols (); |
|
678 if (a_nr != a_nc) |
227
|
679 { |
|
680 (*current_liboctave_error_handler) |
|
681 ("ComplexSCHUR requires square matrix"); |
|
682 return -1; |
|
683 } |
3
|
684 |
|
685 char jobvs = 'V'; |
|
686 char sort; |
|
687 if (*ord == 'A' || *ord == 'D' || *ord == 'a' || *ord == 'd') |
|
688 sort = 'S'; |
|
689 else |
|
690 sort = 'N'; |
|
691 |
|
692 char sense = 'N'; |
|
693 |
238
|
694 int n = a_nc; |
3
|
695 int lwork = 8 * n; |
|
696 int info; |
|
697 int sdim; |
|
698 double rconde; |
|
699 double rcondv; |
|
700 |
|
701 double *rwork = new double [n]; |
|
702 |
|
703 // bwork is not referenced for non-ordered Schur. |
|
704 |
|
705 int *bwork = (int *) NULL; |
|
706 if (*ord == 'A' || *ord == 'D' || *ord == 'a' || *ord == 'd') |
|
707 bwork = new int [n]; |
|
708 |
238
|
709 Complex *s = dup (a.data (), a.length ()); |
3
|
710 |
|
711 Complex *work = new Complex [lwork]; |
|
712 Complex *q = new Complex [n*n]; |
|
713 Complex *w = new Complex [n]; |
|
714 |
|
715 if (*ord == 'A' || *ord == 'a') |
|
716 { |
|
717 F77_FCN (zgeesx) (&jobvs, &sort, complex_select_ana, &sense, |
|
718 &n, s, &n, &sdim, w, q, &n, &rconde, &rcondv, |
|
719 work, &lwork, rwork, bwork, &info, 1L, 1L); |
|
720 } |
|
721 else if (*ord == 'D' || *ord == 'd') |
|
722 { |
|
723 F77_FCN (zgeesx) (&jobvs, &sort, complex_select_dig, &sense, |
|
724 &n, s, &n, &sdim, w, q, &n, &rconde, &rcondv, |
|
725 work, &lwork, rwork, bwork, &info, 1L, 1L); |
|
726 } |
|
727 else |
|
728 { |
274
|
729 F77_FCN (zgeesx) (&jobvs, &sort, (void *) 0, &sense, &n, s, |
3
|
730 &n, &sdim, w, q, &n, &rconde, &rcondv, work, |
|
731 &lwork, rwork, bwork, &info, 1L, 1L); |
|
732 } |
|
733 |
|
734 schur_mat = ComplexMatrix (s,n,n); |
|
735 unitary_mat = ComplexMatrix (q,n,n); |
|
736 |
|
737 delete [] w; |
|
738 delete [] work; |
|
739 delete [] rwork; |
|
740 delete [] bwork; |
|
741 |
|
742 return info; |
|
743 } |
|
744 |
|
745 ostream& |
|
746 operator << (ostream& os, const SCHUR& a) |
|
747 { |
|
748 os << a.schur_matrix () << "\n"; |
|
749 os << a.unitary_matrix () << "\n"; |
|
750 |
|
751 return os; |
|
752 } |
|
753 |
|
754 /* |
|
755 * SVD stuff |
|
756 */ |
|
757 |
|
758 int |
|
759 SVD::init (const Matrix& a) |
|
760 { |
|
761 int info; |
|
762 |
238
|
763 int m = a.rows (); |
|
764 int n = a.cols (); |
3
|
765 |
|
766 char jobu = 'A'; |
|
767 char jobv = 'A'; |
|
768 |
238
|
769 double *tmp_data = dup (a.data (), a.length ()); |
3
|
770 |
|
771 int min_mn = m < n ? m : n; |
|
772 int max_mn = m > n ? m : n; |
|
773 |
|
774 double *u = new double[m*m]; |
|
775 double *s_vec = new double[min_mn]; |
|
776 double *vt = new double[n*n]; |
|
777 |
|
778 int tmp1 = 3*min_mn + max_mn; |
|
779 int tmp2 = 5*min_mn - 4; |
|
780 int lwork = tmp1 > tmp2 ? tmp1 : tmp2; |
|
781 double *work = new double[lwork]; |
|
782 |
|
783 F77_FCN (dgesvd) (&jobu, &jobv, &m, &n, tmp_data, &m, s_vec, u, &m, |
|
784 vt, &n, work, &lwork, &info, 1L, 1L); |
|
785 |
|
786 left_sm = Matrix (u, m, m); |
|
787 sigma = DiagMatrix (s_vec, m, n); |
|
788 Matrix vt_m (vt, n, n); |
|
789 right_sm = Matrix (vt_m.transpose ()); |
|
790 |
|
791 delete [] tmp_data; |
|
792 delete [] work; |
|
793 |
|
794 return info; |
|
795 } |
|
796 |
|
797 ostream& |
|
798 operator << (ostream& os, const SVD& a) |
|
799 { |
|
800 os << a.left_singular_matrix () << "\n"; |
|
801 os << a.singular_values () << "\n"; |
|
802 os << a.right_singular_matrix () << "\n"; |
|
803 |
|
804 return os; |
|
805 } |
|
806 |
|
807 int |
|
808 ComplexSVD::init (const ComplexMatrix& a) |
|
809 { |
|
810 int info; |
|
811 |
238
|
812 int m = a.rows (); |
|
813 int n = a.cols (); |
3
|
814 |
|
815 char jobu = 'A'; |
|
816 char jobv = 'A'; |
|
817 |
238
|
818 Complex *tmp_data = dup (a.data (), a.length ()); |
3
|
819 |
|
820 int min_mn = m < n ? m : n; |
|
821 int max_mn = m > n ? m : n; |
|
822 |
|
823 Complex *u = new Complex[m*m]; |
|
824 double *s_vec = new double[min_mn]; |
|
825 Complex *vt = new Complex[n*n]; |
|
826 |
|
827 int lwork = 2*min_mn + max_mn; |
|
828 Complex *work = new Complex[lwork]; |
|
829 |
|
830 int lrwork = 5*max_mn; |
|
831 double *rwork = new double[lrwork]; |
|
832 |
|
833 F77_FCN (zgesvd) (&jobu, &jobv, &m, &n, tmp_data, &m, s_vec, u, &m, |
|
834 vt, &n, work, &lwork, rwork, &info, 1L, 1L); |
|
835 |
|
836 left_sm = ComplexMatrix (u, m, m); |
|
837 sigma = DiagMatrix (s_vec, m, n); |
|
838 ComplexMatrix vt_m (vt, n, n); |
|
839 right_sm = ComplexMatrix (vt_m.hermitian ()); |
|
840 |
|
841 delete [] tmp_data; |
|
842 delete [] work; |
|
843 |
|
844 return info; |
|
845 } |
|
846 |
|
847 /* |
238
|
848 * DET stuff. |
|
849 */ |
|
850 |
|
851 int |
|
852 DET::value_will_overflow (void) const |
|
853 { |
326
|
854 return det[2] + 1 > log10 (DBL_MAX) ? 1 : 0; |
238
|
855 } |
|
856 |
|
857 int |
|
858 DET::value_will_underflow (void) const |
|
859 { |
326
|
860 return det[2] - 1 < log10 (DBL_MIN) ? 1 : 0; |
238
|
861 } |
|
862 |
|
863 double |
|
864 DET::coefficient (void) const |
|
865 { |
|
866 return det[0]; |
|
867 } |
|
868 |
|
869 int |
|
870 DET::exponent (void) const |
|
871 { |
|
872 return (int) det[1]; |
|
873 } |
|
874 |
|
875 double |
|
876 DET::value (void) const |
|
877 { |
|
878 return det[0] * pow (10.0, det[1]); |
|
879 } |
|
880 |
|
881 int |
|
882 ComplexDET::value_will_overflow (void) const |
|
883 { |
326
|
884 return det[2].real () + 1 > log10 (DBL_MAX) ? 1 : 0; |
238
|
885 } |
|
886 |
|
887 int |
|
888 ComplexDET::value_will_underflow (void) const |
|
889 { |
326
|
890 return det[2].real () - 1 < log10 (DBL_MIN) ? 1 : 0; |
238
|
891 } |
|
892 |
|
893 Complex |
|
894 ComplexDET::coefficient (void) const |
|
895 { |
|
896 return det[0]; |
|
897 } |
|
898 |
|
899 int |
|
900 ComplexDET::exponent (void) const |
|
901 { |
|
902 return (int) (det[1].real ()); |
|
903 } |
|
904 |
|
905 Complex |
|
906 ComplexDET::value (void) const |
|
907 { |
|
908 return det[0] * pow (10.0, det[1].real ()); |
|
909 } |
|
910 |
|
911 /* |
3
|
912 * EIG stuff. |
|
913 */ |
|
914 |
|
915 int |
|
916 EIG::init (const Matrix& a) |
|
917 { |
238
|
918 int a_nr = a.rows (); |
|
919 if (a_nr != a.cols ()) |
227
|
920 { |
|
921 (*current_liboctave_error_handler) ("EIG requires square matrix"); |
|
922 return -1; |
|
923 } |
3
|
924 |
238
|
925 int n = a_nr; |
3
|
926 |
|
927 int info; |
|
928 |
|
929 char jobvl = 'N'; |
|
930 char jobvr = 'V'; |
|
931 |
238
|
932 double *tmp_data = dup (a.data (), a.length ()); |
3
|
933 double *wr = new double[n]; |
|
934 double *wi = new double[n]; |
|
935 Matrix vr (n, n); |
|
936 double *pvr = vr.fortran_vec (); |
|
937 int lwork = 8*n; |
|
938 double *work = new double[lwork]; |
|
939 |
|
940 double dummy; |
|
941 int idummy = 1; |
|
942 |
|
943 F77_FCN (dgeev) (&jobvl, &jobvr, &n, tmp_data, &n, wr, wi, &dummy, |
|
944 &idummy, pvr, &n, work, &lwork, &info, 1L, 1L); |
|
945 |
|
946 lambda.resize (n); |
|
947 v.resize (n, n); |
|
948 |
|
949 for (int j = 0; j < n; j++) |
|
950 { |
|
951 if (wi[j] == 0.0) |
|
952 { |
|
953 lambda.elem (j) = Complex (wr[j]); |
|
954 for (int i = 0; i < n; i++) |
|
955 v.elem (i, j) = vr.elem (i, j); |
|
956 } |
|
957 else |
|
958 { |
|
959 if (j+1 >= n) |
227
|
960 { |
|
961 (*current_liboctave_error_handler) ("EIG: internal error"); |
|
962 return -1; |
|
963 } |
3
|
964 |
|
965 for (int i = 0; i < n; i++) |
|
966 { |
|
967 lambda.elem (j) = Complex (wr[j], wi[j]); |
|
968 lambda.elem (j+1) = Complex (wr[j+1], wi[j+1]); |
|
969 double real_part = vr.elem (i, j); |
|
970 double imag_part = vr.elem (i, j+1); |
|
971 v.elem (i, j) = Complex (real_part, imag_part); |
|
972 v.elem (i, j+1) = Complex (real_part, -imag_part); |
|
973 } |
|
974 j++; |
|
975 } |
|
976 } |
|
977 |
|
978 delete [] tmp_data; |
|
979 delete [] wr; |
|
980 delete [] wi; |
|
981 delete [] work; |
|
982 |
|
983 return info; |
|
984 } |
|
985 |
|
986 int |
|
987 EIG::init (const ComplexMatrix& a) |
|
988 { |
238
|
989 int a_nr = a.rows (); |
|
990 if (a_nr != a.cols ()) |
227
|
991 { |
|
992 (*current_liboctave_error_handler) ("EIG requires square matrix"); |
|
993 return -1; |
|
994 } |
3
|
995 |
238
|
996 int n = a_nr; |
3
|
997 |
|
998 int info; |
|
999 |
|
1000 char jobvl = 'N'; |
|
1001 char jobvr = 'V'; |
|
1002 |
|
1003 lambda.resize (n); |
|
1004 v.resize (n, n); |
|
1005 |
|
1006 Complex *pw = lambda.fortran_vec (); |
|
1007 Complex *pvr = v.fortran_vec (); |
|
1008 |
238
|
1009 Complex *tmp_data = dup (a.data (), a.length ()); |
3
|
1010 |
|
1011 int lwork = 8*n; |
|
1012 Complex *work = new Complex[lwork]; |
|
1013 double *rwork = new double[4*n]; |
|
1014 |
|
1015 Complex dummy; |
|
1016 int idummy = 1; |
|
1017 |
|
1018 F77_FCN (zgeev) (&jobvl, &jobvr, &n, tmp_data, &n, pw, &dummy, |
|
1019 &idummy, pvr, &n, work, &lwork, rwork, &info, 1L, |
|
1020 1L); |
|
1021 |
|
1022 delete [] tmp_data; |
|
1023 delete [] work; |
|
1024 delete [] rwork; |
|
1025 |
|
1026 return info; |
|
1027 } |
|
1028 |
|
1029 /* |
|
1030 * LU stuff. |
|
1031 */ |
|
1032 |
|
1033 LU::LU (const Matrix& a) |
|
1034 { |
238
|
1035 int a_nr = a.rows (); |
|
1036 int a_nc = a.cols (); |
|
1037 if (a_nr == 0 || a_nc == 0 || a_nr != a_nc) |
227
|
1038 { |
|
1039 (*current_liboctave_error_handler) ("LU requires square matrix"); |
|
1040 return; |
|
1041 } |
3
|
1042 |
238
|
1043 int n = a_nr; |
3
|
1044 |
|
1045 int *ipvt = new int [n]; |
|
1046 int *pvt = new int [n]; |
238
|
1047 double *tmp_data = dup (a.data (), a.length ()); |
3
|
1048 int info = 0; |
|
1049 int zero = 0; |
|
1050 double b; |
|
1051 |
|
1052 F77_FCN (dgesv) (&n, &zero, tmp_data, &n, ipvt, &b, &n, &info); |
|
1053 |
|
1054 Matrix A_fact (tmp_data, n, n); |
|
1055 |
|
1056 int i; |
|
1057 |
|
1058 for (i = 0; i < n; i++) |
|
1059 { |
|
1060 ipvt[i] -= 1; |
|
1061 pvt[i] = i; |
|
1062 } |
|
1063 |
|
1064 for (i = 0; i < n - 1; i++) |
|
1065 { |
|
1066 int k = ipvt[i]; |
|
1067 if (k != i) |
|
1068 { |
|
1069 int tmp = pvt[k]; |
|
1070 pvt[k] = pvt[i]; |
|
1071 pvt[i] = tmp; |
|
1072 } |
|
1073 } |
|
1074 |
|
1075 l.resize (n, n, 0.0); |
|
1076 u.resize (n, n, 0.0); |
|
1077 p.resize (n, n, 0.0); |
|
1078 |
|
1079 for (i = 0; i < n; i++) |
|
1080 { |
|
1081 p.elem (i, pvt[i]) = 1.0; |
|
1082 |
|
1083 int j; |
|
1084 |
|
1085 l.elem (i, i) = 1.0; |
|
1086 for (j = 0; j < i; j++) |
|
1087 l.elem (i, j) = A_fact.elem (i, j); |
|
1088 |
|
1089 for (j = i; j < n; j++) |
|
1090 u.elem (i, j) = A_fact.elem (i, j); |
|
1091 } |
|
1092 |
|
1093 delete [] ipvt; |
|
1094 delete [] pvt; |
|
1095 } |
|
1096 |
|
1097 ComplexLU::ComplexLU (const ComplexMatrix& a) |
|
1098 { |
238
|
1099 int a_nr = a.rows (); |
|
1100 int a_nc = a.cols (); |
|
1101 if (a_nr == 0 || a_nc == 0 || a_nr != a_nc) |
227
|
1102 { |
|
1103 (*current_liboctave_error_handler) ("ComplexLU requires square matrix"); |
|
1104 return; |
|
1105 } |
3
|
1106 |
238
|
1107 int n = a_nr; |
3
|
1108 |
|
1109 int *ipvt = new int [n]; |
|
1110 int *pvt = new int [n]; |
238
|
1111 Complex *tmp_data = dup (a.data (), a.length ()); |
3
|
1112 int info = 0; |
|
1113 int zero = 0; |
|
1114 Complex b; |
|
1115 |
|
1116 F77_FCN (zgesv) (&n, &zero, tmp_data, &n, ipvt, &b, &n, &info); |
|
1117 |
|
1118 ComplexMatrix A_fact (tmp_data, n, n); |
|
1119 |
|
1120 int i; |
|
1121 |
|
1122 for (i = 0; i < n; i++) |
|
1123 { |
|
1124 ipvt[i] -= 1; |
|
1125 pvt[i] = i; |
|
1126 } |
|
1127 |
|
1128 for (i = 0; i < n - 1; i++) |
|
1129 { |
|
1130 int k = ipvt[i]; |
|
1131 if (k != i) |
|
1132 { |
|
1133 int tmp = pvt[k]; |
|
1134 pvt[k] = pvt[i]; |
|
1135 pvt[i] = tmp; |
|
1136 } |
|
1137 } |
|
1138 |
|
1139 l.resize (n, n, 0.0); |
|
1140 u.resize (n, n, 0.0); |
|
1141 p.resize (n, n, 0.0); |
|
1142 |
|
1143 for (i = 0; i < n; i++) |
|
1144 { |
|
1145 p.elem (i, pvt[i]) = 1.0; |
|
1146 |
|
1147 int j; |
|
1148 |
|
1149 l.elem (i, i) = 1.0; |
|
1150 for (j = 0; j < i; j++) |
|
1151 l.elem (i, j) = A_fact.elem (i, j); |
|
1152 |
|
1153 for (j = i; j < n; j++) |
|
1154 u.elem (i, j) = A_fact.elem (i, j); |
|
1155 } |
|
1156 |
|
1157 delete [] ipvt; |
|
1158 delete [] pvt; |
|
1159 } |
|
1160 |
|
1161 /* |
|
1162 * QR stuff. |
|
1163 */ |
|
1164 |
|
1165 QR::QR (const Matrix& a) |
|
1166 { |
238
|
1167 int m = a.rows (); |
|
1168 int n = a.cols (); |
3
|
1169 |
|
1170 if (m == 0 || n == 0) |
227
|
1171 { |
|
1172 (*current_liboctave_error_handler) ("QR must have non-empty matrix"); |
|
1173 return; |
|
1174 } |
3
|
1175 |
|
1176 double *tmp_data; |
|
1177 int min_mn = m < n ? m : n; |
|
1178 double *tau = new double[min_mn]; |
|
1179 int lwork = 32*n; |
|
1180 double *work = new double[lwork]; |
|
1181 int info = 0; |
|
1182 |
|
1183 if (m > n) |
|
1184 { |
|
1185 tmp_data = new double [m*m]; |
238
|
1186 copy (tmp_data, a.data (), a.length ()); |
3
|
1187 } |
|
1188 else |
238
|
1189 tmp_data = dup (a.data (), a.length ()); |
3
|
1190 |
|
1191 F77_FCN (dgeqrf) (&m, &n, tmp_data, &m, tau, work, &lwork, &info); |
|
1192 |
|
1193 delete [] work; |
|
1194 |
|
1195 r.resize (m, n, 0.0); |
|
1196 for (int j = 0; j < n; j++) |
|
1197 { |
|
1198 int limit = j < min_mn-1 ? j : min_mn-1; |
|
1199 for (int i = 0; i <= limit; i++) |
|
1200 r.elem (i, j) = tmp_data[m*j+i]; |
|
1201 } |
|
1202 |
|
1203 lwork = 32*m; |
|
1204 work = new double[lwork]; |
|
1205 |
|
1206 F77_FCN (dorgqr) (&m, &m, &min_mn, tmp_data, &m, tau, work, &lwork, &info); |
|
1207 |
|
1208 q = Matrix (tmp_data, m, m); |
|
1209 |
|
1210 delete [] tau; |
|
1211 delete [] work; |
|
1212 } |
|
1213 |
|
1214 ComplexQR::ComplexQR (const ComplexMatrix& a) |
|
1215 { |
238
|
1216 int m = a.rows (); |
|
1217 int n = a.cols (); |
3
|
1218 |
|
1219 if (m == 0 || n == 0) |
227
|
1220 { |
|
1221 (*current_liboctave_error_handler) |
|
1222 ("ComplexQR must have non-empty matrix"); |
|
1223 return; |
|
1224 } |
3
|
1225 |
|
1226 Complex *tmp_data; |
|
1227 int min_mn = m < n ? m : n; |
|
1228 Complex *tau = new Complex[min_mn]; |
|
1229 int lwork = 32*n; |
|
1230 Complex *work = new Complex[lwork]; |
|
1231 int info = 0; |
|
1232 |
|
1233 if (m > n) |
|
1234 { |
|
1235 tmp_data = new Complex [m*m]; |
238
|
1236 copy (tmp_data, a.data (), a.length ()); |
3
|
1237 } |
|
1238 else |
238
|
1239 tmp_data = dup (a.data (), a.length ()); |
3
|
1240 |
|
1241 F77_FCN (zgeqrf) (&m, &n, tmp_data, &m, tau, work, &lwork, &info); |
|
1242 |
|
1243 delete [] work; |
|
1244 |
|
1245 r.resize (m, n, 0.0); |
|
1246 for (int j = 0; j < n; j++) |
|
1247 { |
|
1248 int limit = j < min_mn-1 ? j : min_mn-1; |
|
1249 for (int i = 0; i <= limit; i++) |
|
1250 r.elem (i, j) = tmp_data[m*j+i]; |
|
1251 } |
|
1252 |
|
1253 lwork = 32*m; |
|
1254 work = new Complex[lwork]; |
|
1255 |
|
1256 F77_FCN (zungqr) (&m, &m, &min_mn, tmp_data, &m, tau, work, &lwork, &info); |
|
1257 |
|
1258 q = ComplexMatrix (tmp_data, m, m); |
|
1259 |
|
1260 delete [] tau; |
|
1261 delete [] work; |
|
1262 } |
|
1263 |
|
1264 /* |
|
1265 ;;; Local Variables: *** |
|
1266 ;;; mode: C++ *** |
|
1267 ;;; page-delimiter: "^/\\*" *** |
|
1268 ;;; End: *** |
|
1269 */ |