Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: add nan_to_num to the array API spec #878

Open
ogrisel opened this issue Dec 26, 2024 · 1 comment
Open

RFC: add nan_to_num to the array API spec #878

ogrisel opened this issue Dec 26, 2024 · 1 comment
Labels
API extension Adds new functions or objects to the API. RFC Request for comments. Feature requests and proposed changes.

Comments

@ogrisel
Copy link

ogrisel commented Dec 26, 2024

numpy.nan_to_num has not been included in the array API spec so far.

However, it seems widely adopted by array libraries:

At the time of writing, ndonnx does not implement this function.

I haven't checked in details, but from a cursory glance at the docs, libraries that implement this utility do seem quite consistent already.

@kgryte kgryte added RFC Request for comments. Feature requests and proposed changes. API extension Adds new functions or objects to the API. labels Dec 26, 2024
@rgommers
Copy link
Member

I think nan_to_num wasn't considered previously because it seems a bit niche and simply wasn't on anyone's radar. The function is also pretty ugly:

def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None):

The copy is unnecessary, and the posinf/neginf seems rarely used. I'd also make the fill value mandatory to specify. If we'd implement this with array API standard primitives, I think it'd be something like:

def nan_to_num(x : array, /, *, fill_value: float | complex) -> array:
    if xp.isdtype(x.dtype, ('real floating', 'complex floating')):
        return xp.where(xp.isnan(x), x, fill_value)
    else:
        return x

Maybe it makes more sense to put that into array-api-extra?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API extension Adds new functions or objects to the API. RFC Request for comments. Feature requests and proposed changes.
Projects
None yet
Development

No branches or pull requests

3 participants