v923z / micropython-ulab

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

[FEATURE REQUEST] Implement `unpackbits()` #690

Open page200 opened 6 days ago

page200 commented 6 days ago

Are there plans to implement unpackbits()?

And what are good practices for getting the same result fast while unpackbits() is not implemented?

I'd like to use the resulting bits as Boolean indices into a constant integer array X or do np.nditer(np.nonzero(X)) or whatever is fastest for achieving the same effect.

page200 commented 6 days ago

Here is unpack_bits() in numpy/numpy/_core/src/multiarray/compiled_base.c

v923z commented 6 days ago

Ha! I actually thought of this a while ago, but then I didn't see any immediate use, so I didn't pursue the idea. If you think it's useful, we could definitely add this.

page200 commented 6 days ago

It would be useful.

Maybe we can even forgo the overhead of the parameter checks that NumPy does, or at least do "memoization" so that those checks are performed only once instead of in each call?

v923z commented 6 days ago

Maybe we can even forgo the overhead of the parameter checks that NumPy does, or at least do "memoization" so that those checks are performed only once instead of in each call?

That would break numpy-compatibility. If you're concerned about the keyword arguments, then we should do this in utils and not as a standard numpy function. We could simply define a class, and then unpack_bits could be a method of that class. That would save the time of the parameter checks.