The following code returns an array instead of expected float value.
def f(x):
return x+1
f = np.vectorize(f, otypes=[np.float])
>>> f(10.5)
array(11.5)
Is there a way to force it return simple scalar value if the input is scalar and not the weird array type?
I find it weird it doesn't do it by default given that all other ufuncs like np.cos, np.sin etc do return regular scalars
Edit: This the the code that works:
import numpy as np
import functools
def as_scalar_if_possible(func):
@functools.wraps(func) #this is here just to preserve signature
def wrapper(*args, **kwargs):
return func(*args, **kwargs)[()]
return wrapper
@as_scalar_if_possible
@np.vectorize
def f(x):
return x + 1
print(f(11.5)) # prints 12.5