0
我想與大值
這裏繁衍矩陣是我的代碼RuntimeWarning:在矩陣中long_scalars遇到的溢出乘
import time
import numpy
def mm(mtx_a, mtx_b):
tpos_b = zip(*mtx_b)
rtn = [[ sum(ea*eb for ea,eb in zip(a,b)) for b in tpos_b] for a in mtx_a]
return rtn
print ("first part of project of OS")
N=input("select the size of N*N matrix by entering the power of 10 \n")
N=10**N
startTime = time.time()
try:
P=numpy.random.randint(-100,100,size=(N,N))
Q=numpy.random.randint(-100,100,size=(N,N))
C=mm(P,Q)
except MemoryError:
print("Memory error")
try:
A=numpy.random.randint(-1000,1000,size=(N,N))
B=numpy.random.randint(-5000,5000,size=(N,N))
except MemoryError:
print("Memory error")
try:
D=mm(A,B)
D=mm (D,C)
print D
except NameError:
print("name error")
elapsedTime = time.time() - startTime
print elapsedTime
和我RuntimeWarning:在long_scalars遇到錯誤溢出是沒有辦法跳過此錯誤?它顯示的答案是錯誤的?
人誰比我講好NumPy的能確定這是否是使用INT32的或Int64類型的?如果前者,OP可能只是擴大了他們並完成了它。當我們在這,OP應該可能只是使用'ndarray.dot()'做矩陣乘法... – Kevin
@Kevin沒有問題,但我不得不使用這個函數,我想如果我做矩陣值int64它會好的,但我不知道該怎麼做! – user3275165
您可以使用['astype()'](http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html#numpy.ndarray.astype)將數組強制轉換爲int64。在你做矩陣乘法之前。 – Kevin