Created
April 4, 2012 15:16
-
-
Save jfrelinger/2302548 to your computer and use it in GitHub Desktop.
an linear interpolating look-up table in python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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