| 1 |
/* |
| 2 |
* Copyright(C) 1999-2021 National Technology & Engineering Solutions |
| 3 |
* of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 4 |
* NTESS, the U.S. Government retains certain rights in this software. |
| 5 |
* |
| 6 |
* See packages/seacas/LICENSE for details |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <math.h> |
| 10 |
|
| 11 |
/* Evaluates principal minor polynomial, returns the index of the eigenvalue just |
| 12 |
left of mu. Based on Wilkinson's algorithm AEP, p.302. Found that this algorithm |
| 13 |
could fail in practice when the p recursion overflowed. Fixed by rescaling the |
| 14 |
recursion if it got too large. Routine returns -1 if this re-scaling doesn't work |
| 15 |
for some reason. Otherwise it returns 0. Note Wilkinson indexes beta such that |
| 16 |
first off-diagonal entry in T is beta[2]. We index such that it is beta[1]. */ |
| 17 |
|
| 18 |
int sturmcnt(double *alpha, /* vector of Lanczos scalars */ |
| 19 |
double *beta, /* vector of Lanczos scalars */ |
| 20 |
int j, /* index of Lanczos step we're on */ |
| 21 |
double mu, /* argument to the sequence generating polynomial */ |
| 22 |
double *p /* work vector for sturm sequence */ |
| 23 |
) |
| 24 |
{ |
| 25 |
extern double DOUBLE_MAX; /* maximum double precision number to be used */ |
| 26 |
int cnt; /* number of sign changes in the sequence */ |
| 27 |
|
| 28 |
if (j == 1) { |
| 29 |
/* have to special case this one */ |
| 30 |
if (alpha[1] > mu) { |
| 31 |
cnt = 1; |
| 32 |
} |
| 33 |
else { |
| 34 |
cnt = 0; |
| 35 |
} |
| 36 |
} |
| 37 |
else { |
| 38 |
/* compute the Sturm sequence */ |
| 39 |
double limit = sqrt(DOUBLE_MAX); |
| 40 |
p[0] = 1; |
| 41 |
p[1] = alpha[1] - mu; |
| 42 |
double *pntr_p = &p[2]; |
| 43 |
double *pntr_p1 = &p[1]; |
| 44 |
double *pntr_p2 = &p[0]; |
| 45 |
double *pntr_alpha = &alpha[2]; |
| 46 |
double *pntr_beta = &beta[1]; |
| 47 |
for (int i = 2; i <= j; i++) { |
| 48 |
*pntr_p++ = (*pntr_alpha - mu) * (*pntr_p1) - (*pntr_beta) * (*pntr_beta) * (*pntr_p2); |
| 49 |
pntr_alpha++; |
| 50 |
pntr_beta++; |
| 51 |
pntr_p1++; |
| 52 |
pntr_p2++; |
| 53 |
double scale = fabs(*pntr_p1); |
| 54 |
if (scale > limit) { |
| 55 |
*pntr_p1 /= scale; |
| 56 |
*pntr_p2 /= scale; |
| 57 |
/* re-scale to avoid overflow in p recursion */ |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/* count sign changes */ |
| 62 |
cnt = 0; |
| 63 |
int last_sign = 1; |
| 64 |
pntr_p = &p[1]; |
| 65 |
for (int i = j; i; i--) { |
| 66 |
if (*pntr_p != *pntr_p || fabs(*pntr_p) > limit) { |
| 67 |
return (-1); |
| 68 |
/* ... re-scaling failed; bail out and return error code. |
| 69 |
Note (x != x) is TRUE iff x is NaN, so this check |
| 70 |
should be ok on non-IEEE floating point systems too. */ |
| 71 |
} |
| 72 |
int sign; |
| 73 |
if (*pntr_p > 0) { |
| 74 |
sign = 1; |
| 75 |
} |
| 76 |
else if (*pntr_p < 0) { |
| 77 |
sign = -1; |
| 78 |
} |
| 79 |
else { |
| 80 |
sign = -last_sign; |
| 81 |
} |
| 82 |
if (sign == last_sign) { |
| 83 |
cnt++; |
| 84 |
} |
| 85 |
last_sign = sign; |
| 86 |
pntr_p++; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/* cnt is number of evals strictly > than mu. Instead send back index of the |
| 91 |
eigenvalue just left of mu. */ |
| 92 |
return (j - cnt); /* ... things seem ok. */ |
| 93 |
} |