我希望将矩阵乘以大值
这是我的代码
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中遇到溢出ERROR有什么办法可以跳过这个错误?它显示的答案是错的吗?
答案 0 :(得分: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]])