【发布时间】:2015-04-01 03:02:07
【问题描述】:
大家好,我在运行代码时不断收到此错误
Traceback (most recent call last):
File "Jacobi.py", line 93, in <module>
limit, guess, statement = jacobi(sys.argv[1], sys.argv[2])
File "Jacobi.py", line 65, in jacobi
guess[0, i] = guess2[0, i]
OverflowError: Python int too large to convert to C long
我在下面发布了我的代码,有人能快速解决吗?只是在随机情况下,它不会一直这样做。有人知道为什么吗?
def jacobi(file_name, tolIn):
if file_name is None:
exit()
#Turns file into one big matrix
A = np.loadtxt(file_name, dtype=int64, delimiter=" ")
aRows = int(A.shape[0])
aCols = int(A.shape[1])
bdim = (aRows, 1)
b = np.zeros(bdim)
s = raw_input("Please provide list of your guess values separated by spaces, e.g. 1 2 3: ")
guess = np.matrix(s)
#Creates nx1 b matrix
for j in range(0, aRows):
b[j,0] = float(A[j, aCols-1])
#创建一个矩阵 elim = [aCols-1] N = np.delete(A, elim, 1) A = N
for i in range (0, A.shape[0]):
for k in range (0, A.shape[0]):
A[i,k] = round((A[i,k]),7)
print(A)
print ('\n')
print(b)
tol = float(tolIn)
#Sets intial previous guess so loop does not stop first try
prevGuess = np.zeros((1, aRows))
for i in range (0, aRows):
prevGuess[0,i] = 1000
guess2 = np.zeros((1, aRows))
limit = 0
aRows = int(A.shape[0])
aCols = int(A.shape[1])
while ((np.linalg.norm(guess - prevGuess) > tol) and limit < 100):
for j in range (0, aRows):
guess2[0,j] = b[j,0]
for i in range (0, aCols):
if (i != j):
guess2[0,j] = round((guess2[0,j] -(A[j,i]) * (guess[0,i])), 7)
guess2[0,j] = round((guess2[0,j] / A[j,j]), 7)
for i in range(0, aCols):
prevGuess[0,i] = guess[0, i]
for i in range(0, aCols-1):
guess[0, i] = round((guess2[0, i]), 7)
guess2 = np.zeros((1, aCols))
limit = limit + 1
#answers mod 2 to get real answers
#for i in range(0,aCols):
# guess[0,i] = guess[0,i]%2
#print ('\n')
#print(limit)
#print ('\n')
#print (guess)
#print ('\n')
【问题讨论】: