Let's say I have a 0d Numpy array:
x = numpy.asarray(1.)
This has type numpy.ndarray. But now operate on x:
y = 1.*x
This now has type numpy.float64. The same thing is true when using all Numpy functions. For example, numpy.sin(x) has type numpy.float64.
Why?!
Now let's say I have a function and in the course of operation that function has to promote the dimension of the array (to perform some check or other):
def fun(x):
x = np.asarray(x)
if x.ndim == 0:
x = x[None]
# Perform some checks and operate on x
return x.squeeze()
To be consistent with Numpy I would like this function to return a numpy.float64 when a float is passed to it. But as it stands it returns a 0d array. I could instead return 1.*x.squeeze() but that seems very ugly. Is there a canonical solution to this problem?
x(to perform some check):x = x[None]. You can squeeze back down to get a 0d array:x.squeeze(). But I would like to return anumpy.float64if a float is passed to the function. I can use a flag and then do1.*x.squeeze()but it seems ugly. Happy to update the question if you don't think I'm muddying the waters.