Skip to content
Advertisement

Numpy matrix multiplication with scalar results in negative zeros

I have a random nxn matrix A with floating points, then I call

X = np.eye(A.shape[0])

matrix nxn which has diagonal values (0 elsewhere)

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

now I want to multiply every even row with -2 so I do

X[0::2] *= -2

however it returns

 2  0  0  0
-0 -2 -0 -0
 0  0  2  0
-0 -0 -0 -2 

Why do the zeroes have negative sign? Does this matter at all and if so is it possible to avoid having that? Thank you

SOLUTION

As someone pointed out, the np.eye function returns float matrix by default, fixed by adding np.eye(A.shape[0],dtype=int)

Advertisement

Answer

SOLUTION

As someone pointed out, the np.eye function returns float matrix by default, fixed by adding np.eye(A.shape[0],dtype=int)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement