3

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.

shashank
  • 1,133
  • 2
  • 10
  • 24

3 Answers3

1

""" made my function """

def interpolate(data, x_text):
"""The interpolate function should return the value of f at the point x_test,as given by a linear interpolation from the sample points. """

data_dict={}        
for item in data:
    data_dict[item[0]] = item[1]

lst_x = data_dict.keys()

lst_x.sort()

"""Now find the co-ordinates of two points by using extrapolation and interpolation condition"""

# Condition 1(extrapolated):when x_text less than least value of lst_x.
if x_text <= lst_x[0]:
    x_0 = lst_x[0]
    x_1 = lst_x[1]
    y_0 = data_dict[lst_x[0]]
    y_1 = data_dict[lst_x[1]]


#Condition 2(extrapolated): When x_text is larger than largest value of lst_x. 
elif x_text >= lst_x[-1]:
    x_0 = lst_x[-2]
    x_1 = lst_x[-1]
    y_0 = data_dict[lst_x[-2]]
    y_1 = data_dict[lst_x[-1]]

#Condition 3(interpolated): When x_text lies between two sample points, or exactly on one of the sample points. 
else:
    for i in range(len(data)-1):
        if x_text >= lst_x[i] and x_text <= lst_x[i+1]:
            x_0 = lst_x[i]
            x_1 = lst_x[i+1]
            y_0 = data_dict[lst_x[i]]
            y_1 = data_dict[lst_x[i+1]]
            break


# Calculation of interpolation point by using the equation. 
y = y_1 + ((y_0-y_1)/float(x_0-x_1))*(x_text - x_1)

return y

"""thanks for help nd support """

shashank
  • 1,133
  • 2
  • 10
  • 24
0

I think your question is self-explanatory. You seem to already have done iteration in the first interpolate function. I assume you want to know how to do basic iteration over a tuple.

Your data tuple looks like this:

>>> data = ((2, 1), (3, 1.5), (6, 3))

To iterate over each tuple in this tuple, do:

>>> for x in data:
...     print x
... 
(2, 1)
(3, 1.5)
(6, 3)

Or, to store each element of the tuple in variables x, y while iterating:

>>> for x, y in data:
...     print x, y
... 
2 1
3 1.5
6 3
DrNightmare
  • 156
  • 1
  • 2
  • 8
0

I guess you're asking more about how to go back and forth between tuple of lists and list of tuples. The built-in function zip achieves both. Here's what I tried in IPython using NumPy's interp function.

from numpy import sin, interp
x = range(10)
y = [ sin(i) for i in x ]
x
y
interp(2.5, x, y)
xy = zip(x,y)
xy
# xy is list of tuples: [ (x[0], y[0]), (x[1], y[1]), ... ]
u = zip(*xy)
u
# u is tuple of tuples: ( (x[0], x[1], ... ), (y[0], y[1], ... ) )
interp(2.5, u[0], u[1])

I had to google to get to this SO question: Transpose/Unzip Function (inverse of zip)?, to know this clever trick of zip(*...) to unzip a list of tuples.

Community
  • 1
  • 1
Rishi
  • 121
  • 4