我需要提高算法的速度。该方法将两个矩阵作为参数并执行点乘。唯一的问题是元素必须在 GF(256) 中作为八位字节相乘,然后作为异或相加。由于我正在处理非常大的稀疏矩阵,因此性能很糟糕。
def matrix_oct_mult(U, V, OCT_EXP, OCT_LOG):
temp_sum = 0
if shape(U)[1] == None and shape(V)[1] == None:
for i in range(len(U)):
temp_sum = oct_sum(temp_sum, oct_mult(U[i], V[i], OCT_EXP, OCT_LOG))
return temp_sum
assert shape(U)[1] == shape(V)[0], "Wrong size requirements for matrix dot multiplication"
temp_sum = 0
W = sp.lil_matrix((shape(U)[0], shape(V)[1]))
for i in range (shape(U)[0]):
for z in range(shape(V)[1]):
for j in range (shape(U)[1]):
temp_sum = oct_sum(temp_sum, oct_mult(U[i, j], V[j, z], OCT_EXP, OCT_LOG))
W[i, z] = temp_sum
temp_sum = 0
return W
如您所见,我尝试使用 lil 类,但性能仍然很差。
有什么有效的解决方法吗?