Closed
Description
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
Line 2559 in 1168868
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'