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 /* |
182
|
232 * CHOL stuff |
|
233 */ |
|
234 |
|
235 int |
|
236 CHOL::init (const Matrix& a) |
|
237 { |
|
238 if (a.nr != a.nc) |
|
239 FAIL; |
|
240 |
|
241 char uplo = 'U'; |
|
242 |
|
243 int n = a.nc; |
|
244 int info; |
|
245 |
|
246 double *h = dup (a.data, a.len); |
|
247 |
|
248 F77_FCN (dpotrf) (&uplo, &n, h, &n, &info, 1L); |
|
249 |
|
250 chol_mat = Matrix (h, n, n); |
|
251 |
|
252 // If someone thinks of a more graceful way of doing this (or faster for |
|
253 // that matter :-)), please let me know! |
|
254 |
|
255 if (n > 1) |
|
256 for (int j = 0; j < a.nc; j++) |
|
257 for (int i = j+1; i < a.nr; i++) |
|
258 chol_mat.elem (i, j) = 0.0; |
|
259 |
|
260 |
|
261 return info; |
|
262 } |
|
263 |
|
264 |
|
265 int |
|
266 ComplexCHOL::init (const ComplexMatrix& a) |
|
267 { |
|
268 if (a.nr != a.nc) |
|
269 FAIL; |
|
270 |
|
271 char uplo = 'U'; |
|
272 |
|
273 int n = a.nc; |
|
274 int info; |
|
275 |
|
276 Complex *h = dup (a.data, a.len); |
|
277 |
|
278 F77_FCN (zpotrf) (&uplo, &n, h, &n, &info, 1L); |
|
279 |
|
280 chol_mat = ComplexMatrix (h, n, n); |
|
281 |
|
282 // If someone thinks of a more graceful way of doing this (or faster for |
|
283 // that matter :-)), please let me know! |
|
284 |
|
285 if (n > 1) |
|
286 for (int j = 0; j < a.nc; j++) |
|
287 for (int i = j+1; i < a.nr; i++) |
|
288 chol_mat.elem (i, j) = 0.0; |
|
289 |
|
290 return info; |
|
291 } |
|
292 |
|
293 |
|
294 /* |
3
|
295 * HESS stuff |
|
296 */ |
|
297 |
|
298 int |
|
299 HESS::init (const Matrix& a) |
|
300 { |
|
301 if (a.nr != a.nc) |
|
302 FAIL; |
|
303 |
22
|
304 char jobbal = 'N'; |
3
|
305 char side = 'R'; |
|
306 |
|
307 int n = a.nc; |
|
308 int lwork = 32 * n; |
|
309 int info; |
|
310 int ilo; |
|
311 int ihi; |
|
312 |
|
313 double *h = dup(a.data, a.len); |
|
314 |
|
315 double *tau = new double [n+1]; |
|
316 double *scale = new double [n]; |
|
317 double *z = new double [n*n]; |
|
318 double *work = new double [lwork]; |
|
319 |
|
320 F77_FCN (dgebal) (&jobbal, &n, h, &n, &ilo, &ihi, scale, &info, |
|
321 1L, 1L); |
|
322 |
|
323 F77_FCN (dgehrd) (&n, &ilo, &ihi, h, &n, tau, work, &lwork, &info, |
|
324 1L, 1L); |
|
325 |
|
326 copy(z,h,n*n); |
|
327 |
|
328 F77_FCN (dorghr) (&n, &ilo, &ihi, z, &n, tau, work, &lwork, &info, |
|
329 1L, 1L); |
|
330 |
|
331 F77_FCN (dgebak) (&jobbal, &side, &n, &ilo, &ihi, scale, &n, z, &n, |
|
332 &info, 1L, 1L); |
|
333 |
|
334 // We need to clear out all of the area below the sub-diagonal which was used |
|
335 // to store the unitary matrix. |
|
336 |
|
337 hess_mat = Matrix(h,n,n); |
|
338 unitary_hess_mat = Matrix(z,n,n); |
|
339 |
|
340 // If someone thinks of a more graceful way of doing this (or faster for |
|
341 // that matter :-)), please let me know! |
|
342 |
|
343 if (n > 2) |
|
344 for (int j = 0; j < a.nc; j++) |
|
345 for (int i = j+2; i < a.nr; i++) |
|
346 hess_mat.elem(i,j) = 0; |
|
347 |
|
348 delete [] tau; |
|
349 delete [] work; |
|
350 delete [] scale; |
|
351 |
|
352 return info; |
|
353 } |
|
354 |
|
355 |
|
356 int |
|
357 ComplexHESS::init (const ComplexMatrix& a) |
|
358 { |
|
359 if (a.nr != a.nc) |
|
360 FAIL; |
|
361 |
22
|
362 char job = 'N'; |
3
|
363 char side = 'R'; |
|
364 |
|
365 int n = a.nc; |
|
366 int lwork = 32 * n; |
|
367 int info; |
|
368 int ilo; |
|
369 int ihi; |
|
370 |
|
371 Complex *h = dup(a.data,a.len); |
|
372 |
|
373 double *scale = new double [n]; |
|
374 Complex *tau = new Complex [n-1]; |
|
375 Complex *work = new Complex [lwork]; |
|
376 Complex *z = new Complex [n*n]; |
|
377 |
|
378 F77_FCN (zgebal) (&job, &n, h, &n, &ilo, &ihi, scale, &info, 1L, 1L); |
|
379 |
|
380 F77_FCN (zgehrd) (&n, &ilo, &ihi, h, &n, tau, work, &lwork, &info, 1L, |
|
381 1L); |
|
382 |
|
383 copy(z,h,n*n); |
|
384 |
|
385 F77_FCN (zunghr) (&n, &ilo, &ihi, z, &n, tau, work, &lwork, &info, 1L, |
|
386 1L); |
|
387 |
|
388 F77_FCN (zgebak) (&job, &side, &n, &ilo, &ihi, scale, &n, z, &n, &info, |
|
389 1L, 1L); |
|
390 |
|
391 hess_mat = ComplexMatrix (h,n,n); |
|
392 unitary_hess_mat = ComplexMatrix (z,n,n); |
|
393 |
|
394 // If someone thinks of a more graceful way of doing this (or faster for |
|
395 // that matter :-)), please let me know! |
|
396 |
|
397 if (n > 2) |
|
398 for (int j = 0; j < a.nc; j++) |
|
399 for (int i = j+2; i < a.nr; i++) |
|
400 hess_mat.elem(i,j) = 0; |
|
401 |
|
402 delete [] work; |
|
403 delete [] tau; |
|
404 delete [] scale; |
|
405 |
|
406 return info; |
|
407 } |
|
408 |
|
409 /* |
|
410 * SCHUR stuff |
|
411 */ |
|
412 |
|
413 static int |
|
414 select_ana (double *a, double *b) |
|
415 { |
|
416 return (*a < 0.0); |
|
417 } |
|
418 |
|
419 static int |
|
420 select_dig (double *a, double *b) |
|
421 { |
|
422 return (hypot (*a, *b) < 1.0); |
|
423 } |
|
424 |
|
425 // GAG. |
|
426 extern "C" { static int (*dummy_select)(); } |
|
427 |
|
428 int |
|
429 SCHUR::init (const Matrix& a, const char *ord) |
|
430 { |
|
431 if (a.nr != a.nc) |
|
432 FAIL; |
|
433 |
|
434 char jobvs = 'V'; |
|
435 char sort; |
|
436 |
|
437 if (*ord == 'A' || *ord == 'D' || *ord == 'a' || *ord == 'd') |
|
438 sort = 'S'; |
|
439 else |
|
440 sort = 'N'; |
|
441 |
|
442 char sense = 'N'; |
|
443 |
|
444 int n = a.nc; |
|
445 int lwork = 8 * n; |
|
446 int liwork = 1; |
|
447 int info; |
|
448 int sdim; |
|
449 double rconde; |
|
450 double rcondv; |
|
451 |
|
452 double *s = dup(a.data,a.len); |
|
453 |
|
454 double *wr = new double [n]; |
|
455 double *wi = new double [n]; |
|
456 double *q = new double [n*n]; |
|
457 double *work = new double [lwork]; |
|
458 |
|
459 // These are not referenced for the non-ordered Schur routine. |
|
460 |
|
461 int *iwork = (int *) NULL; |
|
462 int *bwork = (int *) NULL; |
|
463 if (*ord == 'A' || *ord == 'D' || *ord == 'a' || *ord == 'd') |
|
464 { |
|
465 iwork = new int [liwork]; |
|
466 bwork = new int [n]; |
|
467 } |
|
468 |
|
469 if (*ord == 'A' || *ord == 'a') |
|
470 { |
|
471 F77_FCN (dgeesx) (&jobvs, &sort, select_ana, &sense, &n, s, &n, |
|
472 &sdim, wr, wi, q, &n, &rconde, &rcondv, work, |
|
473 &lwork, iwork, &liwork, bwork, &info, 1L, 1L); |
|
474 } |
|
475 else if (*ord == 'D' || *ord == 'd') |
|
476 { |
|
477 F77_FCN (dgeesx) (&jobvs, &sort, select_dig, &sense, &n, s, &n, |
|
478 &sdim, wr, wi, q, &n, &rconde, &rcondv, work, |
|
479 &lwork, iwork, &liwork, bwork, &info, 1L, 1L); |
|
480 |
|
481 } |
|
482 else |
|
483 { |
|
484 F77_FCN (dgeesx) (&jobvs, &sort, dummy_select, &sense, &n, s, |
|
485 &n, &sdim, wr, wi, q, &n, &rconde, &rcondv, |
|
486 work, &lwork, iwork, &liwork, bwork, &info, |
|
487 1L, 1L); |
|
488 } |
|
489 |
|
490 |
|
491 schur_mat = Matrix (s, n, n); |
|
492 unitary_mat = Matrix (q, n, n); |
|
493 |
|
494 delete [] wr; |
|
495 delete [] wi; |
|
496 delete [] work; |
|
497 delete [] iwork; |
|
498 delete [] bwork; |
|
499 |
|
500 return info; |
|
501 } |
|
502 |
|
503 static int |
|
504 complex_select_ana (Complex *a) |
|
505 { |
|
506 return (real (*a) < 0.0); |
|
507 } |
|
508 |
|
509 static int |
|
510 complex_select_dig (Complex *a) |
|
511 { |
|
512 return (abs (*a) < 1.0); |
|
513 } |
|
514 |
|
515 int |
|
516 ComplexSCHUR::init (const ComplexMatrix& a, const char *ord) |
|
517 { |
|
518 if (a.nr != a.nc) |
|
519 FAIL; |
|
520 |
|
521 char jobvs = 'V'; |
|
522 char sort; |
|
523 if (*ord == 'A' || *ord == 'D' || *ord == 'a' || *ord == 'd') |
|
524 sort = 'S'; |
|
525 else |
|
526 sort = 'N'; |
|
527 |
|
528 char sense = 'N'; |
|
529 |
|
530 int n = a.nc; |
|
531 int lwork = 8 * n; |
|
532 int info; |
|
533 int sdim; |
|
534 double rconde; |
|
535 double rcondv; |
|
536 |
|
537 double *rwork = new double [n]; |
|
538 |
|
539 // bwork is not referenced for non-ordered Schur. |
|
540 |
|
541 int *bwork = (int *) NULL; |
|
542 if (*ord == 'A' || *ord == 'D' || *ord == 'a' || *ord == 'd') |
|
543 bwork = new int [n]; |
|
544 |
|
545 Complex *s = dup(a.data,a.len); |
|
546 |
|
547 Complex *work = new Complex [lwork]; |
|
548 Complex *q = new Complex [n*n]; |
|
549 Complex *w = new Complex [n]; |
|
550 |
|
551 if (*ord == 'A' || *ord == 'a') |
|
552 { |
|
553 F77_FCN (zgeesx) (&jobvs, &sort, complex_select_ana, &sense, |
|
554 &n, s, &n, &sdim, w, q, &n, &rconde, &rcondv, |
|
555 work, &lwork, rwork, bwork, &info, 1L, 1L); |
|
556 } |
|
557 else if (*ord == 'D' || *ord == 'd') |
|
558 { |
|
559 F77_FCN (zgeesx) (&jobvs, &sort, complex_select_dig, &sense, |
|
560 &n, s, &n, &sdim, w, q, &n, &rconde, &rcondv, |
|
561 work, &lwork, rwork, bwork, &info, 1L, 1L); |
|
562 } |
|
563 else |
|
564 { |
|
565 F77_FCN (zgeesx) (&jobvs, &sort, dummy_select, &sense, &n, s, |
|
566 &n, &sdim, w, q, &n, &rconde, &rcondv, work, |
|
567 &lwork, rwork, bwork, &info, 1L, 1L); |
|
568 } |
|
569 |
|
570 schur_mat = ComplexMatrix (s,n,n); |
|
571 unitary_mat = ComplexMatrix (q,n,n); |
|
572 |
|
573 delete [] w; |
|
574 delete [] work; |
|
575 delete [] rwork; |
|
576 delete [] bwork; |
|
577 |
|
578 return info; |
|
579 } |
|
580 |
|
581 ostream& |
|
582 operator << (ostream& os, const SCHUR& a) |
|
583 { |
|
584 os << a.schur_matrix () << "\n"; |
|
585 os << a.unitary_matrix () << "\n"; |
|
586 |
|
587 return os; |
|
588 } |
|
589 |
|
590 /* |
|
591 * SVD stuff |
|
592 */ |
|
593 |
|
594 int |
|
595 SVD::init (const Matrix& a) |
|
596 { |
|
597 int info; |
|
598 |
|
599 int m = a.nr; |
|
600 int n = a.nc; |
|
601 |
|
602 char jobu = 'A'; |
|
603 char jobv = 'A'; |
|
604 |
|
605 double *tmp_data = dup (a.data, a.len); |
|
606 |
|
607 int min_mn = m < n ? m : n; |
|
608 int max_mn = m > n ? m : n; |
|
609 |
|
610 double *u = new double[m*m]; |
|
611 double *s_vec = new double[min_mn]; |
|
612 double *vt = new double[n*n]; |
|
613 |
|
614 int tmp1 = 3*min_mn + max_mn; |
|
615 int tmp2 = 5*min_mn - 4; |
|
616 int lwork = tmp1 > tmp2 ? tmp1 : tmp2; |
|
617 double *work = new double[lwork]; |
|
618 |
|
619 F77_FCN (dgesvd) (&jobu, &jobv, &m, &n, tmp_data, &m, s_vec, u, &m, |
|
620 vt, &n, work, &lwork, &info, 1L, 1L); |
|
621 |
|
622 left_sm = Matrix (u, m, m); |
|
623 sigma = DiagMatrix (s_vec, m, n); |
|
624 Matrix vt_m (vt, n, n); |
|
625 right_sm = Matrix (vt_m.transpose ()); |
|
626 |
|
627 delete [] tmp_data; |
|
628 delete [] work; |
|
629 |
|
630 return info; |
|
631 } |
|
632 |
|
633 ostream& |
|
634 operator << (ostream& os, const SVD& a) |
|
635 { |
|
636 os << a.left_singular_matrix () << "\n"; |
|
637 os << a.singular_values () << "\n"; |
|
638 os << a.right_singular_matrix () << "\n"; |
|
639 |
|
640 return os; |
|
641 } |
|
642 |
|
643 int |
|
644 ComplexSVD::init (const ComplexMatrix& a) |
|
645 { |
|
646 int info; |
|
647 |
|
648 int m = a.nr; |
|
649 int n = a.nc; |
|
650 |
|
651 char jobu = 'A'; |
|
652 char jobv = 'A'; |
|
653 |
|
654 Complex *tmp_data = dup (a.data, a.len); |
|
655 |
|
656 int min_mn = m < n ? m : n; |
|
657 int max_mn = m > n ? m : n; |
|
658 |
|
659 Complex *u = new Complex[m*m]; |
|
660 double *s_vec = new double[min_mn]; |
|
661 Complex *vt = new Complex[n*n]; |
|
662 |
|
663 int lwork = 2*min_mn + max_mn; |
|
664 Complex *work = new Complex[lwork]; |
|
665 |
|
666 int lrwork = 5*max_mn; |
|
667 double *rwork = new double[lrwork]; |
|
668 |
|
669 F77_FCN (zgesvd) (&jobu, &jobv, &m, &n, tmp_data, &m, s_vec, u, &m, |
|
670 vt, &n, work, &lwork, rwork, &info, 1L, 1L); |
|
671 |
|
672 left_sm = ComplexMatrix (u, m, m); |
|
673 sigma = DiagMatrix (s_vec, m, n); |
|
674 ComplexMatrix vt_m (vt, n, n); |
|
675 right_sm = ComplexMatrix (vt_m.hermitian ()); |
|
676 |
|
677 delete [] tmp_data; |
|
678 delete [] work; |
|
679 |
|
680 return info; |
|
681 } |
|
682 |
|
683 /* |
|
684 * EIG stuff. |
|
685 */ |
|
686 |
|
687 int |
|
688 EIG::init (const Matrix& a) |
|
689 { |
|
690 if (a.nr != a.nc) |
|
691 FAIL; |
|
692 |
|
693 int n = a.nr; |
|
694 |
|
695 int info; |
|
696 |
|
697 char jobvl = 'N'; |
|
698 char jobvr = 'V'; |
|
699 |
|
700 double *tmp_data = dup (a.data, a.len); |
|
701 double *wr = new double[n]; |
|
702 double *wi = new double[n]; |
|
703 Matrix vr (n, n); |
|
704 double *pvr = vr.fortran_vec (); |
|
705 int lwork = 8*n; |
|
706 double *work = new double[lwork]; |
|
707 |
|
708 double dummy; |
|
709 int idummy = 1; |
|
710 |
|
711 F77_FCN (dgeev) (&jobvl, &jobvr, &n, tmp_data, &n, wr, wi, &dummy, |
|
712 &idummy, pvr, &n, work, &lwork, &info, 1L, 1L); |
|
713 |
|
714 lambda.resize (n); |
|
715 v.resize (n, n); |
|
716 |
|
717 for (int j = 0; j < n; j++) |
|
718 { |
|
719 if (wi[j] == 0.0) |
|
720 { |
|
721 lambda.elem (j) = Complex (wr[j]); |
|
722 for (int i = 0; i < n; i++) |
|
723 v.elem (i, j) = vr.elem (i, j); |
|
724 } |
|
725 else |
|
726 { |
|
727 if (j+1 >= n) |
|
728 FAIL; |
|
729 |
|
730 for (int i = 0; i < n; i++) |
|
731 { |
|
732 lambda.elem (j) = Complex (wr[j], wi[j]); |
|
733 lambda.elem (j+1) = Complex (wr[j+1], wi[j+1]); |
|
734 double real_part = vr.elem (i, j); |
|
735 double imag_part = vr.elem (i, j+1); |
|
736 v.elem (i, j) = Complex (real_part, imag_part); |
|
737 v.elem (i, j+1) = Complex (real_part, -imag_part); |
|
738 } |
|
739 j++; |
|
740 } |
|
741 } |
|
742 |
|
743 delete [] tmp_data; |
|
744 delete [] wr; |
|
745 delete [] wi; |
|
746 delete [] work; |
|
747 |
|
748 return info; |
|
749 } |
|
750 |
|
751 int |
|
752 EIG::init (const ComplexMatrix& a) |
|
753 { |
|
754 |
|
755 if (a.nr != a.nc) |
|
756 FAIL; |
|
757 |
|
758 int n = a.nr; |
|
759 |
|
760 int info; |
|
761 |
|
762 char jobvl = 'N'; |
|
763 char jobvr = 'V'; |
|
764 |
|
765 lambda.resize (n); |
|
766 v.resize (n, n); |
|
767 |
|
768 Complex *pw = lambda.fortran_vec (); |
|
769 Complex *pvr = v.fortran_vec (); |
|
770 |
|
771 Complex *tmp_data = dup (a.data, a.len); |
|
772 |
|
773 int lwork = 8*n; |
|
774 Complex *work = new Complex[lwork]; |
|
775 double *rwork = new double[4*n]; |
|
776 |
|
777 Complex dummy; |
|
778 int idummy = 1; |
|
779 |
|
780 F77_FCN (zgeev) (&jobvl, &jobvr, &n, tmp_data, &n, pw, &dummy, |
|
781 &idummy, pvr, &n, work, &lwork, rwork, &info, 1L, |
|
782 1L); |
|
783 |
|
784 delete [] tmp_data; |
|
785 delete [] work; |
|
786 delete [] rwork; |
|
787 |
|
788 return info; |
|
789 } |
|
790 |
|
791 /* |
|
792 * LU stuff. |
|
793 */ |
|
794 |
|
795 LU::LU (const Matrix& a) |
|
796 { |
|
797 if (a.nr == 0 || a.nc == 0 || a.nr != a.nc) |
|
798 FAIL; |
|
799 |
|
800 int n = a.nr; |
|
801 |
|
802 int *ipvt = new int [n]; |
|
803 int *pvt = new int [n]; |
|
804 double *tmp_data = dup (a.data, a.len); |
|
805 int info = 0; |
|
806 int zero = 0; |
|
807 double b; |
|
808 |
|
809 F77_FCN (dgesv) (&n, &zero, tmp_data, &n, ipvt, &b, &n, &info); |
|
810 |
|
811 Matrix A_fact (tmp_data, n, n); |
|
812 |
|
813 int i; |
|
814 |
|
815 for (i = 0; i < n; i++) |
|
816 { |
|
817 ipvt[i] -= 1; |
|
818 pvt[i] = i; |
|
819 } |
|
820 |
|
821 for (i = 0; i < n - 1; i++) |
|
822 { |
|
823 int k = ipvt[i]; |
|
824 if (k != i) |
|
825 { |
|
826 int tmp = pvt[k]; |
|
827 pvt[k] = pvt[i]; |
|
828 pvt[i] = tmp; |
|
829 } |
|
830 } |
|
831 |
|
832 l.resize (n, n, 0.0); |
|
833 u.resize (n, n, 0.0); |
|
834 p.resize (n, n, 0.0); |
|
835 |
|
836 for (i = 0; i < n; i++) |
|
837 { |
|
838 p.elem (i, pvt[i]) = 1.0; |
|
839 |
|
840 int j; |
|
841 |
|
842 l.elem (i, i) = 1.0; |
|
843 for (j = 0; j < i; j++) |
|
844 l.elem (i, j) = A_fact.elem (i, j); |
|
845 |
|
846 for (j = i; j < n; j++) |
|
847 u.elem (i, j) = A_fact.elem (i, j); |
|
848 } |
|
849 |
|
850 delete [] ipvt; |
|
851 delete [] pvt; |
|
852 } |
|
853 |
|
854 ComplexLU::ComplexLU (const ComplexMatrix& a) |
|
855 { |
|
856 if (a.nr == 0 || a.nc == 0 || a.nr != a.nc) |
|
857 FAIL; |
|
858 |
|
859 int n = a.nr; |
|
860 |
|
861 int *ipvt = new int [n]; |
|
862 int *pvt = new int [n]; |
|
863 Complex *tmp_data = dup (a.data, a.len); |
|
864 int info = 0; |
|
865 int zero = 0; |
|
866 Complex b; |
|
867 |
|
868 F77_FCN (zgesv) (&n, &zero, tmp_data, &n, ipvt, &b, &n, &info); |
|
869 |
|
870 ComplexMatrix A_fact (tmp_data, n, n); |
|
871 |
|
872 int i; |
|
873 |
|
874 for (i = 0; i < n; i++) |
|
875 { |
|
876 ipvt[i] -= 1; |
|
877 pvt[i] = i; |
|
878 } |
|
879 |
|
880 for (i = 0; i < n - 1; i++) |
|
881 { |
|
882 int k = ipvt[i]; |
|
883 if (k != i) |
|
884 { |
|
885 int tmp = pvt[k]; |
|
886 pvt[k] = pvt[i]; |
|
887 pvt[i] = tmp; |
|
888 } |
|
889 } |
|
890 |
|
891 l.resize (n, n, 0.0); |
|
892 u.resize (n, n, 0.0); |
|
893 p.resize (n, n, 0.0); |
|
894 |
|
895 for (i = 0; i < n; i++) |
|
896 { |
|
897 p.elem (i, pvt[i]) = 1.0; |
|
898 |
|
899 int j; |
|
900 |
|
901 l.elem (i, i) = 1.0; |
|
902 for (j = 0; j < i; j++) |
|
903 l.elem (i, j) = A_fact.elem (i, j); |
|
904 |
|
905 for (j = i; j < n; j++) |
|
906 u.elem (i, j) = A_fact.elem (i, j); |
|
907 } |
|
908 |
|
909 delete [] ipvt; |
|
910 delete [] pvt; |
|
911 } |
|
912 |
|
913 /* |
|
914 * QR stuff. |
|
915 */ |
|
916 |
|
917 QR::QR (const Matrix& a) |
|
918 { |
|
919 int m = a.nr; |
|
920 int n = a.nc; |
|
921 |
|
922 if (m == 0 || n == 0) |
|
923 FAIL; |
|
924 |
|
925 double *tmp_data; |
|
926 int min_mn = m < n ? m : n; |
|
927 double *tau = new double[min_mn]; |
|
928 int lwork = 32*n; |
|
929 double *work = new double[lwork]; |
|
930 int info = 0; |
|
931 |
|
932 if (m > n) |
|
933 { |
|
934 tmp_data = new double [m*m]; |
|
935 copy (tmp_data, a.data, a.len); |
|
936 } |
|
937 else |
|
938 tmp_data = dup (a.data, a.len); |
|
939 |
|
940 F77_FCN (dgeqrf) (&m, &n, tmp_data, &m, tau, work, &lwork, &info); |
|
941 |
|
942 delete [] work; |
|
943 |
|
944 r.resize (m, n, 0.0); |
|
945 for (int j = 0; j < n; j++) |
|
946 { |
|
947 int limit = j < min_mn-1 ? j : min_mn-1; |
|
948 for (int i = 0; i <= limit; i++) |
|
949 r.elem (i, j) = tmp_data[m*j+i]; |
|
950 } |
|
951 |
|
952 lwork = 32*m; |
|
953 work = new double[lwork]; |
|
954 |
|
955 F77_FCN (dorgqr) (&m, &m, &min_mn, tmp_data, &m, tau, work, &lwork, &info); |
|
956 |
|
957 q = Matrix (tmp_data, m, m); |
|
958 |
|
959 delete [] tau; |
|
960 delete [] work; |
|
961 } |
|
962 |
|
963 ComplexQR::ComplexQR (const ComplexMatrix& a) |
|
964 { |
|
965 int m = a.nr; |
|
966 int n = a.nc; |
|
967 |
|
968 if (m == 0 || n == 0) |
|
969 FAIL; |
|
970 |
|
971 Complex *tmp_data; |
|
972 int min_mn = m < n ? m : n; |
|
973 Complex *tau = new Complex[min_mn]; |
|
974 int lwork = 32*n; |
|
975 Complex *work = new Complex[lwork]; |
|
976 int info = 0; |
|
977 |
|
978 if (m > n) |
|
979 { |
|
980 tmp_data = new Complex [m*m]; |
|
981 copy (tmp_data, a.data, a.len); |
|
982 } |
|
983 else |
|
984 tmp_data = dup (a.data, a.len); |
|
985 |
|
986 F77_FCN (zgeqrf) (&m, &n, tmp_data, &m, tau, work, &lwork, &info); |
|
987 |
|
988 delete [] work; |
|
989 |
|
990 r.resize (m, n, 0.0); |
|
991 for (int j = 0; j < n; j++) |
|
992 { |
|
993 int limit = j < min_mn-1 ? j : min_mn-1; |
|
994 for (int i = 0; i <= limit; i++) |
|
995 r.elem (i, j) = tmp_data[m*j+i]; |
|
996 } |
|
997 |
|
998 lwork = 32*m; |
|
999 work = new Complex[lwork]; |
|
1000 |
|
1001 F77_FCN (zungqr) (&m, &m, &min_mn, tmp_data, &m, tau, work, &lwork, &info); |
|
1002 |
|
1003 q = ComplexMatrix (tmp_data, m, m); |
|
1004 |
|
1005 delete [] tau; |
|
1006 delete [] work; |
|
1007 } |
|
1008 |
|
1009 /* |
|
1010 ;;; Local Variables: *** |
|
1011 ;;; mode: C++ *** |
|
1012 ;;; page-delimiter: "^/\\*" *** |
|
1013 ;;; End: *** |
|
1014 */ |