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

Add support for computing the hypotenuse via hypot #544

Closed
kgryte opened this issue Dec 1, 2022 · 5 comments · Fixed by #703
Closed

Add support for computing the hypotenuse via hypot #544

kgryte opened this issue Dec 1, 2022 · 5 comments · Fixed by #703
Assignees
Labels
API extension Adds new functions or objects to the API.
Milestone

Comments

@kgryte
Copy link
Contributor

kgryte commented Dec 1, 2022

This RFC requests to include a new API in the array API specification for the purpose of computing the hypotenuse.

Based on array comparison data, the API is available across all major array libraries in the PyData ecosystem (note: TensorFlow implements hypot in its experimental numpy namespace).

While hypot can be implemented in userland using existing array API functions (e.g., sqrt(x1*x1 + x2*x2)), this function is typically implemented in a specialized manner to help prevent over/underflow.

Furthermore, need for this API arose when adding complex number support to the array API specification, particularly with regard to adding complex number support to abs, where the absolute value (magnitude) of a complex number is computed as hypot(real(z), imag(z)).

As this API already exists in the ecosystem and satisfies needs (e.g., numerical stability and complex number support), this API seems a good candidate for inclusion in the standard.

@kgryte kgryte added the API extension Adds new functions or objects to the API. label Dec 1, 2022
@rgommers
Copy link
Member

rgommers commented Dec 1, 2022

This seems like a reasonable addition, given it's already commonly implemented (the linked data shows it is named differently in MXNet though). Are the signatures consistent among libraries?

Furthermore, need for this API arose when adding complex number support to the array API specification

That does not seem directly relevant here. For abs to use a hypot calculation internally, hypot does not have to be exposed as public API.

@kgryte
Copy link
Contributor Author

kgryte commented Dec 1, 2022

That does not seem directly relevant here.

It does in order for the specification to be able to define special case behavior.

@kgryte
Copy link
Contributor Author

kgryte commented Dec 1, 2022

Concerning special case behavior, according to IEEE 754, hypot handles NaN differently than would be the case for the naive implementation. Namely, if only one of x or y in hypot(x,y) is NaN and the other is infinity, the result is infinity. This also applies to abs when computing the absolute value of a complex number (see C99).

Wrt complex number support, while we could explicitly spell out complex number special case behavior for abs, the end result is that all libraries would effectively have to implement IEEE 754 hypot.

@kgryte
Copy link
Contributor Author

kgryte commented Dec 1, 2022

In terms of API, all supporting libraries implement as a standard element-wise binary function: hypot(x1, x2), where x1 and x2 follow conventional broadcast rules.

@rgommers rgommers changed the title RFC: add support for computing the hypotenuse via hypot Add support for computing the hypotenuse via hypot Dec 5, 2022
@rgommers
Copy link
Member

rgommers commented Dec 5, 2022

It does in order for the specification to be able to define special case behavior.

As discussed last Thursday, the special cases for abs can be put in the abs description. This is implemented in gh-546.

We're already nearing the end of 2022, and are close to finalizing the 2022 standard. hypot is not urgent enough to squeeze it in now, so let's defer deciding on that till later. There's a larger queue of suggested new APIs that needs considering in Q1.

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.
Projects
Status: Stage 2
Development

Successfully merging a pull request may close this issue.

2 participants