def curvatures(xData, yData): n = len(xData) - 1 c = np.zeros((n), dtype=float) d = np.ones((n + 1), dtype=float) e = np.zeros((n), dtype=float) k = np.zeros((n + 1), dtype=float) c[0:n - 1] = xData[0:n - 1] - xData[1:n] d[1:n] = 2.0 * (xData[0:n - 1] - xData[2:n + 1]) e[1:n] = xData[1:n] - xData[2:n + 1] k[1:n] = 6.0*(yData[0:n-1] - yData[1:n]) /c[0:n-1] \ -6.0*(yData[1:n] - yData[2:n+1]) /e[1:n] LUdecomp3.LUdecomp3(c, d, e) LUdecomp3.LUsolve3(c, d, e, k) return k
def inversePower3(d, c, s, tol=1.0e-6): n = len(d) e = c.copy() cc = c.copy() # Save original [c] dStar = d - s # Form [A*] = [A] - s[I] LUdecomp3(cc, dStar, e) # Decompose [A*] x = zeros((n), type=Float64) for i in range(n): # Seed [x] with random numbers x[i] = random() xMag = sqrt(dot(x, x)) # Normalize [x] x = x / xMag flag = 0 for i in range(30): # Begin iterations xOld = x.copy() # Save current [x] LUsolve3(cc, dStar, e, x) # Solve [A*][x] = [xOld] xMag = sqrt(dot(x, x)) # Normalize [x] x = x / xMag if dot(xOld, x) < 0.0: # Detect change in sign of [x] sign = -1.0 x = -x else: sign = 1.0 if sqrt(dot(xOld - x, xOld - x)) < tol: return s + sign / xMag, x print 'Inverse power method did not converge'
def curvatures(xData, yData): n = len(xData) - 1 c = np.zeros(n) d = np.ones(n + 1) e = np.zeros(n) k = np.zeros(n + 1) c[0:n - 1] = xData[0:n - 1] - xData[1:n] d[1:n] = 2.0 * (xData[0:n - 1] - xData[2:n + 1]) e[1:n] = xData[1:n] - xData[2:n + 1] k[1:n] =6.0*(yData[0:n-1] - yData[1:n])/(xData[0:n-1] - xData[1:n]) \ - 6.0*(yData[1:n] - yData[2:n+1])/(xData[1:n] - xData[2:n+1]) LUdecomp3(c, d, e) LUsolve3(c, d, e, k) return k
def curvatures(xData, yData): n = len(xData) - 1 c = zeros((n), type=Float64) d = ones((n + 1), type=Float64) e = zeros((n), type=Float64) k = zeros((n + 1), type=Float64) c[0:n - 1] = xData[0:n - 1] - xData[1:n] d[1:n] = 2.0 * (xData[0:n - 1] - xData[2:n + 1]) e[1:n] = xData[1:n] - xData[2:n + 1] k[1:n] =6.0*(yData[0:n-1] - yData[1:n]) \ /(xData[0:n-1] - xData[1:n]) \ -6.0*(yData[1:n] - yData[2:n+1]) \ /(xData[1:n] - xData[2:n+1]) LUdecomp3(c, d, e) LUsolve3(c, d, e, k) return k
def inversePower3(d, c, s, tol=1.0e-6): n = len(d) e = c.copy() dStar = d - s LUdecomp3(c, dStar, e) x = rand(n) xMag = math.sqrt(np.dot(x, x)) # Normalize [x] x = x / xMag flag = 0 for i in range(30): xOld = x.copy() LUsolve3(c, dStar, e, x) xMag = math.sqrt(np.dot(x, x)) # Normalize [x] x = x / xMag if np.dot(xOld, x) < 0.0: # Detect change in sign of [x] sign = -1.0 x = -x else: sign = 1.0 if math.sqrt(np.dot(xOld - x, xOld - x)) < tol: return s + sign / xMag, x print("Inverse power method did not converge")
def inversePower3(d, c, s, tol=1.0e-6): n = len(d) e = c.copy() cc = c.copy() dStar = d - s # Form [A*] = [A] - s[I] LUdecomp3(cc, dStar, e) # Decompose [A*] x = rand(n) # Seed x with random numbers xMag = math.sqrt(np.dot(x, x)) # Normalize [x] x = x / xMag for i in range(30): # Begin iterations xOld = x.copy() # Save current [x] LUsolve3(cc, dStar, e, x) # Solve [A*][x] = [xOld] xMag = math.sqrt(np.dot(x, x)) # Normalize [x] x = x / xMag if np.dot(xOld, x) < 0.0: # Detect change in sign of [x] sign = -1.0 x = -x else: sign = 1.0 if math.sqrt(np.dot(xOld - x, xOld - x)) < tol: return s + sign / xMag, x print('Inverse power method did not converge') return
from numarray import zeros, ones, Float64, array, arange from LUdecomp3 import * from math import pi def equations(x, h, m): # Set up finite difference eqs. h2 = h * h d = ones((m + 1)) * (-2.0 + 4.0 * h2) c = ones((m), type=Float64) e = ones((m), type=Float64) b = ones((m + 1)) * 4.0 * h2 * x d[0] = 1.0 e[0] = 0.0 b[0] = 0.0 c[m - 1] = 2.0 return c, d, e, b xStart = 0.0 # x at left end xStop = pi / 2.0 # x at right end m = 10 # Number of mesh spaces h = (xStop - xStart) / m x = arange(xStart, xStop + h, h) c, d, e, b = equations(x, h, m) c, d, e = LUdecomp3(c, d, e) y = LUsolve3(c, d, e, b) print "\n x y" for i in range(m + 1): print "%14.5e %14.5e" % (x[i], y[i]) raw_input("\nPress return to exit")
#!/usr/bin/python ## example2_14 import numpy as np from LUdecomp3 import * n=6 d = np.ones((n))*2.0 e = np.ones((n-1))*(-1.0) c = e.copy() d[n-1] = 5.0 aInv = np.identity(n) c,d,e = LUdecomp3(c,d,e) for i in range(n): aInv[:,i] = LUsolve3(c,d,e,aInv[:,i]) print("\nThe inverse matrix is:\n",aInv) input("\nPress return to exit")
def inversePower3(d, c, s, tol=1.0e-6, max_iterations=100): """ Inverse power method for triagonal matrix. Inverse power method applied to a tridiagonal matrix [A] = [c\d\c]. Returns the eigenvalue closest to 's' and the corresponding eigenvector. Parameters ---------- d : a numpy array of ndim = 1. The matrix in the diagonal. c : a numpy array of ndim = 1. The matrix in the second-diagonal. s : a double. The eigenvalue you want to find its eigenvector. This is an approximate value. tol : a double (optional). The overlap between consecutive solutions has to be smaller than that to call the eigenvector converged. max_iterations : an int. The maximum number of iteration done before raising an exception. Returns ------- s : a double. The eigenvalue corrected. x : a numpy array of ndim = 1. The corresponding eigenvector. Raises ------ DMRGException if the number of iterations before converging exceeds `max_iterations`. """ n = len(d) e = c.copy() cc = c.copy() # Save original [c] dStar = d - s # Form [A*] = [A] - s[I] LUdecomp3(cc,dStar,e) # Decompose [A*] x = np.random.rand(n) # Seed [x] with random numbers norm = np.linalg.norm(x) # Normalize [x] x /= norm is_converged = False iteration = 0 sign = 1.0 while not is_converged: # Begin iterations iteration +=1 if iteration > max_iterations: raise TridiagonalException('Inverse power method did not converge') xOld = x.copy() # Save current [x] LUsolve3(cc,dStar,e,x) # Solve [A*][x] = [xOld] norm = np.linalg.norm(x) # Normalize [x] x /= norm if np.dot(xOld,x) < 0.0: # Detect change in sign of [x] sign = -1.0 x = -x overlap = np.linalg.norm(xOld - x) if overlap < tol: is_converged =True s += sign/norm return s, x