有一个笨拙的biginteger吗? [英] Is there a numpy biginteger?
问题描述
嗯.在我看来,似乎没有办法将python的bigintegers存储在numpy数组中.要声明一个带有bigints的numpy数组,有什么特别的事情吗?
Hmm. There doesn't seem to me a way to store Python's bigintegers in a numpy array. Is there something special you have to do, to declare a numpy array with bigints?
推荐答案
不是特别的,不是.您可以使用dtype='object'
创建一个数组,该数组创建一个Python对象数组(包括但不限于ints).这将为您带来许多类似Numpy数组的功能,但几乎没有性能优势.
Not specifically, no. You can create an array with dtype='object'
, which creates an array of Python objects (including but not limited to ints). This will get you a lot of Numpy array-like functionality but few to none of the performance benefits.
这就是说,Python对象数组在内存性能方面与Python list
并无显着差异.尽管如果必须使用bigints,可能仍然比使用list
更可取,因为您仍然可以进行按元素进行算术运算,包括使用其他Numpy数组进行运算时.例如:
Which is to say, an array of Python objects is not significantly different from a Python list
in terms of memory performance. Though if you must use bigints it may still be preferable to using a list
since you still get element-wise arithmetic operations, including when doing operations with other Numpy arrays. For example:
In [1]: import numpy as np
In [2]: big = np.array([10**100, 10**101, 10**102], dtype='object')
In [3]: big
Out[3]:
array([ 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000], dtype=object)
In [4]: big + np.array([1, 2, 3])
Out[4]:
array([ 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001,
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002,
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003], dtype=object)
尽管我自己从未使用过此功能,所以我不确定是否还会出现其他令人惊讶的局限性.
I've never used this capability myself though, so I'm not entirely sure what other surprising limitations might arise.
这篇关于有一个笨拙的biginteger吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!