-1
C = numpy.array([a^b for a,b in A,B])

Is what I attempted. I thought it would xor each individual element in A and B which are matrices of their own and store it as the same shape within C. How would you do this and where is the flaw in my logic?

EDIT: All values within A and B are ints, an example would be both being shape (3,4) containing a range of integers from 0-10

Direct xor

C = A^B

Resulted in this error:

TypeError: ufunc 'bitwise_xor' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule 'safe'

The TypeError is confusing me as both A and B contain only ints. A I am 100% certain is all ints. B was constructed the following way:

B = np.vstack((A[1:],np.ones(A.shape[1])))

Should this not be all ints as well?

7
  • 4
    How is the title related to the question? Commented Jul 31, 2014 at 13:09
  • Which values are stored in A and B? Give some examples.
    – miindlek
    Commented Jul 31, 2014 at 13:10
  • @lazy1 hahah sorry as terribly phrased as it is it was my best attempt. How would you phrase it? My focus is just on being able to access the individual elements in each in a "pythonic" way to construct the array
    – John Smith
    Commented Jul 31, 2014 at 13:20
  • Post an example for A.
    – miindlek
    Commented Jul 31, 2014 at 13:46
  • @JohnSmith No you didn't. You have posted an example for B, not for A.
    – miindlek
    Commented Jul 31, 2014 at 15:59

3 Answers 3

2

So, your problem is, that np.ones() returns an array containing double values. You can't xor double values with the xor operator from numpy. To solve it, you should use the dtype parameter when creating B, like this example:

import numpy as np

A = np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]])
B = np.vstack((A[1:], np.ones(A.shape[1], dtype=np.int))) # Change this line.

C = A ^ B

Output:

array([[ 5,  7,  5,  3],
       [ 3, 13, 15, 13],
       [ 6,  9,  8, 11]])
2
  • I am still receiving the TypeError :s
    – John Smith
    Commented Jul 31, 2014 at 13:30
  • I'll need a more detailed error and probably example of input. Commented Aug 4, 2014 at 12:35
1

The ^ operator is defined on numpy arrays, if A and B are tuples then:

C = np.array(A) ^ np.array(B)

The xor is done in numpy level and should be super fast.

1
  • Sorry A and B are numpy arrays however I am receiving a TypeError still
    – John Smith
    Commented Jul 31, 2014 at 13:31
0

You are missing the zip to make your current approach work:

C = numpy.array([a ^ b for a, b in zip(A, B)])

but note that there's a much simpler way:

C = A ^ B

Demo:

>>> A = np.array((1, 2, 3, 4))
>>> B = np.array((2, 3, 1, 4))
>>> A ^ B
array([3, 1, 2, 0])
2
  • I am still receiving a TypeError, if you could look at the edit on my original post I would really appreciate it
    – John Smith
    Commented Jul 31, 2014 at 13:34
  • @JohnSmith instead of bits and pieces of information, please provide a complete, minimal example of the code you're running along with full error traceback, so that others can replicate the issues you're seeing.
    – jonrsharpe
    Commented Jul 31, 2014 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.