Question

I am not sure how to multiply two matrices using the XOR addition. For example, here:

>>> b = numpy.array([[1, 0, 0, 0, 1, 1, 0],
                     [0, 1, 0, 0, 0, 1, 1],
                     [0, 0, 1, 0, 1, 1, 1],
                     [0, 0, 0, 1, 1, 0, 1]])
>>> z = numpy.array([1, 1, 0, 1])
>>> z.dot(b)
array([1, 1, 0, 1, 2, 2, 2])

I would like the 4th, 5th, and 6th indices of the resulting array to be computed by:

1(1) xor 0(1) xor 1(0) xor 1(1) = 0
1(1) xor 1(1) xor 1(0) xor 0(1) = 0
0(1) xor 1(1) xor 1(0) xor 1(1) = 0

Any suggestions?

Was it helpful?

Solution

As I commented, you can use z.dot(b) % 2 to get the values you want. This is because chained xors are equivalent to addition mod 2. That is, the result will be 1 if the number of 1s was odd, and 0 if it was even.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top