2015-05-19 17 views
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遇到錯誤溢出是沒有辦法跳過此錯誤?它顯示的答案是錯誤的?

+0

人誰比我講好NumPy的能確定這是否是使用INT32的或Int64類型的?如果前者,OP可能只是擴大了他們並完成了它。當我們在這,OP應該可能只是使用'ndarray.dot()'做矩陣乘法... – Kevin

+0

@Kevin沒有問題,但我不得不使用這個函數,我想如果我做矩陣值int64它會好的,但我不知道該怎麼做! – user3275165

+0

您可以使用['astype()'](http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html#numpy.ndarray.astype)將數組強制轉換爲int64。在你做矩陣乘法之前。 – Kevin

回答

0

繁殖數組你可以使用numpy.dot(A,B)

A=numpy.random.randint(-1000,1000,size=(1000,1000)) 
B=numpy.random.randint(-5000,5000,size=(1000,1000)) 
C=numpy.dot(A,B) 
>> array([[ -19276757, -143164893, -46042782, ..., 54192473, -11723491, 
     -24682938], 
     [ -13712042, -81161007, -10673794, ..., -60976103, 12684946, 
     -38034048], 
     [ -33019063, -31452735, 6885214, ..., -11402430, -67335345, 
      28926860], 
     ..., 
     [ -75416394, -86704514, 55883100, ..., -9195721, 14717256, 
      1028573], 
     [ -73945244, 72442826, -38529094, ..., -1450784, 34663409, 
     -88472364], 
     [ -21366137, 91174501, 32863977, ..., 11465498, 57130349, 
      47305071]]) 
+0

我知道,但我不想使用這些功能,我只是想比較你說的方式和我的方式表現,但我的方式有一些問題 – user3275165