Skip to content

replace - #1072

Open
willow-ahrens wants to merge 2 commits into
data-apis:mainfrom
willow-ahrens:wma/replace
Open

replace#1072
willow-ahrens wants to merge 2 commits into
data-apis:mainfrom
willow-ahrens:wma/replace

Conversation

@willow-ahrens

@willow-ahrens willow-ahrens commented Oct 29, 2026 •

Copy link
Copy Markdown

fixes #992

adds a function xp.replace(array, old, new) which replaces value old with value new in array. This function is helpful in sparse kernels. for example, after loading a graph matrix, one may wish to replace the zero value with infinity if they wish to then implement a shortest-paths operation: https://github.com/SparseArrayProgrammingSuite/SparseArrayProgrammingSuite/blob/33b2949b4c0e960df8818e94406ed5bd970cc76c/src/saps/util/adjacency.py#L63.

The operation sparse.where(A != old, A, np.inf) needs to allocate a boolean mask to perform this operation, but a fused replace would be a good moment to signal to sparse arrays that they should change fill values explicitly, and could give an opportunity to do so in O(1) time if the explicitly stored entries are already known not to be fill.

This can also be a good way to fix exceptional values, like NaN -> 0 versus NaN -> Inf
https://github.com/SparseArrayProgrammingSuite/SparseArrayProgrammingSuite/blob/33b2949b4c0e960df8818e94406ed5bd970cc76c/src/saps/benchmarks/pcg.py#L1920

This branch has not been deployed

No deployments
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.

ENH: new function replace

1 participant