Skip to content

ENH: add new function replace - #1072

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

willow-ahrens wants to merge 4 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

@lucascolley lucascolley left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @willow-ahrens ! Just one question on the docs.

Comment on lines +324 to +326
For sparse arrays, elements which are not explicitly stored are replaced too:
if `old` equals the fill value, the fill value of the result is `new`.
The result keeps the sparse format of `x` and is never densified.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: this is specific to namespaces satisfying the _compat.is_pydata_sparse_namespace predicate right now, right?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is part of a bigger question. It's my intention for sparse arrays to have the same semantic meaning as dense arrays (So that we don't need to store extra data that specifies stored/not stored). These three lines are meant to clarify the implication of matching dense behavior: apply all ops to the fill value too. Because this function is a bit of a signal to sparse frameworks that they should change the fill value, I meant to add this to make that clear. So in that sense, it's not pydata/sparse specific, Finch also should care about it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, I think the tone of the comment should just be more 'sparse array libraries can and should implement this as a change of fill value, without densification', rather than 'as a promise to array-api-extra users, all sparse arrays will avoid densification'.

I know you didn't intend the latter, but I think it could be read that way, while there is nothing stopping a sparse library from having an implementation of xp.where that in fact densifies, right? In which case, this function would densify too (apart from the special-cased pydata sparse).

Does that make sense?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that might be helpful is to have two separate paragraphs:

  • what happens for generic libraries (that might be sparse, but otherwise implemented in any way)
  • what happens for known sparse libraries (i.e. what is special-cased, or what behaviour do we get given assumptions about how the sparse library implements other functions)

@lucascolley lucascolley changed the title replace ENH: add new function replace Nov 18, 2026
@lucascolley lucascolley added this to the 0.12.1 milestone Nov 18, 2026

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

2 participants