I am fairly new to programming and I'd try writing a linear-interpolation function :
from bisect import bisect_left
def interpolate((x_list, y_list), x_test):
if any(y - x <= 0 for x, y in zip(x_list, x_list[1:])):
raise ValueError("x_list must be in strictly ascending order!")
x_list = x_list = map(float, x_list)
y_list = y_list = map(float, y_list)
intervals = zip(x_list, x_list[1:], y_list, y_list[1:])
slopes = [(y2 - y1)/(x2 - x1) for x1, x2, y1, y2 in intervals]
i = bisect_left(x_list, x_test) - 1
return y_list[i] + slopes[i] * (x_test - x_list[i])
i=interpolate(((2, 3, 6), (1,1.5,3)),5)
print i
now i want to make a new function like this(dummy function) :
def interpolate(data, xtest):
#statements...
return numpy.interp(xtest, [x for (x,y) in data], [y for (x,y) in data])
given data as follows:
data = ( (2, 1), (3, 1.5), (6, 3) )
interpolate(data, 4)
O/P : 2
interpolate(data, 5)
O/P : 2.5
How can i make a tuple (i.e data = ( (2, 1), (3, 1.5), (6, 3) )) and clean way to iterate over that tuple.