question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: Let searchsorted work on arrays with descending values

See original GitHub issue

Quite often arrays are sorted, either ascending or descending, and we want to find where the startpoint of a certain value is at. This is very easy for ascending arrays using searchsorted, but requires a bit more understanding for descending arrays.

It seems relatively straightward to it make it easy for descending arrays also by adding a ascending parameter to searchsorted.

Would this be outside of scope for numpy, or is such an ascending parameter relevant to include in numpy’s searchsorted?

A demonstration:

>>> import numpy as np

>>> def searchsorted(a, v, side='left', sorter=None, ascending=True):
>>>     if not ascending:
>>>         a = a[::-1]
>>>         side = {'left': 'right', 'right': 'left'}[side]
>>>         sorter = sorter[::-1] if sorter is not None else sorter
>>>         return len(a) - np.searchsorted(a, v, side=side, sorter=sorter)
>>>     return np.searchsorted(a, v, side=side, sorter=sorter)

>>> a = np.array([2, 1, 1])
>>> searchsorted(a, 1, ascending=False)
1
>>> searchsorted(a, 2, side='right', ascending=False)
1
``

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
eric-wiesercommented, Aug 12, 2018

Perhaps reverse=True to match the builtin sorted.

I’m +1 on adding an argument to specify explicitly, but -1 on trying to auto-detect like np.digitize does.

0reactions
sebergcommented, May 17, 2019

That probably depends on the solution. If just reversing the array and using the normal searchsorted code is enough, you can do that inside array_searchsorted. If it is not a good solution, you would need to modify PyArray_SearchSorted, but rename the new function so the function signature of PyArray_SearchSorted is not modified (the original would become just 1 line of code calling the new function).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Numpy searchsorted descending order - Stack Overflow
Is searchsorted usable for arrays ordered in descending order? What options do I have if I need to keep the array in descending...
Read more >
Working of the NumPy searchsorted() Function - eduCBA
The NumPy searchsorted() function can be used to find the index value where the new elements should be inserted in the sorted array...
Read more >
numpy.searchsorted — NumPy v1.24 Manual
If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices that sort it....
Read more >
JavaScript Array Sort - W3Schools
You can use it to sort an array in descending order: ... By default, the sort() function sorts values as strings. This works...
Read more >
numpy.searchsorted() - JAX documentation - Read the Docs
If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices that sort it....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found