I have the following simple Python function:
def get_lerp_factor( a, x, b ):
if x <= a: return 0.
if x >= b: return 1.
return (x - a) / (b - a)
Many numpy functions, like numpy.sin(x) can handle a float or an array.
So how can I extend this in the same manner, so that it can also handle a numpy array for x?
def get_lerp_factor( a, x_maybe_array, b ):
out = (x_maybe_array - a) / (b - a) # this should work...
# but now I have to clamp each element of out between 0 and 1
Would I have to specifically check the type of x, and branch accordingly?
How about:
def get_lerp_factor( a, x_anything, b ):
x = np.array( x_anything )
out = ...(x)
# now typecast out back into the same type as x... will this work?
?
mapor a list comprehension to deal with lists and arrays?