You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
the 'dot' function is designed to work ONLY WITH FLOATING POINT NUMBERS, but it doesn't raise an exception or even a warning when you feed it integer values. This is going to confuse people.
In [239]: a=require(randint(0,2,5000),int8)
In [240]: b=require(randint(0,2,5000),int8)
In [241]: dot(a,b); sum(a*b)
Out[241]: -82
Out[241]: 1198
Let's just say it is a very rare use-case that people want sum(a*b) mod 128.
Your example works fine with int32, see below, so your assertion it works only with floats is false. If one uses int8 the expected return values are also of dtype int8. If you use int8 arrays, it's often because you want to use integer arithmetic with its own rules. So I think this ticket can be closed.
In [3]: from numpy.random import randint
In [4]: a = randint(0, 2, 5000).astype(np.int8)
In [5]: a
Out[5]: array([1, 1, 1, ..., 0, 1, 0], dtype=int8)
In [6]: b = randint(0, 2, 5000).astype(np.int8)
In [8]: np.dot(a, b); sum(a*b)
Out[8]: -13
Out[8]: 1267
In [10]: a = randint(0, 2, 5000).astype(np.int32)
In [11]: b = randint(0, 2, 5000).astype(np.int32)
In [12]: np.dot(a, b); sum(a*b)
Out[12]: 1255
Out[12]: 1255
(Any examples must use numpy's functions.)
This is invalid because dot keeps the input dtype causing an overflow. This can be seen by using the dtype argument:
np.sum(a*b).dtype # dtype('int64') on 64-bit linux
np.sum(a*b, dtype=numpy.int8).dtype # dtype('int8') but wrong answer
By default numpy.sum (and probably Python's sum) uses the platform default and will give the correct answer until that precision overflows.
Activity
thouis commentedon Oct 19, 2012
@rgommers wrote on 2011-03-01
Your example works fine with int32, see below, so your assertion it works only with floats is false. If one uses int8 the expected return values are also of dtype int8. If you use int8 arrays, it's often because you want to use integer arithmetic with its own rules. So I think this ticket can be closed.
thouis commentedon Oct 19, 2012
@bsouthey wrote on 2011-03-30
(Any examples must use numpy's functions.)
This is invalid because dot keeps the input dtype causing an overflow. This can be seen by using the dtype argument:
By default numpy.sum (and probably Python's sum) uses the platform default and will give the correct answer until that precision overflows.
thouis commentedon Oct 19, 2012
@rgommers wrote on 2011-03-30
Not sure if you are saying the same thing as I did here, expecting a floating point warning, or yet something else?