Skip to content

Add exponential and vectorized search functions from Interpolations.jl#42

Merged
ChrisRackauckas merged 2 commits into
SciML:mainfrom
ChrisRackauckas-Claude:feature-interpolations-search-functions
Dec 29, 2025
Merged

Add exponential and vectorized search functions from Interpolations.jl#42
ChrisRackauckas merged 2 commits into
SciML:mainfrom
ChrisRackauckas-Claude:feature-interpolations-search-functions

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Contributor

Summary

This PR addresses issue #8 by implementing search functions inspired by Interpolations.jl's specialized index finding routines.

New Functions Added:

  • searchsortedfirstexp(v, x, lo, hi): An exponential search followed by binary search, optimized for correlated sequential lookups where the target is expected to be near the starting position lo. This is similar to Interpolations.jl's searchsortedfirst_exp_left.

  • searchsortedfirstvec(v, x): Batch search for multiple sorted values. When the input vector x is sorted, this leverages monotonicity to avoid redundant searching by reusing the previous search result as the starting point.

  • searchsortedlastvec(v, x): Similar to searchsortedfirstvec but returns the last index where v[i] <= x[i].

How it works:

The exponential search starts with a linear scan of the first 5 elements (for cache efficiency), then switches to exponential doubling to quickly narrow the search range before falling back to binary search. This provides O(log k) complexity where k is the distance from lo to the target, instead of O(log n) for full binary search.

The vectorized search functions check if x is sorted and, if so, maintain a monotonically advancing lower bound for subsequent searches. This significantly reduces comparisons when searching for multiple values that are already ordered.

Test plan

  • All existing tests pass
  • Added tests for searchsortedfirstexp covering basic functionality, edge cases, float vectors, empty/small vectors, repeated elements, and large vectors
  • Added tests for searchsortedfirstvec and searchsortedlastvec comparing against element-wise searchsortedfirst/searchsortedlast
  • Tests verify correct behavior for sorted and unsorted input vectors
  • Tests cover edge cases (values outside range, empty input, single element)

Fixes #8

cc @ChrisRackauckas

🤖 Generated with Claude Code

Implements search functions inspired by Interpolations.jl (issue SciML#8):

- searchsortedfirstexp: Exponential search followed by binary search,
  efficient for correlated sequential lookups where the target is
  expected to be near the starting position.

- searchsortedfirstvec: Batch search for multiple sorted values,
  leveraging monotonicity to avoid redundant searching.

- searchsortedlastvec: Similar to searchsortedfirstvec but returns
  the last index where v[i] <= x.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@ChrisRackauckas ChrisRackauckas merged commit c3fc485 into SciML:main Dec 29, 2025
13 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compare to Interpolations.jl search functions

2 participants