thouis / numpy-trac-migration

numpy Trac to github issues migration
2 stars 3 forks source link

unravel_index(0, ()) should return () (Trac #2120) #5916

Open numpy-gitbot opened 12 years ago

numpy-gitbot commented 12 years ago

Original ticket http://projects.scipy.org/numpy/ticket/2120 on 2012-04-29 by atmention:njsmith, assigned to unknown.

Currently, this idiom works for all arrays that have ndim >= 1:

for i in xrange(np.prod(a.shape)):
  idx = np.unravel_index(i, a.shape)
  a[idx] = ...

(Obviously there are usually better ways to do this, but anyway.)

But, it fails for 0-d arrays:

>>> np.unravel_index(0, ())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: dims must have at least one value

This should just return (), which is the unique index that can be used with 0-d arrays.