Bulba
Bulba

Reputation: 1

How to check for real equality (of numpy arrays) in python?

I have some function in python returning a numpy.array:

matrix = np.array([0.,0.,0.,0.,0.,0.,1.,1.,1.,0.],
             [0.,0.,0.,1.,1.,0.,0.,1.,0.,0.])

def some_function:
    rows1, cols1 = numpy.nonzero(matrix)
    cols2 = numpy.array([6,7,8,3,4,7])
    rows2 = numpy.array([0,0,0,1,1,1])
    print numpy.array_equal(rows1, rows2) # returns True
    print numpy.array_equal(cols1, cols2) # returns True
    return (rows1, cols1)                   # or (rows2, cols2)

It should normally extract the indices of nonzero entries of a matrix (rows1, cols1). However, I can also extract the indices manually (rows2, cols2). The problem is that the program returns different results depending on whether the function returns (rows1, cols1) or (rows2, cols2), although the arrays should be equal.

I should probably add that this code is used in the context of pyipopt, which calls a c++ software package IPOPT. The problem then occurs within this package.

Can it be that the arrays are not "completely" equal? I would say that they somehow must be because I am not modifying anything but returning one instead of the other.

Any idea on how to debug this problem?

Upvotes: 0

Views: 158

Answers (1)

Émilien Tlapale
Émilien Tlapale

Reputation: 903

You could check where the arrays are not equal:

print(where(rows1 != rows2))

But what you are doing is unclear, first there is no nonzeros function in numpy, only a nonzero which returns a tuple of coordinates. Are you only using the one corresponding to the rows?

Upvotes: 1

Related Questions