def eigenvals3(d, c, N): def f(x): # f(x) = |[A] - x[I]| p = sturmSeq(d, c, x) return p[len(p) - 1] lam = zeros(N) r = lamRange(d, c, N) # Bracket eigenvalues for i in range(N): # Solve by Brent's method lam[i] = ridder(f, r[i], r[i + 1]) return lam
def eigenvals3(d, c, N): def f(x): # f(x) = |[A] - x[I]| p = sturmSeq(d, c, x) return p[len(p) - 1] evals = np.empty(N) r = lamRange(d, c, N) # Bracket eigenvalues for i in range(N): # Solve by Brent's method assert (f(r[i]) * f(r[i + 1]) < 0.0) evals[i] = scipy.optimize.brentq(f, r[i], r[i + 1]) return evals
def eigenvals3(d,c,N): def f(x): # f(x) = |[A] - x[I]| p = sturmSeq(d,c,x) return p[len(p)-1] lam = zeros(N) r = lamRange(d,c,N) # Bracket eigenvalues for i in range(N): # Solve by Brent's method lam[i] = ridder(f,r[i],r[i+1]) return lam
def tridiagonal_solver(d, e, eigenvectors=True): """Calculates the eigenvalues and eigenvectors of a tridiagonal and symmetric matrix. Parameters ---------- d : a numpy array with ndim = 1. The elements of the diagonal of the tridiagonal matrix. e : a numpy array with ndim = 1. The off-diagonal elements of the tridiagonal matrix. eigenvectors : a bool (optional). Whether you want to calculate the eigenvectors. Returns ------- evals : a numpy array with ndim = 1. The eigenvalues. evecs : a numpy array with ndim = 2. The eigenvectors. Raises ------ TridiagonalException if `d` and `e` have different sizes. """ if (d.size != e.size + 1): raise TridiagonalException("d, and e have different sizes") num_evals = d.size evals = np.empty(num_evals) evecs = np.empty((num_evals, num_evals)) r = lamRange(d, e, num_evals) assert (len(r) == num_evals + 1) evals = eigenvals3(d, e, num_evals) if eigenvectors: for i in range(num_evals): evals[i], evecs[:, i] = inversePower3(d, e, 1.00000001 * evals[i]) # # An alternative that uses the brakets from lamRange: # # if eigenvectors: # for i in range(num_evals): # s = (r[i] + r[i+1])/2.0 # evals[i], evecs[:, i] = inversePower3(d, e, s) # else: # evals = eigenvals3(d, e, num_evals) return evals, evecs
def tridiagonal_solver(d, e, eigenvectors = True): """Calculates the eigenvalues and eigenvectors of a tridiagonal and symmetric matrix. Parameters ---------- d : a numpy array with ndim = 1. The elements of the diagonal of the tridiagonal matrix. e : a numpy array with ndim = 1. The off-diagonal elements of the tridiagonal matrix. eigenvectors : a bool (optional). Whether you want to calculate the eigenvectors. Returns ------- evals : a numpy array with ndim = 1. The eigenvalues. evecs : a numpy array with ndim = 2. The eigenvectors. Raises ------ TridiagonalException if `d` and `e` have different sizes. """ if (d.size != e.size + 1): raise TridiagonalException("d, and e have different sizes") num_evals = d.size evals = np.empty(num_evals) evecs = np.empty((num_evals, num_evals)) r = lamRange(d, e, num_evals) assert(len(r) == num_evals +1) evals = eigenvals3(d, e, num_evals) if eigenvectors: for i in range(num_evals): evals[i], evecs[:, i] = inversePower3(d, e, 1.00000001*evals[i]) # # An alternative that uses the brakets from lamRange: # # if eigenvectors: # for i in range(num_evals): # s = (r[i] + r[i+1])/2.0 # evals[i], evecs[:, i] = inversePower3(d, e, s) # else: # evals = eigenvals3(d, e, num_evals) return evals, evecs
## example9_13 from numarray import ones from lamRange import * from inversePower3 import * N = 10 n = 100 d = ones((n)) * 2.0 c = ones((n - 1)) * (-1.0) r = lamRange(d, c, N) # Bracket N smallest eigenvalues s = (r[N - 1] + r[N]) / 2.0 # Shift to midpoint of Nth bracket lam, x = inversePower3(d, c, s) # Inverse power method print "Eigenvalue No.", N, " =", lam raw_input("\nPress return to exit")
#!/usr/bin/python ## example9_13 import numpy as np from lamRange import * from inversePower3 import * N = 10 n = 100 d = np.ones(n) * 2.0 c = np.ones(n - 1) * (-1.0) r = lamRange(d, c, N) s = (r[N - 1] + r[N]) / 2.0 lam, x = inversePower3(d, c, s) # Inverse power method print("Eigenvalue No.", N, " =", lam) input("\nPress return to exit")
## example9_13 from numarray import ones from lamRange import * from inversePower3 import * N = 10 n = 100 d = ones((n))*2.0 c = ones((n-1))*(-1.0) r = lamRange(d,c,N) # Bracket N smallest eigenvalues s = (r[N-1] + r[N])/2.0 # Shift to midpoint of Nth bracket lam,x = inversePower3(d,c,s) # Inverse power method print "Eigenvalue No.",N," =",lam raw_input("\nPress return to exit")