Skip to content

integer dot product (Trac #1192) #1790

Closed

Description

@thouis

Original ticket http://projects.scipy.org/numpy/ticket/1192 on 2009-08-11 by trac user jloper, assigned to unknown.

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.

Activity

  1. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    @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.

    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
    
  2. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    @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:

    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.

  3. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    @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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions