import numpy as np
x1 = np.arange(0,63, dtype=np.int8).reshape(63,1)
x2 = np.arange(0,63, dtype=np.int8).reshape(1,63)
o = np.zeros((63,63), dtype=np.int16)
o = np.matmul(x1,x2)
print(o)
Output:
[[ 0 0 0 ... 0 0 0]
[ 0 1 2 ... 60 61 62]
[ 0 2 4 ... 120 122 124]
...
[ 0 60 120 ... 16 76 -120]
[ 0 61 122 ... 76 -119 -58]
[ 0 62 124 ... -120 -58 4]]
The value of x1 and x2 are within the range of np.int8 after np.matmul operation the value is above the int8 range so I am storing it into int16, still I am getting incorrect values. Can someone please explain me why is this happening? Thanks
np.matmul(x1,x2)
returns anint8
temporary array with overflows. The result is then assigned too
but this is too late: the overflow is already done. You should adapt the type before the operation:o = x1.astype(np.int16) @ x2.astype(np.int16)
.Note
o = ...
does not perform a copy but assign the right hand side too
by reference.o = np.zeros((63,63), dtype=np.int16)
is thus useless because of that. If you want a copy, you need to useo[:,:] = ...
but this is not a good idea to do a copy here (this is just slower).