tags:

views:

237

answers:

2

I want to convert an int64 numpy array to a uint64 numpy array, adding 2**63 to the values in the process so that they are still within the valid range allowed by the arrays. So for example if I start from

a = np.array([-2**63,2**63-1], dtype=np.int64)

I want to end up with

np.array([0.,2**64], dtype=np.uint64)

Sounds simple at first, but how would you actually do it?

+1  A: 

I'm not a real numpy expert, but this:

>>> a = np.array([-2**63,2**63-1], dtype=np.int64)
>>> b = np.array([x+2**63 for x in a], dtype=np.uint64)
>>> b
array([                   0, 18446744073709551615], dtype=uint64)

works for me with Python 2.6 and numpy 1.3.0

I assume you meant 2**64-1, not 2**64, in your expected output, since 2**64 won't fit in a uint64. (18446744073709551615 is 2**64-1)

Jack Lloyd
+1  A: 

Use astype():

(a+2**63).astype(uint64)
# array([                   0, 18446744073709551615], dtype=uint64)
unutbu