<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 12 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">Hello,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">One of the test cases in test_einsum causes integer overflow for i2 type. The test goes like this:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">>>> import numpy as np<o:p></o:p></p>
<p class="MsoNormal">>>> dtype = 'i2'<o:p></o:p></p>
<p class="MsoNormal">>>> n = 15<o:p></o:p></p>
<p class="MsoNormal">>>> a = np.arange(4*n, dtype=dtype).reshape(4,n)<o:p></o:p></p>
<p class="MsoNormal">>>> b = np.arange(n*6, dtype=dtype).reshape(n,6)<o:p></o:p></p>
<p class="MsoNormal">>>> c = np.arange(24, dtype=dtype).reshape(4,6)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">It then calculates AxB using einsum. The problem is that the values in the last row of the result do not fit into i2:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">>>> np.einsum("ij,jk", a, b, dtype='f8', casting='unsafe')<o:p></o:p></p>
<p class="MsoNormal">array([[  6090.,   6195.,   6300.,   6405.,   6510.,   6615.],<o:p></o:p></p>
<p class="MsoNormal">       [ 15540.,  15870.,  16200.,  16530.,  16860.,  17190.],<o:p></o:p></p>
<p class="MsoNormal">       [ 24990.,  25545.,  26100.,  26655.,  27210.,  27765.],<o:p></o:p></p>
<p class="MsoNormal">       [ 34440.,  35220.,  36000.,  36780.,  37560.,  38340.]])<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In my build this produces different results depending on whether out or .astype is used:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">>>> np.einsum("ij,jk", a, b, dtype='f8', casting='unsafe').astype(dtype)<o:p></o:p></p>
<p class="MsoNormal">array([[  6090,   6195,   6300,   6405,   6510,   6615],<o:p></o:p></p>
<p class="MsoNormal">       [ 15540,  15870,  16200,  16530,  16860,  17190],<o:p></o:p></p>
<p class="MsoNormal">       [ 24990,  25545,  26100,  26655,  27210,  27765],<o:p></o:p></p>
<p class="MsoNormal">       [-31096, -30316, -29536, -28756, -27976, -27196]], dtype=int16)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">>>> np.einsum("ij,jk", a, b, out=c, dtype='f8', casting='unsafe')<o:p></o:p></p>
<p class="MsoNormal">array([[  6090,   6195,   6300,   6405,   6510,   6615],<o:p></o:p></p>
<p class="MsoNormal">       [ 15540,  15870,  16200,  16530,  16860,  17190],<o:p></o:p></p>
<p class="MsoNormal">       [ 24990,  25545,  26100,  26655,  27210,  27765],<o:p></o:p></p>
<p class="MsoNormal">       [-32768, -32768, -32768, -32768, -32768, -32768]], dtype=int16)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The test wants these (actually the same using numpy.dot) to be equal, so this difference causes it to fail. Both ways to handle overflow seem reasonable to me.
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Does numpy in general assign a defined behavior to integer overflow (e.g. two’s complement)?<o:p></o:p></p>
<p class="MsoNormal">Is this use of integer overflow in the test intentional and is expected to work, or is my build broken?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Best regards,<o:p></o:p></p>
<p class="MsoNormal">Eugene<o:p></o:p></p>
</div>
</body>
</html>

<p><span STYLE="color :#000000;background-color :#FFFFFF">
This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.  
</span></p>