Python int太大而无法转换为C长代码需要修复

时间:2015-04-01 03:02:08

标签: python matrix overflow

嘿伙计们,当我运行我的代码时,我一直收到这个错误

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])

#Creates一个矩阵     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')

1 个答案:

答案 0 :(得分:0)

我相信range()要求最大数量包含在c整数范围内。如果您想解决此问题,可以使用itertools.count()。它从0开始(或作为参数传递的数字)并永远计数(或直到你中断)。

示例:

for i in itertools.count():
    if i == 10:
        break
    print i

docs:https://docs.python.org/2/library/itertools.html#itertools.count