[NumPy-Tickets] [NumPy] #1469: Matrix multiplication (dot) fails silently for large integers v1.3.0rc2

NumPy Trac numpy-tickets@scipy....
Sat May 1 06:34:06 CDT 2010


#1469: Matrix multiplication (dot) fails silently for large integers v1.3.0rc2
----------------------+-----------------------------------------------------
  Reporter:  gtpitt   |       Owner:  somebody
      Type:  defect   |      Status:  closed  
  Priority:  normal   |   Milestone:          
 Component:  Other    |     Version:          
Resolution:  invalid  |    Keywords:          
----------------------+-----------------------------------------------------
Changes (by pv):

  * status:  new => closed
  * resolution:  => invalid


Comment:

 Your matrices are made of 32-bit integers,
 {{{
 >>> import numpy as np
 >>> a = np.array([[0,1],[1,-157249]])
 >>> b = np.array([[8762, -28349],[-54857,177487]])
 >>> a.dtype
 dtype('int32')
 }}}

 These are defined on the machine level so that on overflow, the result
 wraps around:
 {{{
 >>> np.int32(1000000) * np.int32(1000000)
 -727379968
 }}}

 If you want floating-point results, you need to use floating point numbers
 in your matrices:
 {{{
 >>> a = a.astype(float)
 >>> np.dot(b, a)
 array([[ -2.83490000e+04,   4.45786066e+09],
        [  1.77487000e+05,  -2.79097081e+10]])
 }}}
 or an integer type large enough to contain your results
 {{{
 >>> a = a.astype(np.int64)
 >>> np.dot(b, a)
 array([[      -28349, 4457860663],
        [      177487, -27909708120]], dtype=int64)
 }}}

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1469#comment:1>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list