1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
|
/*
* linalg.cpp
* @author David Swofford
* @author Derrick Zwickl
* @author Daniel Ayres
* Based on linalg.cpp by Derrick Zwickl.
*/
//
// NOTE: Portions of this source adapted from MrBayes source code (Huelsenbeck and Ronquist)
// I believe that they originaly appeared in PAUP* source code
# include <stdio.h>
# include <math.h>
# include <float.h>
# include <assert.h>
#define ONE_POINT_ZERO 1.0
#define ZERO_POINT_ZERO 0.0
#include "linalg.h"
#undef NO_ERROR
#undef ERROR
#define NO_ERROR 0
#define ERROR 1
#undef FALSE
#undef TRUE
#define FALSE 0
#define TRUE 1
static void LUBackSubst (double **a, int n, int *indx, double *b);
static int EigenRG (int n, double **a, double *wr, double *wi, double **z, int *iv1, double *fv1);
static void Balanc (int n, double **a, int *pLow, int *pHigh, double *scale);
static void Exchange (int j, int k, int l, int m, int n, double **a, double *scale);
static void ElmHes (int n, int low, int high, double **a, int *intchg);
static void ElTran (int n, int low, int high, double **a, int *intchg, double **z);
static int Hqr2 (int n, int low, int high, double **h, double *wr, double *wi, double **z);
static void BalBak (int n, int low, int high, double *scale, int m, double **z);
static void CDiv (double ar, double ai, double br, double bi, double *cr, double *ci);
#define TINY 1.0e-20
#if !defined(MAX)
# define MAX(a,b) (((a) > (b)) ? (a) : (b))
#endif
#if !defined(MIN)
# define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
/*--------------------------------------------------------------------------------------------------
|
| D_sign
|
| "Sign" function.
*/
inline static double D_sign (double a, double b){
double x = (a >= 0 ? a : -a);
return (b >= 0 ? x : -x);
}
/*--------------------------------------------------------------------------------------------------
|
| InvertMatrix
|
| Invert matrix 'a' using LU-decomposition technique, storing inverse in 'a_inv'. Matrix 'a'
| is destroyed. Returns ERROR if matrix is singular, NO_ERROR otherwise.
*/
int InvertMatrix (double **a, int n, double *col, int *indx, double **a_inv)
/* **a = matrix represented as vector of row pointers */
/* n = order of matrix */
/* *col = work vector of size n */
/* *indx = work vector of size n */
/* **a_inv = inverse of input matrix a (matrix a is destroyed) */
{
int rc, i, j;
rc = LUDecompose(a, n, col, indx, (double *)NULL);
if (rc == FALSE)
{
for (j = 0; j < n; j++)
{
for (i = 0; i < n; i++)
col[i] = 0.0;
col[j] = 1.0;
LUBackSubst(a, n, indx, col);
for (i = 0; i < n; i++)
a_inv[i][j] = col[i];
}
}
return rc;
}
/*--------------------------------------------------------------------------------------------------
|
| LUDecompose
|
| Replace matrix 'a' with its LU-decomposition. Returns ERROR if matrix is singular, NO_ERROR
| otherwise.
*/
int LUDecompose (double **a, int n, double *vv, int *indx, double *pd)
/* **a = the matrix whose LU-decomposition is wanted */
/* n = order of a */
/* *vv = work vector of size n (stores implicit scaling of each row) */
/* *indx => row permutation according to partial pivoting sequence */
/* *pd => 1 if number of row interchanges was even, -1 if odd (NULL OK) */
{
int i, imax, j, k;
double big, dum, sum, temp, d;
d = 1.0;
for (i = 0; i < n; i++)
{
big = 0.0;
for (j = 0; j < n; j++)
{
if ((temp = fabs(a[i][j])) > big)
big = temp;
}
if (big == 0.0)
{
printf("singular matrix in routine LUDecompose");
return ERROR;
}
vv[i] = ONE_POINT_ZERO / big;
}
for (j = 0; j < n; j++)
{
for (i = 0; i < j; i++)
{
sum = a[i][j];
for (k = 0; k < i; k++)
sum -= a[i][k] * a[k][j];
a[i][j] = sum;
}
big = 0.0;
for (i = j; i < n; i++)
{
sum = a[i][j];
for (k = 0; k < j; k++)
sum -= a[i][k] * a[k][j];
a[i][j] = sum;
dum = vv[i] * fabs(sum);
if (dum >= big)
{
big = dum;
imax = i;
}
}
if (j != imax)
{
for (k = 0; k < n; k++)
{
dum = a[imax][k];
a[imax][k] = a[j][k];
a[j][k] = dum;
}
d = -d;
vv[imax] = vv[j];
}
indx[j] = imax;
if (a[j][j] == 0.0)
a[j][j] = TINY;
if (j != n - 1)
{
dum = ONE_POINT_ZERO / (a[j][j]);
for (i = j + 1; i < n; i++)
a[i][j] *= dum;
}
}
if (pd != NULL)
*pd = d;
return NO_ERROR;
}
/*--------------------------------------------------------------------------------------------------
|
| LUBackSubst
|
| Perform back-substition into LU-decomposed matrix in order to obtain inverse.
*/
void LUBackSubst (double **a, int n, int *indx, double *b)
{
int i, ip, j,
ii = -1;
double sum;
for (i = 0; i < n; i++)
{
ip = indx[i];
sum = b[ip];
b[ip] = b[i];
if (ii >= 0)
{
for (j = ii; j <= i - 1; j++)
sum -= a[i][j] * b[j];
}
else if (sum != 0.0)
ii = i;
b[i] = sum;
}
for (i = n - 1; i >= 0; i--)
{
sum = b[i];
for (j = i + 1; j < n; j++)
sum -= a[i][j] * b[j];
b[i] = sum / a[i][i];
}
}
/*--------------------------------------------------------------------------------------------------
|
| EigenRealGeneral
|
| Calculate eigenvalues and eigenvectors of a general real matrix assuming that all eigenvalues
| are real, using routines from the public domain EISPACK package.
*/
int EigenRealGeneral (int n, double **a, double *v, double *vi, double **u, int *iwork, double *work)
/* n = order of a */
/* **a = input matrix in row-ptr representation; will be destroyed */
/* *v = array of size 'n' to receive eigenvalues */
/* *vi = work vector of size 'n' for imaginary components of eigenvalues */
/* **u = matrix in row-ptr representation to receive eigenvectors */
/* *iwork = work vector of size 'n' */
/* *work = work vector of size 'n' */
{
int i, rc;
rc = EigenRG (n, a, v, vi, u, iwork, work);
if (rc != NO_ERROR)
{
puts("\nInternal error in 'EigenRealGeneral'.");
printf ("rc = %d\n", rc);
return ERROR;
}
for (i = 0; i < n; i++)
{
if (vi[i] != 0.0)
return RC_COMPLEX_EVAL;
}
return NO_ERROR;
}
/*--------------------------------------------------------------------------------------------------
|
| EigenRG
|
| This subroutine calls the recommended sequence of subroutines from the eigensystem subroutine
| package (EISPACK) to find the eigenvalues of a real general matrix. It was converted from
| Fortran to C by David Swofford.
|
| ON INPUT:
|
| n is the order of the matrix 'a'
|
| a contains the real general matrix
|
| ON OUTPUT:
|
| wr and wi contain the real and imaginary parts, respectively, of the eigenvalues.
| Complex conjugate pairs of eigenvalues appear consecutively with the eigenvalue having the
| positive imaginary part first.
|
| z contains the real and imaginary parts of the eigenvectors. If the j-th eigenvalue is
| real, the j-th column of z contains its eigenvector. If the j-th eigenvalue is complex
| with positive imaginary part, the j-th and (j+1)-th columns of z contain the real and
| imaginary parts of its eigenvector. The conjugate of this vector is the eigenvector for
| the conjugate eigenvalue.
|
| ierr is an integer output variable set equal to an error completion code described in the
| documentation for Hqr and Hqr2. The normal completion code is zero.
|
| iv1 and fv1 are temporary storage vectors of size n
*/
int EigenRG (int n, double **a, double *wr, double *wi, double **z, int *iv1, double *fv1)
{
static int is1, is2;
int ierr;
Balanc (n, a, &is1, &is2, fv1);
ElmHes (n, is1, is2, a, iv1);
ElTran (n, is1, is2, a, iv1, z);
ierr = Hqr2 (n, is1, is2, a, wr, wi, z);
if (ierr == 0)
BalBak (n, is1, is2, fv1, n, z);
return ierr;
}
/*--------------------------------------------------------------------------------------------------
|
| Balanc
|
| EISPACK routine translated from Fortran to C by David Swofford. Modified EISPACK comments
| follow.
|
| This subroutine is a translation of the algol procedure BALANCE, Num. Math. 13, 293-304(1969)
| by Parlett and Reinsch. Handbook for Auto. Comp., Vol. II-Linear Algebra, 315-326( 1971).
|
| This subroutine balances a real matrix and isolates eigenvalues whenever possible.
|
| ON INPUT:
|
| n is the order of the matrix.
|
| a contains the input matrix to be balanced.
|
| ON OUTPUT:
|
| a contains the balanced matrix.
|
| low and high are two integers such that a(i,j) is equal to zero if
| (1) i is greater than j and
| (2) j=1,...,low-1 or i=high+1,...,n.
|
| scale contains information determining the permutations and scaling factors used.
|
| Suppose that the principal submatrix in rows low through high has been balanced, that p(j)
| denotes the index interchanged with j during the permutation step, and that the elements of the
| diagonal matrix used are denoted by d(i,j). Then
| scale(j) = p(j), for j = 1,...,low-1
| = d(j,j), j = low,...,high
| = p(j) j = high+1,...,n.
| The order in which the interchanges are made is n to high+1, then 1 to low-1.
|
| Note that 1 is returned for high if high is zero formally.
*/
void Balanc (int n, double **a, int *pLow, int *pHigh, double *scale)
{
double c, f, g, r, s, b2;
int i, j, k, l, m, noconv;
b2 = FLT_RADIX * FLT_RADIX;
k = 0;
l = n - 1;
/* search for rows isolating an eigenvalue and push them down */
for (j = l; j >= 0; j--)
{
for (i = 0; i <= l; i++)
{
if (i != j)
{
if (a[j][i] != 0.0)
goto next_j1;
}
}
# if 0 /* bug that dave caught */
m = l;
Exchange(j, k, l, m, n, a, scale);
if (l < 0)
goto leave;
else
j = --l;
# else
m = l;
Exchange(j, k, l, m, n, a, scale);
if (--l < 0)
goto leave;
# endif
next_j1:
;
}
/* search for columns isolating an eigenvalue and push them left */
for (j = k; j <= l; j++)
{
for (i = k; i <= l; i++)
{
if (i != j)
{
if (a[i][j] != 0.0)
goto next_j;
}
}
m = k;
Exchange(j, k, l, m, n, a, scale);
k++;
next_j:
;
}
/* now balance the submatrix in rows k to l */
for (i = k; i <= l; i++)
scale[i] = 1.0;
/* iterative loop for norm reduction */
do {
noconv = FALSE;
for (i = k; i <= l; i++)
{
c = 0.0;
r = 0.0;
for (j = k; j <= l; j++)
{
if (j != i)
{
c += fabs(a[j][i]);
r += fabs(a[i][j]);
}
}
/* guard against zero c or r due to underflow */
if ((c != 0.0) && (r != 0.0))
{
g = r / FLT_RADIX;
f = 1.0;
s = c + r;
while (c < g)
{
f *= FLT_RADIX;
c *= b2;
}
g = r * FLT_RADIX;
while (c >= g)
{
f /= FLT_RADIX;
c /= b2;
}
/* now balance */
if ((c + r) / f < s * .95)
{
g = ONE_POINT_ZERO / f;
scale[i] *= f;
noconv = TRUE;
for (j = k; j < n; j++)
a[i][j] *= g;
for (j = 0; j <= l; j++)
a[j][i] *= f;
}
}
}
}
while (noconv);
leave:
*pLow = k;
*pHigh = l;
}
/*--------------------------------------------------------------------------------------------------
|
| Exchange
|
| Support function for EISPACK routine Balanc.
*/
void Exchange (int j, int k, int l, int m, int n, double **a, double *scale)
{
int i;
double f;
scale[m] = (double)j;
if (j != m)
{
for (i = 0; i <= l; i++)
{
f = a[i][j];
a[i][j] = a[i][m];
a[i][m] = f;
}
for (i = k; i < n; i++)
{
f = a[j][i];
a[j][i] = a[m][i];
a[m][i] = f;
}
}
}
/*--------------------------------------------------------------------------------------------------
|
| ElmHes
|
| EISPACK routine translated from Fortran to C by David Swofford. Modified EISPACK comments
| follow.
|
| This subroutine is a translation of the algol procedure ELMHES, Num. Math. 12, 349-368(1968) by
| Martin and Wilkinson. Handbook for Auto. Comp., Vol. II-Linear Algebra, 339-358 (1971).
|
| Given a real general matrix, this subroutine reduces a submatrix situated in rows and columns
| low through high to upper Hessenberg form by stabilized elementary similarity transformations.
|
| ON INPUT:
|
| n is the order of the matrix.
|
| low and high are integers determined by the balancing subroutine BALANC. If BALANC has not
| been used, set low=1, high=n.
|
| a contains the input matrix.
|
| ON OUTPUT:
|
| a contains the Hessenberg matrix. The multipliers which were used in the reduction are
| stored in the remaining triangle under the Hessenberg matrix.
|
| int contains information on the rows and columns interchanged in the reduction. Only
| elements low through high are used.
*/
void ElmHes (int n, int low, int high, double **a, int *intchg)
{
int i, j, m;
double x, y;
int la, mm1, kp1, mp1;
la = high - 1;
kp1 = low + 1;
if (la < kp1)
return;
for (m = kp1; m <= la; m++)
{
mm1 = m - 1;
x = 0.0;
i = m;
for (j = m; j <= high; j++)
{
if (fabs(a[j][mm1]) > fabs(x))
{
x = a[j][mm1];
i = j;
}
}
intchg[m] = i;
if (i != m)
{
/* interchange rows and columns of a */
for (j = mm1; j < n; j++)
{
y = a[i][j];
a[i][j] = a[m][j];
a[m][j] = y;
}
for (j = 0; j <= high; j++)
{
y = a[j][i];
a[j][i] = a[j][m];
a[j][m] = y;
}
}
if (x != 0.0)
{
mp1 = m + 1;
for (i = mp1; i <= high; i++)
{
y = a[i][mm1];
if (y != 0.0)
{
y /= x;
a[i][mm1] = y;
for (j = m; j < n; j++)
a[i][j] -= y * a[m][j];
for (j = 0; j <= high; j++)
a[j][m] += y * a[j][i];
}
}
}
}
}
/*--------------------------------------------------------------------------------------------------
|
| ElTran
|
| EISPACK routine translated from Fortran to C by David Swofford. Modified EISPACK comments
| follow.
|
| This subroutine is a translation of the algol procedure ELMTRANS, Num. Math. 16, 181-204 (1970)
| by Peters and Wilkinson. Handbook for Auto. Comp., Vol. II-Linear Algebra, 372-395 (1971).
|
| This subroutine accumulates the stabilized elementary similarity transformations used in the
| reduction of a real general matrix to upper Hessenberg form by ElmHes.
|
| ON INPUT:
|
| n is the order of the matrix.
|
| low and high are integers determined by the balancing subroutine Balanc. if Balanc has
| not been used, set low=1, high=n.
|
| a contains the multipliers which were used in the reduction by ElmHes in its lower triangle
| below the subdiagonal.
|
| intchg contains information on the rows and columns interchanged in the reduction by ElmHes.
| Only elements low through high are used.
|
| ON OUTPUT:
|
| z contains the transformation matrix produced in the reduction by ElmHes.
*/
void ElTran (int n, int low, int high, double **a, int *intchg, double **z)
{
int i, j, mp;
/* initialize z to identity matrix */
for (j = 0; j < n; j++)
{
for (i = 0; i < n; i++)
z[i][j] = 0.0;
z[j][j] = 1.0;
}
for (mp = high - 1; mp >= low + 1; mp--)
{
for (i = mp + 1; i <= high; i++)
z[i][mp] = a[i][mp-1];
i = intchg[mp];
if (i != mp)
{
for (j = mp; j <= high; j++)
{
z[mp][j] = z[i][j];
z[i][j] = 0.0;
}
z[i][mp] = 1.0;
}
}
}
/*--------------------------------------------------------------------------------------------------
|
| Hqr2
|
| EISPACK routine translated from Fortran to C by David Swofford. Modified EISPACK comments
| follow.
|
| This subroutine is a translation of the algol procedure HQR2, Num. Math. 16, 181-204 (1970) by
| Peters and Wilkinson. Handbook for Auto. Comp., Vol. II-Linear Algebra, 372-395 (1971).
|
| This subroutine finds the eigenvalues and eigenvectors of a real upper Hessenberg matrix by
| the QR method. The eigenvectors of a real general matrix can also be found if ElmHes and
| ElTran or OrtHes and OrTran have been used to reduce this general matrix to Hessenberg form
| and to accumulate the similarity transformations.
|
| ON INPUT:
|
| n is the order of the matrix
|
| low and high are integers determined by the balancing subroutine Balanc. If Balanc has not
| been used, set low=0, high=n-1.
|
| h contains the upper Hessenberg matrix
|
| z contains the transformation matrix produced by ElTran after the reduction by ElmHes, or
| by OrTran after the reduction by OrtHes, if performed. If the eigenvectors of the
| Hessenberg matrix are desired, z must contain the identity matrix.
|
| ON OUTPUT:
|
| h has been destroyed
|
| wr and wi contain the real and imaginary parts, respectively, of the eigenvalues. The
| eigenvalues are unordered except that complex conjugate pairs of values appear consecutively
| with the eigenvalue having the positive imaginary part first. If an error exit is made, the
| eigenvalues should be correct for indices ierr,...,n-1.
|
| z contains the real and imaginary parts of the eigenvectors. If the i-th eigenvalue is
| real, the i-th column of z contains its eigenvector. If the i-th eigenvalue is complex with
| positive imaginary part, the i-th and (i+1)-th columns of z contain the real and imaginary
| parts of its eigenvector. The eigenvectors are unnormalized. If an error exit is made,
| none of the eigenvectors has been found.
|
| Return value is set to:
| zero for normal return,
| j if the limit of 30*n iterations is exhausted while the j-th eigenvalue is
| being sought.
|
| Calls CDiv for complex division.
*/
//DJZ - Intel compiler 10.0 -O2 optimization breaks this function
//so this pragma reduces the optimization level
#if (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1000)
#pragma intel optimization_level 1
#endif
int Hqr2 (int n, int low, int high, double **h, double *wr, double *wi, double **z)
{
int i, j, k, l, m, na, en, notlas, mp2, itn, its, enm2, twoRoots;
double norm, p, q, r, s, t, w, x, y, ra, sa, vi, vr, zz, tst1, tst2;
/* store roots isolated by Balanc and compute matrix norm */
norm = 0.0;
k = 0;
for (i = 0; i < n; i++)
{
for (j = k; j < n; j++)
norm += fabs(h[i][j]);
k = i;
if ((i < low) || (i > high))
{
wr[i] = h[i][i];
wi[i] = 0.0;
}
}
en = high;
t = 0.0;
itn = n * 30;
/* search for next eigenvalues */
while (en >= low)
{
its = 0;
na = en - 1;
enm2 = na - 1;
twoRoots = FALSE;
/* look for single small sub-diagonal element */
for (;;)
{
for (l = en; l > low; l--)
{
s = fabs(h[l-1][l-1]) + fabs(h[l][l]);
if (s == 0.0)
s = norm;
tst1 = s;
tst2 = tst1 + fabs(h[l][l-1]);
if (tst2 == tst1)
break;
}
/* form shift */
x = h[en][en];
if (l == en)
break;
y = h[na][na];
w = h[en][na] * h[na][en];
if (l == na)
{
twoRoots = TRUE;
break;
}
if (itn == 0)
{
/* set error -- all eigenvalues have not converged after 30*n iterations */
return en;
}
if ((its == 10) || (its == 20))
{
/* form exceptional shift */
t += x;
for (i = low; i <= en; i++)
h[i][i] -= x;
s = fabs(h[en][na]) + fabs(h[na][enm2]);
x = s * (double) 0.75;
y = x;
w = s * (double)-0.4375 * s;
}
its++;
--itn;
/* look for two consecutive small sub-diagonal elements */
for (m = enm2; m >= l; m--)
{
zz = h[m][m];
r = x - zz;
s = y - zz;
p = (r * s - w) / h[m+1][m] + h[m][m+1];
q = h[m+1][m+1] - zz - r - s;
r = h[m+2][m+1];
s = fabs(p) + fabs(q) + fabs(r);
p /= s;
q /= s;
r /= s;
if (m == l)
break;
tst1 = fabs(p) * (fabs(h[m-1][m-1]) + fabs(zz) + fabs(h[m+1][m+1]));
tst2 = tst1 + fabs(h[m][m-1]) * (fabs(q) + fabs(r));
if (tst2 == tst1)
break;
}
mp2 = m + 2;
for (i = mp2; i <= en; i++)
{
h[i][i-2] = 0.0;
if (i != mp2)
h[i][i-3] = 0.0;
}
/* double qr step involving rows l to en and columns m to en */
for (k = m; k <= na; k++)
{
notlas = (k != na);
if (k != m)
{
p = h[k][k-1];
q = h[k+1][k-1];
r = 0.0;
if (notlas)
r = h[k+2][k-1];
x = fabs(p) + fabs(q) + fabs(r);
if (x == 0.0)
continue;
p /= x;
q /= x;
r /= x;
}
s = D_sign(sqrt(p*p + q*q + r*r), p);
if (k != m)
h[k][k-1] = -s * x;
else if (l != m)
h[k][k-1] = -h[k][k-1];
p += s;
x = p / s;
y = q / s;
zz = r / s;
q /= p;
r /= p;
if (!notlas)
{
/* row modification */
for (j = k; j < n; j++)
{
p = h[k][j] + q * h[k+1][j];
h[k][j] -= p * x;
h[k+1][j] -= p * y;
}
j = MIN(en, k + 3);
/* column modification */
for (i = 0; i <= j; i++)
{
p = x * h[i][k] + y * h[i][k+1];
h[i][k] -= p;
h[i][k+1] -= p * q;
}
/* accumulate transformations */
for (i = low; i <= high; i++)
{
p = x * z[i][k] + y * z[i][k+1];
z[i][k] -= p;
z[i][k+1] -= p * q;
}
}
else
{
/* row modification */
for (j = k; j < n; j++)
{
p = h[k][j] + q * h[k+1][j] + r * h[k+2][j];
h[k][j] -= p * x;
h[k+1][j] -= p * y;
h[k+2][j] -= p * zz;
}
j = MIN(en, k + 3);
/* column modification */
for (i = 0; i <= j; i++)
{
p = x * h[i][k] + y * h[i][k+1] + zz * h[i][k+2];
h[i][k] -= p;
h[i][k+1] -= p * q;
h[i][k+2] -= p * r;
}
/* accumulate transformations */
for (i = low; i <= high; i++)
{
p = x * z[i][k] + y * z[i][k+1] + zz * z[i][k+2];
z[i][k] -= p;
z[i][k+1] -= p * q;
z[i][k+2] -= p * r;
}
}
}
}
if (twoRoots)
{
/* two roots found */
p = (y - x) / (double) 2.0;
q = p * p + w;
zz = sqrt(fabs(q));
h[en][en] = x + t;
x = h[en][en];
h[na][na] = y + t;
/* DLS 28aug96: Changed "0.0" to "-1e-12" below. Roundoff errors can cause this value
to dip ever-so-slightly below zero even when eigenvalue is not complex.
*/
if (q >= -1e-12)
{
/* real pair */
zz = p + D_sign(zz, p);
wr[na] = x + zz;
wr[en] = wr[na];
if (zz != 0.0)
wr[en] = x - w/zz;
wi[na] = 0.0;
wi[en] = 0.0;
x = h[en][na];
s = fabs(x) + fabs(zz);
p = x / s;
q = zz / s;
r = sqrt(p*p + q*q);
p /= r;
q /= r;
/* row modification */
for (j = na; j < n; j++)
{
zz = h[na][j];
h[na][j] = q * zz + p * h[en][j];
h[en][j] = q * h[en][j] - p * zz;
}
/* column modification */
for (i = 0; i <= en; i++)
{
zz = h[i][na];
h[i][na] = q * zz + p * h[i][en];
h[i][en] = q * h[i][en] - p * zz;
}
/* accumulate transformations */
for (i = low; i <= high; i++)
{
zz = z[i][na];
z[i][na] = q * zz + p * z[i][en];
z[i][en] = q * z[i][en] - p * zz;
}
}
else
{
/* complex pair */
wr[na] = x + p;
wr[en] = x + p;
wi[na] = zz;
wi[en] = -zz;
}
en = enm2;
}
else
{
/* one root found */
h[en][en] = x + t;
wr[en] = h[en][en];
wi[en] = 0.0;
en = na;
}
}
/* All roots found. Backsubstitute to find vectors of upper triangular form */
if (norm == 0.0)
return 0;
for (en = n - 1; en >= 0; en--)
{
p = wr[en];
q = wi[en];
na = en - 1;
/* DLS 28aug96: Changed "0.0" to -1e-12 below (see comment above) */
if (q < -1e-12)
{
/* complex vector */
m = na;
/* last vector component chosen imaginary so that eigenvector matrix is triangular */
if (fabs(h[en][na]) > fabs(h[na][en]))
{
h[na][na] = q / h[en][na];
h[na][en] = -(h[en][en] - p) / h[en][na];
}
else
CDiv(0.0, -h[na][en], h[na][na] - p, q, &h[na][na], &h[na][en]);
h[en][na] = 0.0;
h[en][en] = 1.0;
enm2 = na - 1;
if (enm2 >= 0)
{
for (i = enm2; i >= 0; i--)
{
w = h[i][i] - p;
ra = 0.0;
sa = 0.0;
for (j = m; j <= en; j++)
{
ra += h[i][j] * h[j][na];
sa += h[i][j] * h[j][en];
}
if (wi[i] < 0.0)
{
zz = w;
r = ra;
s = sa;
}
else
{
m = i;
if (wi[i] == 0.0)
CDiv(-ra, -sa, w, q, &h[i][na], &h[i][en]);
else
{
/* solve complex equations */
x = h[i][i+1];
y = h[i+1][i];
vr = (wr[i] - p) * (wr[i] - p) + wi[i] * wi[i] - q * q;
vi = (wr[i] - p) * (double)2.0 * q;
if ((vr == 0.0) && (vi == 0.0))
{
tst1 = norm * (fabs(w) + fabs(q) + fabs(x) + fabs(y) + fabs(zz));
vr = tst1;
do {
vr *= (double) 0.01;
tst2 = tst1 + vr;
}
while (tst2 > tst1);
}
CDiv(x * r - zz * ra + q * sa, x * s - zz * sa - q * ra, vr, vi, &h[i][na], &h[i][en]);
if (fabs(x) > fabs(zz) + fabs(q))
{
h[i+1][na] = (-ra - w * h[i][na] + q * h[i][en]) / x;
h[i+1][en] = (-sa - w * h[i][en] - q * h[i][na]) / x;
}
else
CDiv(-r - y * h[i][na], -s - y * h[i][en], zz, q, &h[i+1][na], &h[i+1][en]);
}
/* overflow control */
tst1 = fabs(h[i][na]);
tst2 = fabs(h[i][en]);
t = MAX(tst1, tst2);
if (t != 0.0)
{
tst1 = t;
tst2 = tst1 + ONE_POINT_ZERO / tst1;
if (tst2 <= tst1)
{
for (j = i; j <= en; j++)
{
h[j][na] /= t;
h[j][en] /= t;
}
}
}
}
}
}
/* end complex vector */
}
else if (q == 0.0)
{
/* real vector */
m = en;
h[en][en] = 1.0;
if (na >= 0)
{
for (i = na; i >= 0; i--)
{
w = h[i][i] - p;
r = 0.0;
for (j = m; j <= en; j++)
r += h[i][j] * h[j][en];
if (wi[i] < 0.0)
{
zz = w;
s = r;
continue;
}
else
{
m = i;
if (wi[i] == 0.0)
{
t = w;
if (t == 0.0)
{
tst1 = norm;
t = tst1;
do {
t *= (double) 0.01;
tst2 = norm + t;
}
while (tst2 > tst1);
}
h[i][en] = -r / t;
}
else
{
/* solve real equations */
x = h[i][i+1];
y = h[i+1][i];
q = (wr[i] - p) * (wr[i] - p) + wi[i] * wi[i];
t = (x * s - zz * r) / q;
h[i][en] = t;
if (fabs(x) > fabs(zz))
h[i+1][en] = (-r - w * t) / x;
else
h[i+1][en] = (-s - y * t) / zz;
}
/* overflow control */
t = fabs(h[i][en]);
if (t != 0.0)
{
tst1 = t;
tst2 = tst1 + ONE_POINT_ZERO / tst1;
if (tst2 <= tst1)
{
for (j = i; j <= en; j++)
h[j][en] /= t;
}
}
}
}
}
/* end real vector */
}
}
/* end back substitution */
/* vectors of isolated roots */
for (i = 0; i < n; i++)
{
if ((i < low) || (i > high))
{
for (j = i; j < n; j++)
z[i][j] = h[i][j];
}
}
/* multiply by transformation matrix to give vectors of original full matrix */
for (j = n - 1; j >= low; j--)
{
m = MIN(j, high);
for (i = low; i <= high; i++)
{
zz = 0.0;
for (k = low; k <= m; k++)
zz += z[i][k] * h[k][j];
z[i][j] = zz;
}
}
return 0;
}
/*--------------------------------------------------------------------------------------------------
|
| BalBak
|
| EISPACK routine translated from Fortran to C by David Swofford. Modified EISPACK comments
| follow.
|
| This subroutine is a translation of the algol procedure BALBAK, Num. Math. 13, 293-304 (1969)
| by Parlett and Reinsch. Handbook for Auto. Comp., vol. II-Linear Algebra, 315-326 (1971).
|
| This subroutine forms the eigenvectors of a real general matrix by back transforming those of
| the corresponding balanced matrix determined by Balanc.
|
| ON INPUT:
|
| n is the order of the matrix.
|
| low and high are integers determined by Balanc.
|
| scale contains information determining the permutations and scaling factors used by Balanc.
|
| m is the number of columns of z to be back transformed.
|
| z contains the real and imaginary parts of the eigenvectors to be back transformed in its
| first m columns.
|
| ON OUTPUT:
|
| z contains the real and imaginary parts of the transformed eigenvectors in its first m
| columns.
*/
void BalBak (int n, int low, int high, double *scale, int m, double **z)
{
int i, j, k, ii;
double s;
if (m != 0)
{
if (high != low)
{
for (i = low; i <= high; i++)
{
s = scale[i]; /* left hand eigenvectors are back transformed if this statement is
replaced by s = 1.0/scale[i] */
for (j = 0; j < m; j++)
z[i][j] *= s;
}
}
for (ii = 0; ii < n; ii++)
{
i = ii;
if ((i < low) || (i > high))
{
if (i < low)
i = low - ii;
k = (int)scale[i];
if (k != i)
{
for (j = 0; j < m; j++)
{
s = z[i][j];
z[i][j] = z[k][j];
z[k][j] = s;
}
}
}
}
}
}
/*--------------------------------------------------------------------------------------------------
|
| CDiv
|
| Complex division, (cr,ci) = (ar,ai)/(br,bi)
*/
void CDiv (double ar, double ai, double br, double bi, double *cr, double *ci)
{
double s, ais, bis, ars, brs;
s = fabs(br) + fabs(bi);
ars = ar / s;
ais = ai / s;
brs = br / s;
bis = bi / s;
s = brs*brs + bis*bis;
*cr = (ars*brs + ais*bis) / s;
*ci = (ais*brs - ars*bis) / s;
}
|