Skip to content

Instantly share code, notes, and snippets.

@jfrelinger
Created April 4, 2012 15:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfrelinger/2302548 to your computer and use it in GitHub Desktop.
Save jfrelinger/2302548 to your computer and use it in GitHub Desktop.
an linear interpolating look-up table in python.
import bisect
class InterpTable(object):
'''
a look up table that linearly interperlates between values
'''
def __init__(self, x, y):
self.x = x
self.y = y
def __getitem__(self, v):
j = bisect.bisect_left(self.x, v)
i = j-1
if i < 0 :
return self.y[0]
if j >= len(self.x) :
return self.y[ -1 ]
return self.y[i] + (v-self.x[i])*(self.y[j]-self.y[i])/(self.x[j]-self.x[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment