def householder(a): n = len(a) for k in range(n - 2): u = a[k + 1:n, k] uMag = sqrt(dot(u, u)) if u[0] < 0.0: uMag = -uMag u[0] = u[0] + uMag h = dot(u, u) / 2.0 v = matrixmultiply(a[k + 1:n, k + 1:n], u) / h g = dot(u, v) / (2.0 * h) v = v - g * u a[k+1:n,k+1:n] = a[k+1:n,k+1:n] - outerproduct(v,u) \ - outerproduct(u,v) a[k, k + 1] = -uMag return diagonal(a), diagonal(a, 1)
def householder(a): n = len(a) for k in range(n-2): u = a[k+1:n,k] uMag = sqrt(dot(u,u)) if u[0] < 0.0: uMag = -uMag u[0] = u[0] + uMag h = dot(u,u)/2.0 v = matrixmultiply(a[k+1:n,k+1:n],u)/h g = dot(u,v)/(2.0*h) v = v - g*u a[k+1:n,k+1:n] = a[k+1:n,k+1:n] - outerproduct(v,u) \ - outerproduct(u,v) a[k,k+1] = -uMag return diagonal(a),diagonal(a,1)
def jacobi(a, tol=1.0e-9): # Jacobi method def maxElem(a): # Find largest off-diag. element a[k,l] n = len(a) aMax = 0.0 for i in range(n - 1): for j in range(i + 1, n): if abs(a[i, j]) >= aMax: aMax = abs(a[i, j]) k = i l = j return aMax, k, l def rotate(a, p, k, l): # Rotate to make a[k,l] = 0 n = len(a) aDiff = a[l, l] - a[k, k] if abs(a[k, l]) < abs(aDiff) * 1.0e-36: t = a[k, l] / aDiff else: phi = aDiff / (2.0 * a[k, l]) t = 1.0 / (abs(phi) + sqrt(phi**2 + 1.0)) if phi < 0.0: t = -t c = 1.0 / sqrt(t**2 + 1.0) s = t * c tau = s / (1.0 + c) temp = a[k, l] a[k, l] = 0.0 a[k, k] = a[k, k] - t * temp a[l, l] = a[l, l] + t * temp for i in range(k): # Case of i < k temp = a[i, k] a[i, k] = temp - s * (a[i, l] + tau * temp) a[i, l] = a[i, l] + s * (temp - tau * a[i, l]) for i in range(k + 1, l): # Case of k < i < l temp = a[k, i] a[k, i] = temp - s * (a[i, l] + tau * a[k, i]) a[i, l] = a[i, l] + s * (temp - tau * a[i, l]) for i in range(l + 1, n): # Case of i > l temp = a[k, i] a[k, i] = temp - s * (a[l, i] + tau * temp) a[l, i] = a[l, i] + s * (temp - tau * a[l, i]) for i in range(n): # Update transformation matrix temp = p[i, k] p[i, k] = temp - s * (p[i, l] + tau * p[i, k]) p[i, l] = p[i, l] + s * (temp - tau * p[i, l]) n = len(a) maxRot = 5 * (n**2) # Set limit on number of rotations p = identity(n) * 1.0 # Initialize transformation matrix for i in range(maxRot): # Jacobi rotation loop aMax, k, l = maxElem(a) if aMax < tol: return diagonal(a), p rotate(a, p, k, l) print 'Jacobi method did not converge'
def jacobi(a,tol = 1.0e-9): # Jacobi method def maxElem(a): # Find largest off-diag. element a[k,l] n = len(a) aMax = 0.0 for i in range(n-1): for j in range(i+1,n): if abs(a[i,j]) >= aMax: aMax = abs(a[i,j]) k = i; l = j return aMax,k,l def rotate(a,p,k,l): # Rotate to make a[k,l] = 0 n = len(a) aDiff = a[l,l] - a[k,k] if abs(a[k,l]) < abs(aDiff)*1.0e-36: t = a[k,l]/aDiff else: phi = aDiff/(2.0*a[k,l]) t = 1.0/(abs(phi) + sqrt(phi**2 + 1.0)) if phi < 0.0: t = -t c = 1.0/sqrt(t**2 + 1.0); s = t*c tau = s/(1.0 + c) temp = a[k,l] a[k,l] = 0.0 a[k,k] = a[k,k] - t*temp a[l,l] = a[l,l] + t*temp for i in range(k): # Case of i < k temp = a[i,k] a[i,k] = temp - s*(a[i,l] + tau*temp) a[i,l] = a[i,l] + s*(temp - tau*a[i,l]) for i in range(k+1,l): # Case of k < i < l temp = a[k,i] a[k,i] = temp - s*(a[i,l] + tau*a[k,i]) a[i,l] = a[i,l] + s*(temp - tau*a[i,l]) for i in range(l+1,n): # Case of i > l temp = a[k,i] a[k,i] = temp - s*(a[l,i] + tau*temp) a[l,i] = a[l,i] + s*(temp - tau*a[l,i]) for i in range(n): # Update transformation matrix temp = p[i,k] p[i,k] = temp - s*(p[i,l] + tau*p[i,k]) p[i,l] = p[i,l] + s*(temp - tau*p[i,l]) n = len(a) maxRot = 5*(n**2) # Set limit on number of rotations p = identity(n)*1.0 # Initialize transformation matrix for i in range(maxRot): # Jacobi rotation loop aMax,k,l = maxElem(a) if aMax < tol: return diagonal(a),p rotate(a,p,k,l) print 'Jacobi method did not converge'
#!/usr/bin/python ## example2_7 from numarray import zeros, array, Float64, product, diagonal from LUdecomp import * a = array([[ 3.0, -1.0, 4.0], \ [-2.0, 0.0, 5.0], \ [ 7.0, 2.0, -2.0]]) b = array([[ 6.0, 3.0, 7.0], \ [-4.0, 2.0, -5.0]]) a = LUdecomp(a) det = product(diagonal(a)) print "\nDeterminant =", det for i in range(len(b)): x = LUsolve(a, b[i]) print "x", i + 1, "=", x raw_input("\nPress return to exit")
#!/usr/bin/python ## example2_7 from numarray import zeros,array,Float64,product,diagonal from LUdecomp import * a = array([[ 3.0, -1.0, 4.0], \ [-2.0, 0.0, 5.0], \ [ 7.0, 2.0, -2.0]]) b = array([[ 6.0, 3.0, 7.0], \ [-4.0, 2.0, -5.0]]) a = LUdecomp(a) det = product(diagonal(a)) print "\nDeterminant =",det for i in range(len(b)): x = LUsolve(a,b[i]) print "x",i+1,"=",x raw_input("\nPress return to exit")