Skip to content

BUG: "Non-standard" vector-norms of 1-D arrays in norm() always return float64 dtype #21083

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

Closed
asmeurer opened this issue Feb 18, 2022 · 1 comment · Fixed by #21084
Closed
Labels

Comments

@asmeurer
Copy link
Member

Describe the issue:

The "non-standard" norms in norm(), i.e., ord < 0 or ord > 2 on a 1-d array (so it's a vector norm and not a matrix norm), incorrectly promote the result to float64 regardless of the input dtype. This is because the code just manually implements it with

absx = abs(x)
absx **= ord
ret = add.reduce(absx, axis=axis, keepdims=keepdims)
ret **= (1 / ord)
return ret

at

absx = abs(x)
.

Specifically, the last line ret **= (1 / ord) promotes the result to float64 because ret is a scalar and 1/ord is a Python float.

Reproduce the code example:

>>> np.linalg.norm(np.asarray([1., 2.], dtype=np.float32), ord=-1).dtype
dtype('float64')
>>> np.linalg.norm(np.asarray([1., 2.], dtype=np.float16), ord=-1).dtype
dtype('float64')
>>> np.linalg.norm(np.asarray([1., 2.], dtype=np.float32), ord=1).dtype
dtype('float32')
>>> np.linalg.norm(np.asarray([1., 2.], dtype=np.float16), ord=1).dtype
dtype('float16')

Error message:

No response

NumPy/Python version information:

np.version
'1.23.0.dev0+782.g1168868df6'

@asmeurer
Copy link
Member Author

I have a fix for this, but I'm a little confused about what I should add to the tests. Specifically I'm not clear in the current tests why this isn't being tested already.

asmeurer added a commit to asmeurer/numpy that referenced this issue Feb 18, 2022
Previously it would always give float64 because an internal calculation
involved a NumPy scalar and a Python float. The fix is to use a 0-D array
instead of a NumPy scalar so that it type promotes with the float correctly.

Fixes numpy#21083

I don't have a test for this yet because I'm unclear how exactly to test it.
seberg pushed a commit that referenced this issue Jun 16, 2022
* Fix computation of numpy.array_api.linalg.vector_norm

Various pieces were incorrect due to a lack of complete coverage of this
function in the array API test suite.

* Fix the output dtype nonstandard vector norm()

Previously it would always give float64 because an internal calculation
involved a NumPy scalar and a Python float. The fix is to use a 0-D array
instead of a NumPy scalar so that it type promotes with the float correctly.

Fixes #21083

I don't have a test for this yet because I'm unclear how exactly to test it.

* Clean up the numpy.array_api.linalg.vector_norm code a little bit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant