-
Notifications
You must be signed in to change notification settings - Fork 192
/
sturmSeq.py
34 lines (31 loc) · 984 Bytes
/
sturmSeq.py
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
## module sturmSeq
''' p = sturmSeq(c,d,lam).
Returns the Sturm sequence {p[0],p[1],...,p[n]}
associated with the characteristic polynomial
|[A] - lam[I]| = 0, where [A] = [c\d\c] is a n x n
tridiagonal matrix.
numLam = numLambdas(p).
Returns the number of eigenvalues of a tridiagonal
matrix [A] = [c\d\c] that are smaller than 'lam'.
Uses the Sturm sequence {p} obtained from 'sturmSeq'.
'''
from numpy import ones
def sturmSeq(d,c,lam):
n = len(d) + 1
p = ones(n)
p[1] = d[0] - lam
for i in range(2,n):
## if c[i-2] == 0.0: c[i-2] = 1.0e-12
p[i] = (d[i-1] - lam)*p[i-1] - (c[i-2]**2)*p[i-2]
return p
def numLambdas(p):
n = len(p)
signOld = 1
numLam = 0
for i in range(1,n):
if p[i] > 0.0: sign = 1
elif p[i] < 0.0: sign = -1
else: sign = -signOld
if sign*signOld < 0: numLam = numLam + 1
signOld = sign
return numLam