Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

python - Is there a numpy biginteger?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...