1
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

Sristi
  • 11
  • 2
  • You are not storing it into the 'o' you first defined. `matmul` takes an `out` argument, but even `np.matmul(x1,x2, out=o)` does not avoid this overflow. – hpaulj Dec 15 '22 at 19:52

1 Answers1

0

np.matmul(x1,x2) returns an int8 temporary array with overflows. The result is then assigned to o 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 to o by reference. o = np.zeros((63,63), dtype=np.int16) is thus useless because of that. If you want a copy, you need to use o[:,:] = ... but this is not a good idea to do a copy here (this is just slower).

Jérôme Richard
  • 41,678
  • 6
  • 29
  • 59