5

I have the following simple codes for the sum of two vectors.

However, I get the wrong results when I use NumPy.

The results of the codes is as follows:

In [12]: %run -i test.py

The last 2 elements of the sum [7980015996L, 7992002000L]

The last 2 elements of the sum [-609918596 -597932592]

It's not a long integer, Why?

import numpy as np 
def numpysum(n):
  a = np.arange(n) ** 2 
  b = np.arange(n) ** 3 
  c = a + b

  return c

def pythonsum(n): 
  a = range(n) 
  b = range(n) 
  c = []

  for i in range(len(a)): 
      a[i] = i ** 2 
      b[i] = i ** 3 
      c.append(a[i] + b[i])

  return c

size = 2000

c = pythonsum(size)
print "The last 2 elements of the sum", c[-2:]
c = numpysum(size)
print "The last 2 elements of the sum", c[-2:]

1 Answer 1

4

Plain Python integers can grow arbitrarily large. Numpy integers cannot; they are limited by the size of the data type. If they get too big, they will wrap around and become negative. It looks like your array dtype is probably int32, which overflows and results in negative results. You can get the correct results in this case by using int64:

a = np.arange(n, dtype=np.int64) ** 2 
b = np.arange(n, dtype=np.int64) ** 3

However, it will still overflow eventually (if you make size larger). You could also use float64, which allows even larger numbers, but then you will lose precision.

The cap on integer sizes is the price you pay for the speed numpy gives you.

Sign up to request clarification or add additional context in comments.

3 Comments

Could also use dtype=object.
@StefanPochmann: If you do that, there's not much reason to be using numpy for your computations.
You still get all the cool functionality. Like simply using that ** 3 on the array or a + b of two arrays. Or matrix multiplication as a * b etc. In pure Python that takes more code, which is more work to write and read. NumPy isn't just about execution speed.

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.