8000 BUG: "Non-standard" vector-norms of 1-D arrays in norm() always return float64 dtype · Issue #21083 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
BUG: "Non-standard" vector-norms of 1-D arrays in norm() always return float64 dtype #21083
Closed
@asmeurer

Description

@asmeurer

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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0