Numpy模块化算术

yas*_*sin 2 python math numpy modular

如何在numpy中定义一个使用模2运算的矩阵?

例如:

0 0       1 0       1 0
1 1   +   0 1   =   1 0
Run Code Online (Sandbox Code Playgroud)

谢谢!

ken*_*ytm 7

此操作称为"xor".

>>> import numpy
>>> x = numpy.array([[0,0],[1,1]])
>>> y = numpy.array([[1,0],[0,1]])
>>> x ^ y
array([[1, 0],
       [1, 0]])
Run Code Online (Sandbox Code Playgroud)

BTW,(逐元素)乘法模2可以用"和"来完成.

>>> x & y
array([[0, 0],
       [0, 1]])
Run Code Online (Sandbox Code Playgroud)