Base Conversion For An Array Of Integers
Solution 1:
Here's a version that is vectorized:
import numpy as np
def int2base(x, base, size=None, order='decreasing'):
x = np.asarray(x)
if size is None:
size = int(np.ceil(np.log(np.max(x))/np.log(base)))
if order == "decreasing":
powers = base ** np.arange(size - 1, -1, -1)
else:
powers = base ** np.arange(size)
digits = (x.reshape(x.shape + (1,)) // powers) % basereturn digits
If x
has shape shp
, the result has shape shp + (size,)
.
If size
is not given, the size is based on the largest value in x
. order
determines the order of the digits; use order="decreasing"
(the default) to convert, say, 123 to [1, 2, 3]. Use order="increasing"
to get [3, 2, 1]. (The latter might be more natural, as the index of the digit in the result matches the power of the base for that digit.)
Examples:
In [97]: int2base([255, 987654321], 10)
Out[97]:
array([[0, 0, 0, 0, 0, 0, 2, 5, 5],
[9, 8, 7, 6, 5, 4, 3, 2, 1]])
In [98]: int2base([255, 987654321], 10, size=12)
Out[98]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5, 5],
[0, 0, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1]])
In [99]: int2base([255, 987654321], 10, order="increasing")
Out[99]:
array([[5, 5, 2, 0, 0, 0, 0, 0, 0],
[1, 2, 3, 4, 5, 6, 7, 8, 9]])
In [100]: int2base([255, 987654321], 16)
Out[100]:
array([[ 0, 0, 0, 0, 0, 0, 15, 15],
[ 3, 10, 13, 14, 6, 8, 11, 1]])
Solution 2:
I use the "fromBase10" function (below) a fair amount while I am programming in assembly. Note that it doesn't pad the output, but numpy.vectorize
does work with it. Just don't forget to pad.
## Execute this to convert a base 10 into any arbitrary basedeffromBase10(number, base):
ifnotnumber:return'0'
sign = 1if number > 0else -1
alphanum = string.digits + string.ascii_lowercase
nums = alphanum[:base]
res = ''
number *= sign
whilenumber:
number, mod = divmod(number, base)
res += nums[mod]
return (''if sign == 1else'-') + res[::-1]
Note that I copied the basic routine from someone else on Stack Exchange, but I no longer remember where. I just don't want to claim credit where it isn't mine :)
Solution 3:
Thank you Warren for this vectorized implementation.
I made another vectorized implementation based on yours that is more efficient.
defbase_representation(x, base, size=None):
x = np.asarray(x)
if size isNone:
size = int(np.floor(np.log(np.max(x))/np.log(base)))+1
arrays = []
for _ inrange(size):
x, reminder = np.divmod(x, base)
arrays.append(reminder)
return np.stack(arrays, axis=-1)
Note that a bug on size has been corrected : the formula should be size=floor(log(x)/log(base))+1.
Best regards.
Post a Comment for "Base Conversion For An Array Of Integers"