Description
This RFC proposes to add a new API, abs2
, to the standard for computing the square of the absolute value.
For complex numbers, the absolute value is the Euclidean norm of the real and imaginary components.
This API has particular value for complex numbers, as the alternative abs(x)**2
requires taking the square root and the alternative x*conj(x)
potentially requires additional memory allocation before computation.
Background
This was originally discussed in the Array API issue tracker in gh-153 (comment) and included in RFC gh-373 for adding complex number support to the specification.
Prior to that, a proposal was made on the NumPy issue tracker (numpy/numpy#13179) to add abs2
in response to an issue originally filed on NumPy in 2013.
The abs2
proposal was given additional life in late 2021 with an effort to add to SciPy under an abs_sq
alias.
Part of the motivation for adding to SciPy is due to NumPy's desire to avoid expanding its API. This desire stems from not having a clear process or criteria for determining what should and what should not be included in NumPy.
However, the array API standard provides NumPy with such a process, as decision making for new APIs is effectively punted to this standards body.
Accordingly, were the array API specification to add abs2
, NumPy would follow suit.
This brings me to this proposal which seeks to add abs2
to the array API standard and effectively settle the question as to where abs2
should live.
And as abs2
has general applicably outside of NumPy and in other array libraries, abs2
seems like a reasonable and straightforward API to add to the standard.
Proposal
The API for abs2
would follow similar element-wise APIs...
def abs2(x: array, /) -> array
Notes
- This proposal does not follow ENH: add abs_sq() to scipy.special scipy/scipy#15390 in naming the API
abs_sq
, as (1) IMO the increase in characters does not provide any additional clarity in terms of behavior or functionality and (2)abs2
follows precedent elsewhere (Julia and GSL). - For real-valued numbers, the square of the absolute value is the same as the square
x*x
.