4

It is apparent that NumPy has an upper bound for its integers. But my question is, is there a way to store the elements in NumPy arrays, like by keeping the values and the magnitudes separate? Wouldn't that technically allow storing of larger numbers than what the int64 limit allows?

  • 1
    "keeping the values and the magnitudes separate"... In what way is this different to a floating point value? – Jake Levi Jul 05 '20 at 12:59
  • 1
    Since numpy arrays can effectively store bytes, arbitrary data can be stored. That does not make it sensible, though. What specific problem are you trying to *solve* by storing larger numbers? Which *operations* do you want to perform on these numbers? – MisterMiyagi Jul 05 '20 at 13:05
  • the usual operations like multiplication, division, addition, and subtraction –  Jul 05 '20 at 13:41
  • Do you expect these "usual operations" to also behave like the usual *numpy* operations? I.e. do you implicitly assume they provide speed, vectorisation, ease of broadcasting, and such as well? – MisterMiyagi Jul 05 '20 at 15:22
  • `object` dtype can hold arbitrary numeric objects - python ints, `mpmath`, `sympy`. Math will be slower (comparable to list compreprehensions), and limited. – hpaulj Jul 05 '20 at 15:38
  • i don't think they'll function like the usual numpy operations, but maybe that would be possible by making an exclusive class with the help of numpy –  Jul 05 '20 at 16:26
  • Does this answer your question? [numpy arbitrary precision linear algebra](https://stackoverflow.com/questions/6876377/numpy-arbitrary-precision-linear-algebra) – Marcin Kuropatwiński Jul 05 '20 at 16:37
  • @RocknRollDelta Okay, so if these types do *not* necessarily function like the usual numpy operations – what are you trying to achieve, then? What *should* these containers and numbers provide? Python already has arbitrary precision integer and rational number types. Python already has arbitrary element containers. How does numpy contribute here? Even if we go for "useable with the numpy ecosystem", what is the difference to the``object`` dtype? – MisterMiyagi Jul 05 '20 at 17:51
  • oh the object dtype is what i was looking for, thanks! –  Jul 05 '20 at 18:02

1 Answers1

0

for example you can store arbitrary precision integers in numpy array using dtype = object and perform addition, multiplication, element-wise multiplication, subtraction and integer division but not operations, which lead to float results, for example np.exp(x) wont work.

x = np.ones((10,10),dtype=object)

x *= 2**100

x *= x

print(x)

if you want truly arbitrary precision arithmetic matrix classes I would implement on my own with proper operator overload with help of mpmath