Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy Modular arithmetic

How can I define in numpy a matrix that uses operations modulo 2?

For example:

0 0       1 0       1 0
1 1   +   0 1   =   1 0

Thanks!

like image 301
yassin Avatar asked Nov 07 '25 06:11

yassin


1 Answers

This operation is called "xor".

>>> import numpy
>>> x = numpy.array([[0,0],[1,1]])
>>> y = numpy.array([[1,0],[0,1]])
>>> x ^ y
array([[1, 0],
       [1, 0]])

BTW, (element-wise) multiplication modulo 2 can be done with "and".

>>> x & y
array([[0, 0],
       [0, 1]])
like image 171
kennytm Avatar answered Nov 08 '25 22:11

kennytm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!