Improvement on this function
Gordon James Miller
gmiller at promisemark.com
Wed Sep 17 11:31:11 EDT 2003
Hello all.
I'd be interested in getting some feedback on how to do this linear
interpolation function a little more cleanly. The normal way that this
is taught is to find the set of indices in the x list that bracket the
input value, then use the slope between these points to calculate the y
value for the input value.
I had a version working at one point using !! to access particular
elements but I wasn't sure which one was the better solution.
linterp :: [Double] -> [Double] -> Double -> Double
linterp (x1:x2:xs) (y1:y2:ys) x
| x <= x2 || xs == [] = linterpPair x1 x2 y1 y2 x
| otherwise = linterp (x2:xs) (y2:ys) x
where linterpPair x1 x2 y1 y2 x = x1 + (x - x1) * (y2 - y1) / (x2 -
x1)
Gordon James Miller Promisemark, Inc. Senior Computer Scientist
gmiller at promisemark.com (571)330-9012 Light travels faster than sound.
That is why some people appear bright until you hear them speak.
More information about the Haskell
mailing list