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