Skip to content

MAINT: Adjust type promotion in linalg.norm #10390

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

Merged
merged 2 commits into from
Jan 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/release/1.14.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ functions, and if used would likely correspond to a typo.
Previously, this would promote to ``float64`` when arbitrary orders were
passed, despite not doing so under the simple cases::

>>> f32 = np.float32([1, 2])
>>> np.linalg.norm(f32, 2.0).dtype
>>> f32 = np.float32([[1, 2]])
>>> np.linalg.norm(f32, 2.0, axis=-1).dtype
dtype('float32')
>>> np.linalg.norm(f32, 2.0001).dtype
>>> np.linalg.norm(f32, 2.0001, axis=-1).dtype
dtype('float64') # numpy 1.13
dtype('float32') # numpy 1.14

Expand Down
6 changes: 4 additions & 2 deletions numpy/linalg/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2277,7 +2277,7 @@ def norm(x, ord=None, axis=None, keepdims=False):
return abs(x).min(axis=axis, keepdims=keepdims)
elif ord == 0:
# Zero norm
return (x != 0).astype(float).sum(axis=axis, keepdims=keepdims)
return (x != 0).astype(x.real.dtype).sum(axis=axis, keepdims=keepdims)
elif ord == 1:
# special case for speedup
return add.reduce(abs(x), axis=axis, keepdims=keepdims)
Expand All @@ -2292,7 +2292,9 @@ def norm(x, ord=None, axis=None, keepdims=False):
raise ValueError("Invalid norm order for vectors.")
absx = abs(x)
absx **= ord
return add.reduce(absx, axis=axis, keepdims=keepdims) ** (1.0 / ord)
ret = add.reduce(absx, axis=axis, keepdims=keepdims)
ret **= (1 / ord)
return ret
elif len(axis) == 2:
row_axis, col_axis = axis
row_axis = normalize_axis_index(row_axis, nd)
Expand Down