Product of two equals numpy arrays are different
<p>I'm facing very strange problem with arrays in python and numpy. First of all what Im trying to archive is : </p>

<p>1) Get an MxN matrix from KxTxN matrix 
2) Transpose this matrix and calculate product of this transposed matrix and the original one</p>

<p>What I get is some what strange, here comes the code : </p>

<p>First of all, I have read an image with help of cv2, and got K by T by 3 matrix (a field of RGB points), then I'm cutting a small window form it and reshaping this window to M by N matrix : </p>

<pre><code>def clipSubwindowFromImage(img, i, j, winSize):
    winI = img[i - winSize: i + winSize + 1, j - winSize : j + winSize + 1, : ]
    res = np.vstack((winI[:,::3,:].reshape(winI.shape[1],3), winI[:,1::3,:].reshape(winI.shape[1],3), winI[:,2::3,:].reshape(winI.shape[1],3)))
    return res  
</code></pre>

<p>so far so god, say we had <code>winSize = 1, i = 1, j = 1</code> and got a 9x3 matrix as a result: this matrix : </p>

<pre><code>&gt;&gt; subWin = clipSubwindowFromImage(background12x12b, 1, 1, 1)
&gt;&gt; [[201 199 187]
 [216 219 198]
 [226 228 207]
 [243 241 228]
 [240 244 221]
 [233 235 213]
 [239 238 220]
 [238 240 216]
 [233 235 211]]
</code></pre>

<p>Then I just want to get the product in question, like this : </p>

<pre><code>&gt;&gt;r1 = subWin.T.dot(subWin)
&gt;&gt;[[197 234  89]
 [234  65 163]
 [ 89 163 105]]
</code></pre>

<p>Well, it's not right, the right result should be : </p>

<pre><code>&gt;&gt;[[477125 479466 438361]
 [479466 481857 440483]
 [438361 440483 402793]]
</code></pre>

<p>But if I initialize <code>subWin</code> manually like this : </p>

<pre><code>&gt;&gt;subWin = np.array([[201, 199, 187], [216, 219, 198], [226, 228, 207], [243, 241, 228], [240, 244, 221], [233, 235, 213],[239, 238, 220], [238, 240, 216],[233, 235, 211]])
</code></pre>

<p>I get right result. </p>

<p>I can't get it, <code>subWin</code> is the SAME array in both cases (I checked it). Any ideas? </p>

<p>As @Aguy said, your problem comes from the data-type of your array. The dot product of a uint8 array with an other uint8 array gives an array that is also uint8 hence the data-type is overflowed in your case. Here's an example that shows the effect of overflow on your values:</p>

<pre><code>import numpy as np

a = np.array([[201, 199, 187], [216, 219, 198], [226, 228, 207], [243, 241, 228], [240, 244, 221], [233, 235, 213],[239, 238, 220], [238, 240, 216],[233, 235, 211]]) 
b = a.T.dot(a)

print b.dtype
print b
print "overflowed uint8 :"
print b.astype(np.uint8)
</code></pre>

<p>Gives:</p>

<pre><code>&gt;&gt;&gt; int64
&gt;&gt;&gt; [[477125 479466 438361]
&gt;&gt;&gt;  [479466 481857 440483]
&gt;&gt;&gt;  [438361 440483 402793]]
&gt;&gt;&gt; overflowed uint8 :
&gt;&gt;&gt; [[197 234  89]
&gt;&gt;&gt;  [234  65 163]
&gt;&gt;&gt;  [ 89 163 105]]
</code></pre>

<p>Just change the data-type of one array to something more suitable in your dot product and you're good to go : </p>

<pre><code>r1 = subWin.T.dot(subWin.astype(np.uint32))
</code></pre>

python arrays numpy