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

[FEATURE REQUEST] support reshape(-1) for ndarrays #608

Closed Derfies closed 1 year ago

Derfies commented 1 year ago

Super handy to be able to flatten an array to 1 dimension in vanilla numpy, eg:

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.reshape(-1))
-> [1 2 3 4 5 6]

whereas this raises TypeError: shape must be a tuple in ulab.

Using a tuple, ie arr.reshape((-1,)) gives the same issue unfortunately.

Derfies commented 1 year ago

Ah. After a little more perusing I take it that .flatten is the way to go here?

v923z commented 1 year ago

If you want, we could add .reshape(-1) as an alias for .flatten. Are they really the same?

Derfies commented 1 year ago

I've used numpy a fair bit but I don't consider myself an authority on the matter. Perhaps leave it as is rather than confused the next boffin who knows the nuance between these two methods!

v923z commented 1 year ago

One element in the tuple can actually be -1: https://numpy.org/doc/stable/reference/generated/numpy.reshape.html

v923z commented 1 year ago

@Derfies numpy is not quite pedantic, and while the documentation states that the unknown dimension length should be -1, it is actually not checked. In any case, https://github.com/v923z/micropython-ulab/pull/612 implements this feature.

Derfies commented 1 year ago

Thank you!!