rougier / from-python-to-numpy

An open-access book on numpy vectorization techniques, Nicolas P. Rougier, 2017
http://www.labri.fr/perso/nrougier/from-python-to-numpy
Other
2.03k stars 339 forks source link

Error in the find_index functon #107

Open fat-crocodile opened 1 year ago

fat-crocodile commented 1 year ago

Hi!

Z = base[6:2:-1, 5::-1]
find_index(base, Z)

produces an exception

TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

The problem is here, in line 43 https://github.com/rougier/from-python-to-numpy/blob/eb21651fc84d132414603e5a16d93f54ef45ec99/code/find_index.py#L41-L45

We've checked that stop is not None, but step is less than zero and we didn't check start, BOOM!

I suggest code like this:

        if step is not None:
            if step < 0:
                start, stop = stop, start
            if stop is not None:
                stop += np.sign(step)
rougier commented 1 year ago

Sorry for late reply. You mean your fix solve the problem ? Could you make a PR?