我的目标是编写一个执行两个数组的模2乘法的函数(乘法=和,加= xor)。这是我现在的代码。关于如何改进这一点的任何建议都会感激不尽 - 特别是,我/我如何将列表理解直接转化为适当形状的数组?
import numpy as np
import operator
def m2mult(a,b):
c = np.ndarray([reduce(operator.xor, np.logical_and(a[x,:],b[:,y]))
for x in range(0,a.shape[0]) for y in range (0, b.shape[1])])
c.shape = (a.shape[0], b.shape[1])
return c
答案 0 :(得分:4)
你不应该这样做:
a = np.random.randint(0,2,(4,4))
b = np.random.randint(0,2,(4,4))
# Now note that this is true:
# (I will let you figure that out, its a lot of neat broadcasting.
# b.T would be enough as well, because of it)
np.multiply(a[:,None,:], b.T[None,:,:]).sum(-1) == np.dot(a,b)
# note also that .sum(-1) is the same as np.add.reduce(array, axis=-1)
# now we can do the same thing for your logical operations:
a = np.random.randint(0,2,(4,4)).astype(bool)
b = np.random.randint(0,2,(4,4)).astype(bool)
def m2mult(a, b):
mult = np.logical_and(a[:,None,:], b.T[None,:,:])
return np.logical_xor.reduce(mult, axis=-1)
它完全矢量化,速度快,刚刚好![/ p>
答案 1 :(得分:2)
您可以在int
s上执行常见的矩阵乘法,然后进行模2减少,然后转换回bool
:
np.mod(np.dot(a.astype('u1'), b), 2).astype('bool')
它比seberg的解决方案和Jaime对它的修改更快。
+---------------+---------+-----------+----------+
| | 10x10 | 1000x1000 | 750x1250 |
+---------------+---------+-----------+----------+
| m2mult_tz | 33 us | 7.27 s | 4.68 s |
| m2mult_jaime | 56.7 us | 20.4 s | 14.2 s |
| m2mult_seberg | 62.9 us | 20.5 s | 14.3 s |
+---------------+---------+-----------+----------+
对于非常大的数组或者如果您的程序执行此操作,这可能是一个问题 我把这种方法和塞格的解决方案及其对Jaime的修改提出来 以下是我实现不同功能的方法:
import numpy as np
def create_ab(n, m):
a = np.random.randint(0, 2, (n, m)).astype(bool)
b = np.random.randint(0, 2, (m, n)).astype(bool)
return a, b
def m2mult_tz(a, b):
return np.mod(np.dot(a.astype('u1'), b), 2).astype(bool)
def m2mult_seberg(a, b):
return np.logical_xor.reduce(
np.logical_and(a[:,None,:], b.T[None,:,:]),
axis=-1)
def m2mult_jaime(a, b):
return np.logical_xor.reduce(
np.logical_and(a[:, :, None], b),
axis=1)
这里是1000x1000时间的记录(我还检查过结果在所有情况下都是相同的):
In [19]: a, b = create_ab(1000, 1000)
In [20]: timeit m2mult_tz(a, b)
1 loops, best of 3: 7.27 s per loop
In [21]: timeit m2mult_jaime(a, b)
1 loops, best of 3: 20.4 s per loop
In [22]: timeit m2mult_seberg(a, b)
1 loops, best of 3: 20.5 s per loop
In [23]: r_tz = m2mult_tz(a, b)
In [24]: r_jaime = m2mult_jaime(a, b)
In [25]: r_seberg = m2mult_seberg(a, b)
In [26]: np.all(r_tz == r_jaime)
Out[26]: True
In [27]: np.all(r_tz == r_seberg)
Out[27]: True