使用numpy数组左移1

时间:2021-10-19 18:20:41

Python can indeed left shift a bit by large integers

Python确实可以对大整数稍微左移一点

1L << 100
# 1267650600228229401496703205376L

But NumPy seemingly has a problem:

但NumPy似乎有一个问题:

a = np.array([1,2,100])
output = np.left_shift(1L,a)
print output 
# array([          2,           4, 68719476736])

Is there a way to overcome this using NumPy's left_shift operation? Individually accessing the array elements gives the same incorrect result:

有没有一种方法可以通过NumPy的left_shift操作来解决这个问题?单独访问数组元素会得到相同的错误结果:

1L << a[2]
# 68719476736

3 个解决方案

#1


2  

Python long values aren't the same type as the integers held in a. Specifically, Python long values are not limited to 32 bits or 64 bits but instead can take up an arbitrary amount of memory.

Python long值与a中的整数不同,具体来说,Python long值不限于32位或64位,而是可以占用任意数量的内存。

On the other hand, NumPy will create a as an array of int32 or int64 integer values. When you left-shift this array, you get back an array of the same datatype. This isn't enough memory to hold 1 << 100 and the result of the left-shift overflows the memory it's been allocated in the array, producing the incorrect result.

另一方面,NumPy将创建一个int32或int64整数值的数组。当你左移这个数组时,你会得到一个相同数据类型的数组。这并没有足够的内存来容纳1 < 100,而且左移的结果会溢出它在数组中分配的内存,从而产生错误的结果。

To hold integers that large, you'll have to specify a to have the object datatype. For example:

要保持这么大的整数,必须指定a才能具有对象数据类型。例如:

>>> np.left_shift(1, a.astype(object))
array([2, 4, 1267650600228229401496703205376L], dtype=object)

object arrays can hold a mix of different types, including the unlimited-size Python long/integer values. However, a lot of the performance benefits of homogeneous datatype NumPy arrays will be lost when using object arrays.

对象数组可以包含不同类型的组合,包括无限制大小的Python long/integer值。但是,在使用对象数组时,同构数据类型NumPy数组的许多性能优势将会丢失。

#2


1  

Given the array a you create, the elements of output are going to be integers. Unlike Python itself, integers in a numpy darray are of fixed size with a defined maximum and minimum value.

给定创建的数组a,输出的元素将是整数。与Python本身不同,numpy darray中的整数的大小是固定的,具有定义的最大值和最小值。

numpy is arriving at the value given (2 ** 36) by reducing the shift modulo the length of the integer, 64 bits (100 mod 64 == 36).

numpy通过减少这个整数的移位模的长度(64位(100 mod 64 = 36)达到给定值(2 ** 36)。

#3


1  

This is an issue with 64-bit Python too, except that the integer at which this becomes a problem is larger. if you run a[2]=1L << a[2] you will get this Traceback :

对于64位Python来说,这也是一个问题,只是问题所在的整数要大一些。如果你运行一个[2]=1L < a[2],你会得到这个回溯:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long

So as the traceback says Python int too large to convert to C long , so you need to change the array type (that is actually C structure ) to an python object :

因此,正如traceback所说,Python int太大,无法转换成C长,所以您需要将数组类型(实际上是C结构)更改为Python对象:

>>> a = np.array([1,2,100],dtype='O')
>>> a[2]=1L << a[2]
>>> a
array([1, 2, 1267650600228229401496703205376L], dtype=object)

#1


2  

Python long values aren't the same type as the integers held in a. Specifically, Python long values are not limited to 32 bits or 64 bits but instead can take up an arbitrary amount of memory.

Python long值与a中的整数不同,具体来说,Python long值不限于32位或64位,而是可以占用任意数量的内存。

On the other hand, NumPy will create a as an array of int32 or int64 integer values. When you left-shift this array, you get back an array of the same datatype. This isn't enough memory to hold 1 << 100 and the result of the left-shift overflows the memory it's been allocated in the array, producing the incorrect result.

另一方面,NumPy将创建一个int32或int64整数值的数组。当你左移这个数组时,你会得到一个相同数据类型的数组。这并没有足够的内存来容纳1 < 100,而且左移的结果会溢出它在数组中分配的内存,从而产生错误的结果。

To hold integers that large, you'll have to specify a to have the object datatype. For example:

要保持这么大的整数,必须指定a才能具有对象数据类型。例如:

>>> np.left_shift(1, a.astype(object))
array([2, 4, 1267650600228229401496703205376L], dtype=object)

object arrays can hold a mix of different types, including the unlimited-size Python long/integer values. However, a lot of the performance benefits of homogeneous datatype NumPy arrays will be lost when using object arrays.

对象数组可以包含不同类型的组合,包括无限制大小的Python long/integer值。但是,在使用对象数组时,同构数据类型NumPy数组的许多性能优势将会丢失。

#2


1  

Given the array a you create, the elements of output are going to be integers. Unlike Python itself, integers in a numpy darray are of fixed size with a defined maximum and minimum value.

给定创建的数组a,输出的元素将是整数。与Python本身不同,numpy darray中的整数的大小是固定的,具有定义的最大值和最小值。

numpy is arriving at the value given (2 ** 36) by reducing the shift modulo the length of the integer, 64 bits (100 mod 64 == 36).

numpy通过减少这个整数的移位模的长度(64位(100 mod 64 = 36)达到给定值(2 ** 36)。

#3


1  

This is an issue with 64-bit Python too, except that the integer at which this becomes a problem is larger. if you run a[2]=1L << a[2] you will get this Traceback :

对于64位Python来说,这也是一个问题,只是问题所在的整数要大一些。如果你运行一个[2]=1L < a[2],你会得到这个回溯:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long

So as the traceback says Python int too large to convert to C long , so you need to change the array type (that is actually C structure ) to an python object :

因此,正如traceback所说,Python int太大,无法转换成C长,所以您需要将数组类型(实际上是C结构)更改为Python对象:

>>> a = np.array([1,2,100],dtype='O')
>>> a[2]=1L << a[2]
>>> a
array([1, 2, 1267650600228229401496703205376L], dtype=object)