﻿ python code for solving eigenvalue problem by Jacobi’s method_python_开心洋葱网
• 欢迎访问开心洋葱网站，在线教程，推荐使用最新版火狐浏览器和Chrome浏览器访问本网站，欢迎加入开心洋葱` QQ群`
• 为方便开心洋葱网用户，开心洋葱官网已经开启复制功能！
• 欢迎访问开心洋葱网站，手机也能访问哦~欢迎加入开心洋葱多维思维学习平台` QQ群`
• 如果您觉得本站非常有看点，那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~！
• 由于近期流量激增，小站的ECS没能经的起亲们的访问，本站依然没有盈利，如果各位看如果觉着文字不错，还请看官给小站打个赏~~~~~~~~~~~~~！

# python code for solving eigenvalue problem by Jacobi’s method

1739次浏览

python code for solving eigenvalue problem by Jacobi’s method

```''' lam,x = jacobi(a,tol = 1.0e-9).
Solution of std. eigenvalue problem [a]{x} = lam{x}
by Jacobi's method. Returns eigenvalues in vector {lam}
and the eigenvectors as columns of matrix [x].
'''
from numpy import array,identity,diagonal
from math import sqrt

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)
else:
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'
```

[开心洋葱]

• 版权声明

本站的文章和资源来自互联网或者站长的原创，按照 CC BY -NC -SA 3.0 CN协议发布和共享，转载或引用本站文章应遵循相同协议。如果有侵犯版权的资源请尽快联系站长，我们会在24h内删除有争议的资源。
• 合作网站

• 友情链接

• 关于我们

一群热爱思考，热爱生活，有理想的新社会主义接班人的多维思维学习平台，天行健，君子以自强不息。地势坤，君子以厚德载物。
……