0

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)

Sam333
  • 199
  • 14
  • 3
    I cant reproduce the `-0` results. please provide a code of how you generate th `X` array. My guess is that those are floats of a very small number – Ilya Oct 25 '22 at 15:57
  • This is [possible due to the IEEE-754 standard](https://en.wikipedia.org/wiki/IEEE_754#Formats). Related question: https://stackoverflow.com/questions/48255293/in-ieee-754-why-does-adding-negative-zero-result-in-a-no-op-but-adding-positive . Put it shortly, you should not care much about negative zeros. I wonder if they could be sub-normal value though (strongly impacting performance). – Jérôme Richard Oct 25 '22 at 15:59
  • 1
    @MichaelSzczesny Negative zeros do not exist on nearly all platforms (all mainstream platforms use the C2 representation now enforced by some languages like C++). The only platform supporting that are completely obsolete weird platform that Numpy should not even support. I strongly believe this is not integers that are printed. A MWE is needed so to see that. – Jérôme Richard Oct 25 '22 at 16:02
  • I reproduce this matrix by this a = np.random.rand(4,4) and X = np.eye(a.shape[0]) – Sam333 Oct 25 '22 at 16:02
  • 1
    This is not an integer array but a floating-point one. You should use `randint` for integers. – Jérôme Richard Oct 25 '22 at 16:04
  • @JérômeRichard the a matrix is not integer matrix - never said that, but then the np.eye should return identity vector, and that one should have just integers – Sam333 Oct 25 '22 at 16:30
  • 2
    Question edited – Sam333 Oct 25 '22 at 16:33
  • Note that you can answer your own question by posting an answer. This is actually encouraged on StackOverflow . – Jérôme Richard Oct 25 '22 at 17:00

2 Answers2

1

SOLUTION

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

Sam333
  • 199
  • 14
0

The issue is that the numbers are float types so the '0' is not actually 0 but a very small number so when you multiply the number by -1, you actually multiplying the small number which can be negative. You can read about how floas are stored in memory in this post.

Jérôme Richard
  • 41,678
  • 6
  • 29
  • 59
Ilya
  • 730
  • 4
  • 16
  • 1
    ´np.float` has a representation `np.NZERO == -0.0` – Michael Szczesny Oct 25 '22 at 16:23
  • interesting, thanks a lot! But I thought that np.eye returns an identity matrix which has just integers in diagonal – Sam333 Oct 25 '22 at 16:30
  • 1
    *"the '0' is not actually 0 but a very small number"* This is actually *wrong* in this case. There is no reason for `np.eyes` to fill the matrix with non zeros values outside the diagonal. It would be silly. You can check the value is actually a 0 or -0 using just an exact comparison. – Jérôme Richard Oct 25 '22 at 16:58