v923z / micropython-ulab

a numpy-like fast vector module for micropython, circuitpython, and their derivatives
https://micropython-ulab.readthedocs.io/en/latest
MIT License
430 stars 117 forks source link

It would be nice if invert supported boolean types #487

Closed assumptionsoup closed 2 years ago

assumptionsoup commented 2 years ago

In numpy, this is valid:

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
mask = arr > 5
arr[mask] *= 2
arr[~mask] *= 3

Which results in:

arr:   array([ 3,  6,  9, 12, 15, 12, 14, 16, 18, 20])
mask:  array([False, False, False, False, False,  True,  True,  True,  True, True])
~mask: array([ True,  True,  True,  True,  True, False, False, False, False, False])

As of version 3.3.8-2D, ulab doesn't support the invert function on boolean types. Instead it does this:

>>> ~mask
array([255, 255, 255, 255, 255, 254, 254, 254, 254, 254], dtype=float32)

Which, of course, you can't index with.

>>> arr[~mask]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NotImplementedError: operation is implemented for 1D Boolean arrays only

It would be nice if ulab had the same behavior as numpy :)

v923z commented 2 years ago

@assumptionsoup Thanks for reporting the issue! This is indeed an unintended glitch. I know where the problem is, and will roll out a fix later today.

v923z commented 2 years ago

@assumptionsoup Jordan, could you, please, check out https://github.com/v923z/micropython-ulab/pull/488, and see if you still have difficulties?