1

I basically want to write a vectorized function in Numpy that will element-wise compare the binary representations and the data types of involved arrays.

For example,

  • f(np.nan, np.nan) should be True.
  • f(np.datetime64('NaT'), np.nan) should be False.
  • f(np.datetime64('NaT'), np.datetime64('NaT')) should be True.
  • f(np.NZERO, np.PZERO) should be True on platforms that have an identical binary representation for both but False otherwise.
  • Any other weird exceptions (?)

Also, the function should be vectorized, fast (C speed) and at least "look like" a ufunc in the sense that it should support broadcasting and stringing over arrays.

I've tried this, but it doesn't work for NaT, etc: Comparing NumPy arrays so that NaNs compare equal

The following require Pandas, which I don't want to do, and they'll make NaN == NaT. Numpy: Checking if a value is NaT

I could add np.isnat to problem 1, but that wouldn't compare the precision of the object.

And none of these can do the NZERO/PZERO thing.

Hameer Abbasi
  • 1,292
  • 1
  • 12
  • 34
  • 1
    SO is not a code writing service. Show us what you've tried and ask *specific* questions about single problems that you're having. – Paul H Apr 23 '18 at 00:49
  • @PaulH I edited to show what problems I've had with existing solutions. – Hameer Abbasi Apr 23 '18 at 00:55
  • Are you aware that there's more than one NaN representation? There are like 9 million billion of them. What should happen if you're comparing two different NaNs? – user2357112 Apr 23 '18 at 01:19
  • @user2357112 I wasn't aware, but thanks for alerting me to it. In any case, like I said, I only want the _binary representation_. – Hameer Abbasi Apr 23 '18 at 01:21

1 Answers1

1

It seems comparing the underlying view does exactly what I want:

def compare(x, y):
    x, y = np.broadcast_arrays(x, y)
    dtx = x.dtype
    dty = y.dtype
    if dtx != dty:
        return np.zeros(x.shape, dtype=bool)
    xv = x.view((np.uint8, x.itemsize))
    yv = y.view((np.uint8, y.itemsize))
    return np.all(xv == yv, axis=-1)
Hameer Abbasi
  • 1,292
  • 1
  • 12
  • 34