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 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #include "Matrix.h" |
|
29 #include "mx-inlines.cc" |
|
30 |
|
31 /* |
22
|
32 * AEPBALANCE operations |
|
33 */ |
|
34 |
|
35 int |
|
36 AEPBALANCE::init (const Matrix& a, const char *balance_job) |
|
37 { |
|
38 if (a.nr != a.nc) |
|
39 FAIL; |
|
40 |
|
41 int n = a.nc; |
|
42 |
|
43 // Parameters for balance call. |
|
44 |
|
45 int info; |
|
46 int ilo; |
|
47 int ihi; |
|
48 double *scale = new double [n]; |
|
49 |
|
50 // Copy matrix into local structure. |
|
51 |
|
52 balanced_mat = a; |
|
53 |
|
54 F77_FCN (dgebal) (balance_job, &n, balanced_mat.fortran_vec (), |
|
55 &n, &ilo, &ihi, scale, &info, 1L, 1L); |
|
56 |
|
57 // Initialize balancing matrix to identity. |
|
58 |
|
59 balancing_mat = Matrix (n, n, 0.0); |
|
60 for (int i = 0; i < n; i++) |
|
61 balancing_mat.elem (i ,i) = 1.0; |
|
62 |
|
63 F77_FCN (dgebak) (balance_job, "R", &n, &ilo, &ihi, scale, &n, |
|
64 balancing_mat.fortran_vec (), &n, &info, 1L, 1L); |
|
65 |
|
66 delete [] scale; |
|
67 |
|
68 return info; |
|
69 } |
|
70 |
|
71 int |
|
72 ComplexAEPBALANCE::init (const ComplexMatrix& a, const char *balance_job) |
|
73 { |
|
74 |
|
75 int n = a.nc; |
|
76 |
|
77 // Parameters for balance call. |
|
78 |
|
79 int info; |
|
80 int ilo; |
|
81 int ihi; |
|
82 double *scale = new double [n]; |
|
83 |
|
84 // Copy matrix into local structure. |
|
85 |
|
86 balanced_mat = a; |
|
87 |
|
88 F77_FCN (zgebal) (balance_job, &n, balanced_mat.fortran_vec (), |
|
89 &n, &ilo, &ihi, scale, &info, 1L, 1L); |
|
90 |
|
91 // Initialize balancing matrix to identity. |
|
92 |
|
93 balancing_mat = Matrix (n, n, 0.0); |
|
94 for (int i = 0; i < n; i++) |
|
95 balancing_mat (i, i) = 1.0; |
|
96 |
|
97 F77_FCN (zgebak) (balance_job, "R", &n, &ilo, &ihi, scale, &n, |
|
98 balancing_mat.fortran_vec(), &n, &info, 1L, 1L); |
|
99 |
|
100 delete [] scale; |
|
101 |
|
102 return info; |
|
103 } |
|
104 |
|
105 /* |
|
106 * GEPBALANCE operations |
|
107 */ |
|
108 |
|
109 int |
|
110 GEPBALANCE::init (const Matrix& a, const Matrix& b, const char *balance_job) |
|
111 { |
|
112 if (a.nr != a.nc || a.nr != b.nr || b.nr != b.nc) |
|
113 FAIL; |
|
114 |
|
115 int n = a.nc; |
|
116 |
|
117 // Parameters for balance call. |
|
118 |
|
119 int info; |
|
120 int ilo; |
|
121 int ihi; |
|
122 double *cscale = new double [n]; |
|
123 double *cperm = new double [n]; |
|
124 Matrix wk (n, 6, 0.0); |
|
125 |
|
126 // Back out the permutations: |
|
127 // |
|
128 // cscale contains the exponents of the column scaling factors in its |
|
129 // ilo through ihi locations and the reducing column permutations in |
|
130 // its first ilo-1 and its ihi+1 through n locations. |
|
131 // |
|
132 // cperm contains the column permutations applied in grading the a and b |
|
133 // submatrices in its ilo through ihi locations. |
|
134 // |
|
135 // wk contains the exponents of the row scaling factors in its ilo |
|
136 // through ihi locations, the reducing row permutations in its first |
|
137 // ilo-1 and its ihi+1 through n locations, and the row permutations |
|
138 // applied in grading the a and b submatrices in its n+ilo through |
|
139 // n+ihi locations. |
|
140 |
|
141 // Copy matrices into local structure. |
|
142 |
|
143 balanced_a_mat = a; |
|
144 balanced_b_mat = b; |
|
145 |
|
146 // Initialize balancing matrices to identity. |
|
147 |
|
148 left_balancing_mat = Matrix(n,n,0.0); |
|
149 for (int i = 0; i < n; i++) |
|
150 left_balancing_mat (i, i) = 1.0; |
|
151 |
|
152 right_balancing_mat = left_balancing_mat; |
|
153 |
|
154 // Check for permutation option. |
|
155 |
|
156 if (*balance_job == 'P' || *balance_job == 'B') |
|
157 { |
|
158 F77_FCN(reduce)(&n, &n, balanced_a_mat.fortran_vec (), |
|
159 &n, balanced_b_mat.fortran_vec (), &ilo, &ihi, |
|
160 cscale, wk.fortran_vec ()); |
|
161 } |
|
162 else |
|
163 { |
|
164 |
|
165 // Set up for scaling later. |
|
166 |
|
167 ilo = 1; |
|
168 ihi = n; |
|
169 } |
|
170 |
|
171 // Check for scaling option. |
|
172 |
|
173 if ((*balance_job == 'S' || *balance_job == 'B') && ilo != ihi) |
|
174 { |
|
175 F77_FCN(scaleg)(&n, &n, balanced_a_mat.fortran_vec (), |
|
176 &n, balanced_b_mat.fortran_vec (), &ilo, &ihi, |
|
177 cscale, cperm, wk.fortran_vec ()); |
|
178 } |
|
179 else |
|
180 { |
|
181 |
|
182 // Set scaling data to 0's. |
|
183 |
|
184 for (int tmp = ilo-1; tmp < ihi; tmp++) |
|
185 { |
|
186 cscale[tmp] = 0.0; |
|
187 wk.elem(tmp,0) = 0.0; |
|
188 } |
|
189 } |
|
190 |
|
191 // Scaleg returns exponents, not values, so... |
|
192 |
|
193 for (int tmp = ilo-1; tmp < ihi; tmp++) |
|
194 { |
|
195 cscale[tmp] = pow(2.0,cscale[tmp]); |
|
196 wk.elem(tmp,0) = pow(2.0,-wk.elem(tmp,0)); |
|
197 } |
|
198 |
|
199 // Column permutations/scaling. |
|
200 |
|
201 F77_FCN (dgebak) (balance_job, "R", &n, &ilo, &ihi, cscale, &n, |
|
202 right_balancing_mat.fortran_vec (), &n, &info, 1L, |
|
203 1L); |
|
204 |
|
205 // Row permutations/scaling. |
|
206 |
|
207 F77_FCN (dgebak) (balance_job, "L", &n, &ilo, &ihi, &wk.elem (0, 0), &n, |
|
208 left_balancing_mat.fortran_vec (), &n, &info, 1L, 1L); |
|
209 |
|
210 // XXX FIXME XXX --- these four lines need to be added and debugged. |
|
211 // GEPBALANCE::init will work without them, though, so here they are. |
|
212 |
|
213 #if 0 |
|
214 if ((*balance_job == 'P' || *balance_job == 'B') && ilo != ihi) |
|
215 { |
|
216 F77_FCN (gradeq) (&n, &n, balanced_a_mat.fortran_vec (), |
|
217 &n, balanced_b_mat.fortran_vec (), &ilo, &ihi, |
|
218 cperm, &wk.elem (0, 1)); |
|
219 } |
|
220 #endif |
|
221 |
|
222 // Transpose for aa = cc*a*dd convention... |
|
223 left_balancing_mat = left_balancing_mat.transpose (); |
|
224 |
|
225 delete [] cscale; |
|
226 delete [] cperm; |
|
227 |
|
228 return info; |
|
229 } |
|
230 |
|
231 /* |
3
|
232 * HESS stuff |
|
233 */ |
|
234 |
|
235 int |
|
236 HESS::init (const Matrix& a) |
|
237 { |
|
238 if (a.nr != a.nc) |
|
239 FAIL; |
|
240 |
22
|
241 char jobbal = 'N'; |
3
|
242 char side = 'R'; |
|
243 |
|
244 int n = a.nc; |
|
245 int lwork = 32 * n; |
|
246 int info; |
|
247 int ilo; |
|
248 int ihi; |
|
249 |
|
250 double *h = dup(a.data, a.len); |
|
251 |
|
252 double *tau = new double [n+1]; |
|
253 double *scale = new double [n]; |
|
254 double *z = new double [n*n]; |
|
255 double *work = new double [lwork]; |
|
256 |
|
257 F77_FCN (dgebal) (&jobbal, &n, h, &n, &ilo, &ihi, scale, &info, |
|
258 1L, 1L); |
|
259 |
|
260 F77_FCN (dgehrd) (&n, &ilo, &ihi, h, &n, tau, work, &lwork, &info, |
|
261 1L, 1L); |
|
262 |
|
263 copy(z,h,n*n); |
|
264 |
|
265 F77_FCN (dorghr) (&n, &ilo, &ihi, z, &n, tau, work, &lwork, &info, |
|
266 1L, 1L); |
|
267 |
|
268 F77_FCN (dgebak) (&jobbal, &side, &n, &ilo, &ihi, scale, &n, z, &n, |
|
269 &info, 1L, 1L); |
|
270 |
|
271 // We need to clear out all of the area below the sub-diagonal which was used |
|
272 // to store the unitary matrix. |
|
273 |
|
274 hess_mat = Matrix(h,n,n); |
|
275 unitary_hess_mat = Matrix(z,n,n); |
|
276 |
|
277 // If someone thinks of a more graceful way of doing this (or faster for |
|
278 // that matter :-)), please let me know! |
|
279 |
|
280 if (n > 2) |
|
281 for (int j = 0; j < a.nc; j++) |
|
282 for (int i = j+2; i < a.nr; i++) |
|
283 hess_mat.elem(i,j) = 0; |
|
284 |
|
285 delete [] tau; |
|
286 delete [] work; |
|
287 delete [] scale; |
|
288 |
|
289 return info; |
|
290 } |
|
291 |
|
292 |
|
293 int |
|
294 ComplexHESS::init (const ComplexMatrix& a) |
|
295 { |
|
296 if (a.nr != a.nc) |
|
297 FAIL; |
|
298 |
22
|
299 char job = 'N'; |
3
|
300 char side = 'R'; |
|
301 |
|
302 int n = a.nc; |
|
303 int lwork = 32 * n; |
|
304 int info; |
|
305 int ilo; |
|
306 int ihi; |
|
307 |
|
308 Complex *h = dup(a.data,a.len); |
|
309 |
|
310 double *scale = new double [n]; |
|
311 Complex *tau = new Complex [n-1]; |
|
312 Complex *work = new Complex [lwork]; |
|
313 Complex *z = new Complex [n*n]; |
|
314 |
|
315 F77_FCN (zgebal) (&job, &n, h, &n, &ilo, &ihi, scale, &info, 1L, 1L); |
|
316 |
|
317 F77_FCN (zgehrd) (&n, &ilo, &ihi, h, &n, tau, work, &lwork, &info, 1L, |
|
318 1L); |
|
319 |
|
320 copy(z,h,n*n); |
|
321 |
|
322 F77_FCN (zunghr) (&n, &ilo, &ihi, z, &n, tau, work, &lwork, &info, 1L, |
|
323 1L); |
|
324 |
|
325 F77_FCN (zgebak) (&job, &side, &n, &ilo, &ihi, scale, &n, z, &n, &info, |
|
326 1L, 1L); |
|
327 |
|
328 hess_mat = ComplexMatrix (h,n,n); |
|
329 unitary_hess_mat = ComplexMatrix (z,n,n); |
|
330 |
|
331 // If someone thinks of a more graceful way of doing this (or faster for |
|
332 // that matter :-)), please let me know! |
|
333 |
|
334 if (n > 2) |
|
335 for (int j = 0; j < a.nc; j++) |
|
336 for (int i = j+2; i < a.nr; i++) |
|
337 hess_mat.elem(i,j) = 0; |
|
338 |
|
339 delete [] work; |
|
340 delete [] tau; |
|
341 delete [] scale; |
|
342 |
|
343 return info; |
|
344 } |
|
345 |
|
346 /* |
|
347 * SCHUR stuff |
|
348 */ |
|
349 |
|
350 static int |
|
351 select_ana (double *a, double *b) |
|
352 { |
|
353 return (*a < 0.0); |
|
354 } |
|
355 |
|
356 static int |
|
357 select_dig (double *a, double *b) |
|
358 { |
|
359 return (hypot (*a, *b) < 1.0); |
|
360 } |
|
361 |
|
362 // GAG. |
|
363 extern "C" { static int (*dummy_select)(); } |
|
364 |
|
365 int |
|
366 SCHUR::init (const Matrix& a, const char *ord) |
|
367 { |
|
368 if (a.nr != a.nc) |
|
369 FAIL; |
|
370 |
|
371 char jobvs = 'V'; |
|
372 char sort; |
|
373 |
|
374 if (*ord == 'A' || *ord == 'D' || *ord == 'a' || *ord == 'd') |
|
375 sort = 'S'; |
|
376 else |
|
377 sort = 'N'; |
|
378 |
|
379 char sense = 'N'; |
|
380 |
|
381 int n = a.nc; |
|
382 int lwork = 8 * n; |
|
383 int liwork = 1; |
|
384 int info; |
|
385 int sdim; |
|
386 double rconde; |
|
387 double rcondv; |
|
388 |
|
389 double *s = dup(a.data,a.len); |
|
390 |
|
391 double *wr = new double [n]; |
|
392 double *wi = new double [n]; |
|
393 double *q = new double [n*n]; |
|
394 double *work = new double [lwork]; |
|
395 |
|
396 // These are not referenced for the non-ordered Schur routine. |
|
397 |
|
398 int *iwork = (int *) NULL; |
|
399 int *bwork = (int *) NULL; |
|
400 if (*ord == 'A' || *ord == 'D' || *ord == 'a' || *ord == 'd') |
|
401 { |
|
402 iwork = new int [liwork]; |
|
403 bwork = new int [n]; |
|
404 } |
|
405 |
|
406 if (*ord == 'A' || *ord == 'a') |
|
407 { |
|
408 F77_FCN (dgeesx) (&jobvs, &sort, select_ana, &sense, &n, s, &n, |
|
409 &sdim, wr, wi, q, &n, &rconde, &rcondv, work, |
|
410 &lwork, iwork, &liwork, bwork, &info, 1L, 1L); |
|
411 } |
|
412 else if (*ord == 'D' || *ord == 'd') |
|
413 { |
|
414 F77_FCN (dgeesx) (&jobvs, &sort, select_dig, &sense, &n, s, &n, |
|
415 &sdim, wr, wi, q, &n, &rconde, &rcondv, work, |
|
416 &lwork, iwork, &liwork, bwork, &info, 1L, 1L); |
|
417 |
|
418 } |
|
419 else |
|
420 { |
|
421 F77_FCN (dgeesx) (&jobvs, &sort, dummy_select, &sense, &n, s, |
|
422 &n, &sdim, wr, wi, q, &n, &rconde, &rcondv, |
|
423 work, &lwork, iwork, &liwork, bwork, &info, |
|
424 1L, 1L); |
|
425 } |
|
426 |
|
427 |
|
428 schur_mat = Matrix (s, n, n); |
|
429 unitary_mat = Matrix (q, n, n); |
|
430 |
|
431 delete [] wr; |
|
432 delete [] wi; |
|
433 delete [] work; |
|
434 delete [] iwork; |
|
435 delete [] bwork; |
|
436 |
|
437 return info; |
|
438 } |
|
439 |
|
440 static int |
|
441 complex_select_ana (Complex *a) |
|
442 { |
|
443 return (real (*a) < 0.0); |
|
444 } |
|
445 |
|
446 static int |
|
447 complex_select_dig (Complex *a) |
|
448 { |
|
449 return (abs (*a) < 1.0); |
|
450 } |
|
451 |
|
452 int |
|
453 ComplexSCHUR::init (const ComplexMatrix& a, const char *ord) |
|
454 { |
|
455 if (a.nr != a.nc) |
|
456 FAIL; |
|
457 |
|
458 char jobvs = 'V'; |
|
459 char sort; |
|
460 if (*ord == 'A' || *ord == 'D' || *ord == 'a' || *ord == 'd') |
|
461 sort = 'S'; |
|
462 else |
|
463 sort = 'N'; |
|
464 |
|
465 char sense = 'N'; |
|
466 |
|
467 int n = a.nc; |
|
468 int lwork = 8 * n; |
|
469 int info; |
|
470 int sdim; |
|
471 double rconde; |
|
472 double rcondv; |
|
473 |
|
474 double *rwork = new double [n]; |
|
475 |
|
476 // bwork is not referenced for non-ordered Schur. |
|
477 |
|
478 int *bwork = (int *) NULL; |
|
479 if (*ord == 'A' || *ord == 'D' || *ord == 'a' || *ord == 'd') |
|
480 bwork = new int [n]; |
|
481 |
|
482 Complex *s = dup(a.data,a.len); |
|
483 |
|
484 Complex *work = new Complex [lwork]; |
|
485 Complex *q = new Complex [n*n]; |
|
486 Complex *w = new Complex [n]; |
|
487 |
|
488 if (*ord == 'A' || *ord == 'a') |
|
489 { |
|
490 F77_FCN (zgeesx) (&jobvs, &sort, complex_select_ana, &sense, |
|
491 &n, s, &n, &sdim, w, q, &n, &rconde, &rcondv, |
|
492 work, &lwork, rwork, bwork, &info, 1L, 1L); |
|
493 } |
|
494 else if (*ord == 'D' || *ord == 'd') |
|
495 { |
|
496 F77_FCN (zgeesx) (&jobvs, &sort, complex_select_dig, &sense, |
|
497 &n, s, &n, &sdim, w, q, &n, &rconde, &rcondv, |
|
498 work, &lwork, rwork, bwork, &info, 1L, 1L); |
|
499 } |
|
500 else |
|
501 { |
|
502 F77_FCN (zgeesx) (&jobvs, &sort, dummy_select, &sense, &n, s, |
|
503 &n, &sdim, w, q, &n, &rconde, &rcondv, work, |
|
504 &lwork, rwork, bwork, &info, 1L, 1L); |
|
505 } |
|
506 |
|
507 schur_mat = ComplexMatrix (s,n,n); |
|
508 unitary_mat = ComplexMatrix (q,n,n); |
|
509 |
|
510 delete [] w; |
|
511 delete [] work; |
|
512 delete [] rwork; |
|
513 delete [] bwork; |
|
514 |
|
515 return info; |
|
516 } |
|
517 |
|
518 ostream& |
|
519 operator << (ostream& os, const SCHUR& a) |
|
520 { |
|
521 os << a.schur_matrix () << "\n"; |
|
522 os << a.unitary_matrix () << "\n"; |
|
523 |
|
524 return os; |
|
525 } |
|
526 |
|
527 /* |
|
528 * SVD stuff |
|
529 */ |
|
530 |
|
531 int |
|
532 SVD::init (const Matrix& a) |
|
533 { |
|
534 int info; |
|
535 |
|
536 int m = a.nr; |
|
537 int n = a.nc; |
|
538 |
|
539 char jobu = 'A'; |
|
540 char jobv = 'A'; |
|
541 |
|
542 double *tmp_data = dup (a.data, a.len); |
|
543 |
|
544 int min_mn = m < n ? m : n; |
|
545 int max_mn = m > n ? m : n; |
|
546 |
|
547 double *u = new double[m*m]; |
|
548 double *s_vec = new double[min_mn]; |
|
549 double *vt = new double[n*n]; |
|
550 |
|
551 int tmp1 = 3*min_mn + max_mn; |
|
552 int tmp2 = 5*min_mn - 4; |
|
553 int lwork = tmp1 > tmp2 ? tmp1 : tmp2; |
|
554 double *work = new double[lwork]; |
|
555 |
|
556 F77_FCN (dgesvd) (&jobu, &jobv, &m, &n, tmp_data, &m, s_vec, u, &m, |
|
557 vt, &n, work, &lwork, &info, 1L, 1L); |
|
558 |
|
559 left_sm = Matrix (u, m, m); |
|
560 sigma = DiagMatrix (s_vec, m, n); |
|
561 Matrix vt_m (vt, n, n); |
|
562 right_sm = Matrix (vt_m.transpose ()); |
|
563 |
|
564 delete [] tmp_data; |
|
565 delete [] work; |
|
566 |
|
567 return info; |
|
568 } |
|
569 |
|
570 ostream& |
|
571 operator << (ostream& os, const SVD& a) |
|
572 { |
|
573 os << a.left_singular_matrix () << "\n"; |
|
574 os << a.singular_values () << "\n"; |
|
575 os << a.right_singular_matrix () << "\n"; |
|
576 |
|
577 return os; |
|
578 } |
|
579 |
|
580 int |
|
581 ComplexSVD::init (const ComplexMatrix& a) |
|
582 { |
|
583 int info; |
|
584 |
|
585 int m = a.nr; |
|
586 int n = a.nc; |
|
587 |
|
588 char jobu = 'A'; |
|
589 char jobv = 'A'; |
|
590 |
|
591 Complex *tmp_data = dup (a.data, a.len); |
|
592 |
|
593 int min_mn = m < n ? m : n; |
|
594 int max_mn = m > n ? m : n; |
|
595 |
|
596 Complex *u = new Complex[m*m]; |
|
597 double *s_vec = new double[min_mn]; |
|
598 Complex *vt = new Complex[n*n]; |
|
599 |
|
600 int lwork = 2*min_mn + max_mn; |
|
601 Complex *work = new Complex[lwork]; |
|
602 |
|
603 int lrwork = 5*max_mn; |
|
604 double *rwork = new double[lrwork]; |
|
605 |
|
606 F77_FCN (zgesvd) (&jobu, &jobv, &m, &n, tmp_data, &m, s_vec, u, &m, |
|
607 vt, &n, work, &lwork, rwork, &info, 1L, 1L); |
|
608 |
|
609 left_sm = ComplexMatrix (u, m, m); |
|
610 sigma = DiagMatrix (s_vec, m, n); |
|
611 ComplexMatrix vt_m (vt, n, n); |
|
612 right_sm = ComplexMatrix (vt_m.hermitian ()); |
|
613 |
|
614 delete [] tmp_data; |
|
615 delete [] work; |
|
616 |
|
617 return info; |
|
618 } |
|
619 |
|
620 /* |
|
621 * EIG stuff. |
|
622 */ |
|
623 |
|
624 int |
|
625 EIG::init (const Matrix& a) |
|
626 { |
|
627 if (a.nr != a.nc) |
|
628 FAIL; |
|
629 |
|
630 int n = a.nr; |
|
631 |
|
632 int info; |
|
633 |
|
634 char jobvl = 'N'; |
|
635 char jobvr = 'V'; |
|
636 |
|
637 double *tmp_data = dup (a.data, a.len); |
|
638 double *wr = new double[n]; |
|
639 double *wi = new double[n]; |
|
640 Matrix vr (n, n); |
|
641 double *pvr = vr.fortran_vec (); |
|
642 int lwork = 8*n; |
|
643 double *work = new double[lwork]; |
|
644 |
|
645 double dummy; |
|
646 int idummy = 1; |
|
647 |
|
648 F77_FCN (dgeev) (&jobvl, &jobvr, &n, tmp_data, &n, wr, wi, &dummy, |
|
649 &idummy, pvr, &n, work, &lwork, &info, 1L, 1L); |
|
650 |
|
651 lambda.resize (n); |
|
652 v.resize (n, n); |
|
653 |
|
654 for (int j = 0; j < n; j++) |
|
655 { |
|
656 if (wi[j] == 0.0) |
|
657 { |
|
658 lambda.elem (j) = Complex (wr[j]); |
|
659 for (int i = 0; i < n; i++) |
|
660 v.elem (i, j) = vr.elem (i, j); |
|
661 } |
|
662 else |
|
663 { |
|
664 if (j+1 >= n) |
|
665 FAIL; |
|
666 |
|
667 for (int i = 0; i < n; i++) |
|
668 { |
|
669 lambda.elem (j) = Complex (wr[j], wi[j]); |
|
670 lambda.elem (j+1) = Complex (wr[j+1], wi[j+1]); |
|
671 double real_part = vr.elem (i, j); |
|
672 double imag_part = vr.elem (i, j+1); |
|
673 v.elem (i, j) = Complex (real_part, imag_part); |
|
674 v.elem (i, j+1) = Complex (real_part, -imag_part); |
|
675 } |
|
676 j++; |
|
677 } |
|
678 } |
|
679 |
|
680 delete [] tmp_data; |
|
681 delete [] wr; |
|
682 delete [] wi; |
|
683 delete [] work; |
|
684 |
|
685 return info; |
|
686 } |
|
687 |
|
688 int |
|
689 EIG::init (const ComplexMatrix& a) |
|
690 { |
|
691 |
|
692 if (a.nr != a.nc) |
|
693 FAIL; |
|
694 |
|
695 int n = a.nr; |
|
696 |
|
697 int info; |
|
698 |
|
699 char jobvl = 'N'; |
|
700 char jobvr = 'V'; |
|
701 |
|
702 lambda.resize (n); |
|
703 v.resize (n, n); |
|
704 |
|
705 Complex *pw = lambda.fortran_vec (); |
|
706 Complex *pvr = v.fortran_vec (); |
|
707 |
|
708 Complex *tmp_data = dup (a.data, a.len); |
|
709 |
|
710 int lwork = 8*n; |
|
711 Complex *work = new Complex[lwork]; |
|
712 double *rwork = new double[4*n]; |
|
713 |
|
714 Complex dummy; |
|
715 int idummy = 1; |
|
716 |
|
717 F77_FCN (zgeev) (&jobvl, &jobvr, &n, tmp_data, &n, pw, &dummy, |
|
718 &idummy, pvr, &n, work, &lwork, rwork, &info, 1L, |
|
719 1L); |
|
720 |
|
721 delete [] tmp_data; |
|
722 delete [] work; |
|
723 delete [] rwork; |
|
724 |
|
725 return info; |
|
726 } |
|
727 |
|
728 /* |
|
729 * LU stuff. |
|
730 */ |
|
731 |
|
732 LU::LU (const Matrix& a) |
|
733 { |
|
734 if (a.nr == 0 || a.nc == 0 || a.nr != a.nc) |
|
735 FAIL; |
|
736 |
|
737 int n = a.nr; |
|
738 |
|
739 int *ipvt = new int [n]; |
|
740 int *pvt = new int [n]; |
|
741 double *tmp_data = dup (a.data, a.len); |
|
742 int info = 0; |
|
743 int zero = 0; |
|
744 double b; |
|
745 |
|
746 F77_FCN (dgesv) (&n, &zero, tmp_data, &n, ipvt, &b, &n, &info); |
|
747 |
|
748 Matrix A_fact (tmp_data, n, n); |
|
749 |
|
750 int i; |
|
751 |
|
752 for (i = 0; i < n; i++) |
|
753 { |
|
754 ipvt[i] -= 1; |
|
755 pvt[i] = i; |
|
756 } |
|
757 |
|
758 for (i = 0; i < n - 1; i++) |
|
759 { |
|
760 int k = ipvt[i]; |
|
761 if (k != i) |
|
762 { |
|
763 int tmp = pvt[k]; |
|
764 pvt[k] = pvt[i]; |
|
765 pvt[i] = tmp; |
|
766 } |
|
767 } |
|
768 |
|
769 l.resize (n, n, 0.0); |
|
770 u.resize (n, n, 0.0); |
|
771 p.resize (n, n, 0.0); |
|
772 |
|
773 for (i = 0; i < n; i++) |
|
774 { |
|
775 p.elem (i, pvt[i]) = 1.0; |
|
776 |
|
777 int j; |
|
778 |
|
779 l.elem (i, i) = 1.0; |
|
780 for (j = 0; j < i; j++) |
|
781 l.elem (i, j) = A_fact.elem (i, j); |
|
782 |
|
783 for (j = i; j < n; j++) |
|
784 u.elem (i, j) = A_fact.elem (i, j); |
|
785 } |
|
786 |
|
787 delete [] ipvt; |
|
788 delete [] pvt; |
|
789 } |
|
790 |
|
791 ComplexLU::ComplexLU (const ComplexMatrix& a) |
|
792 { |
|
793 if (a.nr == 0 || a.nc == 0 || a.nr != a.nc) |
|
794 FAIL; |
|
795 |
|
796 int n = a.nr; |
|
797 |
|
798 int *ipvt = new int [n]; |
|
799 int *pvt = new int [n]; |
|
800 Complex *tmp_data = dup (a.data, a.len); |
|
801 int info = 0; |
|
802 int zero = 0; |
|
803 Complex b; |
|
804 |
|
805 F77_FCN (zgesv) (&n, &zero, tmp_data, &n, ipvt, &b, &n, &info); |
|
806 |
|
807 ComplexMatrix A_fact (tmp_data, n, n); |
|
808 |
|
809 int i; |
|
810 |
|
811 for (i = 0; i < n; i++) |
|
812 { |
|
813 ipvt[i] -= 1; |
|
814 pvt[i] = i; |
|
815 } |
|
816 |
|
817 for (i = 0; i < n - 1; i++) |
|
818 { |
|
819 int k = ipvt[i]; |
|
820 if (k != i) |
|
821 { |
|
822 int tmp = pvt[k]; |
|
823 pvt[k] = pvt[i]; |
|
824 pvt[i] = tmp; |
|
825 } |
|
826 } |
|
827 |
|
828 l.resize (n, n, 0.0); |
|
829 u.resize (n, n, 0.0); |
|
830 p.resize (n, n, 0.0); |
|
831 |
|
832 for (i = 0; i < n; i++) |
|
833 { |
|
834 p.elem (i, pvt[i]) = 1.0; |
|
835 |
|
836 int j; |
|
837 |
|
838 l.elem (i, i) = 1.0; |
|
839 for (j = 0; j < i; j++) |
|
840 l.elem (i, j) = A_fact.elem (i, j); |
|
841 |
|
842 for (j = i; j < n; j++) |
|
843 u.elem (i, j) = A_fact.elem (i, j); |
|
844 } |
|
845 |
|
846 delete [] ipvt; |
|
847 delete [] pvt; |
|
848 } |
|
849 |
|
850 /* |
|
851 * QR stuff. |
|
852 */ |
|
853 |
|
854 QR::QR (const Matrix& a) |
|
855 { |
|
856 int m = a.nr; |
|
857 int n = a.nc; |
|
858 |
|
859 if (m == 0 || n == 0) |
|
860 FAIL; |
|
861 |
|
862 double *tmp_data; |
|
863 int min_mn = m < n ? m : n; |
|
864 double *tau = new double[min_mn]; |
|
865 int lwork = 32*n; |
|
866 double *work = new double[lwork]; |
|
867 int info = 0; |
|
868 |
|
869 if (m > n) |
|
870 { |
|
871 tmp_data = new double [m*m]; |
|
872 copy (tmp_data, a.data, a.len); |
|
873 } |
|
874 else |
|
875 tmp_data = dup (a.data, a.len); |
|
876 |
|
877 F77_FCN (dgeqrf) (&m, &n, tmp_data, &m, tau, work, &lwork, &info); |
|
878 |
|
879 delete [] work; |
|
880 |
|
881 r.resize (m, n, 0.0); |
|
882 for (int j = 0; j < n; j++) |
|
883 { |
|
884 int limit = j < min_mn-1 ? j : min_mn-1; |
|
885 for (int i = 0; i <= limit; i++) |
|
886 r.elem (i, j) = tmp_data[m*j+i]; |
|
887 } |
|
888 |
|
889 lwork = 32*m; |
|
890 work = new double[lwork]; |
|
891 |
|
892 F77_FCN (dorgqr) (&m, &m, &min_mn, tmp_data, &m, tau, work, &lwork, &info); |
|
893 |
|
894 q = Matrix (tmp_data, m, m); |
|
895 |
|
896 delete [] tau; |
|
897 delete [] work; |
|
898 } |
|
899 |
|
900 ComplexQR::ComplexQR (const ComplexMatrix& a) |
|
901 { |
|
902 int m = a.nr; |
|
903 int n = a.nc; |
|
904 |
|
905 if (m == 0 || n == 0) |
|
906 FAIL; |
|
907 |
|
908 Complex *tmp_data; |
|
909 int min_mn = m < n ? m : n; |
|
910 Complex *tau = new Complex[min_mn]; |
|
911 int lwork = 32*n; |
|
912 Complex *work = new Complex[lwork]; |
|
913 int info = 0; |
|
914 |
|
915 if (m > n) |
|
916 { |
|
917 tmp_data = new Complex [m*m]; |
|
918 copy (tmp_data, a.data, a.len); |
|
919 } |
|
920 else |
|
921 tmp_data = dup (a.data, a.len); |
|
922 |
|
923 F77_FCN (zgeqrf) (&m, &n, tmp_data, &m, tau, work, &lwork, &info); |
|
924 |
|
925 delete [] work; |
|
926 |
|
927 r.resize (m, n, 0.0); |
|
928 for (int j = 0; j < n; j++) |
|
929 { |
|
930 int limit = j < min_mn-1 ? j : min_mn-1; |
|
931 for (int i = 0; i <= limit; i++) |
|
932 r.elem (i, j) = tmp_data[m*j+i]; |
|
933 } |
|
934 |
|
935 lwork = 32*m; |
|
936 work = new Complex[lwork]; |
|
937 |
|
938 F77_FCN (zungqr) (&m, &m, &min_mn, tmp_data, &m, tau, work, &lwork, &info); |
|
939 |
|
940 q = ComplexMatrix (tmp_data, m, m); |
|
941 |
|
942 delete [] tau; |
|
943 delete [] work; |
|
944 } |
|
945 |
|
946 /* |
|
947 ;;; Local Variables: *** |
|
948 ;;; mode: C++ *** |
|
949 ;;; page-delimiter: "^/\\*" *** |
|
950 ;;; End: *** |
|
951 */ |