1

Since python int can grow arbitrarily large (i.e. can compute numbers such as 10 ** 100000), I would like to utilize this feature with NumPy and be able to compute things such as

x = np.arange(10, dtype=np.pyint)
print(x ** 1000)

and have it compute and print the full value for each entry in the range, can that be done?

1
  • Use dtype=object and above will work out. Guaranteeing that all kinds of operators are working as expected though, well, i can't do that. Commented Feb 8, 2021 at 14:32

1 Answer 1

2

With object dtype:

In [44]: x=np.arange(3, dtype=object)
In [45]: x**1000
Out[45]: 
array([0, 1,
       10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376],
      dtype=object)

But be ware that in terms of performance this is the equivalent of:

In [46]: [i**1000 for i in range(3)]
Out[46]: 
[0,
 1,
 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376]

In [51]: timeit [i**1000 for i in range(300)]
5.42 ms ± 111 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [52]: timeit np.arange(300,dtype=object)**1000
5.42 ms ± 111 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Sign up to request clarification or add additional context in comments.

1 Comment

Good point, the limitations of numpy are what allow it to be faster than raw Python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.